Core\Time\TimeOfDay::nanoseconds
$timeOfDay->nanoseconds(): uintReturns the part of a second after the whole seconds, as a uint number of nanoseconds.
The result is from 0 to 999999999. One second has 1000000000 nanoseconds, so half a second is 500000000. A time made with TimeOfDay::at without a nanoseconds option has 0 here.
The result has the same name and type as the nanoseconds option of with. So $t->with({ nanoseconds: 0 }) gives the same time with the part of a second removed. The nanoseconds option must be from 0 to 999999999, and a value outside that range throws a RuntimeError.
Good to know: to show the part of a second, use format. Each S in the pattern prints one digit of the fraction, so SSS prints milliseconds.
Return value
Returns uint. The nanoseconds, from 0 to 999999999. 500000000 is half a second.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Time\DateTime->nanoseconds() and Core\Time\TimeOfDay->nanoseconds() return the part of the second in nanoseconds. The option of with() is nanoseconds too. The old name was nanos in both places. |
Examples
Read the nanoseconds
<?nvs// Shows how to read the part of a second after the whole seconds, in nanoseconds.
Core\Time\TimeOfDay $finish = Core\Time\TimeOfDay::at(10, 0, { second: 5, nanoseconds: 750000000 });
// One second has 1000000000 nanoseconds. Three quarters of a second is 750000000, so this prints 750000000.echo $finish->nanoseconds(), "\n";
// `format` with `SSS` prints the same part as milliseconds. This prints 10:00:05.750.echo $finish->format('HH:mm:ss.SSS'), "\n";75000000010:00:05.750The time of a log line
<?nvs// Shows how to read the part of a second from the time in a log line.
// The log line has the time with milliseconds.var $logged = Core\Time\DateTime::parse('2024-04-22 14:03:09.042', 'yyyy-MM-dd HH:mm:ss.SSS', 'UTC');
// `timeOfDay` keeps the time and drops the date and the zone.Core\Time\TimeOfDay $time = $logged->timeOfDay();
// 42 milliseconds are 42000000 nanoseconds. This prints 9 and then 42000000.echo $time->second(), "\n";echo $time->nanoseconds(), "\n";942000000Compare to the second
<?nvs// Shows how to compare two times of day while ignoring the part of a second.
// A sensor sent the first time. A person typed the second time, without the part of a second.Core\Time\TimeOfDay $sensor = Core\Time\TimeOfDay::at(6, 45, { second: 20, nanoseconds: 318000000 });Core\Time\TimeOfDay $typed = Core\Time\TimeOfDay::at(6, 45, { second: 20 });
// `compareTo` returns 1 when the first time is later. The sensor time has more nanoseconds, so this prints 1.echo $sensor->compareTo($typed), "\n";
// `with({ nanoseconds: 0 })` sets the part of a second to 0. Now the times are equal, so this prints 0.echo $sensor->with({ nanoseconds: 0 })->compareTo($typed), "\n";10