Reputation: 13
I have an example string parsed from a log file in the following format:
"Mon Apr 25 17:47:19 2011"
and I want it to look like
"Mon Apr 25 05:47PM 2011"
How would I do this in general? (i.e. it's not going to be the same string every time of course).
Thanks so much.
Upvotes: 1
Views: 2315
Reputation: 239801
When in doubt, it's usually a good bet to go with DateTime. In this case you could:
use DateTime;
use DateTime::Format::Strptime;
my $parser = DateTime::Format::Strptime->new(
pattern => '%a %b %d %H:%M:%S %Y'
);
my $dt = $parser->parse_datetime('Mon Apr 25 17:47:19 2011');
print $dt->strftime('%a %b %d %I:%M%p %Y'), "\n";
If you need information on the patterns, see the strftime docs or the similar section in the DateTime Manual.
You could also work this using the strftime
function in POSIX and the strptime
function in POSIX::strptime, but the interfaces are less enjoyable.
Upvotes: 10