使用 ICal.net,我使用以下代码序列化日历:
var calendarSerializer = new CalendarSerializer();
var attachmentContent = calendarSerializer.SerializeToString(myCalendar);
当我序列化以 UTC 定义的日期时间(例如,晚上 11:00)时,我得到以下信息:
20240330T230000Z
当我在午夜 12:00 连载时,我得到:
VALUE=DATE:20240331
当我导入日历时,这让 MS-Outlook 感到困惑。
我需要 Midnight 像任何其他 DateTime 一样进行序列化,并且我需要它包含时区,以便它正确转换为本地时间。
这是我用来创建日历对象的代码:
var calendar = new Calendar
{
Method = "PUBLISH",
ProductId = "MyProduct 6/8",
Version = "1.0"
};
var calendarEvent = new CalendarEvent
{
Sequence = 1,
Url = new Uri(location),
Uid = eventId.ToString(),
Description = null, // details added as property
Summary = eventName,
Location = location,
Start = new CalDateTime(startDateTimeUtc, "UTC"),
End = new CalDateTime(endDateTimeUtc, "UTC")
};
添加此:
calendarEvent.Start.HasTime = true;
calendarEvent.End.HasTime = true;
强制序列化器保证时间和时区的存在,即使它只是“000000Z”。这解决了我在导入 MS-Outlook 日历时遇到的问题。