import Foundation public enum LiturgicalCalendarEngine { // MARK: - Moveable feasts public static func easterSunday(year: Int) -> Date { let a = year % 19 let b = year / 100 let c = year % 100 let d = b / 4 let e = b % 4 let f = (b + 8) / 25 let g = (b - f + 1) / 3 let h = (19 * a + b - d - g + 15) % 30 let i = c / 4 let k = c % 4 let l = (32 + 2 * e + 2 * i - h - k) % 7 let m = (a + 11 * h + 22 * l) / 451 let month = (h + l - 7 * m + 114) / 31 let day = ((h + l - 7 * m + 114) % 31) + 1 return DayMath.date(fromKey: DayMath.key(year: year, month: month, day: day)) ?? DayMath.today() } public static func easterOffset(_ offset: Int, year: Int) -> Date { DayMath.adding(days: offset, to: easterSunday(year: year)) } public static func ashWednesday(year: Int) -> Date { easterOffset(-46, year: year) } public static func palmSunday(year: Int) -> Date { easterOffset(-7, year: year) } public static func holyThursday(year: Int) -> Date { easterOffset(-3, year: year) } public static func goodFriday(year: Int) -> Date { easterOffset(-2, year: year) } public static func holySaturday(year: Int) -> Date { easterOffset(-1, year: year) } public static func ascension(year: Int) -> Date { easterOffset(39, year: year) } public static func pentecost(year: Int) -> Date { easterOffset(49, year: year) } public static func corpusChristi(year: Int) -> Date { easterOffset(60, year: year) } public static func sacredHeart(year: Int) -> Date { easterOffset(68, year: year) } public static func trinitySunday(year: Int) -> Date { easterOffset(56, year: year) } /// First Sunday of Advent (the Sunday between 27 November and 3 December). public static func adventStart(year: Int) -> Date { let christmasEve = DayMath.date(fromKey: DayMath.key(year: year, month: 12, day: 24)) ?? DayMath.today() var sunday = christmasEve while DayMath.weekday(sunday) != 1 { sunday = DayMath.adding(days: -1, to: sunday) } return DayMath.adding(days: -21, to: sunday) } public static func christKing(year: Int) -> Date { DayMath.adding(days: -7, to: adventStart(year: year)) } public static func baptismOfTheLord(year: Int) -> Date { let epiphany = DayMath.date(fromKey: DayMath.key(year: year, month: 1, day: 6)) ?? DayMath.today() var sunday = DayMath.adding(days: 1, to: epiphany) while DayMath.weekday(sunday) != 1 { sunday = DayMath.adding(days: 1, to: sunday) } // If Epiphany itself falls on 7 or 8 January, the baptism is moved to the Monday. if DayMath.month(epiphany) == 1, DayMath.dayNumber(epiphany) >= 7 { return DayMath.adding(days: 1, to: epiphany) } return sunday } // MARK: - Seasons public static func season(for date: Date) -> LiturgicalSeason { let day = DayMath.normalize(date) let y = DayMath.year(day) let advent = adventStart(year: y) let christmas = DayMath.date(fromKey: DayMath.key(year: y, month: 12, day: 25)) ?? day let baptism = baptismOfTheLord(year: y) let ash = ashWednesday(year: y) let holyThursday = self.holyThursday(year: y) let easter = easterSunday(year: y) let pentecost = self.pentecost(year: y) if day >= advent && day < christmas { return .advent } if day >= christmas || day <= baptism { return .christmas } if day >= ash && day < holyThursday { return .lent } if day >= easter && day <= pentecost { return .easter } if day > pentecost && day < advent { return .ordinary } return .ordinary } public static func seasonName(for date: Date) -> String { season(for: date).title } // MARK: - Obligation public static func isObligation(date: Date, title: String = "") -> Bool { if DayMath.isSunday(date) { return true } let month = DayMath.month(date) let day = DayMath.dayNumber(date) switch (month, day) { case (1, 1), (1, 6), (8, 15), (11, 1), (12, 25): return true default: break } let lower = title.lowercased() if lower.contains("fronleichnam") { return true } if lower.contains("christi himmelfahrt") { return true } if lower.contains("pfingsten") && !lower.contains("vorabend") { return true } if lower.contains("ostersonntag") || lower.contains("osternacht") { return true } return false } // MARK: - Symbols public static func symbol(for title: String, rank: LiturgicalRank, date: Date) -> FeastSymbol { let t = title.lowercased() if t.contains("weihnacht") || t.contains("geburt des herrn") { return .star } if t.contains("erscheinung") { return .star } if t.contains("ostern") || t.contains("auferstehung") { return .sun } if t.contains("pfingst") || t.contains("heiliger geist") { return .flame } if t.contains("fronleichnam") || t.contains("abendmahl") || t.contains("gründonnerstag") { return .candle } if t.contains("herz jesu") { return .heart } if t.contains("christkönig") { return .crown } if t.contains("allerheiligen") { return .crown } if t.contains("maria") || t.contains("mariä") { return .lily } if t.contains("kreuz") || t.contains("kreuzerhöhung") { return .cross } if t.contains("taufe") { return .dove } if t.contains("wort gottes") || t.contains("evangel") { return .book } if t.contains("lichtmess") || t.contains("darstellung") { return .candle } if t.contains("fronleichnam") { return .candle } if rank == .solemnity { return .cross } if rank == .feast { return .cross } return .book } // MARK: - Local fallback private static let fixedReadings: [String: Readings] = [ "01-01": Readings(first: "Num 6, 22-27", psalm: "Ps 67 (66), 2-3.5.6 u. 8 (R: 2a)", second: "Gal 4, 4-7", gospel: "Lk 2, 16-21"), "01-06": Readings(first: "Jes 60, 1-6", psalm: "Ps 72 (71), 1-2.7-8.10-11.12-13 (R: 11)", second: "Eph 3, 2-3a.5-6", gospel: "Mt 2, 1-12"), "02-02": Readings(first: "Mal 3, 1-4", psalm: "Ps 24 (23), 7-8.9-10 (R: 8a)", second: "Hebr 2, 14-18", gospel: "Lk 2, 22-40"), "03-19": Readings(first: "2 Sam 7, 4-5a.12-14a.16", psalm: "Ps 89 (88), 2-3.4-5.27.29 (R: 37)", second: "Röm 4, 13.16-18.22", gospel: "Mt 1, 16.18-21.24a"), "03-25": Readings(first: "Jes 7, 10-14", psalm: "Ps 40 (39), 5-6.7-8a.8b-10 (R: 8a.9a)", second: "Hebr 10, 4-10", gospel: "Lk 1, 26-38"), "06-24": Readings(first: "Jes 49, 1-6", psalm: "Ps 139 (138), 1-3.13-14ab.14c-15 (R: 14a)", second: "Apg 13, 22-26", gospel: "Lk 1, 57-66.80"), "06-29": Readings(first: "Apg 12, 1-11", psalm: "Ps 34 (33), 2-3.4-5.6-7.8-9 (R: 5b)", second: "2 Tim 4, 6-8.17-18", gospel: "Mt 16, 13-19"), "08-15": Readings(first: "Offb 11, 19a; 12, 1-6a.10ab", psalm: "Ps 45 (44), 11-12.16-17 (R: 10b)", second: "1 Kor 15, 20-27", gospel: "Lk 1, 39-56"), "09-14": Readings(first: "Num 21, 4b-9", psalm: "Ps 78 (77), 1-2.34-35.36-37.38 (R: 7b)", second: "Phil 2, 6-11", gospel: "Joh 3, 13-17"), "11-01": Readings(first: "Offb 7, 2-4.9-14", psalm: "Ps 24 (23), 1-2.3-4.5-6 (R: vgl. 6)", second: "1 Joh 3, 1-3", gospel: "Mt 5, 1-12a"), "11-02": Readings(first: "Jes 25, 6a.7-9", psalm: "Ps 23 (22), 1-3a.3b-4.5.6 (R: 1)", second: "Röm 8, 14-23", gospel: "Mt 25, 31-46"), "12-08": Readings(first: "Gen 3, 9-15.20", psalm: "Ps 98 (97), 1.2-3ab.3cd-4 (R: 1a)", second: "Eph 1, 3-6.11-12", gospel: "Lk 1, 26-38"), "12-24": Readings(first: "2 Sam 7, 1-5.8b-12.14a.16", psalm: "Ps 89 (88), 2-3.20a u. 4-5.27 u. 29 (R: 2a)", second: "", gospel: "Lk 1, 67-79"), "12-25": Readings(first: "Jes 52, 7-10", psalm: "Ps 98 (97), 1.2-3b.3c-4.5-6 (R: vgl. 3cd)", second: "Hebr 1, 1-6", gospel: "Joh 1, 1-18"), "12-26": Readings(first: "Apg 6, 8-10; 7, 54-59", psalm: "Ps 31 (30), 3-4.6.8.17 (R: 6a)", second: "", gospel: "Mt 10, 17-22") ] private static let easterReadings: [Int: Readings] = [ -46: Readings(first: "Joel 2, 12-18", psalm: "Ps 51 (50), 3-4.5-6a.12-13.14 u. 17 (R: vgl. 3a)", second: "2 Kor 5, 20 - 6, 2", gospel: "Mt 6, 1-6.16-18"), -7: Readings(first: "Jes 50, 4-7", psalm: "Ps 22 (21), 8-9.17-18a.19-20.23-24 (R: 2a)", second: "Phil 2, 6-11", gospel: "Mt 21, 1-11"), -3: Readings(first: "Ex 12, 1-8.11-14", psalm: "Ps 116 (115), 12-13.15-16bc.17-18 (R: vgl. 1 Kor 10, 16)", second: "1 Kor 11, 23-26", gospel: "Joh 13, 1-15"), -2: Readings(first: "Jes 52, 13 - 53, 12", psalm: "Ps 31 (30), 2 u. 6.12-13.15-16.17 u. 25 (R: Lk 23, 46)", second: "Hebr 4, 14-16; 5, 7-9", gospel: "Joh 18, 1 - 19, 42"), 0: Readings(first: "Apg 10, 34a.37-43", psalm: "Ps 118 (117), 1-2.16-17.22-23 (R: vgl. 24)", second: "Kol 3, 1-4 oder 1 Kor 5, 6b-8", gospel: "Joh 20, 1-9"), 39: Readings(first: "Apg 1, 1-11", psalm: "Ps 47 (46), 2-3.6-7.8-9 (R: 6)", second: "Eph 1, 17-23", gospel: "Mk 16, 15-20"), 49: Readings(first: "Apg 2, 1-11", psalm: "Ps 104 (103), 1-2.24-25.29-30.31 u. 34 (R: vgl. 30)", second: "1 Kor 12, 3b-7.12-13", gospel: "Joh 20, 19-23"), 56: Readings(first: "Spr 8, 22-31", psalm: "Ps 8, 4-5.6-7.8-9 (R: 2a)", second: "Röm 5, 1-5", gospel: "Joh 16, 12-15"), 60: Readings(first: "Dtn 8, 2-3.14-16a", psalm: "Ps 147, 12-13.14-15.19-20 (R: 12a)", second: "1 Kor 10, 16-17", gospel: "Joh 6, 51-58"), 68: Readings(first: "Hos 11, 1.3-4.8c-9", psalm: "Jes 12, 2-3.4bcd.5-6 (R: 3)", second: "Eph 3, 8-12.14-19", gospel: "Joh 19, 31-37") ] public static func localDay(for date: Date) -> LiturgicalDay { let day = DayMath.normalize(date) let y = DayMath.year(day) let m = DayMath.month(day) let d = DayMath.dayNumber(day) let key = String(format: "%02d-%02d", m, d) let weekday = DayMath.weekday(day) let easter = easterSunday(year: y) let offset = DayMath.daysBetween(easter, day) var title = "" var rank: LiturgicalRank = .weekday var color: LiturgicalColor = .green var readings = Readings() var remark = "" // Moveable celebrations take precedence over fixed ones. switch offset { case -46: title = "Aschermittwoch"; rank = .weekday; color = .violet readings = easterReadings[-46] ?? Readings() case -7: title = "Palmsonntag"; rank = .feast; color = .red readings = easterReadings[-7] ?? Readings() case -3: title = "Gründonnerstag"; rank = .solemnity; color = .white readings = easterReadings[-3] ?? Readings() case -2: title = "Karfreitag"; rank = .solemnity; color = .red readings = easterReadings[-2] ?? Readings() case -1: title = "Karsamstag"; rank = .weekday; color = .violet case 0: title = "Ostersonntag"; rank = .solemnity; color = .white readings = easterReadings[0] ?? Readings() remark = "Hochfest der Auferstehung des Herrn" case 1: title = "Ostermontag"; rank = .solemnity; color = .white readings = Readings(first: "Apg 2, 14.22-33", psalm: "Ps 16 (15), 1-2a u. 5.7-8.9-10.11 (R: 11a)", second: "", gospel: "Mt 28, 8-15") case 39: title = "Christi Himmelfahrt"; rank = .solemnity; color = .white readings = easterReadings[39] ?? Readings() case 49: title = "Pfingsten"; rank = .solemnity; color = .red readings = easterReadings[49] ?? Readings() remark = "Hochfest des Heiligen Geistes" case 50: title = "Pfingstmontag"; rank = .solemnity; color = .red readings = Readings(first: "Apg 10, 34a.37-43", psalm: "Ps 118 (117), 1-2.16-17.22-23 (R: vgl. 24)", second: "Kol 3, 1-4", gospel: "Joh 20, 1-9") case 56: title = "Dreifaltigkeitssonntag"; rank = .solemnity; color = .white readings = easterReadings[56] ?? Readings() case 60: title = "Fronleichnam"; rank = .solemnity; color = .white readings = easterReadings[60] ?? Readings() remark = "Hochfest des Leibes und Blutes Christi" case 68: title = "Heiligstes Herz Jesu"; rank = .solemnity; color = .white readings = easterReadings[68] ?? Readings() default: break } if title.isEmpty { switch key { case "01-01": title = "Neujahr – Hochfest der Gottesmutter Maria"; rank = .solemnity; color = .white case "01-06": title = "Erscheinung des Herrn"; rank = .solemnity; color = .white case "02-02": title = "Darstellung des Herrn"; rank = .feast; color = .white case "03-19": title = "Hl. Josef, Bräutigam der Gottesmutter"; rank = .solemnity; color = .white case "03-25": title = "Verkündigung des Herrn"; rank = .solemnity; color = .white case "06-24": title = "Geburt Johannes' des Täufers"; rank = .solemnity; color = .white case "06-29": title = "Petrus und Paulus"; rank = .solemnity; color = .red case "08-15": title = "Mariä Aufnahme in den Himmel"; rank = .solemnity; color = .white case "09-14": title = "Kreuzerhöhung"; rank = .feast; color = .red case "11-01": title = "Allerheiligen"; rank = .solemnity; color = .white case "11-02": title = "Allerseelen"; rank = .weekday; color = .violet case "12-08": title = "Mariä Empfängnis"; rank = .solemnity; color = .white case "12-24": title = "Heiliger Abend"; rank = .weekday; color = .white case "12-25": title = "Weihnachten – Hochfest der Geburt des Herrn"; rank = .solemnity; color = .white case "12-26": title = "Hl. Stephanus"; rank = .feast; color = .red default: break } if let r = fixedReadings[key] { readings = r } } if title.isEmpty { if weekday == 1 { title = sundayTitle(for: day) color = DayMath.weekday(day) == 1 ? .green : .green if let fest = seasonalSundayRank(for: day) { rank = fest } } else { title = weekdayTitle(for: day) if season(for: day) == .advent || season(for: day) == .lent { color = .violet } else if season(for: day) == .christmas || season(for: day) == .easter { color = .white } else { color = .green } } } if rank == .solemnity { color = .white } if rank == .feast && color == .green { color = .red } return LiturgicalDay( date: day, title: title, rank: rank, color: color, remark: remark, readings: readings, isObligation: isObligation(date: day, title: title), source: "lokale Berechnung" ) } public static func localDays(in range: DayRange) -> [LiturgicalDay] { DayMath.days(in: range).map(localDay(for:)) } private static func seasonalSundayRank(for day: Date) -> LiturgicalRank? { let season = season(for: day) if season == .advent || season == .christmas || season == .lent || season == .easter { if season == .lent && DayMath.daysBetween(palmSunday(year: DayMath.year(day)), day) == 0 { return .feast } return .weekday } return nil } private static func sundayTitle(for day: Date) -> String { let y = DayMath.year(day) let season = season(for: day) switch season { case .advent: let start = adventStart(year: y) let index = DayMath.daysBetween(start, day) / 7 + 1 return ["1. Adventssonntag", "2. Adventssonntag", "3. Adventssonntag", "4. Adventssonntag"].indices.contains(index - 1) ? ["1. Adventssonntag", "2. Adventssonntag", "3. Adventssonntag", "4. Adventssonntag"][index - 1] : "Adventssonntag" case .christmas: let baptism = baptismOfTheLord(year: y) if DayMath.daysBetween(baptism, day) == 0 { return "Taufe des Herrn" } return "Sonntag in der Weihnachtszeit" case .lent: let ash = ashWednesday(year: y) let index = DayMath.daysBetween(ash, day) / 7 + 1 if index >= 6 { return "Palmsonntag" } return "\(index). Fastensonntag" case .easter: let easter = easterSunday(year: y) let index = DayMath.daysBetween(easter, day) / 7 + 1 if index == 1 { return "Ostersonntag" } if index == 8 { return "Pfingsten" } return "\(index). Sonntag der Osterzeit" case .ordinary: return "Sonntag im Jahreskreis" } } private static func weekdayTitle(for day: Date) -> String { let name = DayMath.weekdayName(day) switch season(for: day) { case .advent: return "\(name) im Advent" case .christmas: return "\(name) in der Weihnachtszeit" case .lent: return "\(name) in der Fastenzeit" case .easter: return "\(name) in der Osterzeit" case .ordinary: return "\(name) im Jahreskreis" } } }