Citat:
Jag frågade ChatGPT och fick ett ganska intressant svar. Jag vet inte om det är korrekt men det ser ytligt bra ut.
Kod:
using System;
public static DateTime[] GetSwedishNationalHolidays(int year)
{
DateTime[] holidays = new DateTime[10]; // there are 10 Swedish national holidays
// New Year's Day
holidays[0] = new DateTime(year, 1, 1);
// Epiphany
holidays[1] = new DateTime(year, 1, 6);
// Easter Sunday
DateTime easterSunday = CalculateEasterSunday(year);
// Maundy Thursday
holidays[2] = easterSunday.AddDays(-3);
// Good Friday
holidays[3] = easterSunday.AddDays(-2);
// Easter Monday
holidays[4] = easterSunday.AddDays(1);
// May Day
holidays[5] = new DateTime(year, 5, 1);
// Ascension Day
holidays[6] = easterSunday.AddDays(39);
// National Day
holidays[7] = new DateTime(year, 6, 6);
// Midsummer's Day
holidays[8] = CalculateMidsummersDay(year);
// Christmas Day
holidays[9] = new DateTime(year, 12, 25);
return holidays;
}
// Calculates the date of Easter Sunday for a given year
private static DateTime CalculateEasterSunday(int year)
{
int a = year % 19;
int b = year / 100;
int c = year % 100;
int d = b / 4;
int e = b % 4;
int f = (b + 8) / 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int l = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * l) / 451;
int month = (h + l - 7 * m + 114) / 31;
int day = ((h + l - 7 * m + 114) % 31) + 1;
return new DateTime(year, month, day);
}
// Calculates the date of Midsummer's Day (the Saturday between June 20 and June 26) for a given year
private static DateTime CalculateMidsummersDay(int year)
{
DateTime june20 = new DateTime(year, 6, 20);
DateTime saturday = june20.AddDays(6 - (int)june20.DayOfWeek);
return saturday;
}