List of usage examples for org.joda.time DateTimeFieldType getDurationType
public abstract DurationFieldType getDurationType();
From source file:org.apereo.portal.events.aggr.AggregationInterval.java
License:Apache License
/** * Determine the number of intervals between the start and end dates * * @param start Start, inclusive//from w w w . ja v a2 s . c om * @param end End, exclusive * @return Number of intervals between start and end */ public int determineIntervalsBetween(ReadableInstant start, ReadableInstant end) { if (!this.isSupportsDetermination()) { throw new IllegalArgumentException("Cannot compute intervals between for " + this + " please use " + AggregationIntervalHelper.class); } final DateTimeFieldType dtft; final double ratio; switch (this) { case FIVE_MINUTE: { dtft = MINUTE.getDateTimeFieldType(); ratio = 5; break; } default: { dtft = dateTimeFieldType; ratio = 1; } } final DurationFieldType durationType = dtft.getDurationType(); final Chronology chrono = DateTimeUtils.getInstantChronology(start); return (int) Math .round(durationType.getField(chrono).getDifference(end.getMillis(), start.getMillis()) / ratio); }
From source file:org.gephi.desktop.timeline.DateTick.java
License:Open Source License
public static DateTick create(double min, double max, int width) { DateTime minDate = new DateTime((long) min); DateTime maxDate = new DateTime((long) max); Period period = new Period(minDate, maxDate, PeriodType.yearMonthDayTime()); ;/* w ww. ja va2 s.com*/ int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); int hours = period.getHours(); int minutes = period.getMinutes(); int seconds = period.getSeconds(); //Top type DateTimeFieldType topType; if (years > 0) { topType = DateTimeFieldType.year(); } else if (months > 0) { topType = DateTimeFieldType.monthOfYear(); } else if (days > 0) { topType = DateTimeFieldType.dayOfMonth(); } else if (hours > 0) { topType = DateTimeFieldType.hourOfDay(); } else if (minutes > 0) { topType = DateTimeFieldType.minuteOfHour(); } else if (seconds > 0) { topType = DateTimeFieldType.secondOfMinute(); } else { topType = DateTimeFieldType.millisOfSecond(); } //Bottom type if (topType != DateTimeFieldType.millisOfSecond()) { DateTimeFieldType bottomType; if (topType.equals(DateTimeFieldType.year())) { bottomType = DateTimeFieldType.monthOfYear(); } else if (topType.equals(DateTimeFieldType.monthOfYear())) { bottomType = DateTimeFieldType.dayOfMonth(); } else if (topType.equals(DateTimeFieldType.dayOfMonth())) { bottomType = DateTimeFieldType.hourOfDay(); } else if (topType.equals(DateTimeFieldType.hourOfDay())) { bottomType = DateTimeFieldType.minuteOfHour(); } else if (topType.equals(DateTimeFieldType.minuteOfHour())) { bottomType = DateTimeFieldType.secondOfMinute(); } else { bottomType = DateTimeFieldType.millisOfSecond(); } //Number of ticks Period p = new Period(minDate, maxDate, PeriodType.forFields(new DurationFieldType[] { bottomType.getDurationType() })); int intervals = p.get(bottomType.getDurationType()); if (intervals > 0) { int intervalSize = width / intervals; if (intervalSize >= MIN_PIXELS) { return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType, bottomType }); } } } return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType }); }
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 . j av a2 s. co 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 om*/ * * 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.sleuthkit.autopsy.timeline.utils.RangeDivisionInfo.java
License:Open Source License
/** * Static factory method./*from ww w . j a v a 2 s. c o m*/ * * Determine the period size, number of periods, whole period bounds, and * formatters to use to visualize the given timerange. * * @param timeRange * * @return */ public static RangeDivisionInfo getRangeDivisionInfo(Interval timeRange) { //Check from largest to smallest unit //TODO: make this more generic... reduce code duplication -jm DateTimeFieldType timeUnit; final DateTime startWithZone = timeRange.getStart().withZone(TimeLineController.getJodaTimeZone()); final DateTime endWithZone = timeRange.getEnd().withZone(TimeLineController.getJodaTimeZone()); if (Years.yearsIn(timeRange).isGreaterThan(Years.THREE)) { timeUnit = DateTimeFieldType.year(); long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis(); long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis(); return new RangeDivisionInfo(timeRange, Years.yearsIn(timeRange).get(timeUnit.getDurationType()) + 1, TimeUnits.YEARS, ISODateTimeFormat.year(), lower, upper); } else if (Months.monthsIn(timeRange).isGreaterThan(Months.THREE)) { timeUnit = DateTimeFieldType.monthOfYear(); long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis(); long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis(); return new RangeDivisionInfo(timeRange, Months.monthsIn(timeRange).getMonths() + 1, TimeUnits.MONTHS, DateTimeFormat.forPattern("YYYY'-'MMMM"), lower, upper); // NON-NLS } else if (Days.daysIn(timeRange).isGreaterThan(Days.THREE)) { timeUnit = DateTimeFieldType.dayOfMonth(); long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis(); long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis(); return new RangeDivisionInfo(timeRange, Days.daysIn(timeRange).getDays() + 1, TimeUnits.DAYS, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd"), lower, upper); // NON-NLS } else if (Hours.hoursIn(timeRange).isGreaterThan(Hours.THREE)) { timeUnit = DateTimeFieldType.hourOfDay(); long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis(); long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis(); return new RangeDivisionInfo(timeRange, Hours.hoursIn(timeRange).getHours() + 1, TimeUnits.HOURS, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd HH"), lower, upper); // NON-NLS } else if (Minutes.minutesIn(timeRange).isGreaterThan(Minutes.THREE)) { timeUnit = DateTimeFieldType.minuteOfHour(); long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis(); long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis(); return new RangeDivisionInfo(timeRange, Minutes.minutesIn(timeRange).getMinutes() + 1, TimeUnits.MINUTES, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd HH':'mm"), lower, upper); // NON-NLS } else { timeUnit = DateTimeFieldType.secondOfMinute(); long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis(); long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis(); return new RangeDivisionInfo(timeRange, Seconds.secondsIn(timeRange).getSeconds() + 1, TimeUnits.SECONDS, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd HH':'mm':'ss"), lower, upper); // NON-NLS } }