public function test_from_TemporalAccessor_null() { TestHelper::assertNullException($this, function () { YearMonth::from(null); }); }
/** * Calculates the amount of time until another year-month in terms of the specified unit. * <p> * This calculates the amount of time between two {@code YearMonth} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified year-month. * The result will be negative if the end is before the start. * The {@code Temporal} passed to this method is converted to a * {@code YearMonth} using {@link #from(TemporalAccessor)}. * For example, the amount in years between two year-months can be calculated * using {@code startYearMonth.until(endYearMonth, YEARS)}. * <p> * The calculation returns a whole number, representing the number of * complete units between the two year-months. * For example, the amount in decades between 2012-06 and 2032-05 * will only be one decade as it is one month short of two decades. * <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, MONTHS); * amount = MONTHS.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 MONTHS}, {@code YEARS}, {@code DECADES}, * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} 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 date, exclusive, which is converted to a {@code YearMonth}, not null * @param TemporalUnit $unit the unit to measure the amount in, not null * @return int the amount of time between this year-month and the end year-month * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to a {@code YearMonth} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public function until(Temporal $endExclusive, TemporalUnit $unit) { $end = YearMonth::from($endExclusive); if ($unit instanceof ChronoUnit) { $monthsUntil = $end->getProlepticMonth() - $this->getProlepticMonth(); // no overflow switch ($unit) { case ChronoUnit::MONTHS(): return $monthsUntil; case ChronoUnit::YEARS(): return Math::div($monthsUntil, 12); case ChronoUnit::DECADES(): return Math::div($monthsUntil, 120); case ChronoUnit::CENTURIES(): return Math::div($monthsUntil, 1200); case ChronoUnit::MILLENNIA(): return Math::div($monthsUntil, 12000); case ChronoUnit::ERAS(): return $end->getLong(ChronoField::ERA()) - $this->getLong(ChronoField::ERA()); } throw new UnsupportedTemporalTypeException("Unsupported unit: " . $unit); } return $unit->between($this, $end); }