如何从十六进制时间获得纳秒二次粒度?

Mik*_*man 5 perl hex

我试图将十六进制时间(从内核模块获得第一个输出)转换为纳秒粒度,

580a9272.0a9ce167 
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用perl将其转换为人类可读格式:

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;
}
Run Code Online (Sandbox Code Playgroud)

输出:10月21日星期五18:10:58 2016.178053479

转换器直接使用localtime()和gmtime()我希望时间与纳米粒度,然后年.任何帮助非常感谢.

ike*_*ami 3

POSIX::strftime不支持小数秒,因此您需要分段构建输出。

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
Run Code Online (Sandbox Code Playgroud)

DateTime 本身支持纳秒,因此提供了一种替代方案。

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
Run Code Online (Sandbox Code Playgroud)