简体   繁体   English

Quartz.Net中的Cron触发器以获取小时值

[英]Cron trigger in Quartz.Net for hourly values

I was checking out cron triggers for Quartz.Net and noticed that when I use any other trigger apart from hourly, it offsets the Time to UTC(which should always be the case), but when I do the same for an Hourly cron, it picks the local time. 我正在检查Quartz.Net的cron触发器,并注意到当我使用除小时以外的任何其他触发器时,它会将时间偏移到UTC(应该总是这样),但是当我对Hourd cron执行相同操作时选择当地时间。

For example 例如

Suppose my Start Time is 2014-05-31 15:44:00 假设我的开始时间是2014-05-31 15:44:00

For Hourly Schedule for the next 6 occasions 对于接下来的6次每小时计划

Cron Expression: 0 0 0/1 1/1 * ? * Cron表达式: 0 0 0/1 1/1 * ? * 0 0 0/1 1/1 * ? *

is given as 给出为

5/31/2014 5:30:00 PM +00:00
5/31/2014 6:30:00 PM +00:00
5/31/2014 7:30:00 PM +00:00
5/31/2014 8:30:00 PM +00:00
5/31/2014 9:30:00 PM +00:00
5/31/2014 10:30:00 PM +00:00

This shows the time in localtime. 这显示了当地时间。

But when I try say a weekly schedule 但是当我尝试说每周时间表

Cron: 0 44 15 ? * SUN,MON * Cron: 0 44 15 ? * SUN,MON * 0 44 15 ? * SUN,MON *

The schedule comes up as 日程安排如下

6/1/2014 10:14:00 AM +00:00
6/2/2014 10:14:00 AM +00:00
6/8/2014 10:14:00 AM +00:00
6/9/2014 10:14:00 AM +00:00
6/15/2014 10:14:00 AM +00:00
6/16/2014 10:14:00 AM +00:00

Which is offset to UTC, which is right. 这与UTC偏移,这是正确的。

This is the code which I am using 这是我正在使用的代码

var cron = new Quartz.CronExpression("0 0 0/1 1/1 * ? *");
DateTimeOffset? nextFire = cron.GetNextValidTimeAfter(Convert.ToDateTime("5/31/2014 4:30:00 PM"));

Obviously, I change the parameters dynamically. 显然,我是动态更改参数的。

This is the piece of code which does the schedule calculation 这是执行时间表计算的代码

  var jobItem = (DbContext.jobs.Where(job => job.Id == id)).FirstOrDefault();

                List<DateTimeOffset> scheduleTimes = new List<DateTimeOffset>();

                var time = DateTimeOffset.Now;

                if (!string.IsNullOrEmpty(jobItem.CronExpression))
                {
                    for (int i = 0; i <= 5; i++)
                    {
                        var date = GetScheduleForCron(jobItem.CronExpression, time);
                        scheduleTimes.Add(date.Value);
                        time = date.Value;
                    }

                }
                else
                {
                    var jobAttribute =
                           (from attribute in DbContext.jobattributes
                            where attribute.JobId == jobItem.Id
                            select attribute.SpecificDate).FirstOrDefault();

                    scheduleTimes.Add(jobAttribute.Value);

                }


    public static DateTimeOffset? GetScheduleForCron(string cronexpression, DateTimeOffset date)
    {
        var cron = new CronExpression(cronexpression);
        return cron.GetNextValidTimeAfter(date.DateTime);
    }

EDIT: I used http://www.cronmaker.com/ to cross verify my schedule. 编辑:我使用http://www.cronmaker.com/交叉验证我的日程安排。

You might be confusing the usage of local and UTC times. 您可能会混淆本地时间和UTC时间的使用。 Especially if you have Convert.ToDateTime("5/31/2014 4:30:00 PM") , DateTimeOffset.Now or date.DateTime . 尤其是如果您具有Convert.ToDateTime("5/31/2014 4:30:00 PM")DateTimeOffset.Nowdate.DateTime The General rule with Quartz is that Quartz的一般规则是

UTC times go in, UTC times come out UTC时间进来,UTC时间走出来

. Here's sample that runs your case with expected output (used the non-builder API for brevity): 这是示例,可为您的案例提供预期的输出(为简洁起见,使用了非builder API):

var firstTrigger = new CronTriggerImpl();
firstTrigger.CronExpressionString = "0 0 0/1 1/1 * ? *";
firstTrigger.StartTimeUtc = new DateTime(2014, 05, 31, 15, 44, 00).ToUniversalTime();
Console.WriteLine("first trigger fire times:");
Console.WriteLine(string.Join(Environment.NewLine, TriggerUtils.ComputeFireTimes(firstTrigger, null, 6).Select(x => x.ToLocalTime())));

var secondTrigger = new CronTriggerImpl();
secondTrigger.CronExpressionString = "0 44 15 ? * SUN,MON *";
secondTrigger.StartTimeUtc = new DateTime(2014, 05, 31, 15, 44, 00).ToUniversalTime();
Console.WriteLine("second trigger fire times:");
Console.WriteLine(string.Join(Environment.NewLine, TriggerUtils.ComputeFireTimes(secondTrigger, null, 6).Select(x => x.ToLocalTime())));

It will output (in my GMT+3 locale, cron trigger runs on current locale's time zone by default): 它将输出(在我的GMT + 3语言环境中,cron触发器默认在当前语言环境的时区运行):

first trigger fire times
 31.5.2014 16:00:00 +03:00
 31.5.2014 17:00:00 +03:00
 31.5.2014 18:00:00 +03:00
 31.5.2014 19:00:00 +03:00
 31.5.2014 20:00:00 +03:00
 31.5.2014 21:00:00 +03:00
second trigger fire times:
 1.6.2014 15:44:00 +03:00
 2.6.2014 15:44:00 +03:00
 8.6.2014 15:44:00 +03:00
 9.6.2014 15:44:00 +03:00
 15.6.2014 15:44:00 +03:00
 16.6.2014 15:44:00 +03:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM