I recommend the Date-Manip module for most date calculations in perl.
Sadly, this is one of those date formats that Date::Manip does not currently automatically detect, so you have to use an explicit format string to parse it. If you try to parse the dates with an explicit format string, you will find that they are invalid, because Mar 30, 2013 was a Saturday, but the first line says "Mon" instead of "Sat". The other two dates are wrong as well. So let's parse the dates but ignore the day of week given in the input, and print the correct date 30 days before that.
use warnings; use Date::Manip::Date 6.30;
my$base = Date::Manip::Date->new; $base->config("setdate" => "now,utc"
+);
my $delta = $base->new_delta("-30 days");
open my$infile, "<", "in.txt" or die;
while (<$infile>) {
my$cur = $base->new;
my $parse_err = $cur->parse_format(q"\w+ %b %d %H:%M:%S %Y", $_);
if ($parse_err) {
1; # input line does not contain a valid date, just skip it
} else {
my$mod = $cur->calc($delta);
print $mod->printf("%a %b %d %H:%M:%S %Y");
}
print "\n";
}
__END__
The output you get is this:
Thu Feb 28 10:00:00 2013
Wed Feb 27 10:00:00 2013
Tue Feb 26 10:00:00 2013
See also the related questions: Find 30 days from today's date and Re: Can Date::Manip parse a unix timestamp?.
|