Search code examples
kdb

How do I convert a unix timestamp in int to a `timestamp in KDB?


I'm receiving unix msec timestamps of the form 1673240400000.

How do I convert these to native KDB timestamps?

I've tried various versions of

"P"$ string (1673240400000 % 1000)

But none of these seem to work. I was sure there was a built-in for this?


Solution

  • You need to account for the different epochs.

    • Unix 1970
    • kdb 2000

    Also multiply by 1 million to get nanoseconds

    q)epochOffset:`long$1970.01.01D0
    q)epochOffset
    -946684800000000000
    q)`timestamp$epochOffset+1673240400000*1000000
    2023.01.09D05:00:00.000000000