Skip to content

Fixed TimeZone.init(secondsFromGMT:) failability #1142

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 3 commits into from
Aug 1, 2017
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
15 changes: 9 additions & 6 deletions Foundation/TimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ public struct TimeZone : Hashable, Equatable, ReferenceConvertible {
/// - parameter seconds: The number of seconds from GMT.
/// - returns: A time zone, or `nil` if a valid time zone could not be created from `seconds`.
public init?(secondsFromGMT seconds: Int) {
if let r = NSTimeZone(forSecondsFromGMT: seconds) as NSTimeZone? {
_wrapped = r
_autoupdating = false
} else {
return nil
}
// Seconds boundaries check should actually be placed in NSTimeZone.init(forSecondsFromGMT:) which should return
// nil if the check fails. However, NSTimeZone.init(forSecondsFromGMT:) is not a failable initializer, so it
// cannot return nil.
// It is not a failable initializer because we want to have parity with Darwin's NSTimeZone, which is
// Objective-C and has a wrong _Nonnull annotation.
if (seconds < -18 * 3600 || 18 * 3600 < seconds) { return nil }

_wrapped = NSTimeZone(forSecondsFromGMT: seconds)
_autoupdating = false
}

/// Returns a time zone identified by a given abbreviation.
Expand Down
6 changes: 6 additions & 0 deletions TestFoundation/TestNSTimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ class TestNSTimeZone: XCTestCase {

let tz3 = TimeZone(identifier: "GMT-9999")
XCTAssertNil(tz3)

XCTAssertNotNil(TimeZone(secondsFromGMT: -18 * 3600))
XCTAssertNotNil(TimeZone(secondsFromGMT: 18 * 3600))

XCTAssertNil(TimeZone(secondsFromGMT: -18 * 3600 - 1))
XCTAssertNil(TimeZone(secondsFromGMT: 18 * 3600 + 1))
}

func test_initializingTimeZoneWithAbbreviation() {
Expand Down