Re: Daylight Savings Time handling on persistent connections

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Randall Nortman <postgreslists(at)wonderclown(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Daylight Savings Time handling on persistent connections
Date: 2004-11-02 22:48:21
Message-ID: 200411021448.21907.scrawford@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sunday 31 October 2004 11:44 am, Tom Lane wrote:
> Randall Nortman <postgreslists(at)wonderclown(dot)com> writes:
> > Ah, I see now. PostgreSQL is behaving a bit differently than I
> > expected. The timestamp string above is ambiguous in the
> > timezone US/Eastern -- it could be EST or EDT. I was expecting
> > PostgreSQL to resolve this ambiguity based on the current time
> > when the SQL statement is processed
>
> I think this would be a very bad thing for it to do. It might seem
> to make sense for a timestamp representing "now", but as soon as
> you consider a timestamp that isn't "now" it becomes a sure way to
> shoot yourself in the foot.

Would it help to add the PG locale TZ to the insert statement? For
example the following queries return the TZ as text.

select to_char(now(),'tz');
to_char
---------
pst

select to_char(now()-'3 days'::interval,'tz');
to_char
---------
pdt

So the following might fix this particular situation:
insert into sensor_readings_numeric (...) values (...,'2004-10-31
01:00:00 ' || to_char(now(),'tz'),...)

I realize that it assumes that the data is being inserted at the time
it was taken so a reading taken just before DST changes and inserted
just after will be incorrect but it may work for this particular app.

Of course the better solution is to have the application generate a
fully-qualified timestamp with time zone. Generating all the
timestamps in UTC and explicitly specifying that in the insert is
probably the easiest way to go. Your queries will still have your
local-appropriate TZ:

select '2004-10-31 00:00:00+00'::timestamptz;
timestamptz
------------------------
2004-10-30 17:00:00-07

select '2004-11-01 00:00:00+00'::timestamptz;
timestamptz
------------------------
2004-10-31 16:00:00-08

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Steve Crawford 2004-11-02 23:30:10 Batch processing select
Previous Message Alvaro Herrera 2004-11-02 21:24:59 Re: Calling on all SQL guru's