1Schedule::Cron::Events(U3s)er Contributed Perl DocumentatSicohnedule::Cron::Events(3)
2
3
4

NAME

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

VERSION

10       version 1.96
11

SYNOPSIS

13         use Schedule::Cron::Events;
14         my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
15
16         # a crontab line which triggers an event every 5 minutes
17         # initialize the counter with the current time
18         my $cron1 = new Schedule::Cron::Events( '*/5 * * * * /bin/foo', Seconds => time() );
19
20         # or initialize it with a date, for example 09:51:13 on 21st June, 2002
21         my $cron2 = new Schedule::Cron::Events( '*/5 * * * * /bin/foo', Date => [ 13, 51, 9, 21, 5, 102 ] );
22
23         # you could say this too, to use the current time:
24         my $cron = new Schedule::Cron::Events( '*/5 * * * * /bin/foo',  Date => [ ( localtime(time()) )[0..5] ] );
25
26         # find the next execution time
27         my ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
28         printf("Event will start next at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
29
30         # find the following occurrence of the job
31         ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
32         printf("Following event will start at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
33
34         # reset the counter back to the original date given to new()
35         $cron->resetCounter;
36
37         # find out when the job would have last run
38         ($sec, $min, $hour, $day, $month, $year) = $cron->previousEvent;
39         printf("Last event started at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
40
41         # see when the job would have next run at a point in time
42         $cron->setCounterToDate(0, 18, 1, 26, 9, 85); # that's 26th October, 1985
43         ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
44         printf("Event did start at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
45
46         # turn a local date into a Unix time
47         use Time::Local;
48         my $epochSecs = timelocal($sec, $min, $hour, $day, $month, $year);
49         print "...or that can be expressed as " . $epochSecs . " seconds which is " . localtime($epochSecs) . "\n";
50
51       Here is a sample of the output produced by that code:
52
53         # Event will start next at  0:45:00 on 28 Aug, 2002
54         # Following event will start at  0:50:00 on 28 Aug, 2002
55         # Last event started at  0:40:00 on 28 Aug, 2002
56         # Event did start at  1:20:00 on 26 Oct, 1985
57         # ...or that can be expressed as 499134000 seconds which is Sat Oct 26 01:20:00 1985
58
59       Note that results will vary according to your local time and timezone.
60

DESCRIPTION

62       Given a line from a crontab, tells you the time at which cron will next
63       run the line, or when the last event occurred, relative to any date you
64       choose. The object keeps that reference date internally, and updates it
65       when you call nextEvent() or previousEvent() - such that successive
66       calls will give you a sequence of events going forward, or backwards,
67       in time.
68
69       Use setCounterToNow() to reset this reference time to the current date
70       on your system, or use setCounterToDate() to set the reference to any
71       arbitrary time, or resetCounter() to take the object back to the date
72       you constructed it with.
73
74       This module uses Set::Crontab to understand the date specification, so
75       we should be able to handle all forms of cron entries.
76

NAME

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

METHODS

82       In the following, DATE_LIST is a list of 6 values suitable for passing
83       to Time::Local::timelocal() which are the same as the first 6 values
84       returned by the builtin localtime(), namely these 6 numbers in this
85       order
86
87       ·   seconds
88
89           a number 0 .. 59
90
91       ·   minutes
92
93           a number 0 .. 59
94
95       ·   hours
96
97           a number 0 .. 23
98
99       ·   dayOfMonth
100
101           a number 0 .. 31
102
103       ·   month
104
105           a number 0 .. 11 - January is *0*, December is *11*
106
107       ·   year
108
109           the desired year number *minus 1900*
110
111       new( CRONTAB_ENTRY, Seconds => REFERENCE_TIME, Date => [ DATE_LIST ] )
112           Returns a new object for the specified line from the crontab. The
113           first 5 fields of the line are actually parsed by Set::Crontab,
114           which should be able to handle the original crontab(5) ranges as
115           well as Vixie cron ranges and the like. It's up to you to supply a
116           valid line - if you supply a comment line, an environment variable
117           setting line, or a line which does not seem to begin with 5 fields
118           (e.g. a blank line), this method returns undef.
119
120           Give either the Seconds option or the Date option, not both.
121           Supply a six-element array (as described above) to specify the date
122           at which you want to start.  Alternatively, the reference time is
123           the number of seconds since the epoch for the time you want to
124           start looking from.
125
126           If neither of the 'Seconds' and 'Date' options are given we use the
127           current time().
128
129       resetCounter()
130           Resets the object to the state when created (specifically resetting
131           the internal counter to the initial date provided)
132
133       nextEvent()
134           Returns a DATE_LIST for the next event following the current
135           reference time.  Updates the reference time to the time of the
136           event.
137
138       previousEvent()
139           Returns a DATE_LIST for the last event preceding the current
140           reference time.  Updates the reference time to the time of the
141           event.
142
143       setCounterToNow()
144           Sets the reference time to the current time.
145
146       setCounterToDate( DATE_LIST )
147           Sets the reference time to the time given, specified in seconds
148           since the epoch.
149
150       commandLine()
151           Returns the string that is the command to be executed as specified
152           in the crontab - i.e. without the leading date specification.
153

ERROR HANDLING

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

DEPENDENCIES

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

MAINTENANCE

166       Since January 2012 maintained by Petya Kohts (petya.kohts at gmail.com)
167
169       Copyright 2002 P Kent
170
171       This library is free software; you can redistribute it and/or modify it
172       under the same terms as Perl itself.
173

AUTHOR

175       Petya Kent <pause@selsyn.co.uk>
176
178       This software is copyright (c) 2002 by Petya Kent.
179
180       This is free software; you can redistribute it and/or modify it under
181       the same terms as the Perl 5 programming language system itself.
182
183
184
185perl v5.32.0                      2020-07-28         Schedule::Cron::Events(3)
Impressum