Skip to content

Core\Time\Zone::of

Finds a time zone by its name, such as Europe/Berlin, America/New_York or Asia/Tokyo.

The names come from the IANA time-zone database, which most operating systems use. A named zone has the whole history of its region, including the days when summer time starts and ends. Novis has no default zone, so you need one each time you turn an Instant into a date and a clock time.

When you know the zone as you write the program, you do not need Zone::of. Write its name in quotes, such as $instant->in('Europe/Berlin'). The compiler checks this name, so a wrong name does not compile.

Use Zone::of for text that arrives while the program runs, such as a name a user saved. Upper and lower case do not matter. The zone has the name as the database writes it, so europe/berlin gives the zone Europe/Berlin. A name that is not in the database throws a RuntimeError. An offset such as +02:00 is not a name, so Zone::of throws for it too. Use Zone::fixed for an offset.

Good to know: == compares two zones by their name. Zone::of('europe/berlin') == Zone::of('Europe/Berlin') is true.

Parameters

  • $idstringThe name of the zone, such as Asia/Tokyo. An offset such as +02:00 is not allowed here. Use Zone::fixed for an offset.

Return value

Returns Core\Time\Zone. The Zone with that name. Upper and lower case do not matter, and the zone has the name as the database writes it. So europe/berlin gives the zone Europe/Berlin.

Errors

  • RuntimeError$id is not a zone name in the database, or starts with + or -.

Changelog

VersionChange
0.0.5Core\Time\Zone::of with a name written in quotes is checked when the program compiles. An unknown name, or an offset such as '+02:00', does not compile. A name in a variable throws a RuntimeError when the program runs.
0.0.6Zone::of gives the zone the name the time-zone database uses. Zone::of('europe/berlin') gives the zone Europe/Berlin.

Examples

One instant in three cities

<?nvs
// Shows one meeting time in Berlin as the clock time in three cities.
// 'Europe/Berlin' is written in quotes, so it is checked while compiling.
Core\Time\DateTime $meeting = Core\Time\DateTime::fromIso('2024-06-01 14:00', 'Europe/Berlin');
Core\Time\Instant $start = $meeting->toInstant();
// These names are in a list, so `Core\Time\Zone::of` finds each zone while the program runs.
// This prints 14:00, 08:00 and 21:00.
array<string> $cities = ['Europe/Berlin', 'America/New_York', 'Asia/Tokyo'];
foreach ($cities as string $name) {
Core\Time\Zone $zone = Core\Time\Zone::of($name);
echo $name, ': ', $start->in($zone)->format('HH:mm'), "\n";
}
Output
Europe/Berlin: 14:00
America/New_York: 08:00
Asia/Tokyo: 21:00

A misspelled name

<?nvs
// Shows what happens when a zone name is misspelled.
// A user typed this name into a form. "Europe/Berln" is not in the database,
// so `Core\Time\Zone::of` throws a `RuntimeError`.
string $typed = 'Europe/Berln';
try {
Core\Time\Zone $zone = Core\Time\Zone::of($typed);
echo 'found the zone', "\n";
} catch (RuntimeError $e) {
// This prints the error message. It contains the name that was not found.
echo $e->message, "\n";
}
Output
Core\Time\Zone::of(): unknown time zone `Europe/Berln`

A users saved zone

<?nvs
// Shows a meeting time to each user in the time zone saved in their profile.
array<string> $profiles = ['Ana' => 'America/Sao_Paulo', 'Ben' => 'Europe/London', 'Chitra' => 'Asia/Kolkata'];
Core\Time\Instant $meeting = Core\Time\Instant::fromIso('2024-11-20T21:30:00Z');
// Each saved name becomes a zone, and the meeting is shown in that zone.
// This prints 18:30, 21:30 and 03:00 on the next day.
foreach ($profiles as string $user => string $zoneName) {
Core\Time\Zone $zone = Core\Time\Zone::of($zoneName);
echo $user, ': ', $meeting->in($zone)->format('uuuu-MM-dd HH:mm'), "\n";
}
Output
Ana: 2024-11-20 18:30
Ben: 2024-11-20 21:30
Chitra: 2024-11-21 03:00