I'm glad to see others using DateTime,
but that's not quite right.
January 1st is not neccessarily the beginning of the week, I modified your snippet like so
use DateTime;
my $now = DateTime->now;
my $first = DateTime->new(
year => $now->year,
month => 1,
day => 1,
);
my $last = DateTime->new(
year => $now->year,
month => 12,
day => 26, # 31 - 26 == 5 days, otherwise it's week 1
);
print q[$now ],$now->ymd,"\n";
print q[$first ],$first->ymd,"\n";
print q[$last ],$last->ymd,"\n";
print q[number of weeks ], $last->week_number;
print q[ in the year ], $last->year,"\n";
# roll back the date to the beginning of the week
$first->subtract( days => 7 - $first->day_of_week );
for( 1 .. $last->week_number ) {
printf "week %02d %s - ", $_, $first->ymd;
print $first->add( weeks => 1)->ymd,"\n";
}
__END__
$now 2003-09-08
$first 2003-01-01
$last 2003-12-26
number of weeks 52 in the year 2003
week 01 2002-12-28 - 2003-01-04
week 02 2003-01-04 - 2003-01-11
week 03 2003-01-11 - 2003-01-18
week 04 2003-01-18 - 2003-01-25
week 05 2003-01-25 - 2003-02-01
week 06 2003-02-01 - 2003-02-08
week 07 2003-02-08 - 2003-02-15
week 08 2003-02-15 - 2003-02-22
week 09 2003-02-22 - 2003-03-01
week 10 2003-03-01 - 2003-03-08
week 11 2003-03-08 - 2003-03-15
week 12 2003-03-15 - 2003-03-22
week 13 2003-03-22 - 2003-03-29
week 14 2003-03-29 - 2003-04-05
week 15 2003-04-05 - 2003-04-12
week 16 2003-04-12 - 2003-04-19
week 17 2003-04-19 - 2003-04-26
week 18 2003-04-26 - 2003-05-03
week 19 2003-05-03 - 2003-05-10
week 20 2003-05-10 - 2003-05-17
week 21 2003-05-17 - 2003-05-24
week 22 2003-05-24 - 2003-05-31
week 23 2003-05-31 - 2003-06-07
week 24 2003-06-07 - 2003-06-14
week 25 2003-06-14 - 2003-06-21
week 26 2003-06-21 - 2003-06-28
week 27 2003-06-28 - 2003-07-05
week 28 2003-07-05 - 2003-07-12
week 29 2003-07-12 - 2003-07-19
week 30 2003-07-19 - 2003-07-26
week 31 2003-07-26 - 2003-08-02
week 32 2003-08-02 - 2003-08-09
week 33 2003-08-09 - 2003-08-16
week 34 2003-08-16 - 2003-08-23
week 35 2003-08-23 - 2003-08-30
week 36 2003-08-30 - 2003-09-06
week 37 2003-09-06 - 2003-09-13
week 38 2003-09-13 - 2003-09-20
week 39 2003-09-20 - 2003-09-27
week 40 2003-09-27 - 2003-10-04
week 41 2003-10-04 - 2003-10-11
week 42 2003-10-11 - 2003-10-18
week 43 2003-10-18 - 2003-10-25
week 44 2003-10-25 - 2003-11-01
week 45 2003-11-01 - 2003-11-08
week 46 2003-11-08 - 2003-11-15
week 47 2003-11-15 - 2003-11-22
week 48 2003-11-22 - 2003-11-29
week 49 2003-11-29 - 2003-12-06
week 50 2003-12-06 - 2003-12-13
week 51 2003-12-13 - 2003-12-20
week 52 2003-12-20 - 2003-12-27
update: aren't I funny {grin} every year has 52 weeks ($last->week_number, lol).
| MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!" |
| I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README). |
| ** The third rule of perl club is a statement of fact: pod is sexy. |