balki
balki

Reputation: 27664

Parsing dateTime in perl correct to micro/nano seconds

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)

Upvotes: 1

Views: 2683

Answers (2)

Dave Cross
Dave Cross

Reputation: 69244

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;

Upvotes: 3

CyberDem0n
CyberDem0n

Reputation: 15036

"Nov 23 10:42:31.808381" -- is microseconds, not nanoseconds

If you need microseconds, you must use Time::HiRes; module and gettimeofday method in it.

Upvotes: 0

Related Questions