Hello Monks, I am pretty much new to Perl and writing a log parsing application,
where i need to compare the time stamp of every line in the log file to the previous line.
I tried using the DateCalc function of Date::Manip , but its taking ever to complete a file with some 65000 lines.
So thought of writing my own function. And below is the code snippet.

my (@d_in_m) = ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); my $t1 = "2009-04-12 23:59:55 PM"; my $t2 = "2009-03-30 00:00:02 PM"; my @date1 = split( /-| |:/, $t1 ); my @date2 = split( /-| |:/, $t2 ); my $delta_sec = 0; sub month_delta { my ( $from_month, $to_month, $from_year, $to_year ) = @_; my $delta = 0; if ( ( $to_year > $from_year ) && ( $to_month > $from_month ) ) { $delta += ( $to_year - $from_year ) * 31536000; } for ( my $i = ( ( $from_month + 1 ) > 12 ) ? 1 : ( $from_month + 1 ) + ; $i < $to_month ; $i++ ) { $delta += ( $d_in_m[$i] * 86400 ); } return $delta; } my ( $date1_ref, $date2_ref ) = ( \@date1, \@date2 ); if(($date2[0]<= $date1[0]) && ($date2[1]< $date1[1]) ){ print "swapping the date \n"; ($date1_ref, $date2_ref) = ($date2_ref, $date1_ref); } if ( $date2_ref->[0] . $date2_ref->[1] eq $date1_ref->[0] . $date1_ +ref->[1] ) { $delta_sec = $date2_ref->[5] - $date1_ref->[5]; $delta_sec += ( $date2_ref->[4] - $date1_ref->[4] ) * 60; $delta_sec += ( $date2_ref->[3] - $date1_ref->[3] ) * 3600; $delta_sec += ( $date2_ref->[2] - $date1_ref->[2] ) * 86400; } elsif ( $date1_ref->[2] <= $d_in_m[ $date1_ref->[1] ] ) { $delta_sec = $date2_ref->[5] - $date1_ref->[5]; $delta_sec += ( $date2_ref->[4] - $date1_ref->[4] ) * 60; $delta_sec += ( $date2_ref->[3] - $date1_ref->[3] ) * 3600; $delta_sec += ( $date2_ref->[2] ) * 86400; $delta_sec += ( $d_in_m[ $date1_ref->[1] ] - $date1_ref->[2] ) * 86400 + month_delta( $date1_ref->[1], $date2_ref->[1], $date1_ref->[0], $date2_ref->[0] ); } else { print "Seems to be a invalid comparison delta b/w $t1 and $t2\ +n"; } print "!!!! Delta b/w $t1 and $t2 is " . $delta_sec . "\n";


I have tested this for several combinations and it seems to work.
Yet to handle the leap year case which is any ways of least needed for my program.

This is much more faster than the Date::Manip module.

Please look at this and let me know if i can make it more optimized or missed out any scenarios.


If needed you can use this code also :-)


Thanks

In reply to Code to calculate Delta b/w two dates in seconds by snra_perl

Title:
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, details, 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, summary, 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.