in reply to Re: Re: where does the time go?
in thread where does the time go?

...okay then, how about this?

You didn't like this:

@t = (localtime)[0..5]; $this_month = $t[4]+1; $previous_month = $t[4];

But if it is changed slightly,

$this_month = (localtime)[4]; $previous_month = $this_month++ ? $this_month-1 : 12;

This idea raises hackles for me because it depends upon order of evaluation, and it "cleverly" hides the logic of its decision mechanism (as the conditional operator often does), but it does account for the border case.

---v

Replies are listed 'Best First'.
Re: where does the time go?
by ferrency (Deacon) on Apr 24, 2002 at 18:50 UTC
    That gets the month right, but still doesn't take the year into account.

    And, as others have stated (but I forgot): if you care about the day as well, then you need to add more code to make sure that "one month before March 31" results in "February 28th or 29th depending on whether it's a leap year" and not "February 31st".

    It all depends on what problem you're trying to solve. If all you need is the right month, then your solution works in a clever way. But you're right, it's not the clearest code in the world... :)

    Alan