I'm trying to write SQL expression that will take a unix epoch (seconds since 1970/1/1) to local time in specific time zone and extract the hour value from it. After researching solutions along this time, I'm still not sure how to deal with the time zone. Note that I try to avoid using the NEW_TIME() function as it takes only a limited subset of time zone acronyms and not the full time zone name.
select
(TIMESTAMP '1970-01-01 00:00:00' AT TIME ZONE 'UTC' + numtodsinterval(1464820200,'second')) as ts_utc,
(TIMESTAMP '1970-01-01 00:00:00' AT TIME ZONE 'America/Los_Angeles' + numtodsinterval(1464820200,'second')) as ts_la,
(DATE '1970-01-01' + numtodsinterval(1464820200,'second')) as date_utc,
FROM_TZ(CAST(DATE '1970-01-01' + numtodsinterval(1464820200,'second') as TIMESTAMP), 'UTC') AT TIME ZONE 'America/New_York' as date_ny,
FROM_TZ(CAST(DATE '1970-01-01' + numtodsinterval(1464820200,'second') as TIMESTAMP), 'UTC') AT TIME ZONE 'America/Los_Angeles' as date_la, -- this value is correct
EXTRACT(hour from FROM_TZ(CAST(DATE '1970-01-01' + numtodsinterval(1464820200,'second') as TIMESTAMP), 'UTC') AT TIME ZONE 'America/New_York') as hour,
EXTRACT(TIMEZONE_OFFSET from FROM_TZ(CAST(DATE '1970-01-01' + numtodsinterval(1464820200,'second') as TIMESTAMP), 'UTC') AT TIME ZONE 'America/New_York') as tz_offset,
NEW_TIME((DATE '1970-01-01' + numtodsinterval(1464820200,'second')), 'GMT', 'EDT') as date_edt -- this value is correct
from dual;
and the results are
TS_UTC TS_LA DATE_UTC DATE_NY DATE_LA HOUR TZ_OFFSET DATE_EDT
2016-06-01 23:30:00.0 2016-06-01 23:30:00.0 2016-06-01 22:30:00.0 2016-06-01 15:30:00.0 2016-06-01 15:30:00.0 22 <UnknownType (-104)> 2016-06-01 18:30:00.0
There are several problems with the results
- column 1: start with the timestamp, the value is off by 1 hour than using the DATE
- column 2: creating this timestamp in America/Los_Angeles produces the same value as in UTC
- column 4: converting the timestamp to America/New_York produces the same value as in America/Los_Angeles
- column 6: the hour extracted is the UTC hour of the column 3
- column 7: the timezone offset cannot be extracted as all, resulting in Unknown Type
What would be the correct Oracle SQL to do this?