Convert to and from time durations
Convert to duration
OPAL uses values of type duration to store durations of time. Your data may have inputs in units like seconds, milliseconds, or pairs of timestamps.
To convert from seconds to duration, use duration_sec():
To convert from milliseconds to duration, use duration_ms():
To convert from two timestamps to duration, use subtraction:
Convert from duration
Durations are internally stored as nanosecond integers. You can convert a duration to nanoseconds with a simple cast:
You can convert to some other unit of time by dividing by that time:
The duration units supported in OPAL are:
| Duration | Unit |
|---|---|
| nanosecond | 1ns |
| microsecond | 1us |
| millisecond | 1ms |
| second | 1s |
| minute | 1m |
| hour | 1h |
| day | 1d |
| week | 1w |
Note that duration literals do not compensate for leap seconds or daylight savings time switch.
In contrast to timestamps, durations will generally participate in aggregations and other operations as if they are numbers, yielding duration results. For example, sum(duration) or avg(duration) are well defined operations.