ZonedDurationField(DurationField field, DateTimeZone zone) {
   super(field.getType());
   if (!field.isSupported()) {
     throw new IllegalArgumentException();
   }
   iField = field;
   iTimeField = useTimeArithmetic(field);
   iZone = zone;
 }
Пример #2
0
 /**
  * Gets the values of a period from an interval.
  *
  * @param period the period instant to use
  * @param startInstant the start instant of an interval to query
  * @param endInstant the start instant of an interval to query
  * @return the values of the period extracted from the interval
  */
 public int[] get(ReadablePeriod period, long startInstant, long endInstant) {
   int size = period.size();
   int[] values = new int[size];
   if (startInstant != endInstant) {
     for (int i = 0; i < size; i++) {
       DurationField field = period.getFieldType(i).getField(this);
       int value = field.getDifference(endInstant, startInstant);
       startInstant = field.add(startInstant, value);
       values[i] = value;
     }
   }
   return values;
 }
Пример #3
0
 /**
  * Gets the values of a period from an interval.
  *
  * @param period the period instant to use
  * @param duration the duration to query
  * @return the values of the period extracted from the duration
  */
 public int[] get(ReadablePeriod period, long duration) {
   int size = period.size();
   int[] values = new int[size];
   if (duration != 0) {
     long current = 0;
     for (int i = 0; i < size; i++) {
       DurationField field = period.getFieldType(i).getField(this);
       if (field.isPrecise()) {
         int value = field.getDifference(duration, current);
         current = field.add(current, value);
         values[i] = value;
       }
     }
   }
   return values;
 }
  @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);
  }
  /** @param daysInYear The number of days in each year */
  protected FixedYearLengthChronology(int daysInYear) {
    this.daysInYear = daysInYear;

    this.yearDuration =
        new PreciseDurationField(
            DurationFieldType.years(), daysInYear * dayDuration.getUnitMillis());
    this.centuryDuration =
        new PreciseDurationField(DurationFieldType.centuries(), 100 * yearDuration.getUnitMillis());

    this.dayOfYear =
        new OneBasedPreciseDateTimeField(
            DateTimeFieldType.dayOfYear(), dayDuration, this.yearDuration);
    this.yearOfCentury =
        new PreciseDateTimeField(
            DateTimeFieldType.yearOfCentury(), this.yearDuration, this.centuryDuration);
    this.year = new YearField(this.yearDuration);
  }
Пример #6
0
  private void testAdd(String start, DurationFieldType type, int amt, String end) {
    DateTime dtStart = new DateTime(start, GJChronology.getInstance(DateTimeZone.UTC));
    DateTime dtEnd = new DateTime(end, GJChronology.getInstance(DateTimeZone.UTC));
    assertEquals(dtEnd, dtStart.withFieldAdded(type, amt));
    assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt));

    DurationField field = type.getField(GJChronology.getInstance(DateTimeZone.UTC));
    int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis());
    assertEquals(amt, diff);

    if (type == DurationFieldType.years()
        || type == DurationFieldType.months()
        || type == DurationFieldType.days()) {
      YearMonthDay ymdStart = new YearMonthDay(start, GJChronology.getInstance(DateTimeZone.UTC));
      YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology.getInstance(DateTimeZone.UTC));
      assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt));
      assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt));
    }
  }
 private DurationField convertField(DurationField field, HashMap<Object, Object> converted) {
   if (field == null || !field.isSupported()) {
     return field;
   }
   if (converted.containsKey(field)) {
     return (DurationField) converted.get(field);
   }
   ZonedDurationField zonedField = new ZonedDurationField(field, getZone());
   converted.put(field, zonedField);
   return zonedField;
 }
 static boolean useTimeArithmetic(DurationField field) {
   // Use time of day arithmetic rules for unit durations less than
   // typical time zone offsets.
   return field != null && field.getUnitMillis() < DateTimeConstants.MILLIS_PER_HOUR * 12;
 }
 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) {
   int offset = getOffsetToAdd(subtrahendInstant);
   return iField.getDifferenceAsLong(
       minuendInstant + (iTimeField ? offset : getOffsetToAdd(minuendInstant)),
       subtrahendInstant + offset);
 }
 public long add(long instant, long value) {
   int offset = getOffsetToAdd(instant);
   instant = iField.add(instant + offset, value);
   return instant - (iTimeField ? offset : getOffsetFromLocalToSubtract(instant));
 }
 public long getMillis(long value, long instant) {
   return iField.getMillis(value, addOffset(instant));
 }
 public long getValueAsLong(long duration, long instant) {
   return iField.getValueAsLong(duration, addOffset(instant));
 }
 public int getValue(long duration, long instant) {
   return iField.getValue(duration, addOffset(instant));
 }
 public long getUnitMillis() {
   return iField.getUnitMillis();
 }
 public boolean isPrecise() {
   return iTimeField ? iField.isPrecise() : iField.isPrecise() && this.iZone.isFixed();
 }
Пример #16
0
/**
 * A Chronology in which every year has the same number of days. Such calendar systems are used in
 * many climate simulations.
 *
 * <p>In these Chronologies, a millisecond instant of zero corresponds with 1970-01-01T00:00:00.000Z
 * and a year has a fixed number of milliseconds.
 *
 * <p>There is no concept of an era in these Chronologies, so all durations and fields relating to
 * this concept are not supported. Additionally, the concept of a "weekyear" (the year that "owns" a
 * given week) is not implemented.
 *
 * <p>Instances of this class can only be created in {@link DateTimeZone#UTC}. (Support for time
 * zones makes little sense in this chronology).
 *
 * <p>Instances of this class are immutable.
 *
 * <p><i>Note: Much of this code was copied from the package-private BasicChronology.</i>
 *
 * @author Jon Blower
 * @see "http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/cf-conventions.html#calendar"
 */
abstract class FixedYearLengthChronology extends BaseChronology {

  ///// DURATIONS /////

  private static final DurationField millisecondDuration = MillisDurationField.INSTANCE;

  private static final DurationField secondDuration =
      new PreciseDurationField(DurationFieldType.seconds(), DateTimeConstants.MILLIS_PER_SECOND);

  private static final DurationField minuteDuration =
      new PreciseDurationField(DurationFieldType.minutes(), DateTimeConstants.MILLIS_PER_MINUTE);

  private static final DurationField hourDuration =
      new PreciseDurationField(DurationFieldType.hours(), DateTimeConstants.MILLIS_PER_HOUR);

  private static final DurationField halfdayDuration =
      new PreciseDurationField(
          DurationFieldType.halfdays(), 12 * DateTimeConstants.MILLIS_PER_HOUR);

  private static final DurationField dayDuration =
      new PreciseDurationField(DurationFieldType.days(), 2 * halfdayDuration.getUnitMillis());

  private static final DurationField weekDuration =
      new PreciseDurationField(DurationFieldType.weeks(), 7 * dayDuration.getUnitMillis());

  // We don't know the length of the year or century until we know how many
  // days there are in a year
  private final DurationField yearDuration;
  private final DurationField centuryDuration;

  ///// DATE-TIME FIELDS /////

  private static final DateTimeField millisOfSecond =
      new PreciseDateTimeField(
          DateTimeFieldType.millisOfSecond(), millisecondDuration, secondDuration);

  private static final DateTimeField millisOfDay =
      new PreciseDateTimeField(DateTimeFieldType.millisOfDay(), millisecondDuration, dayDuration);

  private static final DateTimeField secondOfMinute =
      new PreciseDateTimeField(DateTimeFieldType.secondOfMinute(), secondDuration, minuteDuration);

  private static final DateTimeField secondOfDay =
      new PreciseDateTimeField(DateTimeFieldType.secondOfDay(), secondDuration, dayDuration);

  private static final DateTimeField minuteOfHour =
      new PreciseDateTimeField(DateTimeFieldType.minuteOfHour(), minuteDuration, hourDuration);

  private static final DateTimeField minuteOfDay =
      new PreciseDateTimeField(DateTimeFieldType.minuteOfDay(), minuteDuration, dayDuration);

  private static final DateTimeField hourOfDay =
      new PreciseDateTimeField(DateTimeFieldType.hourOfDay(), hourDuration, dayDuration);

  private static final DateTimeField hourOfHalfday =
      new PreciseDateTimeField(DateTimeFieldType.hourOfHalfday(), hourDuration, halfdayDuration);

  private static final DateTimeField halfdayOfDay =
      new PreciseDateTimeField(DateTimeFieldType.halfdayOfDay(), halfdayDuration, dayDuration);

  private static final DateTimeField clockhourOfDay =
      new ZeroIsMaxDateTimeField(hourOfDay, DateTimeFieldType.clockhourOfDay());

  private static final DateTimeField clockhourOfHalfday =
      new ZeroIsMaxDateTimeField(hourOfHalfday, DateTimeFieldType.clockhourOfHalfday());

  private static final DateTimeField dayOfWeek =
      new PreciseDateTimeField(DateTimeFieldType.dayOfWeek(), dayDuration, weekDuration);

  // We don't know the length of the year or century until we know how many
  // days there are in a year
  private final DateTimeField dayOfYear;
  private final DateTimeField yearOfCentury;
  private final DateTimeField year;

  private final int daysInYear;

  private static final class YearField extends PreciseDurationDateTimeField {

    public YearField(DurationField yearDuration) {
      super(DateTimeFieldType.year(), yearDuration);
    }

    @Override
    public int get(long instant) {
      // We need to use Math.floor() to deal with negative instants
      return (int) Math.floor(instant * 1.0 / this.getDurationField().getUnitMillis()) + 1970;
    }

    /** Returns null: the field has no range */
    @Override
    public DurationField getRangeDurationField() {
      return null;
    }

    @Override
    public int getMinimumValue() {
      return this.get(Long.MIN_VALUE);
    }

    @Override
    // We subtract one to ensure that the whole of this year can be
    // encoded
    public int getMaximumValue() {
      return this.get(Long.MAX_VALUE) - 1;
    }
  };

  ///// CONSTRUCTORS AND FACTORIES /////

  /** @param daysInYear The number of days in each year */
  protected FixedYearLengthChronology(int daysInYear) {
    this.daysInYear = daysInYear;

    this.yearDuration =
        new PreciseDurationField(
            DurationFieldType.years(), daysInYear * dayDuration.getUnitMillis());
    this.centuryDuration =
        new PreciseDurationField(DurationFieldType.centuries(), 100 * yearDuration.getUnitMillis());

    this.dayOfYear =
        new OneBasedPreciseDateTimeField(
            DateTimeFieldType.dayOfYear(), dayDuration, this.yearDuration);
    this.yearOfCentury =
        new PreciseDateTimeField(
            DateTimeFieldType.yearOfCentury(), this.yearDuration, this.centuryDuration);
    this.year = new YearField(this.yearDuration);
  }

  ///// DURATION ACCESSORS /////

  @Override
  public final DurationField millis() {
    return millisecondDuration;
  }

  @Override
  public final DurationField seconds() {
    return secondDuration;
  }

  @Override
  public final DurationField minutes() {
    return minuteDuration;
  }

  @Override
  public final DurationField hours() {
    return hourDuration;
  }

  @Override
  public final DurationField halfdays() {
    return halfdayDuration;
  }

  /** Each day has exactly the same length: there is no daylight saving */
  @Override
  public final DurationField days() {
    return dayDuration;
  }

  /** Each week has 7 days */
  @Override
  public final DurationField weeks() {
    return weekDuration;
  }

  @Override
  public abstract DurationField months();

  @Override
  public final DurationField years() {
    return this.yearDuration;
  }

  @Override
  public final DurationField centuries() {
    return this.centuryDuration;
  }

  ///// DATE-TIME FIELD ACCESSORS /////

  @Override
  public final DateTimeField millisOfSecond() {
    return millisOfSecond;
  }

  @Override
  public final DateTimeField millisOfDay() {
    return millisOfDay;
  }

  @Override
  public final DateTimeField secondOfMinute() {
    return secondOfMinute;
  }

  @Override
  public final DateTimeField secondOfDay() {
    return secondOfDay;
  }

  @Override
  public final DateTimeField minuteOfHour() {
    return minuteOfHour;
  }

  @Override
  public final DateTimeField minuteOfDay() {
    return minuteOfDay;
  }

  @Override
  public final DateTimeField hourOfDay() {
    return hourOfDay;
  }

  @Override
  public final DateTimeField hourOfHalfday() {
    return hourOfHalfday;
  }

  @Override
  public final DateTimeField halfdayOfDay() {
    return halfdayOfDay;
  }

  @Override
  public final DateTimeField clockhourOfDay() {
    return clockhourOfDay;
  }

  @Override
  public final DateTimeField clockhourOfHalfday() {
    return clockhourOfHalfday;
  }

  @Override
  public final DateTimeField dayOfWeek() {
    return dayOfWeek;
  }

  @Override
  public abstract DateTimeField dayOfMonth();

  @Override
  public final DateTimeField dayOfYear() {
    return dayOfYear;
  }

  @Override
  public abstract DateTimeField monthOfYear();

  @Override
  public final DateTimeField year() {
    return year;
  }

  @Override
  public final DateTimeField yearOfCentury() {
    return yearOfCentury;
  }

  /** Returns the number of days in the year */
  final int getDaysInYear() {
    return this.daysInYear;
  }

  /** Always returns UTC */
  @Override
  public final DateTimeZone getZone() {
    return DateTimeZone.UTC;
  }

  /** Throws UnsupportedOperationException unless the time zone is UTC */
  @Override
  public final Chronology withZone(DateTimeZone zone) {
    if (zone.equals(DateTimeZone.UTC)) return this.withUTC();
    throw new UnsupportedOperationException("Not supported yet.");
  }

  /** Returns this object */
  @Override
  public final Chronology withUTC() {
    return this;
  }
}