Perl Notes/spamtrainreport.pl

From Federal Burro of Information
Revision as of 02:16, 30 January 2012 by David (talk | contribs) (Created page with "<pre> #!/usr/bin/perl -w use strict; use DateTime; use DateTime::Format::Duration; my $foundtrainstart = 0; my $trainstart; my $trainend; my $datetimedurationformat = DateTime...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

#!/usr/bin/perl -w

use strict;
use DateTime;
use DateTime::Format::Duration;

my $foundtrainstart = 0;
my $trainstart;
my $trainend;
my $datetimedurationformat = DateTime::Format::Duration->new( pattern => '%s' );

while (<>) {

    if ( /Starting spamassassin training/ ) {
        if ( /(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)\sStarting spamassassin training.$/ ) {
            $trainstart = DateTime->new( year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6 );
            $foundtrainstart = 1;
        }
    }
    if ( /Finished spamassassin training./) {
        if ( /(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)\sFinished spamassassin training./) {
            if ( $foundtrainstart eq 1) {
                $trainend = DateTime->new( year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6 );
                my $elapsed = $trainend-$trainstart;
                print "train ".$trainstart." : ".$datetimedurationformat->format_duration($elapsed)."\n";
                $foundtrainstart = 0;
            }
        }
    }

}