Example #1
0
 /**
  * Returns a copy of this date-time with the specified amount subtracted.
  * <p>
  * This returns an {@code OffsetDateTime}, based on this one, with the specified amount subtracted.
  * The amount is typically {@link Period} or {@link Duration} but may be
  * any other type implementing the {@link TemporalAmount} interface.
  * <p>
  * The calculation is delegated to the amount object by calling
  * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
  * to implement the subtraction in any way it wishes, however it typically
  * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
  * of the amount implementation to determine if it can be successfully subtracted.
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param TemporalAmount $amountToSubtract the amount to subtract, not null
  * @return OffsetDateTime an {@code OffsetDateTime} based on this date-time with the subtraction made, not null
  * @throws DateTimeException if the subtraction cannot be made
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function minusAmount(TemporalAmount $amountToSubtract)
 {
     return $amountToSubtract->subtractFrom($this);
 }
Example #2
0
 /**
  * Returns a copy of this date with the specified amount subtracted.
  * <p>
  * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
  * The amount is typically {@link Period} but may be any other type implementing
  * the {@link TemporalAmount} interface.
  * <p>
  * The calculation is delegated to the amount object by calling
  * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
  * to implement the subtraction in any way it wishes, however it typically
  * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
  * of the amount implementation to determine if it can be successfully subtracted.
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param TemporalAmount $amountToSubtract the amount to subtract, not null
  * @return LocalDate a {@code LocalDate} based on this date with the subtraction made, not null
  * @throws DateTimeException if the subtraction cannot be made
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function minusAmount(TemporalAmount $amountToSubtract)
 {
     if ($amountToSubtract instanceof Period) {
         /** @var Period $periodToSubtract */
         $periodToSubtract = $amountToSubtract;
         return $this->minusMonths($periodToSubtract->toTotalMonths())->minusDays($periodToSubtract->getDays());
     }
     return $amountToSubtract->subtractFrom($this);
 }
Example #3
0
 /**
  * Obtains an instance of {@code Duration} from a temporal amount.
  * <p>
  * This obtains a duration based on the specified amount.
  * A {@code TemporalAmount} represents an  amount of time, which may be
  * date-based or time-based, which this factory extracts to a duration.
  * <p>
  * The conversion loops around the set of units from the amount and uses
  * the {@linkplain TemporalUnit#getDuration() duration} of the unit to
  * calculate the total {@code Duration}.
  * Only a subset of units are accepted by this method. The unit must either
  * have an {@linkplain TemporalUnit#isDurationEstimated() exact duration}
  * or be {@link ChronoUnit#DAYS} which is treated as 24 hours.
  * If any other units are found then an exception is thrown.
  *
  * @param TemporalAmount $amount the temporal amount to convert, not null
  * @return Duration the equivalent duration, not null
  * @throws DateTimeException if unable to convert to a {@code Duration}
  * @throws ArithmeticException if numeric overflow occurs
  */
 public static function from(TemporalAmount $amount)
 {
     $duration = self::$ZERO;
     foreach ($amount->getUnits() as $unit) {
         $duration = $duration->plus($amount->get($unit), $unit);
     }
     return $duration;
 }
Example #4
0
 /**
  * Returns a copy of this date-time with the specified amount subtracted.
  * <p>
  * This returns a {@code LocalDateTime}, based on this one, with the specified amount subtracted.
  * The amount is typically {@link Period} or {@link Duration} but may be
  * any other type implementing the {@link TemporalAmount} interface.
  * <p>
  * The calculation is delegated to the amount object by calling
  * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
  * to implement the subtraction in any way it wishes, however it typically
  * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
  * of the amount implementation to determine if it can be successfully subtracted.
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param TemporalAmount $amountToSubtract the amount to subtract, not null
  * @return LocalDateTime a {@code LocalDateTime} based on this date-time with the subtraction made, not null
  * @throws DateTimeException if the subtraction cannot be made
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function minusAmount(TemporalAmount $amountToSubtract)
 {
     if ($amountToSubtract instanceof Period) {
         $periodToSubtract = $amountToSubtract;
         return $this->_with($this->date->minusAmount($periodToSubtract), $this->time);
     }
     return $amountToSubtract->subtractFrom($this);
 }
Example #5
0
 /**
  * Returns a copy of this date-time with the specified amount subtracted.
  * <p>
  * This returns a {@code ZonedDateTime}, based on this one, with the specified amount subtracted.
  * The amount is typically {@link Period} or {@link Duration} but may be
  * any other type implementing the {@link TemporalAmount} interface.
  * <p>
  * The calculation is delegated to the amount object by calling
  * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
  * to implement the subtraction in any way it wishes, however it typically
  * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
  * of the amount implementation to determine if it can be successfully subtracted.
  * <p>
  * This instance is immutable and unaffected by this method call.
  *
  * @param TemporalAmount $amountToSubtract the amount to subtract, not null
  * @return ZonedDateTime a {@code ZonedDateTime} based on this date-time with the subtraction made, not null
  * @throws DateTimeException if the subtraction cannot be made
  * @throws ArithmeticException if numeric overflow occurs
  */
 public function minusAmount(TemporalAmount $amountToSubtract)
 {
     if ($amountToSubtract instanceof Period) {
         $periodToSubtract = $amountToSubtract;
         return $this->resolveLocal($this->dateTime->minusAmount($periodToSubtract));
     }
     return $amountToSubtract->subtractFrom($this);
 }
Example #6
0
 /**
  * Obtains an instance of {@code Period} from a temporal amount.
  * <p>
  * This obtains a period based on the specified amount.
  * A {@code TemporalAmount} represents an  amount of time, which may be
  * date-based or time-based, which this factory extracts to a {@code Period}.
  * <p>
  * The conversion loops around the set of units from the amount and uses
  * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
  * and {@link ChronoUnit#DAYS DAYS} units to create a period.
  * If any other units are found then an exception is thrown.
  * <p>
  * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
  *
  * @param TemporalAmount $amount the temporal amount to convert, not null
  * @return Period the equivalent period, not null
  * @throws DateTimeException if unable to convert to a {@code Period}
  * @throws ArithmeticException if the amount of years, months or days exceeds an int
  */
 public static function from(TemporalAmount $amount)
 {
     if ($amount instanceof Period) {
         return $amount;
     }
     if ($amount instanceof ChronoPeriod) {
         if (IsoChronology::INSTANCE()->equals($amount->getChronology()) == false) {
             throw new DateTimeException("Period requires ISO chronology: " . $amount);
         }
     }
     $years = 0;
     $months = 0;
     $days = 0;
     foreach ($amount->getUnits() as $unit) {
         $unitAmount = $amount->get($unit);
         if ($unit == ChronoUnit::YEARS()) {
             $years = Math::toIntExact($unitAmount);
         } else {
             if ($unit == ChronoUnit::MONTHS()) {
                 $months = Math::toIntExact($unitAmount);
             } else {
                 if ($unit == ChronoUnit::DAYS()) {
                     $days = Math::toIntExact($unitAmount);
                 } else {
                     throw new DateTimeException("Unit must be Years, Months or Days, but was " . $unit);
                 }
             }
         }
     }
     return self::create($years, $months, $days);
 }