/** * Obtains an instance of {@code OffsetDateTime} from a text string using a specific formatter. * <p> * The text is parsed using the formatter, returning a date-time. * * @param string $text the text to parse, not null * @param DateTimeFormatter $formatter the formatter to use, not null * @return OffsetDateTime the parsed offset date-time, not null * @throws DateTimeParseException if the text cannot be parsed */ public static function parseWith($text, DateTimeFormatter $formatter) { return $formatter->parseQuery($text, TemporalQueries::fromCallable([OffsetDateTime::class, 'from'])); }
/** * Obtains an instance of {@code Instant} from a text string such as * {@code 2007-12-03T10:15:30.00Z}. * <p> * The string must represent a valid instant in UTC and is parsed using * {@link DateTimeFormatter#ISO_INSTANT}. * * @param string $text the text to parse, not null * @return Instant the parsed instant, not null * @throws DateTimeParseException if the text cannot be parsed */ public static function parse($text) { return DateTimeFormatter::ISO_INSTANT()->parseQuery($text, TemporalQueries::fromCallable([Instant::class, "from"])); }
/** * @expectedException \InvalidArgumentException */ public function test_parseBest_String_oneRule() { $test = $this->fmt->withLocale(Locale::ENGLISH())->withDecimalStyle(DecimalStyle::STANDARD()); $test->parseBest("30", TemporalQueries::fromCallable([LocalDate::class, 'from'])); }
/** * @dataProvider data_print_localized */ public function test_print_localized(TextStyle $style, LocalDateTime $ldt, ZoneOffset $offset, $expected) { $odt = OffsetDateTime::ofDateTime($ldt, $offset); $zdt = $ldt->atZone($offset); $f = (new DateTimeFormatterBuilder())->appendLocalizedOffset($style)->toFormatter(); $this->assertEquals($f->format($odt), $expected); $this->assertEquals($f->format($zdt), $expected); $this->assertEquals($f->parseQuery($expected, TemporalQueries::fromCallable([ZoneOffset::class, 'from'])), $offset); if ($style == TextStyle::FULL()) { $f = (new DateTimeFormatterBuilder())->appendPattern("ZZZZ")->toFormatter(); $this->assertEquals($f->format($odt), $expected); $this->assertEquals($f->format($zdt), $expected); $this->assertEquals($f->parseQuery($expected, TemporalQueries::fromCallable([ZoneOffset::class, 'from'])), $offset); $f = (new DateTimeFormatterBuilder())->appendPattern("OOOO")->toFormatter(); $this->assertEquals($f->format($odt), $expected); $this->assertEquals($f->format($zdt), $expected); $this->assertEquals($f->parseQuery($expected, TemporalQueries::fromCallable([ZoneOffset::class, 'from'])), $offset); } if ($style == TextStyle::SHORT()) { $f = (new DateTimeFormatterBuilder())->appendPattern("O")->toFormatter(); $this->assertEquals($f->format($odt), $expected); $this->assertEquals($f->format($zdt), $expected); $this->assertEquals($f->parseQuery($expected, TemporalQueries::fromCallable([ZoneOffset::class, 'from'])), $offset); } }
/** * Obtains an instance of {@code Year} from a text string using a specific formatter. * <p> * The text is parsed using the formatter, returning a year. * * @param string $text the text to parse, not null * @param DateTimeFormatter $formatter the formatter to use, not null * @return Year the parsed year, not null * @throws DateTimeParseException if the text cannot be parsed */ public static function parseWith($text, DateTimeFormatter $formatter) { if (!is_string($text)) { throw new \InvalidArgumentException(); } return $formatter->parseQuery($text, TemporalQueries::fromCallable([self::class, 'from'])); }
public function test_basicTest_query() { foreach ($this->samples() as $sample) { $this->assertEquals($sample->query(TemporalQueries::fromCallable(function () { return "foo"; })), "foo"); } }
/** * @dataProvider data_offsetPatterns */ public function test_appendOffset_parse($pattern, $h, $m, $s, $expected) { $this->builder->appendOffset($pattern, "Z"); $f = $this->builder->toFormatter(); $parsed = $f->parseQuery($expected, TemporalQueries::fromCallable([ZoneOffset::class, 'from'])); $this->assertEquals($f->format($parsed), $expected); }
public function test_fieldResolvesToChronoZonedDateTime_overrideZone_matches() { $zdt = ZonedDateTime::of(2010, 6, 30, 12, 30, 0, 0, self::EUROPE_PARIS()); $f = (new DateTimeFormatterBuilder())->appendValue(new ResolvingField($zdt))->toFormatter(); $f = $f->withZone(self::EUROPE_PARIS()); $this->assertEquals($f->parseQuery("1234567890", TemporalQueries::fromCallable([ZonedDateTime::class, 'from'])), $zdt); }
public function test_parse_weekDate() { $expected = LocalDate::of(2004, 1, 28); $this->assertEquals(DateTimeFormatter::ISO_WEEK_DATE()->parseQuery("2004-W05-3", TemporalQueries::fromCallable([LocalDate::class, 'from'])), $expected); }
/** * Appends the time-zone region ID, such as 'Europe/Paris', to the formatter, * rejecting the zone ID if it is a {@code ZoneOffset}. * <p> * This appends an instruction to format/parse the zone ID to the builder * only if it is a region-based ID. * <p> * During formatting, the zone is obtained using a mechanism equivalent * to querying the temporal with {@link TemporalQueries#zoneId()}. * If the zone is a {@code ZoneOffset} or it cannot be obtained then * an exception is thrown unless the section of the formatter is optional. * If the zone is not an offset, then the zone will be printed using * the zone ID from {@link ZoneId#getId()}. * <p> * During parsing, the text must match a known zone or offset. * There are two types of zone ID, offset-based, such as '+01:30' and * region-based, such as 'Europe/London'. These are parsed differently. * If the parse starts with '+', '-', 'UT', 'UTC' or 'GMT', then the parser * expects an offset-based zone and will not match region-based zones. * The offset ID, such as '+02:30', may be at the start of the parse, * or prefixed by 'UT', 'UTC' or 'GMT'. The offset ID parsing is * equivalent to using {@link #appendOffset(String, String)} using the * arguments 'HH:MM:ss' and the no offset string '0'. * If the parse starts with 'UT', 'UTC' or 'GMT', and the parser cannot * match a following offset ID, then {@link ZoneOffset#UTC} is selected. * In all other cases, the list of known region-based zones is used to * find the longest available match. If no match is found, and the parse * starts with 'Z', then {@code ZoneOffset.UTC} is selected. * The parser uses the {@linkplain #parseCaseInsensitive() case sensitive} setting. * <p> * For example, the following will parse: * <pre> * "Europe/London" -- ZoneId.of("Europe/London") * "Z" -- ZoneOffset.UTC * "UT" -- ZoneId.of("UT") * "UTC" -- ZoneId.of("UTC") * "GMT" -- ZoneId.of("GMT") * "+01:30" -- ZoneOffset.of("+01:30") * "UT+01:30" -- ZoneOffset.of("+01:30") * "UTC+01:30" -- ZoneOffset.of("+01:30") * "GMT+01:30" -- ZoneOffset.of("+01:30") * </pre> * <p> * Note that this method is identical to {@code appendZoneId()} except * in the mechanism used to obtain the zone. * Note also that parsing accepts offsets, whereas formatting will never * produce one. * * @return DateTimeFormatterBuilder this, for chaining, not null * @see #appendZoneId() */ public function appendZoneRegionId() { $query = TemporalQueries::fromCallable(function (TemporalAccessor $temporal) { $zone = $temporal->query(TemporalQueries::zoneId()); return $zone !== null && $zone instanceof ZoneOffset === false ? $zone : null; }); $this->appendInternal(new ZoneIdPrinterParser($query, "ZoneRegionId()")); return $this; }