Can you recover the full timezone name from a Postgresql TIMESTAMP WITH TIME ZONE field?

StackOverflow https://stackoverflow.com/questions/21367180

Вопрос

Take the following example:

create temporary table t1 (c1 timestamp with time zone);
insert into t1 values ('2003-04-12 04:05:06 America/New_York'::timestamp with time zone);

If I'm reading the documentation correctly, here Postgresql will use the full timezone name to convert the timestamp to UTC by adding +05:00 hours, then store that.

But if that's true then I can't distinguish between 2003-04-12 04:05:06 America/New_York and 2003-04-12 04:05:06 America/Panama, which has the same UTC offset but a different daylight savings offset.

Is that right?

Это было полезно?

Решение

PostgreSQL doesn't store original time zone. You can do it as separate column when you need it.

http://postgres.cz/wiki/PostgreSQL_SQL_Tricks_III#Domain_for_time_zone

CITEXT is case insensitive text type from PostgreSQL contrib package. You can use a text instead if you don't want to install it.

Другие советы

While it is true that once stored there is no way to get to the original named timezone. I believe when a timestamp with timezone is stored it is converted on the way in to postgres to timestamp and offset +- HHMM, including daylight savings for the timezone. I did an experiment:

create temporary table t1 (c1 timestamp with time zone);
insert into t1 values ('2003-04-12 04:05:06 America/New_York'::timestamp with time zone);
insert into t1 values ('2003-04-12 04:05:06 America/Panama'::timestamp with time zone);


nsp=# select c1 at time zone 'utc' from t1;
      timezone       
---------------------
 2003-04-12 08:05:06
 2003-04-12 09:05:06
(2 rows)

Those appear to be correct to me. In 2003, Panama is UTC-5, no daylight savings. In 2003 America/New_York subtracts an hour starting on April 6 (UTC-4). So, the times are correct, and you can tell the difference between the two.

-g

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top