Thread: Question about timezones

Question about timezones

From
Martijn van Oosterhout
Date:
Hi,

The operating system I run (Linux) comes with many, many timezone files
for many different places in the world. For example:

$ TZ='Australia/Sydney' date
Fri Oct  8 06:15:31 EST 2004
$ TZ='Europe/Amsterdam' date
Thu Oct  7 22:15:38 CEST 2004
$ TZ='Africa/Bissau' date
Thu Oct  7 20:18:44 GMT 2004
$ TZ='America/Phoenix' date
Thu Oct  7 13:19:33 MST 2004

Is there any way I can use these from within postgresql? Those files
contains details about daylight saving changes and other useful details
like that, which a simple PST or EST won't cover. Or should I simply do
all my date/time conversion in my application?

Any ideas?
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment

Re: Question about timezones

From
Steven Klassen
Date:
* Martijn van Oosterhout <kleptog@svana.org> [2004-10-07 22:22:24 +0200]:

> Is there any way I can use these from within postgresql? Those files
> contains details about daylight saving changes and other useful
> details like that, which a simple PST or EST won't cover. Or should
> I simply do all my date/time conversion in my application?

The time zone support seems pretty exhaustive. Check out section B-r
in the document below.

http://www.postgresql.org/docs/7.4/static/datetime-keywords.html

HTH,

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

Re: Question about timezones

From
Martijn van Oosterhout
Date:
On Thu, Oct 07, 2004 at 01:43:49PM -0700, Steven Klassen wrote:
> * Martijn van Oosterhout <kleptog@svana.org> [2004-10-07 22:22:24 +0200]:
>
> > Is there any way I can use these from within postgresql? Those files
> > contains details about daylight saving changes and other useful
> > details like that, which a simple PST or EST won't cover. Or should
> > I simply do all my date/time conversion in my application?
>
> The time zone support seems pretty exhaustive. Check out section B-r
> in the document below.
>
> http://www.postgresql.org/docs/7.4/static/datetime-keywords.html

But it doesn't seem to work to actually work out times across the
world w.r.t. daylight savings.

For example, this script works out, given a time in one timezone, what
it was in another timezone:

$ sh /tmp/translatetz '2004-12-01 12:0:0' Australia/Sydney Europe/Amsterdam
Wed Dec 1 02:00:00 2004
$ sh /tmp/translatetz '2004-08-01 12:0:0' Australia/Sydney Europe/Amsterdam
Sun Aug 1 04:00:00 2004

But Brisbane doesn't have summer time, so:

$ sh /tmp/translatetz '2004-12-01 12:0:0' Australia/Brisbane Europe/Amsterdam
Wed Dec 1 03:00:00 2004

The closest I've been able to get is:

kleptog=# select timezone('MEWT',timezone('AESST','2004-12-01 12:0:0'::timestamp));
      timezone
---------------------
 2004-12-01 02:00:00
(1 row)

kleptog=# select timezone('MEST',timezone('AEST','2004-08-01 12:0:0'::timestamp));
      timezone
---------------------
 2004-08-01 04:00:00
(1 row)

In other words, if I work out myself the appropriate timezones then it
can do it. In that case I may just as well do it all myself. Mind you,
this is 7.3, would a more recent version handle this differently?

--- translatetz ---
#!/bin/sh
X=`TZ=$2 date --date="$1" +%s`
TZ=$3 perl -e 'print scalar(localtime(shift))' $X
--- snip ---
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment

Re: Question about timezones

From
Tom Lane
Date:
Martijn van Oosterhout <kleptog@svana.org> writes:
> But it doesn't seem to work to actually work out times across the
> world w.r.t. daylight savings.
> ...
> For example, this script works out, given a time in one timezone, what
> it was in another timezone:

What we need for that is the ability for AT TIME ZONE to specify a
DST-aware zone name.  Right now it can only take DST-ignorant zone
names.  So you can do
    ('2004-12-01 12:0:0' AT TIME ZONE 'AESST') AT TIME ZONE 'MEWT'
but not
    ('2004-12-01 12:0:0' AT TIME ZONE 'Australia/Sydney') AT TIME ZONE 'Europe/Amsterdam'
which of course is what you want.

The infrastructure needed for this is finally present in 8.0, ie we have
the timezone data available, but actually teaching AT TIME ZONE about it
didn't get done in time.  Likely it will appear in 8.1 (especially if
you step up and do the work ;-)).

            regards, tom lane

Re: Question about timezones

From
Martijn van Oosterhout
Date:
On Fri, Oct 08, 2004 at 09:52:00AM -0400, Tom Lane wrote:
> The infrastructure needed for this is finally present in 8.0, ie we have
> the timezone data available, but actually teaching AT TIME ZONE about it
> didn't get done in time.  Likely it will appear in 8.1 (especially if
> you step up and do the work ;-)).

I've had a look at the code that is in CVS and it looks like everything
needed is basically there. I think what's basically needed is a system
to keep track of tzname => struct state mappings, probably a hash of
some sort.

I'd consider creating a timezone preserving type, but it's not
necessary for what I'm doing. Is that library already built into 8.0?
If that's the case there is an opportunity to create a contrib module
that hooks into it.

Hmm...
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment