package domain import ( "strings" "time" ) // Liturgical calendar primitives shared by the store, the local fallback and // the web views. func EasterSunday(year int) time.Time { a := year % 19 b := year / 100 c := year % 100 d := b / 4 e := b % 4 f := (b + 8) / 25 g := (b - f + 1) / 3 h := (19*a + b - d - g + 15) % 30 i := c / 4 k := c % 4 l := (32 + 2*e + 2*i - h - k) % 7 m := (a + 11*h + 22*l) / 451 month := (h + l - 7*m + 114) / 31 day := ((h + l - 7*m + 114) % 31) + 1 return MustDate(KeyYMD(year, month, day)) } func easterOffset(offset, year int) time.Time { return AddingDays(offset, EasterSunday(year)) } func AshWednesday(year int) time.Time { return easterOffset(-46, year) } func PalmSunday(year int) time.Time { return easterOffset(-7, year) } func HolyThursday(year int) time.Time { return easterOffset(-3, year) } func GoodFriday(year int) time.Time { return easterOffset(-2, year) } func HolySaturday(year int) time.Time { return easterOffset(-1, year) } func Ascension(year int) time.Time { return easterOffset(39, year) } func Pentecost(year int) time.Time { return easterOffset(49, year) } func CorpusChristi(year int) time.Time { return easterOffset(60, year) } func SacredHeart(year int) time.Time { return easterOffset(68, year) } func TrinitySunday(year int) time.Time { return easterOffset(56, year) } // AdventStart returns the first Sunday of Advent in the given year. func AdventStart(year int) time.Time { christmasEve := MustDate(KeyYMD(year, 12, 24)) sunday := christmasEve for Weekday(sunday) != 1 { sunday = AddingDays(-1, sunday) } return AddingDays(-21, sunday) } // ChristKing returns the feast of Christ the King for the given year. func ChristKing(year int) time.Time { return AddingDays(-7, AdventStart(year)) } // BaptismOfTheLord returns the end of the Christmas season. func BaptismOfTheLord(year int) time.Time { epiphany := MustDate(KeyYMD(year, 1, 6)) sunday := AddingDays(1, epiphany) for Weekday(sunday) != 1 { sunday = AddingDays(1, sunday) } if Month(epiphany) == 1 && DayNumber(epiphany) >= 7 { return AddingDays(1, epiphany) } return sunday } // SeasonFor maps a date onto the liturgical season. func SeasonFor(date time.Time) LiturgicalSeason { day := Normalize(date) y := Year(day) advent := AdventStart(y) christmas := MustDate(KeyYMD(y, 12, 25)) baptism := BaptismOfTheLord(y) ash := AshWednesday(y) holyThursday := HolyThursday(y) easter := EasterSunday(y) pentecost := Pentecost(y) switch { case !day.Before(advent) && day.Before(christmas): return SeasonAdvent case !day.Before(christmas) || !day.After(baptism): return SeasonChristmas case !day.Before(ash) && day.Before(holyThursday): return SeasonLent case !day.Before(easter) && !day.After(pentecost): return SeasonEaster case day.After(pentecost) && day.Before(advent): return SeasonOrdinary default: return SeasonOrdinary } } // SymbolFor picks a decorative feast symbol from the title and rank. func SymbolFor(title string, rank LiturgicalRank, date time.Time) FeastSymbol { t := strings.ToLower(title) switch { case strings.Contains(t, "weihnacht"), strings.Contains(t, "geburt des herrn"), strings.Contains(t, "erscheinung"): return FeastStar case strings.Contains(t, "ostern"), strings.Contains(t, "auferstehung"): return FeastSun case strings.Contains(t, "pfingst"), strings.Contains(t, "heiliger geist"): return FeastFlame case strings.Contains(t, "fronleichnam"), strings.Contains(t, "abendmahl"), strings.Contains(t, "gründonnerstag"): return FeastCandle case strings.Contains(t, "herz jesu"): return FeastHeart case strings.Contains(t, "christkönig"), strings.Contains(t, "allerheiligen"): return FeastCrown case strings.Contains(t, "maria"), strings.Contains(t, "mariä"): return FeastLily case strings.Contains(t, "kreuz"), strings.Contains(t, "kreuzerhöhung"): return FeastCross case strings.Contains(t, "taufe"): return FeastDove case strings.Contains(t, "wort gottes"), strings.Contains(t, "evangel"): return FeastBook case strings.Contains(t, "lichtmess"), strings.Contains(t, "darstellung"): return FeastCandle } if rank == RankSolemnity || rank == RankFeast { return FeastCross } return FeastBook }