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;
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");
source share