Today I had to hack a quick script to convert a week number to a date in perl. To my surprise, the answer was not straightforward and I had to test and try a little bit before to get something working.

So here it is:

#!/usr/bin/perl
use strict;
use Time::ParseDate;
use Time::Local;
 
sub convert_week_to_date
{
    my ($year, $week) = @_;
 
    my $ref = timelocal(0, 0, 0, 1, 0, $year);
    my ($s, $m, $h, $dom, $m, $y, $wd, $yd, $i) =
        localtime($ref);
 
    my $time = parsedate("+".$week." weeks",
                         NOW => $ref);
 
    my ($s, $m, $h, $dom, $m, $y, $wd, $yd, $i) =
        localtime($time);
    $wd -= 1;
    my $time = parsedate("-".$wd." days",
                         NOW => $time);
    my ($s, $m, $h, $dom, $m, $y, $wd, $yd, $i) =
        localtime($time);
    return (sprintf("%04d/%02d/%02d",
            $y + 1900, $m + 1, $dom - $wd + 1));
}
 
print convert_week_to_date(2009, 1)."\\n";
print convert_week_to_date(2009, 52)."\\n";
print convert_week_to_date(2010, 1)."\\n";
print convert_week_to_date(2010, 22)."\\n";
print convert_week_to_date(2010, 23)."\\n";
print convert_week_to_date(2010, 24)."\\n";
 

Download this code: convert_week_to_date.pl

Hopefully it will help someone one day.