DateTimeX/Lite.pm

filter: Statementstmt 98.1% 374 / 381 Branchbran 91.1% 133 / 146 Conditioncond 88.5% 100 / 113 MC/DCmcdc 83.3% 50 / 60 Subroutinesub 97.2% 106 / 109 Podpod 89.7% 70 / 78 totaltotal 93.9% 833 / 887 CC 116 SCAR 19.2
CC
116
Cov
95%
_compare
21.7
_handle_offset_modifier
19.0
new
17.0
CRAP 6.8
« DateTimeX/Lite/TimeZone.pm ↑ Summary DateTimeX/Lite/Overload.pm »
1 package DateTimeX::Lite;
2 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use constant +{
Subroutine BEGIN: called CC=2 SCAR=7.1
6 INFINITY => (9 ** 9 ** 9),
7 NEG_INFINITY => -1 * (9 ** 9 ** 9),
8 SECONDS_PER_DAY => 86400,
9 MAX_NANOSECONDS => 1_000_000_000, # 1E9 = almost 32 bits
10 56 ▶ LOCALE_SKIP => $ENV{DATETIMEX_LITE_LOCALE_SKIP} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DATETIMEX_LITE_LOCALE_SKIP'} ? : 0 56 56
11 56 };
12  
13 56 ▶use constant NAN => INFINITY - INFINITY;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 56 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 56 ▶use DateTimeX::Lite::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 56 ▶use DateTimeX::Lite::Infinite;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 56 ▶use DateTimeX::Lite::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 56 ▶use DateTimeX::Lite::LeapSecond;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 56 ▶use DateTimeX::Lite::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 56 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 56 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 56 if (LOCALE_SKIP) {
25 warn "We're skipping locale handling. You shouldn't be doing this unless you're generating locale data";
26 } else {
27 56 require DateTimeX::Lite::Locale;
28 }
29 }
30 56 our $VERSION = '0.00004';
31  
32 56 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
33 56 my @local_c_comp = qw(year month day hour minute second quarter);
34 56 foreach my $comp (@local_c_comp) {
35 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 392 ▶ *{$comp} = sub { $_[0]->{local_c}{$comp} };
Subroutine __ANON__: called CC=1 SCAR=0.0
37 }
38 }
39  
40 56 our $DefaultLocale = 'en_US';
41  
42 55 ▶sub import {
Subroutine import: called CC=3 SCAR=11.1
43 55 my $class = shift;
44 55 foreach my $component (@_) {
45 32 eval "require DateTimeX::Lite::$component";
46 32 ▶ die "DateTimeX::Lite failed to load $component component: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 32 32
47 }
48 }
49  
50 8911 ▶sub utc_rd_values { @{ $_[0] }{ 'utc_rd_days', 'utc_rd_secs', 'rd_nanosecs' } }
Subroutine utc_rd_values: called CC=1 SCAR=0.0
Pod documented
51 12 ▶sub local_rd_values { @{ $_[0] }{ 'local_rd_days', 'local_rd_secs', 'rd_nanosecs' } }
Subroutine local_rd_values: called CC=1 SCAR=0.0
Pod documented
52  
53 # NOTE: no nanoseconds, no leap seconds
54 5347 ▶sub utc_rd_as_seconds { ( $_[0]->{utc_rd_days} * SECONDS_PER_DAY ) + $_[0]->{utc_rd_secs} }
Subroutine utc_rd_as_seconds: called CC=1 SCAR=0.0
Pod documented
55  
56 # NOTE: no nanoseconds, no leap seconds
57 270 ▶sub local_rd_as_seconds { ( $_[0]->{local_rd_days} * SECONDS_PER_DAY ) + $_[0]->{local_rd_secs} }
Subroutine local_rd_as_seconds: called CC=1 SCAR=0.0
Pod documented
58  
59 # RD 1 is JD 1,721,424.5 - a simple offset
60 sub jd
61 24 ▶{
Subroutine jd: called CC=1 SCAR=0.0
Pod documented
62 24 my $self = shift;
63  
64 24 my $jd = $self->{utc_rd_days} + 1_721_424.5;
65  
66 24 my $day_length = DateTimeX::Lite::LeapSecond::day_length( $self->{utc_rd_days} );
67  
68 return ( $jd +
69 ( $self->{utc_rd_secs} / $day_length ) +
70 24 ( $self->{rd_nanosecs} / $day_length / MAX_NANOSECONDS )
71 );
72 }
73  
74 10 ▶sub mjd { $_[0]->jd - 2_400_000.5 }
Subroutine mjd: called CC=1 SCAR=0.0
Pod documented
75  
76 6285 ▶sub clone { bless { %{ $_[0] } }, ref $_[0] }
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
77  
78 0 ▶sub to_datetime {
Subroutine to_datetime: not called CC=2 SCAR=17.9
Pod documented
79 0 eval {
80 0 require DateTime;
81 };
82 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
83 0 Carp::croak("Could not load DateTime: $@");
84 }
85 0 return DateTime->from_object(object => $_[0]);
86 }
87  
88 7104 ▶sub set_time_zone {
Subroutine set_time_zone: called CC=6 SCAR=18.0
Pod documented
89 7104 my ( $self, $tz ) = @_;
90  
91 # This is a bit of a hack but it works because time zone objects
92 # are singletons, and if it doesn't work all we lose is a little
93 # bit of speed.
94 7104 ▶ return $self if $self->{tz} eq $tz;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tz"} eq $tz 621 6483 7104
95  
96 6483 my $was_floating = $self->{tz}->is_floating;
97  
98 6483 ▶ $self->{tz} = ref $tz ? $tz : DateTimeX::Lite::TimeZone->load( name => $tz );
Branch: 1 branch
BranchTrueFalseTotal
ref $tz ? : 6420 63 6483
99  
100 6483 $self->_handle_offset_modifier( $self->second, 1 );
101  
102 # if it either was or now is floating (but not both)
103 6483 ▶ if ( $self->{tz}->is_floating xor $was_floating )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'tz'}->is_floating xor $was_floating) { } 6234 249 6483
elsif (not $was_floating) { } 249 0 249
Condition75%3/4
$self->{"tz"}->is_floating xor $was_floating
l&&rl&&!r!l&&r!l&&!r
062286249
MC/DC100%2/2
$self->{"tz"}->is_floating xor $was_floating
$self->{"tz"}->is_floating $was_floating
Truth table75%3/4
A xor B
A= $self->{"tz"}->is_floatingB= $was_floating
ABresult
110
101
011
000
104 {
105 6234 $self->_calc_utc_rd;
106 }
107 elsif ( ! $was_floating )
108 {
109 249 $self->_calc_local_rd;
110 }
111  
112 6483 return $self;
113 }
114  
115  
116 28649 ▶sub new {
Subroutine new: called CC=17 SCAR=28.3
Pod documented
117 28649 my ($class, %p) = @_;
118  
119 # give default values, first...
120 {
121 28649 my %spec = (
122 day => { default => 1, range => [1, 31] },
123 month => { default => 1, range => [1, 12] },
124 year => {default => 1},
125 hour => {default => 0, range => [0, 23]},
126 minute => {default => 0, range => [0, 59]},
127 second => {default => 0, range => [0, 61]},
128 nanosecond => {default => 0, range => [0,undef]}
129 );
130  
131 28649 ▶ while (my ($key, $spec) = each %spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %spec) 200467 28629 229096
132 200467 my $default = $spec->{default};
133 200467 ▶ $p{$key} = $default unless defined $p{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p{$key} 13969 186498 200467
134  
135 200467 ▶ if (my $range = $spec->{range}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = $spec->{"range"}) 171832 28635 200467
136 171832 my $v = $p{$key};
137 171832 ▶ if ( (defined $range->[0] && $v < $range->[0]) ||
Branch: 1 branch
BranchTrueFalseTotal
if (defined $range->[0] and $v < $range->[0] or defined $range->[1] and $v > $range->[1]) 20 171812 171832
Condition88%8/9
defined $range->[0] and $v < $range->[0] or defined $range->[1] and $v > $range->[1]
l!l&&r!l&&!r
1010171812
defined $range->[0] and $v < $range->[0]
!ll&&!rl&&r
017182210
defined $range->[1] and $v > $range->[1]
!ll&&!rl&&r
2863814317410
MC/DC75%3/4
defined $range->[0] and $v < $range->[0] or defined $range->[1] and $v > $range->[1]
defined $range->[0] $v < $range->[0] defined $range->[1] $v > $range->[1]
Truth table57%4/7
A and B or C and D
A= defined $range->[0]B= $v < $range->[0]C= defined $range->[1]D= $v > $range->[1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
138 (defined $range->[1] && $v > $range->[1]) ) {
139 20 Carp::croak(qq|The '$key' parameter ("$p{$key}") to DateTimeX::Lite::new did not pass the range test|); # hmm, almost
140 }
141 }
142 }
143 }
144 28629 my $day = $p{day};
145 28629 my $month = $p{month};
146 28629 my $year = $p{year};
147 28629 my $hour = $p{hour};
148 28629 my $minute = $p{minute};
149 28629 my $second = $p{second};
150 28629 my $nanosecond = $p{nanosecond};
151  
152 28629 ▶ if ($day > DateTimeX::Lite::Util::month_length($year, $month)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day > DateTimeX::Lite::Util::month_length($year, $month)) 2 28627 28629
153 2 Carp::croak("Invalid day of month (day = $day - month = $month - year = $year\n");
154 }
155  
156 28627 my $self = bless {}, $class;
157  
158 28627 ▶ my $locale = delete $p{language} || delete $p{locale};
Condition100%3/3
delete $p{'language'} || delete $p{'locale'}
l!l&&r!l&&!r
1240274599
MC/DC100%2/2
delete $p{'language'} || delete $p{'locale'}
delete $p{'language'} delete $p{'locale'}
Truth table100%3/3
A || B
A= delete $p{'language'}B= delete $p{'locale'}
ABresult
1X1
011
000
159 28627 ▶ $locale = $DefaultLocale unless defined $locale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locale 4599 24028 28627
160 28627 ▶ my $time_zone = $p{time_zone} || 'floating';
Condition100%2/2
$p{'time_zone'} || 'floating'
l!l
274641163
MC/DC100%1/1
$p{'time_zone'} || 'floating'
$p{'time_zone'}
Truth table100%2/2
A
A= $p{'time_zone'}
Aresult
11
00
161  
162 28627 $self->{offset_modifier} = 0;
163  
164 # XXX This only happens when we're generating the locales
165 28627 if (! LOCALE_SKIP) {
166 28627 ▶ $self->{locale} = blessed $locale ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($locale) ? : 24014 4613 28627
167 $locale : DateTimeX::Lite::Locale->load($locale);
168 }
169  
170 28627 ▶ $self->{tz} = blessed $time_zone ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($time_zone) ? : 17066 11561 28627
171 $time_zone : DateTimeX::Lite::TimeZone->load(name => $time_zone);
172 28627 $self->{local_rd_days} = DateTimeX::Lite::Util::ymd2rd($year, $month, $day);
173 28627 $self->{local_rd_secs} = DateTimeX::Lite::Util::time_as_seconds($hour, $minute, $second);
174 28627 $self->{offfset_modifier} = 0;
175 28627 $self->{rd_nanosecs} = $nanosecond;
176 28627 $self->{formatter} = $p{formatter};
177  
178 28627 DateTimeX::Lite::Util::normalize_nanoseconds($self->{local_rd_secs}, $self->{rd_nanosecs});
179  
180 28627 $self->{utc_year} = $year + 1;
181 28627 $self->_calc_utc_rd;
182 28622 $self->_handle_offset_modifier($second);
183 28622 $self->_calc_local_rd;
184  
185 28622 ▶ if ($second > 59) {
Branch: 1 branch
BranchTrueFalseTotal
if ($second > 59) 46 28576 28622
186 46 ▶ if ($self->{tz}->is_floating || $self->{utc_rd_secs} - SECONDS_PER_DAY + 1 < $second - 59) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tz"}->is_floating or $self->{"utc_rd_secs"} - 86400 + 1 < $second - 59) 3 43 46
Condition100%3/3
$self->{"tz"}->is_floating or $self->{"utc_rd_secs"} - 86400 + 1 < $second - 59
l!l&&r!l&&!r
1243
MC/DC100%2/2
$self->{"tz"}->is_floating or $self->{"utc_rd_secs"} - 86400 + 1 < $second - 59
$self->{"tz"}->is_floating $self->{"utc_rd_secs"} - 86400 + 1 < $second - 59
Truth table100%3/3
A or B
A= $self->{"tz"}->is_floatingB= $self->{"utc_rd_secs"} - 86400 + 1 < $second - 59
ABresult
1X1
011
000
187 3 Carp::croak("Invalid second value ($second)\n");
188 }
189 }
190  
191 28619 return $self;
192 }
193  
194 39305 ▶sub _calc_utc_rd {
Subroutine _calc_utc_rd: called CC=3 SCAR=11.0
195 39305 my $self = shift;
196 39305 delete $self->{utc_c};
197  
198 39305 my $time_zone = $self->{tz};
199 39305 ▶ if ($time_zone->is_utc || $time_zone->is_floating) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_zone->is_utc or $time_zone->is_floating) { } 39119 186 39305
Condition100%3/3
$time_zone->is_utc or $time_zone->is_floating
l!l&&r!l&&!r
2534513774186
MC/DC100%2/2
$time_zone->is_utc or $time_zone->is_floating
$time_zone->is_utc $time_zone->is_floating
Truth table100%3/3
A or B
A= $time_zone->is_utcB= $time_zone->is_floating
ABresult
1X1
011
000
200 39119 $self->{utc_rd_days} = $self->{local_rd_days};
201 39119 $self->{utc_rd_secs} = $self->{local_rd_secs};
202 } else {
203 186 my $offset = $self->_offset_for_local_datetime;
204 178 $offset += $self->{offset_modifier};
205  
206 178 $self->{utc_rd_days} = $self->{local_rd_days};
207 178 $self->{utc_rd_secs} = $self->{local_rd_secs} - $offset;
208 }
209  
210 # We account for leap seconds in the new() method and nowhere else
211 # except date math.
212 39297 DateTimeX::Lite::Util::normalize_tai_seconds( $self->{utc_rd_days}, $self->{utc_rd_secs} );
213 }
214  
215 sub _handle_offset_modifier
216 41994 ▶{
Subroutine _handle_offset_modifier: called CC=19 SCAR=29.5
217 41994 my $self = shift;
218  
219 41994 $self->{offset_modifier} = 0;
220  
221 41994 ▶ return if $self->{tz}->is_floating;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tz"}->is_floating 16180 25814 41994
222  
223 25814 my $second = shift;
224 25814 my $utc_is_valid = shift;
225  
226 25814 my $utc_rd_days = $self->{utc_rd_days};
227  
228 25814 ▶ my $offset = $utc_is_valid ? $self->offset : $self->_offset_for_local_datetime;
Branch: 1 branch
BranchTrueFalseTotal
$utc_is_valid ? : 255 25559 25814
229  
230 25814 ▶ if ( $offset >= 0
Branch: 2 branches
BranchTrueFalseTotal
if ($offset >= 0 and $self->{'local_rd_secs'} >= $offset) { } 25492 322 25814
elsif ($offset < 0 and $self->{'local_rd_secs'} >= 86400 + $offset) { } 30 335 365
Condition100%3/3
$offset >= 0 and $self->{"local_rd_secs"} >= $offset
!ll&&!rl&&r
2794325492
MC/DC100%2/2
$offset >= 0 and $self->{"local_rd_secs"} >= $offset
$offset >= 0 $self->{"local_rd_secs"} >= $offset
Truth table100%3/3
A and B
A= $offset >= 0B= $self->{"local_rd_secs"} >= $offset
ABresult
0X0
100
111
231 && $self->{local_rd_secs} >= $offset
232 )
233 {
234 25492 ▶ if ( $second < 60 && $offset > 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ($second < 60 and $offset > 0) { } 51 25441 25492
elsif ($second == 60 and $self->{'local_rd_secs'} == $offset && $offset > 0 || $offset == 0 && $self->{'local_rd_secs'} > 86399) { } 39 50795 50834
Condition100%3/3
$second < 60 and $offset > 0
!ll&&!rl&&r
592538251
MC/DC100%2/2
$second < 60 and $offset > 0
$second < 60 $offset > 0
Truth table100%3/3
A and B
A= $second < 60B= $offset > 0
ABresult
0X0
100
111
235 {
236 $self->{offset_modifier} =
237 51 DateTimeX::Lite::LeapSecond::day_length( $utc_rd_days - 1 ) - SECONDS_PER_DAY;
238  
239 51 $self->{local_rd_secs} += $self->{offset_modifier};
240 }
241 ▶ elsif ( $second == 60
Condition91%11/12
$second == 60 and $self->{"local_rd_secs"} == $offset && $offset > 0 || $offset == 0 && $self->{"local_rd_secs"} > 86399
!ll&&!rl&&r
253911139
$self->{"local_rd_secs"} == $offset and $offset > 0 or $offset == 0 and $self->{"local_rd_secs"} > 86399
l!l&&r!l&&!r
33611
$self->{"local_rd_secs"} == $offset and $offset > 0
!ll&&!rl&&r
4703
$offset == 0 and $self->{"local_rd_secs"} > 86399
!ll&&!rl&&r
2936
MC/DC80%4/5
$second == 60 and $self->{"local_rd_secs"} == $offset && $offset > 0 || $offset == 0 && $self->{"local_rd_secs"} > 86399
$second == 60 $self->{"local_rd_secs"} == $offset $offset > 0 $offset == 0 $self->{"local_rd_secs"} > 86399
Truth table62%5/8
A and B and C or D and E
A= $second == 60B= $self->{"local_rd_secs"} == $offsetC= $offset > 0D= $offset == 0E= $self->{"local_rd_secs"} > 86399
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
242 &&
243 ( ( $self->{local_rd_secs} == $offset
244 && $offset > 0 )
245 ||
246 ( $offset == 0
247 && $self->{local_rd_secs} > 86399 ) )
248 )
249 {
250 39 my $mod = DateTimeX::Lite::LeapSecond::day_length( $utc_rd_days - 1 ) - SECONDS_PER_DAY;
251  
252 39 ▶ unless ( $mod == 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod == 0) 38 1 39
253 {
254 38 $self->{utc_rd_secs} -= $mod;
255  
256 38 DateTimeX::Lite::Util::normalize_seconds($self);
257 }
258 }
259 }
260 ▶ elsif ( $offset < 0
Condition100%3/3
$offset < 0 and $self->{"local_rd_secs"} >= 86400 + $offset
!ll&&!rl&&r
4324930
MC/DC100%2/2
$offset < 0 and $self->{"local_rd_secs"} >= 86400 + $offset
$offset < 0 $self->{"local_rd_secs"} >= 86400 + $offset
Truth table100%3/3
A and B
A= $offset < 0B= $self->{"local_rd_secs"} >= 86400 + $offset
ABresult
0X0
100
111
261 && $self->{local_rd_secs} >= SECONDS_PER_DAY + $offset )
262 {
263 30 ▶ if ( $second < 60 )
Branch: 2 branches
BranchTrueFalseTotal
if ($second < 60) { } 23 7 30
elsif ($second == 60 and $self->{'local_rd_secs'} == 86400 + $offset) { } 5 2 7
264 {
265 $self->{offset_modifier} =
266 23 DateTimeX::Lite::LeapSecond::day_length( $utc_rd_days - 1 ) - SECONDS_PER_DAY;
267 23 $self->{local_rd_secs} += $self->{offset_modifier};
268 }
269 ▶ elsif ( $second == 60 && $self->{local_rd_secs} == SECONDS_PER_DAY + $offset )
Condition66%2/3
$second == 60 and $self->{"local_rd_secs"} == 86400 + $offset
!ll&&!rl&&r
025
MC/DC50%1/2
$second == 60 and $self->{"local_rd_secs"} == 86400 + $offset
$second == 60 $self->{"local_rd_secs"} == 86400 + $offset
Truth table66%2/3
A and B
A= $second == 60B= $self->{"local_rd_secs"} == 86400 + $offset
ABresult
0X0
100
111
270 {
271 5 my $mod = DateTimeX::Lite::LeapSecond::day_length( $utc_rd_days - 1 ) - SECONDS_PER_DAY;
272  
273 5 ▶ unless ( $mod == 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod == 0) 5 0 5
274 {
275 5 $self->{utc_rd_secs} -= $mod;
276  
277 5 DateTimeX::Lite::Util::normalize_seconds($self);
278 }
279 }
280 }
281 }
282  
283 sub _calc_local_rd
284 28875 ▶{
Subroutine _calc_local_rd: called CC=3 SCAR=11.0
285 28875 my $self = shift;
286  
287 28875 delete $self->{local_c};
288  
289 # We must short circuit for UTC times or else we could end up with
290 # loops between DateTime.pm and DateTimeX::Lite::TimeZone
291 28875 ▶ if ( $self->{tz}->is_utc || $self->{tz}->is_floating )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'tz'}->is_utc or $self->{'tz'}->is_floating) { } 28559 316 28875
Condition100%3/3
$self->{"tz"}->is_utc or $self->{"tz"}->is_floating
l!l&&r!l&&!r
248223737316
MC/DC100%2/2
$self->{"tz"}->is_utc or $self->{"tz"}->is_floating
$self->{"tz"}->is_utc $self->{"tz"}->is_floating
Truth table100%3/3
A or B
A= $self->{"tz"}->is_utcB= $self->{"tz"}->is_floating
ABresult
1X1
011
000
292 {
293 28559 $self->{local_rd_days} = $self->{utc_rd_days};
294 28559 $self->{local_rd_secs} = $self->{utc_rd_secs};
295 }
296 else
297 316 { my $offset = $self->offset;
298  
299 316 $self->{local_rd_days} = $self->{utc_rd_days};
300 316 $self->{local_rd_secs} = $self->{utc_rd_secs} + $offset;
301  
302 # intentionally ignore leap seconds here
303 316 DateTimeX::Lite::Util::normalize_tai_seconds( $self->{local_rd_days}, $self->{local_rd_secs} );
304  
305 316 $self->{local_rd_secs} += $self->{offset_modifier};
306 }
307  
308 28875 $self->_calc_local_components;
309 }
310  
311 sub _calc_local_components
312 28871 ▶{
Subroutine _calc_local_components: called CC=1 SCAR=0.0
313 28871 my $self = shift;
314  
315 28871 @{ $self->{local_c} }{ qw( year month day day_of_week
316 day_of_year quarter day_of_quarter) } =
317 28871 DateTimeX::Lite::Util::rd2ymd( $self->{local_rd_days}, 1 );
318  
319 28871 @{ $self->{local_c} }{ qw( hour minute second ) } =
320 DateTimeX::Lite::Util::seconds_as_components
321 28871 ( $self->{local_rd_secs}, $self->{utc_rd_secs}, $self->{offset_modifier} );
322 }
323  
324 6958 ▶sub from_object {
Subroutine from_object: called CC=7 SCAR=19.5
Pod documented
325 6958 my ($class, %p) = @_;
326 6958 my $object = delete $p{object};
327  
328 6958 my ( $rd_days, $rd_secs, $rd_nanosecs ) = $object->utc_rd_values;
329  
330 # A kludge because until all calendars are updated to return all
331 # three values, $rd_nanosecs could be undef
332 6958 ▶ $rd_nanosecs ||= 0;
Condition100%2/2
$rd_nanosecs ||= 0
l!l
46954
MC/DC100%1/1
$rd_nanosecs ||= 0
$rd_nanosecs
Truth table100%2/2
A
A= $rd_nanosecs
Aresult
11
00
333  
334 # This is a big hack to let _seconds_as_components operate naively
335 # on the given value. If the object _is_ on a leap second, we'll
336 # add that to the generated seconds value later.
337 6958 my $leap_seconds = 0;
338 6958 ▶ if ( $object->can('time_zone') && ! $object->time_zone->is_floating
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("time_zone") and not $object->time_zone->is_floating and $rd_secs > 86399 and $rd_secs <= DateTimeX::Lite::LeapSecond::day_length($rd_days)) 6 6952 6958
Condition88%8/9
$object->can("time_zone") and not $object->time_zone->is_floating and $rd_secs > 86399 and $rd_secs <= DateTimeX::Lite::LeapSecond::day_length($rd_days)
!ll&&!rl&&r
695206
$object->can("time_zone") and not $object->time_zone->is_floating and $rd_secs > 86399
!ll&&!rl&&r
62257276
$object->can("time_zone") and not $object->time_zone->is_floating
!ll&&!rl&&r
36222733
MC/DC75%3/4
$object->can("time_zone") and not $object->time_zone->is_floating and $rd_secs > 86399 and $rd_secs <= DateTimeX::Lite::LeapSecond::day_length($rd_days)
$object->can("time_zone") not $object->time_zone->is_floating $rd_secs > 86399 $rd_secs <= DateTimeX::Lite::LeapSecond::day_length($rd_days)
Truth table80%4/5
A and not(B) and C and D
A= $object->can("time_zone")B= not $object->time_zone->is_floatingC= $rd_secs > 86399D= $rd_secs <= DateTimeX::Lite::LeapSecond::day_length($rd_days)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
339 && $rd_secs > 86399 && $rd_secs <= DateTimeX::Lite::LeapSecond::day_length($rd_days) )
340 {
341 6 $leap_seconds = $rd_secs - 86399;
342 6 $rd_secs -= $leap_seconds;
343 }
344  
345 6958 my %args;
346 6958 @args{ qw( year month day ) } = DateTimeX::Lite::Util::rd2ymd($rd_days);
347 6958 @args{ qw( hour minute second ) } =
348 DateTimeX::Lite::Util::seconds_as_components($rd_secs);
349 6958 $args{nanosecond} = $rd_nanosecs;
350  
351 6958 $args{second} += $leap_seconds;
352  
353 6958 my $new = $class->new( %p, %args, time_zone => 'UTC' );
354  
355 6958 ▶ if ( $object->can('time_zone') )
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can('time_zone')) { } 6955 3 6958
356 {
357 6955 $new->set_time_zone( $object->time_zone );
358 }
359 else
360 {
361 3 $new->set_time_zone( 'floating' );
362 }
363  
364 6958 return $new;
365 }
366  
367  
368 74 ▶sub last_day_of_month {
Subroutine last_day_of_month: called CC=3 SCAR=11.0
Pod documented
369 74 my ($class, %p) = @_;
370 74 ▶ if ($p{month} > 12 || $p{month} < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"month"} > 12 or $p{"month"} < 1) 2 72 74
Condition100%3/3
$p{"month"} > 12 or $p{"month"} < 1
l!l&&r!l&&!r
1172
MC/DC100%2/2
$p{"month"} > 12 or $p{"month"} < 1
$p{"month"} > 12 $p{"month"} < 1
Truth table100%3/3
A or B
A= $p{"month"} > 12B= $p{"month"} < 1
ABresult
1X1
011
000
371 2 Carp::croak(qq|The 'month' parameter ("$p{month}") to DateTimeX::Lite::last_day_of_month did not pass the 'is between 1 and 12' callback|);
372 }
373  
374 72 return $class->new(%p, day => DateTimeX::Lite::Util::month_length($p{year}, $p{month}));
375 }
376  
377 589 ▶sub offset { $_[0]->{tz}->offset_for_datetime( $_[0] ) }
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
378 25745 ▶sub _offset_for_local_datetime { $_[0]->{tz}->offset_for_local_datetime( $_[0] ) }
Subroutine _offset_for_local_datetime: called CC=1 SCAR=0.0
379  
380  
381 17220 ▶sub nanosecond { $_[0]->{rd_nanosecs} }
Subroutine nanosecond: called CC=1 SCAR=0.0
Pod documented
382 3 ▶sub fractional_second { $_[0]->second + $_[0]->nanosecond / MAX_NANOSECONDS }
Subroutine fractional_second: called CC=1 SCAR=0.0
Pod documented
383  
384 8 ▶sub millisecond { _round( $_[0]->{rd_nanosecs} / 1000000 ) }
Subroutine millisecond: called CC=1 SCAR=0.0
Pod documented
385  
386 7 ▶sub microsecond { _round( $_[0]->{rd_nanosecs} / 1000 ) }
Subroutine microsecond: called CC=1 SCAR=0.0
Pod documented
387  
388 sub _round
389 15 ▶{
Subroutine _round: called CC=2 SCAR=6.9
390 15 my $val = shift;
391 15 my $int = int $val;
392  
393 15 ▶ return $val - $int >= 0.5 ? $int + 1 : $int;
Branch: 1 branch
BranchTrueFalseTotal
$val - $int >= 0.5 ? : 4 11 15
394 }
395  
396 20 ▶sub ce_year {
Subroutine ce_year: called CC=2 SCAR=6.9
Pod documented
397 20 my $year = $_[0]->{local_c}{year};
398 20 ▶ return $year <= 0 ? $year - 1 : $year
Branch: 1 branch
BranchTrueFalseTotal
$year <= 0 ? : 10 10 20
399 }
400  
401 2 ▶sub era_name { $_[0]->{locale}->era_wide->[ $_[0]->_era_index() ] }
Subroutine era_name: called CC=1 SCAR=0.0
Pod undocumented
402  
403 10 ▶sub era_abbr { $_[0]->{locale}->era_abbreviated->[ $_[0]->_era_index() ] }
Subroutine era_abbr: called CC=1 SCAR=0.0
Pod undocumented
404  
405 13 ▶sub _era_index { $_[0]->{local_c}{year} <= 0 ? 0 : 1 }
Subroutine _era_index: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'local_c'}{'year'} <= 0 ? : 2 11 13
406  
407 4 ▶sub christian_era { $_[0]->ce_year > 0 ? 'AD' : 'BC' }
Subroutine christian_era: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ce_year > 0 ? : 2 2 4
408 4 ▶sub secular_era { $_[0]->ce_year > 0 ? 'CE' : 'BCE' }
Subroutine secular_era: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ce_year > 0 ? : 2 2 4
409  
410 2 ▶sub year_with_era { (abs $_[0]->ce_year) . $_[0]->era_abbr }
Subroutine year_with_era: called CC=1 SCAR=0.0
Pod undocumented
411 2 ▶sub year_with_christian_era { (abs $_[0]->ce_year) . $_[0]->christian_era }
Subroutine year_with_christian_era: called CC=1 SCAR=0.0
Pod undocumented
412 2 ▶sub year_with_secular_era { (abs $_[0]->ce_year) . $_[0]->secular_era }
Subroutine year_with_secular_era: called CC=1 SCAR=0.0
Pod undocumented
413  
414  
415 6 ▶sub month_name { $_[0]->{locale}->month_format_wide->[ $_[0]->month() - 1] }
Subroutine month_name: called CC=1 SCAR=0.0
Pod documented
416  
417 10 ▶sub month_abbr { $_[0]->{locale}->month_format_abbreviated->[ $_[0]->month() - 1] }
Subroutine month_abbr: called CC=1 SCAR=0.0
Pod documented
418  
419 56 ▶sub weekday_of_month { use integer; ( ( $_[0]->day - 1 ) / 7 ) + 1 }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine weekday_of_month: called CC=1 SCAR=0.0
Pod documented
420  
421 2 ▶sub quarter_name { $_[0]->{locale}->quarter_format_wide->[ $_[0]->quarter() - 1] }
Subroutine quarter_name: called CC=1 SCAR=0.0
Pod documented
422 2 ▶sub quarter_abbr { $_[0]->{locale}->quarter_format_abbreviated->[ $_[0]->quarter() - 1] }
Subroutine quarter_abbr: called CC=1 SCAR=0.0
Pod documented
423  
424 12450 ▶sub day_of_week { $_[0]->{local_c}{day_of_week} }
Subroutine day_of_week: called CC=1 SCAR=0.0
Pod documented
425  
426 sub local_day_of_week
427 4 ▶{
Subroutine local_day_of_week: called CC=2 SCAR=7.0
Pod documented
428 4 my $self = shift;
429  
430 4 my $day = $self->day_of_week();
431  
432 4 my $local_first_day = $self->{locale}->first_day_of_week();
433  
434 4 my $d = ( ( 8 - $local_first_day ) + $day ) % 7;
435  
436 4 ▶ return $d == 0 ? 7 : $d;
Branch: 1 branch
BranchTrueFalseTotal
$d == 0 ? : 0 4 4
437 }
438  
439  
440 5 ▶sub hour_1 { $_[0]->{local_c}{hour} == 0 ? 24 : $_[0]->{local_c}{hour} }
Subroutine hour_1: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'local_c'}{'hour'} == 0 ? : 1 4 5
441  
442 30 ▶sub hour_12 { my $h = $_[0]->hour % 12; return $h ? $h : 12 }
Subroutine hour_12: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$h ? : 23 7 30
443  
444 6 ▶sub day_name { $_[0]->{locale}->day_format_wide->[ $_[0]->day_of_week() - 1 ] }
Subroutine day_name: called CC=1 SCAR=0.0
Pod documented
445  
446 8 ▶sub day_abbr { $_[0]->{locale}->day_format_abbreviated->[ $_[0]->day_of_week() - 1] }
Subroutine day_abbr: called CC=1 SCAR=0.0
Pod documented
447  
448 8 ▶sub day_of_quarter { $_[0]->{local_c}{day_of_quarter} }
Subroutine day_of_quarter: called CC=1 SCAR=0.0
Pod documented
449  
450 70 ▶sub day_of_year { $_[0]->{local_c}{day_of_year} }
Subroutine day_of_year: called CC=1 SCAR=0.0
Pod documented
451  
452 23 ▶sub am_or_pm { $_[0]->{locale}->am_pm_abbreviated->[ $_[0]->hour() < 12 ? 0 : 1 ] }
Subroutine am_or_pm: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->hour < 12 ? : 8 15 23
453  
454 # ISO says that the first week of a year is the first week containing
455 # a Thursday. Extending that says that the first week of the month is
456 # the first week containing a Thursday. ICU agrees.
457 sub week_of_month
458 4 ▶{
Subroutine week_of_month: called CC=1 SCAR=0.0
Pod documented
459 4 my $self = shift;
460  
461 4 my $thu = $self->day + 4 - $self->day_of_week;
462 4 return int( ( $thu + 6 ) / 7 );
463 }
464  
465 sub week
466 37 ▶{
Subroutine week: called CC=6 SCAR=17.9
Pod documented
467 37 my $self = shift;
468  
469 37 ▶ unless ( defined $self->{local_c}{week_year} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"local_c"}{"week_year"}) 31 6 37
470 {
471 # This algorithm was taken from Date::Calc's DateCalc.c file
472 31 my $jan_one_dow_m1 =
473 ( ( DateTimeX::Lite::Util::ymd2rd( $self->year, 1, 1 ) + 6 ) % 7 );
474  
475 $self->{local_c}{week_number} =
476 31 int( ( ( $self->day_of_year - 1 ) + $jan_one_dow_m1 ) / 7 );
477 31 ▶ $self->{local_c}{week_number}++ if $jan_one_dow_m1 < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $jan_one_dow_m1 < 4 18 13 31
478  
479 31 ▶ if ( $self->{local_c}{week_number} == 0 )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'local_c'}{'week_number'} == 0) { } 4 27 31
elsif ($self->{'local_c'}{'week_number'} == 53 and $self->_weeks_in_year($self->year) == 52) { } 5 22 27
480 {
481 4 $self->{local_c}{week_year} = $self->year - 1;
482 $self->{local_c}{week_number} =
483 4 $self->_weeks_in_year( $self->{local_c}{week_year} );
484 }
485 ▶ elsif ( $self->{local_c}{week_number} == 53 &&
Condition100%3/3
$self->{"local_c"}{"week_number"} == 53 and $self->_weeks_in_year($self->year) == 52
!ll&&!rl&&r
2025
MC/DC100%2/2
$self->{"local_c"}{"week_number"} == 53 and $self->_weeks_in_year($self->year) == 52
$self->{"local_c"}{"week_number"} == 53 $self->_weeks_in_year($self->year) == 52
Truth table100%3/3
A and B
A= $self->{"local_c"}{"week_number"} == 53B= $self->_weeks_in_year($self->year) == 52
ABresult
0X0
100
111
486 $self->_weeks_in_year( $self->year ) == 52 )
487 {
488 5 $self->{local_c}{week_number} = 1;
489 5 $self->{local_c}{week_year} = $self->year + 1;
490 }
491 else
492 {
493 22 $self->{local_c}{week_year} = $self->year;
494 }
495 }
496  
497 37 return @{ $self->{local_c} }{ 'week_year', 'week_number' }
498 }
499  
500 # Also from DateCalc.c
501 sub _weeks_in_year
502 11 ▶{
Subroutine _weeks_in_year: called CC=4 SCAR=13.9
503 11 my $self = shift;
504 11 my $year = shift;
505  
506 11 my $dow = DateTimeX::Lite::Util::ymd2rd($year, 1, 1) % 7;
507
508 # Tears starting with a Thursday and leap years starting with a Wednesday
509 # have 53 weeks.
510 11 ▶ return ( $dow == 4 || ( $dow == 3 && DateTimeX::Lite::Util::is_leap_year( $year ) ) )
Branch: 1 branch
BranchTrueFalseTotal
$dow == 4 || $dow == 3 && DateTimeX::Lite::Util::is_leap_year($year) ? : 4 7 11
Condition100%6/6
$dow == 4 or $dow == 3 and DateTimeX::Lite::Util::is_leap_year($year)
l!l&&r!l&&!r
227
$dow == 3 and DateTimeX::Lite::Util::is_leap_year($year)
!ll&&!rl&&r
612
MC/DC100%3/3
$dow == 4 or $dow == 3 and DateTimeX::Lite::Util::is_leap_year($year)
$dow == 4 $dow == 3 DateTimeX::Lite::Util::is_leap_year($year)
Truth table100%4/4
A or B and C
A= $dow == 4B= $dow == 3C= DateTimeX::Lite::Util::is_leap_year($year)
ABCresult
1XX1
0111
00X0
0100
511 ? 53
512 : 52;
513 }
514  
515 5 ▶sub week_year { ($_[0]->week)[0] }
Subroutine week_year: called CC=1 SCAR=0.0
Pod documented
516 6 ▶sub week_number { ($_[0]->week)[1] }
Subroutine week_number: called CC=1 SCAR=0.0
Pod documented
517  
518 sub ymd
519 717 ▶{
Subroutine ymd: called CC=2 SCAR=6.9
Pod documented
520 717 my ( $self, $sep ) = @_;
521 717 ▶ $sep = '-' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 554 163 717
522  
523 return sprintf( "%0.4d%s%0.2d%s%0.2d",
524 $self->year, $sep,
525 $self->{local_c}{month}, $sep,
526 717 $self->{local_c}{day} );
527 }
528  
529 sub mdy
530 3 ▶{
Subroutine mdy: called CC=2 SCAR=6.9
Pod documented
531 3 my ( $self, $sep ) = @_;
532 3 ▶ $sep = '-' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 1 2 3
533  
534 return sprintf( "%0.2d%s%0.2d%s%0.4d",
535 $self->{local_c}{month}, $sep,
536 3 $self->{local_c}{day}, $sep,
537 $self->year );
538 }
539  
540 sub dmy
541 3 ▶{
Subroutine dmy: called CC=2 SCAR=6.9
Pod documented
542 3 my ( $self, $sep ) = @_;
543 3 ▶ $sep = '-' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 1 2 3
544  
545 return sprintf( "%0.2d%s%0.2d%s%0.4d",
546 $self->{local_c}{day}, $sep,
547 3 $self->{local_c}{month}, $sep,
548 $self->year );
549 }
550  
551 sub hms
552 165 ▶{
Subroutine hms: called CC=2 SCAR=6.9
Pod documented
553 165 my ( $self, $sep ) = @_;
554 165 ▶ $sep = ':' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 2 163 165
555  
556 return sprintf( "%0.2d%s%0.2d%s%0.2d",
557 $self->{local_c}{hour}, $sep,
558 $self->{local_c}{minute}, $sep,
559 165 $self->{local_c}{second} );
560 }
561  
562 161 ▶sub iso8601 { join 'T', $_[0]->ymd('-'), $_[0]->hms(':') }
Subroutine iso8601: called CC=1 SCAR=0.0
Pod documented
563  
564 2 ▶sub is_leap_year { DateTimeX::Lite::Util::is_leap_year( $_[0]->year ) }
Subroutine is_leap_year: called CC=1 SCAR=0.0
Pod documented
565  
566 33137 ▶sub time_zone { $_[0]->{tz} }
Subroutine time_zone: called CC=1 SCAR=0.0
Pod documented
567  
568  
569 56 ▶sub is_dst { $_[0]->{tz}->is_dst_for_datetime( $_[0] ) }
Subroutine is_dst: called CC=1 SCAR=0.0
Pod documented
570  
571 3 ▶sub time_zone_long_name { $_[0]->{tz}->name }
Subroutine time_zone_long_name: called CC=1 SCAR=0.0
Pod documented
572 8 ▶sub time_zone_short_name { $_[0]->{tz}->short_name_for_datetime( $_[0] ) }
Subroutine time_zone_short_name: called CC=1 SCAR=0.0
Pod documented
573  
574 17083 ▶sub locale { $_[0]->{locale} }
Subroutine locale: called CC=1 SCAR=0.0
Pod documented
575  
576 # This method exists for the benefit of internal methods which create
577 # a new object based on the current object, like set() and truncate().
578 sub _new_from_self
579 17073 ▶{
Subroutine _new_from_self: called CC=2 SCAR=7.0
580 17073 my $self = shift;
581  
582 17073 my %old = map { $_ => $self->$_() }
583 qw( year month day hour minute second nanosecond
584 locale time_zone );
585 17073 ▶ $old{formatter} = $self->formatter()
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->formatter 0 17073 17073
586 if defined $self->formatter();
587  
588 17073 return (ref $self)->new( %old, @_ );
589 }
590  
591 sub set
592 17055 ▶{
Subroutine set: called CC=1 SCAR=0.0
Pod documented
593 17055 my ($self, %p) = @_;
594  
595 17055 my $new_dt = $self->_new_from_self(%p);
596  
597 17045 %$self = %$new_dt;
598  
599 17045 return $self;
600 }
601  
602 1 ▶sub set_year { $_[0]->set( year => $_[1] ) }
Subroutine set_year: called CC=1 SCAR=0.0
Pod documented
603 1 ▶sub set_month { $_[0]->set( month => $_[1] ) }
Subroutine set_month: called CC=1 SCAR=0.0
Pod documented
604 1 ▶sub set_day { $_[0]->set( day => $_[1] ) }
Subroutine set_day: called CC=1 SCAR=0.0
Pod documented
605 1 ▶sub set_hour { $_[0]->set( hour => $_[1] ) }
Subroutine set_hour: called CC=1 SCAR=0.0
Pod documented
606 1 ▶sub set_minute { $_[0]->set( minute => $_[1] ) }
Subroutine set_minute: called CC=1 SCAR=0.0
Pod documented
607 1 ▶sub set_second { $_[0]->set( second => $_[1] ) }
Subroutine set_second: called CC=1 SCAR=0.0
Pod documented
608 1 ▶sub set_nanosecond { $_[0]->set( nanosecond => $_[1] ) }
Subroutine set_nanosecond: called CC=1 SCAR=0.0
Pod documented
609  
610 1 ▶sub set_locale { $_[0]->set( locale => $_[1] ) }
Subroutine set_locale: called CC=1 SCAR=0.0
Pod documented
611  
612 0 ▶sub set_formatter { $_[0]->{formatter} = $_[1] }
Subroutine set_formatter: not called CC=1 SCAR=6.9
Pod documented
613
614  
615 17073 ▶sub formatter { $_[0]->{formatter} }
Subroutine formatter: called CC=1 SCAR=0.0
Pod documented
616  
617 sub from_epoch
618 28 ▶ {
Subroutine from_epoch: called CC=3 SCAR=11.0
Pod documented
619 28 my ($class, %p) = @_;
620  
621 28 my %args;
622  
623 # Because epoch may come from Time::HiRes
624 28 my $fraction = $p{epoch} - int( $p{epoch} );
625 28 ▶ $args{nanosecond} = int( $fraction * MAX_NANOSECONDS )
Branch: 1 branch
BranchTrueFalseTotal
if $fraction 2 26 28
626 if $fraction;
627  
628 # Note, for very large negative values this may give a
629 # blatantly wrong answer.
630 @args{ qw( second minute hour day month year ) } =
631 28 ( gmtime( int delete $p{epoch} ) )[ 0..5 ];
632 28 $args{year} += 1900;
633 28 $args{month}++;
634  
635 28 my $self = $class->new( %p, %args, time_zone => 'UTC' );
636  
637 28 ▶ $self->set_time_zone( $p{time_zone} ) if exists $p{time_zone};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"time_zone"} 6 22 28
638  
639 28 return $self;
640 }
641  
642 sub _utc_ymd
643 16 ▶{
Subroutine _utc_ymd: called CC=2 SCAR=7.0
644 16 my $self = shift;
645  
646 16 ▶ $self->_calc_utc_components unless exists $self->{utc_c}{year};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"utc_c"}{"year"} 16 0 16
647  
648 16 return @{ $self->{utc_c} }{ qw( year month day ) };
649 }
650  
651 sub _utc_hms
652 16 ▶{
Subroutine _utc_hms: called CC=2 SCAR=7.0
653 16 my $self = shift;
654  
655 16 ▶ $self->_calc_utc_components unless exists $self->{utc_c}{hour};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"utc_c"}{"hour"} 0 16 16
656  
657 16 return @{ $self->{utc_c} }{ qw( hour minute second ) };
658 }
659  
660 # use scalar time in case someone's loaded Time::Piece
661 17 ▶sub now { shift->from_epoch( epoch => (scalar CORE::time), @_ ) }
Subroutine now: called CC=1 SCAR=0.0
Pod documented
662  
663 2 ▶sub today { shift->now(@_)->truncate( to => 'day' ) }
Subroutine today: called CC=1 SCAR=0.0
Pod documented
664  
665 56 my %TruncateDefault = (
666 month => 1,
667 day => 1,
668 hour => 0,
669 minute => 0,
670 second => 0,
671 nanosecond => 0,
672 );
673  
674 26 ▶sub truncate {
Subroutine truncate: called CC=6 SCAR=17.9
Pod documented
675 26 my ($self, %p) = @_;
676  
677 26 my %new;
678 26 ▶ if ( $p{to} eq 'week' )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'to'} eq 'week') { } 8 18 26
679 {
680 8 my $day_diff = $self->day_of_week - 1;
681  
682 8 ▶ if ($day_diff)
Branch: 1 branch
BranchTrueFalseTotal
if ($day_diff) 7 1 8
683 {
684 7 $self->add( days => -1 * $day_diff );
685 }
686  
687 8 return $self->truncate( to => 'day' );
688 }
689 else
690 {
691 18 my $truncate;
692 18 foreach my $f ( qw( year month day hour minute second nanosecond ) ) {
693 126 ▶ $new{$f} = $truncate ? $TruncateDefault{$f} : $self->$f();
Branch: 1 branch
BranchTrueFalseTotal
$truncate ? : 70 56 126
694  
695 126 ▶ $truncate = 1 if $p{to} eq $f;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"to"} eq $f 18 108 126
696 }
697 }
698  
699 18 my $new_dt = $self->_new_from_self(%new);
700  
701 18 %$self = %$new_dt;
702  
703 18 return $self;
704 }
705  
706  
707 sub epoch
708 17 ▶{
Subroutine epoch: called CC=2 SCAR=6.9
Pod documented
709 17 my $self = shift;
710  
711 return $self->{utc_c}{epoch}
712 17 ▶ if exists $self->{utc_c}{epoch};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"utc_c"}{"epoch"} 1 16 17
713  
714 16 require Time::Local;
715 16 my ( $year, $month, $day ) = $self->_utc_ymd;
716 16 my @hms = $self->_utc_hms;
717  
718 $self->{utc_c}{epoch} =
719 16 Time::Local::timegm_nocheck( ( reverse @hms ),
720 $day,
721 $month - 1,
722 $year,
723 );
724  
725 16 return $self->{utc_c}{epoch};
726 }
727  
728 sub hires_epoch
729 1 ▶{
Subroutine hires_epoch: called CC=2 SCAR=7.0
Pod documented
730 1 my $self = shift;
731  
732 1 my $epoch = $self->epoch;
733  
734 1 ▶ return undef unless defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 0 1 1
735  
736 1 my $nano = $self->{rd_nanosecs} / MAX_NANOSECONDS;
737  
738 1 return $epoch + $nano;
739 }
740  
741 0 ▶sub is_finite { 1 }
Subroutine is_finite: not called CC=1 SCAR=6.9
Pod documented
742 6954 ▶sub is_infinite { 0 }
Subroutine is_infinite: called CC=1 SCAR=0.0
Pod documented
743  
744 # added for benefit of DateTime::TimeZone
745 19 ▶sub utc_year { $_[0]->{utc_year} }
Subroutine utc_year: called CC=1 SCAR=0.0
Pod undocumented
746  
747  
748 sub leap_seconds
749 3 ▶{
Subroutine leap_seconds: called CC=2 SCAR=6.9
Pod documented
750 3 my $self = shift;
751  
752 3 ▶ return 0 if $self->{tz}->is_floating;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tz"}->is_floating 1 2 3
753  
754 2 return DateTimeX::Lite::LeapSecond::leap_seconds( $self->{utc_rd_days} );
755 }
756  
757 sub _calc_utc_components
758 16 ▶{
Subroutine _calc_utc_components: called CC=2 SCAR=7.0
759 16 my $self = shift;
760  
761 die "Cannot get UTC components before UTC RD has been calculated\n"
762 16 ▶ unless defined $self->{utc_rd_days};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"utc_rd_days"} 0 16 16
763  
764 16 @{ $self->{utc_c} }{ qw( year month day ) } =
765 16 DateTimeX::Lite::Util::rd2ymd( $self->{utc_rd_days} );
766  
767 16 @{ $self->{utc_c} }{ qw( hour minute second ) } =
768 16 DateTimeX::Lite::Util::seconds_as_components( $self->{utc_rd_secs} );
769 }
770  
771 sub compare
772 966 ▶{
Subroutine compare: called CC=1 SCAR=0.0
Pod documented
773 966 shift->_compare( @_, 0 );
774 }
775  
776 sub compare_ignore_floating
777 1 ▶{
Subroutine compare_ignore_floating: called CC=1 SCAR=0.0
Pod documented
778 1 shift->_compare( @_, 1 );
779 }
780  
781 sub _compare
782 967 ▶{
Subroutine _compare: called CC=21 SCAR=30.8
783 967 ▶ my ( $class, $dt1, $dt2, $consistent ) = ref $_[0] ? ( undef, @_ ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 916 51 967
784  
785 967 ▶ return undef unless defined $dt2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt2 0 967 967
786  
787 967 ▶ if ( ! ref $dt2 && ( $dt2 == INFINITY || $dt2 == NEG_INFINITY ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $dt2 and $dt2 == "Inf" || $dt2 == "-Inf") 6 961 967
Condition100%6/6
not ref $dt2 and $dt2 == "Inf" || $dt2 == "-Inf"
!ll&&!rl&&r
96016
$dt2 == "Inf" or $dt2 == "-Inf"
l!l&&r!l&&!r
421
MC/DC100%3/3
not ref $dt2 and $dt2 == "Inf" || $dt2 == "-Inf"
not ref $dt2 $dt2 == "Inf" $dt2 == "-Inf"
Truth table100%4/4
not(A) and B or C
A= not ref $dt2B= $dt2 == "Inf"C= $dt2 == "-Inf"
ABCresult
0XX0
1000
11X1
1011
788 {
789 6 return $dt1->{utc_rd_days} <=> $dt2;
790 }
791  
792 961 ▶ unless ( (blessed $dt1 && $dt1->can( 'utc_rd_values' )) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($dt1) and $dt1->can("utc_rd_values") and (builtin::blessed($dt2) and $dt2->can("utc_rd_values"))) 2 959 961
Condition66%6/9
builtin::blessed($dt1) and $dt1->can("utc_rd_values") and (builtin::blessed($dt2) and $dt2->can("utc_rd_values"))
!ll&&!rl&&r
02959
builtin::blessed($dt1) and $dt1->can("utc_rd_values")
!ll&&!rl&&r
00961
builtin::blessed($dt2) and $dt2->can("utc_rd_values")
!ll&&!rl&&r
11959
MC/DC50%2/4
builtin::blessed($dt1) and $dt1->can("utc_rd_values") and (builtin::blessed($dt2) and $dt2->can("utc_rd_values"))
builtin::blessed($dt1) $dt1->can("utc_rd_values") builtin::blessed($dt2) $dt2->can("utc_rd_values")
Truth table60%3/5
A and B and C and D
A= builtin::blessed($dt1)B= $dt1->can("utc_rd_values")C= builtin::blessed($dt2)D= $dt2->can("utc_rd_values")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
793 (blessed $dt2 && $dt2->can( 'utc_rd_values' ) ))
794 {
795 2 my $dt1_string = overload::StrVal($dt1);
796 2 my $dt2_string = overload::StrVal($dt2);
797  
798 2 Carp::croak( "A DateTimeX::Lite object can only be compared to"
799 . " another DateTimeX::Lite object ($dt1_string, $dt2_string)." );
800 }
801  
802 959 ▶ if ( ! $consistent &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $consistent and (builtin::blessed($dt1) and $dt1->can("time_zone")) and (builtin::blessed($dt2) and $dt2->can("time_zone"))) 956 3 959
Condition66%8/12
not $consistent and (builtin::blessed($dt1) and $dt1->can("time_zone")) and (builtin::blessed($dt2) and $dt2->can("time_zone"))
!ll&&!rl&&r
12956
not $consistent and (builtin::blessed($dt1) and $dt1->can("time_zone"))
!ll&&!rl&&r
10958
builtin::blessed($dt1) and $dt1->can("time_zone")
!ll&&!rl&&r
00958
builtin::blessed($dt2) and $dt2->can("time_zone")
!ll&&!rl&&r
02956
MC/DC40%2/5
not $consistent and (builtin::blessed($dt1) and $dt1->can("time_zone")) and (builtin::blessed($dt2) and $dt2->can("time_zone"))
not $consistent builtin::blessed($dt1) $dt1->can("time_zone") builtin::blessed($dt2) $dt2->can("time_zone")
Truth table50%3/6
not(A) and B and C and D and E
A= not $consistentB= builtin::blessed($dt1)C= $dt1->can("time_zone")D= builtin::blessed($dt2)E= $dt2->can("time_zone")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
803 (blessed $dt1 && $dt1->can( 'time_zone' )) &&
804 (blessed $dt2 && $dt2->can( 'time_zone' ))
805 )
806 {
807 956 my $is_floating1 = $dt1->time_zone->is_floating;
808 956 my $is_floating2 = $dt2->time_zone->is_floating;
809  
810 956 ▶ if ( $is_floating1 && ! $is_floating2 )
Branch: 2 branches
BranchTrueFalseTotal
if ($is_floating1 and not $is_floating2) { } 2 954 956
elsif ($is_floating2 and not $is_floating1) { } 3 1047 1050
Condition100%3/3
$is_floating1 and not $is_floating2
!ll&&!rl&&r
998552
MC/DC100%2/2
$is_floating1 and not $is_floating2
$is_floating1 not $is_floating2
Truth table100%3/3
A and not(B)
A= $is_floating1B= not $is_floating2
ABresult
0X0
100
111
811 {
812 2 $dt1 = $dt1->clone->set_time_zone( $dt2->time_zone );
813 }
814 ▶ elsif ( $is_floating2 && ! $is_floating1 )
Condition100%3/3
$is_floating2 and not $is_floating1
!ll&&!rl&&r
968553
MC/DC100%2/2
$is_floating2 and not $is_floating1
$is_floating2 not $is_floating1
Truth table100%3/3
A and not(B)
A= $is_floating2B= not $is_floating1
ABresult
0X0
100
111
815 {
816 3 $dt2 = $dt2->clone->set_time_zone( $dt1->time_zone );
817 }
818 }
819  
820 959 my @dt1_components = $dt1->utc_rd_values;
821 959 my @dt2_components = $dt2->utc_rd_values;
822  
823 959 foreach my $i ( 0..2 )
824 {
825 1103 ▶ return $dt1_components[$i] <=> $dt2_components[$i]
Branch: 1 branch
BranchTrueFalseTotal
if $dt1_components[$i] != $dt2_components[$i] 902 201 1103
826 if $dt1_components[$i] != $dt2_components[$i]
827 }
828  
829 57 return 0;
830 }
831  
832 sub from_day_of_year
833 1122 ▶{
Subroutine from_day_of_year: called CC=5 SCAR=16.1
Pod documented
834 1122 my ($class, %p) = @_;
835  
836 1122 my $is_leap_year = DateTimeX::Lite::Util::is_leap_year( $p{year} );
837  
838 Carp::croak( "$p{year} is not a leap year.\n" )
839 1122 ▶ if $p{day_of_year} == 366 && ! $is_leap_year;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"day_of_year"} == 366 and not $is_leap_year 1 1121 1122
Condition100%3/3
$p{"day_of_year"} == 366 and not $is_leap_year
!ll&&!rl&&r
111921
MC/DC100%2/2
$p{"day_of_year"} == 366 and not $is_leap_year
$p{"day_of_year"} == 366 not $is_leap_year
Truth table100%3/3
A and not(B)
A= $p{"day_of_year"} == 366B= not $is_leap_year
ABresult
0X0
100
111
840  
841 1121 my $month = 1;
842 1121 my $day = delete $p{day_of_year};
843  
844 1121 ▶ while ( $month <= 12 && $day > DateTimeX::Lite::Util::month_length( $p{year}, $month ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($month <= 12 and $day > DateTimeX::Lite::Util::month_length($p{"year"}, $month)) 6195 1121 7316
Condition66%2/3
$month <= 12 and $day > DateTimeX::Lite::Util::month_length($p{"year"}, $month)
!ll&&!rl&&r
011216195
MC/DC50%1/2
$month <= 12 and $day > DateTimeX::Lite::Util::month_length($p{"year"}, $month)
$month <= 12 $day > DateTimeX::Lite::Util::month_length($p{"year"}, $month)
Truth table66%2/3
A and B
A= $month <= 12B= $day > DateTimeX::Lite::Util::month_length($p{"year"}, $month)
ABresult
0X0
100
111
845 {
846 6195 $day -= DateTimeX::Lite::Util::month_length( $p{year}, $month );
847 6195 $month++;
848 }
849  
850 1121 return $class->new( %p,
851 month => $month,
852 day => $day,
853 );
854 }
855  
856  
857 56 1;
858  
859 __END__
« DateTimeX/Lite/TimeZone.pm ↑ Summary DateTimeX/Lite/Overload.pm »