Sub topic 13
Daylight Saving Time Pitfalls
Assume you know basic SELECT, WHERE, and interval expressions, and understand that UTC offsets describe the difference from UTC. This chapter focuses on how daylight saving time (DST) changes affect PostgreSQL calculations and queries.
A local day is not always 24 hours
Many regions move clocks forward in spring and backward in autumn. A local calendar day can therefore contain:
- 23 elapsed hours during the spring-forward change.
- 25 elapsed hours during the fall-back change.
- A repeated local clock time during the fall change.
- A local clock time that never occurs during the spring change.
PostgreSQL applies time zone rules when it converts a timestamptz value. It does not treat every day as a fixed number of seconds.
Day intervals and hour intervals differ
For a timestamptz, interval '1 day' preserves the local clock time when a DST transition occurs. interval '24 hours' adds exactly 24 elapsed hours.
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-04The first result keeps noon as the local wall-clock time. The second result represents exactly one day of elapsed time, so it appears at 13:00 after the clock has moved forward.
Use a day or month when the business rule means “the same local time on the next calendar date.” Use hours or seconds when the rule means elapsed duration.
Missing and repeated local times
A plain timestamp has no time zone. When you convert it to timestamptz, PostgreSQL must interpret that wall-clock value in a named zone.
During a spring-forward transition, a value such as 02:30 may not exist. During a fall-back transition, a value such as 01:30 may occur twice. PostgreSQL resolves these cases using the zone's transition rules, but that automatic choice may not match your business meaning.
-- 02:30 does not occur in New York on this date.
SELECT TIMESTAMP '2024-03-10 02:30:00'
AT TIME ZONE 'America/New_York';
-- 01:30 occurs twice in New York on this date.
SELECT TIMESTAMP '2024-11-03 01:30:00'
AT TIME ZONE 'America/New_York';If users enter local times for appointments, validate them and preserve the intended zone or offset. A local timestamp alone cannot identify a unique instant during a repeated hour.
Querying local calendar periods safely
To find events in a local month or day, calculate the boundaries in the target zone and use a half-open range: include the start and exclude the end.
SELECT *
FROM event
WHERE starts_at >= TIMESTAMPTZ '2024-03-10 00:00:00 America/New_York'
AND starts_at < TIMESTAMPTZ '2024-03-11 00:00:00 America/New_York';This includes every instant belonging to the local date, even though that date has only 23 elapsed hours. The < upper bound also prevents an event exactly at the next boundary from appearing in both periods.
Exercise
Which expression should you use for a billing rule that means “the same local time tomorrow”: interval '1 day' or interval '24 hours'?
Show answer
Use interval '1 day'. Use interval '24 hours' only when the rule requires exactly 24 elapsed hours.