1Schedule::Cron::Events(U3s)er Contributed Perl DocumentatSicohnedule::Cron::Events(3)
2
3
4
6 Schedule::Cron::Events - take a line from a crontab and find out when
7 events will occur
8
10 use Schedule::Cron::Events;
11 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
12
13 # a crontab line which triggers an event every 5 minutes
14 # initialize the counter with the current time
15 my $cron1 = new Schedule::Cron::Events( '*/5 * * * * /bin/foo', Seconds => time() );
16
17 # or initialize it with a date, for example 09:51:13 on 21st June, 2002
18 my $cron2 = new Schedule::Cron::Events( '*/5 * * * * /bin/foo', Date => [ 13, 51, 9, 21, 5, 102 ] );
19
20 # you could say this too, to use the current time:
21 my $cron = new Schedule::Cron::Events( '*/5 * * * * /bin/foo', Date => [ ( localtime(time()) )[0..5] ] );
22
23 # find the next execution time
24 my ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
25 printf("Event will start next at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
26
27 # find the following occurrence of the job
28 ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
29 printf("Following event will start at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
30
31 # reset the counter back to the original date given to new()
32 $cron->resetCounter;
33
34 # find out when the job would have last run
35 ($sec, $min, $hour, $day, $month, $year) = $cron->previousEvent;
36 printf("Last event started at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
37
38 # see when the job would have next run at a point in time
39 $cron->setCounterToDate(0, 18, 1, 26, 9, 85); # that's 26th October, 1985
40 ($sec, $min, $hour, $day, $month, $year) = $cron->nextEvent;
41 printf("Event did start at %2d:%02d:%02d on %d %s, %d\n", $hour, $min, $sec, $day, $mon[$month], ($year+1900));
42
43 # turn a local date into a Unix time
44 use Time::Local;
45 my $epochSecs = timelocal($sec, $min, $hour, $day, $month, $year);
46 print "...or that can be expressed as " . $epochSecs . " seconds which is " . localtime($epochSecs) . "\n";
47
48 Here is a sample of the output produced by that code:
49
50 Event will start next at 0:45:00 on 28 Aug, 2002
51 Following event will start at 0:50:00 on 28 Aug, 2002
52 Last event started at 0:40:00 on 28 Aug, 2002
53 Event did start at 1:20:00 on 26 Oct, 1985
54 ...or that can be expressed as 499134000 seconds which is Sat Oct 26 01:20:00 1985
55
56 Note that results will vary according to your local time and timezone.
57
59 Given a line from a crontab, tells you the time at which cron will next
60 run the line, or when the last event occurred, relative to any date you
61 choose. The object keeps that reference date internally, and updates it
62 when you call nextEvent() or previousEvent() - such that successive
63 calls will give you a sequence of events going forward, or backwards,
64 in time.
65
66 Use setCounterToNow() to reset this reference time to the current date
67 on your system, or use setCounterToDate() to set the reference to any
68 arbitrary time, or resetCounter() to take the object back to the date
69 you constructed it with.
70
71 This module uses Set::Crontab to understand the date specification, so
72 we should be able to handle all forms of cron entries.
73
75 In the following, DATE_LIST is a list of 6 values suitable for passing
76 to Time::Local::timelocal() which are the same as the first 6 values
77 returned by the builtin localtime(), namely these 6 numbers in this
78 order
79
80 · seconds
81
82 a number 0 .. 59
83
84 · minutes
85
86 a number 0 .. 59
87
88 · hours
89
90 a number 0 .. 23
91
92 · dayOfMonth
93
94 a number 0 .. 31
95
96 · month
97
98 a number 0 .. 11 - January is *0*, December is *11*
99
100 · year
101
102 the desired year number *minus 1900*
103
104 new( CRONTAB_ENTRY, Seconds => REFERENCE_TIME, Date => [ DATE_LIST ] )
105 Returns a new object for the specified line from the crontab. The
106 first 5 fields of the line are actually parsed by Set::Crontab,
107 which should be able to handle the original crontab(5) ranges
108 aswell as Vixie cron ranges and the like. It's up to you to supply
109 a valid line - if you supply a comment line, an environment
110 variable setting line, or a line which does not seem to begin with
111 5 fields (e.g. a blank line), this method returns undef.
112
113 Give either the Seconds option or the Date option, not both.
114 Supply a six-element array (as described above) to specify the date
115 at which you want to start. Alternatively, the reference time is
116 the number of seconds since the epoch for the time you want to
117 start looking from.
118
119 If neither of the 'Seconds' and 'Date' options are given we use the
120 current time().
121
122 resetCounter()
123 Resets the object to the state when created (specifically resetting
124 the internal counter to the initial date provided)
125
126 nextEvent()
127 Returns a DATE_LIST for the next event following the current
128 reference time. Updates the reference time to the time of the
129 event.
130
131 previousEvent()
132 Returns a DATE_LIST for the last event preceding the current
133 reference time. Updates the reference time to the time of the
134 event.
135
136 setCounterToNow()
137 Sets the reference time to the current time.
138
139 setCounterToDate( DATE_LIST )
140 Sets the reference time to the time given, specified in seconds
141 since the epoch.
142
143 commandLine()
144 Returns the string that is the command to be executed as specified
145 in the crontab - i.e. without the leading date specification.
146
148 If something goes wrong the general approach is to raise a fatal error
149 with confess() so use eval {} to trap these errors. If you supply a
150 comment line to the constructor then you'll simply get back undef, not
151 a fatal error. If you supply a line like 'foo bar */15 baz qux
152 /bin/false' you'll get a confess().
153
155 Set::Crontab, Time::Local, Carp. Date::Manip is no longer required
156 thanks to B Paulsen.
157
159 Copyright 2002 P Kent
160
161 This library is free software; you can redistribute it and/or modify it
162 under the same terms as Perl itself.
163
164
165
166perl v5.12.0 2003-03-05 Schedule::Cron::Events(3)