File Coverage

blib/lib/Ftree/Date/Tiny.pm
Criterion Covered Total %
statement 21 46 45.6
branch 0 2 0.0
condition 0 3 0.0
subroutine 8 20 40.0
pod 8 10 80.0
total 37 81 45.6


line stmt bran cond sub pod time code
1             package Ftree::Date::Tiny;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Ftree::Date::Tiny - A date object, with as little code as possible
8              
9             =head1 SYNOPSIS
10              
11             # Create a date manually
12             $christmas = Ftree::Date::Tiny->new(
13             year => 2006,
14             month => 12,
15             day => 25,
16             );
17            
18             # Show the current date
19             $today = Ftree::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<Ftree::Date::Tiny> is a member of the L<DateTime::Tiny> 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<only> module to implement it completely correct is
55             L<DateTime>. However, to implement it properly L<DateTime> 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<Ftree::Date::Tiny> 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<not> 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<if> you have L<DateTime> installed, any
77             B<Ftree::Date::Tiny> module can be inflated into the equivalent L<DateTime>
78             as needing, loading L<DateTime> on the fly if necesary.
79              
80             For the purposes of date/time logic, all B<Ftree::Date::Tiny> 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<DateTime> 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<DateTime>. Except, of course, that this module implements
95             less of it.
96              
97             =cut
98              
99 1     1   30 use 5.005;
  1         4  
100 1     1   5 use strict;
  1         1  
  1         54  
101 1     1   6 use overload 'bool' => sub () { 1 };
  1         4  
  1         9  
102 1     1   72 use overload '""' => 'as_string';
  1         2  
  1         4  
103 1     1   99 use overload 'eq' => sub { "$_[0]" eq "$_[1]" };
  1     0   2  
  1         6  
  0         0  
104 1     1   74 use overload 'ne' => sub { "$_[0]" ne "$_[1]" };
  1     0   2  
  1         7  
  0         0  
105              
106 1     1   45 use vars qw{$VERSION};
  1         2  
  1         55  
107             BEGIN {
108 1     1   556 $VERSION = '0.01';
109             }
110              
111              
112              
113              
114              
115             #####################################################################
116             # Constructor and Accessors
117              
118             =pod
119              
120             =head2 new
121              
122             my $date = Ftree::Date::Tiny->new(
123             year => 2006,
124             month => 12,
125             day => 31,
126             );
127              
128             The C<new> constructor creates a new B<Ftree::Date::Tiny> object.
129              
130             It takes three named params. C<day> should be the day of the month (1-31),
131             C<month> should be the month of the year (1-12), C<year> as a 4 digit year.
132              
133             These are the only params accepted.
134              
135             Returns a new B<Ftree::Date::Tiny> object.
136              
137             =cut
138             my $format_string = "%d/%m/%Y";
139              
140             sub new {
141 0     0 1   my $class = shift;
142 0           bless { @_ }, $class;
143             }
144              
145             =pod
146              
147             =head2 now
148              
149             my $current_date = Ftree::Date::Tiny->now;
150              
151             The C<now> method creates a new date object for the current date.
152              
153             The date created will be based on localtime, despite the fact that
154             the date is created in the floating time zone.
155              
156             Returns a new B<Ftree::Date::Tiny> object.
157              
158             =cut
159              
160             sub now {
161 0     0 1   my $class = shift;
162 0           my @t = localtime time;
163 0           $class->new(
164             year => $t[5] + 1900,
165             month => $t[4] + 1,
166             day => $t[3],
167             );
168             }
169              
170             =pod
171              
172             =head2 year
173              
174             The C<year> accessor returns the 4-digit year for the date.
175              
176             =cut
177              
178             sub year {
179 0     0 1   $_[0]->{year};
180             }
181              
182             =pod
183              
184             =head2 month
185              
186             The C<month> accessor returns the 1-12 month of the year for the date.
187              
188             =cut
189              
190             sub month {
191 0     0 1   $_[0]->{month};
192             }
193              
194             =pod
195              
196             =head2 day
197              
198             The C<day> accessor returns the 1-31 day of the month for the date.
199              
200             =cut
201              
202             sub day {
203 0     0 1   $_[0]->{day};
204             }
205              
206             =pod
207              
208             =head2 ymd
209              
210             The C<ymd> method returns the most common and accurate stringified date
211             format, which returns in the form "2006-04-12".
212              
213             =cut
214              
215             sub ymd {
216 0     0 1   sprintf( "%04u-%02u-%02u", $_[0]->year, $_[0]->month, $_[0]->day );
217             }
218              
219             sub set_format {
220 0     0 0   $format_string = $_[1];
221             }
222              
223             sub format {
224 0 0 0 0 0   return $_[0]->year
225             if(!defined $_[0]->day && !defined $_[0]->month);
226 0           my $res = $format_string;
227 0           my $day = $_[0]->day;
228 0           my $month = $_[0]->month;
229 0           my $year = $_[0]->year;
230 0           $res =~ s/%d/$day/;
231 0           $res =~ s/%m/$month/;
232 0           $res =~ s/%Y/$year/;
233 0           return $res;
234             }
235              
236              
237             #####################################################################
238             # Type Conversion
239              
240             =pod
241              
242             =head2 as_string
243              
244             The C<as_string> method converts the date to the default string, which
245             at present is the same as that returned by the C<ymd> method above.
246              
247             =cut
248              
249             sub as_string {
250 0     0 1   $_[0]->ymd;
251             }
252              
253             =pod
254              
255             =head2 DateTime
256              
257             The C<DateTime> method is used to inflate the B<Date::Time> object into a
258             full L<DateTime> object.
259              
260             =cut
261              
262             # Convert to "real" DateTime object
263             sub DateTime {
264 0     0 1   require DateTime;
265 0           my $self = shift;
266 0           DateTime->new(
267             day => $self->day,
268             month => $self->month,
269             year => $self->year,
270             locale => 'C',
271             time_zone => 'floating',
272             @_,
273             );
274             }
275              
276             1;
277              
278             =pod
279              
280             =head1 SUPPORT
281              
282             Bugs should be reported via the CPAN bug tracker at
283              
284             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-Tiny>
285              
286             For other issues, or commercial enhancement or support, contact the author.
287              
288             =head1 AUTHOR
289              
290             Adam Kennedy E<lt>cpan@ali.asE<gt>
291              
292             =head1 SEE ALSO
293              
294             L<DateTime>, L<DateTime::Tiny>, L<Time::Tiny>, L<Config::Tiny>, L<ali.as>
295              
296             =head1 COPYRIGHT
297              
298             Copyright 2006 Adam Kennedy.
299              
300             This program is free software; you can redistribute
301             it and/or modify it under the same terms as Perl itself.
302              
303             The full text of the license can be found in the
304             LICENSE file included with this module.
305              
306             =cut