in reply to Another date question
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.
|
|---|