default String defaultOffset(Schedule schedule) { return defaultOffset(schedule.wellKnown()); }
private static String cronExpression(Schedule schedule) { switch (schedule.wellKnown()) { case HOURLY: return HOURLY_CRON; case DAILY: return DAILY_CRON; case WEEKLY: return WEEKLY_CRON; case MONTHLY: return MONTHLY_CRON; case YEARLY: return YEARLY_CRON; default: return schedule.expression(); } }
@Override public String toString() { switch (wellKnown()) { case HOURLY: return "HOURS"; case DAILY: return "DAYS"; case WEEKLY: return "WEEKS"; case MONTHLY: return "MONTHS"; case YEARLY: return "YEARS"; default: return expression(); } }
public static Instant parseAlignedInstant(String dateHour, Schedule schedule) { final Schedule.WellKnown wellKnown = schedule.wellKnown(); Matcher matcher; final ZonedDateTime parsed; if (wellKnown == YEARLY && (matcher = YEAR_PATTERN.matcher(dateHour)).matches()) { parsed = LocalDate.of(parseInt(matcher.group(1)), 1, 1).atStartOfDay(UTC); } else if (wellKnown == MONTHLY && (matcher = YEAR_MONTH_PATTERN.matcher(dateHour)).matches()) { parsed = LocalDate.of(parseInt(matcher.group(1)), parseInt(matcher.group(2)), 1).atStartOfDay(UTC); } else { parsed = tryParseDateHour(dateHour); } final Instant instant = parsed.toInstant(); // test alignment if (!TimeUtil.isAligned(instant, schedule)) { return TimeUtil.previousInstant(instant, schedule); } else { return instant; } }
public static Instant offsetInstant(Instant origin, Schedule schedule, int offset) { Preconditions.checkArgument(isAligned(origin, schedule), "unaligned origin"); return schedule.wellKnown().unit() .map(unit -> { try { return origin.plus(offset, unit); } catch (UnsupportedTemporalTypeException ignored) { return null; } }) .orElseGet(() -> { final ExecutionTime executionTime = ExecutionTime.forCron(cron(schedule)); ZonedDateTime time = origin.atZone(UTC); for (int i = 0; i < Math.abs(offset); i++) { final Optional<ZonedDateTime> execution = offset <= 0 ? executionTime.lastExecution(time) : executionTime.nextExecution(time); time = execution .orElseThrow(AssertionError::new); // with unix cron, this should not happen } return time.toInstant(); }); } }
public static String toParameter(Schedule schedule, Instant instant) { switch (schedule.wellKnown()) { case DAILY: case WEEKLY: return formatDate(instant); case HOURLY: return formatDateHour(instant); case MONTHLY: return formatMonth(instant); case YEARLY: return formatYear(instant); default: return formatDateTime(instant); } }