line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Date::Tiny; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Date::Tiny - A date object with as little code as possible |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Create a date manually |
12
|
|
|
|
|
|
|
$christmas = Date::Tiny->new( |
13
|
|
|
|
|
|
|
year => 2006, |
14
|
|
|
|
|
|
|
month => 12, |
15
|
|
|
|
|
|
|
day => 25, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Show the current date |
19
|
|
|
|
|
|
|
$today = Date::Tiny->now; |
20
|
|
|
|
|
|
|
print "Year : " . $today->year . "\n"; |
21
|
|
|
|
|
|
|
print "Month: " . $today->month . "\n"; |
22
|
|
|
|
|
|
|
print "Day : " . $today->day . "\n"; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
B is a member of the L suite of time modules. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
It implements an extremely lightweight object that represents a date, |
29
|
|
|
|
|
|
|
without any time data. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head2 The Tiny Mandate |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Many CPAN modules which provide the best implementation of a concept |
34
|
|
|
|
|
|
|
can be very large. For some reason, this generally seems to be about |
35
|
|
|
|
|
|
|
3 megabyte of ram usage to load the module. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
For a lot of the situations in which these large and comprehensive |
38
|
|
|
|
|
|
|
implementations exist, some people will only need a small fraction of the |
39
|
|
|
|
|
|
|
functionality, or only need this functionality in an ancillary role. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
The aim of the Tiny modules is to implement an alternative to the large |
42
|
|
|
|
|
|
|
module that implements a subset of the functionality, using as little |
43
|
|
|
|
|
|
|
code as possible. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Typically, this means a module that implements between 50% and 80% of |
46
|
|
|
|
|
|
|
the features of the larger module, but using only 100 kilobytes of code, |
47
|
|
|
|
|
|
|
which is about 1/30th of the larger module. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 The Concept of Tiny Date and Time |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Due to the inherent complexity, Date and Time is intrinsically very |
52
|
|
|
|
|
|
|
difficult to implement properly. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
The arguably B module to implement it completely correct is |
55
|
|
|
|
|
|
|
L. However, to implement it properly L is quite slow |
56
|
|
|
|
|
|
|
and requires 3-4 megabytes of memory to load. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
The challenge in implementing a Tiny equivalent to DateTime is to do so |
59
|
|
|
|
|
|
|
without making the functionality critically flawed, and to carefully |
60
|
|
|
|
|
|
|
select the subset of functionality to implement. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
If you look at where the main complexity and cost exists, you will find |
63
|
|
|
|
|
|
|
that it is relatively cheap to represent a date or time as an object, |
64
|
|
|
|
|
|
|
but much much more expensive to modify or convert the object. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
As a result, B provides the functionality required to |
67
|
|
|
|
|
|
|
represent a date as an object, to stringify the date and to parse it |
68
|
|
|
|
|
|
|
back in, but does B allow you to modify the dates. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The purpose of this is to allow for date object representations in |
71
|
|
|
|
|
|
|
situations like log parsing and fast real-time work. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
The problem with this is that having no ability to modify date limits |
74
|
|
|
|
|
|
|
the usefulness greatly. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
To make up for this, B you have L installed, any |
77
|
|
|
|
|
|
|
B module can be inflated into the equivalent L |
78
|
|
|
|
|
|
|
as needing, loading L on the fly if necesary. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
For the purposes of date/time logic, all B objects exist |
81
|
|
|
|
|
|
|
in the "C" locale, and the "floating" time zone (although obviously in a |
82
|
|
|
|
|
|
|
pure date context, the time zone largely doesn't matter). |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
When converting up to full L objects, these local and time |
85
|
|
|
|
|
|
|
zone settings will be applied (although an ability is provided to |
86
|
|
|
|
|
|
|
override this). |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
In addition, the implementation is strictly correct and is intended to |
89
|
|
|
|
|
|
|
be very easily to sub-class for specific purposes of your own. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 METHODS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
In general, the intent is that the API be as close as possible to the |
94
|
|
|
|
|
|
|
API for L. Except, of course, that this module implements |
95
|
|
|
|
|
|
|
less of it. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
2
|
|
|
2
|
|
31901
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
120
|
|
100
|
|
|
|
|
|
|
BEGIN { |
101
|
2
|
|
|
2
|
|
55
|
require 5.004; |
102
|
2
|
|
|
|
|
84
|
$Date::Tiny::VERSION = '1.04'; |
103
|
|
|
|
|
|
|
} |
104
|
2
|
|
|
2
|
|
1767
|
use overload 'bool' => sub () { 1 }; |
|
2
|
|
|
|
|
1233
|
|
|
2
|
|
|
|
|
15
|
|
105
|
2
|
|
|
2
|
|
119
|
use overload '""' => 'as_string'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
8
|
|
106
|
2
|
|
|
2
|
|
162
|
use overload 'eq' => sub { "$_[0]" eq "$_[1]" }; |
|
2
|
|
|
0
|
|
5
|
|
|
2
|
|
|
|
|
19
|
|
|
0
|
|
|
|
|
0
|
|
107
|
2
|
|
|
2
|
|
171
|
use overload 'ne' => sub { "$_[0]" ne "$_[1]" }; |
|
2
|
|
|
0
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
|
0
|
|
|
|
|
0
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
##################################################################### |
114
|
|
|
|
|
|
|
# Constructor and Accessors |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=pod |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 new |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $date = Date::Tiny->new( |
121
|
|
|
|
|
|
|
year => 2006, |
122
|
|
|
|
|
|
|
month => 12, |
123
|
|
|
|
|
|
|
day => 31, |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The C constructor creates a new B object. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
It takes three named params. C should be the day of the month (1-31), |
129
|
|
|
|
|
|
|
C should be the month of the year (1-12), C as a 4 digit year. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
These are the only params accepted. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Returns a new B object. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub new { |
138
|
3
|
|
|
3
|
1
|
14
|
my $class = shift; |
139
|
3
|
|
|
|
|
21
|
bless { @_ }, $class; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=pod |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 now |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my $current_date = Date::Tiny->now; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
The C method creates a new date object for the current date. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
The date created will be based on localtime, despite the fact that |
151
|
|
|
|
|
|
|
the date is created in the floating time zone. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Returns a new B object. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub now { |
158
|
1
|
|
|
1
|
1
|
175
|
my @t = localtime time; |
159
|
|
|
|
|
|
|
shift->new( |
160
|
1
|
|
|
|
|
8
|
year => $t[5] + 1900, |
161
|
|
|
|
|
|
|
month => $t[4] + 1, |
162
|
|
|
|
|
|
|
day => $t[3], |
163
|
|
|
|
|
|
|
); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=pod |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 year |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The C accessor returns the 4-digit year for the date. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub year { |
175
|
3
|
|
|
3
|
1
|
1099
|
$_[0]->{year}; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=pod |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 month |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
The C accessor returns the 1-12 month of the year for the date. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub month { |
187
|
3
|
|
|
3
|
1
|
19
|
$_[0]->{month}; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=pod |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 day |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
The C accessor returns the 1-31 day of the month for the date. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub day { |
199
|
3
|
|
|
3
|
1
|
17
|
$_[0]->{day}; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=pod |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 ymd |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
The C method returns the most common and accurate stringified date |
207
|
|
|
|
|
|
|
format, which returns in the form "2006-04-12". |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub ymd { |
212
|
0
|
|
|
0
|
1
|
0
|
sprintf( "%04u-%02u-%02u", |
213
|
|
|
|
|
|
|
$_[0]->year, |
214
|
|
|
|
|
|
|
$_[0]->month, |
215
|
|
|
|
|
|
|
$_[0]->day, |
216
|
|
|
|
|
|
|
); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
##################################################################### |
224
|
|
|
|
|
|
|
# Type Conversion |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=pod |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head2 as_string |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
The C method converts the date to the default string, which |
231
|
|
|
|
|
|
|
at present is the same as that returned by the C method above. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
This string matches the ISO 8601 standard for the encoding of a date as |
234
|
|
|
|
|
|
|
a string. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=cut |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub as_string { |
239
|
0
|
|
|
0
|
1
|
0
|
$_[0]->ymd; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=pod |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head2 from_string |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
The C method creates a new B object from a string. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
The string is expected to be a "yyyy-mm-dd" ISO 8601 time string. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
my $almost_christmas = Date::Tiny->from_string( '2006-12-23' ); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Returns a new B object, or throws an exception on error. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=cut |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub from_string { |
257
|
1
|
|
|
1
|
1
|
1403
|
my $string = $_[1]; |
258
|
1
|
50
|
33
|
|
|
11
|
unless ( defined $string and ! ref $string ) { |
259
|
0
|
|
|
|
|
0
|
Carp::croak("Did not provide a string to from_string"); |
260
|
|
|
|
|
|
|
} |
261
|
1
|
50
|
|
|
|
10
|
unless ( $string =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/ ) { |
262
|
0
|
|
|
|
|
0
|
Carp::croak("Invalid time format (does not match ISO 8601 yyyy-mm-dd)"); |
263
|
|
|
|
|
|
|
} |
264
|
1
|
|
|
|
|
9
|
$_[0]->new( |
265
|
|
|
|
|
|
|
year => $1 + 0, |
266
|
|
|
|
|
|
|
month => $2 + 0, |
267
|
|
|
|
|
|
|
day => $3 + 0, |
268
|
|
|
|
|
|
|
); |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=pod |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head2 DateTime |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
The C method is used to create a L object |
276
|
|
|
|
|
|
|
that is equivalent to the B object, for use in |
277
|
|
|
|
|
|
|
comversions and caluculations. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
As mentioned earlier, the object will be set to the 'C' locate, |
280
|
|
|
|
|
|
|
and the 'floating' time zone. |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
If installed, the L module will be loaded automatically. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Returns a L object, or throws an exception if L |
285
|
|
|
|
|
|
|
is not installed on the current host. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=cut |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
sub DateTime { |
290
|
0
|
|
|
0
|
1
|
|
require DateTime; |
291
|
0
|
|
|
|
|
|
my $self = shift; |
292
|
0
|
|
|
|
|
|
DateTime->new( |
293
|
|
|
|
|
|
|
day => $self->day, |
294
|
|
|
|
|
|
|
month => $self->month, |
295
|
|
|
|
|
|
|
year => $self->year, |
296
|
|
|
|
|
|
|
locale => 'C', |
297
|
|
|
|
|
|
|
time_zone => 'floating', |
298
|
|
|
|
|
|
|
@_, |
299
|
|
|
|
|
|
|
); |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
1; |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=pod |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=head1 SUPPORT |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
Bugs should be reported via the CPAN bug tracker at |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
L |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
For other issues, or commercial enhancement or support, contact the author. |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=head1 AUTHOR |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
Adam Kennedy Eadamk@cpan.orgE |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head1 SEE ALSO |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
L, L, L, L, L |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head1 COPYRIGHT |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
Copyright 2006 - 2009 Adam Kennedy. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
This program is free software; you can redistribute |
327
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
The full text of the license can be found in the |
330
|
|
|
|
|
|
|
LICENSE file included with this module. |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=cut |