예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     return LocalDateTime::parse(str_replace(' ', 'T', $value));
 }
예제 #2
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;
 }
예제 #3
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());
 }
예제 #4
0
 /**
  * @dataProvider providerIsAfter
  *
  * @param string  $dateTime1 The base date-time.
  * @param string  $dateTime2 The date-time to compare to.
  * @param boolean $isAfter   The expected result.
  */
 public function testIsAfter($dateTime1, $dateTime2, $isAfter)
 {
     $dateTime1 = LocalDateTime::parse($dateTime1);
     $dateTime2 = LocalDateTime::parse($dateTime2);
     $this->assertSame($isAfter, $dateTime1->isAfter($dateTime2));
 }
예제 #5
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);
 }