Class Duration

extends Object implements Comparable
A Duration, capturing relative times.
Durations can be negative.
Durations are limited to ~292 years (signed 64 bit nanoseconds).

Class summary


constructor --h/int= --m/int= --s/int= --ms/int= --us/int= --ns/int=
Constructs a duration of h hours, m minutes, ms milliseconds, us microseconds, and ns nanoseconds.
constructor ns_/int
Constructs a duration of ns_ nanoseconds.

Constructors

constructor --h/int=0 --m/int=0 --s/int=0 --ms/int=0 --us/int=0 --ns/int=0
Constructs a duration of h hours, m minutes, ms milliseconds, us microseconds, and ns nanoseconds.

Constructs a duration of ns_ nanoseconds.

Statics

The number of microseconds per hour.

The number of microseconds per millisecond.

The number of microseconds per minute.

The number of microseconds per second.

The number of milliseconds per hour.

The number of milliseconds per minute.

The number of milliseconds per second.

The number of nanoseconds per hour.

The number of nanoseconds per microsecond.

The number of nanoseconds per millisecond.

The number of nanoseconds per minute.

The number of nanoseconds per second.

of [block] -> Duration
Calls the given block and measures the duration of the call.
Aliases
  • Stopwatch: Dart
  • Date difference: JavaScript

Parses a duration from the given string.
Accepts string of the form '-?(\d+([.]\d+)?(h|m|ms|us|ns)][ ]?)+'.
Only seconds, ms, and us may have a fractional part.
Spaces are not allowed inside units (like 'u s') but otherwise all spaces are ignored.
Units may only be used once.
Examples

print (Duration.parse "1h2m3s")          // >> 1h2m3s
print (Duration.parse "1h2m3.4s")        // >> 1h2m3.4s
print (Duration.parse "1h2m3.4s5ms")     // >> 1h2m3.4s5ms
print (Duration.parse "1h2m3.4s5ms6us")  // >> 1h2m3.4s5ms6us
print (Duration.parse "66m")             // >> 1h6m
print (Duration.parse "-1h")             // >> -1h

parse str/string [--on-error] -> Duration
Deprecated. Use parse str [--if-error] instead.

parse str/string [--if-error] -> Duration
Variant of parse str that calls if-error if str is not a valid duration.

The number of seconds per hour.

The number of seconds per minute.

Constructs a duration from the given time to now (Time.now).
For times in the past (time < Time.now), this constructs a positive duration.
This operation is equivalent to Time.to-now which is preferred, unless the developer wants to emphasize the Duration type.

Constructs a duration from now (Time.now) to the given time.
For times in the future (Time.now < time), this constructs a positive duration.
Examples

main:
  // In this example, we assume the time is 2021-04-21 15:30:35 UTC.
  time := Time.utc --year=2021 --month=04 --day=21 --h=17 --m=45 --s=35b
  print
    Duration.until time  // >> 2h15m0s

  time = Time.utc --year=2021 --month=04 --day=21 --h=14 --m=45 --s=35b
  print
    Duration.until time  // >> -45m0s

A constant 0-duration singleton.
Prefer to use this for 0-durations to avoid allocations.

Methods

The absolute value of this duration.

This duration in hours.

This duration in minutes.

This duration in milleseconds.

This duration in nanoseconds.

This duration in seconds.

This duration in microseconds.

Whether this duration is 0 (Duration.ZERO).

operator - other/Duration -> any
Subtracts this duration from the other duration.
Examples

t_12s := Duration --s=12
t_5s := Duration --s=5

print t_12s - t_5s  // >> 7s
print t_5s - t_12s  // >> -7s

operator - -> any
Negates this duration.
Examples

t_5s := Duration --s=5

print -t_5s    // >> -5s
print -(-t_5)  // >> 5s

Multiplies this duration with the factor.
Examples

t_5s := Duration --s=5

print t_5s * 2   // 10s
print t_5s * -3  // -15s

print 5 * t_5s  // Error, num's * does not know Duration!

Divides the duration by the factor.
Example

t_9s := Duration --s=9

print t_9s / 3  // >> 3s

operator + other/Duration -> any
Adds this duration to the other duration.
Examples

t_12s := Duration --s=12
t_5s := Duration --s=5

print t_12s + t_5s  // >> 17s

t_42ns := Duration --ns=42

print t_12s + t_42ns  // >> 12.000000042s

Whether this duration is less than the other.
Examples

d_5ns := Duration --ns=5
d_12s := Duration --s=12

d_5ns < d_5ns  // => false
d_12s < d_5ns  // => false

d_5ns < d_12s  // => true

Whether this duration is less than or equal to the other.

d_5ns := Duration --ns=5
d_12s := Duration --s=12

d_5ns <= d_5ns  // => true
d_5ns <= d_12s  // => true

d_12s <= d_5ns  // => false

Whether this duration is equal to the other.
Examples

d_5ns := Duration --ns=5
d_12s := Duration --s=12

d_5ns == d_5ns  // => true
d_12s == t_5ns  // => false

Whether this duration is greater than the other.
Examples

d_5ns := Duration --ns=5
d_12s := Duration --s=12

d_5ns > d_5ns  // => false
d_5ns > d_12s  // => false

d_12s > d_5ns  // => true

Whether this duration is greater than or equal to the other.
Examples

d_5ns := Duration --ns=5
d_12s := Duration --s=12

d_5ns >= d_12s  // => false

d_5ns >= d_5ns  // => true
d_12s >= d_5ns  // => true

periodic [block] -> none
Calls the given block periodically.
Advanced
Ensures that two executions of the block are always spaced out by *at least* this duration. At the end of every call of the block sleeps the remaining amount of this duration.
If an execution took longer than the period, skips one cycle. For example, if the block was supposed to run every 5ms, but the the first execution took 7ms, then this function sleeps 3ms, before it calls the block again at the next cycle (10ms).
This function may drift. If the start of the block is delayed (because of imprecision, or because the system was busy), then the subsequent executions will never catch up with the original schedule.

stringify -> any
See super.
Returns the duration in a compact string format, without loss of precision.
For example, the duration equal to 1234567890us: 20m34.56789s.