package Time::TAI64; # vim: et ts=4 =head1 NAME Time::TAI64 - Perl extension for converting TAI64 strings into standard unix timestamps. =head1 SYNOPSIS Generate TAI64 timestamps use Time::TAI64 qw/tai64n/; use Time::HiRes qw/time/; $now = time; # High precision printf "%s\n", unixtai64n($now); Print out human readable logs use Time::TAI64 qw/:tai64n/; open FILE, "/var/log/multilog/stats"; while(my $line = ) { my($tai,$log) = split(' ',$line,2); printf "%s %s",tai64nlocal($tai),$log; } close FILE; =head1 DESCRIPTION This is a package provides routines to convert TAI64 strings, like timestamps produced by B, into values that can be processed by other perl functions to display the timestamp in human-readable form and/or use in mathematical computations. =cut use strict; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $FUZZ $AUTOLOAD); #require 5.008; require Exporter; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw( tai2unix tai2strftime tai64unix tai64nunix tai64naunix tai64nlocal unixtai64 unixtai64n unixtai64na ); $EXPORT_TAGS{'tai'} = [ qw( tai2unix tai2strftime ) ]; $EXPORT_TAGS{'tai64'} = [ @{ $EXPORT_TAGS{'tai'} }, qw( tai64unix unixtai64 ) ]; $EXPORT_TAGS{'tai64n'} = [ @{ $EXPORT_TAGS{'tai'} }, qw( tai64nunix unixtai64n tai64nlocal ) ]; $EXPORT_TAGS{'tai64na'} = [ @{ $EXPORT_TAGS{'tai'} }, qw( tai64naunix unixtai64na ) ]; $EXPORT_TAGS{'all'} = [ @{ $EXPORT_TAGS{'tai'} }, @{ $EXPORT_TAGS{'tai64'} }, @{ $EXPORT_TAGS{'tai64n'} }, @{ $EXPORT_TAGS{'tai64na'} }, ]; use POSIX qw(strftime); $VERSION = '2.11'; #----------- # ## Extra second difference... leap-seconds... ## #----------- $FUZZ = 10; #----------- # ## Internal Routines ## #----------- #----------- # ## decode_tai64: ## returns the number of seconds; ## #----------- sub _decode_tai64 ($) { my $tok = shift; my $secs = 0; if (substr($tok,0,9) eq '@40000000') { $secs = hex(substr($tok,9,8)) - $FUZZ; } return $secs; } #----------- # ## decode_tai64n: ## returns a two element array containing the number ## of seconds and nanoseconds respectively. #----------- sub _decode_tai64n ($) { my $tok = shift; my $secs = 0; my $nano = 0; if (substr($tok, 0, 9) eq '@40000000') { $secs = hex(substr($tok,9,8)) - $FUZZ; $nano = hex(substr($tok,17,8)); } return ($secs,$nano); } #----------- # ## decode_tai64na: ## returns a three element array containing the number ## of seconds, nanoseconds, and attoseconds respectively. #----------- sub _decode_tai64na ($) { my $tok = shift; my $secs = 0; my $nano = 0; my $atto = 0; if (substr($tok, 0, 9) eq '@40000000') { $secs = hex(substr($tok,9,8)) - $FUZZ; $nano = hex(substr($tok,17,8)); $atto = hex(substr($tok,25,8)); } return ($secs,$nano,$atto); } #----------- # ## encode_tai64: ## returns a 16 character string tai64 encoded ## using the timestamp supplied, preceded by '@'. #----------- sub _encode_tai64 ($) { my $s = shift; $s += $FUZZ; my $t = '@40000000'. sprintf("%08x",$s); return $t; } #----------- # ## encode_tai64n: ## returns a 24 character string tai64n encoded ## using the timestamp supplied, preceded by '@'. #----------- sub _encode_tai64n ($$) { my($s,$n) = @_; my $t = _encode_tai64($s) . sprintf("%08x",$n); return $t; } #----------- # ## encode_tai64na: ## returns a 32 character string tai64na encoded ## using the timestamp supplied, preceded by '@'. #----------- sub _encode_tai64na ($$$) { my($s,$n,$a) = @_; my $t = _encode_tai64n($s,$n) . sprintf("%08x",$a); return $t; } =head1 EXPORTS In order to use any of these functions, they must be properly imported by using any of the following tags to use related functions: =over 4 =item :tai Generic Functions =item tai2unix ( $tai_string ) This method converts a tai64, tai64n, or tai64na string into a unix timestamp. If successfull, this function returns an integer value containing the number of seconds since Jan 1, 1970 as would perl's C