forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSTimeZone.swift
287 lines (230 loc) · 8.35 KB
/
NSTimeZone.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
@_implementationOnly import CoreFoundation
@_spi(SwiftCorelibsFoundation) @_exported import FoundationEssentials
@available(*, unavailable)
extension NSTimeZone : @unchecked Sendable { }
open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
var _timeZone: TimeZone
init(_timeZone tz: TimeZone) {
_timeZone = tz
}
public convenience init?(name tzName: String) {
self.init(name: tzName, data: nil)
}
public init?(name tzName: String, data aData: Data?) {
/* From https://developer.apple.com/documentation/foundation/nstimezone/1387250-init:
"Discussion
As of macOS 10.6, the underlying implementation of this method has been changed to ignore the specified data parameter."
*/
if let tz = TimeZone(identifier: tzName) {
_timeZone = tz
} else {
return nil
}
super.init()
}
public convenience required init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let name = aDecoder.decodeObject(of: NSString.self, forKey: "NS.name")
let data = aDecoder.decodeObject(of: NSData.self, forKey: "NS.data")
if name == nil {
return nil
}
self.init(name: String._unconditionallyBridgeFromObjectiveC(name), data: data?._swiftObject)
}
open override var hash: Int {
_timeZone.hashValue
}
open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? NSTimeZone else { return false }
return isEqual(to: other._swiftObject)
}
open override var description: String {
_timeZone.description
}
// Time zones created with this never have daylight savings and the
// offset is constant no matter the date; the name and abbreviation
// do NOT follow the POSIX convention (of minutes-west).
public convenience init(forSecondsFromGMT seconds: Int) {
let tz = TimeZone(secondsFromGMT: seconds)!
self.init(_timeZone: tz)
}
public convenience init?(abbreviation: String) {
guard let tz = TimeZone(abbreviation: abbreviation) else {
return nil
}
self.init(_timeZone: tz)
}
open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.name._bridgeToObjectiveC(), forKey:"NS.name")
// Darwin versions of this method can and will encode mutable data, however it is not required for compatibility
aCoder.encode(self.data._bridgeToObjectiveC(), forKey:"NS.data")
}
public static var supportsSecureCoding: Bool {
return true
}
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any {
return self
}
open var name: String {
_timeZone.identifier
}
open var data: Data {
TimeZone._dataFromTZFile(_timeZone.identifier)
}
open func secondsFromGMT(for aDate: Date) -> Int {
_timeZone.secondsFromGMT(for: aDate)
}
open func abbreviation(for aDate: Date) -> String? {
_timeZone.abbreviation(for: aDate)
}
open func isDaylightSavingTime(for aDate: Date) -> Bool {
_timeZone.isDaylightSavingTime(for: aDate)
}
open func daylightSavingTimeOffset(for aDate: Date) -> TimeInterval {
_timeZone.daylightSavingTimeOffset(for: aDate)
}
open func nextDaylightSavingTimeTransition(after aDate: Date) -> Date? {
_timeZone.nextDaylightSavingTimeTransition(after: aDate)
}
open class var system: TimeZone {
TimeZone.current
}
open class func resetSystemTimeZone() {
let _ = TimeZone._resetSystemTimeZone()
// Also reset CF's system time zone
CFTimeZoneResetSystem()
}
open class var `default`: TimeZone {
get {
// This assumes that the behavior of CFTimeZoneCopyDefault is the same as FoundationEssential's default, when no default has been set
TimeZone._default
}
set {
TimeZone._default = newValue
// Need to reset the default in two places since CFTimeZone does not call up to Swift on this platform
CFTimeZoneSetDefault(newValue._cfObject)
}
}
open class var local: TimeZone {
TimeZone.autoupdatingCurrent
}
open class var knownTimeZoneNames: [String] {
TimeZone.knownTimeZoneIdentifiers
}
open class var abbreviationDictionary: [String : String] {
get {
TimeZone.abbreviationDictionary
}
set {
TimeZone.abbreviationDictionary = newValue
}
}
open class var timeZoneDataVersion: String {
TimeZone.timeZoneDataVersion
}
open var secondsFromGMT: Int {
_timeZone.secondsFromGMT()
}
/// The abbreviation for the receiver, such as "EDT" (Eastern Daylight Time). (read-only)
///
/// This invokes `abbreviationForDate:` with the current date as the argument.
open var abbreviation: String? {
_timeZone.abbreviation()
}
open var isDaylightSavingTime: Bool {
_timeZone.isDaylightSavingTime()
}
open var daylightSavingTimeOffset: TimeInterval {
_timeZone.daylightSavingTimeOffset()
}
/*@NSCopying*/ open var nextDaylightSavingTimeTransition: Date? {
_timeZone.nextDaylightSavingTimeTransition
}
open func isEqual(to aTimeZone: TimeZone) -> Bool {
self._timeZone == aTimeZone
}
open func localizedName(_ style: NameStyle, locale: Locale?) -> String? {
_timeZone.localizedName(for: style, locale: locale)
}
}
extension NSTimeZone {
public typealias NameStyle = TimeZone.NameStyle
}
extension NSNotification.Name {
public static let NSSystemTimeZoneDidChange = NSNotification.Name(rawValue: kCFTimeZoneSystemTimeZoneDidChangeNotification._swiftObject)
}
// MARK: - Bridging
extension NSTimeZone: _SwiftBridgeable {
typealias SwiftType = TimeZone
var _swiftObject: TimeZone {
_timeZone
}
var _cfObject : CFTimeZone {
let name = self.name
let tz = CFTimeZoneCreateWithName(nil, name._cfObject, true)!
return tz
}
}
extension CFTimeZone : _SwiftBridgeable, _NSBridgeable {
typealias NSType = NSTimeZone
var _nsObject : NSTimeZone {
let name = CFTimeZoneGetName(self)._swiftObject
let tz = TimeZone(identifier: name)!
return NSTimeZone(_timeZone: tz)
}
var _swiftObject: TimeZone {
return _nsObject._swiftObject
}
}
extension TimeZone : _NSBridgeable {
typealias NSType = NSTimeZone
typealias CFType = CFTimeZone
var _nsObject : NSTimeZone {
return _bridgeToObjectiveC()
}
var _cfObject : CFTimeZone {
_nsObject._cfObject
}
}
extension TimeZone : ReferenceConvertible {
public typealias ReferenceType = NSTimeZone
}
extension TimeZone : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSTimeZone {
NSTimeZone(_timeZone: self)
}
public static func _forceBridgeFromObjectiveC(_ input: NSTimeZone, result: inout TimeZone?) {
if !_conditionallyBridgeFromObjectiveC(input, result: &result) {
fatalError("Unable to bridge \(NSTimeZone.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ input: NSTimeZone, result: inout TimeZone?) -> Bool {
result = input._timeZone
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSTimeZone?) -> TimeZone {
var result: TimeZone? = nil
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
}