Exemple #1
0
 /**
  * @param string $value in a format as specified by the PHP DateTime object
  *       there are exceptions as we can handel 5+ digit dates
  *
  * @throws ParseException
  * @return TimeValue
  */
 protected function stringParse($value)
 {
     $rawValue = $value;
     try {
         list($sign, $value) = $this->eraParser->parse($value);
         $value = trim($value);
         $value = $this->monthNameUnlocalizer->unlocalize($value);
         $year = $this->fetchAndNormalizeYear($value);
         $value = $this->getValueWithFixedSeparators($value, $year);
         $this->validateDateTimeInput($value);
         // Parse using the DateTime object (this will allow us to format the date in a nicer way)
         $dateTime = new DateTime($value);
         // Fail if the DateTime object does calculations like changing 2015-00-00 to 2014-12-30.
         if ($year !== null && $dateTime->format('Y') !== substr($year, -4)) {
             throw new ParseException($value . ' is not a valid date.');
         }
         // Input was three numbers? Where the heck does a time come from?
         if ($dateTime->format('H:i:s') !== '00:00:00' && preg_match('/^\\D*\\d+\\D+\\d+\\D+\\d+\\D*$/', $value)) {
             throw new ParseException($value . ' is not a valid date.');
         }
         if ($year !== null && strlen($year) > 4) {
             $timestamp = $sign . $year . $dateTime->format('-m-d\\TH:i:s\\Z');
         } else {
             $timestamp = $sign . $dateTime->format('Y-m-d\\TH:i:s\\Z');
         }
         // Pass the reformatted string into a base parser that parses this +/-Y-m-d\TH:i:s\Z format with a precision
         return $this->isoTimestampParser->parse($timestamp);
     } catch (Exception $exception) {
         throw new ParseException($exception->getMessage(), $rawValue, self::FORMAT_NAME);
     }
 }
 /**
  * @dataProvider localizedDateProvider
  */
 public function testUnlocalize($date, $expected, array $replacements)
 {
     $unlocalizer = new MonthNameUnlocalizer($replacements);
     $this->assertEquals($expected, $unlocalizer->unlocalize($date));
 }