Skip to content

Lesson 3 of 9 · 26m

Serialization, Databases, and the Boundaries Where Time Gets Lost

Make ISO 8601, database types, ORM behavior, APIs, queues, and language runtimes agree on exactly what a timestamp means.

Most timezone bugs are born at boundaries. The application stores a perfectly good UTC instant, then a serializer drops the Z, an ORM returns a naive object, a queue payload stringifies it differently, and the analytics pipeline interprets the result in the cluster's local zone. Nothing crashed. That is what makes it expensive.

Use ISO 8601 with an explicit offset

The safest common interchange format is ISO 8601 with an explicit numeric offset or Z:

{
  "created_at": "2026-11-30T13:15:00Z",
  "trial_ends_at": "2026-12-14T13:15:00Z",
  "customer_timezone": "America/New_York"
}

Avoid 2026-11-30 13:15:00, 11/30/2026, and localized strings in machine contracts. RFC 3339 is the stricter profile many APIs mean when they say ISO 8601. It still represents an instant only when the offset is present.

Choose database types deliberately

In PostgreSQL, timestamptz stores an instant and is the correct default for application event time. timestamp without timezone stores a bare wall-clock value and is appropriate only when you truly mean an unlocated civil time.

CREATE TABLE billing_events (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    occurred_at timestamptz NOT NULL,
    occurred_in_timezone text NOT NULL,
    ingested_at timestamptz NOT NULL DEFAULT now()
);

Why keep occurred_in_timezone? Because the UTC instant tells you when it happened globally, while the original zone tells you what the customer meant locally. You cannot reconstruct intent from an offset after DST rules change; offsets are not zones.

Audit every hop

Trace a timestamp from browser to warehouse:

browser Date -> JSON -> API validator -> ORM -> PostgreSQL
             -> queue payload -> worker -> analytics export
             -> dashboard renderer -> CSV export

At each hop ask three questions: is it timezone-aware, what unit and precision survive, and who performs conversion? The dangerous conversion is usually implicit. JavaScript Date objects serialize with toISOString() in UTC, but new Date("2026-11-30") is parsed as UTC while new Date("2026-11-30 00:00") is commonly parsed as local time. Small syntax differences create date shifts.

Keep date-only values date-only

Birth dates, invoice dates, and service dates are not midnight instants in a timezone. Store them as DATE in SQL or an ISO date string such as 2026-11-30. Converting them to timestamptz at midnight UTC can move them to the previous day in America or the same morning in Asia depending on rendering.

Field notes

  • Configure database sessions explicitly; SET TIME ZONE changes display and some parsing behavior, not the underlying instant in timestamptz.
  • Reject ambiguous strings in validators rather than normalizing them silently.
  • Do not use floating-point epoch values for high-precision financial or audit data; binary rounding is a real interoperability problem.
  • Include example timestamp payloads in contract tests, especially around DST transition dates.

Was this lesson helpful?

Sign in to rate