Using the Time::Local Module
All this information about obtaining the
current number of Epoch seconds and converting it to a human-readable
time string is fine, but in this case we actually need to go in the
opposite direction. That is, we start with
human-readable time
strings from the log file, and we want to convert them to Epoch
seconds so that we can easily determine the elapsed time between
accesses. There is no built-in Perl function to do this, but there is
a standard module
(that is, a module that comes with
the standard Perl distribution) called Time::Local
that lets you do it.
To see how it works, let’s return to the
log_report.plx
script begun in Chapter 8. The first thing we need to do is to pull in
that Time::Local
module with the following line
added up near the top of the script:
use Time::Local;
Next, we’ll add the following
%month_num
hash
just below it, to let us easily translate from the three-letter month
names in the log ('Jan'
, 'Feb'
,
etc.) to the corresponding offset numbers used by
localtime
:
my %month_num = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11, );
Now we’re ready to create the
&get_seconds
subroutine, which we do by adding the following down at the bottom of
the script:
sub get_seconds { # this subroutine accepts a date string of the form # '06/Jul/1999' and a time string of the form '12:14:00' # and returns the number of seconds since the Unix # epoch, as determined ...
Get Perl for Web Site Management now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.