return packedTime; Duration unitDur = unit.getDuration(); if (unitDur.getSeconds() > SECONDS_PER_DAY) { throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
static Pair<LocalDate,LocalTime> getTruncatedDateAndTime( TemporalUnit unit, TemporalValue input, String type ) { if ( unit.isTimeBased() && !(input instanceof DateTimeValue || input instanceof LocalDateTimeValue) ) { throw new UnsupportedTemporalUnitException( String.format( "Cannot truncate %s to %s with a time based unit.", input, type ) ); } LocalDate localDate = input.getDatePart(); LocalTime localTime = input.hasTime() ? input.getLocalTimePart() : LocalTimeValue.DEFAULT_LOCAL_TIME; LocalTime truncatedTime; LocalDate truncatedDate; if ( unit.isDateBased() ) { truncatedDate = DateValue.truncateTo( localDate, unit ); truncatedTime = LocalTimeValue.DEFAULT_LOCAL_TIME; } else { truncatedDate = localDate; truncatedTime = localTime.truncatedTo( unit ); } return Pair.of( truncatedDate, truncatedTime ); }
/** * Returns absolute value of the difference according to time unit. * * @param temporal1 the first {@link Temporal} * @param temporal2 the second {@link Temporal} * @return absolute value of the difference according to time unit. */ protected long getDifference(Temporal temporal1, Temporal temporal2) { return abs(unit.between(temporal1, temporal2)); }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased() || unit.isTimeBased(); } return unit != null && unit.isSupportedBy(this); }
private static long getNext(long time, TemporalUnit unit, long multiplier, ZoneId zoneId) { final long end; try { if (!unit.isDateBased()) end = time + unit.getDuration().multipliedBy(multiplier).toMillis(); else end = ZonedDateTime.ofInstant(Instant.ofEpochMilli(time), zoneId).plus(multiplier, unit).toInstant().toEpochMilli(); } catch (ArithmeticException e) { return multiplier > 0 ? Long.MAX_VALUE : Long.MIN_VALUE; } if (multiplier > 0 && end < time) return Long.MAX_VALUE; else if (multiplier < 0 && end > time) return Long.MIN_VALUE; return end; }
static String dateTimeColumnName(Column<LocalDateTime> column1, long value, TemporalUnit unit) { return column1.name() + ": " + value + " " + unit.toString() + "(s)"; } }
public boolean isSupported(final TemporalUnit unit) { if (unit instanceof CalendarDateUnit) { return true; } else if (unit instanceof ChronoUnit) { return unit.isTimeBased() || (unit == ChronoUnit.DAYS); } else if (unit != null) { return unit.isSupportedBy(this); } else { return false; } }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased(); } return unit != null && unit.isSupportedBy(this); }
static final Instant getCurrentStart(final Instant instant0, final TemporalUnit unit) { if (unit.isDateBased()) { final ZonedDateTime zdt = ZonedDateTime.ofInstant(instant0, zone).truncatedTo(ChronoUnit.DAYS); if (unit.equals(ChronoUnit.DAYS)) return zdt.toInstant(); if (unit.equals(ChronoUnit.WEEKS)) return zdt.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).toInstant(); if (unit.equals(ChronoUnit.YEARS)) return zdt.with(TemporalAdjusters.firstDayOfYear()).toInstant(); if (unit.equals(ChronoUnit.MONTHS)) return zdt.with(TemporalAdjusters.firstDayOfMonth()).toInstant(); else throw new IllegalArgumentException("Invalid temporal unit " + unit); } else return instant0.truncatedTo(unit); }
if (unit instanceof ChronoUnit) { end = end.withZoneSameInstant(zone); if (unit.isDateBased()) { return dateTime.until(end.dateTime, unit); } else { return unit.between(this, end);
public Temporal plus(final long amountToAdd, final TemporalUnit unit) { if (unit instanceof CalendarDateUnit) { return this.step((int) amountToAdd, (CalendarDateUnit) unit); } else if (unit instanceof ChronoUnit) { return this.toInstant().plus(amountToAdd, unit); } else { return unit.addTo(this, amountToAdd); } }
public ZonedDateTime plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { if (unit.isDateBased()) { return resolveLocal(dateTime.plus(amountToAdd, unit)); } else { return unit.addTo(this, amountToAdd);
return plus(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY), 0); if (unit.isDurationEstimated()) { throw new DateTimeException("Unit must not have an estimated duration"); case SECONDS: return plusSeconds(amountToAdd); return plusSeconds(Jdk8Methods.safeMultiply(unit.getDuration().seconds, amountToAdd)); Duration duration = unit.getDuration().multipliedBy(amountToAdd); return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit == MONTHS || unit == YEARS || unit == DECADES || unit == CENTURIES || unit == MILLENNIA || unit == ERAS; } return unit != null && unit.isSupportedBy(this); }
if (value != 0) { if (unit.isDurationEstimated()) { if (unit == ChronoUnit.DAYS) { days = Math.addExact(days, Math.toIntExact(value));
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased() || unit.isTimeBased(); } return unit != null && unit.isSupportedBy(this); }
private final static long getAlignedIntervalStartTime(ReadOnlyTimeSeries timeSeries, long startTime, long samplingInterval, final ZoneId timeZone) { final Instant t0; if (timeSeries != null) { final SampledValue sv = timeSeries.getNextValue(startTime); if (sv == null) return startTime; t0 = Instant.ofEpochMilli(sv.getTimestamp()); } else { t0 = Instant.ofEpochMilli(startTime); } for (TemporalUnit unit :units) { if (samplingInterval == unit.getDuration().toMillis()) { if (!unit.isDateBased()) return t0.truncatedTo(unit).toEpochMilli(); else { final ZonedDateTime day = ZonedDateTime.ofInstant(t0, timeZone).truncatedTo(ChronoUnit.DAYS); final ZonedDateTime truncated; if (unit == ChronoUnit.WEEKS) truncated = day.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); else if (unit == ChronoUnit.MONTHS) truncated = day.with(TemporalAdjusters.firstDayOfMonth()); else truncated = day.with(TemporalAdjusters.firstDayOfYear()); return truncated.toInstant().toEpochMilli(); } } } return startTime; }
static String dateColumnName(Column<LocalDate> column1, int value, TemporalUnit unit) { return column1.name() + ": " + value + " " + unit.toString() + "(s)"; }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isTimeBased(); } return unit != null && unit.isSupportedBy(this); }
private static long getLastStandardIntervalStart(final long start, final TemporalUnit unit, final ZoneId zoneId) { final long last; try { if (unit.isDateBased()) { ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(start), zoneId).truncatedTo(ChronoUnit.DAYS); if (unit == ChronoUnit.MONTHS) zdt = zdt.with(TemporalAdjusters.firstDayOfMonth()); else if (unit == ChronoUnit.YEARS) zdt = zdt.with(TemporalAdjusters.firstDayOfYear()); else if (unit == ChronoUnit.WEEKS) zdt = zdt.with(TemporalAdjusters.previous(DayOfWeek.MONDAY)); last = zdt.toInstant().toEpochMilli(); } else { last = Instant.ofEpochMilli(start).truncatedTo(unit).toEpochMilli(); } } catch (ArithmeticException e) { return Long.MIN_VALUE; } return last; }