Cron trigger time based on UTC

1,840 views
Skip to first unread message

Krug

unread,
Feb 14, 2008, 12:45:56 PM2/14/08
to Quartz.NET
Hello,

I am having some trouble getting the cron trigger to start when I
want it to. I know the problem is based with the TimeZone. I am
creating the triggers in code, not via a XML file. Here is the code.

CronTrigger cronTrigger = new
CronTrigger(this.ComplexTriggerName.Text, this.ComplexGroupName.Text);
DateTime startDate = DateTime.Parse(this.ComplexStartTime.Text);
cronTrigger.Name = this.ComplexTriggerName.Text;
cronTrigger.Group = this.ComplexGroupName.Text;
cronTrigger.StartTimeUtc = startDate;
cronTrigger.CronExpressionString = this.ComplexCronString.Text;

If my cron expression is something like 0 15 10 ? * *" my next
schedule run time is not at 10:15am but instead at 5:15 pm. I am in
Mountain Standard Time which is a 7 hour difference from UTC so that
explains it. But my question is how do I keep that from happening. I
have read other post that use XML for there scheduler classes and in
there you can have an offset on you time. I am not sure how to do
that when you have a trigger created in code. Any help would be much
appreciated.

Thanks

Jeff

Marko Lahma

unread,
Feb 14, 2008, 5:30:24 PM2/14/08
to quar...@googlegroups.com
Hi Jeff,

So what you'd change this:

> DateTime startDate = DateTime.Parse(this.ComplexStartTime.Text);

to:

DateTime startDate =
DateTime.Parse(this.ComplexStartTime.Text).ToUniversalTime();

This has the assumption that you are running your server in the
desired time zone. This is a big hassle in .NET world compared to Java
world. .NET 3.5 has remedy for this but for Quartz.NET it's
unfortunately still a big leap to make.

-Marko

Mike Hooper

unread,
Sep 10, 2016, 2:58:01 PM9/10/16
to Quartz.NET
Marko,

I hate to dredge up such an old post, but I'm having this same problem. And the fix you suggested for Jeff doesn't seem to work for me.

I am getting a DateTime value from a DateTimePicker in C# .NET 4.0. I parse this value to construct a cron trigger. However, when I schedule to job the start time is +4 hours from the value used to create the trigger.
Here's my code. The value of "time" comes from the DateTimePicker.
 DateTime correctedTime = time.ToUniversalTime();//attempting to correct the time offset.
            time = correctedTime;

            string minuteString = time.Minute.ToString();
            string hourString = time.Hour.ToString();
            string dayString = time.DayOfWeek.ToString("D");
            string timeString = "0 " + minuteString + " " + hourString + " ? * " + dayString;
                            //Second - Minute - Hour - DayOfMonth - Month - DayOfWeek - Year

            ITrigger trigger = (ITrigger)TriggerBuilder.Create()
               .WithIdentity(triggerString, "group1")
               .WithCronSchedule(timeString)
               .ForJob(thisJobKey)
               .Build();

            scheduler.ScheduleJob(thisJob, trigger);     

Upon examining the trigger start time, it is +4 hours from the original value of "time".

Any thoughts or suggestions?

Thanks,

Mike
Reply all
Reply to author
Forward
0 new messages