TimeRangeChecker { public boolean evaluate(long timestamp) { if (match(whiteCache, timestamp)) { return true; } if (match(blackCache, timestamp)) { return false; } SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT); fmt.setTimeZone(tz); String date = fmt.format(timestamp); int[] p = parse(date); int[] rl = Arrays.copyOf(p, p.length); int[] ru = Arrays.copyOf(p, p.length); boolean match = match(p, lowerBound, upperBound); for(int j = 0; j != rl.length; ++j) { if (lowerBound[j] != -1) { rl[j] = lowerBound[j]; ru[j] = upperBound[j]; } } if (match) { long ldate = toDate(rl); long udate = toDate(ru); if (ldate == -1 || udate == -1) { return true; } if (udate > ldate) { whiteCache = add(whiteCache, ldate, udate); } else { if (timestamp >= ldate && timestamp >= udate) { udate = adjust(udate, 1); whiteCache = add(whiteCache, ldate, udate); } else { ldate = adjust(ldate, -1); whiteCache = add(whiteCache, ldate, udate); } } return true; } else { long ldate = toDate(rl); long udate = toDate(ru); if (ldate == -1 || udate == -1) { return false; } if (timestamp >= ldate && timestamp >= udate) { ldate = adjust(ldate, 1); blackCache = add(blackCache, udate, ldate); } else { udate = adjust(udate, -1); blackCache = add(blackCache, udate, ldate); } return false; } } TimeRangeChecker(String lower, String upper, TimeZone tz); boolean evaluate(long timestamp);  }
@Test public void verify_month_wrap_range() { TimeRangeChecker checker = new TimeRangeChecker("10_18:10:00", "20_02:20:00", UTC); assertFalse(checker.evaluate(date("2016.02.10_18:09:00"))); assertTrue (checker.evaluate(date("2016.03.10_18:10:00"))); assertTrue (checker.evaluate(date("2016.04.12_10:11:00"))); assertTrue (checker.evaluate(date("2016.05.13_11:18:00"))); assertTrue (checker.evaluate(date("2016.06.14_11:40:00"))); assertTrue (checker.evaluate(date("2016.07.20_02:19:59"))); assertFalse(checker.evaluate(date("2016.08.20_02:20:00"))); assertFalse(checker.evaluate(date("2016.09.20_02:21:00"))); assertFalse(checker.evaluate(date("2016.02.10_18:09:00"))); assertTrue (checker.evaluate(date("2016.03.10_18:10:00"))); assertTrue (checker.evaluate(date("2016.04.12_10:11:00"))); assertTrue (checker.evaluate(date("2016.05.13_11:18:00"))); assertTrue (checker.evaluate(date("2016.06.14_11:40:00"))); assertTrue (checker.evaluate(date("2016.07.20_02:19:59"))); assertFalse(checker.evaluate(date("2016.08.20_02:20:00"))); assertFalse(checker.evaluate(date("2016.09.20_02:21:00"))); }
@Test public void verify_year_wrap_range() { TimeRangeChecker checker = new TimeRangeChecker("02.20_18:10:00", "04.10_02:20:00", UTC); assertFalse(checker.evaluate(date("2016.02.20_18:09:00"))); assertTrue (checker.evaluate(date("2017.02.20_18:10:00"))); assertTrue (checker.evaluate(date("2018.02.25_10:11:00"))); assertTrue (checker.evaluate(date("2019.03.13_11:18:00"))); assertTrue (checker.evaluate(date("2020.03.14_11:40:00"))); assertTrue (checker.evaluate(date("2021.04.10_02:19:59"))); assertFalse(checker.evaluate(date("2022.04.10_02:20:00"))); assertFalse(checker.evaluate(date("2023.09.20_02:21:00"))); assertFalse(checker.evaluate(date("2016.02.20_18:09:00"))); assertTrue (checker.evaluate(date("2017.02.20_18:10:00"))); assertTrue (checker.evaluate(date("2018.02.25_10:11:00"))); assertTrue (checker.evaluate(date("2019.03.13_11:18:00"))); assertTrue (checker.evaluate(date("2020.03.14_11:40:00"))); assertTrue (checker.evaluate(date("2021.04.10_02:19:59"))); assertFalse(checker.evaluate(date("2022.04.10_02:20:00"))); assertFalse(checker.evaluate(date("2023.09.20_02:21:00"))); }
@Test public void verify_year_accurate_range() { TimeRangeChecker checker = new TimeRangeChecker("16.02.20_18:10:00", "16.04.10_02:20:00", UTC); assertFalse(checker.evaluate(date("2015.02.20_18:10:00"))); assertFalse(checker.evaluate(date("2016.02.20_18:09:00"))); assertTrue (checker.evaluate(date("2016.02.20_18:10:00"))); assertTrue (checker.evaluate(date("2016.02.25_10:11:00"))); assertTrue (checker.evaluate(date("2016.03.13_11:18:00"))); assertTrue (checker.evaluate(date("2016.03.14_11:40:00"))); assertTrue (checker.evaluate(date("2016.04.10_02:19:59"))); assertFalse(checker.evaluate(date("2016.04.10_02:20:00"))); assertFalse(checker.evaluate(date("2016.09.20_02:21:00"))); }
@Test public void verify_millenium_accurate_range() { TimeRangeChecker checker = new TimeRangeChecker("2016.02.20_18:10:00", "2016.04.10_02:20:00", UTC); assertFalse(checker.evaluate(date("2015.02.20_18:10:00"))); assertFalse(checker.evaluate(date("2016.02.20_18:09:00"))); assertTrue (checker.evaluate(date("2016.02.20_18:10:00"))); assertTrue (checker.evaluate(date("2016.02.25_10:11:00"))); assertTrue (checker.evaluate(date("2016.03.13_11:18:00"))); assertTrue (checker.evaluate(date("2016.03.14_11:40:00"))); assertTrue (checker.evaluate(date("2016.04.10_02:19:59"))); assertFalse(checker.evaluate(date("2016.04.10_02:20:00"))); assertFalse(checker.evaluate(date("2016.09.20_02:21:00"))); }