Example #1
0
 /**
  * {@inheritdoc}
  */
 public function expand($className, $value, array $options = [])
 {
     try {
         switch ($className) {
             case Duration::class:
                 return Duration::parse($value);
             case LocalDate::class:
                 return LocalDate::parse($value);
             case LocalTime::class:
                 return LocalTime::parse($value);
             case LocalDateTime::class:
                 return LocalDateTime::parse($value);
             case TimeZoneOffset::class:
                 return TimeZoneOffset::parse($value);
             case TimeZoneRegion::class:
                 return TimeZoneRegion::parse($value);
             case YearMonth::class:
                 return YearMonth::parse($value);
             case ZonedDateTime::class:
                 return ZonedDateTime::parse($value);
         }
     } catch (DateTimeParseException $e) {
         throw new ObjectNotConvertibleException($e->getMessage(), $e->getCode(), $e);
     }
     return null;
 }
Example #2
0
 /**
  * @dataProvider providerOf
  *
  * @param string  $localDateTime The local date-time as a string.
  * @param string  $timeZone      The time-zone as a string.
  * @param string  $offset        The expected time-zone offset of the result zoned date-time.
  * @param integer $shift         The expected shift applied to the date-time (when in a gap), in seconds.
  * @param integer $epochSecond   The expected epoch-second the result zoned date-time resolves to.
  * @param integer $nanoOfSecond  The expected nano-of-second of the result zoned date-time.
  */
 public function testOf($localDateTime, $timeZone, $offset, $shift, $epochSecond, $nanoOfSecond)
 {
     $localDateTime = LocalDateTime::parse($localDateTime);
     $timeZone = TimeZone::parse($timeZone);
     $offset = TimeZoneOffset::parse($offset);
     $expectedDateTime = $localDateTime->plusSeconds($shift);
     $zonedDateTime = ZonedDateTime::of($localDateTime, $timeZone);
     $this->assertInstanceOf(ZonedDateTime::class, $zonedDateTime);
     $this->assertLocalDateTimeEquals($expectedDateTime, $zonedDateTime->getDateTime());
     $this->assertTimeZoneEquals($timeZone, $zonedDateTime->getTimeZone());
     $this->assertTimeZoneEquals($offset, $zonedDateTime->getTimeZoneOffset());
     $this->assertSame($epochSecond, $zonedDateTime->getEpochSecond());
     $this->assertSame($nanoOfSecond, $zonedDateTime->getNano());
 }
Example #3
0
 /**
  * @return TimeZoneOffset
  */
 public static function utc()
 {
     return TimeZoneOffset::utc();
 }
Example #4
0
 public function testToDateTimeZone()
 {
     $dateTimeZone = TimeZoneOffset::ofTotalSeconds(-18000)->toDateTimeZone();
     $this->assertInstanceOf(\DateTimeZone::class, $dateTimeZone);
     $this->assertSame('-05:00', $dateTimeZone->getName());
 }
Example #5
0
 /**
  * Parses a time-zone offset.
  *
  * The following ISO 8601 formats are accepted:
  * 
  * * `Z` - for UTC
  * * `±hh:mm`
  * * `±hh:mm:ss`
  *
  * Note that ± means either the plus or minus symbol.
  *
  * @param string              $text
  * @param DateTimeParser|null $parser
  *
  * @return TimeZoneOffset
  *
  * @throws DateTimeParseException
  */
 public static function parse($text, DateTimeParser $parser = null)
 {
     if (!$parser) {
         $parser = IsoParsers::timeZoneOffset();
     }
     return TimeZoneOffset::from($parser->parse($text));
 }
Example #6
0
 public function testIsEqualTo()
 {
     $this->assertTrue(TimeZone::utc()->isEqualTo(TimeZoneOffset::ofTotalSeconds(0)));
     $this->assertFalse(TimeZone::utc()->isEqualTo(TimeZoneOffset::ofTotalSeconds(1)));
 }
Example #7
0
 /**
  * @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));
 }
Example #8
0
 /**
  * Obtains an instance of `ZonedDateTime` from a set of date-time fields.
  *
  * This method is only useful to parsers.
  *
  * @param DateTimeParseResult $result
  *
  * @return ZonedDateTime
  *
  * @throws DateTimeException      If the zoned date-time is not valid.
  * @throws DateTimeParseException If required fields are missing from the result.
  */
 public static function from(DateTimeParseResult $result)
 {
     $localDateTime = LocalDateTime::from($result);
     $timeZoneOffset = TimeZoneOffset::from($result);
     if ($result->hasField(Field\TimeZoneRegion::NAME)) {
         $timeZone = TimeZoneRegion::from($result);
     } else {
         $timeZone = $timeZoneOffset;
     }
     return ZonedDateTime::of($localDateTime, $timeZone, $timeZoneOffset);
 }
Example #9
0
 /**
  * @param integer        $totalSeconds   The expected total offset in seconds.
  * @param TimeZoneOffset $timeZoneOffset The time-zone offset to test.
  */
 protected function assertTimeZoneOffsetIs($totalSeconds, TimeZoneOffset $timeZoneOffset)
 {
     $this->compare([$totalSeconds], [$timeZoneOffset->getTotalSeconds()]);
 }