0

In A brief introduction to q and kdb+ there are several places with creation of time records with code like 0D00:01.

And even random time generation technique using syntax:

n?0D0
fcn?0D00:00:20

I found 0D mentioned only in q4m3 2.5.2 Time Types as optional.

Are there any references to this syntax on code.kx? And are there any other useful date/time random generators exist? I checked for capital-letters, - seems 0D is the only one, see: q)@[value;;::] each ("0",/:.Q.A)

egor7
  • 4,678
  • 7
  • 31
  • 55

1 Answers1

2

Let me first note that the 0D... syntax is not specific to the rand operator. The prefix 0D is needed when a type of a literal kdb would infer without it would be different from what you intended. For example:

q)type 08:09:10.123 / time
-19h
q)type 0D08:09:10.123 / timespan
-16h

The prefix is optional when a type can be inferred unambiguously; in case of timespan literals it's sufficient to supply more than 4 digits after the dot when using the hh:mm:ss.nnnnnnnnn notation:

q)type 08:09:10.123 / time
-19h
q)type 08:09:10.1234 / still time
-19h
q)type 08:09:10.12345 / timespan
-16h

The 0D notation is very handy when you need a timespan value but don't want to specify all the details down to nanoseconds. I think you will agree that 0D00:01 (1 minute) is easier to type and read than 00:01:00.000000000.

Going back to your question, 0D0 is just a zero-valued timespan, the same as 00:00:00.00000000. However, ? treats it as if 1D0 (or 0D24:00:00.000000000) was passed. I didn't see it documented anywhere on code.kx.com but if you think about it you'll agree that generating a timespan in the range [0; 24h) is such a common case that it definitely deserves a shortcut. And there you have it!

Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31