我想从10小时0分钟减去17小时5分钟。
#!/usr/bin/perl
use Time::Piece;
my $t1 = Time::Piece->strptime( '10:00', '%H:%M' );
my $t2 = Time::Piece->strptime( '17:05', '%H:%M' );
my $t = $t2 - $t1;
print $t->hour;
print $t->min;
但我收到了错误
Can't locate object method "hour" via package "Time::Seconds"
我没有Time::Piece的偏好。它刚刚流行并且已经安装在Linux上。
问题
有人能看出为什么会失败吗?
答案 0 :(得分:3)
正如文档所指出的,两个Time :: Piece对象之间的区别是Time :: Seconds对象。所以你也需要使用它并使用相应的方法。
use Time::Piece;
use Time::Seconds; # Already included in Time::Piece
my $t1 = Time::Piece->strptime( '10:00', '%H:%M' );
my $t2 = Time::Piece->strptime( '17:05', '%H:%M' );
my $t = $t2 - $t1; # $t is now a Time::Seconds object
print $t->hours; # The method of which is called hours instead of hour.
print $t->minutes;
输出可能不是你所希望的,因为它给出了十进制值。但是,正如扎伊德指出的那样:
print $t->pretty;
照顾好了。