Need local time instead of UTC

I need to schedule recurring jobs based on local time, for example 02:00 CET (which is UTC+1 normally and UTC+2 during daylight savings time aka “summer time”).
I know this introduces some problems when switching to/from summer to winter time (there is an hour which does not exist and another one that exists twice), but I’m sure these could be dealed with.
As a current workaround I could probably create separate cron expressions for summer and winter time, but as the switching dates change from year to year, I’d have to do this for each year separately, which is not an option.
I’m surprised nobody seems to have asked for this yet, but I understand that Hangfire was built around “fire-and-forget” tasks primarily and the recurring tasks are not a core feature.
If I wanted to implemented this myself (or at least give it a try) where would I start?

Any feedback appreciated!

2 Likes

Just found out that the required changes actually had been made in this commit, but reverted again in this one. Anyone knows why?

Local time oriented schedules would be very nice. We have some task which should be executed at set times, like midnight on a daylight savings based machine.

It would be nice if we could specify the time zone next to the Cron expression, as suggested here:
Wikipedia : Cron > Timezone handling

Hello Guys, I am facing the same problem. I am adding the ScheduledJob but it is not getting added to run by the local time of the machine.

CreatedAt : 2014-11-26 11:09:38.753

and wanted to run it after 15 minutes, which is showing the time of execution like this in database (table : HangFire.State)

{“EnqueueAt”:“2014-11-26T23:34:16.4661024Z”,“ScheduledAt”:“2014-11-26T11:09:38.2330512Z”}

there are no other scheduled job at the time when I add this job.

Can anyone help me?

@odinserj Could you maybe shed light on this feature request? I wonder what prevents Hangfire from taking timezones into account. A desired very common tasks is just to execute a task after midnight (local time). Because the runner is in a time-zone that uses daylight savings time, I cannot just correct the task by a fixed number of hours.

I would also be interested in this as well. We have a need where a customer is sending a file at a specific time and need to run it immediately after they send it (which is at 12:30AM Central time) so we want to make sure our recurring task executes at 12:45AM Central in both CDT and CST throughout the year.

Hi!
I’ve the same problem, but it seems that nobody has found the solution yet.
I’m able to schedule my recurring jobs using cron expressions, but the expressions are intended to be used using my current time (UTC +2), while the server uses UTC time.
How can I resolve this mismatch?

This feature is already implemented, but available only in pre-release version 1.4.0-beta1. So you can use the following syntax to work with time zones:

RecurringJob.AddOrUpdate("UTC", () => Console.WriteLine("UTC"), "15 18 * * *", TimeZoneInfo.Utc);

RecurringJob.AddOrUpdate("Russian", () => Console.WriteLine("Russian"), "15 21 * * *", TimeZoneInfo.Local);

RecurringJob.AddOrUpdate(
    "Hawaiian", 
    () => Console.WriteLine("Hawaiian"),  
    "15 08 * * *", 
    TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time"));

Here are some facts about current implementation, they will be moved to the docs:

  • Default time zone is UTC. This may be confusing for users of Quartz.NET, Cron program in Linux, etc, that use local time by default. But switch to local time by default will introduce breaking changes, so it will be performed in version 2.0.
  • Windows Time Zone IDs are being used to store time zones. So there may be some issues when your Hangfire infrastructure contains both Windows and Linux. I thought about using NodaTime and its DateTimeProviders.Tzdb that uses IANA time zone ids, but it is overkill. Custom time zone providers may be implemented for Hangfire to handle this issue later.
  • Issues with Mono. TimeZoneInfo in Mono has some critical flaws in the latest stable Mono release at the time of this writing (2.10.8). TimeZoneInfo.Local has particular issues on the latest tested version of Mono. On Windows, TimeZoneInfo.Local throws TimeZoneNotFoundException. On Unix it returns a TimeZoneInfo with an Id of “Local”, which isn’t terribly useful (although it may contain the correct rules). – from http://nodatime.org/1.3.x/userguide/mono.html. This may be solved later, with custom time zone providers.
3 Likes

Great, It works! Thanks!
One last question. From version 1.4.0beta1 the app configuration is changed, in fact UseHangfire is now obsolete.
I didn’t find nothing about UseHangfireServer and UseHangfireDashboard methods, how should I use them to configure the app?

Yep, there is no documentation about these methods yet. Here is my classes:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        GlobalConfiguration.Configuration
            .UseLogProvider(new ColouredConsoleLogProvider())
            .UseSqlServerStorage(@"server=.\sqlexpress;database=hangfire.pro;integrated security=sspi;");

        app.UseErrorPage();
        app.UseHangfireServer();
        app.UseHangfireDashboard();
    }
}

Perfect, thank you very much!