line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Badger::Period; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use Badger::Class |
4
|
2
|
|
|
|
|
23
|
version => 0.01, |
5
|
|
|
|
|
|
|
debug => 0, |
6
|
|
|
|
|
|
|
base => 'Badger::Comparable', |
7
|
|
|
|
|
|
|
utils => 'numlike is_object', |
8
|
|
|
|
|
|
|
accessors => 'uri', |
9
|
|
|
|
|
|
|
as_text => 'uri', |
10
|
|
|
|
|
|
|
is_true => 1, |
11
|
|
|
|
|
|
|
constants => 'HASH ARRAY DELIMITER', |
12
|
|
|
|
|
|
|
constant => { |
13
|
|
|
|
|
|
|
FIELD_NAMES => undef, |
14
|
|
|
|
|
|
|
TYPE_NAME => 'period', |
15
|
|
|
|
|
|
|
}, |
16
|
|
|
|
|
|
|
messages => { |
17
|
|
|
|
|
|
|
bad_type => 'Invalid %s: %s', |
18
|
|
|
|
|
|
|
bad_duration => 'Invalid duration: %s', |
19
|
2
|
|
|
2
|
|
13
|
}; |
|
2
|
|
|
|
|
3
|
|
20
|
|
|
|
|
|
|
|
21
|
2
|
|
|
2
|
|
11
|
use Time::Local; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
113
|
|
22
|
2
|
|
|
2
|
|
9
|
use POSIX 'strftime'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
17
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @YMD = qw( year month day ); |
25
|
|
|
|
|
|
|
our @HMS = qw( hour minute second ); |
26
|
|
|
|
|
|
|
our @SMHD = qw( second minute hour day ); |
27
|
|
|
|
|
|
|
our @YMDHMS = (@YMD, @HMS); |
28
|
|
|
|
|
|
|
our $SECONDS = { |
29
|
|
|
|
|
|
|
s => 1, |
30
|
|
|
|
|
|
|
m => 60, |
31
|
|
|
|
|
|
|
h => 60*60, |
32
|
|
|
|
|
|
|
d => 60*60*24, |
33
|
|
|
|
|
|
|
M => 60*60*24*30, |
34
|
|
|
|
|
|
|
y => 60*60*24*365, |
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub split_regex { |
39
|
0
|
|
|
0
|
0
|
0
|
shift->not_implemented; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub join_format { |
44
|
0
|
|
|
0
|
0
|
0
|
shift->not_implemented; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub text_format { |
49
|
1
|
|
|
1
|
0
|
11
|
shift->join_format; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub field_names { |
54
|
28
|
|
|
28
|
0
|
32
|
my $class = shift; |
55
|
28
|
|
50
|
|
|
66
|
my $names = $class->FIELD_NAMES |
56
|
|
|
|
|
|
|
|| return $class->not_implemented; |
57
|
|
|
|
|
|
|
|
58
|
28
|
50
|
|
|
|
203
|
$names = [ split(DELIMITER, $names) ] |
59
|
|
|
|
|
|
|
unless ref $names eq ARRAY; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
return wantarray |
62
|
28
|
50
|
|
|
|
111
|
? @$names |
63
|
|
|
|
|
|
|
: \@$names |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
68
|
|
|
|
|
|
|
# Methods |
69
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub new { |
72
|
5
|
|
|
5
|
1
|
10
|
my $class = shift; |
73
|
5
|
|
|
|
|
13
|
my @fields = $class->field_names; |
74
|
5
|
|
66
|
|
|
11
|
my $self = bless { map { ($_, 0) } @fields }, ref $class || $class; |
|
15
|
|
|
|
|
44
|
|
75
|
5
|
|
|
|
|
10
|
my ($config, $time); |
76
|
|
|
|
|
|
|
|
77
|
5
|
50
|
66
|
|
|
25
|
if (@_ > 1) { |
|
|
100
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# multiple arguments are named params |
79
|
0
|
|
|
|
|
0
|
$config = { @_ }; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
elsif (@_ == 1 && defined $_[0]) { |
82
|
|
|
|
|
|
|
# single argument is a hash of named params, a timestamp or time in |
83
|
|
|
|
|
|
|
# seconds since the epoch |
84
|
3
|
50
|
|
|
|
11
|
$config = ref $_[0] eq HASH ? shift : { time => shift }; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
# otherwise we default to now |
87
|
|
|
|
|
|
|
else { |
88
|
2
|
|
|
|
|
4
|
$config = { time => time() }; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
5
|
50
|
|
|
|
19
|
if ($time = $config->{ time }) { |
92
|
5
|
100
|
66
|
|
|
56
|
if (numlike $time) { |
|
|
50
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# $time is seconds since epoch |
94
|
2
|
|
|
|
|
71
|
(@$config{ @YMDHMS }) = reverse( ( localtime($time) )[0..5] ); |
95
|
2
|
|
|
|
|
12
|
$config->{ year }+= 1900; |
96
|
2
|
|
|
|
|
6
|
$config->{ month }++; |
97
|
2
|
|
|
|
|
5
|
$config->{ etime } = $time; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
elsif (is_object(ref $class || $class, $time)) { |
100
|
0
|
|
|
|
|
0
|
$config->{ uri } = $time->uri; |
101
|
0
|
|
|
|
|
0
|
$config->{ etime } = $time->epoch_time; |
102
|
0
|
|
|
|
|
0
|
$self->split_uri($config->{ uri }, $config); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
else { |
105
|
|
|
|
|
|
|
# $time is a timestamp so split and rejoin into canonical form |
106
|
3
|
|
|
|
|
8
|
$config->{ uri } = $time; |
107
|
3
|
|
|
|
|
15
|
$self->split_uri($config); |
108
|
|
|
|
|
|
|
} |
109
|
4
|
|
|
|
|
17
|
$self->join_uri($config); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# set any fields defined in config, allowing singular (second,month, |
113
|
|
|
|
|
|
|
# etc) and plural (seconds, months, etc) |
114
|
4
|
|
|
|
|
12
|
foreach my $field (@fields) { |
115
|
12
|
|
50
|
|
|
79
|
$self->{ $field } = $config->{ $field } || $config->{"${field}s"} || 0; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
4
|
|
|
|
|
10
|
$self->join_uri; |
119
|
|
|
|
|
|
|
|
120
|
4
|
|
|
|
|
31
|
return $self; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub copy { |
125
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
126
|
1
|
|
|
|
|
2
|
$self->new( $self->{ uri } ); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub split_uri { |
131
|
3
|
|
|
3
|
0
|
7
|
my $self = shift; |
132
|
3
|
|
33
|
|
|
7
|
my $target = shift || $self; |
133
|
3
|
|
|
|
|
10
|
my $regex = $self->split_regex; |
134
|
3
|
|
|
|
|
7
|
my @fields = $self->field_names; |
135
|
|
|
|
|
|
|
|
136
|
3
|
50
|
|
|
|
15
|
$target->{ uri } = '' unless defined $target->{ uri }; |
137
|
|
|
|
|
|
|
|
138
|
6
|
|
|
|
|
20
|
(@$target{ @fields } = map { 0+$_ } $target->{ uri } =~ m/$regex/o) |
139
|
3
|
100
|
|
|
|
36
|
|| return $self->error_msg( bad_type => $self->TYPE_NAME, $target->{ uri } ); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub join_uri { |
144
|
9
|
|
|
9
|
0
|
21
|
my $self = shift; |
145
|
9
|
|
66
|
|
|
29
|
my $target = shift || $self; |
146
|
9
|
|
|
|
|
15
|
my @fields = $self->field_names; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
return ($target->{ uri } = sprintf( |
149
|
|
|
|
|
|
|
$self->join_format, |
150
|
27
|
50
|
|
|
|
67
|
map { defined $_ ? $_ : 0 } |
151
|
9
|
|
|
|
|
24
|
@$target{ @fields } |
152
|
|
|
|
|
|
|
)); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub epoch_time { |
157
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
0
|
|
|
0
|
return $self->{ etime } ||= timelocal( |
160
|
|
|
|
|
|
|
$self->posix_args |
161
|
|
|
|
|
|
|
); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub posix_args { |
166
|
1
|
|
|
1
|
0
|
12
|
my $self = shift; |
167
|
|
|
|
|
|
|
return ( |
168
|
|
|
|
|
|
|
( |
169
|
4
|
100
|
|
|
|
188
|
map { $self->{ $_ } || 0 } |
170
|
|
|
|
|
|
|
@SMHD |
171
|
|
|
|
|
|
|
), |
172
|
|
|
|
|
|
|
($self->{ month } || 1) - 1, |
173
|
1
|
|
50
|
|
|
5
|
($self->{ year } || 1900) - 1900 |
|
|
|
50
|
|
|
|
|
174
|
|
|
|
|
|
|
); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub format { |
179
|
1
|
|
|
1
|
0
|
6
|
my $self = shift; |
180
|
1
|
|
|
|
|
2
|
my $fmt = shift; |
181
|
1
|
|
|
|
|
4
|
return strftime( |
182
|
|
|
|
|
|
|
$fmt, |
183
|
|
|
|
|
|
|
$self->posix_args |
184
|
|
|
|
|
|
|
); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub text { |
189
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
190
|
1
|
|
|
|
|
2
|
my @fields = $self->field_names; |
191
|
|
|
|
|
|
|
return $self->{ text } |
192
|
1
|
|
33
|
|
|
9
|
||= sprintf( $self->text_format, @$self{ @fields } ); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub duration { |
198
|
0
|
|
|
0
|
0
|
0
|
my ($self, $duration) = @_; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# $duration can be a number, assumed to be seconds |
201
|
0
|
0
|
|
|
|
0
|
return $duration |
202
|
|
|
|
|
|
|
if numlike($duration); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# Otherwise the $duration should be of the form "3 minutes". We only |
205
|
|
|
|
|
|
|
# look at the first character of the word (e.g. "3 m"), which creates a |
206
|
|
|
|
|
|
|
# potential conflict between "m(inute) and m(onth)". So we use a capital |
207
|
|
|
|
|
|
|
# 'M' for month. This is based on code by Mark Fisher in CGI.pm. |
208
|
|
|
|
|
|
|
|
209
|
0
|
|
|
|
|
0
|
$duration =~ s/month/Month/i; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# TODO: make this parser a bit smarter so we can support multiple |
212
|
|
|
|
|
|
|
# items (e.g. "2 hours 30 minutes") as per adjust() |
213
|
0
|
0
|
|
|
|
0
|
if ($duration =~ /^ ( -? (?: \d+ | \d*\.\d+ ) ) \s* ([smhdMy]?) /x) { |
214
|
0
|
|
0
|
|
|
0
|
return ($SECONDS->{ $2 } || 1) * $1; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
else { |
217
|
0
|
|
|
|
|
0
|
return $self->error_msg( bad_duration => $duration ); |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub compare { |
223
|
10
|
|
|
10
|
1
|
13
|
my $self = shift; |
224
|
10
|
|
|
|
|
17
|
my @fields = $self->field_names; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# optimisation: if the $self object has an epoch time and a single |
227
|
|
|
|
|
|
|
# numerical argument is passed (also an epoch time) then we can do a |
228
|
|
|
|
|
|
|
# simple comparison |
229
|
|
|
|
|
|
|
return $self->{ etime } <=> $_[0] |
230
|
|
|
|
|
|
|
if $self->{ etime } |
231
|
10
|
0
|
33
|
|
|
27
|
&& @_ == 1 |
|
|
|
33
|
|
|
|
|
232
|
|
|
|
|
|
|
&& numlike $_[0]; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# otherwise we upgrade any argument(s) to another timestamp and comare |
235
|
|
|
|
|
|
|
# them piecewise |
236
|
10
|
50
|
33
|
|
|
42
|
my $comp = @_ && is_object(ref $self || $self, $_[0]) |
237
|
|
|
|
|
|
|
? shift |
238
|
|
|
|
|
|
|
: $self->new(@_); |
239
|
|
|
|
|
|
|
|
240
|
10
|
|
|
|
|
38
|
foreach my $item (@fields) { |
241
|
30
|
100
|
|
|
|
73
|
if ($self->{ $item } < $comp->{ $item }) { |
|
|
100
|
|
|
|
|
|
242
|
5
|
|
|
|
|
29
|
return -1; # -1 - self earlier than comparison timestamp |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
elsif ($self->{ $item } > $comp->{ $item }) { |
245
|
5
|
|
|
|
|
34
|
return 1; # 1 - self later than comparison timestamp |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
} |
248
|
0
|
|
|
|
|
0
|
return 0; # 0 - same time |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub days_in_month { |
253
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
254
|
1
|
|
33
|
|
|
5
|
my $month = shift || $self->{ month }; |
255
|
|
|
|
|
|
|
|
256
|
1
|
50
|
33
|
|
|
15
|
if ($month == 4 || $month == 6 || $month == 9 || $month == 11) { |
|
|
0
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
257
|
1
|
|
|
|
|
7
|
return 30; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
elsif ($month == 2) { |
260
|
0
|
0
|
|
|
|
|
return $self->leap_year(@_) ? 29 : 28; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
else { |
263
|
0
|
|
|
|
|
|
return 31; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
sub leap_year { |
269
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
270
|
0
|
|
0
|
|
|
|
my $year = shift || $self->{ year }; |
271
|
|
|
|
|
|
|
|
272
|
0
|
0
|
|
|
|
|
if ($year % 4) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
273
|
0
|
|
|
|
|
|
return 0; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
elsif ($year % 400 == 0) { |
276
|
0
|
|
|
|
|
|
return 1; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
elsif ($year % 100 == 0) { |
279
|
0
|
|
|
|
|
|
return 0; |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
else { |
282
|
0
|
|
|
|
|
|
return 1; |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
1; |
288
|
|
|
|
|
|
|
__END__ |