日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/10 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的python时间计算_python计算两日期之间工作日时长的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。