/** * @setUp */ public function setUp() { $this->builder = new DateTimeFormatterBuilder(); $this->dta = ZonedDateTime::ofDateTime(LocalDateTime::of(2011, 6, 30, 12, 30, 40, 0), ZoneId::of("Europe/Paris")); $this->locale = Locale::of("en"); $this->decimalStyle = DecimalStyle::STANDARD(); }
public function test_constant_UTC() { $test = ZoneOffset::UTC(); $this->assertEquals($test->getId(), "Z"); $this->assertEquals($test->getDisplayName(TextStyle::FULL(), Locale::UK()), "Z"); $this->assertEquals($test->getRules()->isFixedOffset(), true); $this->assertEquals($test->getRules()->getOffset(Instant::ofEpochSecond(0)), ZoneOffset::UTC()); $this->checkOffset($test->getRules(), $this->createLDT(2008, 6, 30), ZoneOffset::UTC(), 1); }
public function test_print_french_short() { $buf = ''; $this->getFormatterFieldStyle(ChronoField::MONTH_OF_YEAR(), TextStyle::SHORT())->withLocale(Locale::FRENCH())->formatTo(LocalDate::of(2012, 1, 1), $buf); $this->assertEquals("janv.", $buf); }
public function test_withLocale_same() { $base = (new DateTimeFormatterBuilder())->appendLiteral2("ONE")->appendValue3(ChronoField::DAY_OF_MONTH(), 1, 2, SignStyle::NOT_NEGATIVE())->toFormatter2(Locale::ENGLISH())->withDecimalStyle(DecimalStyle::STANDARD()); $test = $base->withLocale(Locale::ENGLISH()); $this->assertSame($test, $base); }
/** * @dataProvider data_offsetBasedValidPrefix */ public function test_factory_of_String_offsetBasedValid_prefixUT($input, $id, $offsetId) { $test = ZoneId::of("UT" . $input); $this->assertEquals($test->getId(), "UT" . $id); $this->assertEquals($test->getRules(), ZoneOffset::of($offsetId)->getRules()); $this->assertEquals($test->normalized(), ZoneOffset::of($offsetId)); $this->assertEquals($test->getRules()->isFixedOffset(), true); $this->assertEquals($test->getRules()->getOffset(Instant::EPOCH()), ZoneOffset::of($offsetId)); if (defined('HHVM_VERSION')) { $this->markTestSkipped('See https://github.com/facebook/hhvm/issues/6852'); } $this->assertEquals($test->getDisplayName(TextStyle::FULL(), Locale::UK()), $this->displayName("UT" . $id)); }
/** * 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); }
private static function ptBR() { return Locale::of("pt", "BR"); }
/** * @dataProvider data_text */ public function test_appendText1arg_format(TemporalField $field, TextStyle $style, $value, $expected) { if ($style == TextStyle::FULL()) { $f = $this->builder->appendText($field)->toFormatter2(Locale::ENGLISH()); $dt = LocalDateTime::of(2010, 1, 1, 0, 0); $dt = $dt->with($field, $value); $text = $f->format($dt); $this->assertEquals($text, $expected); } }
/** * Obtains an instance of {@code Chronology} from a locale. * <p> * See {@link Chronology#ofLocale(Locale)}. * * @param Locale $locale the locale to use to obtain the calendar system, not null * @return Chronology the calendar system associated with the locale, not null * @throws DateTimeException if the locale-specified calendar cannot be found */ static function ofLocale(Locale $locale) { $type = $locale->getUnicodeLocaleType("ca"); if ($type == null || "iso" === $type || "iso8601" === $type) { return IsoChronology::INSTANCE(); } // Not pre-defined; lookup by the type // do { // Chronology chrono = CHRONOS_BY_TYPE->get(type); // if (chrono != null) { // return chrono; // } // // If not found, do the initialization (once) and repeat the lookup // } while (initCache()); // //// Look for a Chronology using ServiceLoader of the Thread's ContextClassLoader //// Application provided Chronologies must not be cached // @SuppressWarnings("rawtypes") // ServiceLoader < Chronology> loader = ServiceLoader->load(Chronology .class); // for (Chronology chrono : $loader) { // if ($type->equals($chrono->getCalendarType())) { // return $chrono; // } // } throw new DateTimeException("Unknown calendar system: " . $type); }
private function parse(DateTimeFormatter $fmt, $zid, $expected, $text, Locale $locale, TextStyle $style, $ci) { if ($ci) { $text = $text->toUpperCase(); } /** @var ZoneId $ret */ $ret = $fmt->parseQuery($text, TemporalQueries::zone())->getId(); // TBD: need an excluding list // assertEquals(...); if ($ret->equals($expected) || $ret->equals($zid) || $ret->equals(ZoneName::toZid($zid)) || $ret->equals($expected->replace("UTC", "UCT"))) { return; } echo printf("[%-5s %s %s %16s] %24s -> %s(%s)%n", $locale->__toString(), $ci ? "ci" : " ", $style == TextStyle::FULL() ? " full" : "short", $zid, $text, $ret, $expected); }
/** * @param $field * @param TextStyle $style * @param Locale $locale * @param callable $transformer * @return array|null */ public static function tryFetchStyleValues($field, $style, Locale $locale, callable $transformer) { $bundle = new ResourceBundle($locale->getLocale(), null); if ($style === null) { $tmp = []; foreach (['stand-alone', 'format'] as $id) { foreach (['wide', 'abbreviated', 'narrow'] as $style) { $values = $bundle['calendar']['gregorian'][$field][$id][$style]; if ($values === null) { continue; } foreach ($values as $key => $value) { $tmp[$value] = $transformer($key); } } } return $tmp; } $id = $style->isStandalone() ? 'stand-alone' : 'format'; $styles = [\IntlDateFormatter::FULL => 'wide', \IntlDateFormatter::MEDIUM => 'abbreviated', \IntlDateFormatter::SHORT => 'narrow']; $values = $bundle['calendar']['gregorian'][$field][$id][$styles[$style->toCalendarStyle()]]; // fallback to stand alone if not found if ($values === null && $id === 'format') { $values = $bundle['calendar']['gregorian'][$field]['stand-alone'][$styles[$style->toCalendarStyle()]]; } // ERA if ($values === null) { $values = $bundle['calendar']['gregorian'][$field][$styles[$style->toCalendarStyle()]]; } // AMPM if ($values === null) { $values = $bundle['calendar']['gregorian'][$field]; } if (!$values) { return null; } $tmp = []; foreach ($values as $key => $value) { $tmp[$value] = $transformer($key); } return $tmp; }
/** * @dataProvider data_rfc */ public function test_print_rfc1123_french(LocalDateTime $base, $offsetId, $expected) { $test = $this->buildAccessorDateTime($base, $offsetId, null); $this->assertEquals(DateTimeFormatter::RFC_1123_DATE_TIME()->withLocale(Locale::FRENCH())->format($test), $expected); }
public function test_getText_nullStyle() { TestHelper::assertNullException($this, function () { Month::JANUARY()->getDisplayName(null, Locale::US()); }); }
public function test_parse_french_short_strict_short_match() { $this->setStrict(true); $pos = new ParsePosition(0); $this->assertEquals($this->getFormatterFieldStyle(ChronoField::MONTH_OF_YEAR(), TextStyle::SHORT())->withLocale(Locale::FRENCH())->parseUnresolved("janv.", $pos)->getLong(ChronoField::MONTH_OF_YEAR()), 1); $this->assertEquals($pos->getIndex(), 5); }
public function test_getLocalizedChronoNPE() { TestHelper::assertNullException($this, function () { DateTimeFormatterBuilder::getLocalizedDateTimePattern(FormatStyle::SHORT(), FormatStyle::SHORT(), null, Locale::US()); }); }
public function test_getText_nullStyle() { TestHelper::assertNullException($this, function () { DayOfWeek::MONDAY()->getDisplayName(null, Locale::US()); }); }
/** * @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)); }
/** * Obtains an instance of {@code WeekFields} appropriate for a locale. * <p> * This will look up appropriate values from the provider of localization data. * * @param Locale $locale the locale to use, not null * @return WeekFields the week-definition, not null */ public static function ofLocale(Locale $locale) { // TODO $locale = Locale::of($locale->getLanguage(), $locale->getCountry()); // elminate variants $cal = IntlCalendar::createInstance(null, $locale->getLocale()); $calDow = $cal->getFirstDayOfWeek(); $dow = DayOfWeek::SUNDAY()->plus($calDow - 1); $minDays = $cal->getMinimalDaysInFirstWeek(); return WeekFields::of($dow, $minDays); }
private static function create(Locale $locale) { $formatter = \NumberFormatter::create($locale->getLocale(), \NumberFormatter::DEFAULT_STYLE); $zeroDigit = $formatter->getSymbol(\NumberFormatter::ZERO_DIGIT_SYMBOL); $positiveSign = $formatter->getSymbol(\NumberFormatter::PLUS_SIGN_SYMBOL); $negativeSign = $formatter->getSymbol(\NumberFormatter::MINUS_SIGN_SYMBOL); $decimalSeparator = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); if ($zeroDigit === '0' && $negativeSign === '-' && $decimalSeparator === '.') { return self::STANDARD(); } return new DecimalStyle($zeroDigit, $positiveSign, $negativeSign, $decimalSeparator); }
public function test_WeekFields_week_based_year() { $locale = Locale::US(); $weekOfYearField = WeekFields::SUNDAY_START()->weekOfYear(); $name = $weekOfYearField->getDisplayName($locale); $this->assertEquals(TestHelper::getEnglishWeek(), $name, TestHelper::INTLinfo($locale->getLocale())); }
/** * @expectedException \OutOfRangeException */ public function test_parseUnresolved_StringParsePosition_invalidPosition() { $test = $this->fmt->withLocale(Locale::ENGLISH())->withDecimalStyle(DecimalStyle::STANDARD()); $pos = new ParsePosition(6); $test->parseUnresolved("ONE30", $pos); }
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); }
public function test_toString_altered() { $base = DecimalStyle::of(Locale::US())->withZeroDigit('A')->withDecimalSeparator('@'); $this->assertEquals($base->__toString(), "DecimalStyle[A+-@]"); }
/** * @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); }