/** * @dataProvider data_resolverStyle */ public function test_resolverStyle($str, ResolverStyle $style, $expectedEx, $year, $month, $day) { $builder = new DateTimeFormatterBuilder(); $builder->appendValue(ChronoField::YEAR_OF_ERA()); $builder->appendLiteral("/"); $builder->appendValue(ChronoField::MONTH_OF_YEAR()); $builder->appendLiteral("/"); $builder->appendValue(ChronoField::DAY_OF_MONTH()); $eraMap = [1 => "CE", 0 => "BCE"]; $optionalFormatter = (new DateTimeFormatterBuilder())->appendLiteral(" ")->appendText3(ChronoField::ERA(), $eraMap)->toFormatter(); $formatter = $builder->appendOptional($optionalFormatter)->toFormatter(); $formatter = $formatter->withResolverStyle($style); if ($expectedEx == null) { $accessor = $formatter->parse($str); $this->assertEquals($accessor->get(ChronoField::YEAR_OF_ERA()), $year); $this->assertEquals($accessor->get(ChronoField::MONTH_OF_YEAR()), $month); $this->assertEquals($accessor->get(ChronoField::DAY_OF_MONTH()), $day); } else { try { $formatter->parse($str); $this->fail(); } catch (\Exception $ex) { $this->assertInstanceOf($expectedEx, $ex); } } }
/** * @dataProvider data_combine */ public function test_derive($field1, $value1, $field2, $value2, $field3, $value3, $field4, $value4, $query, $expectedVal) { $str = ""; $dtfb = new DateTimeFormatterBuilder(); $dtfb->appendValue($field1)->appendLiteral('-'); $str .= $value1 . "-"; if ($field2 != null) { $dtfb->appendValue($field2)->appendLiteral('-'); $str .= $value2 . "-"; } if ($field3 != null) { $dtfb->appendValue($field3)->appendLiteral('-'); $str .= $value3 . "-"; } if ($field4 != null) { $dtfb->appendValue($field4)->appendLiteral('-'); $str .= $value4 . "-"; } $parsed = $dtfb->toFormatter()->parse($str); if ($query == LocalDate::class) { if ($expectedVal != null) { $this->assertEquals($parsed->query(LocalDate::fromQuery()), $expectedVal); } else { try { $parsed->query(LocalDate::fromQuery()); $this->fail(); } catch (DateTimeException $ex) { // expected } } } else { throw new IllegalArgumentException(); } }
/** * @dataProvider data_signStyle */ public function test_signStyle(LocalDate $localDate, SignStyle $style, $expectedEx, $expectedStr) { $builder = new DateTimeFormatterBuilder(); $formatter = $builder->appendValue3(ChronoField::YEAR(), 2, 4, $style)->toFormatter(); $formatter = $formatter->withZone(ZoneOffset::UTC()); if ($expectedEx === null) { $output = $formatter->format($localDate); $this->assertEquals($output, $expectedStr); } else { try { $formatter->format($localDate); $this->fail(); } catch (\Exception $ex) { $this->assertInstanceOf($expectedEx, $ex); } } }
public function test_appendTextMapIncomplete() { $map = [1, "JNY"]; $this->builder->appendText3(ChronoField::MONTH_OF_YEAR(), $map); $f = $this->builder->toFormatter(); $dt = LocalDateTime::of(2010, 2, 1, 0, 0); $this->assertEquals($f->format($dt), "2"); }
public function test_parse_decoratedEmpty_lenient() { $this->builder->parseLenient()->padNext2(4, '-')->optionalStart()->appendValue(ChronoField::DAY_OF_MONTH())->optionalEnd(); $parsed = $this->builder->toFormatter()->parseUnresolved("----", $this->pos); $this->assertEquals($this->pos->getIndex(), 4); $this->assertEquals($this->pos->getErrorIndex(), -1); $this->assertNotNull($parsed); }
public function test_adjacent_lenient_fractionFollows_0digit() { // succeeds because hour/min are fixed width $f = $this->builder->parseLenient()->appendValue2(ChronoField::HOUR_OF_DAY(), 2)->appendValue2(ChronoField::MINUTE_OF_HOUR(), 2)->appendFraction(ChronoField::NANO_OF_SECOND(), 3, 3, false)->toFormatter2(Locale::UK()); $pp = new ParsePosition(0); $parsed = $f->parseUnresolved("1230", $pp); $this->assertEquals($pp->getErrorIndex(), -1); $this->assertEquals($pp->getIndex(), 4); $this->assertEquals($parsed->getLong(ChronoField::HOUR_OF_DAY()), 12); $this->assertEquals($parsed->getLong(ChronoField::MINUTE_OF_HOUR()), 30); }
/** * @dataProvider data_time */ public function test_time_parse(LocalTime $time, FormatStyle $timeStyle, $timeStyleOld, Locale $locale) { $old = \IntlDateFormatter::create($locale->getLocale(), \IntlDateFormatter::NONE, $timeStyleOld, new \DateTimeZone('UTC')); $oldDate = new \DateTime('1970-0-0T' . $time->getHour() . ':' . $time->getMinute() . ':' . $time->getSecond(), new \DateTimeZone('UTC')); $text = $old->format($oldDate); $f = $this->builder->appendLocalized(null, $timeStyle)->toFormatter2($locale); $parsed = $f->parsePos($text, $this->pos); $this->assertEquals($this->pos->getIndex(), strlen($text)); $this->assertEquals($this->pos->getErrorIndex(), -1); $this->assertEquals(LocalTime::from($parsed), $time); }
/** * Gets the formatter to use. * <p> * The formatter will be the most appropriate to use for the date and time style in the locale. * For example, some locales will use the month name while others will use the number. * * @param Locale $locale the locale to use, not null * @param Chronology $chrono the chronology to use, not null * @return DateTimeFormatter the formatter, not null * @throws IllegalArgumentException if the formatter cannot be found */ private function formatter(Locale $locale, Chronology $chrono) { $key = $chrono->getId() . '|' . $locale . '|' . $this->dateStyle . '|' . $this->timeStyle; $formatter = @self::$FORMATTER_CACHE[$key]; if ($formatter === null) { $pattern = DateTimeFormatterBuilder::getLocalizedDateTimePattern($this->dateStyle, $this->timeStyle, $chrono, $locale); $formatter = (new DateTimeFormatterBuilder())->appendPattern($pattern)->toFormatter2($locale); $old = self::$FORMATTER_CACHE[$key] = $formatter; if ($old !== null) { $formatter = $old; } } return $formatter; }
/** * @dataProvider data_formatStyle */ public function test_formatStyle(Temporal $temporal, FormatStyle $style, $formattedStr) { $this->markTestIncomplete('ZoneTextPrinterParser, Localized Zone Names'); $builder = new DateTimeFormatterBuilder(); $formatter = $builder->appendLocalized($style, $style)->appendLiteral(" ")->appendZoneOrOffsetId()->toFormatter(); $formatter = $formatter->withLocale(Locale::US()); $this->assertEquals($formattedStr, $formatter->format($temporal)); }
/** * @expectedException \Celest\IllegalArgumentException */ public function test_print_pattern_localzed_narrow_standalone() { $this->builder->appendLocalizedOffset(TextStyle::NARROW_STANDALONE()); }
/** * @dataProvider provider_printAdjacent */ public function test_printAdjacent($pattern, $text, $year, $month, $day) { $builder = new DateTimeFormatterBuilder(); $builder->appendPattern($pattern); $dtf = $builder->toFormatter(); $ld = LocalDate::of($year, $month, $day); $actual = $dtf->format($ld); $this->assertEquals($text, $actual, "formatter output: " . $dtf); }
protected function getFormatterFraction(TemporalField $field, $minWidth, $maxWidth, $decimalPoint) { return $this->builder->appendFraction($field, $minWidth, $maxWidth, $decimalPoint)->toFormatter2($this->locale)->withDecimalStyle($this->decimalStyle); }
/** * @dataProvider data_parseSuccess */ public function test_parseSuccess_caseInsensitive($text, $expectedIndex, $expectedErrorIndex, $expected) { $this->builder->parseCaseInsensitive()->appendZoneId(); $lcText = strtolower($text); $parsed = $this->builder->toFormatter()->parseUnresolved($lcText, $this->pos); $this->assertEquals($this->pos->getErrorIndex(), $expectedErrorIndex, "Incorrect error index parsing: " . $lcText); $this->assertEquals($this->pos->getIndex(), $expectedIndex, "Incorrect index parsing: " . $lcText); if ($expected !== null) { $zid = $parsed->query(TemporalQueries::zoneId()); $this->assertEquals($parsed->query(TemporalQueries::zoneId()), $expected, "Incorrect zoneId parsing: " . $lcText); $this->assertEquals($parsed->query(TemporalQueries::offset()), null, "Incorrect offset parsing: " . $lcText); $this->assertEquals($parsed->query(TemporalQueries::zone()), $expected, "Incorrect zone parsing: " . $lcText); } else { $this->assertEquals($parsed, null); } }
public function test_getLocalizedLocaleNPE() { TestHelper::assertNullException($this, function () { DateTimeFormatterBuilder::getLocalizedDateTimePattern(FormatStyle::SHORT(), FormatStyle::SHORT(), IsoChronology::INSTANCE(), null); }); }
} /** * Completes this builder by creating the formatter. * This uses the default locale. * * @param ResolverStyle $resolverStyle the resolver style to use, not null * @return DateTimeFormatter the created formatter, not null */ public function toFormatter3(ResolverStyle $resolverStyle, $chrono) { // TODO fix Locale use return $this->toFormatter4(Locale::getDefault(), $resolverStyle, $chrono); } /** * Completes this builder by creating the formatter. * * @param Locale $locale the locale to use for formatting, not null * @param Chronology|null $chrono the chronology to use, may be null * @return DateTimeFormatter the created formatter, not null */ private function toFormatter4(Locale $locale, ResolverStyle $resolverStyle, $chrono) { while ($this->active->parent !== null) { $this->optionalEnd(); } $pp = new CompositePrinterParser($this->printerParsers, false); return new DateTimeFormatter($pp, $locale, DecimalStyle::STANDARD(), $resolverStyle, null, $chrono, null); } } DateTimeFormatterBuilder::init();