This file is indexed.

/usr/share/perl5/Schedule/Cron/Events.pm is in libschedule-cron-events-perl 1.95-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package Schedule::Cron::Events;

use strict;
use Carp 'confess';
use Set::Crontab;
use Time::Local;
use vars qw($VERSION @monthlens);

($VERSION) = ('$Revision: 1.95 $' =~ /([\d\.]+)/ );
@monthlens = ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

## PUBLIC INTERFACE

sub new {
  my $class = shift;
  my $cronline = shift || confess "You must supply a line from a crontab";
  if ($cronline =~ /^\s*#/) { return undef; }
  if ($cronline =~ /^\w+=\S+/) { return undef; }
  if ($cronline !~ /^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+/) { return undef; }

  # https://rt.cpan.org/Ticket/Display.html?id=53899
  $cronline =~ s/^\s+//g;
  
  my %opts = @_;

  #  0    1    2     3     4    5     6     7     8
  # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  #
  #                  $mday is the day of the month
  #
  #                        $mon the month in the range 0..11, with 0 indicating
  #                        January and 11 indicating December.
  #
  #                             $year contains the number of years since 1900. 
  #
  my @date;
  if (exists $opts{'Seconds'}) {
    @date = ( localtime($opts{'Seconds'}) )[0..5];
  } elsif (exists $opts{'Date'}) {
    @date = @{ $opts{'Date'} };

    # input validation
    # https://rt.cpan.org/Ticket/Display.html?id=68393
    if ($date[0] < 0 || $date[0] > 59) {
      confess("Invalid value for seconds [" . $date[0] . "]");
    }
    if ($date[1] < 0 || $date[1] > 59) {
      confess("Invalid value for minutes [" . $date[1] . "]");
    }
    if ($date[2] < 0 || $date[2] > 23) {
      confess("Invalid value for hours [" . $date[2] . "]");
    }
    if ($date[3] < 1 || $date[2] > 31) {
      confess("Invalid value for day of month [" . $date[3] . "]");
    }
    if ($date[4] < 0 || $date[4] > 11) {
      confess("Month must be between 0 (January) and 11 (December), got [" . $date[4] . "]");
    }
    if (time() < 2**31 && $date[5] > 137) {
      confess("Year must be less than 137 (for example, if you need 2013 year, pass 113)");
    }
  } else {
    @date = ( localtime(time()) )[0..5];
  }

  chomp($cronline);
  my %ranges = cronLineToRanges($cronline);

  my $self = {
    'ranges' => \%ranges,
    'e' => [],  # the set of possible months
    '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)
    'g' => [],  # the set of possible hours
    'h' => [],  # the set of possible minutes
    'pa' => 0,  # pointer to which month we are 'currently' on, as far as the counter is concerned 
    'pb' => 0,  # pointer to the day (actually the index of the current element)
    'pc' => 0,  # pointer to the hour
    'pd' => 0,  # pointer to the minute
    'pyear' => 0, # the 'current' year
    'initdate' => \@date,
    'initline' => $cronline,
    'set_e_checked_years' => [],
  };
  
  # now fill the static sets with the sets from Set::Crontab
  @{ $self->{'e'} } = @{ $ranges{'months'} };
  @{ $self->{'g'} } = @{ $ranges{'hours'} };
  @{ $self->{'h'} } = @{ $ranges{'minutes'} };
  bless $self, $class;
  
  $self->resetCounter;
  return $self;
}

sub resetCounter {
  my $self = shift;
  $self->setCounterToDate( @{$self->{'initdate'}} );
}

sub nextEvent {
  my $self = shift || confess "Must be called as a method";
  my @rv = $self->getdate();
  $self->inc_h();
  return @rv;
}

sub previousEvent {
  my $self = shift || confess "Must be called as a method";
  $self->dec_h();
  return $self->getdate();
}

sub setCounterToNow {
  my $self = shift || confess "Must be called as a method";
  $self->setCounterToDate( (localtime(time()))[0..5] );
}

sub setCounterToDate {
  my $self = shift || confess "Must be called as a method";
  unless (@_ == 6) { confess "Must supply a 6-element date list"; }
  
  # add a fudge factor of 60 seconds because cron events happen at the beginning of every minute, at the start of exactly seconds 00
  my ($theMon, $theMday, $theHour, $theMin);
  (undef, $theMin, $theHour, $theMday, $theMon, $self->{'pyear'}) = (localtime(timelocal(@_) + 60))[0..5];
  $theMon++;
  $self->{'pyear'} += 1900;

  # nested ifs... to set the next occurrence time
  my ($exact, $pos) = contains($theMon, @{ $self->{'e'} });
  $self->{'pa'} = $pos;
  if ($exact) {
    $self->set_f();
    ($exact, $pos) = contains($theMday, @{ $self->{'f'} });
    $self->{'pb'} = $pos;
    if ($exact) {
        ($exact, $pos) = contains($theHour, @{ $self->{'g'} });
        $self->{'pc'} = $pos;
        if ($exact) {
          ($exact, $pos) = contains($theMin, @{ $self->{'h'} });
          $self->{'pd'} = $pos;
          if ($pos == -1) { # search wrapped around
            $self->inc_h();
          }
        } else {
          $self->{'pd'} = 0;
          if ($pos == -1) { # search wrapped around
            $self->inc_g();
          }
        }
    } else {
      $self->{'pc'} = 0;
      $self->{'pd'} = 0;
      if ($pos == -1) { # search wrapped around, maybe no valid days this month
        $self->inc_f();
      }
    }
  } else {
    $self->{'pb'} = 0;
    $self->{'pc'} = 0;
    $self->{'pd'} = 0;
    my $rv = $self->set_f();
    if ((! $rv) || ($pos == -1)) {  # search wrapped around or no valid days this month, clock to the next month
      $self->inc_e();
    }
  }

}

sub commandLine {
  my $self = shift || confess "Must be called as a method";
  return $self->{'ranges'}->{'execute'};
}

## Internals only beyond this point

sub cronLineToRanges {
  my $line = shift || confess "Must supply a crontab line";

  my %ranges;
  my @crondate = split(/\s+/, $line, 6);
  if (@crondate < 5) { confess "Could not split the crontab line into enough fields"; }
  
  my $s = new Set::Crontab( $crondate[0], [0..59] );
  $ranges{'minutes'} = [ $s->list() ];
  
  $s = new Set::Crontab( $crondate[1], [0..23] );
  $ranges{'hours'} = [ $s->list() ];
  
  $s = new Set::Crontab( $crondate[2], [0..31] );
  $ranges{'daynums'} = [ $s->list() ];
  if (@{ $ranges{'daynums'} } && ($ranges{'daynums'}->[0] == 0)) {
    shift @{ $ranges{'daynums'} };
  }
  
  $s = new Set::Crontab( $crondate[3], [0..12] );
  $ranges{'months'} = [ $s->list() ];
  if (@{ $ranges{'months'} } && ($ranges{'months'}->[0] == 0)) {
    shift @{ $ranges{'months'} };
  }
  
  $s = new Set::Crontab( $crondate[4], [0..7] );
  $ranges{'weekdays'} = [ $s->list() ];
  if (@{$ranges{'weekdays'}} && $ranges{'weekdays'}->[-1] == 7) {
    pop @{ $ranges{'weekdays'} };
    if ((! @{ $ranges{'weekdays'} }) || $ranges{'weekdays'}->[0] != 0) {
      unshift @{ $ranges{'weekdays'} }, 0;
    }
  }
  
  # emulate cron's logic in determining which days to use
  if ($crondate[2] ne '*' && $crondate[4] ne '*') {
    # leave weekday and daynumber ranges alone, and superimpose
  } elsif ($crondate[2] eq '*' && $crondate[4] ne '*') {
    $ranges{'daynums'} = [];  # restricted by weekday, not daynumber, so only use weekday range
  } elsif ($crondate[4] eq '*' && $crondate[2] ne '*') {
    $ranges{'weekdays'} = []; # restricted by daynumber, so only use daynumber range
  } else {
    $ranges{'weekdays'} = []; # both are '*' so simply use every daynumber
  }
  
  # check that ranges contain things
  foreach (qw(minutes hours months)) {
      unless (@{ $ranges{$_} }) { confess "The $_ range must contain at least one valid value" }
  }
  unless (@{ $ranges{'weekdays'} } || @{ $ranges{'daynums'} }) { confess "The ranges of days (weekdays and monthdays) must contain at least one valid value" }
  
  # sanity checking of ranges here, ensuring they only contain acceptable numbers
  if ($ranges{'minutes'}[-1] > 59) { confess 'minutes only go up to 59'; }
  if ($ranges{'hours'}[-1] > 23) { confess 'hours only go up to 23'; }
  if ($ranges{'months'}[-1] > 12) { confess 'months only go up to 12'; }
  if (@{$ranges{'daynums'}} && $ranges{'daynums'}[-1] > 31) { confess 'daynumber must be 31 or less'; }
  if (@{$ranges{'weekdays'}} && $ranges{'weekdays'}[-1] > 6) { confess 'weekday too large - use 0 to 7'; }

  $ranges{'execute'} = $crondate[5] || '#nothing';
  return %ranges;
}

sub contains {
  my ($val, @set) = @_;
  my $flag = 0;
  while ($flag <= $#set) {
    if ($set[$flag] == $val) {
      TRACE("contains: 1: $set[$flag] == $val");
      return 1, $flag;    
    } elsif ($set[$flag] > $val) {
      TRACE("contains: 0: $set[$flag] > $val");
      return 0, $flag;
    }
    $flag++;
  }
  TRACE("contains: $val not found in <" . join(':', @set) . '>');
  return 0, -1;
}

# return current date on the counter as seconds since epoch
sub getdate {
  my $self = shift || confess "Must be called as a method";
  return (0, $self->{'h'}[$self->{'pd'}], $self->{'g'}[$self->{'pc'}], $self->{'f'}[$self->{'pb'}], $self->{'e'}[$self->{'pa'}]-1, $self->{'pyear'}-1900);
}

# returns a list of days during which next event is possible
sub set_f {
  my $self = shift || confess "Must be called as a method";

  my $flag = _isLeapYear($self->{'pyear'});
  my $monthnum = $self->{'e'}[$self->{'pa'}];
  my $maxday = $monthlens[$monthnum];
  if ($monthnum == 2) { $maxday += $flag; }
  
  my %days = map { $_ => 1 } @{ $self->{'ranges'}{'daynums'} };
  foreach (29, 30, 31) {
    if ($_ > $maxday) { delete $days{$_}; }
  }
  
  # get which weekday is the first of the month
  my $startday = _DayOfWeek($monthnum, 1, $self->{'pyear'});
  # add in, if needed, the selected weekdays
  foreach my $daynum (@{ $self->{'ranges'}{'weekdays'} }) {
    my $offset = $daynum - $startday; # 0 - 6 = -6; start on Saturday, want a Sunday 
    for my $week (1, 8, 15, 22, 29, 36) {
      my $monthday = $week + $offset;
      next if $monthday < 1;
      next if $monthday > $maxday;
      $days{$monthday} = 1;
    }
  }

  @{ $self->{'f'} } = sort { $a <=> $b } keys %days;
  return scalar @{ $self->{'f'} };
}

# routines to increment the counter
sub inc_h {
  my $self = shift || confess "Must be called as a method";
  $self->{'pd'}++;
  if ($self->{'pd'} == 0 || $self->{'pd'} > $#{$self->{'h'}}) {
    $self->{'pd'} = 0;
    $self->inc_g();
  }
}

sub inc_g {
  my $self = shift || confess "Must be called as a method";
  $self->{'pc'}++;
  if ($self->{'pc'} == 0 || $self->{'pc'} > $#{$self->{'g'}}) {
    $self->{'pc'} = 0;
    $self->inc_f();
  }
}

sub inc_f {
  my $self = shift || confess "Must be called as a method";
  $self->{'pb'}++;
  if ($self->{'pb'} == 0 || $self->{'pb'} > $#{$self->{'f'}}) {
    $self->{'pb'} = 0;
    $self->inc_e();
  }
}

# increments currents month, skips to the next year
# when no months left during the current year
#
# if there're no possible months during 5 years in a row, bails out:
# https://rt.cpan.org/Public/Bug/Display.html?id=109246
#
sub inc_e {
  my $self = shift || confess "Must be called as a method";
  $self->{'pa'}++;
  if ($self->{'pa'} == 0 || $self->{'pa'} > $#{$self->{'e'}}) {
    $self->{'pa'} = 0;
    $self->{'pyear'}++;

    # https://rt.cpan.org/Public/Bug/Display.html?id=109246
    push (@{$self->{'set_e_checked_years'}}, $self->{'pyear'});
    if (scalar(@{$self->{'set_e_checked_years'}} > 5)) {
      confess("Cron line [" . $self->{'initline'} . "] does not define any valid point in time, checked years: [" .
          join(",", @{$self->{'set_e_checked_years'}}) .
          "] (ex, 31th of February) ");
    }
  }
  my $rv = $self->set_f();
  unless ($rv) { ###
    $self->inc_e;
  }

  $self->{'set_e_checked_years'} = [];
}


# and to decrement it
sub dec_h {
  my $self = shift || confess "Must be called as a method";
  $self->{'pd'}--;
  if ($self->{'pd'} == -1) {
    $self->{'pd'} = $#{$self->{'h'}};
    $self->dec_g();
  }
}

sub dec_g {
  my $self = shift || confess "Must be called as a method";
  $self->{'pc'}--;
  if ($self->{'pc'} == -1) {
    $self->{'pc'} = $#{$self->{'g'}};
    $self->dec_f();
  }
}

sub dec_f {
  my $self = shift || confess "Must be called as a method";
  $self->{'pb'}--;
  if ($self->{'pb'} == -1) {
    $self->{'pb'} = $#{$self->{'f'}};
    $self->dec_e();
  }
}

sub dec_e {
  my $self = shift || confess "Must be called as a method";
  $self->{'pa'}--;
  if ($self->{'pa'} == -1) {
    $self->{'pa'} = $#{$self->{'e'}};
    $self->{'pyear'}--;
  }
  my $rv = $self->set_f();
  unless ($rv) { ###
    $self->dec_e;
  }
  $self->{'pb'} = $#{$self->{'f'}};
}

# These two routines courtesy of B Paulsen
sub _isLeapYear {
  my $year = shift;
  return  !($year % 400) || ( !( $year % 4 ) && ( $year % 100 ) ) ? 1 : 0;
}

sub _DayOfWeek {
  my ( $month, $day, $year ) = @_;
  
  my $flag = _isLeapYear( $year );
  my @months = (
    $flag ? 0 : 1,
    $flag ? 3 : 4,
    4, 0, 2, 5, 0, 3, 6, 1, 4, 6 );
  my @century = ( 4, 2, 0, 6 );
  
  my $dow = $year % 100;
  $dow += int( $dow / 4 );
  $dow += $day + $months[$month-1];
  $dow += $century[ ( int($year/100) - 1 ) % 4 ];
  
  return ($dow-1) % 7;
}

sub TRACE {}

1;

__END__

=pod

=head1 NAME

Schedule::Cron::Events - take a line from a crontab and find out when events will occur

=head1 SYNOPSIS

  use Schedule::Cron::Events;
  my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

  # a crontab line which triggers an event every 5 minutes
  # initialize the counter with the current time
  my $cron1 = new Schedule::Cron::Events( '*/5 * * * * /bin/foo', Seconds => time() );

  # or initialize it with a date, for example 09:51:13 on 21st June, 2002
  my $cron2 = new Schedule::Cron::Events( '*/5 * * * * /bin/foo', Date => [ 13, 51, 9, 21, 5, 102 ] );

  # you could say this too, to use the current time:
  my $cron = new Schedule::Cron::Events( '*/5 * * * * /bin/foo',  Date => [ ( localtime(time()) )[0..5] ] );

  # find the next execution time
  my ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
  printf("Event will start next at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));

  # find the following occurrence of the job
  ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
  printf("Following event will start at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));

  # reset the counter back to the original date given to new()
  $cron->resetCounter;

  # find out when the job would have last run
  ($sec, $min, $hour, $day, $month, $year) = $cron->previousEvent;
  printf("Last event started at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));

  # see when the job would have next run at a point in time
  $cron->setCounterToDate(0, 18, 1, 26, 9, 85); # that's 26th October, 1985
  ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
  printf("Event did start at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));

  # turn a local date into a Unix time
  use Time::Local;
  my $epochSecs = timelocal($sec, $min, $hour, $day, $month, $year);
  print "...or that can be expressed as " . $epochSecs . " seconds which is " . localtime($epochSecs) . "\n";

Here is a sample of the output produced by that code:

  Event will start next at  0:45:00 on 28 Aug, 2002
  Following event will start at  0:50:00 on 28 Aug, 2002
  Last event started at  0:40:00 on 28 Aug, 2002
  Event did start at  1:20:00 on 26 Oct, 1985
  ...or that can be expressed as 499134000 seconds which is Sat Oct 26 01:20:00 1985

Note that results will vary according to your local time and timezone.

=head1 DESCRIPTION

Given a line from a crontab, tells you the time at which cron will next run the line, or when the last event
occurred, relative to any date you choose. The object keeps that reference date internally, 
and updates it when you call nextEvent() or previousEvent() - such that successive calls will
give you a sequence of events going forward, or backwards, in time.

Use setCounterToNow() to reset this reference time to the current date on your system,
or use setCounterToDate() to set the reference to any arbitrary time, or resetCounter()
to take the object back to the date you constructed it with.

This module uses Set::Crontab to understand the date specification, so we should be able to handle all
forms of cron entries.

=head1 METHODS

In the following, DATE_LIST is a list of 6 values suitable for passing to Time::Local::timelocal()
which are the same as the first 6 values returned by the builtin localtime(), namely 
these 6 numbers in this order

=over 4

=item * seconds

a number 0 .. 59

=item * minutes

a number 0 .. 59

=item * hours

a number 0 .. 23

=item * dayOfMonth

a number 0 .. 31

=item * month

a number 0 .. 11 - January is *0*, December is *11*

=item * year

the desired year number *minus 1900*

=back

=over 4

=item new( CRONTAB_ENTRY, Seconds => REFERENCE_TIME, Date => [ DATE_LIST ] )

Returns a new object for the specified line from the crontab. The first 5 fields of the line
are actually parsed by Set::Crontab, which should be able to handle the original crontab(5) ranges
as well as Vixie cron ranges and the like. It's up to you to supply a valid line - if you supply
a comment line, an environment variable setting line, or a line which does not seem to begin with
5 fields (e.g. a blank line), this method returns undef.

Give either the Seconds option or the Date option, not both.
Supply a six-element array (as described above) to specify the date at which you want to start.
Alternatively, the reference time is the number of seconds since the epoch for the time you want to 
start looking from.

If neither of the 'Seconds' and 'Date' options are given we use the current time().

=item resetCounter()

Resets the object to the state when created (specifically resetting the internal counter to the 
initial date provided)

=item nextEvent()

Returns a DATE_LIST for the next event following the current reference time.
Updates the reference time to the time of the event.

=item previousEvent()

Returns a DATE_LIST for the last event preceding the current reference time.
Updates the reference time to the time of the event.

=item setCounterToNow()

Sets the reference time to the current time.

=item setCounterToDate( DATE_LIST )

Sets the reference time to the time given, specified in seconds since the epoch.

=item commandLine()

Returns the string that is the command to be executed as specified in the crontab - i.e. without the leading
date specification.

=back

=head1 ERROR HANDLING

If something goes wrong the general approach is to raise a fatal error with confess() so use
eval {} to trap these errors. If you supply a comment line to the constructor then you'll simply get back
undef, not a fatal error. If you supply a line like 'foo bar */15 baz qux /bin/false' you'll get a confess().

=head1 DEPENDENCIES

Set::Crontab, Time::Local, Carp. Date::Manip is no longer required thanks to B Paulsen.

=head1 MAINTENANCE

Since January 2012 maintained by Petya Kohts (petya.kohts at gmail.com)

=head1 COPYRIGHT

Copyright 2002 P Kent

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 

=cut