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

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 155 CC 5 SCAR 14.6
CC
5
Cov
0%
dt_gmtime
12.0
dt_localtime
12.0
dt_time
2.0
CRAP 4.3
1 package perl5i::0::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 use Time::y2038;
9  
10  
11 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
12 sub dt_gmtime (;$) {
13 my $time = @_ ? shift : time;
14 return gmtime($time) if wantarray;
15  
16 my($sec, $min, $hour, $mday, $mon, $year) = gmtime($time);
17 $mon++;
18 $year += 1900;
19  
20 require DateTime;
21 return perl5i::0::DateTime::y2038->new(
22 year => $year,
23 month => $mon,
24 day => $mday,
25 hour => $hour,
26 minute => $min,
27 second => $sec,
28 formatter => "perl5i::0::DateTime::Format::CTime"
29 );
30 }
31  
32  
33 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
34 sub dt_localtime (;$) {
35 my $time = @_ ? shift : time;
36 return localtime($time) if wantarray;
37  
38 my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
39 $mon++;
40 $year += 1900;
41  
42 require DateTime;
43 return perl5i::0::DateTime::y2038->new(
44 year => $year,
45 month => $mon,
46 day => $mday,
47 hour => $hour,
48 minute => $min,
49 second => $sec,
50 time_zone => "local",
51 formatter => "perl5i::0::DateTime::Format::CTime"
52 );
53 }
54  
55  
56 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
57 sub dt_time () {
58 require DateTime::Format::Epoch;
59 state $formatter = DateTime::Format::Epoch->new( epoch => DateTime->from_epoch( epoch => 0 ) );
60  
61 require DateTime;
62 return perl5i::0::DateTime::time->from_epoch(
63 epoch => time,
64 formatter => $formatter
65 );
66 }
67  
68  
69 {
70 package perl5i::0::DateTime::y2038;
71  
72 # Don't load DateTime until we need it.
73 our @ISA = qw(DateTime);
74  
75 use overload
76 "eq" => sub {
77 my($dt1, $dt2) = @_;
78 return "$dt1" eq "$dt2" if !eval { $dt2->isa("DateTime") };
79 return $dt1 eq $dt2;
80 };
81  
82 sub from_epoch {
83 my $class = shift;
84  
85 require Time::y2038;
86 no warnings 'redefine';
87 local *CORE::GLOBAL::gmtime = \&Time::y2038::gmtime;
88 local *CORE::GLOBAL::localtime = \&Time::y2038::localtime;
89  
90 return $class->SUPER::from_epoch(@_);
91 }
92  
93  
94 # Copy of DateTime's own epoch() function.
95 sub epoch {
96 my $self = shift;
97  
98 my $zone = $self->time_zone;
99 $self->set_time_zone("UTC");
100  
101 require Time::y2038;
102 my $time = Time::y2038::timegm(
103 $self->sec, $self->min, $self->hour, $self->mday,
104 $self->mon - 1,
105 $self->year - 1900,
106 );
107  
108 $self->set_time_zone($zone);
109  
110 return $time;
111 }
112 }
113  
114 {
115  
116 package perl5i::0::DateTime::time;
117  
118 use parent -norequire, qw(perl5i::0::DateTime::y2038);
119  
120 use overload
121 "0+" => sub { $_[0]->epoch },
122 "-" => sub {
123 my( $a, $b, $reverse ) = @_;
124  
125 if($reverse) {
126 ( $b, $a ) = ( $a, $b );
127 }
128  
129 my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a;
130 my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b;
131  
132 return $time_a - $time_b;
133 },
134  
135 "+" => sub {
136 my( $a, $b, $reverse ) = @_;
137  
138 if($reverse) {
139 ( $b, $a ) = ( $a, $b );
140 }
141  
142 my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a;
143 my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b;
144  
145 return $time_a + $time_b;
146 },
147  
148 "==" => sub {
149 my($a, $b) = @_;
150 return $a+0 == $b+0 if !eval { $b->isa("DateTime") };
151 return $a == $b;
152 },
153  
154 fallback => 1;
155 }
156  
157  
158 {
159  
160 package perl5i::0::DateTime::Format::CTime;
161  
162 use CLASS;
163  
164 sub new {
165 return bless {}, $CLASS;
166 }
167  
168 sub format_datetime {
169 my $self = shift;
170 my $dt = shift;
171  
172 # Straight from the Open Group asctime() docs.
173 return sprintf "%.3s %.3s%3d %.2d:%.2d:%.2d %d",
174 $dt->day_abbr,
175 $dt->month_abbr,
176 $dt->mday,
177 $dt->hour,
178 $dt->min,
179 $dt->sec,
180 $dt->year,
181 ;
182 }
183 }
184  
185  
186 1;