通过8h-20h工作计划中剩余的时间。分钟问题

时间:2018-12-16 15:53:56

标签: python time rounding timedelta

start是工人开始工作的时间。 duration是作业将花费的时间。工人只能在08:00至20h之间工作。 如果工人在19:00开始工作,而工作需要01:30完成,则输出next_day_finish_time将根据需要成为08:30。但是,如果这项工作需要完成01:20,由于时间紧迫,python将无法提供输出。用我的方式,我将那20分钟变成了几个小时。但是20/60会给我0,(333).....等。我试图在If:条件之前舍入该值。但是,当我这样做时,在If:内的time_left_over = final_hour - 20处,我检查了Python在调试中一直获得巨大的价值。

from datetime import datetime, timedelta

start = " 08:00"

start_h = start[1:3]
start_h = int(start_h)

start_m = start[4:]
start_m = int(start_m)

start_m = start_m / 60

start_time = start_h + start_m

duration = " 02:00"

duration_h = duration[1:3]

duration_h = int(duration_h)

duration_m = duration[4:]

duration_m = int(duration_m)

duration_m = duration_m / 60

duration_total = duration_h + duration_m

final_hour = start_time + duration_total

if final_hour > 20:
    time_leftover = final_hour - 20
    time_leftover = str(time_leftover)
    time_split = time_leftover.split(".")

    job_hours = time_split[0]
    job_hours = int(job_hours)

    job_minutes = time_split[1]
    job_minutes = int(job_minutes)
    job_minutes = job_minutes / 10
    job_minutes = job_minutes * 60

    stop_work_time = " 20:00"
    stop_work_time = datetime.strptime(stop_work_time, "%H:%M")

    start_work_time = stop_work_time
    start_work_time = start_work_time + timedelta(hours = 12 + job_hours, minutes = job_minutes)
    start_work_time = datetime.strftime(start_work_time, " %H:%M")
    next_day_finish_time = start_work_time
    print(next_day_finish_time)

else:
    job_finished = duration_total
    job_finished = str(job_finished)
    time_split_2 = job_finished.split(".")

    job_hours_2 = time_split_2[0]
    job_hours_2 = int(job_hours_2)

    job_minutes_2 = time_split_2[1]
    job_minutes_2 = int(job_minutes_2)
    job_minutes_2 = job_minutes_2 / 10
    job_minutes_2 = job_minutes_2 * 60

    finish_time = start
    finish_time = datetime.strptime(finish_time, "%H:%M")
    finish_time = finish_time + timedelta(hours = job_hours_2, minutes = job_minutes_2)
    finish_time = datetime.strftime(finish_time, " %H:%M")
    print(finish_time)

1 个答案:

答案 0 :(得分:0)

  

问题:8小时至20小时工作时间表中剩余的时间

timedelta进行计算。
文档:datetime.timedelta

from datetime import datetime, timedelta

# 08:00 and 20:00.
day_begin = datetime.now().replace(hour=8, minute=0, second=0, microsecond=0)
day_end = datetime.now().replace(hour=20, minute=0, second=0, microsecond=0)

# 19:00
start_job = datetime.now().replace(hour=19, minute=0, second=0, microsecond=0)

# 01:30
job_take = timedelta(hours=1, minutes=30)

job_end = start_job + job_take
overload = job_end - day_end
next_day = day_begin + timedelta(days=1)
job_end2 = next_day + timedelta(seconds=overload.seconds)

print("job_end:{}, overload:{}, job_end2:{}".format(job_end, overload, job_end2))
  

输出

job_end:2018-12-16 20:30:00, overload:0:30:00, job_end2:2018-12-17 08:30:00

使用Python测试:3.5