-->

how to handle user timezone for daylight savings i

2019-08-17 14:43发布

问题:

My service api takes in startDate for quartz Job and day of Month for the job to executed. Internally, I convert this to cron expression and save in quartz.

For example, a user in PST submits a job request today (Nov 3 2017) as follows.

{
"start": "2017-11-03T18:00:00-07:00",
"dayOfMonth" : 15
}

Here the user wants to schedule a job that fires on 15th of each month at 6 PM, starting from 2017-11-03. so the first-day quartz will fire will be 2017-11-15. This is how the above request gets converted to cron expression 0 0 18 15 * ? *, which is correct. Here is how, QRTZ_CRON_TRIGGERS table looks like.

As you notice, time_zone_id is saved as GMT-07:00, but once daylight savings kick-in on Nov 5, it must be GMT-08:00. or else my quartz job will fire one hour earlier. In fact, when I query for nextFireTime, I do get 1510794000000 which is indeed Wednesday November 15, 2017 17:00:00 (pm) in time zone America/Los Angeles (PST)

how do we handle this time_zone_id issue?

P.S: I am using cronTrigger which does not have the notion of preserveHourOfDayAcrossDaylightSavings that is provided by CalendarIntervalTrigger.

回答1:

Do not use offset to represent the timezone. Rather you can ask the user to pass in timezone like "America/Los_Angeles". Then you can use http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronScheduleBuilder.html#inTimeZone(java.util.TimeZone) to create trigger with proper timezone.

inTimeZone(TimeZone.getTimeZone("USER_ENTERED_VALUE")

Finally when you look at QRTZ_CRON_TRIGGERS table, the value for TIME_ZONE_ID will be America/Los_Angeles