Cron Validator

1.0.8 · active · verified Sat Apr 11

cron-validator is a Python library providing tools for validating Unix cron expressions, matching specific datetimes against a cron pattern, and generating a sequence of datetimes that fulfill a given cron expression. It also includes a `CronScheduler` for simple task scheduling. Currently at version 1.0.8, the library receives regular, minor updates primarily for bug fixes and feature enhancements, such as extended cron rule support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to validate cron expressions, check if a specific datetime matches a cron expression, and generate future execution times within a given range.

from datetime import datetime
from cron_validator import CronValidator
from cron_validator.util import str_to_datetime

# 1. Validate a cron expression
assert CronValidator.parse('* * * * *') is not None # Valid Unix cron
assert CronValidator.parse('*/61 * * * *') is None  # Invalid minute (max 59)

# 2. Match a datetime with a cron expression
dt_str = '2019-04-23 01:00'
dt = str_to_datetime(dt_str)
assert CronValidator.match_datetime('* * * * *', dt) is True
assert CronValidator.match_datetime('0 * * * *', dt) is False # Does not match minute 0

# 3. Generate matching datetimes between two dates
from_dt_str = '2026-04-11 10:00'
to_dt_str = '2026-04-11 12:00'
from_dt = str_to_datetime(from_dt_str)
to_dt = str_to_datetime(to_dt_str)

print(f"Next executions for '0 * * * *' between {from_dt_str} and {to_dt_str}:")
for execution_dt in CronValidator.get_execution_time(
    '0 * * * *',
    from_dt=from_dt,
    to_dt=to_dt
):
    print(execution_dt)

# Expected output for the example above:
# 2026-04-11 11:00:00
# 2026-04-11 12:00:00

view raw JSON →