File Coverage

blib/lib/Atompub/DateTime.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Atompub::DateTime;
2              
3 1     1   52899 use strict;
  1         2  
  1         43  
4 1     1   6 use warnings;
  1         2  
  1         36  
5              
6 1     1   484 use Atompub;
  0            
  0            
7             use DateTime;
8             use DateTime::Format::W3CDTF;
9             use DateTime::TimeZone;
10             use HTTP::Date qw(str2time time2isoz time2str);
11             use Perl6::Export::Attrs;
12             use Time::Local;
13              
14             use base qw(Class::Accessor::Fast Class::Data::Inheritable);
15              
16             use overload (
17             q{""} => \&w3c,
18             q{0+} => \&epoch,
19             fallback => 1,
20             );
21              
22             my $tz;
23             sub tz {
24             unless ($tz) {
25             eval { $tz = DateTime::TimeZone->new(name => 'local') };
26             if ($@) { $tz = DateTime::TimeZone->new(name => 'UTC' ) }
27             }
28             $tz;
29             }
30              
31             __PACKAGE__->mk_classdata(fmt => DateTime::Format::W3CDTF->new);
32             __PACKAGE__->mk_accessors(qw(dt));
33              
34             sub new {
35             my($class, @args) = @_;
36             my $self = bless {}, $class;
37             $self->init(@args) or return;
38             $self;
39             }
40              
41             sub init {
42             my($self, $arg) = @_;
43              
44             my $epoch = !$arg ? time
45             : UNIVERSAL::can($arg, 'epoch') ? $arg->epoch
46             : $arg =~ qr{^\d{1,13}$} ? $arg
47             : $arg =~ qr{^\d{14}$} ? _parse_timestamp($arg)
48             : str2time $arg;
49              
50             return unless defined $epoch;
51              
52             $self->dt(DateTime->from_epoch(
53             epoch => $epoch,
54             time_zone => $self->tz,
55             formatter => $self->fmt,
56             ));
57              
58             $self;
59             }
60              
61             sub datetime :Export { __PACKAGE__->new(@_) }
62              
63             sub _parse_timestamp {
64             my @a = $_[0] =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
65             $a[1]--; # month
66             timelocal reverse @a;
67             }
68              
69             sub epoch { $_[0]->dt->epoch }
70              
71             sub iso {
72             my($self) = @_;
73             $self->{iso} ||= sprintf '%s %s', $self->dt->ymd, $self->dt->hms;
74             }
75              
76             sub isoz {
77             my($self) = @_;
78             $self->{isoz} ||= time2isoz $self->epoch;
79             }
80              
81             sub w3c {
82             my($self) = @_;
83             $self->{w3c} ||= '' . $self->dt;
84             }
85              
86             sub w3cz {
87             my($self) = @_;
88             unless ($self->{w3cz}) {
89             my $w3cz = time2isoz $self->epoch;
90             $w3cz =~ s/ /T/;
91             $self->{w3cz} = $w3cz;
92             }
93             $self->{w3cz};
94             }
95              
96             sub str {
97             my($self) = @_;
98             $self->{str} ||= time2str $self->epoch;
99             }
100              
101             1;
102             __END__
103              
104             =head1 NAME
105              
106             Atompub::DateTime - A date and time object for the Atom Publishing Protocol
107              
108              
109             =head1 SYNOPSIS
110              
111             # assuming the local timezone is JST (+09:00)
112              
113             use Atompub::DateTime qw(datetime);
114              
115             $dt = datetime; # current time
116             $dt = datetime(DateTime->new);
117             $dt = datetime(1167609600); # UTC epoch value
118             $dt = datetime('20070101090000');
119             $dt = datetime('2007-01-01 09:00:00');
120             $dt = datetime('2007-01-01 00:00:00Z');
121             $dt = datetime('2007-01-01T09:00:00+09:00');
122             $dt = datetime('2007-01-01T00:00:00Z');
123             $dt = datetime('Mon, 01 Jan 2007 00:00:00 GMT');
124              
125             $dt->epoch; # 1167609600 (UTC epoch value)
126             $dt->iso; # 2007-01-01 09:00:00 (in localtime)
127             $dt->isoz; # 2007-01-01 00:00:00Z
128             $dt->w3c; # 2007-01-01T09:00:00+09:00
129             $dt->w3cz; # 2007-01-01T00:00:00Z
130             $dt->str; # Mon, 01 Jan 2007 00:00:00 GMT
131              
132             my $dt2 = datetime($dt); # copy
133              
134             $dt == $dt2; # compare
135              
136             "$dt"; # $dt->w3c
137              
138             $dt->dt; # DateTime object
139              
140             =head1 METHODS
141              
142             =head2 Atompub::DateTime->new([ $str ])
143              
144             Returns a datetime object representing the time $str.
145             If the function is called without an argument, it will use the current time.
146              
147             =head2 datetime([ $str ])
148              
149             An alias for Atompub::DateTime->new
150              
151             =head2 $datetime->epoch
152              
153             Returns UTC epoch value.
154              
155             1167609600
156              
157             =head2 $datetime->iso
158              
159             Returns a "YYYY-MM-DD hh:mm:ss"-formatted string representing time in the local time zone.
160              
161             2007-01-01 09:00:00
162              
163             =head2 $datetime->isoz
164              
165             Returns a "YYYY-MM-DD hh:mm:ssZ"-formatted string representing Universal Time.
166              
167             2007-01-01 00:00:00Z
168              
169             =head2 $datetime->w3c
170              
171             Returns a "YYYY-MM-DDThh:mm:ssTZ"-formatted string (W3C DateTime Format)
172             representing time in the local time zone.
173              
174             2007-01-01T09:00:00+09:00
175              
176             =head2 $datetime->w3cz
177              
178             Returns a "YYYY-MM-DDThh:mm:ssZ"-formatted string (W3C DateTime Format)
179             representing Universal Time.
180              
181             2007-01-01T00:00:00Z
182              
183             =head2 $datetime->str
184              
185             Returns a human readable representation.
186              
187             Mon, 01 Jan 2007 00:00:00 GMT
188              
189             =head2 $datetime->dt
190              
191             An accessor for the internal L<DateTime> object.
192              
193             =head2 $datetime->tz
194              
195             An accessor for the internal L<DateTime::TimeZone> object.
196              
197             =head2 $datetime->fmt
198              
199             An accessor for the internal L<DateTime::Format> object.
200              
201              
202             =head1 INTERNAL INTERFACES
203              
204             =head2 $datetime->init
205              
206             =head2 $datetime->_parse_timestamp
207              
208              
209             =head1 SEE ALSO
210              
211             L<Atompub>
212              
213              
214             =head1 AUTHOR
215              
216             Takeru INOUE, E<lt>takeru.inoue _ gmail.comE<gt>
217              
218              
219             =head1 LICENCE AND COPYRIGHT
220              
221             Copyright (c) 2007, Takeru INOUE C<< <takeru.inoue _ gmail.com> >>. All rights reserved.
222              
223             This module is free software; you can redistribute it and/or
224             modify it under the same terms as Perl itself. See L<perlartistic>.
225              
226              
227             =head1 DISCLAIMER OF WARRANTY
228              
229             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
230             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
231             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
232             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
233             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
234             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
235             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
236             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
237             NECESSARY SERVICING, REPAIR, OR CORRECTION.
238              
239             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
240             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
241             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
242             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
243             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
244             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
245             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
246             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
247             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
248             SUCH DAMAGES.
249              
250             =cut