public long roundCeiling(long instant) {
   if (iTimeField) {
     int offset = getOffsetToAdd(instant);
     instant = iField.roundCeiling(instant + offset);
     return instant - offset;
   } else {
     long localInstant = iZone.convertUTCToLocal(instant);
     localInstant = iField.roundCeiling(localInstant);
     return iZone.convertLocalToUTC(localInstant, false, instant);
   }
 }
 public long addWrapField(long instant, int value) {
   if (iTimeField) {
     int offset = getOffsetToAdd(instant);
     long localInstant = iField.addWrapField(instant + offset, value);
     return localInstant - offset;
   } else {
     long localInstant = iZone.convertUTCToLocal(instant);
     localInstant = iField.addWrapField(localInstant, value);
     return iZone.convertLocalToUTC(localInstant, false, instant);
   }
 }
Пример #3
0
 @Description("second of the minute of the given timestamp")
 @ScalarFunction("second")
 @SqlType(BigintType.class)
 public static long secondFromTimestamp(@SqlType(TimestampType.class) long timestamp) {
   // Time is effectively UTC so no need for a custom chronology
   return SECOND_OF_MINUTE.get(timestamp);
 }
Пример #4
0
 @Description("second of the minute of the given time")
 @ScalarFunction("second")
 @SqlType(BigintType.class)
 public static long secondFromTimeWithTimeZone(@SqlType(TimeWithTimeZoneType.class) long time) {
   // Time is effectively UTC so no need for a custom chronology
   return SECOND_OF_MINUTE.get(unpackMillisUtc(time));
 }
 public long set(long instant, int value) {
   long localInstant = iZone.convertUTCToLocal(instant);
   localInstant = iField.set(localInstant, value);
   long result = iZone.convertLocalToUTC(localInstant, false, instant);
   if (get(result) != value) {
     throw new IllegalFieldValueException(
         iField.getType(),
         Integer.valueOf(value),
         "Illegal instant due to time zone offset transition: "
             + DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
                 .print(new Instant(localInstant))
             + " ("
             + iZone.getID()
             + ")");
   }
   return result;
 }
 private DateTimeField convertField(DateTimeField field, HashMap<Object, Object> converted) {
   if (field == null || !field.isSupported()) {
     return field;
   }
   if (converted.containsKey(field)) {
     return (DateTimeField) converted.get(field);
   }
   ZonedDateTimeField zonedField =
       new ZonedDateTimeField(
           field,
           getZone(),
           convertField(field.getDurationField(), converted),
           convertField(field.getRangeDurationField(), converted),
           convertField(field.getLeapDurationField(), converted));
   converted.put(field, zonedField);
   return zonedField;
 }
 ZonedDateTimeField(
     DateTimeField field,
     DateTimeZone zone,
     DurationField durationField,
     DurationField rangeDurationField,
     DurationField leapDurationField) {
   super(field.getType());
   if (!field.isSupported()) {
     throw new IllegalArgumentException();
   }
   iField = field;
   iZone = zone;
   iDurationField = durationField;
   iTimeField = useTimeArithmetic(durationField);
   iRangeDurationField = rangeDurationField;
   iLeapDurationField = leapDurationField;
 }
Пример #8
0
  /**
   * Given a year, month, and day, find the number of occurrences of that day in the month
   *
   * @param year the year
   * @param month the month
   * @param day the day
   * @return the number of occurrences of the day in the month
   */
  public int numOccurrences(int year, int month, int day) {
    DateTimeFormatter parser = ISODateTimeFormat.date();
    DateTime date = parser.parseDateTime(year + "-" + month + "-" + "01");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date.toDate());
    GregorianChronology calendar = GregorianChronology.getInstance();
    DateTimeField field = calendar.dayOfMonth();

    int days = 0;
    int count = 0;
    int num = field.getMaximumValue(new LocalDate(year, month, day, calendar));
    while (days < num) {
      if (cal.get(Calendar.DAY_OF_WEEK) == day) {
        count++;
      }
      date = date.plusDays(1);
      cal.setTime(date.toDate());

      days++;
    }
    return count;
  }
  @Override
  public AggregateCount getCounts(String name, Interval interval, DateTimeField resolution) {

    DateTime end = interval.getEnd();
    Chronology c = interval.getChronology();
    DurationField resolutionDuration = resolution.getDurationField();

    long[] counts;

    if (resolutionDuration.getUnitMillis() == DateTimeConstants.MILLIS_PER_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)) {
        hours.add(getMinCountsForHour(name, dt));
        dt.add(step);
      }
      counts =
          MetricUtils.concatArrays(
              hours,
              interval.getStart().getMinuteOfHour(),
              interval.toPeriod().toStandardMinutes().getMinutes() + 1,
              60);

    } else if (resolutionDuration.getUnitMillis() == DateTimeConstants.MILLIS_PER_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,
              24);

    } else {
      throw new IllegalArgumentException("Only minute or hour resolution is currently supported");
    }
    return new AggregateCount(name, interval, counts, resolution);
  }
 public boolean isLeap(long instant) {
   long localInstant = iZone.convertUTCToLocal(instant);
   return iField.isLeap(localInstant);
 }
 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
   int offset = getOffsetToAdd(subtrahendInstant);
   return iField.getDifferenceAsLong(
       minuendInstant + (iTimeField ? offset : getOffsetToAdd(minuendInstant)),
       subtrahendInstant + offset);
 }
 public long set(long instant, String text, Locale locale) {
   // cannot verify that new value stuck because set may be lenient
   long localInstant = iZone.convertUTCToLocal(instant);
   localInstant = iField.set(localInstant, text, locale);
   return iZone.convertLocalToUTC(localInstant, false, instant);
 }
 static int parseMonth(String str) {
   DateTimeField field = ISOChronology.getInstanceUTC().monthOfYear();
   return field.get(field.set(0, str, Locale.ENGLISH));
 }
 public int getMaximumValue(ReadablePartial instant, int[] values) {
   return iField.getMaximumValue(instant, values);
 }
 public int getMaximumValue(long instant) {
   long localInstant = iZone.convertUTCToLocal(instant);
   return iField.getMaximumValue(localInstant);
 }
 public long remainder(long instant) {
   long localInstant = iZone.convertUTCToLocal(instant);
   return iField.remainder(localInstant);
 }
 public boolean isLenient() {
   return iField.isLenient();
 }
 /**
  * Constructor.
  *
  * @param field the base field
  * @param type the field type this field will actually use
  * @throws IllegalArgumentException if wrapped field's minimum value is not zero
  */
 public ZeroIsMaxDateTimeField(DateTimeField field, DateTimeFieldType type) {
   super(field, type);
   if (field.getMinimumValue() != 0) {
     throw new IllegalArgumentException("Wrapped field's minumum value must be zero");
   }
 }
 static int parseDayOfWeek(String str) {
   DateTimeField field = ISOChronology.getInstanceUTC().dayOfWeek();
   return field.get(field.set(0, str, Locale.ENGLISH));
 }
 public int getLeapAmount(long instant) {
   long localInstant = iZone.convertUTCToLocal(instant);
   return iField.getLeapAmount(localInstant);
 }
Пример #21
0
 @Description("week of the year of the given date")
 @ScalarFunction(value = "week", alias = "week_of_year")
 @SqlType(BigintType.class)
 public static long weekFromDate(@SqlType(DateType.class) long date) {
   return WEEK_OF_YEAR.get(date);
 }
 public String getAsShortText(long instant, Locale locale) {
   long localInstant = iZone.convertUTCToLocal(instant);
   return iField.getAsShortText(localInstant, locale);
 }
 public int getMaximumValue() {
   return iField.getMaximumValue();
 }
 public String getAsShortText(int fieldValue, Locale locale) {
   return iField.getAsShortText(fieldValue, locale);
 }
 public int getMaximumValue(ReadablePartial instant) {
   return iField.getMaximumValue(instant);
 }
Пример #26
0
 @Description("quarter of the year of the given date")
 @ScalarFunction("quarter")
 @SqlType(BigintType.class)
 public static long quarterFromDate(@SqlType(DateType.class) long date) {
   return QUARTER.get(date);
 }
 public int getMaximumShortTextLength(Locale locale) {
   return iField.getMaximumShortTextLength(locale);
 }
Пример #28
0
 @Description("year of the given date")
 @ScalarFunction("year")
 @SqlType(BigintType.class)
 public static long yearFromDate(@SqlType(DateType.class) long date) {
   return YEAR.get(date);
 }
Пример #29
0
 /**
  * Validates whether the fields stored in a partial instant are valid.
  *
  * <p>This implementation uses {@link DateTimeField#getMinimumValue(ReadablePartial, int[])} and
  * {@link DateTimeField#getMaximumValue(ReadablePartial, int[])}.
  *
  * @param partial the partial instant to validate
  * @param values the values to validate, not null
  * @throws IllegalArgumentException if the instant is invalid
  */
 public void validate(ReadablePartial partial, int[] values) {
   // check values in standard range, catching really stupid cases like -1
   // this means that the second check will not hit trouble
   int size = partial.size();
   for (int i = 0; i < size; i++) {
     int value = values[i];
     DateTimeField field = partial.getField(i);
     if (value < field.getMinimumValue()) {
       throw new IllegalFieldValueException(
           field.getType(), new Integer(value), new Integer(field.getMinimumValue()), null);
     }
     if (value > field.getMaximumValue()) {
       throw new IllegalFieldValueException(
           field.getType(), new Integer(value), null, new Integer(field.getMaximumValue()));
     }
   }
   // check values in specific range, catching really odd cases like 30th Feb
   for (int i = 0; i < size; i++) {
     int value = values[i];
     DateTimeField field = partial.getField(i);
     if (value < field.getMinimumValue(partial, values)) {
       throw new IllegalFieldValueException(
           field.getType(),
           new Integer(value),
           new Integer(field.getMinimumValue(partial, values)),
           null);
     }
     if (value > field.getMaximumValue(partial, values)) {
       throw new IllegalFieldValueException(
           field.getType(),
           new Integer(value),
           null,
           new Integer(field.getMaximumValue(partial, values)));
     }
   }
 }
Пример #30
0
 @Description("month of the year of the given date")
 @ScalarFunction("month")
 @SqlType(BigintType.class)
 public static long monthFromDate(@SqlType(DateType.class) long date) {
   return MONTH_OF_YEAR.get(date);
 }