2012-04-18 3 views
4

이건 DateTime 버그입니까? 아니면 뭔가 빠졌습니까?Perl DateTime subtract_datetime_absolute 재밌는 동작

sub get_diff_same_day { 
    # return only the time difference between 2 dates 
    my ($dnow, $dt) = @_; 
    my $dtx = $dt->clone(); 
    $dtx->set_year($dnow->year); 
    $dtx->set_month($dnow->month); 
    $dtx->set_day($dnow->day); 
    say $dnow; 
    say $dtx; 

    return $dtx->subtract_datetime_absolute($dnow); 
} 

출력 : 그러나

2012-04-18T09:56:39 
2012-04-18T09:56:40 

0 DateTime::Duration=HASH(0x1e10a34) 
    'days' => 0 
    'end_of_month' => 'wrap' 
    'minutes' => 0 
    'months' => 0 
    'nanoseconds' => 0 
    'seconds' => 3577  # <= huh? 

, 대신 내가 사용하는 경우 subtract_datetime_absolute의는

$dtx - $dnow 

이 저를 제공합니다

그것은 그 subtract_datetime_absolute은 '아무튼 나에게 나타납니다
0 DateTime::Duration=HASH(0x1bada04) 
    'days' => 0 
    'end_of_month' => 'wrap' 
    'minutes' => 0 
    'months' => 0 
    'nanoseconds' => 0 
    'seconds' => 1 

DateTime :: set_xxxx 함수를 고려해야합니다.

아래의 샘플을 편집하십시오.

use Modern::Perl; 
use autodie; 
use DateTime; 


use constant OFFSET => 0; 

## main 
test(); 

sub test { 
    my $now = DateTime->now(time_zone => 'local')->add(hours => OFFSET); 
    my $ddt = get_rand_date(); 
    my $secs = get_secs_same_day_broken ($now, $ddt); 
    my $secs2 = get_secs_same_day($now, $ddt); 

    if ($secs != $secs2) { 
    say "expecting same result ($secs, $secs2)"; 
    } 
} 

sub get_secs_same_day_broken { 
    # return the seconds time difference between 2 dates 
    my ($dnow, $dt) = @_; 
    my $dtx = $dt->clone(); 
    $dtx->set_year($dnow->year); 
    $dtx->set_month($dnow->month); 
    $dtx->set_day($dnow->day); 
    say "A: $dnow vs $dtx"; 
    return $dtx->subtract_datetime_absolute($dnow)->seconds; 
} 

sub get_secs_same_day { 
    # return the seconds time difference between 2 dates 
    my ($dnow, $dt) = @_; 
    my $dtx = $dt->clone(); 
    $dtx->set_year($dnow->year); 
    $dtx->set_month($dnow->month); 
    $dtx->set_day($dnow->day); 
    say "B: $dnow vs $dtx"; 
    return ($dtx - $dnow)->seconds; 
} 

sub get_rand_date { 
    my $d = int(rand(27)) + 1; 
    my $M = int(rand(11)) + 1; 
    my $h = int(rand(24)); 
    my $m = int(rand(60)); 
    my $s = int(rand(60)); 

    my $dt = DateTime->new(day => $d, month => $M, year => 2012, hour => $h, minute => $m, second => $s); 
    $dt->add(hours => OFFSET); 

    return $dt; 
} 
+2

실행 가능한 무언가를 제공하십시오. 나는 이걸 가져 와서 그것을 재현 할 수 없도록하는 데 시간을 허비했었다. 그럼 당신이 뭔가 잘못했기 때문인지, 아니면 당신이 사용하고있는 모듈 버전의 버그 때문인지 모르겠습니다. – ikegami

+1

샘플 프로그램으로 업데이트되었습니다. – Richard

답변

3

$dtx->subtract_datetime_absolute($now)->seconds 초 절대 수와 두 날짜 간의 차이를 리턴한다.

이 시도 :

my $now = DateTime->now(time_zone => 'local'); 
my $dtx = $now->clone->set(hour => 22, minute => 22, second => 22); 

{ 
    use integer; 
    my $seconds = $dtx->subtract_datetime_absolute($now)->seconds; 
    my $minutes = $seconds/60; 
    say $seconds - ($minutes * 60); 
} 

{ 
    my $seconds = ($dtx - $now)->seconds; 
    say $seconds; 
} 
+1

이해하고 감사드립니다. – Richard