Esempio n. 1
0
 public function testCopyEnsureMicrosAreCopied()
 {
     $micro = 254687;
     $dating = Date::createFromFormat('Y-m-d H:i:s.u', '2014-02-01 03:45:27.' . $micro);
     $dating2 = $dating->copy();
     $this->assertSame($micro, $dating2->getMicro());
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof Date) {
         return $value;
     }
     $val = Date::createFromFormat($platform->getDateTimeFormatString(), $value);
     if (!$val) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
     }
     return $val;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  *
  * @throws NotFoundHttpException When invalid date given
  */
 public function apply(Request $request, ParamConverter $configuration)
 {
     $param = $configuration->getName();
     if (!$request->attributes->has($param)) {
         return false;
     }
     $options = $configuration->getOptions();
     $value = $request->attributes->get($param);
     if (!$value && $configuration->isOptional()) {
         return false;
     }
     $invalidDateMessage = 'Invalid date given.';
     try {
         $date = isset($options['format']) ? Date::createFromFormat($options['format'], $value) : new Date($value);
     } catch (Exception $e) {
         throw new NotFoundHttpException($invalidDateMessage);
     }
     if (!$date) {
         throw new NotFoundHttpException($invalidDateMessage);
     }
     $request->attributes->set($param, $date);
     return true;
 }
Esempio n. 4
0
 public function testCreateFromFormatWithMillis()
 {
     $d = Date::createFromFormat('Y-m-d H:i:s.u', '1975-05-21 22:32:11.254687');
     $this->assertSame(254687, $d->getMicro());
 }
 /**
  * Transforms a date string in the configured timezone into a DateTime object.
  *
  * @param string $value A value as produced by PHP's date() function
  *
  * @return \DateTime An instance of \DateTime
  *
  * @throws TransformationFailedException If the given value is not a string,
  *                                       if the date could not be parsed or
  *                                       if the input timezone is not supported.
  */
 public function reverseTransform($value)
 {
     if (empty($value)) {
         return;
     }
     if (!is_string($value)) {
         throw new TransformationFailedException('Expected a string.');
     }
     $outputTz = new \DateTimeZone($this->outputTimezone);
     $dateTime = Date::createFromFormat($this->parseFormat, $value, $outputTz);
     $lastErrors = Date::getLastErrors();
     if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
         throw new TransformationFailedException(implode(', ', array_merge(array_values($lastErrors['warnings']), array_values($lastErrors['errors']))));
     }
     try {
         // On PHP versions < 5.3.7 we need to emulate the pipe operator
         // and reset parts not given in the format to their equivalent
         // of the UNIX base timestamp.
         if (!$this->parseUsingPipe) {
             list($year, $month, $day, $hour, $minute, $second) = explode('-', $dateTime->format('Y-m-d-H-i-s'));
             // Check which of the date parts are present in the pattern
             preg_match('/(' . '(?P<day>[djDl])|' . '(?P<month>[FMmn])|' . '(?P<year>[Yy])|' . '(?P<hour>[ghGH])|' . '(?P<minute>i)|' . '(?P<second>s)|' . '(?P<dayofyear>z)|' . '(?P<timestamp>U)|' . '[^djDlFMmnYyghGHiszU]' . ')*/', $this->parseFormat, $matches);
             // preg_match() does not guarantee to set all indices, so
             // set them unless given
             $matches = array_merge(array('day' => false, 'month' => false, 'year' => false, 'hour' => false, 'minute' => false, 'second' => false, 'dayofyear' => false, 'timestamp' => false), $matches);
             // Reset all parts that don't exist in the format to the
             // corresponding part of the UNIX base timestamp
             if (!$matches['timestamp']) {
                 if (!$matches['dayofyear']) {
                     if (!$matches['day']) {
                         $day = 1;
                     }
                     if (!$matches['month']) {
                         $month = 1;
                     }
                 }
                 if (!$matches['year']) {
                     $year = 1970;
                 }
                 if (!$matches['hour']) {
                     $hour = 0;
                 }
                 if (!$matches['minute']) {
                     $minute = 0;
                 }
                 if (!$matches['second']) {
                     $second = 0;
                 }
                 $dateTime = $dateTime->setDate($year, $month, $day);
                 $dateTime = $dateTime->setTime($hour, $minute, $second);
             }
         }
         if ($this->inputTimezone !== $this->outputTimezone) {
             $dateTime = $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
         }
     } catch (\Exception $e) {
         throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
     }
     return $dateTime;
 }