James
James

Reputation: 1260

kdb+/q:How does one convert a utc timestamp to a datetime?

How could one convert the following timestamp t:1595779091979 into its equivalent `datetime$() representation (UTC)?

For instance if one tries to do so using the following.

q) `datetime$t
    0000.00.00T00:00:00.000

q) `timestamp$t
    2000.01.01D00:26:35.779091979

(Both are incorrect, the time should be 2020.07.26D...)

Thanks

Upvotes: 1

Views: 1498

Answers (1)

Alexander Belopolsky
Alexander Belopolsky

Reputation: 2268

The timestamp 1595779091979 looks like milliseconds since 1970 epoch. If you drop the millis - the conversion is simply

 q)1970.01.01+0D00:00:01*1595779091
2020.07.26D15:58:11.000000000

or keeping the millis:

 q)1970.01.01+0D00:00:00.001*1595779091979
2020.07.26D15:58:11.979000000

Finally, you can add the following definition to your utility library

ts:1970.01.01+0D00:00:00.001*

and use it in your code whenever you need conversion

 q)ts 1595779091979
2020.07.26D15:58:11.979000000

Update: A slightly shorter solution can be written as

ts:1970.01.01D+1000000*

Upvotes: 2

Related Questions