public function test_factory_from_TemporalAccessor_null() { TestHelper::assertNullException($this, function () { OffsetTime::from(null); }); }
/** * Calculates the amount of time until another time in terms of the specified unit. * <p> * This calculates the amount of time between two {@code OffsetTime} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified time. * The result will be negative if the end is before the start. * For example, the amount in hours between two times can be calculated * using {@code startTime.until(endTime, HOURS)}. * <p> * The {@code Temporal} passed to this method is converted to a * {@code OffsetTime} using {@link #from(TemporalAccessor)}. * If the offset differs between the two times, then the specified * end time is normalized to have the same offset as this time. * <p> * The calculation returns a whole number, representing the number of * complete units between the two times. * For example, the amount in hours between 11:30Z and 13:29Z will only * be one hour as it is one minute short of two hours. * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> * // these two lines are equivalent * amount = start.until(end, MINUTES); * amount = MINUTES.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported. * Other {@code ChronoUnit} values will throw an exception. * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} * passing {@code this} as the first argument and the converted input temporal * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * * @param Temporal $endExclusive the end time, exclusive, which is converted to an {@code OffsetTime}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this time and the end time * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to an {@code OffsetTime} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = OffsetTime::from($endExclusive); if ($unit instanceof ChronoUnit) { $nanosUntil = $end->toEpochNano() - $this->toEpochNano(); // no overflow switch ($unit) { case ChronoUnit::NANOS(): return $nanosUntil; case ChronoUnit::MICROS(): return $nanosUntil / 1000; case ChronoUnit::MILLIS(): return $nanosUntil / 1000000; case ChronoUnit::SECONDS(): return $nanosUntil / LocalTime::NANOS_PER_SECOND; case ChronoUnit::MINUTES(): return $nanosUntil / LocalTime::NANOS_PER_MINUTE; case ChronoUnit::HOURS(): return $nanosUntil / LocalTime::NANOS_PER_HOUR; case ChronoUnit::HALF_DAYS(): return $nanosUntil / (12 * LocalTime::NANOS_PER_HOUR); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }