В каждой базе данных реализованы разные функции добавления даты и времени, которые отстой.Так что это будет немного зависеть от того, какую базу данных вы используете.
Например, для postgres мы можем использовать помощник "interval":
# Calculate the timestamp of the next occurrence. This is done
# by taking the last occurrence and adding the number of seconds
# indicated by the schedule.
one_second = SQL("INTERVAL '1 second'")
next_occurrence = Recurring.occurred_at + (one_second * Schedule.delay)
# Get all recurring rows where the current timestamp on the
# postgres server is greater than the calculated next occurrence.
query = (Recurring
.select(Recurring, Schedule)
.join(Schedule)
.where(SQL('current_timestamp') >= next_occurrence))
for recur in query:
print(recur.occurred_at, recur.schedule.delay)
Вы также можете заменить datetimeОбъект для «current_timestamp», если вы предпочитаете:
my_dt = datetime.datetime(2019, 3, 1, 3, 3, 7)
...
.where(Value(my_dt) >= next_occurrence)
Для SQLite, вы должны сделать:
# Convert to a timestamp, add the scheduled seconds, then convert back
# to a datetime string for comparison with the last occurrence.
next_ts = fn.strftime('%s', Recurring.occurred_at) + Schedule.delay
next_occurrence = fn.datetime(next_ts, 'unixepoch')
Для MySQL вы должны сделать:
# from peewee import NodeList
nl = NodeList((SQL('INTERVAL'), Schedule.delay, SQL('SECOND')))
next_occurrence = fn.date_add(Recurring.occurred_at, nl)
И наконец, я бы посоветовал вам попробовать лучшие названия для ваших моделей / полей.т. е. Schedule.interval вместо Schedule.delay и Recurring.last_run вместо seen_at.