Skip to content

[Windows] TimeZone.current returns GMT despite system settings #966

Closed
@jmschonfeld

Description

@jmschonfeld

Running TimeZone.current on Windows always returns GMT despite any present system settings.

This appears to be because on Windows we check TZDEFAULT:

#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)) {
return TimeZone(inner: result)
}
}

However, TZDEFAULT does not appear to be set in the WinSDK/ucrt modules so we instead pickup the default value from our CShims:

#ifndef TZDIR
#define TZDIR "/usr/share/zoneinfo/" /* Time zone object file directory */
#endif /* !defined TZDIR */
#ifndef TZDEFAULT
#define TZDEFAULT "/etc/localtime"
#endif /* !defined TZDEFAULT */

These default paths seem unsuitable for Windows and result in us failing to detect the current timezone and falling back to GMT

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions