Open3
EventBridgeルールのcronをプレビューしたい

aws-croniter: https://pypi.org/project/aws-croniter/
Pythonライブラリ。
NCrontab: https://www.nuget.org/packages/ncrontab/
Cronos: https://github.com/HangfireIO/Cronos
いずれも.NETライブラリ。

ChatGPTにaws-croniterを使用するスクリプトを書かせた。後で確認する。
pip3 install --user aws-croniter
desc-evt-rules.py
import boto3
from aws_croniter import croniter
from datetime import datetime, timezone, timedelta
import re
# JSTタイムゾーン
JST = timezone(timedelta(hours=9))
# EventBridgeクライアント
client = boto3.client('events')
def list_rules():
rules = []
paginator = client.get_paginator('list_rules')
for page in paginator.paginate():
rules.extend(page['Rules'])
return rules
def parse_cron_expression(expr):
# cron(30 0 17 6 ? 2025) -> '30 0 17 6 ? 2025'
match = re.match(r'cron\(([^)]+)\)', expr)
if not match:
return None
return match.group(1)
def main():
base_time = datetime.now(timezone.utc)
rules = list_rules()
for rule in rules:
name = rule['Name']
expr = rule.get('ScheduleExpression')
if expr and expr.startswith('cron('):
cron_expr = parse_cron_expression(expr)
if cron_expr:
try:
itr = croniter(cron_expr, start_time=base_time)
next_time = itr.get_next()
jst_time = next_time.astimezone(JST)
print(f"[{name}] 次回実行日時(JST): {jst_time.strftime('%Y-%m-%d %H:%M:%S')}")
except Exception as e:
print(f"[{name}] cron式の解釈エラー: {e}")
if __name__ == '__main__':
main()