blib/lib/perl5i/1/DateTime.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 220 CC 12 SCAR 23.6
CC
12
Cov
0%
_get_datetime_timezone
42.0
dt_gmtime
12.0
dt_localtime
12.0
CRAP 10.5
1 package perl5i::1::DateTime;
2  
3 # A file to contain the Datetime work for perl5i to get it out of perl5i.pm
4  
5 use 5.010;
6 use strict;
7 use warnings;
8  
9 # Determine if we need Time::y2038 and only load if necessary.
10 # XXX This is a bit of a hack and should go into a config file.
11 use constant NEEDS_y2038 => (
12 ((((CORE::gmtime(2**47-1))[5] || 0) + 1900) != 4461763) ||
13 ((((CORE::gmtime(-62135510400))[5] || 0) + 1900) != 1)
14 );
15  
16 BEGIN {
17 if( NEEDS_y2038 ) {
18 require Time::y2038;
19 Time::y2038->import;
20 }
21 }
22  
23  
24 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
25 sub dt_gmtime (;$) {
26 my $time = @_ ? shift : time;
27 return gmtime($time) if wantarray;
28  
29 my($sec, $min, $hour, $mday, $mon, $year) = gmtime($time);
30 $mon++;
31 $year += 1900;
32  
33 require DateTime;
34 return perl5i::1::DateTime::y2038->new(
35 year => $year,
36 month => $mon,
37 day => $mday,
38 hour => $hour,
39 minute => $min,
40 second => $sec,
41 formatter => "perl5i::1::DateTime::Format::CTime"
42 );
43 }
44  
45  
46 sub _get_datetime_timezone {
47 state $local_tzfile = "/etc/localtime";
48  
49 # Always be sure to honor the TZ environment var
50 return "local" if $ENV{TZ};
51  
52 # Work around a bug in DateTime::TimeZone on FreeBSD where it
53 # can't determine the time zone if /etc/localtime is not a link.
54 # Tzfile is also faster to do localtime calculations.
55 if( -e $local_tzfile ) {
56 # Could go through more effort to figure it out. Meh.
57 my $tzname = "Local";
58 if( -l $local_tzfile ) {
59 if( my $real_tzfile = eval { readlink $local_tzfile } ) {
60 $tzname = $real_tzfile;
61 }
62 }
63 require DateTime::TimeZone::Tzfile;
64 my $tz = DateTime::TimeZone::Tzfile->new(
65 name => $tzname,
66 filename => $local_tzfile
67 );
68 return $tz if $tz;
69 }
70  
71 return "local";
72 }
73  
74 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
75 sub dt_localtime (;$) {
76 my $time = @_ ? shift : time;
77 return localtime($time) if wantarray;
78  
79 my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
80 $mon++;
81 $year += 1900;
82  
83 state $tz = _get_datetime_timezone();
84  
85 require DateTime;
86 return perl5i::1::DateTime::y2038->new(
87 year => $year,
88 month => $mon,
89 day => $mday,
90 hour => $hour,
91 minute => $min,
92 second => $sec,
93 time_zone => $tz,
94 formatter => "perl5i::1::DateTime::Format::CTime"
95 );
96 }
97  
98  
99 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
100 sub dt_time () {
101 require DateTime::Format::Epoch;
102 state $formatter = DateTime::Format::Epoch->new( epoch => DateTime->from_epoch( epoch => 0 ) );
103  
104 require DateTime;
105 return perl5i::1::DateTime::time->from_epoch(
106 epoch => time,
107 formatter => $formatter
108 );
109 }
110  
111  
112 {
113 package perl5i::1::DateTime::y2038;
114  
115 # Don't load DateTime until we need it.
116 our @ISA = qw(DateTime);
117  
118 use overload
119 "eq" => sub {
120 my($dt1, $dt2) = @_;
121 return "$dt1" eq "$dt2" if !eval { $dt2->isa("DateTime") };
122 return $dt1 eq $dt2;
123 };
124  
125 sub say {
126 CORE::say("$_[0]");
127 }
128  
129 sub print {
130 CORE::print("$_[0]");
131 }
132  
133 sub from_epoch {
134 my $class = shift;
135  
136 if( perl5i::1::DateTime::NEEDS_y2038 ) {
137 no warnings 'redefine';
138 local *CORE::GLOBAL::gmtime = \&Time::y2038::gmtime;
139 local *CORE::GLOBAL::localtime = \&Time::y2038::localtime;
140  
141 return $class->SUPER::from_epoch(@_);
142 }
143 else {
144 return $class->SUPER::from_epoch(@_);
145 }
146 }
147  
148  
149 # Copy of DateTime's own epoch() function.
150 if( perl5i::1::DateTime::NEEDS_y2038 ) {
151 *epoch = sub {
152 my $self = shift;
153  
154 my $zone = $self->time_zone;
155 $self->set_time_zone("UTC");
156  
157 require Time::y2038;
158 my $time = Time::y2038::timegm(
159 $self->sec, $self->min, $self->hour, $self->mday,
160 $self->mon - 1,
161 $self->year - 1900,
162 );
163  
164 $self->set_time_zone($zone);
165  
166 return $time;
167 }
168 }
169 }
170  
171 {
172  
173 package perl5i::1::DateTime::time;
174  
175 use parent -norequire, qw(perl5i::1::DateTime::y2038);
176  
177 use overload
178 "0+" => sub { $_[0]->epoch },
179 "-" => sub {
180 my( $a, $b, $reverse ) = @_;
181  
182 if($reverse) {
183 ( $b, $a ) = ( $a, $b );
184 }
185  
186 my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a;
187 my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b;
188  
189 return $time_a - $time_b;
190 },
191  
192 "+" => sub {
193 my( $a, $b, $reverse ) = @_;
194  
195 if($reverse) {
196 ( $b, $a ) = ( $a, $b );
197 }
198  
199 my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a;
200 my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b;
201  
202 return $time_a + $time_b;
203 },
204  
205 "==" => sub {
206 my($a, $b) = @_;
207 return $a+0 == $b+0 if !eval { $b->isa("DateTime") };
208 return $a == $b;
209 },
210  
211 fallback => 1;
212 }
213  
214  
215 {
216  
217 package perl5i::1::DateTime::Format::CTime;
218  
219 use CLASS;
220  
221 sub new {
222 return bless {}, $CLASS;
223 }
224  
225 sub format_datetime {
226 my $self = shift;
227 my $dt = shift;
228  
229 # Straight from the Open Group asctime() docs.
230 return sprintf "%.3s %.3s%3d %.2d:%.2d:%.2d %d",
231 $dt->day_abbr,
232 $dt->month_abbr,
233 $dt->mday,
234 $dt->hour,
235 $dt->min,
236 $dt->sec,
237 $dt->year,
238 ;
239 }
240 }
241  
242  
243 1;