Sub topic 13
Daylight saving time pitfalls
This chapter assumes you know that timestamptz stores an instant, that the session TimeZone setting controls display, and that AT TIME ZONE converts in both directions. It covers where daylight saving time (DST) breaks code that otherwise looks correct, and how to write queries that stay correct.
Why DST breaks things
A time zone with DST has two kinds of transitions each year.
- Gap (spring forward): the clock jumps ahead, so some local times never happen. In
America/New_Yorkon 2024-03-10, the clock goes from 01:59 EST straight to 03:00 EDT. Local times from 02:00 to 02:59 do not exist. - Overlap (fall back): the clock jumps back, so some local times happen twice. On 2024-11-03, the local hour from 01:00 to 01:59 occurs once in EDT and again in EST.
An instant (UTC) is never ambiguous. A local wall-clock time can be missing or ambiguous. Most DST bugs come from treating wall-clock values as if they were instants.
Days are not always 24 hours
In a DST zone, a local calendar day lasts 23, 24, or 25 hours. You can check this by measuring the elapsed time between two local midnights.
SET TimeZone = 'America/New_York';
SELECT extract(epoch FROM (TIMESTAMPTZ '2024-03-11 00:00:00'
- TIMESTAMPTZ '2024-03-10 00:00:00'))::bigint / 3600
AS spring_day_hours;
-- 23
SELECT extract(epoch FROM (TIMESTAMPTZ '2024-11-04 00:00:00'
- TIMESTAMPTZ '2024-11-03 00:00:00'))::bigint / 3600
AS fall_day_hours;
-- 25Any code that assumes 86400 seconds per day is wrong twice a year in these zones. This includes dividing an epoch difference by 86400 to count days, and bucketing with floor(epoch / 86400).
Day, hour, and month units behave differently
PostgreSQL keeps the months, days, and microseconds parts of an interval separate. When you add an interval to a timestamptz, the months and days parts move the local calendar, and the time part moves elapsed time. The session TimeZone decides which local calendar is used.
| Interval | Moves | Across DST |
|---|---|---|
| `'1 month'` | local calendar month | Keeps local clock time |
| `'1 day'` | local calendar day | Keeps local clock time |
| `'24 hours'` | 24 elapsed hours | Local clock time can shift by an hour |
Across the spring change in New York:
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-03-09 12:00:00' + interval '1 day' AS plus_one_day,
TIMESTAMPTZ '2024-03-09 12:00:00' + interval '24 hours' AS plus_24_hours;
-- plus_one_day: 2024-03-10 12:00:00-04
-- plus_24_hours: 2024-03-10 13:00:00-04Across the fall change, the same difference runs the other way:
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-11-02 12:00:00' + interval '1 day' AS plus_one_day,
TIMESTAMPTZ '2024-11-02 12:00:00' + interval '24 hours' AS plus_24_hours;
-- plus_one_day: 2024-11-03 12:00:00-05
-- plus_24_hours: 2024-11-03 11:00:00-05Choose the unit that matches your intent. "Same time tomorrow" is '1 day'. "A timeout 24 hours from now" is '24 hours'.
Nonexistent and ambiguous local times
When you turn a local timestamp into a timestamptz, you must say which zone the wall-clock value belongs to. That works for most values. It cannot give a single right answer for a time in a gap or an overlap.
-- 02:30 does not exist in New York on 2024-03-10
SELECT TIMESTAMP '2024-03-10 02:30:00' AT TIME ZONE 'America/New_York';
-- 01:30 happens twice in New York on 2024-11-03
SELECT TIMESTAMP '2024-11-03 01:30:00' AT TIME ZONE 'America/New_York';PostgreSQL does not raise an error for either query. It picks an interpretation, and you should not depend on which one. If an overlap time matters, supply an explicit offset, because an offset names one instant:
SELECT TIMESTAMPTZ '2024-11-03 01:30:00-04' AS first_pass, -- EDT
TIMESTAMPTZ '2024-11-03 01:30:00-05' AS second_pass; -- ESTFor user-entered local times, validate them in the application. Convert the result back to local time and compare it with the input. If they differ, the input was in a gap.
Abbreviations do not follow DST
A zone abbreviation such as EST is a fixed offset. It does not become EDT in summer. A zone name such as America/New_York carries the full rule history.
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-07-01 12:00:00+00' AT TIME ZONE 'EST' AS fixed_abbrev,
TIMESTAMPTZ '2024-07-01 12:00:00+00' AT TIME ZONE 'America/New_York' AS named_zone;
-- fixed_abbrev: 2024-07-01 07:00:00
-- named_zone: 2024-07-01 08:00:00Use names from pg_timezone_names for anything tied to a place. Use fixed offsets only when the offset itself is the data.
Grouping by local day and local hour
Truncating a timestamptz with date_trunc('day', ...) uses the session zone. To count by a specific zone's local days, convert first, as in the chapter's earlier example:
SELECT date_trunc('day', starts_at AT TIME ZONE 'America/New_York') AS local_day,
count(*) AS events
FROM event
GROUP BY 1
ORDER BY 1;This works for days because the result is a plain timestamp, and each local day has one midnight. It fails for hours. On the fall-back night, two different instants share the local label 01:00, and the query merges them into one bucket:
-- Wrong on 2024-11-03: the two 01:00 hours collapse into one row
SELECT date_trunc('hour', starts_at AT TIME ZONE 'America/New_York') AS local_hour,
count(*)
FROM event
GROUP BY 1;
-- Right: bucket the instants, and let the session zone control display
SET TimeZone = 'America/New_York';
SELECT date_trunc('hour', starts_at) AS hour_start,
count(*)
FROM event
GROUP BY 1
ORDER BY 1;The rule is to bucket sub-day units on instants (timestamptz). Bucket days and larger units on local values, or use the three-argument form date_trunc('day', starts_at, 'America/New_York') on PostgreSQL 12 and later. That form returns a timestamptz for the start of the local day.
Range queries across a transition
A local day or month as a half-open range has a start and an end instant. Compute both ends in the zone, and do not add a fixed number of hours:
SELECT *
FROM event
WHERE starts_at >= TIMESTAMP '2024-03-10 00:00:00' AT TIME ZONE 'America/New_York'
AND starts_at < TIMESTAMP '2024-03-11 00:00:00' AT TIME ZONE 'America/New_York';This range covers 23 hours of elapsed time, which is correct for that day. A condition such as starts_at < start + interval '24 hours' would take in an hour from the next day.
Recurring local-time events
"Every day at 09:00 in New York" is a rule about wall-clock time. Generate the local times first, then convert each one to an instant:
SET TimeZone = 'UTC';
SELECT (d + time '09:00') AT TIME ZONE 'America/New_York' AS starts_at
FROM generate_series(TIMESTAMP '2024-03-08',
TIMESTAMP '2024-03-12',
interval '1 day') AS d;
-- 2024-03-08 14:00:00+00
-- 2024-03-09 14:00:00+00
-- 2024-03-10 13:00:00+00
-- 2024-03-11 13:00:00+00
-- 2024-03-12 13:00:00+00The UTC time shifts by one hour after the transition because the local time stays at 09:00. If you generated instants spaced 24 hours apart, the local time would drift to 10:00.
Future events and changing rules
Governments change DST rules, sometimes with little notice. PostgreSQL uses the IANA time zone database, and updates to it ship with minor releases. A future timestamptz was already converted to an instant using the rules known at insert time. If the rules later change, that instant no longer matches the local time the user meant.
For future appointments that must stay at a local time, store the local time and the zone name, and convert when you read:
CREATE TABLE appointment (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
local_start timestamp NOT NULL,
zone text NOT NULL
);
SELECT id, local_start AT TIME ZONE zone AS starts_at
FROM appointment;Use timestamptz for events that already happened, or that are defined as an instant. Use local timestamp plus a zone name for future events defined as a local time.
Quick checklist
- Use
'1 day'for calendar intent and'24 hours'for elapsed time. - Never assume a day has 86400 seconds.
- Bucket hours on instants, and bucket days on local values.
- Use zone names, not abbreviations.
- Set
TimeZoneexplicitly in jobs and reports. - Store local time plus zone name for future local-time events.
Exercise
In the America/New_York zone, how many elapsed hours are there between local midnight on 2024-11-03 and local midnight on 2024-11-04? Write a query that returns the number.
Show answer
25. The day includes the repeated 01:00 hour. For example: SET TimeZone = 'America/New_York'; SELECT extract(epoch FROM (TIMESTAMPTZ '2024-11-04 00:00:00' - TIMESTAMPTZ '2024-11-03 00:00:00'))::bigint / 3600;
Exercise
A report groups events with datetrunc('hour', startsat AT TIME ZONE 'America/New_York'). Why does it return one row too few on 2024-11-03, and how do you fix it?
Show answer
Converting to a plain local timestamp before truncating gives both 01:00 hours the same label, so their counts merge into one row. Truncate the timestamptz directly with datetrunc('hour', startsat) and set TimeZone to America/New_York for display.