blib/lib/perl5i/1/DateTime.pm

filter: Statementstmt 31.4% 33 / 105 Branchbran 0.0% 0 / 34 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 10 / 25 Podpod 0.0% 0 / 3 totaltotal 26.1% 46 / 176 SCAR 53.5
CC
21
Cov
25%
_get_datetime_timezone
42.0
__ANON__
20.0
__ANON__
20.0
CRAP 209.6
« blib/lib/perl5i/1/SCALAR.pm ↑ Summary blib/lib/perl5i/0/SCALAR.pm »
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 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 ▶use constant NEEDS_y2038 => (
Condition42%3/7
((gmtime 140737488355327)[5] || 0) + 1900 != 4461763 || ((gmtime -62135510400)[5] || 0) + 1900 != 1
l!l&&r!l&&!r
001
(gmtime 140737488355327)[5] || 0
l!l
10
(gmtime -62135510400)[5] || 0
l!l
10
MC/DC0%0/2
((gmtime 140737488355327)[5] || 0) + 1900 != 4461763 || ((gmtime -62135510400)[5] || 0) + 1900 != 1
(gmtime 140737488355327)[5] (gmtime -62135510400)[5]
Truth table0%0/3
A || B
A= (gmtime 140737488355327)[5]B= (gmtime -62135510400)[5]
ABresult
1X1
011
000
12 ((((CORE::gmtime(2**47-1))[5] || 0) + 1900) != 4461763) ||
13 ((((CORE::gmtime(-62135510400))[5] || 0) + 1900) != 1)
14 1 ▶);
Subroutine BEGIN: called CC=4 SCAR=13.9
15  
16 BEGIN {
17 1 ▶ if( NEEDS_y2038 ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 require Time::y2038;
19 Time::y2038->import;
20 }
21 }
22  
23  
24 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
25 sub dt_gmtime (;$) {
26 0 ▶ my $time = @_ ? shift : time;
Subroutine dt_gmtime: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
27 0 ▶ return gmtime($time) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
28  
29 0 my($sec, $min, $hour, $mday, $mon, $year) = gmtime($time);
30 0 $mon++;
31 0 $year += 1900;
32  
33 0 require DateTime;
34 0 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 0 ▶ state $local_tzfile = "/etc/localtime";
Subroutine _get_datetime_timezone: not called CC=6 SCAR=37.4
48  
49 # Always be sure to honor the TZ environment var
50 0 ▶ return "local" if $ENV{TZ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TZ"} 0 0 0
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 0 ▶ if( -e $local_tzfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $local_tzfile) 0 0 0
56 # Could go through more effort to figure it out. Meh.
57 0 my $tzname = "Local";
58 0 ▶ if( -l $local_tzfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $local_tzfile) 0 0 0
59 0 ▶ if( my $real_tzfile = eval { readlink $local_tzfile } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $real_tzfile = eval { do { readlink $local_tzfile } }) 0 0 0
60 0 $tzname = $real_tzfile;
61 }
62 }
63 0 require DateTime::TimeZone::Tzfile;
64 0 my $tz = DateTime::TimeZone::Tzfile->new(
65 name => $tzname,
66 filename => $local_tzfile
67 );
68 0 ▶ return $tz if $tz;
Branch: 1 branch
BranchTrueFalseTotal
if $tz 0 0 0
69 }
70  
71 0 return "local";
72 }
73  
74 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
75 sub dt_localtime (;$) {
76 0 ▶ my $time = @_ ? shift : time;
Subroutine dt_localtime: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
77 0 ▶ return localtime($time) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
78  
79 0 my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
80 0 $mon++;
81 0 $year += 1900;
82  
83 0 state $tz = _get_datetime_timezone();
84  
85 0 require DateTime;
86 0 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 0 ▶ require DateTime::Format::Epoch;
Subroutine dt_time: not called CC=1 SCAR=6.9
Pod undocumented
102 0 state $formatter = DateTime::Format::Epoch->new( epoch => DateTime->from_epoch( epoch => 0 ) );
103  
104 0 require DateTime;
105 0 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 1 our @ISA = qw(DateTime);
117  
118 use overload
119 "eq" => sub {
120 0 ▶ my($dt1, $dt2) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
121 0 ▶ return "$dt1" eq "$dt2" if !eval { $dt2->isa("DateTime") };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $dt2->isa("DateTime") } } 0 0 0
122 0 return $dt1 eq $dt2;
123 1 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 sub say {
126 0 ▶ CORE::say("$_[0]");
Subroutine say: not called CC=1 SCAR=6.9
127 }
128  
129 sub print {
130 0 ▶ CORE::print("$_[0]");
Subroutine print: not called CC=1 SCAR=6.9
131 }
132  
133 sub from_epoch {
134 0 ▶ my $class = shift;
Subroutine from_epoch: not called CC=1 SCAR=1.2
135  
136 0 if( perl5i::1::DateTime::NEEDS_y2038 ) {
137 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
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 0 return $class->SUPER::from_epoch(@_);
145 }
146 }
147  
148  
149 # Copy of DateTime's own epoch() function.
150 1 if( perl5i::1::DateTime::NEEDS_y2038 ) {
151 *epoch = sub {
152 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
153  
154 0 my $zone = $self->time_zone;
155 0 $self->set_time_zone("UTC");
156  
157 0 require Time::y2038;
158 0 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 0 $self->set_time_zone($zone);
165  
166 0 return $time;
167 }
168 }
169 }
170  
171 {
172  
173 1 package perl5i::1::DateTime::time;
174  
175 1 ▶ use parent -norequire, qw(perl5i::1::DateTime::y2038);
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 use overload
178 0 ▶ "0+" => sub { $_[0]->epoch },
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 "-" => sub {
180 0 ▶ my( $a, $b, $reverse ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
181  
182 0 ▶ if($reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) 0 0 0
183 0 ( $b, $a ) = ( $a, $b );
184 }
185  
186 0 ▶ my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $a->isa('DateTime') } } ? : 0 0 0
187 0 ▶ my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $b->isa('DateTime') } } ? : 0 0 0
188  
189 0 return $time_a - $time_b;
190 },
191  
192 "+" => sub {
193 0 ▶ my( $a, $b, $reverse ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
194  
195 0 ▶ if($reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) 0 0 0
196 0 ( $b, $a ) = ( $a, $b );
197 }
198  
199 0 ▶ my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $a->isa('DateTime') } } ? : 0 0 0
200 0 ▶ my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $b->isa('DateTime') } } ? : 0 0 0
201  
202 0 return $time_a + $time_b;
203 },
204  
205 "==" => sub {
206 0 ▶ my($a, $b) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
207 0 ▶ return $a+0 == $b+0 if !eval { $b->isa("DateTime") };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $b->isa("DateTime") } } 0 0 0
208 0 return $a == $b;
209 },
210  
211 1 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
212 }
213  
214  
215 {
216  
217 1 package perl5i::1::DateTime::Format::CTime;
218  
219 1 ▶ use CLASS;
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 sub new {
222 0 ▶ return bless {}, $CLASS;
Subroutine new: not called CC=1 SCAR=6.9
223 }
224  
225 sub format_datetime {
226 0 ▶ my $self = shift;
Subroutine format_datetime: not called CC=1 SCAR=6.9
227 0 my $dt = shift;
228  
229 # Straight from the Open Group asctime() docs.
230 0 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 1;
« blib/lib/perl5i/1/SCALAR.pm ↑ Summary blib/lib/perl5i/0/SCALAR.pm »