$TIMESTAMP function
The $TIMESTAMP alpha function is used to return the current date and time in terms of the number of milliseconds since 0000 GMT year 0000 (one day before 1 January 0001). This is in the form of a 6 byte unsigned binary string which can encode times between 0 CE and approximately 8900 CE. This function is intended as a timestamp for timing events and has the property that event duration can be computed by subtraction using SUBC.
This time stamp will be with respect to GMT and not local time on systems that understand timezones.
The TIMESTAMP packing format can be used to timestamp database rows.
Two constants are defined for sizing strings to receive timestamps and the packed format: _KCML_TIMESTAMP and _KCML_TIMESTAMP_ISO
Here is an example of how $TIMESTAMP could be used to time a loop:
DIM ts_start$_KCML_TIMESTAMP, ts_diff$_KCML_TIMESTAMP, n=1000
ts_start$ = $TIMESTAMP
FOR i = 1 TO n
a$ = s.echo$("hello")
NEXT i
ts_diff$ = $TIMESTAMP SUBC ts_start$
PRINT $PRINTF("Each iteration took %g seconds on average",VAL(STR(ts_diff$, 3, 4), 4) / n / 1000)
A packed timestamp can be converted to a readable 23 byte string (use the constant _KCML_TIMESTAMP_ISO in ISO-8601 format (CCCC-MM-DDTHH:MM:SS.mmm) using $PACK e.g.
DIM t$_KCML_TIMESTAMP $UNPACK(E="TIMESTAMP") $TIMESTAMP TO t$
and ISO timestamp strings can be converted to packed timestamps with $UNPACK.
Syntax example:
DIM oldtimestamp$6, ElapsedMilliSeconds$6 oldtimestamp$ = $TIMESTAMP ... ElapsedMilliSeconds$ = $TIMESTAMP SUBC oldtimestamp$
See also: