- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathISO8601DateFormatter.swift
119 lines (83 loc) · 4.98 KB
/
ISO8601DateFormatter.swift
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
import CoreFoundation
extensionISO8601DateFormatter{
publicstructOptions:OptionSet{
publicprivate(set)varrawValue:UInt
publicinit(rawValue:UInt){self.rawValue = rawValue }
publicstaticvarwithYear=ISO8601DateFormatter.Options(rawValue:1 << 0)
publicstaticvarwithMonth=ISO8601DateFormatter.Options(rawValue:1 << 1)
publicstaticvarwithWeekOfYear=ISO8601DateFormatter.Options(rawValue:1 << 2)
publicstaticvarwithDay=ISO8601DateFormatter.Options(rawValue:1 << 4)
publicstaticvarwithTime=ISO8601DateFormatter.Options(rawValue:1 << 5)
publicstaticvarwithTimeZone=ISO8601DateFormatter.Options(rawValue:1 << 6)
publicstaticvarwithSpaceBetweenDateAndTime=ISO8601DateFormatter.Options(rawValue:1 << 7)
publicstaticvarwithDashSeparatorInDate=ISO8601DateFormatter.Options(rawValue:1 << 8)
publicstaticvarwithColonSeparatorInTime=ISO8601DateFormatter.Options(rawValue:1 << 9)
publicstaticvarwithColonSeparatorInTimeZone=ISO8601DateFormatter.Options(rawValue:1 << 10)
publicstaticvarwithFullDate=ISO8601DateFormatter.Options(rawValue: withYear.rawValue + withMonth.rawValue + withDay.rawValue + withDashSeparatorInDate.rawValue)
publicstaticvarwithFullTime=ISO8601DateFormatter.Options(rawValue: withTime.rawValue + withTimeZone.rawValue + withColonSeparatorInTime.rawValue + withColonSeparatorInTimeZone.rawValue)
publicstaticvarwithInternetDateTime=ISO8601DateFormatter.Options(rawValue: withFullDate.rawValue + withFullTime.rawValue)
}
}
openclassISO8601DateFormatter:Formatter,NSSecureCoding{
typealiasCFType=CFDateFormatter
privatevar__cfObject:CFType?
privatevar_cfObject:CFType{
guardlet obj = __cfObject else{
#if os(macOS) || os(iOS)
letformat=CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue)
#else
letformat=CFISO8601DateFormatOptions(self.formatOptions.rawValue)
#endif
letobj=CFDateFormatterCreateISO8601Formatter(kCFAllocatorSystemDefault, format)!
CFDateFormatterSetProperty(obj, kCFDateFormatterTimeZone, timeZone._cfObject)
__cfObject = obj
return obj
}
return obj
}
/* Please note that there can be a significant performance cost when resetting these properties. Resetting each property can result in regenerating the entire CFDateFormatterRef, which can be very expensive. */
openvartimeZone:TimeZone!{ willSet {_reset()}}
openvarformatOptions:ISO8601DateFormatter.Options{ willSet {_reset()}}
publicoverrideinit(){
timeZone =TimeZone(identifier:"GMT")
formatOptions =[.withInternetDateTime,.withDashSeparatorInDate,.withColonSeparatorInTime,.withColonSeparatorInTimeZone]
super.init()
}
publicrequiredinit?(coder aDecoder:NSCoder){NSUnimplemented()}
openoverridefunc encode(with aCoder:NSCoder){NSUnimplemented()}
publicstaticvarsupportsSecureCoding:Bool{returntrue}
openfunc string(from date:Date)->String{
returnCFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, _cfObject, date._cfObject)._swiftObject
}
openfunc date(from string:String)->Date?{
varrange=CFRange(location:0, length: string.length)
letdate=withUnsafeMutablePointer(to:&range){(rangep:UnsafeMutablePointer<CFRange>)->Date?in
guardlet res =CFDateFormatterCreateDateFromString(kCFAllocatorSystemDefault, _cfObject, string._cfObject, rangep)else{
returnnil
}
return res._swiftObject
}
return date
}
openclassfunc string(from date:Date, timeZone:TimeZone, formatOptions:ISO8601DateFormatter.Options=[])->String{
#if os(macOS) || os(iOS)
letformat=CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue)
#else
letformat=CFISO8601DateFormatOptions(formatOptions.rawValue)
#endif
letobj=CFDateFormatterCreateISO8601Formatter(kCFAllocatorSystemDefault, format)
CFDateFormatterSetProperty(obj, kCFDateFormatterTimeZone, timeZone._cfObject)
returnCFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, obj, date._cfObject)._swiftObject
}
privatefunc _reset(){
__cfObject =nil
}
}