views:

61

answers:

1

In Oracle how do I convert a "timestamp with time zone" to the current time zone of the server? I need to dynamically figure out what the current time zone of the server is.

create table test_table1
(rec1 timestamp with time zone)

insert into test_table1(rec1) values (
to_timestamp_tz('1/1/1900 09:00:00 AM US/EASTERN','dd-mm-yyyy hh:mi:ss AM TZR')
)

SELECT NEW_TIME(rec1, TO_CHAR(rec1, 'TZR'), TO_CHAR(SYSTIMESTAMP, 'TZR'))
FROM test_table1

The example above creates a table with the time zone column. Then I insert a row into the table with the eastern timezone. The select statement doesn't work. What I am trying to achieve is to convert the rec_1 column to the timezone of the server and return that.

A: 

You can get the current system timezone [in some format or other] through :

select to_char(systimestamp,'TZD | TZH TZM [ TZR ]'), DBTIMEZONE
from dual;

The following may help with conversion. Not sure whether your session timezone is the same as your database timezone.

SELECT rec1, SYS_EXTRACT_UTC(rec1), cast(rec1 as timestamp with local time zone)
FROM test_table1

when I use systeimestamp I get null

TZD can return null (eg it knows what the offset it, but hasn't got an 'alias' for it). TZR shouldn't. Maybe something like...

SELECT FROM_TZ(TIMESTAMP '2000-03-28 08:00:00', to_char(systimestamp,'TZR')) 
   FROM DUAL;
Gary
Extracting the rec1 column from the table is straight forward. When I use the query: select to_char(rec1, 'TZD') from test_table1 I get 'EST'/'CST' but when I use systeimestamp I get null. I want to get the current timezone of the system in the 'EST'.. format and use the new_date function to convert it to eastern. So if it is eastern the same time will return and if not then it will be converted.
I got the DBA to set the dbtimezone to the correct timezone (-04:00). Now that I have the that how do I convert this or the database current date/time to eastern time zone. I also want to take Daylight Savings Time into consideration. While the tz_offset is -04:00 with DTS it is really -05:00.