-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathicu_interval.c
397 lines (343 loc) · 9.85 KB
/
icu_interval.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* icu_interval.c
*
* Part of icu_ext: a PostgreSQL extension to expose functionality from ICU
* (see http://icu-project.org)
*
* By Daniel Vérité, 2018-2023. See LICENSE.md
*/
#include "icu_ext.h"
/* Postgres includes */
#include "funcapi.h"
#include "miscadmin.h"
#include "common/int.h"
#include "utils/builtins.h"
#include "utils/timestamp.h"
#include "utils/pg_locale.h"
#include "utils/date.h"
#include "utils/datetime.h"
/* ICU includes */
#include "unicode/ucal.h"
#include "unicode/ucnv.h" /* needed? */
#include "unicode/udat.h"
#include "unicode/ustring.h"
PG_FUNCTION_INFO_V1(icu_interval_in);
PG_FUNCTION_INFO_V1(icu_interval_out);
PG_FUNCTION_INFO_V1(icu_from_interval);
PG_FUNCTION_INFO_V1(icu_timestamptz_add_interval);
PG_FUNCTION_INFO_V1(icu_interval_add_timestamptz);
PG_FUNCTION_INFO_V1(icu_timestamptz_sub_interval);
PG_FUNCTION_INFO_V1(icu_interval_mul);
PG_FUNCTION_INFO_V1(icu_mul_i_interval);
PG_FUNCTION_INFO_V1(icu_interv_plus_interv);
PG_FUNCTION_INFO_V1(icu_interv_minus_interv);
/*
* Add an interval to a timestamp with timezone, given a localized calendar.
* if locale==NULL, use the current ICU locale.
*/
static
Datum
add_interval(TimestampTz ts, const icu_interval_t *ival, const char *locale)
{
UErrorCode status = U_ZERO_ERROR;
UDate date_time = TS_TO_UDATE(ts);
UCalendar *ucal;
UChar* tzid;
int32_t tzid_length;
const char *pg_tz_name = pg_get_timezone_name(session_timezone);
tzid_length = icu_to_uchar(&tzid,
pg_tz_name, /* or UCAL_UNKNOWN_ZONE_ID, like GMT */
strlen(pg_tz_name));
ucal = ucal_open(tzid,
tzid_length,
locale,
UCAL_DEFAULT,
&status);
if (U_FAILURE(status))
{
elog(ERROR, "ucal_open failed: %s\n", u_errorName(status));
}
ucal_setMillis(ucal, date_time, &status);
/* Add years, months, days, with the rules of the given calendar */
if (ival->year != 0)
ucal_add(ucal, UCAL_YEAR, ival->year, &status);
if (ival->month != 0)
ucal_add(ucal, UCAL_MONTH, ival->month, &status);
if (ival->day != 0)
ucal_add(ucal, UCAL_DAY_OF_MONTH, ival->day, &status);
if (ival->time != 0)
ucal_add(ucal, UCAL_MILLISECOND, ival->time/1000, &status);
/* Translate back to a UDate, and then to a postgres timestamptz */
date_time = ucal_getMillis(ucal, &status);
ucal_close(ucal);
if (U_FAILURE(status))
{
elog(ERROR, "calendar translation failed: %s\n", u_errorName(status));
}
PG_RETURN_TIMESTAMPTZ(UDATE_TO_TS(date_time));
}
/*
* This is similar to core's interval_in() except
* there's no typmod support and no adjustment.
*/
Datum
icu_interval_in(PG_FUNCTION_ARGS)
{
icu_interval_t *result;
char *str = PG_GETARG_CSTRING(0);
/* int32 typmod = PG_GETARG_INT32(2); */
int dtype;
int nf;
int dterr;
char *field[MAXDATEFIELDS];
int ftype[MAXDATEFIELDS];
char workbuf[256];
#if PG_VERSION_NUM >= 160000
Node *escontext = fcinfo->context;
DateTimeErrorExtra extra;
#endif
#if PG_VERSION_NUM >= 150000
struct pg_itm_in tt,
*itm_in = &tt;
#else
fsec_t fsec;
struct pg_tm tt,
*tm = &tt;
#endif
#if PG_VERSION_NUM >= 150000
itm_in->tm_year = 0;
itm_in->tm_mon = 0;
itm_in->tm_mday = 0;
itm_in->tm_usec = 0;
#else
tm->tm_year = 0;
tm->tm_mon = 0;
tm->tm_mday = 0;
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
fsec = 0;
#endif
dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
ftype, MAXDATEFIELDS, &nf);
if (dterr == 0)
{
#if PG_VERSION_NUM >= 150000
dterr = DecodeInterval(field, ftype, nf, INTERVAL_FULL_RANGE,
&dtype, itm_in);
/* if those functions think it's a bad format, try ISO8601 style */
if (dterr == DTERR_BAD_FORMAT)
dterr = DecodeISO8601Interval(str,
&dtype, itm_in);
#else
dterr = DecodeInterval(field, ftype, nf, INTERVAL_FULL_RANGE,
&dtype, tm, &fsec);
if (dterr == DTERR_BAD_FORMAT)
dterr = DecodeISO8601Interval(str,
&dtype, tm, &fsec);
#endif
}
if (dterr != 0)
{
if (dterr == DTERR_FIELD_OVERFLOW)
dterr = DTERR_INTERVAL_OVERFLOW;
#if PG_VERSION_NUM >= 160000
DateTimeParseError(dterr, &extra, str, "interval", escontext);
#else
DateTimeParseError(dterr, str, "interval");
#endif
PG_RETURN_NULL();
}
result = (icu_interval_t*) palloc(sizeof(icu_interval_t));
switch (dtype)
{
case DTK_DELTA:
/* do not call itm2interval() to not merge years into months */
#if PG_VERSION_NUM >= 150000
result->month = itm_in->tm_mon;
result->day = itm_in->tm_mday;
result->year = itm_in->tm_year;
result->time = itm_in->tm_usec;
#else
result->month = tm->tm_mon;
result->day = tm->tm_mday;
result->year = tm->tm_year;
result->time = (((((tm->tm_hour * INT64CONST(60)) +
tm->tm_min) * INT64CONST(60)) +
tm->tm_sec) * USECS_PER_SEC) + fsec;
#endif
break;
default:
elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
dtype, str);
}
return PointerGetDatum(result);
}
/*
* Text representation for icu_interval.
* It is essentially identical to "interval" except that
* the year field is not months%12
*/
Datum
icu_interval_out(PG_FUNCTION_ARGS)
{
icu_interval_t *itv = (icu_interval_t*)PG_GETARG_DATUM(0);
char buf[MAXDATELEN + 1];
TimeOffset time, tfrac;
#if PG_VERSION_NUM >= 150000
struct pg_itm itm;
itm.tm_year = itv->year;
itm.tm_mon = itv->month;
itm.tm_mday = itv->day;
/* The following code is copied from interval2itm()
in backend/utils/adt/timestamp.c */
time = itv->time;
tfrac = time / USECS_PER_HOUR;
time -= tfrac * USECS_PER_HOUR;
itm.tm_hour = tfrac;
tfrac = time / USECS_PER_MINUTE;
time -= tfrac * USECS_PER_MINUTE;
itm.tm_min = (int) tfrac;
tfrac = time / USECS_PER_SEC;
time -= tfrac * USECS_PER_SEC;
itm.tm_sec = (int) tfrac;
itm.tm_usec = (int) time;
EncodeInterval(&itm, IntervalStyle, buf);
#else
/* see interval2tm() in backend/utils/adt/timestamp.c */
fsec_t fsec;
struct pg_tm ptm,
*tm = &ptm;
tm->tm_year = itv->month / MONTHS_PER_YEAR;
tm->tm_mon = itv->month % MONTHS_PER_YEAR;
tm->tm_mday = itv->day;
time = itv->time;
tfrac = time / USECS_PER_HOUR;
time -= tfrac * USECS_PER_HOUR;
tm->tm_hour = tfrac;
if ((tm->tm_hour < 0) != (tfrac < 0)) /* !SAMESIGN */
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("interval out of range")));
tfrac = time / USECS_PER_MINUTE;
time -= tfrac * USECS_PER_MINUTE;
tm->tm_min = tfrac;
tfrac = time / USECS_PER_SEC;
fsec = time - (tfrac * USECS_PER_SEC);
tm->tm_sec = tfrac;
EncodeInterval(tm, fsec, IntervalStyle, buf);
#endif
PG_RETURN_CSTRING(pstrdup(buf));
}
Datum
icu_from_interval(PG_FUNCTION_ARGS)
{
Interval *pg_interval = PG_GETARG_INTERVAL_P(0);
icu_interval_t *interval = (icu_interval_t*) palloc(sizeof(icu_interval_t));
interval->time = pg_interval->time;
interval->day = pg_interval->day;
interval->month = pg_interval->month;
interval->year = 0;
return PointerGetDatum(interval);
}
/*
* icu_timestamptz + icu_interval
*/
Datum
icu_timestamptz_add_interval(PG_FUNCTION_ARGS)
{
TimestampTz pg_ts = PG_GETARG_TIMESTAMPTZ(0);
icu_interval_t *itv = (icu_interval_t*) PG_GETARG_DATUM(1);
return add_interval(pg_ts, itv, icu_ext_default_locale);
}
/*
* icu_interval + icu_timestamptz
*/
Datum
icu_interval_add_timestamptz(PG_FUNCTION_ARGS)
{
icu_interval_t *itv = (icu_interval_t*) PG_GETARG_DATUM(0);
TimestampTz pg_ts = PG_GETARG_TIMESTAMPTZ(1);
return add_interval(pg_ts, itv, icu_ext_default_locale);
}
Datum
icu_timestamptz_sub_interval(PG_FUNCTION_ARGS)
{
TimestampTz pg_ts = PG_GETARG_TIMESTAMPTZ(0);
icu_interval_t *itv = (icu_interval_t*) PG_GETARG_DATUM(1);
itv->year = -itv->year;
itv->month = -itv->month;
itv->day = -itv->day;
itv->time = -itv->time;
return add_interval(pg_ts, itv, icu_ext_default_locale);
}
Datum
icu_interval_mul(PG_FUNCTION_ARGS)
{
icu_interval_t *itv = (icu_interval_t*) PG_GETARG_DATUM(0);
int32 factor = PG_GETARG_INT32(1);
icu_interval_t *result;
result = (icu_interval_t *) palloc(sizeof(icu_interval_t));
if (pg_mul_s32_overflow(itv->day, factor, &result->day) ||
pg_mul_s32_overflow(itv->month, factor, &result->month) ||
pg_mul_s32_overflow(itv->year, factor, &result->year) ||
pg_mul_s64_overflow(itv->time, factor, &result->time))
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("interval out of range")));
}
return PointerGetDatum(result);
}
/* integer multiplied by icu_interval */
Datum
icu_mul_i_interval(PG_FUNCTION_ARGS)
{
Datum factor = PG_GETARG_DATUM(0);
Datum itv = PG_GETARG_DATUM(1);
return DirectFunctionCall2(icu_interval_mul, itv, factor);
}
/* icu_interval + icu_interval */
Datum
icu_interv_plus_interv(PG_FUNCTION_ARGS)
{
icu_interval_t *i1 = (icu_interval_t*) PG_GETARG_DATUM(0);
icu_interval_t *i2 = (icu_interval_t*) PG_GETARG_DATUM(1);
icu_interval_t *result;
result = (icu_interval_t *) palloc(sizeof(icu_interval_t));
if (pg_add_s32_overflow(i1->day, i2->day, &result->day) ||
pg_add_s32_overflow(i1->month, i2->month, &result->month) ||
pg_add_s32_overflow(i1->year, i2->year, &result->year) ||
pg_add_s64_overflow(i1->time, i2->time, &result->time))
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("interval out of range")));
}
return PointerGetDatum(result);
}
/* icu_interval - icu_interval */
Datum
icu_interv_minus_interv(PG_FUNCTION_ARGS)
{
icu_interval_t *i1 = (icu_interval_t*) PG_GETARG_DATUM(0);
icu_interval_t *i2 = (icu_interval_t*) PG_GETARG_DATUM(1);
icu_interval_t *result;
result = (icu_interval_t *) palloc(sizeof(icu_interval_t));
if (pg_add_s32_overflow(i1->day, -i2->day, &result->day) ||
pg_add_s32_overflow(i1->month, -i2->month, &result->month) ||
pg_add_s32_overflow(i1->year, -i2->year, &result->year) ||
pg_add_s64_overflow(i1->time, -i2->time, &result->time))
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("interval out of range")));
}
return PointerGetDatum(result);
}
/*
TODO:
- binary
- cast from icu_interval to interval?
- justify_interval?
*/