-
Notifications
You must be signed in to change notification settings - Fork 189
[Windows] Fix incorrect TimeZone.current
lookup logic
#975
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
[Windows] Fix incorrect TimeZone.current
lookup logic
#975
Conversation
@swift-ci please test |
let result: String? = timeZoneIdentifier.withUnsafeBufferPointer { identifier in | ||
return _withResizingUCharBuffer { buffer, size, status in | ||
let len = ucal_getTimeZoneIDForWindowsID(identifier.baseAddress, Int32(identifier.count), nil, buffer, size, &status) | ||
if status.isSuccess { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return status.isSuccess ? len : nil feels like a better way to write this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that seems fine too, I was just copying the format of the above getCanonicalTimeZoneID
function for consistency
swiftlang#975 started to restrict the fallback value for `TZDIR` and it revealed that WASI platform implicitly depends on TZDIR even though it won't have such directory. This patch explicitly handles the case for WASI platform for timezone operations.
#975 started to restrict the fallback value for `TZDIR` and it revealed that WASI platform implicitly depends on TZDIR even though it won't have such directory. This patch explicitly handles the case for WASI platform for timezone operations.
swiftlang#975 started to restrict the fallback value for `TZDIR` and it revealed that WASI platform implicitly depends on TZDIR even though it won't have such directory. This patch explicitly handles the case for WASI platform for timezone operations.
The current implementation of
TimeZone.current
attempts to read from/etc/localtime
which is not a path that exists on Windows. This leads toTimeZone.current
always being GMT on Windows. Instead, this updates the implementation to callGetTimeZoneInformation
, convert the Windows name to an ICU system timezone name, and initialize a timezone with that name (this matches the behavior ofTimeZone
on Swift 5.10 when it was based onCFTimeZone
). This also restricts the default values forTZDEFAULT
/TZDIR
to Darwin + Linux where the paths are reasonable unlike Windows where we know these constants should never exist to avoid accidental usage in the future.There's likely more work to be done here for Windows for a full solution (to allow in some way for developers to interface with Windows time zone identifiers so that
TimeZone
can interop with Windows system APIs) but this change at least fixes the critical issue and restores the Swift 5.10 behavior, we can look into adding additional API for working with Windows time zone identifiers in the future.Resolves #966, rdar://137451317