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   196 use strict;
  26         74  
  26         869  
4 26     26   195 use warnings;
  26         78  
  26         782  
5 26     26   150 use base qw(Exporter);
  26         75  
  26         2948  
6 26     26   187 use boolean qw(true false);
  26         64  
  26         173  
7              
8 26     26   2024 use constant REAL_FLAG => true;
  26         82  
  26         201  
9 26     26   1930 use constant VIRT_FLAG => false;
  26         87  
  26         110  
10              
11             our ($VERSION, @EXPORT_OK, %flag);
12              
13             $VERSION = '0.06';
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 17937     17937   33400 my $self = shift;
38 17937         38931 my ($flags, $string) = @_;
39              
40 17937         36237 foreach my $flag (@$flags) {
41 24612         39636 my $name = (keys %{$flags[$flag]})[0];
  24612         61140  
42 24612 100       75758 if ($flags[$flag]->{$name}) {
43 19681         154535 my $helper = "_$name";
44 19681         64639 $self->$helper(\$string);
45             }
46             else {
47 4931         51666 $string = $self->{data}->{conversion}->{$name}->{lc $string};
48             }
49             }
50              
51 17937         73573 return $string;
52             }
53              
54             sub _weekday_name
55             {
56 3645     3645   7314 my $self = shift;
57 3645         7898 my ($arg) = @_;
58              
59 3645         8423 my $helper = $self->{data}->{helpers};
60              
61 3645 100       24674 if ($$arg =~ $helper->{suffix}) {
62 42         215 $$arg =~ s/$helper->{suffix}//;
63             }
64 3645         14706 $helper->{normalize}->($arg);
65 3645 100       11985 if ($helper->{abbreviated}->($arg)) {
66 660         3091 $$arg = $self->{data}->{weekdays_abbrev}->{$$arg};
67             }
68             }
69              
70             sub _weekday_num
71             {
72 3645     3645   7143 my $self = shift;
73 3645         7875 my ($arg) = @_;
74              
75 3645         13584 $$arg = $self->_Decode_Day_of_Week($$arg);
76             }
77              
78             sub _month_name
79             {
80 3030     3030   6096 my $self = shift;
81 3030         6478 my ($arg) = @_;
82              
83 3030         7443 my $helper = $self->{data}->{helpers};
84              
85 3030         11634 $helper->{normalize}->($arg);
86 3030 100       9593 if ($helper->{abbreviated}->($arg)) {
87 2503         10745 $$arg = $self->{data}->{months_abbrev}->{$$arg};
88             }
89             }
90              
91             sub _month_num
92             {
93 3030     3030   6602 my $self = shift;
94 3030         6812 my ($arg) = @_;
95              
96 3030         12426 $$arg = $self->_Decode_Month($$arg);
97             }
98              
99             sub _time_am
100             {
101 3599     3599   7371 my $self = shift;
102 3599         7407 my ($arg) = @_;
103              
104 3599         11625 $self->_time_meridiem($arg, 'am');
105             }
106              
107             sub _time_pm
108             {
109 2732     2732   5820 my $self = shift;
110 2732         5938 my ($arg) = @_;
111              
112 2732         8361 $self->_time_meridiem($arg, 'pm');
113             }
114              
115             sub _time_meridiem
116             {
117 6331     6331   11078 my $self = shift;
118 6331         13780 my ($time, $period) = @_;
119              
120 6331         22171 my ($hour) = split /:/, $$time;
121              
122 6331 100       35109 my %hours = (
    100          
123             am => $hour - (($hour == 12) ? 12 : 0),
124             pm => $hour + (($hour == 12) ? 0 : 12),
125             );
126              
127 6331         64371 $$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