Re: convert in GMT time zone without summer time - Mailing list pgsql-sql

From Jasen Betts
Subject Re: convert in GMT time zone without summer time
Date
Msg-id ioc0gr$53p$1@reversiblemaps.ath.cx
Whole thread Raw
In response to convert in GMT time zone without summer time  (LaraK <indarija@gmx.net>)
Responses Re: convert in GMT time zone without summer time  (Steve Crawford <scrawford@pinpointresearch.com>)
List pgsql-sql
On 2011-04-15, LaraK <indarija@gmx.net> wrote:
> Hello,
>
> I want write a function that converts a timestamp with time zone to the UTC
> zone. But it should all be stored in the winter time.
>
> For example, it must now, in the summer, the German time back by 2 hours and
> in the winter time only 1 hour. But it expects only back one hour.
>
> Is there a function or a specific time zone?

if I undestand your goal correctly you want to subtract the daylight
savings offset from the given timezone if daylight-savings is in use
in the current time locale.

you can detect daylight-savings by setting testing the timezone offset
at 3 month intervals ( timestamp, timestamp+3months timestamp-3months,
timestamp+6months, timestamp-6months)

the one(s) of them with the least (most negative) offset from UTC will
represent non daylight-saving time.

if your given time has a different offset it's daylight saving time,
add the difference.
calling:
> [CODE]
> SELECT                                    
> to_char(CONVERT_TO_UTC(to_timestamp('2011-03-22 14:17:00', 'YYYY-MM-DD
> hh24:MI:SS'), 'CET'), 'yyyy-mm-dd hh24:MI:SS') AS winter,
> to_char(CONVERT_TO_UTC(to_timestamp('2011-04-22 14:17:00', 'YYYY-MM-DD
> hh24:MI:SS'), 'CET'), 'yyyy-mm-dd hh24:MI:SS') AS summer
> [/CODE]
>
> must come out:
> [CODE]
> WINTER                | SUMMER
> --------------------+-------------------------
> 2011-03-22 13:17:00 | 2011-04-22 12:17:00
> [/CODE]

that test case is ambiguous your inputs are timespamptz but 
have an unspecified timezone (and so get the zone appropriate to 
your time locale). I'm assuming your time locale is "Europe/Berlin" 
and you really mean the following:

SELECT to_char(CONVERT_TO_UTC( '2011-03-22 14:17:00+01'::timestamptz
,'CET'),'yyyy-mm-dd hh24:MI:SS') AS winter, to_char(CONVERT_TO_UTC(
'2011-04-22 14:17:00+02'::timestamptz ,'CET'),'yyyy-mm-dd hh24:MI:SS')
AS summer;

CREATE OR REPLACE FUNCTION  CONVERT_TO_UTC ( timestamptz,  text) returns timestamp as  $$ SELECT $1 at time zone 'UTC';
$$ language sql;
 
In that this function does not use the second parameter it may not be
what you want, on the other hand it's function matches it's name well.
what are you trying to do?

-- 
⚂⚃ 100% natural



pgsql-sql by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: update with recursive query
Next
From: Jasen Betts
Date:
Subject: Re: Get id of a tuple using exception