Search in sources :

Example 1 with TimeIntervalRounding

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

the class TimeZoneRoundingTests method testIntervalRoundingRandom.

/**
     * randomized test on {@link TimeIntervalRounding} with random interval and time zone offsets
     */
public void testIntervalRoundingRandom() {
    for (int i = 0; i < 1000; i++) {
        TimeUnit unit = randomFrom(new TimeUnit[] { TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS });
        long interval = unit.toMillis(randomIntBetween(1, 365));
        DateTimeZone tz = randomDateTimeZone();
        Rounding rounding = new Rounding.TimeIntervalRounding(interval, tz);
        // 1970-01-01T00:00:00Z - 2033-05-18T05:33:20.000+02:00
        long mainDate = Math.abs(randomLong() % (2 * (long) 10e11));
        if (randomBoolean()) {
            mainDate = nastyDate(mainDate, tz, interval);
        }
        // check two intervals around date
        long previousRoundedValue = Long.MIN_VALUE;
        for (long date = mainDate - 2 * interval; date < mainDate + 2 * interval; date += interval / 2) {
            try {
                final long roundedDate = rounding.round(date);
                final long nextRoundingValue = rounding.nextRoundingValue(roundedDate);
                assertThat("Rounding should be idempotent", roundedDate, equalTo(rounding.round(roundedDate)));
                assertThat("Rounded value smaller or equal than unrounded", roundedDate, lessThanOrEqualTo(date));
                assertThat("Values smaller than rounded value should round further down", rounding.round(roundedDate - 1), lessThan(roundedDate));
                assertThat("Rounding should be >= previous rounding value", roundedDate, greaterThanOrEqualTo(previousRoundedValue));
                if (tz.isFixed()) {
                    assertThat("NextRounding value should be greater than date", nextRoundingValue, greaterThan(roundedDate));
                    assertThat("NextRounding value should be interval from rounded value", nextRoundingValue - roundedDate, equalTo(interval));
                    assertThat("NextRounding value should be a rounded date", nextRoundingValue, equalTo(rounding.round(nextRoundingValue)));
                }
                previousRoundedValue = roundedDate;
            } catch (AssertionError e) {
                logger.error("Rounding error at {}, timezone {}, interval: {},", new DateTime(date, tz), tz, interval);
                throw e;
            }
        }
    }
}
Also used : TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) TimeUnit(java.util.concurrent.TimeUnit) TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime)

Example 2 with TimeIntervalRounding

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

the class TimeZoneRoundingTests method testTimeInterval_Kathmandu_DST_Start.

/**
     * test DST start with offset not fitting interval, e.g. Asia/Kathmandu
     * adding 15min on 1986-01-01T00:00:00 the interval from
     * 1986-01-01T00:15:00+05:45 to 1986-01-01T00:20:00+05:45 to only be 5min
     * long
     */
public void testTimeInterval_Kathmandu_DST_Start() {
    long interval = TimeUnit.MINUTES.toMillis(20);
    DateTimeZone tz = DateTimeZone.forID("Asia/Kathmandu");
    Rounding rounding = new TimeIntervalRounding(interval, tz);
    assertThat(rounding.round(time("1985-12-31T23:55:00+05:30")), isDate(time("1985-12-31T23:40:00+05:30"), tz));
    assertThat(rounding.round(time("1986-01-01T00:16:00+05:45")), isDate(time("1986-01-01T00:15:00+05:45"), tz));
    assertThat(time("1986-01-01T00:15:00+05:45") - time("1985-12-31T23:40:00+05:30"), equalTo(TimeUnit.MINUTES.toMillis(20)));
    assertThat(rounding.round(time("1986-01-01T00:26:00+05:45")), isDate(time("1986-01-01T00:20:00+05:45"), tz));
    assertThat(time("1986-01-01T00:20:00+05:45") - time("1986-01-01T00:15:00+05:45"), equalTo(TimeUnit.MINUTES.toMillis(5)));
    assertThat(rounding.round(time("1986-01-01T00:46:00+05:45")), isDate(time("1986-01-01T00:40:00+05:45"), tz));
    assertThat(time("1986-01-01T00:40:00+05:45") - time("1986-01-01T00:20:00+05:45"), equalTo(TimeUnit.MINUTES.toMillis(20)));
}
Also used : TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) DateTimeZone(org.joda.time.DateTimeZone)

Example 3 with TimeIntervalRounding

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

the class TimeZoneRoundingTests method testTimeIntervalRounding.

/**
     * test TimeIntervalRounding, (interval &lt; 12h) with time zone shift
     */
public void testTimeIntervalRounding() {
    DateTimeZone tz = DateTimeZone.forOffsetHours(-1);
    Rounding tzRounding = Rounding.builder(TimeValue.timeValueHours(6)).timeZone(tz).build();
    assertThat(tzRounding.round(time("2009-02-03T00:01:01")), isDate(time("2009-02-02T19:00:00.000Z"), tz));
    assertThat(tzRounding.nextRoundingValue(time("2009-02-02T19:00:00.000Z")), isDate(time("2009-02-03T01:00:00.000Z"), tz));
    assertThat(tzRounding.round(time("2009-02-03T13:01:01")), isDate(time("2009-02-03T13:00:00.000Z"), tz));
    assertThat(tzRounding.nextRoundingValue(time("2009-02-03T13:00:00.000Z")), isDate(time("2009-02-03T19:00:00.000Z"), tz));
}
Also used : TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) DateTimeZone(org.joda.time.DateTimeZone)

Example 4 with TimeIntervalRounding

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

the class TimeZoneRoundingTests method testTimeIntervalCET_DST_End.

/**
     * test DST end with interval rounding
     * CET: 25 October 2015, 03:00:00 clocks were turned backward 1 hour to 25 October 2015, 02:00:00 local standard time
     */
public void testTimeIntervalCET_DST_End() {
    long interval = TimeUnit.MINUTES.toMillis(20);
    DateTimeZone tz = DateTimeZone.forID("CET");
    Rounding rounding = new TimeIntervalRounding(interval, tz);
    assertThat(rounding.round(time("2015-10-25T01:55:00+02:00")), isDate(time("2015-10-25T01:40:00+02:00"), tz));
    assertThat(rounding.round(time("2015-10-25T02:15:00+02:00")), isDate(time("2015-10-25T02:00:00+02:00"), tz));
    assertThat(rounding.round(time("2015-10-25T02:35:00+02:00")), isDate(time("2015-10-25T02:20:00+02:00"), tz));
    assertThat(rounding.round(time("2015-10-25T02:55:00+02:00")), isDate(time("2015-10-25T02:40:00+02:00"), tz));
    // after DST shift
    assertThat(rounding.round(time("2015-10-25T02:15:00+01:00")), isDate(time("2015-10-25T02:00:00+01:00"), tz));
    assertThat(rounding.round(time("2015-10-25T02:35:00+01:00")), isDate(time("2015-10-25T02:20:00+01:00"), tz));
    assertThat(rounding.round(time("2015-10-25T02:55:00+01:00")), isDate(time("2015-10-25T02:40:00+01:00"), tz));
    assertThat(rounding.round(time("2015-10-25T03:15:00+01:00")), isDate(time("2015-10-25T03:00:00+01:00"), tz));
}
Also used : TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) DateTimeZone(org.joda.time.DateTimeZone)

Example 5 with TimeIntervalRounding

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

the class TimeZoneRoundingTests method testTimeIntervalCET_DST_Start.

/**
     * test DST start with interval rounding
     * CET: 27 March 2016, 02:00:00 clocks were turned forward 1 hour to 27 March 2016, 03:00:00 local daylight time
     */
public void testTimeIntervalCET_DST_Start() {
    long interval = TimeUnit.MINUTES.toMillis(20);
    DateTimeZone tz = DateTimeZone.forID("CET");
    Rounding rounding = new TimeIntervalRounding(interval, tz);
    // test DST start
    assertThat(rounding.round(time("2016-03-27T01:55:00+01:00")), isDate(time("2016-03-27T01:40:00+01:00"), tz));
    assertThat(rounding.round(time("2016-03-27T02:00:00+01:00")), isDate(time("2016-03-27T03:00:00+02:00"), tz));
    assertThat(rounding.round(time("2016-03-27T03:15:00+02:00")), isDate(time("2016-03-27T03:00:00+02:00"), tz));
    assertThat(rounding.round(time("2016-03-27T03:35:00+02:00")), isDate(time("2016-03-27T03:20:00+02:00"), tz));
}
Also used : TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) DateTimeZone(org.joda.time.DateTimeZone)

Aggregations

TimeIntervalRounding (org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding)5 TimeUnitRounding (org.elasticsearch.common.rounding.Rounding.TimeUnitRounding)5 DateTimeZone (org.joda.time.DateTimeZone)5 TimeUnit (java.util.concurrent.TimeUnit)1 DateTime (org.joda.time.DateTime)1