Your code, slightly rewritten to make it more readable: (reading a one-liner on a site is not easy.)

#!/usr/bin/perl -l use strict; use warnings; use Time::Local; use POSIX qw(strftime); my $fmt = "%A"; # %a = abbreviated weekday %u = weekday number; my $yr1 = 2008 - 1900; for my $yday1 (272, 273, 274) { my $mytime = timelocal(1,0,0,1,1,$yr1); $mytime += ( 86400 * $yday1); my ($sec,$min,$hour,$day,$mon,$yr,$wday,$yday,$whocares) = localtime +($mytime); $yr += 1900; printf("%d/%02d/%02d%s", $yr, $mon, $day, " " ); my $weekday = strftime($fmt, 0, 0, 0, $day , $mon - 1, $yr - 1900, - +1, -1, -1); printf("%s\n", "$weekday"); }

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
$yr += 1900; should be $yr += 1900;$mon += 1;

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.
This would turn your code into:

#!/usr/bin/perl -l use strict; use warnings; use Date::Calc qw/Add_Delta_Days Date_to_Text/; my $yr1 = 2008; for my $yday1 (272, 273, 274) { print Date_to_Text(Add_Delta_Days($yr1, 1, 1, $yday1)); }

Benefits?

Update: Added: The meaning of your output was
Update2: changed some dates from dd/mm/yyyy into yyyy/mm/dd.


In reply to Re: Number of day in the year to proper date by Animator
in thread Number of day in the year to proper date by gio001

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.