Hi, I'm trying to build a simple python program that will total how many hours a worker has worked based on a start time and end time entry but the problem is this "simple" problem is becoming very complicated and i'm affraid i'm way off base and digging deeper and deeper into a coding nightmare... Think of it as a sort of time card system if you will.

I'm a beginner python coder (only been at it for 3 months now) if that helps explain my ignorance...

Specifically, I want the terminal operator to enter a start and end time in hh:mm format and have python then determine how many hours and minutes the worker worked based on his/her entries, then calculate the wage before taxes based on a pre-set wage variable. The program has to be able to identify either "am" or "pm" time and do the proper conversions.

Heres an example of the output i'm looking for (based on a $12.50/hr wage):
(red text is user input, green text is of course python)

>>> Please enter your start time (hh:mm): 12:30pm
>>> Please enter your end time (hh:mm): 6:00pm
>>> Total time worked: 6hrs 30min
>>> Total wages before taxes: $ 81.25

The problem i'm having, however, is figuring out how to make python calculate the correct amount of hours worked based on the hh:mm input from the user. I've done some string conversions, and splicing to isolate the hours and min into seperate variables but i'm geting lost in all the code. I'd like the resulting code to be simple and elegant.

Like, how do you tell python that from 12:30pm to 6:00pm equals 6 hours 30 min? Or from 7:30am to 4:45pm equals 9 hours 15 min and have that value multiplied by the wage?

Presuming you have all this in separate variables as indicated, try something along these lines:

sh, sm, st = 12, 30, 'pm'
eh, em, et = 6, 0, 'pm'
# First, adjust "hour reported" to real "hour of day"
if sh < 12 and st.lower() == 'pm': sh += 12
if sh == 12 and st.lower() == 'am': sh = 0  # Early morning
if eh < 12 and et.lower() == 'pm': eh += 12
if eh == 12 and et.lower() == 'am': eh = 0  # Early morning
# Calculate minute-of-day
smin = sh * 60 + sm
emin = eh * 60 + em
# Delta time in  minutes
dmin = emin - smin
if dmin < 0: dmin += 1440  # Wrapped around midnight?
# Delta time in hours
dtime = dmin / 60.
print dtime # 5.5 hours from minutes 780->1050 (12:30-18:00).

Thanks for your reply, I tried military time but i'm stuck on how to get python to calculate the correct amount of minutes between the two times.

Here, i'll post the code I have so far below. The problem is it doesnt calculate the final value for actual minutes worked correctly:

def main():

        from math import *
        from string import *

        print "This program calculates the wage earned based on the number of hours worked.\n"

        startTime = raw_input("Enter the start time in hours:min format: ")
        endTime = raw_input("Enter the end time in hours:min format: ")

     #splits time input into usable chunks
        startSplit = split(startTime,":")
        endSplit = split(endTime,":")

     #converts strings to usable integers
        startHrs = int(startSplit[0])
        startMin = int(startSplit[1])
        endHrs = int(endSplit[0])
        endMin = int(endSplit[1])

     #self explanatory... 
        startTimeConvert = ((startHrs+12)*100)+startMin
        print "The start time conversion is ",startTimeConvert

        endTimeConvert = ((endHrs+12)*100)+endMin
        print "The end time conversion is ",endTimeConvert

        totalTime = endTimeConvert - startTimeConvert
        print "Total time is: ",totalTime

    #use "0"+str if time is only 3 digits
        totalHrs = "0"+str(totalTime)[0:1]
        totalMin = "0"+str(totalTime)[2:3]
        print "totalHrs test: ",totalHrs
        print "totalMin test: ",totalMin

    #hourly rate earned
        wage = (int(totalHrs)*12.55)+((int(totalMin)*12.55)/60) 
                   #divided min by 60 to get correct wage for minutes portion
        print "Wage test: $",wage

if __name__ == '__main__':
    main()

I plan on dividing this all up into seperate functions to streamline the script, but that comes later... after it is actually working correctly.

Anyone have any ideas how to finish the code and get it working correctly?

There are 60 minutes in an hour, not 100. If you multiplied by 60 instead of 100 you would be able to subtract the two numbers and get the correct difference.

It's easier to think of noon as 1200 instead of 720 minutes, but if that makes the arithmetic fail, are you sure that's the right way to go?

Also you only add 12 to the hour if it is 1:00pm through 11:00pm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.