Python has alot of functions for working with times. this might be necessary if you’re working with Log files or in my case, payroll files.
the first thing i needed to do was get the time out of a text file with Fixed Length. I then had to convert that string to a datetime object so that i could use the various time functions available in Python.
#get characters located between position 8 and 13. In = employee[8:13].strip() #define a timeformat for use in various places throughout the program TmFmt='%H:%M' #take the previously obtained string and convert to datetime object. TimeIn = datetime.datetime(*time.strptime(In,TmFmt)[:6])
Let’s look at that last line a little closer
TimeIn = datetime.datetime(*time.strptime(In,TmFmt)[:6])
first we take the string In and apply a function called strptime along with the format we have defined in the variable TmFmt. strptime can take a date and time in a string and breaks it into tupples, the outer function then converts this to a datetime object and puts it into TimeIn. my example only contains time, but it could also contain the date (%m=month, %d=day, %y=year).
We can now use Python’s time and datetime functions on TimeIn.
say you had a given time and you wanted to add 8 hours to it, you use a function call timedelta.
Assuming that TimeIn is the datetime object that we created in the previous step, we could do the following.
TimeOut = TimeIn + timedelta(hours=8)
that then adds 8 hours to TimeIn and stores it in TimeOut.
if you had two times, you might want to find the difference between them.
Shift = TimeOut - TimeIn
yep, that simple. except that Shift is a TimeDelta object. you can print it by just typing print Shift, but if you want to do any comparison on it, you have to compare it to another timedelta object. the below example shows testing if the shift time is greater than 5 hours.
If Shift > time.timedelta(hours=5): print "Full Shift"
here’s a small example making use of some of the things mentioned in one script.
assuming a file name 20110729-time.txt containing:
22134508:0016:0020110729 22142508:0016:1520110729 22131608:0016:0020110729 22111809:0017:0020110729
here’s a script to read through that file and tell you what hours each person worked and how many hours they worked, minus their unpaid meal time. and in the case of >8 hours in a day, adds the additional time again for Overtime.
import datetime,time from datetime import timedelta #format for reading time from strings and priting them TmFmt='%H:%M' #open import file and create file for export InputFile = open('20110729-time.txt', 'r') #read through each line of the import File = InputFile.readlines() #For Each Employee in teh above file, process their record for employee in File: ID = employee[0:6] TimeIn = datetime.datetime(*time.strptime(employee[6:11],TmFmt)[:6]) TimeOut = datetime.datetime(*time.strptime(employee[11:16],TmFmt)[:6]) DateIn = employee[16:24] #Get total number of hours for shift TotHours = TimeOut-TimeIn #Subtract unpaid meal time TotHours = TotHours + datetime.timedelta(minutes=-30) #See if shift longer than 8 hours, and if so, add the additional time again if TotHours > datetime.timedelta(hours=8): OTHours = TotHours - datetime.timedelta(hours=8) TotHours = TotHours + OTHours #Print out a block of information detailing their hours print "Employee: " + ID print "Date: " + DateIn print "Time In: "+TimeIn.strftime(TmFmt)+"\t Time Out: " + TimeOut.strftime(TmFmt) print "Total Hours: " + str(TotHours) print "\n" InputFile.close()