Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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]
[james28909]: but then you have the others as well
[Lady_Aleena]: Renaming things like get_THAC0 to just THAC0 was easy. These are harder.
[james28909]: consolidate the three subs into one
[Lady_Aleena]: Um, what?
[james28909]: check is is data or hash or array and do tasks then return needed data
[Lady_Aleena]: james28909, you might want to look at the other two on my scratchpad.
[james28909]: after you send to a sub, you can check if it is array or ref ect with ref

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-05-24 04:50 GMT
Find Nodes?
    Voting Booth?