4

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)
Reck
  • 1,388
  • 11
  • 20
Jirka
  • 391
  • 1
  • 2
  • 14
  • You always remind with the `time`, you have forgot `date` . – Frank AK Feb 27 '18 at 09:07
  • Comment by @Baduker: Correct me if I'm wrong but this might help (and/or be a duplicate): [Python format timedelta greater than 24 hours](https://stackoverflow.com/questions/34134971/python-format-timedelta-greater-than-24-hours-for-display-only-containing-hours) – Mr. T Feb 27 '18 at 09:30
  • @Baduker thanks for the comment I saw this one before but it didn't really help me further – Jirka Feb 27 '18 at 10:53

1 Answers1

1

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

Gabriel Samain
  • 497
  • 3
  • 10
  • So I use datetime.datetime instead of datetime.time then ? – Jirka Feb 27 '18 at 10:51
  • 1
    @LibertyVerleysen Yes you should us datetime.datetime instead of datetime.time. Since datetime.time has no knowledge of the date change (pass midnight which you mentioned). – Reck Feb 27 '18 at 11:28
  • So I tried this but I kinda don't know what to give in with day and so on, does it matter? apparently there aren't standard dates and from the beginning of my problem I don't know what date it is see her clock (the wrong one) can still be day before – Jirka Feb 27 '18 at 13:07
  • Use `start_date = datetime.datetime(1, 1, 1, START_HOUR, START_MINUTE, START_SECOND)` with your data for the departure time. For the arriving time if hour < START_HOUR use `datetime.datetime(1, 1, 2, END_HOUR, END_MINUTE, END_SECOND)`, else `datetime.datetime(1, 1, 1, ....)` It's ugly but if the only thing you have is HH:MM:SS you can't do much better with datetime, you'll be luckier with some `% 24` and `% 60` + a little logic – Gabriel Samain Feb 27 '18 at 15:18
  • @GabrielSamain as mentioned in the question I did solve it without date time before since I didn't knew date time back then. Just thought be easier this way but apparently it isn't. Thanks a lot for all the help, I do understand the date time thing way better now – Jirka Feb 27 '18 at 21:26