|
NAMESchedule::Cron::Events - take a line from a crontab and find out when events will occurSYNOPSISuse 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. DESCRIPTIONGiven 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. METHODSIn 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
ERROR HANDLINGIf 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().DEPENDENCIESSet::Crontab, Time::Local, Carp. Date::Manip is no longer required thanks to B Paulsen.MAINTENANCESince January 2012 maintained by Petya Kohts (petya.kohts at gmail.com)COPYRIGHTCopyright 2002 P KentThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |