this repo has no description
Zig 100.0%
3 1 0

Clone this repository

https://tangled.org/zzstoatzz.io/cron https://tangled.org/did:plc:xbtmt2zjwlrfegqvch7fboei/cron
git@tangled.org:zzstoatzz.io/cron git@tangled.org:did:plc:xbtmt2zjwlrfegqvch7fboei/cron

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

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 units
  • n - specific value
  • n-m - range
  • n,m,o - list
  • jan-dec - month names
  • sun-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"));