Skip to Main Content
Perl for Web Site Management
book

Perl for Web Site Management

by John Callender
October 2001
Beginner content levelBeginner
528 pages
15h 20m
English
O'Reilly Media, Inc.
Content preview from Perl for Web Site Management

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Web Client Programming with Perl

Web Client Programming with Perl

Clinton Wong
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

Ken Williams, Dave Rolsky

Publisher Resources

ISBN: 1565926471Catalog PageErrata