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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.