在 python 中增加工作日的小时数
Add hours to workday in python
我需要以下脚本来计算从上午 9 点到下午 6 点的工作时间,这样如果我添加 5 小时,它将从第二天的上午 9 点开始添加。
示例:如果现在是下午 5 点,我加上 5 小时,工作日在下午 6 点结束,输出将是:13 小时。
17 + 1 = 18 和
9 + 4 = 13 小时
到目前为止,脚本计算小时数而不考虑劳动力限制。
from datetime import datetime, timedelta
updated = ( datetime.now() +
timedelta( hours = 5 )).strftime('%H:%M:%S')
print( updated )
--22:12:00
你在这里:
workday_begin = time(9)
workday_end = time(18)
# compute workday length
workday_hours = datetime.combine(date.today(), workday_end) - datetime.combine(date.today(), workday_begin)
# this is timedelta from your example
duration_hours = timedelta(hours=17)
# ignore times longer than a workday
day_cnt = 0 # this could be used to know how many day we've skipped, not part of question tho
while duration_hours > workday_hours:
day_cnt += 1
duration_hours -= workday_hours
now = datetime.now()
# now = datetime(2021,12,10,11,25,16)
if workday_begin < now.time() < workday_end:
# now is in work-hours
if datetime.combine(date.today(), workday_end) - now < duration_hours:
# duration would exceed work-hours, jumping to next day
day_cnt += 1
duration_hours -= (datetime.combine(date.today(), workday_end) - now)
updated = datetime.combine(date.today(), workday_begin) + duration_hours
else:
# everything is fine, just add hours
updated = now + duration_hours
else:
# now is not in work-hours. Add remaining duration to workday begin
updated = datetime.combine(date.today(), workday_begin) + duration_hours
# keep just a time part
updated = updated.time().strftime('%H:%M:%S')
print( updated )
希望我理解了你的问题。
我需要以下脚本来计算从上午 9 点到下午 6 点的工作时间,这样如果我添加 5 小时,它将从第二天的上午 9 点开始添加。 示例:如果现在是下午 5 点,我加上 5 小时,工作日在下午 6 点结束,输出将是:13 小时。 17 + 1 = 18 和 9 + 4 = 13 小时
到目前为止,脚本计算小时数而不考虑劳动力限制。
from datetime import datetime, timedelta
updated = ( datetime.now() +
timedelta( hours = 5 )).strftime('%H:%M:%S')
print( updated )
--22:12:00
你在这里:
workday_begin = time(9)
workday_end = time(18)
# compute workday length
workday_hours = datetime.combine(date.today(), workday_end) - datetime.combine(date.today(), workday_begin)
# this is timedelta from your example
duration_hours = timedelta(hours=17)
# ignore times longer than a workday
day_cnt = 0 # this could be used to know how many day we've skipped, not part of question tho
while duration_hours > workday_hours:
day_cnt += 1
duration_hours -= workday_hours
now = datetime.now()
# now = datetime(2021,12,10,11,25,16)
if workday_begin < now.time() < workday_end:
# now is in work-hours
if datetime.combine(date.today(), workday_end) - now < duration_hours:
# duration would exceed work-hours, jumping to next day
day_cnt += 1
duration_hours -= (datetime.combine(date.today(), workday_end) - now)
updated = datetime.combine(date.today(), workday_begin) + duration_hours
else:
# everything is fine, just add hours
updated = now + duration_hours
else:
# now is not in work-hours. Add remaining duration to workday begin
updated = datetime.combine(date.today(), workday_begin) + duration_hours
# keep just a time part
updated = updated.time().strftime('%H:%M:%S')
print( updated )
希望我理解了你的问题。