@Test
  public void shouldIgnoreFiresInPastWhenSchedulingCronJob()
      throws InterruptedException, SchedulerException {
    String subject = "cron_ignore_misfire";
    try {
      TestEventListener listener = new TestEventListener();
      eventRegistry.registerListener(listener, subject);

      DateTime now;
      for (now = now();
          now.getSecondOfMinute() > 55 || now.getSecondOfMinute() < 5;
          now = now()) { // we don't want triggers now, only misfires
        Thread.sleep(1000);
      }
      DateTime jobStartTime = now.minusMinutes(3);
      Map<String, Object> params = new HashMap<>();
      params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
      schedulerService.scheduleJob(
          new CronSchedulableJob(
              new MotechEvent(subject, params), "0 0/1 * 1/1 * ? *", jobStartTime, null, true));

      synchronized (listener.getReceivedEvents()) {
        listener.getReceivedEvents().wait(2000);
      }
      assertTrue(listener.getReceivedEvents().size() == 0);
    } finally {
      eventRegistry.clearListenersForBean("test");
      schedulerService.unscheduleAllJobs(subject);
    }
  }
Beispiel #2
0
 private DateTime ensureSameDate(
     DateTime date,
     int years,
     int monthsOfYear,
     int dayOfMonth,
     int hoursOfDay,
     int minutesOfHour,
     int secondsOfMinute,
     DateTimeZone timeZone) {
   if (date.getSecondOfMinute() != secondsOfMinute) {
     date = date.plusSeconds(secondsOfMinute - date.getSecondOfMinute());
   }
   if (date.getMinuteOfHour() != minutesOfHour) {
     date = date.plusMinutes(minutesOfHour - date.getMinuteOfHour());
   }
   if (date.getHourOfDay() != hoursOfDay) {
     date = date.plusHours(hoursOfDay - date.getHourOfDay());
   }
   if (date.getDayOfMonth() != dayOfMonth) {
     date = date.plusDays(dayOfMonth - date.getDayOfMonth());
   }
   if (date.getMonthOfYear() != monthsOfYear) {
     date = date.plusMonths(monthsOfYear - date.getMonthOfYear());
   }
   if (date.getYear() != years) {
     date = date.plusYears(years - date.getYear());
   }
   return date;
 }
 private DateTime findSuitableTimeToScheduleWithSafeBufferFromTriggerTime()
     throws InterruptedException {
   DateTime now;
   for (now = now(); now.getSecondOfMinute() >= 50 || now.getSecondOfMinute() <= 10; now = now()) {
     Thread.sleep(1000);
   }
   return now;
 }
    @Specialization
    public RubyBasicObject timeDecompose(VirtualFrame frame, RubyTime time) {
      CompilerDirectives.transferToInterpreter();
      final DateTime dateTime = time.getDateTime();
      final int sec = dateTime.getSecondOfMinute();
      final int min = dateTime.getMinuteOfHour();
      final int hour = dateTime.getHourOfDay();
      final int day = dateTime.getDayOfMonth();
      final int month = dateTime.getMonthOfYear();
      final int year = dateTime.getYear();

      int wday = dateTime.getDayOfWeek();

      if (wday == 7) {
        wday = 0;
      }

      final int yday = dateTime.getDayOfYear();
      final boolean isdst = false;

      final String envTimeZoneString = readTimeZoneNode.executeRubyString(frame).toString();
      String zoneString = org.jruby.RubyTime.zoneHelper(envTimeZoneString, dateTime, false);
      Object zone;
      if (zoneString.matches(".*-\\d+")) {
        zone = nil();
      } else {
        zone = createString(zoneString);
      }

      final Object[] decomposed =
          new Object[] {sec, min, hour, day, month, year, wday, yday, isdst, zone};
      return createArray(decomposed, decomposed.length);
    }
Beispiel #5
0
  public RubyObject mdump() {
    RubyTime obj = this;
    DateTime dateTime = obj.dt.toDateTime(DateTimeZone.UTC);
    byte dumpValue[] = new byte[8];

    int pe =
        0x1 << 31
            | ((obj.gmt().isTrue()) ? 0x1 : 0x0) << 30
            | (dateTime.getYear() - 1900) << 14
            | (dateTime.getMonthOfYear() - 1) << 10
            | dateTime.getDayOfMonth() << 5
            | dateTime.getHourOfDay();
    int se =
        dateTime.getMinuteOfHour() << 26
            | dateTime.getSecondOfMinute() << 20
            | (dateTime.getMillisOfSecond() * 1000 + (int) usec); // dump usec, not msec

    for (int i = 0; i < 4; i++) {
      dumpValue[i] = (byte) (pe & 0xFF);
      pe >>>= 8;
    }
    for (int i = 4; i < 8; i++) {
      dumpValue[i] = (byte) (se & 0xFF);
      se >>>= 8;
    }
    return RubyString.newString(obj.getRuntime(), new ByteList(dumpValue));
  }
  private void printStat(LiveStatistics ls) {
    long ms = ls.getTimeperiod() * 15000;
    DateTime time = new DateTime(ms);

    StringBuilder sb = new StringBuilder();
    sb.append(time.getDayOfMonth())
        .append(".")
        .append(time.getMonthOfYear())
        .append(".")
        .append(time.getYear());
    sb.append(" ")
        .append(time.getHourOfDay())
        .append(":")
        .append(time.getMinuteOfHour())
        .append(":")
        .append(time.getSecondOfMinute());

    System.out.println(
        ls.getTimeperiod()
            + ";"
            + ls.getAccountName()
            + "; date: "
            + sb.toString()
            + " value: "
            + ls.getValue()
            + " unitType: "
            + ls.getUnitType()
            + " valueType: "
            + ls.getValueType());
  }
 public SUTime.Temporal apply(String text) {
   // TODO: TIMEZONE?
   DateTime dateTime = null;
   try {
     dateTime = formatter.parseDateTime(text);
   } catch (org.joda.time.IllegalFieldValueException e) {
     logger.warning(
         "WARNING: Invalid temporal \""
             + text
             + "\" ("
             + e.getMessage()
             + "). Skipping and continuing...");
     return null;
   }
   assert (dateTime != null);
   if (hasDate && hasTime) {
     return new SUTime.GroundedTime(dateTime);
     //        return new SUTime.IsoDateTime( new SUTime.IsoTime(dateTime.getHourOfDay(),
     // dateTime.getMinuteOfHour(), dateTime.getSecondOfMinute());
     //        Date d = new SUTime.IsoDate(dateTime.getYear(), dateTime.getMonthOfYear(),
     // dateTime.getDayOfMonth()) );
   } else if (hasTime) {
     // TODO: Millisecs?
     return new SUTime.IsoTime(
         dateTime.getHourOfDay(), dateTime.getMinuteOfHour(), dateTime.getSecondOfMinute());
   } else if (hasDate) {
     return new SUTime.IsoDate(
         dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth());
   } else {
     return null;
   }
 }
 private DateTime truncateToSeconds(DateTime dateTime) {
   return new DateTime(
       dateTime.getYear(),
       dateTime.getMonthOfYear(),
       dateTime.getDayOfMonth(),
       dateTime.getHourOfDay(),
       dateTime.getMinuteOfHour(),
       dateTime.getSecondOfMinute());
 }
 public void testCreateNewUtcDate() {
   DateTime utc = now.toDateTime(DateTimeZone.UTC);
   Date actual =
       newDateUtc(
           utc.getYear(),
           utc.getMonthOfYear(),
           utc.getDayOfMonth(),
           utc.getHourOfDay(),
           utc.getMinuteOfHour(),
           utc.getSecondOfMinute());
   assertEquals(utc.getMillis(), actual.getTime());
 }
Beispiel #10
0
 private static LocalDateTime parseJavaLocalDateTimeUI(String str) {
   // First parse to Joda DateTime for timezone calculation
   DateTime dateTime = DTF.parseDateTime(str);
   // Then create LocalDateTime from Joda DateTime and return it
   return LocalDateTime.of(
       dateTime.getYear(),
       dateTime.getMonthOfYear(),
       dateTime.getDayOfMonth(),
       dateTime.getHourOfDay(),
       dateTime.getMinuteOfHour(),
       dateTime.getSecondOfMinute());
 }
  @Test
  public void shouldGetPreviousFireTime() throws InterruptedException {
    try {
      fakeNow(new DateTime());

      Map<String, Object> params = new HashMap<>();
      MotechEvent event = new MotechEvent("test_event", params);
      final String jobId = id("jobId");
      params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
      DateTime now = new DateTime();
      StringBuilder cron = new StringBuilder();
      cron.append(now.getSecondOfMinute()).append(" ").append(now.getMinuteOfHour()).append(" ");
      cron.append(now.getHourOfDay()).append(" * * ?");
      schedulerService.scheduleJob(new CronSchedulableJob(event, cron.toString()));
      Thread.sleep(1000);
      DateTime dateTime = schedulerService.getPreviousFireDate(new CronJobId(event));
      assertEquals(dateTime.getHourOfDay(), now.getHourOfDay());
      assertEquals(dateTime.getMinuteOfHour(), now.getMinuteOfHour());
      assertEquals(dateTime.getSecondOfMinute(), now.getSecondOfMinute());
    } finally {
      stopFakingTime();
    }
  }
 /**
  * Test for issue #9 https://github.com/jmrozanec/cron-utils/issues/9 Reported case: If you write
  * a cron expression that contains a month or day of week, nextExection() ignores it. Expected:
  * should not ignore month or day of week field
  */
 @Test
 public void testDoesNotIgnoreMonthOrDayOfWeek() {
   CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
   CronParser cronParser = new CronParser(cronDefinition);
   // seconds, minutes, hours, dayOfMonth, month, dayOfWeek
   ExecutionTime executionTime = ExecutionTime.forCron(cronParser.parse("0 11 11 11 11 ?"));
   DateTime now = new DateTime(2015, 4, 15, 0, 0, 0);
   DateTime whenToExecuteNext = executionTime.nextExecution(now);
   assertEquals(2015, whenToExecuteNext.getYear());
   assertEquals(11, whenToExecuteNext.getMonthOfYear());
   assertEquals(11, whenToExecuteNext.getDayOfMonth());
   assertEquals(11, whenToExecuteNext.getHourOfDay());
   assertEquals(11, whenToExecuteNext.getMinuteOfHour());
   assertEquals(0, whenToExecuteNext.getSecondOfMinute());
 }
Beispiel #13
0
  @Test(groups = "fast")
  public void testBasicClockOperations() throws Exception {
    final ClockMock clock = new ClockMock();

    final DateTime startingTime = new DateTime(DateTimeZone.UTC);
    // Lame, but required due to the truncation magic
    Awaitility.await()
        .atMost(1001, MILLISECONDS)
        .until(
            new Callable<Boolean>() {
              @Override
              public Boolean call() throws Exception {
                return clock.getUTCNow().isAfter(startingTime);
              }
            });

    clock.setTime(new DateTime(2012, 5, 1, 1, 2, 3, DateTimeZone.UTC));
    Assert.assertEquals(clock.getUTCToday(), new LocalDate(2012, 5, 1));
    final DateTime utcNowAfterSetTime = clock.getUTCNow();
    Assert.assertEquals(utcNowAfterSetTime.getHourOfDay(), 1);
    Assert.assertEquals(utcNowAfterSetTime.getMinuteOfHour(), 2);
    Assert.assertEquals(utcNowAfterSetTime.getSecondOfMinute(), 3);

    clock.addDays(1);
    Assert.assertEquals(clock.getUTCToday(), new LocalDate(2012, 5, 2));

    clock.addMonths(1);
    Assert.assertEquals(clock.getUTCToday(), new LocalDate(2012, 6, 2));

    clock.addYears(1);
    Assert.assertEquals(clock.getUTCToday(), new LocalDate(2013, 6, 2));

    clock.setDay(new LocalDate(2045, 12, 12));
    Assert.assertEquals(clock.getUTCToday(), new LocalDate(2045, 12, 12));

    clock.resetDeltaFromReality();
    Assert.assertTrue(clock.getUTCNow().isAfter(startingTime));
    Assert.assertTrue(clock.getUTCNow().isBefore(startingTime.plusMinutes(1)));
  }
Beispiel #14
0
 public void testCurrentDate() {
   DateTime dateTime = new DateTime(DateUtils.currentDate());
   Assert.assertEquals(0, dateTime.getHourOfDay());
   Assert.assertEquals(0, dateTime.getMinuteOfHour());
   Assert.assertEquals(0, dateTime.getSecondOfMinute());
 }
Beispiel #15
0
  /**
   * 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;
  }
Beispiel #16
0
 @JRubyMethod(name = "sec")
 public RubyInteger sec() {
   return getRuntime().newFixnum(dt.getSecondOfMinute());
 }
Beispiel #17
0
  /**
   * 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;
  }