Skip to content

Core\Time\Duration::nanoseconds

Creates a duration of a given number of nanoseconds. A nanosecond is one billionth of a second. It is the smallest unit a duration has, so no part of the number is lost.

Use it when a timer or a log gives a time in nanoseconds, or when the number is known only while the program runs. When the number is fixed, write the duration directly in the code, such as 250ms. Both ways give the same duration for the same length.

Every int is a valid number of nanoseconds, so this method never throws. A negative number gives a negative duration. The largest duration is about 292 years in each direction. A sum or a product that goes past it throws a RuntimeError.

Parameters

  • $nintThe count of nanoseconds; negative for a duration that runs backwards.

Return value

Returns Core\Time\Duration. A Duration of $n nanoseconds.

Examples

From a measured count

<?nvs
// Shows how to create a duration from a number of nanoseconds, the smallest unit a duration has.
// A timer measured 1500000 nanoseconds. That is 1.5 milliseconds.
Core\Time\Duration $elapsed = Core\Time\Duration::nanoseconds(1500000);
// `toMilliseconds` gives whole milliseconds, so this prints 1.
echo $elapsed->toMilliseconds(), "\n";
// No part is lost. This prints 1500000 again.
echo $elapsed->toNanoseconds(), "\n";
// 90 seconds in nanoseconds has the same length as the duration `90s`.
// `compareTo` returns 0 for two durations of the same length, so this prints 0.
echo Core\Time\Duration::nanoseconds(90000000000)->compareTo(90s), "\n";
Output
1
1500000
0

Every int fits

<?nvs
// Shows that every `int` is a valid number of nanoseconds, and where a duration stops.
// The largest `int` is about 292 years in nanoseconds.
// This prints its length in days, which is 106751.99115741.
Core\Time\Duration $longest = Core\Time\Duration::nanoseconds(9223372036854775807);
echo $longest->toSeconds() / 86400, "\n";
// The smallest `int` works too. This prints the number of nanoseconds unchanged.
echo Core\Time\Duration::nanoseconds(-9223372036854775807 - 1)->toNanoseconds(), "\n";
// A longer duration does not fit. Adding one more nanosecond throws a `RuntimeError`.
try {
$longest->plus(Core\Time\Duration::nanoseconds(1));
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
Output
106751.99115741
-9223372036854775808
Core\Time\Duration::plus(): this duration is longer than `Core\Time\Duration` can hold

Total of timings from a log

<?nvs
// Adds up response times that a log records in nanoseconds, and prints the total and the average.
array<int> $timings = [1250000, 830000, 2900000, 410000];
Core\Time\Duration $total = Core\Time\Duration::nanoseconds(0);
foreach ($timings as int $n) {
$total = $total->plus(Core\Time\Duration::nanoseconds($n));
}
// The four requests took 5390000 nanoseconds together. This prints 5390 microseconds.
echo $total->toMicroseconds(), "\n";
// The average is the total divided by the number of requests. This prints 1347500.
echo $total->toNanoseconds() / 4, "\n";
Output
5390
1347500