Search in sources :

Example 1 with TimeUnitRounding

use of org.elasticsearch.common.rounding.Rounding.TimeUnitRounding in project elasticsearch by elastic.

the class TimeZoneRoundingTests method testRoundingRandom.

/**
     * Randomized test on TimeUnitRounding. Test uses random
     * {@link DateTimeUnit} and {@link DateTimeZone} and often (50% of the time)
     * chooses test dates that are exactly on or close to offset changes (e.g.
     * DST) in the chosen time zone.
     *
     * It rounds the test date down and up and performs various checks on the
     * rounding unit interval that is defined by this. Assumptions tested are
     * described in
     * {@link #assertInterval(long, long, long, Rounding, DateTimeZone)}
     */
public void testRoundingRandom() {
    for (int i = 0; i < 1000; ++i) {
        DateTimeUnit timeUnit = randomTimeUnit();
        DateTimeZone tz = randomDateTimeZone();
        Rounding rounding = new Rounding.TimeUnitRounding(timeUnit, tz);
        // 1970-01-01T00:00:00Z - 2033-05-18T05:33:20.000+02:00
        long date = Math.abs(randomLong() % (2 * (long) 10e11));
        long unitMillis = timeUnit.field(tz).getDurationField().getUnitMillis();
        if (randomBoolean()) {
            nastyDate(date, tz, unitMillis);
        }
        final long roundedDate = rounding.round(date);
        final long nextRoundingValue = rounding.nextRoundingValue(roundedDate);
        assertInterval(roundedDate, date, nextRoundingValue, rounding, tz);
        // check correct unit interval width for units smaller than a day, they should be fixed size except for transitions
        if (unitMillis <= DateTimeConstants.MILLIS_PER_DAY) {
            // if the interval defined didn't cross timezone offset transition, it should cover unitMillis width
            if (tz.getOffset(roundedDate - 1) == tz.getOffset(nextRoundingValue + 1)) {
                assertThat("unit interval width not as expected for [" + timeUnit + "], [" + tz + "] at " + new DateTime(roundedDate), nextRoundingValue - roundedDate, equalTo(unitMillis));
            }
        }
    }
}
Also used : TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime)

Aggregations

TimeIntervalRounding (org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding)1 TimeUnitRounding (org.elasticsearch.common.rounding.Rounding.TimeUnitRounding)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1