Skip to content

SR-10515: Calendar.date(from:) unexpectedly mutates Calendar instance #2162

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
6 changes: 4 additions & 2 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {

open func date(from comps: DateComponents) -> Date? {
var (vector, compDesc) = _convert(comps)


let oldTz = self.timeZone
self.timeZone = comps.timeZone ?? timeZone

var at: CFAbsoluteTime = 0.0
Expand All @@ -524,7 +525,8 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
return _CFCalendarComposeAbsoluteTimeV(_cfObject, t, compDesc, vectorBuffer.baseAddress!, Int32(vectorBuffer.count))
}
}


self.timeZone = oldTz
if res {
return Date(timeIntervalSinceReferenceDate: at)
} else {
Expand Down
27 changes: 27 additions & 0 deletions TestFoundation/TestCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TestCalendar: XCTestCase {
("test_ampmSymbols", test_ampmSymbols),
("test_currentCalendarRRstability", test_currentCalendarRRstability),
("test_hashing", test_hashing),
("test_dateFromDoesntMutate", test_dateFromDoesntMutate),
]
}

Expand Down Expand Up @@ -227,6 +228,32 @@ class TestCalendar: XCTestCase {
]
checkHashable(calendars2, equalityOracle: { $0 == $1 })
}

func test_dateFromDoesntMutate() throws {
// Check that date(from:) does not change the timeZone of the calendar
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
df.timeZone = try TimeZone(identifier: "UTC").unwrapped()

var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "en_US_POSIX")
calendar.timeZone = try TimeZone(secondsFromGMT: 0).unwrapped()

let calendarCopy = calendar
XCTAssertEqual(calendarCopy.timeZone.identifier, "GMT")
XCTAssertEqual(calendarCopy.timeZone.description, "GMT (fixed)")

let dc = try calendarCopy.dateComponents(in: TimeZone(identifier: "America/New_York").unwrapped(), from: df.date(from: "2019-01-01").unwrapped())
XCTAssertEqual(calendarCopy.timeZone.identifier, "GMT")
XCTAssertEqual(calendarCopy.timeZone.description, "GMT (fixed)")

let dt = try calendarCopy.date(from: dc).unwrapped()
XCTAssertEqual(dt.description, "2019-01-01 00:00:00 +0000")
XCTAssertEqual(calendarCopy.timeZone.identifier, "GMT")
XCTAssertEqual(calendarCopy.timeZone.description, "GMT (fixed)")
XCTAssertEqual(calendarCopy.timeZone, calendar.timeZone)
XCTAssertEqual(calendarCopy, calendar)
}
}

class TestNSDateComponents: XCTestCase {
Expand Down