【问题标题】:How to calculate distance between time excluding chunks of time efficiently?如何有效地计算时间之间的距离,不包括时间块?
【发布时间】:2018-08-09 20:01:07
【问题描述】:

我正在尝试计算两个日期之间的分钟数,同时排除任意定义且每周发生的时间段。我还需要能够计算反向,在给定时间的情况下,向前计算 X 分钟数,不包括那些时间段。

例如,我可能有两个时段 [周五下午 5:31 - 周六下午 2:26] 和 [周二凌晨 3:37 - 周四凌晨 1:14] 在计算两个日期之间的分钟数时我不想计算并且在向前计算时。

我目前的代码只针对一个间隙执行此操作,尽管它不是超级高效并且正在成为我系统的压力。我还需要适应多个我目前不这样做的已定义差距。

我为一个间隙执行此操作的代码如下所示(hideStarthideEnter 是间隙的开始和结束日期时间,absoluteLowValue 是我计算距离或时间的开始时间) :

public int absoluteDistance(DateTime high){
    long totalMinutes = new Duration(absoluteLowValue,high).getStandardMinutes();

    if (!gapHider.isHidingGaps())
        return (int)totalMinutes;

    int minutesPerWeek = 10080;
    long minutesPerHide = new Duration(hideStart, hideEnd).getStandardMinutes();

    long numFullWeeks = totalMinutes/minutesPerWeek;
    long remainder = totalMinutes%minutesPerWeek;

    totalMinutes -= numFullWeeks*minutesPerHide;

    DateTime latestEnd = high;
    if (latestEnd.getDayOfWeek() == hideEnd.getDayOfWeek() && latestEnd.getSecondOfDay() < hideEnd.getSecondOfDay()){
        latestEnd = latestEnd.minusWeeks(1);
    }
    while (latestEnd.getDayOfWeek() != hideEnd.getDayOfWeek())
        latestEnd = latestEnd.minusDays(1);
    latestEnd = latestEnd.withTime(hideEnd.getHourOfDay(),
                                hideEnd.getMinuteOfHour(),
                                hideEnd.getSecondOfMinute(),
                                hideEnd.getMillisOfSecond());

    DateTime latestStart = high;
    if (latestStart.getDayOfWeek() == hideStart.getDayOfWeek() && latestStart.getSecondOfDay() < hideStart.getSecondOfDay()){
        latestStart = latestStart.minusWeeks(1);
    }
    while (latestStart.getDayOfWeek() != hideStart.getDayOfWeek())
        latestStart = latestStart.minusDays(1);
    latestStart = latestStart.withTime(hideStart.getHourOfDay(),
                                    hideStart.getMinuteOfHour(),
                                    hideStart.getSecondOfMinute(),
                                    hideStart.getMillisOfSecond());

    long timeToNearestEnd = new Duration(latestEnd, high).getStandardMinutes();
    long timeToNearestStart = new Duration(latestStart, high).getStandardMinutes();

    if (timeToNearestEnd < remainder){
        totalMinutes -= minutesPerHide;
    }else if (timeToNearestStart < remainder){
        totalMinutes -= new Duration(latestStart, high).getStandardMinutes();
    }

    return (int)totalMinutes;
}

public DateTime timeSinceAbsLow(int index){ 
    if (absoluteLowValue != null){

        if (!gapHider.isHidingGaps())
            return absoluteLowValue.plusMinutes(index);

        DateTime date = absoluteLowValue;
        long minutesPerWeek = 10080;
        long minutesPerHide = new Duration(hideStart, hideEnd).getStandardMinutes();
        int difference = (int)(minutesPerWeek - minutesPerHide);
        int count = 0;

        while (index - count >= difference){
            date = date.plusWeeks(1);
            count += difference;
        }

        int remaining = index - count;

        DateTime nextStart = date;

        while (nextStart.getDayOfWeek() != hideStart.getDayOfWeek())
            nextStart = nextStart.plusDays(1);
        nextStart = nextStart.withTime(hideStart.getHourOfDay(),
                                    hideStart.getMinuteOfHour(),
                                    hideStart.getSecondOfMinute(),
                                    hideStart.getMillisOfSecond());

        long timeDiff = new Duration(date, nextStart).getStandardMinutes();

        if (timeDiff < remaining){
            date = nextStart.plusMinutes((int)minutesPerHide);
            count+= timeDiff;
            remaining = index - count;
        }

        date = date.plusMinutes(remaining);
        return date;
    }
    return new DateTime(); 
}

有没有更好或更简单的方法来完成这个过程?我想,如果我添加大量逻辑来循环遍历“间隙”列表,它只会减慢它的速度。我愿意不使用 Jodatime,我只是碰巧目前正在使用它。任何帮助表示赞赏!

【问题讨论】:

    标签: java datetime jodatime duration date-difference


    【解决方案1】:

    如果我理解正确,您想计算开始日期和结束日期之间的总时间,并减去一个或多个时间段。因此,您可以使用 Duration 对象,然后最终转换为您想要的任何内容(分钟、秒等):

    // compute all periods you don't want to count
    List<Duration> hideList = new ArrayList<>();
    // duration between friday and saturday (assuming they have the values of your example)
    hideList.add(new Duration(friday, saturday));
    // add as many periods you want
    
    // total duration between start and end dates
    Duration totalDuration = new Duration(start, end);
    
    // subtract all periods from total
    for (Duration duration : hideList) {
        totalDuration = totalDuration.minus(duration);
    }
    
    // convert to total number of minutes
    long totalMinutes = totalDuration.getStandardMinutes();
    

    我假设hideList 中使用的所有日期都在开始日期和结束日期之间(但使用isAfterisBefore 方法很容易检查)。


    相反,您将totalMinutes 添加到开始日期,然后将hideList 中的所有持续时间相加:

    // sum total minutes to start
    DateTime dt = start.plusMinutes((int) totalMinutes);
    
    // sum all the periods
    for (Duration duration : hideList) {
        dt = dt.plus(duration);
    }
    // dt is the end date
    

    有一个细节:将Duration 转换为分钟数时,会丢失秒和毫秒的精度,因此在执行相反的算法时,这些字段将丢失。如果这不是问题,请按照上述方法进行操作。但是,如果您想保留所有精度,只需从添加 totalDuration 对象开始,而不是添加分钟数:

    // sum totalDuratin to start (to preserve seconds and milliseconds precision)
    DateTime dt = start.plus(totalDuration);
    

    【讨论】:

    • hideList 中的间隔不能保证在开始和结束日期的范围内。该算法的耗时部分是确定哪些持续时间在时间​​段内,然后在最后确定要包含/排除的最后一个持续时间的多少。虽然你的答案会起作用,但考虑到它的限制,它不会改善非常慢的算法。我更多的是寻找一种面向集合论的解决方案,其中整体持续时间是一个集合,间隙是一个集合,我可以执行“A union !B”类型的命令来大幅简化和降低成本
    • @Owen 你可以创建interval = new Interval(friday, saturday) 然后interval.contains(date) 来检查日期是否在这些日期之间;或使用DateTimeisBeforeisAfter 方法单独比较它们。我看不出这些比较的成本有多大,但如果你说它很慢,我假设你已经进行了基准测试并将其确定为瓶颈。无论如何,我没有看到用 Joda-Time 做这件事的另一种方法。祝你好运!
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 2022-01-13
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多