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

Fix calendar sync failing on months having 1st day as Monday #39

Merged
merged 2 commits into from
Apr 12, 2024
Merged

Fix calendar sync failing on months having 1st day as Monday #39

merged 2 commits into from
Apr 12, 2024

Conversation

ReekenX
Copy link
Contributor

@ReekenX ReekenX commented Apr 12, 2024

Somebody created bug in the past #26. It missed steps to reproduce or a fix.

This PR describes how to reproduce and has code fix.

The issue is that calendar sync will fail on months that have Monday as 1st day of the month (e.g. 2024 April 1 is Monday).

Buggy code is:

from calendar import monthrange
first_day, last_day = monthrange(today.year, today.month)

According to the docs:

The monthrange() method is used to get weekday of first day of the month and number of days in month

So in this code variable first_day can return 0 (for Monday) and this will crash datetime().

Before the fix

>>> from icloudpy import ICloudPyService
>>>
>>> api = ICloudPyService('username, 'password')
>>> api.validate_2fa_code(123456)
True
>>> api.calendar.events()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/icloudpy/icloudpy/services/calendar.py", line 71, in events
    self.refresh_client(from_dt, to_dt)
  File "~/icloudpy/icloudpy/services/calendar.py", line 51, in refresh_client
    from_dt = datetime(today.year, today.month, first_day)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: day is out of range for month

After the fix

>>> from icloudpy import ICloudPyService
>>> api = ICloudPyService('username', 'password')
>>> api.calendar.events()
[{'tz': 'Floating', 'icon': 0, 'recurrenceException': False, 'title': 'Test', 'duration': 0, 'allDay': False, [..]]

@mandarons mandarons linked an issue Apr 12, 2024 that may be closed by this pull request
@mandarons mandarons merged commit 2868d43 into mandarons:main Apr 12, 2024
2 checks passed
@mandarons
Copy link
Owner

@ReekenX Thank you for the PR. Appreciate it!

@ReekenX ReekenX deleted the calendar-sync-fix branch April 12, 2024 16:35
@ReekenX
Copy link
Contributor Author

ReekenX commented Apr 15, 2024

Follow up question to @mandarons

Could you bump version to icloudpy 0.5.1 and release to PyPi? Otherwise pip install icloudpy would still give this calendar sync issue.

https://pypi.org/project/icloudpy/

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

Successfully merging this pull request may close these issues.

Fetching calendars fails due to datetime bug
2 participants