繁体   English   中英

在Perl中解析时间戳以毫秒为单位

[英]parse timestamp with millisecond in Perl

假设我有一堆时间戳,如“11/05/2010 16:27:26.003”,如何在Perl中用毫秒解析它们。

基本上,我想比较时间戳,看看它们是在特定时间之前还是之后。

我尝试使用Time :: Local,但似乎Time :: Local只能解析秒。 另一方面,Time :: HiRes并不是真正用于解析文本的。

谢谢,德里克

use DateTime::Format::Strptime;

my $Strp = new DateTime::Format::Strptime(
    pattern => '%m/%d/%Y %H:%M:%S.%3N',
    time_zone   => '-0800',
);

my $now = DateTime->now;
my $dt  = $Strp->parse_datetime('11/05/2010 23:16:42.003');
my $delta = $now - $dt;

print DateTime->compare( $now, $dt );
print $delta->millisecond;

您可以使用Time::Local ,只需添加.003即可:

#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;

my $timestring = "11/05/2010 16:27:26.003";
my ($mon, $d, $y, $h, $min, $s, $fraction) =
    $timestring =~ m{(..)/(..)/(....) (..):(..):(..)([.]...)};
$y -= 1900;
$mon--;

my $seconds = timelocal($s, $min, $h, $d, $mon, $y) + $fraction;

print "seconds: $seconds\n";
print "milliseconds: ", $seconds * 1_000, "\n";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM