DateTime::Format::Builder::Parser::Strptime - strptime based date parsing


DateTime-Format-Builder documentation Contained in the DateTime-Format-Builder distribution.

Index


Code Index:

NAME

Top

DateTime::Format::Builder::Parser::Strptime - strptime based date parsing

SYNOPSIS

Top

   my $parser = DateTime::Format::Builder->create_parser(
	strptime => '%e/%b/%Y:%H:%M:%S %z',
   );

SPECIFICATION

Top

THANKS

Top

See the main module's section.

SUPPORT

Top

Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details.

Alternatively, log them via the CPAN RT system via the web or email:

    http://perl.dellah.org/rt/dtbuilder
    bug-datetime-format-builder@rt.cpan.org

This makes it much easier for me to track things and thus means your problem is less likely to be neglected.

LICENCE AND COPYRIGHT

Top

AUTHOR

Top

Iain Truskett <spoon@cpan.org>

SEE ALSO

Top

datetime@perl.org mailing list.

http://datetime.perl.org/

perl, DateTime, DateTime::Format::Builder


DateTime-Format-Builder documentation Contained in the DateTime-Format-Builder distribution.
package DateTime::Format::Builder::Parser::Strptime;

use strict;
use vars qw( $VERSION @ISA );
use Params::Validate qw( validate SCALAR HASHREF );

$VERSION = '0.77';
use DateTime::Format::Builder::Parser::generic;
@ISA = qw( DateTime::Format::Builder::Parser::generic );

__PACKAGE__->valid_params(
    strptime	=> {
	type	=> SCALAR|HASHREF, # straight pattern or options to DTF::Strptime
    },
);

sub create_parser
{
    my ($self, %args) = @_;

    # Arguments to DTF::Strptime
    my $pattern = $args{strptime};

    # Create our strptime parser
    require DateTime::Format::Strptime;
    my $strptime = DateTime::Format::Strptime->new(
	( ref $pattern ? %$pattern : ( pattern => $pattern ) ),
    );
    unless (ref $self)
    {
	$self = $self->new( %args );
    }
    $self->{strptime} = $strptime;

    # Create our parser
    return $self->generic_parser(
	( map { exists $args{$_} ? ( $_ => $args{$_} ) : () } qw(
	    on_match on_fail preprocess postprocess
	    ) ),
	label => $args{label},
    );
}

sub do_match
{
    my $self = shift;
    my $date = shift;
    local $^W; # bizarre bug
    # Do the match!
    my $dt = eval { $self->{strptime}->parse_datetime( $date ) };
    return $@ ? undef : $dt;
}

sub post_match
{
    return $_[2];
}

1;

__END__