Why Google Calendar API returns timeZone=UTC altho

2019-07-24 23:08发布

问题:

Most of our customers are American, but after connecting their google calendar with our service we read timeZone=UTC:

https://www.googleapis.com/calendar/v3/users/me/calendarList?minAccessRole=owner&access_token=<ACCSES TOKEN>

response:

{
"kind": "calendar#calendarList",
"etag": "\"1407366566837000\"",
"nextSyncToken": "00001407366566837000",
"items": [
    {
        "kind": "calendar#calendarListEntry",
        "etag": "\"1407184723757000\"",
        "id": "sampleemail@gmail.com",
        "summary": "sampleemail@gmail.com",
        "timeZone": "UTC",
        "colorId": "17",
        "backgroundColor": "#9a9cff",
        "foregroundColor": "#000000",
        "selected": true,
        "accessRole": "owner",
        "defaultReminders": [
            {
                "method": "popup",
                "minutes": 30
            }
        ],
        "notificationSettings": {
            "notifications": [
                {
                    "type": "eventCreation",
                    "method": "email"
                },
                {
                    "type": "eventChange",
                    "method": "email"
                },
                {
                    "type": "eventCancellation",
                    "method": "email"
                },
                {
                    "type": "eventResponse",
                    "method": "email"
                }
            ]
        },
        "primary": true
    }
]

}

it happens to a lot of users, and we're pretty sure their calendar is actually not set to UTC, but Google consistently returns UTC. Anyone familiar with this problem?

回答1:

https://support.google.com/calendar/answer/2367918?hl=en

How Calender Treats time zones

Whenever you create an event, Calendar converts it from your time zone to UTC time, using currently known conversion rules. By using one universal time for all events, Calendar can keep all of your guests’ calendars consistent regardless of which time zones they're in. When we display the event on your calendar, it is converted from UTC to appear in your own time zone.

My backend ruby code:

time_zone = 'Europe/Moscow'
task_time_start_utc = task.date_time
task_time_start_moscow = task_time_start_utc.in_time_zone(time_zone)

task_time_end_utc = task_time_start_utc + task.duration.minutes
task_time_end_moscow = task_time_end_utc.in_time_zone(time_zone)

event_property = {
    summary: task.name,
    location: "#{task.lat} #{task.lng}",
    description: string_work_times,
    start: {
        date_time: task_time_start_moscow.to_formatted_s(:iso8601),
        time_zone: time_zone
    },
    end: {
        date_time: task_time_end_moscow.to_formatted_s(:iso8601),
        time_zone: time_zone
    }
}