/** * Returns a copy of this date-time with the specified amount added. * <p> * This returns an {@code OffsetDateTime}, 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 by * {@link LocalDateTime#plus(long, TemporalUnit)}. * The offset is not part of the calculation and will be unchanged in the result. * <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 OffsetDateTime an {@code OffsetDateTime} 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) { return $this->_with($this->dateTime->plus($amountToAdd, $unit), $this->offset); } return $unit->addTo($this, $amountToAdd); }
/** * Returns a copy of this date-time with the specified amount added. * <p> * This returns a {@code ZonedDateTime}, 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. * The zone is not part of the calculation and will be unchanged in the result. * The calculation for date and time units differ. * <p> * Date units operate on the local time-line. * The period is first added to the local date-time, then converted back * to a zoned date-time using the zone ID. * The conversion uses {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)} * with the offset before the addition. * <p> * Time units operate on the instant time-line. * The period is first added to the local date-time, then converted back to * a zoned date-time using the zone ID. * The conversion uses {@link #ofInstant(LocalDateTime, ZoneOffset, ZoneId)} * with the offset before the addition. * <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 ZonedDateTime a {@code ZonedDateTime} 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) { if ($unit->isDateBased()) { return $this->resolveLocal($this->dateTime->plus($amountToAdd, $unit)); } else { return $this->resolveInstant($this->dateTime->plus($amountToAdd, $unit)); } } return $unit->addTo($this, $amountToAdd); }