Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created April 18, 2026 15:09
Show Gist options
  • Select an option

  • Save ynkdir/f8ab2e7afbb85cecce9ddb453b2cac74 to your computer and use it in GitHub Desktop.

Select an option

Save ynkdir/f8ab2e7afbb85cecce9ddb453b2cac74 to your computer and use it in GitHub Desktop.
calculate dst offset with icu api
# calculate dst offset with icu api
#
# /// script
# dependencies = ["win32more"]
# ///
from win32more import String
from win32more.Windows.Win32.Globalization import (
UCAL_DEFAULT,
UCAL_DST_OFFSET,
UErrorCode,
ucal_close,
ucal_get,
ucal_open,
ucal_setDateTime,
)
def U_FAILURE(x):
return x > 0
def ucal_dst(zone_id, year, month, day, hour, minute, second):
status = UErrorCode()
cal = ucal_open(String(zone_id), -1, None, UCAL_DEFAULT, status)
if U_FAILURE(status.value):
raise RuntimeError(status.value)
ucal_setDateTime(cal, year, month, day, hour, minute, second, status)
if U_FAILURE(status.value):
raise RuntimeError(status.value)
dst = ucal_get(cal, UCAL_DST_OFFSET, status)
if U_FAILURE(status.value):
raise RuntimeError(status.value)
ucal_close(cal)
return dst
assert ucal_dst("America/New_York", 2006, 1, 1, 12, 0, 0) == 0
assert ucal_dst("America/New_York", 2006, 5, 1, 12, 0, 0) == 3600000
assert ucal_dst("America/New_York", 1918, 3, 29, 8, 0, 0) == 3600000 # ?
assert ucal_dst("America/New_York", 1918, 3, 31, 8, 0, 0) == 3600000 # ?
assert ucal_dst("America/New_York", 1918, 4, 1, 8, 0, 0) == 3600000 # ?
assert ucal_dst("America/New_York", 1918, 4, 6, 8, 0, 0) == 3600000 # ?
assert ucal_dst("America/New_York", 1918, 4, 7, 8, 0, 0) == 3600000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment