NCronJob
NCronJob copied to clipboard
Time zone support
Allow specifying time zones for job execution instead of relying solely on UTC.
How would you want users to configure their TimeZone? In appSettings (IConfiguration) or through the JobOptionsBuilder? Something like this:
builder.Services.AddNCronJob(n => n
.AddJob<PrintHelloWorldJob>(
p => p.WithCronExpression("*/2 * * * *",timeZoneInfo: TimeZoneInfo.Local)
.WithParameter("Hello from NCronJob"))
);
Part of the challenge is that NCronTab itself does not support different time zones. Are you open to switching out the NCronTab library with Cronos? Cronos is fully compatible with NCronTab and supports TimeZones.
I much prefer the configuration explicitly in code - so basically what you provided as example. I don't have any strong opinion whether or not NCronTab is the way to evaluate cron schedules.
My naive approach would have been to translate UTC time to the given TimeZoneInfo object provided by the user.
var userLocalTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, timeZoneFromJob);
var runDate = entry.CrontabSchedule!.GetNextOccurrence(userLocalTime);
Yeah I thought about just doing a simple conversion but there's a bunch of nuance with time-zone changes, leap years etc. that you can avoid. I don't know this for certain but I've been bit more than once trying to handle my own conversions.
#22