/// <summary> /// Build the actual Trigger -- NOT intended to be invoked by end users, /// but will rather be invoked by a TriggerBuilder which this /// ScheduleBuilder is given to. /// </summary> /// <returns></returns> public override IMutableTrigger Build() { DailyTimeIntervalTriggerImpl st = new DailyTimeIntervalTriggerImpl { RepeatInterval = interval, RepeatIntervalUnit = intervalUnit, MisfireInstruction = misfireInstruction, RepeatCount = repeatCount, TimeZone = timeZone }; if (daysOfWeek != null) { st.DaysOfWeek = new ReadOnlyCompatibleHashSet <DayOfWeek>(daysOfWeek); } else { st.DaysOfWeek = new ReadOnlyCompatibleHashSet <DayOfWeek>(AllDaysOfTheWeek); } if (startTimeOfDayUtc != null) { st.StartTimeOfDay = startTimeOfDayUtc; } else { st.StartTimeOfDay = TimeOfDay.HourAndMinuteOfDay(0, 0); } if (endTimeOfDayUtc != null) { st.EndTimeOfDay = endTimeOfDayUtc; } else { st.EndTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59); } return(st); }
/// <summary> /// Build the actual Trigger -- NOT intended to be invoked by end users, /// but will rather be invoked by a TriggerBuilder which this /// ScheduleBuilder is given to. /// </summary> /// <returns></returns> public override IMutableTrigger Build() { DailyTimeIntervalTriggerImpl st = new DailyTimeIntervalTriggerImpl(); st.RepeatInterval = interval; st.RepeatIntervalUnit = intervalUnit; st.MisfireInstruction = misfireInstruction; st.RepeatCount = repeatCount; if (daysOfWeek != null) { st.DaysOfWeek = daysOfWeek; } else { st.DaysOfWeek = AllDaysOfTheWeek; } if (startTimeOfDayUtc != null) { st.StartTimeOfDay = startTimeOfDayUtc; } else { st.StartTimeOfDay = TimeOfDay.HourAndMinuteOfDay(0, 0); } if (endTimeOfDayUtc != null) { st.EndTimeOfDay = endTimeOfDayUtc; } else { st.EndTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59); } return(st); }
/// <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); DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59).GetTimeOfDayForDate(today); //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> /// 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 = DateTime.UtcNow; DateTimeOffset startTimeOfDayDate = startTimeOfDayUtc.GetTimeOfDayForDate(today).Value; DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59).GetTimeOfDayForDate(today).Value; TimeSpan remainingMillisInDay = maxEndTimeOfDayDate - startTimeOfDayDate; TimeSpan intervalInMillis = TimeSpan.Zero; if (intervalUnit == IntervalUnit.Second) { intervalInMillis = TimeSpan.FromSeconds(interval); } else if (intervalUnit == IntervalUnit.Minute) { intervalInMillis = TimeSpan.FromSeconds(interval * 60); } else if (intervalUnit == IntervalUnit.Hour) { intervalInMillis = TimeSpan.FromSeconds(interval * 60 * 24); } 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 = DateTime.UtcNow.Date; cal = cal.Add(endTimeOfDayDate.TimeOfDay); endTimeOfDayUtc = TimeOfDay.HourMinuteAndSecondOfDay(cal.Hour, cal.Minute, cal.Second); return(this); }