用户可以设置生日提醒。 (我们不关心出生年份) 他还选择是否要在 D 日之前的 0、1、2 或 7 天 (Delta) 收到警报。 用户有时区设置。
我希望服务器在 D 日早上 8 点发送警报 - deleta +- 用户时区
例子:
6 月 12 日,“提前 3 天提醒我”将给出 6 月 9 日。
我的想法是在“经常性事件”对象上保存一个 trigger_datetime 额外字段。 像这样一个 cron 作业每小时在我的服务器上运行一次,它将只检查与 irs 当前时间小时、日期和月份匹配的所有事件,并发送到警报。
从一年到下一年的问题 trigger_date 可能会改变! 如果警报设置在 3 月 1 日,延迟一天可能是 2 月 28 日或 29 日......
也许我不应该使用触发日期技巧并使用其他类型的方案。
欢迎所有计划。
最佳答案
尽管使用简单的 datetime
python 模块,您将能够实现您需要的一切,一个更强大的 python-dateutil扩展可用,特别是如果您需要处理重复发生的事件。下面的代码应该会告诉您如何实现您的目标:
from datetime import *
from dateutil.rrule import rrule, YEARLY
# GLOBAL CONFIG
td_8am = timedelta(seconds=3600*8)
td_jobfrequency = timedelta(seconds=3600) # hourly job
# USER DATA::
# birthday: assumed to be retrieved from some data source
bday = date(1960, 5, 12)
# reminder delta: number of days before the b-day
td_delta = timedelta(days=6)
# difference between the user TZ and the server TZ
tz_diff = timedelta(seconds=3600*5) # example: +5h
# from current time minus the job periodicity and the delta
sday = date.today()
# occr will return the first birthday from today on
occr = rrule(YEARLY, bymonth=bday.month, bymonthday=bday.day, dtstart=sday, count=1)[0]
# adjust: subtract the reminder delta, fixed 8h (8am) and tz difference
occr -= (td_delta + td_8am + tz_diff)
# send the reminder when the adjusted occurance is within this job time period
if datetime.now() - td_jobfrequency < occr < datetime.now():
print occr, '@todo: send the reminder'
else:
print occr, 'no reminder'
而且我建议你不要存储下一年的提醒日期,因为delta
可能会改变,或者timezone
可能会改变,甚至birthday
本身,所以你需要重新计算它。上面的方法基本上是即时计算提醒日期(和时间)。
我可以建议的另一件事是存储上次发送提醒的日期(生日,包括年份)。因此,如果出现系统停机,您不会错过提醒,而是会发送所有未发送的提醒。您将需要调整代码以进行此额外检查和更新。
关于python - Python 中的定期年度日期警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2776311/