Search in sources :

Example 1 with DateTimeField

use of org.joda.time.DateTimeField in project graylog2-server by Graylog2.

the class TimeBasedRotationStrategy method determineRotationPeriodAnchor.

/**
     * Determines the starting point ("anchor") for a period.
     *
     * To produce repeatable rotation points in time, the period is "snapped" to a "grid" of time.
     * For example, an hourly index rotation would be anchored to the last full hour, instead of happening at whatever minute
     * the first rotation was started.
     *
     * This "snapping" is done accordingly with the other parts of a period.
     *
     * For highly irregular periods (those that do not have a small zero component)
     *
     * @param period the rotation period
     * @return the anchor DateTime to calculate rotation periods from
     */
static DateTime determineRotationPeriodAnchor(@Nullable DateTime lastAnchor, Period period) {
    final Period normalized = period.normalizedStandard();
    int years = normalized.getYears();
    int months = normalized.getMonths();
    int weeks = normalized.getWeeks();
    int days = normalized.getDays();
    int hours = normalized.getHours();
    int minutes = normalized.getMinutes();
    int seconds = normalized.getSeconds();
    if (years == 0 && months == 0 && weeks == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        throw new IllegalArgumentException("Invalid rotation period specified");
    }
    // find the largest non-zero stride in the period. that's our anchor type. statement order matters here!
    DateTimeFieldType largestStrideType = null;
    if (seconds > 0)
        largestStrideType = secondOfMinute();
    if (minutes > 0)
        largestStrideType = minuteOfHour();
    if (hours > 0)
        largestStrideType = hourOfDay();
    if (days > 0)
        largestStrideType = dayOfMonth();
    if (weeks > 0)
        largestStrideType = weekOfWeekyear();
    if (months > 0)
        largestStrideType = monthOfYear();
    if (years > 0)
        largestStrideType = year();
    if (largestStrideType == null) {
        throw new IllegalArgumentException("Could not determine rotation stride length.");
    }
    final DateTime anchorTime = MoreObjects.firstNonNull(lastAnchor, Tools.nowUTC());
    final DateTimeField field = largestStrideType.getField(anchorTime.getChronology());
    // use normalized here to make sure we actually have the largestStride type available! see https://github.com/Graylog2/graylog2-server/issues/836
    int periodValue = normalized.get(largestStrideType.getDurationType());
    final long fieldValue = field.roundFloor(anchorTime.getMillis());
    final int fieldValueInUnit = field.get(fieldValue);
    if (periodValue == 0) {
        // https://github.com/Graylog2/graylog2-server/issues/836
        log.warn("Determining stride length failed because of a 0 period. Defaulting back to 1 period to avoid crashing, but this is a bug!");
        periodValue = 1;
    }
    final long difference = fieldValueInUnit % periodValue;
    final long newValue = field.add(fieldValue, -1 * difference);
    return new DateTime(newValue, DateTimeZone.UTC);
}
Also used : DateTimeFieldType(org.joda.time.DateTimeFieldType) Period(org.joda.time.Period) DateTimeField(org.joda.time.DateTimeField) DateTime(org.joda.time.DateTime)

Example 2 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class TestBuddhistChronology method testCalendar.

public void testCalendar() {
    if (TestAll.FAST) {
        return;
    }
    System.out.println("\nTestBuddhistChronology.testCalendar");
    DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, BUDDHIST_UTC);
    long millis = epoch.getMillis();
    long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
    DateTimeField dayOfWeek = BUDDHIST_UTC.dayOfWeek();
    DateTimeField weekOfWeekyear = GJ_UTC.weekOfWeekyear();
    DateTimeField dayOfYear = BUDDHIST_UTC.dayOfYear();
    DateTimeField dayOfMonth = BUDDHIST_UTC.dayOfMonth();
    DateTimeField monthOfYear = BUDDHIST_UTC.monthOfYear();
    DateTimeField year = BUDDHIST_UTC.year();
    DateTimeField yearOfEra = BUDDHIST_UTC.yearOfEra();
    DateTimeField era = BUDDHIST_UTC.era();
    DateTimeField gjDayOfWeek = GJ_UTC.dayOfWeek();
    DateTimeField gjWeekOfWeekyear = GJ_UTC.weekOfWeekyear();
    DateTimeField gjDayOfYear = GJ_UTC.dayOfYear();
    DateTimeField gjDayOfMonth = GJ_UTC.dayOfMonth();
    DateTimeField gjMonthOfYear = GJ_UTC.monthOfYear();
    DateTimeField gjYear = GJ_UTC.year();
    while (millis < end) {
        assertEquals(gjDayOfWeek.get(millis), dayOfWeek.get(millis));
        assertEquals(gjDayOfYear.get(millis), dayOfYear.get(millis));
        assertEquals(gjDayOfMonth.get(millis), dayOfMonth.get(millis));
        assertEquals(gjMonthOfYear.get(millis), monthOfYear.get(millis));
        assertEquals(gjWeekOfWeekyear.get(millis), weekOfWeekyear.get(millis));
        assertEquals(1, era.get(millis));
        int yearValue = gjYear.get(millis);
        if (yearValue <= 0) {
            yearValue++;
        }
        yearValue += 543;
        assertEquals(yearValue, year.get(millis));
        assertEquals(yearValue, yearOfEra.get(millis));
        millis += SKIP;
    }
}
Also used : DateTimeField(org.joda.time.DateTimeField) DateTime(org.joda.time.DateTime)

Example 3 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class TestCopticChronology method testCalendar.

//-----------------------------------------------------------------------
/**
     * Tests era, year, monthOfYear, dayOfMonth and dayOfWeek.
     */
public void testCalendar() {
    if (TestAll.FAST) {
        return;
    }
    System.out.println("\nTestCopticChronology.testCalendar");
    DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, COPTIC_UTC);
    long millis = epoch.getMillis();
    long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
    DateTimeField dayOfWeek = COPTIC_UTC.dayOfWeek();
    DateTimeField dayOfYear = COPTIC_UTC.dayOfYear();
    DateTimeField dayOfMonth = COPTIC_UTC.dayOfMonth();
    DateTimeField monthOfYear = COPTIC_UTC.monthOfYear();
    DateTimeField year = COPTIC_UTC.year();
    DateTimeField yearOfEra = COPTIC_UTC.yearOfEra();
    DateTimeField era = COPTIC_UTC.era();
    int expectedDOW = new DateTime(284, 8, 29, 0, 0, 0, 0, JULIAN_UTC).getDayOfWeek();
    int expectedDOY = 1;
    int expectedDay = 1;
    int expectedMonth = 1;
    int expectedYear = 1;
    while (millis < end) {
        int dowValue = dayOfWeek.get(millis);
        int doyValue = dayOfYear.get(millis);
        int dayValue = dayOfMonth.get(millis);
        int monthValue = monthOfYear.get(millis);
        int yearValue = year.get(millis);
        int yearOfEraValue = yearOfEra.get(millis);
        int monthLen = dayOfMonth.getMaximumValue(millis);
        if (monthValue < 1 || monthValue > 13) {
            fail("Bad month: " + millis);
        }
        // test era
        assertEquals(1, era.get(millis));
        assertEquals("AM", era.getAsText(millis));
        assertEquals("AM", era.getAsShortText(millis));
        // test date
        assertEquals(expectedYear, yearValue);
        assertEquals(expectedYear, yearOfEraValue);
        assertEquals(expectedMonth, monthValue);
        assertEquals(expectedDay, dayValue);
        assertEquals(expectedDOW, dowValue);
        assertEquals(expectedDOY, doyValue);
        // test leap year
        assertEquals(yearValue % 4 == 3, year.isLeap(millis));
        // test month length
        if (monthValue == 13) {
            assertEquals(yearValue % 4 == 3, monthOfYear.isLeap(millis));
            if (yearValue % 4 == 3) {
                assertEquals(6, monthLen);
            } else {
                assertEquals(5, monthLen);
            }
        } else {
            assertEquals(30, monthLen);
        }
        // recalculate date
        expectedDOW = (((expectedDOW + 1) - 1) % 7) + 1;
        expectedDay++;
        expectedDOY++;
        if (expectedDay == 31 && expectedMonth < 13) {
            expectedDay = 1;
            expectedMonth++;
        } else if (expectedMonth == 13) {
            if (expectedYear % 4 == 3 && expectedDay == 7) {
                expectedDay = 1;
                expectedMonth = 1;
                expectedYear++;
                expectedDOY = 1;
            } else if (expectedYear % 4 != 3 && expectedDay == 6) {
                expectedDay = 1;
                expectedMonth = 1;
                expectedYear++;
                expectedDOY = 1;
            }
        }
        millis += SKIP;
    }
}
Also used : DateTimeField(org.joda.time.DateTimeField) DateTime(org.joda.time.DateTime)

Example 4 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class TestEthiopicChronology method testCalendar.

//-----------------------------------------------------------------------
/**
     * Tests era, year, monthOfYear, dayOfMonth and dayOfWeek.
     */
public void testCalendar() {
    if (TestAll.FAST) {
        return;
    }
    System.out.println("\nTestEthiopicChronology.testCalendar");
    DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ETHIOPIC_UTC);
    long millis = epoch.getMillis();
    long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
    DateTimeField dayOfWeek = ETHIOPIC_UTC.dayOfWeek();
    DateTimeField dayOfYear = ETHIOPIC_UTC.dayOfYear();
    DateTimeField dayOfMonth = ETHIOPIC_UTC.dayOfMonth();
    DateTimeField monthOfYear = ETHIOPIC_UTC.monthOfYear();
    DateTimeField year = ETHIOPIC_UTC.year();
    DateTimeField yearOfEra = ETHIOPIC_UTC.yearOfEra();
    DateTimeField era = ETHIOPIC_UTC.era();
    int expectedDOW = new DateTime(8, 8, 29, 0, 0, 0, 0, JULIAN_UTC).getDayOfWeek();
    int expectedDOY = 1;
    int expectedDay = 1;
    int expectedMonth = 1;
    int expectedYear = 1;
    while (millis < end) {
        int dowValue = dayOfWeek.get(millis);
        int doyValue = dayOfYear.get(millis);
        int dayValue = dayOfMonth.get(millis);
        int monthValue = monthOfYear.get(millis);
        int yearValue = year.get(millis);
        int yearOfEraValue = yearOfEra.get(millis);
        int monthLen = dayOfMonth.getMaximumValue(millis);
        if (monthValue < 1 || monthValue > 13) {
            fail("Bad month: " + millis);
        }
        // test era
        assertEquals(1, era.get(millis));
        assertEquals("EE", era.getAsText(millis));
        assertEquals("EE", era.getAsShortText(millis));
        // test date
        assertEquals(expectedYear, yearValue);
        assertEquals(expectedYear, yearOfEraValue);
        assertEquals(expectedMonth, monthValue);
        assertEquals(expectedDay, dayValue);
        assertEquals(expectedDOW, dowValue);
        assertEquals(expectedDOY, doyValue);
        // test leap year
        assertEquals(yearValue % 4 == 3, year.isLeap(millis));
        // test month length
        if (monthValue == 13) {
            assertEquals(yearValue % 4 == 3, monthOfYear.isLeap(millis));
            if (yearValue % 4 == 3) {
                assertEquals(6, monthLen);
            } else {
                assertEquals(5, monthLen);
            }
        } else {
            assertEquals(30, monthLen);
        }
        // recalculate date
        expectedDOW = (((expectedDOW + 1) - 1) % 7) + 1;
        expectedDay++;
        expectedDOY++;
        if (expectedDay == 31 && expectedMonth < 13) {
            expectedDay = 1;
            expectedMonth++;
        } else if (expectedMonth == 13) {
            if (expectedYear % 4 == 3 && expectedDay == 7) {
                expectedDay = 1;
                expectedMonth = 1;
                expectedYear++;
                expectedDOY = 1;
            } else if (expectedYear % 4 != 3 && expectedDay == 6) {
                expectedDay = 1;
                expectedMonth = 1;
                expectedYear++;
                expectedDOY = 1;
            }
        }
        millis += SKIP;
    }
}
Also used : DateTimeField(org.joda.time.DateTimeField) DateTime(org.joda.time.DateTime)

Example 5 with DateTimeField

use of org.joda.time.DateTimeField in project joda-time by JodaOrg.

the class TestIslamicChronology method testCalendar.

//-----------------------------------------------------------------------
/**
     * Tests era, year, monthOfYear, dayOfMonth and dayOfWeek.
     */
public void testCalendar() {
    if (TestAll.FAST) {
        return;
    }
    System.out.println("\nTestIslamicChronology.testCalendar");
    DateTime epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, ISLAMIC_UTC);
    long millis = epoch.getMillis();
    long end = new DateTime(3000, 1, 1, 0, 0, 0, 0, ISO_UTC).getMillis();
    DateTimeField dayOfWeek = ISLAMIC_UTC.dayOfWeek();
    DateTimeField dayOfYear = ISLAMIC_UTC.dayOfYear();
    DateTimeField dayOfMonth = ISLAMIC_UTC.dayOfMonth();
    DateTimeField monthOfYear = ISLAMIC_UTC.monthOfYear();
    DateTimeField year = ISLAMIC_UTC.year();
    DateTimeField yearOfEra = ISLAMIC_UTC.yearOfEra();
    DateTimeField era = ISLAMIC_UTC.era();
    int expectedDOW = new DateTime(622, 7, 16, 0, 0, 0, 0, JULIAN_UTC).getDayOfWeek();
    int expectedDOY = 1;
    int expectedDay = 1;
    int expectedMonth = 1;
    int expectedYear = 1;
    while (millis < end) {
        int dowValue = dayOfWeek.get(millis);
        int doyValue = dayOfYear.get(millis);
        int dayValue = dayOfMonth.get(millis);
        int monthValue = monthOfYear.get(millis);
        int yearValue = year.get(millis);
        int yearOfEraValue = yearOfEra.get(millis);
        int dayOfYearLen = dayOfYear.getMaximumValue(millis);
        int monthLen = dayOfMonth.getMaximumValue(millis);
        if (monthValue < 1 || monthValue > 12) {
            fail("Bad month: " + millis);
        }
        // test era
        assertEquals(1, era.get(millis));
        assertEquals("AH", era.getAsText(millis));
        assertEquals("AH", era.getAsShortText(millis));
        // test date
        assertEquals(expectedDOY, doyValue);
        assertEquals(expectedMonth, monthValue);
        assertEquals(expectedDay, dayValue);
        assertEquals(expectedDOW, dowValue);
        assertEquals(expectedYear, yearValue);
        assertEquals(expectedYear, yearOfEraValue);
        // test leap year
        boolean leap = ((11 * yearValue + 14) % 30) < 11;
        assertEquals(leap, year.isLeap(millis));
        // test month length
        switch(monthValue) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 9:
            case 11:
                assertEquals(30, monthLen);
                break;
            case 2:
            case 4:
            case 6:
            case 8:
            case 10:
                assertEquals(29, monthLen);
                break;
            case 12:
                assertEquals((leap ? 30 : 29), monthLen);
                break;
        }
        // test year length
        assertEquals((leap ? 355 : 354), dayOfYearLen);
        // recalculate date
        expectedDOW = (((expectedDOW + 1) - 1) % 7) + 1;
        expectedDay++;
        expectedDOY++;
        if (expectedDay > monthLen) {
            expectedDay = 1;
            expectedMonth++;
            if (expectedMonth == 13) {
                expectedMonth = 1;
                expectedDOY = 1;
                expectedYear++;
            }
        }
        millis += SKIP;
    }
}
Also used : DateTimeField(org.joda.time.DateTimeField) DateTime(org.joda.time.DateTime)

Aggregations

DateTimeField (org.joda.time.DateTimeField)20 DateTime (org.joda.time.DateTime)5 DividedDateTimeField (org.joda.time.field.DividedDateTimeField)2 OffsetDateTimeField (org.joda.time.field.OffsetDateTimeField)2 RemainderDateTimeField (org.joda.time.field.RemainderDateTimeField)2 DateTimeFieldType (org.joda.time.DateTimeFieldType)1 IllegalFieldValueException (org.joda.time.IllegalFieldValueException)1 Interval (org.joda.time.Interval)1 Period (org.joda.time.Period)1 DelegatedDateTimeField (org.joda.time.field.DelegatedDateTimeField)1 PreciseDateTimeField (org.joda.time.field.PreciseDateTimeField)1 SkipUndoDateTimeField (org.joda.time.field.SkipUndoDateTimeField)1 UnsupportedDateTimeField (org.joda.time.field.UnsupportedDateTimeField)1 ZeroIsMaxDateTimeField (org.joda.time.field.ZeroIsMaxDateTimeField)1