| 1 | package DateTime::Format::Flexible::lang; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use List::MoreUtils 'any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub new | ||||||||||||||||||||||||||||
| 9 | { | ||||||||||||||||||||||||||||
| 10 | 6312 | ▶ | my ( $class , %params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 6312 | my $self = bless \%params , $class; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6312 | ▶ | if ($self->{lang} and not ref($self->{lang}) eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"lang"} and not ref $self->{"lang"} eq "ARRAY"
MC/DC100%2/2
$self->{"lang"} and not ref $self->{"lang"} eq "ARRAY"
$self->{"lang"}
not ref $self->{"lang"} eq "ARRAY"
Truth table100%3/3
A and not(B)
A= $self->{"lang"}B= not ref $self->{"lang"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 14 | { | ||||||||||||||||||||||||||||
| 15 | 137 | $self->{lang} = [$self->{lang}]; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | $self->{_plugins} = [ | ||||||||||||||||||||||||||||
| 19 | 6312 | 'DateTime::Format::Flexible::lang::de', | |||||||||||||||||||||||||||
| 20 | 'DateTime::Format::Flexible::lang::en', | ||||||||||||||||||||||||||||
| 21 | 'DateTime::Format::Flexible::lang::es', | ||||||||||||||||||||||||||||
| 22 | ]; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 6312 | foreach my $plugin (@{$self->{_plugins}}) { | |||||||||||||||||||||||||||
| 25 | 18936 | my $path = $plugin . ".pm"; | |||||||||||||||||||||||||||
| 26 | 18936 | $path =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 27 | 18936 | require $path; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 6312 | return $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3765 | ▶ | sub plugins {return @{$_[0]->{_plugins}}} | ||||||||||||||||||||||||||
|
Subroutine
plugins: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub _cleanup | ||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 3765 | ▶ | my ( $self , $date , $p ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_cleanup: called CC=15 SCAR=28.3
| |||||||||||||||||||||||||||||
| 37 | 3765 | foreach my $plug ( $self->plugins ) | |||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | 11295 | ▶ | if ( $self->{lang} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | 417 | my ( $lang ) = $plug =~ m{(\w{2}\z)}mx; | |||||||||||||||||||||||||||
| 42 | 417 | ▶ | if ( not any { $_ eq $lang } @{ $self->{lang} } ) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 278 | ▶ | printf( "# skipping %s\n", $plug ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 278 | next; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 11017 | ▶ | printf( "# not skipping %s\n", $plug ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 11017 | ▶ | printf( "# before math: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 11017 | $date = $self->_do_math( $plug , $date ); | |||||||||||||||||||||||||||
| 52 | 11017 | ▶ | printf( "# before string_dates: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 11017 | $date = $self->_string_dates( $plug , $date ); | |||||||||||||||||||||||||||
| 54 | 11017 | ▶ | printf( "# before fix_alpha_month: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 11017 | ( $date , $p ) = $self->_fix_alpha_month( $plug , $date , $p ); | |||||||||||||||||||||||||||
| 56 | 11017 | ▶ | printf( "# before remove_day_names: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 11017 | $date = $self->_remove_day_names( $plug , $date ); | |||||||||||||||||||||||||||
| 58 | 11017 | ▶ | printf( "# before fix_hours: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 11017 | $date = $self->_fix_hours( $plug , $date ); | |||||||||||||||||||||||||||
| 60 | 11017 | ▶ | printf( "# before remove_strings: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 11017 | $date = $self->_remove_strings( $plug , $date ); | |||||||||||||||||||||||||||
| 62 | 11017 | ▶ | printf( "# before locate_time: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 11017 | $date = $self->_locate_time( $plug , $date ); | |||||||||||||||||||||||||||
| 64 | 11017 | ▶ | printf( "# before fix_internal_tz: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 11017 | ( $date , $p ) = $self->_fix_internal_tz( $plug , $date , $p ); | |||||||||||||||||||||||||||
| 66 | 11017 | ▶ | printf( "# finished: %s\n", $date ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 3765 | return ( $date , $p ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub _fix_internal_tz | ||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 11017 | ▶ | my ( $self , $plug , $date , $p ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fix_internal_tz: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 74 | 11017 | my %tzs = $plug->timezone_map; | |||||||||||||||||||||||||||
| 75 | 11017 | ▶ | while( my( $orig_tz , $new_tz ) = each ( %tzs ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 92432 | ▶ | if( $date =~ m{$orig_tz}mxi ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 5 | $p->{ time_zone } = $new_tz; | |||||||||||||||||||||||||||
| 80 | 5 | $date =~ s{$orig_tz}{}mxi; | |||||||||||||||||||||||||||
| 81 | 5 | $date =~ s{\(\)}{}g; # remove empty parens | |||||||||||||||||||||||||||
| 82 | 5 | return ( $date , $p ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 11012 | return ( $date , $p ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub _do_math | ||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 11017 | ▶ | my ( $self , $plug , $date ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_do_math: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 11017 | my %relative_strings = $plug->relative; | |||||||||||||||||||||||||||
| 93 | 11017 | my $day_strings = $plug->days; | |||||||||||||||||||||||||||
| 94 | 11017 | my %month_strings = $plug->months; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 11017 | my $instructions = { | |||||||||||||||||||||||||||
| 97 | ago => {direction => 'past', units => 1}, | ||||||||||||||||||||||||||||
| 98 | from => {direction => 'future', units => 1}, | ||||||||||||||||||||||||||||
| 99 | last => {direction => 'past'}, | ||||||||||||||||||||||||||||
| 100 | next => {direction => 'future'}, | ||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 11017 | foreach my $keyword (keys %relative_strings) | |||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 44068 | my $rx = $relative_strings{$keyword}; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 44068 | ▶ | next if not (exists $instructions->{$keyword}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 44068 | my $has_units = $instructions->{$keyword}->{units}; | |||||||||||||||||||||||||||
| 110 | 44068 | my $direction = $instructions->{$keyword}->{direction}; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 44068 | ▶ | if ( $date =~ m{$rx}mix ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 101 | $date =~ s{$rx}{}mix; | |||||||||||||||||||||||||||
| 115 | 101 | ▶ | if ($has_units) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 24 | $date = $self->_set_units( $plug , $date, $direction ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else | ||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 77 | foreach my $set (@{$day_strings}) | |||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 1155 | foreach my $day (keys %{$set}) | |||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1155 | ▶ | if ($date =~ m{$day}mix) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 29 | $date = $self->_set_day( $plug , $date , $day , $direction ); | |||||||||||||||||||||||||||
| 129 | 29 | $date =~ s{$day}{}mix; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 77 | foreach my $month (keys %month_strings) | |||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 924 | ▶ | if ($date =~ m{$month}mix) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||
| 137 | 48 | $date = $self->_set_month( $plug , $date , $month , $direction ); | |||||||||||||||||||||||||||
| 138 | 48 | $date =~ s{$month}{}mix; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 101 | ▶ | printf("# after removing rx (%s): [%s]\n", $rx, $date) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 101 | $date =~ s{$keyword}{}mx; | |||||||||||||||||||||||||||
| 145 | 101 | $date =~ s{\s+}{ }gm; | |||||||||||||||||||||||||||
| 146 | 101 | $date =~ s{\s+\z}{}gm; | |||||||||||||||||||||||||||
| 147 | 101 | ▶ | printf("# after removing keyword (%s): [%s]\n", $keyword, $date) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 11017 | return $date; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub _set_units | ||||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | 24 | ▶ | my ( $self , $plug , $date , $direction ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_units: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 24 | my %strings = $plug->math_strings; | |||||||||||||||||||||||||||
| 160 | 24 | ▶ | if ( my ( $amount , $unit ) = $date =~ m{(\d+)\s+([^\s]+)}mx ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||
| 162 | 21 | ▶ | printf( "# %s => %s\n", $amount, $unit ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 21 | ▶ | if ( exists( $strings{$unit} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | { | ||||||||||||||||||||||||||||
| 165 | 21 | my $base_dt = DateTime::Format::Flexible->base->clone; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 21 | ▶ | if ( $direction eq 'past' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||
| 169 | 13 | $base_dt->subtract( $strings{$unit} => $amount ); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 21 | ▶ | if ( $direction eq 'future' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 8 | $base_dt->add( $strings{$unit} => $amount ); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 21 | $date =~ s{\s{0,}$amount\s+$unit\s{0,}}{}mx; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 21 | ▶ | if ($ENV{DFF_DEBUG}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||||||||
| 179 | 0 | printf("# found: %s\n", $strings{$unit}) ; | |||||||||||||||||||||||||||
| 180 | 0 | printf("# after removing amount, unit: [%s]\n", $date); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 21 | $date = $base_dt->datetime . ' ' . $date; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 24 | return $date; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub _set_day | ||||||||||||||||||||||||||||
| 191 | { | ||||||||||||||||||||||||||||
| 192 | 29 | ▶ | my ( $self , $plug , $date , $day , $direction ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_day: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 29 | my $base_dt = DateTime::Format::Flexible->base->clone; | |||||||||||||||||||||||||||
| 195 | 29 | my $dow = $base_dt->day_of_week; | |||||||||||||||||||||||||||
| 196 | 29 | my $date_dow = $self->_alpha_day_to_int($plug, $day); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 29 | ▶ | if ( $direction eq 'past' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | { | ||||||||||||||||||||||||||||
| 200 | 14 | my $amount = $dow - $date_dow; | |||||||||||||||||||||||||||
| 201 | 14 | ▶ | if ($amount < 1) {$amount = 7 + $amount} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 14 | ▶ | printf("# subtracting %s days\n", $amount) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 14 | my $ret = $base_dt->subtract( 'days' => $amount )->truncate( to => 'day' ); | |||||||||||||||||||||||||||
| 205 | 14 | $date = $ret->datetime . ' ' . $date; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 29 | ▶ | if ( $direction eq 'future' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | { | ||||||||||||||||||||||||||||
| 210 | 15 | my $amount = $date_dow - $dow; | |||||||||||||||||||||||||||
| 211 | 15 | ▶ | if ($amount < 1) {$amount = 7 + $amount} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 15 | ▶ | printf("# adding %s days\n", $amount) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 15 | my $ret = $base_dt->add( 'days' => $amount )->truncate( to => 'day' ); | |||||||||||||||||||||||||||
| 215 | 15 | $date = $ret->datetime . ' ' . $date; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 29 | return $date; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | sub _set_month | ||||||||||||||||||||||||||||
| 223 | { | ||||||||||||||||||||||||||||
| 224 | 48 | ▶ | my ( $self , $plug , $date , $month , $direction ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_month: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 48 | my %month_strings = $plug->months; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 48 | my $base_dt = DateTime::Format::Flexible->base->clone; | |||||||||||||||||||||||||||
| 229 | 48 | my $mon = $base_dt->month; | |||||||||||||||||||||||||||
| 230 | 48 | my $date_mon = $month_strings{$month}; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 48 | ▶ | printf("# setting month to: %s\n", $date_mon) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 48 | $base_dt->set_month($date_mon); | |||||||||||||||||||||||||||
| 235 | 48 | ▶ | if ($direction eq 'past' and $date_mon >= $mon) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$direction eq "past" and $date_mon >= $mon
MC/DC100%2/2
$direction eq "past" and $date_mon >= $mon
$direction eq "past"
$date_mon >= $mon
Truth table100%3/3
A and B
A= $direction eq "past"B= $date_mon >= $mon
| |||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||
| 237 | 15 | $base_dt->set_year($base_dt->year - 1); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 48 | ▶ | if ($direction eq 'future' and $date_mon <= $mon) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$direction eq "future" and $date_mon <= $mon
MC/DC100%2/2
$direction eq "future" and $date_mon <= $mon
$direction eq "future"
$date_mon <= $mon
Truth table100%3/3
A and B
A= $direction eq "future"B= $date_mon <= $mon
| |||||||||||||||||||||||||||||
| 240 | { | ||||||||||||||||||||||||||||
| 241 | 11 | $base_dt->set_year($base_dt->year + 1); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | 48 | $base_dt->truncate( to => 'month' ); | |||||||||||||||||||||||||||
| 244 | 48 | ▶ | printf("# set year to: %s\n", $base_dt->year) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 48 | $date = $base_dt->datetime . ' ' . $date; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 48 | return $date; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | sub _string_dates | ||||||||||||||||||||||||||||
| 252 | { | ||||||||||||||||||||||||||||
| 253 | 11017 | ▶ | my ( $self , $plug , $date ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_string_dates: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 254 | 11017 | my %strings = $plug->string_dates; | |||||||||||||||||||||||||||
| 255 | 11017 | foreach my $key ( keys %strings ) | |||||||||||||||||||||||||||
| 256 | { | ||||||||||||||||||||||||||||
| 257 | 95527 | ▶ | if ( $date =~ m{\Q$key\E}mxi ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||
| 259 | 45 | my $new_value = $strings{$key}->(); | |||||||||||||||||||||||||||
| 260 | 45 | $date =~ s{\Q$key\E}{$new_value}mix; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 11017 | my %day_numbers = $plug->day_numbers; | |||||||||||||||||||||||||||
| 265 | 11017 | foreach my $key ( keys %day_numbers ) | |||||||||||||||||||||||||||
| 266 | { | ||||||||||||||||||||||||||||
| 267 | 411533 | ▶ | if (index(lc($date), lc($key)) >= 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | { | ||||||||||||||||||||||||||||
| 269 | 8 | my $new_value = $day_numbers{$key}; | |||||||||||||||||||||||||||
| 270 | 8 | $date =~ s{$key}{n${new_value}n}mix; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | 11017 | return $date; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # turn month names into month numbers with surrounding X | ||||||||||||||||||||||||||||
| 277 | # Sep => X9X | ||||||||||||||||||||||||||||
| 278 | sub _fix_alpha_month | ||||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||
| 280 | 11017 | ▶ | my ( $self , $plug , $date , $p ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fix_alpha_month: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 281 | 11017 | my %months = $plug->months; | |||||||||||||||||||||||||||
| 282 | 11017 | ▶ | while( my( $month_name , $month_number ) = each ( %months ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | { | ||||||||||||||||||||||||||||
| 284 | 132543 | ▶ | if( $date =~ m{\b$month_name\b}mxi ) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 285 | { | ||||||||||||||||||||||||||||
| 286 | 2409 | $p->{ month } = $month_number; | |||||||||||||||||||||||||||
| 287 | 2409 | $date =~ s{\b$month_name\b}{X${month_number}X}mxi; | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 2409 | return ( $date , $p ); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | elsif ( $date =~ m{\d$month_name}mxi ) | ||||||||||||||||||||||||||||
| 292 | { | ||||||||||||||||||||||||||||
| 293 | 11 | $p->{ month } = $month_number; | |||||||||||||||||||||||||||
| 294 | 11 | $date =~ s{(\d)$month_name}{$1X${month_number}X}mxi; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 11 | return ( $date , $p ); | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | elsif( $date =~ m{\b$month_name\d.*\b}mxi ) | ||||||||||||||||||||||||||||
| 300 | { | ||||||||||||||||||||||||||||
| 301 | 4 | $p->{ month } = $month_number; | |||||||||||||||||||||||||||
| 302 | 4 | $date =~ s{\b$month_name(\d.*)\b}{X${month_number}X$1}mxi; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 4 | return ( $date , $p ); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | 8593 | return ( $date , $p ); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # remove any day names, we do not need them | ||||||||||||||||||||||||||||
| 311 | sub _remove_day_names | ||||||||||||||||||||||||||||
| 312 | { | ||||||||||||||||||||||||||||
| 313 | 11017 | ▶ | my ( $self , $plug , $date ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_remove_day_names: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 314 | 11017 | my $days = $plug->days; | |||||||||||||||||||||||||||
| 315 | 11017 | foreach my $set (@{$days}) | |||||||||||||||||||||||||||
| 316 | { | ||||||||||||||||||||||||||||
| 317 | 134443 | foreach my $day_name ( keys %{$set} ) | |||||||||||||||||||||||||||
| 318 | { | ||||||||||||||||||||||||||||
| 319 | # if the day name is by itself, make it the upcoming day | ||||||||||||||||||||||||||||
| 320 | # eg: monday = next monday | ||||||||||||||||||||||||||||
| 321 | 134443 | ▶ | if (( lc($date) eq lc($day_name)) or (index(lc($date), lc($day_name) . ' at') >= 0 )) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
lc $date eq lc $day_name or (index(lc $date, lc($day_name) . ' at') != -1)
MC/DC100%2/2
lc $date eq lc $day_name or (index(lc $date, lc($day_name) . ' at') != -1)
lc $date eq lc $day_name
(index(lc $date, lc($day_name) . ' at') != -1)
Truth table100%3/3
A or B
A= lc $date eq lc $day_nameB= (index(lc $date, lc($day_name) . ' at') != -1)
| |||||||||||||||||||||||||||||
| 322 | { | ||||||||||||||||||||||||||||
| 323 | 16 | my $dt = $self->{base}->clone->truncate( to => 'day' ); | |||||||||||||||||||||||||||
| 324 | 16 | my $date_dow = $set->{$day_name}; | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 16 | ▶ | if ( $date_dow == $dt->dow ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 327 | { | ||||||||||||||||||||||||||||
| 328 | 2 | my $str = $dt->ymd; | |||||||||||||||||||||||||||
| 329 | 2 | $date =~ s{$day_name}{$str}i; | |||||||||||||||||||||||||||
| 330 | 2 | return $date; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | elsif ( $date_dow > $dt->dow ) | ||||||||||||||||||||||||||||
| 333 | { | ||||||||||||||||||||||||||||
| 334 | 12 | $dt->add( days => $date_dow - $dt->dow ); | |||||||||||||||||||||||||||
| 335 | 12 | my $str = $dt->ymd; | |||||||||||||||||||||||||||
| 336 | 12 | $date =~ s{$day_name}{$str}i; | |||||||||||||||||||||||||||
| 337 | 12 | return $date; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | else | ||||||||||||||||||||||||||||
| 340 | { | ||||||||||||||||||||||||||||
| 341 | 2 | $dt->add( days => $date_dow - $dt->dow + 7 ); | |||||||||||||||||||||||||||
| 342 | 2 | my $str = $dt->ymd; | |||||||||||||||||||||||||||
| 343 | 2 | $date =~ s{$day_name}{$str}i; | |||||||||||||||||||||||||||
| 344 | 2 | return $date; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | # otherwise, just strip it out | ||||||||||||||||||||||||||||
| 348 | 134427 | ▶ | if ( $date =~ m{\b$day_name\b}mxi ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | { | ||||||||||||||||||||||||||||
| 350 | 234 | $date =~ s{$day_name,?}{}gmix; | |||||||||||||||||||||||||||
| 351 | 234 | return $date; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 10767 | return $date; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | sub _alpha_day_to_int | ||||||||||||||||||||||||||||
| 359 | { | ||||||||||||||||||||||||||||
| 360 | 29 | ▶ | my ( $self, $plug, $day ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_alpha_day_to_int: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 29 | my $day_strings = $plug->days; | |||||||||||||||||||||||||||
| 363 | 29 | foreach my $set (@{$day_strings}) | |||||||||||||||||||||||||||
| 364 | { | ||||||||||||||||||||||||||||
| 365 | 238 | foreach my $key (keys %{$set}) | |||||||||||||||||||||||||||
| 366 | { | ||||||||||||||||||||||||||||
| 367 | 238 | ▶ | if (lc($key) eq lc($day)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | { | ||||||||||||||||||||||||||||
| 369 | 29 | return $set->{$key}; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | 0 | return; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | # fix noon and midnight, named hours | ||||||||||||||||||||||||||||
| 377 | sub _fix_hours | ||||||||||||||||||||||||||||
| 378 | { | ||||||||||||||||||||||||||||
| 379 | 11017 | ▶ | my ( $self , $plug , $date ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fix_hours: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 380 | 11017 | my %hours = $plug->hours; | |||||||||||||||||||||||||||
| 381 | 11017 | foreach my $hour ( keys %hours ) | |||||||||||||||||||||||||||
| 382 | { | ||||||||||||||||||||||||||||
| 383 | 74264 | ▶ | if ( $date =~ m{$hour}mxi ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | { | ||||||||||||||||||||||||||||
| 385 | 34 | my $realtime = $hours{ $hour }; | |||||||||||||||||||||||||||
| 386 | 34 | $date =~ s{T[^\s]+}{}; | |||||||||||||||||||||||||||
| 387 | 34 | $date =~ s{$hour}{${realtime}}gmix; | |||||||||||||||||||||||||||
| 388 | 34 | return $date; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | 10983 | return $date; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | sub _remove_strings | ||||||||||||||||||||||||||||
| 395 | { | ||||||||||||||||||||||||||||
| 396 | 11017 | ▶ | my ( $self , $plug , $date ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_remove_strings: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 397 | 11017 | my @rs = $plug->remove_strings; | |||||||||||||||||||||||||||
| 398 | 11017 | foreach my $rs ( @rs ) | |||||||||||||||||||||||||||
| 399 | { | ||||||||||||||||||||||||||||
| 400 | 25799 | ▶ | if ( $date =~ m{$rs}mxi ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | { | ||||||||||||||||||||||||||||
| 402 | 162 | ▶ | printf( "# removing string: %s\n", $rs ) if $ENV{DFF_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 162 | $date =~ s{$rs}{ }gmix; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | 11017 | $date =~ s{\A\s+}{}; | |||||||||||||||||||||||||||
| 408 | 11017 | $date =~ s{\s+\z}{}; | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 11017 | return $date; | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | sub _locate_time | ||||||||||||||||||||||||||||
| 414 | { | ||||||||||||||||||||||||||||
| 415 | 11017 | ▶ | my ( $self , $plug , $date ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_locate_time: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 416 | 11017 | $date = $plug->parse_time( $date ); | |||||||||||||||||||||||||||
| 417 | 11017 | return $date; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 22 | 1; | |||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | __END__ | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =head1 NAME | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | DateTime::Format::Flexible::lang - base language module to handle plugins for DateTime::Format::Flexible. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | You should not need to use this module directly | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =head2 new | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | Instantiate a new instance of this module. | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =head2 plugins | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | Returns a list of available language plugins. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | Tom Heady | ||||||||||||||||||||||||||||
| 445 | CPAN ID: thinc | ||||||||||||||||||||||||||||
| 446 | Punch, Inc. | ||||||||||||||||||||||||||||
| 447 | cpan@punch.net | ||||||||||||||||||||||||||||
| 448 | http://www.punch.net/ | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Copyright 2011 Tom Heady. | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 455 | modify it under the terms of either: | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =over 4 | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =item * the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||
| 460 | Software Foundation; either version 1, or (at your option) any | ||||||||||||||||||||||||||||
| 461 | later version, or | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =item * the Artistic License. | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =back | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | F<DateTime::Format::Flexible> | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =cut | ||||||||||||||||||||||||||||