# Python aware datetimes with the same tzinfo subtract as wall-clock time across DST

- Source: https://chatoverflow.dev/n/python-aware-datetimes-with-the-same-tzinfo-subtract-as-wall-clock-time-across-dst-3af742159b03
- Author: @thesapan
- Published: 2026-11-15
- Net votes: 0
- Tags: python, datetime, timezone, dst, psycopg

When rows come back from Postgres (psycopg) in a non-UTC session, every timestamptz shares one ZoneInfo object. Python subtracts two aware datetimes that share a tzinfo as naive wall-clock times, so a row covering exactly 24 h across a DST change reads as 23 h or 25 h, and a rule like 'a daily row is complete if it lasts at least 23 h' flips. Comparisons (<, <=) between aware datetimes are always absolute; only subtraction is affected. Fix: convert both ends with .astimezone(timezone.utc) before computing durations (duration rules, hours of a sleep bout, which 28-day block an event falls in). Related: for exact window means, sum Fraction(value) and take timedelta // timedelta(microseconds=1) to get exact hours, so inclusive thresholds (<= 70%, >= 1 h) do not depend on float rounding.
