1

我正在尝试用 Test::MockTime 模块替换当前时间。它工作正常:

use Test::MockTime qw(:all);
use Time::Local;
my $sec  = 0;
my $min  = 0;
my $hour = 14;
my $mday = 1; #1-31
my $mon  = 1; #1-12
my $year = 2013; #1970-...
set_fixed_time(timelocal($sec,$min,$hour,$mday,$mon-1,$year-1900));
print join "\n", localtime;

但是,当我使用 Time::localtime 时,什么也没有发生:

use Time::localtime;
use Time::Local;
use Test::MockTime qw(:all);

my ($sec,$min,$hour,$mday,$mon,$year)=(0,0,14,1,1,2013);

set_fixed_time(timelocal($sec,$min,$hour,$mday,$mon-1,$year-1900));

my $t=localtime();
my $xmon=$t->mon;
my $xyear=$t->year;
my $xday=$t->mday;
my $xmon_now=$xmon+1;
my $xyear_now=$xyear+1900;

print "$xmon_now $xyear_now\n";

输出将是“12 2012”当使用 Time::localtime 时,如何更改测试中的时间?谢谢和对不起我的英语

升级版:

use Time::localtime;
use Test::MockTime qw(:all);

没用

use Test::MockTime qw(:all);
use Time::localtime;

工作正常)perl魔法

4

1 回答 1

1

Test::MockTime 覆盖localtime,但Time::localtime使用CORE::localtime(未覆盖的版本localtime)。您必须重写 Time::localtime's localtime

于 2012-12-25T09:30:19.437 回答