コード例 #1
0
ファイル: Duration.php プロジェクト: celest-time/prototype
 /**
  * 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;
 }
コード例 #2
0
ファイル: Period.php プロジェクト: celest-time/prototype
 /**
  * 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);
 }