Core\Time\DateTime::nanoseconds
$dateTime->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 date and time made from a TimeOfDay without a nanoseconds option has 0 here. Like hour and second, nanoseconds reads the time in the time zone of the value. A change of zone does not change the part of a second, so the result is the same in every zone.
The result has the same name and type as the nanoseconds option of with. So $d->with({ nanoseconds: 0 }) gives the same time with the part of a second removed.
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.
var $start = Core\Time\DateTime::fromIso('2024-06-03 12:00:05.25', 'UTC');
// One second has 1000000000 nanoseconds. A quarter of a second is 250000000, so this prints 250000000.echo $start->nanoseconds(), "\n";
// `format` with `SSS` prints the same part as milliseconds. This prints 12:00:05.250.echo $start->format('HH:mm:ss.SSS'), "\n";25000000012:00:05.250The fraction of a parsed timestamp
<?nvs// Shows how to read the part of a second from a timestamp that an API sent.
// The timestamp has nine digits after the seconds.var $received = Core\Time\Instant::fromIso('2024-03-05T10:15:30.123456789Z')->in('UTC');
// `second` is 30, and `nanoseconds` is the part after the seconds. This prints 30 and then 123456789.echo $received->second(), "\n";echo $received->nanoseconds(), "\n";
// This timestamp ends in `.5`, which is half a second. This prints 500000000.echo Core\Time\Instant::fromIso('2024-03-05T10:15:30.5Z')->in('UTC')->nanoseconds(), "\n";30123456789500000000Compare to the second
<?nvs// Shows how to drop the part of a second, so that two times from different systems can be compared.
// The database keeps nanoseconds. The browser sent the same time to the second only.var $zone = Core\Time\Zone::of('Europe/Warsaw');var $saved = Core\Time\Instant::fromIso('2024-09-01T08:00:12.734000000Z')->in($zone);var $sent = Core\Time\DateTime::parse('2024-09-01 10:00:12', 'yyyy-MM-dd HH:mm:ss', $zone);
// The nanoseconds are different. This prints 734000000 and 0.echo $saved->nanoseconds(), ' ', $sent->nanoseconds(), "\n";
// `with({ nanoseconds: 0 })` sets the part of a second to 0. Now both times are 10:00:12.000.var $rounded = $saved->with({ nanoseconds: 0 });echo $rounded->format('HH:mm:ss.SSS'), ' ', $sent->format('HH:mm:ss.SSS'), "\n";
// `toInstant` gives the moment of each time, and `compareTo` returns 0 when they are equal.// This prints same.echo $rounded->toInstant()->compareTo($sent->toInstant()) == 0 ? 'same' : 'different', "\n";734000000 010:00:12.000 10:00:12.000same