in reply to Date Manipulation

As brian_d_foy said, "yes". There are numerous ways to do it.

Basically, find out today's date, and subtract 1 day. If this results in a day number of 0, subtract 1 from the month, and reset the day to the last day yesterday's month. If the new month is 0 (assuming January is 1), subract 1 day from the year, and set the month to December.

In principle, this is no different than any other sort of arithmetic involving borrowing and carrying.

So, you need one array: the number of days in each month (remembering that February sometimes has 29, not 28, days), the logic for determining leap year, (hint: 1900 was not a leap year), and the logic for when month and year boundaries are crossed.

Or you could use something like this, in the spirit of Corion's response):

$today = localtime; sleep(-86400); $yesterday = localtime; print "Yesterday's date was $yesterday\n";

This will use less wall clock time than Corion's solution, but it requires quite a lot of CPU time, or access to a naked singularity. Even so, sleeping for negative time periods is supported on very few O/S; you may need to install the Acme::Time::Reversal module from CPAN.

<

emc

At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

—Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

Replies are listed 'Best First'.
Re^2: Date Manipulation
by Corion (Patriarch) on Dec 18, 2006 at 11:44 UTC

    The DST-safe algorithm I use for calculating the daydate of yesterday or other relative date descriptions is to add/subtract 22 hours from the current gmtime() or localtime() until the date or day of week changes - this is a much simpler (and easier to debug) loop than maintaining any carry.

      I'm lazy. In Perl, I use one of the date modules. In Fortran -- where I did implement something like this -- DST adds another layer of horror that I contemplated and decided to ignore; nobody where I work is likely to be working between 6pm and 7am, so DST wasn't an issue. My general opinion of DST is f*** it, in any case.

      emc

      At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

      —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.