/** * @param DOMElement[] $value * @param string $picture * @param null $language * @return string * @throws InvalidArgumentException */ public static function formatDateTime($value, $picture, $language = null) { Assert::assertArray($value); Assert::assertSchema($value[0], 'dateTime'); if (self::$dateTimeFormatter === null) { // @codeCoverageIgnoreStart if (extension_loaded('intl')) { self::$dateTimeFormatter = Functions\Formatter\IntlDateTimeFormatter::createWithFlagDateTime(); } else { self::$dateTimeFormatter = Functions\Formatter\DateTimeFormatter::createWithFlagDateTime(); } // @codeCoverageIgnoreEnd } if ($language === null) { $locale = self::detectSystemLocale(); } else { $locale = $language; } $date = DateTime::createFromFormat(XsDateTime::FORMAT, $value[0]->nodeValue); return self::$dateTimeFormatter->format($date, $picture, $locale, 'AD'); }
/** * @param DateTimeInterface $date * @param string $picture * @param $locale * @param $calendar * @return string * @throws InvalidArgumentException * @credits https://github.com/Saxonica/Saxon-CE/ https://github.com/Saxonica/Saxon-CE/blob/master/notices/MOZILLA.txt */ public function format(DateTimeInterface $date, $picture, $locale, $calendar) { if ($date->format('Y-m-d') === '2017-01-01') { if ($this->flagsDate && $this->flagsTime) { return DateTimeFormatter::createWithFlagDateTime()->format($date, $picture, $locale, $calendar); } if ($this->flagsDate) { return DateTimeFormatter::createWithFlagDate()->format($date, $picture, $locale, $calendar); } if ($this->flagsTime) { return DateTimeFormatter::createWithFlagTime()->format($date, $picture, $locale, $calendar); } } $result = []; $i = 0; $escaped = false; while (true) { while ($i < strlen($picture) && substr($picture, $i, 1) !== '[') { if ($escaped === false) { $result[] = self::ESCAPE; $escaped = true; } $result[] = substr($picture, $i, 1); if (substr($picture, $i, 1) === ']') { $i++; if ($i == strlen($picture) || substr($picture, $i, 1) != ']') { $exception = new InvalidArgumentException('Wrong formatted date, escape by doubling [[ and ]]'); $exception->setErrorCode('XTDE1340'); throw $exception; } } $i++; } if ($i === strlen($picture)) { break; } // look for '[[' $i++; if ($i < strlen($picture) && substr($picture, $i, 1) === '[') { $result[] = '['; $i++; } else { $close = $i < strlen($picture) ? strpos($picture, "]", $i) : -1; if ($close === -1 || $close === false) { $exception = new InvalidArgumentException('Wrong formatted date, missing ]'); $exception->setErrorCode('XTDE1340'); throw $exception; } $pictureString = new PictureString(substr($picture, $i, $close - $i)); $specifier = $pictureString->getComponentSpecifier(); if (isset($this->components[$specifier])) { if ($pictureString->getPresentationModifier() === 'N') { if ($escaped === false) { $result[] = self::ESCAPE; $escaped = true; } $result[] = strtoupper($this->formatPattern($date, $locale, $this->components[$specifier]->format($pictureString, $date))); } elseif ($pictureString->getPresentationModifier() === 'n') { if ($escaped === false) { $result[] = self::ESCAPE; $escaped = true; } $result[] = strtolower($this->formatPattern($date, $locale, $this->components[$specifier]->format($pictureString, $date))); } else { if ($escaped) { $result[] = self::ESCAPE; $escaped = false; } $result[] = $this->components[$specifier]->format($pictureString, $date); } } else { $exception = new InvalidArgumentException("Component [{$specifier}] is not supported"); $exception->setErrorCode('XTDE1340'); throw $exception; } $i = $close + 1; } } if ($escaped) { $result[] = self::ESCAPE; } return $this->formatPattern($date, $locale, implode('', $result)); }
public function testMonthName() { $formatter = DateTimeFormatter::createWithFlagDate(); $this->assertEquals('16 10 2015', $formatter->format(new DateTime('2015-10-16'), '[D] [MNn] [Y]', 'en_US', null)); }