Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: counting yesterdays hits in a logfile

by muba (Priest)
on Nov 13, 2012 at 03:11 UTC ( #1003547=note: print w/replies, xml ) Need Help??

in reply to counting yesterdays hits in a logfile

First of all, have you seen how Perlmonks tries to format your log file example? Maybe you should put your log file inside code tag as well.

Secondly, and more relevant to your question, your variable $yesterday contains the time it was exactly 24 hours ago. Yesterday contained 24 * 60 * 60 = 86400 seconds, and $yesterday covers exactly one of those - therein lies the basis of your problem.

One way (of many) to solve this, is to calculate two values - $yesterday_start and $yesterday_end, and then check if the time stamp of the line in the log file lies between these two values. DateTime might prove useful. But beware of edge cases such as those days that switch from or to DST and other tricky business like that.

Another way could be to take $yesterday the way you do it now, and strip off the time information so that you only hold the date information. That's probably the easiest, because except for the way you obtaini $yesterday your code can run unaltered. But again, be careful that you don't run into problems on days that DST ends.

And finally, these days the three-arguments open (which you already use) with lexical filehandles is recommended. See perldoc open.

Replies are listed 'Best First'.
Re^2: counting yesterdays hits in a logfile
by jrp370 (Initiate) on Nov 13, 2012 at 21:36 UTC

    thank you for your time I used your advice and changed how I was getting the value of $yesterday and formatted it to match my log file, which made counting the hits a lot simpler.

    my $yesterday = strftime("%d/%b/%Y",localtime(time()-86400));

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003547]
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-05-26 16:05 GMT
Find Nodes?
    Voting Booth?