View Single Post
Old May 26th, 2006, 1:59 AM   #2
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Easy to do with module time.
Enter the two times in hh:mm:ss format, add a date and use time.strptime() and the proper format string to create two time tuples.

time.mktime() converts each time tuple to seconds since epoch. Now you can take the time difference in seconds and give the result in seconds or minutes or fractional hours.
# time difference between two (12 hour) times of a day

import time

hms1 = "08:15:00AM"
hms2 = "04:45:30PM"
mdy1 = "05/15/06 "
epoch_seconds1 = time.mktime(time.strptime(mdy1+hms1, "%m/%d/%y %I:%M:%S%p"))
epoch_seconds2 = time.mktime(time.strptime(mdy1+hms2, "%m/%d/%y %I:%M:%S%p"))
time_diff = epoch_seconds2 - epoch_seconds1
print time_diff, "seconds"
print time_diff/60, "minutes"
print time_diff/(60*60), "hours"
__________________
I looked it up on the Intergnats!

Last edited by Dietrich; May 26th, 2006 at 2:21 AM.
Dietrich is offline   Reply With Quote