在Perl中以毫秒为单位解析时间戳 [英] parse timestamp with millisecond in Perl

查看:800
本文介绍了在Perl中以毫秒为单位解析时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想比较时间戳,看看他们是否在特定时间之前或之后。



我尝试使用Time :: Local,但是似乎Time :: Local只能解析第二个。而时间:: HiRes,另一方面,并​​不是真的解析文本。



谢谢,
Derek

解决方案

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

 #!/ usr / bin / perl 

use严格;
使用警告;

使用Time :: Local;

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

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

打印秒:$ seconds\\\
;
printmilliseconds:,$ seconds * 1_000,\\\
;


Assuming I have a bunch of timestamps like "11/05/2010 16:27:26.003", how do parse them with millisecond in Perl.

Essentially, I would like to compare the timestamp to see if they are before or after a specific time.

I tried using Time::Local, but it seems that Time::Local is only capable to parse up second. And Time::HiRes, on the other hand, isn't really made for parsing text.

Thanks, Derek

解决方案

You can use Time::Local and just add the .003 to it:

#!/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";

这篇关于在Perl中以毫秒为单位解析时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆