DateTime-Format-PGN

 view release on metacpan or  search on metacpan

lib/DateTime/Format/PGN.pm  view on Meta::CPAN

use strict;
use warnings;

package DateTime::Format::PGN;
# ABSTRACT: a Perl module for parsing and formatting date fields in chess game databases in PGN format

our $VERSION = '0.05';

use DateTime::Incomplete 0.08;
use Params::Validate 1.23 qw( validate BOOLEAN );


sub new {
    my( $class ) = shift;

    my %args = validate( @_,
        {
            fix_errors => {
                type        => BOOLEAN,
                default     => 0,
                callbacks   => {
                    'is 0, 1, or undef' =>
                        sub { ! defined( $_[0] ) || $_[0] == 0 || $_[0] == 1 },
                },
            },
            use_incomplete => {
                type        => BOOLEAN,
                default     => 0,
                callbacks   => {
                    'is 0, 1, or undef' =>
                        sub { ! defined( $_[0] ) || $_[0] == 0 || $_[0] == 1 },
                },
            },
        }
    );

    $class = ref( $class ) || $class;

    my $self = bless( \%args, $class );

    return( $self );
}



sub parse_datetime {
    my ( $self, $date ) = @_;
    my @matches = ( '????', '??', '??' );
    
    if ($date =~ m/^(\?{4}|[1-2]\d{3})\.(\?{2}|0[1-9]|1[0-2])\.(\?{2}|0[1-9]|[1-2]\d|3[0-1])$/) {
    
        @matches = ( $1,$2,$3 );
    }
    else {
    
        # We can try to fix frequently occuring faults.
        if ($self->{ fix_errors }) {
             
            # if we find a year, we can try to parse the wrong date.
            if ($date =~ /(\A|\D)([1-2]\d{3})(\D|\Z)/ && $2 > 0) {
        
                $matches[0] = $2;
                
                # Try to find month and day.
                if ($date =~ /(\A|\D)(0?[1-9]|[1-2][0-9]|3[0-1])\D+(0?[1-9]|[1-2][0-9]|3[0-1])(\D|\Z)/) {
                    if (($2 < 13 && $3 > 12) || ($2 == $3 && $2 < 13)) {
                        $matches[1] = $2;
                        $matches[2] = $3;
                    }
                    elsif ($3 < 13 && $2 > 12) {
                        $matches[1] = $3;
                        $matches[2] = $2;
                    }
                }
                elsif (index($date,'Jan') > -1) {
                    $matches[1] = 1;
                    $matches[2] = $2 if $date =~ /(\A|\D)(0?[1-9]|[1-2][0-9]|3[0-1])(\D|\Z)/ && $2 < 32 && $2 > 0;
                }
                elsif (index($date,'Feb') > -1) {
                    $matches[1] = 2;
                    $matches[2] = $2 if $date =~ /(\A|\D)(0?[1-9]|[1-2][0-9])(\D|\Z)/ && $2 < 32 && $2 > 0;
                }
                elsif (index($date,'Mar') > -1) {
                    $matches[1] = 3;
                    $matches[2] = $2 if $date =~ /(\A|\D)(0?[1-9]|[1-2][0-9]|3[0-1])(\D|\Z)/ && $2 < 32 && $2 > 0;
                }
                elsif (index($date,'Apr') > -1) {
                    $matches[1] = 4;
                    $matches[2] = $2 if $date =~ /(\A|\D)(0?[1-9]|[1-2][0-9]|30)(\D|\Z)/ && $2 < 32 && $2 > 0;



( run in 0.652 second using v1.01-cache-2.11-cpan-39bf76dae61 )