Why Google Calendar API returns timeZone=UTC although it's incorrect?

巧了我就是萌 提交于 2019-12-24 08:31:37

问题


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
    }
}


来源:https://stackoverflow.com/questions/25179267/why-google-calendar-api-returns-timezone-utc-although-its-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!