![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
so, how behaves the function extract with epoch parameter related with timezones? I have doubts about it. |
#3
| |||
| |||
|
|
which all seems reasonable. So I would expect that your final example is working properly, but since it gives the same result as your timezone-less tests, perhaps your server's default time zone is at +02? -- David |
#4
| ||||||||
| ||||||||
|
|
which all seems reasonable. So I would expect that your final example is working properly, but since it gives the same result as your timezone-less tests, perhaps your server's default time zone is at +02? -- David Hi, I am definitely on TZ +02, the doubt it is about the way in which PG parses strings as timestamps, it seems that it parses timestamps always in UTC, not caring about the actual time zone: |
|
db_atm=# select current_timestamp; now ------------------------------ 2009-06-26 10:05:57.46624+02 (1 row) |
|
db_atm=# select extract(timezone from current_timestamp); date_part ----------- 7200 (1 row) db_atm=# select extract (epoch from current_timestamp); date_part ------------------ 1246003597.63392 (1 row) db_atm=# select extract (epoch from timestamp '2009-06-26 10:05:57.46624+02'); date_part ------------------ 1246003557.46624 (1 row) |
|
db_atm=# select extract (epoch from timestamp with time zone '2009-06-26 10:05:57.46624+02'); date_part ------------------ 1246003557.46624 (1 row) |
|
db_atm=# select extract (epoch from timestamp with time zone '2009-06-26 10:05:57.46624'); date_part ------------------ 1246003557.46624 (1 row) |
|
db_atm=# select extract (epoch from timestamp '2009-06-26 10:05:57.46624'); date_part ------------------ 1246003557.46624 (1 row) |
|
as you can see the output from epoch is alway the same, in order to export the epoch to JSON I had to write this SP: |
|
(...) db_atm=# select extract(epoch from current_timestamp); date_part ------------------ 1246004613.92817 (1 row) db_atm=# select utcepoch(current_timestamp); utcepoch --------------- 1246011815928 (1 row) db_atm=# select epoch(current_timestamp); epoch --------------- 1246004617439 (1 row) it seems that epoch it is always computed without regard to TZ. |
#5
| |||
| |||
|
|
Coniglio Sgabbiato <nobody (AT) nowhere (DOT) it> writes: Hi, I am definitely on TZ +02, the doubt it is about the way in which PG parses strings as timestamps, it seems that it parses timestamps always in UTC, not caring about the actual time zone: It only ignores the time zone in an input string if it believes it is parsing a string for a data type without a time zone (see again section 8.5.1.3 in the docs). As long as you include a type specification that includes a time zone it will parse what you supply (as I believe my examples in the prior post showed). |
|
as you can see the output from epoch is alway the same, in order to export the epoch to JSON I had to write this SP: |
|
Sure, if you manually adjust that date by some value, it will then represent a different instance in time, and the epoch value will change. That may or may not be appropriate for your specific application, so can't say if it's right or wrong. But I do believe that the epoch query itself is doing the right thing, and accurately represents the time delta from the epoch to whatever instant in time you are supplying to the query. |
#6
| |||
| |||
|
|
David Bolen wrote: Coniglio Sgabbiato <nobody (AT) nowhere (DOT) it> writes: Hi, I am definitely on TZ +02, the doubt it is about the way in which PG parses strings as timestamps, it seems that it parses timestamps always in UTC, not caring about the actual time zone: It only ignores the time zone in an input string if it believes it is parsing a string for a data type without a time zone (see again section 8.5.1.3 in the docs). As long as you include a type specification that includes a time zone it will parse what you supply (as I believe my examples in the prior post showed). I guess that Coniglio (BTW, is this your real first name??) |

|
that PostgreSQL internally converts a timestamp with time zone to UTC and on output always converts it to the local time zone, so that the time zone information is not preserved. |
|
Compare the following surprising results (this is 8.4): test=> SHOW timezone; TimeZone --------------- Europe/Vienna (1 row) This is UTC+02 in summer. test=> SELECT EXTRACT(TIMEZONE FROM (timestamp with time zone '2009-06-26 10:05:57.46624+11')); date_part ----------- 7200 (1 row) test=> SELECT EXTRACT(HOUR FROM (timestamp with time zone '2009-06-26 10:05:57.46624+11')); date_part ----------- 1 (1 row) 7200 = 2*3600 is my local time tone. The gut reaction is "what the **** is going on here", but an EXPLAIN will show it: test=> EXPLAIN VERBOSE SELECT EXTRACT(TIMEZONE FROM (timestamp with time zone '2009-06-26 10:05:57.46624+11')); QUERY PLAN ------------------------------------------------------------------------ Result (cost=0.00..0.01 rows=1 width=0) Output: date_part('timezone'::text, '2009-06-26 01:05:57.46624+02'::timestamp with time zone) (2 rows) So the timestamp is immediately converted to my local time zone. as you can see the output from epoch is alway the same, in order to export the epoch to JSON I had to write this SP: [...] Sure, if you manually adjust that date by some value, it will then represent a different instance in time, and the epoch value will change. That may or may not be appropriate for your specific application, so can't say if it's right or wrong. But I do believe that the epoch query itself is doing the right thing, and accurately represents the time delta from the epoch to whatever instant in time you are supplying to the query. Epoch will be the seconds between 1970-01-01 00:00:00 UTC and the timestamp. What does Coniglio's function "utcepoch" do? As we have seen above, the "extract(timezone from ....)" will always return 7200 for him. So the function always returns for the timestamp + 7200 seconds, no matter what the time zone of the supplied timestamp is. In other words, "utcepoch" will return the number of milliseconds between midnight of 1.1.1970 in the timezone of Coniglio's server and the supplied timestamp. |
#7
| |||
| |||
|
|
I guess that Coniglio (BTW, is this your real first name??) no, it is some sort of funny/moron nickname, my real name is the one you read now ![]() |
|
so, it seems that: db_atm=# select current_timestamp; now ------------------------------- 2009-07-06 11:05:52.618695+02 (1 row) retrieves the current UTC time add up it the 2 hours of current TZ and write out this time with the indication "+02" to make you understand that the local TZ shift was added. BUT, when I call the "epoch" extraction it seems that EXTRACT uses the internal UTC representation, so I am forced to manually add the number of seconds of TZ shift in order to have the actual epoch. May it be so? |
#8
| |||
| |||
|
|
Anselmo Canfora wrote: I guess that Coniglio (BTW, is this your real first name??) no, it is some sort of funny/moron nickname, my real name is the one you read now ![]() Actually, I liked "Coniglio". |

|
so, it seems that: db_atm=# select current_timestamp; now ------------------------------- 2009-07-06 11:05:52.618695+02 (1 row) retrieves the current UTC time add up it the 2 hours of current TZ and write out this time with the indication "+02" to make you understand that the local TZ shift was added. BUT, when I call the "epoch" extraction it seems that EXTRACT uses the internal UTC representation, so I am forced to manually add the number of seconds of TZ shift in order to have the actual epoch. May it be so? Depends on your definition of "the actual epoch". Usually that is: seconds since 1970-01-01 00:00:00 UTC. Your definition is different: Seconds since 1970-01-01 00:00:00 at my local time zone. That's fine, and if that is what you need, who can argue with it. It's just that the name "utcepoch" would not have been my choice for that function. |

#9
| |||
| |||
|
|
Actually, I liked "Coniglio". it is "rabbit" in English ![]() |
|
I am still a little bit confused on this point, given a moment in time, say PT, it seems to me that: extract(epoch from date_trunc('milliseconds', current_timestamp))*1000 _should_ give me the epoch of PT expressed in my local time |
|
so if I add up my timezone shift: extract(timezone from date_trunc('milliseconds', current_timestamp) )*1000; it _should_ give me the UTC epoch of PT, is it right? |
#10
| |||
| |||
|
|
"Epoch" is absolute, so if several people all over the world run SELECT EXTRACT(EPOCH FROM current_timestamp) at the same time they should and will all get the same result. It is the number of seconds that passed since the (absolute!!) timestamp 1970-01-01 00:00:00 UTC |
|
so if I add up my timezone shift: extract(timezone from date_trunc('milliseconds', current_timestamp) )*1000; it _should_ give me the UTC epoch of PT, is it right? .... if you add your timezone shift, you will get the time passed since 1970-01-01 00:00:00 in your local time zone. There is no connection to UTC here. Maybe the following query will make it clearer: test=> SELECT EXTRACT(EPOCH FROM timestamp with time zone '1970-01-01 00:00:00 Europe/Rome'); date_part ----------- -3600 (1 row) Midnight in Rome was one hour *before* the Epoch (no daylight savings time). |
![]() |
| Thread Tools | |
| Display Modes | |
| |