Sub topic 6
Converting with AT TIME ZONE
This chapter assumes you can write basic SQL and run queries in psql. It also assumes you know that UTC is the reference time and that an offset such as +02 means "two hours ahead of UTC". You should have read the earlier chapters on timestamp versus timestamptz and on the session TimeZone setting.
By the end you will be able to predict the result type of every AT TIME ZONE expression and avoid the usual mistakes with it.
Why one operator does two jobs
AT TIME ZONE is a single operator that converts in two directions. Which direction you get depends on the type of the value on its left.
| Left operand | Result type | What happens |
|---|---|---|
| `timestamptz` | `timestamp` | The instant is shown as wall-clock time in the named zone. The zone information is dropped. |
| `timestamp` | `timestamptz` | The wall-clock time is read as local time in the named zone, which fixes the exact instant. |
| `timetz` | `timetz` | The time is moved to the named zone's offset. |
The operator does not change the instant that a timestamptz represents. It either describes that instant in a particular zone's clock (as a timestamp) or builds an instant from a clock reading (as a timestamptz).
From timestamptz to timestamp
When the left side is a timestamptz, the result is a plain timestamp holding the wall-clock time in the zone you name. The session TimeZone setting is not used here, so the result is the same in every session.
SET TimeZone = 'UTC';
-- timestamptz -> timestamp (wall-clock time in Tokyo)
SELECT TIMESTAMPTZ '2024-06-01 07:00:00+00' AT TIME ZONE 'Asia/Tokyo';
-- 2024-06-01 16:00:00Tokyo is UTC+9 all year, so 07:00 UTC is 16:00 on the Tokyo clock. The output has no offset because a timestamp carries none. If you send that value to another system, the receiver cannot tell it was Tokyo time. Keep the zone in your own naming, for example a column called starts_at_tokyo, or keep the original timestamptz next to it.
From timestamp to timestamptz
When the left side is a plain timestamp, PostgreSQL treats it as a clock reading in the named zone and works out which instant that is. The result is a timestamptz, and PostgreSQL then prints it using the session TimeZone.
SET TimeZone = 'UTC';
-- timestamp -> timestamptz (interpret the wall-clock time as Paris time)
SELECT TIMESTAMP '2024-06-01 09:00:00' AT TIME ZONE 'Europe/Paris';
-- 2024-06-01 07:00:00+00Paris is on summer time (UTC+2) on 1 June, so 09:00 in Paris is 07:00 UTC. The printed value depends on the session setting. In a session set to America/New_York, the same expression prints 2024-06-01 03:00:00-04. It is the same instant in both cases.
Predicting the result type
A reliable habit is to ask what type is on the left. Reading the expression from the left:
- If the left side is
timestamptz, the result istimestamp. - If the left side is
timestamp, the result istimestamptz.
You can check this with pg_typeof:
SELECT pg_typeof(now() AT TIME ZONE 'UTC') AS from_timestamptz,
pg_typeof(localtimestamp AT TIME ZONE 'UTC') AS from_timestamp;
-- from_timestamptz: timestamp without time zone
-- from_timestamp: timestamp with time zoneHere now() returns timestamptz, and localtimestamp returns timestamp.
Chaining two conversions
Because each use of the operator flips the type, you can chain two of them to express "take this wall-clock time in zone A and show me the wall-clock time in zone B". Start from a timestamp, interpret it in zone A to get an instant, then describe that instant in zone B.
-- 09:00 in Paris, expressed as wall-clock time in New York
SELECT TIMESTAMP '2024-06-01 09:00:00'
AT TIME ZONE 'Europe/Paris'
AT TIME ZONE 'America/New_York' AS new_york_clock;
-- 2024-06-01 03:00:00The operator is left-associative, so the first conversion runs first. The first step gives a timestamptz, and the second step gives a timestamp. The final result has no zone attached, so label it clearly.
Zone names, abbreviations, and offsets
The right side can be a zone name, an abbreviation, or a fixed offset. They behave differently.
- A full name such as
'Europe/Paris'uses the IANA time zone database and follows daylight saving rules, including past rule changes. - An abbreviation such as
'EST'is a fixed offset in PostgreSQL's abbreviation table. It does not switch to summer time.'EST'means UTC-5 all year, so using it for New York in July gives a wrong answer. - An interval such as
INTERVAL '+02:00'is a fixed offset, with a sign that follows the usual convention for offsets. A POSIX-style string such as'UTC+2'is also accepted, but note the sign trap below.
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-07-01 12:00:00+00' AT TIME ZONE 'America/New_York' AS by_name,
TIMESTAMPTZ '2024-07-01 12:00:00+00' AT TIME ZONE 'EST' AS by_abbrev;
-- by_name: 2024-07-01 08:00:00
-- by_abbrev: 2024-07-01 07:00:00The named zone gives 08:00 because New York is on daylight time (UTC-4) in July. The abbreviation gives 07:00 because EST is fixed at UTC-5.
To see which names your server accepts, query the pg_timezone_names view. To see the abbreviations, query pg_timezone_abbrevs.
SELECT name, abbrev, utc_offset, is_dst
FROM pg_timezone_names
WHERE name IN ('Europe/Paris', 'America/New_York', 'Asia/Tokyo')
ORDER BY name;The utc_offset and is_dst columns show the current offset for each zone, so the result changes with the date you run it.
Edge cases around daylight saving time
Converting an instant to a wall-clock time is always well defined. Converting a wall-clock time to an instant is not always well defined, because some clock readings are missing or repeated.
- In spring, clocks jump forward. A reading inside the skipped hour does not exist.
- In autumn, clocks go back. A reading inside the repeated hour happens twice.
For timestamp AT TIME ZONE, PostgreSQL still returns one answer in both cases. The behavior is documented but easy to forget:
SET TimeZone = 'UTC';
-- 2024-03-10 02:30 does not exist in New York (clocks jump from 02:00 to 03:00)
SELECT TIMESTAMP '2024-03-10 02:30:00' AT TIME ZONE 'America/New_York' AS skipped;
-- 2024-11-03 01:30 happens twice in New York
SELECT TIMESTAMP '2024-11-03 01:30:00' AT TIME ZONE 'America/New_York' AS repeated;For the skipped reading, PostgreSQL uses the offset from before the change and so lands one hour later on the clock, at 03:30 local time. For the repeated reading, PostgreSQL picks the first occurrence, which is the daylight-time one at UTC-4. Run these on your own server before you rely on them, and if your application accepts user-entered local times, validate them. The safest approach is to store the instant as a timestamptz as soon as you know it, and to record the zone name beside it when you need to know where the event happened.
Common mistakes
- Using it to "set the zone" on a timestamptz.
starts_at AT TIME ZONE 'Asia/Tokyo'does not produce atimestamptzin Tokyo. It produces atimestamp, and the zone is gone. If you only want to display Tokyo time, set the sessionTimeZoneinstead. - Applying it twice by accident. Two uses of the operator on the same value flip the type twice. In a view or a function, an extra
AT TIME ZONE 'UTC'can quietly turn an instant into a wall-clock reading, and then the next comparison against atimestamptzshifts by the session offset. - Comparing a timestamp with a timestamptz. PostgreSQL casts the
timestamptotimestamptzusing the sessionTimeZone. The comparison then changes meaning when a client changes its setting. - Using abbreviations for places.
'PST'is a fixed offset, not "Pacific time". Use'America/Los_Angeles'. - Mixing up the direction in a WHERE clause. To filter on local time, convert the column, as in
starts_at AT TIME ZONE 'Europe/Paris' >= TIMESTAMP '2024-06-01'. To use an index onstarts_at, convert the constants instead, by writingTIMESTAMP '2024-06-01' AT TIME ZONE 'Europe/Paris'on the right-hand side.
The last point matters for performance. A condition that wraps the column in a function or operator cannot use an ordinary index on that column. Moving the conversion to the constant keeps the condition usable:
-- Index-friendly: the column is left alone, the bounds are converted
SELECT *
FROM event
WHERE starts_at >= TIMESTAMP '2024-06-01 00:00:00' AT TIME ZONE 'Europe/Paris'
AND starts_at < TIMESTAMP '2024-07-01 00:00:00' AT TIME ZONE 'Europe/Paris';This is a half-open range, which a later chapter covers in detail.
Functional form
The operator has a function equivalent, timezone(zone, value). The zone comes first, which is the reverse of the operator's reading order.
SELECT timezone('Asia/Tokyo', TIMESTAMPTZ '2024-06-01 07:00:00+00');
-- 2024-06-01 16:00:00The function form is useful in tools that do not accept the operator syntax, such as some index definitions and generated columns.
Summary
timestamptz AT TIME ZONE zgives atimestamp: the wall-clock time in zonez.timestamp AT TIME ZONE zgives atimestamptz: the instant for that wall-clock time in zonez.- Use IANA names, not abbreviations, for real places.
- Skipped and repeated local times around daylight saving changes still produce one answer, so validate local input.
- Keep conversions on constants rather than columns when you want index use.
Exercise
The session TimeZone is 'UTC'. What type does TIMESTAMP '2024-06-01 09:00:00' AT TIME ZONE 'Europe/Paris' AT TIME ZONE 'Asia/Tokyo' return, and what value does it show?
Show answer
It returns a plain timestamp. The first operator reads 09:00 as Paris time (UTC+2 in June), giving the instant 07:00 UTC as a timestamptz. The second operator describes that instant on the Tokyo clock (UTC+9), giving 2024-06-01 16:00:00 with no offset.
Exercise
A table has starts_at timestamptz with an index. You want all rows that fall on 15 March 2024 in Berlin local time. Write a WHERE clause that is correct and can use the index.
Show answer
WHERE startsat >= TIMESTAMP '2024-03-15 00:00:00' AT TIME ZONE 'Europe/Berlin' AND startsat < TIMESTAMP '2024-03-16 00:00:00' AT TIME ZONE 'Europe/Berlin'. The bounds are converted to instants, the column is left unwrapped, and the half-open range avoids double-counting a row at midnight.