I am migrating my application from dday.ical to ical.net and am struggling with TimeZones.
I managed to rewrite adding the TimeZone from
IICalendarCollection calendarCollection = iCalendar.LoadFromUri(new Uri(GoogleCalendarUrl));
IICalendar calendar = calendarCollection.FirstOrDefault();
string timeZone = "<some timezone>";
if (!string.IsNullOrWhiteSpace(timeZone))
{
System.TimeZoneInfo timezoneinfo = System.TimeZoneInfo.FindSystemTimeZoneById(timeZone);
calendar.AddTimeZone(timezoneinfo);
}
to the ical.net equivalent
IICalendarCollection calendarCollection = LoadFromUri(new Uri(GoogleCalendarUrl));
ICalendar calendar = calendarCollection.FirstOrDefault();
string timeZone = "<some timezone>";
if (!string.IsNullOrWhiteSpace(timeZone))
{
((Calendar)calendar).AddTimeZone(new VTimeZone(timeZone));
}
But I have no clue on how to use the TimeZone in ical.net
My dday.ical code for using the TimeZone is
iCalTimeZone timezone = null;
if (!string.IsNullOrWhiteSpace(TimeZone))
{
System.TimeZoneInfo timezoneinfo = System.TimeZoneInfo.FindSystemTimeZoneById(TimeZone);
timezone = iCalTimeZone.FromSystemTimeZone(timezoneinfo);
}
Occurrence occurrence = <filled from other code part>;
IEvent iEvent = occurrence.Source as IEvent;
IPeriod period = occurrence.Period;
if ((iEvent != null) && (period != null))
{
if (!string.IsNullOrWhiteSpace(TimeZone))
{
period.StartTime.SetTimeZone(timezone);
period.EndTime.SetTimeZone(timezone);
}
DateTime localStartTime = period.StartTime.Local;
DateTime localEndTime = period.EndTime.Local;
// Do something with local start and end time.
// ...
}
The purpose of my code is to read a private Google Calendar which contains Scheduled events for heating my house (at 19:00 it should be 19 degrees celcius etc) and to use these events to control a heating device.
The events in the Google Calendar have the TimeZone '(GMT+01:00) Amsterdam'.
In the DDay.Cal code the Local property of the StartTime and EndTime for an IEvent were one hour off because of the Amsterdam TimeZone. The code as written above fixed this by setting the TimeZone of the StartTime and EndTime properties of IEvent. This corrected the time, in this case for one hour.
Can anyone please help me on how to (re)write the mentioned use of the TimeZone to the ical.net equivalent?
I think you're trying to work around a bug that exists in dday.ical, but doesn't exist in ical.net.
I think you're trying to get your thermostat to do an action based on a set of recurrence rules. Maybe something like "Set the temp to __C at 06:00" then "Set the temp to __C at 09:00" which would be two events in a calendar that repeats daily on work days. Maybe with a different set of events for weekends. (That's how I would do it anyway.)
You would do that like this:
Each occurrence in
occurrences
has the correct IANA time zone (Europe/Amsterdam), and will do the right thing during seasonal clock changes.. There are no time zone workarounds required.Contents of
icsString
(which is from Google Calendar) is below, and taken from one of the unit tests in ical.net.