1

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?

rianjs
  • 7,767
  • 5
  • 24
  • 40
  • It's not clear what you're trying to do. Messing with the time zones is a means to an end, but what end? What is the real thing you're trying to achieve? Compute a recurrence set? Convert a series of events from one time zone to another? Something else? – rianjs Nov 03 '16 at 17:50
  • I have added the purpose of my code in the post. I hope this is making things clear. –  Nov 03 '16 at 20:43
  • I refactored a lot of the time zone code in ical.net. Behind the scenes it's using NodaTime, so time zone conversions should be faster and more accurate than what dday.ical had before. Are you sure you need to do anything with time zones at all? You are asking how to work around a bug that existed in dday.ical that probably doesn't exist in ical.net. Again, what are you trying to achieve? Compute the time your thermostat should do an action? Is that based on a set of recurrence rules? – rianjs Nov 04 '16 at 00:20
  • You have interpreted it right Rian. I want to compute the time that my thermostat should do an action based on calendar events. I wasn't aware of the bug you mention. Perhaps this workaround is no longer necessary but after migrating to ical.net the time is again one hour 'off'. So perhaps something goes wrong in my code. I have to investigate that. –  Nov 07 '16 at 08:54
  • Start a new thread "My events from Google Calendar are happening one hour off the time I expect them to" with code snippets that show the calendar parsing, ingesting the event, and interpreting the times of the occurrences. I suspect the bug is in your application, not the library. – rianjs Nov 08 '16 at 15:22

1 Answers1

0

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:594oeajmftl3r9qlkb476rpr3c@google.com
CLASS:PUBLIC
CREATED:20061215T212453Z
DESCRIPTION:
LAST-MODIFIED:20061215T212453Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Zähne putzen
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
rianjs
  • 7,767
  • 5
  • 24
  • 40
  • I wasn't aware of this bug so perhaps indeed I don't need to do something special for a TimeZone. I have to investigate this further. What I now can see is that after migrating to ical.net the times are again one hour 'off'. But perhaps the cause is now something else. –  Nov 07 '16 at 08:49