File Coverage

blib/lib/DateTime/Format/Natural/Helpers.pm
Criterion Covered Total %
statement 59 59 100.0
branch 12 12 100.0
condition n/a
subroutine 14 14 100.0
pod n/a
total 85 85 100.0


line stmt bran cond sub pod time code
1             package DateTime::Format::Natural::Helpers;
2              
3 26     26   166 use strict;
  26         58  
  26         1070  
4 26     26   134 use warnings;
  26         68  
  26         1605  
5 26     26   155 use base qw(Exporter);
  26         54  
  26         3706  
6 26     26   203 use boolean qw(true false);
  26         53  
  26         177  
7              
8 26     26   2477 use constant REAL_FLAG => true;
  26         56  
  26         202  
9 26     26   2392 use constant VIRT_FLAG => false;
  26         71  
  26         122  
10              
11             our ($VERSION, @EXPORT_OK, %flag);
12              
13             $VERSION = '0.07';
14             @EXPORT_OK = qw(%flag);
15              
16             my @flags = (
17             { weekday_name => REAL_FLAG },
18             { weekday_num => REAL_FLAG },
19             { month_name => REAL_FLAG },
20             { month_num => REAL_FLAG },
21             { time_am => REAL_FLAG },
22             { time_pm => REAL_FLAG },
23             { last_this_next => VIRT_FLAG },
24             { yes_today_tom => VIRT_FLAG },
25             { noon_midnight => VIRT_FLAG },
26             { morn_aftern_even => VIRT_FLAG },
27             { before_after_from => VIRT_FLAG },
28             );
29              
30             {
31             my $i;
32             %flag = map { (keys %$_)[0] => $i++ } @flags;
33             }
34              
35             sub _helper
36             {
37 18153     18153   36852 my $self = shift;
38 18153         64584 my ($flags, $string) = @_;
39              
40 18153         41551 foreach my $flag (@$flags) {
41 24876         47575 my $name = (keys %{$flags[$flag]})[0];
  24876         86990  
42 24876 100       102076 if ($flags[$flag]->{$name}) {
43 19933         176424 my $helper = "_$name";
44 19933         92872 $self->$helper(\$string);
45             }
46             else {
47 4943         71000 $string = $self->{data}->{conversion}->{$name}->{lc $string};
48             }
49             }
50              
51 18153         90167 return $string;
52             }
53              
54             sub _weekday_name
55             {
56 3645     3645   9002 my $self = shift;
57 3645         10648 my ($arg) = @_;
58              
59 3645         12285 my $helper = $self->{data}->{helpers};
60              
61 3645 100       39564 if ($$arg =~ $helper->{suffix}) {
62 42         427 $$arg =~ s/$helper->{suffix}//;
63             }
64 3645         20958 $helper->{normalize}->($arg);
65 3645 100       17754 if ($helper->{abbreviated}->($arg)) {
66 660         4957 $$arg = $self->{data}->{weekdays_abbrev}->{$$arg};
67             }
68             }
69              
70             sub _weekday_num
71             {
72 3645     3645   8458 my $self = shift;
73 3645         11034 my ($arg) = @_;
74              
75 3645         22019 $$arg = $self->_Decode_Day_of_Week($$arg);
76             }
77              
78             sub _month_name
79             {
80 3078     3078   7844 my $self = shift;
81 3078         8651 my ($arg) = @_;
82              
83 3078         12037 my $helper = $self->{data}->{helpers};
84              
85 3078         17814 $helper->{normalize}->($arg);
86 3078 100       14656 if ($helper->{abbreviated}->($arg)) {
87 2551         17120 $$arg = $self->{data}->{months_abbrev}->{$$arg};
88             }
89             }
90              
91             sub _month_num
92             {
93 3078     3078   7004 my $self = shift;
94 3078         8137 my ($arg) = @_;
95              
96 3078         30062 $$arg = $self->_Decode_Month($$arg);
97             }
98              
99             sub _time_am
100             {
101 3671     3671   9528 my $self = shift;
102 3671         9923 my ($arg) = @_;
103              
104 3671         15312 $self->_time_meridiem($arg, 'am');
105             }
106              
107             sub _time_pm
108             {
109 2816     2816   7627 my $self = shift;
110 2816         8742 my ($arg) = @_;
111              
112 2816         14624 $self->_time_meridiem($arg, 'pm');
113             }
114              
115             sub _time_meridiem
116             {
117 6487     6487   15744 my $self = shift;
118 6487         18188 my ($time, $period) = @_;
119              
120 6487         50825 my ($hour) = split /[:\.]/, $$time;
121              
122 6487 100       55764 my %hours = (
    100          
123             am => $hour - (($hour == 12) ? 12 : 0),
124             pm => $hour + (($hour == 12) ? 0 : 12),
125             );
126              
127 6487         86175 $$time =~ s/^ \d+? (?:(?=[:\.])|$)/$hours{$period}/x;
128             }
129              
130             1;
131             __END__
132              
133             =head1 NAME
134              
135             DateTime::Format::Natural::Helpers - Various helper methods
136              
137             =head1 SYNOPSIS
138              
139             Please see the DateTime::Format::Natural documentation.
140              
141             =head1 DESCRIPTION
142              
143             The C<DateTime::Format::Natural::Helpers> class defines helper methods.
144              
145             =head1 SEE ALSO
146              
147             L<DateTime::Format::Natural>
148              
149             =head1 AUTHOR
150              
151             Steven Schubiger <schubiger@cpan.org>
152              
153             =head1 LICENSE
154              
155             This program is free software; you may redistribute it and/or
156             modify it under the same terms as Perl itself.
157              
158             See L<http://dev.perl.org/licenses/>
159              
160             =cut