Skip to content

[5.2] SR-11569: DateComponents.isValidDate() ignores timeZone #2627

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 1, 2020
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
2 changes: 1 addition & 1 deletion Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
}
if let date = d {
let all: NSCalendar.Unit = [.era, .year, .month, .day, .hour, .minute, .second, .weekday, .weekdayOrdinal, .quarter, .weekOfMonth, .weekOfYear, .yearForWeekOfYear]
let comps = calendar._bridgeToObjectiveC().components(all, from: date)
let comps = cal._bridgeToObjectiveC().components(all, from: date)
var val = era
if val != NSDateComponentUndefined {
if comps.era != val {
Expand Down
39 changes: 31 additions & 8 deletions TestFoundation/TestDateComponents.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

class TestDateComponents: XCTestCase {
static var allTests: [(String, (TestDateComponents) -> () throws -> Void)] {
return [
("test_hash", test_hash),
]
}

func test_hash() {
let c1 = DateComponents(year: 2018, month: 8, day: 1)
Expand Down Expand Up @@ -100,4 +95,32 @@ class TestDateComponents: XCTestCase {
throughValues: integers)
// isLeapMonth does not have enough values to test it here.
}

func test_isValidDate() throws {
// SR-11569
let calendarTimeZone = try XCTUnwrap(TimeZone(secondsFromGMT: 0))
let dateComponentsTimeZone = try XCTUnwrap(TimeZone(secondsFromGMT: 3600))

var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = calendarTimeZone

var dc = DateComponents()
dc.calendar = calendar
dc.timeZone = dateComponentsTimeZone
dc.year = 2019
dc.month = 1
dc.day = 2
dc.hour = 3
dc.minute = 4
dc.second = 5
dc.nanosecond = 6
XCTAssertTrue(dc.isValidDate)
}

static var allTests: [(String, (TestDateComponents) -> () throws -> Void)] {
return [
("test_hash", test_hash),
("test_isValidDate", test_isValidDate),
]
}
}