| 1 | package Schedule::Cron::Events; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp 'confess'; | ||
| 5 | use Set::Crontab; | ||
| 6 | use Time::Local; | ||
| 7 | use vars qw($VERSION @monthlens); | ||
| 8 | |||
| 9 | ($VERSION) = ('$Revision: 1.95 $' =~ /([\d\.]+)/ ); | ||
| 10 | @monthlens = ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); | ||
| 11 | |||
| 12 | ## PUBLIC INTERFACE | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my $cronline = shift || confess "You must supply a line from a crontab"; | ||
| 17 | if ($cronline =~ /^\s*#/) { return undef; } | ||
| 18 | if ($cronline =~ /^\w+=\S+/) { return undef; } | ||
| 19 | if ($cronline !~ /^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+/) { return undef; } | ||
| 20 | |||
| 21 | # https://rt.cpan.org/Ticket/Display.html?id=53899 | ||
| 22 | $cronline =~ s/^\s+//g; | ||
| 23 | |||
| 24 | my %opts = @_; | ||
| 25 | |||
| 26 | # 0 1 2 3 4 5 6 7 8 | ||
| 27 | # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); | ||
| 28 | # | ||
| 29 | # $mday is the day of the month | ||
| 30 | # | ||
| 31 | # $mon the month in the range 0..11, with 0 indicating | ||
| 32 | # January and 11 indicating December. | ||
| 33 | # | ||
| 34 | # $year contains the number of years since 1900. | ||
| 35 | # | ||
| 36 | my @date; | ||
| 37 | if (exists $opts{'Seconds'}) { | ||
| 38 | @date = ( localtime($opts{'Seconds'}) )[0..5]; | ||
| 39 | } elsif (exists $opts{'Date'}) { | ||
| 40 | @date = @{ $opts{'Date'} }; | ||
| 41 | |||
| 42 | # input validation | ||
| 43 | # https://rt.cpan.org/Ticket/Display.html?id=68393 | ||
| 44 | if ($date[0] < 0 || $date[0] > 59) { | ||
| 45 | confess("Invalid value for seconds [" . $date[0] . "]"); | ||
| 46 | } | ||
| 47 | if ($date[1] < 0 || $date[1] > 59) { | ||
| 48 | confess("Invalid value for minutes [" . $date[1] . "]"); | ||
| 49 | } | ||
| 50 | if ($date[2] < 0 || $date[2] > 23) { | ||
| 51 | confess("Invalid value for hours [" . $date[2] . "]"); | ||
| 52 | } | ||
| 53 | if ($date[3] < 1 || $date[2] > 31) { | ||
| 54 | confess("Invalid value for day of month [" . $date[3] . "]"); | ||
| 55 | } | ||
| 56 | if ($date[4] < 0 || $date[4] > 11) { | ||
| 57 | confess("Month must be between 0 (January) and 11 (December), got [" . $date[4] . "]"); | ||
| 58 | } | ||
| 59 | if (time() < 2**31 && $date[5] > 137) { | ||
| 60 | confess("Year must be less than 137 (for example, if you need 2013 year, pass 113)"); | ||
| 61 | } | ||
| 62 | } else { | ||
| 63 | @date = ( localtime(time()) )[0..5]; | ||
| 64 | } | ||
| 65 | |||
| 66 | chomp($cronline); | ||
| 67 | my %ranges = cronLineToRanges($cronline); | ||
| 68 | |||
| 69 | my $self = { | ||
| 70 | 'ranges' => \%ranges, | ||
| 71 | 'e' => [], # the set of possible months | ||
| 72 | 'f' => [], # the set of possible days of the month, which varies (e.g. to omit day 31 in June, or to specify days by weekday) | ||
| 73 | 'g' => [], # the set of possible hours | ||
| 74 | 'h' => [], # the set of possible minutes | ||
| 75 | 'pa' => 0, # pointer to which month we are 'currently' on, as far as the counter is concerned | ||
| 76 | 'pb' => 0, # pointer to the day (actually the index of the current element) | ||
| 77 | 'pc' => 0, # pointer to the hour | ||
| 78 | 'pd' => 0, # pointer to the minute | ||
| 79 | 'pyear' => 0, # the 'current' year | ||
| 80 | 'initdate' => \@date, | ||
| 81 | 'initline' => $cronline, | ||
| 82 | 'set_e_checked_years' => [], | ||
| 83 | }; | ||
| 84 | |||
| 85 | # now fill the static sets with the sets from Set::Crontab | ||
| 86 | @{ $self->{'e'} } = @{ $ranges{'months'} }; | ||
| 87 | @{ $self->{'g'} } = @{ $ranges{'hours'} }; | ||
| 88 | @{ $self->{'h'} } = @{ $ranges{'minutes'} }; | ||
| 89 | bless $self, $class; | ||
| 90 | |||
| 91 | $self->resetCounter; | ||
| 92 | return $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub resetCounter { | ||
| 96 | my $self = shift; | ||
| 97 | $self->setCounterToDate( @{$self->{'initdate'}} ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub nextEvent { | ||
| 101 | my $self = shift || confess "Must be called as a method"; | ||
| 102 | my @rv = $self->getdate(); | ||
| 103 | $self->inc_h(); | ||
| 104 | return @rv; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub previousEvent { | ||
| 108 | my $self = shift || confess "Must be called as a method"; | ||
| 109 | $self->dec_h(); | ||
| 110 | return $self->getdate(); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub setCounterToNow { | ||
| 114 | my $self = shift || confess "Must be called as a method"; | ||
| 115 | $self->setCounterToDate( (localtime(time()))[0..5] ); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub setCounterToDate { | ||
| 119 | my $self = shift || confess "Must be called as a method"; | ||
| 120 | unless (@_ == 6) { confess "Must supply a 6-element date list"; } | ||
| 121 | |||
| 122 | # add a fudge factor of 60 seconds because cron events happen at the beginning of every minute, at the start of exactly seconds 00 | ||
| 123 | my ($theMon, $theMday, $theHour, $theMin); | ||
| 124 | (undef, $theMin, $theHour, $theMday, $theMon, $self->{'pyear'}) = (localtime(timelocal(@_) + 60))[0..5]; | ||
| 125 | $theMon++; | ||
| 126 | $self->{'pyear'} += 1900; | ||
| 127 | |||
| 128 | # nested ifs... to set the next occurrence time | ||
| 129 | my ($exact, $pos) = contains($theMon, @{ $self->{'e'} }); | ||
| 130 | $self->{'pa'} = $pos; | ||
| 131 | if ($exact) { | ||
| 132 | $self->set_f(); | ||
| 133 | ($exact, $pos) = contains($theMday, @{ $self->{'f'} }); | ||
| 134 | $self->{'pb'} = $pos; | ||
| 135 | if ($exact) { | ||
| 136 | ($exact, $pos) = contains($theHour, @{ $self->{'g'} }); | ||
| 137 | $self->{'pc'} = $pos; | ||
| 138 | if ($exact) { | ||
| 139 | ($exact, $pos) = contains($theMin, @{ $self->{'h'} }); | ||
| 140 | $self->{'pd'} = $pos; | ||
| 141 | if ($pos == -1) { # search wrapped around | ||
| 142 | $self->inc_h(); | ||
| 143 | } | ||
| 144 | } else { | ||
| 145 | $self->{'pd'} = 0; | ||
| 146 | if ($pos == -1) { # search wrapped around | ||
| 147 | $self->inc_g(); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } else { | ||
| 151 | $self->{'pc'} = 0; | ||
| 152 | $self->{'pd'} = 0; | ||
| 153 | if ($pos == -1) { # search wrapped around, maybe no valid days this month | ||
| 154 | $self->inc_f(); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } else { | ||
| 158 | $self->{'pb'} = 0; | ||
| 159 | $self->{'pc'} = 0; | ||
| 160 | $self->{'pd'} = 0; | ||
| 161 | my $rv = $self->set_f(); | ||
| 162 | if ((! $rv) || ($pos == -1)) { # search wrapped around or no valid days this month, clock to the next month | ||
| 163 | $self->inc_e(); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | sub commandLine { | ||
| 170 | my $self = shift || confess "Must be called as a method"; | ||
| 171 | return $self->{'ranges'}->{'execute'}; | ||
| 172 | } | ||
| 173 | |||
| 174 | ## Internals only beyond this point | ||
| 175 | |||
| 176 | sub cronLineToRanges { | ||
| 177 | my $line = shift || confess "Must supply a crontab line"; | ||
| 178 | |||
| 179 | my %ranges; | ||
| 180 | my @crondate = split(/\s+/, $line, 6); | ||
| 181 | if (@crondate < 5) { confess "Could not split the crontab line into enough fields"; } | ||
| 182 | |||
| 183 | my $s = new Set::Crontab( $crondate[0], [0..59] ); | ||
| 184 | $ranges{'minutes'} = [ $s->list() ]; | ||
| 185 | |||
| 186 | $s = new Set::Crontab( $crondate[1], [0..23] ); | ||
| 187 | $ranges{'hours'} = [ $s->list() ]; | ||
| 188 | |||
| 189 | $s = new Set::Crontab( $crondate[2], [0..31] ); | ||
| 190 | $ranges{'daynums'} = [ $s->list() ]; | ||
| 191 | if (@{ $ranges{'daynums'} } && ($ranges{'daynums'}->[0] == 0)) { | ||
| 192 | shift @{ $ranges{'daynums'} }; | ||
| 193 | } | ||
| 194 | |||
| 195 | $s = new Set::Crontab( $crondate[3], [0..12] ); | ||
| 196 | $ranges{'months'} = [ $s->list() ]; | ||
| 197 | if (@{ $ranges{'months'} } && ($ranges{'months'}->[0] == 0)) { | ||
| 198 | shift @{ $ranges{'months'} }; | ||
| 199 | } | ||
| 200 | |||
| 201 | $s = new Set::Crontab( $crondate[4], [0..7] ); | ||
| 202 | $ranges{'weekdays'} = [ $s->list() ]; | ||
| 203 | if (@{$ranges{'weekdays'}} && $ranges{'weekdays'}->[-1] == 7) { | ||
| 204 | pop @{ $ranges{'weekdays'} }; | ||
| 205 | if ((! @{ $ranges{'weekdays'} }) || $ranges{'weekdays'}->[0] != 0) { | ||
| 206 | unshift @{ $ranges{'weekdays'} }, 0; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | # emulate cron's logic in determining which days to use | ||
| 211 | if ($crondate[2] ne '*' && $crondate[4] ne '*') { | ||
| 212 | # leave weekday and daynumber ranges alone, and superimpose | ||
| 213 | } elsif ($crondate[2] eq '*' && $crondate[4] ne '*') { | ||
| 214 | $ranges{'daynums'} = []; # restricted by weekday, not daynumber, so only use weekday range | ||
| 215 | } elsif ($crondate[4] eq '*' && $crondate[2] ne '*') { | ||
| 216 | $ranges{'weekdays'} = []; # restricted by daynumber, so only use daynumber range | ||
| 217 | } else { | ||
| 218 | $ranges{'weekdays'} = []; # both are '*' so simply use every daynumber | ||
| 219 | } | ||
| 220 | |||
| 221 | # check that ranges contain things | ||
| 222 | foreach (qw(minutes hours months)) { | ||
| 223 | unless (@{ $ranges{$_} }) { confess "The $_ range must contain at least one valid value" } | ||
| 224 | } | ||
| 225 | unless (@{ $ranges{'weekdays'} } || @{ $ranges{'daynums'} }) { confess "The ranges of days (weekdays and monthdays) must contain at least one valid value" } | ||
| 226 | |||
| 227 | # sanity checking of ranges here, ensuring they only contain acceptable numbers | ||
| 228 | if ($ranges{'minutes'}[-1] > 59) { confess 'minutes only go up to 59'; } | ||
| 229 | if ($ranges{'hours'}[-1] > 23) { confess 'hours only go up to 23'; } | ||
| 230 | if ($ranges{'months'}[-1] > 12) { confess 'months only go up to 12'; } | ||
| 231 | if (@{$ranges{'daynums'}} && $ranges{'daynums'}[-1] > 31) { confess 'daynumber must be 31 or less'; } | ||
| 232 | if (@{$ranges{'weekdays'}} && $ranges{'weekdays'}[-1] > 6) { confess 'weekday too large - use 0 to 7'; } | ||
| 233 | |||
| 234 | $ranges{'execute'} = $crondate[5] || '#nothing'; | ||
| 235 | return %ranges; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub contains { | ||
| 239 | my ($val, @set) = @_; | ||
| 240 | my $flag = 0; | ||
| 241 | while ($flag <= $#set) { | ||
| 242 | if ($set[$flag] == $val) { | ||
| 243 | TRACE("contains: 1: $set[$flag] == $val"); | ||
| 244 | return 1, $flag; | ||
| 245 | } elsif ($set[$flag] > $val) { | ||
| 246 | TRACE("contains: 0: $set[$flag] > $val"); | ||
| 247 | return 0, $flag; | ||
| 248 | } | ||
| 249 | $flag++; | ||
| 250 | } | ||
| 251 | TRACE("contains: $val not found in <" . join(':', @set) . '>'); | ||
| 252 | return 0, -1; | ||
| 253 | } | ||
| 254 | |||
| 255 | # return current date on the counter as seconds since epoch | ||
| 256 | sub getdate { | ||
| 257 | my $self = shift || confess "Must be called as a method"; | ||
| 258 | return (0, $self->{'h'}[$self->{'pd'}], $self->{'g'}[$self->{'pc'}], $self->{'f'}[$self->{'pb'}], $self->{'e'}[$self->{'pa'}]-1, $self->{'pyear'}-1900); | ||
| 259 | } | ||
| 260 | |||
| 261 | # returns a list of days during which next event is possible | ||
| 262 | sub set_f { | ||
| 263 | my $self = shift || confess "Must be called as a method"; | ||
| 264 | |||
| 265 | my $flag = _isLeapYear($self->{'pyear'}); | ||
| 266 | my $monthnum = $self->{'e'}[$self->{'pa'}]; | ||
| 267 | my $maxday = $monthlens[$monthnum]; | ||
| 268 | if ($monthnum == 2) { $maxday += $flag; } | ||
| 269 | |||
| 270 | my %days = map { $_ => 1 } @{ $self->{'ranges'}{'daynums'} }; | ||
| 271 | foreach (29, 30, 31) { | ||
| 272 | if ($_ > $maxday) { delete $days{$_}; } | ||
| 273 | } | ||
| 274 | |||
| 275 | # get which weekday is the first of the month | ||
| 276 | my $startday = _DayOfWeek($monthnum, 1, $self->{'pyear'}); | ||
| 277 | # add in, if needed, the selected weekdays | ||
| 278 | foreach my $daynum (@{ $self->{'ranges'}{'weekdays'} }) { | ||
| 279 | my $offset = $daynum - $startday; # 0 - 6 = -6; start on Saturday, want a Sunday | ||
| 280 | for my $week (1, 8, 15, 22, 29, 36) { | ||
| 281 | my $monthday = $week + $offset; | ||
| 282 | next if $monthday < 1; | ||
| 283 | next if $monthday > $maxday; | ||
| 284 | $days{$monthday} = 1; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | @{ $self->{'f'} } = sort { $a <=> $b } keys %days; | ||
| 289 | return scalar @{ $self->{'f'} }; | ||
| 290 | } | ||
| 291 | |||
| 292 | # routines to increment the counter | ||
| 293 | sub inc_h { | ||
| 294 | my $self = shift || confess "Must be called as a method"; | ||
| 295 | $self->{'pd'}++; | ||
| 296 | if ($self->{'pd'} == 0 || $self->{'pd'} > $#{$self->{'h'}}) { | ||
| 297 | $self->{'pd'} = 0; | ||
| 298 | $self->inc_g(); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | sub inc_g { | ||
| 303 | my $self = shift || confess "Must be called as a method"; | ||
| 304 | $self->{'pc'}++; | ||
| 305 | if ($self->{'pc'} == 0 || $self->{'pc'} > $#{$self->{'g'}}) { | ||
| 306 | $self->{'pc'} = 0; | ||
| 307 | $self->inc_f(); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | sub inc_f { | ||
| 312 | my $self = shift || confess "Must be called as a method"; | ||
| 313 | $self->{'pb'}++; | ||
| 314 | if ($self->{'pb'} == 0 || $self->{'pb'} > $#{$self->{'f'}}) { | ||
| 315 | $self->{'pb'} = 0; | ||
| 316 | $self->inc_e(); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | # increments currents month, skips to the next year | ||
| 321 | # when no months left during the current year | ||
| 322 | # | ||
| 323 | # if there're no possible months during 5 years in a row, bails out: | ||
| 324 | # https://rt.cpan.org/Public/Bug/Display.html?id=109246 | ||
| 325 | # | ||
| 326 | sub inc_e { | ||
| 327 | my $self = shift || confess "Must be called as a method"; | ||
| 328 | $self->{'pa'}++; | ||
| 329 | if ($self->{'pa'} == 0 || $self->{'pa'} > $#{$self->{'e'}}) { | ||
| 330 | $self->{'pa'} = 0; | ||
| 331 | $self->{'pyear'}++; | ||
| 332 | |||
| 333 | # https://rt.cpan.org/Public/Bug/Display.html?id=109246 | ||
| 334 | push (@{$self->{'set_e_checked_years'}}, $self->{'pyear'}); | ||
| 335 | if (scalar(@{$self->{'set_e_checked_years'}} > 5)) { | ||
| 336 | confess("Cron line [" . $self->{'initline'} . "] does not define any valid point in time, checked years: [" . | ||
| 337 | join(",", @{$self->{'set_e_checked_years'}}) . | ||
| 338 | "] (ex, 31th of February) "); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | my $rv = $self->set_f(); | ||
| 342 | unless ($rv) { ### | ||
| 343 | $self->inc_e; | ||
| 344 | } | ||
| 345 | |||
| 346 | $self->{'set_e_checked_years'} = []; | ||
| 347 | } | ||
| 348 | |||
| 349 | |||
| 350 | # and to decrement it | ||
| 351 | sub dec_h { | ||
| 352 | my $self = shift || confess "Must be called as a method"; | ||
| 353 | $self->{'pd'}--; | ||
| 354 | if ($self->{'pd'} == -1) { | ||
| 355 | $self->{'pd'} = $#{$self->{'h'}}; | ||
| 356 | $self->dec_g(); | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | sub dec_g { | ||
| 361 | my $self = shift || confess "Must be called as a method"; | ||
| 362 | $self->{'pc'}--; | ||
| 363 | if ($self->{'pc'} == -1) { | ||
| 364 | $self->{'pc'} = $#{$self->{'g'}}; | ||
| 365 | $self->dec_f(); | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | sub dec_f { | ||
| 370 | my $self = shift || confess "Must be called as a method"; | ||
| 371 | $self->{'pb'}--; | ||
| 372 | if ($self->{'pb'} == -1) { | ||
| 373 | $self->{'pb'} = $#{$self->{'f'}}; | ||
| 374 | $self->dec_e(); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | sub dec_e { | ||
| 379 | my $self = shift || confess "Must be called as a method"; | ||
| 380 | $self->{'pa'}--; | ||
| 381 | if ($self->{'pa'} == -1) { | ||
| 382 | $self->{'pa'} = $#{$self->{'e'}}; | ||
| 383 | $self->{'pyear'}--; | ||
| 384 | } | ||
| 385 | my $rv = $self->set_f(); | ||
| 386 | unless ($rv) { ### | ||
| 387 | $self->dec_e; | ||
| 388 | } | ||
| 389 | $self->{'pb'} = $#{$self->{'f'}}; | ||
| 390 | } | ||
| 391 | |||
| 392 | # These two routines courtesy of B Paulsen | ||
| 393 | sub _isLeapYear { | ||
| 394 | my $year = shift; | ||
| 395 | return !($year % 400) || ( !( $year % 4 ) && ( $year % 100 ) ) ? 1 : 0; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub _DayOfWeek { | ||
| 399 | my ( $month, $day, $year ) = @_; | ||
| 400 | |||
| 401 | my $flag = _isLeapYear( $year ); | ||
| 402 | my @months = ( | ||
| 403 | $flag ? 0 : 1, | ||
| 404 | $flag ? 3 : 4, | ||
| 405 | 4, 0, 2, 5, 0, 3, 6, 1, 4, 6 ); | ||
| 406 | my @century = ( 4, 2, 0, 6 ); | ||
| 407 | |||
| 408 | my $dow = $year % 100; | ||
| 409 | $dow += int( $dow / 4 ); | ||
| 410 | $dow += $day + $months[$month-1]; | ||
| 411 | $dow += $century[ ( int($year/100) - 1 ) % 4 ]; | ||
| 412 | |||
| 413 | return ($dow-1) % 7; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub TRACE {} | ||
| 417 | |||
| 418 | 1; | ||
| 419 | |||
| 420 | __END__ |