-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_ddate.c
More file actions
327 lines (260 loc) · 9.99 KB
/
pg_ddate.c
File metadata and controls
327 lines (260 loc) · 9.99 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/timestamp.h"
#include "libpq/pqformat.h"
#include <time.h>
#include <math.h>
PG_MODULE_MAGIC;
typedef struct {
int32 julian_day;
} DDate;
static const char *seasons[] = {"Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"};
static const char *weekdays[] = {"Sweetmorn", "Boomtime", "Pungenday", "Prickle-Prickle", "Setting Orange"};
static const char *holydays[] = {"Mungday", "Mojoday", "Syaday", "Zaraday", "Maladay"};
static const char *flux_holydays[] = {"Chaoflux", "Discoflux", "Confuflux", "Bureflux", "Afflux"};
static inline bool is_leap_y(int greg_year) { return !(greg_year % 4) && ((greg_year % 100) || !(greg_year % 400)); }
static int parse_season(const char *season_name) {
for (int i = 0; i < 5; i++) {
if (!strcmp(season_name, seasons[i])) return i;
}
return -1;
}
static void julian_to_discordian(int32 julian_day, int *year, int *season, int *day, int *weekday, bool *is_st_tibs) {
DateADT postgres_date;
int pg_year, pg_month, pg_day;
int greg_year, greg_month, greg_day;
int day_of_year;
bool is_leap;
postgres_date = julian_day - POSTGRES_EPOCH_JDATE;
j2date(postgres_date + POSTGRES_EPOCH_JDATE, &pg_year, &pg_month, &pg_day);
greg_year = pg_year;
greg_month = pg_month;
greg_day = pg_day;
*year = greg_year + 1166;
is_leap = is_leap_y(greg_year);
day_of_year = date2j(greg_year, greg_month, greg_day) - date2j(greg_year, 1, 1) + 1;
*is_st_tibs = false;
if (is_leap && day_of_year == 60) {
*is_st_tibs = true;
*season = 0;
*day = 60;
*weekday = -1;
return;
}
if (is_leap && day_of_year > 60) day_of_year--;
*season = (day_of_year - 1) / 73;
*day = ((day_of_year - 1) % 73) + 1;
*weekday = (day_of_year - 1) % 5;
}
static int32 discordian_to_julian(int year, int season, int day, bool is_st_tibs) {
int greg_year = year - 1166;
int day_of_year;
if (is_st_tibs) {
day_of_year = 60;
} else {
day_of_year = season * 73 + day;
if (is_leap_y(greg_year) && day_of_year >= 60) day_of_year++;
}
return date2j(greg_year, 1, 1) + day_of_year - 1;
}
PG_FUNCTION_INFO_V1(ddate_in);
Datum ddate_in(PG_FUNCTION_ARGS) {
char *str = PG_GETARG_CSTRING(0);
DDate *result;
int year, season, day;
bool is_st_tibs = false;
if (strstr(str, "St. Tib's Day")) {
if (sscanf(str, "St. Tib's Day, %d YOLD", &year) == 1) {
is_st_tibs = true;
season = 0;
day = 60;
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid input syntax for type ddate: \"%s\"", str)));
}
} else {
char *comma_pos = strchr(str, ',');
if (comma_pos) {
bool is_holiday = false;
int holiday_season = -1;
for (int i = 0; i < 5; i++) {
if (!strncmp(str, holydays[i], strlen(holydays[i]))) {
is_holiday = true;
holiday_season = i;
day = 5;
break;
}
}
if (!is_holiday) {
for (int i = 0; i < 5; i++) {
if (!strncmp(str, flux_holydays[i], strlen(flux_holydays[i]))) {
is_holiday = true;
holiday_season = i;
day = 50;
break;
}
}
}
if (is_holiday) {
char season_name[32];
int parsed_day;
if (sscanf(comma_pos + 1, " %31s %d, %d YOLD", season_name, &parsed_day, &year) == 3) {
season = parse_season(season_name);
if (season == -1) {
ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid season name in ddate: \"%s\"", str)));
}
if (season != holiday_season || parsed_day != day) {
ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("holiday does not match season and day in ddate: \"%s\"", str)));
}
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid input syntax for type ddate: \"%s\"", str)));
}
} else {
char season_name[32];
if (sscanf(str, "%31s %d, %d YOLD", season_name, &day, &year) == 3) {
season = parse_season(season_name);
if (season == -1) {
ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid season name in ddate: \"%s\"", str)));
}
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid input syntax for type ddate: \"%s\"", str)));
}
}
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid input syntax for type ddate: \"%s\"", str)));
}
}
if (day < 1 || day > 73) {
ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("day %d is out of range for Discordian calendar", day)));
}
result = (DDate *) palloc(sizeof(DDate));
result->julian_day = discordian_to_julian(year, season, day, is_st_tibs);
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(ddate_out);
Datum ddate_out(PG_FUNCTION_ARGS) {
DDate *ddate = (DDate *) PG_GETARG_POINTER(0);
char *result;
int year, season, day, weekday;
bool is_st_tibs;
julian_to_discordian(ddate->julian_day, &year, &season, &day, &weekday, &is_st_tibs);
result = (char *) palloc(128);
if (is_st_tibs) {
snprintf(result, 128, "St. Tib's Day, %d YOLD", year);
} else {
const char *holyday = NULL;
if (day == 5) holyday = holydays[season];
else if (day == 50) holyday = flux_holydays[season];
if (holyday) snprintf(result, 128, "%s, %s %d, %d YOLD", holyday, seasons[season], day, year);
else snprintf(result, 128, "%s, %s %d, %d YOLD", weekdays[weekday], seasons[season], day, year);
}
PG_RETURN_CSTRING(result);
}
PG_FUNCTION_INFO_V1(ddate_recv);
Datum ddate_recv(PG_FUNCTION_ARGS) {
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
DDate *result;
result = (DDate *) palloc(sizeof(DDate));
result->julian_day = pq_getmsgint(buf, sizeof(int32));
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(ddate_send);
Datum ddate_send(PG_FUNCTION_ARGS) {
DDate *ddate = (DDate *) PG_GETARG_POINTER(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint(&buf, ddate->julian_day, sizeof(int32));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
PG_FUNCTION_INFO_V1(date_to_ddate);
Datum date_to_ddate(PG_FUNCTION_ARGS) {
DateADT date = PG_GETARG_DATEADT(0);
DDate *result;
result = (DDate *) palloc(sizeof(DDate));
result->julian_day = date + POSTGRES_EPOCH_JDATE;
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(ddate_to_date);
Datum ddate_to_date(PG_FUNCTION_ARGS) {
DDate *ddate = (DDate *) PG_GETARG_POINTER(0);
DateADT result;
result = ddate->julian_day - POSTGRES_EPOCH_JDATE;
PG_RETURN_DATEADT(result);
}
PG_FUNCTION_INFO_V1(ddate_eq);
Datum ddate_eq(PG_FUNCTION_ARGS) {
DDate *date1 = (DDate *) PG_GETARG_POINTER(0);
DDate *date2 = (DDate *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(date1->julian_day == date2->julian_day);
}
PG_FUNCTION_INFO_V1(ddate_ne);
Datum ddate_ne(PG_FUNCTION_ARGS) {
DDate *date1 = (DDate *) PG_GETARG_POINTER(0);
DDate *date2 = (DDate *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(date1->julian_day != date2->julian_day);
}
PG_FUNCTION_INFO_V1(ddate_lt);
Datum ddate_lt(PG_FUNCTION_ARGS) {
DDate *date1 = (DDate *) PG_GETARG_POINTER(0);
DDate *date2 = (DDate *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(date1->julian_day < date2->julian_day);
}
PG_FUNCTION_INFO_V1(ddate_le);
Datum ddate_le(PG_FUNCTION_ARGS) {
DDate *date1 = (DDate *) PG_GETARG_POINTER(0);
DDate *date2 = (DDate *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(date1->julian_day <= date2->julian_day);
}
PG_FUNCTION_INFO_V1(ddate_gt);
Datum ddate_gt(PG_FUNCTION_ARGS) {
DDate *date1 = (DDate *) PG_GETARG_POINTER(0);
DDate *date2 = (DDate *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(date1->julian_day > date2->julian_day);
}
PG_FUNCTION_INFO_V1(ddate_ge);
Datum ddate_ge(PG_FUNCTION_ARGS) {
DDate *date1 = (DDate *) PG_GETARG_POINTER(0);
DDate *date2 = (DDate *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(date1->julian_day >= date2->julian_day);
}
PG_FUNCTION_INFO_V1(ddate_cmp);
Datum ddate_cmp(PG_FUNCTION_ARGS) {
DDate *date1 = (DDate *) PG_GETARG_POINTER(0);
DDate *date2 = (DDate *) PG_GETARG_POINTER(1);
if (date1->julian_day < date2->julian_day) PG_RETURN_INT32(-1);
else if (date1->julian_day > date2->julian_day) PG_RETURN_INT32(1);
else PG_RETURN_INT32(0);
}
PG_FUNCTION_INFO_V1(ddate_now);
Datum ddate_now(PG_FUNCTION_ARGS) {
DateADT current_date;
DDate *result;
current_date = GetSQLCurrentDate();
result = (DDate *) palloc(sizeof(DDate));
result->julian_day = current_date + POSTGRES_EPOCH_JDATE;
PG_RETURN_POINTER(result);
}
PG_FUNCTION_INFO_V1(ddate_regexeq);
Datum ddate_regexeq(PG_FUNCTION_ARGS) {
DDate *ddate = (DDate *) PG_GETARG_POINTER(0);
text *pattern = PG_GETARG_TEXT_PP(1);
char *ddate_str;
char *pattern_str;
bool result;
ddate_str = DatumGetCString(DirectFunctionCall1(ddate_out, PointerGetDatum(ddate)));
pattern_str = text_to_cstring(pattern);
result = (strstr(ddate_str, pattern_str) != NULL);
pfree(ddate_str);
pfree(pattern_str);
PG_RETURN_BOOL(result);
}