The following will do your conversion for you, assuming that you're trying to do something more complex than just output a localtime value in a different format:
use strict;
use warnings;
my $date = 'Wed Dec 29 08:17:04';
$date = convert($date);
print $date;
BEGIN {
my %c_mon = ('Jan',1,'Feb',2,'Mar',3,'Apr',4,'May',5,'Jun',6,
'Jul',7,'Aug',8,'Sep',9,'Oct',10,'Nov',11,'Dec',12);
sub convert {
my ($mon, $day, $rest) = $_[0] =~ /\w+ (\w+) (\d+) (.+)/; $mon
+ = $c_mon{$mon};
@_ = localtime(); $_[4]++; my $year = $_[5] % 100;
$year-- if "$mon$day" gt "$_[4]$_[3]"; $year = 99 if $year ==
+-1;
return sprintf('%02d', $year) . "-$mon-$day\_$rest";
}
}
As you can see, much of the effort is expended in determining the year, which isn't given in the original date. I've assumed that a date with a month / day after the current month / day is one in the previous year, and that all other dates are in the current year.
If you just want a localtime value in the format given, the following works:
use strict;
use warnings;
@_ = localtime();
my $date = sprintf('%02d-%02d-%02d_%02d:%02d:%02d',
$_[5] % 100, $_[4] + 1, $_[3], $_[2], $_[1], $_[0]
+);
print $date;
I'm assuming you want a 24-hour time.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.