Dear Perl-ites,
I have a son working on his PhD in medical informatics, trying to
parse "portal" logs... and I THOUGHT I could help him extract the
time- His data feed covers serval years... so many time
calculations are required.
I read Dave Rolsky's "The Many Dates and Times of Perl",
and decided to try converting date-time strings into epoch-seconds
with Barr's TimeDate-1.16, which I installed from CPAN last night.
The date and time info from the portal log file
looks like this: Mon Aug 3, 2006 3:45 AM (no seconds)
I Perl-ed it into: Mon, 3 Aug 2006 3:45:00 GMT
which is one of the formats which str2time is supposed to parse.
str2time converted it into epoch-seconds of: 1154576700
As a check, I immediately round-tripped this through time2str,
and got an inconsistent day-of-week: Thu Aug 3 03:45:00 GMT 2006
What have I missed??? Do I have fighting use-statements?
Here are my particulars:
sys: cygwin on Win2K, fresh install 5 Oct 2006
perl -ver: This is perl, v5.8.7 built for cygwin-thread-multi-64int
Perl-code:
Main program has these "Use" statements
use File::Basename;
use Getopt::Std;
use Time::Local
... code omitted
$epoch_sec = to_epoch("Mon Aug 3, 2006 3:45 AM");
...
# takes input containing portal-time
sub to_epoch
{
use Date::Parse;
use Date::Format;
local($ln1,$ln2,$time,$template);
local($dow,$mon,$dom,$year,$hr,$min,$ampm); # vars per input format
$ln1 = shift;
$ln1 =~ s/,//g; # strip comma from after day-of-month
$ln1 =~ s/:/ /g; # replace colon between hr:min with space
($dow,$mon,$dom,$year,$hr,$min,$ampm) = split / /, $ln1;
if ($ampm =~ m/PM/) {
$hr = $hr + 12;
}
# format the line for the Date::Parse module
$ln2 = $dow . ", " . $dom . " " . $mon . " " . $year . " " . $hr . ":" .
$min . ":00 GMT";
$time = str2time($ln2);
# test it, including a round-trip back to string
# template specifies string format... keep it simple
$template = "%C"; # Date::Format ctime format
if ($debug == 3) {
print "\t\t to_epoch: mod'd = [$ln1] \n";
print "\t\t to_epoch: built = [$ln2] \n";
print "\t\t to_epoch: epoch = [$time] \n";
print "\t\t to_epoch: back = [", time2str($template, $time, "GMT"), "]
\n";
}
return $time;
}
Results:
to_epoch: mod'd = [Mon Aug 3 2006 3 45 AM]
to_epoch: built = [Mon, 3 Aug 2006 3:45:00 GMT]
to_epoch: epoch = [1154576700]
to_epoch: back = [Thursday Thu Aug 3 03:45:00 GMT
2006]
Any help appreciated,
Jim