django-scheduler icon indicating copy to clipboard operation
django-scheduler copied to clipboard

api_occurrences has a bug with changed occurrences when DST is involved

Open ataylor32 opened this issue 3 years ago • 1 comments

The following code creates a recurring event that usually occurs on Monday, but there are five instances where it occurs on Tuesday instead. When daylight saving time is involved, some of the events that should have been moved to Tuesday appear to have been copied to Tuesday instead (i.e. the Monday occurrence is still appearing in addition to the Tuesday occurrence).

import datetime
import pytz
from schedule.models import Calendar, Event, Occurrence, Rule
from schedule.views import _api_occurrences

rule = Rule.objects.create(
    name='Weekly',
    description='Weekly',
    frequency='WEEKLY',
)

calendar = Calendar.objects.create(
    name='Test',
    slug='test',
)

event = Event.objects.create(
    start=datetime.datetime(2021, 1, 4, 12, tzinfo=pytz.utc),
    end=datetime.datetime(2021, 1, 4, 13, tzinfo=pytz.utc),
    title='Weekly Meeting',
    rule=rule,
    calendar=calendar,
)

for date in (
    '2021-01-18',
    '2021-05-31',
    '2021-07-05',
    '2021-09-06',
    '2021-12-27',
):
    mon = datetime.date(*map(int, date.split('-')))
    tue = mon + datetime.timedelta(days=1)
    mon_values = {'year': mon.year, 'month': mon.month, 'day': mon.day}
    tue_values = {'year': tue.year, 'month': tue.month, 'day': tue.day}
    Occurrence.objects.create(
        event=event,
        start=event.start.replace(**tue_values),
        end=event.end.replace(**tue_values),
        original_start=event.start.replace(**mon_values),
        original_end=event.end.replace(**mon_values),
    )

# The following code works correctly
for occurrence in event.get_occurrences(
    start=datetime.datetime(2021, 1, 1, tzinfo=pytz.utc),
    end=datetime.datetime(2021, 12, 31, 23, 59, 59, tzinfo=pytz.utc),
):
    print(occurrence.start.strftime('%a, %b %-d, %Y'))

# The following code includes these dates in its output:
#
# Mon, May 31, 2021
# Mon, Jul 5, 2021
# Mon, Sep 6, 2021
#
# This behavior is incorrect and it is happening because of DST. If you
# change the `timezone` kwarg to `'America/Phoenix'` then it works.
for occurrence in _api_occurrences(
    start='2021-01-01T00:00:00',
    end='2021-12-31T23:59:59',
    calendar_slug='test',
    timezone='America/New_York',
):
    print(occurrence['start'].strftime('%a, %b %-d, %Y'))

ataylor32 avatar May 19 '21 20:05 ataylor32

I'm having this issue too. Any updates?

danemco avatar Jun 14 '21 21:06 danemco