python 2个日期时间之间的工作时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 2个日期时间之间的工作时间相关的知识,希望对你有一定的参考价值。

from datetime import datetime, timedelta

def clamp(t, start, end):
    "Return `t` clamped to the range [`start`, `end`]."
    return max(start, min(end, t))

def day_part(t):
    "Return timedelta between midnight and `t`."
    return t - t.replace(hour = 0, minute = 0, second = 0)

def office_time_between(a, b, start = timedelta(hours = 8),
                        stop = timedelta(hours = 17)):
    """
    Return the total office time between `a` and `b` as a timedelta
    object. Office time consists of weekdays from `start` to `stop`
    (default: 08:00 to 17:00).
    """
    zero = timedelta(0)
    assert(zero <= start <= stop <= timedelta(1))
    office_day = stop - start
    days = (b - a).days + 1
    weeks = days // 7
    extra = (max(0, 5 - a.weekday()) + min(5, 1 + b.weekday())) % 5
    weekdays = weeks * 5 + extra
    total = office_day * weekdays
    if a.weekday() < 5:
        total -= clamp(day_part(a) - start, zero, office_day)
    if b.weekday() < 5:
        total -= clamp(stop - day_part(b), zero, office_day)
    return total

以上是关于python 2个日期时间之间的工作时间的主要内容,如果未能解决你的问题,请参考以下文章

python pandas:groupby中2个日期之间的差异

日期之间的2个不同数据框之间的映射

oracle 获取当天日期往前推2个工作日的日期

SQL语句计算两个日期之间有多少个工作日的方法

SQL工作日查询

excel,求计算时间公式,与指定之间之间的还有多少个工作日