Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Create all Country Holydays between 1582 2199

Title: Create all Country Holydays between 1582-2199 Question: You will have a programm to compute all the German Holidays. For all other Countries (e.g. USA ...) you must 1. remove unwanted Holiday-Entries 2. insert your Holiday-Entries 3. change the names in the days-array 4. change the Holiday-Formulas in the Calendar() procedure to your Country Holidays Answer: place this objects on Form1 a Listbox1 a Edit1 with event Edit1KeyPress a Edit2 private Heute: TDateTime; Jahr, Monat, Tag: Word; work: string; end; // define the short names of the weekdays const days: array[1..7] of string = ('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'); // compute the current year and place it into the Edit1 field procedure TForm1.FormShow(Sender: TObject); begin // set the Local Date Format // if you change it to a another format // then also change the copy substrings in SK() ShortDateFormat := 'dd/mm/yyyy'; TwoDigitYearCenturyWindow := 50; Heute := Now; DecodeDate(Heute, Jahr, Monat, Tag); Edit1.Text := IntToStr(Jahr); end; // subroutine to put an formatted Holiday-entry in the Listbox1 procedure TForm1.SK(xDate, Title: string); var work: string; begin work := copy(xDate, 4, 2) + ' ' + copy(xDate, 1, 2); Listbox1.Items.add(Work + ' ' + days[DayOfWeek(StrToDate(xDate))] + ' ' + Title); end; // here we create the holidays for a complete year procedure TForm1.Calendar(cYear: string; dEaster: TDateTime); var I, Year: Integer; KDate: TDateTime; cEaster: string; begin cEaster := DateToStr(dEaster); Year := StrToInt(cYear); Listbox1.clear; // First we put out the New Years Day SK('01.01.' + cYear, 'F Neujahr'); // Then we put out all Holidays dependent from Easter SK(DateToStr(dEaster - 48), 'G Rosenmontag'); SK(DateToStr(dEaster - 47), 'G Fastnacht'); SK(DateToStr(dEaster - 7), 'F Palmsonntag'); SK(DateToStr(dEaster - 3), 'G Grndonnerstag'); SK(DateToStr(dEaster - 2), 'F Karfreitag'); SK(DateToStr(dEaster), 'F Ostersonntag'); SK(DateToStr(dEaster + 1), 'F Ostermontag'); SK(DateToStr(dEaster + 7), 'F Weisser Sonntag'); SK(DateToStr(dEaster + 39), 'F Christi Himmelfahrt'); SK(DateToStr(dEaster + 49), 'F Pfingssonntag'); SK(DateToStr(dEaster + 50), 'F Pfingstmontag'); SK(DateToStr(dEaster + 49 + 7), 'G Dreieinigkeitsfest'); SK(DateToStr(dEaster + 60), 'B Fronleichnam (BW,BY,HE,NW,RP,SL)'); // here we have all fixed Holidays SK('01.05.' + cYear, 'F Maifeiertag'); SK('31.10.' + cYear, 'B Reformationstag (BB,MV,ST,TH)'); SK('01.11.' + cYear, 'B Allerheiligen (BW,BY,NW,RP,ST,TH)'); SK('11.11.' + cYear, 'G Martinstag'); SK('31.12.' + cYear, 'G Sylvester'); SK('14.02.' + cYear, 'G Valentinstag'); SK('06.01.' + cYear, 'B Heilige Drei Knige (BW,BY,ST)'); SK('15.08.' + cYear, 'B Mari Himmelfahrt (BY,SL,SN)'); // this is the formula for Mothers Day - second Sunday in May if (Year = 1923) then begin KDate := StrToDate('01.05.' + cYear); SK(DateToStr(KDate + 15 - DayOfWeek(KDate)), 'G Muttertag'); end; // this is the German National Day if (Year = 1961) and (Year SK('17.06.' + cYear, 'F Tag der deutschen Einheit'); if (Year = 1989) then SK('03.10.' + cYear, 'F Tag der deutschen Einheit'); // here are the three XMas Days KDate := StrToDate('25.12.' + cYear); SK(DateToStr(KDate - 1), 'F Heilig Abend'); SK(DateToStr(KDate), 'F 1.Weihnachtstag'); SK(DateToStr(KDate + 1), 'F 2.Weihnachtstag'); // the ecclesiastical year starts with the first Advent Sunday // here we compute the four Advent Sundays I := DayOfWeek(KDate); if (I 1) then dec(I) else I := 7; KDate := KDate - I - 21; SK(DateToStr(KDate), 'G 1.Advent'); SK(DateToStr(KDate + 7), 'G 2.Advent'); SK(DateToStr(KDate + 14), 'G 3.Advent'); SK(DateToStr(KDate + 21), 'G 4.Advent'); // The German 'Totensonntag' is the last Sunday in the // ecclesiastical year if (Year = 1816) then SK(DateToStr(KDate - 7), 'G Totensonntag'); // The German 'Bu- und Bettag' is on Wednesday 11 weeks // before the german 'Totensonntag' if (Year = 1673) and (Year SK(DateToStr(KDate - 11), 'F Bu- und Bettag'); // the German 'Volkstrauertag' ist the second last // Sunday in the ecclesiastical year SK(DateToStr(KDate - 14), 'G Volkstrauertag'); // The German Thanksgiving Day is the 39. Sunday in the Year SK(DateToStr(9 - DayOfWeek(StrToDate('01.01.' + cYear)) + 39 * 7), 'G Erntedankfest'); end; // Compute Easter Date function TForm1.EasterSunday(Year: Integer; var EasterDate: TDateTime): boolean; { compute Easter Sunday from 1582 to 2199 with formula from Gauss Year compute Easter Sunday for this Year Return False Year incorrect Return True the variable EasterDate has the computed Oster Sunday } const aYears: array[1..5, 1..3] of integer = ((2099, 24, 6), (1899, 24, 5), (1799, 23, 4), (1699, 23, 3), (1581, 22, 2)); var xDay, Month, I: Integer; a, b, c, d, e: Word; begin // here we check the correct year if ((Year 2199)) then begin result := False; exit; end; // and now we get the Month and Day correction values for I := 1 to 5 do begin if (Year aYears[I, 1]) then break; end; // now we compute the Easter Sunday a := Year mod 19; b := Year mod 4; c := Year mod 7; d := (19 * a + aYears[I, 2]) mod 30; e := (2 * b + 4 * c + 6 * d + aYears[I, 3]) mod 7; if (d + e 9) then begin xDay := d + e - 9; Month := 4; if (xDay = 26) then xDay := 19; end else begin xDay := d + e + 22; Month := 3; end; EasterDate := EncodeDate(Year, Month, xDay); result := True; end; // with the input of a Year starts the computing // for Easter-Date and all Holidays procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); var EDate: TDateTime; begin if key = #13 then begin if not (isInteger(Edit1.Text)) then Edit2.Text := 'ungltige Eingabe' else begin if (EasterSunday(StrToInt(Edit1.Text), EDate)) then begin Edit3.Text := 'Feiertage_' + Edit1.Text + '.txt'; Edit2.Text := DateToStr(EDate); Calendar(Edit1.Text, EDate); end else Edit2.Text := 'incorrect Year'; end; end; end; // Check input of numeric function TForm1.isInteger(value: string): Boolean; begin result := True; try StrToInt(value); except result := False; end; end; { Die Feiertagsberechnung fr 2002 ergibt dann folgende Daten: Mt Tg Jahr Wt T Feiertag/Gedenktag -- -- ---- -- - ------------------------------- 01.01.2002 Di F Neujahr 06.01.2002 So B Heilige Drei Knige (BW,BY,ST) 11.02.2002 Mo G Rosenmontag 12.02.2002 Di G Fastnacht 14.02.2002 Do G Valentinstag 24.03.2002 So F Palmsonntag 28.03.2002 Do G Grndonnerstag 29.03.2002 Fr F Karfreitag 31.03.2002 So F Ostersonntag 01.04.2002 Mo F Ostermontag 07.04.2002 So F Weisser Sonntag 01.05.2002 Mi F Maifeiertag 09.05.2002 Do F Christi Himmelfahrt 12.05.2002 So G Muttertag 19.05.2002 So F Pfingssonntag 20.05.2002 Mo F Pfingstmontag 26.05.2002 So G Dreieinigkeitsfest 30.05.2002 Do B Fronleichnam (BW,BY,HE,NW,RP,SL) 15.08.2002 Do B Mari Himmelfahrt (BY,SL,SN) 03.10.2002 Do F Tag der deutschen Einheit 05.10.2002 Fr G Erntedankfest 31.10.2002 Do B Reformationstag (BB,MV,ST,TH) 01.11.2002 Fr B Allerheiligen (BW,BY,NW,RP,ST,TH) 11.11.2002 Mo G Martinstag 17.11.2002 So G Volkstrauertag 24.11.2002 So G Totensonntag 01.12.2002 So G 1.Advent 08.12.2002 So G 2.Advent 15.12.2002 So G 3.Advent 22.12.2002 So G 4.Advent 24.12.2002 Di F Heilig Abend 25.12.2002 Mi F 1.Weihnachtstag 26.12.2002 Do F 2.Weihnachtstag 31.12.2002 Di G Sylvester } end. Regards Kurt