Beispiel #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;
 }
Beispiel #2
0
 /**
  * Obtains an instance of `TimeZone` from a string representation.
  *
  * @param string $text
  *
  * @return TimeZone
  *
  * @throws \Brick\DateTime\Parser\DateTimeParseException
  */
 public static function parse($text)
 {
     $text = (string) $text;
     if ($text === 'Z' || $text === 'z') {
         return TimeZoneOffset::utc();
     }
     if ($text === '') {
         throw new DateTimeParseException('The string is empty.');
     }
     if ($text[0] === '+' || $text[0] === '-') {
         return TimeZoneOffset::parse($text);
     }
     return TimeZoneRegion::parse($text);
 }
Beispiel #3
0
 public function testToString()
 {
     $this->assertSame('America/Los_Angeles', (string) TimeZoneRegion::of('America/Los_Angeles'));
 }
Beispiel #4
0
 /**
  * Parses a region id, such as 'Europe/London'.
  *
  * @param string               $text
  * @param DateTimeParser|null $parser
  *
  * @return TimeZoneRegion
  *
  * @throws DateTimeParseException
  */
 public static function parse($text, DateTimeParser $parser = null)
 {
     if (!$parser) {
         $parser = IsoParsers::timeZoneRegion();
     }
     return TimeZoneRegion::from($parser->parse($text));
 }
Beispiel #5
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);
 }