简体   繁体   中英

Preventing Google Calendar API V3 from changing event times C#

I'm really running into some issues with timezones & haven't had luck with other similar questions.

Basically I want to create & share a Google Calendar event between two people, but I want the DateTime I provide for Start/End to be EXACTLY that & not converted for either of them. I'm in UTC-5, and when I test the events I'm receiving them 5 hours in the past, even though I set the timezone to UTC, strip the "Z" from the date, and set DateTimeKind to local...

The things I've tried:

DateTimeRaw = DateTime.ParseExact(appointment.Start.ToString("MM/dd/yyyy HH:mm"), "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture).ToString("yyyy-MM-ddTHH:mm:ss").Replace("Z", "")

And then the above but with.ToUniversalTime() added to appointment

DateTime = new DateTime(appointment.Start.Year, appointment.Start.Month, ....)

And also...

DateTime.SpecifyKind(DateTime.ParseExact(appointment.Start.ToString("MM/dd/yyyy HH:mm"), "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture), DateTimeKind.Local)

I also tried the above with DateTimeKind.UTC

I've basically been trying a ton of combinations and then decided to stop because I don't really know why it's not working...

Is it possible to preserve the exact same time for all users in Google Calendar? I had used the ICAL.NET library before this & made my own.ics events and it worked perfectly, but the same DateTime code for GCal seems to not work as expected.

Edit:

Some more background on the reasoning for what I'm trying to do. I'm working on a SaaS scheduling application that deals with in-person appointments, but we have no knowledge of where specifically the locations will be. The way we address this is that on the UI & in the stored values every appointment is in UTC, so they see 5pm, select 5pm, stored as 5pm, and eventually should be shared as a Google Calendar event for 5pm. If the user is in a different timezone it will still show 5pm (as everything is UTC), but the expectation is that they'll be in the timezone of the scheduled location for the actual appointment.

We're switching to Google Calendar from sending.ics attachments and the same code for Start/End breaks for Google Calendar specifically.

Google Calendar

When you create a calendar event, you create it by default on your current timezone.

If you want multiple users to have the same event, at the same time, you just need to make sure that each user has their calendar settings on the correct timezone .

If you want to create an event on another timezone that is not your own, you can specify the timezone to the API .

The time zone in which the time is specified. (Formatted as an IANA Time Zone Database name, eg "Europe/Zurich".) For recurring events this field is required and specifies the time zone in which the recurrence is expanded. For single events this field is optional and indicates a custom time zone for the event start/end.

Edit

If you are creating your own scheduling application and want to display these events on Google Calendar you can embedded a Calendar with a specific time-zone.

However, if you are dealing with multiple users that might have different time-zones, it's important to account for that. Your application design is flawed in that aspect and does not support these use-cases.

You mentioned that you are selling a scheduling SaaS application, so I'll use that to generate a business-case to justify my thoughts:

Imagine you have a class with 1 professor (in the US), 1 student (also in the US) and two other students from Japan.

The professor will schedule a class at 1PM at this local time.

Since the class is online, all students need to be available at that time, regardless of their timezone.

If I want to add an event to everyone's calendar, I won't be marking 1PM on every calendar. I will be marking the equivalent time on the user's timezone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM