-
Notifications
You must be signed in to change notification settings - Fork 0
/
temporale.c
100 lines (85 loc) · 3.2 KB
/
temporale.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "calrom.h"
#include "dates.h"
#include "temporale.h"
// date comparison shorthands
#define dt_lt(a, b) g_date_compare(a, b) < 0
#define dt_le(a, b) g_date_compare(a, b) <= 0
void temporale_init(CRTemporale *temporale, CRLiturgicalYear year)
{
// slightly dirty use of pointer arithmetics
g_date_clear(&(temporale->first_advent_date), sizeof(CRTemporale) / sizeof(GDate));
date_nativity(&(temporale->nativity_date), year);
date_first_advent(&(temporale->first_advent_date), &(temporale->nativity_date));
date_baptism_of_lord(&(temporale->baptism_of_lord_date), year);
date_easter(&(temporale->easter_date), year);
date_ash_wednesday(&(temporale->ash_wednesday_date), &(temporale->easter_date));
date_pentecost(&(temporale->pentecost_date), &(temporale->easter_date));
GDate next_christmas;
date_nativity(&next_christmas, year + 1);
date_first_advent(&(temporale->last_date), &next_christmas);
g_date_subtract_days(&(temporale->last_date), 1);
}
void temporale_season(CRSeasonInfo *season_info, const GDate *date, const CRTemporale *temporale)
{
// season
if (dt_lt(date, &(temporale->nativity_date))) {
season_info->season = CR_SEASON_ADVENT;
} else if (dt_le(date, &(temporale->baptism_of_lord_date))) {
season_info->season = CR_SEASON_CHRISTMAS;
} else if (dt_lt(date, &(temporale->ash_wednesday_date))) {
season_info->season = CR_SEASON_ORDINARY;
} else if (dt_lt(date, &(temporale->easter_date))) {
season_info->season = CR_SEASON_LENT;
} else if (dt_le(date, &(temporale->pentecost_date))) {
season_info->season = CR_SEASON_EASTER;
} else {
season_info->season = CR_SEASON_ORDINARY;
}
// season week
season_info->week = 0;
GDate week1_beginning;
g_date_clear(&week1_beginning, 1);
switch (season_info->season) {
case CR_SEASON_ADVENT:
week1_beginning = temporale->first_advent_date;
break;
case CR_SEASON_CHRISTMAS:
week1_beginning = temporale->nativity_date;
break;
case CR_SEASON_LENT:
week1_beginning = temporale->ash_wednesday_date;
break;
case CR_SEASON_EASTER:
week1_beginning = temporale->easter_date;
break;
default: // Ordinary Time
week1_beginning = temporale->baptism_of_lord_date;
g_date_add_days(&week1_beginning, 1);
break;
}
GDateWeekday week1_weekday = g_date_get_weekday(&week1_beginning);
if (week1_weekday != G_DATE_SUNDAY) {
date_following_sunday(&week1_beginning);
}
if (dt_lt(date, &week1_beginning)) {
season_info->week = 0;
} else {
season_info->week = g_date_days_between(&week1_beginning, date) / WEEK + 1;
}
if (season_info->season == CR_SEASON_ORDINARY) {
// Ordinary Time's first Sunday is 2nd
season_info->week++;
if (dt_lt(&(temporale->pentecost_date), date)) {
GDate next_first_advent;
g_date_clear(&next_first_advent, 1);
date_first_advent_in_year(&next_first_advent, g_date_get_year(date));
int weeks_after_date = g_date_days_between(date, &next_first_advent) / WEEK;
const int OT_WEEKS_TOTAL = 34;
season_info->week = OT_WEEKS_TOTAL - weeks_after_date;
GDateWeekday date_weekday = g_date_get_weekday(date);
if (date_weekday == G_DATE_SUNDAY) {
season_info->week++;
}
}
}
}