public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("y M"); $t = YearMonth::of(2010, 12)->format($f); $this->assertEquals($t, "2010 12"); }
public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("y M d H m s"); $t = OffsetDateTime::of(2010, 12, 3, 11, 30, 0, 0, self::OFFSET_PONE())->format($f); $this->assertEquals($t, "2010 12 3 11 30 0"); }
/** * @dataProvider data_instantNoZone */ public function test_parse_instantNoZone_supported(DateTimeFormatter $formatter, $text, Instant $expected) { $actual = $formatter->parse($text); $this->assertEquals($actual->isSupported(CF::INSTANT_SECONDS()), true); $this->assertEquals($actual->isSupported(CF::EPOCH_DAY()), false); $this->assertEquals($actual->isSupported(CF::SECOND_OF_DAY()), false); $this->assertEquals($actual->isSupported(CF::NANO_OF_SECOND()), true); $this->assertEquals($actual->isSupported(CF::MICRO_OF_SECOND()), true); $this->assertEquals($actual->isSupported(CF::MILLI_OF_SECOND()), true); }
public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("H m s"); $t = OffsetTime::of(11, 30, 0, 0, self::OFFSET_PONE())->format($f); $this->assertEquals($t, "11 30 0"); }
public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("y M d"); $t = LocalDate::of(2010, 12, 3)->format($f); $this->assertEquals($t, "2010 12 3"); }
public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("y M d H m s"); $z = ZonedDateTime::ofDateTime($this->dateTime(2010, 12, 3, 11, 30), self::ZONE_PARIS())->format($f); $this->assertEquals($z, "2010 12 3 11 30 0"); }
/** * @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); }
/** * Gets the resolved result of the parse. * * @param ResolverStyle $resolverStyle * @param array $resolverFields * @return TemporalAccessor the result of the parse, not null */ public function toResolved(ResolverStyle $resolverStyle, $resolverFields) { $parsed = $this->currentParsed(); $parsed->chrono = $this->getEffectiveChronology(); $parsed->zone = $parsed->zone !== null ? $parsed->zone : $this->formatter->getZone(); return $parsed->resolve($resolverStyle, $resolverFields); }
public function test_parsed_toString_resolvedDateTime() { $f = DateTimeFormatter::ofPattern("yyyy-MM-dd HH:mm:ss"); $temporal = $f->parse("2010-06-30 11:30:56"); $msg = $temporal->__toString(); $this->assertContains("2010-06-30", $msg, $msg); $this->assertContains("11:30:56", $msg, $msg); }
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); }
/** * @dataProvider data_resolveClockHourOfDay */ public function test_resolveClockHourOfDay(ResolverStyle $style, $value, $expectedHour, $expectedDays) { $str = strval($value); $f = (new DateTimeFormatterBuilder())->appendValue(ChronoField::CLOCK_HOUR_OF_DAY())->toFormatter(); if ($expectedHour !== null) { $accessor = $f->withResolverStyle($style)->parse($str); $this->assertEquals($accessor->query(TemporalQueries::localDate()), null); $this->assertEquals($accessor->query(TemporalQueries::localTime()), LocalTime::of($expectedHour, 0)); $this->assertEquals($accessor->query(DateTimeFormatter::parsedExcessDays()), Period::ofDays($expectedDays)); } else { try { $f->withResolverStyle($style)->parse($str); $this->fail(); } catch (DateTimeParseException $ex) { // $expected } } }
public function parse(DateTimeParseContext $context, $text, $position) { // TODO cache formatter // new context to avoid overwriting fields like year/month/day $minDigits = $this->fractionalDigits < 0 ? 0 : $this->fractionalDigits; $maxDigits = $this->fractionalDigits < 0 ? 9 : $this->fractionalDigits; $parser = (new DateTimeFormatterBuilder())->append(DateTimeFormatter::ISO_LOCAL_DATE())->appendLiteral('T')->appendValue2(ChronoField::HOUR_OF_DAY(), 2)->appendLiteral(':')->appendValue2(ChronoField::MINUTE_OF_HOUR(), 2)->appendLiteral(':')->appendValue2(ChronoField::SECOND_OF_MINUTE(), 2)->appendFraction(ChronoField::NANO_OF_SECOND(), $minDigits, $maxDigits, true)->appendLiteral('Z')->toFormatter()->toPrinterParser(false); $newContext = $context->copy(); $pos = $parser->parse($newContext, $text, $position); if ($pos < 0) { return $pos; } // parser restricts most fields to 2 digits, so definitely int // correctly parsed nano is also guaranteed to be valid $yearParsed = $newContext->getParsed(ChronoField::YEAR()); $month = $newContext->getParsed(ChronoField::MONTH_OF_YEAR()); $day = $newContext->getParsed(ChronoField::DAY_OF_MONTH()); $hour = $newContext->getParsed(ChronoField::HOUR_OF_DAY()); $min = $newContext->getParsed(ChronoField::MINUTE_OF_HOUR()); $secVal = $newContext->getParsed(ChronoField::SECOND_OF_MINUTE()); $nanoVal = $newContext->getParsed(ChronoField::NANO_OF_SECOND()); $sec = $secVal !== null ? $secVal : 0; $nano = $nanoVal !== null ? $nanoVal : 0; $days = 0; if ($hour === 24 && $min === 0 && $sec === 0 && $nano === 0) { $hour = 0; $days = 1; } else { if ($hour === 23 && $min === 59 && $sec === 60) { $context->setParsedLeapSecond(); $sec = 59; } } $year = $yearParsed % 10000; try { $ldt = LocalDateTime::of($year, $month, $day, $hour, $min, $sec, 0)->plusDays($days); $instantSecs = $ldt->toEpochSecond(ZoneOffset::UTC()); $instantSecs += Math::multiplyExact(Math::div($yearParsed, 10000), self::SECONDS_PER_10000_YEARS); } catch (RuntimeException $ex) { // TODO What do we actually catch here and why return ~$position; } $successPos = $pos; $successPos = $context->setParsedField(ChronoField::INSTANT_SECONDS(), $instantSecs, $position, $successPos); return $context->setParsedField(ChronoField::NANO_OF_SECOND(), $nano, $position, $successPos); }
public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("H m s"); $t = LocalTime::of(11, 30, 45)->format($f); $this->assertEquals($t, "11 30 45"); }
public function test_rfc1123_basics() { $this->assertEquals(DateTimeFormatter::RFC_1123_DATE_TIME()->getChronology(), IsoChronology::INSTANCE()); $this->assertEquals(DateTimeFormatter::RFC_1123_DATE_TIME()->getZone(), null); $this->assertEquals(DateTimeFormatter::RFC_1123_DATE_TIME()->getResolverStyle(), ResolverStyle::SMART()); }
public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("y M d H m s"); $t = LocalDateTime::of(2010, 12, 3, 11, 30, 45)->format($f); $this->assertEquals($t, "2010 12 3 11 30 45"); }
/** * Gets the DecimalStyle. * <p> * The DecimalStyle controls the localization of numeric output. * * @return DecimalStyle the DecimalStyle, not null */ public function getDecimalStyle() { return $this->formatter->getDecimalStyle(); }
/** * A string representation of this instant using ISO-8601 representation. * <p> * The format used is the same as {@link DateTimeFormatter#ISO_INSTANT}. * * @return string an ISO-8601 representation of this instant, not null */ public function __toString() { return DateTimeFormatter::ISO_INSTANT()->format($this); }
public function test_format_formatter() { $f = DateTimeFormatter::ofPattern("M d"); $t = MonthDay::of(12, 3)->format($f); $this->assertEquals($t, "12 3"); }
public function test_parse_leapSecond() { $expected = OffsetDateTime::of(1970, 2, 3, 23, 59, 59, 123456789, ZoneOffset::UTC())->toInstant(); $f = (new DateTimeFormatterBuilder())->appendInstant4(-1)->toFormatter(); foreach (ResolverStyle::values() as $style) { $pared = $f->withResolverStyle($style)->parse("1970-02-03T23:59:60.123456789Z"); $this->assertEquals($pared->query(Instant::fromQuery()), $expected); $this->assertEquals($pared->query(DateTimeFormatter::parsedExcessDays()), Period::ZERO()); $this->assertEquals($pared->query(DateTimeFormatter::parsedLeapSecond()), true); } }
/** * @dataProvider provider_patternLocalWeekBasedYearDate */ public function test_print_WeekBasedYear($pattern, $expectedText, LocalDate $date) { $dtf = DateTimeFormatter::ofPatternLocale($pattern, $this->locale); $result = $dtf->format($date); $weekDef = WeekFields::ofLocale($this->locale); $this->assertEquals($result, $expectedText, "Incorrect formatting for " - $pattern - ", weekDef: " . $weekDef); }
/** * Formats this date-time using the specified formatter. * <p> * This date-time will be passed to the formatter to produce a string. * * @param DateTimeFormatter $formatter the formatter to use, not null * @return string the formatted date-time string, not null * @throws DateTimeException if an error occurs during printing */ public function format(DateTimeFormatter $formatter) { return $formatter->format($this); }
/** * Appends a formatter to the builder which will optionally format/parse. * <p> * This method has the same effect as appending each of the constituent * parts directly to this builder surrounded by an {@link #optionalStart()} and * {@link #optionalEnd()}. * <p> * The formatter will format if data is available for all the fields contained within it. * The formatter will parse if the string matches, otherwise no error is returned. * * @param DateTimeFormatter $formatter the formatter to add, not null * @return DateTimeFormatterBuilder this, for chaining, not null */ public function appendOptional(DateTimeFormatter $formatter) { $this->appendInternal($formatter->toPrinterParser(true)); return $this; }