DateTime-Format-Natural

 view release on metacpan or  search on metacpan

lib/DateTime/Format/Natural/Lang/EN.pm  view on Meta::CPAN

                day           => qr/\d{1,3}/,
            },
            order => [qw(
                time_meridiem
                time
                day_ordinal
                day
            )],
            categories => {
                time_meridiem => 'time',
                time          => 'time',
                day_ordinal   => 'day',
                day           => 'day',
            },
            extract => {
                left => {
                    time => qr/(?:$regexes{format_}|$re{day}\s+$re{month}|$re{month}\s+$re{day})/,
                    day  => qr/$re{month}/,
                },
                right => {
                    time => qr/(?:$re{day}\s+$re{month}|$re{month}\s+$re{day})/,
                    day  => qr/(?:$re{month}|day)/i,
                },
            },
        },
    );

    %data_aliases = (
        words => {
            tues  => 'tue',
            thur  => 'thu',
            thurs => 'thu',
        },
        tokens => {
            msec  => 'millisecond',
            msecs => 'milliseconds',
            sec   => 'second',
            secs  => 'seconds',
            min   => 'minute',
            mins  => 'minutes',
            hr    => 'hour',
            hrs   => 'hours',
            yr    => 'year',
            yrs   => 'years',
            '@'   => 'at',
        },
        short => {
            min => 'minute',
            d   => 'day',
        },
    );

    %data_rewrite = (
        at => {
            match   => qr/\S+? \s+? at \s+? (\S+)/ix,
            subst   => qr/\s+? at \b/ix,
            daytime => qr/^(?:noon|midnight)$/i,
        },
    );

    # month/day are expressed in the respective calendar system
    %data_holidays = (
        gregorian => {
            christmas => { month => 12, day => 25 },
            new_year  => { month =>  1, day =>  1 },
        },
        julian => {
            christmas => { month => 12, day => 25 },
            new_year  => { month =>  1, day =>  1 },
        },
    );
}

%extended_checks = (
    meridiem => sub
    {
        my ($first_stack, $rest_stack, $pos, $error) = @_;

        my ($hour) = split /[:\.]/, $first_stack->{$pos->[0]};

        if ($hour == 0) {
            $$error = 'hour zero must be literal 12';
            return false;
        }
        elsif ($hour > 12) {
            $$error = 'hour exceeds 12-hour clock';
            return false;
        }
        return true;
    },
    ordinal => sub
    {
        my ($first_stack, $rest_stack, $pos, $error) = @_;

        my $suffix = do {
            local $_ = $rest_stack->{$pos->[0]}->[0];
            defined $_ ? lc $_ : undef;
        };
        return skip unless defined $suffix;

        my $numeral = $first_stack->{$pos->[0]};

        my %ordinals = (
            1 => { regex => qr/^st$/,  suffix => 'st' },
            2 => { regex => qr/^n?d$/, suffix => 'nd' },
            3 => { regex => qr/^r?d$/, suffix => 'rd' },
        );

        my $fail_message = sub { "letter suffix should be '$_[0]'" };

        if ($numeral == 0) {
            unless ($suffix eq 'th') {
                $$error = $fail_message->('th');
                return false;
            }
            return true;
        }
        elsif ($numeral =~ /([1-3])$/ && $numeral !~ /1\d$/) {
            unless ($suffix =~ $ordinals{$1}->{regex}) {
                $$error = $fail_message->($ordinals{$1}->{suffix});
                return false;



( run in 0.478 second using v1.01-cache-2.11-cpan-f52f0507bed )