Quartz.net在UTC时间创建每日计划

时间:2013-09-29 23:23:07

标签: c# .net quartz.net

我每天早上8点准时开始工作。问题是触发器不尊重我告诉它的时间。

我的代码如下:

    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

2 个答案:

答案 0 :(得分:15)

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

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

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

这些都给了我我追求的东西。我只是希望这个库有一些不错的文档。

答案 1 :(得分:2)

虽然这已经过时了,但无论如何我都会在这里回答。您只需执行以下操作:

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