Skip to content

Commit ae27e1e

Browse files
authored
Merge pull request #2627 from spevans/pr_sr_11569_52
[5.2] SR-11569: DateComponents.isValidDate() ignores timeZone
2 parents 70ef255 + d73b926 commit ae27e1e

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

Foundation/NSCalendar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
19101910
}
19111911
if let date = d {
19121912
let all: NSCalendar.Unit = [.era, .year, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .quarter, .weekOfMonth, .weekOfYear, .yearForWeekOfYear]
1913-
let comps = calendar._bridgeToObjectiveC().components(all, from: date)
1913+
let comps = cal._bridgeToObjectiveC().components(all, from: date)
19141914
var val = era
19151915
if val != NSDateComponentUndefined {
19161916
if comps.era != val {

TestFoundation/TestDateComponents.swift

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
// This source file is part of the Swift.org open source project
22
//
3-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
3+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
6-
// See http://swift.org/LICENSE.txt for license information
7-
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
6+
// See https://swift.org/LICENSE.txt for license information
7+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

1010
class TestDateComponents: XCTestCase {
11-
static var allTests: [(String, (TestDateComponents) -> () throws -> Void)] {
12-
return [
13-
("test_hash", test_hash),
14-
]
15-
}
1611

1712
func test_hash() {
1813
let c1 = DateComponents(year: 2018, month: 8, day: 1)
@@ -100,4 +95,32 @@ class TestDateComponents: XCTestCase {
10095
throughValues: integers)
10196
// isLeapMonth does not have enough values to test it here.
10297
}
98+
99+
func test_isValidDate() throws {
100+
// SR-11569
101+
let calendarTimeZone = try XCTUnwrap(TimeZone(secondsFromGMT: 0))
102+
let dateComponentsTimeZone = try XCTUnwrap(TimeZone(secondsFromGMT: 3600))
103+
104+
var calendar = Calendar(identifier: .gregorian)
105+
calendar.timeZone = calendarTimeZone
106+
107+
var dc = DateComponents()
108+
dc.calendar = calendar
109+
dc.timeZone = dateComponentsTimeZone
110+
dc.year = 2019
111+
dc.month = 1
112+
dc.day = 2
113+
dc.hour = 3
114+
dc.minute = 4
115+
dc.second = 5
116+
dc.nanosecond = 6
117+
XCTAssertTrue(dc.isValidDate)
118+
}
119+
120+
static var allTests: [(String, (TestDateComponents) -> () throws -> Void)] {
121+
return [
122+
("test_hash", test_hash),
123+
("test_isValidDate", test_isValidDate),
124+
]
125+
}
103126
}

0 commit comments

Comments
 (0)