If you have the seconds-since-epoch integer value, like time produces, it's easy to get the date of any number of seconds ago. Problems can arise due to Daylight Saving Time, but starting out from (around) noon on any day, you can always reliably derive the date for $x days in the past. Depending on whether you started out using timegm() or timelocal(), you can use gmtime() or localtime() to regenerate a string — possibly complemented with POSIX' strftime(), for more formatting options.

Starting from a string, there are various date parsing modules available. Personally, I like Time::Local, a standard module, to convert date parts back into seconds since epoch. A bit of regexp manipulation, and you can easily pull the date parts out of the string, and you can complement them with your own values, such as 12 for the hour, for solving that DST problem I mentioned above.

for ($year, $month, $day) { if (/\b\d{1}\b/) { $_ = "0$_"; } }
Don't do that, use sprintf instead. sprintf "%02d", $n formats any number with 2 digits, using a leading 0 if necessary. And you can use it to produce a whole date string consisting of multiple parts, at once. Or like I said, use strftime() from POSIX.

As an example, here's my code that does the equivalent from yours, for dates between 1970 and 2035 (or so). Much shorter!

#!/usr/local/bin/perl -wl use Time::Local; use POSIX 'strftime'; sub get_yesterday { my $date = shift; my ($year, $month, $day) = split/-/,$date; my $time = timegm(0, 0, 12, $day, $month-1, $year); return strftime "%y-%m-%d", gmtime($time - 24*60*60); } print get_yesterday("2005-04-01");
I get:
05-03-31

In reply to Re: Getting yesterday's date, and random dates in the past by bart
in thread 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.