即使你过了午夜,如何知道时差?Python

所以这背后的想法非常简单(是的,这是一个不太现实的问题)

所以有这个女孩的时钟是假的,但她的朋友时钟总是正确的.因此,当她离开家时,她记得她离开时钟的时间.在她的朋友们,她看到她到达的时间,她离开,一旦回到家,她又看到了时间.这样,即使我们不知道她开车了多久,我们也能知道正确的时间.
例如,她于6:32离开她的地方,于14:14与她的朋友一起离开,并于17:21与她的朋友离开,并于13:29到家.

总时间是6:57,她与朋友一起花3:07剩下的时间除以2,所以你知道1次车的时间.

所以如果你把这段时间和你离开朋友家的时间相加,你会发现实际的时间.

现在我已经解决了这个问题,但我当时不知道日期时间函数所以我想也许我可以这样解决它.我对日期时间的麻烦是,当午夜过去时,它会被卡住

例如,当她在9:04离开家时,在6:15到达朋友,在14:54离开,在5:13回家,我得到正确的时间是8:57而应该是20:57.

也许它在我的datetime.timedelta中有所影响,但我不知道如何解决.

任何建议都非常欢迎
非常感谢

我这个时刻的代码:

import datetime

# function nessesary to convert timedelta back into time 
def convert_timedelta(duration):
    days, seconds = duration.days, duration.seconds
    hours = days * 24 + seconds // 3600
    minutes = (seconds % 3600) // 60
    seconds = (seconds % 60)
    return hours, minutes, seconds

# input
hour_department_home = int(input("Hour of department at home: "))
minutes_department_home = int(input("Minutes of department at home: "))

hour_arrival_friend = int(input("Hour of arrivel friends house: "))
minutes_arrival_friend = int(input("Minutes of department friends house: "))

hour_department_friend = int(input("Hour of arrivel friends house: "))
minutes_department_friend = int(input("Minutes of arrivel friends house: "))

hour_arrival_home = int(input("Hour of arrivel home : "))
minutes_arrival_home = int(input("Minutes of arrivel home: "))

# putting into hours
department_home = datetime.time(hour_department_home,minutes_department_home,second=0)
arrival_friend = datetime.time(hour_arrival_friend,minutes_arrival_friend)
department_friend = datetime.time(hour_department_friend,minutes_department_friend)
arrival_home= datetime.time(hour_arrival_home,minutes_arrival_home)

# time at friends
uur_1,uur_2 = datetime.timedelta(hours=department_friend.hour),datetime.timedelta(hours = arrival_friend.hour)
hours_with_friend = uur_1-uur_2

min_1,min_2= datetime.timedelta(minutes=department_friend.minute),datetime.timedelta(minutes=arrival_friend.minute)
min_with_friend = min_1-min_2

total_time_with_friend = hours_with_friend + min_with_friend

# time away
uur_1h,uur_2h = datetime.timedelta(hours=arrival_home.hour),datetime.timedelta(hours = department_home.hour)
hours_away_from_home = uur_1h-uur_2h

min_1h,min_2h= datetime.timedelta(minutes=arrival_home.minute),datetime.timedelta(minutes=department_home.minute)
min_away_from_home = min_1h-min_2h

total_time_away = hours_away_from_home + min_away_from_home

duration_drive = (total_time_away-total_time_with_friend) /2

the_time = datetime.timedelta(hours=department_friend.hour,minutes=department_friend.minute) +duration_drive

hours, minutes, seconds = convert_timedelta(the_time)

print(hours)
print(minutes)

最佳答案 使用datetime.datetime而不是datetime.time,它将考虑到当天的变化

date_1 = datetime.datetime(year1, month1, day1, hour1, minutes1, seconds1)
date_2 = datetime.datetime(year2, month2, day2, hour2, minutes2, seconds2)
timedelta = date_2 - date_1

然后你有timedelta.seconds,timedelta.days,timedelta.microseconds和timedelta.total_seconds()如果你需要一个浮点数

点赞