🛠️ToolsShed

Cron 表达式解析器

解析 Cron 表达式并显示下次执行时间。

Quick Presets

Format: minute hour day-of-month month day-of-week

Cron 解析器将 cron 表达式翻译成通俗易懂的描述,并显示接下来几次预定的运行时间。Cron 是 Unix/Linux 系统、CI/CD 管道、无服务器函数和云调度器中用于定义重复任务的标准作业调度语法。

输入任何 cron 表达式(标准五字段或扩展六字段格式),工具立即用人类可读的语言解释其含义并列出即将到来的执行时间。您还可以使用可视化构建器通过从下拉菜单选择分钟、小时、天和月份来组合表达式。

理解 cron 语法对于计划备份、定期数据库清理、自动化报告和有速率限制的 API 轮询等任务至关重要。常见表达式包括 `0 * * * *`(每小时)、`0 0 * * *`(每天午夜)和 `0 9 * * 1-5`(工作日上午 9 点)。

常见问题

代码实现

from croniter import croniter
from datetime import datetime

# Parse and get next execution times
cron_expr = "0 9 * * 1-5"  # 9 AM every weekday
base = datetime.now()
cron = croniter(cron_expr, base)

print("Next 5 execution times:")
for _ in range(5):
    print(cron.get_next(datetime))

# Check if an expression is valid
def is_valid_cron(expr):
    try:
        croniter(expr)
        return True
    except (ValueError, KeyError):
        return False

print(is_valid_cron("*/15 * * * *"))  # True
print(is_valid_cron("60 * * * *"))    # False

# Get previous execution time
cron2 = croniter("0 0 * * *", base)
print("Last midnight:", cron2.get_prev(datetime))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.