Matteo Pasini
Matteo Pasini

Reputation: 2022

Google calendar api returns different date formats

I have a python script which syncs some of my appointments to two different google accounts, both accounts have a google calendar which is apparently set identically (the time zone set is also the same), at the time of the call to download the list of the appointments on the calendar however, the call returns the appointments with a different date format depending on the calendar I am going to query.

Example of the response returned from the first calendar: enter image description here

Example of the response returned from the second calendar: enter image description here

The library used is the one developed by google (Python version used is 3.6)

The script (which runs in both cases on the same machine) is the following:

params = {
    'calendarId':   self._calendar.id,
    'timeMin':      from_datetime,
    'timeMax':      to_datetime,
    'maxResults':   MAX_APP_DOWNLOADS,
    'singleEvents': True,
    'orderBy':      'startTime',
    'showDeleted':  'true'
}
events_result = self._service.events().list(**params).execute()
events = events_result.get('items', [])

I suppose it is some problem regarding the calendar configuration, but I have not found any settings about it in the settings.

Upvotes: 0

Views: 289

Answers (1)

Ron
Ron

Reputation: 168

Both times returned are in ISO 8601 format.

Update : From the Google API Docs, it is RFC3339. They are mostly similar, but RFC3339 is stricter.

You can use datetime.datetime.fromisoformat or dateutil.parser.isoparse

>>> from datetime import datetime

>>> from dateutil.parser import isoparse

>>> datetime.fromisoformat('2011-11-04T00:05:23+04:00')
datetime.datetime(2011, 11, 4, 0, 5, 23, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400)))

>>> isoparse('2011-11-04T00:05:23Z')
datetime.datetime(2011, 11, 4, 0, 5, 23, tzinfo=datetime.timezone(datetime.timedelta(seconds=0)))

Looks like datetime.datetime.fromisoformat doesn't support reading with the ending 'Z' character, so you should use the dateutil format.

Upvotes: 1

Related Questions