Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres UTC date format & epoch cast, sign inversion

Could someone explain me this sign inversion, i'm lost here...

SELECT EXTRACT(EPOCH FROM '01-01-1970 00:00:00 UTC+01'::timestamp with time zone)

=> 3600

SELECT EXTRACT(EPOCH FROM '1970-01-01 00:00:00+01'::timestamp with time zone)

=> -3600

Postgres 8.3.14

like image 314
131 Avatar asked Dec 06 '11 11:12

131


1 Answers

This

1970-01-01 00:00:00+01

is an ISO 8601 timestamp with a +1 hour offset and +1 means east of Greenwich. The offsets in these

01-01-1970 00:00:00 UTC+01
1970-01-01 00:00:00 UTC+01
1970-01-01 00:00:00 XXX+01
1970-01-01 00:00:00 HAHA+01
1970-01-01 00:00:00 Pancakes+01

will be interpreted as POSIX style timezones where +1 means west of Greenwich:

PostgreSQL will accept POSIX-style time zone specifications of the form STDoffset or STDoffsetDST, where STD is a zone abbreviation, offset is a numeric offset in hours west from UTC

and those even come with a warning:

One should be wary that the POSIX-style time zone feature can lead to silently accepting bogus input, since there is no check on the reasonableness of the zone abbreviations. For example, SET TIMEZONE TO FOOBAR0 will work, leaving the system effectively using a rather peculiar abbreviation for UTC. Another issue to keep in mind is that in POSIX time zone names, positive offsets are used for locations west of Greenwich. Everywhere else, PostgreSQL follows the ISO-8601 convention that positive timezone offsets are east of Greenwich.

Note the west versus east difference.

like image 81
mu is too short Avatar answered Sep 18 '22 19:09

mu is too short