Specifying a time from previous day

jrup jrup at mail.com
Sun Apr 14 17:06:54 EDT 2002


Thank you both for your replies.

I worked on this some more after I'd posted and it turns out I'd ended up
doing basically what Alex suggested. I'd wondered about DST, so I'll work on
redoing it using UCT. I'd pretty much had the file stats part worked out.

Here's what I did, it's not as elegant, but I think it's the same idea:

# current local time in seconds
time.time()

# time 24 hours prior to now in seconds
yesterday = time.time() - 24 * 60 * 60

# put time 24 hours ago into a tuple
YesterdayLocal = time.localtime(yesterday)

# reassign the tuple elements to another tuple so they can be changed
YesterdayTuple = YesterdayLocal[0:]

# then change the hour and everything after it using the info from the
previous day
YesterdayFourPm = (YesterdayTuple[0], YesterdayTuple[1], YesterdayTuple[2],
16, 0, 0, 0, 0, 0)

# change the new yesterday 4:00pm time into seconds to be the constant
# against which the times of files in the log directory will be compared
YesterdayFourPmInSecs = time.mktime(YesterdayFourPm)

# then change the file creation times into seconds and compare with the
# new constant

Aside from the wordiness, and lack of UTC, is this the right idea?
Thanks again,
Jim


"Alex Martelli" <aleax at aleax.it> wrote in message
news:pRku8.15554$b62.440441 at news1.tin.it...
> 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