Normally the solution would be simply:
my $yesterday = time() - (24 * 60 * 60); my ($day, $month, $year) = localtime($yesterday)[3,4,5];
(Remember to add one to the value of $month and 1900 to the value of $year before printing them out.)

However, yesterday's date may not be twenty-four hours ago. The day on which daylight savings time (DST) ends is 25 hours long; the day on which DST begins is only 23 hours long.

Thus if you calculated the date 24 hours earlier during the last hour of the day DST ended you would get an erroneous result using the above formula, i.e., "yesterday's" and today's dates would be the same.

Similarly if you calculated the date 24 hours earlier during the first hour of the first whole day after DST began you would also get an erroneous result using the above formula: "yesterday's" date would be two days before today's date.

'perldoc perlfaq4' has a workaround by Russ Allbery that fixes the DST problem.

Update: Corrected above to state that the formula at top would provide incorrect results in the last hour of the day DST ended, not the first hour of the day after it ended.


In reply to Re: date by sierrathedog04
in thread Yesterday's or last month's date? by renpai

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.