2014-03-06 96 views
0

當試圖創建一個iCal文件時,我收到一個我無法跟蹤的非常奇怪的問題。以下是使用的代碼以及設置爲從08:00開始到11:00結束的事件示例。該文件會創建相關信息,但在嘗試將其添加到Outlook時,一小時內會添加到結束時間。iCal創建時間偏移

DateTime eventDate = DateTime.Parse("19/06/2014"); 
DateTime startTime = DateTime.Parse("09:00:00"); 
DateTime endTime = DateTime.Parse("11:00:00"); 
string location = "Test Location"; 
string title = "Test Title"; 

context.Response.ContentType = "text/x-vcalendar"; 
string filename = String.Format("attachment; filename={0}.ics", eventname.Replace(" ", "-")); 
context.Response.AddHeader("Content-disposition", filename); 

context.Response.Write("BEGIN:VCALENDAR" + Environment.NewLine); 
context.Response.Write("VERSION:2.0" + Environment.NewLine); 
context.Response.Write("METHOD:PUBLISH" + Environment.NewLine); 

context.Response.Write("BEGIN:VTIMEZONE" + Environment.NewLine); 
context.Response.Write("TZID:Europe/London" + Environment.NewLine); 
context.Response.Write("X-LIC-LOCATION:Europe/London" + Environment.NewLine); 
context.Response.Write("BEGIN:DAYLIGHT" + Environment.NewLine); 
context.Response.Write("TZOFFSETFROM:+0000" + Environment.NewLine); 
context.Response.Write("TZOFFSETTO:+0100" + Environment.NewLine); 
context.Response.Write("TZNAME:BST" + Environment.NewLine); 
context.Response.Write("DTSTART:19700329T010000" + Environment.NewLine); 
context.Response.Write("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU" + Environment.NewLine); 
context.Response.Write("END:DAYLIGHT" + Environment.NewLine); 
context.Response.Write("BEGIN:STANDARD" + Environment.NewLine); 
context.Response.Write("TZOFFSETFROM:+0100" + Environment.NewLine); 
context.Response.Write("TZOFFSETTO:+0000" + Environment.NewLine); 
context.Response.Write("TZNAME:GMT" + Environment.NewLine); 
context.Response.Write("DTSTART:19701025T020000" + Environment.NewLine); 
context.Response.Write("RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU" + Environment.NewLine); 
context.Response.Write("END:STANDARD" + Environment.NewLine); 
context.Response.Write("END:VTIMEZONE" + Environment.NewLine); 

context.Response.Write("BEGIN:VEVENT" + Environment.NewLine); 
context.Response.Write("ORGANIZER:MAILTO: [email protected]" + Environment.NewLine); 
context.Response.Write("UID: [email protected]" + Environment.NewLine);    
context.Response.Write("DTSTART:" + startDate.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine); 
context.Response.Write("DTEND:" + GetMeetingEndDate(startDate, endDate).ToString("yyyyMMddTHHmmssZ") + Environment.NewLine); 
context.Response.Write("DTSTAMP:" + DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine); 
context.Response.Write("SUMMARY:" + subject + Environment.NewLine); 
context.Response.Write("DESCRIPTION:" + description + Environment.NewLine); 
context.Response.Write("LAST-MODIFIED:" + DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine); 
context.Response.Write("PRIORITY:5" + Environment.NewLine); 
context.Response.Write("LOCATION;ENCODING=QUOTED-PRINTABLE:" + location + Environment.NewLine); 
context.Response.Write("CLASS:PUBLIC" + Environment.NewLine); 
context.Response.Write("END:VEVENT" + Environment.NewLine); 

context.Response.Write("END:VCALENDAR"); 

這樣做的結果是任命被添加到Outlook與09:00的開始時間和12:00結束時間。結束時間增加了一個小時。

請注意,有關代碼是用於英國/格林威治時間使用。

我已調試此過程,並檢查所有日期,因爲它們正在設置,一切都是正確的。有什麼我失蹤了嗎?我真的不希望不得不在最後一小時強制縮減,以便它適當地添加到Outlook中。

編輯:

以下是GetMeetingEndDate功能。

DateTime GetMeetingEndDate(DateTime startDate, DateTime endDate) 
    { 
     DateTime newDate = new DateTime(); 
     if (endDate < startDate) 
     { 
      newDate = endDate.AddHours(12); 
     } 
     else if (endDate == startDate) 
     { 
      newDate = startDate.AddDays(1).AddHours(-1); 
     } 
     else 
     { 
      newDate = endDate; 
     } 
     return newDate; 
    } 

謝謝。

+0

GetMeetingEndDate函數在做什麼?你能告訴我們它的實現嗎? – anuragal

+0

我已經編輯了'GetMeetingEndDate'函數的主要問題。 – SalfordCC

回答

1

在下面的代碼: -

context.Response.Write("DTSTART:" + startDate.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine); 
context.Response.Write("DTEND:" + GetMeetingEndDate(startDate, endDate).ToString("yyyyMMddTHHmmssZ") + Environment.NewLine); 

DTSTART設置爲值startDate.ToUniversalTime()而在功能GetMeetingEndDate日期而沒有轉換成UTC通過。由於這個startdate總是正確的,但iCal會將您當地的enddate視爲UTC日期。這可能會導致增加額外小時的問題。解決方法是將下面的代碼塊更改爲

context.Response.Write("DTEND:" + GetMeetingEndDate(startDate.ToUniversalTime(), endDate.ToUniversalTime()).ToString("yyyyMMddTHHmmssZ") + Environment.NewLine); 
+0

@SalfordCC - 這是否解決了您的問題? – anuragal

+0

修好了,非常感謝!踢自己,我以前沒有發現!接受爲答案。再次感謝! – SalfordCC