Parse and evaluate standard cron expressions to check datetime matches.
pip install .from datetime import datetime
from crontab_lite import parse, matches, next_fire, prev_fire
# Parse a cron expression
expr = parse("30 12 * * *") # Daily at 12:30
# Check if a datetime matches
dt = datetime(2026, 4, 15, 12, 30)
print(matches(expr, dt)) # True
# Find next occurrence
next_dt = next_fire(expr, datetime(2026, 4, 15, 12, 0))
print(next_dt) # 2026-04-15 12:30:00
# Find previous occurrence
prev_dt = prev_fire(expr, datetime(2026, 4, 15, 13, 0))
print(prev_dt) # 2026-04-15 12:30:00
# Or use the expression string directly
if matches("0 9 * * 1-5", datetime(2026, 4, 15, 9, 0)):
print("It's 9 AM on a weekday!")Parse a standard 5-field cron expression into a structured object.
Fields:
- Minute (0-59)
- Hour (0-23)
- Day of Month (1-31)
- Month (1-12)
- Day of Week (0-7, where 0 and 7 are Sunday)
Syntax:
*� Match any value5� Match specific value1-5� Match range1,3,5� Match list*/5� Match with step1-30/5� Match range with step
Example:
expr = parse("0 9 * * 1-5") # 9 AM on weekdaysRaises: CrontabError for invalid expressions
Check if a timezone-naive datetime matches a cron expression.
Args:
expression: Cron expression string or parsedCronExpressiondt: Timezone-naivedatetimeobject
Returns: True if the datetime matches, False otherwise
Raises: CrontabError for invalid inputs
Example:
if matches("30 12 * * *", datetime(2026, 4, 15, 12, 30)):
print("Noon exactly!")Find the next datetime that matches the expression.
Args:
expression: Cron expression string or parsedCronExpressionafter: Starting datetime (default: current time). Must be timezone-naive.
Returns: The next matching datetime
Raises: CrontabError if no match found within reasonable bounds
Example:
next_noon = next_fire("0 12 * * *", datetime(2026, 4, 15, 11, 0))
# Returns: 2026-04-15 12:00:00Find the previous datetime that matched the expression.
Args:
expression: Cron expression string or parsedCronExpressionbefore: Starting datetime (default: current time). Must be timezone-naive.
Returns: The previous matching datetime
Raises: CrontabError if no match found within reasonable bounds
Example:
prev_noon = prev_fire("0 12 * * *", datetime(2026, 4, 15, 13, 0))
# Returns: 2026-04-15 12:00:00Holds parsed cron fields.
Attributes:
minute: set� Valid minute values (0-59)hour: set� Valid hour values (0-23)dom: set� Valid day-of-month values (1-31)month: set� Valid month values (1-12)dow: set� Valid day-of-week values (0-6)
Raised for all error conditions (invalid expressions, timezone-aware datetimes, etc.).
- 5-field cron syntax (minute hour dom month dow)
- Special characters:
*, ranges (1-5), lists (1,3,5), steps (*/15) - Day-of-week normalization: Both 0 and 7 represent Sunday
- Timezone-naive only: Rejects timezone-aware datetimes for safety
- Reasonable bounds:
next_fireandprev_firesearch up to 4 years - Edge case handling:
- February 29 (leap years only)
- Day 31 in months with fewer days
- Year boundaries
- Invalid syntax and out-of-range values
pytest tests/ -vWith coverage report:
pytest tests/ -v --cov=src/crontab_lite --cov-report=term-missingMIT License � Copyright (c) 2026 Nripanka Das