-
Notifications
You must be signed in to change notification settings - Fork 0
/
dates.c
149 lines (127 loc) · 3.42 KB
/
dates.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "dates.h"
void date_following_sunday(GDate *date)
{
GDateWeekday wday = g_date_weekday(date);
int add = 0;
switch (wday) {
case G_DATE_SUNDAY:
add = 7; break;
case G_DATE_MONDAY:
add = 6; break;
case G_DATE_TUESDAY:
add = 5; break;
case G_DATE_WEDNESDAY:
add = 4; break;
case G_DATE_THURSDAY:
add = 3; break;
case G_DATE_FRIDAY:
add = 2; break;
default: // G_DATE_SATURDAY
add = 1; break;
}
g_date_add_days(date, add);
}
void date_first_advent(GDate *date, const GDate *nativity)
{
*date = *nativity;
GDateWeekday nativity_wday = g_date_get_weekday(nativity);
// Sunday before
int sub = 0;
switch (nativity_wday) {
case G_DATE_MONDAY:
sub = 1; break;
case G_DATE_TUESDAY:
sub = 2; break;
case G_DATE_WEDNESDAY:
sub = 3; break;
case G_DATE_THURSDAY:
sub = 4; break;
case G_DATE_FRIDAY:
sub = 5; break;
case G_DATE_SATURDAY:
sub = 6; break;
case G_DATE_SUNDAY:
sub = 7; break;
}
g_date_subtract_days(date, 3 * WEEK + sub);
}
void date_first_advent_in_year(GDate *date, CRLiturgicalYear year)
{
GDate nativity;
g_date_clear(&nativity, 1);
date_nativity(&nativity, year);
date_first_advent(date, &nativity);
}
void date_nativity(GDate *date, CRLiturgicalYear year)
{
g_date_set_dmy(date, 25, 12, year);
}
void date_baptism_of_lord(GDate *date, CRLiturgicalYear year)
{
g_date_set_dmy(date, 6, 1, year + 1);
GDateWeekday epiphany_wday = g_date_get_weekday(date);
// Sunday after Epiphany
int add = 0;
switch (epiphany_wday) {
// this is a special case:
// Epiphany on Sunday -> Baptism of the Lord is not on the next
// Sunday, but on the Monday immediately following
case G_DATE_SUNDAY:
add = 1; break;
case G_DATE_MONDAY:
add = 6; break;
case G_DATE_TUESDAY:
add = 5; break;
case G_DATE_WEDNESDAY:
add = 4; break;
case G_DATE_THURSDAY:
add = 3; break;
case G_DATE_FRIDAY:
add = 2; break;
case G_DATE_SATURDAY:
add = 1; break;
}
g_date_add_days(date, add);
}
void date_ash_wednesday(GDate *date, const GDate *easter)
{
*date = *easter;
g_date_subtract_days(date, 6 * WEEK + 4);
}
void date_easter(GDate *date, CRLiturgicalYear year)
{
GDateYear cyear = year + 1; // civil year
// algorithm below ported from the 'easter' Ruby gem:
// https://github.com/jrobertson/easter
int golden_number = (cyear % 19) + 1;
int dominical_number = (cyear + (cyear / 4) - (cyear / 100) + (cyear / 400)) % 7;
int solar_correction = (cyear - 1600) / 100 - (cyear - 1600) / 400;
int lunar_correction = (((cyear - 1400) / 100) * 8) / 25;
int paschal_full_moon = (3 - 11 * golden_number + solar_correction - lunar_correction) % 30;
do {
dominical_number += 7;
} while (dominical_number <= 0);
do {
paschal_full_moon += 30;
} while (paschal_full_moon <= 0);
if ((paschal_full_moon == 29) || ((paschal_full_moon == 28) && golden_number > 11)) {
paschal_full_moon -= 1;
}
int difference = (4 - paschal_full_moon - dominical_number) % 7;
if (difference < 0) {
difference += 7;
}
int day_easter = paschal_full_moon + difference + 1;
if (day_easter < 11) {
// Easter occurs in March.
g_date_set_dmy(date, day_easter + 21, 3, cyear);
} else {
// Easter occurs in April.
g_date_set_dmy(date, day_easter - 10, 4, cyear);
}
}
void date_pentecost(GDate *date, const GDate *easter)
{
*date = *easter;
g_date_add_days(date, 7 * WEEK);
}