Migrating from dday.ical to ical.net: how to use a timezone in Event Period

1.6k Views Asked by At

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?

1

There are 1 best solutions below

1
On

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:

var calendar = Calendar.LoadFromStream(new StringReader(icsString)).First();
var searchStart = DateTime.Parse("2006-12-09T07:00:00");
var searchEnd = DateTime.Parse("2007-12-12T23:00:00");
var occurrences = calendar.GetOccurrences(searchStart, searchEnd);

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.

BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:Test Calendar
X-WR-TIMEZONE:Europe/Berlin
BEGIN:VTIMEZONE
TZID:Europe/Berlin
X-LIC-LOCATION:Europe/Berlin
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;TZID=Europe/Berlin:20061211T070000
DURATION:PT3600S
RRULE:FREQ=DAILY;WKST=MO
DTSTAMP:20061223T162148Z
UID:[email protected]
CLASS:PUBLIC
CREATED:20061215T212453Z
DESCRIPTION:
LAST-MODIFIED:20061215T212453Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Zähne putzen
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR