← Back to SQL, page 8

sql-0159 · Medium mid

How should timestamps be stored when users are in different time zones?

Store instants in UTC, using timestamptz in PostgreSQL or UTC ISO text or Unix seconds in SQLite, and convert to the viewer's zone only for display. Store the user's IANA zone name (such as 'Asia/Kolkata') separately when local wall-clock time matters, for example for recurring 9 AM reminders.

Full answer

SQLite's datetime('now') returns UTC, and 'localtime' converts using the server's zone, not the user's:

sql
SELECT datetime('2026-03-01 10:00:00') AS stored_utc,
       datetime(1772359200, 'unixepoch') AS from_unix_utc;
stored_utcfrom_unix_utc
2026-03-01 10:00:002026-03-01 10:00:00

PostgreSQL's timestamptz stores an absolute instant. AT TIME ZONE turns it into the wall-clock time of a named zone:

sql
SELECT TIMESTAMPTZ '2026-03-01 10:00:00+00' AT TIME ZONE 'UTC'          AS wall_clock_utc,
       TIMESTAMPTZ '2026-03-01 10:00:00+00' AT TIME ZONE 'Asia/Kolkata' AS wall_clock_kolkata,
       TIMESTAMPTZ '2026-03-01 10:00:00+00' AT TIME ZONE 'Europe/Berlin' AS wall_clock_berlin;
wall_clock_utcwall_clock_kolkatawall_clock_berlin
2026-03-01 10:00:002026-03-01 15:30:002026-03-01 11:00:00

One stored instant becomes 10:00 in UTC, 15:30 in Kolkata and 11:00 in Berlin. Storage is unambiguous; display is local. A session's TIME ZONE setting has the same effect on how timestamptz values are printed.

Two cases need more than a UTC instant:

  • Future local events ("every day at 09:00 in Berlin") should store local time plus the zone name, because daylight-saving rules can change before the event.
  • Reports "by day" must state which zone defines the day; a UTC day boundary is 05:30 in India.

Avoid storing fixed offsets such as +05:30 as the zone, since offsets change with daylight saving in many regions.

sql
SELECT datetime('2026-03-01 10:00:00') AS stored_utc,
       datetime(1772359200, 'unixepoch') AS from_unix_utc;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Store instants in UTC or timestamptz
  • Convert to the viewer's zone at display time
  • Store IANA zone names for local recurring events
  • Define the zone used for daily reports

Common mistakes

  • Storing local times without a zone
  • Using the server's zone for users elsewhere

Follow-ups

  • How would you group orders by the customer's local day?
  • What goes wrong with a fixed offset during daylight saving changes?

Tips

  • Separate instants from wall-clock schedules