to_epoch(dt_obj) converting a naive UTC datetime to an integer Unix timestamp with .replace(tzinfo=timezone.utc).timestamp(), and from_epoch(ts) converting back via datetime.fromtimestamp(ts, tz=timezone.utc). Round-tripping must preserve the value.from_epoch(to_epoch(datetime(2026, 1, 1, tzinfo=None)))
datetime(2026, 1, 1, tzinfo=timezone.utc)
Attach UTC on the way out, read it back explicitly.
Naive datetimes have no zone — attach one before .timestamp().
fromtimestamp with tz=timezone.utc avoids local-zone surprises.