2013-08-30 110 views
-1

我想計算窗口下時間戳的時間差。給定時間戳的時間差

TT1 = 2013年8月16日23:59:59:785

TT2 = 2013年8月16日23:59:59:753

和outut應該是:000826.288000

我已經嘗試下面的代碼,但獲得輸出爲16799588.000000。

但輸出應該是000826.288000.請幫我正確的時間戳000826.288000。

use DateTime::Format::Strptime; 
my $dp = DateTime::Format::Strptime->new(
    pattern => '%Y/%m/%d %H:%M:%S:%3N' 
); 

# Create two DateTime objects 
my $tt1 = $dp->parse_datetime('2013/08/16 23:59:59:753'); 
my $tt2 = $dp->parse_datetime('2013/08/16 23:59:59:785'); 

# The difference is a DateTime::Duration object 
my $diff1 = $tt2 - $tt1; 
#print " t1 and t2 are : $diff $tt1 and $tt2 \n"; 

my $diff = sprintf "%013.6f", $tt2 - $tt1; 

回答

3

的區別你的時間戳之間爲32毫秒,無論你如何格式化他們,你不會得到000826.288000結果。假設您獲得了適當的持續時間結果:

use DateTime::Duration qw(); 
use DateTime::Format::Duration qw(); 

print DateTime::Format::Duration 
    ->new(pattern => '%06S.%06N') 
    ->format_duration(
     DateTime::Duration->new(seconds => 826, nanoseconds => 288000000) 
    ); 
__END__ 
000826.288000