@Test @Ignore("does not handle daylight savings / TZ stuff, and is hard coded in the impl") public void testTruncateInterval() { DateTime end = new DateTime(); Interval interval; for (DTimeUnit per : types) { switch (per) { case ALL_TIME: break; default: PeriodType type = per.toPeriodType(); DurationFieldType fieldType = type.getFieldType(0); interval = new Interval(Dates.period(0, type), end); interval = Dates.truncateInterval(interval, 10, type); assertEquals(interval.toString(), 0, interval.toPeriod(type).get(fieldType)); interval = new Interval(Dates.period(5, type), end); interval = Dates.truncateInterval(interval, 10, type); assertEquals(interval.toString(), 5, interval.toPeriod(type).get(fieldType)); interval = new Interval(Dates.period(10, type), end); interval = Dates.truncateInterval(interval, 10, type); assertEquals(interval.toString() + ',' + type.getName(), 9, interval.toPeriod(type).get(fieldType)); interval = new Interval(Dates.period(100, type), end); interval = Dates.truncateInterval(interval, 10, null); assertEquals(interval.toString(), 100, interval.toPeriod(type).get(fieldType)); interval = new Interval(Dates.period(11, type), (new DateTime()).plus(Dates.period(10, type))); interval = Dates.truncateInterval(interval, 5, type); assertFalse(interval.getEnd().isAfterNow()); assertEquals(interval.toString(), 1, interval.toPeriod(type).get(fieldType)); interval = new Interval(Dates.period(20, type), (new DateTime()).plus(Dates.period(10, type))); interval = Dates.truncateInterval(interval, 5, type); assertFalse(interval.getEnd().isAfterNow()); assertEquals(interval.toString(), 4, interval.toPeriod(type).get(fieldType)); interval = new Interval((new DateTime()).plus(Dates.period(5, type)), (new DateTime()).plus(Dates.period(10, type))); interval = Dates.truncateInterval(interval, 3, type); assertFalse(interval.getEnd().isAfterNow()); assertFalse(interval.getEnd().isBefore(end)); assertEquals(interval.getStart(), interval.getEnd()); } } }
public static Interval truncateInterval(Interval interval, int limit, PeriodType type) { Interval truncatedInterval = interval; if (type != null) { DateTime now = new DateTime(); if (interval.getEnd().isAfter(now)) { if (interval.getStart().isAfter(now)) { truncatedInterval = new Interval(now, now); } else { truncatedInterval = interval.withEnd(now); } } if (truncatedInterval.toPeriod(type).getValue(0) > --limit) { Period limitPeriod = period(limit, type); DateTime truncatedStart = truncatedInterval.getEnd().minus(limitPeriod); truncatedInterval = truncatedInterval.withStart(truncatedStart); } } return truncatedInterval; }
Dates { public static Interval truncateInterval(Interval interval, int limit, PeriodType type) { Interval truncatedInterval = interval; if (type != null) { DateTime now = new DateTime(); if (interval.getEnd().isAfter(now)) { if (interval.getStart().isAfter(now)) { truncatedInterval = new Interval(now, now); } else { truncatedInterval = interval.withEnd(now); } } if (truncatedInterval.toPeriod(type).getValue(0) > --limit) { Period limitPeriod = period(limit, type); DateTime truncatedStart = truncatedInterval.getEnd().minus(limitPeriod); truncatedInterval = truncatedInterval.withStart(truncatedStart); } } return truncatedInterval; } }
Dates { public static Interval truncateInterval(Interval interval, int limit, PeriodType type) { Interval truncatedInterval = interval; if (type != null) { DateTime now = new DateTime(); if (interval.getEnd().isAfter(now)) { if (interval.getStart().isAfter(now)) { truncatedInterval = new Interval(now, now); } else { truncatedInterval = interval.withEnd(now); } } if (truncatedInterval.toPeriod(type).getValue(0) > --limit) { Period limitPeriod = period(limit, type); DateTime truncatedStart = truncatedInterval.getEnd().minus(limitPeriod); truncatedInterval = truncatedInterval.withStart(truncatedStart); } } return truncatedInterval; }  }
Dates { public static Interval truncateInterval(Interval interval, int limit, PeriodType type) { Interval truncatedInterval = interval; if (type != null) { DateTime now = new DateTime(); if (interval.getEnd().isAfter(now)) { if (interval.getStart().isAfter(now)) { truncatedInterval = new Interval(now, now); } else { truncatedInterval = interval.withEnd(now); } } if (truncatedInterval.toPeriod(type).getValue(0) > --limit) { Period limitPeriod = period(limit, type); DateTime truncatedStart = truncatedInterval.getEnd().minus(limitPeriod); truncatedInterval = truncatedInterval.withStart(truncatedStart); } } return truncatedInterval; }  static Period period(int length, PeriodType type); static Interval truncateInterval(Interval interval, int limit, PeriodType type); static Interval parseInterval(String begStr, String endStr, DateTimeFormatter format); static Interval parseInterval(String begStr, String endStr); static Date endOfDay(Date d); static Date begOfDay(Date d); static Iterable<DateTime> iterableInterval(Interval interval, DTimeUnit type); static Iterator<DateTime> intervalIterator(Interval interval, DTimeUnit type); }
Dates { public static Interval truncateInterval(Interval interval, int limit, PeriodType type) { Interval truncatedInterval = interval; if (type != null) { DateTime now = new DateTime(); if (interval.getEnd().isAfter(now)) { if (interval.getStart().isAfter(now)) { truncatedInterval = new Interval(now, now); } else { truncatedInterval = interval.withEnd(now); } } if (truncatedInterval.toPeriod(type).getValue(0) > --limit) { Period limitPeriod = period(limit, type); DateTime truncatedStart = truncatedInterval.getEnd().minus(limitPeriod); truncatedInterval = truncatedInterval.withStart(truncatedStart); } } return truncatedInterval; }  static Period period(int length, PeriodType type); static Interval truncateInterval(Interval interval, int limit, PeriodType type); static Interval parseInterval(String begStr, String endStr, DateTimeFormatter format); static Interval parseInterval(String begStr, String endStr); static Date endOfDay(Date d); static Date begOfDay(Date d); static Iterable<DateTime> iterableInterval(Interval interval, DTimeUnit type); static Iterator<DateTime> intervalIterator(Interval interval, DTimeUnit type); static final DateTimeFormatter rfc1123; static final DateTimeFormatter yMdFormat; static final DateTimeFormatter yMdHFormat; }