-
Working with dates and times in Python: a practical guide
Python's datetime module is powerful but has a well-known pitfall: naive datetimes lack timezone info, which causes subtle bugs and outright errors when mixing them with timezone-aware datetimes. This guide covers the full datetime workflow in Python 3.9+, from basic object creation and string formatting to correct timezone handling with zoneinfo and arithmetic with timedelta. The core takeaway is to always use UTC-aware datetimes internally and convert to local time only for display.
-
Enforce API contracts at the boundary: Pydantic v2 validators done right
Pydantic v2 silently breaks several v1 assumptions - Optional[str] fields are now required unless you add = None, model validators return None instead of the instance if you forget return self, and lax mode will coerce "3" into an int without complaint. This article walks through field validators, cross-field model validators, and ConfigDict(strict=True) with runnable examples that expose exactly where these traps fire and how to close them.