Reputation: 3373
Here is my code:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use DateTime;
package Weeks;
sub new {
my $class = shift;
my $self = { };
bless $self, $class;
return $self;
}
sub getWeek {
my $self = shift;
my $dummy = shift;
my $year = shift;
my $week = shift;
print "getWeek($dummy, $year, $week)\n";
}
package main;
my $dt = DateTime->new(year => 2015, month => 6, day => 1);
my $weeks = Weeks->new();
print "weeks is $weeks\n";
$weeks->getWeek(2015, 5, 3);
print "Calling getWeek on $dt for " . $dt->year . " week " . $dt->week . "\n";
$weeks->getWeek($dt->year, $dt->week, 3);
When I run this, I get:
getWeek(2015, 5, 3)
Calling getWeek on 2015-06-01T00:00:00 for 2015 week 23
getWeek(2015, 2015, 23)
Why does it send the year twice in the second case?
Upvotes: 0
Views: 39
Reputation: 386541
$dt->week()
returns two values.
$dt->week()
($week_year, $week_number) = $dt->week;
Returns information about the calendar week which contains this datetime object. The values returned by this method are also available separately through the week_year and week_number methods.
The first week of the year is defined by ISO as the one which contains the fourth day of January, which is equivalent to saying that it's the first week to overlap the new year by at least four days.
Typically the week year will be the same as the year that the object is in, but dates at the very beginning of a calendar year often end up in the last week of the prior year, and similarly, the final few days of the year may be placed in the first week of the next year.
You could use ( $dt->week )[1]
to get the week number, but the second value is rather useless without the first. I think you should be using the following:
$weeks->getWeek($dt->week, 3);
Upvotes: 4