문제

I'm trying to wrap my head around Postgresql timezones, and I can't seem to figure this out. EST is "Eastern Standard Time" in America, and is typically UTC-5.

Example 1: Base Test

select '08/31/2011 12:00 pm EST'::timestamptz  at time zone 'EST';
      timezone       
---------------------
 2011-08-31 12:00:00

Example 2: Offset is +5

select '08/31/2011 12:00 pm EST' at time zone '+5';
      timezone       
---------------------
 2011-08-31 12:00:00

Example 3: Offset is -5

 select '08/31/2011 12:00 pm EST' at time zone '-5';
      timezone       
---------------------
 2011-08-31 22:00:00

Clearly, everything is backwards. EST is again... supposed to be UTC-5. Now, I did search through documentation, and it does explain that things are "POSIX", which is backwards. (Positive offset is west of GMT, while negative offsets are east of GMT).

However, how do I get around this? At the application layer, I can always invert the + sign to a - sign, but that seems a bit messy to me IMO. Thus, my ultimate question.

At the database layer (Postgres), is there a way to use the "At Time Zone" syntax so that GMT-5 corresponds to EST? Or do I just have to invert everything at the application layer?

도움이 되었습니까?

해결책

Use interval datatype as written in documentation to get proper behaviour:

In these expressions, the desired time zone zone can be specified either as a text string (e.g., 'PST') or as an interval (e.g., INTERVAL '-08:00').

Base test:

SELECT '08/31/2011 12:00 pm EST'::timestamptz AT TIME ZONE 'EST';
      timezone       
---------------------
 2011-08-31 12:00:00
(1 row)

Timezone info:

SELECT * FROM pg_timezone_abbrevs WHERE abbrev LIKE 'EST';
 abbrev | utc_offset | is_dst 
--------+------------+--------
 EST    | -05:00:00  | f
(1 row)

Proper offset -5:

SELECT '08/31/2011 12:00 pm EST'::timestamptz AT TIME ZONE '-05:00'::interval;
      timezone       
---------------------
 2011-08-31 12:00:00
(1 row)

Proper offset +5:

SELECT '08/31/2011 12:00 pm EST'::timestamptz AT TIME ZONE '+05:00'::interval;
      timezone       
---------------------
 2011-08-31 22:00:00
(1 row) 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top