How do I know the time difference, even if you pass midnight? python

So, the idea behind this is quite simple (yes, this exercise is not quite for real life)

So, there is this girl whose watch is fake, but the watch of her friends is always right. as she leaves home, she recalls how she left the watch. with her friends she sees the time when she arrives, and she leaves, and one day she will see the time again. Thus, even if we do not know how long she rode, we can know the right time. For example, she leaves at 6:32 in her place, she comes with her friends at 14:14 and leaves with her friend at 17:21 and arrives home at 13:29.

Total time: 6:57, from which she spends 3:07 with her friends, the rest of the time is divided by 2, so you know the time of 1 trip by car.

therefore, if you add this time with the time that you left in the friends house, you find the actual time.

Now I solved this problem before, but then I did not know the datetime function, so I decided that I could solve it this way. My problem with datetime is that when midnight passes, it is stuck

For example, when she leaves home at 9:04, she comes to her friend at 6:15 and leaves at 14:54 to return home at 5:13. I understand that the right time is 8:57, while it should be 20: 57.

Perhaps this accompanies me in datetime.timedelta, but I don't know how to work.

Any suggestions are very welcome. Thank you very much.

My code at the moment:

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)
+4
source share
1 answer

Use datetime.datetime instead of datetime.time, it will take into account the day change

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(),

+1

Source: https://habr.com/ru/post/1694165/


All Articles