Skip to content

Commit

Permalink
Merge pull request #62 from adibalwani/wufrequency
Browse files Browse the repository at this point in the history
POA WUFrequency support for MonthlyByWeekAndDay
  • Loading branch information
anantshankar17 authored Apr 29, 2020
2 parents 7749fff + 4441f7a commit b75db46
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ enum Frequency
Daily = 2,
Weekly = 3,
Monthly = 4,
Hourly = 5
Hourly = 5,
MonthlyByWeekAndDay = 6
}

/// <summary>
Expand Down Expand Up @@ -80,10 +81,11 @@ class ServiceSettings
/// The frequency for installing Windows Update.
/// The format and Possible Values :
/// 1. Monthly,5,12:22:32
/// 2. Weekly,Tuesday,12:22:32
/// 3. Daily,12:22:32
/// 4. Once,12-12-2017,12:22:32
/// 5. None
/// 2. MonthlyByWeekAndDay,2,Friday,21:00:00
/// 3. Weekly,Tuesday,12:22:32
/// 4. Daily,12:22:32
/// 5. Once,12-12-2017,12:22:32
/// 6. None
/// </summary>
public string WUFrequency { get; set; }

Expand All @@ -99,6 +101,10 @@ class ServiceSettings

public DayOfWeek DayOfWeek { get; private set; }

public int WeekOfMonth { get; private set; }

public TimeSpan Time { get; private set; }

public DateTime Date { get; private set; }

public bool IsLastDayOfMonth { get; private set; }
Expand Down Expand Up @@ -161,6 +167,22 @@ private void ParseWUFrequency()
}
break;

case Frequency.MonthlyByWeekAndDay:
if (arr.Count() != 4)
{
throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency);
}

this.WeekOfMonth = (int)Convert.ChangeType(arr[1], typeof(int));
this.DayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), arr[2]);
this.Time = TimeSpan.Parse(arr[3]);

if (this.WeekOfMonth < 1 || this.WeekOfMonth > 4)
{
throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency + ". The WeekOfMonth should be between 1 to 4.");
}
break;

case Frequency.Weekly:
if (arr.Count() != 3)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,18 @@ private DateTime GetNextSchedulingTime()
return next;
}

case Frequency.MonthlyByWeekAndDay:
{
var next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime, settings);

if (DateTime.Compare(next, currentTime) < 0)
{
next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime.AddMonths(1), settings);
}

return next;
}

case Frequency.Weekly:
{
DateTime next = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second);
Expand Down Expand Up @@ -684,6 +696,17 @@ private DateTime GetNextSchedulingTime()
}
}

private DateTime GetNextDateTimeForMonthlyByWeekAndDayFrequency(DateTime currentTime, ServiceSettings settings)
{
var firstDayOfMonth = new DateTime(currentTime.Year, currentTime.Month, day: 1);
var firstWeekOfMonth = firstDayOfMonth.AddDays((settings.DayOfWeek + 7 - firstDayOfMonth.DayOfWeek) % 7);
var daysToAdd = 7 * (settings.WeekOfMonth - 1);
var dayOfMonth = firstWeekOfMonth.AddDays(daysToAdd);
var dayOfMonthWithTime = dayOfMonth + settings.Time;

return dayOfMonthWithTime;
}

class CheckpointFileData
{
public DateTime schedulingDateTime = default(DateTime);
Expand Down

0 comments on commit b75db46

Please sign in to comment.