Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get nano second granularity from hex time?

Tags:

hex

perl

I am trying to convert hex time(getting first output from kernel module) into nanosecond granularity,

580a9272.0a9ce167 

and I am trying to convert it using perl into human readable format:

while (<>) {
s/^([a-fA-F0-9]+)(\.)([a-fA-F0-9]+)(\s+.*)/sprintf("%s%s%s%s",&$converter(hex($1)), $2, hex($3), $4)/oe;
} continue {
print;
}

output : Fri Oct 21 18:10:58 2016.178053479

Converter uses localtime() and gmtime() directly I want time with nano granularity and then year.Any help is highly appreciated.

like image 271
MikasaAckerman Avatar asked Oct 18 '22 23:10

MikasaAckerman


1 Answers

POSIX::strftime doesn't support fractional seconds, so you need to build the output in parts.

use POSIX qw( strftime );

my $opt_gmt = 1;
my $hex = '580a9272.0a9ce167';

my ($s, $ns) = map hex($_), split /\./, $hex;
my $formatted_ns = sprintf("%09d", $ns);
my $formatted = strftime("%a %b %d %H:%M:%S.$formatted_ns %Y",
    defined($opt_gmt) ? gmtime($s) : localtime($s));

say $formatted;  # Fri Oct 21 22:10:58.178053479 2016

DateTime has native support for nanoseconds, so that presents an alternative.

use DateTime qw( );

my $opt_gmt = 1;
my $hex = '580a9272.0a9ce167';

my ($s, $ns) = map hex($_), split /\./, $hex;
my $dt = DateTime->from_epoch( epoch => $s );
$dt->set_nanosecond( $ns );
$dt->set_time_zone( defined($opt_gmt) ? 'UTC' : 'local' );

say $dt->strftime("%a %b %d %H:%M:%S.%N %Y");  # Fri Oct 21 22:10:58.178053479 2016
like image 91
ikegami Avatar answered Nov 15 '22 13:11

ikegami