http://www.perlmonks.org?node_id=1003547


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));