duration — Create duration arrays from hour, minute, and second components with MATLAB-compatible construction behavior.
duration constructs duration objects from numeric hour, minute, and second components. It supports MATLAB-compatible scalar expansion, internal elapsed-time representation, and display-format controls.
Syntax
t = duration(hours)
t = duration(hours, minutes)
t = duration(hours, minutes, seconds)
t = duration(___, "Format", format)
t = duration(___, Name, Value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
hours | NumericArray | Yes | — | Hour component. |
minutes | NumericArray | Yes | — | Minute component. |
seconds | NumericArray | Yes | — | Second component. |
args | Any | Variadic | — | Duration constructor arguments. |
Returns
| Name | Type | Description |
|---|---|---|
t | Any | Duration object result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:duration:InvalidArgument | Arguments or option grammar do not match supported duration forms. | duration: invalid argument |
RunMat:duration:InvalidInput | Input values cannot be converted/broadcast/formatted to a valid duration result. | duration: invalid input |
RunMat:duration:Internal | Internal duration state or indexing/evaluation failed unexpectedly. | duration: internal operation failed |
How duration works
duration(H)treats the input as hours.duration(H, M)treats the inputs as hours and minutes.duration(H, M, S)treats the inputs as hours, minutes, and seconds.- Scalar component inputs broadcast to match non-scalar component arrays; non-scalar inputs must share the same shape.
- The
Formatproperty controls text rendering. Supported values arehh:mm:ss,hh:mm,mm:ss,s, andss. - Linear indexing
d(k)is supported and returns duration values. - Comparison operators and
plus/minusduration arithmetic are supported. - Adding a duration to a datetime returns a datetime shifted by that elapsed time.
Does RunMat run duration on the GPU?
duration does not allocate GPU objects or invoke provider kernels. If a numeric input originates on the GPU, RunMat gathers it before building the duration object.
GPU memory and residency
No. RunMat represents duration values as host-side objects with an internal day-count tensor and a Format property. Even when the constructor receives gathered numeric data, the resulting object remains resident on the CPU.
Examples
Constructing a scalar duration from numeric components
t = duration(1, 30, 45)Expected output:
t =
01:30:45Broadcasting scalar minutes across an hour vector
t = duration([1 2], 15)Expected output:
t =
01:15:00 02:15:00Changing the display format without changing the stored value
t = duration(1, 5, 0);
t.Format = 'hh:mm';
disp(t)Expected output:
01:05Adding a duration to a datetime
t0 = datetime(2024, 4, 9, 8, 0, 0);
t1 = t0 + duration(1, 30, 0)Expected output:
t1 =
09-Apr-2024 09:30:00Using duration with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how duration changes the result.
Run a small duration example, explain the result, then change one input and compare the output.
FAQ
What does the Format property change?⌄
Only the textual representation. The stored elapsed time is unchanged, so comparisons and arithmetic still refer to the same duration.
Can I index duration arrays?⌄
Yes. Linear () indexing is supported and preserves the duration type.
Does duration run on the GPU?⌄
No. duration values are represented as host-side objects. Numeric inputs may be gathered first, but the resulting duration object remains on the CPU.
Can I add a duration to a datetime?⌄
Yes. RunMat supports datetime + duration and returns a datetime shifted by the specified elapsed time.
Related Datetime functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how duration is executed, line by line, in Rust.
- View the source for duration in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.