Example #1
0
 public static long getTimeInterval(EnumQuotaPeriod period, DateTimeZone timezone) {
   DateTime ct = getCurrentWindow(period, timezone);
   DateTime tt = null;
   int y = ct.get(DateTimeFieldType.year());
   int m = ct.get(DateTimeFieldType.monthOfYear());
   int d = ct.get(DateTimeFieldType.dayOfMonth());
   int h = ct.get(DateTimeFieldType.hourOfDay());
   switch (period) {
     case Hourly:
       h++;
       tt = new DateTime(y, m, d, h, 0, timezone);
       break;
     case Daily:
       d++;
       tt = new DateTime(y, m, d, 0, 0, timezone);
       break;
     case Monthly:
       m++;
       tt = new DateTime(y, m, 0, 0, 0, timezone);
       break;
     case Yearly:
       y++;
       tt = new DateTime(y, m, 0, 0, 0, timezone);
       break;
   }
   Interval i = new Interval(ct, tt);
   return i.toDurationMillis();
 }
Example #2
0
  public void init(Directory d) {
    this.directory = d;
    searcher = Searcher.create(directory);
    indexer = new LuceneIndexer();

    DateTime dt = new DateTime();
    int hours = dt.get(DateTimeFieldType.hourOfDay());

    EvaExecutors.getInstance()
        .getScheduler()
        .scheduleAtFixedRate(
            new Runnable() {
              @Override
              public void run() {
                vbotDAOHTMLImplementation.getInstance().flush();
                try {
                  SearchManager.getInstanse().updateIndex(true);
                } catch (IOException e) {
                  log.error(e, e);
                }
              }
            },
            24 - hours,
            24,
            TimeUnit.HOURS);
  }
 // Get a beginning-of-week time for consistent tariff evaluation
 private Instant getNextSunday() {
   Instant result = getNowInstant();
   int hour = result.get(DateTimeFieldType.hourOfDay());
   if (hour > 0) result = result.plus((24 - hour) * TimeService.HOUR);
   int day = result.get(DateTimeFieldType.dayOfWeek());
   result = result.plus((7 - day) * TimeService.DAY);
   return result;
 }
 private Calendar convertDateTimeToCalendar(DateTime dt) {
   Calendar calendar = Calendar.getInstance();
   calendar.clear();
   calendar.set(Calendar.HOUR_OF_DAY, dt.get(DateTimeFieldType.hourOfDay()));
   calendar.set(Calendar.MINUTE, dt.get(DateTimeFieldType.minuteOfHour()));
   calendar.set(Calendar.SECOND, dt.get(DateTimeFieldType.secondOfMinute()));
   return calendar;
 }
 public FlightSchedule(
     DateTime validFromInclusive,
     DateTime validUntilExclusive,
     int flightHourOfDay,
     int flightMinuteOfHour) {
   this.validFromInclusive = validFromInclusive;
   this.validUntilInclusive = validUntilExclusive;
   flightTimeOfDay =
       new Partial()
           .with(DateTimeFieldType.hourOfDay(), 12)
           .with(DateTimeFieldType.minuteOfHour(), 30);
 }
 private Date convertPartialToDate(AbstractPartial partial) {
   if (partial == null) {
     return null;
   }
   Calendar calendar = Calendar.getInstance();
   calendar.clear();
   if (isHour()) {
     calendar.set(Calendar.HOUR_OF_DAY, partial.get(DateTimeFieldType.hourOfDay()));
   }
   if (isMinute()) {
     calendar.set(Calendar.MINUTE, partial.get(DateTimeFieldType.minuteOfHour()));
   }
   return calendar.getTime();
 }
    private Object convertCalendarToPartial(Class type, Calendar calendar) throws Exception {
      if (type.equals(Partial.class)) {
        Partial partial = new Partial();
        if (isHour()) {
          partial = partial.with(DateTimeFieldType.hourOfDay(), calendar.get(Calendar.HOUR_OF_DAY));
        }
        if (isMinute()) {
          partial = partial.with(DateTimeFieldType.minuteOfHour(), calendar.get(Calendar.MINUTE));
        }
        return partial;
      } else {
        // ASSUMPTION
        // assume that we want a subtype of BasePartial and that the
        // subtype implements the factory
        // method fromCalendarField(Calendar calendar)
        Method method = type.getMethod("fromCalendarFields", new Class[] {Calendar.class});

        return method.invoke(null, new Object[] {calendar});
      }
    }
Example #8
0
  public static DateTime getCurrentWindow(
      EnumQuotaPeriod period, long timestamp, DateTimeZone timezone) {
    DateTime ct = new DateTime(timestamp);

    switch (period) {
      case Hourly:
        return new DateTime(
            ct.get(DateTimeFieldType.year()),
            ct.get(DateTimeFieldType.monthOfYear()),
            ct.get(DateTimeFieldType.dayOfMonth()),
            ct.get(DateTimeFieldType.hourOfDay()),
            0,
            timezone);
      case Daily:
        return new DateTime(
            ct.get(DateTimeFieldType.year()),
            ct.get(DateTimeFieldType.monthOfYear()),
            ct.get(DateTimeFieldType.dayOfMonth()),
            0,
            0,
            timezone);
      case Monthly:
        return new DateTime(
            ct.get(DateTimeFieldType.year()),
            ct.get(DateTimeFieldType.monthOfYear()),
            0,
            0,
            0,
            timezone);
      case Yearly:
        return new DateTime(ct.get(DateTimeFieldType.year()), 0, 0, 0, 0, timezone);

      default:
        break;
    }
    return null;
  }
Example #9
0
 /**
  * Get the hour of day (0-23) field for this chronology.
  *
  * @return DateTimeField or UnsupportedDateTimeField if unsupported
  */
 public DateTimeField hourOfDay() {
   return UnsupportedDateTimeField.getInstance(DateTimeFieldType.hourOfDay(), hours());
 }
 // Returns the index into the shift array corresponding to the given time.
 int indexOfShift(Instant time) {
   int hour = time.get(DateTimeFieldType.hourOfDay());
   int day = time.get(DateTimeFieldType.dayOfWeek());
   return hour + (day - 1) * HOURS_DAY;
 }
/**
 * 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;
  }
}