Example usage for org.joda.time Period getDays

List of usage examples for org.joda.time Period getDays

Introduction

In this page you can find the example usage for org.joda.time Period getDays.

Prototype

public int getDays() 

Source Link

Document

Gets the days field part of the period.

Usage

From source file:org.graylog2.indexer.rotation.strategies.TimeBasedRotationStrategy.java

License:Open Source License

/**
 * 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.// w  w  w . ja  va2  s  .  c  o m
 *
 * 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
 */
protected 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);
}

From source file:org.graylog2.indexer.rotation.TimeBasedRotationStrategy.java

License:Open Source License

/**
 * 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.//from   ww  w  . j  a  v a2s.  c o  m
 *
 * 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
 */
protected static DateTime determineRotationPeriodAnchor(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 now = Tools.iso8601();

    final DateTimeField field = largestStrideType.getField(now.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(now.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);
}

From source file:org.hawkular.metrics.core.impl.DateTimeService.java

License:Apache License

public DateTime getTimeSlice(DateTime dt, Duration duration) {
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }/*  ww  w. j av  a  2  s.  com*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.hawkular.metrics.datetime.DateTimeService.java

License:Apache License

public static DateTime getTimeSlice(DateTime dt, Duration duration) {
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }/*www. j  a  v  a  2s .  com*/
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.hawkular.metrics.tasks.api.AbstractTrigger.java

License:Apache License

protected DateTime getExecutionTime(long time, Duration duration) {
    DateTime dt = new DateTime(time);
    Period p = duration.toPeriod();

    if (p.getYears() != 0) {
        return dt.yearOfEra().roundFloorCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundFloorCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundFloorCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundFloorCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundFloorCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundFloorCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundFloorCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }//ww  w  .  j a v  a  2  s  .c  o m
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:org.jbpm.process.core.timer.BusinessCalendarImpl.java

License:Apache License

protected String adoptISOFormat(String timeExpression) {

    try {/*w w  w .j a  va2  s.  co  m*/
        Period p = null;
        if (DateTimeUtils.isPeriod(timeExpression)) {
            p = ISOPeriodFormat.standard().parsePeriod(timeExpression);
        } else {
            DateTime dt = ISODateTimeFormat.dateTimeParser().parseDateTime(timeExpression);
            Duration duration = new Duration(System.currentTimeMillis(), dt.getMillis());

            p = duration.toPeriod();
        }
        int days = p.getDays();
        int hours = p.getHours();
        int minutes = p.getMinutes();
        int seconds = p.getSeconds();
        int milis = p.getMillis();

        StringBuffer time = new StringBuffer();
        if (days > 0) {
            time.append(days + "d");
        }
        if (hours > 0) {
            time.append(hours + "h");
        }
        if (minutes > 0) {
            time.append(minutes + "m");
        }
        if (seconds > 0) {
            time.append(seconds + "s");
        }
        if (milis > 0) {
            time.append(milis + "ms");
        }

        return time.toString();
    } catch (Exception e) {
        return timeExpression;
    }
}

From source file:org.jtalks.jcommune.service.transactional.TransactionalUserService.java

License:Open Source License

/**
 * {@inheritDoc}/*from   www  . j  ava  2s. c o m*/
 */
@Override
//  Temporarily disabled. Until we find the bug due to which activated users become not activated.
//    @Scheduled(cron = "0 * * * * *") // cron expression: invoke every hour at :00 min, e.g. 11:00, 12:00 and so on
public void deleteUnactivatedAccountsByTimer() {
    DateTime today = new DateTime();
    for (JCUser user : this.getDao().getNonActivatedUsers()) {
        Period period = new Period(user.getRegistrationDate(), today);
        if (period.getDays() > 0) {
            this.getDao().delete(user);
        }
    }
}

From source file:org.kalypso.commons.time.PeriodUtils.java

License:Open Source License

/**
 * @return {@link Integer#MAX_VALUE} if the amount could not be determined.
 *///from   ww  w  . j  av a2s.  c o  m
public static int findCalendarAmount(final Period period) {
    final int fieldCount = countNonZeroFields(period);
    if (fieldCount > 1)
        throw new IllegalArgumentException(
                "Unable to find calendar amount for periods with more than one field: " + period); //$NON-NLS-1$

    if (period.getDays() != 0)
        return period.getDays();

    if (period.getHours() != 0)
        return period.getHours();

    if (period.getMillis() != 0)
        return period.getMillis();

    if (period.getMinutes() != 0)
        return period.getMinutes();

    if (period.getMonths() != 0)
        return period.getMonths();

    if (period.getSeconds() != 0)
        return period.getSeconds();

    if (period.getWeeks() != 0)
        return period.getWeeks();

    if (period.getYears() != 0)
        return period.getYears();

    return Integer.MAX_VALUE;
}

From source file:org.kalypso.commons.time.PeriodUtils.java

License:Open Source License

public static FIELD findCalendarField(final Period period) {
    final int fieldCount = countNonZeroFields(period);

    if (fieldCount > 1)
        throw new IllegalArgumentException(
                "Unable to find calendar field for periods with more than one field: " + period); //$NON-NLS-1$

    if (period.getDays() != 0)
        return FIELD.DAY_OF_MONTH;

    if (period.getHours() != 0)
        return FIELD.HOUR_OF_DAY;

    if (period.getMillis() != 0)
        return FIELD.MILLISECOND;

    if (period.getMinutes() != 0)
        return FIELD.MINUTE;

    if (period.getMonths() != 0)
        return FIELD.MONTH;

    if (period.getSeconds() != 0)
        return FIELD.SECOND;

    if (period.getWeeks() != 0)
        return FIELD.WEEK_OF_YEAR;

    if (period.getYears() != 0)
        return FIELD.YEAR;

    return null;//  w ww.  j a  va 2  s . com
}

From source file:org.kalypso.ogc.sensor.metadata.MetadataHelper.java

License:Open Source License

public static void setTimestep(final MetadataList mdl, final Period timestep) {
    final int[] values = timestep.getValues();
    int fieldCount = 0;
    for (final int value : values) {
        if (value != 0)
            fieldCount++;/*from  w  w  w. j a va 2s .  com*/
    }

    if (fieldCount > 1)
        throw new IllegalArgumentException(Messages.getString("MetadataHelper_2") + timestep); //$NON-NLS-1$

    int amount = -1;
    int calendarField = -1;

    if (timestep.getDays() != 0) {
        amount = timestep.getDays();
        calendarField = Calendar.DAY_OF_MONTH;
    } else if (timestep.getHours() != 0) {
        amount = timestep.getHours();
        calendarField = Calendar.HOUR_OF_DAY;
    } else if (timestep.getMillis() != 0) {
        amount = timestep.getMillis();
        calendarField = Calendar.MILLISECOND;
    } else if (timestep.getMinutes() != 0) {
        amount = timestep.getMinutes();
        calendarField = Calendar.MINUTE;
    } else if (timestep.getMonths() != 0) {
        amount = timestep.getMonths();
        calendarField = Calendar.MONTH;
    } else if (timestep.getSeconds() != 0) {
        amount = timestep.getSeconds();
        calendarField = Calendar.SECOND;
    } else if (timestep.getWeeks() != 0) {
        amount = timestep.getWeeks();
        calendarField = Calendar.WEEK_OF_YEAR;
    } else if (timestep.getYears() != 0) {
        amount = timestep.getYears();
        calendarField = Calendar.YEAR;
    }

    if (amount == -1)
        throw new IllegalArgumentException(Messages.getString("MetadataHelper_3")); //$NON-NLS-1$

    setTimestep(mdl, calendarField, amount);

    return;
}