How to get nano second granularity from hex time?

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

580a9272.0a9ce167 

and I'm trying to convert it using perl to a 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; } 

Conclusion: Fri 21 October 18:10:58 2016.178053479

The converter uses localtime () and gmtime () directly. I want time with nano graininess, and then year. Any help is much appreciated.

+5
source share
1 answer

POSIX::strftime does not 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 built-in support for nanoseconds, so this is 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 
+3
source

Source: https://habr.com/ru/post/1258728/


All Articles