2017-10-04 227 views
-8

计算2个给定时间戳之间差异的代码。 Start_Time="2017-09-19 19:36:12.763"End_Time="2017-09-19 19:36:22.72"我想在上面两个时间戳之间考虑毫秒的差异。如何计算PERL中2个时间戳之间的差异

这是我的尝试:

#!/usr/bin/perl 

use strict; 
use warnings; 
use Date::Parse; 
use Date::Format; 

$startdat = "2007-11-17 12:51:22"; 
$stopdate = "2007-11-17 12:52:22"; 

my ($years, $months, $days, $hours, $mins, $secs) = split /\W+/, $startdat; 
my ($yeart, $montht, $dayt, $hourt, $mint, $sect) = split /\W+/, $stopdate; 

my $times = timelocal($secs,$mins,$hours,$days,$months,$years); 
my $timet = timelocal($sect,$mint,$hourt,$dayt,$montht,$yeart); 

$time = $timet - $times; 

print $time; 

但是,这给了我一个错误:在@公司无法定位日期/ Parse.pm。

+1

@abbasp:另外,请考虑给你的问题一个更好的标题。 –

+1

@abbasp:我已经修复了您的代码格式。这可能值得阅读[Markdown帮助](https://stackoverflow.com/editing-help)。 –

+0

@Borodin:无论OP更新了评论中的代码,我都编辑了这个问题,不幸的是,在我编辑过相同的代码之前,有人编辑了这个问题。这是问题。不过,我会按照你的意见。 – ssr1012

回答

4

您正在收到该错误消息,因为未安装Date :: Parse。这个模块不是标准Perl发行版的一部分,需要单独安装。如果你想使用它,那么你需要安装它。

但是,

虽然你在代码中使用Date :: Parse和Date :: Format,但实际上并没有使用它们。所以你可以删除这两行。那么你的代码可能会工作。

Date :: Parse和Date :: Format实际上是相当过时的模块。对于Perl中的日期和时间工作,我推荐使用Time::Piece(这是Perl库的标准部分)或DateTime(需要安装它)。

相关问题