public function getLong(TemporalField $field)
 {
     if ($this->effectiveDate != null && $field->isDateBased()) {
         return $this->effectiveDate->getLong($field);
     }
     return $this->temporal->getLong($field);
 }
 private function monthsUntil(ChronoLocalDate $end)
 {
     $range = $this->getChronology()->range(CF::MONTH_OF_YEAR());
     if ($range->getMaximum() !== 12) {
         throw new \AssertionError("ChronoLocalDateImpl only supports Chronologies with 12 months per year");
     }
     $packed1 = $this->getLong(CF::PROLEPTIC_MONTH()) * 32 + $this->get(CF::DAY_OF_MONTH());
     // no overflow
     $packed2 = $end->getLong(CF::PROLEPTIC_MONTH()) * 32 + $end->get(CF::DAY_OF_MONTH());
     // no overflow
     return ($packed2 - $packed1) / 32;
 }
 protected function resolveAligned(ChronoLocalDate $base, $months, $weeks, $dow)
 {
     $date = $base->plus($months, ChronoUnit::MONTHS())->plus($weeks, ChronoUnit::WEEKS());
     if ($dow > 7) {
         $date = $date->plus(($dow - 1) / 7, ChronoUnit::WEEKS());
         $dow = ($dow - 1) % 7 + 1;
     } else {
         if ($dow < 1) {
             $date = $date->plus(Math::subtractExact($dow, 7) / 7, ChronoUnit::WEEKS());
             $dow = ($dow + 6) % 7 + 1;
         }
     }
     return $date->adjust(TemporalAdjusters::nextOrSame(DayOfWeek::of((int) $dow)));
 }
 /**
  * @inheritdoc
  */
 public function isEqual(ChronoLocalDate $other)
 {
     return $this->toEpochDay() == $other->toEpochDay();
 }
Beispiel #5
0
 private function crossCheck()
 {
     // only cross-check date, time and date-time
     // avoid object creation if possible
     if ($this->date !== null) {
         $this->crossCheck1($this->date);
     }
     if ($this->time !== null) {
         $this->crossCheck1($this->time);
         if ($this->date !== null && !$this->fieldValues->isEmpty()) {
             $this->crossCheck1($this->date->atTime($this->time));
         }
     }
 }
 /**
  * @dataProvider provider_reducedWithChrono
  */
 public function test_reducedWithChronoYearOfEra(ChronoLocalDate $date)
 {
     $chrono = $date->getChronology();
     $df = (new DateTimeFormatterBuilder())->appendValueReduced2(ChronoField::YEAR_OF_ERA(), 2, 2, LocalDate::of(2000, 1, 1))->toFormatter()->withChronology($chrono);
     $expected = $date->get(ChronoField::YEAR_OF_ERA());
     $input = $df->format($date);
     $pos = new ParsePosition(0);
     $parsed = $df->parseUnresolved($input, $pos);
     $actual = $parsed->get(ChronoField::YEAR_OF_ERA());
     $this->assertEquals($actual, $expected, "Wrong date parsed, chrono: " . $chrono . ", input: " . $input);
 }
 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);
 }
Beispiel #8
0
 /**
  * Obtains a {@code Period} consisting of the number of years, months,
  * and days between two dates.
  * <p>
  * The start date is included, but the end date is not.
  * The period is calculated by removing complete months, then calculating
  * the remaining number of days, adjusting to ensure that both have the same sign.
  * The number of months is then split into years and months based on a 12 month year.
  * A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
  * For example, from {@code 2010-01-15} to {@code 2011-03-18} is one year, two months and three days.
  * <p>
  * The result of this method can be a negative period if the end is before the start.
  * The negative sign will be the same in each of year, month and day.
  *
  * TODO better Name
  *
  * @param ChronoLocalDate $startDateInclusive the start date, inclusive, not null
  * @param ChronoLocalDate $endDateExclusive the end date, exclusive, not null
  * @return Period the period between this date and the end date, not null
  * @see ChronoLocalDate#untilDate(ChronoLocalDate)
  */
 public static function between(ChronoLocalDate $startDateInclusive, ChronoLocalDate $endDateExclusive)
 {
     return $startDateInclusive->untilDate($endDateExclusive);
 }