Files
Reims-Go/internal/host/timezone_windows.go
T

59 lines
1.6 KiB
Go

package host
import (
"unsafe"
"golang.org/x/sys/windows"
)
var (
icu = windows.NewLazySystemDLL("icu.dll")
icuTimezoneForWindowsID = icu.NewProc("ucal_getTimeZoneIDForWindowsID")
getDynamicTimezone = windows.NewLazySystemDLL("kernel32.dll").NewProc("GetDynamicTimeZoneInformation")
)
type dynamicTimezoneInformation struct {
Bias int32
StandardName [32]uint16
StandardDate windows.Systemtime
StandardBias int32
DaylightName [32]uint16
DaylightDate windows.Systemtime
DaylightBias int32
TimeZoneKeyName [128]uint16
DynamicDaylightTimeDisabled uint8
}
func DetectHostTimezone() string {
var info dynamicTimezoneInformation
result, _, _ := getDynamicTimezone.Call(uintptr(unsafe.Pointer(&info)))
if uint32(result) == 0xffffffff {
return ""
}
if icuTimezoneForWindowsID.Find() == nil {
var buf [65]uint16
var status int32
n, _, _ := icuTimezoneForWindowsID.Call(uintptr(unsafe.Pointer(&info.TimeZoneKeyName[0])), uintptr(^uint32(0)), 0,
uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), uintptr(unsafe.Pointer(&status)))
if tz := readICUTimezone(buf[:], int32(n), status); tz != "" {
return tz
}
}
tz := windowsIANATimezones[windows.UTF16ToString(info.TimeZoneKeyName[:])]
if ValidateIANATimezone(tz) {
return tz
}
return ""
}
func readICUTimezone(buf []uint16, n, status int32) string {
if status > 0 || n <= 0 || int(n) >= len(buf) {
return ""
}
tz := windows.UTF16ToString(buf[:n])
if tz == "Etc/Unknown" || !ValidateIANATimezone(tz) {
return ""
}
return tz
}