File Coverage

blib/lib/DateTime/Tiny.pm
Criterion Covered Total %
statement 40 44 90.9
branch 15 16 93.7
condition 1 3 33.3
subroutine 18 20 90.0
pod 12 12 100.0
total 86 95 90.5


line stmt bran cond sub pod time code
1 1     1   98857 use strict;
  1         9  
  1         24  
2 1     1   4 use warnings;
  1         2  
  1         54  
3             package DateTime::Tiny;
4             # ABSTRACT: A date object, with as little code as possible
5              
6             our $VERSION = '1.07';
7              
8 1     1   934 use overload 'bool' => sub () { 1 };
  1         766  
  1         5  
9 1     1   52 use overload '""' => 'as_string';
  1         1  
  1         3  
10 1     1   77 use overload 'eq' => sub { "$_[0]" eq "$_[1]" };
  1     0   2  
  1         4  
  0         0  
11 1     1   67 use overload 'ne' => sub { "$_[0]" ne "$_[1]" };
  1     0   3  
  1         5  
  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<new> constructor creates a new B<DateTime::Tiny> object.
30             #pod
31             #pod It takes six named parameters. C<day> should be the day of the month (1-31),
32             #pod C<month> should be the month of the year (1-12), C<year> as a 4 digit year.
33             #pod C<hour> should be the hour of the day (0-23), C<minute> should be the
34             #pod minute of the hour (0-59) and C<second> 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<DateTime::Tiny> object.
40             #pod
41             #pod =cut
42              
43             sub new {
44 6     6 1 408812 my $class = shift;
45 6         34 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<now> 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<DateTime::Tiny> object.
60             #pod
61             #pod =cut
62              
63             sub now {
64 1     1 1 35 my @t = localtime time;
65             shift->new(
66 1         8 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<year> accessor returns the 4-digit year for the date.
80             #pod
81             #pod =cut
82              
83             sub year {
84 14 100   14 1 4127 defined $_[0]->{year} ? $_[0]->{year} : 1970;
85             }
86              
87             #pod =pod
88             #pod
89             #pod =method month
90             #pod
91             #pod The C<month> accessor returns the 1-12 month of the year for the date.
92             #pod
93             #pod =cut
94              
95             sub month {
96 14 100   14 1 489 $_[0]->{month} || 1;
97             }
98              
99             #pod =pod
100             #pod
101             #pod =method day
102             #pod
103             #pod The C<day> accessor returns the 1-31 day of the month for the date.
104             #pod
105             #pod =cut
106              
107             sub day {
108 14 100   14 1 494 $_[0]->{day} || 1;
109             }
110              
111             #pod =pod
112             #pod
113             #pod =method hour
114             #pod
115             #pod The C<hour> 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 14 100   14 1 781 $_[0]->{hour} || 0;
123             }
124              
125             #pod =pod
126             #pod
127             #pod =method minute
128             #pod
129             #pod The C<minute> 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 14 100   14 1 477 $_[0]->{minute} || 0;
136             }
137              
138             #pod =pod
139             #pod
140             #pod =method second
141             #pod
142             #pod The C<second> 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 14 100   14 1 512 $_[0]->{second} || 0;
149             }
150              
151             #pod =pod
152             #pod
153             #pod =method ymdhms
154             #pod
155             #pod The C<ymdhms> 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 21 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<from_string> method creates a new B<DateTime::Tiny> 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<DateTime::Tiny> object, or throws an exception on error.
191             #pod
192             #pod =cut
193              
194             sub from_string {
195 3     3 1 446 my $string = $_[1];
196 3 50 33     15 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 3         5     my $d = '[0-9]'; # backwards-compatible way of not matching anything but ASCII digits
201 3 100       49 unless ( $string =~ /^($d$d$d$d)-($d$d)-($d$d)T($d$d):($d$d):($d$d)$/ ) {
202 1         6 require Carp;
203 1         174 Carp::croak("Invalid time format (does not match ISO 8601)");
204             }
205 2         24 $_[0]->new(
206             year   => $1 + 0,
207             month  => $2 + 0,
208             day    => $3 + 0,
209             hour   => $4 + 0,
210             minute => $5 + 0,
211             second => $6 + 0,
212             );
213             }
214              
215             #pod =pod
216             #pod
217             #pod =method as_string
218             #pod
219             #pod The C<as_string> method converts the date to the default string, which
220             #pod at present is the same as that returned by the C<ymdhms> method above.
221             #pod
222             #pod This string conforms to the ISO 8601 standard for the encoding of a combined
223             #pod date and time as a string, without time-zone designator.
224             #pod
225             #pod =cut
226              
227             sub as_string {
228 10     10 1 317 $_[0]->ymdhms;
229             }
230              
231             #pod =pod
232             #pod
233             #pod =method DateTime
234             #pod
235             #pod The C<DateTime> method is used to create a L<DateTime> object
236             #pod that is equivalent to the B<DateTime::Tiny> object, for use in
237             #pod conversions and calculations.
238             #pod
239             #pod As mentioned earlier, the object will be set to the 'C' locale,
240             #pod and the 'floating' time zone.
241             #pod
242             #pod If installed, the L<DateTime> module will be loaded automatically.
243             #pod
244             #pod Returns a L<DateTime> object, or throws an exception if L<DateTime>
245             #pod is not installed on the current host.
246             #pod
247             #pod =cut
248              
249             sub DateTime {
250 1     1 1 488 require DateTime;
251 1         3 my $self = shift;
252 1         4 DateTime->new(
253             day       => $self->day,
254             month     => $self->month,
255             year      => $self->year,
256             hour      => $self->hour,
257             minute    => $self->minute,
258             second    => $self->second,
259             locale    => 'C',
260             time_zone => 'floating',
261             @_,
262             );
263             }
264              
265             1;
266              
267             __END__
268            
269             =pod
270            
271             =encoding UTF-8
272            
273             =head1 NAME
274            
275             DateTime::Tiny - A date object, with as little code as possible
276            
277             =head1 VERSION
278            
279             version 1.07
280            
281             =head1 SYNOPSIS
282            
283             # Create a date manually
284             $christmas = DateTime::Tiny->new(
285             year => 2006,
286             month => 12,
287             day => 25,
288             hour => 10,
289             minute => 45,
290             second => 0,
291             );
292            
293             # Show the current date
294             my $now = DateTime::Tiny->now;
295             print "Year : " . $now->year . "\n";
296             print "Month : " . $now->month . "\n";
297             print "Day : " . $now->day . "\n";
298             print "Hour : " . $now->hour . "\n";
299             print "Minute : " . $now->minute . "\n";
300             print "Second : " . $now->second . "\n";
301            
302             =head1 DESCRIPTION
303            
304             B<DateTime::Tiny> is a most prominent member of the L<DateTime::Tiny>
305             suite of time modules.
306            
307             It implements an extremely lightweight object that represents a datetime.
308            
309             =head2 The Tiny Mandate
310            
311             Many CPAN modules which provide the best implementation of a certain
312             concepts are very large. For some reason, this generally seems to be
313             about 3 megabyte of ram usage to load the module.
314            
315             For a lot of the situations in which these large and comprehensive
316             implementations exist, some people will only need a small fraction of the
317             functionality, or only need this functionality in an ancillary role.
318            
319             The aim of the Tiny modules is to implement an alternative to the large
320             module that implements a useful subset of their functionality, using as
321             little code as possible.
322            
323             Typically, this means a module that implements between 50% and 80% of
324             the features of the larger module (although this is just a guideline),
325             but using only 100 kilobytes of code, which is about 1/30th of the larger
326             module.
327            
328             =head2 The Concept of Tiny Date and Time
329            
330             Due to the inherent complexity, Date and Time is intrinsically very
331             difficult to implement properly.
332            
333             The arguably B<only> module to implement it completely correct is
334             L<DateTime>. However, to implement it properly L<DateTime> is quite slow
335             and requires 3-4 megabytes of memory to load.
336            
337             The challenge in implementing a Tiny equivalent to DateTime is to do so
338             without making the functionality critically flawed, and to carefully
339             select the subset of functionality to implement.
340            
341             If you look at where the main complexity and cost exists, you will find
342             that it is relatively cheap to represent a date or time as an object,
343             but much much more expensive to modify, manipulate or convert the object.
344            
345             As a result, B<DateTime::Tiny> provides the functionality required to
346             represent a date as an object, to stringify the date and to parse it
347             back in, but does B<not> allow you to modify the dates.
348            
349             The purpose of this is to allow for date object representations in
350             situations like log parsing and fast real-time type work.
351            
352             The problem with this is that having no ability to modify date limits
353             the usefulness greatly.
354            
355             To make up for this, B<if> you have L<DateTime> installed, any
356             B<DateTime::Tiny> module can be inflated into the equivalent L<DateTime>
357             as needing, loading L<DateTime> on the fly if necessary.
358            
359             This is somewhat similar to L<DateTime::LazyInit>, but unlike that module
360             B<DateTime::Tiny> objects are not modifiable.
361            
362             For the purposes of date/time logic, all B<DateTime::Tiny> objects exist
363             in the "C" locale, and the "floating" time zone. This may be improved in
364             the future if a suitably tiny way of handling timezones is found.
365            
366             When converting up to full L<DateTime> objects, these locale and time
367             zone settings will be applied (although an ability is provided to
368             override this).
369            
370             In addition, the implementation is strictly correct and is intended to
371             be very easily to sub-class for specific purposes of your own.
372            
373             =head1 USAGE
374            
375             In general, the intent is that the API be as close as possible to the
376             API for L<DateTime>. Except, of course, that this module implements
377             less of it.
378            
379             =head1 METHODS
380            
381             =head2 new
382            
383             my $date = DateTime::Tiny->new(
384             year => 2006,
385             month => 12,
386             day => 31,
387             hour => 10,
388             minute => 45,
389             second => 32,
390             );
391            
392             The C<new> constructor creates a new B<DateTime::Tiny> object.
393            
394             It takes six named parameters. C<day> should be the day of the month (1-31),
395             C<month> should be the month of the year (1-12), C<year> as a 4 digit year.
396             C<hour> should be the hour of the day (0-23), C<minute> should be the
397             minute of the hour (0-59) and C<second> should be the second of the
398             minute (0-59).
399            
400             These are the only parameters accepted.
401            
402             Returns a new B<DateTime::Tiny> object.
403            
404             =head2 now
405            
406             my $current_date = DateTime::Tiny->now;
407            
408             The C<now> method creates a new date object for the current date.
409            
410             The date created will be based on localtime, despite the fact that
411             the date is created in the floating time zone.
412            
413             Returns a new B<DateTime::Tiny> object.
414            
415             =head2 year
416            
417             The C<year> accessor returns the 4-digit year for the date.
418            
419             =head2 month
420            
421             The C<month> accessor returns the 1-12 month of the year for the date.
422            
423             =head2 day
424            
425             The C<day> accessor returns the 1-31 day of the month for the date.
426            
427             =head2 hour
428            
429             The C<hour> accessor returns the hour component of the time as
430             an integer from zero to twenty-three (0-23) in line with 24-hour
431             time.
432            
433             =head2 minute
434            
435             The C<minute> accessor returns the minute component of the time
436             as an integer from zero to fifty-nine (0-59).
437            
438             =head2 second
439            
440             The C<second> accessor returns the second component of the time
441             as an integer from zero to fifty-nine (0-59).
442            
443             =head2 ymdhms
444            
445             The C<ymdhms> method returns the most common and accurate stringified date
446             format, which returns in the form "2006-04-12T23:59:59".
447            
448             =head2 from_string
449            
450             The C<from_string> method creates a new B<DateTime::Tiny> object from a string.
451            
452             The string is expected to be an ISO 8601 combined date and time, with
453             separators (including the 'T' separator) and no time zone designator. No
454             other ISO 8601 formats are supported.
455            
456             my $almost_midnight = DateTime::Tiny->from_string( '2006-12-20T23:59:59' );
457            
458             Returns a new B<DateTime::Tiny> object, or throws an exception on error.
459            
460             =head2 as_string
461            
462             The C<as_string> method converts the date to the default string, which
463             at present is the same as that returned by the C<ymdhms> method above.
464            
465             This string conforms to the ISO 8601 standard for the encoding of a combined
466             date and time as a string, without time-zone designator.
467            
468             =head2 DateTime
469            
470             The C<DateTime> method is used to create a L<DateTime> object
471             that is equivalent to the B<DateTime::Tiny> object, for use in
472             conversions and calculations.
473            
474             As mentioned earlier, the object will be set to the 'C' locale,
475             and the 'floating' time zone.
476            
477             If installed, the L<DateTime> module will be loaded automatically.
478            
479             Returns a L<DateTime> object, or throws an exception if L<DateTime>
480             is not installed on the current host.
481            
482             =head1 HISTORY
483            
484             This module was written by Adam Kennedy in 2006. In 2016, David Golden
485             adopted it as a caretaker maintainer.
486            
487             =head1 SEE ALSO
488            
489             L<DateTime>, L<Date::Tiny>, L<Time::Tiny>, L<Config::Tiny>, L<ali.as>
490            
491             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
492            
493             =head1 SUPPORT
494            
495             =head2 Bugs / Feature Requests
496            
497             Please report any bugs or feature requests through the issue tracker
498             at L<https://github.com/dagolden/DateTime-Tiny/issues>.
499             You will be notified automatically of any progress on your issue.
500            
501             =head2 Source Code
502            
503             This is open source software. The code repository is available for
504             public review and contribution under the terms of the license.
505            
506             L<https://github.com/dagolden/DateTime-Tiny>
507            
508             git clone https://github.com/dagolden/DateTime-Tiny.git
509            
510             =head1 AUTHORS
511            
512             =over 4
513            
514             =item *
515            
516             Adam Kennedy <adamk@cpan.org>
517            
518             =item *
519            
520             David Golden <dagolden@cpan.org>
521            
522             =back
523            
524             =head1 CONTRIBUTORS
525            
526             =for stopwords Ken Williams Nigel Gregoire Ovid
527            
528             =over 4
529            
530             =item *
531            
532             Ken Williams <Ken.Williams@WindLogics.com>
533            
534             =item *
535            
536             Nigel Gregoire <nigelg@airg.com>
537            
538             =item *
539            
540             Ovid <curtis_ovid_poe@yahoo.com>
541            
542             =back
543            
544             =head1 COPYRIGHT AND LICENSE
545            
546             This software is copyright (c) 2006 by Adam Kennedy.
547            
548             This is free software; you can redistribute it and/or modify it under
549             the same terms as the Perl 5 programming language system itself.
550            
551             =cut
552