Nexus API Reference

Duration
in package
implements Stringable

Read onlyYes
FinalYes

Nanosecond-precision, immutable duration value object.

Duration is the standard way to express time spans throughout Nexus — timeouts, scheduled delays, backoff windows, and mailbox blocking calls all accept a Duration. The value is stored as a single int64 nanosecond count, giving zero-overhead conversions in hot paths without floating-point drift. All arithmetic methods return a new instance; the original is never mutated.

Example:

$timeout  = Duration::seconds(5);
$backoff  = Duration::millis(200)->multipliedBy(3);   // 600 ms
$total    = $timeout->plus($backoff);                  // 5s 600ms

$ref->ask(new GetCount(), $timeout);
$ctx->scheduleOnce(Duration::millis(500), fn() => $system->shutdown($timeout));
Tags
see
Runtime::scheduleOnce()

for timer-based usage

see
ActorRef::ask()

for request-response timeouts

psalm-api
psalm-immutable

Table of Contents

Interfaces

Stringable

Methods

__toString()  : string
compareTo()  : int
Compare this duration to another, returning a negative int, zero, or positive int.
dividedBy()  : self
Return a new Duration divided by the given integer divisor (truncated toward zero).
equals()  : bool
Return true if this duration has the same nanosecond count as the other.
isGreaterThan()  : bool
Return true if this duration is strictly longer than the other.
isLessThan()  : bool
Return true if this duration is strictly shorter than the other.
isZero()  : bool
Return true if this duration has a nanosecond count of exactly zero.
micros()  : self
Create a Duration of the given number of microseconds.
millis()  : self
Create a Duration of the given number of milliseconds.
minus()  : self
Return a new Duration equal to this duration minus the given duration.
multipliedBy()  : self
Return a new Duration scaled by the given integer factor.
nanos()  : self
Create a Duration of the given number of nanoseconds.
plus()  : self
Return a new Duration equal to this duration plus the given duration.
seconds()  : self
Create a Duration of the given number of whole seconds.
toMicros()  : int
Return the duration truncated to whole microseconds.
toMillis()  : int
Return the duration truncated to whole milliseconds.
toNanos()  : int
Return the duration as a nanosecond count.
toSeconds()  : int
Return the duration truncated to whole seconds.
toSecondsFloat()  : float
Return the duration as a floating-point number of seconds.
zero()  : self
Create a zero-length Duration.

Methods

__toString()

public __toString() : string
Return values
string

compareTo()

Compare this duration to another, returning a negative int, zero, or positive int.

public compareTo(self $other) : int

Suitable for use with usort() or similar comparison callbacks.

Parameters
$other : self
Return values
int

dividedBy()

Return a new Duration divided by the given integer divisor (truncated toward zero).

public dividedBy(int $divisor) : self
Parameters
$divisor : int

Must be non-zero.

Return values
self

equals()

Return true if this duration has the same nanosecond count as the other.

public equals(self $other) : bool
Parameters
$other : self
Return values
bool

isGreaterThan()

Return true if this duration is strictly longer than the other.

public isGreaterThan(self $other) : bool
Parameters
$other : self
Return values
bool

isLessThan()

Return true if this duration is strictly shorter than the other.

public isLessThan(self $other) : bool
Parameters
$other : self
Return values
bool

isZero()

Return true if this duration has a nanosecond count of exactly zero.

public isZero() : bool
Return values
bool

micros()

Create a Duration of the given number of microseconds.

public static micros(int $micros) : self
Parameters
$micros : int
Return values
self

millis()

Create a Duration of the given number of milliseconds.

public static millis(int $millis) : self
Parameters
$millis : int
Return values
self

minus()

Return a new Duration equal to this duration minus the given duration.

public minus(self $other) : self
Parameters
$other : self
Return values
self

multipliedBy()

Return a new Duration scaled by the given integer factor.

public multipliedBy(int $factor) : self
Parameters
$factor : int

Multiplier; negative values produce a negative duration.

Return values
self

nanos()

Create a Duration of the given number of nanoseconds.

public static nanos(int $nanos) : self
Parameters
$nanos : int
Return values
self

plus()

Return a new Duration equal to this duration plus the given duration.

public plus(self $other) : self
Parameters
$other : self
Return values
self

seconds()

Create a Duration of the given number of whole seconds.

public static seconds(int $seconds) : self
Parameters
$seconds : int
Return values
self

toMicros()

Return the duration truncated to whole microseconds.

public toMicros() : int
Return values
int

toMillis()

Return the duration truncated to whole milliseconds.

public toMillis() : int
Return values
int

toNanos()

Return the duration as a nanosecond count.

public toNanos() : int
Return values
int

toSeconds()

Return the duration truncated to whole seconds.

public toSeconds() : int
Return values
int

toSecondsFloat()

Return the duration as a floating-point number of seconds.

public toSecondsFloat() : float

Prefer toNanos() / toMillis() in hot paths to avoid floating-point overhead.

Return values
float

zero()

Create a zero-length Duration.

public static zero() : self
Return values
self

        
On this page

Search results