在perl中解析dateTime正确到微/纳秒 [英] Parsing dateTime in perl correct to micro/nano seconds

查看:106
本文介绍了在perl中解析dateTime正确到微/纳秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想寻找一个Perl模块,该模块采用这样的日期字符串,如11月23日10:42:31.808381及其格式,如%b%d ....,并给我一个dateTime对象/或打印它以相同的方式指定为另一种格式。 Time :: Piece没有分辨率达到nano秒。有没有任何可以帮助我的模块?
我使用perl 5.8(这没有命名反向引用)

解决方案

DateTime :: Format :: Strptime 支持微秒和纳秒。



您的格式稍微奇怪,因为它不包括一年。我添加了一个你使这个演示代码工作。

 #!/ usr / bin / perl 

使用严格;
使用警告;

使用DateTime :: Format :: Strptime;

my $ strp = DateTime :: Format :: Strptime-> new(
pattern =>'%b%d%Y%H:%M:%S.%6N ',
on_error =>'croak',
);

我的$ string ='2010年11月23日10:42:31.808381';

我的$ dt = $ strp-> parse_datetime($ string);

print $ dt-> microsecond;


Im looking for a perl module which takes a date string like this "Nov 23 10:42:31.808381" and its format something like "%b %d ...." this and get me a dateTime object/or print it into another format specified in the same way. Time::Piece doesnt have resolution upto nano seconds. Is there any module which will help me? Im using perl 5.8 (this doesnt have named backreferences)

解决方案

DateTime::Format::Strptime has support for microseconds and nanoseconds.

Your format is slightly strange as it doesn't include a year. I've added one you make this demo code work.

#!/usr/bin/perl

use strict;
use warnings;

use DateTime::Format::Strptime;

my $strp = DateTime::Format::Strptime->new(
    pattern => '%b %d %Y %H:%M:%S.%6N',
    on_error => 'croak',
);

my $string = 'Nov 23 2010 10:42:31.808381';

my $dt = $strp->parse_datetime($string);

print $dt->microsecond;

这篇关于在perl中解析dateTime正确到微/纳秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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