Time Zone Query

Started by Blake Crosbyover 25 years ago3 messagesgeneral
Jump to latest
#1Blake Crosby
me@blakecrosby.com

I have a (hopefully) quick question about the timestamp type and time zones.

Currently the database resides on a box in time zone with offset -05.
Inserts and Selects are being done from time zone +11.

Currently in the table the dates/times are stored with the -05 time zone
information attached:

date
------------------------
2001-01-01 08:42:24-05

I can get the correct date/time for my local time zone (+11) by using SET
TIMEZONE TO 'Australia/Sydney' . What I would like to know, am I able to
store the date/time in the database with the correct time zone offset (+11)
and not (-05)? ie, so it looks like this:

date
------------------------
2001-01-02 12:42:24+11

I want to be able to select the date from the table and not have to worry
about applying the proper time zone offset by using SET TIMEZONE TO.

Regards,
Blake Crosby.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Blake Crosby (#1)
Re: Time Zone Query

Blake Crosby <me@blakecrosby.com> writes:

I have a (hopefully) quick question about the timestamp type and time zones.
Currently the database resides on a box in time zone with offset -05.
Inserts and Selects are being done from time zone +11.

Currently in the table the dates/times are stored with the -05 time zone
information attached:

No, the times in the database are all GMT. They will get *displayed*
with some particular timezone offset, as determined by the TIMEZONE
SET variable.

I can get the correct date/time for my local time zone (+11) by using SET
TIMEZONE TO 'Australia/Sydney' . What I would like to know, am I able to
store the date/time in the database with the correct time zone offset (+11)
and not (-05)? ie, so it looks like this:

You have a fundamental misconception here...

I want to be able to select the date from the table and not have to worry
about applying the proper time zone offset by using SET TIMEZONE TO.

You have to set TIMEZONE to your local timezone if you want dates
displayed in something other than the server's default timezone.
Perhaps what you are really looking for is the PGTZ environment
variable. If you keep PGTZ set in your environment, libpq will issue
the SET TIMEZONE command for you during connection startup.

regards, tom lane

#3Blake Crosby
me@blakecrosby.com
In reply to: Tom Lane (#2)
Re: Time Zone Query

At 12:09 PM 4/01/2001 -0500, you wrote:

No, the times in the database are all GMT. They will get *displayed*
with some particular timezone offset, as determined by the TIMEZONE
SET variable.

You have to set TIMEZONE to your local timezone if you want dates
displayed in something other than the server's default timezone.
Perhaps what you are really looking for is the PGTZ environment
variable. If you keep PGTZ set in your environment, libpq will issue
the SET TIMEZONE command for you during connection startup.

regards, tom lane

After further investigation it turns out I am looking for a timestamp and
timetz hybrid. Possibly timestamptz ? :)

My solution has been to store the timezone offset in a int4 field, then
apply that offset when selecting data from my timestamp type fields.

Regards,
Blake Crosby