pycron: Cron-like Parser

3.2.0 · active · verified Sun Apr 12

pycron is a simple Python library for parsing cron-like expressions and determining if a given datetime matches the specified conditions. It supports standard cron formats, day names, and step values. The current version is 3.2.0, and the library is actively maintained with several releases per year.

Warnings

Install

Imports

Quickstart

Demonstrates how to use `is_now` to check if a cron expression matches a given datetime (or the current time), and `has_been` for checking if a condition was met within a time range.

import datetime
from pycron import is_now, has_been

# Check if current time (or a specific datetime) matches a cron string
# This example checks if it's currently every minute
matches_every_minute = is_now('* * * * *')
print(f"Matches every minute (current time): {matches_every_minute}")

# Check for a specific time, e.g., 9:30 AM every weekday
specific_dt = datetime.datetime(2023, 10, 26, 9, 30, 0) # Thursday
matches_specific_time = is_now('30 9 * * 1-5', specific_dt)
print(f"Matches 9:30 AM on a weekday (2023-10-26 09:30): {matches_specific_time}")

# Using has_been (anacron style)
# Check if '0 0 * * *' (midnight) has occurred since an hour ago
now = datetime.datetime.now()
one_hour_ago = now - datetime.timedelta(hours=1)
midnight_occurred = has_been('0 0 * * *', one_hour_ago, now)
print(f"Midnight occurred between {one_hour_ago.strftime('%H:%M')} and {now.strftime('%H:%M')}: {midnight_occurred}")

view raw JSON →