2

Hi I am migrating from DDay.ical to Ical.Net nuget pacakages but I get stuck in the following code which add Timezone in DDay.Ical calendar please help

Previous code:

List<DOAppointment> lst = objResponse.Appointments;
string timeZoneName = objResponse.UserTimezone; 
iCalendar calendar = new DDay.iCal.iCalendar();
var timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName); 
calendar.AddTimeZone(iCalTimeZone.FromSystemTimeZone(timeZone));

Migrating into Ical.Net:

  List<DOAppointment> lst = objResponse.Appointments;
  string timeZoneName = objResponse.UserTimezone;
  Ical.Net.Calendar calendar = new Ical.Net.Calendar();
  var  timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName); 
  ITimeZone tzID = timeZone;
  calendar.AddTimeZone(tzID);

Here I know that calendar.AddTimezone will take ITimezone but how to pass it I am not getting please help.

rianjs
  • 7,767
  • 5
  • 24
  • 40
Tanmay
  • 590
  • 8
  • 20

1 Answers1

1

VTimeZone is the concrete implementation of the ITimeZone interface. The ical.net example would look like this:

var calendar = new Ical.Net.Calendar();

// ical.net supports BCL time zones as well as IANA time zones
calendar.AddTimeZone(new VTimeZone(objResponse.UserTimezone));

In the future, I may change the VTimeZone constructor to throw an exception if the time zone string doesn't match anything known. Right now, though, it's pretty dumb. Behind the scenes, ical.net will default to the system time zone that the code is running on, if all else fails. That's probably not good.

I also added a wiki page with an adaptation of your question:

https://github.com/rianjs/ical.net/wiki/Working-with-time-zones

rianjs
  • 7,767
  • 5
  • 24
  • 40
  • It will work similar as my previous code?haven't debug it yet need to do some changes but it will do the same right as previous code is written by someone else n I am just migrating it i don't know much but what i get is taking the timezone of user and by that getting the system timezone – Tanmay Aug 25 '16 at 14:45
  • 1
    Yeah, those lines are taking a user's time zone, and trying to find the corresponding Windows time zone. ical.net supports a broader range of time zones, so unless the person has provided a string like "not a real time zone", you should be OK. And even in that case, it will fall back to the time zone the system is running, so the user might get unexpected results, but it won't explode at runtime. – rianjs Aug 25 '16 at 15:15