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?.


In reply to Re: Perl date calculation and formatting by ambrus
in thread Perl date calculation and formatting by Anonymous Monk

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.