List of usage examples for org.joda.time Minutes minutesIn
public static Minutes minutesIn(ReadableInterval interval)
Minutes
representing the number of whole minutes in the specified interval. From source file:ca.barrenechea.ticker.utils.TimeUtils.java
License:Apache License
public static TimeSpan getSpan(long start, long end) { if (end <= start) { return new TimeSpan(0, 0, 0); }/*from w w w . j a v a 2 s. c o m*/ Interval interval = new Interval(start, end); Days days = Days.daysIn(interval); Hours hours = Hours.hoursIn(interval).minus(days.toStandardHours()); Minutes minutes = Minutes.minutesIn(interval).minus(days.toStandardMinutes()) .minus(hours.toStandardMinutes()); return new TimeSpan(days.getDays(), hours.getHours(), minutes.getMinutes()); }
From source file:com.cisco.dvbu.ps.utils.date.DateDiffTimestamp.java
License:Open Source License
/** * Called to invoke the stored procedure. Will only be called a * single time per instance. Can throw CustomProcedureException or * SQLException if there is an error during invoke. *///from w w w. j a va2 s .co m public void invoke(Object[] inputValues) throws CustomProcedureException, SQLException { Timestamp startTimestamp = null; Timestamp endTimestamp = null; Calendar startCal = null; Calendar endCal = null; DateTime startDateTime = null; DateTime endDateTime = null; String datePart = null; long dateLength = 0; try { result = null; if (inputValues[0] == null) { result = new Long(dateLength); return; } if (inputValues[1] == null) { result = new Long(dateLength); return; } if (inputValues[2] == null) { result = new Long(dateLength); return; } datePart = (String) inputValues[0]; startTimestamp = (Timestamp) inputValues[1]; // long startMilliseconds = startTimestamp.getTime() + // (startTimestamp.getNanos() / 1000000); long startMilliseconds = startTimestamp.getTime() + (startTimestamp.getNanos() % 1000000L >= 500000L ? 1 : 0); startCal = Calendar.getInstance(); startCal.setTimeInMillis(startMilliseconds); endTimestamp = (Timestamp) inputValues[2]; // long endMilliseconds = endTimestamp.getTime() + // (endTimestamp.getNanos() / 1000000); long endMilliseconds = endTimestamp.getTime() + (endTimestamp.getNanos() % 1000000L >= 500000L ? 1 : 0); endCal = Calendar.getInstance(); endCal.setTimeInMillis(endMilliseconds); startDateTime = new DateTime(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH) + 1, startCal.get(Calendar.DAY_OF_MONTH), startCal.get(Calendar.HOUR_OF_DAY), startCal.get(Calendar.MINUTE), startCal.get(Calendar.SECOND), startCal.get(Calendar.MILLISECOND)); endDateTime = new DateTime(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH) + 1, endCal.get(Calendar.DAY_OF_MONTH), endCal.get(Calendar.HOUR_OF_DAY), endCal.get(Calendar.MINUTE), endCal.get(Calendar.SECOND), endCal.get(Calendar.MILLISECOND)); Interval interval = new Interval(startDateTime, endDateTime); if (datePart.equalsIgnoreCase("second") || datePart.equalsIgnoreCase("ss")) { Seconds seconds = Seconds.secondsIn(interval); dateLength = seconds.getSeconds(); } else if (datePart.equalsIgnoreCase("minute") || datePart.equalsIgnoreCase("mi")) { Minutes minutes = Minutes.minutesIn(interval); dateLength = minutes.getMinutes(); } else if (datePart.equalsIgnoreCase("hour") || datePart.equalsIgnoreCase("hh")) { Hours hours = Hours.hoursIn(interval); dateLength = hours.getHours(); } else if (datePart.equalsIgnoreCase("day") || datePart.equalsIgnoreCase("dd")) { Days days = Days.daysIn(interval); dateLength = days.getDays(); } else if (datePart.equalsIgnoreCase("week") || datePart.equalsIgnoreCase("wk")) { Weeks weeks = Weeks.weeksIn(interval); dateLength = weeks.getWeeks(); } else if (datePart.equalsIgnoreCase("month") || datePart.equalsIgnoreCase("mm")) { Months months = Months.monthsIn(interval); dateLength = months.getMonths(); } else if (datePart.equalsIgnoreCase("year") || datePart.equalsIgnoreCase("yy")) { Years years = Years.yearsIn(interval); dateLength = years.getYears(); } else if (datePart.equalsIgnoreCase("millisecond") || datePart.equalsIgnoreCase("ms")) { dateLength = (endTimestamp.getTime() - startTimestamp.getTime()); // millis } else if (datePart.equalsIgnoreCase("microsecond") || datePart.equalsIgnoreCase("mcs")) { dateLength = ((endTimestamp.getTime() - startTimestamp.getTime()) / 1000) // seconds * 1000000L // micros + (endTimestamp.getNanos() - startTimestamp.getNanos()) / 1000; // nanos/1000 } else if (datePart.equalsIgnoreCase("nanosecond") || datePart.equalsIgnoreCase("ns")) { dateLength = ((endTimestamp.getTime() - startTimestamp.getTime()) / 1000) // seconds * 1000000000L // nanos + (endTimestamp.getNanos() - startTimestamp.getNanos()); // nanos } else { throw new IllegalArgumentException(datePart); } result = new Long(dateLength); } catch (Throwable t) { throw new CustomProcedureException(t); } }
From source file:com.google.android.apps.paco.EsmGenerator2.java
License:Open Source License
public List<DateTime> generateForSchedule(DateTime startDate, SignalSchedule schedule) { this.schedule = schedule; this.periodStartDate = adjustStartDateToBeginningOfPeriod(startDate); times = new ArrayList<DateTime>(); if (schedule.getEsmFrequency() == null || schedule.getEsmFrequency() == 0) { return times; }//from ww w. j a v a 2s. c o m List<Integer> schedulableDays; switch (schedule.getEsmPeriodInDays()) { case SignalSchedule.ESM_PERIOD_DAY: if (!schedule.getEsmWeekends() && TimeUtil.isWeekend(periodStartDate)) { return times; } else { schedulableDays = Arrays.asList(1); } break; case SignalSchedule.ESM_PERIOD_WEEK: schedulableDays = getPeriodDaysForWeek(); break; case SignalSchedule.ESM_PERIOD_MONTH: schedulableDays = getPeriodDaysForMonthOf(periodStartDate); break; default: throw new IllegalStateException("Cannot get here."); } Minutes dayLengthIntervalInMinutes = Minutes .minutesIn(new Interval(schedule.getEsmStartHour(), schedule.getEsmEndHour())); Minutes totalMinutesInPeriod = dayLengthIntervalInMinutes.multipliedBy(schedulableDays.size()); Minutes sampleBlockTimeInMinutes = totalMinutesInPeriod.dividedBy(schedule.getEsmFrequency()); Minutes timeoutInMinutes = Minutes.minutes(schedule.getMinimumBuffer()); Random rand = new Random(); for (int signal = 0; signal < schedule.getEsmFrequency(); signal++) { int candidateTimeInBlock; DateTime candidateTime; int periodAttempts = 1000; do { candidateTimeInBlock = rand.nextInt(sampleBlockTimeInMinutes.getMinutes()); // map candidatePeriod and candidateTime back onto days of period // note, sometimes a candidate period will map across days in period // because start and end hours make for non-contiguous days int totalMinutesToAdd = sampleBlockTimeInMinutes.getMinutes() * signal + candidateTimeInBlock; int daysToAdd = totalMinutesToAdd / dayLengthIntervalInMinutes.getMinutes(); int minutesToAdd = 0; if (totalMinutesToAdd <= dayLengthIntervalInMinutes.getMinutes()) { // within one day minutesToAdd = totalMinutesToAdd; } else { minutesToAdd = totalMinutesToAdd % dayLengthIntervalInMinutes.getMinutes(); } DateTime plusDays = periodStartDate.plusDays(schedulableDays.get(daysToAdd) - 1); candidateTime = plusDays.withMillisOfDay(schedule.getEsmStartHour().intValue()) .plusMinutes(minutesToAdd); periodAttempts--; } while (periodAttempts > 0 && (!isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes) || (!schedule.getEsmWeekends() && TimeUtil.isWeekend(candidateTime)))); if (isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes) && (schedule.getEsmWeekends() || !TimeUtil.isWeekend(candidateTime))) { times.add(candidateTime); } } return times; }
From source file:com.metamx.druid.query.group.GroupByQueryQueryToolChest.java
License:Open Source License
@Override public ServiceMetricEvent.Builder makeMetricBuilder(GroupByQuery query) { int numMinutes = 0; for (Interval interval : query.getIntervals()) { numMinutes += Minutes.minutesIn(interval).getMinutes(); }/* w ww.j a v a 2 s .c om*/ return new ServiceMetricEvent.Builder().setUser2(query.getDataSource()) .setUser3(String.format("%,d dims", query.getDimensions().size())).setUser4("groupBy") .setUser5(Joiner.on(",").join(query.getIntervals())).setUser6(String.valueOf(query.hasFilters())) .setUser7(String.format("%,d aggs", query.getAggregatorSpecs().size())) .setUser9(Minutes.minutes(numMinutes).toString()); }
From source file:com.metamx.druid.query.metadata.SegmentMetadataQueryQueryToolChest.java
License:Open Source License
@Override public ServiceMetricEvent.Builder makeMetricBuilder(SegmentMetadataQuery query) { int numMinutes = 0; for (Interval interval : query.getIntervals()) { numMinutes += Minutes.minutesIn(interval).getMinutes(); }/* w w w .j av a 2s. com*/ return new ServiceMetricEvent.Builder().setUser2(query.getDataSource()).setUser4(query.getType()) .setUser5(Joiner.on(",").join(query.getIntervals())).setUser6(String.valueOf(query.hasFilters())) .setUser9(Minutes.minutes(numMinutes).toString()); }
From source file:com.metamx.druid.query.search.SearchQueryQueryToolChest.java
License:Open Source License
@Override public ServiceMetricEvent.Builder makeMetricBuilder(SearchQuery query) { int numMinutes = 0; for (Interval interval : query.getIntervals()) { numMinutes += Minutes.minutesIn(interval).getMinutes(); }/*w w w . ja va2 s .c om*/ return new ServiceMetricEvent.Builder().setUser2(query.getDataSource()).setUser4("search") .setUser5(COMMA_JOIN.join(query.getIntervals())).setUser6(String.valueOf(query.hasFilters())) .setUser9(Minutes.minutes(numMinutes).toString()); }
From source file:com.metamx.druid.query.timeseries.TimeseriesQueryQueryToolChest.java
License:Open Source License
@Override public ServiceMetricEvent.Builder makeMetricBuilder(TimeseriesQuery query) { int numMinutes = 0; for (Interval interval : query.getIntervals()) { numMinutes += Minutes.minutesIn(interval).getMinutes(); }/*from w w w. ja v a2s . co m*/ return new ServiceMetricEvent.Builder().setUser2(query.getDataSource()).setUser4("timeseries") .setUser5(COMMA_JOIN.join(query.getIntervals())).setUser6(String.valueOf(query.hasFilters())) .setUser7(String.format("%,d aggs", query.getAggregatorSpecs().size())) .setUser9(Minutes.minutes(numMinutes).toString()); }
From source file:com.pacoapp.paco.shared.scheduling.EsmGenerator2.java
License:Open Source License
public List<DateTime> generateForSchedule(DateTime startDate, Schedule schedule2) { this.schedule = schedule2; this.periodStartDate = adjustStartDateToBeginningOfPeriod(startDate); times = new ArrayList<DateTime>(); if (schedule2.getEsmFrequency() == null || schedule2.getEsmFrequency() == 0) { return times; }/*from w ww . j a v a 2 s.co m*/ List<Integer> schedulableDays; switch (schedule2.getEsmPeriodInDays()) { case Schedule.ESM_PERIOD_DAY: if (!schedule2.getEsmWeekends() && TimeUtil.isWeekend(periodStartDate)) { return times; } else { schedulableDays = Arrays.asList(1); } break; case Schedule.ESM_PERIOD_WEEK: schedulableDays = getPeriodDaysForWeek(); break; case Schedule.ESM_PERIOD_MONTH: schedulableDays = getPeriodDaysForMonthOf(periodStartDate); break; default: throw new IllegalStateException("Cannot get here."); } Minutes dayLengthIntervalInMinutes = Minutes .minutesIn(new Interval(schedule2.getEsmStartHour(), schedule2.getEsmEndHour())); Minutes totalMinutesInPeriod = dayLengthIntervalInMinutes.multipliedBy(schedulableDays.size()); Minutes sampleBlockTimeInMinutes = totalMinutesInPeriod.dividedBy(schedule2.getEsmFrequency()); Minutes timeoutInMinutes = Minutes.minutes(schedule2.getMinimumBuffer()); Random rand = new Random(); for (int signal = 0; signal < schedule2.getEsmFrequency(); signal++) { int candidateTimeInBlock; DateTime candidateTime; int periodAttempts = 1000; do { candidateTimeInBlock = rand.nextInt(sampleBlockTimeInMinutes.getMinutes()); // map candidatePeriod and candidateTime back onto days of period // note, sometimes a candidate period will map across days in period // because start and end hours make for non-contiguous days int totalMinutesToAdd = sampleBlockTimeInMinutes.getMinutes() * signal + candidateTimeInBlock; int daysToAdd = totalMinutesToAdd / dayLengthIntervalInMinutes.getMinutes(); int minutesToAdd = 0; if (totalMinutesToAdd <= dayLengthIntervalInMinutes.getMinutes()) { // within one day minutesToAdd = totalMinutesToAdd; } else { minutesToAdd = totalMinutesToAdd % dayLengthIntervalInMinutes.getMinutes(); } DateTime plusDays = periodStartDate.plusDays(schedulableDays.get(daysToAdd) - 1); candidateTime = plusDays.withMillisOfDay(schedule2.getEsmStartHour().intValue()) .plusMinutes(minutesToAdd); periodAttempts--; } while (periodAttempts > 0 && (!isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes) || (!schedule2.getEsmWeekends() && TimeUtil.isWeekend(candidateTime)))); if (isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes) && (schedule2.getEsmWeekends() || !TimeUtil.isWeekend(candidateTime))) { times.add(candidateTime); } } return times; }
From source file:io.druid.query.count.FrequencyCountQueryQueryToolChest.java
License:Open Source License
@Override public ServiceMetricEvent.Builder makeMetricBuilder(FrequencyCountQuery query) { int numMinutes = 0; for (Interval interval : query.getIntervals()) { numMinutes += Minutes.minutesIn(interval).getMinutes(); }/*from ww w . ja v a 2s .c om*/ return new ServiceMetricEvent.Builder().setUser2(query.getDataSource()).setUser4(query.getType()) .setUser5(Joiner.on(",").join(query.getIntervals())).setUser6(String.valueOf(query.hasFilters())) .setUser7(String.valueOf(query.hasFilters())).setUser9(Minutes.minutes(numMinutes).toString()); }
From source file:org.sleuthkit.autopsy.timeline.utils.RangeDivisionInfo.java
License:Open Source License
/** * Static factory method./* w ww. j a v a2 s .com*/ * * 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 } }