chrise
chrise

Reputation: 4253

KDB creating microsecond timestamp

I created ms precision timestamps in KDB by

update Ts: Date + Time + MSec from Table

Now I am trying to do the same with microsecond precision. n seems to be the nanosec precision time type

n   8   16  00:00:00.000000000  0Nn timespan        Timespan    TimeSpan

so I tried

select Date + "n"$Time + (1000*USec), Date, Time, USec from Table

But it seems like it treats "n" as having millisecond precision and it adds (USEC*1000) ms instead of ns

2017-07-01T23:59:58.000000  2017-06-23  09:10:02.000    744596

How can I make this work. I also tried casting into 'p' but that yields a type error

Upvotes: 0

Views: 2350

Answers (1)

ostewart
ostewart

Reputation: 106

KDB evaluates from right to left. Thus in your example it is adding 1000*Usec to Time as the first operation, which results in the value being treated as milliseconds rather than nanoseconds. If you move the (1000*USec) to the left of casting time to a timespan it will add nanoseconds instead.

q)show Table: ([]Date: .z.D - 1 0; Time: "t"$2?23:59:59; MSec: 2?1000; USec: 2?1000)
Date       Time         MSec USec
---------------------------------
2017.06.26 16:04:52.000 652  635
2017.06.27 13:33:25.000 66   108
q)update ts: Date + (1000*USec) + "n"$Time + MSec from Table
Date       Time         MSec USec ts
---------------------------------------------------------------
2017.06.26 16:04:52.000 652  635  2017.06.26D16:04:52.652635000
2017.06.27 13:33:25.000 66   108  2017.06.27D13:33:25.066108000

Upvotes: 2

Related Questions