public function testToDateTimeZone() { $dateTimeZone = TimeZoneOffset::ofTotalSeconds(-18000)->toDateTimeZone(); $this->assertInstanceOf(\DateTimeZone::class, $dateTimeZone); $this->assertSame('-05:00', $dateTimeZone->getName()); }
/** * @dataProvider providerNow * * @param integer $second The second to set the clock to. * @param integer $nano The nanosecond adjustment to the clock. * @param integer $offset The time-zone offset to get the time at. * @param integer $h The expected hour. * @param integer $m The expected minute. * @param integer $s The expected second. * @param integer $n The expected nano. */ public function testNow($second, $nano, $offset, $h, $m, $s, $n) { $this->setClockTime($second, $nano); $timeZone = TimeZoneOffset::ofTotalSeconds($offset); $this->assertLocalTimeIs($h, $m, $s, $n, LocalTime::now($timeZone)); }
public function testIsEqualTo() { $this->assertTrue(TimeZone::utc()->isEqualTo(TimeZoneOffset::ofTotalSeconds(0))); $this->assertFalse(TimeZone::utc()->isEqualTo(TimeZoneOffset::ofTotalSeconds(1))); }
/** * Creates a ZonedDateTime from an instant and a time zone. * * This resolves the instant to a date and time without ambiguity. * * @param Instant $instant The instant. * @param TimeZone $timeZone The time zone. * * @return ZonedDateTime */ public static function ofInstant(Instant $instant, TimeZone $timeZone) { $dateTimeZone = $timeZone->toDateTimeZone(); // We need to pass a DateTimeZone to avoid a PHP warning... $dateTime = new \DateTime('@' . $instant->getEpochSecond(), $dateTimeZone); // ... but this DateTimeZone is ignored because of the timestamp, so we set it again. $dateTime->setTimezone($dateTimeZone); $localDateTime = LocalDateTime::parse($dateTime->format('Y-m-d\\TH:i:s')); $localDateTime = $localDateTime->withNano($instant->getNano()); if ($timeZone instanceof TimeZoneOffset) { $timeZoneOffset = $timeZone; } else { $timeZoneOffset = TimeZoneOffset::ofTotalSeconds($dateTimeZone->getOffset($dateTime)); } return new ZonedDateTime($localDateTime, $timeZoneOffset, $timeZone, $dateTime); }