C# 创建时间偏移

C# 创建时间偏移,c#,asp.net,outlook,icalendar,C#,Asp.net,Outlook,Icalendar,当试图创建一个iCal文件时,我遇到了一个我无法追踪的非常奇怪的问题。以下是使用的代码和设置为08:00开始、11:00结束的事件示例。该文件创建时包含相关信息,但在尝试将其添加到Outlook时,已将一小时添加到结束时间 DateTime eventDate = DateTime.Parse("19/06/2014"); DateTime startTime = DateTime.Parse("09:00:00"); DateTime endTime = DateTime.Parse("11:

当试图创建一个iCal文件时,我遇到了一个我无法追踪的非常奇怪的问题。以下是使用的代码和设置为08:00开始、11:00结束的事件示例。该文件创建时包含相关信息,但在尝试将其添加到Outlook时,已将一小时添加到结束时间

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: test@domain.com" + Environment.NewLine);
context.Response.Write("UID: test2@domain.com" + 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。结束时间增加了一个小时

请注意,关于的代码仅供英国/GMT使用

我已经调试了这个过程,并检查了所有的日期,因为他们正在设置,一切都是正确的。这个有什么我遗漏的吗?我真的不想在最后一个小时强制减少,只是为了让它正确地添加到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;
    }
DateTime GetMeetingEndDate(DateTime startDate,DateTime endDate)
{
DateTime newDate=新日期时间();
如果(结束日期<开始日期)
{
newDate=endDate.AddHours(12);
}
else if(endDate==startDate)
{
newDate=startDate.AddDays(1.AddHours(-1);
}
其他的
{
newDate=endDate;
}
返回newDate;
}
谢谢。

在下面的代码中:-

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);

GetMeetingEndDate
函数在做什么?你能给我们展示一下它的实现吗?我已经用
GetMeetingEndDate
函数的实现编辑了主要问题。这就解决了它,非常感谢!我真后悔以前没发现!被接受为答案。再次感谢!