我应该先用代码写出来,因为我认为这可能更直接(或者至少对我来说很熟悉)以毫秒时间戳来考虑。如果我有时间,我仍然可以回去这样做。
在相关说明中,我发现Interval trees 是一本有趣的读物,尽管我没有继续阅读。
我不相信以下是正确的,尤其是我关于如何选择 0200/0600 的日期部分的“规则”。需要磨擦测试,但是。 . .
如果 Sam 开车 20 小时或更长时间进行一次旅行,那么他已经以某种方式触及了时间范围。 (例如,行程 1 和 2)
如果 Sam 每次旅行的时间少于 20 小时,那么我们可以使用他的旅行持续时间来限制我们的可能性范围。
0200/0600 的日期分量是根据开始日期建立的。如果 startDateTime 的时间部分介于 0000 和 0600(含)之间,则 0200/0600 共享 startDateTime 的日期部分,否则为次日。
duration = endDateTime - startDateTime
mustStartTime = 0200 - duration
mustEndTime = 0600 + duration
touched = (startDateTime >= mustStartTime && endDateTime <= mustEndTime)
第三次旅行
- 开始日期时间:2017 年 1 月 6 日 00.00.00
- 结束日期时间:2017 年 1 月 6 日 05.00.00
- 触动:真
duration = 5 hours
mustStartTime = 2100 = 0200 - 5
mustEndTime = 1100 = 0600 + 5
touched = (0000 >= 2100 && 0500 <= 1100) = (true && true)
第四次旅行
- 开始日期时间:06-JAN-2017 06.01.00
- 结束日期时间:2017 年 1 月 6 日 23.00.00
- 触动:错误
duration = 16hr 59min
mustStartTime = 1001 = 0200 - 16 hr 59 min
mustEndTime = 2159 = 0500 + 16 hr 59 min
touched = false = (0601 >= 1001 && 2300 <= 2159) = (false && false)
行程 5
- 开始日期时间:2017 年 1 月 6 日 03.00.00
- EndDateTime : 06-JAN-2017 04.00.00
- 触动:真
duration = 1 hour
mustStartTime = 0100 = 0200 - 1 hour
mustEndTime = 0700 = 0600 + 1 hour
touched = true = (0300 >= 0100 && 0400 <= 0700) = (true && true)
行程 6
- 开始日期时间:2017 年 1 月 6 日 00.00.00
- EndDateTime : 06-JAN-2017 01.30.00
- 触动:错误
duration = 1hr 30 min
mustStartTime = 0030 = 0200 - 1hr 30 min
mustEndTime = 0730 = 0600 + 1hr 30 min
touched = false = (0000 >= 0030 && 0130 <= 0730) = (false && true)
行程 7
- 开始日期时间:2017 年 1 月 6 日 05.00.00
- EndDateTime : 06-JAN-2017 06.00.00
- 触动:真
duration = 1 hour
mustStartTime = 0100 = 0200 - 1 hour
mustEndTime = 0700 = 0600 + 1 hour
touched = (0500 >= 0100 && 0600 <= 0700) = (true && true)
更新
现在有代码和测试。所有的断言都是正确的!绝对感觉像是在重新发明轮子!
public class OverlappingDateRangeUtil {
/**
* 1000 ms * 60 s * 60 m
*/
public static final long MS_IN_AN_HOUR = 1000 * 60 * 60;
public static final long MS_IN_TWO_HOURS = 2 * MS_IN_AN_HOUR;
public static final long MS_IN_SIX_HOURS = 3 * MS_IN_TWO_HOURS;
public static final long MS_IN_TWENTY_HOURS = 20 * MS_IN_AN_HOUR;
private static boolean tripLongerThanTwentyHours(long duration) {
return duration >= MS_IN_TWENTY_HOURS;
}
private static long getTruncDateFor0200And0600(Date start) {
Calendar cal = new GregorianCalendar();
cal.setTime(start);
int startHour = cal.get(Calendar.HOUR);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
boolean after0600 = startHour >=6 && start.getTime() % 60000 > 0;
if(after0600) {
cal.add(Calendar.DATE, 1);
}
return cal.getTimeInMillis();
}
public static boolean dateRangeTouches0200to0600(Date start, Date end) {
boolean toReturn = false;
long duration = end.getTime() - start.getTime();
if(tripLongerThanTwentyHours(duration)) {
toReturn = true;
}
else {
long truncTestDate = getTruncDateFor0200And0600(start);
long oh200 = truncTestDate + MS_IN_TWO_HOURS;
long oh600 = truncTestDate + MS_IN_SIX_HOURS;
long mustStart = oh200 - duration;
long mustEnd = oh600 + duration;
toReturn = start.getTime() >= mustStart && end.getTime() <= mustEnd;
}
return toReturn;
}
}
public class OverlappingDateRangeUtilTest {
private DateFormat dateTimeFormat;
@Before
public void setUp() throws Exception {
dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
}
@Test
public void testDateRangeTouches0200to0600() throws ParseException {
Date trip1Start = dateTimeFormat.parse("01/01/2017 00:15:00");
Date trip1End = dateTimeFormat.parse("01/03/2017 01:45:00");
assertTrue(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip1Start, trip1End));
Date trip2Start = dateTimeFormat.parse("01/04/2017 13:00:00");
Date trip2End = dateTimeFormat.parse("01/05/2017 13:00:00");
assertTrue(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip2Start, trip2End));
Date trip3Start = dateTimeFormat.parse("01/06/2017 00:00:00");
Date trip3End = dateTimeFormat.parse("01/06/2017 05:00:00");
assertTrue(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip3Start, trip3End));
Date trip4Start = dateTimeFormat.parse("01/06/2017 06:01:00");
Date trip4End = dateTimeFormat.parse("01/06/2017 23:00:00");
assertFalse(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip4Start, trip4End));
Date trip5Start = dateTimeFormat.parse("01/06/2017 06:01:00");
Date trip5End = dateTimeFormat.parse("01/06/2017 06:01:00");
assertFalse(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip5Start, trip5End));
Date trip6Start = dateTimeFormat.parse("01/06/2017 04:00:00");
Date trip6End = dateTimeFormat.parse("01/06/2017 04:00:00");
assertTrue(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip6Start, trip6End));
Date trip7Start = dateTimeFormat.parse("01/06/2017 03:00:00");
Date trip7End = dateTimeFormat.parse("01/06/2017 04:00:00");
assertTrue(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip7Start, trip7End));
Date trip8Start = dateTimeFormat.parse("01/06/2017 00:00:00");
Date trip8End = dateTimeFormat.parse("01/06/2017 01:30:00");
assertFalse(OverlappingDateRangeUtil.dateRangeTouches0200to0600(trip8Start, trip8End));
}
}