python时间计算_python计算两日期之间工作日时长

1. 原因:使用dateutil的rrule时,计算速度比较慢

def axx():

from dateutil import rrule

received_time = datetime.datetime.strptime('2019-04-21 23:00:00', '%Y-%m-%d %H:%M:%S')

complete_time = datetime.datetime.strptime('2019-04-22 01:00:00', '%Y-%m-%d %H:%M:%S')

workdays = [x for x in range(7) if x not in [5, 6]]

time_period = rrule.rrule(rrule.MINUTELY, dtstart=received_time, until=complete_time, byweekday=workdays).count()

print(time_period)

2. 尝试使用pandas的bdate_range,但是发现只统计工作日天数,即便不足1天也是按1天算的,不符合需求,因为我要分钟

def xxa():

import pandas as pd

date = pd.bdate_range('2019-04-21 23:00:00', '2019-04-22 01:00:00', freq='min')

minutes = len(date)

print(minutes)

print(minutes/(60*60))

3. 从stackoverflow找到一个方法

def xax():

from business_duration import businessDuration

import pandas as pd

received_time = pd.to_datetime('2019-04-21 23:00:00')

complete_time = pd.to_datetime('2019-04-22 01:15:00')

period = businessDuration(received_time, complete_time, unit='min')

print(period)

4. 自己使用pandas写的,还需测试

def aaa():

import pandas as pd

# test case 1

# received_time = '2019-04-21 23:00:00'

# complete_time = '2019-04-22 01:00:00'

# received_time = '2019-04-19 23:00:00'

# complete_time = '2019-04-20 01:00:00'

# test case 2

# received_time = '2019-04-18 23:00:00'

# complete_time = '2019-04-20 01:00:00'

# received_time = '2019-04-21 23:00:00'

# complete_time = '2019-04-23 01:00:00'

# test case 3

# received_time = '2019-04-21 23:00:00'

# complete_time = '2019-04-24 01:00:00'

# received_time = '2019-04-18 23:00:00'

# complete_time = '2019-04-20 01:00:00'

# test case 5

received_time = '2019-04-19 23:00:00'

complete_time = '2019-04-22 01:00:00'

received_date = pd.to_datetime(received_time)

complete_date = pd.to_datetime(complete_time)

date_period = pd.bdate_range(received_time, complete_time)

if date_period[0] == date_period[-1]:

if date_period[0] > received_date:

start = date_period[0]

end = complete_date

else:

start = received_date

end = date_period[0] + datetime.timedelta(days=1)

day_time = len(pd.date_range(start, end, freq='min')) - 1

print('Workdays:' + str(day_time) + ' minutes')

else:

if (complete_date - date_period[-1]).days > 0:

end = date_period[-1] + datetime.timedelta(days=1)

else:

end = complete_date

if received_date < date_period[0]:

start = date_period[0]

else:

start = received_date

received_per = pd.date_range(start, date_period[0] + datetime.timedelta(days=1), freq='min')

complete_per = pd.date_range(date_period[-1], end, freq='min')

middle_time = (len(date_period) - 2) * 1440

days_time = len(received_per) + middle_time + len(complete_per) - 2

print('Workdays:' + str(days_time) + ' minutes')

参考:https://stackoverflow.com/questions/46899627/business-hours-between-two-dates-in-pandas-dataframe-including-holidays?rq=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值