more useful options | |
PerlMonks |
Re: Number of day in the year to proper dateby Animator (Hermit) |
on Oct 02, 2008 at 23:21 UTC ( [id://715111]=note: print w/replies, xml ) | Need Help?? |
Your code, slightly rewritten to make it more readable: (reading a one-liner on a site is not easy.)
The output of the code: 2008/09/30 Tuesday 2008/09/31 Wednesday 2008/10/01 Wednesday Why does it produce different dates? You got the monts wrong. January is month 0, february is month 1, .... In the code:
my $mytime = timelocal(1,0,0,1,1,$yr1); => Should be: my $mytime = timelocal(1,0,0,1,0,$yr1); and If you make the changes then the output will be: 2008/09/29 Monday 2008/09/30 Tuesday 2008/10/01 Wednesday The meaning of your output was: 2008/09/30 Tuesday => This really was 2008/10/30 with the weekday of 2008/09/30 2008/09/31 Wednesday => This really was 2008/10/31 with the weekday of 2008/10/01 2008/10/01 Wednesday => This really was 2008/11/01 with the weekday of 2008/10/01 An easy way to confirm the date is to add print scalar localtime($mytime); (and to change the $fmt = "%x";). Adding it in the original code will give you the following output: 2008/09/30 2008/30/09 Tuesday Thu Oct 30 00:00:01 2008 2008/09/31 2008/10/01 Wednesday Fri Oct 31 00:00:01 2008 2008/10/01 2008/10/01 Wednesday Sat Nov 1 00:00:01 2008 Now: on to what is wrong with the code?
The solution to all your problems is not to write the date calculation routines yourself. There are several modules out there that do an excellent job! For example: Date::Calc.
Benefits?
Update: Added: The meaning of your output was
In Section
Seekers of Perl Wisdom
|
|