Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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.


Comment on Re: counting yesterdays hits in a logfile
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003547]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2014-07-10 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls