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.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"; } }
If you just want a localtime value in the format given, the following works:
I'm assuming you want a 24-hour time.use strict; use warnings; @_ = localtime(); my $date = sprintf('%02d-%02d-%02d_%02d:%02d:%02d', $_[5] % 100, $_[4] + 1, $_[3], $_[2], $_[1], $_[0] +); print $date;
In reply to Re: Optimal Date conversion? (Too much information)
by TedPride
in thread Optimal Date conversion? (Too much information)
by McMahon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |