Пример #1
0
 /**
  * Returns a copy of this date-time with the specified amount added.
  * <p>
  * This returns a {@code LocalDateTime}, based on this one, with the amount
  * in terms of the unit added. If it is not possible to add the amount, because the
  * unit is not supported or for some other reason, an exception is thrown.
  * <p>
  * If the field is a {@link ChronoUnit} then the addition is implemented here.
  * Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}.
  * Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with
  * any overflow in days added equivalent to using {@link #plusDays(long)}.
  * <p>
  * If the field is not a {@code ChronoUnit}, then the result of this method
  * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
  * passing {@code this} as the argument. In this case, the unit determines
  * whether and how to perform the addition.
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param int $amountToAdd the amount of the unit to add to the result, may be negative
  * @param TemporalUnit $unit the unit of the amount to add, not null
  * @return LocalDateTime a {@code LocalDateTime} based on this date-time with the specified amount added, not null
  * @throws DateTimeException if the addition cannot be made
  * @throws UnsupportedTemporalTypeException if the unit is not supported
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function plus($amountToAdd, TemporalUnit $unit)
 {
     if ($unit instanceof ChronoUnit) {
         $f = $unit;
         switch ($f) {
             case ChronoUnit::NANOS():
                 return $this->plusNanos($amountToAdd);
             case ChronoUnit::MICROS():
                 return $this->plusDays($amountToAdd / LocalTime::MICROS_PER_DAY)->plusNanos($amountToAdd % LocalTime::MICROS_PER_DAY * 1000);
             case ChronoUnit::MILLIS():
                 return $this->plusDays($amountToAdd / LocalTime::MILLIS_PER_DAY)->plusNanos($amountToAdd % LocalTime::MILLIS_PER_DAY * 1000000);
             case ChronoUnit::SECONDS():
                 return $this->plusSeconds($amountToAdd);
             case ChronoUnit::MINUTES():
                 return $this->plusMinutes($amountToAdd);
             case ChronoUnit::HOURS():
                 return $this->plusHours($amountToAdd);
             case ChronoUnit::HALF_DAYS():
                 return $this->plusDays($amountToAdd / 256)->plusHours($amountToAdd % 256 * 12);
                 // no overflow (256 is multiple of 2)
         }
         return $this->_with($this->date->plus($amountToAdd, $unit), $this->time);
     }
     return $unit->addTo($this, $amountToAdd);
 }