HangfireIO / Cronos

A fully-featured .NET library for working with Cron expressions. Built with time zones in mind and intuitively handles daylight saving time transitions
MIT License
1.01k stars 118 forks source link

Feature request: Method for checking if now is an Active Period #69

Closed ixm7 closed 4 months ago

ixm7 commented 4 months ago

This is similar to https://github.com/HangfireIO/Cronos/issues/58 but, instead of checking if now is a valid occurrence, the need is for a method to check if now is an active period.

Use case: a service has its own heartbeat for each task, but the user wants to specify active periods for some tasks (e.g. weekdays between 8 and 5). Outside that active period, the task should not run. The requested method should make it easy to determine if Now is an active vs inactive period. The method could have the following signature: Expression.IsActive(DateTime RefUtcDateTime, Integer ResolutionInSeconds)

If the RefUtcDateTime (typically it would be Now) is within the specified number of seconds from the next and previous occurrences, we are in an active period, and the method should return True.

odinserj commented 4 months ago

You can use the GetOccurrences method and pass DateTime.UtcNow and DateTime.UtcNow.AddSeconds(X) values there with the inclusive flag set, and return true if there are any occurrences within the specified interval.

ixm7 commented 4 months ago

Thanks; I wasn't aware of that option.