private function doTestOffset(ZoneOffset $offset, $hours, $minutes, $seconds) { $this->assertEquals($offset->getTotalSeconds(), $hours * 60 * 60 + $minutes * 60 + $seconds); if ($hours == 0 && $minutes == 0 && $seconds == 0) { $id = "Z"; } else { $str = $hours < 0 || $minutes < 0 || $seconds < 0 ? "-" : "+"; $str .= substr(Math::abs($hours) + 100, 1); $str .= ":"; $str .= substr(Math::abs($minutes) + 100, 1); if ($seconds != 0) { $str .= ":"; $str .= substr(Math::abs($seconds) + 100, 1); } $id = $str; } $this->assertEquals($offset->getId(), $id); $this->assertEquals($offset, ZoneOffset::ofHoursMinutesSeconds($hours, $minutes, $seconds)); if ($seconds == 0) { $this->assertEquals($offset, ZoneOffset::ofHoursMinutes($hours, $minutes)); if ($minutes == 0) { $this->assertEquals($offset, ZoneOffset::ofHours($hours)); } } $this->assertEquals(ZoneOffset::of($id), $offset); $this->assertEquals($offset->__toString(), $id); }
/** * Obtains an instance of {@code ZoneId} wrapping an offset. * <p> * If the prefix is "GMT", "UTC", or "UT" a {@code ZoneId} * with the prefix and the non-zero offset is returned. * If the prefix is empty {@code ""} the {@code ZoneOffset} is returned. * * @param string $prefix the time-zone ID, not null * @param ZoneOffset $offset the offset, not null * @return ZoneId the zone ID, not null * @throws IllegalArgumentException if the prefix is not one of * "GMT", "UTC", or "UT", or "" */ public static function ofOffset($prefix, ZoneOffset $offset) { if (!is_string($prefix)) { throw new \InvalidArgumentException(); } if (strlen($prefix) === 0) { return $offset; } if ($prefix !== "GMT" && $prefix !== "UTC" && $prefix !== "UT") { throw new IllegalArgumentException("prefix should be GMT, UTC or UT, is: " . $prefix); } if ($offset->getTotalSeconds() !== 0) { $prefix .= $offset->getId(); } return new ZoneRegion($prefix, $offset->getRules()); }