How do I set the timezone in an iCal feed using DDay.iCal?

我怕爱的太早我们不能终老 提交于 2019-12-21 05:07:48

问题


I'm creating an iCal feed using DDay.iCal. It works, but I can't figure out how to set the timezone for the feed. Here's the basic code:

iCalendar iCal = new iCalendar();

// <-- Set the Timezone HERE to PST (Pacific Daylight Time)

Event evt = iCal.Create<Event>();

evt.Start = new iCalDateTime(meeting.MeetDate);
evt.End = evt.Start.AddHours(4); // 4 hour event
evt.Description = "This meeting...";
evt.Summary = "Event Summary";

Any ideas?


回答1:


In the other answer, the author fails to mention the line above those three lines that's in example 6:

// First load a file containing time zone information for North & South America
IICalendar timeZones = iCalendar.LoadFromFile("America.ics")[0];

So that won't work. An option would be:

iCalendar iCal = new iCalendar();

System.TimeZoneInfo timezoneinfo = System.TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
iCalTimeZone timezone = iCalTimeZone.FromSystemTimeZone(timezoneinfo);
iCal.AddTimeZone(timezone);

Or simply add the local timezone:

iCalendar iCal = new iCalendar();
iCal.AddLocalTimeZone();

To find all registered timezones, use this snippet:

ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
Console.WriteLine("The local system has the following {0} time zones", zones.Count);
foreach (TimeZoneInfo zone in zones.OrderBy(z => z.Id))
    Console.WriteLine(zone.Id);

Console.ReadLine();



回答2:


Example6 in the download is setting timezones and whatnot for events. Check that out.

Relevant lines:

IICalendar iCal = new iCalendar();
iCal.AddChild(timeZones.GetTimeZone("America/New_York"));
iCal.AddChild(timeZones.GetTimeZone("America/Denver"));            

// Set the event to start at 11:00 A.M. New York time on January 2, 2007.
evt.Start = new iCalDateTime(2007, 1, 2, 11, 0, 0, "America/New_York", iCal)


来源:https://stackoverflow.com/questions/3774548/how-do-i-set-the-timezone-in-an-ical-feed-using-dday-ical

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!