通常,如果我的日程表使用
cron
格式,我可以使用以下代码找到下一个日程表:
import croniter
import datetime
now = datetime.datetime.now()
sched = '1 15 1,15 * *'
cron = croniter.croniter(sched, now)
for i in range(4):
nextdate = cron.get_next(datetime.datetime)
print(nextdate)
>> 2024-12-15 15:01:00
>> 2025-01-01 15:01:00
>> 2025-01-15 15:01:00
>> 2025-02-01 15:01:00
但是我怎样才能用
quartz cron
格式做到这一点?
我尝试使用
0 5 23 * * ?
运行上面的代码并收到此错误:
raise CroniterBadCronError(
"[{0}] is not acceptable. Question mark can only used "
"in day_of_month or day_of_week".format(expr_format))
croniter.croniter.CroniterBadCronError: [0 5 23 * * ?] is not acceptable. Question mark can only used in day_of_month or day_of_week
我发现这个answer建议如何将
quartz cron
与croniter
一起使用,但这不是我所期望的,因为它需要更改字段顺序。
然后我发现这个 cron-schedule-triggers 库适合我。这是代码:
import datetime
from cstriggers.core.trigger import QuartzCron
sched = '0 5 23 * * ?'
start_date = datetime.datetime.now()
cron_obj = QuartzCron(schedule_string=sched, start_date=start_date)
print(cron_obj.next_trigger(isoformat=True))
>> 2024-12-09T23:05:00.152219