| 1 | package DateTime::Event::NameDay; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 5 | ▶ | use vars qw ($VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 5 | $VERSION = '0.02'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 5 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 5 | ▶ | use Params::Validate qw( validate SCALAR OBJECT HASHREF ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 5 | ▶ | use DateTime; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 5 | ▶ | use DateTime::Set; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 5 | ▶ | use DateTime::Calendar::Christian; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | |||||||||||||||
| 17 | 5 | my %namedays = (); | |||||||||||||
| 18 | |||||||||||||||
| 19 | |||||||||||||||
| 20 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 21 | 1 | my $class = shift; | |||||||||||||
| 22 | 1 | my %p = validate( @_, | |||||||||||||
| 23 | { country => { type => SCALAR, | ||||||||||||||
| 24 | default => undef, | ||||||||||||||
| 25 | # Leave the heavy validation to set | ||||||||||||||
| 26 | }, | ||||||||||||||
| 27 | date_args => { type => HASHREF, | ||||||||||||||
| 28 | default => {}, | ||||||||||||||
| 29 | }, | ||||||||||||||
| 30 | }, | ||||||||||||||
| 31 | ); | ||||||||||||||
| 32 | 1 | my $self = { }; | |||||||||||||
| 33 | 1 | bless $self, $class; | |||||||||||||
| 34 | 1 | $self->set( %p ); | |||||||||||||
| 35 | |||||||||||||||
| 36 | 1 | return $self; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 1 | ▶ | sub set { | ||||||||||||
|
Subroutine
set: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||
| 40 | 1 | my $self = shift; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 1 | my %p = validate( @_, | |||||||||||||
| 43 | { country => { type => SCALAR, | ||||||||||||||
| 44 | optional => 1, | ||||||||||||||
| 45 | callbacks => | ||||||||||||||
| 46 | {'known day mapping' => \&_check_country } | ||||||||||||||
| 47 | }, | ||||||||||||||
| 48 | date_args => { type => HASHREF, | ||||||||||||||
| 49 | default => {}, | ||||||||||||||
| 50 | }, | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | ); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 1 | ▶ | if (defined $p{country}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 1 | $self->{country} = lc $p{country}; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 1 | ▶ | if (defined $p{date_args}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 1 | $self->{date_args} = $p{date_args}; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 1 | return $self; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | ▶ | sub country { | ||||||||||||
|
Subroutine
country: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 66 | 0 | my ($self) = @_; | |||||||||||||
| 67 | 0 | ▶ | return undef unless ref $self; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | return $self->{country}; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | |||||||||||||||
| 72 | 7 | ▶ | sub date_args { | ||||||||||||
|
Subroutine
date_args: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||
| 73 | 7 | my ($self) = @_; | |||||||||||||
| 74 | 7 | ▶ | return {} unless ref $self; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 0 | return $self->{date_args}; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | sub get_daynames | ||||||||||||||
| 79 | 8 | ▶ | { | ||||||||||||
|
Subroutine
get_daynames: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||
| 80 | 8 | my $self = shift; | |||||||||||||
| 81 | 8 | my %p = validate( @_, | |||||||||||||
| 82 | { country => { type => SCALAR, | ||||||||||||||
| 83 | optional => 1, | ||||||||||||||
| 84 | callbacks => | ||||||||||||||
| 85 | {'known day mapping' => \&_check_country } | ||||||||||||||
| 86 | }, | ||||||||||||||
| 87 | date => { type => OBJECT, | ||||||||||||||
| 88 | can => 'utc_rd_values', | ||||||||||||||
| 89 | }, | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | ); | ||||||||||||||
| 92 | |||||||||||||||
| 93 | # Work out our country | ||||||||||||||
| 94 | 7 | my $country = lc $p{country}; | |||||||||||||
| 95 | 7 | ▶ | if (not defined $country) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | 0 | $country = $self->country(); | |||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | ▶ | croak "Unable to determine the correct country" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | unless defined $country; | ||||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | # Get the namedays for the given date | ||||||||||||||
| 103 | # - Find our section | ||||||||||||||
| 104 | 7 | my $nameday_info = | |||||||||||||
| 105 | $self->_init_nameday_country(namedays => \%namedays, | ||||||||||||||
| 106 | country => $country); | ||||||||||||||
| 107 | |||||||||||||||
| 108 | # - Convert to the Julian calendar | ||||||||||||||
| 109 | my $adj_dt = DateTime::Calendar::Christian->from_object | ||||||||||||||
| 110 | (object => $p{date}, | ||||||||||||||
| 111 | reform_date => $nameday_info->{reform_date}, | ||||||||||||||
| 112 | 7 | %{ $self->date_args() }, | |||||||||||||
| 113 | ); | ||||||||||||||
| 114 | |||||||||||||||
| 115 | # - Get the appropriate nameday based on month number and day | ||||||||||||||
| 116 | 7 | my $names = $nameday_info->{names}{ $adj_dt->month() }{ $adj_dt->day() }; | |||||||||||||
| 117 | 7 | ▶ | my @names = defined $names ? @$names : (); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | |||||||||||||||
| 119 | 7 | return @names; | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 1 | ▶ | sub get_namedays { | ||||||||||||
|
Subroutine
get_namedays: called CC=6 SCAR=20.3
Pod
undocumented
| |||||||||||||||
| 123 | 1 | my $self = shift; | |||||||||||||
| 124 | 1 | my %p = validate( @_, | |||||||||||||
| 125 | { country => { type => SCALAR, | ||||||||||||||
| 126 | optional => 1, | ||||||||||||||
| 127 | callbacks => | ||||||||||||||
| 128 | {'known day mapping' => \&_check_country } | ||||||||||||||
| 129 | }, | ||||||||||||||
| 130 | date_args => { type => HASHREF, | ||||||||||||||
| 131 | default => undef, | ||||||||||||||
| 132 | }, | ||||||||||||||
| 133 | name => { type => SCALAR, | ||||||||||||||
| 134 | }, | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | ); | ||||||||||||||
| 137 | |||||||||||||||
| 138 | # Work out our country | ||||||||||||||
| 139 | 1 | my $country = lc $p{country}; | |||||||||||||
| 140 | 1 | ▶ | if (not defined $country) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 0 | $country = $self->country(); | |||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | ▶ | croak "Unable to determine the correct country" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | unless defined $country; | ||||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | # Work out the date args | ||||||||||||||
| 148 | 1 | my $date_args = $p{date_args}; | |||||||||||||
| 149 | 1 | ▶ | if (not defined $date_args) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 1 | ▶ | $date_args = ref $self ? $self->date_args() : {}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | # Get the canonical name | ||||||||||||||
| 154 | 1 | my $name = _clean_name( $p{name} ); | |||||||||||||
| 155 | |||||||||||||||
| 156 | # Find the month and day for the given name | ||||||||||||||
| 157 | 1 | my $nameday_info = | |||||||||||||
| 158 | $self->_init_nameday_country(namedays => \%namedays, | ||||||||||||||
| 159 | country => $country); | ||||||||||||||
| 160 | croak "Unknown name '$p{name}' for country '$p{country}'" | ||||||||||||||
| 161 | 1 | ▶ | unless exists $nameday_info->{reverse_names}{$name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | 1 | my ($month, $day) = @{ $nameday_info->{reverse_names}{$name} }; | |||||||||||||
| 163 | |||||||||||||||
| 164 | # Build a set of all of the days that the given name is for | ||||||||||||||
| 165 | my $set = DateTime::Set->from_recurrence | ||||||||||||||
| 166 | (next => | ||||||||||||||
| 167 | 0 | ▶ | sub { _make_recurrence($_[0], $nameday_info->{reform_date}, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 168 | 0 | $month, $day, 1, $date_args); | |||||||||||||
| 169 | }, | ||||||||||||||
| 170 | previous => | ||||||||||||||
| 171 | 1 | ▶ | sub { _make_recurrence($_[0], $nameday_info->{reform_date}, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 172 | 1 | $month, $day, -1, $date_args); | |||||||||||||
| 173 | }, | ||||||||||||||
| 174 | 1 | ); | |||||||||||||
| 175 | |||||||||||||||
| 176 | 0 | return $set; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 1 | ▶ | sub _make_recurrence { | ||||||||||||
|
Subroutine
_make_recurrence: called CC=4 SCAR=23.1
| |||||||||||||||
| 180 | 1 | my ($last, $reform_date, $month, $day, $direction, $date_args) = @_; | |||||||||||||
| 181 | |||||||||||||||
| 182 | 1 | my $dt = DateTime::Calendar::Christian->from_object | |||||||||||||
| 183 | (object => $last, | ||||||||||||||
| 184 | reform_date => $reform_date, | ||||||||||||||
| 185 | %$date_args, | ||||||||||||||
| 186 | ); | ||||||||||||||
| 187 | 1 | $dt->truncate(to => 'day'); | |||||||||||||
| 188 | 1 | my $target = $dt->clone(); | |||||||||||||
| 189 | 1 | $target->set( month => $month, | |||||||||||||
| 190 | day => $day, | ||||||||||||||
| 191 | ); | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 0 | ▶ | if ($direction == 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 194 | 0 | ▶ | if ($dt >= $target) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 195 | 0 | $target->add( years => 1); | |||||||||||||
| 196 | } | ||||||||||||||
| 197 | } else { | ||||||||||||||
| 198 | 0 | ▶ | if ($dt <= $target) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 199 | 0 | $target->subtract( years => 1); | |||||||||||||
| 200 | } | ||||||||||||||
| 201 | } | ||||||||||||||
| 202 | |||||||||||||||
| 203 | 0 | $target->set( month => $month, | |||||||||||||
| 204 | day => $day, | ||||||||||||||
| 205 | ); | ||||||||||||||
| 206 | |||||||||||||||
| 207 | 0 | return DateTime->from_object(object => $target, %$date_args); | |||||||||||||
| 208 | } | ||||||||||||||
| 209 | |||||||||||||||
| 210 | 8 | ▶ | sub _init_nameday_country { | ||||||||||||
|
Subroutine
_init_nameday_country: called CC=9 SCAR=22.0
| |||||||||||||||
| 211 | 8 | my $self = shift; | |||||||||||||
| 212 | 8 | my %args = @_; | |||||||||||||
| 213 | 8 | my $country = $args{country}; | |||||||||||||
| 214 | 8 | my $namedays = $args{namedays}; | |||||||||||||
| 215 | |||||||||||||||
| 216 | |||||||||||||||
| 217 | return $namedays->{$country} | ||||||||||||||
| 218 | 8 | ▶ | if exists $namedays->{$country}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | |||||||||||||||
| 220 | # Okay, load the nameday info from the sub item | ||||||||||||||
| 221 | 6 | my $package = "DateTime::Event::NameDay::$country"; | |||||||||||||
| 222 | 6 | eval "require $package;"; | |||||||||||||
| 223 | |||||||||||||||
| 224 | 6 | my ($reform_date, $data) = $package->nameday_data(); | |||||||||||||
| 225 | 6 | my @data = split /\n/, $data; | |||||||||||||
| 226 | 6 | undef $data; | |||||||||||||
| 227 | |||||||||||||||
| 228 | 6 | $namedays->{$country} = | |||||||||||||
| 229 | {reform_date => $reform_date, | ||||||||||||||
| 230 | names => {}, | ||||||||||||||
| 231 | reverse_names => {}, | ||||||||||||||
| 232 | }; | ||||||||||||||
| 233 | 6 | my $forward = $namedays->{$country}{names}; | |||||||||||||
| 234 | 6 | my $reverse = $namedays->{$country}{reverse_names}; | |||||||||||||
| 235 | |||||||||||||||
| 236 | |||||||||||||||
| 237 | # Format of files: | ||||||||||||||
| 238 | # - #s are comments, blank lines are ignored | ||||||||||||||
| 239 | # - leading whitespace and trailing whitespace is ignorred | ||||||||||||||
| 240 | # - months occur by number on their own line followed by a : (i.e. 1:) | ||||||||||||||
| 241 | # - name days are given by number followed by a space then a , separated list of names | ||||||||||||||
| 242 | # surrounding whitespace is trimmed | ||||||||||||||
| 243 | # - names starting with * are immovable holidays | ||||||||||||||
| 244 | # | ||||||||||||||
| 245 | # e.g.: | ||||||||||||||
| 246 | # # Nameday file | ||||||||||||||
| 247 | # # Source: www.whatever.com | ||||||||||||||
| 248 | # | ||||||||||||||
| 249 | # 1: # January | ||||||||||||||
| 250 | # 1 *New Year's Day | ||||||||||||||
| 251 | # 2 Svea | ||||||||||||||
| 252 | # 3 Alfred, Alfrida | ||||||||||||||
| 253 | # ... | ||||||||||||||
| 254 | # | ||||||||||||||
| 255 | # 2: # February | ||||||||||||||
| 256 | # ... | ||||||||||||||
| 257 | |||||||||||||||
| 258 | 6 | my $month = undef; | |||||||||||||
| 259 | 6 | foreach my $line (@data) { | |||||||||||||
| 260 | 2346 | $line =~ s/\s*\#.*//; | |||||||||||||
| 261 | 2346 | ▶ | next if $line =~ /^\s*$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 262 | |||||||||||||||
| 263 | 2254 | ▶ | if ($line =~ /^\s*(\d+)\s+(.*)/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 264 | 2182 | my ($day, $names) = ($1, $2); | |||||||||||||
| 265 | |||||||||||||||
| 266 | # We have a day | ||||||||||||||
| 267 | 2182 | ▶ | croak "Malformed nameday file for '$package': Missing month before line '$line'" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 268 | unless defined $month; | ||||||||||||||
| 269 | |||||||||||||||
| 270 | # Split the names apart and store the forward mapping | ||||||||||||||
| 271 | # For the moment remove the * indicating holidays | ||||||||||||||
| 272 | 2182 | $names =~ s/\s+$//; | |||||||||||||
| 273 | 2182 | my @names = map { s/^*//; $_ } split /\s*,\s*/, $names; | |||||||||||||
| 274 | 2182 | $forward->{$month}{$day} = \@names; | |||||||||||||
| 275 | |||||||||||||||
| 276 | # Store the reverse mapping | ||||||||||||||
| 277 | 2182 | foreach my $name (@names) { | |||||||||||||
| 278 | 3449 | my $n = _clean_name($name); | |||||||||||||
| 279 | croak "Duplicate name '$name' (cleaned '$n')" | ||||||||||||||
| 280 | 3449 | ▶ | if exists $reverse->{$n}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 281 | 3449 | $reverse->{$n} = [$month, $day]; | |||||||||||||
| 282 | } | ||||||||||||||
| 283 | } | ||||||||||||||
| 284 | elsif ($line =~ /^\s*(\d+):\s*$/) { | ||||||||||||||
| 285 | # Change the month | ||||||||||||||
| 286 | 72 | $month = $1; | |||||||||||||
| 287 | } | ||||||||||||||
| 288 | } | ||||||||||||||
| 289 | 6 | @data = (); | |||||||||||||
| 290 | |||||||||||||||
| 291 | 6 | return $namedays->{$country}; | |||||||||||||
| 292 | } | ||||||||||||||
| 293 | |||||||||||||||
| 294 | # Get the canonical sort string for the name... all lowercase | ||||||||||||||
| 295 | # I would like to make it insensitive to accents too, but there | ||||||||||||||
| 296 | # is no good way to do that yet | ||||||||||||||
| 297 | 3450 | ▶ | sub _clean_name { | ||||||||||||
|
Subroutine
_clean_name: called CC=1 SCAR=0.0
| |||||||||||||||
| 298 | 3450 | my $name = shift; | |||||||||||||
| 299 | |||||||||||||||
| 300 | 3450 | $name = lc $name; | |||||||||||||
| 301 | # $name = NFD( lc($name) ); | ||||||||||||||
| 302 | # $name =~ s/\pM//g; | ||||||||||||||
| 303 | |||||||||||||||
| 304 | 3450 | return $name; | |||||||||||||
| 305 | } | ||||||||||||||
| 306 | |||||||||||||||
| 307 | # See if the given country is one we support | ||||||||||||||
| 308 | 10 | ▶ | sub _check_country { | ||||||||||||
|
Subroutine
_check_country: called CC=2 SCAR=6.9
| |||||||||||||||
| 309 | 10 | my $country = lc shift; | |||||||||||||
| 310 | |||||||||||||||
| 311 | # See if we can load the module | ||||||||||||||
| 312 | 10 | my $package = "DateTime::Event::NameDay::$country"; | |||||||||||||
| 313 | 10 | eval "require $package;"; | |||||||||||||
| 314 | |||||||||||||||
| 315 | 10 | ▶ | return $@ ? 0 : 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 316 | } | ||||||||||||||
| 317 | |||||||||||||||
| 318 | 5 | 1; | |||||||||||||
| 319 | |||||||||||||||
| 320 | __END__ | ||||||||||||||