Search code examples
kdb

Round the kdb datetime to the nearest even second


I'm new in kdb q. I'm trying to round the kdb datetime to the nearest even second.

For example, for the table below:

tp_time   price     synp
------------------------
05:00:03  1697.15   1697.15
05:00:05  1697.15   1697.15
...

And my expected table is the following:

tp_time   price     synp
------------------------
05:00:02  1697.15   1697.15
05:00:04  1697.15   1697.15
...

Could someone gives me some hints about this question? Thanks so much!


Solution

  • The tp_time type in the question asked is second (v) so the 2 xbar tp_time will work nicely; however, if the tp_time is of type time(t) or timespan(n) then the following will generate the correct results regardless of the datatype.

    q)t:([] tp_time:05:00:03.123 05:00:05.345 ; price:1697.15  1697.15; synp:1697.15  1697.15) //check out the meta of the table
    
    q)update tp_time:2 xbar tp_time.second from t    //usinng tp_time.second
    tp_time  price   synp
    ------------------------
    05:00:02 1697.15 1697.15
    05:00:04 1697.15 1697.15
    

    Here we are accessing the second constituent of tp_time (tp_time.second).