簡體   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