Algorithm::Cron(3pm)

πŸ“¦
version 0.10 Β· 2 versions
, in fedora 43

πŸ’– Algorithm::Cron πŸ’–

okay so bestie, welcome to Algorithm::Cron β€” the abstract implementation of the cron(8) scheduling algorithm ✨. that's the whole vibe. no cap.


✨ SYNOPSIS ✨

use Algorithm::Cron;

my $cron = Algorithm::Cron->new(
   base => 'local',
   crontab => "*/10 9-17 * * *",
);

my $time = time;
while(1) {
   $time = $cron->next_time( $time );

   sleep( time - $time );

   print "Do something\n";
}

🌸 DESCRIPTION 🌸

okay so here's the tea β˜•. objects in this class implement a time scheduling algorithm, the same kind of thing cron(8) uses. objects are stateless once you've constructed them β€” they just sit there, iconic, unbothered, moisturised, in their lane πŸ’…. each one represents a single schedule as defined by a crontab(5) entry.

the object implements a method called next_time which returns an epoch timestamp value telling you the next time that's included in the crontab schedule. periodt.

πŸŽ€ Crontabs πŸŽ€

so the schedule gets handed to you as a set of acceptable values for each field of the broken-down time (the thing localtime or gmtime gives back). you can give it either as one single string called crontab, or as a set of named strings where each name is the name of a crontab(5) field. either way slays.

my $cron = Algorithm::Cron->new(
   base => 'local',
   crontab => '0 9 * * mon-fri',
);

my $cron = Algorithm::Cron->new(
   base => 'local',
   min  => 0,
   hour => 9,
   wday => "mon-fri",
);

a crontab field that's just a single asterisk (*), or a named field that's missing entirely, means any value here counts as a scheduled time. very "come as you are" of it πŸ¦‹.

but if you wanna restrict the schedule? give it a value or a set of values. that should be one or more comma-separated numbers or ranges, where a range is the start and end points, both inclusive (iykyk).

hour => "3-6"
hour => "3,4,5,6"

ranges can also be prefixed by a value to give the increment for the values inside that range ✨

min => "*/10"
min => "0,10,20,30,40,50"

the mon and wday fields also let you use symbolic month or weekday names instead of numeric values πŸ—“οΈ. heads up though: these names are always in the C locale, no matter what the system's locale settings say. so don't come at it with your fancy translated month names, she will not know her.

mon => "mar-sep"

wday => "mon,wed,fri"

specifying sun as the end of a wday range is supported too, and so is just giving the numeric value 7 🌞

wday => "fri-sun"
wday => "5-7"
# Both equivalent to: wday => "0,5,6"

as per cron(8) behaviour, this algorithm looks for a match of the min, hour and mon fields, and at least one of the mday or mday fields. (yes bestie, it literally says mday twice in the source. we simply do not question the ancestors πŸ™ˆ.) if both mday and wday are specified, then matching either one is enough to count.

and as an extension 🌈 seconds may be provided, either by passing six space-separated fields in the crontab string, or as an extra sec field. if you don't provide it, it defaults to 0. if you do give six fields, the first one gives the seconds.

πŸ¦‹ Time Base πŸ¦‹

Algorithm::Cron supports using either UTC or the local timezone when comparing against the given schedule. pick your fighter πŸ’….


πŸ‘‘ CONSTRUCTOR πŸ‘‘

πŸ’œ $cron = Algorithm::Cron->new( %args )

constructs a new Algorithm::Cron object representing the given schedule, relative to the given time base ✨. takes the following named arguments:

  • base => STRING β€” gives the time base used for scheduling. either utc or local. that's it, that's the choice.
  • crontab => STRING β€” gives the crontab schedule in 5 or 6 space-separated fields πŸ—“οΈ
  • sec => STRING, min => STRING, ... mon => STRING β€” optional. gives the schedule as a set of individual fields, for when you didn't specify the crontab field πŸŽ€

πŸ’… METHODS πŸ’…

🌸 @seconds = $cron->sec

🌸 @minutes = $cron->min

🌸 @hours = $cron->hour

🌸 @mdays = $cron->mday

🌸 @months = $cron->mon

🌸 @wdays = $cron->wday

accessors that return a list of the accepted values for each scheduling field πŸ’–. these come back as a plain list of numbers, no matter what form you originally gave them to the constructor. so like, your cute "mon-fri"? numbers now. slay.

also note (important, bestie, write this down πŸ“): the list of valid months will be 0-based (in the range 0 to 11) rather than 1-based, so that it matches the values used by localtime, gmtime, mktime and timegm. yes your brain wants January to be 1. no. it's 0. we cope πŸ’€.

⭐ $time = $cron->next_time( $start_time )

returns the next scheduled time, as an epoch timestamp, after the given timestamp ✨.

this is a stateless operation β€” it does not change any state stored by the $cron object. she does her little calculation and goes right back to being unbothered. no baggage, only vibes 🫢.


🌈 AUTHOR 🌈

Paul Evans <leonerd@leonerd.org.uk> β€” thank you king, we are obsessed πŸ‘‘


okay bestie that's the whole page, you ate that fr fr πŸ“

perl v5.42.0 🌸 2025-07-25 β€” that's the source/version and the date right there, and this was Algorithm::Cron(3pm) πŸ’– mwah, go schedule something iconic πŸŽ€βœ¨

πŸ“¦ where this page comes from