pyicloud icon indicating copy to clipboard operation
pyicloud copied to clipboard

Calendar: ValueError: day is out of range for month

Open passie opened this issue 7 years ago • 4 comments

File "/usr/local/lib/python2.7/site-packages/pyicloud/services/calendar.py", line 44, in refresh_client from_dt = datetime(today.year, today.month, first_day) ValueError: day is out of range for month

passie avatar May 11 '17 11:05 passie

Hey @passie,

I can verify this, the issue is with datetime and calendar module that are also loaded in the calendar.py first_day variable is set with value 0, this should have been 1, because the first day of the month is always 1.

def refresh_client(self, from_dt=None, to_dt=None):
        """
        Refreshes the CalendarService endpoint, ensuring that the
        event data is up-to-date. If no 'from_dt' or 'to_dt' datetimes
        have been given, the range becomes this month.
        """
        today = datetime.today()
        first_day, last_day = monthrange(today.year, today.month)
        if not from_dt:
            # workaround for issue #132
            from_dt = datetime(today.year, today.month, first_day + 1)
        if not to_dt:
            to_dt = datetime(today.year, today.month, last_day)
        params = dict(self.params)
        params.update({
            'lang': 'en-us',
            'usertz': get_localzone().zone,
            'startDate': from_dt.strftime('%Y-%m-%d'),
            'endDate': to_dt.strftime('%Y-%m-%d')
        })
        req = self.session.get(self._calendar_refresh_url, params=params)
        self.response = req.json()

I will fork the git repository and will request a merge request.

hvanderlaan avatar May 26 '17 11:05 hvanderlaan

Is it still happening with latest version ?

Quentame avatar Mar 19 '20 10:03 Quentame

yes, it's still happening. I just try to use it and:

In [9]: api.calendar.events()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-b2b122ac54f1> in <module>
----> 1 api.calendar.events()

~/.local/lib/python3.8/site-packages/pyicloud/services/calendar.py in events(self, from_dt, to_dt)
     63         Retrieves events for a given date range, by default, this month.
     64         """
---> 65         self.refresh_client(from_dt, to_dt)
     66         return self.response.get("Event")
     67

~/.local/lib/python3.8/site-packages/pyicloud/services/calendar.py in refresh_client(self, from_dt, to_dt)
     44         first_day, last_day = monthrange(today.year, today.month)
     45         if not from_dt:
---> 46             from_dt = datetime(today.year, today.month, first_day)
     47         if not to_dt:
     48             to_dt = datetime(today.year, today.month, last_day)

ValueError: day is out of range for month

eduenriquez avatar Mar 01 '21 15:03 eduenriquez

monthrange does not return the first day of the month but the weekday of the first day of the month. Weekdays in python start at 0 (Sunday). PR #331 solves this correctly by always using 1 as the start of the month.

RichieB2B avatar Oct 12 '21 08:10 RichieB2B