Fernando
Fernando

Reputation: 127

Google calendar API ruby, datetime timezone

I have this code. which creates a event in my google calendar.

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

It is a basic example my cf_event object has the starts_at attr, which is a timeWithZone.

so i had to do cf_event.starts_at.to_datetime.rfc3339.

Output "2019-03-15T17:50:00+00:00"

The problem is, that the code creates, the event but with -6 hours.

If i go to google calendars my event it´s the day 15 at (11 - 11:50), and not (17 - 17:50)

what am i doing wrong?

Upvotes: 1

Views: 319

Answers (1)

Jacque
Jacque

Reputation: 765

You are inputting the time as 17:50:00 in UTC+0 (the +00:00 from your date_time stands for UTC or UTC+0), however, you specified a time_zone where you used Monterrey which follows the time zone UTC-6.

My guess here is that, your code takes 17:50:00 in UTC+0 then converts it to Monterrey time zone UTC-6 which results to 11:50:00. Either try removing the +00:00 or try making it -06:00.

Upvotes: 1

Related Questions