lib/Ftree/Date/Tiny.pm

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