我使用Time::Piece
来解析日期。
#!/usr/bin/perl -w
use warnings;
use Time::Piece;
my $date = '20-11-2038';
my $frmt = '%d-%m-%Y';
my $t = Time::Piece->strptime($date, $frmt);
$t = $t + ONE_DAY;
print $t->ymd('/'), "\n";
我只能使用1970年至2038年之间的日期。如果我使用任何其他日期,我会收到错误
Day too big - 25160 > 24853
Cannot handle date (0, 0, 0, 20, 11, 2038) at C:/perl/lib/Time/Piece.pm line 315
还有其他方法吗?
答案 0 :(得分:0)
我无法重现您的错误,我得到:
Argument "ONE_DAY" isn't numeric in addition (+) at ...lib/5.10.1/x86_64-linux-thread-multi/Time/Piece.pm line 571.
但是,如果我添加use Time::Seconds
它工作正常(我更改了代码以显示1970-2038范围之外的日期):
use 5.010;
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
my $tmin = Time::Piece->strptime('20-11-1970', '%d-%m-%Y');
my $tmax = Time::Piece->strptime('20-11-2038', '%d-%m-%Y');
say $tmin->ymd('-');
say $tmax->ymd('-');
say '---';
my $tmin2 = $tmin - ONE_YEAR;
my $tmax2 = $tmax + ONE_YEAR;
say $tmin2->ymd('-');
say $tmax2->ymd('-');
结果是:
1970-11-20
2038-11-20
---
1969-11-19
2039-11-20
更新
This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
Time::Piece is up to date. (1.29)