[Tutor] Time script help sought!

Liam Clarke cyresse at gmail.com
Wed Jan 12 00:37:52 CET 2005


It's kind of easy - 

so that stuff you've got there is in a file called dude.inp, for
argument's sake -


import datetime

filename=file("dude.inp","r")
lineList=filename.readlines()
filename.close()

for line in lineList:
      #This is the hard bit, formatting your in dates
      splitLine=line.split(", ")    
      for (item, dat, num, startTime, finishTime) in splitLine:
           
           sepStart=startTime.split(":")
           startHours=sepStart[0]
           startMin=sepStart[1]
           startSec=sepStart[2]

           finishStart= finishTime.split(":")
           finishHours=sepStart[0]
           finishMin=sepStart[1]
           finishSec=sepStart[2]
            
           #Now for the easy bit, manipulating the time

           startObj = datetime.datetime(01, 1, 1, startHours,
startMin, startSec)
           finishObj = datetime.dateime(01, 1, 1, startHours,
startMin, startSec)

           #If you're going to simply have all times start from 00:00,
you only want the length
           #of the time period right? So....

           lengthDelta = finishObj - startObj #Python does the time
calcs for ya!
           lengthObj = datetime.datetime(lengthDelta) 
           

OK - so, once you've formatted the input to get the hours, mins, and
secs, you create a datetime object for each time which is set for that
01/01/1901, at that particular time.
Notice the 01, 1, 1, - you can't use 01 01 for the month and day \

a datetime object goes like this - 
datetime.datetime(year, month, day, hours, minutes, secs)

So, you've got the two datetime objects, Python handles the maths, and gives a 
timedelta object, which you use to create a new datetime object (as a
timedelta object can't use strftime) which you can then use to format
your time with strftime!

Real easy... if there's anything I've messed up, let me know, and I'll
clarify it for you ( I coded something similar at home).

Or if anything needs expansion upon...

Regards,

Liam Clarke


On Tue, 11 Jan 2005 16:51:31 -0500, kevin parks <kp8 at mac.com> wrote:
> I am still working on it and also fixing the input data. I think for
> simplicity and consistency's sake i will have *all* time values input
> and output as hh:mm:ss maybe that would be easier for now.. so i am
> editing the input stuff now...
> 
> so it should all look like:
> 
> Item_1, DAT_1, 1, 00:00:23, 00:08:23
> 
> Item_2, DAT_1, 2, 00:08:23, 00:09:41
> 
> Item_3, DAT_1, 3, 00:09:41, 00:10:41
> Item_3, DAT_1, 4, 00:10:47, 00:11:19
> Item_3, DAT_1, 5, 00:11:21, 00:11:55
> Item_3, DAT_1, 6, 00:11:58, 00:12:10
> Item_3, DAT_1, 7, 00:12:15, 00:12:45
> Item_3, DAT_1, 8, 00:12:58, 00:24:20
> 
> Item_4, DAT_1, 9, 00:24:33
> Item_4, DAT_1, 10, 00:25:48
> Item_4, DAT_1, 11, 00:29:48
> Item_4, DAT_1, 12, 00:31:46
> Item_4, DAT_1, 13, 00:34:17
> Item_4, DAT_1, 14, 00:35:21
> Item_4, DAT_1, 15, 00:36:06
> Item_4, DAT_1, 16, 00:37:01, 00:37:38
> 
> no comments either i can copy them later...
> 
> this is kind of hard...
> 
> -k
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list