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

NameTypeRequiredDefaultDescription
hoursNumericArrayYesHour component.
minutesNumericArrayYesMinute component.
secondsNumericArrayYesSecond component.
argsAnyVariadicDuration constructor arguments.

Returns

NameTypeDescription
tAnyDuration object result.

Errors

IdentifierWhenMessage
RunMat:duration:InvalidArgumentArguments or option grammar do not match supported duration forms.duration: invalid argument
RunMat:duration:InvalidInputInput values cannot be converted/broadcast/formatted to a valid duration result.duration: invalid input
RunMat:duration:InternalInternal 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 Format property controls text rendering. Supported values are hh:mm:ss, hh:mm, mm:ss, s, and ss.
  • Linear indexing d(k) is supported and returns duration values.
  • Comparison operators and plus/minus duration 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:45

Broadcasting scalar minutes across an hour vector

t = duration([1 2], 15)

Expected output:

t =
01:15:00  02:15:00

Changing the display format without changing the stored value

t = duration(1, 5, 0);
t.Format = 'hh:mm';
disp(t)

Expected output:

01:05

Adding 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:00

Using 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.

datetime · day · hour · minute · month · second · year

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how duration is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.