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

Re: [ID 20000307.005] Date Problem

Ronald J Kimball
March 7, 2000 08:42
Re: [ID 20000307.005] Date Problem
Message ID:
On Tue, Mar 07, 2000 at 04:23:12PM +0000, David Gillen wrote:
> Problem. Perl not showing the day correctly, Eg today is Tue March the 7th,
> the program get Tue March the 8th. Seems to be due to the 29 Feb Problem for
> this year.

> Example Code: (Don't laugh I know it ugly)

That's putting it mildly, I'm afraid.

> $weekday = (Sun,Mon,Tue,Wed,Thur,Fri,Sat)[(localtime)[6]];

> $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]];

The day of month argument does not start at 0; because it's supposed to be
a number anyway, it does not need to be indexed into an array.  You're
effectively adding 1 to the day of the month.

> $month =
> (January,February,March,April,May,June,July,August,September,October,Novembe
> r,December)[(localtime)[3]];
> print "It is $weekday the $day th of $month";

How many times do you need to call localtime anyway?

($wday, $mday, $mon) = (localtime)[6,3,4];
$weekday = (qw(Sun Mon Tue Wed Thu Fri Sat))[$wday];
$month   = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];

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