urlgoogle-calendar-apigoogle-gadget

How to determine the current week in a Google Calendar sidebar gadget


I am writing a Google Calendar sidebar gadget to keep track of the total hours per event tag (as determined in details of the event i.e. "tags: work").

Users can change the current week, month, day they are viewing in the calendar and I want to be able to count up the hours pertaining to their current view.

I don't see anywhere in the gadget API (or any other Google Calendar API) that allows gadgets to access the currently displayed view. I have noticed that the URL has an anchor tag that looks like

g|week-2+23127+23137+23131

which corresponds to viewing Monday Feb. 23, 2015 - Sunday March 1, 2015 in week mode.

I have also noticed the following relationships:

If there is a way to get the currently displayed view using the API, that would be ideal but I would settle for parsing the anchor tag. Unfortunately I cannot decipher how the numbers work.


Solution

  • Google API

    The currently displayed date range can be accessed using the following call:

    google.calendar.subscribeToDates(function(d) { // do something });

    where d is a Google date range d.startTime and d.endTime being the beginning and end.

    Numbers

    The numbers in the URL do not correspond directly to epoch date and time. Rather, each year has 512 days associated with it and each month has 32 days. For example, February has 28 days regularly but every leap year it has 29. The calendar never has to adjust for this since it simply allots each month 32 days and comes out with a nice even number every time.

    A careful examination of the date ranges displayed will also show you that if you subtract the number for December 31 from January 1 you get 130. Accounting for the beginning and the end (don't count December 31 and January 1) will give you 128.

    12 * 32 + 128 = 512 -- 12 months a year, 32 days a month and a 128 gap per year

    Also, for some reason January 1, 1970 has the associated number of 33 so add that to your calculations when determining dates.