James
James

Reputation: 1260

KDB:How to parse a millisecond timestamp in kdb

I'm trying to parse the following utc timestamps in kdb

t:1605083972601 1605083972853 1605083972854 1605083972860 1605083972865

such that it resolves at least to the millisecond. I have tried to do the following:

`datetime$("P"$10$string[`long$t])
`datetime$("P"$10$string[`long$(t*1000)])

which both return:

2020.11.11T08:39:32.000 2020.11.11T08:39:32.000 2020.11.11T08:39:32.000 2020.11.11T08:39:32.000

Obviously having it round to the second is inadequate. How does one effectively achieve this in kdb? Thanks

Upvotes: 2

Views: 1329

Answers (2)

terrylynch
terrylynch

Reputation: 13572

Another approach which makes it explicit that what you have are number of milliseconds since unix epoch:

q)f:1970.01.01+0D00:00:00.001*
q)f 1605083972601 1605083972853 1605083972854 1605083972860 1605083972865
2020.11.11D08:39:32.601000000 2020.11.11D08:39:32.853000000 2020.11.11D08:39:..

Upvotes: 1

Anton Dovzhenko
Anton Dovzhenko

Reputation: 2569

Following function converts unix to Kdb timestamp:

{`timestamp$(1000000*x)+`long$1970.01.01D0-2000.01.01D0}

Kdb timestamp starts from 1st Jan 2000. That is why unix timestamp has to be adjusted by 1970.01.01 and 2000.01.01 difference

Upvotes: 4

Related Questions