Unix timestamp in Intersystems Caché

I wanted to have Unix Timestamp in Intersystems Caché, but their timestamp is a little different than the one you are used  to in other programming languages, their timestamp is “ddddd,sssss.fff”, while Unix Timestamp is defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.

So to convert Intersystems version to Unix Timestamp we use this:

({CACHETIMESTAMP} - 47117 * 86400 + $PIECE({CACHETIMESTAMP},",",2))

So to convert Unix Timestamp to Intersystems version we use this:

(({UNIXTIMESTAMP}\86400 + 47117)_","_({UNIXTIMESTAMP} # 86400))

We can combine these and create our own macros that will do the conversion on the fly when compiling.

#define toUnixTimestamp(%ct) (%ct - 47117 * 86400 + $PIECE(%ct,",",2))
#define toCacheTimestamp(%ut) ((%ut\86400 + 47117)_","_(%ut # 86400))
#define addToUnixTimestamp(%ct, %a) ($$$toUnixTimestamp(%ct) + %a)