From 81d15a2932a37bfd5a6838e9f77dff66f50b9973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Hansmann?= Date: Wed, 8 Jan 2025 11:03:06 +0100 Subject: [PATCH] PB-1319: treatment of timezone-naive timestamps The format_time method now interprets timezone-naive timestamps as local times. Timezone-naive timestamps are not expected in the response, but if so, we enforce treatment as local times. --- chsdi/lib/opentransapi/opentransapi.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/chsdi/lib/opentransapi/opentransapi.py b/chsdi/lib/opentransapi/opentransapi.py index 24c396b67d..d77c9608e9 100644 --- a/chsdi/lib/opentransapi/opentransapi.py +++ b/chsdi/lib/opentransapi/opentransapi.py @@ -18,12 +18,16 @@ def format_time(str_date_time): # - timezone offsets, e.g. "+01:00" # - sometimes the returned timestamps have an unexpected number of # fractional seconds, e.g. 7 instead of 6, should be handled, too + local_tz = timezone('Europe/Zurich') date_time = isoparse(str_date_time) + # If for some reason we receive a timezone-naive timestamp, we assume it is a + # local time + if date_time.tzinfo is None: + date_time = date_time.replace(tzinfo=local_tz) + # Convert to local timezone explicitly - local_tz = timezone('Europe/Zurich') # Replace with your local timezone local_date_time = date_time.astimezone(local_tz) - # Return time in local time, as needed. return local_date_time.strftime('%d/%m/%Y %H:%M')