File Coverage

blib/lib/DateTimeX/Format/POSIX/Strptime.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package DateTimeX::Format::POSIX::Strptime;
2 2     2   23719 use strict;
  2         4  
  2         79  
3 2     2   12 use warnings;
  2         3  
  2         93  
4              
5             our $VERSION = '00.01_03';
6              
7 2     2   11 use feature ':5.10';
  2         8  
  2         279  
8 2     2   2252 use mro 'c3';
  2         1683  
  2         11  
9 2     2   114 use 5.010;
  2         8  
  2         77  
10              
11 2     2   2479 use Moose;
  0            
  0            
12             use Carp;
13              
14             with 'DateTimeX::Format::CustomPattern';
15              
16             use POSIX::strptime;
17             use POSIX qw();
18              
19             sub parse_datetime {
20             my ( $self, $time, $args ) = @_;
21             my $pattern = $args->{pattern};
22             my $time_zone = $args->{time_zone};
23             my $locale = $args->{locale};
24            
25             if ( $pattern =~ m/%([cxX])/ ) {
26             my $cldr;
27             if ( $1 eq 'c' ) {
28             $cldr = $self->locale->datetime_format_default
29             }
30             elsif ( $1 eq 'x' ) {
31             $cldr = $self->locale->date_format_default
32             }
33             elsif ( $1 eq 'X' ) {
34             $cldr = $self->locale->time_format_default
35             }
36             Class::MOP::load_class( 'DateTime::Format::CLDR' );
37            
38             my $dt = DateTime::Format::CLDR->new(
39             pattern => $cldr
40             , locale => $locale
41             , time_zone => $time_zone
42             , on_error => 'croak'
43             )->parse_datetime( $time );
44             }
45             my @time = POSIX::strptime($time,$pattern);
46              
47             ## Day and month are 1-based
48             $time[4] += 1;
49              
50             ## Year is +=1900
51             $time[5] += 1900 if defined $time[5];
52              
53             $self->new_datetime({
54             time_zone => $time_zone
55             , locale => $locale
56             , second => $time[0]
57             , minute => $time[1]
58             , hour => $time[2]
59             , day => $time[3]
60             , month => $time[4]
61             , year => $time[5]
62             });
63              
64             }
65              
66             sub format_datetime {
67             my ( $self, $dt, $env ) = @_;
68              
69             my $pattern = $env->{pattern};
70             $pattern //= $self->has_pattern
71             ? $self->pattern
72             : croak 'No pattern to format object with'
73             ;
74              
75             $dt->strftime( $pattern );
76             }
77              
78             1;
79              
80             no Moose;
81             __PACKAGE__->meta->make_immutable;
82              
83             __END__
84              
85             =head1 NAME
86              
87             DateTimeX::Format::POSIX::Strptime - OO interface into the POSIX library's strptime
88              
89             =head1 SYNOPSIS
90              
91             use DateTimeX::Format::Strptime;
92              
93             my $dtf = DateTimeX::Format::Strptime({ locale => 'en_US', time_zone => 'America/Chicago', pattern => $pattern });
94              
95             $dtf->parse_datetime( "time" );
96              
97             $dtf->pattern( $newPattern );
98              
99             ## Call-only pattern won't be cached
100             $dtf->parse_datetime( "time", { pattern => $pattern } );
101              
102             $dtf->format_datetime( $dt, $pattern );
103              
104             =head1 DESCRIPTION
105              
106             This module does *not* reimpliment strptime(3) into perl. It binds into the POSIX library using L<POSIX::strptime> and uses Moose for the rest. This is massively simplier and less error-prone than L<DateTime::Format::Strptime> which is an attempt at a total perl implimentation of POSIX strptime(3).
107              
108             This module differs from L<DateTime::Format::Strptime> in a few ways: (a) it doesn't have complex PrintError/RaiseError code, it simply dies if it has reason to believe there was an error; (b) it doesn't have complex diagnostic code, and it doesn't really need it either: the work is in the POSIX library not perl; (c) it has all of the advantages of the L<DateTimeX::Format>, and L<DateTimeX::Format::CustomPattern> roles.
109              
110             =head1 CONSTRUCTOR AND METHODS
111              
112             See the two accompanying roles L<DateTimeX::Format>, and L<DateTimeX::Format::CustomPattern> which provide the constructor, and details about the how the methods B<parse_datetime> and B<format_datetime> work.
113              
114             =head1 AUTHOR
115              
116             Evan Carroll, C<< <me at evancarroll.com> >>
117              
118             =head1 BUGS
119              
120             Please report any bugs or feature requests to C<bug-datetimex-format-posix-strptime at rt.cpan.org>, or through
121             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DateTimeX-Format-POSIX-Strptime>. I will be notified, and then you'll
122             automatically be notified of progress on your bug as I make changes.
123              
124             =head1 SUPPORT
125              
126             You can find documentation for this module with the perldoc command.
127              
128             perldoc DateTimeX::Format::POSIX::Strptime
129              
130             You can also look for information at:
131              
132             =over 4
133              
134             =item * RT: CPAN's request tracker
135              
136             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DateTimeX-Format-POSIX-Strptime>
137              
138             =item * AnnoCPAN: Annotated CPAN documentation
139              
140             L<http://annocpan.org/dist/DateTimeX-Format-POSIX-Strptime>
141              
142             =item * CPAN Ratings
143              
144             L<http://cpanratings.perl.org/d/DateTimeX-Format-POSIX-Strptime>
145              
146             =item * Search CPAN
147              
148             L<http://search.cpan.org/dist/DateTimeX-Format-POSIX-Strptime/>
149              
150             =back
151              
152             =head1 COPYRIGHT & LICENSE
153              
154             Copyright 2009 Evan Carroll, all rights reserved.
155              
156             This program is free software; you can redistribute it and/or modify it
157             under the same terms as Perl itself.