aboutsummaryrefslogtreecommitdiff
path: root/internal/caldata/readings.go
blob: 2810026321c0bc748347fe8d5a7481939c6b0fc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package caldata

import (
	"time"

	"github.com/lukaszkasprzak/lectio/internal/calendar"
)

// Readings resolves a day's Mass readings: the observed celebration's inline
// propers, else the temporal lectionary for its slug, else -- for an EF
// weekday with no proper Mass -- the preceding Sunday's Mass (the 1962 rule
// that green-season ferias repeat the Sunday). Shared by the CLI (cli.dayReadings
// delegates here) and calfeed.Build (injected as its readings func).
func Readings(sel calendar.Selection, layers []calendar.Layer, date time.Time, day calendar.LiturgicalDay) []calendar.Reading {
	var rs []calendar.Reading
	for _, m := range day.Observed.Masses {
		rs = append(rs, m.Readings...)
	}
	if len(rs) > 0 {
		return rs
	}
	// temporal table: EF by slug, OF by slug+cycle.
	key := day.Observed.Slug
	if sel.Form != "old" && day.SundayCycle != "" {
		key = day.Observed.Slug + "-" + day.SundayCycle
	}
	if r := TemporalReadings(sel.Form, key); len(r) > 0 {
		return r
	}
	if sel.Form == "old" && day.Observed.Layer == "temporal" && date.Weekday() != time.Sunday {
		sun := date.AddDate(0, 0, -int(date.Weekday())) // preceding Sunday (Sunday = 0)
		sd := calendar.Compute(sun, sel, layers)
		return TemporalReadings(sel.Form, sd.Observed.Slug)
	}
	return nil
}