Example #1
0
 /**
  * Obtains a {@code Duration} representing the duration between two temporal objects.
  * <p>
  * This calculates the duration between two temporal objects. If the objects
  * are of different types, then the duration is calculated based on the type
  * of the first object. For example, if the first argument is a {@code LocalTime}
  * then the second argument is converted to a {@code LocalTime}.
  * <p>
  * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
  * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
  * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
  * <p>
  * The result of this method can be a negative period if the end is before the start.
  * To guarantee to obtain a positive duration call {@link #abs()} on the result.
  *
  * @param Temporal $startInclusive the start instant, inclusive, not null
  * @param Temporal $endExclusive the end instant, exclusive, not null
  * @return Duration a {@code Duration}, not null
  * @throws DateTimeException if the seconds between the temporals cannot be obtained
  * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
  *
  * TODO check
  */
 public static function between(Temporal $startInclusive, Temporal $endExclusive)
 {
     try {
         return self::ofNanos($startInclusive->until($endExclusive, ChronoUnit::NANOS()));
     } catch (\Exception $ex) {
         $secs = $startInclusive->until($endExclusive, ChronoUnit::SECONDS());
         try {
             $nanos = $endExclusive->getLong(ChronoField::NANO_OF_SECOND()) - $startInclusive->getLong(ChronoField::NANO_OF_SECOND());
             if ($secs > 0 && $nanos < 0) {
                 $secs++;
             } else {
                 if ($secs < 0 && $nanos > 0) {
                     $secs--;
                 }
             }
         } catch (DateTimeException $ex2) {
             $nanos = 0;
         }
         return self::ofSeconds($secs, $nanos);
     }
 }
Example #2
0
 public function between(Temporal $temporal1Inclusive, Temporal $temporal2Exclusive)
 {
     return $temporal1Inclusive->until($temporal2Exclusive, $this);
 }