Google Calendar API Timezone daylight and standard time issue

Viewed 296

When I create an event giving it a near date, all works well.

But when I create an event which is a few months later, say that it falls in outside the daylight saving time period DST (while I am currently in the daylight saving time period), the event is created with an unwanted 1 hour offset.

The code below results in the following output:

Starts 2020-12-20 9:00 Ends 2020-12-20 11:00

 $event = array(
        'summary' => $attributes['summary'],
        'location' => $attributes['location'],
        'description' => $attributes['description'],
        'start' => array(
            'dateTime' => 2020-12-20T10:10:00-04:00',
            'timeZone' => 'America/Toronto',
        ),
        'end' => array(
            'dateTime' => 2020-12-20T10:12:00-04:00',
            'timeZone' => 'America/Toronto',
        ),
        'anyoneCanAddSelf'=>true,
        'guestsCanInviteOthers'=>true,
    );

Question

How should I change my code so it works automatically and so that I don't need to manually change my code / the offset twice a year?

1 Answers

The issue is that you are overspecifying the dateTime parameter to include an offset, but you are also providing a timeZone parameter. In this case, the timeZone parameter is conflicting with the offset value in the dateTime (America/Toronto is offset -05:00 on that date).

Given the conflict between the two - it chooses to use the offset specified in the dateTime.

The documentation for the Event resource specifies, in the end.dateTime attribute:

The time, as a combined date-time value (formatted according to RFC3339). A time zone offset is required unless a time zone is explicitly specified in timeZone.

(emphasis added)

The easiest way to solve the problem would be to remove the offset value from the dateTime you specify and rely on the timeZone setting.

Related