/** * @return string */ public function __toString() { return (string) ZonedDateTime::ofInstant($this, TimeZone::utc()); }
/** * @param \DateTimeZone $dateTimeZone * * @return TimeZone */ public static function fromDateTimeZone(\DateTimeZone $dateTimeZone) { return TimeZone::parse($dateTimeZone->getName()); }
/** * @dataProvider providerNow * * @param integer $epochSecond The epoch second to set the clock time to. * @param string $timeZone The time-zone to get the current year in. * @param integer $expectedYear The expected year. */ public function testNow($epochSecond, $timeZone, $expectedYear) { $this->setClockTime($epochSecond); $this->assertYearIs($expectedYear, Year::now(TimeZone::parse($timeZone))); }
public function testChangeTimeZone() { $timezone1 = TimeZone::parse('UTC'); $timezone2 = TimeZone::parse('America/Los_Angeles'); $datetime1 = ZonedDateTime::ofInstant(Instant::of(1000000000), $timezone1); $datetime2 = $datetime1->withTimeZoneSameInstant($timezone2); $this->assertSame($timezone1, $datetime1->getTimezone()); $this->assertSame($timezone2, $datetime2->getTimezone()); $this->assertSame('2001-09-08T18:46:40', (string) $datetime2->getDateTime()); $datetime2 = $datetime1->withTimeZoneSameLocal($timezone2); $this->assertSame($timezone1, $datetime1->getTimezone()); $this->assertSame($timezone2, $datetime2->getTimezone()); $this->assertSame('2001-09-09T01:46:40', (string) $datetime2->getDateTime()); }
/** * @dataProvider providerNow * * @param integer $epochSecond The epoch second. * @param string $timeZone The time-zone. * @param integer $year The expected year. * @param integer $month The expected month. */ public function testNow($epochSecond, $timeZone, $year, $month) { $previousClock = Clock::setDefault(new FixedClock(Instant::of($epochSecond))); $this->assertYearMonthIs($year, $month, YearMonth::now(TimeZone::parse($timeZone))); Clock::setDefault($previousClock); }
/** * @dataProvider providerNow * * @param integer $epochSecond The epoch second to set the clock time to. * @param string $timeZone The time-zone to get the current day-of-week in. * @param integer $expectedDayOfWeek The expected day-of-week, from 1 to 7. */ public function testNow($epochSecond, $timeZone, $expectedDayOfWeek) { $this->setClockTime($epochSecond); $this->assertDayOfWeekIs($expectedDayOfWeek, DayOfWeek::now(TimeZone::parse($timeZone))); }
/** * @dataProvider providerNow * * @param integer $epochSecond The epoch second to set the clock to. * @param string $timeZone The time-zone to get the date in. * @param integer $year The expected year. * @param integer $month The expected month. * @param integer $day The expected day. */ public function testNow($epochSecond, $timeZone, $year, $month, $day) { $this->setClockTime($epochSecond); $this->assertLocalDateIs($year, $month, $day, LocalDate::now(TimeZone::parse($timeZone))); }
/** * @dataProvider providerFromDateTimeZone * * @param string $tz The time-zone name. */ public function testFromDateTimeZone($tz) { $dateTimeZone = new \DateTimeZone($tz); $this->assertSame($tz, TimeZone::fromDateTimeZone($dateTimeZone)->getId()); }
/** * @dataProvider providerAtTimeZone * * @param string $dateTime The date-time. * @param string $timeZone The time-zone. * @param integer $epochSeconds The expected epoch second of the resulting instant. * @param integer $nanos The expected nano-of-second of the resulting instant. */ public function testAtTimeZone($dateTime, $timeZone, $epochSeconds, $nanos) { $zonedDateTime = LocalDateTime::parse($dateTime)->atTimeZone(TimeZone::parse($timeZone)); $this->assertReadableInstantIs($epochSeconds, $nanos, $zonedDateTime); }
/** * 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); }
/** * @dataProvider providerNow * * @param integer $epochSecond The epoch second. * @param string $timeZone The time-zone. * @param integer $month The expected month. * @param integer $day The expected day. */ public function testNow($epochSecond, $timeZone, $month, $day) { $previousClock = Clock::setDefault(new FixedClock(Instant::of($epochSecond))); $this->assertMonthDayIs($month, $day, MonthDay::now(TimeZone::parse($timeZone))); Clock::setDefault($previousClock); }
/** * @param TimeZone $expected The expected time-zone. * @param TimeZone $actual The actual time-zone. */ protected function assertTimeZoneEquals(TimeZone $expected, TimeZone $actual) { $this->assertTrue($actual->isEqualTo($expected), "{$actual} != {$expected}"); }