in reply to Date munging suddenly broken

It never worked right. here are my results for Oct 3 backwards 8 days...
28, 9, 2000
28, 9, 2000
28, 9, 2000
28, 9, 2000
28, 9, 2000
27, 9, 2000
21, 9, 2000
21, 9, 2000

That isn't right at all... How about this:

sub next_friday { my $now = shift || time(); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$rest) = localtime($now); my $then = (($wday-5) * 60 * 60 * 24); ($sec,$min,$hour,$mday,$mon,$year,$rest) = localtime($now - $then); $mon += 1; $year += 1900; return "$year.$mon.$mday"; }

That is evil as heck but it works. If you want this friday to return next friday, you can ++$wday before the $then calc.

Notice I fancied it up enough that you can send it arbritary times or it will default to time(). Notice I made it a sub. Notice what I did to the logic.

Now, forget all that except as a basic lesson in how things work and use Date::Manip =)

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
RE: RE: Date munging suddenly broken
by btrott (Parson) on Oct 05, 2000 at 02:44 UTC
    It's not a big deal, but if it were me, I wouldn't want to pollute my code with a bunch of unnecessary lexicals. I prefer to use array slices. So those two localtime calls would become:
    my $wday = (localtime $now)[6]; .... my($mday, $mon, $year) = (localtime($now-$then))[3..5];
    Which, in my opinion, is cleaner.