Skip to content

Timespan

TIMESPAN represents a signed 64-bit count of nanoseconds. It is a duration, not a date or a time of day. One day is exactly 86,400 seconds; applying a duration does not perform calendar or daylight-saving adjustments.

Literals and conversions

1D                            ; one day
0D01:00:00                    ; one hour
-0D00:00:00.000000001         ; minus one nanosecond
0Nn                           ; null
0Wn                           ; maximum signed count
-0Wn                          ; negative maximum signed count
(as 'I64 1D)                  ; 86400000000000
(as 'TIMESPAN 1000000000)      ; 0D00:00:01.000000000
(as 'TIMESPAN 01:00:00.000)    ; 0D01:00:00.000000000
(as 'TIMESPAN "1D01:00:00")    ; 1D01:00:00.000000000

The syntax is [-]daysD[hh[:mm[:ss[.fraction]]]]. Hours may exceed 23 (0D24:00:00 is 1D); minute and second fields must be between 00 and 59. Compact hour/minute/second forms are also accepted. The day field has at most six digits. A fraction is padded or truncated to nine decimal places. The initial minus sign negates the whole duration. Printing uses the full day/time form, except for null and the signed endpoint spellings, so values read back with their type and nanosecond precision. Integer arithmetic wraps in 64 bits; INT64_MIN is reserved for null. Floating casts round to the nearest integer count, with half counts rounded away from zero.

Vectors use per-element duration syntax, for example [1D 0Nn -1D]. as 'TIMESPAN also converts a vector element by element. TIME converts milliseconds to nanoseconds exactly. Casting a timestamp to TIMESPAN extracts its time of day; casting a timespan to I64 exposes its raw count.

Arithmetic and result types

Expression Result
timestamp - timestamp, time - time TIMESPAN
timestamp + span, timestamp - span, span + timestamp TIMESTAMP
time + span, time - span, span + time, span - time TIME, floored to the millisecond; no 24 h wrap, a result past the 32-bit range is null
time + time Type error, as timestamp + timestamp
time div span, time / span Type error (a span divided by a time reads the time as its millisecond count)
xbar time span TIME: the start of the time's bucket, floored to the millisecond
span + span, span - span, neg span, abs span TIMESPAN
span + integer, integer + span, span - integer TIMESPAN; integer counts nanoseconds
span * integer, integer * span TIMESPAN
span + float, float + span, span - float, float - span, span * float, float * span (F64 or F32) F64: the float arithmetic of the nanosecond count; a null operand gives null
span div integer, span div span TIMESPAN
span div F64, span div F32 TIMESPAN: the float is rounded to an integer count first, as for an integer divisor
span / span, span / integer F64
span % integer TIMESPAN
span % F64, F64 % span, and the same with F32 F64: the float % of the nanosecond count, the floor modulo rounded once (in [0,b) for b > 0, (b,0] for b < 0); a zero divisor gives null
span % span, span * span Type error
Comparisons with another span or an integer count BOOL
sum spans TIMESPAN; skips nulls
avg spans, med spans, quantile spans p TIMESPAN, rounded to the nanosecond (half away from zero); skip nulls
dev, stddev, dev_pop, mdev TIMESPAN; a variance is a squared duration and stays F64
min, max, first, last Preserve TIMESPAN
min span number, max span number, either order, any numeric type TIMESPAN: the number counts nanoseconds, a float rounded first (see below)
sums, mins, maxs, msum, mmin, mmax, deltas TIMESPAN
avgs, mavg TIMESPAN, rounded to the nanosecond
ratios F64
not span BOOL; true for a zero or null duration, element-wise over a vector

avg, med and quantile of a vector are exact to the nanosecond at any magnitude: the total is summed in 128 bits and the order statistics are taken from the integer values. The grouped aggregates, avgs, mavg, mdev and the deviations compute in F64 and round back, so they are exact up to 2^53 nanoseconds (about 104 days) per value.

Inside a query the same functions give the same answers, nulls included.

On empty or all-null duration vectors, min and max return 0Nn, as every other type returns its null; this holds inside a query too, for a group with no non-null value.

F64 and F32 operands promote duration addition, subtraction, multiplication and % to F64 nanosecond arithmetic: (+ 0D00:00:00.000000010 2.5e) is 12.5, as with 2.5. Integer division preserves the left operand's type: a span divided by a span is a duration containing the quotient count, not a dimensionless integer. Division floors toward negative infinity. For example, (div -0D00:00:00.000000001 2) is minus one nanosecond, and (% -0D00:00:00.000000001 1000000000) is 999,999,999 nanoseconds.

Nulls propagate through arithmetic. Integer division, remainder, and duration bucket widths of zero return a typed null. F64 results retain the engine's existing policy of converting non-finite results to 0Nf.

xbar accepts a TIMESPAN bucket width for timestamps and times:

(xbar 2024.01.15D12:34:56.000000000 0D01:00:00)
; 2024.01.15D12:00:00.000000000
(xbar 10:37:00.000 0D00:15:00)
; 10:30:00.000

Scalar, vector and query column expressions use the same duration arithmetic. Ordinary, encoded and partitioned storage preserve duration types and nulls. Explicit CSV schemas accept TIMESPAN and CSV writes use its literal syntax. Parquet duration and clock-time annotations are not remapped by this change.

min and max with a number

The two-argument min and max of a TIMESPAN and a number, in either order and over atoms, vectors or query columns, are a TIMESPAN whichever operand wins. The number is converted to a duration first and the two durations are compared:

  • an integer or boolean is a count of nanoseconds;
  • an F64 or F32 is rounded to the nearest count, half away from zero, as (as 'TIMESPAN x) and a float div divisor are: (min 0D00:00:00.000000010 2.5) is three nanoseconds;
  • a value beyond the duration range saturates to 0Wn or -0Wn;
  • a null of either side is the lowest value, as for every other type: min with a null is 0Nn, max with a null is the other operand.
(max 0D00:00:00.000000010 2.5)         ; 0D00:00:00.000000010
(min 2.5 0D00:00:00.000000010)         ; 0D00:00:00.000000003
(max [0D00:00:00.000000001] [3.5])     ; [0D00:00:00.000000004]
(max 0D00:00:00.000000010 0Nf)         ; 0D00:00:00.000000010

This differs from +, -, * and %, where a float operand makes the result an F64 count: min and max pick one of their operands, so the answer stays a duration.

Migrating timestamp and time differences

Timestamp subtraction previously returned I64, and time subtraction a TIME. Both now return TIMESPAN, including in queries, updates and deltas, and time ± span returns a TIME (it returned a TIMESPAN). time + time is a type error. To retain the previous integer-count behavior, explicitly cast the difference before further arithmetic ((as 'TIME (div (as 'I64 (- t1 t0)) 1000000)) gives back the old TIME difference):

(as 'I64 (- finish start))
(div (as 'I64 (- finish start)) 1000000000)

For a typed period, (div (- finish start) period) returns a TIMESPAN holding the quotient count. Cast that quotient to I64 when it represents a bar index. Use an integer nanosecond width for remainder, because two TIMESPAN operands are not admitted by %:

(as 'I64 (div (- finish start) period))
(% (- finish start) (as 'I64 period))

Reference checks

The installed q 5.0 binary (2026.07.23) was used to generate 65 scalar cases for test/rfl/temporal/timespan_operators.rfl. The fixture checks both result types and raw counts for parsing, endpoint spellings, mixed arithmetic, division, remainder, comparisons, timestamp subtraction and aggregates. These checks establish the tested duration behavior; the existing F64 non-finite policy described above still applies.