/** * @inheritdoc */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof ChronoLocalDate) { return $temporal; } $chrono = $temporal->query(TemporalQueries::chronology()); if ($chrono === null) { throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " . get_class($temporal)); } return $chrono->date($temporal); }
/** * @inheritdoc */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof ChronoZonedDateTime) { return $temporal; } /** @var Chronology $chrono */ $chrono = $temporal->query(TemporalQueries::chronology()); if ($chrono === null) { throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " . get_class($temporal)); } return $chrono->zonedDateTimeFrom($temporal); }
private function assertParsed(TemporalAccessor $parsed, TemporalField $field, $value) { if ($value === null) { $this->assertEquals(false, $parsed->isSupported($field)); } else { $this->assertEquals(true, $parsed->isSupported($field)); $this->assertEquals($value, $parsed->getLong($field)); } }
public static function from(TemporalAccessor $temporal) { $obj = $temporal->query(TemporalQueries::chronology()); return $obj !== null ? $obj : IsoChronology::INSTANCE(); }
public function isSupportedBy(TemporalAccessor $temporal) { return $temporal->isSupported(ChronoField::DAY_OF_YEAR()) && $temporal->isSupported(ChronoField::MONTH_OF_YEAR()) && $temporal->isSupported(ChronoField::YEAR()) && IsoFields::isIso($temporal); }
public function getFrom(TemporalAccessor $temporal) { return $temporal->getLong(CF::EPOCH_DAY()) + $this->offset; }
public function getFrom(TemporalAccessor $temporal) { return $temporal->getLong($this); }
/** * A query for {@code LocalTime} returning null if not found. * @param TemporalAccessor $temporal * @return null|LocalTime */ public static function _localTime(TemporalAccessor $temporal) { if ($temporal->isSupported(ChronoField::NANO_OF_DAY())) { return LocalTime::ofNanoOfDay($temporal->getLong(ChronoField::NANO_OF_DAY())); } return null; }
private function crossCheck1(TemporalAccessor $target) { foreach ($this->fieldValues as $field => $entry) { /** @var CF $field */ if ($target->isSupported($field)) { try { $val1 = $target->getLong($field); } catch (\RuntimeException $ex) { continue; } $val2 = $entry; if ($val1 !== $val2) { throw new DateTimeException("Conflict found: Field " . $field . " " . $val1 . " differs from " . $field . " " . $val2 . " derived from " . $target); } $this->fieldValues->remove($field); } } }
/** * Obtains an instance of {@code DayOfWeek} from a temporal object. * <p> * This obtains a day-of-week based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code DayOfWeek}. * <p> * The conversion extracts the {@link ChronoField#DAY_OF_WEEK DAY_OF_WEEK} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code DayOfWeek::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return DayOfWeek the day-of-week, not null * @throws DateTimeException if unable to convert to a {@code DayOfWeek} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof DayOfWeek) { return $temporal; } try { return self::of($temporal->get(ChronoField::DAY_OF_WEEK())); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain DayOfWeek from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
/** * Obtains an instance of {@code LocalDateTime} from a temporal object. * <p> * This obtains a local date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalDateTime}. * <p> * The conversion extracts and combines the {@code LocalDate} and the * {@code LocalTime} from the temporal object. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDateTime::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return LocalDateTime the local date-time, not null * @throws DateTimeException if unable to convert to a {@code LocalDateTime} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof LocalDateTime) { return $temporal; } else { if ($temporal instanceof ZonedDateTime) { return $temporal->toLocalDateTime(); } else { if ($temporal instanceof OffsetDateTime) { return $temporal->toLocalDateTime(); } } } try { $date = LocalDate::from($temporal); $time = LocalTime::from($temporal); return new LocalDateTime($date, $time); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
public function isSupportedBy(TemporalAccessor $temporal) { return $temporal->isSupported(ChronoField::EPOCH_DAY()) && AbstractChronology::from($temporal)->equals(IsoChronology::INSTANCE()); }
/** * Map the field range to a week range of a week year. * @param TemporalAccessor $temporal the temporal * @return ValueRange the ValueRange with the range adjusted to weeks. */ private function rangeWeekOfWeekBasedYear(TemporalAccessor $temporal) { if (!$temporal->isSupported(CF::DAY_OF_YEAR())) { return self::WEEK_OF_YEAR_RANGE(); } $dow = $this->localizedDayOfWeek($temporal); $doy = $temporal->get(CF::DAY_OF_YEAR()); $offset = $this->startOfWeekOffset($doy, $dow); $week = $this->computeWeek($offset, $doy); if ($week === 0) { // Day is in end of week of previous year // Recompute from the last day of the previous year $date = AbstractChronology::from($temporal)->dateFrom($temporal); $date = $date->minus($doy + 7, ChronoUnit::DAYS()); // Back down into previous year return $this->rangeWeekOfWeekBasedYear($date); } // Check if day of year is in partial week associated with next year $dayRange = $temporal->range(CF::DAY_OF_YEAR()); $yearLen = $dayRange->getMaximum(); $newYearWeek = $this->computeWeek($offset, $yearLen + $this->weekDef->getMinimalDaysInFirstWeek()); if ($week >= $newYearWeek) { // Overlaps with weeks of following year; recompute from a week in following year $date = AbstractChronology::from($temporal)->dateFrom($temporal); $date = $date->plus($yearLen - $doy + 1 + 7, ChronoUnit::DAYS()); return $this->rangeWeekOfWeekBasedYear($date); } return ValueRange::of(1, $newYearWeek - 1); }
/** * @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())); }
public function isSupportedBy(TemporalAccessor $temporal) { return $temporal->isSupported(ChronoField::EPOCH_DAY()) && IsoFields::isIso($temporal); }
/** * Obtains an instance of {@code Instant} from a temporal object. * <p> * This obtains an instant based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code Instant}. * <p> * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS} * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code Instant::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return Instant the instant, not null * @throws DateTimeException if unable to convert to an Instant {@code Instant} */ public static function from(TemporalAccessor $temporal) { if ($temporal instanceof Instant) { return $temporal; } try { $instantSecs = $temporal->getLong(ChronoField::INSTANT_SECONDS()); $nanoOfSecond = $temporal->get(ChronoField::NANO_OF_SECOND()); return Instant::ofEpochSecond($instantSecs, $nanoOfSecond); } catch (DateTimeException $ex) { throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " . $temporal . " of type " . get_class($temporal), $ex); } }
/** * Returns a string version of the context for debugging. * * @return string a string representation of the context, not null */ public function __toString() { return $this->temporal->toString(); }
/** * Obtains an instance of {@code LocalTime} from a temporal object. * <p> * This obtains a local time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalTime}. * <p> * The conversion uses the {@link TemporalQueries#localTime()} query, which relies * on extracting the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code self::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return LocalTime the local time, not null * @throws DateTimeException if unable to convert to a {@code LocalTime} */ public static function from(TemporalAccessor $temporal) { $time = $temporal->query(TemporalQueries::localTime()); if ($time === null) { throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " . $temporal . " of type " . get_class($temporal)); } return $time; }
/** * Obtains an instance of {@code ZoneId} from a temporal object. * <p> * This obtains a zone based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ZoneId}. * <p> * A {@code TemporalAccessor} represents some form of date and time information. * This factory converts the arbitrary temporal object to an instance of {@code ZoneId}. * <p> * The conversion will try to obtain the zone in a way that favours region-based * zones over offset-based zones using {@link TemporalQueries#zone()}. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ZoneId::from}. * * @param TemporalAccessor $temporal the temporal object to convert, not null * @return ZoneId the zone ID, not null * @throws DateTimeException if unable to convert to a {@code ZoneId} */ public static function from(TemporalAccessor $temporal) { $obj = $temporal->query(TemporalQueries::zone()); if ($obj == null) { throw new DateTimeException("Unable to obtain ZoneId from TemporalAccessor: " . $temporal . " of type " . get_class($temporal)); } return $obj; }
/** * @dataProvider data_query */ public function test_query(TemporalAccessor $temporal, TemporalQuery $query, $expected) { $this->assertEquals($temporal->query($query), $expected); }
/** * 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()); } }