datetime to UTC
Posted in 1999
Listers,
I'm wondering if there is a more elegant way of converting
a datetime (year to second) value to UTC format using SQL.
I'm using the following:
CREATE PROCEDURE datetime2utc
( Datetime_in datetime year to second )
RETURNING integer; DEFINE offSet interval second(9) to second;
DEFINE UTC interval second(9) to second;
DEFINE UTCC char(10);
DEFINE UTCI integer;
DEFINE epoch datetime year to second;
-- set debug file to "/tmp/datetime2utc_trace";
-- trace on;
-- When UTC time begins
LET epoch = DATETIME( 1970-01-01 00:00:00 ) YEAR TO SECOND;
-- Compute an offset that accounts for TZ
LET offSet = ( SELECT CURRENT - ( epoch + DBINFO('utc_current') UNITS SECOND )
FROM SysTables
WHERE TabId = 99 );
-- Compute the "interval" value
LET UTC = ( Datetime_in - epoch - offSet );
-- Convert the interval to char
LET UTCC = UTC;
-- Convert the char to integer
LET UTCI = UTCC;
-- NOTE: Discrepancies of 1-2 seconds may occur on busy systems.
RETURN UTCI;
END PROCEDURE;
I haven't done any exhaustive testing but this procedure seems to
produce correct results on my test system (Solaris 2.6, IDS 7.31.UC2).
Also note that I intend to use this in situations where the datetime
value passed in is not more than a few hours off of the current time.
One problem I'm seeing is discrepancies of 1-2 seconds can occur on
busy systems.
I was hoping there was a dbinfo function to handle this but I haven't
been able to find one. Does such a function exist? Is there a better
way to do this?
TIA.
Larry Kemmerling
Product Systems Engineer
AT&T Wireless Services
Aviation Communications Division