[Tutor] time calc

David Rock david at graniteweb.com
Tue Jun 22 00:34:01 EDT 2004


* kevin parks <kp8 at mac.com> [2004-06-18 13:36]:
> hi all.
> 
> I have a bit of a task that i have been doing by hand, but as it turns 
> out there is pages and pages more of this so i would like to make a 
> python script that does this work and have it handy.
> 
> I have a list that has several columns like so:
> 
> 1    1    1    00:23
> 1    2    2    8:23
> 1    3    3    9:41
> 1    4    3    10:47
> 1    5    3     11:21
> 
> 
> What this is a list that has tape number, program number, item number 
> and start time for a recording.
> 
> What i want to do is append the duration to the end of this list by 
> subtracting the start of the item from the start time of the next so 
> that the first line would look like:
> 
> 1    1    1    00:23    08:00
> 1    2    2    8:23      01:18
> 
> etc.
> 
> So the task is really two part : reading in a list of numbers and 
> adding one more piece of data and (2) doing time (base 60) math. It is 
> the time math part i simply can't get my head around. Anyone know if 
> there is a module that does time math? or a method for doing this by 
> hand?

Look at the datetime module in python 2.3 I use it a lot to do date
calculations (subtract one time from another, etc).
http://www.python.org/doc/current/lib/module-datetime.html

Especially look at the timedelta and datetime objects
http://www.python.org/doc/current/lib/datetime-timedelta.html
http://www.python.org/doc/current/lib/module-datetime.html

These are probably overkill, but they are VERY powerful date calc tools

Here is an example of how I have used these to figure out the date from
7 days ago. Where this really starts to get cool is when you are
overlapping month and year ranges, or in your case, rolling over hours.

import time
import datetime
def get_last_week():
    dtup_now = time.localtime()        # Gets current time tuple
    y,m,d = dtup_now[:3]               # Gets year,month,day from tuple
    d_today = datetime.datetime(y,m,d) # Creates datetime object from current date
    d_delta = datetime.timedelta(7)    # Creates timedelta object of 7 days
    d_last_week = d_today - d_delta    # subtracts 7 days from current day
    return d_last_week

last_week = get_last_week()
print last_week.month, last_week.day, last_week.year

-- 
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040621/18ea35a0/attachment.bin


More information about the Tutor mailing list