in reply to Getting Ordinal Number of a Week in a Given Date

Without using a module:
$doy = (localtime)[7]; # day of year $wk = int($doy/7); $wk++ if $doy % 7;

Replies are listed 'Best First'.
Re^2: Getting Ordinal Number of a Week in a Given Date
by Skeeve (Parson) on Sep 01, 2006 at 10:42 UTC

    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?
        Does Date::Calc take this into account?

        Yes:

        use strict; use warnings; use Date::Calc qw ( Decode_Date_EU Week_of_Year ); my @date = ( 2006, 1, 1 ); print Week_of_Year( @date );

        Prints 522005, ie the 52nd week of 2005.

        I don't think so:
        perl -MPOSIX -MTime::Local -le'print strftime q/%W/, localtime timelocal(0,0,0,1,0,2005)'
        Result: 00

        s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
        +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

      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