MLUG: Re: [UUG/MLUG] perl and y2k
Re: [UUG/MLUG] perl and y2k
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


On Wed, 5 Jan 2000, Stephen Montgomery-Smith wrote:

> > So is there a commonly known solution to make localtime spit out 00 or
> > something that is a little more accurate than 100 (?)  I've seen 
> > this on several sites (including mine) =)
> 
> How about:
> 
> printf ('Submitted:  %02d/%02d/%02d  %02d:%02d:%02d', $month,
>   $mday, $year%100, $hour, $min, $sec);
> 
>           ^^^^^^

Yup, that's the right answer.  

Along these lines, and while we all run off to fix our code, some of you
might be interested in the fact that perl does something potentially
unexpected with the % operator when the first argument is negative, so
that:

    [EMAIL:PROTECTED]$ perl -e '$a=-98; printf("%02d\n",$a%100);'
    02

I mention this because it is surprisingly easy to hose yourself with
calculations involving modulus operators.  (I say it's easy because I've
done it at least twice myself. :-)) There's a similar gotcha with
rounding, and serious perl hackers might want to consult their copy of the
Perl Cookbook on these and related issues.

jking