Example #1
0
 public function addTo(Temporal $temporal, $amount)
 {
     switch ($this) {
         case IsoFields::WEEK_BASED_YEARS():
             return $temporal->with(IsoFields::WEEK_BASED_YEAR(), Math::addExact($temporal->get(IsoFields::WEEK_BASED_YEAR()), $amount));
         case IsoFields::QUARTER_YEARS():
             // no overflow (256 is multiple of 4)
             return $temporal->plus($amount / 256, ChronoUnit::YEARS())->plus($amount % 256 * 3, ChronoUnit::MONTHS());
         default:
             throw new IllegalStateException("Unreachable");
     }
 }
 public function adjustInto(Temporal $temporal, $newValue)
 {
     // Check the new value and get the old value of the field
     $newVal = $this->range->checkValidIntValue($newValue, $this);
     // lenient check range
     $currentVal = $temporal->get($this);
     if ($newVal === $currentVal) {
         return $temporal;
     }
     if ($this->rangeUnit == ChronoUnit::FOREVER()) {
         // replace year of WeekBasedYear
         // Create a new date object with the same chronology,
         // the desired year and the same week and dow.
         $idow = $temporal->get($this->weekDef->dayOfWeek);
         $wowby = $temporal->get($this->weekDef->weekOfWeekBasedYear);
         return $this->ofWeekBasedYear(AbstractChronology::from($temporal), $newValue, $wowby, $idow);
     } else {
         // Compute the difference and add that using the base unit of the field
         return $temporal->plus($newVal - $currentVal, $this->baseUnit);
     }
 }