Core\Time\Duration
A length of time, such as 30 seconds or an hour and a half. You write a fixed one directly in the code, like 30s or 1h30m, and build a computed one with seconds, minutes or another constructor. It is an exact count of nanoseconds and can be negative. plus, minus and compareTo work on two of them.
A Duration is a signed count of nanoseconds. You write one directly in the code as a number and a unit — 30s, 250ms, 1h30m, 7d — with the units ns, us, ms, s, m, h, d and w, and Duration::parse reads the same grammar from text at run time. It is exact: days(1) and 1d are twenty-four hours, never a calendar day, which is what Core\Time\DateTime::plus($n, Core\Unit::Day) is. echo renders a duration back in that same syntax, so its text round-trips through parse. An argument to parse written directly in the code is read when the program compiles, so a bad one is a compile error; text computed at run time throws a ParseError. toClock writes a duration as hours and minutes, such as 25:30, and Duration::fromClock reads that text back. Two durations are ordered with compareTo; the < operators do not compile on a Duration.
<?nvsvar $d = 1h30m;echo $d, "\n";echo $d->toSeconds(), "\n";
echo $d->plus(15m), "\n";echo $d->minus(2h), "\n";echo $d->multipliedBy(2), "\n";echo $d->negated(), "\n";
echo Core\Time\Duration::parse('250ms')->toMilliseconds(), "\n";echo Core\Time\Duration::minutes(90)->compareTo($d), "\n";echo Core\Time\Duration::seconds(90061), "\n";echo 1500ms->toString(), "\n";
string $flag = '--since=30 seconds';try { Core\Time\Duration::parse(Core\Str::slice($flag, 8));} catch (ParseError $bad) { echo "not a duration\n";}1h30m54001h45m-30m3h-1h30m25001d1h1m1s1s500msnot a durationReturned by
You get a Core\Time\Duration from these methods:
Core\Ldap\Entry::durationCore\RateLimit\Decision::retryAfterCore\Time::monotonicCore\Time\Instant::sinceCore\Time\Zone::offsetAt
Members
21 members.
| Member | Signature |
|---|---|
nanoseconds | nanoseconds(int $n): Core\Time\Duration |
microseconds | microseconds(int $n): Core\Time\Duration |
milliseconds | milliseconds(int $n): Core\Time\Duration |
seconds | seconds(int $n): Core\Time\Duration |
minutes | minutes(int $n): Core\Time\Duration |
hours | hours(int $n): Core\Time\Duration |
days | days(int $n): Core\Time\Duration |
weeks | weeks(int $n): Core\Time\Duration |
parse | parse(string $text): Core\Time\Duration |
fromClock | fromClock(string $text): Core\Time\Duration |
toNanoseconds | toNanoseconds(): int |
toMicroseconds | toMicroseconds(): int |
toMilliseconds | toMilliseconds(): int |
toSeconds | toSeconds(): int |
plus | plus(Core\Time\Duration $d): Core\Time\Duration |
minus | minus(Core\Time\Duration $d): Core\Time\Duration |
multipliedBy | multipliedBy(int $factor): Core\Time\Duration |
negated | negated(): Core\Time\Duration |
compareTo | compareTo(Core\Time\Duration $other): int |
toString | toString(): string |
toClock | toClock({ seconds?: bool }): string |
