/// <summary> /// Determine with this time of day is before the given time of day. /// </summary> /// <param name="timeOfDay"></param> /// <returns>True this time of day is before the given time of day.</returns> public bool Before(TimeOfDay timeOfDay) { if (timeOfDay.hour > hour) { return true; } if (timeOfDay.hour < hour) { return false; } if (timeOfDay.minute > minute) { return true; } if (timeOfDay.minute < minute) { return false; } if (timeOfDay.second > second) { return true; } if (timeOfDay.second < second) { return false; } return false; // must be equal... }
public static DailyTimeIntervalScheduleBuilder Around(this DailyTimeIntervalScheduleBuilder x, TimeOfDay time) { return x.OnEveryDay() .StartingDailyAt(time).WithRepeatCount(0) .EndingDailyAt(new TimeOfDay(time.Hour, time.Minute + 5)); }
/// <summary> /// Calculate and set the EndTimeOfDay using count, interval and StarTimeOfDay. This means /// that these must be set before this method is call. /// </summary> /// <param name="count"></param> /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns> public DailyTimeIntervalScheduleBuilder EndingDailyAfterCount(int count) { if (count <= 0) { throw new ArgumentException("Ending daily after count must be a positive number!"); } if (startTimeOfDayUtc == null) { throw new ArgumentException("You must set the StartDailyAt() before calling this EndingDailyAfterCount()!"); } DateTimeOffset today = SystemTime.UtcNow(); DateTimeOffset startTimeOfDayDate = startTimeOfDayUtc.GetTimeOfDayForDate(today).Value; DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59).GetTimeOfDayForDate(today).Value; //apply proper offsets according to timezone TimeZoneInfo targetTimeZone = timeZone ?? TimeZoneInfo.Local; startTimeOfDayDate = new DateTimeOffset(startTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(startTimeOfDayDate.DateTime, targetTimeZone)); maxEndTimeOfDayDate = new DateTimeOffset(maxEndTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(maxEndTimeOfDayDate.DateTime, targetTimeZone)); TimeSpan remainingMillisInDay = maxEndTimeOfDayDate - startTimeOfDayDate; TimeSpan intervalInMillis; if (intervalUnit == IntervalUnit.Second) { intervalInMillis = TimeSpan.FromSeconds(interval); } else if (intervalUnit == IntervalUnit.Minute) { intervalInMillis = TimeSpan.FromMinutes(interval); } else if (intervalUnit == IntervalUnit.Hour) { intervalInMillis = TimeSpan.FromHours(interval); } else { throw new ArgumentException("The IntervalUnit: " + intervalUnit + " is invalid for this trigger."); } if (remainingMillisInDay < intervalInMillis) { throw new ArgumentException("The startTimeOfDay is too late with given Interval and IntervalUnit values."); } long maxNumOfCount = (remainingMillisInDay.Ticks / intervalInMillis.Ticks); if (count > maxNumOfCount) { throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount); } TimeSpan incrementInMillis = TimeSpan.FromTicks((count - 1) * intervalInMillis.Ticks); DateTimeOffset endTimeOfDayDate = startTimeOfDayDate.Add(incrementInMillis); if (endTimeOfDayDate > maxEndTimeOfDayDate) { throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount); } DateTime cal = SystemTime.UtcNow().Date; cal = cal.Add(endTimeOfDayDate.TimeOfDay); endTimeOfDayUtc = TimeOfDay.HourMinuteAndSecondOfDay(cal.Hour, cal.Minute, cal.Second); return(this); }
/// <summary> /// Set the startTimeOfDay for this trigger to end firing each day at the given time. /// </summary> /// <param name="timeOfDayUtc"></param> /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns> public DailyTimeIntervalScheduleBuilder EndingDailyAt(TimeOfDay timeOfDayUtc) { endTimeOfDayUtc = timeOfDayUtc; return(this); }
static void Main(string[] args) { #if DEBUG Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info }; #endif bool exit = false; TimeOfDay restart = new TimeOfDay(0, 0); if (args.Length == 2) { int hour, minute; if(int.TryParse(args[0], out hour) && int.TryParse(args[1], out minute)) { if(hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59) { restart = new TimeOfDay(hour, minute); } else { Console.WriteLine("First parameter can be an hour (between 0 and 23), second one a minute (between 0 and 59)"); } } } Console.WriteLine("Restart will be daily at {0:00}:{1:00}", restart.Hour, restart.Minute); IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule (s => s.WithIntervalInHours(24) .OnEveryDay() .StartingDailyAt(restart) ) .Build(); IJobDetail job = JobBuilder.Create<RestartJob>().WithIdentity("RestartJob","CWSJobs").Build(); scheduler.ScheduleJob(job, trigger); while (!exit) { if (c.Test()) { Console.WriteLine("CWSCronJobs is connected to CWSRestart"); var result = c.GetWatcherStatus(); bool resultStatus = false; if (result != null && bool.TryParse(result["ENABLED"].ToString(), out resultStatus)) { if (resultStatus) Console.WriteLine("The watcher is running"); else Console.WriteLine("The watcher is suspended"); } } Console.WriteLine("Press \"q\" to quit"); string input = Console.ReadLine(); if (string.Equals("q", input, StringComparison.InvariantCultureIgnoreCase)) exit = true; } scheduler.Shutdown(); c.Dispose(); }
/// <summary> /// Set the trigger to begin firing each day at the given time. /// </summary> /// <param name="timeOfDayUtc"></param> /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns> public DailyTimeIntervalScheduleBuilder StartingDailyAt(TimeOfDay timeOfDayUtc) { startTimeOfDayUtc = timeOfDayUtc ?? throw new ArgumentException("Start time of day cannot be null!"); return(this); }