Skip to content

[6.0.x] [Windows] Fix incorrect TimeZone.current lookup logic #978

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
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 Sources/FoundationEssentials/TimeZone/TimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ extension TimeZone {

extension TimeZone {
internal static func dataFromTZFile(_ name: String) -> Data {
#if NO_TZFILE
#if NO_TZFILE || os(Windows)
return Data()
#else
let path = TZDIR + "/" + name
Expand Down
26 changes: 14 additions & 12 deletions Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ dynamic package func _timeZoneGMTClass() -> _TimeZoneProtocol.Type {
}
#endif

#if os(Windows)
dynamic package func _timeZoneIdentifier(forWindowsIdentifier windowsIdentifier: String) -> String? {
nil
}
#endif

/// Singleton which listens for notifications about preference changes for TimeZone and holds cached values for current, fixed time zones, etc.
struct TimeZoneCache : Sendable {
// MARK: - State
Expand Down Expand Up @@ -129,18 +135,14 @@ struct TimeZoneCache : Sendable {
}

#if os(Windows)
let hFile = TZDEFAULT.withCString(encodedAs: UTF16.self) {
CreateFileW($0, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nil, OPEN_EXISTING, 0, nil)
}
defer { CloseHandle(hFile) }
let dwSize = GetFinalPathNameByHandleW(hFile, nil, 0, VOLUME_NAME_DOS)
let path = withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwSize)) {
_ = GetFinalPathNameByHandleW(hFile, $0.baseAddress, dwSize, VOLUME_NAME_DOS)
return String(decodingCString: $0.baseAddress!, as: UTF16.self)
}
if let rangeOfZoneInfo = path._range(of: "\(TZDIR)\\", anchored: false, backwards: false) {
let name = path[rangeOfZoneInfo.upperBound...]
if let result = fixed(String(name)) {
var timeZoneInfo = TIME_ZONE_INFORMATION()
if GetTimeZoneInformation(&timeZoneInfo) != TIME_ZONE_ID_INVALID {
let windowsName = withUnsafePointer(to: &(timeZoneInfo.StandardName)) {
$0.withMemoryRebound(to: WCHAR.self, capacity: 32) {
String(decoding: UnsafeBufferPointer(start: $0, count: wcslen($0)), as: UTF16.self)
}
}
if let identifier = _timeZoneIdentifier(forWindowsIdentifier: windowsName), let result = fixed(identifier) {
return TimeZone(inner: result)
}
}
Expand Down
24 changes: 24 additions & 0 deletions Sources/FoundationInternationalization/TimeZone/TimeZone_ICU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ private func _timeZoneICUClass_localized() -> _TimeZoneProtocol.Type? {
}
#endif

#if os(Windows)
@_dynamicReplacement(for: _timeZoneIdentifier(forWindowsIdentifier:))
private func _timeZoneIdentifier_ICU(forWindowsIdentifier windowsIdentifier: String) -> String? {
_TimeZoneICU.getSystemTimeZoneID(forWindowsIdentifier: windowsIdentifier)
}
#endif

internal final class _TimeZoneICU: _TimeZoneProtocol, Sendable {
init?(secondsFromGMT: Int) {
fatalError("Unexpected init")
Expand Down Expand Up @@ -309,6 +316,23 @@ internal final class _TimeZoneICU: _TimeZoneProtocol, Sendable {
return result
}

#if os(Windows)
internal static func getSystemTimeZoneID(forWindowsIdentifier identifier: String) -> String? {
let timeZoneIdentifier = Array(identifier.utf16)
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 {
return len
} else {
return nil
}
}
}
return result
}
#endif

internal static func timeZoneNamesFromICU() -> [String] {
let filteredTimeZoneNames = [
"ACT",
Expand Down
2 changes: 2 additions & 0 deletions Sources/_FoundationCShims/include/_CStdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@
#include <tzfile.h>
#else

#if TARGET_OS_MAC || TARGET_OS_LINUX
#ifndef TZDIR
#define TZDIR "/usr/share/zoneinfo/" /* Time zone object file directory */
#endif /* !defined TZDIR */

#ifndef TZDEFAULT
#define TZDEFAULT "/etc/localtime"
#endif /* !defined TZDEFAULT */
#endif /* TARGET_OS_MAC || TARGET_OS_LINUX */

#endif

Expand Down