0
 from datetime import datetime

 FMT = '%H%M'
 rate1 = 35.34 
 Midnight = "0000"

signOnSun1 = raw_input("What time did you sign on Sunday: ");
signOffSun1 = raw_input("What time did you sign off Sunday: ");

totalShift = (datetime.strptime(signOffSun1, FMT) - datetime.strptime (signOnSun1,   FMT))

midnightToSignOff = (datetime.strptime(signOffSun1, FMT) - datetime.strptime (Midnight, FMT))
midnightToSignOff = diff.total_seconds()/60.0/60

basically this is what i have. if i sign on at 1800 and sign off that night at 0200 i can't return a proper answer of 8 8 hours

1 Answers1

0

The problem is the program doesn't know that the shift ended on the next day. To account for that, you can check if the sign on time is later than the sign off time, if that's the case, add a day to the sign off time.

from datetime import datetime, timedelta

FMT = '%H%M'
sign_on_time_str = raw_input("What time did you sign on Sunday: ");
sign_off_time_str = raw_input("What time did you sign off Sunday: ");
sign_on_time = datetime.strptime(sign_on_time_str, FMT)
sign_off_time = datetime.strptime(sign_off_time_str, FMT)

if sign_on_time > sign_off_time:
    sign_off_time += timedelta(days=1)

total_shift = sign_off_time - sign_on_time
print total_shift.total_seconds() / 60 / 60
Hesham Attia
  • 979
  • 8
  • 13