@Test public void testTimeWindowPredicateInUtc() { TimeWindow timeWindow = TimeWindow.newBuilder() .withDays(Day.Monday) .withwithHourlyTimeWindows(8, 16) .build(); Supplier<Boolean> predicate = TimeWindowFunctions.isInTimeWindowPredicate(titusRuntime, timeWindow); clock.jumpForwardTo(DayOfWeek.MONDAY).resetTime(7, 0, 0); assertThat(predicate.get()).isFalse(); clock.resetTime(11, 0, 0); assertThat(predicate.get()).isTrue(); clock.resetTime(17, 0, 0); assertThat(predicate.get()).isFalse(); }
public static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, TimeWindow timeWindow) { if (isEmpty(timeWindow)) { return () -> true; } List<Function<DayOfWeek, Boolean>> dayPredicates = new ArrayList<>(); timeWindow.getDays().forEach(day -> dayPredicates.add(buildDayPredicate(day))); List<Function<Integer, Boolean>> hourPredicates = new ArrayList<>(); timeWindow.getHourlyTimeWindows().forEach(h -> hourPredicates.add(buildHourlyTimeWindows(h))); Function<DayOfWeek, Boolean> combinedDayPredicate = dayPredicates.isEmpty() ? day -> true : oneOf(dayPredicates); Function<Integer, Boolean> combinedHourPredicate = hourPredicates.isEmpty() ? hour -> true : oneOf(hourPredicates); ZoneId zoneId; try { zoneId = DateTimeExt.toZoneId(timeWindow.getTimeZone()); } catch (Exception e) { titusRuntime.getCodeInvariants().unexpectedError("Unrecognized time zone (data not properly validated)", e); return () -> false; } return () -> { ZonedDateTime dateTime = Instant.ofEpochMilli(titusRuntime.getClock().wallTime()).atZone(zoneId); return combinedDayPredicate.apply(dateTime.getDayOfWeek()) && combinedHourPredicate.apply(dateTime.getHour()); }; }
TimeWindowFunctions { public static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, TimeWindow timeWindow) { if (isEmpty(timeWindow)) { return () -> true; } List<Function<DayOfWeek, Boolean>> dayPredicates = new ArrayList<>(); timeWindow.getDays().forEach(day -> dayPredicates.add(buildDayPredicate(day))); List<Function<Integer, Boolean>> hourPredicates = new ArrayList<>(); timeWindow.getHourlyTimeWindows().forEach(h -> hourPredicates.add(buildHourlyTimeWindows(h))); Function<DayOfWeek, Boolean> combinedDayPredicate = dayPredicates.isEmpty() ? day -> true : oneOf(dayPredicates); Function<Integer, Boolean> combinedHourPredicate = hourPredicates.isEmpty() ? hour -> true : oneOf(hourPredicates); ZoneId zoneId; try { zoneId = DateTimeExt.toZoneId(timeWindow.getTimeZone()); } catch (Exception e) { titusRuntime.getCodeInvariants().unexpectedError("Unrecognized time zone (data not properly validated)", e); return () -> false; } return () -> { ZonedDateTime dateTime = Instant.ofEpochMilli(titusRuntime.getClock().wallTime()).atZone(zoneId); return combinedDayPredicate.apply(dateTime.getDayOfWeek()) && combinedHourPredicate.apply(dateTime.getHour()); }; } }
TimeWindowFunctions { public static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, TimeWindow timeWindow) { if (isEmpty(timeWindow)) { return () -> true; } List<Function<DayOfWeek, Boolean>> dayPredicates = new ArrayList<>(); timeWindow.getDays().forEach(day -> dayPredicates.add(buildDayPredicate(day))); List<Function<Integer, Boolean>> hourPredicates = new ArrayList<>(); timeWindow.getHourlyTimeWindows().forEach(h -> hourPredicates.add(buildHourlyTimeWindows(h))); Function<DayOfWeek, Boolean> combinedDayPredicate = dayPredicates.isEmpty() ? day -> true : oneOf(dayPredicates); Function<Integer, Boolean> combinedHourPredicate = hourPredicates.isEmpty() ? hour -> true : oneOf(hourPredicates); ZoneId zoneId; try { zoneId = DateTimeExt.toZoneId(timeWindow.getTimeZone()); } catch (Exception e) { titusRuntime.getCodeInvariants().unexpectedError("Unrecognized time zone (data not properly validated)", e); return () -> false; } return () -> { ZonedDateTime dateTime = Instant.ofEpochMilli(titusRuntime.getClock().wallTime()).atZone(zoneId); return combinedDayPredicate.apply(dateTime.getDayOfWeek()) && combinedHourPredicate.apply(dateTime.getHour()); }; }  }
TimeWindowFunctions { public static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, TimeWindow timeWindow) { if (isEmpty(timeWindow)) { return () -> true; } List<Function<DayOfWeek, Boolean>> dayPredicates = new ArrayList<>(); timeWindow.getDays().forEach(day -> dayPredicates.add(buildDayPredicate(day))); List<Function<Integer, Boolean>> hourPredicates = new ArrayList<>(); timeWindow.getHourlyTimeWindows().forEach(h -> hourPredicates.add(buildHourlyTimeWindows(h))); Function<DayOfWeek, Boolean> combinedDayPredicate = dayPredicates.isEmpty() ? day -> true : oneOf(dayPredicates); Function<Integer, Boolean> combinedHourPredicate = hourPredicates.isEmpty() ? hour -> true : oneOf(hourPredicates); ZoneId zoneId; try { zoneId = DateTimeExt.toZoneId(timeWindow.getTimeZone()); } catch (Exception e) { titusRuntime.getCodeInvariants().unexpectedError("Unrecognized time zone (data not properly validated)", e); return () -> false; } return () -> { ZonedDateTime dateTime = Instant.ofEpochMilli(titusRuntime.getClock().wallTime()).atZone(zoneId); return combinedDayPredicate.apply(dateTime.getDayOfWeek()) && combinedHourPredicate.apply(dateTime.getHour()); }; }  static boolean isEmpty(TimeWindow timeWindow); static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, TimeWindow timeWindow); static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, Collection<TimeWindow> timeWindows); }
TimeWindowFunctions { public static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, TimeWindow timeWindow) { if (isEmpty(timeWindow)) { return () -> true; } List<Function<DayOfWeek, Boolean>> dayPredicates = new ArrayList<>(); timeWindow.getDays().forEach(day -> dayPredicates.add(buildDayPredicate(day))); List<Function<Integer, Boolean>> hourPredicates = new ArrayList<>(); timeWindow.getHourlyTimeWindows().forEach(h -> hourPredicates.add(buildHourlyTimeWindows(h))); Function<DayOfWeek, Boolean> combinedDayPredicate = dayPredicates.isEmpty() ? day -> true : oneOf(dayPredicates); Function<Integer, Boolean> combinedHourPredicate = hourPredicates.isEmpty() ? hour -> true : oneOf(hourPredicates); ZoneId zoneId; try { zoneId = DateTimeExt.toZoneId(timeWindow.getTimeZone()); } catch (Exception e) { titusRuntime.getCodeInvariants().unexpectedError("Unrecognized time zone (data not properly validated)", e); return () -> false; } return () -> { ZonedDateTime dateTime = Instant.ofEpochMilli(titusRuntime.getClock().wallTime()).atZone(zoneId); return combinedDayPredicate.apply(dateTime.getDayOfWeek()) && combinedHourPredicate.apply(dateTime.getHour()); }; }  static boolean isEmpty(TimeWindow timeWindow); static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, TimeWindow timeWindow); static Supplier<Boolean> isInTimeWindowPredicate(TitusRuntime titusRuntime, Collection<TimeWindow> timeWindows);  }