-1

我想计算 windows 上低于时间戳的时间差异。

tt1= 2013/08/16 23:59:59:785

tt2 = 2013/08/16 23:59:59:753

和输出应该是: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;
4

1 回答 1

3

您的时间戳之间的差异是 32 毫秒,无论您如何格式化它们,您都不会得到000826.288000结果。假设您得到了正确的 Duration 结果:

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
于 2013-08-30T10:57:23.117 回答