示例#1
0
 /**
  * Returns a copy of this time with the specified amount added.
  * <p>
  * This returns a {@code LocalTime}, 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 supported fields behave as follows:
  * <ul>
  * <li>{@code NANOS} -
  *  Returns a {@code LocalTime} with the specified number of nanoseconds added.
  *  This is equivalent to {@link #plusNanos(long)}.
  * <li>{@code MICROS} -
  *  Returns a {@code LocalTime} with the specified number of microseconds added.
  *  This is equivalent to {@link #plusNanos(long)} with the amount
  *  multiplied by 1,000.
  * <li>{@code MILLIS} -
  *  Returns a {@code LocalTime} with the specified number of milliseconds added.
  *  This is equivalent to {@link #plusNanos(long)} with the amount
  *  multiplied by 1,000,000.
  * <li>{@code SECONDS} -
  *  Returns a {@code LocalTime} with the specified number of seconds added.
  *  This is equivalent to {@link #plusSeconds(long)}.
  * <li>{@code MINUTES} -
  *  Returns a {@code LocalTime} with the specified number of minutes added.
  *  This is equivalent to {@link #plusMinutes(long)}.
  * <li>{@code HOURS} -
  *  Returns a {@code LocalTime} with the specified number of hours added.
  *  This is equivalent to {@link #plusHours(long)}.
  * <li>{@code HALF_DAYS} -
  *  Returns a {@code LocalTime} with the specified number of half-days added.
  *  This is equivalent to {@link #plusHours(long)} with the amount
  *  multiplied by 12.
  * </ul>
  * <p>
  * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
  * <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 LocalTime a {@code LocalTime} based on this 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) {
         switch ($unit) {
             case ChronoUnit::NANOS():
                 return $this->plusNanos($amountToAdd);
             case ChronoUnit::MICROS():
                 return $this->plusNanos($amountToAdd % self::MICROS_PER_DAY * 1000);
             case ChronoUnit::MILLIS():
                 return $this->plusNanos($amountToAdd % self::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->plusHours($amountToAdd % 2 * 12);
         }
         throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit);
     }
     return $unit->addTo($this, $amountToAdd);
 }
示例#2
0
 /**
  * Returns a copy of this instant with the specified amount added.
  * <p>
  * This returns an Instant {@code Instant}, 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 supported fields behave as follows:
  * <ul>
  * <li>{@code NANOS} -
  *  Returns a {@code Instant} with the specified number of nanoseconds added.
  *  This is equivalent to {@link #plusNanos(long)}.
  * <li>{@code MICROS} -
  *  Returns a {@code Instant} with the specified number of microseconds added.
  *  This is equivalent to {@link #plusNanos(long)} with the amount
  *  multiplied by 1,000.
  * <li>{@code MILLIS} -
  *  Returns a {@code Instant} with the specified number of milliseconds added.
  *  This is equivalent to {@link #plusNanos(long)} with the amount
  *  multiplied by 1,000,000.
  * <li>{@code SECONDS} -
  *  Returns a {@code Instant} with the specified number of seconds added.
  *  This is equivalent to {@link #plusSeconds(long)}.
  * <li>{@code MINUTES} -
  *  Returns a {@code Instant} with the specified number of minutes added.
  *  This is equivalent to {@link #plusSeconds(long)} with the amount
  *  multiplied by 60.
  * <li>{@code HOURS} -
  *  Returns a {@code Instant} with the specified number of hours added.
  *  This is equivalent to {@link #plusSeconds(long)} with the amount
  *  multiplied by 3,600.
  * <li>{@code HALF_DAYS} -
  *  Returns a {@code Instant} with the specified number of half-days added.
  *  This is equivalent to {@link #plusSeconds(long)} with the amount
  *  multiplied by 43,200 (12 hours).
  * <li>{@code DAYS} -
  *  Returns a {@code Instant} with the specified number of days added.
  *  This is equivalent to {@link #plusSeconds(long)} with the amount
  *  multiplied by 86,400 (24 hours).
  * </ul>
  * <p>
  * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
  * <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 Instant an {@code Instant} based on this instant 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) {
         switch ($unit) {
             case ChronoUnit::NANOS():
                 return $this->plusNanos($amountToAdd);
             case ChronoUnit::MICROS():
                 return $this->_plus(Math::div($amountToAdd, 1000000), $amountToAdd % 1000000 * 1000);
             case ChronoUnit::MILLIS():
                 return $this->plusMillis($amountToAdd);
             case ChronoUnit::SECONDS():
                 return $this->plusSeconds($amountToAdd);
             case ChronoUnit::MINUTES():
                 return $this->plusSeconds(Math::multiplyExact($amountToAdd, LocalTime::SECONDS_PER_MINUTE));
             case ChronoUnit::HOURS():
                 return $this->plusSeconds(Math::multiplyExact($amountToAdd, LocalTime::SECONDS_PER_HOUR));
             case ChronoUnit::HALF_DAYS():
                 return $this->plusSeconds(Math::multiplyExact($amountToAdd, Math::div(LocalTime::SECONDS_PER_DAY, 2)));
             case ChronoUnit::DAYS():
                 return $this->plusSeconds(Math::multiplyExact($amountToAdd, LocalTime::SECONDS_PER_DAY));
         }
         throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit);
     }
     return $unit->addTo($this, $amountToAdd);
 }
示例#3
0
 /**
  * Returns a copy of this date with the specified amount added.
  * <p>
  * This returns a {@code LocalDate}, 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>
  * In some cases, adding the amount can cause the resulting date to become invalid.
  * For example, adding one month to 31st January would result in 31st February.
  * In cases like this, the unit is responsible for resolving the date.
  * Typically it will choose the previous valid date, which would be the last valid
  * day of February in this example.
  * <p>
  * If the field is a {@link ChronoUnit} then the addition is implemented here.
  * The supported fields behave as follows:
  * <ul>
  * <li>{@code DAYS} -
  *  Returns a {@code LocalDate} with the specified number of days added.
  *  This is equivalent to {@link #plusDays(long)}.
  * <li>{@code WEEKS} -
  *  Returns a {@code LocalDate} with the specified number of weeks added.
  *  This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week.
  * <li>{@code MONTHS} -
  *  Returns a {@code LocalDate} with the specified number of months added.
  *  This is equivalent to {@link #plusMonths(long)}.
  *  The day-of-month will be unchanged unless it would be invalid for the new
  *  month and year. In that case, the day-of-month is adjusted to the maximum
  *  valid value for the new month and year.
  * <li>{@code YEARS} -
  *  Returns a {@code LocalDate} with the specified number of years added.
  *  This is equivalent to {@link #plusYears(long)}.
  *  The day-of-month will be unchanged unless it would be invalid for the new
  *  month and year. In that case, the day-of-month is adjusted to the maximum
  *  valid value for the new month and year.
  * <li>{@code DECADES} -
  *  Returns a {@code LocalDate} with the specified number of decades added.
  *  This is equivalent to calling {@link #plusYears(long)} with the amount
  *  multiplied by 10.
  *  The day-of-month will be unchanged unless it would be invalid for the new
  *  month and year. In that case, the day-of-month is adjusted to the maximum
  *  valid value for the new month and year.
  * <li>{@code CENTURIES} -
  *  Returns a {@code LocalDate} with the specified number of centuries added.
  *  This is equivalent to calling {@link #plusYears(long)} with the amount
  *  multiplied by 100.
  *  The day-of-month will be unchanged unless it would be invalid for the new
  *  month and year. In that case, the day-of-month is adjusted to the maximum
  *  valid value for the new month and year.
  * <li>{@code MILLENNIA} -
  *  Returns a {@code LocalDate} with the specified number of millennia added.
  *  This is equivalent to calling {@link #plusYears(long)} with the amount
  *  multiplied by 1,000.
  *  The day-of-month will be unchanged unless it would be invalid for the new
  *  month and year. In that case, the day-of-month is adjusted to the maximum
  *  valid value for the new month and year.
  * <li>{@code ERAS} -
  *  Returns a {@code LocalDate} with the specified number of eras added.
  *  Only two eras are supported so the amount must be one, zero or minus one.
  *  If the amount is non-zero then the year is changed such that the year-of-era
  *  is unchanged.
  *  The day-of-month will be unchanged unless it would be invalid for the new
  *  month and year. In that case, the day-of-month is adjusted to the maximum
  *  valid value for the new month and year.
  * </ul>
  * <p>
  * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
  * <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 LocalDate a {@code LocalDate} based on this date 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) {
         /** @var ChronoUnit $f */
         $f = $unit;
         switch ($f) {
             case ChronoUnit::DAYS():
                 return $this->plusDays($amountToAdd);
             case ChronoUnit::WEEKS():
                 return $this->plusWeeks($amountToAdd);
             case ChronoUnit::MONTHS():
                 return $this->plusMonths($amountToAdd);
             case ChronoUnit::YEARS():
                 return $this->plusYears($amountToAdd);
             case ChronoUnit::DECADES():
                 return $this->plusYears(Math::multiplyExact($amountToAdd, 10));
             case ChronoUnit::CENTURIES():
                 return $this->plusYears(Math::multiplyExact($amountToAdd, 100));
             case ChronoUnit::MILLENNIA():
                 return $this->plusYears(Math::multiplyExact($amountToAdd, 1000));
             case ChronoUnit::ERAS():
                 return $this->with(ChronoField::ERA(), Math::addExact($this->getLong(ChronoField::ERA()), $amountToAdd));
         }
         throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit);
     }
     return $unit->addTo($this, $amountToAdd);
 }
 /**
  * @inheritdoc
  */
 public function plus($amountToAdd, TemporalUnit $unit)
 {
     if ($unit instanceof ChronoUnit) {
         throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit);
     }
     return ChronoLocalDateImpl::ensureValid($this->getChronology(), $unit->addTo($this, $amountToAdd));
 }