mktime causing overflowError due to daylight savings time

80 Views Asked by At

I'm currently getting an Overflow error on the following code when daylight savings is set to true. I've tried a number of date/time combinations and it appears to only be the 9th parameter ("is_dst") being set to "1" which is triggering the issue. The python versions tested on were 3.8.10.final.0 and 3.10.12.final.0 on databricks.

Is there anything peculiar around daylight savings time that could cause the issue - I don't believe that anything about the dates should lead to an overflow error

import time
time.mktime(time.struct_time((2023, 10, 5, 12, 0, 0, 6, 68, -1)))
1696507200.0
time.mktime(time.struct_time((2023, 10, 5, 12, 0, 0, 6, 68, 0)))
1696507200.0
time.mktime(time.struct_time((2023, 10, 5, 12, 0, 0, 6, 68, 1)))
OverflowError: mktime argument out of range

More explicit version

import pytz
from datetime import datetime, timezone

utc_dt = datetime.now(timezone.utc)  # UTC time
dt = utc_dt.astimezone()  # local time

btz = pytz.timezone("Australia/Brisbane") # currently +10
stz = pytz.timezone("Australia/Sydney") # currently +11

#brisbane
d1 = datetime.now(btz)
print(d1)
x1= d1.timetuple()
print(x1)
time.mktime(x1)
#sydney
d2 = datetime.now(stz)
print(d2)
x2= d2.timetuple()
print(x2)
time.mktime(x2)

The output is

2023-10-06 11:35:10.010458+10:00
time.struct_time(tm_year=2023, tm_mon=10, tm_mday=6, tm_hour=11, tm_min=35, tm_sec=10, tm_wday=4, tm_yday=279, tm_isdst=0)
2023-10-06 12:35:10.010884+11:00
time.struct_time(tm_year=2023, tm_mon=10, tm_mday=6, tm_hour=12, tm_min=35, tm_sec=10, tm_wday=4, tm_yday=279, tm_isdst=1)
OverflowError: mktime argument out of range

This became an issue earlier this week when Sydney switched from AEST to AEDT (daylight savings time). The server is set to this timezone

1

There are 1 best solutions below

1
eshirvana On

mktime calls the underlying C library, and It is platform dependent.

The tm_isdst parameter is used to indicate whether Daylight Saving Time (DST) is in effect for the given time. When tm_isdst is set to 1, it signifies that DST is in effect, and this can cause issues in the time.mktime function, especially if it interacts with the system's DST settings.

To avoid this issue, you should generally avoid setting tm_isdst to 1 when creating a struct_time. Instead, let Python handle the DST information automatically based on your system's settings.