I think most of you would agree that the cal command output is correct.

That depends... Wikipedia says that the ISO week number 1 "has 4 January in it." So it appears to me that your output of the cal command does not correspond to that definition, and in fact cal (or rather, ncal, since my cal doesn't have -w) on my system outputs something different:

$ ncal -w 1 2016 Januar 2016 Mo 4 11 18 25 Di 5 12 19 26 Mi 6 13 20 27 Do 7 14 21 28 Fr 1 8 15 22 29 Sa 2 9 16 23 30 So 3 10 17 24 31 53 1 2 3 4

Is your cal's -w option perhaps giving you the "week of month"? (DateTime's ->week_of_month, see also)

So DateTime's output for ->week_number seems to match the ISO definition:

use warnings; use strict; use DateTime; my $dt = DateTime->new(year=>2015,month=>12,day=>30); for (1..10) { print $dt->ymd, " week ",$dt->week_number,"\n"; $dt->add(days=>1); } __END__ 2015-12-30 week 53 2015-12-31 week 53 2016-01-01 week 53 2016-01-02 week 53 2016-01-03 week 53 2016-01-04 week 1 2016-01-05 week 1 2016-01-06 week 1 2016-01-07 week 1 2016-01-08 week 1

If you have a different definition of "week number" from those two (week of month, or ISO week), you'll have to define that clearly, and then there's probably a way to do some DateTime math to derive that week number.


In reply to Re: Another date question by haukex
in thread Another date question by sshingor

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.