Question

I am building an application that needs to handle different timezones, including timezones with daylight savings time. I am storing all the dates/times using Postgressql's timestamp with timezone data type, and all are stored in the UTC timezone (I modified the timezone entry in postgresql.conf).

Here is an example table that I have created:

CREATE TABLE category_city
(
category_city_id serial NOT NULL,
appt_start_time timestamp(0) with time zone,
appt_end_time timestamp(0) with time zone,
appt_days_of_week character varying,
CONSTRAINT category_city_pkey PRIMARY KEY (category_city_id )
);

I have been testing some conversions using the 'America\Edmonton' timezone. Currently, this timezone is 6 hours behind UTC. I have a timestamp in this table that has the value 1970-01-01 15:00:00+00.

Now I perform this query:

SELECT appt_start_time at time zone 'America/Edmonton' as start
FROM category_city 

This should give me the timestamp 1970-01-01 09:00:00+00, but instead it gives me the timestamp 1970-01-01 08:00:00+00, an offset of -7. which is the correct offset when not in DST, but is obviously not correct right now (DST is in effect).

I must be missing something because I'm sure I'm not the only one that needs to handle different timezones with DST.

The time on my server is correct, this is the output of the date command on Ubuntu:

Wed Apr 30 17:11:59 MDT 2014

Can anyone see something that I am overlooking, or has any experienced something similar and found a way to workaround it? Any help would be appreciated!

Was it helpful?

Solution

At 1970-01-01 09:00:00+00 the time at the timezone 'America/Edmonton' was 1970-01-01 08:00:00 regardless of when you ask. If you want the time at a specific timezone you need to make it explicit:

select '1970-01-01 15:00:00+00' at time zone 'MST' as start;
        start        
---------------------
 1970-01-01 08:00:00

select '1970-01-01 15:00:00+00' at time zone 'MDT' as start;
        start        
---------------------
 1970-01-01 09:00:00

I guess it is easy to see above why that time at the MDT timezone will always be the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top