# FAQ 4.17 How do I find yesterday's date?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.

--------------------------------------------------------------------

4.17: How do I find yesterday's date?

If you only need to find the date (and not the same time), you can use
the Date::Calc module.

my @date = Add_Delta_Days( Today(), -1 );

print "@date\n";

Most people try to use the time rather than the calendar to figure out
dates, but that assumes that your days are twenty-four hours each. For
most people, there are two days a year when they aren't: the switch to
and from summer time throws this off. Russ Allbery offers this solution.

sub yesterday {
my \$now  = defined \$_[0] ? \$_[0] : time;
my \$then = \$now - 60 * 60 * 24;
my \$ndst = (localtime \$now)[8] > 0;
my \$tdst = (localtime \$then)[8] > 0;
\$then - (\$tdst - \$ndst) * 60 * 60;
}

Should give you "this time yesterday" in seconds since epoch relative to
the first argument or the current time if no argument is given and
suitable for passing to localtime or whatever else you need to do with
it. \$ndst is whether we're currently in daylight savings time; \$tdst is
whether the point 24 hours ago was in daylight savings time. If \$tdst
and \$ndst are the same, a boundary wasn't crossed, and the correction
will subtract 0. If \$tdst is 1 and \$ndst is 0, subtract an hour more
from yesterday's time since we gained an extra hour while going off
daylight savings time. If \$tdst is 0 and \$ndst is 1, subtract a negative
hour (add an hour) to yesterday's time since we lost an hour.

All of this is because during those days when one switches off or onto
DST, a "day" isn't 24 hours long; it's either 23 or 25.

The explicit settings of \$ndst and \$tdst are necessary because localtime
only says it returns the system tm struct, and the system tm struct at
least on Solaris doesn't guarantee any particular positive value (like,
say, 1) for isdst, just a positive value. And that value can potentially
be negative, if DST information isn't available (this sub just treats
those cases like no DST).

Note that between 2am and 3am on the day after the time zone switches
off daylight savings time, the exact hour of "yesterday" corresponding
to the current hour is not clearly defined. Note also that if used
between 2am and 3am the day after the change to daylight savings time,
the result will be between 3am and 4am of the previous day; it's
arguable whether this is correct.

This sub does not attempt to deal with leap seconds (most things don't).

--------------------------------------------------------------------

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.