Question
· Sep 3

$ZDATETIME($h,3,1,3)

Hi,

I am using this "$ZDATETIME($h,3,1,3)" and getting response as 2024-09-03 12:07:45.000
But I need proper values at the end instead of zeros (000)

Something like below -
2024-09-03 12:07:45.658

Discussion (6)1
Log in or sign up to continue

This will be that caveat I warned of which is detailed in the documentation.

You could do something like:

Write $ZDATETIME($h_"."_$P($NOW(),".",2),3,1,3)

Which takes the value of $H and appends the milliseconds from $NOW() to then form your datestamp:

However the documentation I linked to warns that there can be a discrepancy between $H and $NOW() so this approach could then lead to your timestamp being off by up to a second. As you are trying to work to the level of milliseconds, I suspect accuracy is very important and therefore I would not recommend this approach.

Take a look here and see if this example of comparing $h, $ZTIMESTAMP, and $NOW() helps, and the example of converting from UTC to the local timezone helps.

The link in my last reply actually contains the answer, which is always useful. I have tweaked it slightly so that it's a single line, but the output is the same.

To get the current date and time with milliseconds, you can do the following:

WRITE $ZDATETIME($ZDATETIMEH($ZTIMESTAMP,-3),3,1,3)

This is:

  1. Starting with the output of $ZTIMESTAMP
  2. Converting to a $HOROLOG format adjusted for your local timezone using $ZDATETIMEH
  3. Converting to the desired format using $ZDATETIME

I hope this helps!

Together with $now() and timezone adjustment you can have the desired result

for time=$h, $now($ztz-$s($SYSTEM.Util.IsDST():60,1:0)) write time,?20,$zdt(time,3,1,3),!
// assuming 60 min summertime offset
//
// you should get an output like thisL
67086,83334         2024-09-03 23:08:54.000
67086,83334.1341026 2024-09-03 23:08:54.134

There is an (old) undocumented function which gives the $h value with several  decimal places, unfortunately the recommended replacement is more or less the above solution instead of a simple $zlts (z-local-timestamp).

By the way, if you need again and again a local timestamp with decimals, just create a user defined system variable. Make a one line entry into the %ZLANGV00.mac routine:

%ZLANGV00 ; User defined (system) variables

	// Local timestamp with decimals
ZLTS()	quit $now($ztz-$s($SYSTEM.Util.IsDST():60,1:0))

You can use whatever name you want as long as it starts with Z, contains uppercase chars only and do not conflict with existing names.  Use it as a standard $-variable

write $zlts, $zdt($zlts,3,1,3)
67086,85681.092746
2024-09-03 23:48:01.092