cron#
cron expression parser and iterator in zig.
usage#
const cron = @import("cron");
// parse expression: minute hour day month dow
const schedule = try cron.Cron.parse("*/15 * * * *");
// get next occurrence (microseconds)
const now = std.time.timestamp() * 1_000_000;
if (schedule.next(now)) |next_ts| {
const dt = cron.DateTime.fromTimestamp(@divFloor(next_ts, 1_000_000));
// dt.year, dt.month, dt.day, dt.hour, dt.minute
}
// check if time matches
if (schedule.matches(now)) { ... }
// get multiple next occurrences
var buf: [10]i64 = undefined;
const count = schedule.nextN(now, 10, &buf);
// iterator pattern
var iter = schedule.iter(now);
while (iter.next()) |ts| {
// process each occurrence
}
supported syntax#
*- any value*/n- every n unitsn- specific valuen-m- rangen,m,o- listjan-dec- month namessun-sat- day names
day/dow behavior#
standard cron behavior:
* * 15 * *- 15th of every month* * * * 1-5- weekdays only* * 15 * 1-5- 15th OR any weekday (OR logic)
use day_or: false for AND logic:
const schedule = try cron.Cron.parseWithOptions("* * 15 * 1-5", .{ .day_or = false });
// 15th only if it's a weekday
building#
zig build test # run tests
zig build run # run example
integration#
add to your build.zig.zon:
.dependencies = .{
.cron = .{
.path = "../cron",
},
},
then in build.zig:
const cron_dep = b.dependency("cron", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("cron", cron_dep.module("cron"));