blib/lib/DateTime/Incomplete.pm

filter: Statementstmt 84.5% 420 / 497 Branchbran 77.7% 196 / 252 Conditioncond 64.4% 38 / 59 MC/DCmcdc 38.4% 15 / 39 Subroutinesub 75.2% 88 / 117 Podpod 84.0% 37 / 44 totaltotal 78.7% 794 / 1008 CC 172 SCAR 18.4
CC
172
Cov
81%
STORABLE_freeze
42.0
to_recurrence
13.0
last_day_of_month
12.0
CRAP 6.3
↑ Summary
1 package DateTime::Incomplete;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use DateTime::Set 0.0901;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use DateTime::Event::Recurrence;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Params::Validate qw( validate );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 my $UNDEF_CHAR;
12 6 my ( @FIELDS, %FIELD_LENGTH, @TIME_FIELDS, @FIELDS_SORTED );
13  
14 BEGIN
15 6 ▶{
Subroutine BEGIN: called CC=10 SCAR=23.0
16 6 $VERSION = '0.08';
17  
18 6 $UNDEF_CHAR = 'x';
19  
20 6 @FIELDS = ( year => 0, month => 1, day => 1,
21 hour => 0, minute => 0, second => 0, nanosecond => 0 );
22 6 %FIELD_LENGTH = (
23 year => 4, month => 2, day => 2,
24 hour => 2, minute => 2, second => 2, nanosecond => 9,
25 time_zone => 0, locale => 0 );
26 6 @TIME_FIELDS = qw( hour minute second nanosecond );
27  
28 6 @FIELDS_SORTED = qw( year month day
29 hour minute second nanosecond
30 time_zone locale );
31  
32 # Generate named accessors
33  
34 6 for my $field ( @FIELDS_SORTED )
35 {
36 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 54 ▶ *{$field} = sub { $_[0]->_get($field) };
Subroutine __ANON__: called CC=1 SCAR=0.0
38 54 ▶ *{"has_$field"} = sub { $_[0]->_has($field) };
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 54 ▶ next if $field eq 'nanosecond';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "nanosecond" 6 48 54
41  
42 48 my $length = $FIELD_LENGTH{$field};
43  
44 48 ▶ next unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 12 36 48
45  
46 36 ▶ *{"_$field"} = sub { defined $_[0]->$field() ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->$field ? : 122 91 213
47 sprintf( "%0.${length}d", $_[0]->$field() ) :
48 36 $UNDEF_CHAR x $length };
49 }
50  
51 # Generate DateTime read-only functions
52  
53 6 for my $meth ( qw/
54 epoch
55 hires_epoch
56 is_dst
57 utc_rd_values
58 utc_rd_as_seconds
59 / )
60 {
61 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 30 *{$meth} = sub
63 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 # to_datetime() dies if there is no "base"
65 # we get 'undef' if this happens
66 0 eval { (shift)->to_datetime( @_ )->$meth() };
67 30 };
68 }
69  
70 6 for my $meth ( qw/
71 week week_year week_number week_of_month
72 day_name day_abbr
73 day_of_week wday dow
74 day_of_year doy
75 quarter day_of_quarter doq
76 weekday_of_month
77 jd mjd
78 / )
79 {
80 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 102 ▶ *{$meth} = sub { $_[0]->_datetime_method( $meth, 'year', 'month', 'day' ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
82 }
83  
84 6 for my $meth ( qw/
85 is_leap_year ce_year era year_with_era
86 / )
87 {
88 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 24 ▶ *{$meth} = sub { $_[0]->_datetime_method( $meth, 'year' ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
90 }
91  
92 6 for my $meth ( qw/
93 month_name month_abbr
94 / )
95 {
96 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 12 ▶ *{$meth} = sub { $_[0]->_datetime_method( $meth, 'month' ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
98 }
99  
100 6 for my $meth ( qw/
101 hour_1 hour_12 hour_12_0
102 / )
103 {
104 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 18 ▶ *{$meth} = sub { $_[0]->_datetime_method( $meth, 'hour' ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
106 }
107  
108 6 for my $meth ( qw/
109 millisecond microsecond
110 / )
111 {
112 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
113 0 ▶ *{$meth} = sub { $_[0]->_datetime_method( $meth, 'nanosecond' ) };
Subroutine __ANON__: not called CC=1 SCAR=0.4
114 }
115 }
116  
117 6 *_nanosecond = \&_format_nanosecs;
118  
119 6 *mon = \&month;
120 6 *day_of_month = \&day;
121 6 *mday = \&day;
122 6 *min = \&minute;
123 6 *sec = \&second;
124  
125 # Internal sub to call "DateTime" methods
126 sub _datetime_method
127 38 ▶{
Subroutine _datetime_method: called CC=8 SCAR=20.8
128 38 my ( $self, $method ) = ( shift, shift );
129 38 my @fields = @_; # list of required fields
130 38 my $date;
131 38 for ( @fields )
132 {
133 68 ▶ return undef unless ( $self->_has($_) )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has($_) 6 62 68
134 }
135 32 my %param;
136  
137 # if we don't need 'year', then we can safely set it to whatever.
138 32 ▶ $param{year} = 1970 if ! @fields || $fields[0] ne 'year';
Branch: 1 branch
BranchTrueFalseTotal
if not @fields or $fields[0] ne "year" 14 18 32
Condition100%3/3
not @fields or $fields[0] ne "year"
l!l&&r!l&&!r
6818
MC/DC100%2/2
not @fields or $fields[0] ne "year"
not @fields $fields[0] ne "year"
Truth table100%3/3
not(A) or B
A= not @fieldsB= $fields[0] ne "year"
ABresult
1X1
011
000
139  
140 32 ▶ $param{locale} = $self->locale if $self->has_locale;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_locale 29 3 32
141 32 ▶ $param{time_zone} = $self->time_zone if $self->has_time_zone;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_time_zone 30 2 32
142 32 $param{$_} = $self->$_() for @fields;
143 32 $date = DateTime->new( %param );
144
145 32 return $date->$method();
146 }
147  
148 # DATETIME-LIKE METHODS
149  
150 0 ▶sub fractional_second {
Subroutine fractional_second: not called CC=1 SCAR=6.9
Pod undocumented
151 0 $_[0]->_datetime_method( 'fractional_second', 'second', 'nanosecond' );
152 }
153  
154 4 ▶sub offset {
Subroutine offset: called CC=1 SCAR=0.0
Pod undocumented
155 4 $_[0]->_datetime_method( 'offset' );
156 }
157 2 ▶sub time_zone_short_name {
Subroutine time_zone_short_name: called CC=1 SCAR=0.0
Pod undocumented
158 2 $_[0]->_datetime_method( 'time_zone_short_name' );
159 }
160 0 ▶sub time_zone_long_name {
Subroutine time_zone_long_name: not called CC=1 SCAR=6.9
Pod undocumented
161 0 $_[0]->_datetime_method( 'time_zone_long_name' );
162 }
163  
164 sub _from_datetime
165 2 ▶{
Subroutine _from_datetime: called CC=2 SCAR=6.9
166 2 my $class = shift;
167 2 my $dt = shift;
168 2 my %param;
169 2 $param{$_} = $dt->$_() for @FIELDS_SORTED;
170 2 return $class->new( %param );
171 }
172  
173 0 ▶sub last_day_of_month {
Subroutine last_day_of_month: not called CC=3 SCAR=24.8
Pod undocumented
174 0 my $self = shift;
175 0 my %param = @_;
176 0 my $result = $self->_from_datetime( DateTime->last_day_of_month( @_ ) );
177 0 for ( @TIME_FIELDS ) {
178 0 ▶ $result->set( $_, undef ) unless defined $param{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{$_} 0 0 0
179 }
180 0 return $result;
181 }
182  
183 1 ▶sub from_epoch {
Subroutine from_epoch: called CC=1 SCAR=0.0
Pod documented
184 1 return (shift)->_from_datetime( DateTime->from_epoch( @_ ) );
185 }
186 1 ▶sub now {
Subroutine now: called CC=1 SCAR=0.0
Pod documented
187 1 return (shift)->_from_datetime( DateTime->now( @_ ) );
188 }
189 0 ▶sub from_object {
Subroutine from_object: not called CC=1 SCAR=6.9
Pod documented
190 0 return (shift)->_from_datetime( DateTime->from_object( @_ ) );
191 }
192  
193 0 ▶sub from_day_of_year {
Subroutine from_day_of_year: not called CC=3 SCAR=24.8
Pod documented
194 0 my $self = shift;
195 0 my %param = @_;
196 0 my $result = $self->_from_datetime( DateTime->from_day_of_year( @_ ) );
197 0 for ( @TIME_FIELDS ) {
198 0 ▶ $result->set( $_, undef ) unless defined $param{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{$_} 0 0 0
199 }
200 0 return $result;
201 }
202  
203 sub today
204 1 ▶{
Subroutine today: called CC=3 SCAR=11.0
Pod documented
205 1 my $class = shift;
206 1 my $now = DateTime->now( @_ );
207 1 my %param;
208 1 my %fields = ( %FIELD_LENGTH );
209 1 delete $fields{$_} for ( qw/ hour minute second nanosecond / );
210 1 $param{$_} = $now->$_() for ( keys %fields );
211 1 return $class->new( %param );
212 }
213  
214 sub new
215 18 ▶{
Subroutine new: called CC=5 SCAR=16.2
Pod documented
216 # parameter checking is done in "set" method.
217 18 my $class = shift;
218 18 my %param = @_;
219 18 my $base = delete $param{base};
220 18 ▶ die "base must be a datetime" if defined $base &&
Branch: 1 branch
BranchTrueFalseTotal
if defined $base and not UNIVERSAL::can($base, "utc_rd_values") 0 18 18
Condition66%2/3
defined $base and not UNIVERSAL::can($base, "utc_rd_values")
!ll&&!rl&&r
1710
MC/DC0%0/2
defined $base and not UNIVERSAL::can($base, "utc_rd_values")
defined $base not UNIVERSAL::can($base, "utc_rd_values")
Truth table66%2/3
A and not(B)
A= defined $baseB= not UNIVERSAL::can($base, "utc_rd_values")
ABresult
0X0
100
111
221 ! UNIVERSAL::can( $base, 'utc_rd_values' );
222 18 my $self = bless {
223 has => \%param,
224 }, $class;
225 18 $self->set_base( $base );
226 18 ▶ $self->set( locale => $self->{has}{locale} ) if $self->{has}{locale};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has"}{"locale"} 7 11 18
227 18 ▶ $self->set_time_zone( $self->{has}{time_zone} ) if $self->{has}{time_zone};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has"}{"time_zone"} 7 11 18
228 18 return $self;
229 }
230  
231 sub set_base
232 20 ▶{
Subroutine set_base: called CC=5 SCAR=16.3
Pod documented
233 20 my $self = shift;
234 20 $self->{base} = shift;
235 20 ▶ if ( defined $self->{base} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"base"}) 3 17 20
236 {
237 3 my ($key, $value);
238 3 ▶ while (($key, $value) = each %{$self->{has}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"has"};}) 21 3 24
239 21 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 6 15 21
240 15 ▶ if ( $key eq 'time_zone' )
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "time_zone") 0 15 15
241 {
242 0 $self->{base}->set_time_zone( $value );
243 0 next;
244 }
245 15 $self->{base}->set( $key => $value );
246 }
247 }
248 }
249  
250 sub base
251 0 ▶{
Subroutine base: not called CC=2 SCAR=17.9
Pod documented
252 0 ▶ return undef unless defined $_[0]->{base};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"base"} 0 0 0
253 0 $_[0]->{base}->clone;
254 }
255  
256 sub has_base
257 0 ▶{
Subroutine has_base: not called CC=2 SCAR=17.9
Pod documented
258 0 ▶ return defined $_[0]->{base} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'base'} ? : 0 0 0
259 }
260  
261 sub set
262 47 ▶{
Subroutine set: called CC=5 SCAR=16.1
Pod documented
263 47 my $self = shift;
264 47 my %p = @_;
265  
266 47 ▶ while ( my ( $k, $v ) = each %p )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %p) 49 47 96
267 {
268 49 ▶ if ( $k eq 'locale' )
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "locale") 7 42 49
269 {
270 7 $self->_set_locale($v);
271 7 next;
272 }
273  
274 42 ▶ $self->{base}->set( $k => $v ) if $self->{base} && defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"base"} and defined $v 2 40 42
Condition100%3/3
$self->{"base"} and defined $v
!ll&&!rl&&r
30102
MC/DC100%2/2
$self->{"base"} and defined $v
$self->{"base"} defined $v
Truth table100%3/3
A and B
A= $self->{"base"}B= defined $v
ABresult
0X0
100
111
275  
276 42 $self->{has}{ $k } = $v;
277 }
278 }
279  
280 sub _get
281 1850 ▶{
Subroutine _get: called CC=1 SCAR=0.0
282 1850 $_[0]->{has}{$_[1]};
283 }
284  
285 sub _has
286 340 ▶{
Subroutine _has: called CC=2 SCAR=6.9
287 340 ▶ defined $_[0]->{has}{$_[1]} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'has'}{$_[1]} ? : 188 152 340
288 }
289  
290 12 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod documented
291 # returns true or false
292 12 my $self = shift;
293 12 foreach (@_) {
294 12 ▶ return 0 unless $self->_has( $_ )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has($_) 9 3 12
295 }
296 3 return 1
297 }
298  
299 0 ▶sub has_date {
Subroutine has_date: not called CC=3 SCAR=24.8
Pod documented
300 0 ▶ $_[0]->has_year && $_[0]->has_month && $_[0]->has_day
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_year and $_[0]->has_month and $_[0]->has_day 0 0 0
Condition0%0/3
$_[0]->has_year and $_[0]->has_month
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->has_year and $_[0]->has_month
$_[0]->has_year $_[0]->has_month
Truth table0%0/3
A and B
A= $_[0]->has_yearB= $_[0]->has_month
ABresult
0X0
100
111
301 }
302  
303 0 ▶sub has_time {
Subroutine has_time: not called CC=3 SCAR=24.8
Pod documented
304 0 ▶ $_[0]->has_hour && $_[0]->has_minute && $_[0]->has_second
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_hour and $_[0]->has_minute and $_[0]->has_second 0 0 0
Condition0%0/3
$_[0]->has_hour and $_[0]->has_minute
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->has_hour and $_[0]->has_minute
$_[0]->has_hour $_[0]->has_minute
Truth table0%0/3
A and B
A= $_[0]->has_hourB= $_[0]->has_minute
ABresult
0X0
100
111
305 }
306  
307 1 ▶sub defined_fields {
Subroutine defined_fields: called CC=3 SCAR=11.0
Pod documented
308 # no params, returns a list of fields
309 1 my $self = shift;
310 1 my @has = ();
311 1 for ( @FIELDS_SORTED )
312 {
313 9 ▶ push @has, $_ if $self->_has( $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has($_) 5 4 9
314 }
315 1 return @has;
316 }
317  
318 5 ▶sub can_be_datetime {
Subroutine can_be_datetime: called CC=7 SCAR=19.5
Pod documented
319 5 my $self = shift;
320 5 ▶ return 0 if ! $self->has_year;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_year 1 4 5
321 4 my $can = 1;
322 4 for ( qw( month day hour minute second nanosecond ) )
323 {
324 20 ▶ return 0 if ! $can && $self->_has( $_ );
Branch: 1 branch
BranchTrueFalseTotal
if not $can and $self->_has($_) 1 19 20
Condition100%3/3
not $can and $self->_has($_)
!ll&&!rl&&r
1811
MC/DC100%2/2
not $can and $self->_has($_)
not $can $self->_has($_)
Truth table100%3/3
not(A) and B
A= not $canB= $self->_has($_)
ABresult
0X0
100
111
325 19 ▶ $can = 0 if $can && ! $self->_has( $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $can and not $self->_has($_) 3 16 19
Condition100%3/3
$can and not $self->_has($_)
!ll&&!rl&&r
1153
MC/DC100%2/2
$can and not $self->_has($_)
$can not $self->_has($_)
Truth table100%3/3
A and not(B)
A= $canB= not $self->_has($_)
ABresult
0X0
100
111
326 }
327 3 return 1;
328 }
329  
330 #sub become_datetime {
331 # my $self = shift;
332 # return undef unless $self->has_year;
333 # # warn "param = @{[ %{$self->{has}} ]} ";
334 # # return DateTime->new( %{$self->{has}} );
335 # my @parm = map { ( $_, $self->$_() ) } $self->defined_fields;
336 # # warn "param = @parm";
337 # return DateTime->new( @parm );
338 #}
339  
340 sub set_time_zone
341 8 ▶{
Subroutine set_time_zone: called CC=5 SCAR=16.6
Pod documented
342 8 ▶ die "set_time_zone() requires a time_zone value" unless $#_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ == 1 0 8 8
343 8 my $time_zone = $_[1];
344 8 ▶ if ( defined $time_zone )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time_zone) 8 0 8
345 {
346 8 ▶ $time_zone = DateTime::TimeZone->new( name => $time_zone ) unless ref $time_zone;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $time_zone 5 3 8
347 8 ▶ $_[0]->{base}->set_time_zone( $time_zone ) if defined $_[0]->{base};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"base"} 0 8 8
348 }
349 8 $_[0]->{has}{time_zone} = $time_zone;
350 }
351  
352 sub _set_locale
353 7 ▶{
Subroutine _set_locale: called CC=5 SCAR=16.6
354 7 ▶ die "set_locale() requires a locale value" unless $#_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ == 1 0 7 7
355 7 my $locale = $_[1];
356 7 ▶ if ( defined $locale )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $locale) 7 0 7
357 {
358 7 ▶ $locale = DateTime::Locale->load( $locale ) unless ref $locale;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $locale 4 3 7
359 7 ▶ $_[0]->{base}->set( locale => $locale ) if defined $_[0]->{base};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"base"} 0 7 7
360 }
361 7 $_[0]->{has}{locale} = $locale;
362 }
363  
364 sub clone
365 16 ▶{
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
366 16 my $base;
367 16 ▶ $base = $_[0]->{base}->clone if defined $_[0]->{base};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"base"} 6 10 16
368 bless {
369 16 has => { %{ $_[0]->{has} } },
370 base => $base,
371 },
372 ref $_[0];
373 }
374  
375 0 ▶sub is_finite { 1 }
Subroutine is_finite: not called CC=1 SCAR=6.9
Pod documented
376 0 ▶sub is_infinite { 0 }
Subroutine is_infinite: not called CC=1 SCAR=6.9
Pod documented
377  
378  
379 sub truncate
380 1 ▶{
Subroutine truncate: called CC=4 SCAR=13.9
Pod documented
381 1 my $self = shift;
382 1 my %p = validate( @_,
383 { to =>
384 { regex => qr/^(?:year|month|day|hour|minute|second)$/ },
385 },
386 );
387  
388 1 my @fields = @FIELDS;
389 1 my $field;
390 my $value;
391 1 my $set = 0;
392  
393 1 ▶ while ( @fields )
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 7 1 8
394 {
395 7 ( $field, $value ) = ( shift @fields, shift @fields );
396 7 ▶ $self->set( $field => $value ) if $set;
Branch: 1 branch
BranchTrueFalseTotal
if $set 3 4 7
397 7 ▶ $set = 1 if $p{to} eq $field;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"to"} eq $field 1 6 7
398 }
399 1 return $self;
400 }
401  
402  
403 # Stringification methods
404  
405 sub ymd
406 27 ▶{
Subroutine ymd: called CC=1 SCAR=0.0
Pod documented
407 27 my ( $self, $sep ) = ( @_, '-' );
408 27 return $self->_year . $sep. $self->_month . $sep . $self->_day;
409 }
410 6 *date = \&ymd;
411  
412 sub mdy
413 0 ▶{
Subroutine mdy: not called CC=1 SCAR=6.9
Pod documented
414 0 my ( $self, $sep ) = ( @_, '-' );
415 0 return $self->_month . $sep. $self->_day . $sep . $self->_year;
416 }
417  
418 sub dmy
419 0 ▶{
Subroutine dmy: not called CC=1 SCAR=6.9
Pod documented
420 0 my ( $self, $sep ) = ( @_, '-' );
421 0 return $self->_day . $sep. $self->_month . $sep . $self->_year;
422 }
423  
424 sub hms
425 28 ▶{
Subroutine hms: called CC=1 SCAR=0.0
Pod documented
426 28 my ( $self, $sep ) = ( @_, ':' );
427 28 return $self->_hour . $sep. $self->_minute . $sep . $self->_second;
428 }
429 # don't want to override CORE::time()
430 6 *DateTime::Incomplete::time = \&hms;
431  
432 25 ▶sub iso8601 { join 'T', $_[0]->ymd('-'), $_[0]->hms(':') }
Subroutine iso8601: called CC=1 SCAR=0.0
Pod documented
433 6 *datetime = \&iso8601;
434  
435  
436 # "strftime"
437  
438 # Modified from DateTime::strftime %formats; many changes.
439 my %formats =
440 2 ▶ ( 'a' => sub { $_[0]->has_day ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_day ? : 1 1 2
441 $_[0]->day_abbr :
442 $UNDEF_CHAR x 3 },
443 3 ▶ 'A' => sub { $_[0]->has_day ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_day ? : 2 1 3
444 $_[0]->day_name :
445 $UNDEF_CHAR x 5 },
446 4 ▶ 'b' => sub { $_[0]->has_month ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_month ? : 2 2 4
447 $_[0]->month_abbr :
448 $UNDEF_CHAR x 3 },
449 3 ▶ 'B' => sub { $_[0]->has_month ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_month ? : 2 1 3
450 $_[0]->month_name :
451 $UNDEF_CHAR x 5 },
452 1 ▶ 'c' => sub { $_[0]->has_locale ?
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_locale ? : 0 1 1
453 $_[0]->strftime( $_[0]->locale->default_datetime_format ) :
454 $_[0]->datetime },
455 3 ▶ 'C' => sub { $_[0]->has_year ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_year ? : 2 1 3
456 int( $_[0]->year / 100 ) :
457 $UNDEF_CHAR x 2},
458 7 ▶ 'd' => sub { $_[0]->_day },
Subroutine __ANON__: called CC=1 SCAR=0.0
459 3 ▶ 'D' => sub { $_[0]->strftime( '%m/%d/%y' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
460 3 ▶ 'e' => sub { $_[0]->has_month ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_month ? : 2 1 3
461 sprintf( '%2d', $_[0]->day_of_month ) :
462 " $UNDEF_CHAR" },
463 0 ▶ 'F' => sub { $_[0]->ymd('-') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
464 0 ▶ 'g' => sub { substr( $_[0]->week_year, -2 ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
465 0 ▶ 'G' => sub { $_[0]->week_year },
Subroutine __ANON__: not called CC=1 SCAR=6.9
466 9 ▶ 'H' => sub { $_[0]->_hour },
Subroutine __ANON__: called CC=1 SCAR=0.0
467 3 ▶ 'I' => sub { $_[0]->has_hour ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_hour ? : 2 1 3
468 sprintf( '%02d', $_[0]->hour_12 ) :
469 $UNDEF_CHAR x 2 },
470 3 ▶ 'j' => sub { defined $_[0]->day_of_year ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->day_of_year ? : 2 1 3
471 $_[0]->day_of_year :
472 $UNDEF_CHAR x 3 },
473 3 ▶ 'k' => sub { $_[0]->_hour },
Subroutine __ANON__: called CC=1 SCAR=0.0
474 3 ▶ 'l' => sub { $_[0]->has_hour ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_hour ? : 2 1 3
475 sprintf( '%2d', $_[0]->hour_12 ) :
476 " $UNDEF_CHAR" },
477 6 ▶ 'm' => sub { $_[0]->_month },
Subroutine __ANON__: called CC=1 SCAR=0.0
478 9 ▶ 'M' => sub { $_[0]->_minute },
Subroutine __ANON__: called CC=1 SCAR=0.0
479 0 ▶ 'n' => sub { "\n" }, # should this be OS-sensitive?
Subroutine __ANON__: not called CC=1 SCAR=6.9
480 4 ▶ 'N' => sub { (shift)->_format_nanosecs( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
481 0 ▶ 'p' => sub { $_[0]->_format_am_pm },
Subroutine __ANON__: not called CC=1 SCAR=6.9
482 0 ▶ 'P' => sub { lc $_[0]->_format_am_pm },
Subroutine __ANON__: not called CC=1 SCAR=6.9
483 0 ▶ 'r' => sub { $_[0]->strftime( '%I:%M:%S %p' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
484 3 ▶ 'R' => sub { $_[0]->strftime( '%H:%M' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
485 0 ▶ 's' => sub { $_[0]->_format_epoch },
Subroutine __ANON__: not called CC=1 SCAR=6.9
486 6 ▶ 'S' => sub { $_[0]->_second },
Subroutine __ANON__: called CC=1 SCAR=0.0
487 0 ▶ 't' => sub { "\t" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
488 3 ▶ 'T' => sub { $_[0]->strftime( '%H:%M:%S' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
489 0 ▶ 'u' => sub { $_[0]->day_of_week },
Subroutine __ANON__: not called CC=1 SCAR=6.9
490 # algorithm from Date::Format::wkyr
491 3 ▶ 'U' => sub { my $dow = $_[0]->day_of_week;
Subroutine __ANON__: called CC=3 SCAR=11.0
492 3 ▶ return $UNDEF_CHAR x 2 unless defined $dow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dow 1 2 3
493 2 ▶ $dow = 0 if $dow == 7; # convert to 0-6, Sun-Sat
Branch: 1 branch
BranchTrueFalseTotal
if $dow == 7 0 2 2
494 2 my $doy = $_[0]->day_of_year - 1;
495 2 return int( ( $doy - $dow + 13 ) / 7 - 1 )
496 },
497 3 ▶ 'w' => sub { my $dow = $_[0]->day_of_week;
Subroutine __ANON__: called CC=2 SCAR=6.9
498 3 ▶ return $UNDEF_CHAR unless defined $dow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dow 1 2 3
499 2 return $dow % 7;
500 },
501 3 ▶ 'W' => sub { my $dow = $_[0]->day_of_week;
Subroutine __ANON__: called CC=2 SCAR=6.9
502 3 ▶ return $UNDEF_CHAR x 2 unless defined $dow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dow 1 2 3
503 2 my $doy = $_[0]->day_of_year - 1;
504 2 return int( ( $doy - $dow + 13 ) / 7 - 1 )
505 },
506 1 ▶ 'x' => sub { $_[0]->has_locale ?
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_locale ? : 0 1 1
507 $_[0]->strftime( $_[0]->locale->default_date_format ) :
508 $_[0]->date },
509 1 ▶ 'X' => sub { $_[0]->has_locale ?
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_locale ? : 0 1 1
510 $_[0]->strftime( $_[0]->locale->default_time_format ) :
511 $_[0]->time },
512 9 ▶ 'y' => sub { $_[0]->has_year ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_year ? : 6 3 9
513 sprintf( '%02d', substr( $_[0]->year, -2 ) ) :
514 $UNDEF_CHAR x 2 },
515 8 ▶ 'Y' => sub { $_[0]->_year },
Subroutine __ANON__: called CC=1 SCAR=0.0
516 3 ▶ 'z' => sub { defined $_[0]->time_zone ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->time_zone ? : 2 1 3
517 DateTime::TimeZone::offset_as_string( $_[0]->offset ) :
518 $UNDEF_CHAR x 5 },
519 3 ▶ 'Z' => sub { defined $_[0]->time_zone ?
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->time_zone ? : 2 1 3
520 $_[0]->time_zone_short_name :
521 $UNDEF_CHAR x 5 },
522 3 ▶ '%' => sub { '%' },
Subroutine __ANON__: called CC=1 SCAR=0.0
523 6 );
524  
525 6 $formats{h} = $formats{b};
526  
527 0 ▶sub _format_epoch {
Subroutine _format_epoch: not called CC=2 SCAR=17.9
528 0 my $epoch;
529 0 $epoch = $_[0]->epoch;
530 0 ▶ return $UNDEF_CHAR x 6 unless defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 0 0 0
531 0 return $epoch;
532 }
533  
534 0 ▶sub _format_am_pm {
Subroutine _format_am_pm: not called CC=2 SCAR=17.9
535 0 ▶ defined $_[0]->locale ?
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->locale ? : 0 0 0
536 $_[0]->locale->am_pm( $_[0] ) :
537 $UNDEF_CHAR x 2
538 }
539  
540 sub _format_nanosecs
541 4 ▶{
Subroutine _format_nanosecs: called CC=4 SCAR=18.7
542 4 my $self = shift;
543 4 ▶ my $precision = shift || 9;
Condition100%2/2
shift() || 9
l!l
31
MC/DC100%1/1
shift() || 9
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
544  
545 4 ▶ return $UNDEF_CHAR x $precision unless defined $self->nanosecond;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->nanosecond 4 0 4
546  
547 # rd_nanosecs can have a fractional separator
548 0 my ( $ret, $frac ) = split /[.,]/, $self->nanosecond;
549 0 $ret = sprintf "09d" => $ret; # unless length( $ret ) == 9;
550 0 ▶ $ret .= $frac if $frac;
Branch: 1 branch
BranchTrueFalseTotal
if $frac 0 0 0
551  
552 0 return substr( $ret, 0, $precision );
553 }
554  
555 sub strftime
556 108 ▶{
Subroutine strftime: called CC=7 SCAR=19.6
Pod documented
557 108 my $self = shift;
558 # make a copy or caller's scalars get munged
559 108 my @formats = @_;
560  
561 108 my @r;
562 108 foreach my $f (@formats)
563 {
564 109 $f =~ s/
565 %\{(\w+)\}
566 /
567 6 ▶ if ( $self->can($1) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($1)) 6 0 6
568 {
569 6 my $tmp = $self->$1();
570 defined $tmp ?
571 $tmp :
572 ( exists $FIELD_LENGTH{$1} ?
573 6 ▶ $UNDEF_CHAR x $FIELD_LENGTH{$1} :
Branch: 2 branches
BranchTrueFalseTotal
defined $tmp ? : 4 2 6
exists $FIELD_LENGTH{$1} ? : 2 0 2
574 $UNDEF_CHAR x 2 );
575 }
576 /sgex;
577  
578 # regex from Date::Format - thanks Graham!
579 109 $f =~ s/
580 %([%a-zA-Z])
581 /
582 115 ▶ $formats{$1} ? $formats{$1}->($self) : $1
Branch: 1 branch
BranchTrueFalseTotal
$formats{$1} ? : 115 0 115
583 /sgex;
584  
585 # %3N
586 109 $f =~ s/
587 %(\d+)N
588 /
589 3 $formats{N}->($self, $1)
590 /sgex;
591  
592 109 ▶ return $f unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 107 2 109
593  
594 2 push @r, $f;
595 }
596  
597 1 return @r;
598 }
599  
600 # DATETIME::INCOMPLETE METHODS
601  
602  
603 sub is_undef
604 4 ▶{
Subroutine is_undef: called CC=3 SCAR=11.0
Pod documented
605 4 for ( values %{$_[0]->{has}} )
606 {
607 17 ▶ return 0 if defined $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 2 15 17
608 }
609 2 return 1;
610 }
611  
612  
613 sub to_datetime
614 14 ▶{
Subroutine to_datetime: called CC=8 SCAR=21.0
Pod documented
615 14 my $self = shift;
616 14 my %param = @_;
617 $param{base} = $self->{base} if defined $self->{base} &&
618 14 ▶ ! exists $param{base};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"base"} and not exists $param{"base"} 2 12 14
Condition100%3/3
defined $self->{"base"} and not exists $param{"base"}
!ll&&!rl&&r
1112
MC/DC100%2/2
defined $self->{"base"} and not exists $param{"base"}
defined $self->{"base"} not exists $param{"base"}
Truth table100%3/3
A and not(B)
A= defined $self->{"base"}B= not exists $param{"base"}
ABresult
0X0
100
111
619 14 my $result;
620 14 ▶ if ( defined $param{base} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'base'} and UNIVERSAL::can($param{'base'}, 'utc_rd_values')) { } 4 10 14
Condition66%2/3
defined $param{"base"} and UNIVERSAL::can($param{"base"}, "utc_rd_values")
!ll&&!rl&&r
1004
MC/DC50%1/2
defined $param{"base"} and UNIVERSAL::can($param{"base"}, "utc_rd_values")
defined $param{"base"} UNIVERSAL::can($param{"base"}, "utc_rd_values")
Truth table66%2/3
A and B
A= defined $param{"base"}B= UNIVERSAL::can($param{"base"}, "utc_rd_values")
ABresult
0X0
100
111
621 UNIVERSAL::can( $param{base}, 'utc_rd_values' ) )
622 {
623 4 $result = $param{base}->clone;
624 }
625 else
626 {
627 10 $result = DateTime->today;
628 }
629 14 my @params;
630 14 for my $key ( @FIELDS_SORTED )
631 {
632 126 my $value = $self->{has}{$key};
633 126 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 66 60 126
634 60 ▶ if ( $key eq 'time_zone' )
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "time_zone") 0 60 60
635 {
636 0 $result->set_time_zone( $value );
637 0 next;
638 }
639 60 push @params, ( $key => $value );
640 }
641 14 $result->set( @params );
642 14 return $result;
643 }
644  
645 4 ▶sub contains {
Subroutine contains: called CC=9 SCAR=24.4
Pod documented
646 4 my $self = shift;
647 4 my $dt = shift;
648 4 ▶ die "no datetime" unless defined $dt &&
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt and UNIVERSAL::can($dt, "utc_rd_values") 0 4 4
Condition33%1/3
defined $dt and UNIVERSAL::can($dt, "utc_rd_values")
!ll&&!rl&&r
004
MC/DC0%0/2
defined $dt and UNIVERSAL::can($dt, "utc_rd_values")
defined $dt UNIVERSAL::can($dt, "utc_rd_values")
Truth table33%1/3
A and B
A= defined $dtB= UNIVERSAL::can($dt, "utc_rd_values")
ABresult
0X0
100
111
649 UNIVERSAL::can( $dt, 'utc_rd_values' );
650  
651 4 ▶ if ( $self->has_time_zone )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_time_zone) 0 4 4
652 {
653 0 $dt = $dt->clone;
654 0 $dt->set_time_zone( $self->time_zone );
655 }
656  
657 4 my ($key, $value);
658 4 ▶ while (($key, $value) = each %{$self->{has}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"has"};}) 20 2 22
659 20 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 8 12 20
660 12 ▶ if ( $key eq 'time_zone' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "time_zone" or $key eq "locale") 0 12 12
Condition33%1/3
$key eq "time_zone" or $key eq "locale"
l!l&&r!l&&!r
0012
MC/DC0%0/2
$key eq "time_zone" or $key eq "locale"
$key eq "time_zone" $key eq "locale"
Truth table33%1/3
A or B
A= $key eq "time_zone"B= $key eq "locale"
ABresult
1X1
011
000
661 $key eq 'locale' )
662 {
663 # time_zone and locale are ignored.
664 0 next;
665 }
666 12 ▶ return 0 unless $dt->$key() == $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $dt->$key == $value 2 10 12
667 }
668 2 return 1;
669 }
670  
671 # _fix_time_zone
672 # internal method used by next, previous
673 #
674 50 ▶sub _fix_time_zone {
Subroutine _fix_time_zone: called CC=8 SCAR=21.3
675 50 my ($self, $base, $code) = @_;
676 $base = $self->{base} if defined $self->{base} &&
677 50 ▶ ! defined $base;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"base"} and not defined $base 0 50 50
Condition66%2/3
defined $self->{"base"} and not defined $base
!ll&&!rl&&r
36140
MC/DC0%0/2
defined $self->{"base"} and not defined $base
defined $self->{"base"} not defined $base
Truth table66%2/3
A and not(B)
A= defined $self->{"base"}B= not defined $base
ABresult
0X0
100
111
678 50 ▶ die "no base datetime" unless defined $base &&
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base and UNIVERSAL::can($base, "utc_rd_values") 0 50 50
Condition33%1/3
defined $base and UNIVERSAL::can($base, "utc_rd_values")
!ll&&!rl&&r
0050
MC/DC0%0/2
defined $base and UNIVERSAL::can($base, "utc_rd_values")
defined $base UNIVERSAL::can($base, "utc_rd_values")
Truth table33%1/3
A and B
A= defined $baseB= UNIVERSAL::can($base, "utc_rd_values")
ABresult
0X0
100
111
679 UNIVERSAL::can( $base, 'utc_rd_values' );
680 50 my $base_tz = $base->time_zone;
681 50 my $result = $base->clone;
682 50 ▶ $result->set_time_zone( $self->time_zone )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_time_zone 2 48 50
683 if $self->has_time_zone;
684 50 $result = $code->($self, $result);
685 return undef
686 50 ▶ unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 4 46 50
687 46 ▶ $result->set_time_zone( $self->time_zone )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_time_zone 2 44 46
688 if $self->has_time_zone;
689 46 $result->set_time_zone( $base_tz );
690 46 return $result;
691 }
692  
693 sub next
694 28 ▶{
Subroutine next: called CC=1 SCAR=0.0
Pod documented
695 # returns 'next or equal'
696 28 my $self = shift;
697 28 my $base = shift;
698  
699 return $self->_fix_time_zone( $base,
700 28 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
701 28 my ($self, $result) = @_;
702 28 REDO: for (1..10) {
703 # warn "next: self ".$self->datetime." base ".$result->datetime;
704  
705 57 my @fields = @FIELDS;
706 57 my ( $field, $overflow, $bigger_field );
707 57 ▶ while ( @fields )
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 272 26 298
708 {
709 272 ( $field, undef ) = ( shift @fields, shift @fields );
710 272 ▶ if ( defined $self->$field() )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->$field) 160 112 272
711 {
712 160 $overflow = ( $self->$field() < $result->$field() );
713 160 ▶ return undef if $overflow && $field eq $FIELDS[0];
Branch: 1 branch
BranchTrueFalseTotal
if $overflow and $field eq $FIELDS[0] 0 160 160
Condition66%2/3
$overflow and $field eq $FIELDS[0]
!ll&&!rl&&r
140200
MC/DC0%0/2
$overflow and $field eq $FIELDS[0]
$overflow $field eq $FIELDS[0]
Truth table66%2/3
A and B
A= $overflowB= $field eq $FIELDS[0]
ABresult
0X0
100
111
714  
715 160 ▶ if ( $self->$field() != $result->$field() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$field != $result->$field) 65 95 160
716 {
717 65 eval { $result->set( $field => $self->$field() ) };
718 65 ▶ if ( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 12 53 65
719 {
720 12 $result->set( @fields );
721 12 eval { $result->set( $field => $self->$field() ) };
722 12 ▶ if ( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 11 1 12
723 {
724 11 $overflow = 1;
725 }
726 }
727  
728 65 ▶ if ( $overflow )
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow) { } 31 34 65
729 {
730 31 $result->add( $bigger_field . 's' => 1 );
731 31 next REDO;
732 }
733 else
734 {
735 34 $result->set( @fields );
736 }
737 }
738 }
739 241 $bigger_field = $field;
740 }
741 26 return $result;
742 }
743 2 return undef;
744 28 } );
745 }
746  
747 sub previous
748 22 ▶{
Subroutine previous: called CC=1 SCAR=0.0
Pod documented
749 # returns 'previous or equal'
750 22 my $self = shift;
751 22 my $base = shift;
752  
753 return $self->_fix_time_zone( $base,
754 22 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
755 22 my ($self, $result) = @_;
756 # warn "# previous: self ".$self->datetime." base ".$result->datetime." ".$result->time_zone->name;
757  
758 22 my ( $field, $value, $overflow, $bigger_field );
759  
760 22 REDO: for (1..10) {
761 52 my @fields = @FIELDS;
762 52 ▶ while ( @fields )
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 220 20 240
763 {
764 220 ( $field, $value ) = ( shift @fields, shift @fields );
765 220 ▶ if ( defined $self->$field() )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->$field) 135 85 220
766 {
767 135 $overflow = ( $self->$field() > $result->$field() );
768 135 ▶ return undef if $overflow && $field eq $FIELDS[0];
Branch: 1 branch
BranchTrueFalseTotal
if $overflow and $field eq $FIELDS[0] 0 135 135
Condition66%2/3
$overflow and $field eq $FIELDS[0]
!ll&&!rl&&r
103320
MC/DC0%0/2
$overflow and $field eq $FIELDS[0]
$overflow $field eq $FIELDS[0]
Truth table66%2/3
A and B
A= $overflowB= $field eq $FIELDS[0]
ABresult
0X0
100
111
769  
770 135 ▶ if ( $self->$field() != $result->$field() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$field != $result->$field) 111 24 135
771 {
772 111 ▶ if ( $overflow )
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow) 32 79 111
773 {
774 32 $result->set( $field => $value, @fields );
775 32 $result->subtract( nanoseconds => 1 );
776 32 next REDO;
777 }
778 79 my $diff = $result->$field() - $self->$field() ;
779 79 $diff--;
780 79 $result->subtract( $field . 's' => $diff );
781 79 $result->set( @fields );
782 79 $result->subtract( nanoseconds => 1 );
783 79 ▶ if ( $result->$field() != $self->$field() )
Branch: 1 branch
BranchTrueFalseTotal
if ($result->$field != $self->$field) 0 79 79
784 {
785 0 $result->set( @fields );
786 0 $result->subtract( nanoseconds => 1 );
787 }
788 }
789 }
790 188 $bigger_field = $field;
791 }
792 20 return $result;
793 }
794 2 return undef;
795 22 } );
796 }
797  
798 sub closest
799 5 ▶{
Subroutine closest: called CC=8 SCAR=21.6
Pod documented
800 # returns 'closest datetime'
801  
802 5 my $self = shift;
803 5 my $base = shift;
804 $base = $self->{base} if defined $self->{base} &&
805 5 ▶ ! defined $base;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"base"} and not defined $base 0 5 5
Condition66%2/3
defined $self->{"base"} and not defined $base
!ll&&!rl&&r
320
MC/DC0%0/2
defined $self->{"base"} and not defined $base
defined $self->{"base"} not defined $base
Truth table66%2/3
A and not(B)
A= defined $self->{"base"}B= not defined $base
ABresult
0X0
100
111
806 5 ▶ die "no base datetime" unless defined $base &&
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base and UNIVERSAL::can($base, "utc_rd_values") 0 5 5
Condition33%1/3
defined $base and UNIVERSAL::can($base, "utc_rd_values")
!ll&&!rl&&r
005
MC/DC0%0/2
defined $base and UNIVERSAL::can($base, "utc_rd_values")
defined $base UNIVERSAL::can($base, "utc_rd_values")
Truth table33%1/3
A and B
A= defined $baseB= UNIVERSAL::can($base, "utc_rd_values")
ABresult
0X0
100
111
807 UNIVERSAL::can( $base, 'utc_rd_values' );
808  
809 5 my $dt1 = $self->previous( $base );
810 5 my $dt2 = $self->next( $base );
811  
812 5 ▶ return $dt1 unless defined $dt2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt2 1 4 5
813 4 ▶ return $dt2 unless defined $dt1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt1 0 4 4
814  
815 4 my $delta = $base - $dt1;
816 4 ▶ return $dt1 if ( $dt2 - $delta ) >= $base;
Branch: 1 branch
BranchTrueFalseTotal
if $dt2 - $delta >= $base 2 2 4
817 2 return $dt2;
818 }
819  
820 sub start
821 7 ▶{
Subroutine start: called CC=2 SCAR=6.9
Pod documented
822 7 my $self = shift;
823 7 ▶ return undef unless $self->has_year;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_year 3 4 7
824 4 my $dt = $self->to_datetime;
825 4 $dt->subtract( years => 1 );
826 4 return $self->next( $dt );
827 }
828  
829 sub end
830 8 ▶{
Subroutine end: called CC=3 SCAR=11.0
Pod documented
831 8 my $self = shift;
832 8 ▶ return undef unless $self->has_year;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_year 3 5 8
833 5 my $dt = $self->to_datetime;
834 5 $dt->add( years => 1 );
835 5 my $end = $self->previous( $dt );
836 5 ▶ $end->add( nanoseconds => 1 ) unless $self->has_nanosecond;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_nanosecond 3 2 5
837 5 return $end;
838 }
839  
840 sub to_span
841 5 ▶{
Subroutine to_span: called CC=6 SCAR=18.0
Pod documented
842 5 my $self = shift;
843 5 my $start = $self->start;
844 5 my $end = $self->end;
845  
846 5 ▶ return DateTime::Set->empty_set->complement->span
Branch: 1 branch
BranchTrueFalseTotal
unless $start or $end 3 2 5
Condition66%2/3
$start or $end
l!l&&r!l&&!r
203
MC/DC50%1/2
$start or $end
$start $end
Truth table66%2/3
A or B
A= $startB= $end
ABresult
1X1
011
000
847 if ! $start && ! $end;
848  
849 2 my @start;
850 2 ▶ @start = ( 'start', $start ) if $start;
Branch: 1 branch
BranchTrueFalseTotal
if $start 2 0 2
851  
852 2 my @end;
853 2 ▶ if ( $end )
Branch: 1 branch
BranchTrueFalseTotal
if ($end) 2 0 2
854 {
855 2 ▶ if ( $self->has_nanosecond )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_nanosecond) { } 1 1 2
856 {
857 1 @end = ( 'end', $end );
858 }
859 else
860 {
861 1 @end = ( 'before', $end );
862 }
863 }
864  
865 2 return DateTime::Span->from_datetimes( @start, @end );
866 }
867  
868 sub to_recurrence
869 10 ▶{
Subroutine to_recurrence: called CC=13 SCAR=25.6
Pod documented
870 10 my $self = shift;
871 10 my %param;
872  
873 10 my $freq = '';
874 10 my $year;
875 10 for ( qw( second minute hour day month year ) )
876 {
877 60 my $by = $_ . 's'; # months, hours
878 60 ▶ if ( exists $self->{has}{$_} && defined $self->{has}{$_} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"has"}{$_} and defined $self->{"has"}{$_}) 43 17 60
Condition100%3/3
exists $self->{"has"}{$_} and defined $self->{"has"}{$_}
!ll&&!rl&&r
61143
MC/DC100%2/2
exists $self->{"has"}{$_} and defined $self->{"has"}{$_}
exists $self->{"has"}{$_} defined $self->{"has"}{$_}
Truth table100%3/3
A and B
A= exists $self->{"has"}{$_}B= defined $self->{"has"}{$_}
ABresult
0X0
100
111
879 {
880 43 ▶ if ( $_ eq 'year' )
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "year") 5 38 43
881 {
882 5 $year = $self->$_();
883 5 next;
884 }
885 38 $param{$by} = [ $self->$_() ];
886 38 next;
887 }
888 17 ▶ $freq = $_ unless $freq;
Branch: 1 branch
BranchTrueFalseTotal
unless $freq 8 9 17
889 # TODO: use a hash
890 17 ▶ $param{$by} = [ 1 .. 12 ] if $_ eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "month" 1 16 17
891 17 ▶ $param{$by} = [ 1 .. 31 ] if $_ eq 'day';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "day" 4 13 17
892 17 ▶ $param{$by} = [ 0 .. 23 ] if $_ eq 'hour';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "hour" 1 16 17
893 17 ▶ $param{$by} = [ 0 .. 59 ] if $_ eq 'minute';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "minute" 5 12 17
894 17 ▶ $param{$by} = [ 0 .. 59 ] if $_ eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "second" 1 16 17
895 }
896 10 ▶ if ( $freq eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if ($freq eq "") 2 8 10
897 {
898 # it is a single date
899 2 my $dt = DateTime->new( %{$self->{has}} );
900 2 return DateTime::Set->from_datetimes( dates => [ $dt ] );
901 }
902  
903 # for ( keys %param ) { print STDERR " param $_ = @{$param{$_}} \n"; }
904  
905 8 my $r = DateTime::Event::Recurrence->yearly( %param );
906 8 ▶ if ( defined $year ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $year) 3 5 8
907 3 my $span = DateTime::Span->from_datetimes(
908 start => DateTime->new( year => $year ),
909 before => DateTime->new( year => $year + 1 ) );
910 3 $r = $r->intersection( $span );
911 }
912 8 return $r;
913 }
914  
915 sub to_spanset
916 3 ▶{
Subroutine to_spanset: called CC=4 SCAR=13.9
Pod documented
917 3 my $self = shift;
918 3 my @reset;
919 3 for ( qw( second minute hour day month year ) )
920 {
921 10 ▶ if ( $self->has( $_ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has($_)) 2 8 10
922 {
923 2 my %fields = @FIELDS;
924 2 @reset = map { $_ => $fields{$_} } @reset;
925 2 my $dti = $self->clone;
926 2 ▶ $dti->set( @reset ) if @reset;
Branch: 1 branch
BranchTrueFalseTotal
if @reset 1 1 2
927  
928 2 return DateTime::SpanSet->from_set_and_duration (
929 set => $dti->to_recurrence,
930 $_ . 's' => 1,
931 );
932 }
933 8 push @reset, $_;
934 }
935 1 return $self->to_span;
936 }
937  
938 sub STORABLE_freeze
939 0 ▶{
Subroutine STORABLE_freeze: not called CC=6 SCAR=37.4
Pod undocumented
940 0 my ( $self, $cloning ) = @_;
941 0 ▶ return if $cloning;
Branch: 1 branch
BranchTrueFalseTotal
if $cloning 0 0 0
942  
943 0 my @data;
944 0 for my $key ( @FIELDS_SORTED )
945 {
946 0 ▶ next unless defined $self->{has}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"has"}{$key} 0 0 0
947  
948 0 ▶ if ( $key eq 'locale' )
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'locale') { } 0 0 0
elsif ($key eq 'time_zone') { } 0 0 0
949 {
950 0 push @data, "locale:" . $self->{has}{locale}->id;
951 }
952 elsif ( $key eq 'time_zone' )
953 {
954 0 push @data, "tz:" . $self->{has}{time_zone}->name;
955 }
956 else
957 {
958 0 push @data, "$key:" . $self->{has}{$key};
959 }
960 }
961 0 return join( '|', @data ), [$self->base];
962 }
963  
964 sub STORABLE_thaw
965 0 ▶{
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
966 0 my ( $self, $cloning, $data, $base ) = @_;
967 0 my %data = map { split /:/ } split /\|/, $data;
968 0 my $locale = delete $data{locale};
969 0 my $tz = delete $data{tz};
970 0 $self->{has} = \%data;
971 0 $self->set_time_zone( $tz );
972 0 $self->set( locale => $locale );
973 0 $self->{base} = $base->[0];
974 0 return $self;
975 }
976  
977 6 1;
978  
979 __END__
↑ Summary