/** * @inheritdoc */ public function query(TemporalQuery $query) { if ($query == TemporalQueries::precision()) { return ChronoUnit::ERAS(); } return parent::query($query); }
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)); }
/** * Calculates the amount of time until another date in terms of the specified unit. * <p> * This calculates the amount of time between two {@code LocalDate} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified date. * The result will be negative if the end is before the start. * The {@code Temporal} passed to this method is converted to a * {@code LocalDate} using {@link #from(TemporalAccessor)}. * For example, the amount in days between two dates can be calculated * using {@code startDate.until(endDate, DAYS)}. * <p> * The calculation returns a whole number, representing the number of * complete units between the two dates. * For example, the amount in months between 2012-06-15 and 2012-08-14 * will only be one month as it is one day short of two months. * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> * // these two lines are equivalent * amount = start.until(end, MONTHS); * amount = MONTHS.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} * are supported. Other {@code ChronoUnit} values will throw an exception. * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} * passing {@code this} as the first argument and the converted input temporal * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $endExclusive the end date, exclusive, which is converted to a {@code LocalDate}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this date and the end date * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to a {@code LocalDate} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = LocalDate::from($endExclusive); if ($unit instanceof ChronoUnit) { switch ($unit) { case ChronoUnit::DAYS(): return $this->daysUntil($end); case ChronoUnit::WEEKS(): return Math::div($this->daysUntil($end), 7); case ChronoUnit::MONTHS(): return $this->monthsUntil($end); case ChronoUnit::YEARS(): return Math::div($this->monthsUntil($end), 12); case ChronoUnit::DECADES(): return Math::div($this->monthsUntil($end), 120); case ChronoUnit::CENTURIES(): return Math::div($this->monthsUntil($end), 1200); case ChronoUnit::MILLENNIA(): return Math::div($this->monthsUntil($end), 12000); case ChronoUnit::ERAS(): return $end->getLong(ChronoField::ERA()) - $this->getLong(ChronoField::ERA()); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }
public function test_isSupported_TemporalUnit() { // TODO $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(null), false); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::NANOS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::MICROS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::MILLIS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::SECONDS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::MINUTES()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::HOURS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::HALF_DAYS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::DAYS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::WEEKS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::MONTHS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::YEARS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::DECADES()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::CENTURIES()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::MILLENNIA()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::ERAS()), true); $this->assertEquals($this->TEST_DATE_TIME->isUnitSupported(CU::FOREVER()), false); }
public function test_isSupported_TemporalUnit() { //$this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(null), false); TODO $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::NANOS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::MICROS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::MILLIS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::SECONDS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::MINUTES()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::HOURS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::HALF_DAYS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::DAYS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::WEEKS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::MONTHS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::YEARS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::DECADES()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::CENTURIES()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::MILLENNIA()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::ERAS()), true); $this->assertEquals(self::TEST_200707_15_12_30_40_987654321()->isUnitSupported(CU::FOREVER()), false); }
function data_minus_long_TemporalUnit() { return [[YearMonth::of(1, 10), 1, CU::YEARS(), YearMonth::of(0, 10), null], [YearMonth::of(1, 10), 12, CU::YEARS(), YearMonth::of(-11, 10), null], [YearMonth::of(1, 10), 0, CU::YEARS(), YearMonth::of(1, 10), null], [YearMonth::of(999999999, 12), 0, CU::YEARS(), YearMonth::of(999999999, 12), null], [YearMonth::of(-999999999, 1), 0, CU::YEARS(), YearMonth::of(-999999999, 1), null], [YearMonth::of(0, 1), 999999999, CU::YEARS(), YearMonth::of(-999999999, 1), null], [YearMonth::of(0, 12), -999999999, CU::YEARS(), YearMonth::of(999999999, 12), null], [YearMonth::of(1, 10), 1, CU::MONTHS(), YearMonth::of(1, 9), null], [YearMonth::of(1, 10), 12, CU::MONTHS(), YearMonth::of(0, 10), null], [YearMonth::of(1, 10), 0, CU::MONTHS(), YearMonth::of(1, 10), null], [YearMonth::of(999999999, 12), 0, CU::MONTHS(), YearMonth::of(999999999, 12), null], [YearMonth::of(-999999999, 1), 0, CU::MONTHS(), YearMonth::of(-999999999, 1), null], [YearMonth::of(-999999999, 2), 1, CU::MONTHS(), YearMonth::of(-999999999, 1), null], [YearMonth::of(999999999, 11), -1, CU::MONTHS(), YearMonth::of(999999999, 12), null], [YearMonth::of(1, 10), 1, CU::ERAS(), YearMonth::of(0, 10), null], [YearMonth::of(5, 10), 1, CU::CENTURIES(), YearMonth::of(-95, 10), null], [YearMonth::of(5, 10), 1, CU::DECADES(), YearMonth::of(-5, 10), null], [YearMonth::of(999999999, 12), -1, CU::MONTHS(), null, DateTimeException::class], [YearMonth::of(-999999999, 1), 1, CU::MONTHS(), null, DateTimeException::class], [YearMonth::of(1, 1), 0, CU::DAYS(), null, DateTimeException::class], [YearMonth::of(1, 1), 0, CU::WEEKS(), null, DateTimeException::class]]; }
public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = $this->getChronology()->dateFrom($endExclusive); if ($unit instanceof ChronoUnit) { switch ($unit) { case CU::DAYS(): return $this->daysUntil($end); case CU::WEEKS(): return $this->daysUntil($end) / 7; case CU::MONTHS(): return $this->monthsUntil($end); case CU::YEARS(): return $this->monthsUntil($end) / 12; case CU::DECADES(): return $this->monthsUntil($end) / 120; case CU::CENTURIES(): return $this->monthsUntil($end) / 1200; case CU::MILLENNIA(): return $this->monthsUntil($end) / 12000; case CU::ERAS(): return $end->getLong(CF::ERA()) - $this->getLong(CF::ERA()); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }
/** * Calculates the amount of time until another year in terms of the specified unit. * <p> * This calculates the amount of time between two {@code Year} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified year. * The result will be negative if the end is before the start. * The {@code Temporal} passed to this method is converted to a * {@code Year} using {@link #from(TemporalAccessor)}. * For example, the amount in decades between two year can be calculated * using {@code startYear.until(endYear, DECADES)}. * <p> * The calculation returns a whole number, representing the number of * complete units between the two years. * For example, the amount in decades between 2012 and 2031 * will only be one decade as it is one year short of two decades. * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> * // these two lines are equivalent * amount = start.until(end, YEARS); * amount = YEARS.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code YEARS}, {@code DECADES}, {@code CENTURIES}, * {@code MILLENNIA} and {@code ERAS} are supported. * Other {@code ChronoUnit} values will throw an exception. * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} * passing {@code this} as the first argument and the converted input temporal * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $endExclusive the end date, exclusive, which is converted to a {@code Year}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this year and the end year * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to a {@code Year} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = Year::from($endExclusive); if ($unit instanceof ChronoUnit) { $yearsUntil = $end->year - $this->year; // no overflow switch ($unit) { case ChronoUnit::YEARS(): return $yearsUntil; case ChronoUnit::DECADES(): return Math::div($yearsUntil, 10); case ChronoUnit::CENTURIES(): return Math::div($yearsUntil, 100); case ChronoUnit::MILLENNIA(): return Math::div($yearsUntil, 1000); case ChronoUnit::ERAS(): return $end->getLong(ChronoField::ERA()) - $this->getLong(ChronoField::ERA()); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }
function data_minus_long_TemporalUnit() { return [[Year::of(1), 1, ChronoUnit::YEARS(), Year::of(0), null], [Year::of(1), -12, ChronoUnit::YEARS(), Year::of(13), null], [Year::of(1), 0, ChronoUnit::YEARS(), Year::of(1), null], [Year::of(999999999), 0, ChronoUnit::YEARS(), Year::of(999999999), null], [Year::of(-999999999), 0, ChronoUnit::YEARS(), Year::of(-999999999), null], [Year::of(0), -999999999, ChronoUnit::YEARS(), Year::of(999999999), null], [Year::of(0), 999999999, ChronoUnit::YEARS(), Year::of(-999999999), null], [Year::of(999999999), 1, ChronoUnit::ERAS(), Year::of(-999999999 + 1), null], [Year::of(105), 1, ChronoUnit::CENTURIES(), Year::of(5), null], [Year::of(15), 1, ChronoUnit::DECADES(), Year::of(5), null], [Year::of(-999999999), 1, ChronoUnit::YEARS(), null, 'Celest\\DateTimeException'], [Year::of(1), -999999999, ChronoUnit::YEARS(), null, 'Celest\\DateTimeException'], [Year::of(1), 0, ChronoUnit::DAYS(), null, 'Celest\\DateTimeException'], [Year::of(1), 0, ChronoUnit::WEEKS(), null, 'Celest\\DateTimeException'], [Year::of(1), 0, ChronoUnit::MONTHS(), null, 'Celest\\DateTimeException']]; }