Duration
in package
implements
Stringable
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
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
stringcompareTo()
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
intdividedBy()
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
selfequals()
Return true if this duration has the same nanosecond count as the other.
public
equals(self $other) : bool
Parameters
- $other : self
Return values
boolisGreaterThan()
Return true if this duration is strictly longer than the other.
public
isGreaterThan(self $other) : bool
Parameters
- $other : self
Return values
boolisLessThan()
Return true if this duration is strictly shorter than the other.
public
isLessThan(self $other) : bool
Parameters
- $other : self
Return values
boolisZero()
Return true if this duration has a nanosecond count of exactly zero.
public
isZero() : bool
Return values
boolmicros()
Create a Duration of the given number of microseconds.
public
static micros(int $micros) : self
Parameters
- $micros : int
Return values
selfmillis()
Create a Duration of the given number of milliseconds.
public
static millis(int $millis) : self
Parameters
- $millis : int
Return values
selfminus()
Return a new Duration equal to this duration minus the given duration.
public
minus(self $other) : self
Parameters
- $other : self
Return values
selfmultipliedBy()
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
selfnanos()
Create a Duration of the given number of nanoseconds.
public
static nanos(int $nanos) : self
Parameters
- $nanos : int
Return values
selfplus()
Return a new Duration equal to this duration plus the given duration.
public
plus(self $other) : self
Parameters
- $other : self
Return values
selfseconds()
Create a Duration of the given number of whole seconds.
public
static seconds(int $seconds) : self
Parameters
- $seconds : int
Return values
selftoMicros()
Return the duration truncated to whole microseconds.
public
toMicros() : int
Return values
inttoMillis()
Return the duration truncated to whole milliseconds.
public
toMillis() : int
Return values
inttoNanos()
Return the duration as a nanosecond count.
public
toNanos() : int
Return values
inttoSeconds()
Return the duration truncated to whole seconds.
public
toSeconds() : int
Return values
inttoSecondsFloat()
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
floatzero()
Create a zero-length Duration.
public
static zero() : self