postgresqldatetimetimezonepostgresql-9.1rails-postgresql

PostgreSQL date() with timezone


I'm having an issue selecting dates properly from Postgres - they are being stored in UTC, but not converting with the Date() function properly.

Converting the timestamp to a date gives me the wrong date if it's past 4pm PST.

2012-06-21 should be 2012-06-20 in this case.

The starts_at column datatype is timestamp without time zone. Here are my queries:

Without converting to PST timezone:

Select starts_at from schedules where id = 40;

      starts_at      
---------------------
 2012-06-21 01:00:00

Converting gives this:

Select (starts_at at time zone 'pst') from schedules where id = 40;
        timezone        
------------------------
 2012-06-21 02:00:00-07

But neither convert to the correct date in the timezone.


Solution

  • I don't see the exact type of starts_at in your question. You really should include this information, it is the key to the solution. I'll have to guess.

    PostgreSQL always stores UTC time for the type timestamp with time zone internally. Input and output (display) are adjusted to the current timezone setting or to the given time zone. The effect of AT TIME ZONE also changes with the underlying data type. See:

    If you extract a date from type timestamp [without time zone], you get the date for the current time zone. The day in the output will be the same as in the display of the timestamp value.

    If you extract a date from type timestamp with time zone (timestamptz for short), the time zone offset is "applied" first. You still get the date for the current time zone, which agrees with the display of the timestamp. The same point in time translates to the next day in parts of Europe, when it is past 4 p.m. in California for instance. To get the date for a certain time zone, apply AT TIME ZONE first.

    Therefore, what you describe at the top of the question contradicts your example.

    Given that starts_at is a timestamp [without time zone] and the time on your server is set to the local time. Test with:

    SELECT now();
    

    Does it display the same time as a clock on your wall? If yes (and the db server is running with correct time), the timezone setting of your current session agrees with your local time zone. If no, you may want to visit the setting of timezone in your postgresql.conf or your client for the session. Details in the manual.

    Be aware that the timezone offset used the opposite sign of what's displayed in timestamp literals. See:

    To get your local date from starts_at just

    SELECT starts_at::date
    

    Tantamount to:

    SELECT date(starts_at)
    

    BTW, your local time is at UTC-7 right now, not UTC-8, because daylight savings time is in effect (not among the brighter ideas of the human race).

    Pacific Standard TIME (PST) is normally 8 hours "earlier" (bigger timestamp value) than UTC (Universal Time Zone), but during daylight saving periods (like now) it can be 7 hours. That's why timestamptz is displayed as 2012-06-21 02:00:00-07 in your example. The construct AT TIME ZONE 'PST' takes daylight saving time into account. These two expressions yield different results (one in winter, one in summer) and may result in different dates when cast:

    SELECT '2012-06-21 01:00:00'::timestamp AT TIME ZONE 'PST'
         , '2012-12-21 01:00:00'::timestamp AT TIME ZONE 'PST'