public function query(TemporalQuery $query) { if ($query == TemporalQueries::zoneId()) { return ZoneId::of("Europe/Paris"); } return parent::query($query); }
public function format(DateTimePrintContext $context, &$buf) { /** @var ZoneID $zone */ $zone = $context->getValue(TemporalQueries::zoneId()); if ($zone === null) { return false; } $zname = $zone->getId(); if (!$zone instanceof ZoneOffset) { $dt = $context->getTemporal(); $name = $this->getDisplayName($zname, $dt->isSupported(ChronoField::INSTANT_SECONDS()) ? $zone->getRules()->isDaylightSavings(Instant::from($dt)) ? self::$DST : self::$STD : self::$GENERIC, $context->getLocale()); if ($name !== null) { $zname = $name; } } $buf .= $zname; return true; }
function data_query() { return [[self::TEST_2008_06(), TemporalQueries::chronology(), IsoChronology::INSTANCE()], [self::TEST_2008_06(), TemporalQueries::zoneId(), null], [self::TEST_2008_06(), TemporalQueries::precision(), CU::MONTHS()], [self::TEST_2008_06(), TemporalQueries::zone(), null], [self::TEST_2008_06(), TemporalQueries::offset(), null], [self::TEST_2008_06(), TemporalQueries::localDate(), null], [self::TEST_2008_06(), TemporalQueries::localTime(), null]]; }
public function query(TemporalQuery $query) { if ($query == TemporalQueries::zoneId()) { return $this->_this; } return parent::query($query); }
function data_query() { return [[self::TEST_2008_6_30_11_30_59_000000500(), TemporalQueries::chronology(), IsoChronology::INSTANCE()], [self::TEST_2008_6_30_11_30_59_000000500(), TemporalQueries::zoneId(), null], [self::TEST_2008_6_30_11_30_59_000000500(), TemporalQueries::precision(), CU::NANOS()], [self::TEST_2008_6_30_11_30_59_000000500(), TemporalQueries::zone(), self::OFFSET_PONE()], [self::TEST_2008_6_30_11_30_59_000000500(), TemporalQueries::offset(), self::OFFSET_PONE()], [self::TEST_2008_6_30_11_30_59_000000500(), TemporalQueries::localDate(), LocalDate::of(2008, 6, 30)], [self::TEST_2008_6_30_11_30_59_000000500(), TemporalQueries::localTime(), LocalTime::of(11, 30, 59, 500)]]; }
function data_query() { return [[DayOfWeek::FRIDAY(), TemporalQueries::chronology(), null], [DayOfWeek::FRIDAY(), TemporalQueries::zoneId(), null], [DayOfWeek::FRIDAY(), TemporalQueries::precision(), ChronoUnit::DAYS()], [DayOfWeek::FRIDAY(), TemporalQueries::zone(), null], [DayOfWeek::FRIDAY(), TemporalQueries::offset(), null], [DayOfWeek::FRIDAY(), TemporalQueries::localDate(), null], [DayOfWeek::FRIDAY(), TemporalQueries::localTime(), null]]; }
private static function adjust(TemporalAccessor $temporal, DateTimeFormatter $formatter) { // normal case first (early return is an optimization) $overrideChrono = $formatter->getChronology(); $overrideZone = $formatter->getZone(); if ($overrideChrono == null && $overrideZone == null) { return $temporal; } // ensure minimal change (early return is an optimization) $temporalChrono = $temporal->query(TemporalQueries::chronology()); $temporalZone = $temporal->query(TemporalQueries::zoneId()); if ($temporalChrono !== null && $temporalChrono->equals($overrideChrono)) { $overrideChrono = null; } if ($temporalZone !== null && $temporalZone->equals($overrideZone)) { $overrideZone = null; } if ($overrideChrono === null && $overrideZone === null) { return $temporal; } // make adjustment $effectiveChrono = $overrideChrono != null ? $overrideChrono : $temporalChrono; if ($overrideZone != null) { // if have zone and instant, calculation is simple, defaulting chrono if necessary if ($temporal->isSupported(ChronoField::INSTANT_SECONDS())) { $chrono = $effectiveChrono != null ? $effectiveChrono : IsoChronology::INSTANCE(); return $chrono->zonedDateTime(Instant::from($temporal), $overrideZone); } // block changing zone on OffsetTime, and similar problem cases if ($overrideZone->normalized() instanceof ZoneOffset && $temporal->isSupported(ChronoField::OFFSET_SECONDS()) && $temporal->get(ChronoField::OFFSET_SECONDS()) != $overrideZone->getRules()->getOffset(Instant::EPOCH())->getTotalSeconds()) { throw new DateTimeException("Unable to apply override zone '" . $overrideZone . "' because the temporal object being formatted has a different offset but" . " does not represent an instant: " . $temporal); } } $effectiveZone = $overrideZone !== null ? $overrideZone : $temporalZone; $effectiveDate = null; if ($overrideChrono !== null) { if ($temporal->isSupported(ChronoField::EPOCH_DAY())) { $effectiveDate = $effectiveChrono->dateFrom($temporal); } else { // check for date fields other than epoch-day, ignoring case of converting null to ISO if (!($overrideChrono == IsoChronology::INSTANCE() && $temporalChrono === null)) { foreach (ChronoField::values() as $f) { if ($f->isDateBased() && $temporal->isSupported($f)) { throw new DateTimeException("Unable to apply override chronology '" . $overrideChrono . "' because the temporal object being formatted contains date fields but" . " does not represent a whole date: " . $temporal); } } } $effectiveDate = null; } } else { $effectiveDate = null; } // combine available data // this is a non-standard temporal that is almost a pure delegate // this better handles map-like underlying temporal instances return new Test($effectiveDate, $temporal, $effectiveZone, $effectiveChrono); }
/** * Queries this date-time using the specified query. * <p> * This queries this date-time using the specified query strategy object. * The {@code TemporalQuery} object defines the logic to be used to * obtain the result. Read the documentation of the query to understand * what the result of this method will be. * <p> * The result of this method is obtained by invoking the * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the * specified query passing {@code this} as the argument. * * @param <R> the type of the result * @param TemporalQuery $query the query to invoke, not null * @return mixed the query result, null may be returned (defined by the query) * @throws DateTimeException if unable to query (defined by the query) * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ public function query(TemporalQuery $query) { if ($query == TemporalQueries::offset() || $query == TemporalQueries::zone()) { return $this->getOffset(); } else { if ($query == TemporalQueries::zoneId()) { return null; } else { if ($query == TemporalQueries::localDate()) { return $this->toLocalDate(); } else { if ($query == TemporalQueries::localTime()) { return $this->toLocalTime(); } else { if ($query == TemporalQueries::chronology()) { return IsoChronology::INSTANCE(); } else { if ($query == TemporalQueries::precision()) { return ChronoUnit::NANOS(); } } } } } } // inline TemporalAccessor.super.query(query) as an optimization // non-JDK classes are not permitted to make this optimization return $query->queryFrom($this); }
function data_query() { return [[self::TEST_11_30_59_500_PONE(), TemporalQueries::chronology(), null], [self::TEST_11_30_59_500_PONE(), TemporalQueries::zoneId(), null], [self::TEST_11_30_59_500_PONE(), TemporalQueries::precision(), CU::NANOS()], [self::TEST_11_30_59_500_PONE(), TemporalQueries::zone(), self::OFFSET_PONE()], [self::TEST_11_30_59_500_PONE(), TemporalQueries::offset(), self::OFFSET_PONE()], [self::TEST_11_30_59_500_PONE(), TemporalQueries::localDate(), null], [self::TEST_11_30_59_500_PONE(), TemporalQueries::localTime(), LocalTime::of(11, 30, 59, 500)]]; }
function data_query() { return [[self::TEST_200707_15_12_30_40_987654321(), TemporalQueries::chronology(), IsoChronology::INSTANCE()], [self::TEST_200707_15_12_30_40_987654321(), TemporalQueries::zoneId(), null], [self::TEST_200707_15_12_30_40_987654321(), TemporalQueries::precision(), CU::NANOS()], [self::TEST_200707_15_12_30_40_987654321(), TemporalQueries::zone(), null], [self::TEST_200707_15_12_30_40_987654321(), TemporalQueries::offset(), null], [self::TEST_200707_15_12_30_40_987654321(), TemporalQueries::localDate(), LocalDate::of(2007, 7, 15)], [self::TEST_200707_15_12_30_40_987654321(), TemporalQueries::localTime(), LocalTime::of(12, 30, 40, 987654321)]]; }
public function test_query_zoneId() { $this->assertEquals($this->TEST_DATE_TIME->query(TemporalQueries::zoneId()), $this->TEST_DATE_TIME->getZone()); $this->assertEquals(TemporalQueries::zoneId()->queryFrom($this->TEST_DATE_TIME), $this->TEST_DATE_TIME->getZone()); }
/** * @dataProvider provider_parseDigitsAdjacentLenient */ public function test_parseDigitsAdjacentLenient($input, $parseLen, $parseMonth, $parsedDay) { $this->setStrict(false); $pos = new ParsePosition(0); $f = $this->builder->appendValue3(ChronoField::MONTH_OF_YEAR(), 1, 2, SignStyle::NORMAL())->appendValue2(ChronoField::DAY_OF_MONTH(), 2)->toFormatter2($this->locale)->withDecimalStyle($this->decimalStyle); $parsed = $f->parseUnresolved($input, $pos); if ($pos->getErrorIndex() !== -1) { $this->assertEquals($pos->getErrorIndex(), $parseLen); } else { $this->assertEquals($pos->getIndex(), $parseLen); $this->assertEquals($parsed->getLong(ChronoField::MONTH_OF_YEAR()), $parseMonth); $this->assertEquals($parsed->getLong(ChronoField::DAY_OF_MONTH()), $parsedDay); $this->assertEquals($parsed->query(TemporalQueries::chronology()), null); $this->assertEquals($parsed->query(TemporalQueries::zoneId()), null); } }
public function test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() { $mdt = MinguoDate::of(100, 6, 30); $mzdt = $mdt->atTime(LocalTime::NOON())->atZone(self::EUROPE_PARIS()); $f = (new DateTimeFormatterBuilder())->appendValue(new ResolvingField($mzdt))->toFormatter(); $f = $f->withChronology(MinguoChronology::INSTANCE()); $accessor = $f->parse("1234567890"); $this->assertEquals($accessor->query(TemporalQueries::localDate()), LocalDate::from($mdt)); $this->assertEquals($accessor->query(TemporalQueries::localTime()), LocalTime::NOON()); $this->assertEquals($accessor->query(TemporalQueries::chronology()), MinguoChronology::INSTANCE()); $this->assertEquals($accessor->query(TemporalQueries::zoneId()), self::EUROPE_PARIS()); }
/** * @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); } }
function data_query() { return [[self::$TEST_2008, TemporalQueries::chronology(), IsoChronology::INSTANCE()], [self::$TEST_2008, TemporalQueries::zoneId(), null], [self::$TEST_2008, TemporalQueries::precision(), ChronoUnit::YEARS()], [self::$TEST_2008, TemporalQueries::zone(), null], [self::$TEST_2008, TemporalQueries::offset(), null], [self::$TEST_2008, TemporalQueries::localDate(), null], [self::$TEST_2008, TemporalQueries::localTime(), null]]; }
function data_query() { return [[self::TEST_123040987654321(), TemporalQueries::chronology(), null], [self::TEST_123040987654321(), TemporalQueries::zoneId(), null], [self::TEST_123040987654321(), TemporalQueries::precision(), CU::NANOS()], [self::TEST_123040987654321(), TemporalQueries::zone(), null], [self::TEST_123040987654321(), TemporalQueries::offset(), null], [self::TEST_123040987654321(), TemporalQueries::localDate(), null], [self::TEST_123040987654321(), TemporalQueries::localTime(), self::TEST_123040987654321()]]; }
/** * @param TemporalAccessor $parsed * @param Expected $expected */ private function assertParseMatch($parsed, $expected) { foreach ($expected->fieldValues as $field => $val) { $this->assertEquals($parsed->isSupported($field), true); $parsed->getLong($field); } $this->assertEquals($expected->chrono, $parsed->query(TemporalQueries::chronology())); $this->assertEquals($expected->zone, $parsed->query(TemporalQueries::zoneId())); }
function data_query() { return [[Month::JUNE(), TemporalQueries::chronology(), IsoChronology::INSTANCE()], [Month::JUNE(), TemporalQueries::zoneId(), null], [Month::JUNE(), TemporalQueries::precision(), ChronoUnit::MONTHS()], [Month::JUNE(), TemporalQueries::zone(), null], [Month::JUNE(), TemporalQueries::offset(), null], [Month::JUNE(), TemporalQueries::localDate(), null], [Month::JUNE(), TemporalQueries::localTime(), null]]; }
/** * Queries this instant using the specified query. * <p> * This queries this instant using the specified query strategy object. * The {@code TemporalQuery} object defines the logic to be used to * obtain the result. Read the documentation of the query to understand * what the result of this method will be. * <p> * The result of this method is obtained by invoking the * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the * specified query passing {@code this} as the argument. * * @param <R> the type of the result * @param query TemporalQuery the query to invoke, not null * @return mixed the query result, null may be returned (defined by the query) * @throws DateTimeException if unable to query (defined by the query) * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ public function query(TemporalQuery $query) { if ($query == TemporalQueries::precision()) { return ChronoUnit::NANOS(); } // inline TemporalAccessor.super.query(query) as an optimization if ($query == TemporalQueries::chronology() || $query == TemporalQueries::zoneId() || $query == TemporalQueries::zone() || $query == TemporalQueries::offset() || $query == TemporalQueries::localDate() || $query == TemporalQueries::localTime()) { return null; } return $query->queryFrom($this); }
public function query(TemporalQuery $query) { if ($query == TemporalQueries::zoneId()) { return $this->zone; } else { if ($query == TemporalQueries::chronology()) { return $this->chrono; } else { if ($query == TemporalQueries::localDate()) { return $this->date != null ? LocalDate::from($this->date) : null; } else { if ($query == TemporalQueries::localTime()) { return $this->time; } else { if ($query == TemporalQueries::zone() || $query == TemporalQueries::offset()) { return $query->queryFrom($this); } else { if ($query == TemporalQueries::precision()) { return null; // not a complete date/time } } } } } } // inline TemporalAccessor.super.query(query) as an optimization // non-JDK classes are not permitted to make this optimization return $query->queryFrom($this); }
/** * @dataProvider data_success */ public function test_parse_success($s, $caseSensitive, $text, $pos, $expectedPos) { $this->setCaseSensitive($caseSensitive); $ppos = new ParsePosition($pos); $parsed = $this->getFormatterString($s)->parseUnresolved($text, $ppos); if ($ppos->getErrorIndex() != -1) { $this->assertEquals($ppos->getIndex(), $expectedPos); } else { $this->assertEquals($ppos->getIndex(), $expectedPos); $this->assertEquals($parsed->isSupported(ChronoField::YEAR()), false); $this->assertEquals($parsed->query(TemporalQueries::chronology()), null); $this->assertEquals($parsed->query(TemporalQueries::zoneId()), null); } }
/** * Queries this offset using the specified query. * <p> * This queries this offset using the specified query strategy object. * The {@code TemporalQuery} object defines the logic to be used to * obtain the result. Read the documentation of the query to understand * what the result of this method will be. * <p> * The result of this method is obtained by invoking the * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the * specified query passing {@code this} as the argument. * * @param <R> the type of the result * @param $query TemporalQuery the query to invoke, not null * @return mixed the query result, null may be returned (defined by the query) * @throws DateTimeException if unable to query (defined by the query) * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ public function query(TemporalQuery $query) { if ($query == TemporalQueries::offset() || $query == TemporalQueries::zone()) { return $this; } // inlined from \Celest\Temporal\AbstractTemporalAccessor::query if ($query == TemporalQueries::zoneId() || $query == TemporalQueries::chronology() || $query == TemporalQueries::precision()) { return null; } return $query->queryFrom($this); }
public function data_query() { return [[ZoneOffset::UTC(), TemporalQueries::chronology(), null], [ZoneOffset::UTC(), TemporalQueries::zoneId(), null], [ZoneOffset::UTC(), TemporalQueries::precision(), null], [ZoneOffset::UTC(), TemporalQueries::zone(), ZoneOffset::UTC()], [ZoneOffset::UTC(), TemporalQueries::offset(), ZoneOffset::UTC()], [ZoneOffset::UTC(), TemporalQueries::localDate(), null], [ZoneOffset::UTC(), TemporalQueries::localTime(), null]]; }
/** * 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; }