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.

In reply to Re: Optimal Date conversion? (Too much information) by TedPride
in thread Optimal Date conversion? (Too much information) by McMahon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



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