Perl DateTime发送多个参数?

时间:2015-03-27 20:35:45

标签: perl

这是我的代码:

#!/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);

当我跑步时,我得到:

getWeek(2015, 5, 3)
Calling getWeek on 2015-06-01T00:00:00 for 2015 week 23
getWeek(2015, 2015, 23)

为什么在第二种情况下它会发送两年?

1 个答案:

答案 0 :(得分:4)

$dt->week()会返回两个值。

  

$ DT->周()

($week_year, $week_number) = $dt->week;
     

返回有关包含此datetime对象的日历周的信息。此方法返回的值也可通过week_year和week_number方法单独获得。

     

一年中的第一周由ISO定义为包含1月第4天的那一周,这相当于说它是新年重叠至少4天的第一周。< / p>      

通常,周年将与对象所在的年份相同,但是在日历年的最开始的日期通常在上一年的最后一周结束,同样地,最后几天的日期也是如此。这一年可能会放在第二年的第一周。

您可以使用( $dt->week )[1]来获取周数,但第二个值在没有第一个值的情况下相当无用。我认为你应该使用以下内容:

$weeks->getWeek($dt->week, 3);