function data_toString() { return [[Instant::ofEpochSecond(65, 567), "1970-01-01T00:01:05.000000567Z"], [Instant::ofEpochSecond(65, 560), "1970-01-01T00:01:05.000000560Z"], [Instant::ofEpochSecond(65, 560000), "1970-01-01T00:01:05.000560Z"], [Instant::ofEpochSecond(65, 560000000), "1970-01-01T00:01:05.560Z"], [Instant::ofEpochSecond(1, 0), "1970-01-01T00:00:01Z"], [Instant::ofEpochSecond(60, 0), "1970-01-01T00:01:00Z"], [Instant::ofEpochSecond(3600, 0), "1970-01-01T01:00:00Z"], [Instant::ofEpochSecond(-1, 0), "1969-12-31T23:59:59Z"], [LocalDateTime::of(0, 1, 2, 0, 0)->toInstant(ZoneOffset::UTC()), "0000-01-02T00:00:00Z"], [LocalDateTime::of(0, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "0000-01-01T12:30:00Z"], [LocalDateTime::of(0, 1, 1, 0, 0, 0, 1)->toInstant(ZoneOffset::UTC()), "0000-01-01T00:00:00.000000001Z"], [LocalDateTime::of(0, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "0000-01-01T00:00:00Z"], [LocalDateTime::of(-1, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "-0001-12-31T23:59:59.999999999Z"], [LocalDateTime::of(-1, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-0001-12-31T12:30:00Z"], [LocalDateTime::of(-1, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "-0001-12-30T12:30:00Z"], [LocalDateTime::of(-9999, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "-9999-01-02T12:30:00Z"], [LocalDateTime::of(-9999, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "-9999-01-01T12:30:00Z"], [LocalDateTime::of(-9999, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "-9999-01-01T00:00:00Z"], [LocalDateTime::of(-10000, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "-10000-12-31T23:59:59.999999999Z"], [LocalDateTime::of(-10000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-10000-12-31T12:30:00Z"], [LocalDateTime::of(-10000, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "-10000-12-30T12:30:00Z"], [LocalDateTime::of(-15000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-15000-12-31T12:30:00Z"], [LocalDateTime::of(-19999, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "-19999-01-02T12:30:00Z"], [LocalDateTime::of(-19999, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "-19999-01-01T12:30:00Z"], [LocalDateTime::of(-19999, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "-19999-01-01T00:00:00Z"], [LocalDateTime::of(-20000, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "-20000-12-31T23:59:59.999999999Z"], [LocalDateTime::of(-20000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-20000-12-31T12:30:00Z"], [LocalDateTime::of(-20000, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "-20000-12-30T12:30:00Z"], [LocalDateTime::of(-25000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "-25000-12-31T12:30:00Z"], [LocalDateTime::of(9999, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "9999-12-30T12:30:00Z"], [LocalDateTime::of(9999, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "9999-12-31T12:30:00Z"], [LocalDateTime::of(9999, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "9999-12-31T23:59:59.999999999Z"], [LocalDateTime::of(10000, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "+10000-01-01T00:00:00Z"], [LocalDateTime::of(10000, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "+10000-01-01T12:30:00Z"], [LocalDateTime::of(10000, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "+10000-01-02T12:30:00Z"], [LocalDateTime::of(15000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "+15000-12-31T12:30:00Z"], [LocalDateTime::of(19999, 12, 30, 12, 30)->toInstant(ZoneOffset::UTC()), "+19999-12-30T12:30:00Z"], [LocalDateTime::of(19999, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "+19999-12-31T12:30:00Z"], [LocalDateTime::of(19999, 12, 31, 23, 59, 59, 999999999)->toInstant(ZoneOffset::UTC()), "+19999-12-31T23:59:59.999999999Z"], [LocalDateTime::of(20000, 1, 1, 0, 0)->toInstant(ZoneOffset::UTC()), "+20000-01-01T00:00:00Z"], [LocalDateTime::of(20000, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC()), "+20000-01-01T12:30:00Z"], [LocalDateTime::of(20000, 1, 2, 12, 30)->toInstant(ZoneOffset::UTC()), "+20000-01-02T12:30:00Z"], [LocalDateTime::of(25000, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC()), "+25000-12-31T12:30:00Z"], [LocalDateTime::of(-999999999, 1, 1, 12, 30)->toInstant(ZoneOffset::UTC())->minus(1, CU::DAYS()), "-1000000000-12-31T12:30:00Z"], [LocalDateTime::of(999999999, 12, 31, 12, 30)->toInstant(ZoneOffset::UTC())->plus(1, CU::DAYS()), "+1000000000-01-01T12:30:00Z"], [Instant::MIN(), "-1000000000-01-01T00:00:00Z"], [Instant::MAX(), "+1000000000-12-31T23:59:59.999999999Z"]]; }
/** * A constant for a period of zero, measured in days. * @return MockSimplePeriod */ public static function ZERO_DAYS() { return new MockSimplePeriod(0, CU::DAYS()); }
public function test_until_nullTemporal() { TestHelper::assertNullException($this, function () { $this->TEST_DATE_TIME_PARIS->until(null, CU::DAYS()); }); }
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)); }
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); }
/** * @expectedException \Celest\DateTimeException */ public function test_until_invalidType() { $start = LocalDateTime::of(2010, 6, 30, 2, 30); $start->until(LocalTime::of(11, 30), CU::DAYS()); }
function data_goodTemporalUnit() { return [[2, CU::DAYS()], [2, CU::MONTHS()], [2, CU::YEARS()]]; }
function provider_factory_of_badTemporalUnit() { return [[0, CU::MICROS()], [0, CU::MILLIS()], [0, CU::MINUTES()], [0, CU::HOURS()], [0, CU::HALF_DAYS()], [0, CU::DAYS()], [0, CU::MONTHS()], [0, CU::YEARS()], [0, CU::DECADES()], [0, CU::CENTURIES()], [0, CU::MILLENNIA()]]; }
/** * 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); }
/** * Returns the previous-or-same day-of-week adjuster, which adjusts the date to the * first occurrence of the specified day-of-week before the date being adjusted * unless it is already on that day in which case the same object is returned. * <p> * The ISO calendar system behaves as follows:<br> * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br> * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br> * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input). * <p> * The behavior is suitable for use with most calendar systems. * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit, * and assumes a seven day week. * * @param DayOfWeek $dayOfWeek the day-of-week to check for or move the date to, not null * @return TemporalAdjuster the previous-or-same day-of-week adjuster, not null */ public static function previousOrSame(DayOfWeek $dayOfWeek) { $dowValue = $dayOfWeek->getValue(); return self::fromCallable(function (Temporal $temporal) use($dowValue) { $calDow = $temporal->get(ChronoField::DAY_OF_WEEK()); if ($calDow == $dowValue) { return $temporal; } $daysDiff = $dowValue - $calDow; return $temporal->minus($daysDiff >= 0 ? 7 - $daysDiff : -$daysDiff, ChronoUnit::DAYS()); }); }
public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = $this->getChronology()->localDateTime($endExclusive); if ($unit instanceof ChronoUnit) { if ($unit->isTimeBased()) { $amount = $end->getLong(ChronoField::EPOCH_DAY()) - $this->date->getLong(ChronoField::EPOCH_DAY()); switch ($unit) { case ChronoUnit::NANOS(): $amount = Math::multiplyExact($amount, self::NANOS_PER_DAY); break; case ChronoUnit::MICROS(): $amount = Math::multiplyExact($amount, self::MICROS_PER_DAY); break; case ChronoUnit::MILLIS(): $amount = Math::multiplyExact($amount, self::MILLIS_PER_DAY); break; case ChronoUnit::SECONDS(): $amount = Math::multiplyExact($amount, self::SECONDS_PER_DAY); break; case ChronoUnit::MINUTES(): $amount = Math::multiplyExact($amount, self::MINUTES_PER_DAY); break; case ChronoUnit::HOURS(): $amount = Math::multiplyExact($amount, self::HOURS_PER_DAY); break; case ChronoUnit::HALF_DAYS(): $amount = Math::multiplyExact($amount, 2); break; } return Math::addExact($amount, $this->time->until($end->toLocalTime(), $unit)); } $endDate = $end->toLocalDate(); if ($end->toLocalTime()->isBefore($this->time)) { $endDate = $endDate->minus(1, ChronoUnit::DAYS()); } return $this->date->until($endDate, $unit); } return $unit->between($this, $end); }
/** * @expectedException \Celest\Temporal\UnsupportedTemporalTypeException */ public function test_until_TemporalUnit_unsupportedUnit() { self::TEST_123040987654321()->until(self::TEST_123040987654321(), CU::DAYS()); }
public function test_truncatedTo_normal() { $this->assertEquals(self::TEST_11_30_59_500_PONE()->truncatedTo(CU::NANOS()), self::TEST_11_30_59_500_PONE()); $this->assertEquals(self::TEST_11_30_59_500_PONE()->truncatedTo(CU::SECONDS()), self::TEST_11_30_59_500_PONE()->withNano(0)); $this->assertEquals(self::TEST_11_30_59_500_PONE()->truncatedTo(CU::DAYS()), self::TEST_11_30_59_500_PONE()->adjust(LocalTime::MIDNIGHT())); }
function data_fieldUnit() { return [[CF::YEAR(), CU::YEARS(), CU::FOREVER()], [CF::MONTH_OF_YEAR(), CU::MONTHS(), CU::YEARS()], [CF::DAY_OF_MONTH(), CU::DAYS(), CU::MONTHS()], [CF::DAY_OF_WEEK(), CU::DAYS(), CU::WEEKS()], [CF::DAY_OF_YEAR(), CU::DAYS(), CU::YEARS()], [CF::HOUR_OF_DAY(), CU::HOURS(), CU::DAYS()], [CF::MINUTE_OF_DAY(), CU::MINUTES(), CU::DAYS()], [CF::MINUTE_OF_HOUR(), CU::MINUTES(), CU::HOURS()], [CF::SECOND_OF_DAY(), CU::SECONDS(), CU::DAYS()], [CF::SECOND_OF_MINUTE(), CU::SECONDS(), CU::MINUTES()], [CF::MILLI_OF_DAY(), CU::MILLIS(), CU::DAYS()], [CF::MILLI_OF_SECOND(), CU::MILLIS(), CU::SECONDS()], [CF::MICRO_OF_SECOND(), CU::MICROS(), CU::SECONDS()], [CF::MICRO_OF_DAY(), CU::MICROS(), CU::DAYS()], [CF::NANO_OF_SECOND(), CU::NANOS(), CU::SECONDS()], [CF::NANO_OF_DAY(), CU::NANOS(), CU::DAYS()]]; }
/** * Queries this day-of-week using the specified query. * <p> * This queries this day-of-week 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 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::DAYS(); } return parent::query($query); }
function data_untilUnit() { return [[OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 13, 1, 1, 0, self::OFFSET_PONE()), CU::HALF_DAYS(), 1], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 2, 1, 1, 0, self::OFFSET_PONE()), CU::HOURS(), 1], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 2, 1, 1, 0, self::OFFSET_PONE()), CU::MINUTES(), 60], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 2, 1, 1, 0, self::OFFSET_PONE()), CU::SECONDS(), 3600], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 2, 1, 1, 0, self::OFFSET_PONE()), CU::MILLIS(), 3600 * 1000], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 2, 1, 1, 0, self::OFFSET_PONE()), CU::MICROS(), 3600 * 1000 * 1000], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 2, 1, 1, 0, self::OFFSET_PONE()), CU::NANOS(), 3600 * 1000 * 1000 * 1000], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 14, 1, 1, 0, self::OFFSET_PTWO()), CU::HALF_DAYS(), 1], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 3, 1, 1, 0, self::OFFSET_PTWO()), CU::HOURS(), 1], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 3, 1, 1, 0, self::OFFSET_PTWO()), CU::MINUTES(), 60], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 3, 1, 1, 0, self::OFFSET_PTWO()), CU::SECONDS(), 3600], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 3, 1, 1, 0, self::OFFSET_PTWO()), CU::MILLIS(), 3600 * 1000], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 3, 1, 1, 0, self::OFFSET_PTWO()), CU::MICROS(), 3600 * 1000 * 1000], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 6, 30, 3, 1, 1, 0, self::OFFSET_PTWO()), CU::NANOS(), 3600 * 1000 * 1000 * 1000], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 7, 1, 1, 1, 0, 999999999, self::OFFSET_PONE()), CU::DAYS(), 0], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 7, 1, 1, 1, 1, 0, self::OFFSET_PONE()), CU::DAYS(), 1], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 8, 29, 1, 1, 1, 0, self::OFFSET_PONE()), CU::MONTHS(), 1], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 8, 30, 1, 1, 1, 0, self::OFFSET_PONE()), CU::MONTHS(), 2], [OffsetDateTime::of(2010, 6, 30, 1, 1, 1, 0, self::OFFSET_PONE()), OffsetDateTime::of(2010, 8, 31, 1, 1, 1, 0, self::OFFSET_PONE()), CU::MONTHS(), 2]]; }
/** * @dataProvider data_weekFields */ public function test_rangeWeekOfWeekBasedYear(DayOfWeek $firstDayOfWeek, $minDays) { $weekFields = WeekFields::of($firstDayOfWeek, $minDays); $dowField = $weekFields->dayOfWeek(); $wowByField = $weekFields->weekOfWeekBasedYear(); $day1 = LocalDate::of(2012, 1, $weekFields->getMinimalDaysInFirstWeek()); $day1 = $day1->with($wowByField, 1)->with($dowField, 1); $day2 = LocalDate::of(2013, 1, $weekFields->getMinimalDaysInFirstWeek()); $day2 = $day2->with($wowByField, 1)->with($dowField, 1); $expectedWeeks = ChronoUnit::DAYS()->between($day1, $day2) / 7; $range = $day1->range($wowByField); $this->assertEquals($range->getMaximum(), $expectedWeeks, "Range incorrect"); }
/** * Returns a copy of this duration with the specified duration added. * <p> * The duration amount is measured in terms of the specified unit. * Only a subset of units are accepted by this method. * The unit must either have an {@linkplain TemporalUnit#isDurationEstimated() exact duration} or * be {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception. * <p> * This instance is immutable and unaffected by this method call. * * @param int $amountToAdd the amount to add, measured in terms of the unit, positive or negative * @param TemporalUnit $unit the unit that the amount is measured in, must have an exact duration, not null * @return Duration a {@code Duration} based on this duration with the specified duration added, not null * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function plus($amountToAdd, TemporalUnit $unit) { if ($unit == ChronoUnit::DAYS()) { return $this->_plus(Math::multiplyExact($amountToAdd, LocalTime::SECONDS_PER_DAY), 0); } if ($unit->isDurationEstimated()) { throw new UnsupportedTemporalTypeException("Unit must not have an estimated duration"); } if ($amountToAdd == 0) { return $this; } if ($unit instanceof ChronoUnit) { switch ($unit) { case ChronoUnit::NANOS(): return $this->plusNanos($amountToAdd); case ChronoUnit::MICROS(): return $this->plusSeconds(Math::div($amountToAdd, 1000000 * 1000) * 1000)->plusNanos($amountToAdd % (1000000 * 1000) * 1000); case ChronoUnit::MILLIS(): return $this->plusMillis($amountToAdd); case ChronoUnit::SECONDS(): return $this->plusSeconds($amountToAdd); } return $this->plusSeconds(Math::multiplyExact($unit->getDuration()->seconds, $amountToAdd)); } $duration = $unit->getDuration()->multipliedBy($amountToAdd); return $this->plusSeconds($duration->getSeconds())->plusNanos($duration->getNano()); }
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]]; }
public function test_until_TemporalUnit_nullEnd() { TestHelper::assertNullException($this, function () { self::TEST_2008_06()->until(null, CU::DAYS()); }); }
/** * @inheritdoc */ public function query(TemporalQuery $query) { if ($query == TemporalQueries::zoneId() || $query == TemporalQueries::zone() || $query == TemporalQueries::offset()) { return null; } else { if ($query == TemporalQueries::localTime()) { return null; } else { if ($query == TemporalQueries::chronology()) { return $this->getChronology(); } else { if ($query == TemporalQueries::precision()) { return ChronoUnit::DAYS(); } } } } // inline TemporalAccessor.super.query(query) as an optimization // non-JDK classes are not permitted to make this optimization return $query->queryFrom($this); }
/** * Calculates the amount of time until another instant in terms of the specified unit. * <p> * This calculates the amount of time between two {@code Instant} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified instant. * The result will be negative if the end is before the start. * The calculation returns a whole number, representing the number of * complete units between the two instants. * The {@code Temporal} passed to this method is converted to a * {@code Instant} using {@link #from(TemporalAccessor)}. * For example, the amount in days between two dates can be calculated * using {@code startInstant.until(endInstant, SECONDS)}. * <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, SECONDS); * amount = SECONDS.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 NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS} * 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 an Instant {@code Instant}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this instant and the end instant * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to an Instant {@code Instant} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = Instant::from($endExclusive); if ($unit instanceof ChronoUnit) { $f = $unit; switch ($f) { case ChronoUnit::NANOS(): return $this->nanosUntil($end); case ChronoUnit::MICROS(): return Math::div($this->nanosUntil($end), 1000); case ChronoUnit::MILLIS(): return Math::subtractExact($end->toEpochMilli(), $this->toEpochMilli()); case ChronoUnit::SECONDS(): return $this->secondsUntil($end); case ChronoUnit::MINUTES(): return Math::div($this->secondsUntil($end), LocalTime::SECONDS_PER_MINUTE); case ChronoUnit::HOURS(): return Math::div($this->secondsUntil($end), LocalTime::SECONDS_PER_HOUR); case ChronoUnit::HALF_DAYS(): return Math::div($this->secondsUntil($end), 12 * LocalTime::SECONDS_PER_HOUR); case ChronoUnit::DAYS(): return Math::div($this->secondsUntil($end), LocalTime::SECONDS_PER_DAY); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }
public function getBaseUnit() { return ChronoUnit::DAYS(); }
/** * 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); }
protected function resolveYAD(FieldValues $fieldValues, ResolverStyle $resolverStyle) { $y = $this->range(ChronoField::YEAR())->checkValidIntValue($fieldValues->remove(ChronoField::YEAR()), ChronoField::YEAR()); if ($resolverStyle == ResolverStyle::LENIENT()) { $weeks = Math::subtractExact($fieldValues->remove(ChronoField::ALIGNED_WEEK_OF_YEAR()), 1); $dow = Math::subtractExact($fieldValues->remove(ChronoField::DAY_OF_WEEK()), 1); return $this->resolveAligned($this->dateYearDay($y, 1), 0, $weeks, $dow); } $aw = $this->range(ChronoField::ALIGNED_WEEK_OF_YEAR())->checkValidIntValue($fieldValues->remove(ChronoField::ALIGNED_WEEK_OF_YEAR()), ChronoField::ALIGNED_WEEK_OF_YEAR()); $dow = $this->range(ChronoField::DAY_OF_WEEK())->checkValidIntValue($fieldValues->remove(ChronoField::DAY_OF_WEEK()), ChronoField::DAY_OF_WEEK()); $date = $this->dateYearDay($y, 1)->plus(($aw - 1) * 7, ChronoUnit::DAYS())->adjust(TemporalAdjusters::nextOrSame(DayOfWeek::of($dow))); if ($resolverStyle == ResolverStyle::STRICT() && $date->get(ChronoField::YEAR()) != $y) { throw new DateTimeException("Strict mode rejected resolved date as it is in a different year"); } return $date; }
/** * Rata Die field. * <p> * Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO). * The field always refers to the local date-time, ignoring the offset or zone. * <p> * For date-times, 'RATA_DIE.getFrom()' assumes the same value from * midnight until just before the next midnight. * When 'RATA_DIE.adjustInto()' is applied to a date-time, the time of day portion remains unaltered. * 'RATA_DIE.adjustInto()' and 'RATA_DIE.getFrom()' only apply to {@code Temporal} objects * that can be converted into {@link ChronoField#EPOCH_DAY}. * An {@link UnsupportedTemporalTypeException} is thrown for any other type of object. * <p> * In the resolving phase of parsing, a date can be created from a Rata Die field. * In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode} * the Rata Die value is validated against the range of valid values. * In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs. * @return TemporalField */ public static function RATA_DIE() { if (self::$RATA_DIE === null) { self::$RATA_DIE = new JulianField("RataDie", CU::DAYS(), CU::FOREVER(), 719163); } return self::$RATA_DIE; }
/** * Subtracts this period from the specified temporal object. * <p> * This returns a temporal object of the same observable type as the input * with this period subtracted. * If the temporal has a chronology, it must be the ISO chronology. * <p> * In most cases, it is clearer to reverse the calling pattern by using * {@link Temporal#minus(TemporalAmount)}. * <pre> * // these two lines are equivalent, but the second approach is recommended * dateTime = thisPeriod.subtractFrom(dateTime); * dateTime = dateTime.minus(thisPeriod); * </pre> * <p> * The calculation operates as follows. * First, the chronology of the temporal is checked to ensure it is ISO chronology or null. * Second, if the months are zero, the years are subtracted if non-zero, otherwise * the combination of years and months is subtracted if non-zero. * Finally, any days are subtracted. * <p> * This approach ensures that a partial period can be subtracted from a partial date. * For example, a period of years and/or months can be subtracted from a {@code YearMonth}, * but a period including days cannot. * The approach also subtracts years and months together when necessary, which ensures * correct behaviour at the end of the month. * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $temporal the temporal object to adjust, not null * @return Temporal an object of the same type with the adjustment made, not null * @throws DateTimeException if unable to subtract * @throws ArithmeticException if numeric overflow occurs */ public function subtractFrom(Temporal $temporal) { $this->validateChrono($temporal); if ($this->months == 0) { if ($this->years != 0) { $temporal = $temporal->minus($this->years, ChronoUnit::YEARS()); } } else { $totalMonths = $this->toTotalMonths(); if ($totalMonths != 0) { $temporal = $temporal->minus($totalMonths, ChronoUnit::MONTHS()); } } if ($this->days != 0) { $temporal = $temporal->minus($this->days, ChronoUnit::DAYS()); } return $temporal; }