[comp.lang.perl] Date/Time conversion

chip@osh3.OSHA.GOV (Chip Yamasaki) (05/08/91)

I have a question on Date and Time conversions.  I am writing a Perl
script to parse the UUCP logfiles to record the information in a
database.  The logfiles contain the date and time information in a
format like:

5/6-0:09:49

I would like to be able to take this and turn it back into the Unix
clock time (seconds since . . .).  If I have to I could load these
values into a tm type structure, but I can't find a function for that
either.

Does anybody out there have a function like this in their Perl library? 
Also, are there any Perl libraries out there with other such usefull
functions?


Thanks!
-- 
--
Charles "Chip" Yamasaki
chip@oshcomm.osha.gov

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/08/91)

In article <1991May07.184931.19141@osh3.OSHA.GOV> chip@osh3.OSHA.GOV (Chip Yamasaki) writes:
: I have a question on Date and Time conversions.  I am writing a Perl
: script to parse the UUCP logfiles to record the information in a
: database.  The logfiles contain the date and time information in a
: format like:
: 
: 5/6-0:09:49
: 
: I would like to be able to take this and turn it back into the Unix
: clock time (seconds since . . .).  If I have to I could load these
: values into a tm type structure, but I can't find a function for that
: either.
: 
: Does anybody out there have a function like this in their Perl library? 

Anyone with 4.0 does.  Incant this:

    ($thismon, $thisyear) = (localtime)[4,5];
    require 'timelocal.pl';
    ($mon,$mday,$hr,$mn,$sc) = $date =~ m#(\d+)/(\d+)-(\d+):(\d+):(\d+)#;
    $logyear = $thismon || $mon != 12 ? $thisyear : $thisyear - 1;
    $mon--;
    $secs_since_1970 = &timelocal($sc,$mn,$hr,$mday,$mon,$logyear);

The fancy stuff just lets you analyze a December log in January.  You might
need to pass an extra argument to &timelocal to account for Daylight
Savings Time.

Larry