Croner

Croner

Trigger functions or evaluate cron expressions, in JavaScript/TypeScript, Rust, Python, .NET and C++.

The next five weekdays at 09:30 in Stockholm, in each language. The Rust example uses the chrono-tz crate for the time zone.

JavaScript
import { Cron } from "croner";

const job = new Cron("30 9 * * MON-FRI", { timezone: "Europe/Stockholm" });
console.log(job.nextRuns(5));
Rust
use chrono::Utc;
use chrono_tz::Europe::Stockholm;
use croner::Cron;
use std::str::FromStr;

let cron = Cron::from_str("30 9 * * MON-FRI").expect("Invalid pattern");
let now = Utc::now().with_timezone(&Stockholm);
for run in cron.iter_after(now).take(5) {
    println!("{run}");
}
Python
from croner import Cron

job = Cron("30 9 * * MON-FRI", timezone="Europe/Stockholm")
print(job.next_runs(5))
.NET
using Croner;

var cron = Cron.Parse("30 9 * * MON-FRI");
var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Stockholm");
foreach (var run in cron.GetNextOccurrences(DateTimeOffset.UtcNow, tz).Take(5))
{
    Console.WriteLine(run);
}
C++
#include <croner/croner.hpp>
#include <iostream>

int main() {
  croner::CronOptions opts;
  opts.zone = croner::locate_zone("Europe/Stockholm");
  const croner::Cron cron("30 9 * * MON-FRI", opts);
  for (auto run : cron.next_runs(5)) std::cout << croner::to_iso_string(run) << "\n";
}

Implementations#

Croner is available for several languages. All implementations use the same pattern syntax and the same rules for daylight saving time. Where they differ, for example in which fields accept ?, the Pattern page says so.

Language Repository Package Evaluate Schedule Describe
JavaScript / TypeScript croner npm, JSR ✓ ✓
Rust croner-rust crates.io ✓ ✓
Python croner-py PyPI ✓ ✓ ✓
.NET croner-dotnet NuGet ✓ ✓ ✓
C++ croner-cpp Header-only, see Installation ✓ ✓

Evaluate: find next and previous runs, and check if a date matches. Schedule: run a function on the pattern, with pause, resume, stop and overrun protection. Describe: a human readable description of the pattern, like "At 09:30, on Monday, Tuesday, Wednesday, Thursday, and Friday."

The usage documentation for JavaScript is on this site. The other implementations are documented in the README of each repository.

Features#

The JavaScript version also:

More JavaScript examples are in Usage (JavaScript), and a comparison with other JavaScript libraries in Comparison (JavaScript).