Make it easier to specify calendar event times in target timezone
See original GitHub issueRepro
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using System;
using System.IO;
class Program
{
static CalendarService Service;
static void Main()
{
var key = File.ReadAllText("service-account-key.json");
var credential = GoogleCredential.FromJson(key).CreateScoped(CalendarService.Scope.Calendar)
.CreateWithUser("me@mydomain.com");
Service = new CalendarService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "TimeDemo"
});
DateTime start, end;
start = new DateTime(2017, 8, 14, 9, 0, 0, DateTimeKind.Local); // 9am local
end = start.AddHours(1);
AddEvent("Local", start, end);
start = new DateTime(2017, 8, 14, 9, 0, 0, DateTimeKind.Utc); // 9am UTC
end = start.AddHours(1);
AddEvent("Utc", start, end);
start = new DateTime(2017, 8, 14, 9, 0, 0, DateTimeKind.Unspecified); // 9am unspecified
end = start.AddHours(1);
AddEvent("Unspecified", start, end);
}
static void AddEvent(string title, DateTime start, DateTime end)
{
var ev = new Event {
Summary = title,
Start = new EventDateTime { DateTime = start, TimeZone = "Europe/London" },
End = new EventDateTime { DateTime = end, TimeZone = "Europe/London" }
};
Service.Events.Insert(ev, "primary").Execute();
}
}
Expected result
Create the events at 9am London time, taking into account daylight savings, because the TimeZone was specified in the API as “Europe/London”. Or at least do this for the “Unspecified” event.
Actual result
- Creates a “Utc” event at 9am UTC (10am London time).
- Creates “Local” and “Unspecified” events at 9am in the timezone of the client machine. If I change my computer clock or deploy to a server in a different timezone, these events end up at a different time - not good!
- The selected TimeZone of “(GMT+01:00) London” is displayed when I open any of the events in Google Calendar.
Question
I understand that this is probably by design, but it gave me a real headache! I wonder if there’s a way the API could make it easier to say “Create an event at 9am in Europe/London”?
At the moment I’m doing this; there’s probably a more extensible way but it works for now.
static TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
static string ConvertToDateTimeRaw(DateTime date)
{
var suffix = tzi.IsDaylightSavingTime(date) ? "+01:00" : "Z";
return date.ToString("yyyy-M-dTHH:mm:ss") + suffix;
}
static void AddEvent(string title, DateTime start, DateTime end)
{
var ev = new Event {
Summary = title,
Start = new EventDateTime { DateTimeRaw = ConvertToDateTimeRaw(start) },
End = new EventDateTime { DateTimeRaw = ConvertToDateTimeRaw(end) }
};
Service.Events.Insert(ev, "primary").Execute();
}
Suggestion
How about a ZonedDateTime
property, which creates an event at the specified DateTime
(ignoring its DateTimeKind
) in the specified timezone?
var ev = new Event {
Summary = title,
Start = new EventDateTime { ZonedDateTime = new ZonedDateTime(start, "Europe/London") },
End = new EventDateTime { ZonedDateTime = new ZonedDateTime(end, "Europe/London") }
};
Issue Analytics
- State:
- Created 6 years ago
- Comments:9
Top Results From Across the Web
Calendar Timezone Changes
You can create events and view calendars in other time zones by turning on time zone support in Calendar on iCloud.com.
Read more >Use different time zones in Calendar on Mac
In the Calendar app on your Mac, choose Calendar > Settings, then click Advanced. Select “Turn on time zone support.” Double-click the event,...
Read more >Setting timezones for individual events - Pages & Content
Just tell the client "Set every event Start/End time based on your own timezone". Then add that three-letter tag to the CSS. Live...
Read more >Google calendar API all-day events timezone
In my application, I have user's calendars representations that people can schedule on by selecting from free time that is available to them....
Read more >How to find and select a time-zone in Google Calendar ...
When interactively (not programmatically) creating an event in Google Calendar, I can click on the time-zones link to see a list of 100s...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okay, here’s a proposal for how we could do this:
We’ll need to check that we can get handle unspecified values coming back from the API as well.
Main drawback:
Alternative:
I’m not sure what Json.NET does when parsing this - whether it populated DateTime or DateTimeRaw. More experimentation would be required.
Given that:
I think we will not do any work on this issue; so closing. Please re-open if you disagree.