Skip to content

SR-10516: TimeZone.nextDaylightSavingTimeTransition does not return nil #2160

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 1 commit into from
Apr 23, 2019
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
4 changes: 3 additions & 1 deletion Foundation/NSTimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
guard type(of: self) === NSTimeZone.self else {
NSRequiresConcreteImplementation()
}
return Date(timeIntervalSinceReferenceDate: CFTimeZoneGetNextDaylightSavingTimeTransition(_cfObject, aDate.timeIntervalSinceReferenceDate))
let ti = CFTimeZoneGetNextDaylightSavingTimeTransition(_cfObject, aDate.timeIntervalSinceReferenceDate)
guard ti > 0 else { return nil }
return Date(timeIntervalSinceReferenceDate: ti)
}

open class var system: TimeZone {
Expand Down
36 changes: 34 additions & 2 deletions TestFoundation/TestTimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

import CoreFoundation

class TestTimeZone: XCTestCase {

Expand Down Expand Up @@ -228,7 +227,39 @@ class TestTimeZone: XCTestCase {
}
}
}


func test_nextDaylightSavingTimeTransition() throws {
// Timezones without DST
let gmt = try TimeZone(secondsFromGMT: 0).unwrapped()
let msk = try TimeZone(identifier: "Europe/Moscow").unwrapped()

// Timezones with DST
let bst = try TimeZone(abbreviation: "BST").unwrapped()
let aest = try TimeZone(identifier: "Australia/Sydney").unwrapped()

XCTAssertNil(gmt.nextDaylightSavingTimeTransition)
XCTAssertNil(msk.nextDaylightSavingTimeTransition)
XCTAssertNotNil(bst.nextDaylightSavingTimeTransition)
XCTAssertNotNil(aest.nextDaylightSavingTimeTransition)

let formatter = DateFormatter()
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.dateFormat = "yyyy-MM-dd"

let dt1 = try formatter.date(from: "2018-01-01").unwrapped()
XCTAssertNil(gmt.nextDaylightSavingTimeTransition(after: dt1))
XCTAssertNil(msk.nextDaylightSavingTimeTransition(after: dt1))
XCTAssertEqual(bst.nextDaylightSavingTimeTransition(after: dt1)?.description, "2018-03-25 01:00:00 +0000")
XCTAssertEqual(aest.nextDaylightSavingTimeTransition(after: dt1)?.description, "2018-03-31 16:00:00 +0000")

formatter.timeZone = aest
let dt2 = try formatter.date(from: "2018-06-06").unwrapped()
XCTAssertNil(gmt.nextDaylightSavingTimeTransition(after: dt2))
XCTAssertNil(msk.nextDaylightSavingTimeTransition(after: dt2))
XCTAssertEqual(bst.nextDaylightSavingTimeTransition(after: dt2)?.description, "2018-10-28 01:00:00 +0000")
XCTAssertEqual(aest.nextDaylightSavingTimeTransition(after: dt2)?.description, "2018-10-06 16:00:00 +0000")
}

static var allTests: [(String, (TestTimeZone) -> () throws -> Void)] {
var tests: [(String, (TestTimeZone) -> () throws -> Void)] = [
("test_abbreviation", test_abbreviation),
Expand All @@ -247,6 +278,7 @@ class TestTimeZone: XCTestCase {
("test_knownTimeZones", test_knownTimeZones),
("test_systemTimeZoneName", test_systemTimeZoneName),
("test_autoupdatingTimeZone", test_autoupdatingTimeZone),
("test_nextDaylightSavingTimeTransition", test_nextDaylightSavingTimeTransition),
]

#if !os(Windows)
Expand Down