public function format(DateTimePrintContext $context, &$buf) { $chrono = $context->getValue(TemporalQueries::chronology()); if ($chrono == null) { return false; } if ($this->textStyle == null) { $buf .= $chrono->getId(); } else { $buf .= $this->getChronologyName($chrono, $context->getLocale()); } return true; }
public function format(DateTimePrintContext $context, &$buf) { $value = $context->getValueField($this->field); if ($value === null) { return false; } $text = null; $chrono = $context->getTemporal()->query(TemporalQueries::chronology()); if ($chrono === null || $chrono == IsoChronology::INSTANCE()) { $text = $this->provider->getText($this->field, $value, $this->textStyle, $context->getLocale()); } else { $text = $this->provider->getText2($chrono, $this->field, $value, $this->textStyle, $context->getLocale()); } if ($text === null) { return $this->numberPrinterParser()->format($context, $buf); } $buf .= $text; return true; }
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]]; }
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)]]; }
/** * 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); }
/** * 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); }
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]]; }
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)]]; }
/** * @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_reducedWithLateChronoChangeTwice() { $df = (new DateTimeFormatterBuilder())->appendValueReduced2(ChronoField::YEAR(), 2, 2, LocalDate::of(2000, 1, 1))->appendLiteral(" ")->appendChronologyId()->appendLiteral(" ")->appendChronologyId()->toFormatter(); $expected = 2044; $input = "44 ThaiBuddhist ISO"; $pos = new ParsePosition(0); $parsed = $df->parseUnresolved($input, $pos); $this->assertEquals($pos->getIndex(), strlen($input), "Input not parsed completely: " . $pos); $this->assertEquals($pos->getErrorIndex(), -1, "Error index should be -1 (no-error)"); $actual = $parsed->get(ChronoField::YEAR()); $this->assertEquals($expected, $actual, sprintf("Wrong date parsed, chrono: %s, input: %s", $parsed->query(TemporalQueries::chronology()), $input)); }
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]]; }
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); }
public static function from(TemporalAccessor $temporal) { $obj = $temporal->query(TemporalQueries::chronology()); return $obj !== null ? $obj : IsoChronology::INSTANCE(); }
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()); }
/** * Queries this year using the specified query. * <p> * This queries this year 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::chronology()) { return IsoChronology::INSTANCE(); } else { if ($query == TemporalQueries::precision()) { return ChronoUnit::YEARS(); } } return parent::query($query); }
public function query(TemporalQuery $query) { if ($query == TemporalQueries::chronology()) { return null; } throw new UnsupportedOperationException(); }
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)]]; }
/** * Queries this month-day using the specified query. * <p> * This queries this month-day 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::chronology()) { return IsoChronology::INSTANCE(); } return parent::query($query); }
public function test_query_chrono() { $this->assertEquals($this->TEST_DATE_TIME->query(TemporalQueries::chronology()), IsoChronology::INSTANCE()); $this->assertEquals(TemporalQueries::chronology()->queryFrom($this->TEST_DATE_TIME), IsoChronology::INSTANCE()); }
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]]; }
/** * Validates that the temporal has the correct chronology. */ private function validateChrono(TemporalAccessor $temporal) { $temporalChrono = $temporal->query(TemporalQueries::chronology()); if ($temporalChrono != null && IsoChronology::INSTANCE()->equals($temporalChrono) == false) { throw new DateTimeException("Chronology mismatch, expected: ISO, actual: " . $temporalChrono->getId()); } }