Skip to content

Core\Time\Zone::system

Returns the time zone of the computer the program runs on.

Novis has no default time zone. Each conversion between an Instant and a date with a clock time needs a zone, and you write that zone in the call. Zone::system is how a program uses the zone of the computer: you call it once and pass the result to each call that needs a zone.

The result has the name of the zone when the computer has one, such as Europe/Berlin. Some computers have only an offset and no name. Then the result is a fixed zone at that offset, such as +02:00. Zone::system never throws an error.

The same program can give different results on different computers. A server often runs in UTC, and a laptop runs in the zone of its user. The examples do not print the local time for that reason.

Return value

Returns Core\Time\Zone. The computer's Zone with its IANA name, such as Europe/Berlin. When the computer has no zone name, the result is a fixed zone at its current offset, such as +02:00.

Examples

The local time is the same instant

<?nvs
// Shows the current time in the time zone of this computer.
Core\Time\Instant $now = Core\Time::now();
Core\Time\DateTime $local = $now->in(Core\Time\Zone::system());
// `toInstant` turns the local time back into an instant.
// The local time is only another way to show the same instant, so this prints "same instant".
if ($local->toInstant()->toEpochSeconds() == $now->toEpochSeconds()) {
echo 'same instant', "\n";
} else {
echo 'a different instant', "\n";
}
Output
same instant

The offset of this computer

<?nvs
// Shows how far the clock of this computer is from UTC right now.
Core\Time\Zone $here = Core\Time\Zone::system();
int $seconds = $here->offsetAt(Core\Time::now())->toSeconds();
// Every zone on Earth today is between 12 hours behind and 14 hours ahead of UTC.
// So this prints "between -12h and +14h" on every computer.
if ($seconds >= -12 * 3600 && $seconds <= 14 * 3600) {
echo 'between -12h and +14h', "\n";
} else {
echo 'outside the usual range', "\n";
}
Output
between -12h and +14h

A zone option for a tool

<?nvs
// Shows a command-line tool that uses the computer's zone unless the user names another one.
final class Report
{
// An empty option means the user did not name a zone.
// Then the tool uses the zone of the computer.
public static function zoneFor(string $option): Core\Time\Zone
{
if ($option == '') {
return Core\Time\Zone::system();
}
return Core\Time\Zone::of($option);
}
}
// Here the user typed `--zone Asia/Tokyo`, so this prints 2024-05-01 18:00.
Core\Time\Instant $backup = Core\Time\Instant::fromIso('2024-05-01T09:00:00Z');
echo 'Backup finished: ', $backup->in(Report::zoneFor('Asia/Tokyo'))->format('uuuu-MM-dd HH:mm'), "\n";
Output
Backup finished: 2024-05-01 18:00