1package DateTime::Format::Builder::Parser::Strptime; 2 3=head1 NAME 4 5DateTime::Format::Builder::Parser::Strptime - strptime based date parsing 6 7=head1 SYNOPSIS 8 9 my $parser = DateTime::Format::Builder->create_parser( 10 strptime => '%e/%b/%Y:%H:%M:%S %z', 11 ); 12 13=head1 SPECIFICATION 14 15=over 4 16 17=item * 18 19B<strptime> takes as its argument a strptime string. 20See L<DateTime::Format::Strptime> for more information 21on valid patterns. 22 23=back 24 25=cut 26 27use strict; 28use vars qw( $VERSION @ISA ); 29use Params::Validate qw( validate SCALAR HASHREF ); 30 31$VERSION = '0.77'; 32use DateTime::Format::Builder::Parser::generic; 33@ISA = qw( DateTime::Format::Builder::Parser::generic ); 34 35__PACKAGE__->valid_params( 36 strptime => { 37 type => SCALAR|HASHREF, # straight pattern or options to DTF::Strptime 38 }, 39); 40 41sub create_parser 42{ 43 my ($self, %args) = @_; 44 45 # Arguments to DTF::Strptime 46 my $pattern = $args{strptime}; 47 48 # Create our strptime parser 49 require DateTime::Format::Strptime; 50 my $strptime = DateTime::Format::Strptime->new( 51 ( ref $pattern ? %$pattern : ( pattern => $pattern ) ), 52 ); 53 unless (ref $self) 54 { 55 $self = $self->new( %args ); 56 } 57 $self->{strptime} = $strptime; 58 59 # Create our parser 60 return $self->generic_parser( 61 ( map { exists $args{$_} ? ( $_ => $args{$_} ) : () } qw( 62 on_match on_fail preprocess postprocess 63 ) ), 64 label => $args{label}, 65 ); 66} 67 68sub do_match 69{ 70 my $self = shift; 71 my $date = shift; 72 local $^W; # bizarre bug 73 # Do the match! 74 my $dt = eval { $self->{strptime}->parse_datetime( $date ) }; 75 return $@ ? undef : $dt; 76} 77 78sub post_match 79{ 80 return $_[2]; 81} 82 831; 84 85__END__ 86 87=head1 THANKS 88 89See L<the main module's section|DateTime::Format::Builder/"THANKS">. 90 91=head1 SUPPORT 92 93Support for this module is provided via the datetime@perl.org email 94list. See http://lists.perl.org/ for more details. 95 96Alternatively, log them via the CPAN RT system via the web or email: 97 98 http://perl.dellah.org/rt/dtbuilder 99 bug-datetime-format-builder@rt.cpan.org 100 101This makes it much easier for me to track things and thus means 102your problem is less likely to be neglected. 103 104=head1 LICENCE AND COPYRIGHT 105 106Copyright E<copy> Iain Truskett, 2003. All rights reserved. 107 108This library is free software; you can redistribute it and/or modify 109it under the same terms as Perl itself, either Perl version 5.000 or, 110at your option, any later version of Perl 5 you may have available. 111 112The full text of the licences can be found in the F<Artistic> and 113F<COPYING> files included with this module, or in L<perlartistic> and 114L<perlgpl> as supplied with Perl 5.8.1 and later. 115 116=head1 AUTHOR 117 118Iain Truskett <spoon@cpan.org> 119 120=head1 SEE ALSO 121 122C<datetime@perl.org> mailing list. 123 124http://datetime.perl.org/ 125 126L<perl>, L<DateTime>, 127L<DateTime::Format::Builder> 128 129=cut 130 131 132