宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
//  SwiftDate
//  Parse, validate, manipulate, and display dates, time and timezones in Swift
//
//  Created by Daniele Margutti
//   - Web: https://www.danielemargutti.com
//   - Twitter: https://twitter.com/danielemargutti
//   - Mail: hello@danielemargutti.com
//
//  Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//
 
import Foundation
 
public class DOTNETParser: StringToDateTransformable {
 
    internal static let pattern = "\\/Date\\((-?\\d+)((?:[\\+\\-]\\d+)?)\\)\\/"
 
    public static func parse(_ string: String) -> (seconds: TimeInterval, tz: TimeZone)? {
        do {
            let parser = try NSRegularExpression(pattern: DOTNETParser.pattern, options: .caseInsensitive)
            guard let match = parser.firstMatch(in: string, options: .reportCompletion, range: NSRange(location: 0, length: string.count)) else {
                return nil
            }
 
            guard let milliseconds = TimeInterval((string as NSString).substring(with: match.range(at: 1))) else { return nil }
 
            // Parse timezone
            let raw_tz = ((string as NSString).substring(with: match.range(at: 2)) as NSString)
            guard raw_tz.length > 1 else {
                return nil
            }
            let tz_sign: String = raw_tz.substring(to: 1)
            if tz_sign != "+" && tz_sign != "-" {
                return nil
            }
 
            let tz_hours: String = raw_tz.substring(with: NSRange(location: 1, length: 2))
            let tz_minutes: String = raw_tz.substring(with: NSRange(location: 3, length: 2))
 
            let tz_offset = (Int(tz_hours)! * 60 * 60) + ( Int(tz_minutes)! * 60 )
            guard let tz_obj = TimeZone(secondsFromGMT: tz_offset) else {
                return nil
            }
            return ( (milliseconds / 1000), tz_obj )
        } catch {
            return nil
        }
    }
 
    public static func parse(_ string: String, region: Region?, options: Any?) -> DateInRegion? {
        guard let result = DOTNETParser.parse(string) else { return nil }
        let regionSet = region ?? Region.ISO
        let adaptedRegion = Region(calendar: regionSet.calendar, zone: regionSet.timeZone, locale: regionSet.locale)
        return DateInRegion(seconds: result.seconds, region: adaptedRegion)
    }
 
}
 
public class DOTNETFormatter: DateToStringTrasformable {
 
    public static func format(_ date: DateRepresentable, options: Any?) -> String {
        let milliseconds = (date.date.timeIntervalSince1970 * 1000.0)
        let tzOffsets = (date.region.timeZone.secondsFromGMT(for: date.date) / 3600)
        let formattedStr = String(format: "/Date(%.0f%+03d00)/", milliseconds, tzOffsets)
        return formattedStr
    }
 
}