Python annual date re-warning

The user can set a day alert for a birthday. (We don’t care about the year of birth) He also chooses whether he wants to be warned 0, 1, 2, ou 7 days (Delta) before the Day. Users have a time zone setting.

I want the server to send alerts at 8 a.m. clockwise D day - deletea + - user

Example:

June 12th, “warn me 3 days before” will be given on June 9th.

My idea was that the extra field of the trigger_sensor is stored on the recurring event object. Similarly, cron Work performed every hour on my server will check all events corresponding to the current hour, day and month, and send a warning.

The problem from year to next trigger_date may change! If the warning is set on March 1, with a one-day delay, which may be February 28 or 29.

Perhaps I should not use the launch date trick and use some other scheme.

All plans are welcome.

+3
source share
5 answers

Despite using a simple datetimepython module, you can implement everything you need, a much more powerful python-dateutil extension . available, especially if you need to work with recurring events. The code below should give you an idea of ​​how to achieve your goal:

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, . ( ) .

, , - ( , ) . , , , , . .

+3

, , , Python datetime, timedelta :

from datetime import timedelta, date
start_date = date(2010, 6, 12)
notification_date = start_date + timedelta(days=365) - timedelta(days=3)
print notification_date

: 2011-06-09, timedelta , . , , , , - . , , , , , .

+2

.

  • Cron, , .

  • , - . . .

0

- .

* , date (GetDate(), GetDate() + 2, GetDATE() + 5, GETDATE() + 7) date-GetDate() = alertFrequency

0

,

SELECT `user` FROM `mytable` WHERE now() >= (`birthday` - INTERVAL `delta` DAY);

?

0

Source: https://habr.com/ru/post/1744232/


All Articles