develooper Front page | perl.perl5.porters | Postings from March 2000

Re: [ID 20000307.005] Date Problem

M.J.T. Guy
March 7, 2000 09:01
Re: [ID 20000307.005] Date Problem
Message ID:
David Gillen <> wrote
> Problem. Perl not showing the day correctly, Eg today is Tue March the 7th,
> the program get Tue March the 8th.


> $day =
> (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,
> 29,30,31)[(localtime)[3]];

Perl arrays by default are indexed from 0.    So that'll give you
the day+1, as you observe.    And that code's pretty silly anyway.
(localtime)[3] already gives you a day 1..31.

Other points in your code:  the month is (localtime)[4].
You'll save much typing by writing 1..31 instead of (1,2,3, ... ).
You should always read localtime as a single operation, else you may
get inconsistent day/month etc if your program runs as the time is
changing.  So write
($day,$mon,$wday) = (localtime)[3,4,6];
It's also more efficient that way (in CPU time and in typing).

Finally, please note that perlbug is for reporting bugs in Perl, not
a helpdesk for debugging user programs.    For programming help, you
could try the various comp.lang.perl.* newsgroups.   But make sure
you've done your homework first (i.e. RTFM and RTFFAQ).

Mike Guy Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About