Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date::Manip Help 1

Status
Not open for further replies.

nfaber

Technical User
Joined
Oct 22, 2001
Messages
446
Location
US
I am trying to use the Date::Manip module to check the difference between the time of 2 events. I have a log file I am reading:

12:25:19 10/21/03 0 155.49.10.30 421364 1.3.6.1.4.1.11.2.3.2.3 4 0

12:25:24 10/21/03 0 155.49.10.30 421864 1.3.6.1.4.1.11.2.3.2.3 4 0

12:25:29 10/21/03 0 155.49.10.30 422365 1.3.6.1.4.1.11.2.3.2.3 4 0

12:30:19 10/21/03 0 155.49.10.30 451376 1.3.6.1.4.1.11.2.3.2.3 4 0

12:30:24 10/21/03 0 155.49.10.30 451878 1.3.6.1.4.1.11.2.3.2.3 4 0


Here is my code, using the example form the perldoc:

#!D:\perl\bin\perl

use Date::Manip;

my $log_path = "d:\\TrapRec\\TrapLog\\";
my $count=0;
open ( TRAPLOG, "$log_path\\TrapRcvr.log" ) or die ("Cannot open trap log $1" );

while (<TRAPLOG>) {
chomp;
next if $_ =~ /^\s*$/;
$count++;
($time, $date, $junk1, $ip, $junk2, $oid, $junk3, $junk4 ) = split (/\s+/);
$date1 = (&quot;$time $date&quot;) if ($count = 1);
$date2 = (&quot;$time $date&quot;) if ($count = 10);
next if ($count != 10);
$strdate=ParseDate($date1);
$enddate=ParseDate($date2);
$delta =DateCalc($strdate,$enddate,\$err);
print &quot;$delta\n&quot;;
}

For an output, I get a long string of:

+0:0:0:0:0:0:0


What am I doing wrong?
 
The problem lies with the way you are building $date1 and $date2.

ParseDate calls the routine ParseDateString. ParseDateString then combs through your data to do the operations that you desire, but ParseDateString cannot accept an arbitrary ordering of time and date. Here are the valid formats that ParseDateString will accept:

YYYYMMDDHHMNSSF...
YYYYMMDDHHMNSS
YYYYMMDDHHMN
YYYYMMDDHH
YY-MMDDHHMNSSF...
YY-MMDDHHMNSS
YY-MMDDHHMN
YY-MMDDHH
YYYYMMDD
YYYYMM
YYYY
YY-MMDD
YY-MM
YY
YYYYwWWD ex. 1965-W02-2
YYwWWD
YYYYDOY ex. 1965-045
YYDOY

Your format looks like this:

HH:MM:SS MM/DD/YY

 
Ok I made the changes to get the date in the right format, however now I get NO output. My program just throws out blank lines. Here is my new code:

#!D:\perl\bin\perl

use Date::Manip;

my $log_path = &quot;d:\\TrapRec\\TrapLog\\&quot;;
my $count=0;
open ( TRAPLOG, &quot;$log_path\\TrapRcvr.log&quot; ) or die (&quot;Cannot open trap log $1&quot; );

while (<TRAPLOG>) {
chomp;
next if $_ =~ /^\s*$/;
$count++;
($time, $date, $junk1, $ip, $junk2, $oid, $junk3, $junk4 ) = split (/\s+/);
($hr, $min, $sec) = split (/:/,$time);
($mon, $day, $yr) = split (/\//,$date);
$date1 = (&quot;$yr$mon$day$hr$min$sec&quot;) if ($count = 1);
$date2 = (&quot;$yr$mon$day$hr$min$sec&quot;) if ($count = 10);
next if ($count != 10);
$strdate=ParseDate($date1);
$enddate=ParseDate($date2);
$delta =DateCalc($strdate,$enddate,\$err);
print &quot;$delta\n&quot;;
}
 
Your $yr only contains &quot;03&quot; or YY, but you have built your date in the context of a YYYYMMDDHHMNSS format. You either need to add the first two digits to the date or create your date string like this:

$date1 = (&quot;$yr-$mon$day$hr$min$sec&quot;) if ($count = 1);
$date2 = (&quot;$yr-$mon$day$hr$min$sec&quot;) if ($count = 10);


 
Thanks raklet,

I changed my string to:

YY-MMDDHHMNSS per your suggestion.

I found the main problem for it not working as well.

$date1 = (&quot;$yr-$mon$day$hr$min$sec&quot;) if ($count = 1);
should be

$date1 = (&quot;$yr-$mon$day$hr$min$sec&quot;) if ($count == 1);
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top