Skip to content

Commit

Permalink
PB-1319: treatment of timezone-naive timestamps
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hansmannj committed Jan 8, 2025
1 parent 7fabdc4 commit 81d15a2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions chsdi/lib/opentransapi/opentransapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 81d15a2

Please sign in to comment.