It's possible to use the Date::Manip or POSIX modules, but they both have some caveats.
Seeing as I only found how to use the above after I'd rolled my own, I thought I'd share both the information and my own solution, so that (crossed fingers) Google might help anyone else needing the same...
(However, this only works on systems where the POSIX implementation meets the "Single Unix" specification. Hence my system (WinXP) just returns "%V", which is less than useful...)$weekNum = POSIX::strftime("%V", gmtime time);
Using Date::Manip:
Using none of the above:$weekNum = UnixDate(ParseDate("today"), "%W");
# Returns the week number of NOW sub currentWeekNumber { # Get current year, day of year (0..364/5) day of week (0..6) my ($year, $dayOfWeek, $dayOfYear) = (gmtime time)[5,6,7]; # Adjust DayOfWeek from American 0==Sunday, to ISO 0==Monday # and year from "since 1900" to the real year return weekNumber(($dayOfWeek + 6) % 7, $dayOfYear, $year + 1900); } # Returns the week number of the specified time # Year is the real year # Day of week is 0..6 where 0==Monday # Day of year is 0..364 (or 365) where 0==Jan1 sub weekNumber { # Get parameters my ($dayOfWeek, $dayOfYear, $year) = @_; die if ($dayOfWeek < 0); die if ($dayOfWeek > 6); die if ($dayOfYear < 0); die if ($dayOfYear >= 366); die if ($year < 0); # Locate the nearest Thursday # (Done by locating the Monday at or before and going forwards 3 day +s) my $dayOfNearestThurs = $dayOfYear - $dayOfWeek + 3; # Is nearest thursday in last year or next year? if ($dayOfNearestThurs < 0) { # Nearest Thurs is last year # We are at the start of the year # Adjust by the number of days in LAST year $dayOfNearestThurs += daysInYear($year-1); } my $daysInThisYear = daysInYear($year); if ($dayOfNearestThurs > $daysInThisYear) { # Nearest Thurs is next year # We are at the end of the year # Adjust by the number of days in THIS year $dayOfNearestThurs -= $daysInThisYear; } # Which week does the Thurs fall into? my $weekNum = int ($dayOfNearestThurs / 7); # Week numbering starts with 1 $weekNum += 1; # Pad with 0s to force 2 digit representation return substr "0"x2 . $weekNum, -2; } # Returns the number of... sub daysInYear { return 366 unless $_[0] % 400; return 365 unless $_[0] % 100; return 366 unless $_[0] % 4; return 365; }
In reply to ISO 8601 week number by wol
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |