how to insert timestamp into kdb?

Hi, Masters:
  I have a date time value(format is YYYY.MM.DD) and a time value(mili_second, which is kdb standard `time format).

  According to the API of C to K, it was said “kz” and “ymd” functions should be applied while transfer timestamp data to KDB. My code for C++ are:

    K temp_kxdata;

    temp_kxdata = kz(ymd(YYYY, MM, DD) + mili_second);

  But RDB reports it is “Invalid DateTime”.

  So, how to convert it into KDB’s timestamp?

Regards

Zheng

The example of mili_second is 60545983
in kdb, `time$60545983

output is: 16:49:05.983

I need the datetime in kdb is: 2018.10.24T16:49:05.983

BTW, today is 1024, Happy Developer’s Day!

Hi Zheng,

The underlying datatype of datetime is a float value representing days since kdb epoch (2000.01.01), so for example, if we do a cast, we can see:

q)“f”$2000.01.03T18:00:00.00

2.75

So, in order for you to make a datetime from the milliseconds as integer, you need to divide by the number of milliseconds in a day, i.e.

kz(ymd(YYYY, MM, DD) + mili_second/86400000.0)

However, it’s worth pointing out that datetime as a datatype is deprecated, replaced by the timestamp type. This is the same size (8 bytes) but gives nanosecond precision. The underlying value for timestamp is long integer nanoseconds from the epoch date. To make one of these types in C, you could do the following:

ktj(-KP, (mili_second*1000000)+(ymd(YYYY,MM,DD)*86400000000000))

Obviously this would require a schema change, so it doesn’t solve your immediate problem, but it is worth noting for the future

Regards

Cormac

Hi, Cormac:
  Thank you so much!

  Yes, the code works. As you said, it’s reasonable to treat the timestamp as a long integer number(nano second format), so convert both mili_second and date into the nano seconds and plus them together matches KDB+ `timestamp format.

Thanks!

Zheng