Skip to content

Commit

Permalink
Handle different strftime format spec on Windows. Fixes #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
liffiton committed Nov 6, 2024
1 parent 99e2755 commit c99b4d6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/gened/tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only

import datetime as dt
import platform

import pytz
from flask import request, session
Expand Down Expand Up @@ -35,9 +36,14 @@ def localtime_filter(value: dt.datetime) -> str:
'''Use timezone from the session object, if available, to localize datetimes from UTC.'''
# https://stackoverflow.com/a/34832184
utc_dt = pytz.utc.localize(value)

# Windows uses a different format string for non-zero-padded hours in strftime
# https://strftime.org/
hr_fmt = '%#I' if platform.system() == "Windows" else '%-I'

if 'timezone' not in session:
return utc_dt.strftime("%Y-%m-%d %-I:%M%P %Z") # %Z to include 'UTC'
return utc_dt.strftime(f"%Y-%m-%d {hr_fmt}:%M%P %Z") # %Z to include 'UTC'
else:
local_tz = pytz.timezone(session['timezone'])
local_dt = utc_dt.astimezone(local_tz)
return local_dt.strftime("%Y-%m-%d %-I:%M%P")
return local_dt.strftime(f"%Y-%m-%d {hr_fmt}:%M%P")

0 comments on commit c99b4d6

Please sign in to comment.