Convert timestamp string to UTC time
Hoping someone can help.
I need to convert a timestamp from the following format (YYYYMMDDhhmmss e.g. 20160105125915) to UTC time in the same format.
I presume I need to convert it to the system format ( like $HOROLOG ) and then use something like ConvertTimeStampToHorolog or perhaps LocalWithZTIMEZONEtoUTC but not sure of the syntax.
Thanks,
Hi Conor.
If you try to convert directly this datetime to a $HOROLOG format, you'll have problems :)
Date is a valid format, however the time needs to convert a valid format, so modify the variable to convert a valid datetime format.
Then you have a datetime in format YYY-mm-dd HH:MM:ss
Now, you need to convert this variable to $HOROLOG format
now, convert to UTC using your local time, if you know what is the UTC difference, apply directly.
Example: I'm in Spain, so is CET (UTC+1), however in summer (CEST) is UTC+2
If you use the method ##class(%SYSTEM.Util).LocalWithZTIMEZONEtoUTC you'll have the UTC as zone time, but your Ensemble doesn't know if it's Summer o Winter time.
if is Summer time, you need to reduce an hour your local time
if you want to compare both datetimes (now, I'm in CEST, so I'm in UTC+2)
The result is:
Now, you have the same problem, you have a time separated by ":", so only need to revert the conversion
and compare:
Check the info about theses methods:
$ZDATETIMEH
$ZDATETIME
$EXTRACT
##class(%SYSTEM.Util).LocalWithZTIMEZONEtoUTC
I hope I have solved your problem
Best regards,
Francisco Lopez
P.S. Don't forget check the answer if it's the correct answer.
P.P.S. [EDITED] According to Eduard Lebedyuk (see below), you can replace
to
Thanks Mr. Lebedyuk
Thank you so much Francisco, I accepted your answer. This would have taken me hours to figure out!
The time taken to explain is much appreciated,
Conor
Funny how you say 'unreadable' about a more compact layout, yet you are using short commands;)
definitely the shortest !
like this:
set ts=20160105125915
;; convert to $H
set th=$zdth($e(ts,1,8)_" "_$e(ts,9,10)_":"_$e(ts,11,12)_":"_(ts#100),8)
;; convert to UTC
set tu=$ZDTH(th,-3)
;; show $ZTS format (on UTC+0200 Vienna,Austria)
write $ZTD(tu,3)
2016-01-05 13:59:15
You can of course do it in a single unreadable long cascaded function chain.
HTH
yeah,
that's for slow typing senior writer's without autocomplete.
BTW. wrapping of this editor is sometime more than disturbing.
To convert string of any format into $horolog, use TO_DATE function:
To convert string of any format to timestamp use TO_TIMESTAMP function:
These functions are available in Caché ObjectScript and SQL.
Result (GMT+02:00, Chisinau):
Note:
For example, if in the OS to disable automatic daylight saving time, the result will be different:
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue