10

我想每天早上 8 点(UTC 时间)解雇一份工作。问题是触发器不尊重我告诉它的时间。

我的代码如下:

    ITrigger trigger = TriggerBuilder.Create()
        .WithDailyTimeIntervalSchedule(
             s => s.WithIntervalInHours(24)
                 .OnEveryDay()
                 .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8,0)))
        .Build();

   var times = TriggerUtils.ComputeFireTimes(trigger as IOperableTrigger, null, 10);

   foreach (var time in times)
       Console.WriteLine(time.UtcDateTime);

输出是:

30/09/2013 10:00:00 PM
1/10/2013 10:00:00 PM
2/10/2013 10:00:00 PM
3/10/2013 10:00:00 PM
4/10/2013 10:00:00 PM
5/10/2013 10:00:00 PM
6/10/2013 9:00:00 PM
7/10/2013 9:00:00 PM
8/10/2013 9:00:00 PM
9/10/2013 9:00:00 PM

6日的时间改变的原因是夏令时从本周末开始。

我如何让它像我告诉它的那样在 UTC 时间上午 8 点触发?

编辑:这太疯狂了,它甚至可以使用 Cron 时间表:

ITrigger trigger = TriggerBuilder.Create()
    .WithCronSchedule("0 0 8 * * ?")
    .Build();

var times = TriggerUtils.ComputeFireTimes(trigger as IOperableTrigger, null, 10);

foreach (var time in times)
    Console.WriteLine(time.UtcDateTime);

输出:

30/09/2013 10:00:00 PM
1/10/2013 10:00:00 PM
2/10/2013 10:00:00 PM
3/10/2013 10:00:00 PM
4/10/2013 10:00:00 PM
5/10/2013 9:00:00 PM
6/10/2013 9:00:00 PM
7/10/2013 9:00:00 PM
8/10/2013 9:00:00 PM
9/10/2013 9:00:00 PM
4

2 回答 2

22

在拉下源代码并深入研究之后,我找到了几个解决方案。

我最初找到了这样的解决方案:

var trigger4 = new DailyTimeIntervalTriggerImpl
{
    StartTimeUtc = DateTime.UtcNow,
    StartTimeOfDay = new TimeOfDay(8, 0, 0),
    RepeatIntervalUnit = IntervalUnit.Hour,
    RepeatInterval = 24,
    TimeZone = TimeZoneInfo.Utc
};

...这导致我调整了我的原始代码:

ITrigger trigger2 = TriggerBuilder.Create()
    .WithDailyTimeIntervalSchedule(
        s => s.WithIntervalInHours(24)
            .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 0))
            .InTimeZone(TimeZoneInfo.Utc))
    .Build();

这两个都给了我想要的东西。我只是希望这个库有一些像样的文档。

于 2013-09-30T12:10:58.010 回答
6

虽然这是旧的,但我还是会在这里回答。您可以简单地执行以下操作:

ITrigger trigger = TriggerBuilder.Create() .WithCronSchedule("0 0 8 * * ?", cron => { cron.InTimeZone(TimeZoneInfo.Utc); } ) .Build();

于 2017-04-25T16:31:50.560 回答