0
votes

I am attempting to use the Google .Net Client Libraries to query for Google Calendar events.

I am successfully pulling down events but it seems to completely ignore the TimeMin and TimeMax and return events outside of that time-frame.

As a test, I widened this window from this time yesterday to this time tomorrow with AddDays().

My code is:

    EventsResource.ListRequest req = service.Events.List(calendarId);

    // Limit the calendar to today
    req.TimeMin = DateTime.Now.AddDays(-1);
    req.TimeMax = DateTime.Now.AddDays(1);

    var events = req.Execute().Items;

For today (11/12/14 ... so 11/11/14 to 11/13/14 using the code above) this is returning events from 11/5/14.

Using the .Net libraries, these two properties are defined as nullable DateTime objects (DateTime?), so I am not string-formatting these to any standard.

Am I doing something wrong here?

EDIT: I came across the following link. If this information is true, how exactly would this be handled from .Net where these fields are DateTime?.

/events/list accepts timeMin and timeMax arguments and these are simply stated as accepting a ‘datetime’ argument. Of the myriad possible standardized date-time formats, I have discovered that this value should be a UTC date-time (with offset explicitly set at 00:00) formatted as RFC3339 (yyyy-MM-ddThh:mm:ss.sss+00:00).

Google Calendar API v3 Undocumentation

1
Try: DateTime.Now.AddDays(-1).ToString("o");luc
I can't do that, as they are DateTime? types. "Cannot implicitly convert type 'string' to 'System.DateTime?'. There are no string properties with the Google .Net Client Libraries for this.Patrick
They used to be in an older version.. Thanks for letting me know! Would it be possible for you to get the requests and responses from the Calendar API? You should be able to use fiddler for that telerik.com/fiddlerluc
Fiddler shows what appears (?) to be correct formatting on the GET: GET googleapis.com/calendar/v3/calendars/[calendarID]/… HTTP/1.1Patrick
Yes, the query seems indeed correct. Reading from your answer, it seems that you just did not realise the events you were getting from seemingly outside the range are actually recurring events. All recurring events that have at least one instance in the given range will be returned on list requests.luc

1 Answers

4
votes

I was able to get this to work by setting the SingleEvents property to true.

It seems some of the items that were showing out of the date range were re-occuring events. Setting SingleEvents to true shows the re-occuring event that falls within the date range specified by TimeMin/TimeMax.

    EventsResource.ListRequest req = service.Events.List(calendarId);

    // Limit the calendar to today
    req.TimeMin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
    req.TimeMax = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

    req.SingleEvents = true;

    var events = req.Execute().Items;