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

标签 c# icalendar recurring-events dday

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

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/

相关文章:

ruby-on-rails - Gmail 中不存在 ics 附件

ios - 如何实现两周模式的日历 View (使用JTCalendar示例)

date - 如何管理特定本地日期的所有时区用户的重复事件?

c# - 用 ">"替换列选择会删除代码行

c# - 连接问题

c# - 即使对于 32 位,“编辑并继续”在 VS2010 中也不起作用

calendar - 为什么 EWS 托管 API SyncFolderItems 不返回定期约会

方法调用中的 C# 空检查链

calendar - 如何发送添加日历事件(非邀请)的电子邮件?

php - 在每个月的 "nth"工作日重复事件