c# - Cron trigger in Quartz.Net for hourly values -
i checking out cron triggers quartz.net , noticed when use other trigger apart hourly, offsets time utc(which should case), when same hourly cron, picks local time.
for example
suppose start time 2014-05-31 15:44:00
for hourly schedule next 6 occasions
cron expression:
0 0 0/1 1/1 * ? *
is given
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 time in localtime.
but when try weekly schedule
cron:
0 44 15 ? * sun,mon *
the schedule comes as
6/1/2014 10:14:00 +00:00 6/2/2014 10:14:00 +00:00 6/8/2014 10:14:00 +00:00 6/9/2014 10:14:00 +00:00 6/15/2014 10:14:00 +00:00 6/16/2014 10:14:00 +00:00
which offset utc, right.
this code 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, change parameters dynamically.
this piece of code 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)) { (int = 0; <= 5; i++) { var date = getscheduleforcron(jobitem.cronexpression, time); scheduletimes.add(date.value); time = date.value; } } else { var jobattribute = (from attribute in dbcontext.jobattributes 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: used http://www.cronmaker.com/ cross verify schedule.
you might confusing usage of local , utc times. if have convert.todatetime("5/31/2014 4:30:00 pm")
, datetimeoffset.now
or date.datetime
. general rule quartz
utc times go in, utc times come out
. here's sample runs case expected output (used non-builder api brevity):
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 output (in gmt+3 locale, cron trigger runs on current locale's time zone default):
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
Comments
Post a Comment