0
from Tkinter import *
from datetime import datetime
from datetime import timedelta
import math

rate1 = str(35.34) 
amp = 2.40
pmp = 2.50



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

if (signOnSun1 < 0600):
    rate1 = rate1 + amp
elif (signOnSun1 > 1100):
    rate1 = rate1 + pmp
elif (signOffSun1 > 1800):
    rate1 = rate1 + pmp
else:
    rate1 = 35.34

FMT = '%H%M'    
timeWorkedSun1 =  ( (datetime.strptime(signOffSun1, FMT)) -  (datetime.strptime   (signOnSun1, FMT)))

* I want to convert timeWorkedSun1 to a float so i can multiply it with rate1 but it seems to be the bane of my life. Any ideas?*

2
  • A float of what? Hours? Seconds? Parts of a day? Commented Dec 20, 2014 at 5:39
  • so the output i receive is in the format 00:00:00. which is great. but for example if the format says 8:00:00 i want to be able to multiply that with a float. for example 8:00:00 * 34.45 doesn't work. So I'm basically calculating the hours i work per day and multiplying that with my hourly rate. thats the aim Commented Dec 20, 2014 at 5:40

2 Answers 2

3

timeWorkedSun1 is of type datetime.timedelta. Call its total_seconds method to translate it into a number of second (then divide by 3600 to get it in hours-and-fractions). I.e:

time_mul_by_hourly_rate = (timeWorkedSun1.total_seconds() / 3600.0) * rate1
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming what you want to do is ask a user to enter two hours:minutes figures, and calculate the hours + fractions of hours between them, then using datetime as you do now, you'd do something like (simplified):

signOn = "09:15"
signOff = "15:45"

FMT = "%H:%M"
timeWorkedSun =  datetime.strptime(signOff, FMT) -  datetime.strptime(signOn, FMT)

# timeWorkedSun is now a datetime.timedelta
fractionHours = timeWorkedSun.total_seconds()/60.0/60

Alternately, without datetime code, we could do:

signOn = "09:15"
signOff = "15:45"

signOnP = [int(n) for n in signOn.split(":")]
signOffP = [int(n) for n in signOff.split(":")]

signOnH = signOnP[0] + signOnP[1]/60.0
signOffH = signOffP[0] + signOffP[1]/60.0

hours = signOffH - signOnH

However, that'll fail if someone started at 6pm on one day and ended at 3am the next day, so you might want to rethink your logic

2 Comments

2 big issues: timeWorkedSun.seconds is NOT callable (it's an attribute!) AND it's not the total number of seconds -- try datetime.timedelta(days=1).seconds ...!-). What you need is .total_seconds() as in my answer. You're right about time shifts passing midnight, though...
you my friend are a freaked genius!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.