Search in sources :

Example 31 with DateTimeZone

use of org.joda.time.DateTimeZone in project elasticsearch by elastic.

the class TimeZoneRoundingTests method testTimeUnitRoundingDST.

public void testTimeUnitRoundingDST() {
    Rounding tzRounding;
    // testing savings to non savings switch
    DateTimeZone cet = DateTimeZone.forID("CET");
    tzRounding = Rounding.builder(DateTimeUnit.HOUR_OF_DAY).timeZone(cet).build();
    assertThat(tzRounding.round(time("2014-10-26T01:01:01", cet)), isDate(time("2014-10-26T01:00:00+02:00"), cet));
    assertThat(tzRounding.nextRoundingValue(time("2014-10-26T01:00:00", cet)), isDate(time("2014-10-26T02:00:00+02:00"), cet));
    assertThat(tzRounding.nextRoundingValue(time("2014-10-26T02:00:00", cet)), isDate(time("2014-10-26T02:00:00+01:00"), cet));
    // testing non savings to savings switch
    tzRounding = Rounding.builder(DateTimeUnit.HOUR_OF_DAY).timeZone(cet).build();
    assertThat(tzRounding.round(time("2014-03-30T01:01:01", cet)), isDate(time("2014-03-30T01:00:00+01:00"), cet));
    assertThat(tzRounding.nextRoundingValue(time("2014-03-30T01:00:00", cet)), isDate(time("2014-03-30T03:00:00", cet), cet));
    assertThat(tzRounding.nextRoundingValue(time("2014-03-30T03:00:00", cet)), isDate(time("2014-03-30T04:00:00", cet), cet));
    // testing non savings to savings switch (America/Chicago)
    DateTimeZone chg = DateTimeZone.forID("America/Chicago");
    Rounding tzRounding_utc = Rounding.builder(DateTimeUnit.HOUR_OF_DAY).timeZone(DateTimeZone.UTC).build();
    assertThat(tzRounding.round(time("2014-03-09T03:01:01", chg)), isDate(time("2014-03-09T03:00:00", chg), chg));
    Rounding tzRounding_chg = Rounding.builder(DateTimeUnit.HOUR_OF_DAY).timeZone(chg).build();
    assertThat(tzRounding_chg.round(time("2014-03-09T03:01:01", chg)), isDate(time("2014-03-09T03:00:00", chg), chg));
    // testing savings to non savings switch 2013 (America/Chicago)
    assertThat(tzRounding_utc.round(time("2013-11-03T06:01:01", chg)), isDate(time("2013-11-03T06:00:00", chg), chg));
    assertThat(tzRounding_chg.round(time("2013-11-03T06:01:01", chg)), isDate(time("2013-11-03T06:00:00", chg), chg));
    // testing savings to non savings switch 2014 (America/Chicago)
    assertThat(tzRounding_utc.round(time("2014-11-02T06:01:01", chg)), isDate(time("2014-11-02T06:00:00", chg), chg));
    assertThat(tzRounding_chg.round(time("2014-11-02T06:01:01", chg)), isDate(time("2014-11-02T06:00:00", chg), chg));
}
Also used : TimeUnitRounding(org.elasticsearch.common.rounding.Rounding.TimeUnitRounding) TimeIntervalRounding(org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding) DateTimeZone(org.joda.time.DateTimeZone)

Example 32 with DateTimeZone

use of org.joda.time.DateTimeZone in project elasticsearch by elastic.

the class TimeZoneRoundingTests method testIntervalRounding_HalfDay_DST.

/**
     * Test for half day rounding intervals scrossing DST.
     */
public void testIntervalRounding_HalfDay_DST() {
    DateTimeZone tz = DateTimeZone.forID("CET");
    long interval = TimeUnit.HOURS.toMillis(12);
    Rounding rounding = new Rounding.TimeIntervalRounding(interval, tz);
    assertThat(rounding.round(time("2016-03-26T01:00:00+01:00")), isDate(time("2016-03-26T00:00:00+01:00"), tz));
    assertThat(rounding.round(time("2016-03-26T13:00:00+01:00")), isDate(time("2016-03-26T12:00:00+01:00"), tz));
    assertThat(rounding.round(time("2016-03-27T01:00:00+01:00")), isDate(time("2016-03-27T00:00:00+01:00"), tz));
    assertThat(rounding.round(time("2016-03-27T13:00:00+02:00")), isDate(time("2016-03-27T12:00:00+02:00"), tz));
    assertThat(rounding.round(time("2016-03-28T01:00:00+02:00")), isDate(time("2016-03-28T00:00:00+02:00"), tz));
    assertThat(rounding.round(time("2016-03-28T13:00:00+02:00")), isDate(time("2016-03-28T12:00: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)

Example 33 with DateTimeZone

use of org.joda.time.DateTimeZone in project elasticsearch by elastic.

the class TimeZoneRoundingTests method testUTCIntervalRounding.

public void testUTCIntervalRounding() {
    Rounding tzRounding = Rounding.builder(TimeValue.timeValueHours(12)).build();
    DateTimeZone tz = DateTimeZone.UTC;
    assertThat(tzRounding.round(time("2009-02-03T01:01:01")), isDate(time("2009-02-03T00:00:00.000Z"), tz));
    assertThat(tzRounding.nextRoundingValue(time("2009-02-03T00:00:00.000Z")), isDate(time("2009-02-03T12:00:00.000Z"), tz));
    assertThat(tzRounding.round(time("2009-02-03T13:01:01")), isDate(time("2009-02-03T12:00:00.000Z"), tz));
    assertThat(tzRounding.nextRoundingValue(time("2009-02-03T12:00:00.000Z")), isDate(time("2009-02-04T00:00:00.000Z"), tz));
    tzRounding = Rounding.builder(TimeValue.timeValueHours(48)).build();
    assertThat(tzRounding.round(time("2009-02-03T01:01:01")), isDate(time("2009-02-03T00:00:00.000Z"), tz));
    assertThat(tzRounding.nextRoundingValue(time("2009-02-03T00:00:00.000Z")), isDate(time("2009-02-05T00:00:00.000Z"), tz));
    assertThat(tzRounding.round(time("2009-02-05T13:01:01")), isDate(time("2009-02-05T00:00:00.000Z"), tz));
    assertThat(tzRounding.nextRoundingValue(time("2009-02-05T00:00:00.000Z")), isDate(time("2009-02-07T00: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 34 with DateTimeZone

use of org.joda.time.DateTimeZone in project elasticsearch by elastic.

the class TimeZoneRoundingTests method testTimeIntervalRounding.

/**
     * test TimeIntervalRounding, (interval < 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 35 with DateTimeZone

use of org.joda.time.DateTimeZone 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

DateTimeZone (org.joda.time.DateTimeZone)263 DateTime (org.joda.time.DateTime)123 ArrayList (java.util.ArrayList)36 Test (org.testng.annotations.Test)35 LocalDate (org.joda.time.LocalDate)27 Test (org.junit.Test)21 TimeIntervalRounding (org.elasticsearch.common.rounding.Rounding.TimeIntervalRounding)18 TimeUnitRounding (org.elasticsearch.common.rounding.Rounding.TimeUnitRounding)18 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)17 IOException (java.io.IOException)16 HashMap (java.util.HashMap)15 UUID (java.util.UUID)14 TimeFormat (org.gephi.graph.api.TimeFormat)13 MetricExpression (com.linkedin.thirdeye.client.MetricExpression)12 Date (java.util.Date)12 Chronology (org.joda.time.Chronology)12 LocalDateTime (org.joda.time.LocalDateTime)12 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)11 LocalTime (org.joda.time.LocalTime)10 SubscriptionBaseTransition (org.killbill.billing.subscription.api.user.SubscriptionBaseTransition)10