Output:use Modern::Perl qw/2014/; use DateTime; use DateTime::Format::Flexible; use DateTime::Duration; my %data; while (<DATA>) { chomp; my ( $datetime, $msecs, $id, $description ) = split /,/; $msecs *=1000000; $datetime .= ":$msecs"; my $dt = DateTime::Format::Flexible->parse_datetime($datetime); $data{$id}->{$description} = $dt; } for my $id ( keys %data ) { print "Results for $id: "; if ( exists $data{$id}{'P5 Move request'} and exists $data{$id}{'TP Service Request'} ) { my $duration = $data{$id}{'P5 Move request'} ->subtract_datetime_absolute( $data{$id}{'TP Service Request +'} ); say "$data{$id}{'P5 Move request'} -> $data{$id}{'TP Service Request'} = " +, $duration->seconds + $duration->nanoseconds/1000000000, ' se +conds'; } else { say 'Missing data'; } } __DATA__ 2014-05-29 10:22:21,880,165ab6a8-e736-11e3-8748-8d365226be24,TP Servic +e Request 2014-05-29 10:22:21,962,165ab6a8-e736-11e3-8748-8d365226be24,ProcessNa +me: TC 2014-05-29 10:22:21,965,165ab6a8-e736-11e3-8748-8d365226be24,P5 Starte +d (...) 2014-05-29 07:14:04,048,1770ebca-e722-11e3-b793-c6903cc19f13,P5 Move r +equest
As you see, calculating the time between a "P5 Move request" and "TP Service Request" gets really easy: a few look-ups in the data structure is all that is needed. As the datetime is a DateTime-object, calculating the difference (i.e. duration) between two datetimes requires no complicated calculations.Results for 1770ebca-e722-11e3-b793-c6903cc19f13: Missing data Results for 165ab6a8-e736-11e3-8748-8d365226be24: 2014-05-29T10:23:08 +-> 2014-05-29T10:22:21 = 46.426 seconds
Two more comments:
Update: Amended code to take into account the millisecond field in the timelog.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
My blog: Imperial DeltronicsIn reply to Re: processing timelog
by CountZero
in thread processing timelog
by sandy105
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |