File Coverage

blib/lib/Net/DNS/Extlang/Time.pm
Criterion Covered Total %
statement 23 43 53.4
branch 3 20 15.0
condition 0 3 0.0
subroutine 8 9 88.8
pod n/a
total 34 75 45.3


line stmt bran cond sub pod time code
1             ## RRSIG time stamp for T and T6
2             package Net::DNS::Extlang::Time;
3              
4             our $VERSION = '0.1';
5             =head1 NAME
6              
7             Net::DNS::Extlang::Time - Helper routines for timestamps
8              
9             Called only from Extlang generated code. No user servicable parts.
10              
11             =cut
12 1     1   787 use base qw(Exporter);
  1         2  
  1         76  
13 1     1   6 use vars qw(@EXPORT);
  1         1  
  1         47  
14             @EXPORT = qw(_encodetime _string2time);
15              
16 1     1   4 use strict;
  1         2  
  1         14  
17 1     1   4 use Carp;
  1         1  
  1         63  
18 1     1   407 use Time::Local;
  1         1730  
  1         48  
19 1     1   6 use constant UTIL => defined eval 'require Scalar::Util';
  1         1  
  1         41  
20              
21             my $y1998 = timegm( 0, 0, 0, 1, 0, 1998 );
22             my $y2026 = timegm( 0, 0, 0, 1, 0, 2026 );
23             my $y2082 = $y2026 << 1;
24             my $y2054 = $y2082 - $y1998;
25             my $m2026 = int( 0x80000000 - $y2026 );
26             my $m2054 = int( 0x80000000 - $y2054 );
27             my $t2082 = int( $y2082 & 0x7FFFFFFF );
28             my $t2100 = 1960058752;
29              
30             sub _string2time { ## parse time specification string
31 4     4   2963 my $arg = shift;
32 4 50       10 croak 'undefined time' unless defined $arg;
33 4 50       38 return int($arg) if length($arg) < 12;
34 0         0 my ( $y, $m, @dhms ) = unpack 'a4 a2 a2 a2 a2 a2', $arg . '00';
35 0 0 0     0 unless ( $arg gt '20380119031407' ) { # calendar folding
36 0 0       0 return timegm( reverse(@dhms), $m - 1, $y ) if $y < 2026;
37 0         0 return timegm( reverse(@dhms), $m - 1, $y - 56 ) + $y2026;
38             } elsif ( $y > 2082 ) {
39             my $z = timegm( reverse(@dhms), $m - 1, $y - 84 ); # expunge 29 Feb 2100
40             return $z < 1456790400 ? $z + $y2054 : $z + $y2054 - 86400;
41             }
42 0         0 return ( timegm( reverse(@dhms), $m - 1, $y - 56 ) + $y2054 ) - $y1998;
43             }
44              
45             # return encoded time
46             sub _encodetime {
47 8     8   1137 my $time = shift;
48              
49 8 50       61 return undef unless $time;
50 0           return UTIL ? Scalar::Util::dualvar( $time, _time2string($time) ) : _time2string($time);
51             }
52              
53             sub _time2string { ## format time specification string
54 0     0     my $arg = shift;
55 0 0         croak 'undefined time' unless defined $arg;
56 0           my $ls31 = int( $arg & 0x7FFFFFFF );
57 0 0         if ( $arg & 0x80000000 ) {
    0          
58              
59 0 0         if ( $ls31 > $t2082 ) {
60 0 0         $ls31 += 86400 unless $ls31 < $t2100; # expunge 29 Feb 2100
61 0           my ( $yy, $mm, @dhms ) = reverse( ( gmtime( $ls31 + $m2054 ) )[0 .. 5] );
62 0           return sprintf '%d%02d%02d%02d%02d%02d', $yy + 1984, $mm + 1, @dhms;
63             }
64              
65 0           my ( $yy, $mm, @dhms ) = reverse( ( gmtime( $ls31 + $m2026 ) )[0 .. 5] );
66 0           return sprintf '%d%02d%02d%02d%02d%02d', $yy + 1956, $mm + 1, @dhms;
67              
68              
69             } elsif ( $ls31 > $y2026 ) {
70 0           my ( $yy, $mm, @dhms ) = reverse( ( gmtime( $ls31 - $y2026 ) )[0 .. 5] );
71 0           return sprintf '%d%02d%02d%02d%02d%02d', $yy + 1956, $mm + 1, @dhms;
72             }
73              
74 0           my ( $yy, $mm, @dhms ) = reverse( ( gmtime $ls31 )[0 .. 5] );
75 0           return sprintf '%d%02d%02d%02d%02d%02d', $yy + 1900, $mm + 1, @dhms;
76             }
77             1;
78             __END__