lamp has asked for the wisdom of the Perl Monks concerning the following question:
I'm following no modules approach to find the day of year based on the user provided date.
--Binoj
It's printing the correct value. Is there any way to optimize the above code / do it in a better way?#!/usr/local/bin/perl sub dayofyear { ($day1,$month,$year)=@_; my(@d_in_m)=(0,31,28,31,30,31,30,31,31,30,31,30,31); $d_in_m[2]=29 if (&leap($year)); for($i=1;$i<$month;$i++) { $k += $d_in_m[$i]; } $k += $day1; return $k; } sub leap { $y = shift; return 0 unless $y % 4 == 0; return 1 unless $y % 100 == 0; return 0 unless $y % 400 == 0; return 1; } print dayofyear(10,3,2005);
--Binoj
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: calculate day of year
by Corion (Patriarch) on Feb 22, 2005 at 08:26 UTC | |
Re: day of year calculation
by reneeb (Chaplain) on Feb 22, 2005 at 11:48 UTC | |
Re: day of year calculation
by betterworld (Curate) on Feb 22, 2005 at 11:56 UTC | |
Re: calculate day of year
by slayven (Pilgrim) on Feb 22, 2005 at 09:19 UTC | |
Re: day of year calculation
by mlh2003 (Scribe) on Feb 22, 2005 at 13:52 UTC | |
by fglock (Vicar) on Feb 22, 2005 at 14:53 UTC |
Back to
Seekers of Perl Wisdom