Re: Q: finding distance between 2 time's
On Mon, 29 Jun 2009 19:15:08 +, John Gordon wrote: if time_difference 3601: That's a potential off-by-one error. [...] The right test is: if time_difference = 3600: Aren't those two comparisons the same? Not if time_difference is a float. -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
John Gordon wrote: In 023130ef$0$19421$c3e8...@news.astraweb.com Steven D'Aprano st...@remove-this-cybersource.com.au writes: if time_difference 3601: That's a potential off-by-one error. [...] The right test is: if time_difference = 3600: Aren't those two comparisons the same? Only if time_difference is an integer. --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
On May 30, 4:10 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: On Sat, 30 May 2009 12:06:55 +0200, jkv wrote: I added a few lines to your script, and now it ought to only print files newer than 3601 seconds (3600 seconds is one hour). ... #if file newer than one hour print a line if time_difference 3601: That's a potential off-by-one error. That may print files that are older than one hour. Admittedly, they'll be off by less than one second, but if you're going to write code, write correct code. The right test is: if time_difference = 3600: (and you may even want to deal with files that have a *negative* time difference, e.g. they were created apparently in the future). This is particularly necessary if you use time.time() to generate the current time, since that returns fractions of a second. But even if you don't, it's still the right thing to do: it's defensive programming. Rather than assume that all file systems store timestamps accurate only to a second, assume that some file system, somewhere, will be accurate to fractions of a second, and code accordingly. That way, no matter what the file system does, your code will still do the right thing, and (in this case) it doesn't even cost you anything. -- Steven Hi Steve thanks for your comment. But I only need it to re-run some test scripts in case there seems to be hick-up in our live download of satellite images. It's not critical within seconds, not even within minutes... But in principel the potential error could of cause be important - in other context. :-) M -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
On May 30, 11:37 pm, jkv j...@unixcluster.dk wrote: mar...@hvidberg.net wrote: Thanks both The first answer is quite instuctive, the other one might be the one I'll use in t I didn't receive the other answer, could you please forward it to me? So 2x thanks. You are welcome. I took another look at your code, and you can compress it all to a if oneliner: (and thanks to Steven for the = reminder) if os.path.isfile(x): nSize = os.path.getsize(x) #if oneliner if time.mktime(time.localtime()) - time.mktime(time.localtime(os.path.getmtime(x))) = 3600: print ('HT fil - %s %d @ %s') % (x, nSize, time.asctime) I encurrage both Clarity and Brevity, but usually in that order. In other words, I don't mind to split a statement in two (or three) sub-sentenses, if it increases Clarity. As long as it dosn't so large that I loose the overview, and as long as it dosn't affect runtime performance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
jkv wrote: Hi Martin, What i usally do is to convert the two times to seconds since epoch and compare the two values in seconds. You can use time.mktime to convert localtime to seconds since epoch. There's no need to convert - simply retrieve the times as absolute times to begin with: file_age_in_seconds = time.time() - os.path.getmtime(filename) Only convert to local time for presentation. - Anders -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
On May 30, 8:49 pm, John Machin sjmac...@lexicon.net wrote: import time You are in a maze of twisty little functions, all alike. Quote of the week. Perhaps the year. I hope you don't mind me using it in the future. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Q: finding distance between 2 time's
I made this little script (below) to look througt a dir to see if there are any files newer than .e.g. 1 hour. I have the loop through the dir working and can retreive file time as well as present time. both time variables are in the format returned by time.localtime() My question: How do I find the difference between such two time variables, to calculate the 'age' of the file? :-) Martin --8-- Code begin - import os, time def buildList(directory): listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) if os.path.isdir(x): print ('dir - %s') % x if os.path.isfile(x): tstF = time.localtime(os.path.getmtime(x)) nSize = os.path.getsize(x) print ('fil - %s %d @ %s') % (x, nSize, time.asctime (tstF)) return 0 tstN = time.localtime() print tstN print Time now: %s % time.asctime(tstN) buildList('C:\Martin\Work\Work_Eclipse\.metadata') -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
Hi Martin, What i usally do is to convert the two times to seconds since epoch and compare the two values in seconds. You can use time.mktime to convert localtime to seconds since epoch. I added a few lines to your script, and now it ought to only print files newer than 3601 seconds (3600 seconds is one hour). The modification to you script is just some quick code, im pretty sure on can compress it a bit. Regards, Johnny script: import os, time def buildList(directory): listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) if os.path.isdir(x): print ('dir - %s') % x if os.path.isfile(x): current_time = time.localtime() tstF = time.localtime(os.path.getmtime(x)) #convert the current time to seconds since epoch current_epoch=time.mktime(current_time) #convert the timestamp on the file to seconds since epoch file_epoch = time.mktime(tstF) nSize = os.path.getsize(x) time_difference = current_epoch - file_epoch #if file newer than one hour print a line if time_difference 3601: print ('NEW FILE - %s %d @ %s') % (x, nSize, time.asctime (tstF)) return 0 tstN = time.localtime() print tstN print Time now: %s % time.asctime(tstN) buildList('/') mar...@hvidberg.net wrote: I made this little script (below) to look througt a dir to see if there are any files newer than .e.g. 1 hour. I have the loop through the dir working and can retreive file time as well as present time. both time variables are in the format returned by time.localtime() My question: How do I find the difference between such two time variables, to calculate the 'age' of the file? :-) Martin --8-- Code begin - import os, time def buildList(directory): listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) if os.path.isdir(x): print ('dir - %s') % x if os.path.isfile(x): tstF = time.localtime(os.path.getmtime(x)) nSize = os.path.getsize(x) print ('fil - %s %d @ %s') % (x, nSize, time.asctime (tstF)) return 0 tstN = time.localtime() print tstN print Time now: %s % time.asctime(tstN) buildList('C:\Martin\Work\Work_Eclipse\.metadata') -- Regards, jkv http://unixcluster.dk/public.key -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
Thanks both The first answer is quite instuctive, the other one might be the one I'll use in the code, it's nicely compact and clear. So 2x thanks. :-) Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
On Sat, 30 May 2009 12:06:55 +0200, jkv wrote: I added a few lines to your script, and now it ought to only print files newer than 3601 seconds (3600 seconds is one hour). ... #if file newer than one hour print a line if time_difference 3601: That's a potential off-by-one error. That may print files that are older than one hour. Admittedly, they'll be off by less than one second, but if you're going to write code, write correct code. The right test is: if time_difference = 3600: (and you may even want to deal with files that have a *negative* time difference, e.g. they were created apparently in the future). This is particularly necessary if you use time.time() to generate the current time, since that returns fractions of a second. But even if you don't, it's still the right thing to do: it's defensive programming. Rather than assume that all file systems store timestamps accurate only to a second, assume that some file system, somewhere, will be accurate to fractions of a second, and code accordingly. That way, no matter what the file system does, your code will still do the right thing, and (in this case) it doesn't even cost you anything. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
mar...@hvidberg.net wrote: Thanks both The first answer is quite instuctive, the other one might be the one I'll use in t I didn't receive the other answer, could you please forward it to me? So 2x thanks. You are welcome. I took another look at your code, and you can compress it all to a if oneliner: (and thanks to Steven for the = reminder) if os.path.isfile(x): nSize = os.path.getsize(x) #if oneliner if time.mktime(time.localtime()) - time.mktime(time.localtime(os.path.getmtime(x))) = 3600: print ('HT fil - %s %d @ %s') % (x, nSize, time.asctime) -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
On May 30, 7:33 pm, mar...@hvidberg.net wrote: I made this little script (below) to look througt a dir to see if there are any files newer than .e.g. 1 hour. I have the loop through the dir working and can retreive file time as well as present time. both time variables are in the format returned by time.localtime() My question: How do I find the difference between such two time variables, to calculate the 'age' of the file? http://en.wikipedia.org/wiki/Subtraction This technique in well worth learning, as it can be applied in many real-world situations, sometimes not even requiring a computer. For example: The letter was postmarked on the 18th of May; it was delivered on the 29th of May; how many days was it in transit? You give a $10 note for an article priced at $6.35; how much change should you get? HTH, John -- http://mail.python.org/mailman/listinfo/python-list