Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api_occurrences has a bug with changed occurrences when DST is involved #509

Open
ataylor32 opened this issue May 19, 2021 · 1 comment
Open

Comments

@ataylor32
Copy link

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'))
@danemco
Copy link

danemco commented Jun 14, 2021

I'm having this issue too. Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants