你不能从持续时间减去的时间。例如,九分钟减一点是毫无意义的。
这里有$t1
等于10:15am
和$t2
等于17:30
或5:30pm
。所以$t2 - $t1
是他们之间的时间,或7.25小时。
现在您试图从该结果中减去$t3
,即7:24am
。但是7.25小时减去上午7:24是持续时间减去一天中的时间,并且不能完成。这就是为什么您会收到消息 Can't use non Seconds object
,因为您正试图从Time::Seconds
对象(持续时间)中减去Time::Piece
对象(一天中的某个时间)。
更新
如果您在持续时间工作,那么你需要在整个这样的Time::Seconds
模块。
use strict;
use warnings;
use Time::Seconds;
my $t1 = Time::Seconds->new(10 * ONE_HOUR + 15 * ONE_MINUTE); # 10:15
my $t2 = Time::Seconds->new(17 * ONE_HOUR + 30 * ONE_MINUTE); # 17:30
my $t3 = Time::Seconds->new(7 * ONE_HOUR + 24 * ONE_MINUTE); # 7:24
my $t = $t2 - $t1 - $t3;
print $t->minutes, "\n";
输出
-9
或者你更愿意从您的Time::Piece
对象减去午夜00:00
,这样
use strict;
use warnings;
use Time::Piece;
use constant MIDNIGHT => Time::Piece->strptime('00:00', '%H:%M');
my $t1 = Time::Piece->strptime('10:15', '%H:%M');
my $t2 = Time::Piece->strptime('17:30', '%H:%M');
my $t3 = Time::Piece->strptime( '7:24', '%H:%M');
$_ -= MIDNIGHT for $t1, $t2, $t3;
my $t = $t2 - $t1 - $t3;
print $t->minutes;
也输出。
注意,你不会得到你想要用的是什么模量在$t->minutes % 60
因为-9 % 60
是51
分钟。
更新2
另一种选择是,以编写使用以前选择的辅助程序。此示例具有子例程new_duration
,它使用Time::Piece->strptime
解析传入的字符串,然后在返回生成的Time::Seconds
对象之前减去午夜。
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
use constant MIDNIGHT => Time::Piece->strptime('00:00', '%H:%M');
my $t1 = new_duration('10:15');
my $t2 = new_duration('17:30');
my $t3 = new_duration('7:24');
my $t = $t2 - $t1 - $t3;
print $t->minutes;
sub new_duration {
Time::Piece->strptime(shift, '%H:%M') - MIDNIGHT;
}
输出
-9
'$ 3'和'$ t3'不是一回事。 – geoffspear
非常好看,这解释了为什么我没有得到我想重现的原始错误。现在脚本产生“正确”的错误。 –
是那些时间或持续时间?你想做什么(英文单词)? $ t2有时候总共有三个持续时间('$ t','$ t1'和'$ t3'),你试图获得第三个持续时间? – ikegami