-
Notifications
You must be signed in to change notification settings - Fork 5
/
jalali_utils.c
214 lines (180 loc) · 6.05 KB
/
jalali_utils.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
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
#include "postgres.h"
#include "parser/scansup.h"
#include "pgtime.h"
#include "nodes/makefuncs.h"
#include "utils/timestamp.h"
#include "utils/datetime.h"
#include "utils/date.h"
const int g_days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int j_days_in_month[12] = {31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29};
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(format_jalali_timestamp);
PG_FUNCTION_INFO_V1(format_jalali_date);
PG_FUNCTION_INFO_V1(jalali_part_timestamp);
PG_FUNCTION_INFO_V1(jalali_part_date);
struct pg_tm tm_to_jalali(struct pg_tm *tm);
int time_t_to_jalali_part(text* units, pg_time_t *t);
struct pg_tm tm_to_jalali(struct pg_tm *tm){
struct pg_tm jalali;
int j_day_no, j_np;
int g_day_no, i;
int gy = tm->tm_year + 300;
g_day_no = 365 * gy + (gy + 3) / 4 - (gy + 99) / 100 + (gy + 399) / 400;
for (i = 0; i < tm->tm_mon; i++) {
g_day_no += g_days_in_month[i];
}
if (tm->tm_mon > 1 && ((gy % 4 == 0 && gy % 100 != 0) || (gy % 400 == 0))) {
g_day_no += 1;
}
g_day_no += (tm->tm_mday - 1);
j_day_no = g_day_no - 79;
j_np = j_day_no / 12053;
j_day_no %= 12053;
jalali.tm_year = 979 + (33 * j_np) + 4 * (j_day_no / 1461);
j_day_no %= 1461;
if (j_day_no >= 366) {
jalali.tm_year += (j_day_no - 1) / 365;
j_day_no = (j_day_no - 1) % 365;
}
for (i = 0; i < 11; i++){
if (j_day_no < j_days_in_month[i]){
i -= 1;
break;
}
j_day_no -= j_days_in_month[i];
}
jalali.tm_mon = i + (i == 11 ? 1 : 2);
jalali.tm_mday = j_day_no + 1;
jalali.tm_hour = tm->tm_hour;
jalali.tm_min = tm->tm_min;
jalali.tm_sec = tm->tm_sec;
if (jalali.tm_mon <= 6) {
jalali.tm_yday = (jalali.tm_mon - 1) * 31 + jalali.tm_mday;
} else {
jalali.tm_yday = 186 + (jalali.tm_mon - 7) * 30 + jalali.tm_mday;
}
jalali.tm_wday = (tm->tm_wday + 1) % 7;
return jalali;
};
Datum format_jalali_timestamp(PG_FUNCTION_ARGS){
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
bool with_time = PG_GETARG_BOOL(1);
pg_time_t t = timestamptz_to_time_t(timestamp);
pg_tz *tehran = pg_tzset("Asia/Tehran");
struct pg_tm *tm = pg_localtime(&t, tehran);
struct pg_tm jdate = tm_to_jalali(tm);
if (with_time) {
text *output = (text *) palloc(VARHDRSZ + 19);
char *result = psprintf("%d/%02d/%02d %02d:%02d:%02d",
jdate.tm_year, jdate.tm_mon, jdate.tm_mday,
jdate.tm_hour, jdate.tm_min, jdate.tm_sec);
SET_VARSIZE(output, VARHDRSZ + 19);
memcpy(output->vl_dat, result, 19);
PG_RETURN_CSTRING(output);
PG_RETURN_TEXT_P(output);
} else {
text *output = (text *) palloc(VARHDRSZ + 10);
char *result = psprintf("%d/%02d/%02d",
jdate.tm_year, jdate.tm_mon, jdate.tm_mday);
SET_VARSIZE(output, VARHDRSZ + 10);
memcpy(output->vl_dat, result, 10);
PG_RETURN_TEXT_P(output);
}
}
Datum format_jalali_date(PG_FUNCTION_ARGS){
DateADT date = PG_GETARG_DATEADT(0);
Timestamp timestamp = date2timestamp_no_overflow(date);
pg_time_t t = timestamptz_to_time_t(timestamp);
struct pg_tm tm = *pg_gmtime(&t);
struct pg_tm jdate = tm_to_jalali(&tm);
text *output = (text *) palloc(VARHDRSZ + 10);
char *result = psprintf("%d/%02d/%02d",
jdate.tm_year, jdate.tm_mon, jdate.tm_mday);
SET_VARSIZE(output, VARHDRSZ + 10);
memcpy(output->vl_dat, result, 10);
PG_RETURN_TEXT_P(output);
}
int time_t_to_jalali_part(text* units, pg_time_t* t) {
char *lowunits;
int type, val;
float8 result;
pg_tz *tehran = pg_tzset("Asia/Tehran");
struct pg_tm *tm = pg_localtime(t, tehran);
struct pg_tm jdatetime = tm_to_jalali(tm);
lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
VARSIZE_ANY_EXHDR(units),
false);
type = DecodeUnits(0, lowunits, &val);
if (type == UNKNOWN_FIELD)
type = DecodeSpecial(0, lowunits, &val);
switch (val)
{
case DTK_SECOND:
result = (&jdatetime)->tm_sec;
break;
case DTK_MINUTE:
result = (&jdatetime)->tm_min;
break;
case DTK_HOUR:
result = (&jdatetime)->tm_hour;
break;
case DTK_DAY:
result = (&jdatetime)->tm_mday;
break;
case DTK_MONTH:
result = (&jdatetime)->tm_mon;
break;
case DTK_YEAR:
result = (&jdatetime)->tm_year;
break;
case DTK_DOW:
result = tm->tm_year;
result = j2day(date2j(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday)) - 6;
if (result < 0) {
result += 7;
}
break;
case DTK_DOY:
if ((&jdatetime)-> tm_mon <= 6) {
result = (((&jdatetime)-> tm_mon - 1) * 31) + (&jdatetime)-> tm_mday;
} else {
result = 186 + (&jdatetime)-> tm_mday;
result += ((&jdatetime)-> tm_mon - 7) * 30;
}
break;
case DTK_QUARTER:
result = ((&jdatetime)->tm_mon - 1) / 3 + 1;
break;
case DTK_DECADE:
if ((&jdatetime)->tm_year > 0)
result = ((&jdatetime)->tm_year / 10) * 10;
else
result = -((8 - ((&jdatetime)->tm_year - 1)) / 10) * 10;
break;
case DTK_CENTURY:
result = ((&jdatetime)->tm_year + 99) / 100;
break;
default:
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\"time\" units \"%s\" not recognized",
lowunits)));
result = 0;
}
return result;
}
Datum jalali_part_timestamp(PG_FUNCTION_ARGS) {
text *units = PG_GETARG_TEXT_PP(0);
TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
pg_time_t t = timestamptz_to_time_t(timestamp);
int result = time_t_to_jalali_part(units, &t);
PG_RETURN_INT32(result);
}
Datum jalali_part_date(PG_FUNCTION_ARGS) {
text *units = PG_GETARG_TEXT_PP(0);
DateADT date = PG_GETARG_DATEADT(1);
TimestampTz timestamp = date2timestamp_no_overflow(date);
pg_time_t t = timestamptz_to_time_t(timestamp);
int result = time_t_to_jalali_part(units, &t);
PG_RETURN_INT32(result);
}