コード例 #1
0
ファイル: ExecutionTime.java プロジェクト: kallem/cron-utils
  /**
   * If date is not match, will return previous closest match. If date is match, will return this
   * date.
   *
   * @param date - reference DateTime instance - never null;
   * @return DateTime instance, never null. Value obeys logic specified above.
   * @throws NoSuchValueException
   */
  DateTime previousClosestMatch(DateTime date) throws NoSuchValueException {
    List<Integer> year = yearsValueGenerator.generateCandidates(date.getYear(), date.getYear());
    TimeNode days = generateDays(cronDefinition, date);
    int highestMonth = months.getValues().get(months.getValues().size() - 1);
    int highestDay = days.getValues().get(days.getValues().size() - 1);
    int highestHour = hours.getValues().get(hours.getValues().size() - 1);
    int highestMinute = minutes.getValues().get(minutes.getValues().size() - 1);
    int highestSecond = seconds.getValues().get(seconds.getValues().size() - 1);

    NearestValue nearestValue;
    DateTime newDate;
    if (year.isEmpty()) {
      int previousYear = yearsValueGenerator.generatePreviousValue(date.getYear());
      if (highestDay > 28) {
        int highestDayOfMonth =
            new DateTime(previousYear, highestMonth, 1, 0, 0).dayOfMonth().getMaximumValue();
        if (highestDay > highestDayOfMonth) {
          nearestValue = days.getPreviousValue(highestDay, 1);
          if (nearestValue.getShifts() > 0) {
            newDate =
                new DateTime(previousYear, highestMonth, 1, 23, 59, 59, date.getZone())
                    .minusMonths(nearestValue.getShifts())
                    .dayOfMonth()
                    .withMaximumValue();
            return previousClosestMatch(newDate);
          } else {
            highestDay = nearestValue.getValue();
          }
        }
      }
      return initDateTime(
          previousYear,
          highestMonth,
          highestDay,
          highestHour,
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!months.getValues().contains(date.getMonthOfYear())) {
      nearestValue = months.getPreviousValue(date.getMonthOfYear(), 0);
      int previousMonths = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), 12, 31, 23, 59, 59, date.getZone())
                .minusYears(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          previousMonths,
          highestDay,
          highestHour,
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!days.getValues().contains(date.getDayOfMonth())) {
      nearestValue = days.getPreviousValue(date.getDayOfMonth(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), date.getMonthOfYear(), 1, 23, 59, 59, date.getZone())
                .minusMonths(nearestValue.getShifts())
                .dayOfMonth()
                .withMaximumValue();
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          nearestValue.getValue(),
          highestHour,
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!hours.getValues().contains(date.getHourOfDay())) {
      nearestValue = hours.getPreviousValue(date.getHourOfDay(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    23,
                    59,
                    59,
                    date.getZone())
                .minusDays(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          nearestValue.getValue(),
          highestMinute,
          highestSecond,
          date.getZone());
    }
    if (!minutes.getValues().contains(date.getMinuteOfHour())) {
      nearestValue = minutes.getPreviousValue(date.getMinuteOfHour(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    59,
                    59,
                    date.getZone())
                .minusHours(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          nearestValue.getValue(),
          highestSecond,
          date.getZone());
    }
    if (!seconds.getValues().contains(date.getSecondOfMinute())) {
      nearestValue = seconds.getPreviousValue(date.getSecondOfMinute(), 0);
      int previousSeconds = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    date.getMinuteOfHour(),
                    59,
                    date.getZone())
                .minusMinutes(nearestValue.getShifts());
        return previousClosestMatch(newDate);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          date.getMinuteOfHour(),
          previousSeconds,
          date.getZone());
    }
    return date;
  }
コード例 #2
0
ファイル: ExecutionTime.java プロジェクト: kallem/cron-utils
  /**
   * If date is not match, will return next closest match. If date is match, will return this date.
   *
   * @param date - reference DateTime instance - never null;
   * @return DateTime instance, never null. Value obeys logic specified above.
   * @throws NoSuchValueException
   */
  DateTime nextClosestMatch(DateTime date) throws NoSuchValueException {
    List<Integer> year = yearsValueGenerator.generateCandidates(date.getYear(), date.getYear());
    TimeNode days = null;
    int lowestMonth = months.getValues().get(0);
    int lowestHour = hours.getValues().get(0);
    int lowestMinute = minutes.getValues().get(0);
    int lowestSecond = seconds.getValues().get(0);

    NearestValue nearestValue;
    DateTime newDate;
    if (year.isEmpty()) {
      int newYear = yearsValueGenerator.generateNextValue(date.getYear());
      days = generateDays(cronDefinition, new DateTime(newYear, lowestMonth, 1, 0, 0));
      return initDateTime(
          yearsValueGenerator.generateNextValue(date.getYear()),
          lowestMonth,
          days.getValues().get(0),
          lowestHour,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    if (!months.getValues().contains(date.getMonthOfYear())) {
      nearestValue = months.getNextValue(date.getMonthOfYear(), 0);
      int nextMonths = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), 1, 1, 0, 0, 0, date.getZone())
                .plusYears(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getMonthOfYear()) {
        date = date.plusYears(1);
      }
      days = generateDays(cronDefinition, new DateTime(date.getYear(), nextMonths, 1, 0, 0));
      return initDateTime(
          date.getYear(),
          nextMonths,
          days.getValues().get(0),
          lowestHour,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    days = generateDays(cronDefinition, date);
    if (!days.getValues().contains(date.getDayOfMonth())) {
      nearestValue = days.getNextValue(date.getDayOfMonth(), 0);
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(date.getYear(), date.getMonthOfYear(), 1, 0, 0, 0, date.getZone())
                .plusMonths(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getDayOfMonth()) {
        date = date.plusMonths(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          nearestValue.getValue(),
          lowestHour,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    if (!hours.getValues().contains(date.getHourOfDay())) {
      nearestValue = hours.getNextValue(date.getHourOfDay(), 0);
      int nextHours = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    0,
                    0,
                    0,
                    date.getZone())
                .plusDays(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getHourOfDay()) {
        date = date.plusDays(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          nextHours,
          lowestMinute,
          lowestSecond,
          date.getZone());
    }
    if (!minutes.getValues().contains(date.getMinuteOfHour())) {
      nearestValue = minutes.getNextValue(date.getMinuteOfHour(), 0);
      int nextMinutes = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    0,
                    0,
                    date.getZone())
                .plusHours(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getMinuteOfHour()) {
        date = date.plusHours(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          nextMinutes,
          lowestSecond,
          date.getZone());
    }
    if (!seconds.getValues().contains(date.getSecondOfMinute())) {
      nearestValue = seconds.getNextValue(date.getSecondOfMinute(), 0);
      int nextSeconds = nearestValue.getValue();
      if (nearestValue.getShifts() > 0) {
        newDate =
            new DateTime(
                    date.getYear(),
                    date.getMonthOfYear(),
                    date.getDayOfMonth(),
                    date.getHourOfDay(),
                    date.getMinuteOfHour(),
                    0,
                    date.getZone())
                .plusMinutes(nearestValue.getShifts());
        return nextClosestMatch(newDate);
      }
      if (nearestValue.getValue() < date.getSecondOfMinute()) {
        date = date.plusMinutes(1);
      }
      return initDateTime(
          date.getYear(),
          date.getMonthOfYear(),
          date.getDayOfMonth(),
          date.getHourOfDay(),
          date.getMinuteOfHour(),
          nextSeconds,
          date.getZone());
    }
    return date;
  }