Skip to content

Commit 7c8f145

Browse files
authored
Merge pull request #2762 from readdle/win-tz-daylight
[Windows] Fix TimeZone display name and daylight saving time check
2 parents f9e60d6 + 7bb7c4b commit 7c8f145

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

CoreFoundation/NumberDate.subproj/CFTimeZone.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,9 +1663,10 @@ CFStringRef CFTimeZoneCopyAbbreviation(CFTimeZoneRef tz, CFAbsoluteTime at) {
16631663
}
16641664
ucal_setMillis(ucal, (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0, &status);
16651665

1666+
UCalendarDisplayNameType nameType = ucal_inDaylightTime(ucal, &status) ? UCAL_SHORT_DST : UCAL_SHORT_STANDARD;
16661667
UChar buffer[64];
16671668
int32_t length;
1668-
length = ucal_getTimeZoneDisplayName(ucal, UCAL_SHORT_STANDARD, "C", buffer, sizeof(buffer), &status);
1669+
length = ucal_getTimeZoneDisplayName(ucal, nameType, "C", buffer, sizeof(buffer), &status);
16691670

16701671
ucal_close(ucal);
16711672

@@ -1678,10 +1679,22 @@ CFStringRef CFTimeZoneCopyAbbreviation(CFTimeZoneRef tz, CFAbsoluteTime at) {
16781679
}
16791680

16801681
Boolean CFTimeZoneIsDaylightSavingTime(CFTimeZoneRef tz, CFAbsoluteTime at) {
1681-
CFIndex idx;
16821682
__CFGenericValidateType(tz, CFTimeZoneGetTypeID());
1683+
#if TARGET_OS_WIN32
1684+
UErrorCode status = U_ZERO_ERROR;
1685+
UCalendar *ucal = __CFCalendarCreateUCalendar(NULL, CFSTR("C"), tz);
1686+
if (ucal == NULL) {
1687+
return FALSE;
1688+
}
1689+
ucal_setMillis(ucal, (at + kCFAbsoluteTimeIntervalSince1970) * 1000.0, &status);
1690+
1691+
UBool isDaylightTime = ucal_inDaylightTime(ucal, &status);
1692+
return isDaylightTime ? TRUE : FALSE;
1693+
#else
1694+
CFIndex idx;
16831695
idx = __CFBSearchTZPeriods(tz, at);
16841696
return __CFTZPeriodIsDST(&(tz->_periods[idx]));
1697+
#endif
16851698
}
16861699

16871700
CFTimeInterval CFTimeZoneGetDaylightSavingTimeOffset(CFTimeZoneRef tz, CFAbsoluteTime at) {

Tests/Foundation/Tests/TestTimeZone.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ class TestTimeZone: XCTestCase {
256256
XCTAssertEqual(aest.nextDaylightSavingTimeTransition(after: dt2)?.description, "2018-10-06 16:00:00 +0000")
257257
}
258258

259+
func test_isDaylightSavingTime() throws {
260+
let eukv = try XCTUnwrap(TimeZone(identifier: "Europe/Kiev"))
261+
262+
let dateNoDST = Date(timeIntervalSince1970: 1585432800.0) // March 29, 2020
263+
let dateDST = Date(timeIntervalSince1970: 1585515600.0) // March 30, 2020
264+
265+
XCTAssertFalse(eukv.isDaylightSavingTime(for: dateNoDST))
266+
XCTAssertTrue(eukv.isDaylightSavingTime(for: dateDST))
267+
}
268+
259269
static var allTests: [(String, (TestTimeZone) -> () throws -> Void)] {
260270
var tests: [(String, (TestTimeZone) -> () throws -> Void)] = [
261271
("test_abbreviation", test_abbreviation),
@@ -275,6 +285,7 @@ class TestTimeZone: XCTestCase {
275285
("test_systemTimeZoneName", test_systemTimeZoneName),
276286
("test_autoupdatingTimeZone", test_autoupdatingTimeZone),
277287
("test_nextDaylightSavingTimeTransition", test_nextDaylightSavingTimeTransition),
288+
("test_isDaylightSavingTime", test_isDaylightSavingTime),
278289
]
279290

280291
#if !os(Windows)

0 commit comments

Comments
 (0)