Specialday calculator

Date: 2019-08-29

Attentiondays

public class Specialday
    {
        public Specialday(string name, DateTime date, bool legalOmission = false)
        {
            Name = name;
            Date = date;
            LegalOmission = legalOmission;
        }
        public string Name { get; set; }
        public DateTime Date { get; set; }
        public bool LegalOmission { get; set; }
    }

    public class SpecialdayCalculator
    {
        public static IEnumerable<int> EachYear(int fromYear, int toYear)
        {
            while (fromYear <= toYear)
            {
                yield return fromYear++;
            }
        }

        public IEnumerable<Specialday> GetSpecialdays(int fromYear, int toYear)
        {
            foreach (var year in EachYear(fromYear, toYear))
            {
                foreach (var day in GetSpecialdays(year))
                {
                    yield return day;
                }
            }
        }

        public IEnumerable<Specialday> GetSpecialdays(int year)
        {
            var specialdays = new List<Specialday>
            {
                new Specialday("Nieuwjaarsdag", new DateTime(year, 1, 1), true),
                new Specialday("Bevrijdingsdag", new DateTime(year, 5, 5)),
                new Specialday("1e Kerstdag", new DateTime(year, 12, 25), true),
                new Specialday("2e Kerstdag", new DateTime(year, 12, 26), true)
            };

            var kingsDate = new DateTime(year, 4, 27);
            if (kingsDate.DayOfWeek == DayOfWeek.Sunday) kingsDate = kingsDate.AddDays(-1);
            specialdays.Add(new Specialday("Koningsdag", kingsDate, true));

            var easterDate = GetEasterSunday(year);
            specialdays.Add(new Specialday("Goede Vrijdag", easterDate.AddDays(-2)));
            specialdays.Add(new Specialday("1e Paasdag", easterDate, true));
            specialdays.Add(new Specialday("2e Paasdag", easterDate.AddDays(1), true));

            specialdays.Add(new Specialday("Hemelvaartsdag", easterDate.AddDays(39), true));
            specialdays.Add(new Specialday("1e Pinksterdag", easterDate.AddDays(49), true)); // 10 dagen na hemelvaart
            specialdays.Add(new Specialday("2e Pinksterdag", easterDate.AddDays(50), true));

            specialdays.Add(new Specialday("Oudjaarsdag", new DateTime(year + 1, 1, 1).AddDays(-1)));
            return specialdays.OrderBy(d => d.Date);
        }

        /// <summary>
        /// Hier snapt niemand wat van, maar we gaan er vanuit dat het werkt
        /// </summary>
        /// <param name="year"></param>
        public static DateTime GetEasterSunday(int year)
        {
            int g = year % 19;
            int c = year / 100;
            int h = (c - c / 4 - (8 * c + 13) / 25 + 19 * g + 15) % 30;
            int i = h - h / 28 * (1 - h / 28 * (29 / (h + 1)) * ((21 - g) / 11));
            int day = i - ((year + year / 4 + i + 2 - c + c / 4) % 7) + 28;
            int month = 3;
            if (day > 31)
            {
                month++;
                day -= 31;
            }
            return new DateTime(year, month, day);
        }
    }

25370cookie-checkSpecialday calculator