Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python
Difference between two times (working ugly code, needs polish)
 

Index | Next | Previous | View Flat


Shawn at Milochik

Sep 11, 2007, 1:46 PM


Views: 85
Permalink
Difference between two times (working ugly code, needs polish)

I have done what I wanted, but I think there must be a much better way.

Given two timestamps in the following format, I just want to figure
out how far apart they are (in days, seconds, whatever).

Format:

YYYY-MM-DD_MM:SS

Example:
2007-09-11_16:41


It seems to me that to do what I want, I need to convert a string into
a number (time.mktime()), such as this: 1189543487.0

Once I have that, I can just subtract one from the other and do
whatever I want. The ugly part is converting something like
2007-09-11_16:41 to the numeric equivalent.

Below is my code. Any suggestions?

Thanks in advance.


Here is what I have (works):

#runTimeStamp is the current time, set when the script begins execution
def recAge(lastUpdate, runTimeStamp):

import re

oneDay = 60 * 60 * 24

lastUpdate = re.sub(r'\D+', ',', lastUpdate)
lastUpdate = lastUpdate + ",0,0,0,0"
lastUpdate = [int(x) for x in lastUpdate.split(',')]
lastUpdate = time.mktime(tuple(lastUpdate))

runTimeStamp = re.sub(r'\D+', ',', runTimeStamp)
runTimeStamp = runTimeStamp + ",0,0,0,0"
runTimeStamp = [int(x) for x in runTimeStamp.split(',')]
runTimeStamp = time.mktime(tuple(runTimeStamp))

return (runTimeStamp - lastUpdate) / oneDay
--
http://mail.python.org/mailman/listinfo/python-list

Subject User Time
Difference between two times (working ugly code, needs polish) Shawn at Milochik Sep 11, 2007, 1:46 PM
    Re: Difference between two times (working ugly code, needs polish) bdesth.quelquechose at free Sep 9, 2007, 8:15 AM
    Re: Difference between two times (working ugly code, needs polish) grante at visi Sep 11, 2007, 1:53 PM
        Re: Difference between two times (working ugly code, needs polish) Shawn at Milochik Sep 11, 2007, 2:20 PM
    Re: Difference between two times (working ugly code, needs polish) steve at holdenweb Sep 11, 2007, 3:09 PM
        Re: Difference between two times (working ugly code, needs polish) Shawn at Milochik Sep 11, 2007, 5:31 PM
    Re: Difference between two times (working ugly code, needs polish) iainking at gmail Sep 12, 2007, 1:59 AM
    Re: Difference between two times (working ugly code, needs polish) Shawn at Milochik Sep 12, 2007, 3:42 AM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.