date and time comparison how to

Dave Angel d at davea.name
Mon Oct 29 23:11:29 EDT 2012


On 10/29/2012 10:13 PM, noydb wrote:
> I guess I get there eventually!  
> This seems to work
>
>     pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.path.getmtime(pdf)))
>     intermediateTime = time.strptime(pdf_timeStamp, "%m%d%y%H%M%S")
>     pdfFile_compareTime = time.mktime(intermediateTime)
>
> (and I'll do the same to the user entered date-n-time and then compare)
>
>
> Lastly, so can anyone chime in and tell me if this is a good method or not?  Is there a better way?

Please read the rest of the thread in particular the message 3 hours ago
from Gary Herron

                import datetime, os, stat
                mtime = os.lstat(filename)[stat.ST_MTIME]   // the files
modification time
                dt = datetime.datetime.fromtimestamp(mtime)

Now you can compare two datetimes simply by
   if dt1 < dt2:

Or you can subtract them, and examine the difference.

What's the need for all that string conversion stuff?



-- 

DaveA




More information about the Python-list mailing list