How can I set the time zone as null in Google Calendar Integration?

岁酱吖の 提交于 2021-01-28 07:01:23

问题


I have written a code in ASP.NET MVC that integrates with google calendar API and sets an event into the user's calendar. My Code is :

 public static string CreateEvent(CalendarEventViewModel model)
        {
            string Id = "";
            // List events.
            Event newEvent = new Event()
            {
                Summary = model.Summary,
                Location = model.Location,
                Description = model.Description,
                Start = new EventDateTime()
                {
                    DateTime = DateTime.Parse(model.StartDateTime),
                    TimeZone = "America/Los_Angeles",
                },
                End = new EventDateTime()
                {
                    DateTime = DateTime.Parse(model.EndDateTime),
                    TimeZone = "America/Los_Angeles",
                },
                Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=1" },
                Attendees = new EventAttendee[] 
                {
                     new EventAttendee() { Email = model.CompanyEmailAddress },
                },
                Reminders = new Event.RemindersData()
                {
                    UseDefault = false,
                    Overrides = new EventReminder[] 
                    {
                        new EventReminder() { Method = "email", Minutes = 24 * 60 },
                        new EventReminder() { Method = "sms", Minutes = 10 },
                    }
                }
            };

            String calendarId = "primary";
            if (model.IsInsert)
            {
              Event createdEvent =   SharedService.service.Events.Insert(newEvent, calendarId).Execute();
              Id = createdEvent.Id;
            }
            else
            {
                SharedService.service.Events.Update(newEvent, calendarId, model.Id).Execute();
                Id = "";
            }
            return Id;
        }

It works perfectly but here I need to remove the timezone or set it to null TimeZone = "America/Los_Angeles", because the admin and user's time zone are different. For example:- If admins set the event for 5:00 am, then the time for events in the user's google calendar also must be 5:00 am due to the timezone currently there are different. How Can I do this?

I tried removing the timezone part from the code but I got an error saying the timezone was missing.


回答1:


Issue:

You need to specify a timeZone, either in start.dateTime or in start.timeZone. From the API docs:

start.dateTime: 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.

Explanation:

A timezone is needed in order to specify a specific dateTime for the event. It indicates which timezone the dateTime refers to. For example, let's say the time is 5:00 AM. 5:00 AM where? In LA? In Nepal? 5:00 AM in LA is not the same time as 5:00 AM in Nepal. And a specific event can only start at a specific time. If it starts at 5:00 AM in LA, it starts at (I think) 5:45 PM in Kathmandu. You cannot have a specific event starting both at 5:00 AM in LA and in Kathmandu: that's two different times!

So, the following is not possible. If two users (and their Calendars) are in different timezones, a specific event will show in different times:

For example:- If admins set the event for 5:00 am, then the time for events in the user's google calendar also must be 5:00 am due to the timezone currently there are different.

Of course, you can always change the time zone for your Calendar (see section Change the time zone of one calendar here), but this won't change the actual time an event starts; it will just show the start times in a different time zone. An event starting at 5:00 AM LA time will still start at 5:45 PM Nepal time.

If you really want an event to start at 5:00 AM both in LA and in Kathmandu time, you will have to create two different events.



来源:https://stackoverflow.com/questions/64346734/how-can-i-set-the-time-zone-as-null-in-google-calendar-integration

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