File Coverage

blib/lib/DateTime/Format/Natural/Rewrite.pm
Criterion Covered Total %
statement 52 52 100.0
branch 16 16 100.0
condition 6 6 100.0
subroutine 7 7 100.0
pod n/a
total 81 81 100.0


line stmt bran cond sub pod time code
1             package DateTime::Format::Natural::Rewrite;
2              
3 26     26   250 use strict;
  26         64  
  26         1076  
4 26     26   134 use warnings;
  26         54  
  26         31787  
5              
6             our $VERSION = '0.10';
7              
8             sub _rewrite
9             {
10 12144     12144   27312 my $self = shift;
11 12144         36571 my ($date_string) = @_;
12              
13 12144         39350 foreach my $type (qw(regular aliases conditional)) {
14 36432         85494 my $method = "_rewrite_$type";
15 36432         155553 $self->$method($date_string);
16             }
17             }
18              
19             sub _rewrite_regular
20             {
21 12144     12144   27382 my $self = shift;
22 12144         33246 my ($date_string) = @_;
23              
24 12144         59772 $$date_string =~ s/,(?!\d)//g;
25 12144         100417 $$date_string =~ s/\s+?(a\.?m\.?|p\.?m\.?)\b/$1/gi;
26             }
27              
28             sub _rewrite_conditional
29             {
30 12144     12144   32421 my $self = shift;
31 12144         33981 my ($date_string) = @_;
32              
33 12144         35882 my $rewrite = $self->{data}->{rewrite};
34              
35             REWRITE: {
36 12144 100       24585 if ($$date_string =~ /$rewrite->{at}{match}/g) {
  14667         323676  
37 2529         12732 my $last_token = $1;
38             my @regexes = (
39             (map $self->{data}->__RE($_), qw(time time_am time_pm)),
40             $rewrite->{at}{daytime},
41 2529         25773 );
42 2529         9076 foreach my $regex (@regexes) {
43 5505 100       52649 if ($last_token =~ $regex) {
44 2523 100       14411 $$date_string =~ s/\G/:00/ if $last_token =~ /^\d{1,2}$/;
45 2523         25735 $$date_string =~ s/$rewrite->{at}{subst}//;
46 2523         11337 redo REWRITE;
47             }
48             }
49             }
50             }
51             }
52              
53             sub _rewrite_aliases
54             {
55 12144     12144   25960 my $self = shift;
56 12144         33176 my ($date_string) = @_;
57              
58 12144         47889 my $aliases = $self->{data}->{aliases};
59              
60 12144 100       69734 if ($$date_string =~ /\s+/) {
61 10360         34270 foreach my $type (qw(words tokens)) {
62 20720         46693 foreach my $alias (keys %{$aliases->{$type}}) {
  20720         128267  
63 145040 100       424528 if ($alias =~ /^\w+$/) {
64 134680         1284567 $$date_string =~ s/\b $alias \b/$aliases->{$type}{$alias}/gix;
65             }
66             else {
67 10360         64833 $$date_string =~ s/(?:^|(?<=\s)) $alias (?:(?=\s)|$)/$aliases->{$type}{$alias}/gix;
68             }
69             }
70             }
71             }
72             else {
73 1784         3689 foreach my $alias (keys %{$aliases->{words}}) {
  1784         9115  
74 5352         71940 $$date_string =~ s/^ $alias $/$aliases->{words}{$alias}/ix;
75             }
76 1784         4447 foreach my $alias (keys %{$aliases->{short}}) {
  1784         7673  
77 3568         93608 $$date_string =~ s/(?<=\d) $alias $/$aliases->{short}{$alias}/ix;
78             }
79             }
80             }
81              
82             sub _rewrite_duration
83             {
84 1258     1258   3145 my $self = shift;
85 1258         3651 my ($date_strings) = @_;
86              
87 1258         9781 my ($formatted) = $date_strings->[0] =~ $self->{data}->__regexes('format');
88 1258         8371 my %count = $self->_count_separators($formatted);
89              
90 1258 100 100     5631 return unless ($self->_check_formatted('ymd', \%count)
91             || $self->_check_formatted('md', \%count));
92              
93 137 100       2755 if ($date_strings->[0] =~ /^ \Q$formatted\E \s+ \d{1,2} $/x) {
94 12         50 $date_strings->[0] .= ':00';
95             }
96 137 100 100     1450 if (@$date_strings == 2
97             && $date_strings->[1] =~ /^ \d{1,2} $/x)
98             {
99 12         83 $date_strings->[1] .= ':00';
100             }
101             }
102              
103             1;
104             __END__
105              
106             =head1 NAME
107              
108             DateTime::Format::Natural::Rewrite - Aliasing and rewriting of date strings
109              
110             =head1 SYNOPSIS
111              
112             Please see the DateTime::Format::Natural documentation.
113              
114             =head1 DESCRIPTION
115              
116             The C<DateTime::Format::Natural::Rewrite> class handles aliases and regular
117             rewrites of date strings.
118              
119             =head1 SEE ALSO
120              
121             L<DateTime::Format::Natural>
122              
123             =head1 AUTHOR
124              
125             Steven Schubiger <schubiger@cpan.org>
126              
127             =head1 LICENSE
128              
129             This program is free software; you may redistribute it and/or
130             modify it under the same terms as Perl itself.
131              
132             See L<http://dev.perl.org/licenses/>
133              
134             =cut