![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
Time conversions
I can't seem to find any already existing module that allows for simple time converstions. That is, the time between 6:30am and 2:30pm is ....
Sure, I could make one myself, but I thought why re-invent the wheel. Do people just make there own, or am I missing something ?? PS DateTime module doesn't seem to have any conversions/calculation functions/methods. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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 1:21 AM. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
There's an easier method: use the datetime module.
import datetime six_thirty = datetime.datetime(2006, 5, 26, 6, 30) two_thirty = datetime.datetime(2006, 5, 26, 14, 30) difference = two_thirty - six_thirty print "Hours difference:", difference.seconds / 60 / 60 |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
__________________
I looked it up on the Intergnats! Last edited by Dietrich; May 26th, 2006 at 5:18 PM. |
|
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
![]() |
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Arevos,
thank you for datetime code. I learned that datetime.datetime() can take a short version of the time tuple! (year,month,day,hour,min) rather than (year,month,day,hour,min,sec,weekday,yearday,dls-flag), must have defaults, very interesting.
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#7 |
|
Newbie
|
Thanks for the replies guys. I mustn't of had Thread Subscribing onwhen I posted this question. I have at least 2 uses for this kind of code.
Thanks alot. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|