Age Owner Branch data TLA Line data Source code
1 : : /* src/interfaces/ecpg/pgtypeslib/interval.c */
2 : :
3 : : #include "postgres_fe.h"
4 : :
5 : : #include <time.h>
6 : : #include <math.h>
7 : : #include <limits.h>
8 : :
9 : : #include "common/string.h"
10 : : #include "dt.h"
11 : : #include "pgtypes_error.h"
12 : : #include "pgtypes_interval.h"
13 : : #include "pgtypeslib_extern.h"
14 : :
15 : : /*
16 : : * copy&pasted from .../src/backend/utils/adt/datetime.c
17 : : * and changed struct pg_tm to struct tm
18 : : */
19 : : static void
3265 tgl@sss.pgh.pa.us 20 :CBC 38 : AdjustFractSeconds(double frac, struct /* pg_ */ tm *tm, fsec_t *fsec, int scale)
21 : : {
22 : : int sec;
23 : :
6394 meskes@postgresql.or 24 [ + - ]: 38 : if (frac == 0)
25 : 38 : return;
6197 bruce@momjian.us 26 :UBC 0 : frac *= scale;
27 : 0 : sec = (int) frac;
6394 meskes@postgresql.or 28 : 0 : tm->tm_sec += sec;
6197 bruce@momjian.us 29 : 0 : frac -= sec;
30 : 0 : *fsec += rint(frac * 1000000);
31 : : }
32 : :
33 : :
34 : : /*
35 : : * copy&pasted from .../src/backend/utils/adt/datetime.c
36 : : * and changed struct pg_tm to struct tm
37 : : */
38 : : static void
3265 tgl@sss.pgh.pa.us 39 : 0 : AdjustFractDays(double frac, struct /* pg_ */ tm *tm, fsec_t *fsec, int scale)
40 : : {
41 : : int extra_days;
42 : :
6394 meskes@postgresql.or 43 [ # # ]: 0 : if (frac == 0)
44 : 0 : return;
6197 bruce@momjian.us 45 : 0 : frac *= scale;
46 : 0 : extra_days = (int) frac;
6394 meskes@postgresql.or 47 : 0 : tm->tm_mday += extra_days;
6197 bruce@momjian.us 48 : 0 : frac -= extra_days;
6394 meskes@postgresql.or 49 : 0 : AdjustFractSeconds(frac, tm, fsec, SECS_PER_DAY);
50 : : }
51 : :
52 : : /* copy&pasted from .../src/backend/utils/adt/datetime.c */
53 : : static int
3133 peter_e@gmx.net 54 : 0 : ParseISO8601Number(const char *str, char **endptr, int *ipart, double *fpart)
55 : : {
56 : : double val;
57 : :
6394 meskes@postgresql.or 58 [ # # # # : 0 : if (!(isdigit((unsigned char) *str) || *str == '-' || *str == '.'))
# # ]
59 : 0 : return DTERR_BAD_FORMAT;
60 : 0 : errno = 0;
428 peter@eisentraut.org 61 : 0 : val = strtod(str, endptr);
62 : : /* did we not see anything that looks like a double? */
6394 meskes@postgresql.or 63 [ # # # # ]: 0 : if (*endptr == str || errno != 0)
64 : 0 : return DTERR_BAD_FORMAT;
65 : : /* watch out for overflow */
66 [ # # # # ]: 0 : if (val < INT_MIN || val > INT_MAX)
67 : 0 : return DTERR_FIELD_OVERFLOW;
68 : : /* be very sure we truncate towards zero (cf dtrunc()) */
69 [ # # ]: 0 : if (val >= 0)
70 : 0 : *ipart = (int) floor(val);
71 : : else
72 : 0 : *ipart = (int) -floor(-val);
73 : 0 : *fpart = val - *ipart;
74 : 0 : return 0;
75 : : }
76 : :
77 : : /* copy&pasted from .../src/backend/utils/adt/datetime.c */
78 : : static int
3133 peter_e@gmx.net 79 : 0 : ISO8601IntegerWidth(const char *fieldstart)
80 : : {
81 : : /* We might have had a leading '-' */
6394 meskes@postgresql.or 82 [ # # ]: 0 : if (*fieldstart == '-')
83 : 0 : fieldstart++;
84 : 0 : return strspn(fieldstart, "0123456789");
85 : : }
86 : :
87 : :
88 : : /*
89 : : * copy&pasted from .../src/backend/utils/adt/datetime.c
90 : : * and changed struct pg_tm to struct tm
91 : : */
92 : : static inline void
3265 tgl@sss.pgh.pa.us 93 :CBC 30 : ClearPgTm(struct /* pg_ */ tm *tm, fsec_t *fsec)
94 : : {
6394 meskes@postgresql.or 95 : 30 : tm->tm_year = 0;
6197 bruce@momjian.us 96 : 30 : tm->tm_mon = 0;
6394 meskes@postgresql.or 97 : 30 : tm->tm_mday = 0;
98 : 30 : tm->tm_hour = 0;
6197 bruce@momjian.us 99 : 30 : tm->tm_min = 0;
100 : 30 : tm->tm_sec = 0;
101 : 30 : *fsec = 0;
6394 meskes@postgresql.or 102 : 30 : }
103 : :
104 : : /*
105 : : * copy&pasted from .../src/backend/utils/adt/datetime.c
106 : : *
107 : : * * changed struct pg_tm to struct tm
108 : : *
109 : : * * Made the function static
110 : : */
111 : : static int
112 : 1 : DecodeISO8601Interval(char *str,
113 : : int *dtype, struct /* pg_ */ tm *tm, fsec_t *fsec)
114 : : {
6197 bruce@momjian.us 115 : 1 : bool datepart = true;
116 : 1 : bool havefield = false;
117 : :
6394 meskes@postgresql.or 118 : 1 : *dtype = DTK_DELTA;
119 : 1 : ClearPgTm(tm, fsec);
120 : :
121 [ + - + - ]: 1 : if (strlen(str) < 2 || str[0] != 'P')
122 : 1 : return DTERR_BAD_FORMAT;
123 : :
6394 meskes@postgresql.or 124 :UBC 0 : str++;
125 [ # # ]: 0 : while (*str)
126 : : {
127 : : char *fieldstart;
128 : : int val;
129 : : double fval;
130 : : char unit;
131 : : int dterr;
132 : :
6197 bruce@momjian.us 133 [ # # ]: 0 : if (*str == 'T') /* T indicates the beginning of the time part */
134 : : {
6394 meskes@postgresql.or 135 : 0 : datepart = false;
136 : 0 : havefield = false;
137 : 0 : str++;
138 : 0 : continue;
139 : : }
140 : :
141 : 0 : fieldstart = str;
142 : 0 : dterr = ParseISO8601Number(str, &str, &val, &fval);
143 [ # # ]: 0 : if (dterr)
144 : 0 : return dterr;
145 : :
146 : : /*
147 : : * Note: we could step off the end of the string here. Code below
148 : : * *must* exit the loop if unit == '\0'.
149 : : */
150 : 0 : unit = *str++;
151 : :
152 [ # # ]: 0 : if (datepart)
153 : : {
6197 bruce@momjian.us 154 [ # # # # : 0 : switch (unit) /* before T: Y M W D */
# # # ]
155 : : {
6394 meskes@postgresql.or 156 : 0 : case 'Y':
157 : 0 : tm->tm_year += val;
1761 bruce@momjian.us 158 : 0 : tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
6394 meskes@postgresql.or 159 : 0 : break;
160 : 0 : case 'M':
161 : 0 : tm->tm_mon += val;
162 : 0 : AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
163 : 0 : break;
164 : 0 : case 'W':
165 : 0 : tm->tm_mday += val * 7;
166 : 0 : AdjustFractDays(fval, tm, fsec, 7);
167 : 0 : break;
168 : 0 : case 'D':
169 : 0 : tm->tm_mday += val;
170 : 0 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
171 : 0 : break;
6197 bruce@momjian.us 172 : 0 : case 'T': /* ISO 8601 4.4.3.3 Alternative Format / Basic */
173 : : case '\0':
6394 meskes@postgresql.or 174 [ # # # # ]: 0 : if (ISO8601IntegerWidth(fieldstart) == 8 && !havefield)
175 : : {
176 : 0 : tm->tm_year += val / 10000;
6197 bruce@momjian.us 177 : 0 : tm->tm_mon += (val / 100) % 100;
6394 meskes@postgresql.or 178 : 0 : tm->tm_mday += val % 100;
179 : 0 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
180 [ # # ]: 0 : if (unit == '\0')
181 : 0 : return 0;
182 : 0 : datepart = false;
183 : 0 : havefield = false;
184 : 0 : continue;
185 : : }
186 : : /* Else fall through to extended alternative format */
187 : : pg_fallthrough;
188 : : case '-': /* ISO 8601 4.4.3.3 Alternative Format,
189 : : * Extended */
190 [ # # ]: 0 : if (havefield)
191 : 0 : return DTERR_BAD_FORMAT;
192 : :
193 : 0 : tm->tm_year += val;
1761 bruce@momjian.us 194 : 0 : tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
6394 meskes@postgresql.or 195 [ # # ]: 0 : if (unit == '\0')
196 : 0 : return 0;
197 [ # # ]: 0 : if (unit == 'T')
198 : : {
199 : 0 : datepart = false;
200 : 0 : havefield = false;
201 : 0 : continue;
202 : : }
203 : :
204 : 0 : dterr = ParseISO8601Number(str, &str, &val, &fval);
205 [ # # ]: 0 : if (dterr)
206 : 0 : return dterr;
6197 bruce@momjian.us 207 : 0 : tm->tm_mon += val;
6394 meskes@postgresql.or 208 : 0 : AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
209 [ # # ]: 0 : if (*str == '\0')
210 : 0 : return 0;
211 [ # # ]: 0 : if (*str == 'T')
212 : : {
213 : 0 : datepart = false;
214 : 0 : havefield = false;
215 : 0 : continue;
216 : : }
217 [ # # ]: 0 : if (*str != '-')
218 : 0 : return DTERR_BAD_FORMAT;
219 : 0 : str++;
220 : :
221 : 0 : dterr = ParseISO8601Number(str, &str, &val, &fval);
222 [ # # ]: 0 : if (dterr)
223 : 0 : return dterr;
224 : 0 : tm->tm_mday += val;
225 : 0 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
226 [ # # ]: 0 : if (*str == '\0')
227 : 0 : return 0;
228 [ # # ]: 0 : if (*str == 'T')
229 : : {
230 : 0 : datepart = false;
231 : 0 : havefield = false;
232 : 0 : continue;
233 : : }
234 : 0 : return DTERR_BAD_FORMAT;
235 : 0 : default:
236 : : /* not a valid date unit suffix */
237 : 0 : return DTERR_BAD_FORMAT;
238 : : }
239 : : }
240 : : else
241 : : {
6197 bruce@momjian.us 242 [ # # # # : 0 : switch (unit) /* after T: H M S */
# # ]
243 : : {
6394 meskes@postgresql.or 244 : 0 : case 'H':
245 : 0 : tm->tm_hour += val;
246 : 0 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
247 : 0 : break;
248 : 0 : case 'M':
249 : 0 : tm->tm_min += val;
250 : 0 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
251 : 0 : break;
252 : 0 : case 'S':
253 : 0 : tm->tm_sec += val;
254 : 0 : AdjustFractSeconds(fval, tm, fsec, 1);
255 : 0 : break;
6197 bruce@momjian.us 256 : 0 : case '\0': /* ISO 8601 4.4.3.3 Alternative Format */
257 [ # # # # ]: 0 : if (ISO8601IntegerWidth(fieldstart) == 6 && !havefield)
258 : : {
6394 meskes@postgresql.or 259 : 0 : tm->tm_hour += val / 10000;
6197 bruce@momjian.us 260 : 0 : tm->tm_min += (val / 100) % 100;
261 : 0 : tm->tm_sec += val % 100;
6394 meskes@postgresql.or 262 : 0 : AdjustFractSeconds(fval, tm, fsec, 1);
263 : 0 : return 0;
264 : : }
265 : : /* Else fall through to extended alternative format */
266 : : pg_fallthrough;
267 : : case ':': /* ISO 8601 4.4.3.3 Alternative Format,
268 : : * Extended */
269 [ # # ]: 0 : if (havefield)
270 : 0 : return DTERR_BAD_FORMAT;
271 : :
272 : 0 : tm->tm_hour += val;
273 : 0 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
274 [ # # ]: 0 : if (unit == '\0')
275 : 0 : return 0;
276 : :
277 : 0 : dterr = ParseISO8601Number(str, &str, &val, &fval);
278 [ # # ]: 0 : if (dterr)
279 : 0 : return dterr;
6197 bruce@momjian.us 280 : 0 : tm->tm_min += val;
6394 meskes@postgresql.or 281 : 0 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
282 [ # # ]: 0 : if (*str == '\0')
283 : 0 : return 0;
284 [ # # ]: 0 : if (*str != ':')
285 : 0 : return DTERR_BAD_FORMAT;
286 : 0 : str++;
287 : :
288 : 0 : dterr = ParseISO8601Number(str, &str, &val, &fval);
289 [ # # ]: 0 : if (dterr)
290 : 0 : return dterr;
6197 bruce@momjian.us 291 : 0 : tm->tm_sec += val;
6394 meskes@postgresql.or 292 : 0 : AdjustFractSeconds(fval, tm, fsec, 1);
293 [ # # ]: 0 : if (*str == '\0')
294 : 0 : return 0;
295 : 0 : return DTERR_BAD_FORMAT;
296 : :
297 : 0 : default:
298 : : /* not a valid time unit suffix */
299 : 0 : return DTERR_BAD_FORMAT;
300 : : }
301 : : }
302 : :
303 : 0 : havefield = true;
304 : : }
305 : :
306 : 0 : return 0;
307 : : }
308 : :
309 : :
310 : :
311 : : /*
312 : : * copy&pasted from .../src/backend/utils/adt/datetime.c
313 : : * with 3 exceptions
314 : : *
315 : : * * changed struct pg_tm to struct tm
316 : : *
317 : : * * ECPG code called this without a 'range' parameter
318 : : * removed 'int range' from the argument list and
319 : : * places where DecodeTime is called; and added
320 : : * int range = INTERVAL_FULL_RANGE;
321 : : *
322 : : * * ECPG seems not to have a global IntervalStyle
323 : : * so added
324 : : * int IntervalStyle = INTSTYLE_POSTGRES;
325 : : */
326 : : int
3265 tgl@sss.pgh.pa.us 327 :CBC 29 : DecodeInterval(char **field, int *ftype, int nf, /* int range, */
328 : : int *dtype, struct /* pg_ */ tm *tm, fsec_t *fsec)
329 : : {
6197 bruce@momjian.us 330 : 29 : int IntervalStyle = INTSTYLE_POSTGRES_VERBOSE;
331 : 29 : int range = INTERVAL_FULL_RANGE;
3209 peter_e@gmx.net 332 : 29 : bool is_before = false;
333 : : char *cp;
8462 meskes@postgresql.or 334 : 29 : int fmask = 0,
335 : : tmask,
336 : : type;
337 : : int i;
338 : : int dterr;
339 : : int val;
340 : : double fval;
341 : :
342 : 29 : *dtype = DTK_DELTA;
343 : 29 : type = IGNORE_DTF;
6197 bruce@momjian.us 344 : 29 : ClearPgTm(tm, fsec);
345 : :
346 : : /* read through list backwards to pick up units before values */
8462 meskes@postgresql.or 347 [ + + ]: 117 : for (i = nf - 1; i >= 0; i--)
348 : : {
349 [ + - + + : 89 : switch (ftype[i])
- ]
350 : : {
351 : 1 : case DTK_TIME:
6197 bruce@momjian.us 352 : 1 : dterr = DecodeTime(field[i], /* range, */
353 : : &tmask, tm, fsec);
6394 meskes@postgresql.or 354 [ - + ]: 1 : if (dterr)
6394 meskes@postgresql.or 355 :UBC 0 : return dterr;
8462 meskes@postgresql.or 356 :CBC 1 : type = DTK_DAY;
357 : 1 : break;
358 : :
8462 meskes@postgresql.or 359 :UBC 0 : case DTK_TZ:
360 : :
361 : : /*
362 : : * Timezone is a token with a leading sign character and at
363 : : * least one digit; there could be ':', '.', '-' embedded in
364 : : * it as well.
365 : : */
3183 peter_e@gmx.net 366 [ # # # # ]: 0 : Assert(*field[i] == '-' || *field[i] == '+');
367 : :
368 : : /*
369 : : * Try for hh:mm or hh:mm:ss. If not, fall through to
370 : : * DTK_NUMBER case, which can handle signed float numbers and
371 : : * signed year-month values.
372 : : */
6394 meskes@postgresql.or 373 [ # # # # ]: 0 : if (strchr(field[i] + 1, ':') != NULL &&
6197 bruce@momjian.us 374 : 0 : DecodeTime(field[i] + 1, /* INTERVAL_FULL_RANGE, */
375 : : &tmask, tm, fsec) == 0)
376 : : {
8462 meskes@postgresql.or 377 [ # # ]: 0 : if (*field[i] == '-')
378 : : {
379 : : /* flip the sign on all fields */
380 : 0 : tm->tm_hour = -tm->tm_hour;
381 : 0 : tm->tm_min = -tm->tm_min;
382 : 0 : tm->tm_sec = -tm->tm_sec;
383 : 0 : *fsec = -(*fsec);
384 : : }
385 : :
386 : : /*
387 : : * Set the next type to be a day, if units are not
388 : : * specified. This handles the case of '1 +02:03' since we
389 : : * are reading right to left.
390 : : */
391 : 0 : type = DTK_DAY;
392 : 0 : tmask = DTK_M(TZ);
393 : 0 : break;
394 : : }
395 : : pg_fallthrough;
396 : :
397 : : case DTK_DATE:
398 : : case DTK_NUMBER:
6394 meskes@postgresql.or 399 [ - + ]:CBC 44 : if (type == IGNORE_DTF)
400 : : {
401 : : /* use typmod to decide what rightmost field is */
6394 meskes@postgresql.or 402 [ # # # # :UBC 0 : switch (range)
# # # ]
403 : : {
404 : 0 : case INTERVAL_MASK(YEAR):
405 : 0 : type = DTK_YEAR;
406 : 0 : break;
407 : 0 : case INTERVAL_MASK(MONTH):
408 : : case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
409 : 0 : type = DTK_MONTH;
410 : 0 : break;
411 : 0 : case INTERVAL_MASK(DAY):
412 : 0 : type = DTK_DAY;
413 : 0 : break;
414 : 0 : case INTERVAL_MASK(HOUR):
415 : : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
416 : : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
417 : : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
418 : 0 : type = DTK_HOUR;
419 : 0 : break;
420 : 0 : case INTERVAL_MASK(MINUTE):
421 : : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
422 : 0 : type = DTK_MINUTE;
423 : 0 : break;
424 : 0 : case INTERVAL_MASK(SECOND):
425 : : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
426 : : case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
427 : 0 : type = DTK_SECOND;
428 : 0 : break;
429 : 0 : default:
430 : 0 : type = DTK_SECOND;
431 : 0 : break;
432 : : }
433 : : }
434 : :
6394 meskes@postgresql.or 435 :CBC 44 : errno = 0;
3689 tgl@sss.pgh.pa.us 436 : 44 : val = strtoint(field[i], &cp, 10);
6394 meskes@postgresql.or 437 [ - + ]: 44 : if (errno == ERANGE)
6394 meskes@postgresql.or 438 :UBC 0 : return DTERR_FIELD_OVERFLOW;
439 : :
6394 meskes@postgresql.or 440 [ - + ]:CBC 44 : if (*cp == '-')
441 : : {
442 : : /* SQL "years-months" syntax */
443 : : int val2;
444 : :
3689 tgl@sss.pgh.pa.us 445 :UBC 0 : val2 = strtoint(cp + 1, &cp, 10);
6394 meskes@postgresql.or 446 [ # # # # : 0 : if (errno == ERANGE || val2 < 0 || val2 >= MONTHS_PER_YEAR)
# # ]
447 : 0 : return DTERR_FIELD_OVERFLOW;
448 [ # # ]: 0 : if (*cp != '\0')
449 : 0 : return DTERR_BAD_FORMAT;
450 : 0 : type = DTK_MONTH;
451 [ # # ]: 0 : if (*field[i] == '-')
452 : 0 : val2 = -val2;
453 : 0 : val = val * MONTHS_PER_YEAR + val2;
454 : 0 : fval = 0;
455 : : }
6394 meskes@postgresql.or 456 [ - + ]:CBC 44 : else if (*cp == '.')
457 : : {
6394 meskes@postgresql.or 458 :UBC 0 : errno = 0;
428 peter@eisentraut.org 459 : 0 : fval = strtod(cp, &cp);
6394 meskes@postgresql.or 460 [ # # # # ]: 0 : if (*cp != '\0' || errno != 0)
461 : 0 : return DTERR_BAD_FORMAT;
462 : :
463 [ # # ]: 0 : if (*field[i] == '-')
7676 bruce@momjian.us 464 : 0 : fval = -fval;
465 : : }
8462 meskes@postgresql.or 466 [ + - ]:CBC 44 : else if (*cp == '\0')
467 : 44 : fval = 0;
468 : : else
6394 meskes@postgresql.or 469 :UBC 0 : return DTERR_BAD_FORMAT;
470 : :
8462 meskes@postgresql.or 471 :CBC 44 : tmask = 0; /* DTK_M(type); */
472 : :
473 : 44 : switch (type)
[ - - + +
+ + - - +
- - - - ]
474 : : {
8462 meskes@postgresql.or 475 :UBC 0 : case DTK_MICROSEC:
6394 476 : 0 : *fsec += rint(val + fval);
477 : 0 : tmask = DTK_M(MICROSECOND);
8462 478 : 0 : break;
479 : :
480 : 0 : case DTK_MILLISEC:
6394 481 : 0 : *fsec += rint((val + fval) * 1000);
482 : 0 : tmask = DTK_M(MILLISECOND);
8462 483 : 0 : break;
484 : :
8462 meskes@postgresql.or 485 :CBC 5 : case DTK_SECOND:
486 : 5 : tm->tm_sec += val;
6394 487 : 5 : *fsec += rint(fval * 1000000);
488 : :
489 : : /*
490 : : * If any subseconds were specified, consider this
491 : : * microsecond and millisecond input as well.
492 : : */
493 [ + - ]: 5 : if (fval == 0)
494 : 5 : tmask = DTK_M(SECOND);
495 : : else
6394 meskes@postgresql.or 496 :UBC 0 : tmask = DTK_ALL_SECS_M;
8462 meskes@postgresql.or 497 :CBC 5 : break;
498 : :
499 : 7 : case DTK_MINUTE:
500 : 7 : tm->tm_min += val;
6394 501 : 7 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
8462 502 : 7 : tmask = DTK_M(MINUTE);
503 : 7 : break;
504 : :
505 : 25 : case DTK_HOUR:
506 : 25 : tm->tm_hour += val;
6394 507 : 25 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
8462 508 : 25 : tmask = DTK_M(HOUR);
6394 509 : 25 : type = DTK_DAY;
8462 510 : 25 : break;
511 : :
512 : 6 : case DTK_DAY:
513 : 6 : tm->tm_mday += val;
6394 514 : 6 : AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
7676 bruce@momjian.us 515 [ - + ]: 6 : tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
8462 meskes@postgresql.or 516 : 6 : break;
517 : :
8462 meskes@postgresql.or 518 :UBC 0 : case DTK_WEEK:
519 : 0 : tm->tm_mday += val * 7;
6394 520 : 0 : AdjustFractDays(fval, tm, fsec, 7);
7676 bruce@momjian.us 521 [ # # ]: 0 : tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
8462 meskes@postgresql.or 522 : 0 : break;
523 : :
524 : 0 : case DTK_MONTH:
525 : 0 : tm->tm_mon += val;
6394 526 : 0 : AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
8462 527 : 0 : tmask = DTK_M(MONTH);
528 : 0 : break;
529 : :
8462 meskes@postgresql.or 530 :CBC 1 : case DTK_YEAR:
531 : 1 : tm->tm_year += val;
1761 bruce@momjian.us 532 : 1 : tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
7676 533 [ - + ]: 1 : tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
8462 meskes@postgresql.or 534 : 1 : break;
535 : :
8462 meskes@postgresql.or 536 :UBC 0 : case DTK_DECADE:
537 : 0 : tm->tm_year += val * 10;
1761 bruce@momjian.us 538 : 0 : tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 10);
7676 539 [ # # ]: 0 : tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
8462 meskes@postgresql.or 540 : 0 : break;
541 : :
542 : 0 : case DTK_CENTURY:
543 : 0 : tm->tm_year += val * 100;
1761 bruce@momjian.us 544 : 0 : tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 100);
7676 545 [ # # ]: 0 : tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
8462 meskes@postgresql.or 546 : 0 : break;
547 : :
548 : 0 : case DTK_MILLENNIUM:
549 : 0 : tm->tm_year += val * 1000;
1761 bruce@momjian.us 550 : 0 : tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 1000);
7676 551 [ # # ]: 0 : tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
8462 meskes@postgresql.or 552 : 0 : break;
553 : :
554 : 0 : default:
6394 555 : 0 : return DTERR_BAD_FORMAT;
556 : : }
8462 meskes@postgresql.or 557 :CBC 44 : break;
558 : :
559 : 44 : case DTK_STRING:
560 : : case DTK_SPECIAL:
561 : 44 : type = DecodeUnits(i, field[i], &val);
562 [ - + ]: 44 : if (type == IGNORE_DTF)
8462 meskes@postgresql.or 563 :UBC 0 : continue;
564 : :
8462 meskes@postgresql.or 565 :CBC 44 : tmask = 0; /* DTK_M(type); */
566 [ + - - + ]: 44 : switch (type)
567 : : {
568 : 43 : case UNITS:
569 : 43 : type = val;
570 : 43 : break;
571 : :
8462 meskes@postgresql.or 572 :UBC 0 : case AGO:
3209 peter_e@gmx.net 573 : 0 : is_before = true;
8462 meskes@postgresql.or 574 : 0 : type = val;
575 : 0 : break;
576 : :
577 : 0 : case RESERV:
5780 tgl@sss.pgh.pa.us 578 : 0 : tmask = (DTK_DATE_M | DTK_TIME_M);
8462 meskes@postgresql.or 579 : 0 : *dtype = val;
580 : 0 : break;
581 : :
8462 meskes@postgresql.or 582 :CBC 1 : default:
6394 583 : 1 : return DTERR_BAD_FORMAT;
584 : : }
8462 585 : 43 : break;
586 : :
8462 meskes@postgresql.or 587 :UBC 0 : default:
6394 588 : 0 : return DTERR_BAD_FORMAT;
589 : : }
590 : :
8462 meskes@postgresql.or 591 [ - + ]:CBC 88 : if (tmask & fmask)
6394 meskes@postgresql.or 592 :UBC 0 : return DTERR_BAD_FORMAT;
8462 meskes@postgresql.or 593 :CBC 88 : fmask |= tmask;
594 : : }
595 : :
596 : : /* ensure that at least one time field has been found */
6394 597 [ - + ]: 28 : if (fmask == 0)
6394 meskes@postgresql.or 598 :UBC 0 : return DTERR_BAD_FORMAT;
599 : :
600 : : /* ensure fractional seconds are fractional */
8462 meskes@postgresql.or 601 [ - + ]:CBC 28 : if (*fsec != 0)
602 : : {
603 : : int sec;
604 : :
7540 bruce@momjian.us 605 :UBC 0 : sec = *fsec / USECS_PER_SEC;
606 : 0 : *fsec -= sec * USECS_PER_SEC;
8462 meskes@postgresql.or 607 : 0 : tm->tm_sec += sec;
608 : : }
609 : :
610 : : /*----------
611 : : * The SQL standard defines the interval literal
612 : : * '-1 1:00:00'
613 : : * to mean "negative 1 days and negative 1 hours", while Postgres
614 : : * traditionally treats this as meaning "negative 1 days and positive
615 : : * 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
616 : : * to all fields if there are no other explicit signs.
617 : : *
618 : : * We leave the signs alone if there are additional explicit signs.
619 : : * This protects us against misinterpreting postgres-style dump output,
620 : : * since the postgres-style output code has always put an explicit sign on
621 : : * all fields following a negative field. But note that SQL-spec output
622 : : * is ambiguous and can be misinterpreted on load! (So it's best practice
623 : : * to dump in postgres style, not SQL style.)
624 : : *----------
625 : : */
6394 meskes@postgresql.or 626 [ - + - - ]:CBC 28 : if (IntervalStyle == INTSTYLE_SQL_STANDARD && *field[0] == '-')
627 : : {
628 : : /* Check for additional explicit signs */
6197 bruce@momjian.us 629 :UBC 0 : bool more_signs = false;
630 : :
6394 meskes@postgresql.or 631 [ # # ]: 0 : for (i = 1; i < nf; i++)
632 : : {
633 [ # # # # ]: 0 : if (*field[i] == '-' || *field[i] == '+')
634 : : {
635 : 0 : more_signs = true;
636 : 0 : break;
637 : : }
638 : : }
639 : :
640 [ # # ]: 0 : if (!more_signs)
641 : : {
642 : : /*
643 : : * Rather than re-determining which field was field[0], just force
644 : : * 'em all negative.
645 : : */
646 [ # # ]: 0 : if (*fsec > 0)
647 : 0 : *fsec = -(*fsec);
648 [ # # ]: 0 : if (tm->tm_sec > 0)
649 : 0 : tm->tm_sec = -tm->tm_sec;
650 [ # # ]: 0 : if (tm->tm_min > 0)
651 : 0 : tm->tm_min = -tm->tm_min;
652 [ # # ]: 0 : if (tm->tm_hour > 0)
653 : 0 : tm->tm_hour = -tm->tm_hour;
654 [ # # ]: 0 : if (tm->tm_mday > 0)
655 : 0 : tm->tm_mday = -tm->tm_mday;
656 [ # # ]: 0 : if (tm->tm_mon > 0)
657 : 0 : tm->tm_mon = -tm->tm_mon;
658 [ # # ]: 0 : if (tm->tm_year > 0)
659 : 0 : tm->tm_year = -tm->tm_year;
660 : : }
661 : : }
662 : :
663 : : /* finally, AGO negates everything */
8462 meskes@postgresql.or 664 [ - + ]:CBC 28 : if (is_before)
665 : : {
8462 meskes@postgresql.or 666 :UBC 0 : *fsec = -(*fsec);
6394 667 : 0 : tm->tm_sec = -tm->tm_sec;
668 : 0 : tm->tm_min = -tm->tm_min;
669 : 0 : tm->tm_hour = -tm->tm_hour;
670 : 0 : tm->tm_mday = -tm->tm_mday;
671 : 0 : tm->tm_mon = -tm->tm_mon;
672 : 0 : tm->tm_year = -tm->tm_year;
673 : : }
674 : :
6394 meskes@postgresql.or 675 :CBC 28 : return 0;
676 : : }
677 : :
678 : :
679 : : /* copy&pasted from .../src/backend/utils/adt/datetime.c */
680 : : static char *
681 : 270 : AddVerboseIntPart(char *cp, int value, const char *units,
682 : : bool *is_zero, bool *is_before)
683 : : {
684 [ + + ]: 270 : if (value == 0)
685 : 195 : return cp;
686 : : /* first nonzero value sets is_before */
687 [ + + ]: 75 : if (*is_zero)
688 : : {
689 : 54 : *is_before = (value < 0);
690 : 54 : value = abs(value);
691 : : }
692 [ - + ]: 21 : else if (*is_before)
6394 meskes@postgresql.or 693 :UBC 0 : value = -value;
1855 bruce@momjian.us 694 [ + + ]:CBC 75 : sprintf(cp, " %d %s%s", value, units, (value == 1) ? "" : "s");
3209 peter_e@gmx.net 695 : 75 : *is_zero = false;
6394 meskes@postgresql.or 696 : 75 : return cp + strlen(cp);
697 : : }
698 : :
699 : : /* copy&pasted from .../src/backend/utils/adt/datetime.c */
700 : : static char *
6394 meskes@postgresql.or 701 :UBC 0 : AddPostgresIntPart(char *cp, int value, const char *units,
702 : : bool *is_zero, bool *is_before)
703 : : {
704 [ # # ]: 0 : if (value == 0)
705 : 0 : return cp;
706 [ # # ]: 0 : sprintf(cp, "%s%s%d %s%s",
707 [ # # ]: 0 : (!*is_zero) ? " " : "",
708 [ # # # # ]: 0 : (*is_before && value > 0) ? "+" : "",
709 : : value,
710 : : units,
711 : : (value != 1) ? "s" : "");
712 : :
713 : : /*
714 : : * Each nonzero field sets is_before for (only) the next one. This is a
715 : : * tad bizarre but it's how it worked before...
716 : : */
717 : 0 : *is_before = (value < 0);
3209 peter_e@gmx.net 718 : 0 : *is_zero = false;
6394 meskes@postgresql.or 719 : 0 : return cp + strlen(cp);
720 : : }
721 : :
722 : : /* copy&pasted from .../src/backend/utils/adt/datetime.c */
723 : : static char *
724 : 0 : AddISO8601IntPart(char *cp, int value, char units)
725 : : {
726 [ # # ]: 0 : if (value == 0)
727 : 0 : return cp;
728 : 0 : sprintf(cp, "%d%c", value, units);
729 : 0 : return cp + strlen(cp);
730 : : }
731 : :
732 : : /* copy&pasted from .../src/backend/utils/adt/datetime.c */
733 : : static void
6394 meskes@postgresql.or 734 :CBC 10 : AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
735 : : {
736 [ + - ]: 10 : if (fsec == 0)
737 : : {
738 [ - + ]: 10 : if (fillzeros)
6394 meskes@postgresql.or 739 :UBC 0 : sprintf(cp, "%02d", abs(sec));
740 : : else
6394 meskes@postgresql.or 741 :CBC 10 : sprintf(cp, "%d", abs(sec));
742 : : }
743 : : else
744 : : {
6394 meskes@postgresql.or 745 [ # # ]:UBC 0 : if (fillzeros)
1331 peter@eisentraut.org 746 : 0 : sprintf(cp, "%02d.%0*d", abs(sec), precision, abs(fsec));
747 : : else
748 : 0 : sprintf(cp, "%d.%0*d", abs(sec), precision, abs(fsec));
6394 meskes@postgresql.or 749 : 0 : TrimTrailingZeros(cp);
750 : : }
6394 meskes@postgresql.or 751 :CBC 10 : }
752 : :
753 : :
754 : : /*
755 : : * copy&pasted from .../src/backend/utils/adt/datetime.c
756 : : *
757 : : * Change pg_tm to tm
758 : : */
759 : :
760 : : void
3265 tgl@sss.pgh.pa.us 761 : 54 : EncodeInterval(struct /* pg_ */ tm *tm, fsec_t fsec, int style, char *str)
762 : : {
8462 meskes@postgresql.or 763 : 54 : char *cp = str;
6394 764 : 54 : int year = tm->tm_year;
6197 bruce@momjian.us 765 : 54 : int mon = tm->tm_mon;
6394 meskes@postgresql.or 766 : 54 : int mday = tm->tm_mday;
767 : 54 : int hour = tm->tm_hour;
6197 bruce@momjian.us 768 : 54 : int min = tm->tm_min;
769 : 54 : int sec = tm->tm_sec;
3209 peter_e@gmx.net 770 : 54 : bool is_before = false;
771 : 54 : bool is_zero = true;
772 : :
773 : : /*
774 : : * The sign of year and month are guaranteed to match, since they are
775 : : * stored internally as "month". But we'll need to check for is_before and
776 : : * is_zero when determining the signs of day and hour/minute/seconds
777 : : * fields.
778 : : */
8462 meskes@postgresql.or 779 [ - - - + ]: 54 : switch (style)
780 : : {
781 : : /* SQL Standard interval format */
6394 meskes@postgresql.or 782 :UBC 0 : case INTSTYLE_SQL_STANDARD:
783 : : {
6197 bruce@momjian.us 784 [ # # # # ]: 0 : bool has_negative = year < 0 || mon < 0 ||
1107 tgl@sss.pgh.pa.us 785 [ # # # # ]: 0 : mday < 0 || hour < 0 ||
786 [ # # # # : 0 : min < 0 || sec < 0 || fsec < 0;
# # ]
6197 bruce@momjian.us 787 [ # # # # ]: 0 : bool has_positive = year > 0 || mon > 0 ||
1107 tgl@sss.pgh.pa.us 788 [ # # # # ]: 0 : mday > 0 || hour > 0 ||
789 [ # # # # : 0 : min > 0 || sec > 0 || fsec > 0;
# # ]
6197 bruce@momjian.us 790 [ # # # # ]: 0 : bool has_year_month = year != 0 || mon != 0;
791 [ # # # # ]: 0 : bool has_day_time = mday != 0 || hour != 0 ||
1107 tgl@sss.pgh.pa.us 792 [ # # # # : 0 : min != 0 || sec != 0 || fsec != 0;
# # ]
6197 bruce@momjian.us 793 : 0 : bool has_day = mday != 0;
794 [ # # # # ]: 0 : bool sql_standard_value = !(has_negative && has_positive) &&
1107 tgl@sss.pgh.pa.us 795 [ # # # # ]: 0 : !(has_year_month && has_day_time);
796 : :
797 : : /*
798 : : * SQL Standard wants only 1 "<sign>" preceding the whole
799 : : * interval ... but can't do that if mixed signs.
800 : : */
6394 meskes@postgresql.or 801 [ # # # # ]: 0 : if (has_negative && sql_standard_value)
802 : : {
803 : 0 : *cp++ = '-';
804 : 0 : year = -year;
6197 bruce@momjian.us 805 : 0 : mon = -mon;
6394 meskes@postgresql.or 806 : 0 : mday = -mday;
807 : 0 : hour = -hour;
6197 bruce@momjian.us 808 : 0 : min = -min;
809 : 0 : sec = -sec;
6394 meskes@postgresql.or 810 : 0 : fsec = -fsec;
811 : : }
812 : :
813 [ # # # # ]: 0 : if (!has_negative && !has_positive)
814 : : {
815 : 0 : sprintf(cp, "0");
816 : : }
817 [ # # ]: 0 : else if (!sql_standard_value)
818 : : {
819 : : /*
820 : : * For non sql-standard interval values, force outputting
821 : : * the signs to avoid ambiguities with intervals with
822 : : * mixed sign components.
823 : : */
6197 bruce@momjian.us 824 [ # # # # ]: 0 : char year_sign = (year < 0 || mon < 0) ? '-' : '+';
825 [ # # ]: 0 : char day_sign = (mday < 0) ? '-' : '+';
826 [ # # # # : 0 : char sec_sign = (hour < 0 || min < 0 ||
# # ]
827 [ # # ]: 0 : sec < 0 || fsec < 0) ? '-' : '+';
828 : :
6394 meskes@postgresql.or 829 : 0 : sprintf(cp, "%c%d-%d %c%d %c%d:%02d:",
830 : : year_sign, abs(year), abs(mon),
831 : : day_sign, abs(mday),
832 : : sec_sign, abs(hour), abs(min));
8462 833 : 0 : cp += strlen(cp);
6394 834 : 0 : AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
835 : : }
836 [ # # ]: 0 : else if (has_year_month)
837 : : {
838 : 0 : sprintf(cp, "%d-%d", year, mon);
839 : : }
840 [ # # ]: 0 : else if (has_day)
841 : : {
842 : 0 : sprintf(cp, "%d %d:%02d:", mday, hour, min);
8462 843 : 0 : cp += strlen(cp);
6394 844 : 0 : AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
845 : : }
846 : : else
847 : : {
848 : 0 : sprintf(cp, "%d:%02d:", hour, min);
8462 849 : 0 : cp += strlen(cp);
6394 850 : 0 : AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
851 : : }
852 : : }
8462 853 : 0 : break;
854 : :
855 : : /* ISO 8601 "time-intervals by duration only" */
6394 856 : 0 : case INTSTYLE_ISO_8601:
857 : : /* special-case zero to avoid printing nothing */
858 [ # # # # : 0 : if (year == 0 && mon == 0 && mday == 0 &&
# # # # ]
6197 bruce@momjian.us 859 [ # # # # : 0 : hour == 0 && min == 0 && sec == 0 && fsec == 0)
# # ]
860 : : {
6394 meskes@postgresql.or 861 : 0 : sprintf(cp, "PT0S");
862 : 0 : break;
863 : : }
864 : 0 : *cp++ = 'P';
865 : 0 : cp = AddISO8601IntPart(cp, year, 'Y');
6197 bruce@momjian.us 866 : 0 : cp = AddISO8601IntPart(cp, mon, 'M');
6394 meskes@postgresql.or 867 : 0 : cp = AddISO8601IntPart(cp, mday, 'D');
868 [ # # # # : 0 : if (hour != 0 || min != 0 || sec != 0 || fsec != 0)
# # # # ]
869 : 0 : *cp++ = 'T';
870 : 0 : cp = AddISO8601IntPart(cp, hour, 'H');
6197 bruce@momjian.us 871 : 0 : cp = AddISO8601IntPart(cp, min, 'M');
6394 meskes@postgresql.or 872 [ # # # # ]: 0 : if (sec != 0 || fsec != 0)
873 : : {
874 [ # # # # ]: 0 : if (sec < 0 || fsec < 0)
875 : 0 : *cp++ = '-';
876 : 0 : AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
8462 877 : 0 : cp += strlen(cp);
6394 878 : 0 : *cp++ = 'S';
6113 879 : 0 : *cp = '\0';
880 : : }
6394 881 : 0 : break;
882 : :
883 : : /* Compatible with postgresql < 8.4 when DateStyle = 'iso' */
884 : 0 : case INTSTYLE_POSTGRES:
885 : 0 : cp = AddPostgresIntPart(cp, year, "year", &is_zero, &is_before);
886 : 0 : cp = AddPostgresIntPart(cp, mon, "mon", &is_zero, &is_before);
887 : 0 : cp = AddPostgresIntPart(cp, mday, "day", &is_zero, &is_before);
888 [ # # # # : 0 : if (is_zero || hour != 0 || min != 0 || sec != 0 || fsec != 0)
# # # # #
# ]
889 : : {
6197 bruce@momjian.us 890 [ # # # # : 0 : bool minus = (hour < 0 || min < 0 || sec < 0 || fsec < 0);
# # # # ]
891 : :
6394 meskes@postgresql.or 892 [ # # ]: 0 : sprintf(cp, "%s%s%02d:%02d:",
893 [ # # ]: 0 : is_zero ? "" : " ",
894 [ # # ]: 0 : (minus ? "-" : (is_before ? "+" : "")),
895 : : abs(hour), abs(min));
8462 896 : 0 : cp += strlen(cp);
6394 897 : 0 : AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
898 : : }
899 : 0 : break;
900 : :
901 : : /* Compatible with postgresql < 8.4 when DateStyle != 'iso' */
6394 meskes@postgresql.or 902 :CBC 54 : case INTSTYLE_POSTGRES_VERBOSE:
903 : : default:
904 : 54 : strcpy(cp, "@");
905 : 54 : cp++;
906 : 54 : cp = AddVerboseIntPart(cp, year, "year", &is_zero, &is_before);
907 : 54 : cp = AddVerboseIntPart(cp, mon, "mon", &is_zero, &is_before);
908 : 54 : cp = AddVerboseIntPart(cp, mday, "day", &is_zero, &is_before);
909 : 54 : cp = AddVerboseIntPart(cp, hour, "hour", &is_zero, &is_before);
910 : 54 : cp = AddVerboseIntPart(cp, min, "min", &is_zero, &is_before);
911 [ + + - + ]: 54 : if (sec != 0 || fsec != 0)
912 : : {
913 : 10 : *cp++ = ' ';
914 [ + - - + : 10 : if (sec < 0 || (sec == 0 && fsec < 0))
- - ]
915 : : {
6394 meskes@postgresql.or 916 [ # # ]:UBC 0 : if (is_zero)
3209 peter_e@gmx.net 917 : 0 : is_before = true;
6394 meskes@postgresql.or 918 [ # # ]: 0 : else if (!is_before)
919 : 0 : *cp++ = '-';
920 : : }
6394 meskes@postgresql.or 921 [ - + ]:CBC 10 : else if (is_before)
6394 meskes@postgresql.or 922 :UBC 0 : *cp++ = '-';
6394 meskes@postgresql.or 923 :CBC 10 : AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
8462 924 : 10 : cp += strlen(cp);
925 : : /* We output "ago", not negatives, so use abs(). */
6394 926 : 10 : sprintf(cp, " sec%s",
927 [ + + - + ]: 10 : (abs(sec) != 1 || fsec != 0) ? "s" : "");
3209 peter_e@gmx.net 928 : 10 : is_zero = false;
929 : : }
930 : : /* identically zero? then put in a unitless zero... */
6394 meskes@postgresql.or 931 [ - + ]: 54 : if (is_zero)
6394 meskes@postgresql.or 932 :UBC 0 : strcat(cp, " 0");
6394 meskes@postgresql.or 933 [ - + ]:CBC 54 : if (is_before)
6394 meskes@postgresql.or 934 :UBC 0 : strcat(cp, " ago");
8462 meskes@postgresql.or 935 :CBC 54 : break;
936 : : }
3183 peter_e@gmx.net 937 : 54 : }
938 : :
939 : :
940 : : /*
941 : : * interval2tm()
942 : : * Convert an interval data type to a tm structure.
943 : : */
944 : : static int
3265 tgl@sss.pgh.pa.us 945 : 54 : interval2tm(interval span, struct tm *tm, fsec_t *fsec)
946 : : {
947 : : int64 time;
948 : :
8462 meskes@postgresql.or 949 [ + + ]: 54 : if (span.month != 0)
950 : : {
7618 bruce@momjian.us 951 : 3 : tm->tm_year = span.month / MONTHS_PER_YEAR;
952 : 3 : tm->tm_mon = span.month % MONTHS_PER_YEAR;
953 : : }
954 : : else
955 : : {
8462 meskes@postgresql.or 956 : 51 : tm->tm_year = 0;
957 : 51 : tm->tm_mon = 0;
958 : : }
959 : :
960 : 54 : time = span.time;
961 : :
7540 bruce@momjian.us 962 : 54 : tm->tm_mday = time / USECS_PER_DAY;
963 : 54 : time -= tm->tm_mday * USECS_PER_DAY;
964 : 54 : tm->tm_hour = time / USECS_PER_HOUR;
965 : 54 : time -= tm->tm_hour * USECS_PER_HOUR;
966 : 54 : tm->tm_min = time / USECS_PER_MINUTE;
967 : 54 : time -= tm->tm_min * USECS_PER_MINUTE;
968 : 54 : tm->tm_sec = time / USECS_PER_SEC;
969 : 54 : *fsec = time - (tm->tm_sec * USECS_PER_SEC);
970 : :
8462 meskes@postgresql.or 971 : 54 : return 0;
972 : : } /* interval2tm() */
973 : :
974 : : static int
3265 tgl@sss.pgh.pa.us 975 : 28 : tm2interval(struct tm *tm, fsec_t fsec, interval * span)
976 : : {
4407 bruce@momjian.us 977 [ + - ]: 28 : if ((double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon > INT_MAX ||
978 [ - + ]: 28 : (double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon < INT_MIN)
4503 bruce@momjian.us 979 :UBC 0 : return -1;
7618 bruce@momjian.us 980 :CBC 28 : span->month = tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
981 : 28 : span->time = (((((((tm->tm_mday * INT64CONST(24)) +
7532 982 : 28 : tm->tm_hour) * INT64CONST(60)) +
983 : 28 : tm->tm_min) * INT64CONST(60)) +
984 : 28 : tm->tm_sec) * USECS_PER_SEC) + fsec;
985 : :
8462 meskes@postgresql.or 986 : 28 : return 0;
987 : : } /* tm2interval() */
988 : :
989 : : interval *
7198 990 : 16 : PGTYPESinterval_new(void)
991 : : {
992 : : interval *result;
993 : :
994 : 16 : result = (interval *) pgtypes_alloc(sizeof(interval));
995 : : /* result can be NULL if we run out of memory */
996 : 16 : return result;
997 : : }
998 : :
999 : : void
7178 bruce@momjian.us 1000 : 13 : PGTYPESinterval_free(interval * intvl)
1001 : : {
7198 meskes@postgresql.or 1002 : 13 : free(intvl);
1003 : 13 : }
1004 : :
1005 : : interval *
8460 1006 : 29 : PGTYPESinterval_from_asc(char *str, char **endptr)
1007 : : {
8299 1008 : 29 : interval *result = NULL;
1009 : : fsec_t fsec;
1010 : : struct tm tt,
8462 1011 : 29 : *tm = &tt;
1012 : : int dtype;
1013 : : int nf;
1014 : : char *field[MAXDATEFIELDS];
1015 : : int ftype[MAXDATEFIELDS];
1016 : : char lowstr[MAXDATELEN + MAXDATEFIELDS];
1017 : : char *realptr;
8335 bruce@momjian.us 1018 [ + + ]: 29 : char **ptr = (endptr != NULL) ? endptr : &realptr;
1019 : :
8462 meskes@postgresql.or 1020 : 29 : tm->tm_year = 0;
1021 : 29 : tm->tm_mon = 0;
1022 : 29 : tm->tm_mday = 0;
1023 : 29 : tm->tm_hour = 0;
1024 : 29 : tm->tm_min = 0;
1025 : 29 : tm->tm_sec = 0;
1026 : 29 : fsec = 0;
1027 : :
4485 noah@leadboat.com 1028 [ - + ]: 29 : if (strlen(str) > MAXDATELEN)
1029 : : {
8462 meskes@postgresql.or 1030 :UBC 0 : errno = PGTYPES_INTVL_BAD_INTERVAL;
1031 : 0 : return NULL;
1032 : : }
1033 : :
6219 meskes@postgresql.or 1034 [ + - + + ]:CBC 58 : if (ParseDateTime(str, lowstr, field, ftype, &nf, ptr) != 0 ||
6394 1035 [ + - ]: 30 : (DecodeInterval(field, ftype, nf, &dtype, tm, &fsec) != 0 &&
1036 : 1 : DecodeISO8601Interval(str, &dtype, tm, &fsec) != 0))
1037 : : {
8462 1038 : 1 : errno = PGTYPES_INTVL_BAD_INTERVAL;
1039 : 1 : return NULL;
1040 : : }
1041 : :
8299 1042 : 28 : result = (interval *) pgtypes_alloc(sizeof(interval));
8462 1043 [ - + ]: 28 : if (!result)
8462 meskes@postgresql.or 1044 :UBC 0 : return NULL;
1045 : :
8462 meskes@postgresql.or 1046 [ - + ]:CBC 28 : if (dtype != DTK_DELTA)
1047 : : {
8462 meskes@postgresql.or 1048 :UBC 0 : errno = PGTYPES_INTVL_BAD_INTERVAL;
7999 1049 : 0 : free(result);
8462 1050 : 0 : return NULL;
1051 : : }
1052 : :
8462 meskes@postgresql.or 1053 [ - + ]:CBC 28 : if (tm2interval(tm, fsec, result) != 0)
1054 : : {
8462 meskes@postgresql.or 1055 :UBC 0 : errno = PGTYPES_INTVL_BAD_INTERVAL;
7999 1056 : 0 : free(result);
8462 1057 : 0 : return NULL;
1058 : : }
1059 : :
7298 meskes@postgresql.or 1060 :CBC 28 : errno = 0;
8462 1061 : 28 : return result;
1062 : : }
1063 : :
1064 : : char *
7532 bruce@momjian.us 1065 : 54 : PGTYPESinterval_to_asc(interval * span)
1066 : : {
1067 : : struct tm tt,
8462 meskes@postgresql.or 1068 : 54 : *tm = &tt;
1069 : : fsec_t fsec;
1070 : : char buf[MAXDATELEN + 1];
6197 bruce@momjian.us 1071 : 54 : int IntervalStyle = INTSTYLE_POSTGRES_VERBOSE;
1072 : :
8462 meskes@postgresql.or 1073 [ - + ]: 54 : if (interval2tm(*span, tm, &fsec) != 0)
1074 : : {
8462 meskes@postgresql.or 1075 :UBC 0 : errno = PGTYPES_INTVL_BAD_INTERVAL;
1076 : 0 : return NULL;
1077 : : }
1078 : :
3183 peter_e@gmx.net 1079 :CBC 54 : EncodeInterval(tm, fsec, IntervalStyle, buf);
1080 : :
8335 bruce@momjian.us 1081 : 54 : return pgtypes_strdup(buf);
1082 : : }
1083 : :
1084 : : int
7207 meskes@postgresql.or 1085 : 17 : PGTYPESinterval_copy(interval * intvlsrc, interval * intvldest)
1086 : : {
1087 : 17 : intvldest->time = intvlsrc->time;
1088 : 17 : intvldest->month = intvlsrc->month;
1089 : :
8462 1090 : 17 : return 0;
1091 : : }
|