gpt4 book ai didi

c# - DDay Ical 库是否正确计算重复规则?

转载 作者:太空狗 更新时间:2023-10-29 21:12:09 24 4
gpt4 key购买 nike

我已经进行了一些测试。这是我的代码:

var systemTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var icalTimeZone = iCalTimeZone.FromSystemTimeZone(systemTimeZone);

var startTimeSearch = new DateTime(2015, 9, 8, 0, 0, 0, DateTimeKind.Utc);
var endTimeSearch = new DateTime(2015, 12, 1, 00, 0, 0, DateTimeKind.Utc);

var iCalendar = new iCalendar();
var pacificTimeZone = _iCalendar.AddTimeZone(icalTimeZone);

var event = new Event
{
Summary = "This is an event at 2015-09-08 10:30 PST (2015-09-08 17:30 UTC)",
DTStart = new iCalDateTime(2015, 9, 8, 10, 30, 0, pacificTimeZone.TZID, iCalendar),
Duration = new TimeSpan(0, 1, 0, 0)
};

var rp = new RecurrencePattern("FREQ=WEEKLY;UNTIL=20151112T080000Z;WKST=SU;BYDAY=TU");

event.RecurrenceRules.Add(rp);
iCalendar.Events.Add(_event);
var occurrences = iCalendar.GetOccurrences(startTimeSearch, endTimeSearch);

结果如下:

出现 10 次 - 这是出现的 Period.StartTime.Value 和 UTC 属性

- 9/8/2015 10:30:00 - UTC 9/8/2015 17:30:00
- 9/15/2015 10:30:00 - UTC 9/15/2015 17:30:00
- 9/22/2015 10:30:00 - UTC 9/22/2015 17:30:00
- 9/29/2015 10:30:00 - UTC 9/29/2015 17:30:00
- 10/6/2015 10:30:00 - UTC 10/6/2015 17:30:00
- 10/13/2015 10:30:00 - UTC 10/13/2015 17:30:00
- 10/20/2015 10:30:00 - UTC 10/20/2015 17:30:00
- 10/27/2015 10:30:00 - UTC 10/27/2015 17:30:00
-11/3/2015 10:30:00 - UTC 11/3/2015 17:30:00 (这应该是 18:30:00 !)
- 11/10/2015 10:30:00 - UTC 11/10/2015 17:30:00 (这应该是 18:30:00 !)

enter image description here

如您所见,最后两个的 UTC 时间应为 18:30:00。所以我想知道这是否只是 DDay Ical 的工作方式,我不应该指望它能正确获得 UTC 日期时间,或者我是否做错了什么。

最佳答案

由于夏令时,您会得到这个时差。一种可能的解决方案是不通过名称 Pacific Standard Time

获取时区
 // First load a file containing time zone information for Pacific Standard Time
var systemTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

代替这个使用系统时区

var systemTimeZone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault();

或者简单地添加本地时区:

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

要查找所有已注册的时区,click here

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

我只是在我的代码中使用 GetSystemTimeZones()

public static void Test1()
{
var systemTimeZone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault();
//TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var icalTimeZone = iCalTimeZone.FromSystemTimeZone(systemTimeZone);

var startTimeSearch = new DateTime(2015, 9, 8, 0, 0, 0, DateTimeKind.Utc);
var endTimeSearch = new DateTime(2015, 12, 1, 00, 0, 0, DateTimeKind.Utc);

var iCalendar = new iCalendar();
var pacificTimeZone = iCalendar.AddTimeZone(icalTimeZone);

var _event =
new Event
{
Summary = "This is an event at 2015-09-08 10:30 PST (2015-09-08 17:30 UTC)",
DTStart = new iCalDateTime(2015, 9, 8, 10, 30, 0, pacificTimeZone.TZID, iCalendar),
Duration = new TimeSpan(0, 1, 0, 0)
};

var rp = new RecurrencePattern("FREQ=WEEKLY;UNTIL=20151112T080000Z;WKST=SU;BYDAY=TU");

_event.RecurrenceRules.Add(rp);
iCalendar.Events.Add(_event);

var occurrences = iCalendar.GetOccurrences(startTimeSearch, endTimeSearch);
}

当我调试时它得到了类似的输出

stack

您可以相应地更改时区并获得结果。希望对你有帮助。

关于c# - DDay Ical 库是否正确计算重复规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com