Beispiel #1
0
 /**
  * Returns a copy of this `LocalDateTime` with the specified period added.
  *
  * @param integer $hours   The hours to add, validated as an integer. May be negative.
  * @param integer $minutes The minutes to add, validated as an integer. May be negative.
  * @param integer $seconds The seconds to add, validated as an integer. May be negative.
  * @param integer $nanos   The nanos to add, validated as an integer. May be negative.
  * @param integer $sign    The sign, validated as an integer of value `1` to add or `-1` to subtract.
  *
  * @return LocalDateTime The combined result.
  */
 private function plusWithOverflow($hours, $minutes, $seconds, $nanos, $sign)
 {
     $totDays = intdiv($hours, LocalTime::HOURS_PER_DAY) + intdiv($minutes, LocalTime::MINUTES_PER_DAY) + intdiv($seconds, LocalTime::SECONDS_PER_DAY);
     $totDays *= $sign;
     $totSeconds = $seconds % LocalTime::SECONDS_PER_DAY + $minutes % LocalTime::MINUTES_PER_DAY * LocalTime::SECONDS_PER_MINUTE + $hours % LocalTime::HOURS_PER_DAY * LocalTime::SECONDS_PER_HOUR;
     $curSoD = $this->time->toSecondOfDay();
     $totSeconds = $totSeconds * $sign + $curSoD;
     $totNanos = $nanos * $sign + $this->time->getNano();
     $totSeconds += Math::floorDiv($totNanos, LocalTime::NANOS_PER_SECOND);
     $newNano = Math::floorMod($totNanos, LocalTime::NANOS_PER_SECOND);
     $totDays += Math::floorDiv($totSeconds, LocalTime::SECONDS_PER_DAY);
     $newSoD = Math::floorMod($totSeconds, LocalTime::SECONDS_PER_DAY);
     $newTime = $newSoD === $curSoD ? $this->time : LocalTime::ofSecondOfDay($newSoD, $newNano);
     return $this->with($this->date->plusDays($totDays), $newTime);
 }
Beispiel #2
0
 /**
  * Compares this LocalTime with another.
  *
  * @param LocalTime $that The time to compare to.
  *
  * @return integer [-1,0,1] If this time is before, on, or after the given time.
  */
 public function compareTo(LocalTime $that)
 {
     $seconds = $this->toSecondOfDay() - $that->toSecondOfDay();
     if ($seconds !== 0) {
         return $seconds > 0 ? 1 : -1;
     }
     $nanos = $this->nano - $that->nano;
     if ($nanos !== 0) {
         return $nanos > 0 ? 1 : -1;
     }
     return 0;
 }