Re: displaying UTC time in local time

Поиск
Список
Период
Сортировка
От mark_postgres_user
Тема Re: displaying UTC time in local time
Дата
Msg-id 316CEBF131EA6A45A20643EF767F435424AB2D@FEMAIL2A.FireEye.com
обсуждение исходный текст
Ответ на Re: displaying UTC time in local time  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-admin
Tom, thanks for your reply below.  It was very helpful in getting my mind straight about with and without timezones.  Thanks again.

Mark


From: Tom Lane-2 [via PostgreSQL] [ml-node+[hidden email]]
Sent: Tuesday, October 29, 2013 11:37 PM
To: Mark Ikemoto
Subject: Re: displaying UTC time in local time

mark_postgres_user <[hidden email]> writes:
> To recap:  So, because my timestamps are defined as timestamp without
> timezone data type and are stored in UTC, when I display them in their
> default form they'll be displayed in UTC.  I can use AT TIME ZONE to convert
> the displayed time to any timezone including my local timezone.

> So why is it that I can get the displayed time to be local only if I use "AT
> TIMEZONE 'UTC'" ?  Anything else will either be ignored or display UTC.

When applied to a timestamp-without-timezone, AT TIME ZONE means "convert
argument 1 to timestamp-with-timezone, assuming that it represents local
time in the timezone named by argument 2".  So if your stored values do
in fact represent UTC times, the *only* correct application of AT TIME
ZONE to them is "AT TIME ZONE 'UTC'"; anything else will generate a wrong
ts-with-tz value.

Once you have a correct value, the output function for datatype
timestamp-with-timezone automatically takes care of displaying it in
the zone defined by the timezone parameter (which I assume you've got
set to whatever you think local time is).

If you really want to do all this manually, you could do something like

   SELECT (ts-without-tz-value AT TIME ZONE 'UTC') AT TIME ZONE 'foo'

which will first convert the ts-without-tz value to ts-with-tz under the
assumption that it represents local time in UTC, and then convert the
ts-with-tz value back to ts-without-tz, producing the correct local time
in zone 'foo'.  Since it's now ts-without-tz, it'll be displayed as-is,
without any consultation of the timezone parameter.

But TBH it seems clear to me that you did this wrong.  The way you are
thinking about this column says that you should be storing it as
ts-with-tz.  Then it'll automatically be displayed in your local zone,
and if you want to see it in some other zone, a single application of
AT TIME ZONE will do that for you.

                        regards, tom lane


--
Sent via pgsql-admin mailing list ([hidden email])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



If you reply to this email, your message will be added to the discussion below:
http://postgresql.1045698.n5.nabble.com/displaying-UTC-time-in-local-time-tp5776169p5776337.html
To unsubscribe from displaying UTC time in local time, click here.
NAML

______________________________________________________________________
This email and any attachments thereto may contain private, confidential, and/or privileged material for the sole use of the intended recipient. Any review, copying, or distribution of this email (or any attachments thereto) by others is strictly prohibited. If you are not the intended recipient, please contact the sender immediately and permanently delete the original and any copies of this email and any attachments thereto.
______________________________________________________________________


View this message in context: RE: displaying UTC time in local time
Sent from the PostgreSQL - admin mailing list archive at Nabble.com.

В списке pgsql-admin по дате отправления:

Предыдущее
От: mark_postgres_user
Дата:
Сообщение: Re: displaying UTC time in local time
Следующее
От: Christian Ullrich
Дата:
Сообщение: Re: Connecting to Oracle from Postgresql 9.2 on Windows