Determine if spring or fall and adjust time if DST or not - oracle

I have a query like this:
SELECT
to_date(to_char((from_tz(to_timestamp(to_char(my_column, 'YYYY-MM-DD HH:MI:SS PM'), 'YYYY-MM-DD HH:MI:SS PM') ,'America/Edmonton')at time zone 'America/Vancouver'),'YYYY-MM-DD'),'YYYY-MM-DD') as Date_Column
FROM
my_table
It converts the time between MST to PST. This works most of the time except when daylight savings time occurs. Then a situation happens when in the clock moves forward an hour, and then since an hour is lost (for example 1:59 to the 3:00 am) then we are trying to convert a fictional time.
So I am aware of the issue, and I know Oracle will throw this error for that reason:
ORA-01878: specified field not found in datetime or interval
I've looked all over the Internet for a possible work around but can't really find anything that works. I just want to modify my query so that it can tell what time of year it is and either do the conversation when the time exists, or handle the time somehow when it does not exists. Perhaps add the extra hour when it's missing, and remove it when it's the other time of year.
Does anyone have a solution for this? Is this an impossible problem?

Instead of all the conversions you are using, just use AT TIME ZONE. You may need to take extra care depending on the data type of your column. The AT TIME ZONE functionality will always take into account daylights savings time.
If your column is a TIMESTAMP WITH TIME ZONE, that would be ideal
because you could just say AT TIME ZONE 'PST' and you would get the
time in PST.
If the column is the datatype TIMESTAMP, when you use AT TIME ZONE it
will convert from the time zone of the current session to whatever
you specify.
If your column is a DATE, you will need to convert it to a TIMESTAMP first, then you can use the AT TIME ZONE function. Converting the DATE to TIMESTAMP will make the time zone 0:00 offset so be sure to account for that.
Another handy trick is to use AT LOCAL. This will convert the timestamp to the time zone of the current session. This may be handy if you have users of your application that are in multiple time zones.
Example
I am in the time zone -04:00 (East coast US) which is why that appears as my time zone in the examples below.
WITH
my_table
AS
(SELECT TIMESTAMP '2021-01-01 8:00:00 -7:00' AS ts_with_tz,
TIMESTAMP '2021-01-01 8:00:00' AS ts,
DATE '2021-01-01' + (8 / 24) AS dt
FROM DUAL)
SELECT 1 AS example_num,
t.ts_with_tz,
t.ts,
t.dt
FROM my_table t
UNION ALL
SELECT 2 AS example_num,
t.ts_with_tz AT TIME ZONE 'PST',
t.ts AT TIME ZONE 'PST',
TO_TIMESTAMP (t.dt) AT TIME ZONE 'PST'
FROM my_table t
ORDER BY 1;
RESULT
EXAMPLE_NUM TS_WITH_TZ TS DT
______________ ______________________________________________________ ______________________________________________________ ______________________________________________________
1 01-JAN-21 08.00.00.000000000 AM -07:00 01-JAN-21 08.00.00.000000000 AM AMERICA/NEW_YORK 01-JAN-21 08.00.00.000000000 AM AMERICA/NEW_YORK
2 01-JAN-21 07.00.00.000000000 AM AMERICA/LOS_ANGELES 01-JAN-21 05.00.00.000000000 AM AMERICA/LOS_ANGELES 31-DEC-20 09.00.00.000000000 PM AMERICA/LOS_ANGELES

Related

Comparing TIMESTAMP WITH TIME ZONE to DATE

I need to compare database generated dates (column values defaulting to SYSDATE) to hand written time stamps recorded with the time zone. This is the comparison I am trying:
where trunc(updated, 'mi') >= to_timestamp_tz('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR')
I assume that a character string converted to a TIMESTAMP WITH TIME ZONE should be comparable to a DATE. However, this only works if the database is located in my own time zone. Otherwise, I have to manually convert the external time stamp to the database time zone. For example, if I am in BST and the database is in EST, I have to write:
where trunc(updated, 'mi') >= to_timestamp_tz('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR')
Where 04:45 = 10:45 - 6, and 6 is the difference between BST and EST. This looks absolutely counter-intuitive as the original time stamp has been recorded in PST and is therefore entered as US/Pacific. Could anybody please explain why this conversion is needed? I would also appreciate if somebody suggests a better solution.
You can cast your DATE column to a TIMESTAMP WITH TIME ZONE value like this:
WHERE
FROM_TZ(CAST(TRUNC(updated, 'mi') AS TIMESTAMP),
(SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual)) >= to_timestamp_tz('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR')
but it works also the other way around, i.e. convert TIMESTAMP WITH TIME ZONE to DATE in database time zone:
WHERE
TRUNC(updated, 'mi') >=
CAST(TO_TIMESTAMP_TZ('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR') AT TIME ZONE (SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual) AS DATE)
SYDATE is provided in the time zone of database server's operating system (NOT DBTIMEZONE) , thus you have to use (SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual) or provide hard-coded value if appropriate.
This approach fails if any users inserted/updated any updated using his current local time zone. In such cases the time zone information is lost and there is no way to recover it.
If you are running this query in any of oracle clients, You can also change the session setting by running
ALTER SESSION SET TIME_ZONE ='BST';
When you change the session time_zone all the date values coming from database column and time stamp values passed without time zone will be converted to BST.
hence it will ensure that the comparison is happening in common time zone

Trying to retrieve the UTC time-stamp in Oracle, does using "from_tz" cause an issue?

Will using the following query to retrieve the UTC timestamp from an oracle database cause an issue? I do not want to alter the database timezone parameter in order to retrieve the correct date. I do not want to do alter database set time_zone.
My query at the moment is:
select from_tz(CAST (sys_extract_utc(systimestamp) AS TIMESTAMP), '+00:00') from dual;
I would like to know if this query will result in the correct UTC date in all circumstances regardless of the EST/EDT status.
I don't see anything wrong with your query. Note that if you want to work in UTC for your session, you could simply:
ALTER SESSION SET TIME_ZONE = '0:00';
select CURRENT_TIMESTAMP from dual;
output
11/1/2016 5:48:55.115282 PM +00:00
That would change your current_timestamp (and localtimestamp) for your entire session.
Your query is just setting timezone but not converting time. If that's what you were looking for it is ok. Is local time is 3AM you will return 3AM UTC.
I think you're looking for that query:
select cast(sysdate as timestamp) at time zone 'UTC' from dual;
And the apply
select from_tz(cast(systimestamp at time zone 'UTC' as timestamp), '+0:00') from dual;
Some general notes:
SYSTIMESTAMP and SYSDATE are given in time zone of your database server's operating system. Thus changing database time zone (i.e. DBTIMEZONE) does not change anything.
CAST (sys_extract_utc(systimestamp) AS TIMESTAMP), resp. cast(systimestamp at time zone 'UTC' as timestamp) have a problem. You convert your timestamp to UTC but by CAST(... AS TIMESTAMP) you remove any time zone information from that value. If you like to do any further conversion (e.g. again to TIMESTAMP WITH TIME ZONE value) then your input UTC value is considered to be a SESSIONTIMEZONE value.
from_tz(CAST (sys_extract_utc(systimestamp) AS TIMESTAMP), '+00:00') does following:
Get current time in time zone of your database server's operating system, include time zone information.
Convert this time to UTC, cut time zone information
Append time zone information (+00:00) to this value
Correct output but redundant conversion
cast(sysdate as timestamp) at time zone 'UTC' does following:
Get current time in time zone of your database server's operating system, without any time zone information.
Cast to TIMESTAMP (basically no effect at all)
Consider this value as time in your local session time zone (SESSIONTIMEZONE) and convert to UTC.
Correct output only if time zone of your database server's operating system is the same as your local session time zone, otherwise you get wrong result.
from_tz(cast(systimestamp at time zone 'UTC' as timestamp), '+0:00') does following:
Get current time in time zone of your database server's operating system, include time zone information.
Convert this time to UTC
Cut time zone information
Append time zone information (+00:00) to this value
Correct output but redundant conversion
from_tz(cast(systimestamp as timestamp), '+0:00') does following:
Get current time in time zone of your database server's operating system, include time zone information.
Cut time zone information
Append time zone information (+00:00) to this value
Correct output only if time zone of your database server's operating system is UTC.

Converting to CHAR losing daylight saving

I have the following query:
SELECT
from_tz(CAST(e.Event_Time_UTC AS TIMESTAMP), 'UTC') AS UTC,
from_tz(CAST(e.Event_Time_UTC AS TIMESTAMP), 'UTC') AT TIME ZONE 'Brazil/East' AS BRT,
TO_CHAR(from_tz(CAST(e.Event_Time_UTC AS TIMESTAMP), 'UTC') AT TIME ZONE 'Brazil/East', 'HH24:MI') AS Hora
FROM
MY_EVENTS e
ORDER BY
UTC
I`m getting the UTC time and converting to local.
Everything works fine when I convert directly, but when I use the TO_CHAR, I lose the Daylight saving and end up with the result looking like this:
Can someone please explain what`s happening, please?
EDIT: Here's the result from v$timezone_file:

Issue with timezone in oracle

Below query is used in my code to get the timezone from database. (DB is in central time zone)
SELECT DBTIMEZONE FROM DUAL;
-05:00
When daylight saving is on (CDT), its expected result.
but when daylight saving ends (CST), my result should be GMT-06, but i am still getting GMT-05.
Googled and got below options :
SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual;
SELECT TZ_OFFSET('CST6CDT') FROM DUAL;
Will above queries run correctly accordingly to CST and CDT ??
DBTIMEZONE does not determine the time zone of SYSTIMESTAMP (or SYSDATE)
Time zone of SYSTIMESTAMP is the time zone of database server's operating system.
DBTIMEZONE is only relevant for data type TIMESTAMP WITH LOCAL TIME ZONE and defines the time zone in which those values are stored internaly. You cannot change DBTIMEZONE on your database if the database contains a table with a TIMESTAMP WITH LOCAL TIME ZONE column and the column contains data.

SQL server query for time zone

Is there any way to get the current time for particular given time zone?
For EX: GetDateTime("Morocco Standard Time")
It should give the local time of time zone "Morocco Standard Time"
Thanks in Advance,
Merlin
I know that the question is quite old, but maybe the answer will help someone.
If you are using SQL Server 2016 you can use new feature AT TIME ZONE. Let's get to the result that you need step by step.
First, use function SYSDATETIMEOFFSET() to retrieve current date and time in UTC time zone. The return type of this function is DATETIMEOFFSET, which stores not only date, but also related time zone information:
SELECT SYSDATETIMEOFFSET()
-- Result is 2016-08-20 13:15:25.3760873 +03:00
Then this with AT TIME ZONE statement. This returns DATETIMEOFFSET in the time zone that you need:
SELECT SYSDATETIMEOFFSET() AT TIME ZONE 'Morocco Standard Time'
-- Result is 2016-08-20 11:15:25.3760873 +01:00
Additionally if you want to lose time zone information and use DATETIME, you can cast the above result to DATETIME:
SELECT CAST( SYSDATETIMEOFFSET() AT TIME ZONE 'Morocco Standard Time' AS DATETIME)
-- Result is 2016-08-20 11:15:25.376

Resources