![]() |
date and time comparison how to
All,
I need help with a date and time comparison. Say a user enters a date-n-time and a file on disk. I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process. How best to do? I have looked at the datetime module, tried a few things, no luck. Is os.stat a part of it? Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time. ?? Any help would be appreciated! |
Re: date and time comparison how to
On 10/29/2012 04:13 PM, noydb wrote:
> All, > > I need help with a date and time comparison. > > Say a user enters a date-n-time and a file on disk. I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process. > > How best to do? I have looked at the datetime module, tried a few things, no luck. > > Is os.stat a part of it? Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time. ?? > > Any help would be appreciated! Use the datetime module (distributed with Python) to compare date/times. You can turn a filesystem time into a datetime with something like the following: import datetime, os, stat mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification time dt = datetime.datetime.fromtimestamp(mtime) -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418 |
Re: date and time comparison how to
On 2012-10-30 00:04, Gary Herron wrote:
> On 10/29/2012 04:13 PM, noydb wrote: >> All, >> >> I need help with a date and time comparison. >> >> Say a user enters a date-n-time and a file on disk. I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process. >> >> How best to do? I have looked at the datetime module, tried a few things, no luck. >> >> Is os.stat a part of it? Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time. ?? >> >> Any help would be appreciated! > > Use the datetime module (distributed with Python) to compare date/times. > > You can turn a filesystem time into a datetime with something like the > following: > import datetime, os, stat > mtime = os.lstat(filename)[stat.ST_MTIME] // the > files modification time > dt = datetime.datetime.fromtimestamp(mtime) > > Instead of os.lstat(filename)[stat.ST_MTIME] you could use os.path.getmtime(filename). |
Re: date and time comparison how to
Thanks, I did find this...
pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.pat h.getmtime(pdf))) >> pdf_timestamp >> '102909133000' .... but now how to do the comparison? Cannot just do a raw string comparison, gotta declare it a date |
Re: date and time comparison how to
Thanks, I did find this...
pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.pat h.getmtime(pdf))) >> pdf_timestamp >> '102909133000' .... but now how to do the comparison? Cannot just do a raw string comparison, gotta declare it a date |
Re: date and time comparison how to
if I do time.time() I get 1351562187.757, do it again I get 1351562212.2650001 --- so I can compare those, the latter is later then the former. Good.SO how do I turn pdf_timeStamp (a string) above into time in this (as from time.time()) format? Am I on the right track -- is that the way to do a time comparison?
|
Re: date and time comparison how to
if I do time.time() I get 1351562187.757, do it again I get 1351562212.2650001 --- so I can compare those, the latter is later then the former. Good.SO how do I turn pdf_timeStamp (a string) above into time in this (as from time.time()) format? Am I on the right track -- is that the way to do a time comparison?
|
Re: date and time comparison how to
I guess I get there eventually!
This seems to work pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.pat h.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? |
Re: date and time comparison how to
I guess I get there eventually!
This seems to work pdf_timeStamp = time.strftime("%m%d%y%H%M%S",time.localtime(os.pat h.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? |
Re: date and time comparison how to
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.pat h.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 |
| All times are GMT. The time now is 10:54 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.