Skip to content

Changed NSDate, NSTimeZone, NSDateFormatter API to match Darwin version #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
*/
public func isDateInYesterday(_ date: NSDate) -> Bool {
if let interval = rangeOfUnit(.Day, forDate: NSDate()) {
let inYesterday = interval.start.dateByAddingTimeInterval(-60.0)
let inYesterday = interval.start.addingTimeInterval(-60.0)
return compareDate(date, toDate: inYesterday, toUnitGranularity: .Day) == .OrderedSame
} else {
return false
Expand All @@ -826,7 +826,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
*/
public func isDateInTomorrow(_ date: NSDate) -> Bool {
if let interval = rangeOfUnit(.Day, forDate: NSDate()) {
let inTomorrow = interval.end.dateByAddingTimeInterval(60.0)
let inTomorrow = interval.end.addingTimeInterval(60.0)
return compareDate(date, toDate: inTomorrow, toUnitGranularity: .Day) == .OrderedSame
} else {
return false
Expand Down Expand Up @@ -878,7 +878,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
let comp = NSDateComponents()
comp.weekday = range.start
if let nextStart = nextDateAfterDate(date, matchingComponents: comp, options: options.union(.MatchNextTime)) {
let start = nextStart.dateByAddingTimeInterval(range.onsetTime)
let start = nextStart.addingTimeInterval(range.onsetTime)
comp.weekday = range.end
if let nextEnd = nextDateAfterDate(date, matchingComponents: comp, options: options.union(.MatchNextTime)) {
var end = nextEnd
Expand All @@ -890,7 +890,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
}
}
if range.ceaseTime > 0 {
end = end.dateByAddingTimeInterval(range.ceaseTime)
end = end.addingTimeInterval(range.ceaseTime)
} else {
if let dayEnd = rangeOfUnit(.Day, forDate: end) {
end = startOfDayForDate(dayEnd.end)
Expand Down Expand Up @@ -1042,7 +1042,7 @@ public class NSCalendar : NSObject, NSCopying, NSSecureCoding {
if opts.contains(.MatchStrictly) {
options.unionInPlace(.MatchStrictly)
}
if let result = nextDateAfterDate(range.start.dateByAddingTimeInterval(-0.5), matchingComponents: comps, options: options) {
if let result = nextDateAfterDate(range.start.addingTimeInterval(-0.5), matchingComponents: comps, options: options) {
if result.compare(range.start) == .OrderedAscending {
return nextDateAfterDate(range.start, matchingComponents: comps, options: options)
}
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {

public override func isEqual(_ object: AnyObject?) -> Bool {
if let date = object as? NSDate {
return isEqualToDate(date)
return isEqual(date)
} else {
return false
}
Expand Down Expand Up @@ -149,19 +149,19 @@ public class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {

extension NSDate {

public func timeIntervalSinceDate(_ anotherDate: NSDate) -> NSTimeInterval {
public func timeIntervalSince(_ anotherDate: NSDate) -> NSTimeInterval {
return self.timeIntervalSinceReferenceDate - anotherDate.timeIntervalSinceReferenceDate
}

public var timeIntervalSinceNow: NSTimeInterval {
return timeIntervalSinceDate(NSDate())
return timeIntervalSince(NSDate())
}

public var timeIntervalSince1970: NSTimeInterval {
return timeIntervalSinceReferenceDate + NSTimeIntervalSince1970
}

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

Expand Down Expand Up @@ -193,7 +193,7 @@ extension NSDate {
}
}

public func isEqualToDate(_ otherDate: NSDate) -> Bool {
public func isEqual(to otherDate: NSDate) -> Bool {
return timeIntervalSinceReferenceDate == otherDate.timeIntervalSinceReferenceDate
}
}
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public class NSDateFormatter : NSFormatter {

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

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

Expand All @@ -65,14 +65,14 @@ public class NSDateFormatter : NSFormatter {
return date
}

public class func localizedStringFromDate(_ date: NSDate, dateStyle dstyle: NSDateFormatterStyle, timeStyle tstyle: NSDateFormatterStyle) -> String {
public class func localizedString(from date: NSDate, dateStyle dstyle: NSDateFormatterStyle, timeStyle tstyle: NSDateFormatterStyle) -> String {
let df = NSDateFormatter()
df.dateStyle = dstyle
df.timeStyle = tstyle
return df.stringForObjectValue(date)!
}

public class func dateFormatFromTemplate(_ tmplate: String, options opts: Int, locale: NSLocale?) -> String? {
public class func dateFormat(fromTemplate tmplate: String, options opts: Int, locale: NSLocale?) -> String? {
guard let res = CFDateFormatterCreateDateFormatFromTemplate(kCFAllocatorSystemDefault, tmplate._cfObject, CFOptionFlags(opts), locale?._cfObject) else {
return nil
}
Expand Down
28 changes: 14 additions & 14 deletions Foundation/NSTimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {

public override func isEqual(_ object: AnyObject?) -> Bool {
if let tz = object as? NSTimeZone {
return isEqualToTimeZone(tz)
return isEqual(to: tz)
} else {
return false
}
Expand Down Expand Up @@ -129,39 +129,39 @@ public class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
}
}

public func secondsFromGMTForDate(_ aDate: NSDate) -> Int {
public func secondsFromGMT(for aDate: NSDate) -> Int {
if self.dynamicType === NSTimeZone.self {
return Int(CFTimeZoneGetSecondsFromGMT(_cfObject, aDate.timeIntervalSinceReferenceDate))
} else {
NSRequiresConcreteImplementation()
}
}

public func abbreviationForDate(_ aDate: NSDate) -> String? {
public func abbreviation(for aDate: NSDate) -> String? {
if self.dynamicType === NSTimeZone.self {
return CFTimeZoneCopyAbbreviation(_cfObject, aDate.timeIntervalSinceReferenceDate)._swiftObject
} else {
NSRequiresConcreteImplementation()
}
}

public func isDaylightSavingTimeForDate(_ aDate: NSDate) -> Bool {
public func isDaylightSavingTime(for aDate: NSDate) -> Bool {
if self.dynamicType === NSTimeZone.self {
return CFTimeZoneIsDaylightSavingTime(_cfObject, aDate.timeIntervalSinceReferenceDate)
} else {
NSRequiresConcreteImplementation()
}
}

public func daylightSavingTimeOffsetForDate(_ aDate: NSDate) -> NSTimeInterval {
public func daylightSavingTimeOffset(for aDate: NSDate) -> NSTimeInterval {
if self.dynamicType === NSTimeZone.self {
return CFTimeZoneGetDaylightSavingTimeOffset(_cfObject, aDate.timeIntervalSinceReferenceDate)
} else {
NSRequiresConcreteImplementation()
}
}

public func nextDaylightSavingTimeTransitionAfterDate(_ aDate: NSDate) -> NSDate? {
public func nextDaylightSavingTimeTransition(after aDate: NSDate) -> NSDate? {
if self.dynamicType === NSTimeZone.self {
return NSDate(timeIntervalSinceReferenceDate: CFTimeZoneGetNextDaylightSavingTimeTransition(_cfObject, aDate.timeIntervalSinceReferenceDate))
} else {
Expand Down Expand Up @@ -215,25 +215,25 @@ extension NSTimeZone {
/// This invokes `abbreviationForDate:` with the current date as the argument.
public var abbreviation: String? {
let currentDate = NSDate()
return abbreviationForDate(currentDate)
return abbreviation(for: currentDate)
}

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

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

public func localizedName(_ style: NSTimeZoneNameStyle, locale: NSLocale?) -> String? { NSUnimplemented() }
}
public enum NSTimeZoneNameStyle : Int {
case Standard // Central Standard Time
case ShortStandard // CST
case DaylightSaving // Central Daylight Time
case ShortDaylightSaving // CDT
case Generic // Central Time
case ShortGeneric // CT
case standard // Central Standard Time
case shortStandard // CST
case daylightSaving // Central Daylight Time
case shortDaylightSaving // CDT
case generic // Central Time
case shortGeneric // CT
}

16 changes: 8 additions & 8 deletions TestFoundation/TestNSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TestNSDate : XCTestCase {
let ti: NSTimeInterval = 1
let d1 = NSDate()
let d2 = NSDate(timeInterval: ti, sinceDate: d1)
XCTAssertEqual(d2.timeIntervalSinceDate(d1), ti)
XCTAssertEqual(d2.timeIntervalSince(d1), ti)
}

func test_DistantFuture() {
Expand All @@ -82,36 +82,36 @@ class TestNSDate : XCTestCase {
func test_DateByAddingTimeInterval() {
let ti: NSTimeInterval = 1
let d1 = NSDate()
let d2 = d1.dateByAddingTimeInterval(ti)
let d2 = d1.addingTimeInterval(ti)
XCTAssertNotNil(d2)
}

func test_EarlierDate() {
let ti: NSTimeInterval = 1
let d1 = NSDate()
let d2 = d1.dateByAddingTimeInterval(ti)
let d2 = d1.addingTimeInterval(ti)
XCTAssertEqual(d1.earlierDate(d2), d1)
}

func test_LaterDate() {
let ti: NSTimeInterval = 1
let d1 = NSDate()
let d2 = d1.dateByAddingTimeInterval(ti)
let d2 = d1.addingTimeInterval(ti)
XCTAssertEqual(d1.laterDate(d2), d2)
}

func test_Compare() {
let ti: NSTimeInterval = 1
let d1 = NSDate()
let d2 = d1.dateByAddingTimeInterval(ti)
let d2 = d1.addingTimeInterval(ti)
XCTAssertEqual(d1.compare(d2), NSComparisonResult.OrderedAscending)
}

func test_IsEqualToDate() {
let ti: NSTimeInterval = 1
let d1 = NSDate()
let d2 = d1.dateByAddingTimeInterval(ti)
let d3 = d1.dateByAddingTimeInterval(ti)
XCTAssertTrue(d2.isEqualToDate(d3))
let d2 = d1.addingTimeInterval(ti)
let d3 = d1.addingTimeInterval(ti)
XCTAssertTrue(d2.isEqual(to: d3))
}
}
16 changes: 8 additions & 8 deletions TestFoundation/TestNSDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class TestNSDateFormatter: XCTestCase {
for (timestamp, stringResult) in timestamps {

let testDate = NSDate(timeIntervalSince1970: timestamp)
let sf = f.stringFromDate(testDate)
let sf = f.string(from: testDate)

XCTAssertEqual(sf, stringResult)
}
Expand Down Expand Up @@ -149,7 +149,7 @@ class TestNSDateFormatter: XCTestCase {
for (timestamp, stringResult) in timestamps {

let testDate = NSDate(timeIntervalSince1970: timestamp)
let sf = f.stringFromDate(testDate)
let sf = f.string(from: testDate)

XCTAssertEqual(sf, stringResult)
}
Expand Down Expand Up @@ -181,7 +181,7 @@ class TestNSDateFormatter: XCTestCase {
for (timestamp, stringResult) in timestamps {

let testDate = NSDate(timeIntervalSince1970: timestamp)
let sf = f.stringFromDate(testDate)
let sf = f.string(from: testDate)

XCTAssertEqual(sf, stringResult)
}
Expand Down Expand Up @@ -215,7 +215,7 @@ class TestNSDateFormatter: XCTestCase {
for (timestamp, stringResult) in timestamps {

let testDate = NSDate(timeIntervalSince1970: timestamp)
let sf = f.stringFromDate(testDate)
let sf = f.string(from: testDate)

XCTAssertEqual(sf, stringResult)
}
Expand Down Expand Up @@ -248,7 +248,7 @@ class TestNSDateFormatter: XCTestCase {
for (timestamp, stringResult) in timestamps {

let testDate = NSDate(timeIntervalSince1970: timestamp)
let sf = f.stringFromDate(testDate)
let sf = f.string(from: testDate)

XCTAssertEqual(sf, stringResult)
}
Expand All @@ -261,7 +261,7 @@ class TestNSDateFormatter: XCTestCase {

for (timestamp, stringResult) in quarterTimestamps {
let testDate = NSDate(timeIntervalSince1970: timestamp)
let sf = f.stringFromDate(testDate)
let sf = f.string(from: testDate)

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

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

}

Expand Down
4 changes: 2 additions & 2 deletions TestFoundation/TestNSTimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class TestNSTimeZone: XCTestCase {
func test_abbreviation() {
let tz = NSTimeZone.systemTimeZone()
let abbreviation1 = tz.abbreviation
let abbreviation2 = tz.abbreviationForDate(NSDate())
let abbreviation2 = tz.abbreviation(for: NSDate())
XCTAssertEqual(abbreviation1, abbreviation2, "\(abbreviation1) should be equal to \(abbreviation2)")
}

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

Expand Down