Skip to content

[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

Merged

Conversation

jmschonfeld
Copy link
Contributor

The current implementation of TimeZone.current attempts to read from /etc/localtime which is not a path that exists on Windows. This leads to TimeZone.current always being GMT on Windows. Instead, this updates the implementation to call GetTimeZoneInformation, convert the Windows name to an ICU system timezone name, and initialize a timezone with that name (this matches the behavior of TimeZone on Swift 5.10 when it was based on CFTimeZone). This also restricts the default values for TZDEFAULT/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

@jmschonfeld
Copy link
Contributor Author

@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 {
Copy link
Member

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.

Copy link
Contributor Author

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

@jmschonfeld jmschonfeld merged commit 889c38f into swiftlang:main Oct 11, 2024
3 checks passed
@jmschonfeld jmschonfeld deleted the windows/fix-current-timezone branch October 11, 2024 17:49
jmschonfeld added a commit to jmschonfeld/swift-foundation that referenced this pull request Oct 11, 2024
jmschonfeld added a commit to jmschonfeld/swift-foundation that referenced this pull request Oct 11, 2024
kateinoigakukun added a commit to kateinoigakukun/swift-foundation that referenced this pull request Oct 16, 2024
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.
kateinoigakukun added a commit that referenced this pull request Oct 18, 2024
#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.
cthielen pushed a commit to cthielen/swift-foundation that referenced this pull request Nov 8, 2024
cthielen pushed a commit to cthielen/swift-foundation that referenced this pull request Nov 8, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Windows] TimeZone.current returns GMT despite system settings
3 participants