WeekdayInMonth.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Globalization;
  2. using JetBrains.Annotations;
  3. namespace Content.Server.Holiday.ShouldCelebrate
  4. {
  5. /// <summary>
  6. /// For a holiday that happens the first instance of a weekday on a month.
  7. /// </summary>
  8. [UsedImplicitly]
  9. public sealed partial class WeekdayInMonth : DefaultHolidayShouldCelebrate
  10. {
  11. [DataField("weekday")] private DayOfWeek _weekday = DayOfWeek.Monday;
  12. [DataField("occurrence")] private uint _occurrence = 1;
  13. public override bool ShouldCelebrate(DateTime date, HolidayPrototype holiday)
  14. {
  15. // Not the needed month.
  16. if (date.Month != (int) holiday.BeginMonth)
  17. return false;
  18. // Occurrence NEEDS to be between 1 and 4.
  19. _occurrence = Math.Max(1, Math.Min(_occurrence, 4));
  20. var calendar = new GregorianCalendar();
  21. var d = new DateTime(date.Year, date.Month, 1, calendar);
  22. for (var i = 1; i <= 7; i++)
  23. {
  24. if (d.DayOfWeek != _weekday)
  25. {
  26. d = d.AddDays(1);
  27. continue;
  28. }
  29. d = d.AddDays(7 * (_occurrence-1));
  30. return date.Day == d.Day;
  31. }
  32. return false;
  33. }
  34. }
  35. }