AT TIME ZONE Converts Both Directions
The same operator turns timestamptz into a zone local wall clock and turns a bare timestamp into an instant, with DST handled by IANA rules.
Code
-- AT TIME ZONE 双向 converts:
-- 作用于 timestamptz -> 得到该时区的本地挂钟时间(timestamp)
-- 作用于 timestamp -> 得到"这个本地时间在该时区"的 timestamptz
SELECT
'2026-06-01 12:00:00+00'::timestamptz
AT TIME ZONE 'Asia/Shanghai' AS shanghai_local,
'2026-06-01 12:00:00'::timestamp
AT TIME ZONE 'Asia/Shanghai' AS shanghai_instant;
-- 夏令时边界由数据库按 IANA 规则换算,不是固定偏移的数学
SELECT
'2026-07-04 12:00:00+00'::timestamptz
AT TIME ZONE 'America/New_York' AS ny_summer_local,
'2026-01-04 12:00:00+00'::timestamptz
AT TIME ZONE 'America/New_York' AS ny_winter_local;
-- 常见用法:存储统一 UTC,展示层按用户时区 converts
SELECT event_id, placed_at AT TIME ZONE 'Europe/Berlin' AS user_local_time
FROM events
ORDER BY placed_at
LIMIT 10;What the code does
The file runs SELECT statements, touching events.
Mechanically, the statements are built from: an ORDER BY that fixes the output order; a row cap (LIMIT) bounding the result size.
Applying it
Run the statement against the events table; the LIMIT keeps the probe cheap while you tune the predicates.
Statements that matter
SELECT
'2026-07-04 12:00:00+00'::timestamptz
AT TIME ZONE 'America/New_York' AS ny_summer_local,
'2026-01-04 12:00:00+00'::timestamptz
AT TIME ZONE 'America/New_York' AS ny_winter_local;
SELECT
'2026-06-01 12:00:00+00'::timestamptz
AT TIME ZONE 'Asia/Shanghai' AS shanghai_local,
'2026-06-01 12:00:00'::timestamp
AT TIME ZONE 'Asia/Shanghai' AS shanghai_instant;
SELECT event_id, placed_at AT TIME ZONE 'Europe/Berlin' AS user_local_time
FROM events
ORDER BY placed_at
LIMIT 10;
Interactive Tools for This Topic
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.
FAQ
- Name the tables involved in this file.
- The references are `events`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.