2014-03-03 2 views
2

17 시간 10 시간 0 분에서 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; 

는하지만 난 Time::Piece에 대한 선호가없는

Can't locate object method "hour" via package "Time::Seconds" 

오류를 얻을. 그냥 인기가 있고 리눅스에 이미 설치되어 있습니다.

질문이 실패한 이유

사람이 볼 수 있을까요? 문서가 지적 하듯이

답변

3

두 시간 :: 조각 개체 차이가 :: 초 개체 시간입니다. 따라서 해당 방법을 사용하고 해당 방법을 사용해야합니다.

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; 

출력은 가능성이 당신이 그것을 소수점 값을 제공으로 바라는 것들되지 않습니다. 그러나 Zaid은 다음과 같이 지적합니다.

print $t->pretty; 

이렇게 처리합니다.

+1

또는 단순히는'인쇄 $의 T-는> 꽤,'내가 대답에 그 추가됩니다,'7시간 5 분, 0 seconds' – Zaid

+0

아주 좋은 지적을 제공합니다. – DeVadder

+0

왜'Time :: Seconds'가 필요한가요? 'Time :: Piece'에 대한 perldoc에는'$ t-> hour'가 나열되어 있습니다. 예, 7 시간 반환 오분 =) –

관련 문제