File Coverage

blib/lib/Date/Tiny.pm
Criterion Covered Total %
statement 29 40 72.5
branch 2 4 50.0
condition 1 3 33.3
subroutine 12 17 70.5
pod 9 9 100.0
total 53 73 72.6


line stmt bran cond sub pod time code
1 1     1   16369 use strict;
  1         2  
  1         34  
2 1     1   6 use warnings;
  1         2  
  1         76  
3             package Date::Tiny;
4             # ABSTRACT: A date object, with as little code as possible
5              
6             our $VERSION = '1.07';
7              
8 1     1   911 use overload 'bool' => sub () { 1 };
  1         786  
  1         9  
9 1     1   45 use overload '""' => 'as_string';
  1         1  
  1         3  
10 1     1   59 use overload 'eq' => sub { "$_[0]" eq "$_[1]" };
  1     0   1  
  1         5  
  0         0  
11 1     1   48 use overload 'ne' => sub { "$_[0]" ne "$_[1]" };
  1     0   0  
  1         4  
  0         0  
12              
13              
14              
15              
16              
17             #####################################################################
18             # Constructor and Accessors
19              
20             #pod =pod
21             #pod
22             #pod =method new
23             #pod
24             #pod my $date = Date::Tiny->new(
25             #pod year => 2006,
26             #pod month => 12,
27             #pod day => 31,
28             #pod );
29             #pod
30             #pod The C constructor creates a new B object.
31             #pod
32             #pod It takes three named parameters. C should be the day of the month (1-31),
33             #pod C should be the month of the year (1-12), C as a 4 digit year.
34             #pod
35             #pod These are the only parameters accepted.
36             #pod
37             #pod Returns a new B object.
38             #pod
39             #pod =cut
40              
41             sub new {
42 3     3 1 13 my $class = shift;
43 3         12 bless { @_ }, $class;
44             }
45              
46             #pod =pod
47             #pod
48             #pod =method now
49             #pod
50             #pod my $current_date = Date::Tiny->now;
51             #pod
52             #pod The C method creates a new date object for the current date.
53             #pod
54             #pod The date created will be based on localtime, despite the fact that
55             #pod the date is created in the floating time zone.
56             #pod
57             #pod Returns a new B object.
58             #pod
59             #pod =cut
60              
61             sub now {
62 1     1 1 93 my @t = localtime time;
63             shift->new(
64 1         6 year => $t[5] + 1900,
65             month => $t[4] + 1,
66             day => $t[3],
67             );
68             }
69              
70             #pod =pod
71             #pod
72             #pod =method year
73             #pod
74             #pod The C accessor returns the 4-digit year for the date.
75             #pod
76             #pod =cut
77              
78             sub year {
79 3     3 1 1257 $_[0]->{year};
80             }
81              
82             #pod =pod
83             #pod
84             #pod =method month
85             #pod
86             #pod The C accessor returns the 1-12 month of the year for the date.
87             #pod
88             #pod =cut
89              
90             sub month {
91 3     3 1 15 $_[0]->{month};
92             }
93              
94             #pod =pod
95             #pod
96             #pod =method day
97             #pod
98             #pod The C accessor returns the 1-31 day of the month for the date.
99             #pod
100             #pod =cut
101              
102             sub day {
103 3     3 1 15 $_[0]->{day};
104             }
105              
106             #pod =pod
107             #pod
108             #pod =method ymd
109             #pod
110             #pod The C method returns the most common and accurate stringified date
111             #pod format, which returns in the form "2006-04-12".
112             #pod
113             #pod =cut
114              
115             sub ymd {
116 0     0 1 0 sprintf( "%04u-%02u-%02u",
117             $_[0]->year,
118             $_[0]->month,
119             $_[0]->day,
120             );
121             }
122              
123              
124              
125              
126              
127             #####################################################################
128             # Type Conversion
129              
130             #pod =pod
131             #pod
132             #pod =method as_string
133             #pod
134             #pod The C method converts the date to the default string, which
135             #pod at present is the same as that returned by the C method above.
136             #pod
137             #pod This string matches the ISO 8601 standard for the encoding of a date as
138             #pod a string.
139             #pod
140             #pod =cut
141              
142             sub as_string {
143 0     0 1 0 $_[0]->ymd;
144             }
145              
146             #pod =pod
147             #pod
148             #pod =method from_string
149             #pod
150             #pod The C method creates a new B object from a string.
151             #pod
152             #pod The string is expected to be a "yyyy-mm-dd" ISO 8601 time string.
153             #pod
154             #pod my $almost_christmas = Date::Tiny->from_string( '2006-12-23' );
155             #pod
156             #pod Returns a new B object, or throws an exception on error.
157             #pod
158             #pod =cut
159              
160             sub from_string {
161 1     1 1 1915 my $string = $_[1];
162 1 50 33     7 unless ( defined $string and ! ref $string ) {
163 0         0 require Carp;
164 0         0 Carp::croak("Did not provide a string to from_string");
165             }
166 1 50       9 unless ( $string =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/ ) {
167 0         0 require Carp;
168 0         0 Carp::croak("Invalid time format (does not match ISO 8601 yyyy-mm-dd)");
169             }
170 1         8 $_[0]->new(
171             year => $1 + 0,
172             month => $2 + 0,
173             day => $3 + 0,
174             );
175             }
176              
177             #pod =pod
178             #pod
179             #pod =method DateTime
180             #pod
181             #pod The C method is used to create a L object
182             #pod that is equivalent to the B object, for use in
183             #pod conversions and calculations.
184             #pod
185             #pod As mentioned earlier, the object will be set to the 'C' locate,
186             #pod and the 'floating' time zone.
187             #pod
188             #pod If installed, the L module will be loaded automatically.
189             #pod
190             #pod Returns a L object, or throws an exception if L
191             #pod is not installed on the current host.
192             #pod
193             #pod =cut
194              
195             sub DateTime {
196 0     0 1   require DateTime;
197 0           my $self = shift;
198 0           DateTime->new(
199             day => $self->day,
200             month => $self->month,
201             year => $self->year,
202             locale => 'C',
203             time_zone => 'floating',
204             @_,
205             );
206             }
207              
208             1;
209              
210             __END__