{"id":3438,"library":"cron-validator","title":"Cron Validator","description":"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.","status":"active","version":"1.0.8","language":"en","source_language":"en","source_url":"https://github.com/vcoder4c/cron-validator","tags":["cron","scheduler","validation","datetime","unix","eventbridge"],"install":[{"cmd":"pip install cron-validator","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"CronValidator","correct":"from cron_validator import CronValidator"},{"symbol":"CronScheduler","correct":"from cron_validator import CronScheduler"},{"note":"Utility function for parsing datetime strings; standard `datetime` module or `dateutil` are alternatives.","symbol":"str_to_datetime","correct":"from cron_validator.util import str_to_datetime"}],"quickstart":{"code":"from datetime import datetime\nfrom cron_validator import CronValidator\nfrom cron_validator.util import str_to_datetime\n\n# 1. Validate a cron expression\nassert CronValidator.parse('* * * * *') is not None # Valid Unix cron\nassert CronValidator.parse('*/61 * * * *') is None  # Invalid minute (max 59)\n\n# 2. Match a datetime with a cron expression\ndt_str = '2019-04-23 01:00'\ndt = str_to_datetime(dt_str)\nassert CronValidator.match_datetime('* * * * *', dt) is True\nassert CronValidator.match_datetime('0 * * * *', dt) is False # Does not match minute 0\n\n# 3. Generate matching datetimes between two dates\nfrom_dt_str = '2026-04-11 10:00'\nto_dt_str = '2026-04-11 12:00'\nfrom_dt = str_to_datetime(from_dt_str)\nto_dt = str_to_datetime(to_dt_str)\n\nprint(f\"Next executions for '0 * * * *' between {from_dt_str} and {to_dt_str}:\")\nfor execution_dt in CronValidator.get_execution_time(\n    '0 * * * *',\n    from_dt=from_dt,\n    to_dt=to_dt\n):\n    print(execution_dt)\n\n# Expected output for the example above:\n# 2026-04-11 11:00:00\n# 2026-04-11 12:00:00","lang":"python","description":"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."},"warnings":[{"fix":"Consult the library's GitHub README for specific syntax support. Test cron expressions thoroughly if migrating from other cron systems.","message":"The library supports 'partially extended rules based on the Amazon EventBridge rule set' (from v1.0.6), which means its cron dialect might differ from strict Unix cron or Quartz cron standards. Always verify specific expressions against the library's implementation. For example, AWS EventBridge cron expressions do not support seconds.","severity":"gotcha","affected_versions":">=1.0.6"},{"fix":"Integrate `CronScheduler.time_for_execution()` checks into an existing event loop or use a separate thread/process to prevent blocking your main application.","message":"The `CronScheduler` is provided for repetitive tasks, but its sample usage often involves a blocking `while True` loop. This design is not suitable for asynchronous applications or long-running processes without external event loop integration (e.g., `asyncio`, `APScheduler`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to version 1.0.7 or later to use named values. For older versions, use numeric representations (e.g., `1-12` for months, `0-6` or `1-7` for days of week).","message":"Prior to v1.0.7, the library did not support month names (e.g., `JAN`, `FEB`) or day of week names (e.g., `MON`, `TUE`) in cron expressions. Using such names in earlier versions would have resulted in validation failures.","severity":"deprecated","affected_versions":"<1.0.7"},{"fix":"Upgrade to version 1.0.1 or later to ensure all generated execution datetimes are minute-aligned (seconds and microseconds set to 0). If using older versions, manually normalize the datetimes if minute-level precision is critical.","message":"In versions prior to 1.0.1, the `get_execution_time` method might not have rounded execution datetimes to the minute level, potentially returning datetimes with non-zero seconds or microseconds.","severity":"breaking","affected_versions":"<1.0.1"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}