public function __construct($name, TemporalUnit $baseUnit, TemporalUnit $rangeUnit, $offset) { $this->name = $name; $this->baseUnit = $baseUnit; $this->rangeUnit = $rangeUnit; $this->range = ValueRange::of(-365243219162 + $offset, 365241780471 + $offset); $this->offset = $offset; }
public function rangeRefinedBy(TemporalAccessor $temporal) { if ($this->isSupportedBy($temporal) == false) { throw new UnsupportedTemporalTypeException("Unsupported field: DayOfQuarter"); } $qoy = $temporal->getLong(IsoFields::QUARTER_OF_YEAR()); if ($qoy == 1) { $year = $temporal->getLong(ChronoField::YEAR()); return IsoChronology::INSTANCE()->isLeapYear($year) ? ValueRange::of(1, 91) : ValueRange::of(1, 90); } else { if ($qoy == 2) { return ValueRange::of(1, 91); } else { if ($qoy == 3 || $qoy == 4) { return ValueRange::of(1, 92); } } } // else value not from 1 to 4, so drop through return $this->range(); }
public function range(ChronoField $field) { switch ($field) { case CF::PROLEPTIC_MONTH(): $range = CF::PROLEPTIC_MONTH()->range(); return ValueRange::of($range->getMinimum() + self::YEARS_DIFFERENCE * 12, $range->getMaximum() + self::YEARS_DIFFERENCE * 12); case CF::YEAR_OF_ERA(): $range = CF::YEAR()->range(); return ValueRange::ofVariable(1, -($range->getMinimum() + self::YEARS_DIFFERENCE) + 1, $range->getMaximum() + self::YEARS_DIFFERENCE); case CF::YEAR(): $range = CF::YEAR()->range(); return ValueRange::of($range->getMinimum() + self::YEARS_DIFFERENCE, $range->getMaximum() + self::YEARS_DIFFERENCE); } return $field->range(); }
public static function init() { self::$NANO_OF_SECOND = new ChronoField(0, "NanoOfSecond", ChronoUnit::NANOS(), ChronoUnit::SECONDS(), ValueRange::of(0, 999999999)); self::$NANO_OF_DAY = new ChronoField(1, "NanoOfDay", ChronoUnit::NANOS(), ChronoUnit::DAYS(), ValueRange::of(0, 86400 * 1000000000 - 1)); self::$MICRO_OF_SECOND = new ChronoField(2, "MicroOfSecond", ChronoUnit::MICROS(), ChronoUnit::SECONDS(), ValueRange::of(0, 999999)); self::$MICRO_OF_DAY = new ChronoField(3, "MicroOfDay", ChronoUnit::MICROS(), ChronoUnit::DAYS(), ValueRange::of(0, 86400 * 1000000 - 1)); self::$MILLI_OF_SECOND = new ChronoField(4, "MilliOfSecond", ChronoUnit::MILLIS(), ChronoUnit::SECONDS(), ValueRange::of(0, 999)); self::$MILLI_OF_DAY = new ChronoField(5, "MilliOfDay", ChronoUnit::MILLIS(), ChronoUnit::DAYS(), ValueRange::of(0, 86400 * 1000 - 1)); self::$SECOND_OF_MINUTE = new ChronoField(6, "SecondOfMinute", ChronoUnit::SECONDS(), ChronoUnit::MINUTES(), ValueRange::of(0, 59), "second"); self::$SECOND_OF_DAY = new ChronoField(7, "SecondOfDay", ChronoUnit::SECONDS(), ChronoUnit::DAYS(), ValueRange::of(0, 86400 - 1)); self::$MINUTE_OF_HOUR = new ChronoField(8, "MinuteOfHour", ChronoUnit::MINUTES(), ChronoUnit::HOURS(), ValueRange::of(0, 59), "minute"); self::$MINUTE_OF_DAY = new ChronoField(9, "MinuteOfDay", ChronoUnit::MINUTES(), ChronoUnit::DAYS(), ValueRange::of(0, 24 * 60 - 1)); self::$HOUR_OF_AMPM = new ChronoField(10, "HourOfAmPm", ChronoUnit::HOURS(), ChronoUnit::HALF_DAYS(), ValueRange::of(0, 11)); self::$CLOCK_HOUR_OF_AMPM = new ChronoField(11, "ClockHourOfAmPm", ChronoUnit::HOURS(), ChronoUnit::HALF_DAYS(), ValueRange::of(1, 12)); self::$HOUR_OF_DAY = new ChronoField(12, "HourOfDay", ChronoUnit::HOURS(), ChronoUnit::DAYS(), ValueRange::of(0, 23), "hour"); self::$CLOCK_HOUR_OF_DAY = new ChronoField(13, "ClockHourOfDay", ChronoUnit::HOURS(), ChronoUnit::DAYS(), ValueRange::of(1, 24)); self::$AMPM_OF_DAY = new ChronoField(14, "AmPmOfDay", ChronoUnit::HALF_DAYS(), ChronoUnit::DAYS(), ValueRange::of(0, 1), "dayperiod"); self::$DAY_OF_WEEK = new ChronoField(15, "DayOfWeek", ChronoUnit::DAYS(), ChronoUnit::WEEKS(), ValueRange::of(1, 7), "weekday"); self::$ALIGNED_DAY_OF_WEEK_IN_MONTH = new ChronoField(16, "AlignedDayOfWeekInMonth", ChronoUnit::DAYS(), ChronoUnit::WEEKS(), ValueRange::of(1, 7)); self::$ALIGNED_DAY_OF_WEEK_IN_YEAR = new ChronoField(17, "AlignedDayOfWeekInYear", ChronoUnit::DAYS(), ChronoUnit::WEEKS(), ValueRange::of(1, 7)); self::$DAY_OF_MONTH = new ChronoField(18, "DayOfMonth", ChronoUnit::DAYS(), ChronoUnit::MONTHS(), ValueRange::ofVariable(1, 28, 31), "day"); self::$DAY_OF_YEAR = new ChronoField(19, "DayOfYear", ChronoUnit::DAYS(), ChronoUnit::YEARS(), ValueRange::ofVariable(1, 365, 366)); self::$EPOCH_DAY = new ChronoField(20, "EpochDay", ChronoUnit::DAYS(), ChronoUnit::FOREVER(), ValueRange::of(Year::MIN_VALUE * 365.25, Year::MAX_VALUE * 365.25)); self::$ALIGNED_WEEK_OF_MONTH = new ChronoField(21, "AlignedWeekOfMonth", ChronoUnit::WEEKS(), ChronoUnit::MONTHS(), ValueRange::ofVariable(1, 4, 5)); self::$ALIGNED_WEEK_OF_YEAR = new ChronoField(22, "AlignedWeekOfYear", ChronoUnit::WEEKS(), ChronoUnit::YEARS(), ValueRange::of(1, 53)); self::$MONTH_OF_YEAR = new ChronoField(23, "MonthOfYear", ChronoUnit::MONTHS(), ChronoUnit::YEARS(), ValueRange::of(1, 12), "month"); self::$PROLEPTIC_MONTH = new ChronoField(24, "ProlepticMonth", ChronoUnit::MONTHS(), ChronoUnit::FOREVER(), ValueRange::of(Year::MIN_VALUE * 12, Year::MAX_VALUE * 12 + 11)); self::$YEAR_OF_ERA = new ChronoField(25, "YearOfEra", ChronoUnit::YEARS(), ChronoUnit::FOREVER(), ValueRange::ofVariable(1, Year::MAX_VALUE, Year::MAX_VALUE + 1)); self::$YEAR = new ChronoField(26, "Year", ChronoUnit::YEARS(), ChronoUnit::FOREVER(), ValueRange::of(Year::MIN_VALUE, Year::MAX_VALUE), "year"); self::$ERA = new ChronoField(27, "Era", ChronoUnit::ERAS(), ChronoUnit::FOREVER(), ValueRange::of(0, 1), "era"); self::$INSTANT_SECONDS = new ChronoField(28, "InstantSeconds", ChronoUnit::SECONDS(), ChronoUnit::FOREVER(), ValueRange::of(Long::MIN_VALUE, Long::MAX_VALUE)); self::$OFFSET_SECONDS = new ChronoField(29, "OffsetSeconds", ChronoUnit::SECONDS(), ChronoUnit::FOREVER(), ValueRange::of(-18 * 3600, 18 * 3600)); }
/** * Gets the range of valid values for the specified field. * <p> * The range object expresses the minimum and maximum valid values for a field. * This date is used to enhance the accuracy of the returned range. * If it is not possible to return the range, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return * appropriate range instances. * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the range can be obtained is determined by the field. * * @param TemporalField $field the field to query the range for, not null * @return ValueRange the range of valid values for the field, not null * @throws DateTimeException if the range for the field cannot be obtained * @throws UnsupportedTemporalTypeException if the field is not supported */ public function range(TemporalField $field) { if ($field instanceof ChronoField) { /** @var ChronoField $f */ $f = $field; if ($f->isDateBased()) { switch ($f) { case ChronoField::DAY_OF_MONTH(): return ValueRange::of(1, $this->lengthOfMonth()); case ChronoField::DAY_OF_YEAR(): return ValueRange::of(1, $this->lengthOfYear()); case ChronoField::ALIGNED_WEEK_OF_MONTH(): return ValueRange::of(1, $this->getMonth() == Month::FEBRUARY() && $this->isLeapYear() == false ? 4 : 5); case ChronoField::YEAR_OF_ERA(): return $this->getYear() <= 0 ? ValueRange::of(1, Year::MAX_VALUE + 1) : ValueRange::of(1, Year::MAX_VALUE); } return $field->range(); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->rangeRefinedBy($this); }
public function range(TemporalField $field) { if ($field instanceof ChronoField) { if ($this->isSupported($field)) { $f = $field; switch ($f) { case CF::DAY_OF_MONTH(): case CF::DAY_OF_YEAR(): case CF::ALIGNED_WEEK_OF_MONTH(): return $this->isoDate->range($field); case CF::YEAR_OF_ERA(): $range = CF::YEAR()->range(); $max = $this->getProlepticYear() <= 0 ? -($range->getMinimum() + ThaiBuddhistChronology::YEARS_DIFFERENCE) + 1 : $range->getMaximum() + ThaiBuddhistChronology::YEARS_DIFFERENCE; return ValueRange::of(1, $max); } return $this->getChronology()->range($f); } throw new UnsupportedTemporalTypeException("Unsupported field: " . $field); } return $field->rangeRefinedBy($this); }
/** * Gets the range of valid values for the specified field. * <p> * The range object expresses the minimum and maximum valid values for a field. * This year is used to enhance the accuracy of the returned range. * If it is not possible to return the range, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return * appropriate range instances. * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the range can be obtained is determined by the field. * * @param TemporalField $field the field to query the range for, not null * @return ValueRange the range of valid values for the field, not null * @throws DateTimeException if the range for the field cannot be obtained * @throws UnsupportedTemporalTypeException if the field is not supported */ public function range(TemporalField $field) { if ($field == ChronoField::YEAR_OF_ERA()) { return $this->year <= 0 ? ValueRange::of(1, self::MAX_VALUE + 1) : ValueRange::of(1, self::MAX_VALUE); } return parent::range($field); }
public function test_range() { $this->assertEquals(CF::MONTH_OF_YEAR()->range(), ValueRange::of(1, 12)); $this->assertEquals(CF::MONTH_OF_YEAR()->rangeRefinedBy(LocalDate::of(2000, 2, 29)), ValueRange::of(1, 12)); $this->assertEquals(CF::DAY_OF_MONTH()->range(), ValueRange::ofVariable(1, 28, 31)); $this->assertEquals(CF::DAY_OF_MONTH()->rangeRefinedBy(LocalDate::of(2000, 2, 29)), ValueRange::of(1, 29)); }
/** * @internal * @param LocalDate $date * @return ValueRange */ public static function getWeekRange(LocalDate $date) { $wby = self::getWeekBasedYear($date); return ValueRange::of(1, self::getWeekRangeInt($wby)); }
/** * @group long */ public function test_loop() { // loop round at least one 400 $year cycle, including before 1970 $date = LocalDate::of(1960, 1, 5); // Tuseday of $week 1 1960 $year = 1960; $wby = 1960; $weekLen = 52; $week = 1; while ($date->getYear() < 2400) { $loopDow = $date->getDayOfWeek(); if ($date->getYear() != $year) { $year = $date->getYear(); } if ($loopDow == DayOfWeek::MONDAY()) { $week++; if ($week == 53 && $weekLen == 52 || $week == 54) { $week = 1; $firstDayOfWeekBasedYear = $date->plusDays(14)->withDayOfYear(1); $firstDay = $firstDayOfWeekBasedYear->getDayOfWeek(); $weekLen = $firstDay == DayOfWeek::THURSDAY() || $firstDay == DayOfWeek::WEDNESDAY() && $firstDayOfWeekBasedYear->isLeapYear() ? 53 : 52; $wby++; } } $this->assertEquals(IsoFields::WEEK_OF_WEEK_BASED_YEAR()->rangeRefinedBy($date), ValueRange::of(1, $weekLen), "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals(IsoFields::WEEK_OF_WEEK_BASED_YEAR()->getFrom($date), $week, "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals($date->get(IsoFields::WEEK_OF_WEEK_BASED_YEAR()), $week, "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals(IsoFields::WEEK_BASED_YEAR()->getFrom($date), $wby, "Failed on " . $date . " " . $date->getDayOfWeek()); $this->assertEquals($date->get(IsoFields::WEEK_BASED_YEAR()), $wby, "Failed on " . $date . " " . $date->getDayOfWeek()); $date = $date->plusDays(1); } }
public function rangeRefinedBy(TemporalAccessor $temporal) { return ValueRange::of(1, 20); }
public function range() { return ValueRange::of(1, 4); }
/** * 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); }