lib/Badger/Date.pm

filter: Statementstmt 64.4% 38 / 59 Branchbran 42.8% 12 / 28 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 8 / 8 Podpod 83.3% 5 / 6 totaltotal 56.7% 67 / 118 CC 21 SCAR 40.7
CC
21
Cov
55%
adjust
85.7
__ANON__
2.7
Date
2.0
CRAP 58.6
« lib/Badger/Filesystem/Universal.pm ↑ Summary lib/Badger/App.pm »
1 package Badger::Date;
2  
3 use Badger::Class
4 2 ▶ version => 0.03,
Subroutine BEGIN: called CC=1 SCAR=0.0
5 debug => 0,
6 import => 'class CLASS',
7 base => 'Badger::Period',
8 utils => 'numlike is_object',
9 accessors => 'uri',
10 constants => 'HASH',
11 constant => {
12 DATE => __PACKAGE__,
13 TYPE_NAME => 'date',
14 FIELD_NAMES => q{year month day},
15 split_regex => qr{(\d{4})\D(\d{1,2})\D(\d{1,2})},
16 join_format => q{%04d-%02d-%02d},
17 },
18 exports => {
19 any => 'DATE Date Today',
20 2 };
21  
22 2 our @YMD = qw( year month day );
23 2 our @CACHE = qw( date time etime longmonth longdate uri );
24  
25  
26 #-----------------------------------------------------------------------
27 # Method generator: year()/years(), month()/months(), day()/days()
28 #-----------------------------------------------------------------------
29  
30 class->methods(
31 map {
32 2 my $item = $_; # lexical copy for closure
33 6 my $items = $_ . 's'; # provide singular and plural versions
34 6 ▶ my $code = sub {
Subroutine __ANON__: called CC=2 SCAR=10.1
35 6 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 6 6
36 0 $_[0]->{ $item } = $_[1];
37 0 $_[0]->join_uri;
38 0 return $_[0];
39 }
40 6 return $_[0]->{ $item };
41 6 };
42 6 $item => $code,
43 $items => $code
44 }
45 @YMD
46 );
47  
48  
49 #-----------------------------------------------------------------------
50 # Constructor subroutines
51 #-----------------------------------------------------------------------
52  
53 2 ▶sub Date {
Subroutine Date: called CC=2 SCAR=6.9
Pod documented
54 return @_
55 2 ▶ ? DATE->new(@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
56 : DATE
57 }
58  
59 2 ▶sub Today {
Subroutine Today: called CC=1 SCAR=0.0
Pod documented
60 2 DATE->today;
61 }
62  
63  
64 #-----------------------------------------------------------------------
65 # Methods
66 #-----------------------------------------------------------------------
67  
68 2 ▶sub today {
Subroutine today: called CC=1 SCAR=0.0
Pod documented
69 2 shift->new;
70 }
71  
72  
73 1 ▶sub date {
Subroutine date: called CC=1 SCAR=0.0
Pod documented
74 1 shift->text;
75 }
76  
77  
78 1 ▶sub adjust {
Subroutine adjust: called CC=19 SCAR=44.5
Pod documented
79 1 my $self = shift;
80 1 my ($args, $element, $dim);
81 1 my $fix_month = 0;
82  
83 1 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 1 1
84 # single argument can be a reference to a hash: { days => 3, etc }
85 # or a number/string representing a duration: "3 days", "1 year"
86 0 ▶ $args = ref $_[0] eq HASH
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
87 ? shift
88 : { seconds => $self->duration(shift) };
89 }
90 else {
91 # multiple arguments are named parameters: days => 3, etc.
92 1 $args = { @_ };
93 }
94  
95 # If we're only adjusting by a month or a year, then we fix the day
96 # within the range of the number of days in the new month. For example:
97 # 2007-01-31 + 1 month = 2007-02-28. We must handle this for a year
98 # adjustment for the case: 2008-02-29 + 1 year = 2009-02-28
99 1 ▶ if ((scalar(keys %$args) == 1) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$args == 1 and defined $args->{"month"} || defined $args->{"months"} || defined $args->{"year"} || defined $args->{"years"}) 0 1 1
Condition33%4/12
scalar keys %$args == 1 and defined $args->{"month"} || defined $args->{"months"} || defined $args->{"year"} || defined $args->{"years"}
!ll&&!rl&&r
010
defined $args->{"month"} or defined $args->{"months"} or defined $args->{"year"} or defined $args->{"years"}
l!l&&r!l&&!r
001
defined $args->{"month"} or defined $args->{"months"} or defined $args->{"year"}
l!l&&r!l&&!r
001
defined $args->{"month"} or defined $args->{"months"}
l!l&&r!l&&!r
001
MC/DC0%0/5
scalar keys %$args == 1 and defined $args->{"month"} || defined $args->{"months"} || defined $args->{"year"} || defined $args->{"years"}
scalar keys %$args == 1 defined $args->{"month"} defined $args->{"months"} defined $args->{"year"} defined $args->{"years"}
Truth table16%1/6
A and B or C or D or E
A= scalar keys %$args == 1B= defined $args->{"month"}C= defined $args->{"months"}D= defined $args->{"year"}E= defined $args->{"years"}
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
100 (defined $args->{ month } || defined $args->{ months } ||
101 defined $args->{ year } || defined $args->{ years })) {
102 0 $fix_month = 1;
103 }
104  
105 1 $self->debug("adjust: ", $self->dump_data($args)) if DEBUG;
106  
107 # allow each element to be singular or plural: day/days, etc.
108 1 foreach $element (@YMD) {
109 $args->{ $element } = $args->{ "${element}s" }
110 3 ▶ unless defined $args->{ $element };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$element} 3 0 3
111 }
112  
113 # adjust the time by the parameters specified
114 1 foreach $element (@YMD) {
115 $self->{ $element } += $args->{ $element }
116 3 ▶ if defined $args->{ $element };
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$element} 1 2 3
117 }
118  
119 # Handle negative days/months/years
120 1 ▶ while ($self->{ day } <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"day"} <= 0) 0 1 1
121 0 $self->{ month }--;
122 0 ▶ unless ($self->{ month } > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"month"} > 0) 0 0 0
123 0 $self->{ month } += 12;
124 0 $self->{ year }--;
125 }
126 0 $self->{ day } += $self->days_in_month;
127 }
128 1 ▶ while ($self->{ month } <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"month"} <= 0) 0 1 1
129 0 $self->{ month } += 12;
130 0 $self->{ year } --;
131 }
132 1 ▶ while ($self->{ month } > 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"month"} > 12) 0 1 1
133 0 $self->{ month } -= 12;
134 0 $self->{ year } ++;
135 }
136  
137 # handle day wrap-around
138 1 ▶ while ($self->{ day } > ($dim = $self->days_in_month)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"day"} > ($dim = $self->days_in_month)) 0 1 1
139 # If we're adjusting by a single month or year and the day is
140 # greater than the number days in the new month, then we adjust
141 # the new day to be the last day in the month. Otherwise we
142 # increment the month and remove the number of days in the current
143 # month.
144 0 ▶ if ($fix_month) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix_month) { } 0 0 0
145 0 $self->{ day } = $dim;
146 }
147 else {
148 0 $self->{ day } -= $dim;
149 0 ▶ if ($self->{ month } == 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'month'} == 12) { } 0 0 0
150 0 $self->{ month } = 1;
151 0 $self->{ year }++;
152 }
153 else {
154 0 $self->{ month }++;
155 }
156 }
157 }
158  
159 1 $self->uncache;
160 1 $self->join_uri;
161  
162 1 return $self;
163 }
164  
165  
166 1 ▶sub uncache {
Subroutine uncache: called CC=1 SCAR=0.0
Pod undocumented
167 1 my $self = shift;
168 1 delete @$self{@CACHE};
169 1 return $self;
170 }
171  
172  
173 2 1;
174 __END__
« lib/Badger/Filesystem/Universal.pm ↑ Summary lib/Badger/App.pm »