Description
Bug description:
dt_utc = datetime.now(timezone.utc).replace(microsecond=1)
dt_utc_str = dt_utc.strftime("%Y-%m-%d %H:%M:%S")
print(f"timenow : dt_utc_str : {dt_utc_str}")
if self.timezone:
print(f"timenow : timezone : {self.timezone}")
# dt_event = dt_utc.replace(tzinfo=ZoneInfo(self.timezone))
dt_event = dt_utc.astimezone(ZoneInfo(self.timezone))
dt_event_str = dt_event.strftime("%Y-%m-%d %H:%M:%S")
print(f"timenow : dt_event_str : {dt_event_str}")
tz_offset_ = dt_event.utcoffset()
tz_offset_str = str(tz_offset_)
print(f"timenow : tzoffstr : {tz_offset_str}")
print(
f"timenow : tz_offset_ : {tz_offset_} | type : {type(tz_offset_)}"
)
# error : invalid literal ... -1 day, 20:00:00 [workaround]
# parse timezone string to decimal hours
parts = [p for p in tz_offset_str.split(",") if p]
days = int(parts[0].split()[0]) if "day" in parts[0] else 0
h, m, s = map(int, parts[-1].strip().split(":"))
# convert to decimal
self.tz_offset = days * 24 + h + m / 60 + s / 3600
print(f"timenow : tz_offset : {self.tz_offset}")
-
terminal : expected / correct
timenow : dt_utc_str : 2025-04-17 15:46:10
timenow : timezone : Europe/Vienna
timenow : dt_event_str : 2025-04-17 17:46:10
timenow : tzoffstr : 2:00:00
timenow : tz_offset_ : 2:00:00 | type : <class 'datetime.timedelta'>
timenow : tz_offset : 2.0 -
terminal : unexpected / wrong
timenow : dt_utc_str : 2025-04-17 15:46:36
timenow : timezone : America/Lima
timenow : dt_event_str : 2025-04-17 10:46:36
timenow : tzoffstr : -1 day, 19:00:00
timenow : tz_offset_ : -1 day, 19:00:00 | type : <class 'datetime.timedelta'>
timenow : tz_offset : -5.0
seems western longitudes return '-1 day, hh:mm:ss' offset
tested with usa, peru, chile, venezuela : all west longitude (-ve) return '-1 day...'
with slovenia, austria, china, australia : works ok, as expected, no 'day' in return
the workaround i am using (bottom of above py code) is working properly > tz_offset is correct
basically it is -24 (hours) + hh:mm:ss (as decimal number)
os : linux mint22 (latest, greatest)
python : 3.11.12 in virtual env (Python 3.11.12 (main, Apr 9 2025, 08:55:55) [GCC 13.3.0] on linux)
guest os (with dev venv) is running in virual machine on identical host (mint22)
have fun
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Projects
Status
Status