Reputation: 421
So the idea behind this is quite simple(yes it is an excercise not quite a real life problem)
So there is this girl who's clock is false but her friends clock is always right. so as she leaves from home she remembers the time she left her clock. at her friends she sees the time she arrives and she leaves and once home she sees the time again. This way even if we don't know how long she drove we can know the right time. E.g she leaves at 6:32 at her place she arrives with her friends at 14:14 and leaves with her friend at 17:21 and arrives home at 13:29.
The total time gone is 6:57 of which she spend 3:07 with her friends the rest of the time is divided by 2 so you know the time of 1 car trip.
so if you add up this time with the time you left at the friends house you find the actual time.
Now I have solved this problem before but I didn't knew the datetime function back then so I figured maybe I could solve it that way. My trouble with datetime is that when midnight passes it is stuck
For example when she leaves at home at 9:04 arrives at friend by 6:15 and leaves at 14:54 to be back home at 5:13 I get that the correct time is 8:57 while it should be 20:57.
Maybe it is someting in my datetime.timedelta but I don't kknow how to work around.
Any suggestions are very welcome Thanks a lot
My code on this 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)
Upvotes: 4
Views: 1987
Reputation: 507
Use datetime.datetime instead of datetime.time, it will take into account the change of day
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
and then you have timedelta.seconds
, timedelta.days
, timedelta.microseconds
and timedelta.total_seconds()
if you need a floating number
Upvotes: 1