blib/lib/Badger/Date.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 108 CC 11 SCAR 41.9
CC
11
Cov
0%
adjust
110.0
Date
6.0
Today
2.0
CRAP 66.0
1 package Badger::Date;
2  
3 use Badger::Class
4 version => 0.03,
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 };
21  
22 our @YMD = qw( year month day );
23 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 my $item = $_; # lexical copy for closure
33 my $items = $_ . 's'; # provide singular and plural versions
34 my $code = sub {
35 if (@_ > 1) {
36 $_[0]->{ $item } = $_[1];
37 $_[0]->join_uri;
38 return $_[0];
39 }
40 return $_[0]->{ $item };
41 };
42 $item => $code,
43 $items => $code
44 }
45 @YMD
46 );
47  
48  
49 #-----------------------------------------------------------------------
50 # Constructor subroutines
51 #-----------------------------------------------------------------------
52  
53 sub Date {
54 return @_
55 ? DATE->new(@_)
56 : DATE
57 }
58  
59 sub Today {
60 DATE->today;
61 }
62  
63  
64 #-----------------------------------------------------------------------
65 # Methods
66 #-----------------------------------------------------------------------
67  
68 sub today {
69 shift->new;
70 }
71  
72  
73 sub date {
74 shift->text;
75 }
76  
77  
78 sub adjust {
79 my $self = shift;
80 my ($args, $element, $dim);
81 my $fix_month = 0;
82  
83 if (@_ == 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 $args = ref $_[0] eq HASH
87 ? shift
88 : { seconds => $self->duration(shift) };
89 }
90 else {
91 # multiple arguments are named parameters: days => 3, etc.
92 $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 if ((scalar(keys %$args) == 1) &&
100 (defined $args->{ month } || defined $args->{ months } ||
101 defined $args->{ year } || defined $args->{ years })) {
102 $fix_month = 1;
103 }
104  
105 $self->debug("adjust: ", $self->dump_data($args)) if DEBUG;
106  
107 # allow each element to be singular or plural: day/days, etc.
108 foreach $element (@YMD) {
109 $args->{ $element } = $args->{ "${element}s" }
110 unless defined $args->{ $element };
111 }
112  
113 # adjust the time by the parameters specified
114 foreach $element (@YMD) {
115 $self->{ $element } += $args->{ $element }
116 if defined $args->{ $element };
117 }
118  
119 # Handle negative days/months/years
120 while ($self->{ day } <= 0) {
121 $self->{ month }--;
122 unless ($self->{ month } > 0) {
123 $self->{ month } += 12;
124 $self->{ year }--;
125 }
126 $self->{ day } += $self->days_in_month;
127 }
128 while ($self->{ month } <= 0) {
129 $self->{ month } += 12;
130 $self->{ year } --;
131 }
132 while ($self->{ month } > 12) {
133 $self->{ month } -= 12;
134 $self->{ year } ++;
135 }
136  
137 # handle day wrap-around
138 while ($self->{ day } > ($dim = $self->days_in_month)) {
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 if ($fix_month) {
145 $self->{ day } = $dim;
146 }
147 else {
148 $self->{ day } -= $dim;
149 if ($self->{ month } == 12) {
150 $self->{ month } = 1;
151 $self->{ year }++;
152 }
153 else {
154 $self->{ month }++;
155 }
156 }
157 }
158  
159 $self->uncache;
160 $self->join_uri;
161  
162 return $self;
163 }
164  
165  
166 sub uncache {
167 my $self = shift;
168 delete @$self{@CACHE};
169 return $self;
170 }
171  
172  
173 1;
174 __END__