Core\Time\Zone::fixed
Core\Time\Zone::fixed(Core\Time\Duration $offset): Core\Time\ZoneCore\Time\Zone::fixed( Core\Time\Duration $offset, ): Core\Time\Zone
Makes a time zone that is always the same distance from UTC, such as +05:30 or -03:00.
You give the distance as a Duration. It is positive for a zone east of UTC and negative for a zone west of UTC. The zone's name is the offset itself, for example +05:30. The offset never changes, so a fixed zone has no summer time. A named zone such as Europe/Berlin changes its offset twice a year, and a fixed zone does not.
Use a fixed zone when you only know the offset and not the name of the place. A timestamp such as 2024-03-05T10:00:00+05:30 is one example. The offset must be a whole number of seconds, and at most 25 hours, 59 minutes and 59 seconds away from UTC. Any other offset throws a RuntimeError.
Good to know: == compares two zones by their name. Two fixed zones with the same offset have the same name, so they are equal.
Parameters
$offsetCore\Time\DurationThe distance from UTC as aDuration, such as5h30m. It is negative for a zone west of UTC.
Return value
Returns Core\Time\Zone. A Zone whose name is the offset, written +HH:MM, or +HH:MM:SS when it has seconds.
Errors
RuntimeError$offsethas a part smaller than a second, or is more than 25:59:59 away from UTC.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\Zone::fixed with a written offset, such as Core\Time\Zone::fixed(5h30m), is built once when the program compiles. Each use of it costs no call and no memory. |
0.0.6 | == and != now compare a Date, TimeOfDay, Instant, Duration, Zone or DateTime by its value, so 90m == 1h30m is true. Two DateTime values are equal when they name the same moment, even in different time zones. Objects of every other class are still equal only when they are the same object. |
Examples
A zone from an offset
<?nvs// Shows the same moment in two zones that are a fixed distance from UTC.
Core\Time\Instant $at = Core\Time\Instant::fromIso('2024-03-05T10:00:00Z');
// `Core\Time\Zone::fixed` makes a zone from an offset. `5h30m` is the offset for India.Core\Time\Zone $india = Core\Time\Zone::fixed(5h30m);// A negative offset is a zone west of UTC. This one is three hours behind.Core\Time\Zone $west = Core\Time\Zone::fixed(Core\Time\Duration::hours(-3));
// `VV` prints the zone's name, and for a fixed zone the name is the offset.// This prints 15:30 +05:30 and 07:00 -03:00.echo $at->in($india)->format('HH:mm VV'), "\n";echo $at->in($west)->format('HH:mm VV'), "\n";15:30 +05:3007:00 -03:00No summer time
<?nvs// Shows that a fixed zone keeps its offset all year, and a named zone does not.
Core\Time\Zone $fixed = Core\Time\Zone::fixed(1h);Core\Time\Zone $berlin = Core\Time\Zone::of('Europe/Berlin');
array<string> $days = ['2024-01-15T12:00:00Z', '2024-07-15T12:00:00Z'];
// `offsetAt` returns the offset on that day, as a `Duration`.// The fixed zone prints 1h both times. Berlin prints 1h in January and 2h in July.foreach ($days as string $day) { Core\Time\Instant $at = Core\Time\Instant::fromIso($day); echo $fixed->offsetAt($at)->toString(), ' ', $berlin->offsetAt($at)->toString(), "\n";}1h 1h1h 2hA customers saved offset
<?nvs// Shows an order time in each customer's own time, from an offset saved in minutes.
// Each customer saved the distance from UTC of their clock, in minutes.array<int> $customers = ['Ana' => -180, 'Ben' => 0, 'Chitra' => 330];Core\Time\Instant $ordered = Core\Time\Instant::fromIso('2024-11-20T18:45:00Z');
// `Core\Time\Duration::minutes` turns the saved number into an offset.// This prints 15:45, 18:45 and 00:15 on the next day.foreach ($customers as string $name => int $minutes) { Core\Time\Zone $zone = Core\Time\Zone::fixed(Core\Time\Duration::minutes($minutes)); echo $name, ': ', $ordered->in($zone)->format('uuuu-MM-dd HH:mm xxx'), "\n";}Ana: 2024-11-20 15:45 -03:00Ben: 2024-11-20 18:45 +00:00Chitra: 2024-11-21 00:15 +05:30