From 98eb17647db4467be42ee3876888683f6f2ccd09 Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Wed, 11 Sep 2024 12:15:07 +0200 Subject: [PATCH] TestKit: fix timezone id check Correctly determine datetime values coming from TestKit that represent datetimes not existing according to the local tz db. --- testkitbackend/fromtestkit.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/testkitbackend/fromtestkit.py b/testkitbackend/fromtestkit.py index c0724583f..fa5f17931 100644 --- a/testkitbackend/fromtestkit.py +++ b/testkitbackend/fromtestkit.py @@ -137,12 +137,22 @@ def to_param(m): if timezone_id is not None: utc_offset = timedelta(seconds=utc_offset_s) tz = pytz.timezone(timezone_id) - localized_datetime = tz.localize(datetime, is_dst=False) - if localized_datetime.utcoffset() == utc_offset: - return localized_datetime - localized_datetime = tz.localize(datetime, is_dst=True) - if localized_datetime.utcoffset() == utc_offset: - return localized_datetime + try: + localized_datetime = tz.localize(datetime, is_dst=None) + if localized_datetime.utcoffset() == utc_offset: + return localized_datetime + except pytz.AmbiguousTimeError: + localized_datetime = tz.localize(datetime, is_dst=False) + if localized_datetime.utcoffset() == utc_offset: + return localized_datetime + localized_datetime = tz.localize(datetime, is_dst=True) + if localized_datetime.utcoffset() == utc_offset: + return localized_datetime + except pytz.NonExistentTimeError as e: + raise ValueError( + f"local datetime {datetime} does not exist in timezone " + f"{timezone_id}" + ) from e raise ValueError( "cannot localize datetime %s to timezone %s with UTC " "offset %s" % (datetime, timezone_id, utc_offset)