http://www.perlmonks.org?node_id=190401

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a log of working hours which I'd like to parse. The idea is that I calculate the net hours I've done from this file and insert these values into a MySQL db. The log file has the following format (one entry per line):

dd-mm-yy hh:mm am-dd-mm-yy hh:mm pm

I've been using the regex below to pull out all the values (which works fine) and work out the hours. The one small hitch is that I need to convert pm hours into 24h format for the calculation to work (i.e. 3pm should become 15h). I've tried to do this with a conditional but cannot get it to evaluate properly - all times, regardless if am or pm, get converted to 24h format.
@lines = <LOGFILE>; foreach $line (@lines) { if ($line != m/\s/) { # regex to turn dates into epoch ($dd, $mm, $yy, $hh, $mt, $tt, $tdd, $tmm, $tyy, $thh, $tmt, $ttt) = $ +line =~ /(\d+)-(\d+)-(\d+)\s(\d+):(\d+)\s(am|pm)-(\d+)-(\d+)-(\d+)\s( +\d+):(\d+)\s(am|pm)/; if ($tt == 'pm') { $hh = $hh + 12; } if ($ttt == 'pm') { $thh = $thh + 12; } $t_from = timelocal(0,$mt,$hh,$dd,$mm,$yy); $t_to = timelocal(0,$tmt,$thh,$tdd,$tmm,$tyy); $s_elapsed = $t_to - $t_from; ...
I'm guessing that this is one of those extremely simple problems - I just can't see where I'm going wrong. Any help would be greatly appreciated!

Mark