首页 > 解决方案 > 谷歌日历 API 红宝石,日期时间时区

问题描述

我有这个代码。它在我的谷歌日历中创建了一个事件。

    client = Signet::OAuth2::Client.new(client_options)
    client.update!(session[:authorization])

    service = Google::Apis::CalendarV3::CalendarService.new
    service.authorization = client

    event = Google::Apis::CalendarV3::Event.new({
        start: Google::Apis::CalendarV3::EventDateTime.new(
            date_time: cf_event.starts_at.to_datetime.rfc3339,
            time_zone: 'America/Monterrey'
        ),
        end: Google::Apis::CalendarV3::EventDateTime.new(
            date_time: cf_event.ends_at.to_datetime.rfc3339,
            time_zone: 'America/Monterrey'  
        ),

        summary: cf_event.title
    })

这是一个基本示例,我的cf_event对象具有starts_atattr,即 timeWithZone。

所以我不得不这样做cf_event.starts_at.to_datetime.rfc3339

输出“2019-03-15T17:50:00+00:00”

问题是,代码创建了事件,但有 -6 小时。

如果我去谷歌日历我的活动是第 15 天 (11 - 11:50),而不是 (17 - 17:50)

我究竟做错了什么?

标签: rubygoogle-calendar-api

解决方案


您正在输入时间17:50:00UTC+0+00:00您的date_time代表UTCUTC+0),但是,您指定了time_zone您使用蒙特雷的地方,它遵循时区UTC-6

我的猜测是,您的代码17:50:00输入UTC+0然后将其转换为蒙特雷时区UTC-6,结果为11:50:00. 尝试删除+00:00或尝试制作它-06:00


推荐阅读