Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a fast way to get the week numeration in a year? For example, this week is 35th week in 2006, next week is 36th week of this year then.
  • Comment on Getting Ordinal Number of a Week in a Given Date

Replies are listed 'Best First'.
Re: Getting Ordinal Number of a Week in a Given Date
by bangers (Pilgrim) on Sep 01, 2006 at 08:09 UTC
    Date::Calc has a Week_of_Year method that should give you what you're looking for
Re: Getting Ordinal Number of a Week in a Given Date
by jwkrahn (Abbot) on Sep 01, 2006 at 11:35 UTC
    $ perl -MPOSIX -le'print strftime q/%W/, localtime' 35
Re: Getting Ordinal Number of a Week in a Given Date
by Skeeve (Parson) on Sep 01, 2006 at 13:01 UTC
    I just discovered some interesting facts about week number calculation...

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Getting Ordinal Number of a Week in a Given Date
by Praveen (Friar) on Sep 01, 2006 at 08:18 UTC
Re: Getting Ordinal Number of a Week in a Given Date
by cdarke (Prior) on Sep 01, 2006 at 08:24 UTC
    Without using a module:
    $doy = (localtime)[7]; # day of year $wk = int($doy/7); $wk++ if $doy % 7;

      This won't work. For example Jan 1. 2005 is first day. But week 53 (at least in Germany) of 2004.

      The OP also didn't take into account that the rules in different countries might differ. For example: In Germany weeks start on Mondays, in the US they start on Sundays (if I remember correctly). So the german calendar week rule is slightly differnt from the american. If I remember correctly week 1 of a given year is the week containing the first Wednesday of a year.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Interesting, and apologies. Does Date::Calc take this into account? Presumably POSIX::strftime is locale sensitive?

        In the US, it's a bit more complicated than "Sunday is the first day of the week." Calendars do have Sunday first. Most US companies where I've worked started their week on Monday. Some start their week on Sunday. I vaguely remember working at one which started its week on Saturday.

        Even disregarding which day starts the week, when week 1 starts varies.

        emc

        Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

        Albert Einstein