Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: TO_TIMESTAMP_TZ and unix timestamps
On 26 Jan 2005 09:52:56 -0800, "SuperMega" <jagacontest_at_hotmail.com> wrote:
>I have a table with a column filled with unix timestamps (how many
>seconds from whatever 1970) When people access that field it is not
>taking into account daylight saving time. Originally I was tasked with
>writing a huge case statement that would subtract an hour based on the
>time of year. I did a little digging and found the TO_TIMESTAMP_TZ
>function and read that it will handle all daylight saving time
>adjustments. So i set my timezone and restarted the db.
>
>SQL> select dbtimezone from dual;
>
>DBTI
>----
>EST
>
>Then I made my TO_TIMESTAMP_TZ statement, 2 of them one a couple days
>before the DST change and one a couple days after(in 24 hour
>increments), thinking that the 2nd one would display and hour before,
>but no change.
>
>
>SQL>SELECT TO_TIMESTAMP_TZ('01/01/1970 00:00:00 EST', 'mm/dd/yyyy
>HH24:MI:SS TZD')
>+ ((1112390687 / 86400) - (18000/86400))
>FROM dual;
>TO_TIMESTAMP_TZ('01/
>--------------------
>Apr 01 2005 16:24:47
>
>
>SQL>SELECT TO_TIMESTAMP_TZ('01/01/1970 00:00:00 EST', 'mm/dd/yyyy
>HH24:MI:SS TZD')
>+ ((1112649887/86400) - (18000/86400)) FROM dual;
>
>TO_TIMESTAMP_TZ('01/
>--------------------
>Apr 04 2005 16:24:47
It's not a complete answer, but it looks like trying to do date arithmetic (adding integer seconds on) to a TIMESTAMP WITH TIMEZONE ends up converting it to a plain DATE first, losing the timezone.
TO_TIMESTAMP_TZ('01/01/197000:
This isn't the timestamp string format, this is the plain date format.
SQL> SELECT dump(TO_TIMESTAMP_TZ('01/01/1970 00:00:00 EST', 'mm/dd/yyyy
HH24:MI:SS TZD')
2 + ((1112390687 / 86400) - (18000/86400)))
3 FROM dual;
DUMP(TO_TIMESTAMP_TZ('01/01/19
Type 13 is the internal DATE format (12 is the external one).
You at least stay with timestamps if you use INTERVAL instead of adding fractional days, but you can only go up to 9 digits for a SECONDS interval.
SQL> SELECT TO_TIMESTAMP_TZ('01/01/1970 00:00:00 EST', 'mm/dd/yyyy HH24:MI:SS TZD')
2 + INTERVAL '999999999' SECOND(9) 3 + INTERVAL '112390688' SECOND(9) 4 - INTERVAL '18000' SECOND(9)5 FROM dual;
TO_TIMESTAMP_TZ('01/01/197000:
2 + INTERVAL '999999999' SECOND(9) 3 + INTERVAL '112649888' SECOND(9) 4 - INTERVAL '18000' SECOND(9)5 FROM dual;
TO_TIMESTAMP_TZ('01/01/197000:
SQL> alter session set time_zone = 'US/Eastern';
Session altered
SQL> SELECT TO_TIMESTAMP_TZ('01/01/1970 00:00:00 EST', 'mm/dd/yyyy HH24:MI:SS TZD')
2 + INTERVAL '999999999' SECOND(9) 3 + INTERVAL '112390688' SECOND(9) 4 - INTERVAL '18000' SECOND(9)5 FROM dual;
TO_TIMESTAMP_TZ('01/01/197000:
2 + INTERVAL '999999999' SECOND(9) 3 + INTERVAL '112649888' SECOND(9) 4 - INTERVAL '18000' SECOND(9)5 FROM dual;
TO_TIMESTAMP_TZ('01/01/197000:
-- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis toolReceived on Tue Feb 01 2005 - 16:38:39 CST