File Coverage

blib/lib/Date/FromToday.pm
Criterion Covered Total %
statement 54 54 100.0
branch 7 8 87.5
condition n/a
subroutine 13 13 100.0
pod 1 1 100.0
total 75 76 98.6


line stmt bran cond sub pod time code
1             package Date::FromToday;
2              
3             our $VERSION = '0.05';
4              
5 1     1   305892 use strict;
  1         2  
  1         44  
6              
7 1     1   2924 use Moose;
  1         444192  
  1         12  
8 1     1   8994 use Moose::Util::TypeConstraints;
  1         16  
  1         10  
9 1     1   2611 use Carp;
  1         2  
  1         84  
10 1     1   6 use Date::Calc qw{ Today Day_of_Week Add_Delta_Days check_date };
  1         3  
  1         74  
11              
12 1     1   1700 use namespace::autoclean;
  1         1817  
  1         8  
13              
14              
15             subtype 'ValidDate'
16             => as 'Str'
17             => where { _check_date( $_ ) }
18             => message { "This date ($_), does not match MM_DD_YYYY!" };
19              
20              
21             has '_calculated_date' => (
22             is => 'ro',
23             isa => 'Str',
24             lazy => 1,
25             builder => '_calculated_date_builder'
26             );
27              
28             has 'day' => (
29             is => 'ro',
30             isa => 'Num',
31             lazy => 1,
32             builder => '_day_builder'
33             );
34              
35             has 'month' => (
36             is => 'ro',
37             isa => 'Str',
38             lazy => 1,
39             builder => '_month_builder'
40             );
41              
42             has 'year' => (
43             is => 'ro',
44             isa => 'Num',
45             lazy => 1,
46             builder => '_year_builder'
47             );
48              
49              
50             has 'move' => (
51             is => 'ro',
52             isa => 'Num',
53             required => 1,
54             );
55              
56             has 'date_string_format' => (
57             is => 'ro',
58             isa => 'Str',
59             default => '{M}_{D}_{Y}',
60             );
61              
62             has 'month_translator' => (
63             is => 'ro',
64             isa => 'ArrayRef[Str]',
65             );
66              
67             has 'year_digits' => (
68             is => 'ro',
69             isa => enum([qw[ 1 2 3 4 ]]),
70             default => '4',
71             );
72              
73             # used to force the date instead of today
74             has 'force_today' => (
75             is => 'ro',
76             isa => 'ValidDate',
77             );
78              
79             has 'leading_zeros' => (
80             is => 'ro',
81             isa => enum([qw[ 0 1 ]]),
82             default => 1,
83             );
84              
85             # check the date we're forcing is a valid date
86             sub _check_date{
87 7     7   16 my ( $date ) = @_;
88              
89 7         36 my @date_elements = split "_", $date;
90              
91 7         43 return check_date( $date_elements[2], $date_elements[0], $date_elements[1]);
92             }
93              
94             # return the $self->force_today date formatted as Today();
95             sub _force_today {
96 7     7   14 my ( $self ) = @_;
97              
98 7         284 my ( $month, $day, $year ) = split "_", $self->force_today;
99              
100 7         32 return ( $year, $month, $day );
101             }
102              
103             # internally calculate the date for later output
104             sub _calculated_date_builder {
105 11     11   22 my ( $self ) = @_;
106              
107             # calculate the date
108 11 100       419 my ( $year, $month, $day ) = defined($self->force_today)? $self->_force_today : Today();
109 11         729 ( $year, $month, $day ) = Add_Delta_Days( $year, $month, $day, $self->move );
110              
111             # if you want leading zeros, do it
112 11 100       1249 ( $month, $day ) = ( sprintf("%02d", $month), sprintf("%02d", $day) ) if $self->leading_zeros;
113              
114             # format the year for how many digits of the year you want
115 11         60 my @year = split "", $year;
116 11         490 foreach ( 0 .. ( 4 - ( ($self->year_digits) + 1 ) ) ) {
117 6         15 shift @year;
118             }
119 11         31 $year = join "", @year;
120              
121             #return "02_11_2011";
122 11         456 return "$month\_$day\_$year";
123             }
124              
125             sub _day_builder {
126 11     11   20 my ( $self ) = @_;
127              
128 11         495 my @date_elements = split "_", $self->_calculated_date;
129              
130 11         540 return $date_elements[1];
131             }
132              
133             sub _month_builder {
134 11     11   26 my ( $self ) = @_;
135              
136 11         436 my @date_elements = split "_", $self->_calculated_date;
137              
138 11 100       467 if ( defined( $self->month_translator )) {
139 3 50       6 if ( defined(${$self->month_translator}[( $date_elements[0] - 1 ) ]) ) {
  3         132  
140              
141 3         5 return ${$self->month_translator}[ ( $date_elements[0]-1 ) ];
  3         111  
142              
143             }
144             }
145              
146 8         295 return $date_elements[0];
147             }
148              
149             sub _year_builder {
150 11     11   41 my ( $self ) = @_;
151              
152 11         418 my @date_elements = split "_", $self->_calculated_date;
153              
154 11         397 return $date_elements[2];
155             }
156              
157             # still to do... work with format atrubute to make it
158             sub date_string {
159 22     22 1 44513 my ( $self ) = @_;
160              
161 22         969 my $date_string = $self->date_string_format;
162 22         797 my ( $month, $day, $year ) = ( $self->month, $self->day, $self->year );
163              
164 22         96 $date_string =~ s/{M}/$month/g;
165 22         74 $date_string =~ s/{D}/$day/g;
166 22         65 $date_string =~ s/{Y}/$year/g;
167              
168 22         188 return $date_string;
169             }
170              
171              
172             =head1 NAME
173              
174             Date::FromToday - Calculate the date in the past or future X days from today
175              
176             =head1 VERSION
177              
178             Version 0.05
179              
180             =head1 SYNOPSIS
181              
182             C<Date::FromToday> is a Perl module for calculating a date in the past or
183             future X number of days from today. It allows for custom formatting of the
184             date string with month/day/year placement, seperators, leading zeros, month
185             translation, forcing today's date, number of digits in the year.
186              
187              
188             use Date::FromToday;
189              
190             my $date = Date::FromToday->new( move => -1 );
191              
192             # prints yesterdays date in MM_DD_YYYY
193             print $date->date_string;
194              
195              
196             -or-
197              
198             my $date = Date::FromToday->new(
199             move => -1,
200             month_translator => [
201             qw(
202             Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec
203             ),
204             ],
205             date_string_format => '{M}.{D}.{Y}',
206             leading_zeros => 0,
207             year_digits => 2,
208             );
209              
210             # prints yesterday's date looking like Jan.1.11
211             print $date->date_string;
212              
213             =head1 CONSTRUCTOR AND STARTUP
214              
215             =head2 new()
216              
217             Creates and returns Date::FromToday object.
218              
219             my $date = Date::FromToday->new( move => -1 );
220              
221             Here are the parms for Date::FromToday
222              
223             =over 4
224              
225             =item * C<< move => $days_to_add_or_subtract >>
226              
227             Adds or subtracts days to the current date. Negative numbers move back
228             in time, positive move into future. Required.
229              
230             =item * C<< date_string_format => {M}_{D}_{Y} >>
231              
232             Decides on how to format the date_string method. M will be replaced by the
233             Month, D with the Day, and Y with the Year.
234             The delimiter is also configureable, M*D^Y = 12*31^2021
235              
236             =item * C<< leading_zeros => [0|1] >>
237              
238             Determines if leading zeros will be added. Default = 1 which means it will be
239             done.
240              
241             =item * C<< month_translator => $month_names_list_ref >>
242              
243             Determines how the month will be displayed:
244             month_translator => [
245             qw(
246             Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec
247             ),
248             ],
249              
250             =item * C<< force_today => MM_DD_YYYY >>
251              
252             You can also force the current date. Must be in MM_DD_YYYY format.
253              
254             =item * C<< year_digits => [1|2|3|4] >>
255              
256             Specifies the number of digits in the year:
257             4 ~ 1895
258             3 ~ 895
259             2 ~ 95
260             1 ~ 5
261              
262             =back
263              
264             =head1 METHODS
265              
266             =head2 day
267              
268             Returns the calculated day, either numeric or translated from
269              
270             =head2 month
271              
272             Returns the calculated month
273              
274             =head2 year
275              
276             Returns the calculated year
277              
278              
279             =head2 date_string
280              
281             Returns the date in a string as specified by the 'date_sting_format' param.
282              
283              
284              
285             =head1 AUTHOR
286              
287             Adam Wohld, C<< <adam at spatialsystems.org> >>
288              
289             =head1 BUGS
290              
291             Please report any bugs or feature requests to C<bug-date-fromtoday at rt.cpan.org>, or through
292             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-FromToday>. I will be notified, and then you'll
293             automatically be notified of progress on your bug as I make changes.
294              
295              
296              
297              
298             =head1 SUPPORT
299              
300             You can find documentation for this module with the perldoc command.
301              
302             perldoc Date::FromToday
303              
304              
305             You can also look for information at:
306              
307             =over 4
308              
309             =item * RT: CPAN's request tracker
310              
311             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Date-FromToday>
312              
313             =item * AnnoCPAN: Annotated CPAN documentation
314              
315             L<http://annocpan.org/dist/Date-FromToday>
316              
317             =item * CPAN Ratings
318              
319             L<http://cpanratings.perl.org/d/Date-FromToday>
320              
321             =item * Search CPAN
322              
323             L<http://search.cpan.org/dist/Date-FromToday/>
324              
325             =back
326              
327             =head1 LICENSE AND COPYRIGHT
328              
329             Copyright 2011 Adam Wohld.
330              
331             This program is free software; you can redistribute it and/or modify it
332             under the terms of either: the GNU General Public License as published
333             by the Free Software Foundation; or the Artistic License.
334              
335             See http://dev.perl.org/licenses/ for more information.
336              
337              
338             =cut
339              
340              
341             1; # end Date::FromToday