I've got several scripts which rely on previous calendar dates. Not just yesterday, but possibly 60, 30, etc days in the past. A typical example would be to fill a hash with the caledar dates for the past 45 days. I wrote a subrouting which works fine. If I want a date other that yesterday, I just loop through the routine for the desired number of days. Is there a module which handles this type of date calculation easily? Or a standard way of handling this in Perl? I'm just looking for input.

My subroutine accepts a date such as 2005-03-01, and returns the previous date, similarly formatted.

sub get_yesterday { my $date = shift; my ($year, $month, $day) = split/-/,$date; $month--; my @days_per_month = qw(31 28 31 30 31 30 31 31 30 31 30 31); if (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) { $days_per_month[1] = '29'; } if ($day == 1) { if ($month == 0) { $month = '11'; $year--; } else { $month--; } $day = $days_per_month[$month]; } else { $day--; } $month++; for ($year, $month, $day) { if (/\b\d{1}\b/) { $_ = "0$_"; } } return "$year-$month-$day"; }

In reply to Getting yesterday's date, and random dates in the past by mhearse

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.