List of usage examples for org.joda.time Chronology hourOfDay
public abstract DateTimeField hourOfDay();
From source file:com.arpnetworking.kairosdb.aggregators.MovingWindowAggregator.java
License:Apache License
private DateTimeField timeUnitToTimeField(final TimeUnit timeUnit) { final Chronology chronology = GregorianChronology.getInstance(_timeZone); switch (timeUnit) { case YEARS:/* w w w . j a v a2 s . c om*/ return chronology.year(); case MONTHS: return chronology.monthOfYear(); case WEEKS: return chronology.weekOfWeekyear(); case DAYS: return chronology.dayOfMonth(); case HOURS: return chronology.hourOfDay(); case MINUTES: return chronology.minuteOfHour(); case SECONDS: return chronology.secondOfDay(); default: return chronology.millisOfSecond(); } }
From source file:net.sourceforge.fenixedu.util.HourMinuteSecond.java
License:Open Source License
/** * Gets the field for a specific index in the chronology specified. * <p>//from www .ja v a 2 s . c om * This method must not use any instance variables. * * @param index * the index to retrieve * @param chrono * the chronology to use * @return the field */ @Override protected DateTimeField getField(int index, Chronology chrono) { switch (index) { case HOUR_OF_DAY: return chrono.hourOfDay(); case MINUTE_OF_HOUR: return chrono.minuteOfHour(); case SECOND_OF_MINUTE: return chrono.secondOfMinute(); default: throw new IndexOutOfBoundsException("Invalid index: " + index); } }
From source file:org.jruby.ext.date.RubyDateTime.java
License:LGPL
@JRubyMethod(name = "civil", alias = "new", meta = true, optional = 8) public static RubyDateTime civil(ThreadContext context, IRubyObject self, IRubyObject[] args) { // year=-4712, month=1, mday=1, // hour=0, minute=0, second=0, offset=0, start=Date::ITALY final int len = args.length; int hour = 0, minute = 0, second = 0; long millis = 0; long subMillisNum = 0, subMillisDen = 1; int off = 0;// w w w . j ava 2 s. co m long sg = ITALY; if (len == 8) sg = val2sg(context, args[7]); if (len >= 7) off = val2off(context, args[6]); final int year = (sg > 0) ? getYear(args[0]) : args[0].convertToInteger().getIntValue(); final int month = getMonth(args[1]); final long[] rest = new long[] { 0, 1 }; final int day = (int) getDay(context, args[2], rest); if (len >= 4 || rest[0] != 0) { hour = getHour(context, len >= 4 ? args[3] : RubyFixnum.zero(context.runtime), rest); } if (len >= 5 || rest[0] != 0) { minute = getMinute(context, len >= 5 ? args[4] : RubyFixnum.zero(context.runtime), rest); } if (len >= 6 || rest[0] != 0) { IRubyObject sec = len >= 6 ? args[5] : RubyFixnum.zero(context.runtime); second = getSecond(context, sec, rest); final long r0 = rest[0], r1 = rest[1]; if (r0 != 0) { millis = (1000 * r0) / r1; subMillisNum = ((1000 * r0) - (millis * r1)); subMillisDen = r1; } } if (hour == 24 && (minute != 0 || second != 0 || millis != 0)) { throw context.runtime.newArgumentError("invalid date"); } final Chronology chronology = getChronology(context, sg, off); DateTime dt = civilDate(context, year, month, day, chronology); // hour: 0, minute: 0, second: 0 try { long ms = dt.getMillis(); ms = chronology.hourOfDay().set(ms, hour == 24 ? 0 : hour); ms = chronology.minuteOfHour().set(ms, minute); ms = chronology.secondOfMinute().set(ms, second); dt = dt.withMillis(ms + millis); if (hour == 24) dt = dt.plusDays(1); } catch (IllegalArgumentException ex) { debug(context, "invalid date", ex); throw context.runtime.newArgumentError("invalid date"); } return (RubyDateTime) new RubyDateTime(context.runtime, (RubyClass) self, dt, off, sg, subMillisNum, subMillisDen).normalizeSubMillis(); }
From source file:org.springframework.analytics.metrics.redis.RedisAggregateCounterRepository.java
License:Apache License
/** * For each query, we need to convert the interval into two variations. One is the start and end points rounded to * the resolution (used to calculate the number of entries to be returned from the query). The second is the start * and end buckets we have to retrieve which may contain entries for the interval. For example, when querying * at day resolution, the number of entries is the number of Joda time days between the start (rounded down to a * day boundary) and the end plus one day (also rounded down). However, we need load the data from the buckets * from the month the start day occurs in to the month end day occurs in. These are then concatenated, using the * start day as the start index into the first array, and writing the total number of entries in sequence from that * point into the combined result counts array. *//* w ww .j av a 2s . c o m*/ @Override public AggregateCounter getCounts(String name, Interval interval, AggregateCounterResolution resolution) { DateTime end = interval.getEnd(); Chronology c = interval.getChronology(); long[] counts; if (resolution == AggregateCounterResolution.minute) { // Iterate through each hour in the interval and load the minutes for it MutableDateTime dt = new MutableDateTime(interval.getStart()); dt.setRounding(c.hourOfDay()); Duration step = Duration.standardHours(1); List<long[]> hours = new ArrayList<long[]>(); while (dt.isBefore(end) || dt.isEqual(end)) { hours.add(getMinCountsForHour(name, dt)); dt.add(step); } counts = MetricUtils.concatArrays(hours, interval.getStart().getMinuteOfHour(), interval.toPeriod().toStandardMinutes().getMinutes() + 1); } else if (resolution == AggregateCounterResolution.hour) { DateTime cursor = new DateTime(c.dayOfMonth().roundFloor(interval.getStart().getMillis())); List<long[]> days = new ArrayList<long[]>(); Duration step = Duration.standardHours(24); while (cursor.isBefore(end)) { days.add(getHourCountsForDay(name, cursor)); cursor = cursor.plus(step); } counts = MetricUtils.concatArrays(days, interval.getStart().getHourOfDay(), interval.toPeriod().toStandardHours().getHours() + 1); } else if (resolution == AggregateCounterResolution.day) { DateTime startDay = new DateTime(c.dayOfYear().roundFloor(interval.getStart().getMillis())); DateTime endDay = new DateTime(c.dayOfYear().roundFloor(end.plusDays(1).getMillis())); int nDays = Days.daysBetween(startDay, endDay).getDays(); DateTime cursor = new DateTime(c.monthOfYear().roundFloor(interval.getStart().getMillis())); List<long[]> months = new ArrayList<long[]>(); DateTime endMonth = new DateTime( c.monthOfYear().roundCeiling(interval.getEnd().plusMonths(1).getMillis())); while (cursor.isBefore(endMonth)) { months.add(getDayCountsForMonth(name, cursor)); cursor = cursor.plusMonths(1); } counts = MetricUtils.concatArrays(months, interval.getStart().getDayOfMonth() - 1, nDays); } else if (resolution == AggregateCounterResolution.month) { DateTime startMonth = new DateTime(c.monthOfYear().roundFloor(interval.getStartMillis())); DateTime endMonth = new DateTime(c.monthOfYear().roundFloor(end.plusMonths(1).getMillis())); int nMonths = Months.monthsBetween(startMonth, endMonth).getMonths(); DateTime cursor = new DateTime(c.year().roundFloor(interval.getStartMillis())); List<long[]> years = new ArrayList<long[]>(); DateTime endYear = new DateTime(c.year().roundCeiling(interval.getEnd().plusYears(1).getMillis())); while (cursor.isBefore(endYear)) { years.add(getMonthCountsForYear(name, cursor)); cursor = cursor.plusYears(1); } counts = MetricUtils.concatArrays(years, interval.getStart().getMonthOfYear() - 1, nMonths); } else if (resolution == AggregateCounterResolution.year) { DateTime startYear = new DateTime(interval.getStart().getYear(), 1, 1, 0, 0); DateTime endYear = new DateTime(end.getYear() + 1, 1, 1, 0, 0); int nYears = Years.yearsBetween(startYear, endYear).getYears(); Map<String, Long> yearCounts = getYearCounts(name); counts = new long[nYears]; for (int i = 0; i < nYears; i++) { int year = startYear.plusYears(i).getYear(); Long count = yearCounts.get(Integer.toString(year)); if (count == null) { count = 0L; } counts[i] = count; } } else { throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution); } return new AggregateCounter(name, interval, counts, resolution); }
From source file:org.springframework.xd.analytics.metrics.redis.RedisAggregateCounterRepository.java
License:Apache License
/** * For each query, we need to convert the interval into two variations. One is the start and end points rounded to * the resolution (used to calculate the number of entries to be returned from the query). The second is the start * and end buckets we have to retrieve which may contain entries for the interval. For example, when querying * at day resolution, the number of entries is the number of Joda time days between the start (rounded down to a * day boundary) and the end plus one day (also rounded down). However, we need load the data from the buckets * from the month the start day occurs in to the month end day occurs in. These are then concatenated, using the * start day as the start index into the first array, and writing the total number of entries in sequence from that * point into the combined result counts array. *//*from ww w . j av a 2 s . c o m*/ @Override public AggregateCount getCounts(String name, Interval interval, AggregateCountResolution resolution) { DateTime end = interval.getEnd(); Chronology c = interval.getChronology(); long[] counts; if (resolution == AggregateCountResolution.minute) { // Iterate through each hour in the interval and load the minutes for it MutableDateTime dt = new MutableDateTime(interval.getStart()); dt.setRounding(c.hourOfDay()); Duration step = Duration.standardHours(1); List<long[]> hours = new ArrayList<long[]>(); while (dt.isBefore(end) || dt.isEqual(end)) { hours.add(getMinCountsForHour(name, dt)); dt.add(step); } counts = MetricUtils.concatArrays(hours, interval.getStart().getMinuteOfHour(), interval.toPeriod().toStandardMinutes().getMinutes() + 1); } else if (resolution == AggregateCountResolution.hour) { DateTime cursor = new DateTime(c.dayOfMonth().roundFloor(interval.getStart().getMillis())); List<long[]> days = new ArrayList<long[]>(); Duration step = Duration.standardHours(24); while (cursor.isBefore(end)) { days.add(getHourCountsForDay(name, cursor)); cursor = cursor.plus(step); } counts = MetricUtils.concatArrays(days, interval.getStart().getHourOfDay(), interval.toPeriod().toStandardHours().getHours() + 1); } else if (resolution == AggregateCountResolution.day) { DateTime startDay = new DateTime(c.dayOfYear().roundFloor(interval.getStart().getMillis())); DateTime endDay = new DateTime(c.dayOfYear().roundFloor(end.plusDays(1).getMillis())); int nDays = Days.daysBetween(startDay, endDay).getDays(); DateTime cursor = new DateTime(c.monthOfYear().roundFloor(interval.getStart().getMillis())); List<long[]> months = new ArrayList<long[]>(); DateTime endMonth = new DateTime( c.monthOfYear().roundCeiling(interval.getEnd().plusMonths(1).getMillis())); while (cursor.isBefore(endMonth)) { months.add(getDayCountsForMonth(name, cursor)); cursor = cursor.plusMonths(1); } counts = MetricUtils.concatArrays(months, interval.getStart().getDayOfMonth() - 1, nDays); } else if (resolution == AggregateCountResolution.month) { DateTime startMonth = new DateTime(c.monthOfYear().roundFloor(interval.getStartMillis())); DateTime endMonth = new DateTime(c.monthOfYear().roundFloor(end.plusMonths(1).getMillis())); int nMonths = Months.monthsBetween(startMonth, endMonth).getMonths(); DateTime cursor = new DateTime(c.year().roundFloor(interval.getStartMillis())); List<long[]> years = new ArrayList<long[]>(); DateTime endYear = new DateTime(c.year().roundCeiling(interval.getEnd().plusYears(1).getMillis())); while (cursor.isBefore(endYear)) { years.add(getMonthCountsForYear(name, cursor)); cursor = cursor.plusYears(1); } counts = MetricUtils.concatArrays(years, interval.getStart().getMonthOfYear() - 1, nMonths); } else if (resolution == AggregateCountResolution.year) { DateTime startYear = new DateTime(interval.getStart().getYear(), 1, 1, 0, 0); DateTime endYear = new DateTime(end.getYear() + 1, 1, 1, 0, 0); int nYears = Years.yearsBetween(startYear, endYear).getYears(); Map<String, Long> yearCounts = getYearCounts(name); counts = new long[nYears]; for (int i = 0; i < nYears; i++) { int year = startYear.plusYears(i).getYear(); Long count = yearCounts.get(Integer.toString(year)); if (count == null) { count = 0L; } counts[i] = count; } } else { throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution); } return new AggregateCount(name, interval, counts, resolution); }