Search code examples
datetimetimekdb

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


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


Solution

  • 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