Skip to content

Commit e232b6b

Browse files
authored
TestKit: fix timezone id check (#1090)
Correctly determine datetime values coming from TestKit that represent datetimes not existing according to the local tz db.
1 parent a8dc1a5 commit e232b6b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

testkitbackend/fromtestkit.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,22 @@ def to_param(m):
137137
if timezone_id is not None:
138138
utc_offset = timedelta(seconds=utc_offset_s)
139139
tz = pytz.timezone(timezone_id)
140-
localized_datetime = tz.localize(datetime, is_dst=False)
141-
if localized_datetime.utcoffset() == utc_offset:
142-
return localized_datetime
143-
localized_datetime = tz.localize(datetime, is_dst=True)
144-
if localized_datetime.utcoffset() == utc_offset:
145-
return localized_datetime
140+
try:
141+
localized_datetime = tz.localize(datetime, is_dst=None)
142+
if localized_datetime.utcoffset() == utc_offset:
143+
return localized_datetime
144+
except pytz.AmbiguousTimeError:
145+
localized_datetime = tz.localize(datetime, is_dst=False)
146+
if localized_datetime.utcoffset() == utc_offset:
147+
return localized_datetime
148+
localized_datetime = tz.localize(datetime, is_dst=True)
149+
if localized_datetime.utcoffset() == utc_offset:
150+
return localized_datetime
151+
except pytz.NonExistentTimeError as e:
152+
raise ValueError(
153+
f"local datetime {datetime} does not exist in timezone "
154+
f"{timezone_id}"
155+
) from e
146156
raise ValueError(
147157
"cannot localize datetime %s to timezone %s with UTC "
148158
"offset %s" % (datetime, timezone_id, utc_offset)

0 commit comments

Comments
 (0)