Monday 13 July 2009

Converting date from local time zone to GMT, BST


I just thought I should add some technical bits to my blog that could be helpful to people trying to find some quick help. Here is one.

--*1*--
Oracle functions to transpose time between different time zones to GMT.

Oracle provides a number of functions to transpose between the different time zones to GMT. You need and need to store 'Olson' code to convert from the required time zone.

Please note when using these functions if the country area is used e.g. 'Europe\London' then this will also convert to BST (if we have entered day light savings). However if the 'GMT' code is used then this will always be set to GMT.

SELECT
-- Show timestamp as if in 'Europe/London'
CAST ('01-JAN-2009 09:00:01.01' AS TIMESTAMP) at time zone 'Europe/London' as A,
-- Show timestamp as if in 'Turkey'
CAST ('01-JAN-2009 09:00:01.01' AS TIMESTAMP) at time zone 'Turkey' as B,
-- Take timestamp from 'Europe/London' and show as 'Europe/London'
FROM_TZ(CAST('01-JAN-2009 09:00:01.01' AS TIMESTAMP), 'Europe/London' )  AT TIME ZONE 'Europe/London' as C,
-- Take timestamp from 'Turkey' and show as 'Europe/London'
FROM_TZ(CAST('01-JAN-2009 09:00:01.01' AS TIMESTAMP), 'Turkey' )  AT TIME ZONE 'Europe/London' as D,
-- Take timestamp from 'Europe/London' and show as 'Turkey' remove Timezone
CAST (FROM_TZ(CAST ('01-JAN-2009 09:00:01.01' AS TIMESTAMP), 'Europe/London' )  AT TIME ZONE 'Turkey' AS TIMESTAMP) as E,
-- Take timestamp from 'Turkey'
FROM_TZ(CAST('01-JAN-2009 09:00:01.01' AS TIMESTAMP), 'Turkey' ) as F,
-- Show offset for 'EST'
tz_offset('EST') as G,
-- Show offset for 'Turkey'
tz_offset('Turkey') as H
FROM
DUAL

A list of the time zone names can be retrieved from the Oracle view V$TIMEZONE_NAMES

--***--

No comments: