Re: why does extract ( HOUR FROM TIMESTAMP '2010-01-01 00:00:00' at time zone 'utc' ) == 16?

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Cc: bubba postgres <bubba(dot)postgres(at)gmail(dot)com>
Subject: Re: why does extract ( HOUR FROM TIMESTAMP '2010-01-01 00:00:00' at time zone 'utc' ) == 16?
Date: 2011-03-18 18:04:47
Message-ID: 4D839EBF.9080703@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 03/18/2011 10:17 AM, bubba postgres wrote:
> Thank you for your thorough reply. It will take some time to digest
> your advice, but yes, I am specifically trying to avoid all TZ issues
> by using UTC everywhere all the time. My assumption was that Timestamp
> without timezone meant UTC, guess not.
>
> Regards,
> -JD
>

If you need to deal with multiple time zones, you can't avoid TZ issues.
But be aware that regardless of how you specify a point in time
(timestamp with time zone), PostgreSQL stores it internally at UTC and
that point in time can be displayed in any time zone you wish.

create table tzexamp (mytimestamp timestamptz);

-- The following are equivalent (based on my default timezone of
US/Pacific):
insert into tzexamp values (timestamptz '2010-01-01 00:00:00');
insert into tzexamp values (timestamptz '2010-01-01 08:00:00-00');
insert into tzexamp values (timestamptz '2010-01-01 03:00:00 EST5EDT');
insert into tzexamp values (abstime(1262332800));
insert into tzexamp values (timestamptz 'January 1 02:00:00 2010
posix/America/Chicago');
set timezone to 'Asia/Macao';
insert into tzexamp values (timestamptz '2010-01-01 16:00:00');
set timezone to default;

select * from tzexamp;
mytimestamp
------------------------
2010-01-01 00:00:00-08
2010-01-01 00:00:00-08
2010-01-01 00:00:00-08
2010-01-01 00:00:00-08
2010-01-01 00:00:00-08
2010-01-01 00:00:00-08

But for the client connecting from Japan:

set timezone to 'Asia/Tokyo';

select * from tzexamp;
mytimestamp
------------------------
2010-01-01 17:00:00+09
2010-01-01 17:00:00+09
2010-01-01 17:00:00+09
2010-01-01 17:00:00+09
2010-01-01 17:00:00+09
2010-01-01 17:00:00+09

Or, of course, GMT:

set timezone to 'UTC';

select * from tzexamp;
mytimestamp
------------------------
2010-01-01 08:00:00+00
2010-01-01 08:00:00+00
2010-01-01 08:00:00+00
2010-01-01 08:00:00+00
2010-01-01 08:00:00+00
2010-01-01 08:00:00+00

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2011-03-18 18:09:47 Re: SOCK_wait_for_ready function call caused a query to get stuck
Previous Message Tomas Vondra 2011-03-18 17:30:49 Re: query taking much longer since Postgres 8.4 upgrade