python計算兩日期之間工作日時長

2021-09-19 16:46:21 字數 3170 閱讀 2247

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')

參考:

近兩日工作

將xhtml樹進行了簡單修改做成了乙個展示頁面,沒有摸索連線資料庫的功能,打算以後將其自帶的php mysql例子改造成 jsp的。有關資料庫的設計看到cl和gm如此嫻熟的進行設計,真是感慨如潮啊,需要思考的東西和模式有很多,乙個硬幣可以看成n面的啊!今天下午去聽了css講座,收穫頗多,這種培訓果然...

Python計算兩個日期之間天數

有的時候要統計兩個日期之間的相距天數,可能有很多種方法,但使用datetime模組的datetime方法無疑是裡面比較簡單的,具體 如下 import datetime d1 datetime.datetime 2018,10,31 第乙個日期 d2 datetime.datetime 2019,0...

計算兩個日期之間的工作日數

計算兩個日期之間的工作日數,星期6,星期天,不算工作日 dt1和dt2之間相隔多少工作日,其中dt3 dt4的時間為公休日,這裡公休日可以用以個陣列,或者從乙個xml表裡面讀取,以便扣除 要計算的起始時間 要計算的結束時間 公休起始時間 公休結束時間 intreturn private int di...