Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know a timezone of a timestamp in postgresql 8.3

I'm using postgresql 8.3 and i would like to know the timezone of a particular timestamp (a column in a table).

In the documentation i've found the keyword "timezone"

But i don't understand how to apply it in a column of table. Is it possible ?

like image 831
egesuato Avatar asked Feb 16 '09 16:02

egesuato


People also ask

Does Postgres timestamp have timezone?

Introduction to PostgreSQL timestamp The timestamp datatype allows you to store both date and time. However, it does not have any time zone data. It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically.

How do you read a timestamp with time zones?

To calculate UTC time one has to subtract the offset from the local time, e.g. for "15:00−03:30" do 15:00 − (−03:30) to get 18:30 UTC. Show activity on this post. That timestamp has a timezone offset that is telling you what time it was and the UTC offset. With no offset it becomes 2017-02-03T14:16:59.094-00:00 .

What is timestamp with timezone in PostgreSQL?

The PostgreSQL Timestamp data type is used to store the time and date values for a specified column. We used different TIMESTAMP functions, for example, NOW(), CURRENT_TIMESTAMP, CURRENT_TIME, TIMEOFDAY(), and timezone(zone, timestamp) to enhance and handle the TIME and DATE value from the particular table.


2 Answers

I assume you have a column named ct which has the type TIMESTAMPTZ in the table t. Then you can use:

SELECT EXTRACT(TIMEZONE FROM ct) FROM t;

to get the offset of the timezone in seconds. It that gives you 3600 from UTC/GMT that means either GMT+1, CET or whatever. The value returned depends on your TIMEZONE setting.

Sample (I live in Germany, actual timezone ist GMT+1/CET):

test=# select '2008-01-01 12:00:00 GMT+5'::timestamptz;
      timestamptz       
------------------------
 2008-01-01 18:00:00+01

test=# set timezone to 'gmt';
SET
test=# select '2008-01-01 12:00:00 GMT+5'::timestamptz;
      timestamptz       
------------------------
 2008-01-01 17:00:00+00

As you can see it always outputs anything in the configured timezone. So the offset you will get with EXTRACT(TIMEZONE FROM ...) depends on your TIMEZONE setting. The timezone which was given on INSERT is lost, because it is not worth to be saved. The thing that counts is that everything is correct and that should not depend on the TIMEZONE setting. PostgreSQL does that very well.

like image 165
Johannes Weiss Avatar answered Oct 20 '22 17:10

Johannes Weiss


"PostgreSQL does that very well."

I really like PostgreSQL, but in this particular feature it does not do it well. Timezone is not only offset to GMT. Timezone is tight to political rules that implies daylight saving. As there are plenty of timezones with the same offset and different daylight saving rules - when PG forgets about original timezone it looses the information in fact.

Personally I store original timezone separately for the dates it matters in the form 'America/New_York'. If anybody has better solution - it's welcome.

like image 26
W Strzalka Avatar answered Oct 20 '22 17:10

W Strzalka