Skip to content

Commit 797b221

Browse files
committed
gh-148658: Fix DST timezone name for negative timestamps on Windows (#148658)
Set tm_isdst to 0 for timezones that don't observe DST to ensure strftime('%Z') returns the standard time name.
1 parent 2a07ff9 commit 797b221

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

Python/pytime.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111
#ifdef MS_WINDOWS
1212
# include <winsock2.h> // struct timeval
13+
# include <windows.h> // time zone APIs
1314
#endif
1415

1516
#if defined(__APPLE__)
@@ -355,8 +356,24 @@ _PyTime_windows_filetime(time_t timer, struct tm *tm, int is_local)
355356
// `time.gmtime` and `time.localtime` will return `struct_time` containing this
356357
tm->tm_yday = _PyTime_calc_yday(&st_result);
357358

358-
/* DST flag: -1 (unknown) for local time on historical dates, 0 for UTC */
359-
tm->tm_isdst = is_local ? -1 : 0;
359+
/* DST flag: -1 (unknown) for local time on historical dates, 0 for UTC.
360+
* For timezones that don't observe DST, set tm_isdst to 0 to ensure
361+
* strftime('%Z') returns the standard time name (gh-148658). */
362+
if (is_local) {
363+
TIME_ZONE_INFORMATION tzi;
364+
DWORD tz_result = GetTimeZoneInformation(&tzi);
365+
if (tz_result != TIME_ZONE_ID_INVALID &&
366+
tzi.DaylightDate.wMonth == 0) {
367+
/* Timezone does not observe DST */
368+
tm->tm_isdst = 0;
369+
}
370+
else {
371+
tm->tm_isdst = -1;
372+
}
373+
}
374+
else {
375+
tm->tm_isdst = 0;
376+
}
360377

361378
return 0;
362379
}

0 commit comments

Comments
 (0)