Specifying a time from previous day

Alex Martelli aleax at aleax.it
Sun Apr 14 15:34:45 EDT 2002


jrup wrote:

> Hi,
> 
> This is my first post to this group and I am new to python. I have some
> experience with perl and vbscript for sysadmin-type scripting, but am not
> a programmer.
> 
> I need to search some log files, but only if they were created after
> 4:00pm the previous day.

>>> import time
>>> time.time()
1018811834.620291

this gives us the time in seconds since some arbitrary starting point
("the epoch").  24 hours ago was clearly 24*60*60 seconds ago, i.e.:

>>> time.time()-24*60*60
1018725558.410575

Given this representation for time, we can obtain one in terms of
year-month-day-hour-minute-second-etc (9-items tuple) -- say
in local time (GMT, aka UTC, is the alternative):

>>> yesterday = time.localtime(time.time()-24*60*60)
>>> yesterday
(2002, 4, 13, 21, 20, 29, 5, 103, 1)
>>>

As you want a given time, you'll only get the first three items of
this (year, month, day), then your specified time (16, 0, 0), then
2 zeros, and finally the same last item as yesterday (DST flag: this will 
break by 1 hour when DST has just started or finished -- if you could have 
your specs in UTC...); time.mktime can rebuild the "time in seconds
since the epoch" from this:

>>> threshold = time.mktime(yesterday[:3]+(16,0,0)+(0,0,yesterday[-1]))
>>> threshold
1018706400.0

When you examine a file, os.stat gives you a lot of data about it:

>>> os.stat('/home/alex/.bashrc')
(33188, 32453L, 776L, 1, 501, 501, 403L, 1018648057, 1018267187, 1018267187)

The last three numbers are times in the seconds-since-the-epoch
form -- last access, modification, inode change.  It appears that what
you want is to look at files such that os.stat(thefilename)[-2] > threshold,
where threshold is computed as above.


I hope this information is enough for you to complete your program.


Alex




More information about the Python-list mailing list