Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2006, 8:18 PM   #1
DaveQB
Newbie
 
DaveQB's Avatar
 
Join Date: Apr 2006
Location: Sydney
Posts: 14
Rep Power: 0 DaveQB is on a distinguished road
Send a message via ICQ to DaveQB Send a message via Skype™ to DaveQB
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.
DaveQB is offline   Reply With Quote
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
Old May 26th, 2006, 4:02 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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
One thing to note is that you have to specify a date as well as a time. You cannot find the difference between two datetime.time modules. This is probably because the number of seconds between two times is sometimes different from the usual. At daylight saving time, for instance, or in the rare case of leap-seconds.
Arevos is offline   Reply With Quote
Old May 26th, 2006, 5:51 PM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Talking

Quote:
Originally Posted by Arevos
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
One thing to note is that you have to specify a date as well as a time. You cannot find the difference between two datetime.time modules. This is probably because the number of seconds between two times is sometimes different from the usual. At daylight saving time, for instance, or in the rare case of leap-seconds.
The problem here comes when you ask the user to input the times like 6:30am and 2:30pm. Now you have to prep the input for the time tuples!
__________________
I looked it up on the Intergnats!

Last edited by Dietrich; May 26th, 2006 at 6:18 PM.
Dietrich is offline   Reply With Quote
Old May 26th, 2006, 6:09 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Dietrich
The problem here comes when you ask the user to input the times like 6:30am and 2:30pm. Not all of use live in a 24 hour clock world.
Ohhhh. In which case, I agree with your solution
Arevos is offline   Reply With Quote
Old May 26th, 2006, 6:23 PM   #6
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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!
Dietrich is offline   Reply With Quote
Old Jul 4th, 2006, 12:15 AM   #7
DaveQB
Newbie
 
DaveQB's Avatar
 
Join Date: Apr 2006
Location: Sydney
Posts: 14
Rep Power: 0 DaveQB is on a distinguished road
Send a message via ICQ to DaveQB Send a message via Skype™ to DaveQB
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.
DaveQB is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:02 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC