How does one parse an arbitrary unix timestamp to a datetime in kdb?

Viewed 129

I am trying to convert a unix timestamp i.e. 1618876798363946752 (~April 2021) into its corresponding datetime in kdb/q. I have tried using the following function. (as per this)

q)dt:{`datetime$(1970.01.01+0D00:00:00.001 * `long$x)};

However it yields the incorrect result:

q)dt 1618876798363946752
2254.09.24T08:41:10.211

What is the general rule of thumb and associated methodology for converting a Unix timestamp of an arbitrary length to its associated datetime in kdb+?

Thank you for your advice. Best Regards

2 Answers

I've slightly adjusted @terrylynch's answer found here to account for the full precision of a timestamp.

q)`datetime$1970.01.01+0D00:00:00.000000001*1618876798363946752
2021.04.19T23:59:58.363

Just adding another way of conversion:

q)`timestamp$1618876798363946752 + `long$1970.01.01D
2021.04.19D23:59:58.363946752

and function could be

q)dt: {`timestamp$x + `long$1970.01.01D}
q)dt 1618876798363946752
2021.04.19D23:59:58.363946752
Related