timezone difference in timestamp?

Started by meltedownover 19 years ago4 messagesgeneral
Jump to latest
#1meltedown
asdf@fake.com

Short version: I'm trying to turn a unix timestamp into a psql
timestamp, but there is a 5 hour difference. Is this because of
timezones ? Can I just subtract 5 hours to get the right value ?

Detailed version:
I start with a unix timestamp:
$startofday =1162789200;

Which has this date:
Array
(
[seconds] => 0
[minutes] => 0
[hours] => 0
[mday] => 6
[wday] => 1
[mon] => 11
[year] => 2006
[yday] => 309
[weekday] => Monday
[month] => November
[0]: => 1162789200 )
)

Then I make a pgsql timestamp like this:
"select timestamp '1970-01-01' + interval '$startofday seconds' as
timestamp"

And the result is:
timestamp 2006-11-06 05:00:00 timestamp

It seems like it should be 2006-11-06 00:00:00

This seems to work but can I always get the correct value by subtracting
5 hours like this?
"select timestamp '1970-01-01' + interval '$startofday seconds'-interval
'5 hours' as timestamp"

#2Martijn van Oosterhout
kleptog@svana.org
In reply to: meltedown (#1)
Re: timezone difference in timestamp?

On Mon, Nov 06, 2006 at 03:30:35PM +0000, meltedown wrote:

Short version: I'm trying to turn a unix timestamp into a psql
timestamp, but there is a 5 hour difference. Is this because of
timezones ? Can I just subtract 5 hours to get the right value ?

Not sure what you're using, but by my calculations postgresql is
correct.

$ perl -e 'print scalar(gmtime(1162789200)),"\n"'
Mon Nov 6 05:00:00 2006

Make sure you understand whether the dates your comparing are in the
same timezone. Maybe you want 'timestamp with time zone'.

Hope this helps,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

From each according to his ability. To each according to his ability to litigate.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: meltedown (#1)
Re: timezone difference in timestamp?

meltedown <asdf@fake.com> writes:

Short version: I'm trying to turn a unix timestamp into a psql
timestamp, but there is a 5 hour difference. Is this because of
timezones ? Can I just subtract 5 hours to get the right value ?

"select timestamp '1970-01-01' + interval '$startofday seconds' as
timestamp"

If it really is a Unix timestamp --- ie, referenced to midnight GMT
1970-01-01 --- then you need to start with midnight GMT not midnight
local time as the basis. So,

select timestamp with time zone '1970-01-01 00:00 GMT' + interval ...

or better yet

select timestamp with time zone 'epoch' + interval ...

regards, tom lane

#4meltedown
asdf@fake.com
In reply to: Tom Lane (#3)
Re: timezone difference in timestamp?

Tom Lane wrote:

meltedown <asdf@fake.com> writes:

Short version: I'm trying to turn a unix timestamp into a psql
timestamp, but there is a 5 hour difference. Is this because of
timezones ? Can I just subtract 5 hours to get the right value ?

"select timestamp '1970-01-01' + interval '$startofday seconds' as
timestamp"

If it really is a Unix timestamp --- ie, referenced to midnight GMT
1970-01-01 --- then you need to start with midnight GMT not midnight
local time as the basis. So,

select timestamp with time zone '1970-01-01 00:00 GMT' + interval ...

or better yet

select timestamp with time zone 'epoch' + interval ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Thanks, that answered my question. I thought as much, but thanks for
that fine explanation.