I need to covert the format of a datetime variable found in a log file from format: "05/1/13 16:45" to format: "May 01 2013 16:45:00 GMT".
I presume the approach would be to parse the text datetime into a Perl datetime variable and format the results appropriately.
I can parse & load the variable, however I having trouble formatting it.
Any suggestions would be greatly appreciated.
use POSIX 'strftime';
use Date::Calc qw(:all);
$datetimestring = "4/5/13 16:09";
#split datetimestring into date & time
($date_raw, $time_raw) = split(/ /, $datetimestring);
print "the date_raw is: $date_raw \n";
#split the date into MM DD YY
($m0_raw, $d0_raw, $y0_raw) = split(/\//, $date_raw);
$m_raw = sprintf ("%02d", $m0_raw);
print "the m_raw is: $m_raw \n";
$d_raw = sprintf ("%02d", $d0_raw);
print "the d_raw is: $d_raw \n";
$y_raw = 2000 + $y0_raw;
print "the y_raw is: $y_raw \n\n";
print "the time_raw is: $time_raw \n";
#split the time_raw into HH MM
($hh_raw, $mm_raw) = split(/:/, $time_raw);
print "the hh_raw is: $hh_raw \n";
print "the mm_raw is: $mm_raw \n\n";
$s0_raw = 0;
$ss_raw = sprintf ("%02d", $s0_raw);
#load a Perl datetime variable using mktime
$datetime1 = Mktime($y_raw,$m_raw,$d_raw, $hh_raw, $mm_raw, $ss_ra
print "the loaded datetime variable is: $datetime1 \n";
#format the datetime variable to match the require format MON dd yyyy
$time2 = strftime(%b %d, %y %T, $m_raw, $d0_raw, $y_raw, $hh_raw,
print "the formatted date time is: $time2 \n";
The error result I get is:
Argument "d" isn't numeric in modulus (%) at playdate.pl line 36.
Illegal modulus zero at playdate.pl line 36.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||