DateTime::Event::Cron - DateTime extension for generating recurrence sets from crontab lines and files.
use DateTime::Event::Cron;
# DateTime::Set construction from crontab line $crontab = '*/3 15 1-10 3,4,5 */2'; $set = DateTime::Event::Cron->from_cron($crontab); $iter = $set->iterator(after => DateTime->now); while (1) { my $next = $iter->next; my $now = DateTime->now; sleep(($next->subtract_datetime_absolute($now))->seconds); # do stuff... }
# List of DateTime::Set objects from crontab file @sets = DateTime::Event::Cron->from_crontab(file => '/etc/crontab'); $now = DateTime->now; print "Now: ", $now->datetime, "\n"; foreach (@sets) { my $next = $_->next($now); print $next->datetime, "\n"; }
# DateTime::Set parameters $crontab = '* * * * *';
$now = DateTime->now; %set_parms = ( after => $now ); $set = DateTime::Event::Cron->from_cron(cron => $crontab, %set_parms); $dt = $set->next; print "Now: ", $now->datetime, " and next: ", $dt->datetime, "\n";
# Spans for DateTime::Set $crontab = '* * * * *'; $now = DateTime->now; $now2 = $now->clone; $span = DateTime::Span->from_datetimes( start => $now->add(minutes => 1), end => $now2->add(hours => 1), ); %parms = (cron => $crontab, span => $span); $set = DateTime::Event::Cron->from_cron(%parms); # ...do things with the DateTime::Set
# Every RTFCT relative to 12am Jan 1st this year $crontab = '7-10 6,12-15 10-28/2 */3 3,4,5'; $date = DateTime->now->truncate(to => 'year'); $set = DateTime::Event::Cron->from_cron(cron => $crontab, after => $date);
# Rather than generating DateTime::Set objects, next/prev # calculations can be made directly:
# Every day at 10am, 2pm, and 6pm. Reference date # defaults to DateTime->now. $crontab = '10,14,18 * * * *'; $dtc = DateTime::Event::Cron->new_from_cron(cron => $crontab); $next_datetime = $dtc->next; $last_datetime = $dtc->previous; ...
# List of DateTime::Event::Cron objects from # crontab file @dtc = DateTime::Event::Cron->new_from_crontab(file => '/etc/crontab');
# Full cron lines with user, such as from /etc/crontab # or files in /etc/cron.d, are supported and auto-detected: $crontab = '* * * * * gump /bin/date'; $dtc = DateTime::Event::Cron->new(cron => $crontab);
# Auto-detection of users is disabled if you explicitly # enable/disable via the user_mode parameter: $dtc = DateTime::Event::Cron->new(cron => $crontab, user_mode => 1); my $user = $dtc->user; my $command = $dtc->command;
# Unparsed original cron entry my $original = $dtc->original;
DateTime::Event::Cron generated DateTime events or DateTime::Set objects based on crontab-style entries.
The cron fields are typical crontab-style entries. For more information, see crontab(5) and extensions described in Set::Crontab. The fields can be passed as a single string or as a reference to an array containing each field. Only the first five fields are retained.
See DateTime::Set for methods provided by Set objects, such as
next()
and previous()
.
$crontab_fh
can be either a filename or filehandle
reference. See new() for details on %parm. Optionally takes parameters
for DateTime::Set which will be passed along to each set for each line.
$fh
can be either a filename or a filehandle reference.
Optional parameters include the boolean 'user_mode' as mentioned above.
$date
defaults to DateTime->now unless provided.
$date
defaults to DateTime->now unless provided.
next()
and previous()
except that the provided datetime is
modified to the new datetime.
Matthew P. Sisk <sisk@mojotoad.com>
Copyright (c) 2003 Matthew P. Sisk. All rights reserved. All wrongs revenged. This program is free software; you can distribute it and/or modify it under the same terms as Perl itself.
DateTime(3), DateTime::Set(3), DateTime::Event::Recurrence(3), DateTime::Event::ICal(3), DateTime::Span(3), Set::Crontab(3), crontab(5)