NCronJob icon indicating copy to clipboard operation
NCronJob copied to clipboard

Time zone support

Open linkdotnet opened this issue 1 year ago • 4 comments
trafficstars

Allow specifying time zones for job execution instead of relying solely on UTC.

linkdotnet avatar Apr 11 '24 15:04 linkdotnet

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.

falvarez1 avatar Apr 17 '24 16:04 falvarez1

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);

linkdotnet avatar Apr 17 '24 16:04 linkdotnet

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.

falvarez1 avatar Apr 17 '24 17:04 falvarez1

#22

falvarez1 avatar Apr 17 '24 17:04 falvarez1