question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it easier to specify calendar event times in target timezone

See original GitHub issue

Repro

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:closed
  • Created 6 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
jskeetcommented, Aug 14, 2017

Okay, here’s a proposal for how we could do this:

  • Change the code generator to make everything partial
  • If there’s any build infrastructure that completely wipes out directories, be more selective so that any manually written code is preserved
  • Add a Discovery doc patch entry to remove the start/end times so we can write our own code there
  • Add the property back into EventDateTime, but also another property for “PreserveUnspecifiedDateTime” or something similar

We’ll need to check that we can get handle unspecified values coming back from the API as well.

Main drawback:

  • Users will have to specify PreserveUnspecifiedDateTime for each event. Unless we can autodetect, they’ll need to set it on received events too

Alternative:

  • Have a static property to determine the behavior, and either modify the global behavior of GetDateTimeFromString etc, or use the partial technique above to use it just for EventDateTime. (It could be a static property in that class.)

I’m not sure what Json.NET does when parsing this - whether it populated DateTime or DateTimeRaw. More experimentation would be required.

0reactions
chrisdunelmcommented, Dec 8, 2017

Given that:

  • This repo is in maintenance mode.
  • There is a workaround for this problem.
  • A solution to this is moderately complex.

I think we will not do any work on this issue; so closing. Please re-open if you disagree.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found