如何从持续时间中找到下一个 "available" 时间段间隔?
How can I find the next "available" time slot Interval from a duration?
我有一个 JodaTime
Intervals
的列表。这些 Intervals
包含时间规范来自:
(军用时间)
00:00:00 - 10:00:00
11:30:00 - 15:00:00 (3:00:00 PM)
15:30:00 - 23:59:59 (11:59:59 PM).
我的问题是我想使用来自用户的 Interval
抽象来检测适合用户所需间隔的时间段。
例如,我用这段代码计算:
optimizeIntervals(generateMeetingIntervals());
generateBetweenIntervals();
ArrayList<Interval> during = getMeetingDuringIntervals();
ArrayList<Interval> between = getMeetingBetweenIntervals();
Interval desiredDuration = new Interval(now,now.plusMinutes(requestedDuration));
for(int i = 0; i<between.size();i++){
Interval current = between.get(i);
if(current.getEnd().isAfter(now)){
if (current.contains(testing)){
setNextAvailableStart(now);
setNextAvailableEnd(current.getEnd());
}
}
}
但是我不能使用 Interval
s,因为我需要找到 下一个符合用户标准的可用时间间隔。 如果我将 Interval
用于 desiredDuration
这意味着,如果用户想要一个“15 分钟长”的时间段,我希望算法找到 下一个可用的 15 分钟时间段 。但是,此算法不起作用,因为间隔仅检查 start
和 end
时间是否在包含间隔的 start
和 end
时间之间。
示例:
now = 10:30 AM
desiredDuration = 30 minutes
而desiredDuration
计算为10:30 AM to 11:00 AM
的区间
循环运行:
10:30 AM to 11:00 AM
是否包含在 00:00:00 to 10:00:00
中? - 没有
10:30 AM to 11:00 AM
是否包含在 11:30:00 to 15:00:00
中? - 没有应该是
但是,在第二次检查时,我只想看看在该时间范围内是否有 30 minutes
的持续时间。我该怎么做?
问题是,你用错了class;你想使用 Duration
。您要问的问题是:是否有 30 分钟的时间 window。你不关心什么时候开始,只是 "does it exist?" 所以,使用 Duration
,它没有开始时间或结束时间的概念。
请注意,您正在使用 if(current.getEnd().isAfter(now))
anyway 来回答间隔是否在当前期间之后。
所以,检查每个周期,生成一个可能的拟合,确保它足够长,然后如果可行就保存它。这是一些代码:
Duration desiredDuration = new Duration(requestedDuration);
Interval validDuration = null;
for(int i = 0; i<between.size();i++) {
Interval current = between.get(i);
if(current.getEnd().isAfter(now)) {
Interval candidateDuration = current.withDurationAfterStart(desiredDuration);
if(current.contains(candidateDuration)) {
validDuration = candidateDuration;
break;
}
}
}
正如@durron597 所说,我的问题是使用 Interval
而不是 Duration
。这是我的有效实现:
this.meetingData = testMeetingData;
availableNow = false;
DateTime now = caseToTest;
//Calculate range abstractions and optimize them.
optimizeIntervals(generateMeetingIntervals());
generateBetweenIntervals();
ArrayList<Interval> during = getMeetingDuringIntervals();
ArrayList<Interval> between = getMeetingBetweenIntervals();
Interval testing = new Interval(now, now.plusMinutes(requestedDuration));
if (testMeetingData.size()>0) {
for (int i = 0; i < between.size(); i++) {
Interval current = between.get(i);
if (!current.getEnd().isBefore(now) && !current.getEnd().isEqual(now)) {
if (current.toDuration().isLongerThan(testing.toDuration())) {
setNextAvailableStart(current.getStart());
setNextAvailableEnd(current.getEnd());
if (current.contains(testing)) {
setNextAvailableStart(now);
setAvailableNow(true);
}
break;
}
}
}
}
else{
setNextAvailableStart(new DateTime().withTimeAtStartOfDay());
setNextAvailableEnd(new DateTime().withTime(23,59,59,999));
setAvailableNow(true);
}
我有一个 JodaTime
Intervals
的列表。这些 Intervals
包含时间规范来自:
(军用时间)
00:00:00 - 10:00:00
11:30:00 - 15:00:00 (3:00:00 PM)
15:30:00 - 23:59:59 (11:59:59 PM).
我的问题是我想使用来自用户的 Interval
抽象来检测适合用户所需间隔的时间段。
例如,我用这段代码计算:
optimizeIntervals(generateMeetingIntervals());
generateBetweenIntervals();
ArrayList<Interval> during = getMeetingDuringIntervals();
ArrayList<Interval> between = getMeetingBetweenIntervals();
Interval desiredDuration = new Interval(now,now.plusMinutes(requestedDuration));
for(int i = 0; i<between.size();i++){
Interval current = between.get(i);
if(current.getEnd().isAfter(now)){
if (current.contains(testing)){
setNextAvailableStart(now);
setNextAvailableEnd(current.getEnd());
}
}
}
但是我不能使用 Interval
s,因为我需要找到 下一个符合用户标准的可用时间间隔。 如果我将 Interval
用于 desiredDuration
这意味着,如果用户想要一个“15 分钟长”的时间段,我希望算法找到 下一个可用的 15 分钟时间段 。但是,此算法不起作用,因为间隔仅检查 start
和 end
时间是否在包含间隔的 start
和 end
时间之间。
示例:
now = 10:30 AM
desiredDuration = 30 minutes
而desiredDuration
计算为10:30 AM to 11:00 AM
循环运行:
10:30 AM to 11:00 AM
是否包含在 00:00:00 to 10:00:00
中? - 没有
10:30 AM to 11:00 AM
是否包含在 11:30:00 to 15:00:00
中? - 没有应该是
但是,在第二次检查时,我只想看看在该时间范围内是否有 30 minutes
的持续时间。我该怎么做?
问题是,你用错了class;你想使用 Duration
。您要问的问题是:是否有 30 分钟的时间 window。你不关心什么时候开始,只是 "does it exist?" 所以,使用 Duration
,它没有开始时间或结束时间的概念。
请注意,您正在使用 if(current.getEnd().isAfter(now))
anyway 来回答间隔是否在当前期间之后。
所以,检查每个周期,生成一个可能的拟合,确保它足够长,然后如果可行就保存它。这是一些代码:
Duration desiredDuration = new Duration(requestedDuration);
Interval validDuration = null;
for(int i = 0; i<between.size();i++) {
Interval current = between.get(i);
if(current.getEnd().isAfter(now)) {
Interval candidateDuration = current.withDurationAfterStart(desiredDuration);
if(current.contains(candidateDuration)) {
validDuration = candidateDuration;
break;
}
}
}
正如@durron597 所说,我的问题是使用 Interval
而不是 Duration
。这是我的有效实现:
this.meetingData = testMeetingData;
availableNow = false;
DateTime now = caseToTest;
//Calculate range abstractions and optimize them.
optimizeIntervals(generateMeetingIntervals());
generateBetweenIntervals();
ArrayList<Interval> during = getMeetingDuringIntervals();
ArrayList<Interval> between = getMeetingBetweenIntervals();
Interval testing = new Interval(now, now.plusMinutes(requestedDuration));
if (testMeetingData.size()>0) {
for (int i = 0; i < between.size(); i++) {
Interval current = between.get(i);
if (!current.getEnd().isBefore(now) && !current.getEnd().isEqual(now)) {
if (current.toDuration().isLongerThan(testing.toDuration())) {
setNextAvailableStart(current.getStart());
setNextAvailableEnd(current.getEnd());
if (current.contains(testing)) {
setNextAvailableStart(now);
setAvailableNow(true);
}
break;
}
}
}
}
else{
setNextAvailableStart(new DateTime().withTimeAtStartOfDay());
setNextAvailableEnd(new DateTime().withTime(23,59,59,999));
setAvailableNow(true);
}