File Coverage

blib/lib/DateTime/Tiny.pm
Criterion Covered Total %
statement 34 43 79.0
branch 14 16 87.5
condition 1 3 33.3
subroutine 17 20 85.0
pod 12 12 100.0
total 78 94 82.9


line stmt bran cond sub pod time code
1 1     1   12641 use strict;
  1         2  
  1         22  
2 1     1   3 use warnings;
  1         1  
  1         56  
3             package DateTime::Tiny;
4             # ABSTRACT: A date object, with as little code as possible
5              
6             our $VERSION = '1.05'; # TRIAL
7              
8 1     1   873 use overload 'bool' => sub () { 1 };
  1         692  
  1         7  
9 1     1   44 use overload '""' => 'as_string';
  1         0  
  1         3  
10 1     1   54 use overload 'eq' => sub { "$_[0]" eq "$_[1]" };
  1     0   1  
  1         4  
  0         0  
11 1     1   46 use overload 'ne' => sub { "$_[0]" ne "$_[1]" };
  1     0   1  
  1         4  
  0         0  
12              
13             #####################################################################
14             # Constructor and Accessors
15              
16             #pod =pod
17             #pod
18             #pod =method new
19             #pod
20             #pod my $date = DateTime::Tiny->new(
21             #pod year => 2006,
22             #pod month => 12,
23             #pod day => 31,
24             #pod hour => 10,
25             #pod minute => 45,
26             #pod second => 32,
27             #pod );
28             #pod
29             #pod The C constructor creates a new B object.
30             #pod
31             #pod It takes six named parameters. C should be the day of the month (1-31),
32             #pod C should be the month of the year (1-12), C as a 4 digit year.
33             #pod C should be the hour of the day (0-23), C should be the
34             #pod minute of the hour (0-59) and C should be the second of the
35             #pod minute (0-59).
36             #pod
37             #pod These are the only parameters accepted.
38             #pod
39             #pod Returns a new B object.
40             #pod
41             #pod =cut
42              
43             sub new {
44 5     5 1 1123 my $class = shift;
45 5         21 bless { @_ }, $class;
46             }
47              
48             #pod =pod
49             #pod
50             #pod =method now
51             #pod
52             #pod my $current_date = DateTime::Tiny->now;
53             #pod
54             #pod The C method creates a new date object for the current date.
55             #pod
56             #pod The date created will be based on localtime, despite the fact that
57             #pod the date is created in the floating time zone.
58             #pod
59             #pod Returns a new B object.
60             #pod
61             #pod =cut
62              
63             sub now {
64 1     1 1 95 my @t = localtime time;
65             shift->new(
66 1         9 year => $t[5] + 1900,
67             month => $t[4] + 1,
68             day => $t[3],
69             hour => $t[2],
70             minute => $t[1],
71             second => $t[0],
72             );
73             }
74              
75             #pod =pod
76             #pod
77             #pod =method year
78             #pod
79             #pod The C accessor returns the 4-digit year for the date.
80             #pod
81             #pod =cut
82              
83             sub year {
84 12 100   12 1 615 defined $_[0]->{year} ? $_[0]->{year} : 1970;
85             }
86              
87             #pod =pod
88             #pod
89             #pod =method month
90             #pod
91             #pod The C accessor returns the 1-12 month of the year for the date.
92             #pod
93             #pod =cut
94              
95             sub month {
96 12 100   12 1 42 $_[0]->{month} || 1;
97             }
98              
99             #pod =pod
100             #pod
101             #pod =method day
102             #pod
103             #pod The C accessor returns the 1-31 day of the month for the date.
104             #pod
105             #pod =cut
106              
107             sub day {
108 12 100   12 1 38 $_[0]->{day} || 1;
109             }
110              
111             #pod =pod
112             #pod
113             #pod =method hour
114             #pod
115             #pod The C accessor returns the hour component of the time as
116             #pod an integer from zero to twenty-three (0-23) in line with 24-hour
117             #pod time.
118             #pod
119             #pod =cut
120              
121             sub hour {
122 12 100   12 1 244 $_[0]->{hour} || 0;
123             }
124              
125             #pod =pod
126             #pod
127             #pod =method minute
128             #pod
129             #pod The C accessor returns the minute component of the time
130             #pod as an integer from zero to fifty-nine (0-59).
131             #pod
132             #pod =cut
133              
134             sub minute {
135 12 100   12 1 35 $_[0]->{minute} || 0;
136             }
137              
138             #pod =pod
139             #pod
140             #pod =method second
141             #pod
142             #pod The C accessor returns the second component of the time
143             #pod as an integer from zero to fifty-nine (0-59).
144             #pod
145             #pod =cut
146              
147             sub second {
148 12 100   12 1 80 $_[0]->{second} || 0;
149             }
150              
151             #pod =pod
152             #pod
153             #pod =method ymdhms
154             #pod
155             #pod The C method returns the most common and accurate stringified date
156             #pod format, which returns in the form "2006-04-12T23:59:59".
157             #pod
158             #pod =cut
159              
160             sub ymdhms {
161 10     10 1 14 sprintf( "%04u-%02u-%02uT%02u:%02u:%02u",
162             $_[0]->year,
163             $_[0]->month,
164             $_[0]->day,
165             $_[0]->hour,
166             $_[0]->minute,
167             $_[0]->second,
168             );
169             }
170              
171              
172              
173              
174              
175             #####################################################################
176             # Type Conversion
177              
178             #pod =pod
179             #pod
180             #pod =method from_string
181             #pod
182             #pod The C method creates a new B object from a string.
183             #pod
184             #pod The string is expected to be an ISO 8601 combined date and time, with
185             #pod separators (including the 'T' separator) and no time zone designator. No
186             #pod other ISO 8601 formats are supported.
187             #pod
188             #pod my $almost_midnight = DateTime::Tiny->from_string( '2006-12-20T23:59:59' );
189             #pod
190             #pod Returns a new B object, or throws an exception on error.
191             #pod
192             #pod =cut
193              
194             sub from_string {
195 2     2 1 2 my $string = $_[1];
196 2 50 33     9 unless ( defined $string and ! ref $string ) {
197 0         0 require Carp;
198 0         0 Carp::croak("Did not provide a string to from_string");
199             }
200 2 50       8 unless ( $string =~ /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)$/ ) {
201 0         0 require Carp;
202 0         0 Carp::croak("Invalid time format (does not match ISO 8601)");
203             }
204 2         12 $_[0]->new(
205             year => $1 + 0,
206             month => $2 + 0,
207             day => $3 + 0,
208             hour => $4 + 0,
209             minute => $5 + 0,
210             second => $6 + 0,
211             );
212             }
213              
214             #pod =pod
215             #pod
216             #pod =method as_string
217             #pod
218             #pod The C method converts the date to the default string, which
219             #pod at present is the same as that returned by the C method above.
220             #pod
221             #pod This string conforms to the ISO 8601 standard for the encoding of a combined
222             #pod date and time as a string, without time-zone designator.
223             #pod
224             #pod =cut
225              
226             sub as_string {
227 10     10 1 1236 $_[0]->ymdhms;
228             }
229              
230             #pod =pod
231             #pod
232             #pod =method DateTime
233             #pod
234             #pod The C method is used to create a L object
235             #pod that is equivalent to the B object, for use in
236             #pod conversions and calculations.
237             #pod
238             #pod As mentioned earlier, the object will be set to the 'C' locale,
239             #pod and the 'floating' time zone.
240             #pod
241             #pod If installed, the L module will be loaded automatically.
242             #pod
243             #pod Returns a L object, or throws an exception if L
244             #pod is not installed on the current host.
245             #pod
246             #pod =cut
247              
248             sub DateTime {
249 0     0 1   require DateTime;
250 0           my $self = shift;
251 0           DateTime->new(
252             day => $self->day,
253             month => $self->month,
254             year => $self->year,
255             hour => $self->hour,
256             minute => $self->minute,
257             second => $self->second,
258             locale => 'C',
259             time_zone => 'floating',
260             @_,
261             );
262             }
263              
264             1;
265              
266             __END__