Menü Schließen

Calendar tools and algorithms

Calculating a leap year

The correct calculation of a leap year was first introduced by pope Gregor in 1582. The Gregorian Calendar is still commonly used in most parts of the world. Before that, the leap years were simply defined being every 4th year.

public static bool IsLeapYear(int year)
{
  if (year % 100 == 0) 
  {
    if (year % 400 == 0) 
    {
      return true;
    }
    else if (year % 4 == 0)
    {
      return true;
    }
  }
  return false;
}

The algorithm is fairly simple:

  • If the year is divisable by 4 but not by 100, it is a leap year (e.g. 2008).
  • If the year is divisable by 100 but not by 400, it is not a leap year (e.g. 2100).
  • If the year is divisable by 400 it is a leap year (e.g. 2000).

Calculating the Date of Easter Sunday

All christian holidays in Germany that are not at a fixed date can be calculated relative to Easter Sunday. The following algorithm finds the date of easter sunday in a given year.

public static DateTime GetEasterSunday(int year)
{
  int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
  a = year % 19;
  b = year / 100;
  c = (8 * b + 13) / 25 - 2;
  d = b - (year / 400) - 2;
  e = (19 * (year % 19) + ((15 - c + d) % 30)) % 30;
  if (e == 28)
  {
    if (a > 10)
    {
      e = 27;
    }
  }
  else if (e == 29)
  {
    e = 28;
  }
  f = (d + 6 * e + 2 * (year % 4) + 4 * (year % 7) + 6) % 7;
  DateTime retDate = new DateTime(year, 3, 1);
  return retDate.AddDays(e + f + 21);
}

Calculating German Holidays

The following function returns a list of the common German holidays. Variable christian holidays are based on the date of Easter Sunday, which is calculated using the algorithm above.

public static DateTime[] GetHolidays(int year)
{
  List<DateTime> list = new List<DateTime>();
  for (DateTime day = new DateTime(year, 1, 1); day < new DateTime(++year, 1, 1); day = day.AddDays(1))
  {
    if (IsHoliday(day))
      list.Add(day);
  }
  return list.ToArray();
}

public static bool IsHoliday(DateTime day)
{
  // Neujahr
  if (day.Month == 1 && day.Day == 1)
    return true;
  // Ostern
  DateTime osterSonntag = GetEasterSunday(day.Year);
  if (day == osterSonntag.AddDays(-2))
    return true;
  if (day == osterSonntag)
    return true;
  if (day == osterSonntag.AddDays(1))
    return true;
  // Maifeiertag
  if (day.Month == 5 && day.Day == 1)
    return true;
  // Christi Himmelfahrt
  if (day == osterSonntag.AddDays(39))
    return true;
  // Pfingstmontag
  if (day == osterSonntag.AddDays(50))
    return true;
  // Fronleichnam
  if (day == osterSonntag.AddDays(60))
    return true;
  // TdDE
  if (day.Month == 10 && day.Day == 3)
    return true;
  // Allerheiligen
  if (day.Month == 11 && day.Day == 1)
    return true;
  // Weihnachten
  if (day.Month == 12 && (day.Day == 25 || day.Day == 26))
    return true;
  return false;
}

 

Ähnliche Beiträge

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert