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 ); }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased(); } 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; }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased() || unit.isTimeBased(); } return unit != null && unit.isSupportedBy(this); }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased() || unit.isTimeBased(); } return unit != null && unit.isSupportedBy(this); }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased() || unit.isTimeBased(); } return unit != null && unit.isSupportedBy(this); }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased() || unit.isTimeBased(); } return unit != null && unit.isSupportedBy(this); }
@Override public boolean isSupported(TemporalUnit unit) { if (unit instanceof ChronoUnit) { return unit.isDateBased() || unit.isTimeBased(); } 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); }
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; }
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; }
if (!o1.getClass().equals(o2.getClass())) { boolean o1datebased = o1.getUnits().stream().anyMatch(u -> u.isDateBased()); boolean o2datebased = o2.getUnits().stream().anyMatch(u -> u.isDateBased()); if (o1datebased != o2datebased) { if (o1datebased) {
if (!o1.getClass().equals(o2.getClass())) { boolean o1datebased = o1.getUnits().stream().anyMatch(u -> u.isDateBased()); boolean o2datebased = o2.getUnits().stream().anyMatch(u -> u.isDateBased()); if (o1datebased != o2datebased) { if (o1datebased) {
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 ); }
public ZonedDateTime plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { if (unit.isDateBased()) { return resolveLocal(dateTime.plus(amountToAdd, unit)); } else {
if (unit instanceof ChronoUnit) { end = end.withZoneSameInstant(zone); if (unit.isDateBased()) { return dateTime.until(end.dateTime, unit); } else {