Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
While working on a small log monitoring utility, I encountered a few issues trying to convert datetime string appearing in a log entry to a valid localtime value such that localtime($logtime_in_seconds) would represent the exact time the log entry was made.

I've played with a number of Date:: modules to no avail. One particular module that I thought would help is Date::Manip. Here's the code snippet that demonstrates the problem (or shell I call it a 'bug'?):
use strict; use Date::Manip; my $line = qq{ - - [15/May/2003:01:05:02 -0600] "GET /foobar"}; my( @mon ) = ( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", + "Sep" , "Oct", "Nov", "Dec" ); my $i = 0; my %mon = map { $_ => ++$i } @mon; if ($aline =~ m/ ([\d\.]+) # IP .*?\[([^\s]+?)\s # date ([-+]\d+) # zone \]\s (.*) # text /x) { my ($ip, $date, $zone, $text) = ($1, $2, $3, $4); print "($ip, $date, $zone, $text)\n"; my ($d, $mon, $timestr) = split("/", $date); $d =~ s/^0//g; my ($y, $h, $mn, $s) = split(":", $timestr); # calculate zone offset (in seconds) my $zf = ($zone =~ m/^-/) ? -1 : 1; # zone factor my @z = split("",$zone); shift @z; my $zsec = $z[0]*216000+$z[1]*3600+$z[2]*60+$z[3]; print "Log date values: $mon{$mon}, $d, $y, $h, $mn, $s\n"; print "*** ERROR ***>> Date_SecsSince1970($mon{$mon}, $d, $y, $h, +$mn, $s) = " . localtime(Date_SecsSince1970($mon{$mon}, $d, $y, $h, $mn, $s)) + . " <<***\n"; my $time = Date_SecsSince1970($mon{$mon}, $d, $y, $h, $mn, $s) + $ +zf*$zsec; print "parsed time: ".localtime($time)."; original: $date $zone\n" +; }
This produces the following output:
(, 15/May/2003:01:05:02, -0600, "GET /foobar") Log date values: 5, 15, 2003, 01, 05, 02 *** ERROR ***>> Date_SecsSince1970(5, 15, 2003, 01, 05, 02) = Wed May +14 18:05:02 2003 <<*** parsed time: Wed May 14 12:05:02 2003; original: 15/May/2003:01:05:02 +-0600
Note the *** ERROR *** line. While the log date value is 15 (of May), the Date_SecsSince1970() method converts it to 14th (or, to be precise, it returns an invalid second count value by, apparently, missing a day). You may disregard the hour value as this may be related to time zone offset calculations (the $zsec variable).

Having played with this module for awhile, I now feel it may not be the right tool in this case. Has any of you come across similar issues? What would be the best module / approach to use?

"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true."

Robert Wilensky, University of California

In reply to Converting datetime string to valid localtime value (seconds) by vladb

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (8)
    As of 2020-07-06 21:25 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found