Example #1
0
 /**
  * @return string
  */
 public function __toString()
 {
     return (string) ZonedDateTime::ofInstant($this, TimeZone::utc());
 }
Example #2
0
 /**
  * @param \DateTimeZone $dateTimeZone
  *
  * @return TimeZone
  */
 public static function fromDateTimeZone(\DateTimeZone $dateTimeZone)
 {
     return TimeZone::parse($dateTimeZone->getName());
 }
Example #3
0
 /**
  * @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)));
 }
Example #4
0
 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());
 }
Example #5
0
 /**
  * @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);
 }
Example #6
0
 /**
  * @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)));
 }
Example #7
0
 /**
  * @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)));
 }
Example #8
0
 /**
  * @dataProvider providerFromDateTimeZone
  *
  * @param string $tz The time-zone name.
  */
 public function testFromDateTimeZone($tz)
 {
     $dateTimeZone = new \DateTimeZone($tz);
     $this->assertSame($tz, TimeZone::fromDateTimeZone($dateTimeZone)->getId());
 }
Example #9
0
 /**
  * @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);
 }
Example #10
0
 /**
  * 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);
 }
Example #11
0
 /**
  * @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);
 }
Example #12
0
 /**
  * @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}");
 }