Skip to content

Commit d67a289

Browse files
Robert F. Dickersonparkera
authored andcommitted
Changed NSDate, NSTimeZone, NSDateFormatter API to match Darwin version (#325)
* Changed NSDate API to match Darwin version * Changed NSDateFormatter API to match Darwin version * Changed NSTimeZone API to match Darwin version * Changed Date Tests to match Darwin version
1 parent 3f5a070 commit d67a289

File tree

7 files changed

+46
-46
lines changed

7 files changed

+46
-46
lines changed

Foundation/NSCalendar.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
814814
*/
815815
public func isDateInYesterday(_ date: NSDate) -> Bool {
816816
if let interval = rangeOfUnit(.Day, forDate: NSDate()) {
817-
let inYesterday = interval.start.dateByAddingTimeInterval(-60.0)
817+
let inYesterday = interval.start.addingTimeInterval(-60.0)
818818
return compareDate(date, toDate: inYesterday, toUnitGranularity: .Day) == .OrderedSame
819819
} else {
820820
return false
@@ -826,7 +826,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
826826
*/
827827
public func isDateInTomorrow(_ date: NSDate) -> Bool {
828828
if let interval = rangeOfUnit(.Day, forDate: NSDate()) {
829-
let inTomorrow = interval.end.dateByAddingTimeInterval(60.0)
829+
let inTomorrow = interval.end.addingTimeInterval(60.0)
830830
return compareDate(date, toDate: inTomorrow, toUnitGranularity: .Day) == .OrderedSame
831831
} else {
832832
return false
@@ -878,7 +878,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
878878
let comp = NSDateComponents()
879879
comp.weekday = range.start
880880
if let nextStart = nextDateAfterDate(date, matchingComponents: comp, options: options.union(.MatchNextTime)) {
881-
let start = nextStart.dateByAddingTimeInterval(range.onsetTime)
881+
let start = nextStart.addingTimeInterval(range.onsetTime)
882882
comp.weekday = range.end
883883
if let nextEnd = nextDateAfterDate(date, matchingComponents: comp, options: options.union(.MatchNextTime)) {
884884
var end = nextEnd
@@ -890,7 +890,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
890890
}
891891
}
892892
if range.ceaseTime > 0 {
893-
end = end.dateByAddingTimeInterval(range.ceaseTime)
893+
end = end.addingTimeInterval(range.ceaseTime)
894894
} else {
895895
if let dayEnd = rangeOfUnit(.Day, forDate: end) {
896896
end = startOfDayForDate(dayEnd.end)
@@ -1042,7 +1042,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
10421042
if opts.contains(.MatchStrictly) {
10431043
options.unionInPlace(.MatchStrictly)
10441044
}
1045-
if let result = nextDateAfterDate(range.start.dateByAddingTimeInterval(-0.5), matchingComponents: comps, options: options) {
1045+
if let result = nextDateAfterDate(range.start.addingTimeInterval(-0.5), matchingComponents: comps, options: options) {
10461046
if result.compare(range.start) == .OrderedAscending {
10471047
return nextDateAfterDate(range.start, matchingComponents: comps, options: options)
10481048
}

Foundation/NSDate.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
3030

3131
public override func isEqual(_ object: AnyObject?) -> Bool {
3232
if let date = object as? NSDate {
33-
return isEqualToDate(date)
33+
return isEqual(date)
3434
} else {
3535
return false
3636
}
@@ -149,19 +149,19 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
149149

150150
extension NSDate {
151151

152-
public func timeIntervalSinceDate(_ anotherDate: NSDate) -> NSTimeInterval {
152+
public func timeIntervalSince(_ anotherDate: NSDate) -> NSTimeInterval {
153153
return self.timeIntervalSinceReferenceDate - anotherDate.timeIntervalSinceReferenceDate
154154
}
155155

156156
public var timeIntervalSinceNow: NSTimeInterval {
157-
return timeIntervalSinceDate(NSDate())
157+
return timeIntervalSince(NSDate())
158158
}
159159

160160
public var timeIntervalSince1970: NSTimeInterval {
161161
return timeIntervalSinceReferenceDate + NSTimeIntervalSince1970
162162
}
163163

164-
public func dateByAddingTimeInterval(_ ti: NSTimeInterval) -> NSDate {
164+
public func addingTimeInterval(_ ti: NSTimeInterval) -> NSDate {
165165
return NSDate(timeIntervalSinceReferenceDate:_timeIntervalSinceReferenceDate + ti)
166166
}
167167

@@ -193,7 +193,7 @@ extension NSDate {
193193
}
194194
}
195195

196-
public func isEqualToDate(_ otherDate: NSDate) -> Bool {
196+
public func isEqual(to otherDate: NSDate) -> Bool {
197197
return timeIntervalSinceReferenceDate == otherDate.timeIntervalSinceReferenceDate
198198
}
199199
}

Foundation/NSDateFormatter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public class NSDateFormatter : NSFormatter {
4747

4848
public override func stringForObjectValue(_ obj: AnyObject) -> String? {
4949
guard let date = obj as? NSDate else { return nil }
50-
return stringFromDate(date)
50+
return string(from: date)
5151
}
5252

53-
public func stringFromDate(_ date: NSDate) -> String {
53+
public func string(from date: NSDate) -> String {
5454
return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, _cfObject, date._cfObject)._swiftObject
5555
}
5656

@@ -65,14 +65,14 @@ public class NSDateFormatter : NSFormatter {
6565
return date
6666
}
6767

68-
public class func localizedStringFromDate(_ date: NSDate, dateStyle dstyle: NSDateFormatterStyle, timeStyle tstyle: NSDateFormatterStyle) -> String {
68+
public class func localizedString(from date: NSDate, dateStyle dstyle: NSDateFormatterStyle, timeStyle tstyle: NSDateFormatterStyle) -> String {
6969
let df = NSDateFormatter()
7070
df.dateStyle = dstyle
7171
df.timeStyle = tstyle
7272
return df.stringForObjectValue(date)!
7373
}
7474

75-
public class func dateFormatFromTemplate(_ tmplate: String, options opts: Int, locale: NSLocale?) -> String? {
75+
public class func dateFormat(fromTemplate tmplate: String, options opts: Int, locale: NSLocale?) -> String? {
7676
guard let res = CFDateFormatterCreateDateFormatFromTemplate(kCFAllocatorSystemDefault, tmplate._cfObject, CFOptionFlags(opts), locale?._cfObject) else {
7777
return nil
7878
}

Foundation/NSTimeZone.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
6565

6666
public override func isEqual(_ object: AnyObject?) -> Bool {
6767
if let tz = object as? NSTimeZone {
68-
return isEqualToTimeZone(tz)
68+
return isEqual(to: tz)
6969
} else {
7070
return false
7171
}
@@ -129,39 +129,39 @@ public class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
129129
}
130130
}
131131

132-
public func secondsFromGMTForDate(_ aDate: NSDate) -> Int {
132+
public func secondsFromGMT(for aDate: NSDate) -> Int {
133133
if self.dynamicType === NSTimeZone.self {
134134
return Int(CFTimeZoneGetSecondsFromGMT(_cfObject, aDate.timeIntervalSinceReferenceDate))
135135
} else {
136136
NSRequiresConcreteImplementation()
137137
}
138138
}
139139

140-
public func abbreviationForDate(_ aDate: NSDate) -> String? {
140+
public func abbreviation(for aDate: NSDate) -> String? {
141141
if self.dynamicType === NSTimeZone.self {
142142
return CFTimeZoneCopyAbbreviation(_cfObject, aDate.timeIntervalSinceReferenceDate)._swiftObject
143143
} else {
144144
NSRequiresConcreteImplementation()
145145
}
146146
}
147147

148-
public func isDaylightSavingTimeForDate(_ aDate: NSDate) -> Bool {
148+
public func isDaylightSavingTime(for aDate: NSDate) -> Bool {
149149
if self.dynamicType === NSTimeZone.self {
150150
return CFTimeZoneIsDaylightSavingTime(_cfObject, aDate.timeIntervalSinceReferenceDate)
151151
} else {
152152
NSRequiresConcreteImplementation()
153153
}
154154
}
155155

156-
public func daylightSavingTimeOffsetForDate(_ aDate: NSDate) -> NSTimeInterval {
156+
public func daylightSavingTimeOffset(for aDate: NSDate) -> NSTimeInterval {
157157
if self.dynamicType === NSTimeZone.self {
158158
return CFTimeZoneGetDaylightSavingTimeOffset(_cfObject, aDate.timeIntervalSinceReferenceDate)
159159
} else {
160160
NSRequiresConcreteImplementation()
161161
}
162162
}
163163

164-
public func nextDaylightSavingTimeTransitionAfterDate(_ aDate: NSDate) -> NSDate? {
164+
public func nextDaylightSavingTimeTransition(after aDate: NSDate) -> NSDate? {
165165
if self.dynamicType === NSTimeZone.self {
166166
return NSDate(timeIntervalSinceReferenceDate: CFTimeZoneGetNextDaylightSavingTimeTransition(_cfObject, aDate.timeIntervalSinceReferenceDate))
167167
} else {
@@ -215,25 +215,25 @@ extension NSTimeZone {
215215
/// This invokes `abbreviationForDate:` with the current date as the argument.
216216
public var abbreviation: String? {
217217
let currentDate = NSDate()
218-
return abbreviationForDate(currentDate)
218+
return abbreviation(for: currentDate)
219219
}
220220

221221
public var daylightSavingTime: Bool { NSUnimplemented() }
222222
public var daylightSavingTimeOffset: NSTimeInterval { NSUnimplemented() }
223223
/*@NSCopying*/ public var nextDaylightSavingTimeTransition: NSDate? { NSUnimplemented() }
224224

225-
public func isEqualToTimeZone(_ aTimeZone: NSTimeZone) -> Bool {
225+
public func isEqual(to aTimeZone: NSTimeZone) -> Bool {
226226
return CFEqual(self._cfObject, aTimeZone._cfObject)
227227
}
228228

229229
public func localizedName(_ style: NSTimeZoneNameStyle, locale: NSLocale?) -> String? { NSUnimplemented() }
230230
}
231231
public enum NSTimeZoneNameStyle : Int {
232-
case Standard // Central Standard Time
233-
case ShortStandard // CST
234-
case DaylightSaving // Central Daylight Time
235-
case ShortDaylightSaving // CDT
236-
case Generic // Central Time
237-
case ShortGeneric // CT
232+
case standard // Central Standard Time
233+
case shortStandard // CST
234+
case daylightSaving // Central Daylight Time
235+
case shortDaylightSaving // CDT
236+
case generic // Central Time
237+
case shortGeneric // CT
238238
}
239239

TestFoundation/TestNSDate.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class TestNSDate : XCTestCase {
6666
let ti: NSTimeInterval = 1
6767
let d1 = NSDate()
6868
let d2 = NSDate(timeInterval: ti, sinceDate: d1)
69-
XCTAssertEqual(d2.timeIntervalSinceDate(d1), ti)
69+
XCTAssertEqual(d2.timeIntervalSince(d1), ti)
7070
}
7171

7272
func test_DistantFuture() {
@@ -82,36 +82,36 @@ class TestNSDate : XCTestCase {
8282
func test_DateByAddingTimeInterval() {
8383
let ti: NSTimeInterval = 1
8484
let d1 = NSDate()
85-
let d2 = d1.dateByAddingTimeInterval(ti)
85+
let d2 = d1.addingTimeInterval(ti)
8686
XCTAssertNotNil(d2)
8787
}
8888

8989
func test_EarlierDate() {
9090
let ti: NSTimeInterval = 1
9191
let d1 = NSDate()
92-
let d2 = d1.dateByAddingTimeInterval(ti)
92+
let d2 = d1.addingTimeInterval(ti)
9393
XCTAssertEqual(d1.earlierDate(d2), d1)
9494
}
9595

9696
func test_LaterDate() {
9797
let ti: NSTimeInterval = 1
9898
let d1 = NSDate()
99-
let d2 = d1.dateByAddingTimeInterval(ti)
99+
let d2 = d1.addingTimeInterval(ti)
100100
XCTAssertEqual(d1.laterDate(d2), d2)
101101
}
102102

103103
func test_Compare() {
104104
let ti: NSTimeInterval = 1
105105
let d1 = NSDate()
106-
let d2 = d1.dateByAddingTimeInterval(ti)
106+
let d2 = d1.addingTimeInterval(ti)
107107
XCTAssertEqual(d1.compare(d2), NSComparisonResult.OrderedAscending)
108108
}
109109

110110
func test_IsEqualToDate() {
111111
let ti: NSTimeInterval = 1
112112
let d1 = NSDate()
113-
let d2 = d1.dateByAddingTimeInterval(ti)
114-
let d3 = d1.dateByAddingTimeInterval(ti)
115-
XCTAssertTrue(d2.isEqualToDate(d3))
113+
let d2 = d1.addingTimeInterval(ti)
114+
let d3 = d1.addingTimeInterval(ti)
115+
XCTAssertTrue(d2.isEqual(to: d3))
116116
}
117117
}

TestFoundation/TestNSDateFormatter.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class TestNSDateFormatter: XCTestCase {
118118
for (timestamp, stringResult) in timestamps {
119119

120120
let testDate = NSDate(timeIntervalSince1970: timestamp)
121-
let sf = f.stringFromDate(testDate)
121+
let sf = f.string(from: testDate)
122122

123123
XCTAssertEqual(sf, stringResult)
124124
}
@@ -149,7 +149,7 @@ class TestNSDateFormatter: XCTestCase {
149149
for (timestamp, stringResult) in timestamps {
150150

151151
let testDate = NSDate(timeIntervalSince1970: timestamp)
152-
let sf = f.stringFromDate(testDate)
152+
let sf = f.string(from: testDate)
153153

154154
XCTAssertEqual(sf, stringResult)
155155
}
@@ -181,7 +181,7 @@ class TestNSDateFormatter: XCTestCase {
181181
for (timestamp, stringResult) in timestamps {
182182

183183
let testDate = NSDate(timeIntervalSince1970: timestamp)
184-
let sf = f.stringFromDate(testDate)
184+
let sf = f.string(from: testDate)
185185

186186
XCTAssertEqual(sf, stringResult)
187187
}
@@ -215,7 +215,7 @@ class TestNSDateFormatter: XCTestCase {
215215
for (timestamp, stringResult) in timestamps {
216216

217217
let testDate = NSDate(timeIntervalSince1970: timestamp)
218-
let sf = f.stringFromDate(testDate)
218+
let sf = f.string(from: testDate)
219219

220220
XCTAssertEqual(sf, stringResult)
221221
}
@@ -248,7 +248,7 @@ class TestNSDateFormatter: XCTestCase {
248248
for (timestamp, stringResult) in timestamps {
249249

250250
let testDate = NSDate(timeIntervalSince1970: timestamp)
251-
let sf = f.stringFromDate(testDate)
251+
let sf = f.string(from: testDate)
252252

253253
XCTAssertEqual(sf, stringResult)
254254
}
@@ -261,7 +261,7 @@ class TestNSDateFormatter: XCTestCase {
261261

262262
for (timestamp, stringResult) in quarterTimestamps {
263263
let testDate = NSDate(timeIntervalSince1970: timestamp)
264-
let sf = f.stringFromDate(testDate)
264+
let sf = f.string(from: testDate)
265265

266266
XCTAssertEqual(sf, stringResult)
267267
}
@@ -270,11 +270,11 @@ class TestNSDateFormatter: XCTestCase {
270270
let testDate = NSDate(timeIntervalSince1970: 1457738454)
271271
f.dateStyle = .MediumStyle
272272
f.timeStyle = .MediumStyle
273-
XCTAssertEqual(f.stringFromDate(testDate), "Mar 11, 2016, 11:20:54 PM")
273+
XCTAssertEqual(f.string(from: testDate), "Mar 11, 2016, 11:20:54 PM")
274274
XCTAssertEqual(f.dateFormat, "MMM d, y, h:mm:ss a")
275275

276276
f.dateFormat = "dd-MM-yyyy"
277-
XCTAssertEqual(f.stringFromDate(testDate), "11-03-2016")
277+
XCTAssertEqual(f.string(from: testDate), "11-03-2016")
278278

279279
}
280280

TestFoundation/TestNSTimeZone.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class TestNSTimeZone: XCTestCase {
3535
func test_abbreviation() {
3636
let tz = NSTimeZone.systemTimeZone()
3737
let abbreviation1 = tz.abbreviation
38-
let abbreviation2 = tz.abbreviationForDate(NSDate())
38+
let abbreviation2 = tz.abbreviation(for: NSDate())
3939
XCTAssertEqual(abbreviation1, abbreviation2, "\(abbreviation1) should be equal to \(abbreviation2)")
4040
}
4141

4242
func test_initializingTimeZoneWithOffset() {
4343
let tz = NSTimeZone(name: "GMT-0400")
4444
XCTAssertNotNil(tz)
45-
let seconds = tz?.secondsFromGMTForDate(NSDate())
45+
let seconds = tz?.secondsFromGMT(for: NSDate())
4646
XCTAssertEqual(seconds, -14400, "GMT-0400 should be -14400 seconds but got \(seconds) instead")
4747
}
4848

0 commit comments

Comments
 (0)