@Override @SuppressWarnings("unchecked") public D plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7)); case MONTHS: return plusMonths(amountToAdd); case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd)); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return (D) ChronoLocalDate.super.plus(amountToAdd, unit); }
@Test public void test_isSupported_TemporalUnit() { assertEquals(TEST_DATE_TIME.isSupported((TemporalUnit) null), false); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.NANOS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MICROS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MILLIS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.SECONDS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MINUTES), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.HOURS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.HALF_DAYS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.DAYS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.WEEKS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MONTHS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.YEARS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.DECADES), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.CENTURIES), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MILLENNIA), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.ERAS), true); assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.FOREVER), false); }
@Override public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusMinutes(amountToAdd); case HOURS: return plusHours(amountToAdd); case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2) } return with(date.plus(amountToAdd, unit), time); } return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd)); }
/** * Returns a copy of this duration with the specified duration added. * <p> * The duration amount is measured in terms of the specified unit. * 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. Other units throw an exception. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the period, measured in terms of the unit, positive or negative * @param unit the unit that the period is measured in, must have an exact duration, not null * @return a {@code Duration} based on this duration with the specified duration added, not null * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public Duration plus(long amountToAdd, TemporalUnit unit) { Objects.requireNonNull(unit, "unit"); if (unit == DAYS) { return plus(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY), 0); } if (unit.isDurationEstimated()) { throw new UnsupportedTemporalTypeException("Unit must not have an estimated duration"); } if (amountToAdd == 0) { return this; } if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); } return plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd)); } Duration duration = unit.getDuration().multipliedBy(amountToAdd); return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano()); }
/** * Returns a copy of this duration with the specified duration added. * <p> * The duration amount is measured in terms of the specified unit. * 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. Other units throw an exception. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount to add, measured in terms of the unit, positive or negative * @param unit the unit that the amount is measured in, must have an exact duration, not null * @return a {@code Duration} based on this duration with the specified duration added, not null * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public Duration plus(long amountToAdd, TemporalUnit unit) { Objects.requireNonNull(unit, "unit"); if (unit == DAYS) { return plus(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY), 0); } if (unit.isDurationEstimated()) { throw new UnsupportedTemporalTypeException("Unit must not have an estimated duration"); } if (amountToAdd == 0) { return this; } if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); } return plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd)); } Duration duration = unit.getDuration().multipliedBy(amountToAdd); return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano()); }
@Test(dataProvider="calendars") public void test_badMinusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDate date = chrono.date(refDate); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDate date2 = chrono2.date(refDate); TemporalUnit adjuster = new FixedTemporalUnit(date2); if (chrono != chrono2) { try { date.minus(1, adjuster); Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass() + ", can not be cast to " + date2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDate result = date.minus(1, adjuster); assertEquals(result, date2, "WithAdjuster failed to replace date"); } } }
@Test public void test_isSupported_TemporalUnit() { assertEquals(TEST_2007_07_15.isSupported((TemporalUnit) null), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.NANOS), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MICROS), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MILLIS), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.SECONDS), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MINUTES), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.HOURS), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.HALF_DAYS), false); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.DAYS), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.WEEKS), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MONTHS), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.YEARS), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.DECADES), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.CENTURIES), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MILLENNIA), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.ERAS), true); assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.FOREVER), false); }
@Test(dataProvider="calendars") public void test_badPlusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalUnit adjuster = new FixedTemporalUnit(cdt2); if (chrono != chrono2) { try { cdt.plus(1, adjuster); Assert.fail("TemporalUnit.doPlus plus should have thrown a ClassCastException" + cdt + ", can not be cast to " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.plus(1, adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test public void test_Period_getUnits() { Period period = Period.of(2012, 1, 1); List<TemporalUnit> units = period.getUnits(); assertEquals(units.size(), 3, "Period.getUnits should return 3 units"); assertEquals(units.get(0), ChronoUnit.YEARS, "Period.getUnits contains ChronoUnit.YEARS"); assertEquals(units.get(1), ChronoUnit.MONTHS, "Period.getUnits contains ChronoUnit.MONTHS"); assertEquals(units.get(2), ChronoUnit.DAYS, "Period.getUnits contains ChronoUnit.DAYS"); }
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) { Instant i1 = Instant.ofEpochSecond(seconds1, nanos1); Instant i2 = Instant.ofEpochSecond(seconds2, nanos2); long amount = i1.until(i2, unit); assertEquals(amount, expected); }
private MockSimplePeriod(long amount, TemporalUnit unit) { Objects.requireNonNull(unit, "unit"); if (unit == FOREVER) { throw new DateTimeException("Cannot create a period of the Forever unit"); } this.amount = amount; this.unit = unit; }
@Test public void test_minus_longTemporalUnit_invalidUnit() { for (TemporalUnit unit : INVALID_UNITS) { try { TEST_12_30_40_987654321.minus(1, unit); fail("Unit should not be allowed " + unit); } catch (DateTimeException ex) { // expected } } }
/** * Checks if any of the supported units of this period are negative. * * @return true if any unit of this period is negative */ default boolean isNegative() { for (TemporalUnit unit : getUnits()) { if (get(unit) < 0) { return true; } } return false; }
@Override public Temporal subtractFrom(Temporal temporal) { for(Map.Entry<TemporalUnit, Long> entry : values.entrySet()) { temporal = temporal.minus(entry.getValue(), entry.getKey()); } return temporal; }
@Test(groups="{tck}") public void test_duration_getUnits() { Duration duration = Duration.ofSeconds(5000, 1000); List<TemporalUnit> units = duration.getUnits(); assertEquals(units.size(), 2, "Period.getUnits length"); assertTrue(units.contains(ChronoUnit.SECONDS), "Period.getUnits contains ChronoUnit.SECONDS"); assertTrue(units.contains(ChronoUnit.NANOS), "contains ChronoUnit.NANOS"); }
@Override public long until(Temporal endExclusive, TemporalUnit unit) { Objects.requireNonNull(endExclusive, "endExclusive"); @SuppressWarnings("unchecked") ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) getChronology().localDateTime(endExclusive); if (unit instanceof ChronoUnit) { if (unit.isTimeBased()) { long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY); switch ((ChronoUnit) unit) { case NANOS: amount = Math.multiplyExact(amount, NANOS_PER_DAY); break; case MICROS: amount = Math.multiplyExact(amount, MICROS_PER_DAY); break; case MILLIS: amount = Math.multiplyExact(amount, MILLIS_PER_DAY); break; case SECONDS: amount = Math.multiplyExact(amount, SECONDS_PER_DAY); break; case MINUTES: amount = Math.multiplyExact(amount, MINUTES_PER_DAY); break; case HOURS: amount = Math.multiplyExact(amount, HOURS_PER_DAY); break; case HALF_DAYS: amount = Math.multiplyExact(amount, 2); break; } return Math.addExact(amount, time.until(end.toLocalTime(), unit)); } ChronoLocalDate endDate = end.toLocalDate(); if (end.toLocalTime().isBefore(time)) { endDate = endDate.minus(1, ChronoUnit.DAYS); } return date.until(endDate, unit); } Objects.requireNonNull(unit, "unit"); return unit.between(this, end); }
@Test(dataProvider="plus_long_TemporalUnit") public void test_plus_long_TemporalUnit(YearMonth base, long amount, TemporalUnit unit, YearMonth expectedYearMonth, Class<?> expectedEx) { if (expectedEx == null) { assertEquals(base.plus(amount, unit), expectedYearMonth); } else { try { YearMonth result = base.plus(amount, unit); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Override public long until(Temporal endExclusive, TemporalUnit unit) { CopticDate end = getChronology().date(endExclusive); if (unit instanceof ChronoUnit) { return LocalDate.from(this).until(end, unit); // TODO: this is wrong } return unit.between(this, end); }
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit_between(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) { Instant i1 = Instant.ofEpochSecond(seconds1, nanos1); Instant i2 = Instant.ofEpochSecond(seconds2, nanos2); long amount = unit.between(i1, i2); assertEquals(amount, expected); }
/** * */ public static Instant truncatedTo(Date d, TemporalUnit unit) { return d.toInstant().truncatedTo(unit); }
@Override public long get(TemporalUnit unit) { throw new UnsupportedOperationException("Not supported yet."); }
public static Interval last(long amount, TemporalUnit unit) { return last(amount, unit, Clock.systemUTC()); }
@Override public HijrahDate minus(long amountToSubtract, TemporalUnit unit) { return super.minus(amountToSubtract, unit); }
public static Interval last(long amount, TemporalUnit unit, ZoneId zone) { return last(amount, unit, Clock.system(zone)); }
public Builder setExpires(int duration, TemporalUnit unit) { this.cookie.expires = ZonedDateTime.now(ZoneId.of("UTC")).plus(duration, unit); return this; }
public static Instant truncatedTo(Instant d, TemporalUnit unit) { return d.truncatedTo(unit); }
@Override public TemporalUnit getRangeUnit() { throw new UnsupportedOperationException("Not supported yet."); }
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit_between(LocalTime time1, LocalTime time2, TemporalUnit unit, long expected) { long amount = unit.between(time1, time2); assertEquals(amount, expected); }
@Test(expectedExceptions=NullPointerException.class) public void test_minus_longTemporalUnit_null() { TEST_2007_07_15_12_30_40_987654321.minus(1, (TemporalUnit) null); }
/** * Returns a copy of this time with the specified amount added. * <p> * This returns a {@code LocalTime}, based on this one, with the amount * in terms of the unit added. If it is not possible to add the amount, because the * unit is not supported or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoUnit} then the addition is implemented here. * The supported fields behave as follows: * <ul> * <li>{@code NANOS} - * Returns a {@code LocalTime} with the specified number of nanoseconds added. * This is equivalent to {@link #plusNanos(long)}. * <li>{@code MICROS} - * Returns a {@code LocalTime} with the specified number of microseconds added. * This is equivalent to {@link #plusNanos(long)} with the amount * multiplied by 1,000. * <li>{@code MILLIS} - * Returns a {@code LocalTime} with the specified number of milliseconds added. * This is equivalent to {@link #plusNanos(long)} with the amount * multiplied by 1,000,000. * <li>{@code SECONDS} - * Returns a {@code LocalTime} with the specified number of seconds added. * This is equivalent to {@link #plusSeconds(long)}. * <li>{@code MINUTES} - * Returns a {@code LocalTime} with the specified number of minutes added. * This is equivalent to {@link #plusMinutes(long)}. * <li>{@code HOURS} - * Returns a {@code LocalTime} with the specified number of hours added. * This is equivalent to {@link #plusHours(long)}. * <li>{@code HALF_DAYS} - * Returns a {@code LocalTime} with the specified number of half-days added. * This is equivalent to {@link #plusHours(long)} with the amount * multiplied by 12. * </ul> * <p> * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} * passing {@code this} as the argument. In this case, the unit determines * whether and how to perform the addition. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the unit to add to the result, may be negative * @param unit the unit of the amount to add, not null * @return a {@code LocalTime} based on this time with the specified amount added, not null * @throws DateTimeException if the addition cannot be made * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override public LocalTime plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); case MILLIS: return plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusMinutes(amountToAdd); case HOURS: return plusHours(amountToAdd); case HALF_DAYS: return plusHours((amountToAdd % 2) * 12); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }
public static TimePeriod ofUnits(Map<TemporalUnit, Long> values) { final ImmutableMap.Builder<TemporalUnit, Long> builder = ImmutableMap.builder(); values.keySet().stream().sorted(Collections.reverseOrder()) .forEach(unit -> builder.put(unit, values.get(unit))); return new TimePeriod(builder.build()); }
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit(LocalDate date1, LocalDate date2, TemporalUnit unit, long expected) { long amount = date1.until(date2, unit); assertEquals(amount, expected); }
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit_negated(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) { long amount = dt2.until(dt1, unit); assertEquals(amount, -expected); }
@Test(dataProvider="OfTemporalUnit") public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) { Duration t = Duration.of(amount, unit); assertEquals(t.getSeconds(), expectedSeconds); assertEquals(t.getNano(), expectedNanoOfSecond); }
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit(OffsetTime offsetTime1, OffsetTime offsetTime2, TemporalUnit unit, long expected) { long amount = offsetTime1.until(offsetTime2, unit); assertEquals(amount, expected); }
public static Instant plus(Instant time, TemporalUnit unit, int value) { return LocalDateTime.ofInstant(time, ZoneOffset.UTC).plus(value, unit).toInstant(ZoneOffset.UTC); }