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 = anchorTimeFrom(lastAnchor);

        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);
    }
@Test
    public void determineAnchor() {
        final DateTime initialTime = new DateTime(2014, 3, 15, 14, 48, 35, 0, DateTimeZone.UTC);

        final InstantMillisProvider clock = new InstantMillisProvider(initialTime);
        DateTimeUtils.setCurrentMillisProvider(clock);
        Period period;

        // should snap to 14:00:00
        period = hours(1);
        final DateTime hourAnchor = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
        assertEquals(14, hourAnchor.getHourOfDay());
        assertEquals(0, hourAnchor.getMinuteOfHour());
        assertEquals(0, hourAnchor.getSecondOfMinute());

        // should snap to 14:45:00
        period = minutes(5);
        final DateTime fiveMins = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
        assertEquals(14, fiveMins.getHourOfDay());
        assertEquals(45, fiveMins.getMinuteOfHour());
        assertEquals(0, fiveMins.getSecondOfMinute());

        // should snap to 2014-3-15 00:00:00
        period = Period.days(1).withHours(6);
        final DateTime dayAnd6Hours = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
        assertEquals(2014, dayAnd6Hours.getYear());
        assertEquals(3, dayAnd6Hours.getMonthOfYear());
        assertEquals(15, dayAnd6Hours.getDayOfMonth());
        assertEquals(0, dayAnd6Hours.getHourOfDay());
        assertEquals(0, dayAnd6Hours.getMinuteOfHour());
        assertEquals(0, dayAnd6Hours.getSecondOfMinute());

        period = Period.days(30);
        final DateTime thirtyDays = TimeBasedRotationStrategy.determineRotationPeriodAnchor(null, period);
        assertEquals(2014, thirtyDays.getYear());
        assertEquals(2, thirtyDays.getMonthOfYear());
        assertEquals(17, thirtyDays.getDayOfMonth());
        assertEquals(0, thirtyDays.getHourOfDay());
        assertEquals(0, thirtyDays.getMinuteOfHour());
        assertEquals(0, thirtyDays.getSecondOfMinute());

        period = hours(1);
        final DateTime diffAnchor = TimeBasedRotationStrategy.determineRotationPeriodAnchor(initialTime.minusMinutes(61), period);
        assertEquals(2014, diffAnchor.getYear());
        assertEquals(3, diffAnchor.getMonthOfYear());
        assertEquals(15, diffAnchor.getDayOfMonth());
        assertEquals(13, diffAnchor.getHourOfDay());
        assertEquals(0, diffAnchor.getMinuteOfHour());
        assertEquals(0, diffAnchor.getSecondOfMinute());

    }