Sub topic 4
Time Zone Names, Abbreviations, and Offsets
This chapter assumes that you know basic SELECT, INSERT, and WHERE queries. You should also know that UTC is a global reference time and that a UTC offset describes a difference from UTC.
PostgreSQL accepts three common ways to identify a time zone:
- A named zone, such as
America/New_York. - An abbreviation, such as
ESTorUTC. - A numeric offset, such as
-05:00or+02:00.
These forms do not carry the same information.
Named time zones
A named time zone comes from the IANA time zone database. Names such as America/New_York describe a region whose rules can change during the year. Those rules include daylight saving time, historical changes, and future updates.
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-07-01 12:00:00+00' AS summer_time,
TIMESTAMPTZ '2024-01-01 12:00:00+00' AS winter_time;
-- summer_time: 2024-07-01 08:00:00-04
-- winter_time: 2024-01-01 07:00:00-05The same named zone uses different offsets at different times. The name is therefore better than storing -05:00 when you need local calendar behavior.
You can inspect the names available in your PostgreSQL installation:
SELECT name, abbrev, utc_offset, is_dst
FROM pg_timezone_names
WHERE name IN ('America/New_York', 'Europe/Paris', 'UTC');utc_offset is the offset currently associated with the listed zone. is_dst indicates whether daylight saving time is active for that row at the relevant current date.
Abbreviations and fixed offsets
An abbreviation is short, but it can be ambiguous. EST commonly means UTC−05:00 in North America. CST can refer to several different regional conventions. PostgreSQL resolves abbreviations using its time zone abbreviation configuration, not by asking which country you intended.
A numeric offset is unambiguous for one instant:
SELECT TIMESTAMPTZ '2024-06-01 09:00:00-04' AS by_offset,
TIMESTAMPTZ '2024-06-01 09:00:00 America/New_York' AS by_name;Both values identify the same instant on that date. However, -04:00 does not mean “New York.” It means only four hours behind UTC. It will remain that offset in winter, while New York changes to -05:00.
Choosing a form
| Form | Example | Daylight saving rules |
|---|---|---|
| Named zone | `Europe/Paris` | Applied automatically |
| Abbreviation | `CET` | Depends on configured meaning |
| Numeric offset | `+01:00` | Never changes by itself |
A timestamptz stores an instant, not the original name or offset text. PostgreSQL uses the session TimeZone setting when it displays that instant. Therefore, if an application must remember a user's preferred region, store that region separately, for example in a text column containing America/New_York.
Exercise
Which should you store for a user's appointment region: -05:00 or America/New_York?
Show answer
Store America/New_York. It preserves the regional daylight saving rules. Store -05:00 only when a fixed offset is the actual requirement.