principleMajorpending
Timezone handling in applications — always store UTC
Viewed 0 times
UTCtimezoneISO 8601DSTdaylight savingIntl.DateTimeFormatstore UTC
nodejspythonbrowser
Problem
Application shows wrong times for users in different timezones. Daylight saving time transitions cause events to show at the wrong time. Database stores local times, making conversion impossible.
Solution
Golden rule: store all times in UTC, convert to local time only for display. (1) Database: use TIMESTAMP WITH TIME ZONE (PostgreSQL) or store as UTC with explicit column naming (_utc suffix). (2) JavaScript: use toISOString() for UTC strings. Luxon or date-fns-tz for timezone conversion. (3) Python: always use datetime.now(timezone.utc), never datetime.now(). (4) API: send ISO 8601 with timezone offset: 2024-01-15T10:30:00Z. (5) Frontend: convert UTC to user's local timezone using Intl.DateTimeFormat or library. (6) For recurring events: store timezone name (America/New_York), not offset — offsets change with DST.
Why
UTC is unambiguous — no DST transitions, no timezone rules. Converting from UTC to local time is straightforward with timezone name. Converting between local times is error-prone.
Revisions (0)
No revisions yet.