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.
sub is_leap_year { my $year = shift; if($year%100 == 0) { return 1 if $year%400 == 0; } else{ return 1 if($year%4 == 0); } return undef; }
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 | |
by swampyankee (Parson) on Dec 18, 2006 at 15:33 UTC |