Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * timestamp.c
4 : : * Functions for the built-in SQL types "timestamp" and "interval".
5 : : *
6 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/utils/adt/timestamp.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : :
16 : : #include "postgres.h"
17 : :
18 : : #include <ctype.h>
19 : : #include <math.h>
20 : : #include <limits.h>
21 : : #include <sys/time.h>
22 : :
23 : : #include "access/xact.h"
24 : : #include "catalog/pg_type.h"
25 : : #include "common/int.h"
26 : : #include "common/int128.h"
27 : : #include "funcapi.h"
28 : : #include "libpq/pqformat.h"
29 : : #include "miscadmin.h"
30 : : #include "nodes/nodeFuncs.h"
31 : : #include "nodes/supportnodes.h"
32 : : #include "optimizer/optimizer.h"
33 : : #include "parser/scansup.h"
34 : : #include "utils/array.h"
35 : : #include "utils/builtins.h"
36 : : #include "utils/date.h"
37 : : #include "utils/datetime.h"
38 : : #include "utils/float.h"
39 : : #include "utils/numeric.h"
40 : : #include "utils/skipsupport.h"
41 : : #include "utils/sortsupport.h"
42 : :
43 : : /*
44 : : * gcc's -ffast-math switch breaks routines that expect exact results from
45 : : * expressions like timeval / SECS_PER_HOUR, where timeval is double.
46 : : */
47 : : #ifdef __FAST_MATH__
48 : : #error -ffast-math is known to break this code
49 : : #endif
50 : :
51 : : #define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
52 : :
53 : : /* Set at postmaster start */
54 : : TimestampTz PgStartTime;
55 : :
56 : : /* Set at configuration reload */
57 : : TimestampTz PgReloadTime;
58 : :
59 : : typedef struct
60 : : {
61 : : Timestamp current;
62 : : Timestamp finish;
63 : : Interval step;
64 : : int step_sign;
65 : : } generate_series_timestamp_fctx;
66 : :
67 : : typedef struct
68 : : {
69 : : TimestampTz current;
70 : : TimestampTz finish;
71 : : Interval step;
72 : : int step_sign;
73 : : pg_tz *attimezone;
74 : : } generate_series_timestamptz_fctx;
75 : :
76 : : /*
77 : : * The transition datatype for interval aggregates is declared as internal.
78 : : * It's a pointer to an IntervalAggState allocated in the aggregate context.
79 : : */
80 : : typedef struct IntervalAggState
81 : : {
82 : : int64 N; /* count of finite intervals processed */
83 : : Interval sumX; /* sum of finite intervals processed */
84 : : /* These counts are *not* included in N! Use IA_TOTAL_COUNT() as needed */
85 : : int64 pInfcount; /* count of +infinity intervals */
86 : : int64 nInfcount; /* count of -infinity intervals */
87 : : } IntervalAggState;
88 : :
89 : : #define IA_TOTAL_COUNT(ia) \
90 : : ((ia)->N + (ia)->pInfcount + (ia)->nInfcount)
91 : :
92 : : static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec);
93 : : static Timestamp dt2local(Timestamp dt, int timezone);
94 : : static bool AdjustIntervalForTypmod(Interval *interval, int32 typmod,
95 : : Node *escontext);
96 : : static TimestampTz timestamp2timestamptz(Timestamp timestamp);
97 : : static Timestamp timestamptz2timestamp(TimestampTz timestamp);
98 : :
99 : : static void EncodeSpecialInterval(const Interval *interval, char *str);
100 : : static void interval_um_internal(const Interval *interval, Interval *result);
101 : :
102 : : /* common code for timestamptypmodin and timestamptztypmodin */
103 : : static int32
843 michael@paquier.xyz 104 :CBC 77 : anytimestamp_typmodin(bool istz, ArrayType *ta)
105 : : {
106 : : int32 *tl;
107 : : int n;
108 : :
109 : 77 : tl = ArrayGetIntegerTypmods(ta, &n);
110 : :
111 : : /*
112 : : * we're not too tense about good error message here because grammar
113 : : * shouldn't allow wrong number of modifiers for TIMESTAMP
114 : : */
115 [ - + ]: 77 : if (n != 1)
843 michael@paquier.xyz 116 [ # # ]:UBC 0 : ereport(ERROR,
117 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
118 : : errmsg("invalid type modifier")));
119 : :
843 michael@paquier.xyz 120 :CBC 77 : return anytimestamp_typmod_check(istz, tl[0]);
121 : : }
122 : :
123 : : /* exported so parse_expr.c can use it */
124 : : int32
3308 tgl@sss.pgh.pa.us 125 : 362 : anytimestamp_typmod_check(bool istz, int32 typmod)
126 : : {
127 [ - + ]: 362 : if (typmod < 0)
6825 tgl@sss.pgh.pa.us 128 [ # # # # ]:UBC 0 : ereport(ERROR,
129 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
130 : : errmsg("TIMESTAMP(%d)%s precision must not be negative",
131 : : typmod, (istz ? " WITH TIME ZONE" : ""))));
3308 tgl@sss.pgh.pa.us 132 [ + + ]:CBC 362 : if (typmod > MAX_TIMESTAMP_PRECISION)
133 : : {
6825 134 [ + - + + ]: 18 : ereport(WARNING,
135 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
136 : : errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
137 : : typmod, (istz ? " WITH TIME ZONE" : ""),
138 : : MAX_TIMESTAMP_PRECISION)));
139 : 18 : typmod = MAX_TIMESTAMP_PRECISION;
140 : : }
141 : :
142 : 362 : return typmod;
143 : : }
144 : :
145 : : /* common code for timestamptypmodout and timestamptztypmodout */
146 : : static char *
147 : 10 : anytimestamp_typmodout(bool istz, int32 typmod)
148 : : {
149 [ + + ]: 10 : const char *tz = istz ? " with time zone" : " without time zone";
150 : :
151 [ + - ]: 10 : if (typmod >= 0)
4261 peter_e@gmx.net 152 : 10 : return psprintf("(%d)%s", (int) typmod, tz);
153 : : else
1096 drowley@postgresql.o 154 :UBC 0 : return pstrdup(tz);
155 : : }
156 : :
157 : :
158 : : /*****************************************************************************
159 : : * USER I/O ROUTINES *
160 : : *****************************************************************************/
161 : :
162 : : /* timestamp_in()
163 : : * Convert a string to internal form.
164 : : */
165 : : Datum
9220 tgl@sss.pgh.pa.us 166 :CBC 8593 : timestamp_in(PG_FUNCTION_ARGS)
167 : : {
168 : 8593 : char *str = PG_GETARG_CSTRING(0);
169 : : #ifdef NOT_USED
170 : : Oid typelem = PG_GETARG_OID(1);
171 : : #endif
8739 lockhart@fourpalms.o 172 : 8593 : int32 typmod = PG_GETARG_INT32(2);
1002 tgl@sss.pgh.pa.us 173 : 8593 : Node *escontext = fcinfo->context;
174 : : Timestamp result;
175 : : fsec_t fsec;
176 : : struct pg_tm tt,
9334 lockhart@fourpalms.o 177 : 8593 : *tm = &tt;
178 : : int tz;
179 : : int dtype;
180 : : int nf;
181 : : int dterr;
182 : : char *field[MAXDATEFIELDS];
183 : : int ftype[MAXDATEFIELDS];
184 : : char workbuf[MAXDATELEN + MAXDATEFIELDS];
185 : : DateTimeErrorExtra extra;
186 : :
7408 neilc@samurai.com 187 : 8593 : dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
188 : : field, ftype, MAXDATEFIELDS, &nf);
8046 tgl@sss.pgh.pa.us 189 [ + - ]: 8593 : if (dterr == 0)
1002 190 : 8593 : dterr = DecodeDateTime(field, ftype, nf,
191 : : &dtype, tm, &fsec, &tz, &extra);
8046 192 [ + + ]: 8593 : if (dterr != 0)
193 : : {
1002 194 : 57 : DateTimeParseError(dterr, &extra, str, "timestamp", escontext);
195 : 12 : PG_RETURN_NULL();
196 : : }
197 : :
9334 lockhart@fourpalms.o 198 [ + + + + : 8536 : switch (dtype)
- ]
199 : : {
200 : 8345 : case DTK_DATE:
8744 201 [ + + ]: 8345 : if (tm2timestamp(tm, fsec, NULL, &result) != 0)
1002 tgl@sss.pgh.pa.us 202 [ + - ]: 9 : ereturn(escontext, (Datum) 0,
203 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
204 : : errmsg("timestamp out of range: \"%s\"", str)));
9334 lockhart@fourpalms.o 205 : 8336 : break;
206 : :
207 : 12 : case DTK_EPOCH:
8744 208 : 12 : result = SetEpochTimestamp();
9334 209 : 12 : break;
210 : :
211 : 104 : case DTK_LATE:
9220 tgl@sss.pgh.pa.us 212 : 104 : TIMESTAMP_NOEND(result);
9334 lockhart@fourpalms.o 213 : 104 : break;
214 : :
215 : 75 : case DTK_EARLY:
9220 tgl@sss.pgh.pa.us 216 : 75 : TIMESTAMP_NOBEGIN(result);
9334 lockhart@fourpalms.o 217 : 75 : break;
218 : :
9334 lockhart@fourpalms.o 219 :UBC 0 : default:
8077 tgl@sss.pgh.pa.us 220 [ # # ]: 0 : elog(ERROR, "unexpected dtype %d while parsing timestamp \"%s\"",
221 : : dtype, str);
222 : : TIMESTAMP_NOEND(result);
223 : : }
224 : :
1002 tgl@sss.pgh.pa.us 225 :CBC 8527 : AdjustTimestampForTypmod(&result, typmod, escontext);
226 : :
9220 227 : 8527 : PG_RETURN_TIMESTAMP(result);
228 : : }
229 : :
230 : : /* timestamp_out()
231 : : * Convert a timestamp to external form.
232 : : */
233 : : Datum
234 : 21952 : timestamp_out(PG_FUNCTION_ARGS)
235 : : {
7266 bruce@momjian.us 236 : 21952 : Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
237 : : char *result;
238 : : struct pg_tm tt,
8744 lockhart@fourpalms.o 239 : 21952 : *tm = &tt;
240 : : fsec_t fsec;
241 : : char buf[MAXDATELEN + 1];
242 : :
8739 243 [ + + + + ]: 21952 : if (TIMESTAMP_NOT_FINITE(timestamp))
244 : 267 : EncodeSpecialTimestamp(timestamp, buf);
7388 bruce@momjian.us 245 [ + - ]: 21685 : else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
4924 peter_e@gmx.net 246 : 21685 : EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf);
247 : : else
8077 tgl@sss.pgh.pa.us 248 [ # # ]:UBC 0 : ereport(ERROR,
249 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
250 : : errmsg("timestamp out of range")));
251 : :
8744 lockhart@fourpalms.o 252 :CBC 21952 : result = pstrdup(buf);
253 : 21952 : PG_RETURN_CSTRING(result);
254 : : }
255 : :
256 : : /*
257 : : * timestamp_recv - converts external binary format to timestamp
258 : : */
259 : : Datum
8153 tgl@sss.pgh.pa.us 260 :UBC 0 : timestamp_recv(PG_FUNCTION_ARGS)
261 : : {
262 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
263 : :
264 : : #ifdef NOT_USED
265 : : Oid typelem = PG_GETARG_OID(1);
266 : : #endif
7363 267 : 0 : int32 typmod = PG_GETARG_INT32(2);
268 : : Timestamp timestamp;
269 : : struct pg_tm tt,
7765 270 : 0 : *tm = &tt;
271 : : fsec_t fsec;
272 : :
273 : 0 : timestamp = (Timestamp) pq_getmsgint64(buf);
274 : :
275 : : /* range check: see if timestamp_out would like it */
276 [ # # # # ]: 0 : if (TIMESTAMP_NOT_FINITE(timestamp))
277 : : /* ok */ ;
3461 278 [ # # ]: 0 : else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0 ||
279 [ # # # # ]: 0 : !IS_VALID_TIMESTAMP(timestamp))
7765 280 [ # # ]: 0 : ereport(ERROR,
281 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
282 : : errmsg("timestamp out of range")));
283 : :
1002 284 : 0 : AdjustTimestampForTypmod(×tamp, typmod, NULL);
285 : :
7765 286 : 0 : PG_RETURN_TIMESTAMP(timestamp);
287 : : }
288 : :
289 : : /*
290 : : * timestamp_send - converts timestamp to binary format
291 : : */
292 : : Datum
8153 293 : 0 : timestamp_send(PG_FUNCTION_ARGS)
294 : : {
7266 bruce@momjian.us 295 : 0 : Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
296 : : StringInfoData buf;
297 : :
8153 tgl@sss.pgh.pa.us 298 : 0 : pq_begintypsend(&buf);
299 : 0 : pq_sendint64(&buf, timestamp);
300 : 0 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
301 : : }
302 : :
303 : : Datum
6825 tgl@sss.pgh.pa.us 304 :CBC 18 : timestamptypmodin(PG_FUNCTION_ARGS)
305 : : {
6505 bruce@momjian.us 306 : 18 : ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
307 : :
6825 tgl@sss.pgh.pa.us 308 : 18 : PG_RETURN_INT32(anytimestamp_typmodin(false, ta));
309 : : }
310 : :
311 : : Datum
312 : 5 : timestamptypmodout(PG_FUNCTION_ARGS)
313 : : {
6505 bruce@momjian.us 314 : 5 : int32 typmod = PG_GETARG_INT32(0);
315 : :
6825 tgl@sss.pgh.pa.us 316 : 5 : PG_RETURN_CSTRING(anytimestamp_typmodout(false, typmod));
317 : : }
318 : :
319 : :
320 : : /*
321 : : * timestamp_support()
322 : : *
323 : : * Planner support function for the timestamp_scale() and timestamptz_scale()
324 : : * length coercion functions (we need not distinguish them here).
325 : : */
326 : : Datum
2401 327 : 12 : timestamp_support(PG_FUNCTION_ARGS)
328 : : {
329 : 12 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
330 : 12 : Node *ret = NULL;
331 : :
332 [ + + ]: 12 : if (IsA(rawreq, SupportRequestSimplify))
333 : : {
334 : 6 : SupportRequestSimplify *req = (SupportRequestSimplify *) rawreq;
335 : :
336 : 6 : ret = TemporalSimplify(MAX_TIMESTAMP_PRECISION, (Node *) req->fcall);
337 : : }
338 : :
339 : 12 : PG_RETURN_POINTER(ret);
340 : : }
341 : :
342 : : /* timestamp_scale()
343 : : * Adjust time type for specified scale factor.
344 : : * Used by PostgreSQL type system to stuff columns.
345 : : */
346 : : Datum
8739 lockhart@fourpalms.o 347 : 31086 : timestamp_scale(PG_FUNCTION_ARGS)
348 : : {
7266 bruce@momjian.us 349 : 31086 : Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
8739 lockhart@fourpalms.o 350 : 31086 : int32 typmod = PG_GETARG_INT32(1);
351 : : Timestamp result;
352 : :
353 : 31086 : result = timestamp;
354 : :
1002 tgl@sss.pgh.pa.us 355 : 31086 : AdjustTimestampForTypmod(&result, typmod, NULL);
356 : :
8739 lockhart@fourpalms.o 357 : 31086 : PG_RETURN_TIMESTAMP(result);
358 : : }
359 : :
360 : : /*
361 : : * AdjustTimestampForTypmod --- round off a timestamp to suit given typmod
362 : : * Works for either timestamp or timestamptz.
363 : : *
364 : : * Returns true on success, false on failure (if escontext points to an
365 : : * ErrorSaveContext; otherwise errors are thrown).
366 : : */
367 : : bool
1002 tgl@sss.pgh.pa.us 368 : 62886 : AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
369 : : {
370 : : static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
371 : : INT64CONST(1000000),
372 : : INT64CONST(100000),
373 : : INT64CONST(10000),
374 : : INT64CONST(1000),
375 : : INT64CONST(100),
376 : : INT64CONST(10),
377 : : INT64CONST(1)
378 : : };
379 : :
380 : : static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
381 : : INT64CONST(500000),
382 : : INT64CONST(50000),
383 : : INT64CONST(5000),
384 : : INT64CONST(500),
385 : : INT64CONST(50),
386 : : INT64CONST(5),
387 : : INT64CONST(0)
388 : : };
389 : :
8539 lockhart@fourpalms.o 390 [ + + + + ]: 62886 : if (!TIMESTAMP_NOT_FINITE(*time)
391 [ + + + + ]: 62488 : && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
392 : : {
7411 bruce@momjian.us 393 [ + - - + ]: 31709 : if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
1002 tgl@sss.pgh.pa.us 394 [ # # ]:UBC 0 : ereturn(escontext, false,
395 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
396 : : errmsg("timestamp(%d) precision must be between %d and %d",
397 : : typmod, 0, MAX_TIMESTAMP_PRECISION)));
398 : :
8539 lockhart@fourpalms.o 399 [ + + ]:CBC 31709 : if (*time >= INT64CONST(0))
400 : : {
7410 bruce@momjian.us 401 : 31315 : *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
7266 402 : 31315 : TimestampScales[typmod];
403 : : }
404 : : else
405 : : {
8069 406 : 394 : *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
407 : 394 : * TimestampScales[typmod]);
408 : : }
409 : : }
410 : :
2173 akorotkov@postgresql 411 : 62886 : return true;
412 : : }
413 : :
414 : : /* timestamptz_in()
415 : : * Convert a string to internal form.
416 : : */
417 : : Datum
8744 lockhart@fourpalms.o 418 : 20891 : timestamptz_in(PG_FUNCTION_ARGS)
419 : : {
420 : 20891 : char *str = PG_GETARG_CSTRING(0);
421 : : #ifdef NOT_USED
422 : : Oid typelem = PG_GETARG_OID(1);
423 : : #endif
8739 424 : 20891 : int32 typmod = PG_GETARG_INT32(2);
1002 tgl@sss.pgh.pa.us 425 : 20891 : Node *escontext = fcinfo->context;
426 : : TimestampTz result;
427 : : fsec_t fsec;
428 : : struct pg_tm tt,
8744 lockhart@fourpalms.o 429 : 20891 : *tm = &tt;
430 : : int tz;
431 : : int dtype;
432 : : int nf;
433 : : int dterr;
434 : : char *field[MAXDATEFIELDS];
435 : : int ftype[MAXDATEFIELDS];
436 : : char workbuf[MAXDATELEN + MAXDATEFIELDS];
437 : : DateTimeErrorExtra extra;
438 : :
7408 neilc@samurai.com 439 : 20891 : dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
440 : : field, ftype, MAXDATEFIELDS, &nf);
8046 tgl@sss.pgh.pa.us 441 [ + - ]: 20891 : if (dterr == 0)
1002 442 : 20891 : dterr = DecodeDateTime(field, ftype, nf,
443 : : &dtype, tm, &fsec, &tz, &extra);
8046 444 [ + + ]: 20891 : if (dterr != 0)
445 : : {
1002 446 : 63 : DateTimeParseError(dterr, &extra, str, "timestamp with time zone",
447 : : escontext);
448 : 12 : PG_RETURN_NULL();
449 : : }
450 : :
8744 lockhart@fourpalms.o 451 [ + + + + : 20828 : switch (dtype)
- ]
452 : : {
453 : 20615 : case DTK_DATE:
454 [ + + ]: 20615 : if (tm2timestamp(tm, fsec, &tz, &result) != 0)
1002 tgl@sss.pgh.pa.us 455 [ + - ]: 12 : ereturn(escontext, (Datum) 0,
456 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
457 : : errmsg("timestamp out of range: \"%s\"", str)));
8744 lockhart@fourpalms.o 458 : 20603 : break;
459 : :
460 : 6 : case DTK_EPOCH:
461 : 6 : result = SetEpochTimestamp();
462 : 6 : break;
463 : :
464 : 125 : case DTK_LATE:
465 : 125 : TIMESTAMP_NOEND(result);
466 : 125 : break;
467 : :
468 : 82 : case DTK_EARLY:
469 : 82 : TIMESTAMP_NOBEGIN(result);
470 : 82 : break;
471 : :
8744 lockhart@fourpalms.o 472 :UBC 0 : default:
8077 tgl@sss.pgh.pa.us 473 [ # # ]: 0 : elog(ERROR, "unexpected dtype %d while parsing timestamptz \"%s\"",
474 : : dtype, str);
475 : : TIMESTAMP_NOEND(result);
476 : : }
477 : :
1002 tgl@sss.pgh.pa.us 478 :CBC 20816 : AdjustTimestampForTypmod(&result, typmod, escontext);
479 : :
8744 lockhart@fourpalms.o 480 : 20816 : PG_RETURN_TIMESTAMPTZ(result);
481 : : }
482 : :
483 : : /*
484 : : * Try to parse a timezone specification, and return its timezone offset value
485 : : * if it's acceptable. Otherwise, an error is thrown.
486 : : *
487 : : * Note: some code paths update tm->tm_isdst, and some don't; current callers
488 : : * don't care, so we don't bother being consistent.
489 : : */
490 : : static int
2999 tgl@sss.pgh.pa.us 491 : 99 : parse_sane_timezone(struct pg_tm *tm, text *zone)
492 : : {
493 : : char tzname[TZ_STRLEN_MAX + 1];
494 : : int dterr;
495 : : int tz;
496 : :
4204 alvherre@alvh.no-ip. 497 : 99 : text_to_cstring_buffer(zone, tzname, sizeof(tzname));
498 : :
499 : : /*
500 : : * Look up the requested timezone. First we try to interpret it as a
501 : : * numeric timezone specification; if DecodeTimezone decides it doesn't
502 : : * like the format, we try timezone abbreviations and names.
503 : : *
504 : : * Note pg_tzset happily parses numeric input that DecodeTimezone would
505 : : * reject. To avoid having it accept input that would otherwise be seen
506 : : * as invalid, it's enough to disallow having a digit in the first
507 : : * position of our input string.
508 : : */
4202 heikki.linnakangas@i 509 [ + + ]: 99 : if (isdigit((unsigned char) *tzname))
4204 alvherre@alvh.no-ip. 510 [ + - ]: 3 : ereport(ERROR,
511 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
512 : : errmsg("invalid input syntax for type %s: \"%s\"",
513 : : "numeric time zone", tzname),
514 : : errhint("Numeric time zones must have \"-\" or \"+\" as first character.")));
515 : :
1002 tgl@sss.pgh.pa.us 516 : 96 : dterr = DecodeTimezone(tzname, &tz);
517 [ + + ]: 96 : if (dterr != 0)
518 : : {
519 : : int type,
520 : : val;
521 : : pg_tz *tzp;
522 : :
523 [ + + ]: 42 : if (dterr == DTERR_TZDISP_OVERFLOW)
4204 alvherre@alvh.no-ip. 524 [ + - ]: 6 : ereport(ERROR,
525 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
526 : : errmsg("numeric time zone \"%s\" out of range", tzname)));
1002 tgl@sss.pgh.pa.us 527 [ - + ]: 36 : else if (dterr != DTERR_BAD_FORMAT)
4204 alvherre@alvh.no-ip. 528 [ # # ]:UBC 0 : ereport(ERROR,
529 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
530 : : errmsg("time zone \"%s\" not recognized", tzname)));
531 : :
904 tgl@sss.pgh.pa.us 532 :CBC 36 : type = DecodeTimezoneName(tzname, &val, &tzp);
533 : :
534 [ + + ]: 33 : if (type == TZNAME_FIXED_OFFSET)
535 : : {
536 : : /* fixed-offset abbreviation */
3978 537 : 6 : tz = -val;
538 : : }
904 539 [ + + ]: 27 : else if (type == TZNAME_DYNTZ)
540 : : {
541 : : /* dynamic-offset abbreviation, resolve using specified time */
3978 542 : 6 : tz = DetermineTimeZoneAbbrevOffset(tm, tzname, tzp);
543 : : }
544 : : else
545 : : {
546 : : /* full zone name */
904 547 : 21 : tz = DetermineTimeZoneOffset(tm, tzp);
548 : : }
549 : : }
550 : :
4204 alvherre@alvh.no-ip. 551 : 87 : return tz;
552 : : }
553 : :
554 : : /*
555 : : * Look up the requested timezone, returning a pg_tz struct.
556 : : *
557 : : * This is the same as DecodeTimezoneNameToTz, but starting with a text Datum.
558 : : */
559 : : static pg_tz *
903 tgl@sss.pgh.pa.us 560 : 48 : lookup_timezone(text *zone)
561 : : {
562 : : char tzname[TZ_STRLEN_MAX + 1];
563 : :
564 : 48 : text_to_cstring_buffer(zone, tzname, sizeof(tzname));
565 : :
566 : 48 : return DecodeTimezoneNameToTz(tzname);
567 : : }
568 : :
569 : : /*
570 : : * make_timestamp_internal
571 : : * workhorse for make_timestamp and make_timestamptz
572 : : */
573 : : static Timestamp
4204 alvherre@alvh.no-ip. 574 : 117 : make_timestamp_internal(int year, int month, int day,
575 : : int hour, int min, double sec)
576 : : {
577 : : struct pg_tm tm;
578 : : TimeOffset date;
579 : : TimeOffset time;
580 : : int dterr;
1803 tgl@sss.pgh.pa.us 581 : 117 : bool bc = false;
582 : : Timestamp result;
583 : :
4204 alvherre@alvh.no-ip. 584 : 117 : tm.tm_year = year;
585 : 117 : tm.tm_mon = month;
586 : 117 : tm.tm_mday = day;
587 : :
588 : : /* Handle negative years as BC */
1803 tgl@sss.pgh.pa.us 589 [ + + ]: 117 : if (tm.tm_year < 0)
590 : : {
591 : 3 : bc = true;
592 : 3 : tm.tm_year = -tm.tm_year;
593 : : }
594 : :
595 : 117 : dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
596 : :
4204 alvherre@alvh.no-ip. 597 [ + + ]: 117 : if (dterr != 0)
598 [ + - ]: 3 : ereport(ERROR,
599 : : (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
600 : : errmsg("date field value out of range: %d-%02d-%02d",
601 : : year, month, day)));
602 : :
603 [ - + - - : 114 : if (!IS_VALID_JULIAN(tm.tm_year, tm.tm_mon, tm.tm_mday))
- - - + -
- - - ]
4204 alvherre@alvh.no-ip. 604 [ # # ]:UBC 0 : ereport(ERROR,
605 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
606 : : errmsg("date out of range: %d-%02d-%02d",
607 : : year, month, day)));
608 : :
4204 alvherre@alvh.no-ip. 609 :CBC 114 : date = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
610 : :
611 : : /* Check for time overflow */
1920 tgl@sss.pgh.pa.us 612 [ - + ]: 114 : if (float_time_overflows(hour, min, sec))
4204 alvherre@alvh.no-ip. 613 [ # # ]:UBC 0 : ereport(ERROR,
614 : : (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
615 : : errmsg("time field value out of range: %d:%02d:%02g",
616 : : hour, min, sec)));
617 : :
618 : : /* This should match tm2time */
4204 alvherre@alvh.no-ip. 619 :CBC 114 : time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1920 tgl@sss.pgh.pa.us 620 : 114 : * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
621 : :
387 nathan@postgresql.or 622 [ + - - + : 114 : if (unlikely(pg_mul_s64_overflow(date, USECS_PER_DAY, &result) ||
- + ]
623 : : pg_add_s64_overflow(result, time, &result)))
4204 alvherre@alvh.no-ip. 624 [ # # ]:UBC 0 : ereport(ERROR,
625 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
626 : : errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
627 : : year, month, day,
628 : : hour, min, sec)));
629 : :
630 : : /* final range check catches just-out-of-range timestamps */
3461 tgl@sss.pgh.pa.us 631 [ + - - + ]:CBC 114 : if (!IS_VALID_TIMESTAMP(result))
3461 tgl@sss.pgh.pa.us 632 [ # # ]:UBC 0 : ereport(ERROR,
633 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
634 : : errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
635 : : year, month, day,
636 : : hour, min, sec)));
637 : :
4204 alvherre@alvh.no-ip. 638 :CBC 114 : return result;
639 : : }
640 : :
641 : : /*
642 : : * make_timestamp() - timestamp constructor
643 : : */
644 : : Datum
645 : 12 : make_timestamp(PG_FUNCTION_ARGS)
646 : : {
647 : 12 : int32 year = PG_GETARG_INT32(0);
648 : 12 : int32 month = PG_GETARG_INT32(1);
649 : 12 : int32 mday = PG_GETARG_INT32(2);
650 : 12 : int32 hour = PG_GETARG_INT32(3);
651 : 12 : int32 min = PG_GETARG_INT32(4);
652 : 12 : float8 sec = PG_GETARG_FLOAT8(5);
653 : : Timestamp result;
654 : :
655 : 12 : result = make_timestamp_internal(year, month, mday,
656 : : hour, min, sec);
657 : :
658 : 9 : PG_RETURN_TIMESTAMP(result);
659 : : }
660 : :
661 : : /*
662 : : * make_timestamptz() - timestamp with time zone constructor
663 : : */
664 : : Datum
665 : 6 : make_timestamptz(PG_FUNCTION_ARGS)
666 : : {
667 : 6 : int32 year = PG_GETARG_INT32(0);
668 : 6 : int32 month = PG_GETARG_INT32(1);
669 : 6 : int32 mday = PG_GETARG_INT32(2);
670 : 6 : int32 hour = PG_GETARG_INT32(3);
671 : 6 : int32 min = PG_GETARG_INT32(4);
672 : 6 : float8 sec = PG_GETARG_FLOAT8(5);
673 : : Timestamp result;
674 : :
675 : 6 : result = make_timestamp_internal(year, month, mday,
676 : : hour, min, sec);
677 : :
678 : 6 : PG_RETURN_TIMESTAMPTZ(timestamp2timestamptz(result));
679 : : }
680 : :
681 : : /*
682 : : * Construct a timestamp with time zone.
683 : : * As above, but the time zone is specified as seventh argument.
684 : : */
685 : : Datum
686 : 99 : make_timestamptz_at_timezone(PG_FUNCTION_ARGS)
687 : : {
688 : 99 : int32 year = PG_GETARG_INT32(0);
689 : 99 : int32 month = PG_GETARG_INT32(1);
690 : 99 : int32 mday = PG_GETARG_INT32(2);
691 : 99 : int32 hour = PG_GETARG_INT32(3);
692 : 99 : int32 min = PG_GETARG_INT32(4);
693 : 99 : float8 sec = PG_GETARG_FLOAT8(5);
694 : 99 : text *zone = PG_GETARG_TEXT_PP(6);
695 : : TimestampTz result;
696 : : Timestamp timestamp;
697 : : struct pg_tm tt;
698 : : int tz;
699 : : fsec_t fsec;
700 : :
701 : 99 : timestamp = make_timestamp_internal(year, month, mday,
702 : : hour, min, sec);
703 : :
704 [ - + ]: 99 : if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
4204 alvherre@alvh.no-ip. 705 [ # # ]:UBC 0 : ereport(ERROR,
706 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
707 : : errmsg("timestamp out of range")));
708 : :
4204 alvherre@alvh.no-ip. 709 :CBC 99 : tz = parse_sane_timezone(&tt, zone);
710 : :
3461 tgl@sss.pgh.pa.us 711 : 87 : result = dt2local(timestamp, -tz);
712 : :
713 [ + - - + ]: 87 : if (!IS_VALID_TIMESTAMP(result))
3461 tgl@sss.pgh.pa.us 714 [ # # ]:UBC 0 : ereport(ERROR,
715 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
716 : : errmsg("timestamp out of range")));
717 : :
3461 tgl@sss.pgh.pa.us 718 :CBC 87 : PG_RETURN_TIMESTAMPTZ(result);
719 : : }
720 : :
721 : : /*
722 : : * to_timestamp(double precision)
723 : : * Convert UNIX epoch to timestamptz.
724 : : */
725 : : Datum
3448 726 : 27 : float8_timestamptz(PG_FUNCTION_ARGS)
727 : : {
728 : 27 : float8 seconds = PG_GETARG_FLOAT8(0);
729 : : TimestampTz result;
730 : :
731 : : /* Deal with NaN and infinite inputs ... */
732 [ + + ]: 27 : if (isnan(seconds))
733 [ + - ]: 3 : ereport(ERROR,
734 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
735 : : errmsg("timestamp cannot be NaN")));
736 : :
737 [ + + ]: 24 : if (isinf(seconds))
738 : : {
739 [ + + ]: 6 : if (seconds < 0)
740 : 3 : TIMESTAMP_NOBEGIN(result);
741 : : else
742 : 3 : TIMESTAMP_NOEND(result);
743 : : }
744 : : else
745 : : {
746 : : /* Out of range? */
747 [ + - ]: 18 : if (seconds <
748 : : (float8) SECS_PER_DAY * (DATETIME_MIN_JULIAN - UNIX_EPOCH_JDATE)
3131 749 [ - + ]: 18 : || seconds >=
750 : : (float8) SECS_PER_DAY * (TIMESTAMP_END_JULIAN - UNIX_EPOCH_JDATE))
3448 tgl@sss.pgh.pa.us 751 [ # # ]:UBC 0 : ereport(ERROR,
752 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
753 : : errmsg("timestamp out of range: \"%g\"", seconds)));
754 : :
755 : : /* Convert UNIX epoch to Postgres epoch */
3448 tgl@sss.pgh.pa.us 756 :CBC 18 : seconds -= ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
757 : :
3131 758 : 18 : seconds = rint(seconds * USECS_PER_SEC);
759 : 18 : result = (int64) seconds;
760 : :
761 : : /* Recheck in case roundoff produces something just out of range */
3448 762 [ + - - + ]: 18 : if (!IS_VALID_TIMESTAMP(result))
3448 tgl@sss.pgh.pa.us 763 [ # # ]:UBC 0 : ereport(ERROR,
764 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
765 : : errmsg("timestamp out of range: \"%g\"",
766 : : PG_GETARG_FLOAT8(0))));
767 : : }
768 : :
3448 tgl@sss.pgh.pa.us 769 :CBC 24 : PG_RETURN_TIMESTAMP(result);
770 : : }
771 : :
772 : : /* timestamptz_out()
773 : : * Convert a timestamp to external form.
774 : : */
775 : : Datum
8744 lockhart@fourpalms.o 776 : 35691 : timestamptz_out(PG_FUNCTION_ARGS)
777 : : {
8153 tgl@sss.pgh.pa.us 778 : 35691 : TimestampTz dt = PG_GETARG_TIMESTAMPTZ(0);
779 : : char *result;
780 : : int tz;
781 : : struct pg_tm tt,
10225 bruce@momjian.us 782 : 35691 : *tm = &tt;
783 : : fsec_t fsec;
784 : : const char *tzn;
785 : : char buf[MAXDATELEN + 1];
786 : :
8744 lockhart@fourpalms.o 787 [ + + + + ]: 35691 : if (TIMESTAMP_NOT_FINITE(dt))
9220 tgl@sss.pgh.pa.us 788 : 376 : EncodeSpecialTimestamp(dt, buf);
7388 bruce@momjian.us 789 [ + - ]: 35315 : else if (timestamp2tm(dt, &tz, tm, &fsec, &tzn, NULL) == 0)
4924 peter_e@gmx.net 790 : 35315 : EncodeDateTime(tm, fsec, true, tz, tzn, DateStyle, buf);
791 : : else
8077 tgl@sss.pgh.pa.us 792 [ # # ]:UBC 0 : ereport(ERROR,
793 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
794 : : errmsg("timestamp out of range")));
795 : :
9220 tgl@sss.pgh.pa.us 796 :CBC 35691 : result = pstrdup(buf);
797 : 35691 : PG_RETURN_CSTRING(result);
798 : : }
799 : :
800 : : /*
801 : : * timestamptz_recv - converts external binary format to timestamptz
802 : : */
803 : : Datum
8153 tgl@sss.pgh.pa.us 804 :UBC 0 : timestamptz_recv(PG_FUNCTION_ARGS)
805 : : {
806 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
807 : :
808 : : #ifdef NOT_USED
809 : : Oid typelem = PG_GETARG_OID(1);
810 : : #endif
7363 811 : 0 : int32 typmod = PG_GETARG_INT32(2);
812 : : TimestampTz timestamp;
813 : : int tz;
814 : : struct pg_tm tt,
7765 815 : 0 : *tm = &tt;
816 : : fsec_t fsec;
817 : :
818 : 0 : timestamp = (TimestampTz) pq_getmsgint64(buf);
819 : :
820 : : /* range check: see if timestamptz_out would like it */
821 [ # # # # ]: 0 : if (TIMESTAMP_NOT_FINITE(timestamp))
822 : : /* ok */ ;
3461 823 [ # # ]: 0 : else if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0 ||
824 [ # # # # ]: 0 : !IS_VALID_TIMESTAMP(timestamp))
7765 825 [ # # ]: 0 : ereport(ERROR,
826 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
827 : : errmsg("timestamp out of range")));
828 : :
1002 829 : 0 : AdjustTimestampForTypmod(×tamp, typmod, NULL);
830 : :
7765 831 : 0 : PG_RETURN_TIMESTAMPTZ(timestamp);
832 : : }
833 : :
834 : : /*
835 : : * timestamptz_send - converts timestamptz to binary format
836 : : */
837 : : Datum
8153 838 : 0 : timestamptz_send(PG_FUNCTION_ARGS)
839 : : {
8069 bruce@momjian.us 840 : 0 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
841 : : StringInfoData buf;
842 : :
8153 tgl@sss.pgh.pa.us 843 : 0 : pq_begintypsend(&buf);
844 : 0 : pq_sendint64(&buf, timestamp);
845 : 0 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
846 : : }
847 : :
848 : : Datum
6825 tgl@sss.pgh.pa.us 849 :CBC 59 : timestamptztypmodin(PG_FUNCTION_ARGS)
850 : : {
6505 bruce@momjian.us 851 : 59 : ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
852 : :
6825 tgl@sss.pgh.pa.us 853 : 59 : PG_RETURN_INT32(anytimestamp_typmodin(true, ta));
854 : : }
855 : :
856 : : Datum
857 : 5 : timestamptztypmodout(PG_FUNCTION_ARGS)
858 : : {
6505 bruce@momjian.us 859 : 5 : int32 typmod = PG_GETARG_INT32(0);
860 : :
6825 tgl@sss.pgh.pa.us 861 : 5 : PG_RETURN_CSTRING(anytimestamp_typmodout(true, typmod));
862 : : }
863 : :
864 : :
865 : : /* timestamptz_scale()
866 : : * Adjust time type for specified scale factor.
867 : : * Used by PostgreSQL type system to stuff columns.
868 : : */
869 : : Datum
8739 lockhart@fourpalms.o 870 : 228 : timestamptz_scale(PG_FUNCTION_ARGS)
871 : : {
8153 tgl@sss.pgh.pa.us 872 : 228 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
8739 lockhart@fourpalms.o 873 : 228 : int32 typmod = PG_GETARG_INT32(1);
874 : : TimestampTz result;
875 : :
876 : 228 : result = timestamp;
877 : :
1002 tgl@sss.pgh.pa.us 878 : 228 : AdjustTimestampForTypmod(&result, typmod, NULL);
879 : :
8739 lockhart@fourpalms.o 880 : 228 : PG_RETURN_TIMESTAMPTZ(result);
881 : : }
882 : :
883 : :
884 : : /* interval_in()
885 : : * Convert a string to internal form.
886 : : *
887 : : * External format(s):
888 : : * Uses the generic date/time parsing and decoding routines.
889 : : */
890 : : Datum
9220 tgl@sss.pgh.pa.us 891 : 32842 : interval_in(PG_FUNCTION_ARGS)
892 : : {
893 : 32842 : char *str = PG_GETARG_CSTRING(0);
894 : : #ifdef NOT_USED
895 : : Oid typelem = PG_GETARG_OID(1);
896 : : #endif
8724 lockhart@fourpalms.o 897 : 32842 : int32 typmod = PG_GETARG_INT32(2);
1002 tgl@sss.pgh.pa.us 898 : 32842 : Node *escontext = fcinfo->context;
899 : : Interval *result;
900 : : struct pg_itm_in tt,
1253 901 : 32842 : *itm_in = &tt;
902 : : int dtype;
903 : : int nf;
904 : : int range;
905 : : int dterr;
906 : : char *field[MAXDATEFIELDS];
907 : : int ftype[MAXDATEFIELDS];
908 : : char workbuf[256];
909 : : DateTimeErrorExtra extra;
910 : :
911 : 32842 : itm_in->tm_year = 0;
912 : 32842 : itm_in->tm_mon = 0;
913 : 32842 : itm_in->tm_mday = 0;
914 : 32842 : itm_in->tm_usec = 0;
915 : :
6205 916 [ + + ]: 32842 : if (typmod >= 0)
917 : 168 : range = INTERVAL_RANGE(typmod);
918 : : else
919 : 32674 : range = INTERVAL_FULL_RANGE;
920 : :
7408 neilc@samurai.com 921 : 32842 : dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
922 : : ftype, MAXDATEFIELDS, &nf);
8046 tgl@sss.pgh.pa.us 923 [ + - ]: 32842 : if (dterr == 0)
6143 924 : 32842 : dterr = DecodeInterval(field, ftype, nf, range,
925 : : &dtype, itm_in);
926 : :
927 : : /* if those functions think it's a bad format, try ISO8601 style */
928 [ + + ]: 32842 : if (dterr == DTERR_BAD_FORMAT)
5931 bruce@momjian.us 929 : 306 : dterr = DecodeISO8601Interval(str,
930 : : &dtype, itm_in);
931 : :
8046 tgl@sss.pgh.pa.us 932 [ + + ]: 32842 : if (dterr != 0)
933 : : {
934 [ + + ]: 477 : if (dterr == DTERR_FIELD_OVERFLOW)
935 : 360 : dterr = DTERR_INTERVAL_OVERFLOW;
1002 936 : 477 : DateTimeParseError(dterr, &extra, str, "interval", escontext);
937 : 12 : PG_RETURN_NULL();
938 : : }
939 : :
8744 lockhart@fourpalms.o 940 : 32365 : result = (Interval *) palloc(sizeof(Interval));
941 : :
9334 942 [ + + + - ]: 32365 : switch (dtype)
943 : : {
944 : 31879 : case DTK_DELTA:
1253 tgl@sss.pgh.pa.us 945 [ + + ]: 31879 : if (itmin2interval(itm_in, result) != 0)
1002 946 [ + - ]: 9 : ereturn(escontext, (Datum) 0,
947 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
948 : : errmsg("interval out of range")));
8744 lockhart@fourpalms.o 949 : 31870 : break;
950 : :
662 dean.a.rasheed@gmail 951 : 285 : case DTK_LATE:
952 : 285 : INTERVAL_NOEND(result);
953 : 285 : break;
954 : :
955 : 201 : case DTK_EARLY:
956 : 201 : INTERVAL_NOBEGIN(result);
957 : 201 : break;
958 : :
10194 lockhart@fourpalms.o 959 :UBC 0 : default:
8077 tgl@sss.pgh.pa.us 960 [ # # ]: 0 : elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
961 : : dtype, str);
962 : : }
963 : :
1002 tgl@sss.pgh.pa.us 964 :CBC 32356 : AdjustIntervalForTypmod(result, typmod, escontext);
965 : :
8744 lockhart@fourpalms.o 966 : 32350 : PG_RETURN_INTERVAL_P(result);
967 : : }
968 : :
969 : : /* interval_out()
970 : : * Convert a time span to external form.
971 : : */
972 : : Datum
9220 tgl@sss.pgh.pa.us 973 : 8347 : interval_out(PG_FUNCTION_ARGS)
974 : : {
975 : 8347 : Interval *span = PG_GETARG_INTERVAL_P(0);
976 : : char *result;
977 : : struct pg_itm tt,
1253 978 : 8347 : *itm = &tt;
979 : : char buf[MAXDATELEN + 1];
980 : :
662 dean.a.rasheed@gmail 981 [ + + + + : 8347 : if (INTERVAL_NOT_FINITE(span))
+ + + + +
+ + + ]
982 : 1012 : EncodeSpecialInterval(span, buf);
983 : : else
984 : : {
985 : 7335 : interval2itm(*span, itm);
986 : 7335 : EncodeInterval(itm, IntervalStyle, buf);
987 : : }
988 : :
9220 tgl@sss.pgh.pa.us 989 : 8347 : result = pstrdup(buf);
990 : 8347 : PG_RETURN_CSTRING(result);
991 : : }
992 : :
993 : : /*
994 : : * interval_recv - converts external binary format to interval
995 : : */
996 : : Datum
8153 tgl@sss.pgh.pa.us 997 :UBC 0 : interval_recv(PG_FUNCTION_ARGS)
998 : : {
999 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
1000 : :
1001 : : #ifdef NOT_USED
1002 : : Oid typelem = PG_GETARG_OID(1);
1003 : : #endif
7363 1004 : 0 : int32 typmod = PG_GETARG_INT32(2);
1005 : : Interval *interval;
1006 : :
8153 1007 : 0 : interval = (Interval *) palloc(sizeof(Interval));
1008 : :
7411 bruce@momjian.us 1009 : 0 : interval->time = pq_getmsgint64(buf);
7353 1010 : 0 : interval->day = pq_getmsgint(buf, sizeof(interval->day));
7411 1011 : 0 : interval->month = pq_getmsgint(buf, sizeof(interval->month));
1012 : :
1002 tgl@sss.pgh.pa.us 1013 : 0 : AdjustIntervalForTypmod(interval, typmod, NULL);
1014 : :
8153 1015 : 0 : PG_RETURN_INTERVAL_P(interval);
1016 : : }
1017 : :
1018 : : /*
1019 : : * interval_send - converts interval to binary format
1020 : : */
1021 : : Datum
1022 : 0 : interval_send(PG_FUNCTION_ARGS)
1023 : : {
1024 : 0 : Interval *interval = PG_GETARG_INTERVAL_P(0);
1025 : : StringInfoData buf;
1026 : :
1027 : 0 : pq_begintypsend(&buf);
1028 : 0 : pq_sendint64(&buf, interval->time);
2887 andres@anarazel.de 1029 : 0 : pq_sendint32(&buf, interval->day);
1030 : 0 : pq_sendint32(&buf, interval->month);
8153 tgl@sss.pgh.pa.us 1031 : 0 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
1032 : : }
1033 : :
1034 : : /*
1035 : : * The interval typmod stores a "range" in its high 16 bits and a "precision"
1036 : : * in its low 16 bits. Both contribute to defining the resolution of the
1037 : : * type. Range addresses resolution granules larger than one second, and
1038 : : * precision specifies resolution below one second. This representation can
1039 : : * express all SQL standard resolutions, but we implement them all in terms of
1040 : : * truncating rightward from some position. Range is a bitmap of permitted
1041 : : * fields, but only the temporally-smallest such field is significant to our
1042 : : * calculations. Precision is a count of sub-second decimal places to retain.
1043 : : * Setting all bits (INTERVAL_FULL_PRECISION) gives the same truncation
1044 : : * semantics as choosing MAX_INTERVAL_PRECISION.
1045 : : */
1046 : : Datum
6825 tgl@sss.pgh.pa.us 1047 :CBC 177 : intervaltypmodin(PG_FUNCTION_ARGS)
1048 : : {
6505 bruce@momjian.us 1049 : 177 : ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
1050 : : int32 *tl;
1051 : : int n;
1052 : : int32 typmod;
1053 : :
6658 tgl@sss.pgh.pa.us 1054 : 177 : tl = ArrayGetIntegerTypmods(ta, &n);
1055 : :
1056 : : /*
1057 : : * tl[0] - interval range (fields bitmask) tl[1] - precision (optional)
1058 : : *
1059 : : * Note we must validate tl[0] even though it's normally guaranteed
1060 : : * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
1061 : : */
6825 1062 [ + - ]: 177 : if (n > 0)
1063 : : {
1064 [ + - ]: 177 : switch (tl[0])
1065 : : {
1066 : 177 : case INTERVAL_MASK(YEAR):
1067 : : case INTERVAL_MASK(MONTH):
1068 : : case INTERVAL_MASK(DAY):
1069 : : case INTERVAL_MASK(HOUR):
1070 : : case INTERVAL_MASK(MINUTE):
1071 : : case INTERVAL_MASK(SECOND):
1072 : : case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
1073 : : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
1074 : : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
1075 : : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1076 : : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
1077 : : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1078 : : case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1079 : : case INTERVAL_FULL_RANGE:
1080 : : /* all OK */
1081 : 177 : break;
6825 tgl@sss.pgh.pa.us 1082 :UBC 0 : default:
1083 [ # # ]: 0 : ereport(ERROR,
1084 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1085 : : errmsg("invalid INTERVAL type modifier")));
1086 : : }
1087 : : }
1088 : :
6825 tgl@sss.pgh.pa.us 1089 [ + + ]:CBC 177 : if (n == 1)
1090 : : {
1091 [ + - ]: 129 : if (tl[0] != INTERVAL_FULL_RANGE)
1092 : 129 : typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, tl[0]);
1093 : : else
6825 tgl@sss.pgh.pa.us 1094 :UBC 0 : typmod = -1;
1095 : : }
6825 tgl@sss.pgh.pa.us 1096 [ + - ]:CBC 48 : else if (n == 2)
1097 : : {
1098 [ - + ]: 48 : if (tl[1] < 0)
6825 tgl@sss.pgh.pa.us 1099 [ # # ]:UBC 0 : ereport(ERROR,
1100 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1101 : : errmsg("INTERVAL(%d) precision must not be negative",
1102 : : tl[1])));
6825 tgl@sss.pgh.pa.us 1103 [ - + ]:CBC 48 : if (tl[1] > MAX_INTERVAL_PRECISION)
1104 : : {
6825 tgl@sss.pgh.pa.us 1105 [ # # ]:UBC 0 : ereport(WARNING,
1106 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1107 : : errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1108 : : tl[1], MAX_INTERVAL_PRECISION)));
1109 : 0 : typmod = INTERVAL_TYPMOD(MAX_INTERVAL_PRECISION, tl[0]);
1110 : : }
1111 : : else
6825 tgl@sss.pgh.pa.us 1112 :CBC 48 : typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
1113 : : }
1114 : : else
1115 : : {
6825 tgl@sss.pgh.pa.us 1116 [ # # ]:UBC 0 : ereport(ERROR,
1117 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1118 : : errmsg("invalid INTERVAL type modifier")));
1119 : : typmod = 0; /* keep compiler quiet */
1120 : : }
1121 : :
6825 tgl@sss.pgh.pa.us 1122 :CBC 177 : PG_RETURN_INT32(typmod);
1123 : : }
1124 : :
1125 : : Datum
6825 tgl@sss.pgh.pa.us 1126 :UBC 0 : intervaltypmodout(PG_FUNCTION_ARGS)
1127 : : {
6505 bruce@momjian.us 1128 : 0 : int32 typmod = PG_GETARG_INT32(0);
6825 tgl@sss.pgh.pa.us 1129 : 0 : char *res = (char *) palloc(64);
1130 : : int fields;
1131 : : int precision;
1132 : : const char *fieldstr;
1133 : :
1134 [ # # ]: 0 : if (typmod < 0)
1135 : : {
1136 : 0 : *res = '\0';
1137 : 0 : PG_RETURN_CSTRING(res);
1138 : : }
1139 : :
1140 : 0 : fields = INTERVAL_RANGE(typmod);
1141 : 0 : precision = INTERVAL_PRECISION(typmod);
1142 : :
1143 [ # # # # : 0 : switch (fields)
# # # # #
# # # # #
# ]
1144 : : {
1145 : 0 : case INTERVAL_MASK(YEAR):
1146 : 0 : fieldstr = " year";
1147 : 0 : break;
1148 : 0 : case INTERVAL_MASK(MONTH):
1149 : 0 : fieldstr = " month";
1150 : 0 : break;
1151 : 0 : case INTERVAL_MASK(DAY):
1152 : 0 : fieldstr = " day";
1153 : 0 : break;
1154 : 0 : case INTERVAL_MASK(HOUR):
1155 : 0 : fieldstr = " hour";
1156 : 0 : break;
1157 : 0 : case INTERVAL_MASK(MINUTE):
1158 : 0 : fieldstr = " minute";
1159 : 0 : break;
1160 : 0 : case INTERVAL_MASK(SECOND):
1161 : 0 : fieldstr = " second";
1162 : 0 : break;
1163 : 0 : case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
1164 : 0 : fieldstr = " year to month";
1165 : 0 : break;
1166 : 0 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
1167 : 0 : fieldstr = " day to hour";
1168 : 0 : break;
1169 : 0 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
1170 : 0 : fieldstr = " day to minute";
1171 : 0 : break;
1172 : 0 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1173 : 0 : fieldstr = " day to second";
1174 : 0 : break;
1175 : 0 : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
1176 : 0 : fieldstr = " hour to minute";
1177 : 0 : break;
1178 : 0 : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1179 : 0 : fieldstr = " hour to second";
1180 : 0 : break;
1181 : 0 : case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1182 : 0 : fieldstr = " minute to second";
1183 : 0 : break;
1184 : 0 : case INTERVAL_FULL_RANGE:
1185 : 0 : fieldstr = "";
1186 : 0 : break;
1187 : 0 : default:
1188 [ # # ]: 0 : elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1189 : : fieldstr = "";
1190 : : break;
1191 : : }
1192 : :
1193 [ # # ]: 0 : if (precision != INTERVAL_FULL_PRECISION)
6204 1194 : 0 : snprintf(res, 64, "%s(%d)", fieldstr, precision);
1195 : : else
6825 1196 : 0 : snprintf(res, 64, "%s", fieldstr);
1197 : :
1198 : 0 : PG_RETURN_CSTRING(res);
1199 : : }
1200 : :
1201 : : /*
1202 : : * Given an interval typmod value, return a code for the least-significant
1203 : : * field that the typmod allows to be nonzero, for instance given
1204 : : * INTERVAL DAY TO HOUR we want to identify "hour".
1205 : : *
1206 : : * The results should be ordered by field significance, which means
1207 : : * we can't use the dt.h macros YEAR etc, because for some odd reason
1208 : : * they aren't ordered that way. Instead, arbitrarily represent
1209 : : * SECOND = 0, MINUTE = 1, HOUR = 2, DAY = 3, MONTH = 4, YEAR = 5.
1210 : : */
1211 : : static int
3175 tgl@sss.pgh.pa.us 1212 :CBC 18 : intervaltypmodleastfield(int32 typmod)
1213 : : {
1214 [ + + ]: 18 : if (typmod < 0)
1215 : 6 : return 0; /* SECOND */
1216 : :
1217 [ + + - - : 12 : switch (INTERVAL_RANGE(typmod))
- - - - +
- - - - -
- ]
1218 : : {
1219 : 3 : case INTERVAL_MASK(YEAR):
1220 : 3 : return 5; /* YEAR */
1221 : 6 : case INTERVAL_MASK(MONTH):
1222 : 6 : return 4; /* MONTH */
3175 tgl@sss.pgh.pa.us 1223 :UBC 0 : case INTERVAL_MASK(DAY):
1224 : 0 : return 3; /* DAY */
1225 : 0 : case INTERVAL_MASK(HOUR):
1226 : 0 : return 2; /* HOUR */
1227 : 0 : case INTERVAL_MASK(MINUTE):
1228 : 0 : return 1; /* MINUTE */
1229 : 0 : case INTERVAL_MASK(SECOND):
1230 : 0 : return 0; /* SECOND */
1231 : 0 : case INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH):
1232 : 0 : return 4; /* MONTH */
1233 : 0 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR):
1234 : 0 : return 2; /* HOUR */
3175 tgl@sss.pgh.pa.us 1235 :CBC 3 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
1236 : 3 : return 1; /* MINUTE */
3175 tgl@sss.pgh.pa.us 1237 :UBC 0 : case INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1238 : 0 : return 0; /* SECOND */
1239 : 0 : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE):
1240 : 0 : return 1; /* MINUTE */
1241 : 0 : case INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1242 : 0 : return 0; /* SECOND */
1243 : 0 : case INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND):
1244 : 0 : return 0; /* SECOND */
1245 : 0 : case INTERVAL_FULL_RANGE:
1246 : 0 : return 0; /* SECOND */
1247 : 0 : default:
1248 [ # # ]: 0 : elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1249 : : break;
1250 : : }
1251 : : return 0; /* can't get here, but keep compiler quiet */
1252 : : }
1253 : :
1254 : :
1255 : : /*
1256 : : * interval_support()
1257 : : *
1258 : : * Planner support function for interval_scale().
1259 : : *
1260 : : * Flatten superfluous calls to interval_scale(). The interval typmod is
1261 : : * complex to permit accepting and regurgitating all SQL standard variations.
1262 : : * For truncation purposes, it boils down to a single, simple granularity.
1263 : : */
1264 : : Datum
2401 tgl@sss.pgh.pa.us 1265 :CBC 18 : interval_support(PG_FUNCTION_ARGS)
1266 : : {
1267 : 18 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
4959 rhaas@postgresql.org 1268 : 18 : Node *ret = NULL;
1269 : :
2401 tgl@sss.pgh.pa.us 1270 [ + + ]: 18 : if (IsA(rawreq, SupportRequestSimplify))
1271 : : {
1272 : 9 : SupportRequestSimplify *req = (SupportRequestSimplify *) rawreq;
1273 : 9 : FuncExpr *expr = req->fcall;
1274 : : Node *typmod;
1275 : :
1276 [ - + ]: 9 : Assert(list_length(expr->args) >= 2);
1277 : :
1278 : 9 : typmod = (Node *) lsecond(expr->args);
1279 : :
1939 1280 [ + - + - ]: 9 : if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
1281 : : {
2401 1282 : 9 : Node *source = (Node *) linitial(expr->args);
1283 : 9 : int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1284 : : bool noop;
1285 : :
1286 [ - + ]: 9 : if (new_typmod < 0)
2401 tgl@sss.pgh.pa.us 1287 :UBC 0 : noop = true;
1288 : : else
1289 : : {
2401 tgl@sss.pgh.pa.us 1290 :CBC 9 : int32 old_typmod = exprTypmod(source);
1291 : : int old_least_field;
1292 : : int new_least_field;
1293 : : int old_precis;
1294 : : int new_precis;
1295 : :
1296 : 9 : old_least_field = intervaltypmodleastfield(old_typmod);
1297 : 9 : new_least_field = intervaltypmodleastfield(new_typmod);
1298 [ + + ]: 9 : if (old_typmod < 0)
1299 : 6 : old_precis = INTERVAL_FULL_PRECISION;
1300 : : else
1301 : 3 : old_precis = INTERVAL_PRECISION(old_typmod);
1302 : 9 : new_precis = INTERVAL_PRECISION(new_typmod);
1303 : :
1304 : : /*
1305 : : * Cast is a no-op if least field stays the same or decreases
1306 : : * while precision stays the same or increases. But
1307 : : * precision, which is to say, sub-second precision, only
1308 : : * affects ranges that include SECOND.
1309 : : */
1310 [ - + - - ]: 9 : noop = (new_least_field <= old_least_field) &&
2401 tgl@sss.pgh.pa.us 1311 [ # # ]:UBC 0 : (old_least_field > 0 /* SECOND */ ||
1312 [ # # ]: 0 : new_precis >= MAX_INTERVAL_PRECISION ||
1313 : : new_precis >= old_precis);
1314 : : }
2401 tgl@sss.pgh.pa.us 1315 [ - + ]:CBC 9 : if (noop)
2401 tgl@sss.pgh.pa.us 1316 :UBC 0 : ret = relabel_to_typmod(source, new_typmod);
1317 : : }
1318 : : }
1319 : :
4959 rhaas@postgresql.org 1320 :CBC 18 : PG_RETURN_POINTER(ret);
1321 : : }
1322 : :
1323 : : /* interval_scale()
1324 : : * Adjust interval type for specified fields.
1325 : : * Used by PostgreSQL type system to stuff columns.
1326 : : */
1327 : : Datum
8724 lockhart@fourpalms.o 1328 : 108 : interval_scale(PG_FUNCTION_ARGS)
1329 : : {
1330 : 108 : Interval *interval = PG_GETARG_INTERVAL_P(0);
1331 : 108 : int32 typmod = PG_GETARG_INT32(1);
1332 : : Interval *result;
1333 : :
1334 : 108 : result = palloc(sizeof(Interval));
1335 : 108 : *result = *interval;
1336 : :
1002 tgl@sss.pgh.pa.us 1337 : 108 : AdjustIntervalForTypmod(result, typmod, NULL);
1338 : :
8724 lockhart@fourpalms.o 1339 : 108 : PG_RETURN_INTERVAL_P(result);
1340 : : }
1341 : :
1342 : : /*
1343 : : * Adjust interval for specified precision, in both YEAR to SECOND
1344 : : * range and sub-second precision.
1345 : : *
1346 : : * Returns true on success, false on failure (if escontext points to an
1347 : : * ErrorSaveContext; otherwise errors are thrown).
1348 : : */
1349 : : static bool
1002 tgl@sss.pgh.pa.us 1350 : 32464 : AdjustIntervalForTypmod(Interval *interval, int32 typmod,
1351 : : Node *escontext)
1352 : : {
1353 : : static const int64 IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
1354 : : INT64CONST(1000000),
1355 : : INT64CONST(100000),
1356 : : INT64CONST(10000),
1357 : : INT64CONST(1000),
1358 : : INT64CONST(100),
1359 : : INT64CONST(10),
1360 : : INT64CONST(1)
1361 : : };
1362 : :
1363 : : static const int64 IntervalOffsets[MAX_INTERVAL_PRECISION + 1] = {
1364 : : INT64CONST(500000),
1365 : : INT64CONST(50000),
1366 : : INT64CONST(5000),
1367 : : INT64CONST(500),
1368 : : INT64CONST(50),
1369 : : INT64CONST(5),
1370 : : INT64CONST(0)
1371 : : };
1372 : :
1373 : : /* Typmod has no effect on infinite intervals */
662 dean.a.rasheed@gmail 1374 [ + + + + : 32464 : if (INTERVAL_NOT_FINITE(interval))
+ + + + +
+ + + ]
1375 : 510 : return true;
1376 : :
1377 : : /*
1378 : : * Unspecified range and precision? Then not necessary to adjust. Setting
1379 : : * typmod to -1 is the convention for all data types.
1380 : : */
6205 tgl@sss.pgh.pa.us 1381 [ + + ]: 31954 : if (typmod >= 0)
1382 : : {
8434 lockhart@fourpalms.o 1383 : 231 : int range = INTERVAL_RANGE(typmod);
1384 : 231 : int precision = INTERVAL_PRECISION(typmod);
1385 : :
1386 : : /*
1387 : : * Our interpretation of intervals with a limited set of fields is
1388 : : * that fields to the right of the last one specified are zeroed out,
1389 : : * but those to the left of it remain valid. Thus for example there
1390 : : * is no operational difference between INTERVAL YEAR TO MONTH and
1391 : : * INTERVAL MONTH. In some cases we could meaningfully enforce that
1392 : : * higher-order fields are zero; for example INTERVAL DAY could reject
1393 : : * nonzero "month" field. However that seems a bit pointless when we
1394 : : * can't do it consistently. (We cannot enforce a range limit on the
1395 : : * highest expected field, since we do not have any equivalent of
1396 : : * SQL's <interval leading field precision>.) If we ever decide to
1397 : : * revisit this, interval_support will likely require adjusting.
1398 : : *
1399 : : * Note: before PG 8.4 we interpreted a limited set of fields as
1400 : : * actually causing a "modulo" operation on a given value, potentially
1401 : : * losing high-order as well as low-order information. But there is
1402 : : * no support for such behavior in the standard, and it seems fairly
1403 : : * undesirable on data consistency grounds anyway. Now we only
1404 : : * perform truncation or rounding of low-order fields.
1405 : : */
1406 [ + + ]: 231 : if (range == INTERVAL_FULL_RANGE)
1407 : : {
1408 : : /* Do nothing... */
1409 : : }
1410 [ + + ]: 225 : else if (range == INTERVAL_MASK(YEAR))
1411 : : {
7352 bruce@momjian.us 1412 : 33 : interval->month = (interval->month / MONTHS_PER_YEAR) * MONTHS_PER_YEAR;
7353 1413 : 33 : interval->day = 0;
7411 1414 : 33 : interval->time = 0;
1415 : : }
8434 lockhart@fourpalms.o 1416 [ + + ]: 192 : else if (range == INTERVAL_MASK(MONTH))
1417 : : {
7353 bruce@momjian.us 1418 : 36 : interval->day = 0;
7411 1419 : 36 : interval->time = 0;
1420 : : }
1421 : : /* YEAR TO MONTH */
8434 lockhart@fourpalms.o 1422 [ + + ]: 156 : else if (range == (INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH)))
1423 : : {
7353 bruce@momjian.us 1424 : 9 : interval->day = 0;
7411 1425 : 9 : interval->time = 0;
1426 : : }
8434 lockhart@fourpalms.o 1427 [ + + ]: 147 : else if (range == INTERVAL_MASK(DAY))
1428 : : {
7353 bruce@momjian.us 1429 : 6 : interval->time = 0;
1430 : : }
8434 lockhart@fourpalms.o 1431 [ + + ]: 141 : else if (range == INTERVAL_MASK(HOUR))
1432 : : {
7411 bruce@momjian.us 1433 : 6 : interval->time = (interval->time / USECS_PER_HOUR) *
1434 : : USECS_PER_HOUR;
1435 : : }
8434 lockhart@fourpalms.o 1436 [ + + ]: 135 : else if (range == INTERVAL_MASK(MINUTE))
1437 : : {
7411 bruce@momjian.us 1438 : 6 : interval->time = (interval->time / USECS_PER_MINUTE) *
1439 : : USECS_PER_MINUTE;
1440 : : }
8434 lockhart@fourpalms.o 1441 [ + + ]: 129 : else if (range == INTERVAL_MASK(SECOND))
1442 : : {
1443 : : /* fractional-second rounding will be dealt with below */
1444 : : }
1445 : : /* DAY TO HOUR */
1446 [ + + ]: 111 : else if (range == (INTERVAL_MASK(DAY) |
1447 : : INTERVAL_MASK(HOUR)))
1448 : : {
7411 bruce@momjian.us 1449 : 12 : interval->time = (interval->time / USECS_PER_HOUR) *
1450 : : USECS_PER_HOUR;
1451 : : }
1452 : : /* DAY TO MINUTE */
8434 lockhart@fourpalms.o 1453 [ + + ]: 99 : else if (range == (INTERVAL_MASK(DAY) |
1454 : : INTERVAL_MASK(HOUR) |
1455 : : INTERVAL_MASK(MINUTE)))
1456 : : {
7411 bruce@momjian.us 1457 : 36 : interval->time = (interval->time / USECS_PER_MINUTE) *
1458 : : USECS_PER_MINUTE;
1459 : : }
1460 : : /* DAY TO SECOND */
8434 lockhart@fourpalms.o 1461 [ + + ]: 63 : else if (range == (INTERVAL_MASK(DAY) |
1462 : : INTERVAL_MASK(HOUR) |
1463 : : INTERVAL_MASK(MINUTE) |
1464 : : INTERVAL_MASK(SECOND)))
1465 : : {
1466 : : /* fractional-second rounding will be dealt with below */
1467 : : }
1468 : : /* HOUR TO MINUTE */
1469 [ + + ]: 45 : else if (range == (INTERVAL_MASK(HOUR) |
1470 : : INTERVAL_MASK(MINUTE)))
1471 : : {
7411 bruce@momjian.us 1472 : 6 : interval->time = (interval->time / USECS_PER_MINUTE) *
1473 : : USECS_PER_MINUTE;
1474 : : }
1475 : : /* HOUR TO SECOND */
8434 lockhart@fourpalms.o 1476 [ + + ]: 39 : else if (range == (INTERVAL_MASK(HOUR) |
1477 : : INTERVAL_MASK(MINUTE) |
1478 : : INTERVAL_MASK(SECOND)))
1479 : : {
1480 : : /* fractional-second rounding will be dealt with below */
1481 : : }
1482 : : /* MINUTE TO SECOND */
1483 [ - + ]: 27 : else if (range == (INTERVAL_MASK(MINUTE) |
1484 : : INTERVAL_MASK(SECOND)))
1485 : : {
1486 : : /* fractional-second rounding will be dealt with below */
1487 : : }
1488 : : else
8077 tgl@sss.pgh.pa.us 1489 [ # # ]:UBC 0 : elog(ERROR, "unrecognized interval typmod: %d", typmod);
1490 : :
1491 : : /* Need to adjust sub-second precision? */
8434 lockhart@fourpalms.o 1492 [ + + ]:CBC 231 : if (precision != INTERVAL_FULL_PRECISION)
1493 : : {
7411 bruce@momjian.us 1494 [ + - - + ]: 39 : if (precision < 0 || precision > MAX_INTERVAL_PRECISION)
1002 tgl@sss.pgh.pa.us 1495 [ # # ]:UBC 0 : ereturn(escontext, false,
1496 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1497 : : errmsg("interval(%d) precision must be between %d and %d",
1498 : : precision, 0, MAX_INTERVAL_PRECISION)));
1499 : :
8539 lockhart@fourpalms.o 1500 [ + + ]:CBC 39 : if (interval->time >= INT64CONST(0))
1501 : : {
571 tgl@sss.pgh.pa.us 1502 [ + + ]: 36 : if (pg_add_s64_overflow(interval->time,
1503 : 36 : IntervalOffsets[precision],
1504 : 36 : &interval->time))
1505 [ + - ]: 3 : ereturn(escontext, false,
1506 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1507 : : errmsg("interval out of range")));
1508 : 33 : interval->time -= interval->time % IntervalScales[precision];
1509 : : }
1510 : : else
1511 : : {
1512 [ + - ]: 3 : if (pg_sub_s64_overflow(interval->time,
1513 : 3 : IntervalOffsets[precision],
1514 : 3 : &interval->time))
1515 [ + - ]: 3 : ereturn(escontext, false,
1516 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1517 : : errmsg("interval out of range")));
571 tgl@sss.pgh.pa.us 1518 :UBC 0 : interval->time -= interval->time % IntervalScales[precision];
1519 : : }
1520 : : }
1521 : : }
1522 : :
1002 tgl@sss.pgh.pa.us 1523 :CBC 31948 : return true;
1524 : : }
1525 : :
1526 : : /*
1527 : : * make_interval - numeric Interval constructor
1528 : : */
1529 : : Datum
4204 alvherre@alvh.no-ip. 1530 : 66 : make_interval(PG_FUNCTION_ARGS)
1531 : : {
1532 : 66 : int32 years = PG_GETARG_INT32(0);
1533 : 66 : int32 months = PG_GETARG_INT32(1);
1534 : 66 : int32 weeks = PG_GETARG_INT32(2);
1535 : 66 : int32 days = PG_GETARG_INT32(3);
1536 : 66 : int32 hours = PG_GETARG_INT32(4);
1537 : 66 : int32 mins = PG_GETARG_INT32(5);
1538 : 66 : double secs = PG_GETARG_FLOAT8(6);
1539 : : Interval *result;
1540 : :
1541 : : /*
1542 : : * Reject out-of-range inputs. We reject any input values that cause
1543 : : * integer overflow of the corresponding interval fields.
1544 : : */
4203 tgl@sss.pgh.pa.us 1545 [ + + + + ]: 66 : if (isinf(secs) || isnan(secs))
678 dean.a.rasheed@gmail 1546 : 6 : goto out_of_range;
1547 : :
4204 alvherre@alvh.no-ip. 1548 : 60 : result = (Interval *) palloc(sizeof(Interval));
1549 : :
1550 : : /* years and months -> months */
678 dean.a.rasheed@gmail 1551 [ + + + + ]: 114 : if (pg_mul_s32_overflow(years, MONTHS_PER_YEAR, &result->month) ||
1552 : 54 : pg_add_s32_overflow(result->month, months, &result->month))
1553 : 12 : goto out_of_range;
1554 : :
1555 : : /* weeks and days -> days */
1556 [ + + + + ]: 90 : if (pg_mul_s32_overflow(weeks, DAYS_PER_WEEK, &result->day) ||
1557 : 42 : pg_add_s32_overflow(result->day, days, &result->day))
1558 : 12 : goto out_of_range;
1559 : :
1560 : : /* hours and mins -> usecs (cannot overflow 64-bit) */
1561 : 36 : result->time = hours * USECS_PER_HOUR + mins * USECS_PER_MINUTE;
1562 : :
1563 : : /* secs -> usecs */
1564 : 36 : secs = rint(float8_mul(secs, USECS_PER_SEC));
1565 [ + + + + : 60 : if (!FLOAT8_FITS_IN_INT64(secs) ||
+ + ]
1566 : 27 : pg_add_s64_overflow(result->time, (int64) secs, &result->time))
1567 : 12 : goto out_of_range;
1568 : :
1569 : : /* make sure that the result is finite */
662 1570 [ - + - - : 21 : if (INTERVAL_NOT_FINITE(result))
- - - + -
- - - ]
662 dean.a.rasheed@gmail 1571 :UBC 0 : goto out_of_range;
1572 : :
4204 alvherre@alvh.no-ip. 1573 :CBC 21 : PG_RETURN_INTERVAL_P(result);
1574 : :
678 dean.a.rasheed@gmail 1575 : 42 : out_of_range:
1576 [ + - ]: 42 : ereport(ERROR,
1577 : : errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1578 : : errmsg("interval out of range"));
1579 : :
1580 : : PG_RETURN_NULL(); /* keep compiler quiet */
1581 : : }
1582 : :
1583 : : /* EncodeSpecialTimestamp()
1584 : : * Convert reserved timestamp data type to string.
1585 : : */
1586 : : void
9334 lockhart@fourpalms.o 1587 : 667 : EncodeSpecialTimestamp(Timestamp dt, char *str)
1588 : : {
8744 1589 [ + + ]: 667 : if (TIMESTAMP_IS_NOBEGIN(dt))
1590 : 325 : strcpy(str, EARLY);
1591 [ + - ]: 342 : else if (TIMESTAMP_IS_NOEND(dt))
1592 : 342 : strcpy(str, LATE);
1593 : : else /* shouldn't happen */
6171 tgl@sss.pgh.pa.us 1594 [ # # ]:UBC 0 : elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
6171 tgl@sss.pgh.pa.us 1595 :CBC 667 : }
1596 : :
1597 : : static void
662 dean.a.rasheed@gmail 1598 : 1012 : EncodeSpecialInterval(const Interval *interval, char *str)
1599 : : {
1600 [ + + + - : 1012 : if (INTERVAL_IS_NOBEGIN(interval))
+ - ]
1601 : 497 : strcpy(str, EARLY);
1602 [ + - + - : 515 : else if (INTERVAL_IS_NOEND(interval))
+ - ]
1603 : 515 : strcpy(str, LATE);
1604 : : else /* shouldn't happen */
662 dean.a.rasheed@gmail 1605 [ # # ]:UBC 0 : elog(ERROR, "invalid argument for EncodeSpecialInterval");
662 dean.a.rasheed@gmail 1606 :CBC 1012 : }
1607 : :
1608 : : Datum
9220 tgl@sss.pgh.pa.us 1609 : 35278 : now(PG_FUNCTION_ARGS)
1610 : : {
7374 1611 : 35278 : PG_RETURN_TIMESTAMPTZ(GetCurrentTransactionStartTimestamp());
1612 : : }
1613 : :
1614 : : Datum
7074 bruce@momjian.us 1615 : 3 : statement_timestamp(PG_FUNCTION_ARGS)
1616 : : {
1617 : 3 : PG_RETURN_TIMESTAMPTZ(GetCurrentStatementStartTimestamp());
1618 : : }
1619 : :
1620 : : Datum
1621 : 16 : clock_timestamp(PG_FUNCTION_ARGS)
1622 : : {
1623 : 16 : PG_RETURN_TIMESTAMPTZ(GetCurrentTimestamp());
1624 : : }
1625 : :
1626 : : Datum
6334 tgl@sss.pgh.pa.us 1627 :UBC 0 : pg_postmaster_start_time(PG_FUNCTION_ARGS)
1628 : : {
7374 1629 : 0 : PG_RETURN_TIMESTAMPTZ(PgStartTime);
1630 : : }
1631 : :
1632 : : Datum
6334 1633 : 0 : pg_conf_load_time(PG_FUNCTION_ARGS)
1634 : : {
1635 : 0 : PG_RETURN_TIMESTAMPTZ(PgReloadTime);
1636 : : }
1637 : :
1638 : : /*
1639 : : * GetCurrentTimestamp -- get the current operating system time
1640 : : *
1641 : : * Result is in the form of a TimestampTz value, and is expressed to the
1642 : : * full precision of the gettimeofday() syscall
1643 : : */
1644 : : TimestampTz
7374 tgl@sss.pgh.pa.us 1645 :CBC 4220748 : GetCurrentTimestamp(void)
1646 : : {
1647 : : TimestampTz result;
1648 : : struct timeval tp;
1649 : :
1650 : 4220748 : gettimeofday(&tp, NULL);
1651 : :
7330 1652 : 4220748 : result = (TimestampTz) tp.tv_sec -
1653 : : ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
4686 heikki.linnakangas@i 1654 : 4220748 : result = (result * USECS_PER_SEC) + tp.tv_usec;
1655 : :
1656 : 4220748 : return result;
1657 : : }
1658 : :
1659 : : /*
1660 : : * GetSQLCurrentTimestamp -- implements CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(n)
1661 : : */
1662 : : TimestampTz
843 michael@paquier.xyz 1663 : 174 : GetSQLCurrentTimestamp(int32 typmod)
1664 : : {
1665 : : TimestampTz ts;
1666 : :
3308 tgl@sss.pgh.pa.us 1667 : 174 : ts = GetCurrentTransactionStartTimestamp();
1668 [ + + ]: 174 : if (typmod >= 0)
1002 1669 : 36 : AdjustTimestampForTypmod(&ts, typmod, NULL);
843 michael@paquier.xyz 1670 : 174 : return ts;
1671 : : }
1672 : :
1673 : : /*
1674 : : * GetSQLLocalTimestamp -- implements LOCALTIMESTAMP, LOCALTIMESTAMP(n)
1675 : : */
1676 : : Timestamp
1677 : 33 : GetSQLLocalTimestamp(int32 typmod)
1678 : : {
1679 : : Timestamp ts;
1680 : :
3308 tgl@sss.pgh.pa.us 1681 : 33 : ts = timestamptz2timestamp(GetCurrentTransactionStartTimestamp());
1682 [ + + ]: 33 : if (typmod >= 0)
1002 1683 : 3 : AdjustTimestampForTypmod(&ts, typmod, NULL);
843 michael@paquier.xyz 1684 : 33 : return ts;
1685 : : }
1686 : :
1687 : : /*
1688 : : * timeofday(*) -- returns the current time as a text.
1689 : : */
1690 : : Datum
2522 andres@anarazel.de 1691 : 400 : timeofday(PG_FUNCTION_ARGS)
1692 : : {
1693 : : struct timeval tp;
1694 : : char templ[128];
1695 : : char buf[128];
1696 : : pg_time_t tt;
1697 : :
1698 : 400 : gettimeofday(&tp, NULL);
1699 : 400 : tt = (pg_time_t) tp.tv_sec;
1700 : 400 : pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1701 : 400 : pg_localtime(&tt, session_timezone));
1702 : 400 : snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1703 : :
1704 : 400 : PG_RETURN_TEXT_P(cstring_to_text(buf));
1705 : : }
1706 : :
1707 : : /*
1708 : : * TimestampDifference -- convert the difference between two timestamps
1709 : : * into integer seconds and microseconds
1710 : : *
1711 : : * This is typically used to calculate a wait timeout for select(2),
1712 : : * which explains the otherwise-odd choice of output format.
1713 : : *
1714 : : * Both inputs must be ordinary finite timestamps (in current usage,
1715 : : * they'll be results from GetCurrentTimestamp()).
1716 : : *
1717 : : * We expect start_time <= stop_time. If not, we return zeros,
1718 : : * since then we're already past the previously determined stop_time.
1719 : : */
1720 : : void
7018 tgl@sss.pgh.pa.us 1721 : 514385 : TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
1722 : : long *secs, int *microsecs)
1723 : : {
1724 : 514385 : TimestampTz diff = stop_time - start_time;
1725 : :
1726 [ + + ]: 514385 : if (diff <= 0)
1727 : : {
1728 : 17 : *secs = 0;
1729 : 17 : *microsecs = 0;
1730 : : }
1731 : : else
1732 : : {
1733 : 514368 : *secs = (long) (diff / USECS_PER_SEC);
1734 : 514368 : *microsecs = (int) (diff % USECS_PER_SEC);
1735 : : }
1736 : 514385 : }
1737 : :
1738 : : /*
1739 : : * TimestampDifferenceMilliseconds -- convert the difference between two
1740 : : * timestamps into integer milliseconds
1741 : : *
1742 : : * This is typically used to calculate a wait timeout for WaitLatch()
1743 : : * or a related function. The choice of "long" as the result type
1744 : : * is to harmonize with that; furthermore, we clamp the result to at most
1745 : : * INT_MAX milliseconds, because that's all that WaitLatch() allows.
1746 : : *
1747 : : * We expect start_time <= stop_time. If not, we return zero,
1748 : : * since then we're already past the previously determined stop_time.
1749 : : *
1750 : : * Subtracting finite and infinite timestamps works correctly, returning
1751 : : * zero or INT_MAX as appropriate.
1752 : : *
1753 : : * Note we round up any fractional millisecond, since waiting for just
1754 : : * less than the intended timeout is undesirable.
1755 : : */
1756 : : long
1761 1757 : 248111 : TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
1758 : : {
1759 : : TimestampTz diff;
1760 : :
1761 : : /* Deal with zero or negative elapsed time quickly. */
954 1762 [ + + ]: 248111 : if (start_time >= stop_time)
1761 1763 : 4 : return 0;
1764 : : /* To not fail with timestamp infinities, we must detect overflow. */
954 1765 [ - + ]: 248107 : if (pg_sub_s64_overflow(stop_time, start_time, &diff))
954 tgl@sss.pgh.pa.us 1766 :UBC 0 : return (long) INT_MAX;
954 tgl@sss.pgh.pa.us 1767 [ - + ]:CBC 248107 : if (diff >= (INT_MAX * INT64CONST(1000) - 999))
954 tgl@sss.pgh.pa.us 1768 :UBC 0 : return (long) INT_MAX;
1769 : : else
1761 tgl@sss.pgh.pa.us 1770 :CBC 248107 : return (long) ((diff + 999) / 1000);
1771 : : }
1772 : :
1773 : : /*
1774 : : * TimestampDifferenceExceeds -- report whether the difference between two
1775 : : * timestamps is >= a threshold (expressed in milliseconds)
1776 : : *
1777 : : * Both inputs must be ordinary finite timestamps (in current usage,
1778 : : * they'll be results from GetCurrentTimestamp()).
1779 : : */
1780 : : bool
6704 1781 : 599966 : TimestampDifferenceExceeds(TimestampTz start_time,
1782 : : TimestampTz stop_time,
1783 : : int msec)
1784 : : {
1785 : 599966 : TimestampTz diff = stop_time - start_time;
1786 : :
1787 : 599966 : return (diff >= msec * INT64CONST(1000));
1788 : : }
1789 : :
1790 : : /*
1791 : : * Check if the difference between two timestamps is >= a given
1792 : : * threshold (expressed in seconds).
1793 : : */
1794 : : bool
199 akapila@postgresql.o 1795 :UBC 0 : TimestampDifferenceExceedsSeconds(TimestampTz start_time,
1796 : : TimestampTz stop_time,
1797 : : int threshold_sec)
1798 : : {
1799 : : long secs;
1800 : : int usecs;
1801 : :
1802 : : /* Calculate the difference in seconds */
1803 : 0 : TimestampDifference(start_time, stop_time, &secs, &usecs);
1804 : :
1805 : 0 : return (secs >= threshold_sec);
1806 : : }
1807 : :
1808 : : /*
1809 : : * Convert a time_t to TimestampTz.
1810 : : *
1811 : : * We do not use time_t internally in Postgres, but this is provided for use
1812 : : * by functions that need to interpret, say, a stat(2) result.
1813 : : *
1814 : : * To avoid having the function's ABI vary depending on the width of time_t,
1815 : : * we declare the argument as pg_time_t, which is cast-compatible with
1816 : : * time_t but always 64 bits wide (unless the platform has no 64-bit type).
1817 : : * This detail should be invisible to callers, at least at source code level.
1818 : : */
1819 : : TimestampTz
6411 tgl@sss.pgh.pa.us 1820 :CBC 21284 : time_t_to_timestamptz(pg_time_t tm)
1821 : : {
1822 : : TimestampTz result;
1823 : :
7330 1824 : 21284 : result = (TimestampTz) tm -
1825 : : ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
1826 : 21284 : result *= USECS_PER_SEC;
1827 : :
1828 : 21284 : return result;
1829 : : }
1830 : :
1831 : : /*
1832 : : * Convert a TimestampTz to time_t.
1833 : : *
1834 : : * This too is just marginally useful, but some places need it.
1835 : : *
1836 : : * To avoid having the function's ABI vary depending on the width of time_t,
1837 : : * we declare the result as pg_time_t, which is cast-compatible with
1838 : : * time_t but always 64 bits wide (unless the platform has no 64-bit type).
1839 : : * This detail should be invisible to callers, at least at source code level.
1840 : : */
1841 : : pg_time_t
7018 1842 : 20833 : timestamptz_to_time_t(TimestampTz t)
1843 : : {
1844 : : pg_time_t result;
1845 : :
6411 1846 : 20833 : result = (pg_time_t) (t / USECS_PER_SEC +
1847 : : ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
1848 : :
7018 1849 : 20833 : return result;
1850 : : }
1851 : :
1852 : : /*
1853 : : * Produce a C-string representation of a TimestampTz.
1854 : : *
1855 : : * This is mostly for use in emitting messages. The primary difference
1856 : : * from timestamptz_out is that we force the output format to ISO. Note
1857 : : * also that the result is in a static buffer, not pstrdup'd.
1858 : : *
1859 : : * See also pg_strftime.
1860 : : */
1861 : : const char *
6704 1862 : 1642 : timestamptz_to_str(TimestampTz t)
1863 : : {
1864 : : static char buf[MAXDATELEN + 1];
1865 : : int tz;
1866 : : struct pg_tm tt,
1867 : 1642 : *tm = &tt;
1868 : : fsec_t fsec;
1869 : : const char *tzn;
1870 : :
1871 [ + - - + ]: 1642 : if (TIMESTAMP_NOT_FINITE(t))
6704 tgl@sss.pgh.pa.us 1872 :UBC 0 : EncodeSpecialTimestamp(t, buf);
6704 tgl@sss.pgh.pa.us 1873 [ + - ]:CBC 1642 : else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
4924 peter_e@gmx.net 1874 : 1642 : EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1875 : : else
6704 tgl@sss.pgh.pa.us 1876 :UBC 0 : strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1877 : :
6704 tgl@sss.pgh.pa.us 1878 :CBC 1642 : return buf;
1879 : : }
1880 : :
1881 : :
1882 : : void
8539 lockhart@fourpalms.o 1883 : 156524 : dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
1884 : : {
1885 : : TimeOffset time;
1886 : :
9334 1887 : 156524 : time = jd;
1888 : :
7411 bruce@momjian.us 1889 : 156524 : *hour = time / USECS_PER_HOUR;
1890 : 156524 : time -= (*hour) * USECS_PER_HOUR;
1891 : 156524 : *min = time / USECS_PER_MINUTE;
1892 : 156524 : time -= (*min) * USECS_PER_MINUTE;
1893 : 156524 : *sec = time / USECS_PER_SEC;
1894 : 156524 : *fsec = time - (*sec * USECS_PER_SEC);
2999 tgl@sss.pgh.pa.us 1895 : 156524 : } /* dt2time() */
1896 : :
1897 : :
1898 : : /*
1899 : : * timestamp2tm() - Convert timestamp data type to POSIX time structure.
1900 : : *
1901 : : * Note that year is _not_ 1900-based, but is an explicit full value.
1902 : : * Also, month is one-based, _not_ zero-based.
1903 : : * Returns:
1904 : : * 0 on success
1905 : : * -1 on out of range
1906 : : *
1907 : : * If attimezone is NULL, the global timezone setting will be used.
1908 : : */
1909 : : int
1910 : 156518 : timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
1911 : : {
1912 : : Timestamp date;
1913 : : Timestamp time;
1914 : : pg_time_t utime;
1915 : :
1916 : : /* Use session timezone if caller asks for default */
4327 1917 [ + + ]: 156518 : if (attimezone == NULL)
1918 : 120096 : attimezone = session_timezone;
1919 : :
7272 1920 : 156518 : time = dt;
7411 bruce@momjian.us 1921 [ + + ]: 156518 : TMODULO(time, date, USECS_PER_DAY);
1922 : :
8539 lockhart@fourpalms.o 1923 [ + + ]: 156518 : if (time < INT64CONST(0))
1924 : : {
7411 bruce@momjian.us 1925 : 51583 : time += USECS_PER_DAY;
1926 : 51583 : date -= 1;
1927 : : }
1928 : :
1929 : : /* add offset to go from J2000 back to standard Julian date */
7272 tgl@sss.pgh.pa.us 1930 : 156518 : date += POSTGRES_EPOCH_JDATE;
1931 : :
1932 : : /* Julian day routine does not work for negative Julian days */
1933 [ + - - + ]: 156518 : if (date < 0 || date > (Timestamp) INT_MAX)
7272 tgl@sss.pgh.pa.us 1934 :UBC 0 : return -1;
1935 : :
7272 tgl@sss.pgh.pa.us 1936 :CBC 156518 : j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1937 : 156518 : dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1938 : :
1939 : : /* Done if no TZ conversion wanted */
7765 1940 [ + + ]: 156518 : if (tzp == NULL)
1941 : : {
1942 : 42939 : tm->tm_isdst = -1;
1943 : 42939 : tm->tm_gmtoff = 0;
1944 : 42939 : tm->tm_zone = NULL;
1945 [ - + ]: 42939 : if (tzn != NULL)
7765 tgl@sss.pgh.pa.us 1946 :UBC 0 : *tzn = NULL;
7765 tgl@sss.pgh.pa.us 1947 :CBC 42939 : return 0;
1948 : : }
1949 : :
1950 : : /*
1951 : : * If the time falls within the range of pg_time_t, use pg_localtime() to
1952 : : * rotate to the local time zone.
1953 : : *
1954 : : * First, convert to an integral timestamp, avoiding possibly
1955 : : * platform-specific roundoff-in-wrong-direction errors, and adjust to
1956 : : * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1957 : : * coding avoids hardwiring any assumptions about the width of pg_time_t,
1958 : : * so it should behave sanely on machines without int64.
1959 : : */
7411 bruce@momjian.us 1960 : 113579 : dt = (dt - *fsec) / USECS_PER_SEC +
1961 : : (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY;
7765 tgl@sss.pgh.pa.us 1962 : 113579 : utime = (pg_time_t) dt;
1963 [ + - ]: 113579 : if ((Timestamp) utime == dt)
1964 : : {
4327 1965 : 113579 : struct pg_tm *tx = pg_localtime(&utime, attimezone);
1966 : :
7765 1967 : 113579 : tm->tm_year = tx->tm_year + 1900;
1968 : 113579 : tm->tm_mon = tx->tm_mon + 1;
1969 : 113579 : tm->tm_mday = tx->tm_mday;
1970 : 113579 : tm->tm_hour = tx->tm_hour;
1971 : 113579 : tm->tm_min = tx->tm_min;
1972 : 113579 : tm->tm_sec = tx->tm_sec;
1973 : 113579 : tm->tm_isdst = tx->tm_isdst;
1974 : 113579 : tm->tm_gmtoff = tx->tm_gmtoff;
1975 : 113579 : tm->tm_zone = tx->tm_zone;
7351 bruce@momjian.us 1976 : 113579 : *tzp = -tm->tm_gmtoff;
7765 tgl@sss.pgh.pa.us 1977 [ + + ]: 113579 : if (tzn != NULL)
4923 peter_e@gmx.net 1978 : 44059 : *tzn = tm->tm_zone;
1979 : : }
1980 : : else
1981 : : {
1982 : : /*
1983 : : * When out of range of pg_time_t, treat as GMT
1984 : : */
7765 tgl@sss.pgh.pa.us 1985 :UBC 0 : *tzp = 0;
1986 : : /* Mark this as *no* time zone available */
8722 lockhart@fourpalms.o 1987 : 0 : tm->tm_isdst = -1;
7765 tgl@sss.pgh.pa.us 1988 : 0 : tm->tm_gmtoff = 0;
1989 : 0 : tm->tm_zone = NULL;
9334 lockhart@fourpalms.o 1990 [ # # ]: 0 : if (tzn != NULL)
1991 : 0 : *tzn = NULL;
1992 : : }
1993 : :
9334 lockhart@fourpalms.o 1994 :CBC 113579 : return 0;
1995 : : }
1996 : :
1997 : :
1998 : : /* tm2timestamp()
1999 : : * Convert a tm structure to a timestamp data type.
2000 : : * Note that year is _not_ 1900-based, but is an explicit full value.
2001 : : * Also, month is one-based, _not_ zero-based.
2002 : : *
2003 : : * Returns -1 on failure (value out of range).
2004 : : */
2005 : : int
2999 tgl@sss.pgh.pa.us 2006 : 110531 : tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
2007 : : {
2008 : : TimeOffset date;
2009 : : TimeOffset time;
2010 : :
2011 : : /* Prevent overflow in Julian-day routines */
9334 lockhart@fourpalms.o 2012 [ + + + + : 110531 : if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
+ - - + -
- - - ]
2013 : : {
6874 tgl@sss.pgh.pa.us 2014 : 6 : *result = 0; /* keep compiler quiet */
9334 lockhart@fourpalms.o 2015 : 6 : return -1;
2016 : : }
2017 : :
8191 tgl@sss.pgh.pa.us 2018 : 110525 : date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
8539 lockhart@fourpalms.o 2019 : 110525 : time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
2020 : :
387 nathan@postgresql.or 2021 [ + + - + : 110525 : if (unlikely(pg_mul_s64_overflow(date, USECS_PER_DAY, result) ||
+ + ]
2022 : : pg_add_s64_overflow(*result, time, result)))
2023 : : {
6874 tgl@sss.pgh.pa.us 2024 : 3 : *result = 0; /* keep compiler quiet */
8100 2025 : 3 : return -1;
2026 : : }
9334 lockhart@fourpalms.o 2027 [ + + ]: 110522 : if (tzp != NULL)
2028 : 52511 : *result = dt2local(*result, -(*tzp));
2029 : :
2030 : : /* final range check catches just-out-of-range timestamps */
3461 tgl@sss.pgh.pa.us 2031 [ + + + + ]: 110522 : if (!IS_VALID_TIMESTAMP(*result))
2032 : : {
2033 : 14 : *result = 0; /* keep compiler quiet */
2034 : 14 : return -1;
2035 : : }
2036 : :
9334 lockhart@fourpalms.o 2037 : 110508 : return 0;
2038 : : }
2039 : :
2040 : :
2041 : : /* interval2itm()
2042 : : * Convert an Interval to a pg_itm structure.
2043 : : * Note: overflow is not possible, because the pg_itm fields are
2044 : : * wide enough for all possible conversion results.
2045 : : */
2046 : : void
1253 tgl@sss.pgh.pa.us 2047 : 8395 : interval2itm(Interval span, struct pg_itm *itm)
2048 : : {
2049 : : TimeOffset time;
2050 : : TimeOffset tfrac;
2051 : :
2052 : 8395 : itm->tm_year = span.month / MONTHS_PER_YEAR;
2053 : 8395 : itm->tm_mon = span.month % MONTHS_PER_YEAR;
2054 : 8395 : itm->tm_mday = span.day;
9334 lockhart@fourpalms.o 2055 : 8395 : time = span.time;
2056 : :
7256 tgl@sss.pgh.pa.us 2057 : 8395 : tfrac = time / USECS_PER_HOUR;
2058 : 8395 : time -= tfrac * USECS_PER_HOUR;
1253 2059 : 8395 : itm->tm_hour = tfrac;
7256 2060 : 8395 : tfrac = time / USECS_PER_MINUTE;
2061 : 8395 : time -= tfrac * USECS_PER_MINUTE;
1253 2062 : 8395 : itm->tm_min = (int) tfrac;
7256 2063 : 8395 : tfrac = time / USECS_PER_SEC;
1253 2064 : 8395 : time -= tfrac * USECS_PER_SEC;
2065 : 8395 : itm->tm_sec = (int) tfrac;
2066 : 8395 : itm->tm_usec = (int) time;
2067 : 8395 : }
2068 : :
2069 : : /* itm2interval()
2070 : : * Convert a pg_itm structure to an Interval.
2071 : : * Returns 0 if OK, -1 on overflow.
2072 : : *
2073 : : * This is for use in computations expected to produce finite results. Any
2074 : : * inputs that lead to infinite results are treated as overflows.
2075 : : */
2076 : : int
1253 tgl@sss.pgh.pa.us 2077 :UBC 0 : itm2interval(struct pg_itm *itm, Interval *span)
2078 : : {
2079 : 0 : int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2080 : :
2081 [ # # # # ]: 0 : if (total_months > INT_MAX || total_months < INT_MIN)
2082 : 0 : return -1;
2083 : 0 : span->month = (int32) total_months;
2084 : 0 : span->day = itm->tm_mday;
2085 [ # # ]: 0 : if (pg_mul_s64_overflow(itm->tm_hour, USECS_PER_HOUR,
2086 : 0 : &span->time))
2087 : 0 : return -1;
2088 : : /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2089 [ # # ]: 0 : if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2090 : 0 : &span->time))
2091 : 0 : return -1;
2092 [ # # ]: 0 : if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2093 : 0 : &span->time))
2094 : 0 : return -1;
2095 [ # # ]: 0 : if (pg_add_s64_overflow(span->time, itm->tm_usec,
2096 : 0 : &span->time))
2097 : 0 : return -1;
662 dean.a.rasheed@gmail 2098 [ # # # # : 0 : if (INTERVAL_NOT_FINITE(span))
# # # # #
# # # ]
2099 : 0 : return -1;
9334 lockhart@fourpalms.o 2100 : 0 : return 0;
2101 : : }
2102 : :
2103 : : /* itmin2interval()
2104 : : * Convert a pg_itm_in structure to an Interval.
2105 : : * Returns 0 if OK, -1 on overflow.
2106 : : *
2107 : : * Note: if the result is infinite, it is not treated as an overflow. This
2108 : : * avoids any dump/reload hazards from pre-17 databases that do not support
2109 : : * infinite intervals, but do allow finite intervals with all fields set to
2110 : : * INT_MIN/INT_MAX (outside the documented range). Such intervals will be
2111 : : * silently converted to +/-infinity. This may not be ideal, but seems
2112 : : * preferable to failure, and ought to be pretty unlikely in practice.
2113 : : */
2114 : : int
1253 tgl@sss.pgh.pa.us 2115 :CBC 39114 : itmin2interval(struct pg_itm_in *itm_in, Interval *span)
2116 : : {
2117 : 39114 : int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2118 : :
4237 bruce@momjian.us 2119 [ + + + + ]: 39114 : if (total_months > INT_MAX || total_months < INT_MIN)
2120 : 9 : return -1;
1253 tgl@sss.pgh.pa.us 2121 : 39105 : span->month = (int32) total_months;
2122 : 39105 : span->day = itm_in->tm_mday;
2123 : 39105 : span->time = itm_in->tm_usec;
9334 lockhart@fourpalms.o 2124 : 39105 : return 0;
2125 : : }
2126 : :
2127 : : static TimeOffset
8539 2128 : 110525 : time2t(const int hour, const int min, const int sec, const fsec_t fsec)
2129 : : {
7352 bruce@momjian.us 2130 : 110525 : return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2131 : : }
2132 : :
2133 : : static Timestamp
1082 pg@bowt.ie 2134 : 60843 : dt2local(Timestamp dt, int timezone)
2135 : : {
2136 : 60843 : dt -= (timezone * USECS_PER_SEC);
9334 lockhart@fourpalms.o 2137 : 60843 : return dt;
2138 : : }
2139 : :
2140 : :
2141 : : /*****************************************************************************
2142 : : * PUBLIC ROUTINES *
2143 : : *****************************************************************************/
2144 : :
2145 : :
2146 : : Datum
9220 tgl@sss.pgh.pa.us 2147 :UBC 0 : timestamp_finite(PG_FUNCTION_ARGS)
2148 : : {
7266 bruce@momjian.us 2149 : 0 : Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
2150 : :
8934 2151 [ # # # # ]: 0 : PG_RETURN_BOOL(!TIMESTAMP_NOT_FINITE(timestamp));
2152 : : }
2153 : :
2154 : : Datum
9220 tgl@sss.pgh.pa.us 2155 :CBC 159 : interval_finite(PG_FUNCTION_ARGS)
2156 : : {
662 dean.a.rasheed@gmail 2157 : 159 : Interval *interval = PG_GETARG_INTERVAL_P(0);
2158 : :
2159 [ + + + - : 159 : PG_RETURN_BOOL(!INTERVAL_NOT_FINITE(interval));
- + + + +
- - + ]
2160 : : }
2161 : :
2162 : :
2163 : : /*----------------------------------------------------------
2164 : : * Relational operators for timestamp.
2165 : : *---------------------------------------------------------*/
2166 : :
2167 : : void
2999 tgl@sss.pgh.pa.us 2168 : 14190 : GetEpochTime(struct pg_tm *tm)
2169 : : {
2170 : : struct pg_tm *t0;
7678 bruce@momjian.us 2171 : 14190 : pg_time_t epoch = 0;
2172 : :
7778 tgl@sss.pgh.pa.us 2173 : 14190 : t0 = pg_gmtime(&epoch);
2174 : :
2517 2175 [ - + ]: 14190 : if (t0 == NULL)
2517 tgl@sss.pgh.pa.us 2176 [ # # ]:UBC 0 : elog(ERROR, "could not convert epoch to timestamp: %m");
2177 : :
9334 lockhart@fourpalms.o 2178 :CBC 14190 : tm->tm_year = t0->tm_year;
2179 : 14190 : tm->tm_mon = t0->tm_mon;
2180 : 14190 : tm->tm_mday = t0->tm_mday;
2181 : 14190 : tm->tm_hour = t0->tm_hour;
2182 : 14190 : tm->tm_min = t0->tm_min;
2183 : 14190 : tm->tm_sec = t0->tm_sec;
2184 : :
7765 tgl@sss.pgh.pa.us 2185 : 14190 : tm->tm_year += 1900;
9334 lockhart@fourpalms.o 2186 : 14190 : tm->tm_mon++;
7765 tgl@sss.pgh.pa.us 2187 : 14190 : }
2188 : :
2189 : : Timestamp
8744 lockhart@fourpalms.o 2190 : 14187 : SetEpochTimestamp(void)
2191 : : {
2192 : : Timestamp dt;
2193 : : struct pg_tm tt,
2194 : 14187 : *tm = &tt;
2195 : :
2196 : 14187 : GetEpochTime(tm);
2197 : : /* we don't bother to test for failure ... */
2198 : 14187 : tm2timestamp(tm, 0, NULL, &dt);
2199 : :
9334 2200 : 14187 : return dt;
2201 : : } /* SetEpochTimestamp() */
2202 : :
2203 : : /*
2204 : : * We are currently sharing some code between timestamp and timestamptz.
2205 : : * The comparison functions are among them. - thomas 2001-09-25
2206 : : *
2207 : : * timestamp_relop - is timestamp1 relop timestamp2
2208 : : */
2209 : : int
8892 tgl@sss.pgh.pa.us 2210 : 375137 : timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
2211 : : {
7410 bruce@momjian.us 2212 [ + + ]: 375137 : return (dt1 < dt2) ? -1 : ((dt1 > dt2) ? 1 : 0);
2213 : : }
2214 : :
2215 : : Datum
9220 tgl@sss.pgh.pa.us 2216 : 53989 : timestamp_eq(PG_FUNCTION_ARGS)
2217 : : {
2218 : 53989 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2219 : 53989 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2220 : :
8892 2221 : 53989 : PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
2222 : : }
2223 : :
2224 : : Datum
9220 2225 : 393 : timestamp_ne(PG_FUNCTION_ARGS)
2226 : : {
2227 : 393 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2228 : 393 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2229 : :
8892 2230 : 393 : PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) != 0);
2231 : : }
2232 : :
2233 : : Datum
9220 2234 : 190768 : timestamp_lt(PG_FUNCTION_ARGS)
2235 : : {
2236 : 190768 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2237 : 190768 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2238 : :
8892 2239 : 190768 : PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) < 0);
2240 : : }
2241 : :
2242 : : Datum
9220 2243 : 49711 : timestamp_gt(PG_FUNCTION_ARGS)
2244 : : {
2245 : 49711 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2246 : 49711 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2247 : :
8892 2248 : 49711 : PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
2249 : : }
2250 : :
2251 : : Datum
9220 2252 : 9458 : timestamp_le(PG_FUNCTION_ARGS)
2253 : : {
2254 : 9458 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2255 : 9458 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2256 : :
8892 2257 : 9458 : PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) <= 0);
2258 : : }
2259 : :
2260 : : Datum
9220 2261 : 9772 : timestamp_ge(PG_FUNCTION_ARGS)
2262 : : {
2263 : 9772 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2264 : 9772 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2265 : :
8892 2266 : 9772 : PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
2267 : : }
2268 : :
2269 : : Datum
9220 2270 : 20923 : timestamp_cmp(PG_FUNCTION_ARGS)
2271 : : {
2272 : 20923 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2273 : 20923 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2274 : :
8892 2275 : 20923 : PG_RETURN_INT32(timestamp_cmp_internal(dt1, dt2));
2276 : : }
2277 : :
2278 : : Datum
5022 2279 : 298 : timestamp_sortsupport(PG_FUNCTION_ARGS)
2280 : : {
4836 bruce@momjian.us 2281 : 298 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
2282 : :
1253 john.naylor@postgres 2283 : 298 : ssup->comparator = ssup_datum_signed_cmp;
5022 tgl@sss.pgh.pa.us 2284 : 298 : PG_RETURN_VOID();
2285 : : }
2286 : :
2287 : : /* note: this is used for timestamptz also */
2288 : : static Datum
155 pg@bowt.ie 2289 :UBC 0 : timestamp_decrement(Relation rel, Datum existing, bool *underflow)
2290 : : {
2291 : 0 : Timestamp texisting = DatumGetTimestamp(existing);
2292 : :
2293 [ # # ]: 0 : if (texisting == PG_INT64_MIN)
2294 : : {
2295 : : /* return value is undefined */
2296 : 0 : *underflow = true;
2297 : 0 : return (Datum) 0;
2298 : : }
2299 : :
2300 : 0 : *underflow = false;
2301 : 0 : return TimestampGetDatum(texisting - 1);
2302 : : }
2303 : :
2304 : : /* note: this is used for timestamptz also */
2305 : : static Datum
2306 : 0 : timestamp_increment(Relation rel, Datum existing, bool *overflow)
2307 : : {
2308 : 0 : Timestamp texisting = DatumGetTimestamp(existing);
2309 : :
2310 [ # # ]: 0 : if (texisting == PG_INT64_MAX)
2311 : : {
2312 : : /* return value is undefined */
2313 : 0 : *overflow = true;
2314 : 0 : return (Datum) 0;
2315 : : }
2316 : :
2317 : 0 : *overflow = false;
2318 : 0 : return TimestampGetDatum(texisting + 1);
2319 : : }
2320 : :
2321 : : Datum
2322 : 0 : timestamp_skipsupport(PG_FUNCTION_ARGS)
2323 : : {
2324 : 0 : SkipSupport sksup = (SkipSupport) PG_GETARG_POINTER(0);
2325 : :
2326 : 0 : sksup->decrement = timestamp_decrement;
2327 : 0 : sksup->increment = timestamp_increment;
2328 : 0 : sksup->low_elem = TimestampGetDatum(PG_INT64_MIN);
2329 : 0 : sksup->high_elem = TimestampGetDatum(PG_INT64_MAX);
2330 : :
2331 : 0 : PG_RETURN_VOID();
2332 : : }
2333 : :
2334 : : Datum
6637 tgl@sss.pgh.pa.us 2335 :CBC 3255 : timestamp_hash(PG_FUNCTION_ARGS)
2336 : : {
2337 : 3255 : return hashint8(fcinfo);
2338 : : }
2339 : :
2340 : : Datum
2928 rhaas@postgresql.org 2341 : 30 : timestamp_hash_extended(PG_FUNCTION_ARGS)
2342 : : {
2343 : 30 : return hashint8extended(fcinfo);
2344 : : }
2345 : :
2346 : : Datum
359 peter@eisentraut.org 2347 :UBC 0 : timestamptz_hash(PG_FUNCTION_ARGS)
2348 : : {
2349 : 0 : return hashint8(fcinfo);
2350 : : }
2351 : :
2352 : : Datum
2353 : 0 : timestamptz_hash_extended(PG_FUNCTION_ARGS)
2354 : : {
2355 : 0 : return hashint8extended(fcinfo);
2356 : : }
2357 : :
2358 : : /*
2359 : : * Cross-type comparison functions for timestamp vs timestamptz
2360 : : */
2361 : :
2362 : : int32
1795 tgl@sss.pgh.pa.us 2363 :CBC 8037 : timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
2364 : : {
2365 : : TimestampTz dt1;
2366 : : int overflow;
2367 : :
2368 : 8037 : dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2369 [ - + ]: 8037 : if (overflow > 0)
2370 : : {
2371 : : /* dt1 is larger than any finite timestamp, but less than infinity */
1795 tgl@sss.pgh.pa.us 2372 [ # # ]:UBC 0 : return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2373 : : }
1795 tgl@sss.pgh.pa.us 2374 [ + + ]:CBC 8037 : if (overflow < 0)
2375 : : {
2376 : : /* dt1 is less than any finite timestamp, but more than -infinity */
2377 [ - + ]: 6 : return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2378 : : }
2379 : :
2380 : 8031 : return timestamptz_cmp_internal(dt1, dt2);
2381 : : }
2382 : :
2383 : : Datum
7838 2384 : 906 : timestamp_eq_timestamptz(PG_FUNCTION_ARGS)
2385 : : {
2386 : 906 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
7678 bruce@momjian.us 2387 : 906 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
2388 : :
1795 tgl@sss.pgh.pa.us 2389 : 906 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) == 0);
2390 : : }
2391 : :
2392 : : Datum
7838 tgl@sss.pgh.pa.us 2393 :UBC 0 : timestamp_ne_timestamptz(PG_FUNCTION_ARGS)
2394 : : {
2395 : 0 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
7678 bruce@momjian.us 2396 : 0 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
2397 : :
1795 tgl@sss.pgh.pa.us 2398 : 0 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) != 0);
2399 : : }
2400 : :
2401 : : Datum
7838 tgl@sss.pgh.pa.us 2402 :CBC 1602 : timestamp_lt_timestamptz(PG_FUNCTION_ARGS)
2403 : : {
2404 : 1602 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
7678 bruce@momjian.us 2405 : 1602 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
2406 : :
1795 tgl@sss.pgh.pa.us 2407 : 1602 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) < 0);
2408 : : }
2409 : :
2410 : : Datum
7838 2411 : 1599 : timestamp_gt_timestamptz(PG_FUNCTION_ARGS)
2412 : : {
2413 : 1599 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
7678 bruce@momjian.us 2414 : 1599 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
2415 : :
1795 tgl@sss.pgh.pa.us 2416 : 1599 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) > 0);
2417 : : }
2418 : :
2419 : : Datum
7838 2420 : 1899 : timestamp_le_timestamptz(PG_FUNCTION_ARGS)
2421 : : {
2422 : 1899 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
7678 bruce@momjian.us 2423 : 1899 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
2424 : :
1795 tgl@sss.pgh.pa.us 2425 : 1899 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) <= 0);
2426 : : }
2427 : :
2428 : : Datum
7838 2429 : 1752 : timestamp_ge_timestamptz(PG_FUNCTION_ARGS)
2430 : : {
2431 : 1752 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
7678 bruce@momjian.us 2432 : 1752 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
2433 : :
1795 tgl@sss.pgh.pa.us 2434 : 1752 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) >= 0);
2435 : : }
2436 : :
2437 : : Datum
7838 2438 : 53 : timestamp_cmp_timestamptz(PG_FUNCTION_ARGS)
2439 : : {
2440 : 53 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
7678 bruce@momjian.us 2441 : 53 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
2442 : :
1795 tgl@sss.pgh.pa.us 2443 : 53 : PG_RETURN_INT32(timestamp_cmp_timestamptz_internal(timestampVal, dt2));
2444 : : }
2445 : :
2446 : : Datum
7838 tgl@sss.pgh.pa.us 2447 :UBC 0 : timestamptz_eq_timestamp(PG_FUNCTION_ARGS)
2448 : : {
7678 bruce@momjian.us 2449 : 0 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
7838 tgl@sss.pgh.pa.us 2450 : 0 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2451 : :
1795 2452 : 0 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt1) == 0);
2453 : : }
2454 : :
2455 : : Datum
7838 tgl@sss.pgh.pa.us 2456 :CBC 48 : timestamptz_ne_timestamp(PG_FUNCTION_ARGS)
2457 : : {
7678 bruce@momjian.us 2458 : 48 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
7838 tgl@sss.pgh.pa.us 2459 : 48 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2460 : :
1795 2461 : 48 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt1) != 0);
2462 : : }
2463 : :
2464 : : Datum
7838 tgl@sss.pgh.pa.us 2465 :UBC 0 : timestamptz_lt_timestamp(PG_FUNCTION_ARGS)
2466 : : {
7678 bruce@momjian.us 2467 : 0 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
7838 tgl@sss.pgh.pa.us 2468 : 0 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2469 : :
1795 2470 : 0 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt1) > 0);
2471 : : }
2472 : :
2473 : : Datum
7838 2474 : 0 : timestamptz_gt_timestamp(PG_FUNCTION_ARGS)
2475 : : {
7678 bruce@momjian.us 2476 : 0 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
7838 tgl@sss.pgh.pa.us 2477 : 0 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2478 : :
1795 2479 : 0 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt1) < 0);
2480 : : }
2481 : :
2482 : : Datum
7838 2483 : 0 : timestamptz_le_timestamp(PG_FUNCTION_ARGS)
2484 : : {
7678 bruce@momjian.us 2485 : 0 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
7838 tgl@sss.pgh.pa.us 2486 : 0 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2487 : :
1795 2488 : 0 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt1) >= 0);
2489 : : }
2490 : :
2491 : : Datum
7838 tgl@sss.pgh.pa.us 2492 :CBC 3 : timestamptz_ge_timestamp(PG_FUNCTION_ARGS)
2493 : : {
7678 bruce@momjian.us 2494 : 3 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
7838 tgl@sss.pgh.pa.us 2495 : 3 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2496 : :
1795 2497 : 3 : PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt1) <= 0);
2498 : : }
2499 : :
2500 : : Datum
7838 tgl@sss.pgh.pa.us 2501 :GBC 67 : timestamptz_cmp_timestamp(PG_FUNCTION_ARGS)
2502 : : {
7678 bruce@momjian.us 2503 : 67 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
7838 tgl@sss.pgh.pa.us 2504 : 67 : Timestamp timestampVal = PG_GETARG_TIMESTAMP(1);
2505 : :
1795 2506 : 67 : PG_RETURN_INT32(-timestamp_cmp_timestamptz_internal(timestampVal, dt1));
2507 : : }
2508 : :
2509 : :
2510 : : /*
2511 : : * interval_relop - is interval1 relop interval2
2512 : : *
2513 : : * Interval comparison is based on converting interval values to a linear
2514 : : * representation expressed in the units of the time field (microseconds,
2515 : : * in the case of integer timestamps) with days assumed to be always 24 hours
2516 : : * and months assumed to be always 30 days. To avoid overflow, we need a
2517 : : * wider-than-int64 datatype for the linear representation, so use INT128.
2518 : : */
2519 : :
2520 : : static inline INT128
5999 tgl@sss.pgh.pa.us 2521 :CBC 297658 : interval_cmp_value(const Interval *interval)
2522 : : {
2523 : : INT128 span;
2524 : : int64 days;
2525 : :
2526 : : /*
2527 : : * Combine the month and day fields into an integral number of days.
2528 : : * Because the inputs are int32, int64 arithmetic suffices here.
2529 : : */
1441 2530 : 297658 : days = interval->month * INT64CONST(30);
3076 2531 : 297658 : days += interval->day;
2532 : :
2533 : : /* Widen time field to 128 bits */
1441 2534 : 297658 : span = int64_to_int128(interval->time);
2535 : :
2536 : : /* Scale up days to microseconds, forming a 128-bit product */
3076 2537 : 297658 : int128_add_int64_mul_int64(&span, days, USECS_PER_DAY);
2538 : :
5999 2539 : 297658 : return span;
2540 : : }
2541 : :
2542 : : static int
1148 peter@eisentraut.org 2543 : 147021 : interval_cmp_internal(const Interval *interval1, const Interval *interval2)
2544 : : {
3076 tgl@sss.pgh.pa.us 2545 : 147021 : INT128 span1 = interval_cmp_value(interval1);
2546 : 147021 : INT128 span2 = interval_cmp_value(interval2);
2547 : :
2548 : 147021 : return int128_compare(span1, span2);
2549 : : }
2550 : :
2551 : : static int
662 dean.a.rasheed@gmail 2552 : 2443 : interval_sign(const Interval *interval)
2553 : : {
2554 : 2443 : INT128 span = interval_cmp_value(interval);
2555 : 2443 : INT128 zero = int64_to_int128(0);
2556 : :
2557 : 2443 : return int128_compare(span, zero);
2558 : : }
2559 : :
2560 : : Datum
9220 tgl@sss.pgh.pa.us 2561 : 28513 : interval_eq(PG_FUNCTION_ARGS)
2562 : : {
2563 : 28513 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2564 : 28513 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2565 : :
8892 2566 : 28513 : PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2567 : : }
2568 : :
2569 : : Datum
9220 2570 : 54 : interval_ne(PG_FUNCTION_ARGS)
2571 : : {
2572 : 54 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2573 : 54 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2574 : :
8892 2575 : 54 : PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2576 : : }
2577 : :
2578 : : Datum
9220 2579 : 70926 : interval_lt(PG_FUNCTION_ARGS)
2580 : : {
2581 : 70926 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2582 : 70926 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2583 : :
8892 2584 : 70926 : PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2585 : : }
2586 : :
2587 : : Datum
9220 2588 : 5663 : interval_gt(PG_FUNCTION_ARGS)
2589 : : {
2590 : 5663 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2591 : 5663 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2592 : :
8892 2593 : 5663 : PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2594 : : }
2595 : :
2596 : : Datum
9220 2597 : 3207 : interval_le(PG_FUNCTION_ARGS)
2598 : : {
2599 : 3207 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2600 : 3207 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2601 : :
8892 2602 : 3207 : PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2603 : : }
2604 : :
2605 : : Datum
9220 2606 : 2964 : interval_ge(PG_FUNCTION_ARGS)
2607 : : {
2608 : 2964 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2609 : 2964 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2610 : :
8892 2611 : 2964 : PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2612 : : }
2613 : :
2614 : : Datum
9220 2615 : 35250 : interval_cmp(PG_FUNCTION_ARGS)
2616 : : {
2617 : 35250 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2618 : 35250 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2619 : :
8892 2620 : 35250 : PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2621 : : }
2622 : :
2623 : : /*
2624 : : * Hashing for intervals
2625 : : *
2626 : : * We must produce equal hashvals for values that interval_cmp_internal()
2627 : : * considers equal. So, compute the net span the same way it does,
2628 : : * and then hash that.
2629 : : */
2630 : : Datum
9210 2631 : 1143 : interval_hash(PG_FUNCTION_ARGS)
2632 : : {
5999 2633 : 1143 : Interval *interval = PG_GETARG_INTERVAL_P(0);
3076 2634 : 1143 : INT128 span = interval_cmp_value(interval);
2635 : : int64 span64;
2636 : :
2637 : : /*
2638 : : * Use only the least significant 64 bits for hashing. The upper 64 bits
2639 : : * seldom add any useful information, and besides we must do it like this
2640 : : * for compatibility with hashes calculated before use of INT128 was
2641 : : * introduced.
2642 : : */
2643 : 1143 : span64 = int128_to_int64(span);
2644 : :
2645 : 1143 : return DirectFunctionCall1(hashint8, Int64GetDatumFast(span64));
2646 : : }
2647 : :
2648 : : Datum
2928 rhaas@postgresql.org 2649 : 30 : interval_hash_extended(PG_FUNCTION_ARGS)
2650 : : {
2651 : 30 : Interval *interval = PG_GETARG_INTERVAL_P(0);
2652 : 30 : INT128 span = interval_cmp_value(interval);
2653 : : int64 span64;
2654 : :
2655 : : /* Same approach as interval_hash */
2656 : 30 : span64 = int128_to_int64(span);
2657 : :
2658 : 30 : return DirectFunctionCall2(hashint8extended, Int64GetDatumFast(span64),
2659 : : PG_GETARG_DATUM(1));
2660 : : }
2661 : :
2662 : : /* overlaps_timestamp() --- implements the SQL OVERLAPS operator.
2663 : : *
2664 : : * Algorithm is per SQL spec. This is much harder than you'd think
2665 : : * because the spec requires us to deliver a non-null answer in some cases
2666 : : * where some of the inputs are null.
2667 : : */
2668 : : Datum
9220 tgl@sss.pgh.pa.us 2669 : 36 : overlaps_timestamp(PG_FUNCTION_ARGS)
2670 : : {
2671 : : /*
2672 : : * The arguments are Timestamps, but we leave them as generic Datums to
2673 : : * avoid unnecessary conversions between value and reference forms --- not
2674 : : * to mention possible dereferences of null pointers.
2675 : : */
2676 : 36 : Datum ts1 = PG_GETARG_DATUM(0);
2677 : 36 : Datum te1 = PG_GETARG_DATUM(1);
2678 : 36 : Datum ts2 = PG_GETARG_DATUM(2);
2679 : 36 : Datum te2 = PG_GETARG_DATUM(3);
9039 2680 : 36 : bool ts1IsNull = PG_ARGISNULL(0);
2681 : 36 : bool te1IsNull = PG_ARGISNULL(1);
2682 : 36 : bool ts2IsNull = PG_ARGISNULL(2);
2683 : 36 : bool te2IsNull = PG_ARGISNULL(3);
2684 : :
2685 : : #define TIMESTAMP_GT(t1,t2) \
2686 : : DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2687 : : #define TIMESTAMP_LT(t1,t2) \
2688 : : DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2689 : :
2690 : : /*
2691 : : * If both endpoints of interval 1 are null, the result is null (unknown).
2692 : : * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2693 : : * take ts1 as the lesser endpoint.
2694 : : */
2695 [ - + ]: 36 : if (ts1IsNull)
2696 : : {
9039 tgl@sss.pgh.pa.us 2697 [ # # ]:UBC 0 : if (te1IsNull)
2698 : 0 : PG_RETURN_NULL();
2699 : : /* swap null for non-null */
9307 lockhart@fourpalms.o 2700 : 0 : ts1 = te1;
9039 tgl@sss.pgh.pa.us 2701 : 0 : te1IsNull = true;
2702 : : }
9039 tgl@sss.pgh.pa.us 2703 [ + - ]:CBC 36 : else if (!te1IsNull)
2704 : : {
2705 [ - + ]: 36 : if (TIMESTAMP_GT(ts1, te1))
2706 : : {
8934 bruce@momjian.us 2707 :UBC 0 : Datum tt = ts1;
2708 : :
9039 tgl@sss.pgh.pa.us 2709 : 0 : ts1 = te1;
2710 : 0 : te1 = tt;
2711 : : }
2712 : : }
2713 : :
2714 : : /* Likewise for interval 2. */
9039 tgl@sss.pgh.pa.us 2715 [ - + ]:CBC 36 : if (ts2IsNull)
2716 : : {
9039 tgl@sss.pgh.pa.us 2717 [ # # ]:UBC 0 : if (te2IsNull)
2718 : 0 : PG_RETURN_NULL();
2719 : : /* swap null for non-null */
9307 lockhart@fourpalms.o 2720 : 0 : ts2 = te2;
9039 tgl@sss.pgh.pa.us 2721 : 0 : te2IsNull = true;
2722 : : }
9039 tgl@sss.pgh.pa.us 2723 [ + - ]:CBC 36 : else if (!te2IsNull)
2724 : : {
2725 [ - + ]: 36 : if (TIMESTAMP_GT(ts2, te2))
2726 : : {
8934 bruce@momjian.us 2727 :UBC 0 : Datum tt = ts2;
2728 : :
9039 tgl@sss.pgh.pa.us 2729 : 0 : ts2 = te2;
2730 : 0 : te2 = tt;
2731 : : }
2732 : : }
2733 : :
2734 : : /*
2735 : : * At this point neither ts1 nor ts2 is null, so we can consider three
2736 : : * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2737 : : */
9039 tgl@sss.pgh.pa.us 2738 [ - + ]:CBC 36 : if (TIMESTAMP_GT(ts1, ts2))
2739 : : {
2740 : : /*
2741 : : * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2742 : : * in the presence of nulls it's not quite completely so.
2743 : : */
9039 tgl@sss.pgh.pa.us 2744 [ # # ]:UBC 0 : if (te2IsNull)
2745 : 0 : PG_RETURN_NULL();
2746 [ # # ]: 0 : if (TIMESTAMP_LT(ts1, te2))
2747 : 0 : PG_RETURN_BOOL(true);
2748 [ # # ]: 0 : if (te1IsNull)
2749 : 0 : PG_RETURN_NULL();
2750 : :
2751 : : /*
2752 : : * If te1 is not null then we had ts1 <= te1 above, and we just found
2753 : : * ts1 >= te2, hence te1 >= te2.
2754 : : */
2755 : 0 : PG_RETURN_BOOL(false);
2756 : : }
9039 tgl@sss.pgh.pa.us 2757 [ + + ]:CBC 36 : else if (TIMESTAMP_LT(ts1, ts2))
2758 : : {
2759 : : /* This case is ts2 < te1 OR te2 < te1 */
2760 [ - + ]: 30 : if (te1IsNull)
9039 tgl@sss.pgh.pa.us 2761 :UBC 0 : PG_RETURN_NULL();
9039 tgl@sss.pgh.pa.us 2762 [ + + ]:CBC 30 : if (TIMESTAMP_LT(ts2, te1))
2763 : 12 : PG_RETURN_BOOL(true);
2764 [ - + ]: 18 : if (te2IsNull)
9039 tgl@sss.pgh.pa.us 2765 :UBC 0 : PG_RETURN_NULL();
2766 : :
2767 : : /*
2768 : : * If te2 is not null then we had ts2 <= te2 above, and we just found
2769 : : * ts2 >= te1, hence te2 >= te1.
2770 : : */
9039 tgl@sss.pgh.pa.us 2771 :CBC 18 : PG_RETURN_BOOL(false);
2772 : : }
2773 : : else
2774 : : {
2775 : : /*
2776 : : * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2777 : : * rather silly way of saying "true if both are non-null, else null".
2778 : : */
2779 [ + - - + ]: 6 : if (te1IsNull || te2IsNull)
9039 tgl@sss.pgh.pa.us 2780 :UBC 0 : PG_RETURN_NULL();
9039 tgl@sss.pgh.pa.us 2781 :CBC 6 : PG_RETURN_BOOL(true);
2782 : : }
2783 : :
2784 : : #undef TIMESTAMP_GT
2785 : : #undef TIMESTAMP_LT
2786 : : }
2787 : :
2788 : :
2789 : : /*----------------------------------------------------------
2790 : : * "Arithmetic" operators on date/times.
2791 : : *---------------------------------------------------------*/
2792 : :
2793 : : Datum
9220 tgl@sss.pgh.pa.us 2794 :UBC 0 : timestamp_smaller(PG_FUNCTION_ARGS)
2795 : : {
2796 : 0 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2797 : 0 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2798 : : Timestamp result;
2799 : :
2800 : : /* use timestamp_cmp_internal to be sure this agrees with comparisons */
8065 2801 [ # # ]: 0 : if (timestamp_cmp_internal(dt1, dt2) < 0)
2802 : 0 : result = dt1;
2803 : : else
2804 : 0 : result = dt2;
9220 2805 : 0 : PG_RETURN_TIMESTAMP(result);
2806 : : }
2807 : :
2808 : : Datum
9220 tgl@sss.pgh.pa.us 2809 :CBC 42 : timestamp_larger(PG_FUNCTION_ARGS)
2810 : : {
2811 : 42 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2812 : 42 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2813 : : Timestamp result;
2814 : :
8065 2815 [ - + ]: 42 : if (timestamp_cmp_internal(dt1, dt2) > 0)
8065 tgl@sss.pgh.pa.us 2816 :UBC 0 : result = dt1;
2817 : : else
8065 tgl@sss.pgh.pa.us 2818 :CBC 42 : result = dt2;
9220 2819 : 42 : PG_RETURN_TIMESTAMP(result);
2820 : : }
2821 : :
2822 : :
2823 : : Datum
2824 : 3223 : timestamp_mi(PG_FUNCTION_ARGS)
2825 : : {
2826 : 3223 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2827 : 3223 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2828 : : Interval *result;
2829 : :
2830 : 3223 : result = (Interval *) palloc(sizeof(Interval));
2831 : :
2832 : : /*
2833 : : * Handle infinities.
2834 : : *
2835 : : * We treat anything that amounts to "infinity - infinity" as an error,
2836 : : * since the interval type has nothing equivalent to NaN.
2837 : : */
8744 lockhart@fourpalms.o 2838 [ + + + + : 3223 : if (TIMESTAMP_NOT_FINITE(dt1) || TIMESTAMP_NOT_FINITE(dt2))
+ - - + ]
2839 : : {
662 dean.a.rasheed@gmail 2840 [ + + ]: 42 : if (TIMESTAMP_IS_NOBEGIN(dt1))
2841 : : {
2842 [ + + ]: 18 : if (TIMESTAMP_IS_NOBEGIN(dt2))
2843 [ + - ]: 6 : ereport(ERROR,
2844 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2845 : : errmsg("interval out of range")));
2846 : : else
2847 : 12 : INTERVAL_NOBEGIN(result);
2848 : : }
2849 [ + - ]: 24 : else if (TIMESTAMP_IS_NOEND(dt1))
2850 : : {
2851 [ + + ]: 24 : if (TIMESTAMP_IS_NOEND(dt2))
2852 [ + - ]: 6 : ereport(ERROR,
2853 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2854 : : errmsg("interval out of range")));
2855 : : else
2856 : 18 : INTERVAL_NOEND(result);
2857 : : }
662 dean.a.rasheed@gmail 2858 [ # # ]:UBC 0 : else if (TIMESTAMP_IS_NOBEGIN(dt2))
2859 : 0 : INTERVAL_NOEND(result);
2860 : : else /* TIMESTAMP_IS_NOEND(dt2) */
2861 : 0 : INTERVAL_NOBEGIN(result);
2862 : :
662 dean.a.rasheed@gmail 2863 :CBC 30 : PG_RETURN_INTERVAL_P(result);
2864 : : }
2865 : :
929 tgl@sss.pgh.pa.us 2866 [ + + ]: 3181 : if (unlikely(pg_sub_s64_overflow(dt1, dt2, &result->time)))
2867 [ + - ]: 6 : ereport(ERROR,
2868 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2869 : : errmsg("interval out of range")));
2870 : :
9334 lockhart@fourpalms.o 2871 : 3175 : result->month = 0;
7353 bruce@momjian.us 2872 : 3175 : result->day = 0;
2873 : :
2874 : : /*----------
2875 : : * This is wrong, but removing it breaks a lot of regression tests.
2876 : : * For example:
2877 : : *
2878 : : * test=> SET timezone = 'EST5EDT';
2879 : : * test=> SELECT
2880 : : * test-> ('2005-10-30 13:22:00-05'::timestamptz -
2881 : : * test(> '2005-10-29 13:22:00-04'::timestamptz);
2882 : : * ?column?
2883 : : * ----------------
2884 : : * 1 day 01:00:00
2885 : : * (1 row)
2886 : : *
2887 : : * so adding that to the first timestamp gets:
2888 : : *
2889 : : * test=> SELECT
2890 : : * test-> ('2005-10-29 13:22:00-04'::timestamptz +
2891 : : * test(> ('2005-10-30 13:22:00-05'::timestamptz -
2892 : : * test(> '2005-10-29 13:22:00-04'::timestamptz)) at time zone 'EST';
2893 : : * timezone
2894 : : * --------------------
2895 : : * 2005-10-30 14:22:00
2896 : : * (1 row)
2897 : : *----------
2898 : : */
7228 2899 : 3175 : result = DatumGetIntervalP(DirectFunctionCall1(interval_justify_hours,
2900 : : IntervalPGetDatum(result)));
2901 : :
7317 2902 : 3175 : PG_RETURN_INTERVAL_P(result);
2903 : : }
2904 : :
2905 : : /*
2906 : : * interval_justify_interval()
2907 : : *
2908 : : * Adjust interval so 'month', 'day', and 'time' portions are within
2909 : : * customary bounds. Specifically:
2910 : : *
2911 : : * 0 <= abs(time) < 24 hours
2912 : : * 0 <= abs(day) < 30 days
2913 : : *
2914 : : * Also, the sign bit on all three fields is made equal, so either
2915 : : * all three fields are negative or all are positive.
2916 : : */
2917 : : Datum
7124 2918 : 33 : interval_justify_interval(PG_FUNCTION_ARGS)
2919 : : {
2920 : 33 : Interval *span = PG_GETARG_INTERVAL_P(0);
2921 : : Interval *result;
2922 : : TimeOffset wholeday;
2923 : : int32 wholemonth;
2924 : :
2925 : 33 : result = (Interval *) palloc(sizeof(Interval));
2926 : 33 : result->month = span->month;
2927 : 33 : result->day = span->day;
2928 : 33 : result->time = span->time;
2929 : :
2930 : : /* do nothing for infinite intervals */
662 dean.a.rasheed@gmail 2931 [ + + + + : 33 : if (INTERVAL_NOT_FINITE(result))
- + + + +
+ + - ]
2932 : 6 : PG_RETURN_INTERVAL_P(result);
2933 : :
2934 : : /* pre-justify days if it might prevent overflow */
1286 tgl@sss.pgh.pa.us 2935 [ + + + + ]: 27 : if ((result->day > 0 && result->time > 0) ||
2936 [ + + + + ]: 24 : (result->day < 0 && result->time < 0))
2937 : : {
2938 : 6 : wholemonth = result->day / DAYS_PER_MONTH;
2939 : 6 : result->day -= wholemonth * DAYS_PER_MONTH;
2940 [ - + ]: 6 : if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
1286 tgl@sss.pgh.pa.us 2941 [ # # ]:UBC 0 : ereport(ERROR,
2942 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2943 : : errmsg("interval out of range")));
2944 : : }
2945 : :
2946 : : /*
2947 : : * Since TimeOffset is int64, abs(wholeday) can't exceed about 1.07e8. If
2948 : : * we pre-justified then abs(result->day) is less than DAYS_PER_MONTH, so
2949 : : * this addition can't overflow. If we didn't pre-justify, then day and
2950 : : * time are of different signs, so it still can't overflow.
2951 : : */
7124 bruce@momjian.us 2952 [ + + ]:CBC 27 : TMODULO(result->time, wholeday, USECS_PER_DAY);
1286 tgl@sss.pgh.pa.us 2953 : 27 : result->day += wholeday;
2954 : :
7124 bruce@momjian.us 2955 : 27 : wholemonth = result->day / DAYS_PER_MONTH;
2956 : 27 : result->day -= wholemonth * DAYS_PER_MONTH;
1286 tgl@sss.pgh.pa.us 2957 [ + + ]: 27 : if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2958 [ + - ]: 12 : ereport(ERROR,
2959 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2960 : : errmsg("interval out of range")));
2961 : :
7124 bruce@momjian.us 2962 [ + + ]: 15 : if (result->month > 0 &&
2963 [ + - + + : 9 : (result->day < 0 || (result->day == 0 && result->time < 0)))
+ - ]
2964 : : {
2965 : 3 : result->day += DAYS_PER_MONTH;
2966 : 3 : result->month--;
2967 : : }
2968 [ + + ]: 12 : else if (result->month < 0 &&
6912 2969 [ + - - + : 6 : (result->day > 0 || (result->day == 0 && result->time > 0)))
- - ]
2970 : : {
7124 bruce@momjian.us 2971 :UBC 0 : result->day -= DAYS_PER_MONTH;
2972 : 0 : result->month++;
2973 : : }
2974 : :
7124 bruce@momjian.us 2975 [ + + + + ]:CBC 15 : if (result->day > 0 && result->time < 0)
2976 : : {
2977 : 3 : result->time += USECS_PER_DAY;
2978 : 3 : result->day--;
2979 : : }
2980 [ + + - + ]: 12 : else if (result->day < 0 && result->time > 0)
2981 : : {
7124 bruce@momjian.us 2982 :UBC 0 : result->time -= USECS_PER_DAY;
2983 : 0 : result->day++;
2984 : : }
2985 : :
7124 bruce@momjian.us 2986 :CBC 15 : PG_RETURN_INTERVAL_P(result);
2987 : : }
2988 : :
2989 : : /*
2990 : : * interval_justify_hours()
2991 : : *
2992 : : * Adjust interval so 'time' contains less than a whole day, adding
2993 : : * the excess to 'day'. This is useful for
2994 : : * situations (such as non-TZ) where '1 day' = '24 hours' is valid,
2995 : : * e.g. interval subtraction and division.
2996 : : */
2997 : : Datum
7353 2998 : 4177 : interval_justify_hours(PG_FUNCTION_ARGS)
2999 : : {
7266 3000 : 4177 : Interval *span = PG_GETARG_INTERVAL_P(0);
3001 : : Interval *result;
3002 : : TimeOffset wholeday;
3003 : :
7353 3004 : 4177 : result = (Interval *) palloc(sizeof(Interval));
3005 : 4177 : result->month = span->month;
7256 tgl@sss.pgh.pa.us 3006 : 4177 : result->day = span->day;
7353 bruce@momjian.us 3007 : 4177 : result->time = span->time;
3008 : :
3009 : : /* do nothing for infinite intervals */
662 dean.a.rasheed@gmail 3010 [ + + + - : 4177 : if (INTERVAL_NOT_FINITE(result))
- + + + +
- + - ]
3011 : 6 : PG_RETURN_INTERVAL_P(result);
3012 : :
7256 tgl@sss.pgh.pa.us 3013 [ + + ]: 4171 : TMODULO(result->time, wholeday, USECS_PER_DAY);
1286 3014 [ + + ]: 4171 : if (pg_add_s32_overflow(result->day, wholeday, &result->day))
3015 [ + - ]: 3 : ereport(ERROR,
3016 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3017 : : errmsg("interval out of range")));
3018 : :
7124 bruce@momjian.us 3019 [ + + - + ]: 4168 : if (result->day > 0 && result->time < 0)
3020 : : {
7124 bruce@momjian.us 3021 :UBC 0 : result->time += USECS_PER_DAY;
3022 : 0 : result->day--;
3023 : : }
7124 bruce@momjian.us 3024 [ + + - + ]:CBC 4168 : else if (result->day < 0 && result->time > 0)
3025 : : {
7124 bruce@momjian.us 3026 :UBC 0 : result->time -= USECS_PER_DAY;
3027 : 0 : result->day++;
3028 : : }
3029 : :
9220 tgl@sss.pgh.pa.us 3030 :CBC 4168 : PG_RETURN_INTERVAL_P(result);
3031 : : }
3032 : :
3033 : : /*
3034 : : * interval_justify_days()
3035 : : *
3036 : : * Adjust interval so 'day' contains less than 30 days, adding
3037 : : * the excess to 'month'.
3038 : : */
3039 : : Datum
7353 bruce@momjian.us 3040 : 1002 : interval_justify_days(PG_FUNCTION_ARGS)
3041 : : {
7266 3042 : 1002 : Interval *span = PG_GETARG_INTERVAL_P(0);
3043 : : Interval *result;
3044 : : int32 wholemonth;
3045 : :
7353 3046 : 1002 : result = (Interval *) palloc(sizeof(Interval));
7256 tgl@sss.pgh.pa.us 3047 : 1002 : result->month = span->month;
7353 bruce@momjian.us 3048 : 1002 : result->day = span->day;
3049 : 1002 : result->time = span->time;
3050 : :
3051 : : /* do nothing for infinite intervals */
662 dean.a.rasheed@gmail 3052 [ + + + - : 1002 : if (INTERVAL_NOT_FINITE(result))
- + + + +
+ + - ]
3053 : 6 : PG_RETURN_INTERVAL_P(result);
3054 : :
7256 tgl@sss.pgh.pa.us 3055 : 996 : wholemonth = result->day / DAYS_PER_MONTH;
3056 : 996 : result->day -= wholemonth * DAYS_PER_MONTH;
1286 3057 [ + + ]: 996 : if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
3058 [ + - ]: 3 : ereport(ERROR,
3059 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3060 : : errmsg("interval out of range")));
3061 : :
7124 bruce@momjian.us 3062 [ + + - + ]: 993 : if (result->month > 0 && result->day < 0)
3063 : : {
7124 bruce@momjian.us 3064 :UBC 0 : result->day += DAYS_PER_MONTH;
3065 : 0 : result->month--;
3066 : : }
7124 bruce@momjian.us 3067 [ - + - - ]:CBC 993 : else if (result->month < 0 && result->day > 0)
3068 : : {
7124 bruce@momjian.us 3069 :UBC 0 : result->day -= DAYS_PER_MONTH;
3070 : 0 : result->month++;
3071 : : }
3072 : :
7353 bruce@momjian.us 3073 :CBC 993 : PG_RETURN_INTERVAL_P(result);
3074 : : }
3075 : :
3076 : : /* timestamp_pl_interval()
3077 : : * Add an interval to a timestamp data type.
3078 : : * Note that interval has provisions for qualitative year/month and day
3079 : : * units, so try to do the right thing with them.
3080 : : * To add a month, increment the month, and use the same day of month.
3081 : : * Then, if the next month has fewer days, set the day of month
3082 : : * to the last day of month.
3083 : : * To add a day, increment the mday, and use the same time of day.
3084 : : * Lastly, add in the "quantitative time".
3085 : : */
3086 : : Datum
7875 tgl@sss.pgh.pa.us 3087 : 4891 : timestamp_pl_interval(PG_FUNCTION_ARGS)
3088 : : {
7266 bruce@momjian.us 3089 : 4891 : Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
9220 tgl@sss.pgh.pa.us 3090 : 4891 : Interval *span = PG_GETARG_INTERVAL_P(1);
3091 : : Timestamp result;
3092 : :
3093 : : /*
3094 : : * Handle infinities.
3095 : : *
3096 : : * We treat anything that amounts to "infinity - infinity" as an error,
3097 : : * since the timestamp type has nothing equivalent to NaN.
3098 : : */
662 dean.a.rasheed@gmail 3099 [ + + + - : 4891 : if (INTERVAL_IS_NOBEGIN(span))
+ - ]
3100 : : {
3101 [ + + ]: 138 : if (TIMESTAMP_IS_NOEND(timestamp))
3102 [ + - ]: 12 : ereport(ERROR,
3103 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3104 : : errmsg("timestamp out of range")));
3105 : : else
3106 : 126 : TIMESTAMP_NOBEGIN(result);
3107 : : }
3108 [ + + + - : 4753 : else if (INTERVAL_IS_NOEND(span))
+ - ]
3109 : : {
3110 [ + + ]: 102 : if (TIMESTAMP_IS_NOBEGIN(timestamp))
3111 [ + - ]: 12 : ereport(ERROR,
3112 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3113 : : errmsg("timestamp out of range")));
3114 : : else
3115 : 90 : TIMESTAMP_NOEND(result);
3116 : : }
3117 [ + + + + ]: 4651 : else if (TIMESTAMP_NOT_FINITE(timestamp))
8744 lockhart@fourpalms.o 3118 : 57 : result = timestamp;
3119 : : else
3120 : : {
3121 [ + + ]: 4594 : if (span->month != 0)
3122 : : {
3123 : : struct pg_tm tt,
3124 : 1314 : *tm = &tt;
3125 : : fsec_t fsec;
3126 : :
7352 bruce@momjian.us 3127 [ - + ]: 1314 : if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
8077 tgl@sss.pgh.pa.us 3128 [ # # ]:UBC 0 : ereport(ERROR,
3129 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3130 : : errmsg("timestamp out of range")));
3131 : :
496 tgl@sss.pgh.pa.us 3132 [ - + ]:CBC 1314 : if (pg_add_s32_overflow(tm->tm_mon, span->month, &tm->tm_mon))
496 tgl@sss.pgh.pa.us 3133 [ # # ]:UBC 0 : ereport(ERROR,
3134 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3135 : : errmsg("timestamp out of range")));
7352 bruce@momjian.us 3136 [ + + ]:CBC 1314 : if (tm->tm_mon > MONTHS_PER_YEAR)
3137 : : {
3138 : 699 : tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
3139 : 699 : tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
3140 : : }
8077 tgl@sss.pgh.pa.us 3141 [ + + ]: 615 : else if (tm->tm_mon < 1)
3142 : : {
7352 bruce@momjian.us 3143 : 585 : tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
3144 : 585 : tm->tm_mon = tm->tm_mon % MONTHS_PER_YEAR + MONTHS_PER_YEAR;
3145 : : }
3146 : :
3147 : : /* adjust for end of month boundary problems... */
8077 tgl@sss.pgh.pa.us 3148 [ + + + + : 1314 : if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
+ - + + ]
3149 [ - + - - : 6 : tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
- - ]
3150 : :
7352 bruce@momjian.us 3151 [ - + ]: 1314 : if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0)
8077 tgl@sss.pgh.pa.us 3152 [ # # ]:UBC 0 : ereport(ERROR,
3153 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3154 : : errmsg("timestamp out of range")));
3155 : : }
3156 : :
7353 bruce@momjian.us 3157 [ + + ]:CBC 4594 : if (span->day != 0)
3158 : : {
3159 : : struct pg_tm tt,
3160 : 1542 : *tm = &tt;
3161 : : fsec_t fsec;
3162 : : int julian;
3163 : :
7352 3164 [ - + ]: 1542 : if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
7353 bruce@momjian.us 3165 [ # # ]:UBC 0 : ereport(ERROR,
3166 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3167 : : errmsg("timestamp out of range")));
3168 : :
3169 : : /*
3170 : : * Add days by converting to and from Julian. We need an overflow
3171 : : * check here since j2date expects a non-negative integer input.
3172 : : */
589 tgl@sss.pgh.pa.us 3173 :CBC 1542 : julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
3174 [ + - ]: 1542 : if (pg_add_s32_overflow(julian, span->day, &julian) ||
3175 [ + + ]: 1542 : julian < 0)
3176 [ + - ]: 3 : ereport(ERROR,
3177 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3178 : : errmsg("timestamp out of range")));
7353 bruce@momjian.us 3179 : 1539 : j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3180 : :
7352 3181 [ - + ]: 1539 : if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0)
7353 bruce@momjian.us 3182 [ # # ]:UBC 0 : ereport(ERROR,
3183 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3184 : : errmsg("timestamp out of range")));
3185 : : }
3186 : :
496 tgl@sss.pgh.pa.us 3187 [ + + ]:CBC 4591 : if (pg_add_s64_overflow(timestamp, span->time, ×tamp))
3188 [ + - ]: 3 : ereport(ERROR,
3189 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3190 : : errmsg("timestamp out of range")));
3191 : :
3461 3192 [ + - - + ]: 4588 : if (!IS_VALID_TIMESTAMP(timestamp))
3461 tgl@sss.pgh.pa.us 3193 [ # # ]:UBC 0 : ereport(ERROR,
3194 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3195 : : errmsg("timestamp out of range")));
3196 : :
8744 lockhart@fourpalms.o 3197 :CBC 4588 : result = timestamp;
3198 : : }
3199 : :
3200 : 4861 : PG_RETURN_TIMESTAMP(result);
3201 : : }
3202 : :
3203 : : Datum
7875 tgl@sss.pgh.pa.us 3204 : 1086 : timestamp_mi_interval(PG_FUNCTION_ARGS)
3205 : : {
7266 bruce@momjian.us 3206 : 1086 : Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
8744 lockhart@fourpalms.o 3207 : 1086 : Interval *span = PG_GETARG_INTERVAL_P(1);
3208 : : Interval tspan;
3209 : :
662 dean.a.rasheed@gmail 3210 : 1086 : interval_um_internal(span, &tspan);
3211 : :
7875 tgl@sss.pgh.pa.us 3212 : 1086 : return DirectFunctionCall2(timestamp_pl_interval,
3213 : : TimestampGetDatum(timestamp),
3214 : : PointerGetDatum(&tspan));
3215 : : }
3216 : :
3217 : :
3218 : : /* timestamptz_pl_interval_internal()
3219 : : * Add an interval to a timestamptz, in the given (or session) timezone.
3220 : : *
3221 : : * Note that interval has provisions for qualitative year/month and day
3222 : : * units, so try to do the right thing with them.
3223 : : * To add a month, increment the month, and use the same day of month.
3224 : : * Then, if the next month has fewer days, set the day of month
3225 : : * to the last day of month.
3226 : : * To add a day, increment the mday, and use the same time of day.
3227 : : * Lastly, add in the "quantitative time".
3228 : : */
3229 : : static TimestampTz
903 3230 : 75997 : timestamptz_pl_interval_internal(TimestampTz timestamp,
3231 : : Interval *span,
3232 : : pg_tz *attimezone)
3233 : : {
3234 : : TimestampTz result;
3235 : : int tz;
3236 : :
3237 : : /*
3238 : : * Handle infinities.
3239 : : *
3240 : : * We treat anything that amounts to "infinity - infinity" as an error,
3241 : : * since the timestamptz type has nothing equivalent to NaN.
3242 : : */
662 dean.a.rasheed@gmail 3243 [ + + + - : 75997 : if (INTERVAL_IS_NOBEGIN(span))
+ - ]
3244 : : {
3245 [ + + ]: 216 : if (TIMESTAMP_IS_NOEND(timestamp))
3246 [ + - ]: 6 : ereport(ERROR,
3247 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3248 : : errmsg("timestamp out of range")));
3249 : : else
3250 : 210 : TIMESTAMP_NOBEGIN(result);
3251 : : }
3252 [ + + + - : 75781 : else if (INTERVAL_IS_NOEND(span))
+ - ]
3253 : : {
3254 [ + + ]: 180 : if (TIMESTAMP_IS_NOBEGIN(timestamp))
3255 [ + - ]: 6 : ereport(ERROR,
3256 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3257 : : errmsg("timestamp out of range")));
3258 : : else
3259 : 174 : TIMESTAMP_NOEND(result);
3260 : : }
3261 [ + + + + ]: 75601 : else if (TIMESTAMP_NOT_FINITE(timestamp))
9220 tgl@sss.pgh.pa.us 3262 : 60 : result = timestamp;
3263 : : else
3264 : : {
3265 : : /* Use session timezone if caller asks for default */
903 3266 [ + + ]: 75541 : if (attimezone == NULL)
3267 : 44237 : attimezone = session_timezone;
3268 : :
9334 lockhart@fourpalms.o 3269 [ + + ]: 75541 : if (span->month != 0)
3270 : : {
3271 : : struct pg_tm tt,
3272 : 27924 : *tm = &tt;
3273 : : fsec_t fsec;
3274 : :
903 tgl@sss.pgh.pa.us 3275 [ - + ]: 27924 : if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, attimezone) != 0)
8077 tgl@sss.pgh.pa.us 3276 [ # # ]:UBC 0 : ereport(ERROR,
3277 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3278 : : errmsg("timestamp out of range")));
3279 : :
496 tgl@sss.pgh.pa.us 3280 [ - + ]:CBC 27924 : if (pg_add_s32_overflow(tm->tm_mon, span->month, &tm->tm_mon))
496 tgl@sss.pgh.pa.us 3281 [ # # ]:UBC 0 : ereport(ERROR,
3282 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3283 : : errmsg("timestamp out of range")));
7352 bruce@momjian.us 3284 [ + + ]:CBC 27924 : if (tm->tm_mon > MONTHS_PER_YEAR)
3285 : : {
3286 : 27036 : tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
3287 : 27036 : tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
3288 : : }
8077 tgl@sss.pgh.pa.us 3289 [ + + ]: 888 : else if (tm->tm_mon < 1)
3290 : : {
7352 bruce@momjian.us 3291 : 675 : tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
3292 : 675 : tm->tm_mon = tm->tm_mon % MONTHS_PER_YEAR + MONTHS_PER_YEAR;
3293 : : }
3294 : :
3295 : : /* adjust for end of month boundary problems... */
8077 tgl@sss.pgh.pa.us 3296 [ + + + + : 27924 : if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
+ + + + ]
3297 [ + + + + : 27 : tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
+ - ]
3298 : :
903 3299 : 27924 : tz = DetermineTimeZoneOffset(tm, attimezone);
3300 : :
7352 bruce@momjian.us 3301 [ - + ]: 27924 : if (tm2timestamp(tm, fsec, &tz, ×tamp) != 0)
8077 tgl@sss.pgh.pa.us 3302 [ # # ]:UBC 0 : ereport(ERROR,
3303 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3304 : : errmsg("timestamp out of range")));
3305 : : }
3306 : :
7353 bruce@momjian.us 3307 [ + + ]:CBC 75541 : if (span->day != 0)
3308 : : {
3309 : : struct pg_tm tt,
3310 : 1827 : *tm = &tt;
3311 : : fsec_t fsec;
3312 : : int julian;
3313 : :
903 tgl@sss.pgh.pa.us 3314 [ - + ]: 1827 : if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, attimezone) != 0)
7353 bruce@momjian.us 3315 [ # # ]:UBC 0 : ereport(ERROR,
3316 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3317 : : errmsg("timestamp out of range")));
3318 : :
3319 : : /*
3320 : : * Add days by converting to and from Julian. We need an overflow
3321 : : * check here since j2date expects a non-negative integer input.
3322 : : * In practice though, it will give correct answers for small
3323 : : * negative Julian dates; we should allow -1 to avoid
3324 : : * timezone-dependent failures, as discussed in timestamp.h.
3325 : : */
589 tgl@sss.pgh.pa.us 3326 :CBC 1827 : julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
3327 [ + - ]: 1827 : if (pg_add_s32_overflow(julian, span->day, &julian) ||
3328 [ + + ]: 1827 : julian < -1)
3329 [ + - ]: 3 : ereport(ERROR,
3330 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3331 : : errmsg("timestamp out of range")));
7353 bruce@momjian.us 3332 : 1824 : j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
3333 : :
903 tgl@sss.pgh.pa.us 3334 : 1824 : tz = DetermineTimeZoneOffset(tm, attimezone);
3335 : :
7352 bruce@momjian.us 3336 [ - + ]: 1824 : if (tm2timestamp(tm, fsec, &tz, ×tamp) != 0)
7353 bruce@momjian.us 3337 [ # # ]:UBC 0 : ereport(ERROR,
3338 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3339 : : errmsg("timestamp out of range")));
3340 : : }
3341 : :
496 tgl@sss.pgh.pa.us 3342 [ + + ]:CBC 75538 : if (pg_add_s64_overflow(timestamp, span->time, ×tamp))
3343 [ + - ]: 3 : ereport(ERROR,
3344 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3345 : : errmsg("timestamp out of range")));
3346 : :
3461 3347 [ + - - + ]: 75535 : if (!IS_VALID_TIMESTAMP(timestamp))
3461 tgl@sss.pgh.pa.us 3348 [ # # ]:UBC 0 : ereport(ERROR,
3349 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3350 : : errmsg("timestamp out of range")));
3351 : :
8744 lockhart@fourpalms.o 3352 :CBC 75535 : result = timestamp;
3353 : : }
3354 : :
903 tgl@sss.pgh.pa.us 3355 : 75979 : return result;
3356 : : }
3357 : :
3358 : : /* timestamptz_mi_interval_internal()
3359 : : * As above, but subtract the interval.
3360 : : */
3361 : : static TimestampTz
3362 : 1059 : timestamptz_mi_interval_internal(TimestampTz timestamp,
3363 : : Interval *span,
3364 : : pg_tz *attimezone)
3365 : : {
3366 : : Interval tspan;
3367 : :
662 dean.a.rasheed@gmail 3368 : 1059 : interval_um_internal(span, &tspan);
3369 : :
903 tgl@sss.pgh.pa.us 3370 : 1059 : return timestamptz_pl_interval_internal(timestamp, &tspan, attimezone);
3371 : : }
3372 : :
3373 : : /* timestamptz_pl_interval()
3374 : : * Add an interval to a timestamptz, in the session timezone.
3375 : : */
3376 : : Datum
3377 : 43430 : timestamptz_pl_interval(PG_FUNCTION_ARGS)
3378 : : {
3379 : 43430 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
3380 : 43430 : Interval *span = PG_GETARG_INTERVAL_P(1);
3381 : :
3382 : 43430 : PG_RETURN_TIMESTAMP(timestamptz_pl_interval_internal(timestamp, span, NULL));
3383 : : }
3384 : :
3385 : : Datum
3386 : 816 : timestamptz_mi_interval(PG_FUNCTION_ARGS)
3387 : : {
3388 : 816 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
3389 : 816 : Interval *span = PG_GETARG_INTERVAL_P(1);
3390 : :
3391 : 816 : PG_RETURN_TIMESTAMP(timestamptz_mi_interval_internal(timestamp, span, NULL));
3392 : : }
3393 : :
3394 : : /* timestamptz_pl_interval_at_zone()
3395 : : * Add an interval to a timestamptz, in the specified timezone.
3396 : : */
3397 : : Datum
3398 : 3 : timestamptz_pl_interval_at_zone(PG_FUNCTION_ARGS)
3399 : : {
3400 : 3 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
3401 : 3 : Interval *span = PG_GETARG_INTERVAL_P(1);
3402 : 3 : text *zone = PG_GETARG_TEXT_PP(2);
3403 : 3 : pg_tz *attimezone = lookup_timezone(zone);
3404 : :
3405 : 3 : PG_RETURN_TIMESTAMP(timestamptz_pl_interval_internal(timestamp, span, attimezone));
3406 : : }
3407 : :
3408 : : Datum
3409 : 3 : timestamptz_mi_interval_at_zone(PG_FUNCTION_ARGS)
3410 : : {
3411 : 3 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
3412 : 3 : Interval *span = PG_GETARG_INTERVAL_P(1);
3413 : 3 : text *zone = PG_GETARG_TEXT_PP(2);
3414 : 3 : pg_tz *attimezone = lookup_timezone(zone);
3415 : :
3416 : 3 : PG_RETURN_TIMESTAMP(timestamptz_mi_interval_internal(timestamp, span, attimezone));
3417 : : }
3418 : :
3419 : : /* interval_um_internal()
3420 : : * Negate an interval.
3421 : : */
3422 : : static void
662 dean.a.rasheed@gmail 3423 : 4020 : interval_um_internal(const Interval *interval, Interval *result)
3424 : : {
3425 [ + + + + : 4020 : if (INTERVAL_IS_NOBEGIN(interval))
+ - ]
3426 : 135 : INTERVAL_NOEND(result);
3427 [ + + + - : 3885 : else if (INTERVAL_IS_NOEND(interval))
+ - ]
3428 : 339 : INTERVAL_NOBEGIN(result);
3429 : : else
3430 : : {
3431 : : /* Negate each field, guarding against overflow */
3432 [ + + + + ]: 7089 : if (pg_sub_s64_overflow(INT64CONST(0), interval->time, &result->time) ||
3433 [ + + ]: 7083 : pg_sub_s32_overflow(0, interval->day, &result->day) ||
3434 : 3540 : pg_sub_s32_overflow(0, interval->month, &result->month) ||
3435 [ - + - - : 3537 : INTERVAL_NOT_FINITE(result))
- - + + +
+ + - ]
3436 [ + - ]: 15 : ereport(ERROR,
3437 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3438 : : errmsg("interval out of range")));
3439 : : }
3440 : 4005 : }
3441 : :
3442 : : Datum
9220 tgl@sss.pgh.pa.us 3443 : 1857 : interval_um(PG_FUNCTION_ARGS)
3444 : : {
3445 : 1857 : Interval *interval = PG_GETARG_INTERVAL_P(0);
3446 : : Interval *result;
3447 : :
3448 : 1857 : result = (Interval *) palloc(sizeof(Interval));
662 dean.a.rasheed@gmail 3449 : 1857 : interval_um_internal(interval, result);
3450 : :
9220 tgl@sss.pgh.pa.us 3451 : 1842 : PG_RETURN_INTERVAL_P(result);
3452 : : }
3453 : :
3454 : :
3455 : : Datum
9220 tgl@sss.pgh.pa.us 3456 :UBC 0 : interval_smaller(PG_FUNCTION_ARGS)
3457 : : {
3458 : 0 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3459 : 0 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3460 : : Interval *result;
3461 : :
3462 : : /* use interval_cmp_internal to be sure this agrees with comparisons */
8065 3463 [ # # ]: 0 : if (interval_cmp_internal(interval1, interval2) < 0)
3464 : 0 : result = interval1;
3465 : : else
3466 : 0 : result = interval2;
9220 3467 : 0 : PG_RETURN_INTERVAL_P(result);
3468 : : }
3469 : :
3470 : : Datum
3471 : 0 : interval_larger(PG_FUNCTION_ARGS)
3472 : : {
3473 : 0 : Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3474 : 0 : Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3475 : : Interval *result;
3476 : :
8065 3477 [ # # ]: 0 : if (interval_cmp_internal(interval1, interval2) > 0)
3478 : 0 : result = interval1;
3479 : : else
3480 : 0 : result = interval2;
9220 3481 : 0 : PG_RETURN_INTERVAL_P(result);
3482 : : }
3483 : :
3484 : : static void
662 dean.a.rasheed@gmail 3485 :CBC 264 : finite_interval_pl(const Interval *span1, const Interval *span2, Interval *result)
3486 : : {
3487 [ - + - - : 264 : Assert(!INTERVAL_NOT_FINITE(span1));
- - - + -
- - - ]
3488 [ + + + - : 264 : Assert(!INTERVAL_NOT_FINITE(span2));
+ - + + +
- - + ]
3489 : :
3490 [ + - + - ]: 528 : if (pg_add_s32_overflow(span1->month, span2->month, &result->month) ||
3491 [ + - ]: 528 : pg_add_s32_overflow(span1->day, span2->day, &result->day) ||
3492 : 264 : pg_add_s64_overflow(span1->time, span2->time, &result->time) ||
3493 [ + + + - : 264 : INTERVAL_NOT_FINITE(result))
+ + + + +
- + + ]
3494 [ + - ]: 6 : ereport(ERROR,
3495 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3496 : : errmsg("interval out of range")));
3497 : 258 : }
3498 : :
3499 : : Datum
9220 tgl@sss.pgh.pa.us 3500 : 279 : interval_pl(PG_FUNCTION_ARGS)
3501 : : {
3502 : 279 : Interval *span1 = PG_GETARG_INTERVAL_P(0);
3503 : 279 : Interval *span2 = PG_GETARG_INTERVAL_P(1);
3504 : : Interval *result;
3505 : :
3506 : 279 : result = (Interval *) palloc(sizeof(Interval));
3507 : :
3508 : : /*
3509 : : * Handle infinities.
3510 : : *
3511 : : * We treat anything that amounts to "infinity - infinity" as an error,
3512 : : * since the interval type has nothing equivalent to NaN.
3513 : : */
662 dean.a.rasheed@gmail 3514 [ + + + - : 279 : if (INTERVAL_IS_NOBEGIN(span1))
+ - ]
3515 : : {
3516 [ + + + - : 27 : if (INTERVAL_IS_NOEND(span2))
+ - ]
3517 [ + - ]: 3 : ereport(ERROR,
3518 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3519 : : errmsg("interval out of range")));
3520 : : else
3521 : 24 : INTERVAL_NOBEGIN(result);
3522 : : }
3523 [ + + + - : 252 : else if (INTERVAL_IS_NOEND(span1))
+ - ]
3524 : : {
3525 [ + + + - : 21 : if (INTERVAL_IS_NOBEGIN(span2))
+ - ]
3526 [ + - ]: 3 : ereport(ERROR,
3527 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3528 : : errmsg("interval out of range")));
3529 : : else
3530 : 18 : INTERVAL_NOEND(result);
3531 : : }
3532 [ + + + - : 231 : else if (INTERVAL_NOT_FINITE(span2))
- + + + +
- + - ]
3533 : 69 : memcpy(result, span2, sizeof(Interval));
3534 : : else
3535 : 162 : finite_interval_pl(span1, span2, result);
3536 : :
3537 : 267 : PG_RETURN_INTERVAL_P(result);
3538 : : }
3539 : :
3540 : : static void
3541 : 774 : finite_interval_mi(const Interval *span1, const Interval *span2, Interval *result)
3542 : : {
3543 [ + + + + : 774 : Assert(!INTERVAL_NOT_FINITE(span1));
+ - + + +
+ - + ]
3544 [ + + + + : 774 : Assert(!INTERVAL_NOT_FINITE(span2));
+ - + + +
+ - + ]
3545 : :
3546 [ + - + - ]: 1548 : if (pg_sub_s32_overflow(span1->month, span2->month, &result->month) ||
3547 [ + - ]: 1548 : pg_sub_s32_overflow(span1->day, span2->day, &result->day) ||
3548 : 774 : pg_sub_s64_overflow(span1->time, span2->time, &result->time) ||
3549 [ + + + - : 774 : INTERVAL_NOT_FINITE(result))
- + + + +
- + - ]
4237 bruce@momjian.us 3550 [ + - ]: 6 : ereport(ERROR,
3551 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3552 : : errmsg("interval out of range")));
9220 tgl@sss.pgh.pa.us 3553 : 768 : }
3554 : :
3555 : : Datum
3556 : 897 : interval_mi(PG_FUNCTION_ARGS)
3557 : : {
3558 : 897 : Interval *span1 = PG_GETARG_INTERVAL_P(0);
3559 : 897 : Interval *span2 = PG_GETARG_INTERVAL_P(1);
3560 : : Interval *result;
3561 : :
3562 : 897 : result = (Interval *) palloc(sizeof(Interval));
3563 : :
3564 : : /*
3565 : : * Handle infinities.
3566 : : *
3567 : : * We treat anything that amounts to "infinity - infinity" as an error,
3568 : : * since the interval type has nothing equivalent to NaN.
3569 : : */
662 dean.a.rasheed@gmail 3570 [ + + + + : 897 : if (INTERVAL_IS_NOBEGIN(span1))
+ + ]
3571 : : {
3572 [ + + + - : 27 : if (INTERVAL_IS_NOBEGIN(span2))
+ - ]
3573 [ + - ]: 3 : ereport(ERROR,
3574 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3575 : : errmsg("interval out of range")));
3576 : : else
3577 : 24 : INTERVAL_NOBEGIN(result);
3578 : : }
3579 [ + + + + : 870 : else if (INTERVAL_IS_NOEND(span1))
+ + ]
3580 : : {
3581 [ + + + - : 24 : if (INTERVAL_IS_NOEND(span2))
+ - ]
3582 [ + - ]: 3 : ereport(ERROR,
3583 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3584 : : errmsg("interval out of range")));
3585 : : else
3586 : 21 : INTERVAL_NOEND(result);
3587 : : }
3588 [ + + + + : 846 : else if (INTERVAL_IS_NOBEGIN(span2))
+ + ]
3589 : 3 : INTERVAL_NOEND(result);
3590 [ + + + + : 843 : else if (INTERVAL_IS_NOEND(span2))
+ + ]
3591 : 93 : INTERVAL_NOBEGIN(result);
3592 : : else
3593 : 750 : finite_interval_mi(span1, span2, result);
3594 : :
9220 tgl@sss.pgh.pa.us 3595 : 885 : PG_RETURN_INTERVAL_P(result);
3596 : : }
3597 : :
3598 : : /*
3599 : : * There is no interval_abs(): it is unclear what value to return:
3600 : : * http://archives.postgresql.org/pgsql-general/2009-10/msg01031.php
3601 : : * http://archives.postgresql.org/pgsql-general/2009-11/msg00041.php
3602 : : */
3603 : :
3604 : : Datum
3605 : 5826 : interval_mul(PG_FUNCTION_ARGS)
3606 : : {
7349 bruce@momjian.us 3607 : 5826 : Interval *span = PG_GETARG_INTERVAL_P(0);
9220 tgl@sss.pgh.pa.us 3608 : 5826 : float8 factor = PG_GETARG_FLOAT8(1);
3609 : : double month_remainder_days,
3610 : : sec_remainder,
3611 : : result_double;
6912 bruce@momjian.us 3612 : 5826 : int32 orig_month = span->month,
3613 : 5826 : orig_day = span->day;
3614 : : Interval *result;
3615 : :
9220 tgl@sss.pgh.pa.us 3616 : 5826 : result = (Interval *) palloc(sizeof(Interval));
3617 : :
3618 : : /*
3619 : : * Handle NaN and infinities.
3620 : : *
3621 : : * We treat "0 * infinity" and "infinity * 0" as errors, since the
3622 : : * interval type has nothing equivalent to NaN.
3623 : : */
662 dean.a.rasheed@gmail 3624 [ + + ]: 5826 : if (isnan(factor))
658 3625 : 6 : goto out_of_range;
3626 : :
662 3627 [ + + + - : 5820 : if (INTERVAL_NOT_FINITE(span))
- + + + +
- + - ]
3628 : : {
3629 [ + + ]: 30 : if (factor == 0.0)
658 3630 : 6 : goto out_of_range;
3631 : :
3632 [ + + ]: 24 : if (factor < 0.0)
662 3633 : 12 : interval_um_internal(span, result);
3634 : : else
3635 : 12 : memcpy(result, span, sizeof(Interval));
3636 : :
3637 : 24 : PG_RETURN_INTERVAL_P(result);
3638 : : }
3639 [ + + ]: 5790 : if (isinf(factor))
3640 : : {
3641 : 12 : int isign = interval_sign(span);
3642 : :
3643 [ + + ]: 12 : if (isign == 0)
658 3644 : 6 : goto out_of_range;
3645 : :
3646 [ + + ]: 6 : if (factor * isign < 0)
662 3647 : 3 : INTERVAL_NOBEGIN(result);
3648 : : else
3649 : 3 : INTERVAL_NOEND(result);
3650 : :
3651 : 6 : PG_RETURN_INTERVAL_P(result);
3652 : : }
3653 : :
4237 bruce@momjian.us 3654 : 5778 : result_double = span->month * factor;
658 dean.a.rasheed@gmail 3655 [ + - + - : 5778 : if (isnan(result_double) || !FLOAT8_FITS_IN_INT32(result_double))
+ + ]
3656 : 3 : goto out_of_range;
4237 bruce@momjian.us 3657 : 5775 : result->month = (int32) result_double;
3658 : :
3659 : 5775 : result_double = span->day * factor;
658 dean.a.rasheed@gmail 3660 [ + - + - : 5775 : if (isnan(result_double) || !FLOAT8_FITS_IN_INT32(result_double))
+ + ]
3661 : 3 : goto out_of_range;
4237 bruce@momjian.us 3662 : 5772 : result->day = (int32) result_double;
3663 : :
3664 : : /*
3665 : : * The above correctly handles the whole-number part of the month and day
3666 : : * products, but we have to do something with any fractional part
3667 : : * resulting when the factor is non-integral. We cascade the fractions
3668 : : * down to lower units using the conversion factors DAYS_PER_MONTH and
3669 : : * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3670 : : * so by the representation. The user can choose to cascade up later,
3671 : : * using justify_hours and/or justify_days.
3672 : : */
3673 : :
3674 : : /*
3675 : : * Fractional months full days into days.
3676 : : *
3677 : : * Floating point calculation are inherently imprecise, so these
3678 : : * calculations are crafted to produce the most reliable result possible.
3679 : : * TSROUND() is needed to more accurately produce whole numbers where
3680 : : * appropriate.
3681 : : */
6941 3682 : 5772 : month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3683 : 5772 : month_remainder_days = TSROUND(month_remainder_days);
3684 : 5772 : sec_remainder = (orig_day * factor - result->day +
2999 tgl@sss.pgh.pa.us 3685 : 5772 : month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
6941 bruce@momjian.us 3686 : 5772 : sec_remainder = TSROUND(sec_remainder);
3687 : :
3688 : : /*
3689 : : * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3690 : : * cascade from months and days. It might still be >24 if the combination
3691 : : * of cascade and the seconds factor operation itself.
3692 : : */
1065 peter@eisentraut.org 3693 [ - + ]: 5772 : if (fabs(sec_remainder) >= SECS_PER_DAY)
3694 : : {
658 dean.a.rasheed@gmail 3695 [ # # ]:UBC 0 : if (pg_add_s32_overflow(result->day,
3696 : 0 : (int) (sec_remainder / SECS_PER_DAY),
3697 : : &result->day))
3698 : 0 : goto out_of_range;
6912 bruce@momjian.us 3699 : 0 : sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3700 : : }
3701 : :
3702 : : /* cascade units down */
658 dean.a.rasheed@gmail 3703 [ + + ]:CBC 5772 : if (pg_add_s32_overflow(result->day, (int32) month_remainder_days,
3704 : : &result->day))
3705 : 3 : goto out_of_range;
4237 bruce@momjian.us 3706 : 5769 : result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
2130 tgl@sss.pgh.pa.us 3707 [ + - + - : 5769 : if (isnan(result_double) || !FLOAT8_FITS_IN_INT64(result_double))
+ + ]
658 dean.a.rasheed@gmail 3708 : 3 : goto out_of_range;
4237 bruce@momjian.us 3709 : 5766 : result->time = (int64) result_double;
3710 : :
662 dean.a.rasheed@gmail 3711 [ + + + - : 5766 : if (INTERVAL_NOT_FINITE(result))
- + - + -
- - - ]
658 3712 : 3 : goto out_of_range;
3713 : :
7317 bruce@momjian.us 3714 : 5763 : PG_RETURN_INTERVAL_P(result);
3715 : :
658 dean.a.rasheed@gmail 3716 : 33 : out_of_range:
3717 [ + - ]: 33 : ereport(ERROR,
3718 : : errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3719 : : errmsg("interval out of range"));
3720 : :
3721 : : PG_RETURN_NULL(); /* keep compiler quiet */
3722 : : }
3723 : :
3724 : : Datum
9220 tgl@sss.pgh.pa.us 3725 : 5715 : mul_d_interval(PG_FUNCTION_ARGS)
3726 : : {
3727 : : /* Args are float8 and Interval *, but leave them as generic Datum */
3728 : 5715 : Datum factor = PG_GETARG_DATUM(0);
7349 bruce@momjian.us 3729 : 5715 : Datum span = PG_GETARG_DATUM(1);
3730 : :
3731 : 5715 : return DirectFunctionCall2(interval_mul, span, factor);
3732 : : }
3733 : :
3734 : : Datum
9220 tgl@sss.pgh.pa.us 3735 : 111 : interval_div(PG_FUNCTION_ARGS)
3736 : : {
8539 lockhart@fourpalms.o 3737 : 111 : Interval *span = PG_GETARG_INTERVAL_P(0);
9220 tgl@sss.pgh.pa.us 3738 : 111 : float8 factor = PG_GETARG_FLOAT8(1);
3739 : : double month_remainder_days,
3740 : : sec_remainder,
3741 : : result_double;
6912 bruce@momjian.us 3742 : 111 : int32 orig_month = span->month,
3743 : 111 : orig_day = span->day;
3744 : : Interval *result;
3745 : :
9220 tgl@sss.pgh.pa.us 3746 : 111 : result = (Interval *) palloc(sizeof(Interval));
3747 : :
3748 [ - + ]: 111 : if (factor == 0.0)
8077 tgl@sss.pgh.pa.us 3749 [ # # ]:UBC 0 : ereport(ERROR,
3750 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
3751 : : errmsg("division by zero")));
3752 : :
3753 : : /*
3754 : : * Handle NaN and infinities.
3755 : : *
3756 : : * We treat "infinity / infinity" as an error, since the interval type has
3757 : : * nothing equivalent to NaN. Otherwise, dividing by infinity is handled
3758 : : * by the regular division code, causing all fields to be set to zero.
3759 : : */
662 dean.a.rasheed@gmail 3760 [ + + ]:CBC 111 : if (isnan(factor))
658 3761 : 6 : goto out_of_range;
3762 : :
662 3763 [ + + + - : 105 : if (INTERVAL_NOT_FINITE(span))
- + + + +
- + - ]
3764 : : {
3765 [ + + ]: 24 : if (isinf(factor))
658 3766 : 12 : goto out_of_range;
3767 : :
662 3768 [ + + ]: 12 : if (factor < 0.0)
3769 : 6 : interval_um_internal(span, result);
3770 : : else
3771 : 6 : memcpy(result, span, sizeof(Interval));
3772 : :
3773 : 12 : PG_RETURN_INTERVAL_P(result);
3774 : : }
3775 : :
658 3776 : 81 : result_double = span->month / factor;
3777 [ + - + - : 81 : if (isnan(result_double) || !FLOAT8_FITS_IN_INT32(result_double))
+ + ]
3778 : 3 : goto out_of_range;
3779 : 78 : result->month = (int32) result_double;
3780 : :
3781 : 78 : result_double = span->day / factor;
3782 [ + - + - : 78 : if (isnan(result_double) || !FLOAT8_FITS_IN_INT32(result_double))
+ + ]
3783 : 3 : goto out_of_range;
3784 : 75 : result->day = (int32) result_double;
3785 : :
3786 : : /*
3787 : : * Fractional months full days into days. See comment in interval_mul().
3788 : : */
6941 bruce@momjian.us 3789 : 75 : month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3790 : 75 : month_remainder_days = TSROUND(month_remainder_days);
3791 : 75 : sec_remainder = (orig_day / factor - result->day +
2999 tgl@sss.pgh.pa.us 3792 : 75 : month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
6941 bruce@momjian.us 3793 : 75 : sec_remainder = TSROUND(sec_remainder);
1065 peter@eisentraut.org 3794 [ + + ]: 75 : if (fabs(sec_remainder) >= SECS_PER_DAY)
3795 : : {
658 dean.a.rasheed@gmail 3796 [ - + ]: 3 : if (pg_add_s32_overflow(result->day,
3797 : 3 : (int) (sec_remainder / SECS_PER_DAY),
3798 : : &result->day))
658 dean.a.rasheed@gmail 3799 :UBC 0 : goto out_of_range;
6912 bruce@momjian.us 3800 :CBC 3 : sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3801 : : }
3802 : :
3803 : : /* cascade units down */
658 dean.a.rasheed@gmail 3804 [ - + ]: 75 : if (pg_add_s32_overflow(result->day, (int32) month_remainder_days,
3805 : : &result->day))
658 dean.a.rasheed@gmail 3806 :UBC 0 : goto out_of_range;
658 dean.a.rasheed@gmail 3807 :CBC 75 : result_double = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3808 [ + - + - : 75 : if (isnan(result_double) || !FLOAT8_FITS_IN_INT64(result_double))
+ + ]
3809 : 3 : goto out_of_range;
3810 : 72 : result->time = (int64) result_double;
3811 : :
662 3812 [ + + + - : 72 : if (INTERVAL_NOT_FINITE(result))
- + - + -
- - - ]
658 3813 : 3 : goto out_of_range;
3814 : :
7317 bruce@momjian.us 3815 : 69 : PG_RETURN_INTERVAL_P(result);
3816 : :
658 dean.a.rasheed@gmail 3817 : 30 : out_of_range:
3818 [ + - ]: 30 : ereport(ERROR,
3819 : : errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3820 : : errmsg("interval out of range"));
3821 : :
3822 : : PG_RETURN_NULL(); /* keep compiler quiet */
3823 : : }
3824 : :
3825 : :
3826 : : /*
3827 : : * in_range support functions for timestamps and intervals.
3828 : : *
3829 : : * Per SQL spec, we support these with interval as the offset type.
3830 : : * The spec's restriction that the offset not be negative is a bit hard to
3831 : : * decipher for intervals, but we choose to interpret it the same as our
3832 : : * interval comparison operators would.
3833 : : */
3834 : :
3835 : : Datum
2768 tgl@sss.pgh.pa.us 3836 : 567 : in_range_timestamptz_interval(PG_FUNCTION_ARGS)
3837 : : {
3838 : 567 : TimestampTz val = PG_GETARG_TIMESTAMPTZ(0);
3839 : 567 : TimestampTz base = PG_GETARG_TIMESTAMPTZ(1);
3840 : 567 : Interval *offset = PG_GETARG_INTERVAL_P(2);
3841 : 567 : bool sub = PG_GETARG_BOOL(3);
3842 : 567 : bool less = PG_GETARG_BOOL(4);
3843 : : TimestampTz sum;
3844 : :
662 dean.a.rasheed@gmail 3845 [ + + ]: 567 : if (interval_sign(offset) < 0)
2768 tgl@sss.pgh.pa.us 3846 [ + - ]: 6 : ereport(ERROR,
3847 : : (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3848 : : errmsg("invalid preceding or following size in window function")));
3849 : :
3850 : : /*
3851 : : * Deal with cases where both base and offset are infinite, and computing
3852 : : * base +/- offset would cause an error. As for float and numeric types,
3853 : : * we assume that all values infinitely precede +infinity and infinitely
3854 : : * follow -infinity. See in_range_float8_float8() for reasoning.
3855 : : */
662 dean.a.rasheed@gmail 3856 [ + + + - : 561 : if (INTERVAL_IS_NOEND(offset) &&
+ - + + +
+ ]
3857 : : (sub ? TIMESTAMP_IS_NOEND(base) : TIMESTAMP_IS_NOBEGIN(base)))
3858 : 114 : PG_RETURN_BOOL(true);
3859 : :
3860 : : /* We don't currently bother to avoid overflow hazards here */
2768 tgl@sss.pgh.pa.us 3861 [ + + ]: 447 : if (sub)
903 3862 : 240 : sum = timestamptz_mi_interval_internal(base, offset, NULL);
3863 : : else
3864 : 207 : sum = timestamptz_pl_interval_internal(base, offset, NULL);
3865 : :
2768 3866 [ + + ]: 447 : if (less)
3867 : 177 : PG_RETURN_BOOL(val <= sum);
3868 : : else
3869 : 270 : PG_RETURN_BOOL(val >= sum);
3870 : : }
3871 : :
3872 : : Datum
3873 : 1233 : in_range_timestamp_interval(PG_FUNCTION_ARGS)
3874 : : {
3875 : 1233 : Timestamp val = PG_GETARG_TIMESTAMP(0);
3876 : 1233 : Timestamp base = PG_GETARG_TIMESTAMP(1);
3877 : 1233 : Interval *offset = PG_GETARG_INTERVAL_P(2);
3878 : 1233 : bool sub = PG_GETARG_BOOL(3);
3879 : 1233 : bool less = PG_GETARG_BOOL(4);
3880 : : Timestamp sum;
3881 : :
662 dean.a.rasheed@gmail 3882 [ + + ]: 1233 : if (interval_sign(offset) < 0)
2768 tgl@sss.pgh.pa.us 3883 [ + - ]: 9 : ereport(ERROR,
3884 : : (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3885 : : errmsg("invalid preceding or following size in window function")));
3886 : :
3887 : : /*
3888 : : * Deal with cases where both base and offset are infinite, and computing
3889 : : * base +/- offset would cause an error. As for float and numeric types,
3890 : : * we assume that all values infinitely precede +infinity and infinitely
3891 : : * follow -infinity. See in_range_float8_float8() for reasoning.
3892 : : */
662 dean.a.rasheed@gmail 3893 [ + + + - : 1224 : if (INTERVAL_IS_NOEND(offset) &&
+ - + + +
+ ]
3894 : : (sub ? TIMESTAMP_IS_NOEND(base) : TIMESTAMP_IS_NOBEGIN(base)))
3895 : 114 : PG_RETURN_BOOL(true);
3896 : :
3897 : : /* We don't currently bother to avoid overflow hazards here */
2768 tgl@sss.pgh.pa.us 3898 [ + + ]: 1110 : if (sub)
3899 : 516 : sum = DatumGetTimestamp(DirectFunctionCall2(timestamp_mi_interval,
3900 : : TimestampGetDatum(base),
3901 : : IntervalPGetDatum(offset)));
3902 : : else
3903 : 594 : sum = DatumGetTimestamp(DirectFunctionCall2(timestamp_pl_interval,
3904 : : TimestampGetDatum(base),
3905 : : IntervalPGetDatum(offset)));
3906 : :
3907 [ + + ]: 1110 : if (less)
3908 : 603 : PG_RETURN_BOOL(val <= sum);
3909 : : else
3910 : 507 : PG_RETURN_BOOL(val >= sum);
3911 : : }
3912 : :
3913 : : Datum
3914 : 564 : in_range_interval_interval(PG_FUNCTION_ARGS)
3915 : : {
3916 : 564 : Interval *val = PG_GETARG_INTERVAL_P(0);
3917 : 564 : Interval *base = PG_GETARG_INTERVAL_P(1);
3918 : 564 : Interval *offset = PG_GETARG_INTERVAL_P(2);
3919 : 564 : bool sub = PG_GETARG_BOOL(3);
3920 : 564 : bool less = PG_GETARG_BOOL(4);
3921 : : Interval *sum;
3922 : :
662 dean.a.rasheed@gmail 3923 [ + + ]: 564 : if (interval_sign(offset) < 0)
2768 tgl@sss.pgh.pa.us 3924 [ + - ]: 6 : ereport(ERROR,
3925 : : (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3926 : : errmsg("invalid preceding or following size in window function")));
3927 : :
3928 : : /*
3929 : : * Deal with cases where both base and offset are infinite, and computing
3930 : : * base +/- offset would cause an error. As for float and numeric types,
3931 : : * we assume that all values infinitely precede +infinity and infinitely
3932 : : * follow -infinity. See in_range_float8_float8() for reasoning.
3933 : : */
662 dean.a.rasheed@gmail 3934 [ + + + - : 840 : if (INTERVAL_IS_NOEND(offset) &&
+ - + + +
+ ]
3935 [ + + + - : 282 : (sub ? INTERVAL_IS_NOEND(base) : INTERVAL_IS_NOBEGIN(base)))
+ - + + +
- + - ]
3936 : 114 : PG_RETURN_BOOL(true);
3937 : :
3938 : : /* We don't currently bother to avoid overflow hazards here */
2768 tgl@sss.pgh.pa.us 3939 [ + + ]: 444 : if (sub)
3940 : 240 : sum = DatumGetIntervalP(DirectFunctionCall2(interval_mi,
3941 : : IntervalPGetDatum(base),
3942 : : IntervalPGetDatum(offset)));
3943 : : else
3944 : 204 : sum = DatumGetIntervalP(DirectFunctionCall2(interval_pl,
3945 : : IntervalPGetDatum(base),
3946 : : IntervalPGetDatum(offset)));
3947 : :
3948 [ + + ]: 444 : if (less)
3949 : 174 : PG_RETURN_BOOL(interval_cmp_internal(val, sum) <= 0);
3950 : : else
3951 : 270 : PG_RETURN_BOOL(interval_cmp_internal(val, sum) >= 0);
3952 : : }
3953 : :
3954 : :
3955 : : /*
3956 : : * Prepare state data for an interval aggregate function, that needs to compute
3957 : : * sum and count, in the aggregate's memory context.
3958 : : *
3959 : : * The function is used when the state data needs to be allocated in aggregate's
3960 : : * context. When the state data needs to be allocated in the current memory
3961 : : * context, we use palloc0 directly e.g. interval_avg_deserialize().
3962 : : */
3963 : : static IntervalAggState *
662 dean.a.rasheed@gmail 3964 : 27 : makeIntervalAggState(FunctionCallInfo fcinfo)
3965 : : {
3966 : : IntervalAggState *state;
3967 : : MemoryContext agg_context;
3968 : : MemoryContext old_context;
3969 : :
3970 [ - + ]: 27 : if (!AggCheckCallContext(fcinfo, &agg_context))
662 dean.a.rasheed@gmail 3971 [ # # ]:UBC 0 : elog(ERROR, "aggregate function called in non-aggregate context");
3972 : :
662 dean.a.rasheed@gmail 3973 :CBC 27 : old_context = MemoryContextSwitchTo(agg_context);
3974 : :
3975 : 27 : state = (IntervalAggState *) palloc0(sizeof(IntervalAggState));
3976 : :
3977 : 27 : MemoryContextSwitchTo(old_context);
3978 : :
3979 : 27 : return state;
3980 : : }
3981 : :
3982 : : /*
3983 : : * Accumulate a new input value for interval aggregate functions.
3984 : : */
3985 : : static void
3986 : 162 : do_interval_accum(IntervalAggState *state, Interval *newval)
3987 : : {
3988 : : /* Infinite inputs are counted separately, and do not affect "N" */
3989 [ + + + - : 162 : if (INTERVAL_IS_NOBEGIN(newval))
+ + ]
3990 : : {
3991 : 30 : state->nInfcount++;
3992 : 30 : return;
3993 : : }
3994 : :
3995 [ + + + - : 132 : if (INTERVAL_IS_NOEND(newval))
+ + ]
3996 : : {
3997 : 30 : state->pInfcount++;
3998 : 30 : return;
3999 : : }
4000 : :
4001 : 102 : finite_interval_pl(&state->sumX, newval, &state->sumX);
4002 : 102 : state->N++;
4003 : : }
4004 : :
4005 : : /*
4006 : : * Remove the given interval value from the aggregated state.
4007 : : */
4008 : : static void
4009 : 102 : do_interval_discard(IntervalAggState *state, Interval *newval)
4010 : : {
4011 : : /* Infinite inputs are counted separately, and do not affect "N" */
4012 [ + + + - : 102 : if (INTERVAL_IS_NOBEGIN(newval))
+ + ]
4013 : : {
4014 : 12 : state->nInfcount--;
4015 : 12 : return;
4016 : : }
4017 : :
4018 [ + + + - : 90 : if (INTERVAL_IS_NOEND(newval))
+ + ]
4019 : : {
4020 : 24 : state->pInfcount--;
4021 : 24 : return;
4022 : : }
4023 : :
4024 : : /* Handle the to-be-discarded finite value. */
4025 : 66 : state->N--;
4026 [ + + ]: 66 : if (state->N > 0)
4027 : 24 : finite_interval_mi(&state->sumX, newval, &state->sumX);
4028 : : else
4029 : : {
4030 : : /* All values discarded, reset the state */
4031 [ - + ]: 42 : Assert(state->N == 0);
4032 : 42 : memset(&state->sumX, 0, sizeof(state->sumX));
4033 : : }
4034 : : }
4035 : :
4036 : : /*
4037 : : * Transition function for sum() and avg() interval aggregates.
4038 : : */
4039 : : Datum
4040 : 204 : interval_avg_accum(PG_FUNCTION_ARGS)
4041 : : {
4042 : : IntervalAggState *state;
4043 : :
4044 [ + + ]: 204 : state = PG_ARGISNULL(0) ? NULL : (IntervalAggState *) PG_GETARG_POINTER(0);
4045 : :
4046 : : /* Create the state data on the first call */
4047 [ + + ]: 204 : if (state == NULL)
4048 : 27 : state = makeIntervalAggState(fcinfo);
4049 : :
4050 [ + + ]: 204 : if (!PG_ARGISNULL(1))
4051 : 162 : do_interval_accum(state, PG_GETARG_INTERVAL_P(1));
4052 : :
4053 : 204 : PG_RETURN_POINTER(state);
4054 : : }
4055 : :
4056 : : /*
4057 : : * Combine function for sum() and avg() interval aggregates.
4058 : : *
4059 : : * Combine the given internal aggregate states and place the combination in
4060 : : * the first argument.
4061 : : */
4062 : : Datum
662 dean.a.rasheed@gmail 4063 :UBC 0 : interval_avg_combine(PG_FUNCTION_ARGS)
4064 : : {
4065 : : IntervalAggState *state1;
4066 : : IntervalAggState *state2;
4067 : :
4068 [ # # ]: 0 : state1 = PG_ARGISNULL(0) ? NULL : (IntervalAggState *) PG_GETARG_POINTER(0);
4069 [ # # ]: 0 : state2 = PG_ARGISNULL(1) ? NULL : (IntervalAggState *) PG_GETARG_POINTER(1);
4070 : :
4071 [ # # ]: 0 : if (state2 == NULL)
4072 : 0 : PG_RETURN_POINTER(state1);
4073 : :
4074 [ # # ]: 0 : if (state1 == NULL)
4075 : : {
4076 : : /* manually copy all fields from state2 to state1 */
4077 : 0 : state1 = makeIntervalAggState(fcinfo);
4078 : :
4079 : 0 : state1->N = state2->N;
4080 : 0 : state1->pInfcount = state2->pInfcount;
4081 : 0 : state1->nInfcount = state2->nInfcount;
4082 : :
4083 : 0 : state1->sumX.day = state2->sumX.day;
4084 : 0 : state1->sumX.month = state2->sumX.month;
4085 : 0 : state1->sumX.time = state2->sumX.time;
4086 : :
4087 : 0 : PG_RETURN_POINTER(state1);
4088 : : }
4089 : :
4090 : 0 : state1->N += state2->N;
4091 : 0 : state1->pInfcount += state2->pInfcount;
4092 : 0 : state1->nInfcount += state2->nInfcount;
4093 : :
4094 : : /* Accumulate finite interval values, if any. */
4095 [ # # ]: 0 : if (state2->N > 0)
4096 : 0 : finite_interval_pl(&state1->sumX, &state2->sumX, &state1->sumX);
4097 : :
4098 : 0 : PG_RETURN_POINTER(state1);
4099 : : }
4100 : :
4101 : : /*
4102 : : * interval_avg_serialize
4103 : : * Serialize IntervalAggState for interval aggregates.
4104 : : */
4105 : : Datum
4106 : 0 : interval_avg_serialize(PG_FUNCTION_ARGS)
4107 : : {
4108 : : IntervalAggState *state;
4109 : : StringInfoData buf;
4110 : : bytea *result;
4111 : :
4112 : : /* Ensure we disallow calling when not in aggregate context */
4113 [ # # ]: 0 : if (!AggCheckCallContext(fcinfo, NULL))
4114 [ # # ]: 0 : elog(ERROR, "aggregate function called in non-aggregate context");
4115 : :
4116 : 0 : state = (IntervalAggState *) PG_GETARG_POINTER(0);
4117 : :
4118 : 0 : pq_begintypsend(&buf);
4119 : :
4120 : : /* N */
4121 : 0 : pq_sendint64(&buf, state->N);
4122 : :
4123 : : /* sumX */
4124 : 0 : pq_sendint64(&buf, state->sumX.time);
4125 : 0 : pq_sendint32(&buf, state->sumX.day);
4126 : 0 : pq_sendint32(&buf, state->sumX.month);
4127 : :
4128 : : /* pInfcount */
4129 : 0 : pq_sendint64(&buf, state->pInfcount);
4130 : :
4131 : : /* nInfcount */
4132 : 0 : pq_sendint64(&buf, state->nInfcount);
4133 : :
4134 : 0 : result = pq_endtypsend(&buf);
4135 : :
4136 : 0 : PG_RETURN_BYTEA_P(result);
4137 : : }
4138 : :
4139 : : /*
4140 : : * interval_avg_deserialize
4141 : : * Deserialize bytea into IntervalAggState for interval aggregates.
4142 : : */
4143 : : Datum
4144 : 0 : interval_avg_deserialize(PG_FUNCTION_ARGS)
4145 : : {
4146 : : bytea *sstate;
4147 : : IntervalAggState *result;
4148 : : StringInfoData buf;
4149 : :
4150 [ # # ]: 0 : if (!AggCheckCallContext(fcinfo, NULL))
4151 [ # # ]: 0 : elog(ERROR, "aggregate function called in non-aggregate context");
4152 : :
4153 : 0 : sstate = PG_GETARG_BYTEA_PP(0);
4154 : :
4155 : : /*
4156 : : * Initialize a StringInfo so that we can "receive" it using the standard
4157 : : * recv-function infrastructure.
4158 : : */
4159 [ # # ]: 0 : initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
4160 [ # # # # : 0 : VARSIZE_ANY_EXHDR(sstate));
# # # # #
# ]
4161 : :
4162 : 0 : result = (IntervalAggState *) palloc0(sizeof(IntervalAggState));
4163 : :
4164 : : /* N */
4165 : 0 : result->N = pq_getmsgint64(&buf);
4166 : :
4167 : : /* sumX */
4168 : 0 : result->sumX.time = pq_getmsgint64(&buf);
4169 : 0 : result->sumX.day = pq_getmsgint(&buf, 4);
4170 : 0 : result->sumX.month = pq_getmsgint(&buf, 4);
4171 : :
4172 : : /* pInfcount */
4173 : 0 : result->pInfcount = pq_getmsgint64(&buf);
4174 : :
4175 : : /* nInfcount */
4176 : 0 : result->nInfcount = pq_getmsgint64(&buf);
4177 : :
4178 : 0 : pq_getmsgend(&buf);
4179 : :
4180 : 0 : PG_RETURN_POINTER(result);
4181 : : }
4182 : :
4183 : : /*
4184 : : * Inverse transition function for sum() and avg() interval aggregates.
4185 : : */
4186 : : Datum
662 dean.a.rasheed@gmail 4187 :CBC 132 : interval_avg_accum_inv(PG_FUNCTION_ARGS)
4188 : : {
4189 : : IntervalAggState *state;
4190 : :
4191 [ + - ]: 132 : state = PG_ARGISNULL(0) ? NULL : (IntervalAggState *) PG_GETARG_POINTER(0);
4192 : :
4193 : : /* Should not get here with no state */
4194 [ - + ]: 132 : if (state == NULL)
662 dean.a.rasheed@gmail 4195 [ # # ]:UBC 0 : elog(ERROR, "interval_avg_accum_inv called with NULL state");
4196 : :
662 dean.a.rasheed@gmail 4197 [ + + ]:CBC 132 : if (!PG_ARGISNULL(1))
4198 : 102 : do_interval_discard(state, PG_GETARG_INTERVAL_P(1));
4199 : :
4200 : 132 : PG_RETURN_POINTER(state);
4201 : : }
4202 : :
4203 : : /* avg(interval) aggregate final function */
4204 : : Datum
4205 : 84 : interval_avg(PG_FUNCTION_ARGS)
4206 : : {
4207 : : IntervalAggState *state;
4208 : :
4209 [ + - ]: 84 : state = PG_ARGISNULL(0) ? NULL : (IntervalAggState *) PG_GETARG_POINTER(0);
4210 : :
4211 : : /* If there were no non-null inputs, return NULL */
4212 [ + - + + ]: 84 : if (state == NULL || IA_TOTAL_COUNT(state) == 0)
9182 tgl@sss.pgh.pa.us 4213 : 9 : PG_RETURN_NULL();
4214 : :
4215 : : /*
4216 : : * Aggregating infinities that all have the same sign produces infinity
4217 : : * with that sign. Aggregating infinities with different signs results in
4218 : : * an error.
4219 : : */
662 dean.a.rasheed@gmail 4220 [ + + + + ]: 75 : if (state->pInfcount > 0 || state->nInfcount > 0)
4221 : : {
4222 : : Interval *result;
4223 : :
4224 [ + + + + ]: 54 : if (state->pInfcount > 0 && state->nInfcount > 0)
4225 [ + - ]: 3 : ereport(ERROR,
4226 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4227 : : errmsg("interval out of range")));
4228 : :
4229 : 51 : result = (Interval *) palloc(sizeof(Interval));
4230 [ + + ]: 51 : if (state->pInfcount > 0)
4231 : 30 : INTERVAL_NOEND(result);
4232 : : else
4233 : 21 : INTERVAL_NOBEGIN(result);
4234 : :
4235 : 51 : PG_RETURN_INTERVAL_P(result);
4236 : : }
4237 : :
9182 tgl@sss.pgh.pa.us 4238 : 21 : return DirectFunctionCall2(interval_div,
4239 : : IntervalPGetDatum(&state->sumX),
4240 : : Float8GetDatum((double) state->N));
4241 : : }
4242 : :
4243 : : /* sum(interval) aggregate final function */
4244 : : Datum
662 dean.a.rasheed@gmail 4245 : 81 : interval_sum(PG_FUNCTION_ARGS)
4246 : : {
4247 : : IntervalAggState *state;
4248 : : Interval *result;
4249 : :
4250 [ + - ]: 81 : state = PG_ARGISNULL(0) ? NULL : (IntervalAggState *) PG_GETARG_POINTER(0);
4251 : :
4252 : : /* If there were no non-null inputs, return NULL */
4253 [ + - + + ]: 81 : if (state == NULL || IA_TOTAL_COUNT(state) == 0)
4254 : 9 : PG_RETURN_NULL();
4255 : :
4256 : : /*
4257 : : * Aggregating infinities that all have the same sign produces infinity
4258 : : * with that sign. Aggregating infinities with different signs results in
4259 : : * an error.
4260 : : */
4261 [ + + + + ]: 72 : if (state->pInfcount > 0 && state->nInfcount > 0)
4262 [ + - ]: 3 : ereport(ERROR,
4263 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4264 : : errmsg("interval out of range")));
4265 : :
4266 : 69 : result = (Interval *) palloc(sizeof(Interval));
4267 : :
4268 [ + + ]: 69 : if (state->pInfcount > 0)
4269 : 30 : INTERVAL_NOEND(result);
4270 [ + + ]: 39 : else if (state->nInfcount > 0)
4271 : 21 : INTERVAL_NOBEGIN(result);
4272 : : else
4273 : 18 : memcpy(result, &state->sumX, sizeof(Interval));
4274 : :
4275 : 69 : PG_RETURN_INTERVAL_P(result);
4276 : : }
4277 : :
4278 : : /* timestamp_age()
4279 : : * Calculate time difference while retaining year/month fields.
4280 : : * Note that this does not result in an accurate absolute time span
4281 : : * since year and month are out of context once the arithmetic
4282 : : * is done.
4283 : : */
4284 : : Datum
9220 tgl@sss.pgh.pa.us 4285 : 18 : timestamp_age(PG_FUNCTION_ARGS)
4286 : : {
4287 : 18 : Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
4288 : 18 : Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
4289 : : Interval *result;
4290 : : fsec_t fsec1,
4291 : : fsec2;
4292 : : struct pg_itm tt,
9334 lockhart@fourpalms.o 4293 : 18 : *tm = &tt;
4294 : : struct pg_tm tt1,
4295 : 18 : *tm1 = &tt1;
4296 : : struct pg_tm tt2,
4297 : 18 : *tm2 = &tt2;
4298 : :
9220 tgl@sss.pgh.pa.us 4299 : 18 : result = (Interval *) palloc(sizeof(Interval));
4300 : :
4301 : : /*
4302 : : * Handle infinities.
4303 : : *
4304 : : * We treat anything that amounts to "infinity - infinity" as an error,
4305 : : * since the interval type has nothing equivalent to NaN.
4306 : : */
662 dean.a.rasheed@gmail 4307 [ + + ]: 18 : if (TIMESTAMP_IS_NOBEGIN(dt1))
4308 : : {
4309 [ + + ]: 6 : if (TIMESTAMP_IS_NOBEGIN(dt2))
4310 [ + - ]: 3 : ereport(ERROR,
4311 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4312 : : errmsg("interval out of range")));
4313 : : else
4314 : 3 : INTERVAL_NOBEGIN(result);
4315 : : }
4316 [ + + ]: 12 : else if (TIMESTAMP_IS_NOEND(dt1))
4317 : : {
4318 [ + + ]: 6 : if (TIMESTAMP_IS_NOEND(dt2))
4319 [ + - ]: 3 : ereport(ERROR,
4320 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4321 : : errmsg("interval out of range")));
4322 : : else
4323 : 3 : INTERVAL_NOEND(result);
4324 : : }
4325 [ + + ]: 6 : else if (TIMESTAMP_IS_NOBEGIN(dt2))
4326 : 3 : INTERVAL_NOEND(result);
4327 [ + - ]: 3 : else if (TIMESTAMP_IS_NOEND(dt2))
4328 : 3 : INTERVAL_NOBEGIN(result);
662 dean.a.rasheed@gmail 4329 [ # # # # ]:UBC 0 : else if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
4330 : 0 : timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
4331 : : {
4332 : : /* form the symbolic difference */
1253 tgl@sss.pgh.pa.us 4333 : 0 : tm->tm_usec = fsec1 - fsec2;
7410 bruce@momjian.us 4334 : 0 : tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
4335 : 0 : tm->tm_min = tm1->tm_min - tm2->tm_min;
4336 : 0 : tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
4337 : 0 : tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
4338 : 0 : tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
4339 : 0 : tm->tm_year = tm1->tm_year - tm2->tm_year;
4340 : :
4341 : : /* flip sign if necessary... */
9334 lockhart@fourpalms.o 4342 [ # # ]: 0 : if (dt1 < dt2)
4343 : : {
1253 tgl@sss.pgh.pa.us 4344 : 0 : tm->tm_usec = -tm->tm_usec;
9334 lockhart@fourpalms.o 4345 : 0 : tm->tm_sec = -tm->tm_sec;
4346 : 0 : tm->tm_min = -tm->tm_min;
4347 : 0 : tm->tm_hour = -tm->tm_hour;
4348 : 0 : tm->tm_mday = -tm->tm_mday;
4349 : 0 : tm->tm_mon = -tm->tm_mon;
4350 : 0 : tm->tm_year = -tm->tm_year;
4351 : : }
4352 : :
4353 : : /* propagate any negative fields into the next higher field */
1253 tgl@sss.pgh.pa.us 4354 [ # # ]: 0 : while (tm->tm_usec < 0)
4355 : : {
4356 : 0 : tm->tm_usec += USECS_PER_SEC;
6625 bruce@momjian.us 4357 : 0 : tm->tm_sec--;
4358 : : }
4359 : :
7584 tgl@sss.pgh.pa.us 4360 [ # # ]: 0 : while (tm->tm_sec < 0)
4361 : : {
7352 bruce@momjian.us 4362 : 0 : tm->tm_sec += SECS_PER_MINUTE;
9334 lockhart@fourpalms.o 4363 : 0 : tm->tm_min--;
4364 : : }
4365 : :
7584 tgl@sss.pgh.pa.us 4366 [ # # ]: 0 : while (tm->tm_min < 0)
4367 : : {
7352 bruce@momjian.us 4368 : 0 : tm->tm_min += MINS_PER_HOUR;
9334 lockhart@fourpalms.o 4369 : 0 : tm->tm_hour--;
4370 : : }
4371 : :
7584 tgl@sss.pgh.pa.us 4372 [ # # ]: 0 : while (tm->tm_hour < 0)
4373 : : {
7352 bruce@momjian.us 4374 : 0 : tm->tm_hour += HOURS_PER_DAY;
9334 lockhart@fourpalms.o 4375 : 0 : tm->tm_mday--;
4376 : : }
4377 : :
7584 tgl@sss.pgh.pa.us 4378 [ # # ]: 0 : while (tm->tm_mday < 0)
4379 : : {
9334 lockhart@fourpalms.o 4380 [ # # ]: 0 : if (dt1 < dt2)
4381 : : {
4382 [ # # # # : 0 : tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
# # ]
4383 : 0 : tm->tm_mon--;
4384 : : }
4385 : : else
4386 : : {
4387 [ # # # # : 0 : tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
# # ]
4388 : 0 : tm->tm_mon--;
4389 : : }
4390 : : }
4391 : :
7584 tgl@sss.pgh.pa.us 4392 [ # # ]: 0 : while (tm->tm_mon < 0)
4393 : : {
7352 bruce@momjian.us 4394 : 0 : tm->tm_mon += MONTHS_PER_YEAR;
9334 lockhart@fourpalms.o 4395 : 0 : tm->tm_year--;
4396 : : }
4397 : :
4398 : : /* recover sign if necessary... */
4399 [ # # ]: 0 : if (dt1 < dt2)
4400 : : {
1253 tgl@sss.pgh.pa.us 4401 : 0 : tm->tm_usec = -tm->tm_usec;
9334 lockhart@fourpalms.o 4402 : 0 : tm->tm_sec = -tm->tm_sec;
4403 : 0 : tm->tm_min = -tm->tm_min;
4404 : 0 : tm->tm_hour = -tm->tm_hour;
4405 : 0 : tm->tm_mday = -tm->tm_mday;
4406 : 0 : tm->tm_mon = -tm->tm_mon;
4407 : 0 : tm->tm_year = -tm->tm_year;
4408 : : }
4409 : :
1253 tgl@sss.pgh.pa.us 4410 [ # # ]: 0 : if (itm2interval(tm, result) != 0)
8077 4411 [ # # ]: 0 : ereport(ERROR,
4412 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4413 : : errmsg("interval out of range")));
4414 : : }
4415 : : else
4416 [ # # ]: 0 : ereport(ERROR,
4417 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4418 : : errmsg("timestamp out of range")));
4419 : :
9220 tgl@sss.pgh.pa.us 4420 :CBC 12 : PG_RETURN_INTERVAL_P(result);
4421 : : }
4422 : :
4423 : :
4424 : : /* timestamptz_age()
4425 : : * Calculate time difference while retaining year/month fields.
4426 : : * Note that this does not result in an accurate absolute time span
4427 : : * since year and month are out of context once the arithmetic
4428 : : * is done.
4429 : : */
4430 : : Datum
8744 lockhart@fourpalms.o 4431 : 18 : timestamptz_age(PG_FUNCTION_ARGS)
4432 : : {
8153 tgl@sss.pgh.pa.us 4433 : 18 : TimestampTz dt1 = PG_GETARG_TIMESTAMPTZ(0);
4434 : 18 : TimestampTz dt2 = PG_GETARG_TIMESTAMPTZ(1);
4435 : : Interval *result;
4436 : : fsec_t fsec1,
4437 : : fsec2;
4438 : : struct pg_itm tt,
8744 lockhart@fourpalms.o 4439 : 18 : *tm = &tt;
4440 : : struct pg_tm tt1,
4441 : 18 : *tm1 = &tt1;
4442 : : struct pg_tm tt2,
4443 : 18 : *tm2 = &tt2;
4444 : : int tz1;
4445 : : int tz2;
4446 : :
4447 : 18 : result = (Interval *) palloc(sizeof(Interval));
4448 : :
4449 : : /*
4450 : : * Handle infinities.
4451 : : *
4452 : : * We treat anything that amounts to "infinity - infinity" as an error,
4453 : : * since the interval type has nothing equivalent to NaN.
4454 : : */
662 dean.a.rasheed@gmail 4455 [ + + ]: 18 : if (TIMESTAMP_IS_NOBEGIN(dt1))
4456 : : {
4457 [ + + ]: 6 : if (TIMESTAMP_IS_NOBEGIN(dt2))
4458 [ + - ]: 3 : ereport(ERROR,
4459 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4460 : : errmsg("interval out of range")));
4461 : : else
4462 : 3 : INTERVAL_NOBEGIN(result);
4463 : : }
4464 [ + + ]: 12 : else if (TIMESTAMP_IS_NOEND(dt1))
4465 : : {
4466 [ + + ]: 6 : if (TIMESTAMP_IS_NOEND(dt2))
4467 [ + - ]: 3 : ereport(ERROR,
4468 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4469 : : errmsg("interval out of range")));
4470 : : else
4471 : 3 : INTERVAL_NOEND(result);
4472 : : }
4473 [ + + ]: 6 : else if (TIMESTAMP_IS_NOBEGIN(dt2))
4474 : 3 : INTERVAL_NOEND(result);
4475 [ + - ]: 3 : else if (TIMESTAMP_IS_NOEND(dt2))
4476 : 3 : INTERVAL_NOBEGIN(result);
662 dean.a.rasheed@gmail 4477 [ # # # # ]:UBC 0 : else if (timestamp2tm(dt1, &tz1, tm1, &fsec1, NULL, NULL) == 0 &&
4478 : 0 : timestamp2tm(dt2, &tz2, tm2, &fsec2, NULL, NULL) == 0)
4479 : : {
4480 : : /* form the symbolic difference */
1253 tgl@sss.pgh.pa.us 4481 : 0 : tm->tm_usec = fsec1 - fsec2;
7410 bruce@momjian.us 4482 : 0 : tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
4483 : 0 : tm->tm_min = tm1->tm_min - tm2->tm_min;
4484 : 0 : tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
4485 : 0 : tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
4486 : 0 : tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
4487 : 0 : tm->tm_year = tm1->tm_year - tm2->tm_year;
4488 : :
4489 : : /* flip sign if necessary... */
8744 lockhart@fourpalms.o 4490 [ # # ]: 0 : if (dt1 < dt2)
4491 : : {
1253 tgl@sss.pgh.pa.us 4492 : 0 : tm->tm_usec = -tm->tm_usec;
8744 lockhart@fourpalms.o 4493 : 0 : tm->tm_sec = -tm->tm_sec;
4494 : 0 : tm->tm_min = -tm->tm_min;
4495 : 0 : tm->tm_hour = -tm->tm_hour;
4496 : 0 : tm->tm_mday = -tm->tm_mday;
4497 : 0 : tm->tm_mon = -tm->tm_mon;
4498 : 0 : tm->tm_year = -tm->tm_year;
4499 : : }
4500 : :
4501 : : /* propagate any negative fields into the next higher field */
1253 tgl@sss.pgh.pa.us 4502 [ # # ]: 0 : while (tm->tm_usec < 0)
4503 : : {
4504 : 0 : tm->tm_usec += USECS_PER_SEC;
6625 bruce@momjian.us 4505 : 0 : tm->tm_sec--;
4506 : : }
4507 : :
7584 tgl@sss.pgh.pa.us 4508 [ # # ]: 0 : while (tm->tm_sec < 0)
4509 : : {
7352 bruce@momjian.us 4510 : 0 : tm->tm_sec += SECS_PER_MINUTE;
8744 lockhart@fourpalms.o 4511 : 0 : tm->tm_min--;
4512 : : }
4513 : :
7584 tgl@sss.pgh.pa.us 4514 [ # # ]: 0 : while (tm->tm_min < 0)
4515 : : {
7352 bruce@momjian.us 4516 : 0 : tm->tm_min += MINS_PER_HOUR;
8744 lockhart@fourpalms.o 4517 : 0 : tm->tm_hour--;
4518 : : }
4519 : :
7584 tgl@sss.pgh.pa.us 4520 [ # # ]: 0 : while (tm->tm_hour < 0)
4521 : : {
7352 bruce@momjian.us 4522 : 0 : tm->tm_hour += HOURS_PER_DAY;
8744 lockhart@fourpalms.o 4523 : 0 : tm->tm_mday--;
4524 : : }
4525 : :
7584 tgl@sss.pgh.pa.us 4526 [ # # ]: 0 : while (tm->tm_mday < 0)
4527 : : {
8744 lockhart@fourpalms.o 4528 [ # # ]: 0 : if (dt1 < dt2)
4529 : : {
4530 [ # # # # : 0 : tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
# # ]
4531 : 0 : tm->tm_mon--;
4532 : : }
4533 : : else
4534 : : {
4535 [ # # # # : 0 : tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
# # ]
4536 : 0 : tm->tm_mon--;
4537 : : }
4538 : : }
4539 : :
7584 tgl@sss.pgh.pa.us 4540 [ # # ]: 0 : while (tm->tm_mon < 0)
4541 : : {
7352 bruce@momjian.us 4542 : 0 : tm->tm_mon += MONTHS_PER_YEAR;
8744 lockhart@fourpalms.o 4543 : 0 : tm->tm_year--;
4544 : : }
4545 : :
4546 : : /*
4547 : : * Note: we deliberately ignore any difference between tz1 and tz2.
4548 : : */
4549 : :
4550 : : /* recover sign if necessary... */
4551 [ # # ]: 0 : if (dt1 < dt2)
4552 : : {
1253 tgl@sss.pgh.pa.us 4553 : 0 : tm->tm_usec = -tm->tm_usec;
8744 lockhart@fourpalms.o 4554 : 0 : tm->tm_sec = -tm->tm_sec;
4555 : 0 : tm->tm_min = -tm->tm_min;
4556 : 0 : tm->tm_hour = -tm->tm_hour;
4557 : 0 : tm->tm_mday = -tm->tm_mday;
4558 : 0 : tm->tm_mon = -tm->tm_mon;
4559 : 0 : tm->tm_year = -tm->tm_year;
4560 : : }
4561 : :
1253 tgl@sss.pgh.pa.us 4562 [ # # ]: 0 : if (itm2interval(tm, result) != 0)
8077 4563 [ # # ]: 0 : ereport(ERROR,
4564 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4565 : : errmsg("interval out of range")));
4566 : : }
4567 : : else
4568 [ # # ]: 0 : ereport(ERROR,
4569 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4570 : : errmsg("timestamp out of range")));
4571 : :
8744 lockhart@fourpalms.o 4572 :CBC 12 : PG_RETURN_INTERVAL_P(result);
4573 : : }
4574 : :
4575 : :
4576 : : /*----------------------------------------------------------
4577 : : * Conversion operators.
4578 : : *---------------------------------------------------------*/
4579 : :
4580 : :
4581 : : /* timestamp_bin()
4582 : : * Bin timestamp into specified interval.
4583 : : */
4584 : : Datum
1627 peter@eisentraut.org 4585 : 138 : timestamp_bin(PG_FUNCTION_ARGS)
4586 : : {
4587 : 138 : Interval *stride = PG_GETARG_INTERVAL_P(0);
4588 : 138 : Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
4589 : 138 : Timestamp origin = PG_GETARG_TIMESTAMP(2);
4590 : : Timestamp result,
4591 : : stride_usecs,
4592 : : tm_diff,
4593 : : tm_modulo,
4594 : : tm_delta;
4595 : :
4596 [ + - - + ]: 138 : if (TIMESTAMP_NOT_FINITE(timestamp))
1627 peter@eisentraut.org 4597 :UBC 0 : PG_RETURN_TIMESTAMP(timestamp);
4598 : :
1627 peter@eisentraut.org 4599 [ + - - + ]:CBC 138 : if (TIMESTAMP_NOT_FINITE(origin))
1627 peter@eisentraut.org 4600 [ # # ]:UBC 0 : ereport(ERROR,
4601 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4602 : : errmsg("origin out of range")));
4603 : :
662 dean.a.rasheed@gmail 4604 [ + + + - :CBC 138 : if (INTERVAL_NOT_FINITE(stride))
- + + + +
- + - ]
4605 [ + - ]: 6 : ereport(ERROR,
4606 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4607 : : errmsg("timestamps cannot be binned into infinite intervals")));
4608 : :
1627 peter@eisentraut.org 4609 [ + + ]: 132 : if (stride->month != 0)
4610 [ + - ]: 6 : ereport(ERROR,
4611 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4612 : : errmsg("timestamps cannot be binned into intervals containing months or years")));
4613 : :
556 tgl@sss.pgh.pa.us 4614 [ + + ]: 126 : if (unlikely(pg_mul_s64_overflow(stride->day, USECS_PER_DAY, &stride_usecs)) ||
4615 [ - + ]: 123 : unlikely(pg_add_s64_overflow(stride_usecs, stride->time, &stride_usecs)))
4616 [ + - ]: 3 : ereport(ERROR,
4617 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4618 : : errmsg("interval out of range")));
4619 : :
1501 john.naylor@postgres 4620 [ + + ]: 123 : if (stride_usecs <= 0)
1507 4621 [ + - ]: 6 : ereport(ERROR,
4622 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4623 : : errmsg("stride must be greater than zero")));
4624 : :
556 tgl@sss.pgh.pa.us 4625 [ + + ]: 117 : if (unlikely(pg_sub_s64_overflow(timestamp, origin, &tm_diff)))
4626 [ + - ]: 3 : ereport(ERROR,
4627 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4628 : : errmsg("interval out of range")));
4629 : :
4630 : : /* These calculations cannot overflow */
4631 : 114 : tm_modulo = tm_diff % stride_usecs;
4632 : 114 : tm_delta = tm_diff - tm_modulo;
4633 : 114 : result = origin + tm_delta;
4634 : :
4635 : : /*
4636 : : * We want to round towards -infinity, not 0, when tm_diff is negative and
4637 : : * not a multiple of stride_usecs. This adjustment *can* cause overflow,
4638 : : * since the result might now be out of the range origin .. timestamp.
4639 : : */
4640 [ + + ]: 114 : if (tm_modulo < 0)
4641 : : {
4642 [ + - ]: 39 : if (unlikely(pg_sub_s64_overflow(result, stride_usecs, &result)) ||
4643 [ + + - + ]: 39 : !IS_VALID_TIMESTAMP(result))
4644 [ + - ]: 3 : ereport(ERROR,
4645 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4646 : : errmsg("timestamp out of range")));
4647 : : }
4648 : :
1627 peter@eisentraut.org 4649 : 111 : PG_RETURN_TIMESTAMP(result);
4650 : : }
4651 : :
4652 : : /* timestamp_trunc()
4653 : : * Truncate timestamp to specified units.
4654 : : */
4655 : : Datum
9220 tgl@sss.pgh.pa.us 4656 : 705 : timestamp_trunc(PG_FUNCTION_ARGS)
4657 : : {
6374 4658 : 705 : text *units = PG_GETARG_TEXT_PP(0);
7266 bruce@momjian.us 4659 : 705 : Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
4660 : : Timestamp result;
4661 : : int type,
4662 : : val;
4663 : : char *lowunits;
4664 : : fsec_t fsec;
4665 : : struct pg_tm tt,
9334 lockhart@fourpalms.o 4666 : 705 : *tm = &tt;
4667 : :
6374 tgl@sss.pgh.pa.us 4668 [ - + ]: 705 : lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4669 [ - + - - : 705 : VARSIZE_ANY_EXHDR(units),
- - - - -
+ ]
4670 : : false);
4671 : :
9334 lockhart@fourpalms.o 4672 : 705 : type = DecodeUnits(0, lowunits, &val);
4673 : :
8077 tgl@sss.pgh.pa.us 4674 [ + + ]: 705 : if (type == UNITS)
4675 : : {
253 michael@paquier.xyz 4676 [ + - + + ]: 702 : if (TIMESTAMP_NOT_FINITE(timestamp))
4677 : : {
4678 : : /*
4679 : : * Errors thrown here for invalid units should exactly match those
4680 : : * below, else there will be unexpected discrepancies between
4681 : : * finite- and infinite-input cases.
4682 : : */
4683 [ + + ]: 6 : switch (val)
4684 : : {
4685 : 3 : case DTK_WEEK:
4686 : : case DTK_MILLENNIUM:
4687 : : case DTK_CENTURY:
4688 : : case DTK_DECADE:
4689 : : case DTK_YEAR:
4690 : : case DTK_QUARTER:
4691 : : case DTK_MONTH:
4692 : : case DTK_DAY:
4693 : : case DTK_HOUR:
4694 : : case DTK_MINUTE:
4695 : : case DTK_SECOND:
4696 : : case DTK_MILLISEC:
4697 : : case DTK_MICROSEC:
4698 : 3 : PG_RETURN_TIMESTAMP(timestamp);
4699 : : break;
4700 : 3 : default:
4701 [ + - ]: 3 : ereport(ERROR,
4702 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4703 : : errmsg("unit \"%s\" not supported for type %s",
4704 : : lowunits, format_type_be(TIMESTAMPOID))));
4705 : : result = 0;
4706 : : }
4707 : : }
4708 : :
7352 bruce@momjian.us 4709 [ - + ]: 696 : if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
8077 tgl@sss.pgh.pa.us 4710 [ # # ]:UBC 0 : ereport(ERROR,
4711 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4712 : : errmsg("timestamp out of range")));
4713 : :
8744 lockhart@fourpalms.o 4714 [ + + + - :CBC 696 : switch (val)
- - - + +
+ + + +
+ ]
4715 : : {
7855 bruce@momjian.us 4716 : 15 : case DTK_WEEK:
4717 : : {
4718 : : int woy;
4719 : :
7266 4720 : 15 : woy = date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
4721 : :
4722 : : /*
4723 : : * If it is week 52/53 and the month is January, then the
4724 : : * week must belong to the previous year. Also, some
4725 : : * December dates belong to the next year.
4726 : : */
4727 [ - + - - ]: 15 : if (woy >= 52 && tm->tm_mon == 1)
7266 bruce@momjian.us 4728 :UBC 0 : --tm->tm_year;
7266 bruce@momjian.us 4729 [ - + - - ]:CBC 15 : if (woy <= 1 && tm->tm_mon == MONTHS_PER_YEAR)
7266 bruce@momjian.us 4730 :UBC 0 : ++tm->tm_year;
7266 bruce@momjian.us 4731 :CBC 15 : isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
4732 : 15 : tm->tm_hour = 0;
4733 : 15 : tm->tm_min = 0;
4734 : 15 : tm->tm_sec = 0;
4735 : 15 : fsec = 0;
4736 : 15 : break;
4737 : : }
8744 lockhart@fourpalms.o 4738 : 3 : case DTK_MILLENNIUM:
4739 : : /* see comments in timestamptz_trunc */
7687 bruce@momjian.us 4740 [ + - ]: 3 : if (tm->tm_year > 0)
7678 4741 : 3 : tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
4742 : : else
7678 bruce@momjian.us 4743 :UBC 0 : tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
4744 : : /* FALL THRU */
4745 : : case DTK_CENTURY:
4746 : : /* see comments in timestamptz_trunc */
7687 bruce@momjian.us 4747 [ + - ]:CBC 6 : if (tm->tm_year > 0)
7678 4748 : 6 : tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
4749 : : else
7678 bruce@momjian.us 4750 :UBC 0 : tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
4751 : : /* FALL THRU */
4752 : : case DTK_DECADE:
4753 : : /* see comments in timestamptz_trunc */
7687 bruce@momjian.us 4754 [ + + - + ]:CBC 6 : if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
4755 : : {
7687 bruce@momjian.us 4756 [ # # ]:UBC 0 : if (tm->tm_year > 0)
4757 : 0 : tm->tm_year = (tm->tm_year / 10) * 10;
4758 : : else
7678 4759 : 0 : tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
4760 : : }
4761 : : /* FALL THRU */
4762 : : case DTK_YEAR:
8744 lockhart@fourpalms.o 4763 :CBC 6 : tm->tm_mon = 1;
4764 : : /* FALL THRU */
4765 : 6 : case DTK_QUARTER:
8078 bruce@momjian.us 4766 : 6 : tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
4767 : : /* FALL THRU */
8744 lockhart@fourpalms.o 4768 : 6 : case DTK_MONTH:
4769 : 6 : tm->tm_mday = 1;
4770 : : /* FALL THRU */
4771 : 618 : case DTK_DAY:
4772 : 618 : tm->tm_hour = 0;
4773 : : /* FALL THRU */
4774 : 630 : case DTK_HOUR:
4775 : 630 : tm->tm_min = 0;
4776 : : /* FALL THRU */
4777 : 642 : case DTK_MINUTE:
4778 : 642 : tm->tm_sec = 0;
4779 : : /* FALL THRU */
4780 : 654 : case DTK_SECOND:
4781 : 654 : fsec = 0;
4782 : 654 : break;
4783 : :
4784 : 12 : case DTK_MILLISEC:
7411 bruce@momjian.us 4785 : 12 : fsec = (fsec / 1000) * 1000;
8744 lockhart@fourpalms.o 4786 : 12 : break;
4787 : :
4788 : 12 : case DTK_MICROSEC:
4789 : 12 : break;
4790 : :
4791 : 3 : default:
8077 tgl@sss.pgh.pa.us 4792 [ + - ]: 3 : ereport(ERROR,
4793 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4794 : : errmsg("unit \"%s\" not supported for type %s",
4795 : : lowunits, format_type_be(TIMESTAMPOID))));
4796 : : result = 0;
4797 : : }
4798 : :
8744 lockhart@fourpalms.o 4799 [ - + ]: 693 : if (tm2timestamp(tm, fsec, NULL, &result) != 0)
8077 tgl@sss.pgh.pa.us 4800 [ # # ]:UBC 0 : ereport(ERROR,
4801 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4802 : : errmsg("timestamp out of range")));
4803 : : }
4804 : : else
4805 : : {
8077 tgl@sss.pgh.pa.us 4806 [ + - ]:CBC 3 : ereport(ERROR,
4807 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4808 : : errmsg("unit \"%s\" not recognized for type %s",
4809 : : lowunits, format_type_be(TIMESTAMPOID))));
4810 : : result = 0;
4811 : : }
4812 : :
8744 lockhart@fourpalms.o 4813 : 693 : PG_RETURN_TIMESTAMP(result);
4814 : : }
4815 : :
4816 : : /* timestamptz_bin()
4817 : : * Bin timestamptz into specified interval using specified origin.
4818 : : */
4819 : : Datum
1627 peter@eisentraut.org 4820 : 66 : timestamptz_bin(PG_FUNCTION_ARGS)
4821 : : {
4822 : 66 : Interval *stride = PG_GETARG_INTERVAL_P(0);
4823 : 66 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
1614 4824 : 66 : TimestampTz origin = PG_GETARG_TIMESTAMPTZ(2);
4825 : : TimestampTz result,
4826 : : stride_usecs,
4827 : : tm_diff,
4828 : : tm_modulo,
4829 : : tm_delta;
4830 : :
1627 4831 [ + - - + ]: 66 : if (TIMESTAMP_NOT_FINITE(timestamp))
1627 peter@eisentraut.org 4832 :UBC 0 : PG_RETURN_TIMESTAMPTZ(timestamp);
4833 : :
1627 peter@eisentraut.org 4834 [ + - - + ]:CBC 66 : if (TIMESTAMP_NOT_FINITE(origin))
1627 peter@eisentraut.org 4835 [ # # ]:UBC 0 : ereport(ERROR,
4836 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4837 : : errmsg("origin out of range")));
4838 : :
662 dean.a.rasheed@gmail 4839 [ - + - - :CBC 66 : if (INTERVAL_NOT_FINITE(stride))
- - - + -
- - - ]
662 dean.a.rasheed@gmail 4840 [ # # ]:UBC 0 : ereport(ERROR,
4841 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4842 : : errmsg("timestamps cannot be binned into infinite intervals")));
4843 : :
1627 peter@eisentraut.org 4844 [ + + ]:CBC 66 : if (stride->month != 0)
4845 [ + - ]: 6 : ereport(ERROR,
4846 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4847 : : errmsg("timestamps cannot be binned into intervals containing months or years")));
4848 : :
556 tgl@sss.pgh.pa.us 4849 [ + + ]: 60 : if (unlikely(pg_mul_s64_overflow(stride->day, USECS_PER_DAY, &stride_usecs)) ||
4850 [ - + ]: 57 : unlikely(pg_add_s64_overflow(stride_usecs, stride->time, &stride_usecs)))
4851 [ + - ]: 3 : ereport(ERROR,
4852 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4853 : : errmsg("interval out of range")));
4854 : :
1501 john.naylor@postgres 4855 [ + + ]: 57 : if (stride_usecs <= 0)
1507 4856 [ + - ]: 6 : ereport(ERROR,
4857 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4858 : : errmsg("stride must be greater than zero")));
4859 : :
556 tgl@sss.pgh.pa.us 4860 [ + + ]: 51 : if (unlikely(pg_sub_s64_overflow(timestamp, origin, &tm_diff)))
4861 [ + - ]: 3 : ereport(ERROR,
4862 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4863 : : errmsg("interval out of range")));
4864 : :
4865 : : /* These calculations cannot overflow */
4866 : 48 : tm_modulo = tm_diff % stride_usecs;
4867 : 48 : tm_delta = tm_diff - tm_modulo;
4868 : 48 : result = origin + tm_delta;
4869 : :
4870 : : /*
4871 : : * We want to round towards -infinity, not 0, when tm_diff is negative and
4872 : : * not a multiple of stride_usecs. This adjustment *can* cause overflow,
4873 : : * since the result might now be out of the range origin .. timestamp.
4874 : : */
4875 [ + + ]: 48 : if (tm_modulo < 0)
4876 : : {
4877 [ + - ]: 3 : if (unlikely(pg_sub_s64_overflow(result, stride_usecs, &result)) ||
4878 [ - + - - ]: 3 : !IS_VALID_TIMESTAMP(result))
4879 [ + - ]: 3 : ereport(ERROR,
4880 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4881 : : errmsg("timestamp out of range")));
4882 : : }
4883 : :
1627 peter@eisentraut.org 4884 : 45 : PG_RETURN_TIMESTAMPTZ(result);
4885 : : }
4886 : :
4887 : : /*
4888 : : * Common code for timestamptz_trunc() and timestamptz_trunc_zone().
4889 : : *
4890 : : * tzp identifies the zone to truncate with respect to. We assume
4891 : : * infinite timestamps have already been rejected.
4892 : : */
4893 : : static TimestampTz
2488 tgl@sss.pgh.pa.us 4894 : 675 : timestamptz_trunc_internal(text *units, TimestampTz timestamp, pg_tz *tzp)
4895 : : {
4896 : : TimestampTz result;
4897 : : int tz;
4898 : : int type,
4899 : : val;
7614 4900 : 675 : bool redotz = false;
4901 : : char *lowunits;
4902 : : fsec_t fsec;
4903 : : struct pg_tm tt,
8744 lockhart@fourpalms.o 4904 : 675 : *tm = &tt;
4905 : :
6374 tgl@sss.pgh.pa.us 4906 [ - + ]: 675 : lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4907 [ - + - - : 675 : VARSIZE_ANY_EXHDR(units),
- - - - -
+ ]
4908 : : false);
4909 : :
7792 4910 : 675 : type = DecodeUnits(0, lowunits, &val);
4911 : :
8077 4912 [ + + ]: 675 : if (type == UNITS)
4913 : : {
253 michael@paquier.xyz 4914 [ + - + + ]: 669 : if (TIMESTAMP_NOT_FINITE(timestamp))
4915 : : {
4916 : : /*
4917 : : * Errors thrown here for invalid units should exactly match those
4918 : : * below, else there will be unexpected discrepancies between
4919 : : * finite- and infinite-input cases.
4920 : : */
4921 [ + + ]: 12 : switch (val)
4922 : : {
4923 : 6 : case DTK_WEEK:
4924 : : case DTK_MILLENNIUM:
4925 : : case DTK_CENTURY:
4926 : : case DTK_DECADE:
4927 : : case DTK_YEAR:
4928 : : case DTK_QUARTER:
4929 : : case DTK_MONTH:
4930 : : case DTK_DAY:
4931 : : case DTK_HOUR:
4932 : : case DTK_MINUTE:
4933 : : case DTK_SECOND:
4934 : : case DTK_MILLISEC:
4935 : : case DTK_MICROSEC:
30 4936 : 6 : return timestamp;
4937 : : break;
4938 : :
253 4939 : 6 : default:
4940 [ + - ]: 6 : ereport(ERROR,
4941 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4942 : : errmsg("unit \"%s\" not supported for type %s",
4943 : : lowunits, format_type_be(TIMESTAMPTZOID))));
4944 : : result = 0;
4945 : : }
4946 : : }
4947 : :
2488 tgl@sss.pgh.pa.us 4948 [ - + ]: 657 : if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, tzp) != 0)
8077 tgl@sss.pgh.pa.us 4949 [ # # ]:UBC 0 : ereport(ERROR,
4950 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4951 : : errmsg("timestamp out of range")));
4952 : :
8744 lockhart@fourpalms.o 4953 [ + + + + :CBC 657 : switch (val)
- - - + +
+ + + +
+ ]
4954 : : {
7855 bruce@momjian.us 4955 : 3 : case DTK_WEEK:
4956 : : {
4957 : : int woy;
4958 : :
7266 4959 : 3 : woy = date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
4960 : :
4961 : : /*
4962 : : * If it is week 52/53 and the month is January, then the
4963 : : * week must belong to the previous year. Also, some
4964 : : * December dates belong to the next year.
4965 : : */
4966 [ - + - - ]: 3 : if (woy >= 52 && tm->tm_mon == 1)
7266 bruce@momjian.us 4967 :UBC 0 : --tm->tm_year;
7266 bruce@momjian.us 4968 [ - + - - ]:CBC 3 : if (woy <= 1 && tm->tm_mon == MONTHS_PER_YEAR)
7266 bruce@momjian.us 4969 :UBC 0 : ++tm->tm_year;
7266 bruce@momjian.us 4970 :CBC 3 : isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
4971 : 3 : tm->tm_hour = 0;
4972 : 3 : tm->tm_min = 0;
4973 : 3 : tm->tm_sec = 0;
4974 : 3 : fsec = 0;
4975 : 3 : redotz = true;
4976 : 3 : break;
4977 : : }
4978 : : /* one may consider DTK_THOUSAND and DTK_HUNDRED... */
8744 lockhart@fourpalms.o 4979 : 3 : case DTK_MILLENNIUM:
4980 : :
4981 : : /*
4982 : : * truncating to the millennium? what is this supposed to
4983 : : * mean? let us put the first year of the millennium... i.e.
4984 : : * -1000, 1, 1001, 2001...
4985 : : */
7687 bruce@momjian.us 4986 [ + - ]: 3 : if (tm->tm_year > 0)
7678 4987 : 3 : tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
4988 : : else
7678 bruce@momjian.us 4989 :UBC 0 : tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
4990 : : /* FALL THRU */
4991 : : case DTK_CENTURY:
4992 : : /* truncating to the century? as above: -100, 1, 101... */
7687 bruce@momjian.us 4993 [ + + ]:CBC 15 : if (tm->tm_year > 0)
7678 4994 : 12 : tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
4995 : : else
4996 : 3 : tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
4997 : : /* FALL THRU */
4998 : : case DTK_DECADE:
4999 : :
5000 : : /*
5001 : : * truncating to the decade? first year of the decade. must
5002 : : * not be applied if year was truncated before!
5003 : : */
7687 5004 [ + + + + ]: 24 : if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
5005 : : {
5006 [ + + ]: 9 : if (tm->tm_year > 0)
5007 : 6 : tm->tm_year = (tm->tm_year / 10) * 10;
5008 : : else
7678 5009 : 3 : tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
5010 : : }
5011 : : /* FALL THRU */
5012 : : case DTK_YEAR:
8744 lockhart@fourpalms.o 5013 : 24 : tm->tm_mon = 1;
5014 : : /* FALL THRU */
5015 : 24 : case DTK_QUARTER:
8078 bruce@momjian.us 5016 : 24 : tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
5017 : : /* FALL THRU */
8744 lockhart@fourpalms.o 5018 : 24 : case DTK_MONTH:
5019 : 24 : tm->tm_mday = 1;
5020 : : /* FALL THRU */
5021 : 636 : case DTK_DAY:
5022 : 636 : tm->tm_hour = 0;
7614 tgl@sss.pgh.pa.us 5023 : 636 : redotz = true; /* for all cases >= DAY */
5024 : : /* FALL THRU */
8744 lockhart@fourpalms.o 5025 : 639 : case DTK_HOUR:
5026 : 639 : tm->tm_min = 0;
5027 : : /* FALL THRU */
5028 : 642 : case DTK_MINUTE:
5029 : 642 : tm->tm_sec = 0;
5030 : : /* FALL THRU */
5031 : 645 : case DTK_SECOND:
5032 : 645 : fsec = 0;
5033 : 645 : break;
5034 : 3 : case DTK_MILLISEC:
7351 bruce@momjian.us 5035 : 3 : fsec = (fsec / 1000) * 1000;
8744 lockhart@fourpalms.o 5036 : 3 : break;
5037 : 3 : case DTK_MICROSEC:
5038 : 3 : break;
5039 : :
5040 : 3 : default:
8077 tgl@sss.pgh.pa.us 5041 [ + - ]: 3 : ereport(ERROR,
5042 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5043 : : errmsg("unit \"%s\" not supported for type %s",
5044 : : lowunits, format_type_be(TIMESTAMPTZOID))));
5045 : : result = 0;
5046 : : }
5047 : :
7614 5048 [ + + ]: 654 : if (redotz)
2488 5049 : 639 : tz = DetermineTimeZoneOffset(tm, tzp);
5050 : :
8744 lockhart@fourpalms.o 5051 [ - + ]: 654 : if (tm2timestamp(tm, fsec, &tz, &result) != 0)
8077 tgl@sss.pgh.pa.us 5052 [ # # ]:UBC 0 : ereport(ERROR,
5053 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5054 : : errmsg("timestamp out of range")));
5055 : : }
5056 : : else
5057 : : {
8077 tgl@sss.pgh.pa.us 5058 [ + - ]:CBC 6 : ereport(ERROR,
5059 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5060 : : errmsg("unit \"%s\" not recognized for type %s",
5061 : : lowunits, format_type_be(TIMESTAMPTZOID))));
5062 : : result = 0;
5063 : : }
5064 : :
2488 5065 : 654 : return result;
5066 : : }
5067 : :
5068 : : /* timestamptz_trunc()
5069 : : * Truncate timestamptz to specified units in session timezone.
5070 : : */
5071 : : Datum
5072 : 639 : timestamptz_trunc(PG_FUNCTION_ARGS)
5073 : : {
5074 : 639 : text *units = PG_GETARG_TEXT_PP(0);
5075 : 639 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
5076 : : TimestampTz result;
5077 : :
5078 : 639 : result = timestamptz_trunc_internal(units, timestamp, session_timezone);
5079 : :
5080 : 630 : PG_RETURN_TIMESTAMPTZ(result);
5081 : : }
5082 : :
5083 : : /* timestamptz_trunc_zone()
5084 : : * Truncate timestamptz to specified units in specified timezone.
5085 : : */
5086 : : Datum
5087 : 36 : timestamptz_trunc_zone(PG_FUNCTION_ARGS)
5088 : : {
5089 : 36 : text *units = PG_GETARG_TEXT_PP(0);
5090 : 36 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
5091 : 36 : text *zone = PG_GETARG_TEXT_PP(2);
5092 : : TimestampTz result;
5093 : : pg_tz *tzp;
5094 : :
5095 : : /*
5096 : : * Look up the requested timezone.
5097 : : */
903 5098 : 36 : tzp = lookup_timezone(zone);
5099 : :
2488 5100 : 36 : result = timestamptz_trunc_internal(units, timestamp, tzp);
5101 : :
8744 lockhart@fourpalms.o 5102 : 30 : PG_RETURN_TIMESTAMPTZ(result);
5103 : : }
5104 : :
5105 : : /* interval_trunc()
5106 : : * Extract specified field from interval.
5107 : : */
5108 : : Datum
9220 tgl@sss.pgh.pa.us 5109 : 12 : interval_trunc(PG_FUNCTION_ARGS)
5110 : : {
6374 5111 : 12 : text *units = PG_GETARG_TEXT_PP(0);
9220 5112 : 12 : Interval *interval = PG_GETARG_INTERVAL_P(1);
5113 : : Interval *result;
5114 : : int type,
5115 : : val;
5116 : : char *lowunits;
5117 : : struct pg_itm tt,
9334 lockhart@fourpalms.o 5118 : 12 : *tm = &tt;
5119 : :
9220 tgl@sss.pgh.pa.us 5120 : 12 : result = (Interval *) palloc(sizeof(Interval));
5121 : :
6374 5122 [ - + ]: 12 : lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
5123 [ - + - - : 12 : VARSIZE_ANY_EXHDR(units),
- - - - -
+ ]
5124 : : false);
5125 : :
9334 lockhart@fourpalms.o 5126 : 12 : type = DecodeUnits(0, lowunits, &val);
5127 : :
8744 5128 [ + + ]: 12 : if (type == UNITS)
5129 : : {
253 michael@paquier.xyz 5130 [ + + + - : 9 : if (INTERVAL_NOT_FINITE(interval))
- + + - +
- + - ]
5131 : : {
5132 : : /*
5133 : : * Errors thrown here for invalid units should exactly match those
5134 : : * below, else there will be unexpected discrepancies between
5135 : : * finite- and infinite-input cases.
5136 : : */
5137 [ + + ]: 9 : switch (val)
5138 : : {
5139 : 6 : case DTK_MILLENNIUM:
5140 : : case DTK_CENTURY:
5141 : : case DTK_DECADE:
5142 : : case DTK_YEAR:
5143 : : case DTK_QUARTER:
5144 : : case DTK_MONTH:
5145 : : case DTK_DAY:
5146 : : case DTK_HOUR:
5147 : : case DTK_MINUTE:
5148 : : case DTK_SECOND:
5149 : : case DTK_MILLISEC:
5150 : : case DTK_MICROSEC:
5151 : 6 : memcpy(result, interval, sizeof(Interval));
5152 : 6 : PG_RETURN_INTERVAL_P(result);
5153 : : break;
5154 : :
5155 : 3 : default:
5156 [ + - + - ]: 3 : ereport(ERROR,
5157 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5158 : : errmsg("unit \"%s\" not supported for type %s",
5159 : : lowunits, format_type_be(INTERVALOID)),
5160 : : (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
5161 : : result = 0;
5162 : : }
5163 : : }
5164 : :
1253 tgl@sss.pgh.pa.us 5165 :UBC 0 : interval2itm(*interval, tm);
1213 5166 [ # # # # : 0 : switch (val)
# # # # #
# # # # ]
5167 : : {
5168 : 0 : case DTK_MILLENNIUM:
5169 : : /* caution: C division may have negative remainder */
5170 : 0 : tm->tm_year = (tm->tm_year / 1000) * 1000;
5171 : : /* FALL THRU */
5172 : 0 : case DTK_CENTURY:
5173 : : /* caution: C division may have negative remainder */
5174 : 0 : tm->tm_year = (tm->tm_year / 100) * 100;
5175 : : /* FALL THRU */
5176 : 0 : case DTK_DECADE:
5177 : : /* caution: C division may have negative remainder */
5178 : 0 : tm->tm_year = (tm->tm_year / 10) * 10;
5179 : : /* FALL THRU */
5180 : 0 : case DTK_YEAR:
5181 : 0 : tm->tm_mon = 0;
5182 : : /* FALL THRU */
5183 : 0 : case DTK_QUARTER:
5184 : 0 : tm->tm_mon = 3 * (tm->tm_mon / 3);
5185 : : /* FALL THRU */
5186 : 0 : case DTK_MONTH:
5187 : 0 : tm->tm_mday = 0;
5188 : : /* FALL THRU */
5189 : 0 : case DTK_DAY:
5190 : 0 : tm->tm_hour = 0;
5191 : : /* FALL THRU */
5192 : 0 : case DTK_HOUR:
5193 : 0 : tm->tm_min = 0;
5194 : : /* FALL THRU */
5195 : 0 : case DTK_MINUTE:
5196 : 0 : tm->tm_sec = 0;
5197 : : /* FALL THRU */
5198 : 0 : case DTK_SECOND:
5199 : 0 : tm->tm_usec = 0;
5200 : 0 : break;
5201 : 0 : case DTK_MILLISEC:
5202 : 0 : tm->tm_usec = (tm->tm_usec / 1000) * 1000;
5203 : 0 : break;
5204 : 0 : case DTK_MICROSEC:
5205 : 0 : break;
5206 : :
5207 : 0 : default:
8077 5208 [ # # # # ]: 0 : ereport(ERROR,
5209 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5210 : : errmsg("unit \"%s\" not supported for type %s",
5211 : : lowunits, format_type_be(INTERVALOID)),
5212 : : (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
5213 : : }
5214 : :
1213 5215 [ # # ]: 0 : if (itm2interval(tm, result) != 0)
5216 [ # # ]: 0 : ereport(ERROR,
5217 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5218 : : errmsg("interval out of range")));
5219 : : }
5220 : : else
5221 : : {
8077 tgl@sss.pgh.pa.us 5222 [ + - ]:CBC 3 : ereport(ERROR,
5223 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5224 : : errmsg("unit \"%s\" not recognized for type %s",
5225 : : lowunits, format_type_be(INTERVALOID))));
5226 : : }
5227 : :
9220 tgl@sss.pgh.pa.us 5228 :UBC 0 : PG_RETURN_INTERVAL_P(result);
5229 : : }
5230 : :
5231 : : /* isoweek2j()
5232 : : *
5233 : : * Return the Julian day which corresponds to the first day (Monday) of the given ISO 8601 year and week.
5234 : : * Julian days are used to convert between ISO week dates and Gregorian dates.
5235 : : *
5236 : : * XXX: This function has integer overflow hazards, but restructuring it to
5237 : : * work with the soft-error handling that its callers do is likely more
5238 : : * trouble than it's worth.
5239 : : */
5240 : : int
6777 bruce@momjian.us 5241 :CBC 795 : isoweek2j(int year, int week)
5242 : : {
5243 : : int day0,
5244 : : day4;
5245 : :
5246 : : /* fourth day of current year */
5247 : 795 : day4 = date2j(year, 1, 4);
5248 : :
5249 : : /* day0 == offset to first day of week (Monday) */
8191 tgl@sss.pgh.pa.us 5250 : 795 : day0 = j2day(day4 - 1);
5251 : :
6777 bruce@momjian.us 5252 : 795 : return ((week - 1) * 7) + (day4 - day0);
5253 : : }
5254 : :
5255 : : /* isoweek2date()
5256 : : * Convert ISO week of year number to date.
5257 : : * The year field must be specified with the ISO year!
5258 : : * karel 2000/08/07
5259 : : */
5260 : : void
5261 : 18 : isoweek2date(int woy, int *year, int *mon, int *mday)
5262 : : {
5263 : 18 : j2date(isoweek2j(*year, woy), year, mon, mday);
5264 : 18 : }
5265 : :
5266 : : /* isoweekdate2date()
5267 : : *
5268 : : * Convert an ISO 8601 week date (ISO year, ISO week) into a Gregorian date.
5269 : : * Gregorian day of week sent so weekday strings can be supplied.
5270 : : * Populates year, mon, and mday with the correct Gregorian values.
5271 : : * year must be passed in as the ISO year.
5272 : : */
5273 : : void
4751 5274 : 12 : isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
5275 : : {
5276 : : int jday;
5277 : :
6777 5278 : 12 : jday = isoweek2j(*year, isoweek);
5279 : : /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4751 5280 [ - + ]: 12 : if (wday > 1)
4751 bruce@momjian.us 5281 :UBC 0 : jday += wday - 2;
5282 : : else
4751 bruce@momjian.us 5283 :CBC 12 : jday += 6;
6777 5284 : 12 : j2date(jday, year, mon, mday);
9139 5285 : 12 : }
5286 : :
5287 : : /* date2isoweek()
5288 : : *
5289 : : * Returns ISO week number of year.
5290 : : */
5291 : : int
8934 5292 : 1212 : date2isoweek(int year, int mon, int mday)
5293 : : {
5294 : : int day0,
5295 : : day4,
5296 : : dayn,
5297 : : week;
5298 : :
5299 : : /* current day */
9139 5300 : 1212 : dayn = date2j(year, mon, mday);
5301 : :
5302 : : /* fourth day of current year */
5303 : 1212 : day4 = date2j(year, 1, 4);
5304 : :
5305 : : /* day0 == offset to first day of week (Monday) */
8191 tgl@sss.pgh.pa.us 5306 : 1212 : day0 = j2day(day4 - 1);
5307 : :
5308 : : /*
5309 : : * We need the first week containing a Thursday, otherwise this day falls
5310 : : * into the previous year for purposes of counting weeks
5311 : : */
7411 bruce@momjian.us 5312 [ + + ]: 1212 : if (dayn < day4 - day0)
5313 : : {
8191 tgl@sss.pgh.pa.us 5314 : 18 : day4 = date2j(year - 1, 1, 4);
5315 : :
5316 : : /* day0 == offset to first day of week (Monday) */
5317 : 18 : day0 = j2day(day4 - 1);
5318 : : }
5319 : :
56 tgl@sss.pgh.pa.us 5320 :GNC 1212 : week = (dayn - (day4 - day0)) / 7 + 1;
5321 : :
5322 : : /*
5323 : : * Sometimes the last few days in a year will fall into the first week of
5324 : : * the next year, so check for this.
5325 : : */
5326 [ + + ]: 1212 : if (week >= 52)
5327 : : {
8191 tgl@sss.pgh.pa.us 5328 :CBC 135 : day4 = date2j(year + 1, 1, 4);
5329 : :
5330 : : /* day0 == offset to first day of week (Monday) */
5331 : 135 : day0 = j2day(day4 - 1);
5332 : :
7411 bruce@momjian.us 5333 [ + + ]: 135 : if (dayn >= day4 - day0)
56 tgl@sss.pgh.pa.us 5334 :GNC 81 : week = (dayn - (day4 - day0)) / 7 + 1;
5335 : : }
5336 : :
5337 : 1212 : return week;
5338 : : }
5339 : :
5340 : :
5341 : : /* date2isoyear()
5342 : : *
5343 : : * Returns ISO 8601 year number.
5344 : : * Note: zero or negative results follow the year-zero-exists convention.
5345 : : */
5346 : : int
7926 bruce@momjian.us 5347 :CBC 7293 : date2isoyear(int year, int mon, int mday)
5348 : : {
5349 : : int day0,
5350 : : day4,
5351 : : dayn,
5352 : : week;
5353 : :
5354 : : /* current day */
5355 : 7293 : dayn = date2j(year, mon, mday);
5356 : :
5357 : : /* fourth day of current year */
5358 : 7293 : day4 = date2j(year, 1, 4);
5359 : :
5360 : : /* day0 == offset to first day of week (Monday) */
5361 : 7293 : day0 = j2day(day4 - 1);
5362 : :
5363 : : /*
5364 : : * We need the first week containing a Thursday, otherwise this day falls
5365 : : * into the previous year for purposes of counting weeks
5366 : : */
7411 5367 [ + + ]: 7293 : if (dayn < day4 - day0)
5368 : : {
7926 5369 : 114 : day4 = date2j(year - 1, 1, 4);
5370 : :
5371 : : /* day0 == offset to first day of week (Monday) */
5372 : 114 : day0 = j2day(day4 - 1);
5373 : :
5374 : 114 : year--;
5375 : : }
5376 : :
56 tgl@sss.pgh.pa.us 5377 :GNC 7293 : week = (dayn - (day4 - day0)) / 7 + 1;
5378 : :
5379 : : /*
5380 : : * Sometimes the last few days in a year will fall into the first week of
5381 : : * the next year, so check for this.
5382 : : */
5383 [ + + ]: 7293 : if (week >= 52)
5384 : : {
7926 bruce@momjian.us 5385 :CBC 855 : day4 = date2j(year + 1, 1, 4);
5386 : :
5387 : : /* day0 == offset to first day of week (Monday) */
5388 : 855 : day0 = j2day(day4 - 1);
5389 : :
7411 5390 [ + + ]: 855 : if (dayn >= day4 - day0)
7926 5391 : 513 : year++;
5392 : : }
5393 : :
5394 : 7293 : return year;
5395 : : }
5396 : :
5397 : :
5398 : : /* date2isoyearday()
5399 : : *
5400 : : * Returns the ISO 8601 day-of-year, given a Gregorian year, month and day.
5401 : : * Possible return values are 1 through 371 (364 in non-leap years).
5402 : : */
5403 : : int
6777 5404 : 762 : date2isoyearday(int year, int mon, int mday)
5405 : : {
5406 : 762 : return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
5407 : : }
5408 : :
5409 : : /*
5410 : : * NonFiniteTimestampTzPart
5411 : : *
5412 : : * Used by timestamp_part and timestamptz_part when extracting from infinite
5413 : : * timestamp[tz]. Returns +/-Infinity if that is the appropriate result,
5414 : : * otherwise returns zero (which should be taken as meaning to return NULL).
5415 : : *
5416 : : * Errors thrown here for invalid units should exactly match those that
5417 : : * would be thrown in the calling functions, else there will be unexpected
5418 : : * discrepancies between finite- and infinite-input cases.
5419 : : */
5420 : : static float8
3516 tgl@sss.pgh.pa.us 5421 : 306 : NonFiniteTimestampTzPart(int type, int unit, char *lowunits,
5422 : : bool isNegative, bool isTz)
5423 : : {
5424 [ + + - + ]: 306 : if ((type != UNITS) && (type != RESERV))
1342 tgl@sss.pgh.pa.us 5425 [ # # # # ]:UBC 0 : ereport(ERROR,
5426 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5427 : : errmsg("unit \"%s\" not recognized for type %s",
5428 : : lowunits,
5429 : : format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
5430 : :
3516 tgl@sss.pgh.pa.us 5431 [ + + - ]:CBC 306 : switch (unit)
5432 : : {
5433 : : /* Oscillating units */
5434 : 198 : case DTK_MICROSEC:
5435 : : case DTK_MILLISEC:
5436 : : case DTK_SECOND:
5437 : : case DTK_MINUTE:
5438 : : case DTK_HOUR:
5439 : : case DTK_DAY:
5440 : : case DTK_MONTH:
5441 : : case DTK_QUARTER:
5442 : : case DTK_WEEK:
5443 : : case DTK_DOW:
5444 : : case DTK_ISODOW:
5445 : : case DTK_DOY:
5446 : : case DTK_TZ:
5447 : : case DTK_TZ_MINUTE:
5448 : : case DTK_TZ_HOUR:
5449 : 198 : return 0.0;
5450 : :
5451 : : /* Monotonically-increasing units */
5452 : 108 : case DTK_YEAR:
5453 : : case DTK_DECADE:
5454 : : case DTK_CENTURY:
5455 : : case DTK_MILLENNIUM:
5456 : : case DTK_JULIAN:
5457 : : case DTK_ISOYEAR:
5458 : : case DTK_EPOCH:
5459 [ + + ]: 108 : if (isNegative)
5460 : 54 : return -get_float8_infinity();
5461 : : else
5462 : 54 : return get_float8_infinity();
5463 : :
3516 tgl@sss.pgh.pa.us 5464 :UBC 0 : default:
1342 5465 [ # # # # ]: 0 : ereport(ERROR,
5466 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5467 : : errmsg("unit \"%s\" not supported for type %s",
5468 : : lowunits,
5469 : : format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
5470 : : return 0.0; /* keep compiler quiet */
5471 : : }
5472 : : }
5473 : :
5474 : : /* timestamp_part() and extract_timestamp()
5475 : : * Extract specified field from timestamp.
5476 : : */
5477 : : static Datum
1614 peter@eisentraut.org 5478 :CBC 5361 : timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
5479 : : {
6374 tgl@sss.pgh.pa.us 5480 : 5361 : text *units = PG_GETARG_TEXT_PP(0);
7266 bruce@momjian.us 5481 : 5361 : Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
5482 : : int64 intresult;
5483 : : Timestamp epoch;
5484 : : int type,
5485 : : val;
5486 : : char *lowunits;
5487 : : fsec_t fsec;
5488 : : struct pg_tm tt,
9334 lockhart@fourpalms.o 5489 : 5361 : *tm = &tt;
5490 : :
6374 tgl@sss.pgh.pa.us 5491 [ - + ]: 5361 : lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
5492 [ - + - - : 5361 : VARSIZE_ANY_EXHDR(units),
- - - - -
+ ]
5493 : : false);
5494 : :
7792 5495 : 5361 : type = DecodeUnits(0, lowunits, &val);
5496 [ + + ]: 5361 : if (type == UNKNOWN_FIELD)
5497 : 1857 : type = DecodeSpecial(0, lowunits, &val);
5498 : :
3516 5499 [ + + + + ]: 5361 : if (TIMESTAMP_NOT_FINITE(timestamp))
5500 : : {
1614 peter@eisentraut.org 5501 : 144 : double r = NonFiniteTimestampTzPart(type, val, lowunits,
5502 : : TIMESTAMP_IS_NOBEGIN(timestamp),
5503 : : false);
5504 : :
662 dean.a.rasheed@gmail 5505 [ + + ]: 144 : if (r != 0.0)
5506 : : {
1614 peter@eisentraut.org 5507 [ + + ]: 54 : if (retnumeric)
5508 : : {
5509 [ + + ]: 12 : if (r < 0)
5510 : 6 : return DirectFunctionCall3(numeric_in,
5511 : : CStringGetDatum("-Infinity"),
5512 : : ObjectIdGetDatum(InvalidOid),
5513 : : Int32GetDatum(-1));
5514 [ + - ]: 6 : else if (r > 0)
5515 : 6 : return DirectFunctionCall3(numeric_in,
5516 : : CStringGetDatum("Infinity"),
5517 : : ObjectIdGetDatum(InvalidOid),
5518 : : Int32GetDatum(-1));
5519 : : }
5520 : : else
5521 : 42 : PG_RETURN_FLOAT8(r);
5522 : : }
5523 : : else
3516 tgl@sss.pgh.pa.us 5524 : 90 : PG_RETURN_NULL();
5525 : : }
5526 : :
8077 5527 [ + + ]: 5217 : if (type == UNITS)
5528 : : {
7352 bruce@momjian.us 5529 [ - + ]: 4782 : if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
8077 tgl@sss.pgh.pa.us 5530 [ # # ]:UBC 0 : ereport(ERROR,
5531 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5532 : : errmsg("timestamp out of range")));
5533 : :
8744 lockhart@fourpalms.o 5534 [ + + + + :CBC 4782 : switch (val)
+ + + + +
+ + + + +
+ + + - ]
5535 : : {
5536 : 378 : case DTK_MICROSEC:
1609 tgl@sss.pgh.pa.us 5537 : 378 : intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
8744 lockhart@fourpalms.o 5538 : 378 : break;
5539 : :
5540 : 378 : case DTK_MILLISEC:
1614 peter@eisentraut.org 5541 [ + + ]: 378 : if (retnumeric)
5542 : : /*---
5543 : : * tm->tm_sec * 1000 + fsec / 1000
5544 : : * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5545 : : */
1609 tgl@sss.pgh.pa.us 5546 : 189 : PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
5547 : : else
1614 peter@eisentraut.org 5548 : 189 : PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
5549 : : break;
5550 : :
8744 lockhart@fourpalms.o 5551 : 378 : case DTK_SECOND:
1614 peter@eisentraut.org 5552 [ + + ]: 378 : if (retnumeric)
5553 : : /*---
5554 : : * tm->tm_sec + fsec / 1'000'000
5555 : : * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5556 : : */
1609 tgl@sss.pgh.pa.us 5557 : 189 : PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
5558 : : else
1614 peter@eisentraut.org 5559 : 189 : PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
5560 : : break;
5561 : :
8744 lockhart@fourpalms.o 5562 : 189 : case DTK_MINUTE:
1614 peter@eisentraut.org 5563 : 189 : intresult = tm->tm_min;
8744 lockhart@fourpalms.o 5564 : 189 : break;
5565 : :
5566 : 189 : case DTK_HOUR:
1614 peter@eisentraut.org 5567 : 189 : intresult = tm->tm_hour;
8744 lockhart@fourpalms.o 5568 : 189 : break;
5569 : :
5570 : 237 : case DTK_DAY:
1614 peter@eisentraut.org 5571 : 237 : intresult = tm->tm_mday;
8744 lockhart@fourpalms.o 5572 : 237 : break;
5573 : :
5574 : 237 : case DTK_MONTH:
1614 peter@eisentraut.org 5575 : 237 : intresult = tm->tm_mon;
8744 lockhart@fourpalms.o 5576 : 237 : break;
5577 : :
5578 : 237 : case DTK_QUARTER:
1614 peter@eisentraut.org 5579 : 237 : intresult = (tm->tm_mon - 1) / 3 + 1;
8744 lockhart@fourpalms.o 5580 : 237 : break;
5581 : :
5582 : 237 : case DTK_WEEK:
1614 peter@eisentraut.org 5583 : 237 : intresult = date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
8744 lockhart@fourpalms.o 5584 : 237 : break;
5585 : :
5586 : 237 : case DTK_YEAR:
7830 bruce@momjian.us 5587 [ + + ]: 237 : if (tm->tm_year > 0)
1614 peter@eisentraut.org 5588 : 231 : intresult = tm->tm_year;
5589 : : else
5590 : : /* there is no year 0, just 1 BC and 1 AD */
5591 : 6 : intresult = tm->tm_year - 1;
8744 lockhart@fourpalms.o 5592 : 237 : break;
5593 : :
5594 : 237 : case DTK_DECADE:
5595 : :
5596 : : /*
5597 : : * what is a decade wrt dates? let us assume that decade 199
5598 : : * is 1990 thru 1999... decade 0 starts on year 1 BC, and -1
5599 : : * is 11 BC thru 2 BC...
5600 : : */
7678 bruce@momjian.us 5601 [ + + ]: 237 : if (tm->tm_year >= 0)
1614 peter@eisentraut.org 5602 : 231 : intresult = tm->tm_year / 10;
5603 : : else
5604 : 6 : intresult = -((8 - (tm->tm_year - 1)) / 10);
8744 lockhart@fourpalms.o 5605 : 237 : break;
5606 : :
5607 : 237 : case DTK_CENTURY:
5608 : :
5609 : : /* ----
5610 : : * centuries AD, c>0: year in [ (c-1)* 100 + 1 : c*100 ]
5611 : : * centuries BC, c<0: year in [ c*100 : (c+1) * 100 - 1]
5612 : : * there is no number 0 century.
5613 : : * ----
5614 : : */
7819 bruce@momjian.us 5615 [ + + ]: 237 : if (tm->tm_year > 0)
1614 peter@eisentraut.org 5616 : 231 : intresult = (tm->tm_year + 99) / 100;
5617 : : else
5618 : : /* caution: C division may have negative remainder */
5619 : 6 : intresult = -((99 - (tm->tm_year - 1)) / 100);
8744 lockhart@fourpalms.o 5620 : 237 : break;
5621 : :
5622 : 237 : case DTK_MILLENNIUM:
5623 : : /* see comments above. */
7819 bruce@momjian.us 5624 [ + + ]: 237 : if (tm->tm_year > 0)
1614 peter@eisentraut.org 5625 : 231 : intresult = (tm->tm_year + 999) / 1000;
5626 : : else
5627 : 6 : intresult = -((999 - (tm->tm_year - 1)) / 1000);
8744 lockhart@fourpalms.o 5628 : 237 : break;
5629 : :
8652 5630 : 426 : case DTK_JULIAN:
1614 peter@eisentraut.org 5631 [ + + ]: 426 : if (retnumeric)
1 michael@paquier.xyz 5632 :GNC 189 : PG_RETURN_NUMERIC(numeric_add_safe(int64_to_numeric(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)),
5633 : : numeric_div_safe(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * INT64CONST(1000000) + fsec),
5634 : : int64_to_numeric(SECS_PER_DAY * INT64CONST(1000000)),
5635 : : NULL),
5636 : : NULL));
5637 : : else
1614 peter@eisentraut.org 5638 :CBC 237 : PG_RETURN_FLOAT8(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) +
5639 : : ((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) +
5640 : : tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY);
5641 : : break;
5642 : :
6777 bruce@momjian.us 5643 : 237 : case DTK_ISOYEAR:
1614 peter@eisentraut.org 5644 : 237 : intresult = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
5645 : : /* Adjust BC years */
5646 [ + + ]: 237 : if (intresult <= 0)
5647 : 6 : intresult -= 1;
6777 bruce@momjian.us 5648 : 237 : break;
5649 : :
3653 stark@mit.edu 5650 : 474 : case DTK_DOW:
5651 : : case DTK_ISODOW:
1614 peter@eisentraut.org 5652 : 474 : intresult = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
5653 [ + + + + ]: 474 : if (val == DTK_ISODOW && intresult == 0)
5654 : 15 : intresult = 7;
3653 stark@mit.edu 5655 : 474 : break;
5656 : :
5657 : 237 : case DTK_DOY:
1614 peter@eisentraut.org 5658 : 237 : intresult = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
5659 : 237 : - date2j(tm->tm_year, 1, 1) + 1);
3653 stark@mit.edu 5660 : 237 : break;
5661 : :
8739 lockhart@fourpalms.o 5662 :UBC 0 : case DTK_TZ:
5663 : : case DTK_TZ_MINUTE:
5664 : : case DTK_TZ_HOUR:
5665 : : default:
8077 tgl@sss.pgh.pa.us 5666 [ # # ]: 0 : ereport(ERROR,
5667 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5668 : : errmsg("unit \"%s\" not supported for type %s",
5669 : : lowunits, format_type_be(TIMESTAMPOID))));
5670 : : intresult = 0;
5671 : : }
5672 : : }
8744 lockhart@fourpalms.o 5673 [ + - ]:CBC 435 : else if (type == RESERV)
5674 : : {
5675 [ + - ]: 435 : switch (val)
5676 : : {
5677 : 435 : case DTK_EPOCH:
3461 tgl@sss.pgh.pa.us 5678 : 435 : epoch = SetEpochTimestamp();
5679 : : /* (timestamp - epoch) / 1000000 */
1614 peter@eisentraut.org 5680 [ + + ]: 435 : if (retnumeric)
5681 : : {
5682 : : Numeric result;
5683 : :
5684 [ + + ]: 195 : if (timestamp < (PG_INT64_MAX + epoch))
5685 : 192 : result = int64_div_fast_to_numeric(timestamp - epoch, 6);
5686 : : else
5687 : : {
1 michael@paquier.xyz 5688 :GNC 3 : result = numeric_div_safe(numeric_sub_safe(int64_to_numeric(timestamp),
5689 : : int64_to_numeric(epoch),
5690 : : NULL),
5691 : : int64_to_numeric(1000000),
5692 : : NULL);
1614 peter@eisentraut.org 5693 :CBC 3 : result = DatumGetNumeric(DirectFunctionCall2(numeric_round,
5694 : : NumericGetDatum(result),
5695 : : Int32GetDatum(6)));
5696 : : }
5697 : 195 : PG_RETURN_NUMERIC(result);
5698 : : }
5699 : : else
5700 : : {
5701 : : float8 result;
5702 : :
5703 : : /* try to avoid precision loss in subtraction */
5704 [ + + ]: 240 : if (timestamp < (PG_INT64_MAX + epoch))
5705 : 237 : result = (timestamp - epoch) / 1000000.0;
5706 : : else
5707 : 3 : result = ((float8) timestamp - epoch) / 1000000.0;
5708 : 240 : PG_RETURN_FLOAT8(result);
5709 : : }
5710 : : break;
5711 : :
8744 lockhart@fourpalms.o 5712 :UBC 0 : default:
8077 tgl@sss.pgh.pa.us 5713 [ # # ]: 0 : ereport(ERROR,
5714 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5715 : : errmsg("unit \"%s\" not supported for type %s",
5716 : : lowunits, format_type_be(TIMESTAMPOID))));
5717 : : intresult = 0;
5718 : : }
5719 : : }
5720 : : else
5721 : : {
5722 [ # # ]: 0 : ereport(ERROR,
5723 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5724 : : errmsg("unit \"%s\" not recognized for type %s",
5725 : : lowunits, format_type_be(TIMESTAMPOID))));
5726 : : intresult = 0;
5727 : : }
5728 : :
1614 peter@eisentraut.org 5729 [ + + ]:CBC 3600 : if (retnumeric)
5730 : 189 : PG_RETURN_NUMERIC(int64_to_numeric(intresult));
5731 : : else
5732 : 3411 : PG_RETURN_FLOAT8(intresult);
5733 : : }
5734 : :
5735 : : Datum
5736 : 4380 : timestamp_part(PG_FUNCTION_ARGS)
5737 : : {
5738 : 4380 : return timestamp_part_common(fcinfo, false);
5739 : : }
5740 : :
5741 : : Datum
5742 : 981 : extract_timestamp(PG_FUNCTION_ARGS)
5743 : : {
5744 : 981 : return timestamp_part_common(fcinfo, true);
5745 : : }
5746 : :
5747 : : /* timestamptz_part() and extract_timestamptz()
5748 : : * Extract specified field from timestamp with time zone.
5749 : : */
5750 : : static Datum
5751 : 18738 : timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
5752 : : {
6374 tgl@sss.pgh.pa.us 5753 : 18738 : text *units = PG_GETARG_TEXT_PP(0);
8153 5754 : 18738 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
5755 : : int64 intresult;
5756 : : Timestamp epoch;
5757 : : int tz;
5758 : : int type,
5759 : : val;
5760 : : char *lowunits;
5761 : : fsec_t fsec;
5762 : : struct pg_tm tt,
8744 lockhart@fourpalms.o 5763 : 18738 : *tm = &tt;
5764 : :
6374 tgl@sss.pgh.pa.us 5765 [ - + ]: 18738 : lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
5766 [ - + - - : 18738 : VARSIZE_ANY_EXHDR(units),
- - - - -
+ ]
5767 : : false);
5768 : :
7792 5769 : 18738 : type = DecodeUnits(0, lowunits, &val);
5770 [ + + ]: 18738 : if (type == UNKNOWN_FIELD)
5771 : 14964 : type = DecodeSpecial(0, lowunits, &val);
5772 : :
3516 5773 [ + + + + ]: 18738 : if (TIMESTAMP_NOT_FINITE(timestamp))
5774 : : {
1614 peter@eisentraut.org 5775 : 162 : double r = NonFiniteTimestampTzPart(type, val, lowunits,
5776 : : TIMESTAMP_IS_NOBEGIN(timestamp),
5777 : : true);
5778 : :
662 dean.a.rasheed@gmail 5779 [ + + ]: 162 : if (r != 0.0)
5780 : : {
1614 peter@eisentraut.org 5781 [ + + ]: 54 : if (retnumeric)
5782 : : {
5783 [ + + ]: 12 : if (r < 0)
5784 : 6 : return DirectFunctionCall3(numeric_in,
5785 : : CStringGetDatum("-Infinity"),
5786 : : ObjectIdGetDatum(InvalidOid),
5787 : : Int32GetDatum(-1));
5788 [ + - ]: 6 : else if (r > 0)
5789 : 6 : return DirectFunctionCall3(numeric_in,
5790 : : CStringGetDatum("Infinity"),
5791 : : ObjectIdGetDatum(InvalidOid),
5792 : : Int32GetDatum(-1));
5793 : : }
5794 : : else
5795 : 42 : PG_RETURN_FLOAT8(r);
5796 : : }
5797 : : else
3516 tgl@sss.pgh.pa.us 5798 : 108 : PG_RETURN_NULL();
5799 : : }
5800 : :
8077 5801 [ + + ]: 18576 : if (type == UNITS)
5802 : : {
4923 peter_e@gmx.net 5803 [ - + ]: 4842 : if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
8077 tgl@sss.pgh.pa.us 5804 [ # # ]:UBC 0 : ereport(ERROR,
5805 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5806 : : errmsg("timestamp out of range")));
5807 : :
8744 lockhart@fourpalms.o 5808 [ + + + + :CBC 4842 : switch (val)
+ + + + +
+ + + + +
+ + + + +
+ - ]
5809 : : {
5810 : 192 : case DTK_TZ:
1614 peter@eisentraut.org 5811 : 192 : intresult = -tz;
8744 lockhart@fourpalms.o 5812 : 192 : break;
5813 : :
5814 : 192 : case DTK_TZ_MINUTE:
1614 peter@eisentraut.org 5815 : 192 : intresult = (-tz / SECS_PER_MINUTE) % MINS_PER_HOUR;
8744 lockhart@fourpalms.o 5816 : 192 : break;
5817 : :
5818 : 192 : case DTK_TZ_HOUR:
1614 peter@eisentraut.org 5819 : 192 : intresult = -tz / SECS_PER_HOUR;
8744 lockhart@fourpalms.o 5820 : 192 : break;
5821 : :
5822 : 384 : case DTK_MICROSEC:
1609 tgl@sss.pgh.pa.us 5823 : 384 : intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
8744 lockhart@fourpalms.o 5824 : 384 : break;
5825 : :
5826 : 384 : case DTK_MILLISEC:
1614 peter@eisentraut.org 5827 [ + + ]: 384 : if (retnumeric)
5828 : : /*---
5829 : : * tm->tm_sec * 1000 + fsec / 1000
5830 : : * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5831 : : */
1609 tgl@sss.pgh.pa.us 5832 : 192 : PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
5833 : : else
1614 peter@eisentraut.org 5834 : 192 : PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
5835 : : break;
5836 : :
8744 lockhart@fourpalms.o 5837 : 384 : case DTK_SECOND:
1614 peter@eisentraut.org 5838 [ + + ]: 384 : if (retnumeric)
5839 : : /*---
5840 : : * tm->tm_sec + fsec / 1'000'000
5841 : : * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5842 : : */
1609 tgl@sss.pgh.pa.us 5843 : 192 : PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
5844 : : else
1614 peter@eisentraut.org 5845 : 192 : PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
5846 : : break;
5847 : :
8744 lockhart@fourpalms.o 5848 : 192 : case DTK_MINUTE:
1614 peter@eisentraut.org 5849 : 192 : intresult = tm->tm_min;
8744 lockhart@fourpalms.o 5850 : 192 : break;
5851 : :
5852 : 192 : case DTK_HOUR:
1614 peter@eisentraut.org 5853 : 192 : intresult = tm->tm_hour;
8744 lockhart@fourpalms.o 5854 : 192 : break;
5855 : :
5856 : 192 : case DTK_DAY:
1614 peter@eisentraut.org 5857 : 192 : intresult = tm->tm_mday;
8744 lockhart@fourpalms.o 5858 : 192 : break;
5859 : :
5860 : 192 : case DTK_MONTH:
1614 peter@eisentraut.org 5861 : 192 : intresult = tm->tm_mon;
8744 lockhart@fourpalms.o 5862 : 192 : break;
5863 : :
5864 : 192 : case DTK_QUARTER:
1614 peter@eisentraut.org 5865 : 192 : intresult = (tm->tm_mon - 1) / 3 + 1;
8744 lockhart@fourpalms.o 5866 : 192 : break;
5867 : :
5868 : 192 : case DTK_WEEK:
1614 peter@eisentraut.org 5869 : 192 : intresult = date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday);
8744 lockhart@fourpalms.o 5870 : 192 : break;
5871 : :
5872 : 204 : case DTK_YEAR:
7595 tgl@sss.pgh.pa.us 5873 [ + + ]: 204 : if (tm->tm_year > 0)
1614 peter@eisentraut.org 5874 : 201 : intresult = tm->tm_year;
5875 : : else
5876 : : /* there is no year 0, just 1 BC and 1 AD */
5877 : 3 : intresult = tm->tm_year - 1;
8744 lockhart@fourpalms.o 5878 : 204 : break;
5879 : :
5880 : 192 : case DTK_DECADE:
5881 : : /* see comments in timestamp_part */
7678 bruce@momjian.us 5882 [ + + ]: 192 : if (tm->tm_year > 0)
1614 peter@eisentraut.org 5883 : 189 : intresult = tm->tm_year / 10;
5884 : : else
5885 : 3 : intresult = -((8 - (tm->tm_year - 1)) / 10);
8744 lockhart@fourpalms.o 5886 : 192 : break;
5887 : :
5888 : 192 : case DTK_CENTURY:
5889 : : /* see comments in timestamp_part */
7687 bruce@momjian.us 5890 [ + + ]: 192 : if (tm->tm_year > 0)
1614 peter@eisentraut.org 5891 : 189 : intresult = (tm->tm_year + 99) / 100;
5892 : : else
5893 : 3 : intresult = -((99 - (tm->tm_year - 1)) / 100);
8744 lockhart@fourpalms.o 5894 : 192 : break;
5895 : :
5896 : 192 : case DTK_MILLENNIUM:
5897 : : /* see comments in timestamp_part */
7687 bruce@momjian.us 5898 [ + + ]: 192 : if (tm->tm_year > 0)
1614 peter@eisentraut.org 5899 : 189 : intresult = (tm->tm_year + 999) / 1000;
5900 : : else
5901 : 3 : intresult = -((999 - (tm->tm_year - 1)) / 1000);
8744 lockhart@fourpalms.o 5902 : 192 : break;
5903 : :
8652 5904 : 384 : case DTK_JULIAN:
1614 peter@eisentraut.org 5905 [ + + ]: 384 : if (retnumeric)
1 michael@paquier.xyz 5906 :GNC 192 : PG_RETURN_NUMERIC(numeric_add_safe(int64_to_numeric(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)),
5907 : : numeric_div_safe(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * INT64CONST(1000000) + fsec),
5908 : : int64_to_numeric(SECS_PER_DAY * INT64CONST(1000000)),
5909 : : NULL),
5910 : : NULL));
5911 : : else
1614 peter@eisentraut.org 5912 :CBC 192 : PG_RETURN_FLOAT8(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) +
5913 : : ((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) +
5914 : : tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY);
5915 : : break;
5916 : :
6777 bruce@momjian.us 5917 : 192 : case DTK_ISOYEAR:
1614 peter@eisentraut.org 5918 : 192 : intresult = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
5919 : : /* Adjust BC years */
5920 [ + + ]: 192 : if (intresult <= 0)
5921 : 3 : intresult -= 1;
6777 bruce@momjian.us 5922 : 192 : break;
5923 : :
3653 stark@mit.edu 5924 : 414 : case DTK_DOW:
5925 : : case DTK_ISODOW:
1614 peter@eisentraut.org 5926 : 414 : intresult = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
5927 [ + + + + ]: 414 : if (val == DTK_ISODOW && intresult == 0)
5928 : 9 : intresult = 7;
3653 stark@mit.edu 5929 : 414 : break;
5930 : :
5931 : 192 : case DTK_DOY:
1614 peter@eisentraut.org 5932 : 192 : intresult = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
5933 : 192 : - date2j(tm->tm_year, 1, 1) + 1);
3653 stark@mit.edu 5934 : 192 : break;
5935 : :
8744 lockhart@fourpalms.o 5936 :UBC 0 : default:
8077 tgl@sss.pgh.pa.us 5937 [ # # ]: 0 : ereport(ERROR,
5938 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5939 : : errmsg("unit \"%s\" not supported for type %s",
5940 : : lowunits, format_type_be(TIMESTAMPTZOID))));
5941 : : intresult = 0;
5942 : : }
5943 : : }
8744 lockhart@fourpalms.o 5944 [ + - ]:CBC 13734 : else if (type == RESERV)
5945 : : {
5946 [ + - ]: 13734 : switch (val)
5947 : : {
5948 : 13734 : case DTK_EPOCH:
3461 tgl@sss.pgh.pa.us 5949 : 13734 : epoch = SetEpochTimestamp();
5950 : : /* (timestamp - epoch) / 1000000 */
1614 peter@eisentraut.org 5951 [ + + ]: 13734 : if (retnumeric)
5952 : : {
5953 : : Numeric result;
5954 : :
5955 [ + + ]: 13539 : if (timestamp < (PG_INT64_MAX + epoch))
5956 : 13536 : result = int64_div_fast_to_numeric(timestamp - epoch, 6);
5957 : : else
5958 : : {
1 michael@paquier.xyz 5959 :GNC 3 : result = numeric_div_safe(numeric_sub_safe(int64_to_numeric(timestamp),
5960 : : int64_to_numeric(epoch),
5961 : : NULL),
5962 : : int64_to_numeric(1000000),
5963 : : NULL);
1614 peter@eisentraut.org 5964 :CBC 3 : result = DatumGetNumeric(DirectFunctionCall2(numeric_round,
5965 : : NumericGetDatum(result),
5966 : : Int32GetDatum(6)));
5967 : : }
5968 : 13539 : PG_RETURN_NUMERIC(result);
5969 : : }
5970 : : else
5971 : : {
5972 : : float8 result;
5973 : :
5974 : : /* try to avoid precision loss in subtraction */
5975 [ + + ]: 195 : if (timestamp < (PG_INT64_MAX + epoch))
5976 : 192 : result = (timestamp - epoch) / 1000000.0;
5977 : : else
5978 : 3 : result = ((float8) timestamp - epoch) / 1000000.0;
5979 : 195 : PG_RETURN_FLOAT8(result);
5980 : : }
5981 : : break;
5982 : :
8744 lockhart@fourpalms.o 5983 :UBC 0 : default:
8077 tgl@sss.pgh.pa.us 5984 [ # # ]: 0 : ereport(ERROR,
5985 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5986 : : errmsg("unit \"%s\" not supported for type %s",
5987 : : lowunits, format_type_be(TIMESTAMPTZOID))));
5988 : : intresult = 0;
5989 : : }
5990 : : }
5991 : : else
5992 : : {
5993 [ # # ]: 0 : ereport(ERROR,
5994 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5995 : : errmsg("unit \"%s\" not recognized for type %s",
5996 : : lowunits, format_type_be(TIMESTAMPTZOID))));
5997 : :
5998 : : intresult = 0;
5999 : : }
6000 : :
1614 peter@eisentraut.org 6001 [ + + ]:CBC 3690 : if (retnumeric)
6002 : 234 : PG_RETURN_NUMERIC(int64_to_numeric(intresult));
6003 : : else
6004 : 3456 : PG_RETURN_FLOAT8(intresult);
6005 : : }
6006 : :
6007 : : Datum
6008 : 4359 : timestamptz_part(PG_FUNCTION_ARGS)
6009 : : {
6010 : 4359 : return timestamptz_part_common(fcinfo, false);
6011 : : }
6012 : :
6013 : : Datum
6014 : 14379 : extract_timestamptz(PG_FUNCTION_ARGS)
6015 : : {
6016 : 14379 : return timestamptz_part_common(fcinfo, true);
6017 : : }
6018 : :
6019 : : /*
6020 : : * NonFiniteIntervalPart
6021 : : *
6022 : : * Used by interval_part when extracting from infinite interval. Returns
6023 : : * +/-Infinity if that is the appropriate result, otherwise returns zero
6024 : : * (which should be taken as meaning to return NULL).
6025 : : *
6026 : : * Errors thrown here for invalid units should exactly match those that
6027 : : * would be thrown in the calling functions, else there will be unexpected
6028 : : * discrepancies between finite- and infinite-input cases.
6029 : : */
6030 : : static float8
662 dean.a.rasheed@gmail 6031 : 192 : NonFiniteIntervalPart(int type, int unit, char *lowunits, bool isNegative)
6032 : : {
6033 [ + + - + ]: 192 : if ((type != UNITS) && (type != RESERV))
662 dean.a.rasheed@gmail 6034 [ # # ]:UBC 0 : ereport(ERROR,
6035 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6036 : : errmsg("unit \"%s\" not recognized for type %s",
6037 : : lowunits, format_type_be(INTERVALOID))));
6038 : :
662 dean.a.rasheed@gmail 6039 [ + + - ]:CBC 192 : switch (unit)
6040 : : {
6041 : : /* Oscillating units */
6042 : 102 : case DTK_MICROSEC:
6043 : : case DTK_MILLISEC:
6044 : : case DTK_SECOND:
6045 : : case DTK_MINUTE:
6046 : : case DTK_WEEK:
6047 : : case DTK_MONTH:
6048 : : case DTK_QUARTER:
6049 : 102 : return 0.0;
6050 : :
6051 : : /* Monotonically-increasing units */
6052 : 90 : case DTK_HOUR:
6053 : : case DTK_DAY:
6054 : : case DTK_YEAR:
6055 : : case DTK_DECADE:
6056 : : case DTK_CENTURY:
6057 : : case DTK_MILLENNIUM:
6058 : : case DTK_EPOCH:
6059 [ + + ]: 90 : if (isNegative)
6060 : 45 : return -get_float8_infinity();
6061 : : else
6062 : 45 : return get_float8_infinity();
6063 : :
662 dean.a.rasheed@gmail 6064 :UBC 0 : default:
6065 [ # # ]: 0 : ereport(ERROR,
6066 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6067 : : errmsg("unit \"%s\" not supported for type %s",
6068 : : lowunits, format_type_be(INTERVALOID))));
6069 : : return 0.0; /* keep compiler quiet */
6070 : : }
6071 : : }
6072 : :
6073 : : /* interval_part() and extract_interval()
6074 : : * Extract specified field from interval.
6075 : : */
6076 : : static Datum
1614 peter@eisentraut.org 6077 :CBC 1188 : interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
6078 : : {
6374 tgl@sss.pgh.pa.us 6079 : 1188 : text *units = PG_GETARG_TEXT_PP(0);
9220 6080 : 1188 : Interval *interval = PG_GETARG_INTERVAL_P(1);
6081 : : int64 intresult;
6082 : : int type,
6083 : : val;
6084 : : char *lowunits;
6085 : : struct pg_itm tt,
9334 lockhart@fourpalms.o 6086 : 1188 : *tm = &tt;
6087 : :
6374 tgl@sss.pgh.pa.us 6088 [ - + ]: 1188 : lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
6089 [ - + - - : 1188 : VARSIZE_ANY_EXHDR(units),
- - - - -
+ ]
6090 : : false);
6091 : :
9334 lockhart@fourpalms.o 6092 : 1188 : type = DecodeUnits(0, lowunits, &val);
8724 6093 [ + + ]: 1188 : if (type == UNKNOWN_FIELD)
9334 6094 : 117 : type = DecodeSpecial(0, lowunits, &val);
6095 : :
662 dean.a.rasheed@gmail 6096 [ + + + - : 1188 : if (INTERVAL_NOT_FINITE(interval))
- + + + +
- + - ]
6097 : : {
6098 : 192 : double r = NonFiniteIntervalPart(type, val, lowunits,
6099 [ + + + - : 192 : INTERVAL_IS_NOBEGIN(interval));
+ - ]
6100 : :
6101 [ + + ]: 192 : if (r != 0.0)
6102 : : {
6103 [ + + ]: 90 : if (retnumeric)
6104 : : {
6105 [ + + ]: 84 : if (r < 0)
6106 : 42 : return DirectFunctionCall3(numeric_in,
6107 : : CStringGetDatum("-Infinity"),
6108 : : ObjectIdGetDatum(InvalidOid),
6109 : : Int32GetDatum(-1));
6110 [ + - ]: 42 : else if (r > 0)
6111 : 42 : return DirectFunctionCall3(numeric_in,
6112 : : CStringGetDatum("Infinity"),
6113 : : ObjectIdGetDatum(InvalidOid),
6114 : : Int32GetDatum(-1));
6115 : : }
6116 : : else
6117 : 6 : PG_RETURN_FLOAT8(r);
6118 : : }
6119 : : else
6120 : 102 : PG_RETURN_NULL();
6121 : : }
6122 : :
8744 lockhart@fourpalms.o 6123 [ + + ]: 996 : if (type == UNITS)
6124 : : {
1253 tgl@sss.pgh.pa.us 6125 : 897 : interval2itm(*interval, tm);
1213 6126 [ + + + + : 897 : switch (val)
+ + + + +
+ + + +
+ ]
6127 : : {
6128 : 90 : case DTK_MICROSEC:
6129 : 90 : intresult = tm->tm_sec * INT64CONST(1000000) + tm->tm_usec;
6130 : 90 : break;
6131 : :
6132 : 90 : case DTK_MILLISEC:
6133 [ + + ]: 90 : if (retnumeric)
6134 : : /*---
6135 : : * tm->tm_sec * 1000 + fsec / 1000
6136 : : * = (tm->tm_sec * 1'000'000 + fsec) / 1000
6137 : : */
6138 : 60 : PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + tm->tm_usec, 3));
6139 : : else
6140 : 30 : PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + tm->tm_usec / 1000.0);
6141 : : break;
6142 : :
6143 : 90 : case DTK_SECOND:
6144 [ + + ]: 90 : if (retnumeric)
6145 : : /*---
6146 : : * tm->tm_sec + fsec / 1'000'000
6147 : : * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
6148 : : */
6149 : 60 : PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + tm->tm_usec, 6));
6150 : : else
6151 : 30 : PG_RETURN_FLOAT8(tm->tm_sec + tm->tm_usec / 1000000.0);
6152 : : break;
6153 : :
6154 : 60 : case DTK_MINUTE:
6155 : 60 : intresult = tm->tm_min;
6156 : 60 : break;
6157 : :
6158 : 60 : case DTK_HOUR:
6159 : 60 : intresult = tm->tm_hour;
6160 : 60 : break;
6161 : :
6162 : 60 : case DTK_DAY:
6163 : 60 : intresult = tm->tm_mday;
6164 : 60 : break;
6165 : :
386 6166 : 60 : case DTK_WEEK:
6167 : 60 : intresult = tm->tm_mday / 7;
6168 : 60 : break;
6169 : :
1213 6170 : 60 : case DTK_MONTH:
6171 : 60 : intresult = tm->tm_mon;
6172 : 60 : break;
6173 : :
6174 : 60 : case DTK_QUARTER:
6175 : :
6176 : : /*
6177 : : * We want to maintain the rule that a field extracted from a
6178 : : * negative interval is the negative of the field's value for
6179 : : * the sign-reversed interval. The broken-down tm_year and
6180 : : * tm_mon aren't very helpful for that, so work from
6181 : : * interval->month.
6182 : : */
386 6183 [ + + ]: 60 : if (interval->month >= 0)
6184 : 45 : intresult = (tm->tm_mon / 3) + 1;
6185 : : else
6186 : 15 : intresult = -(((-interval->month % MONTHS_PER_YEAR) / 3) + 1);
1213 6187 : 60 : break;
6188 : :
6189 : 60 : case DTK_YEAR:
6190 : 60 : intresult = tm->tm_year;
6191 : 60 : break;
6192 : :
6193 : 72 : case DTK_DECADE:
6194 : : /* caution: C division may have negative remainder */
6195 : 72 : intresult = tm->tm_year / 10;
6196 : 72 : break;
6197 : :
6198 : 72 : case DTK_CENTURY:
6199 : : /* caution: C division may have negative remainder */
6200 : 72 : intresult = tm->tm_year / 100;
6201 : 72 : break;
6202 : :
6203 : 60 : case DTK_MILLENNIUM:
6204 : : /* caution: C division may have negative remainder */
6205 : 60 : intresult = tm->tm_year / 1000;
6206 : 60 : break;
6207 : :
6208 : 3 : default:
6209 [ + - ]: 3 : ereport(ERROR,
6210 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6211 : : errmsg("unit \"%s\" not supported for type %s",
6212 : : lowunits, format_type_be(INTERVALOID))));
6213 : : intresult = 0;
6214 : : }
6215 : : }
7411 bruce@momjian.us 6216 [ + + + - ]: 99 : else if (type == RESERV && val == DTK_EPOCH)
6217 : : {
1614 peter@eisentraut.org 6218 [ + + ]: 96 : if (retnumeric)
6219 : : {
6220 : : Numeric result;
6221 : : int64 secs_from_day_month;
6222 : : int64 val;
6223 : :
6224 : : /*
6225 : : * To do this calculation in integer arithmetic even though
6226 : : * DAYS_PER_YEAR is fractional, multiply everything by 4 and then
6227 : : * divide by 4 again at the end. This relies on DAYS_PER_YEAR
6228 : : * being a multiple of 0.25 and on SECS_PER_DAY being a multiple
6229 : : * of 4.
6230 : : */
1236 6231 : 66 : secs_from_day_month = ((int64) (4 * DAYS_PER_YEAR) * (interval->month / MONTHS_PER_YEAR) +
6232 : 66 : (int64) (4 * DAYS_PER_MONTH) * (interval->month % MONTHS_PER_YEAR) +
6233 : 66 : (int64) 4 * interval->day) * (SECS_PER_DAY / 4);
6234 : :
6235 : : /*---
6236 : : * result = secs_from_day_month + interval->time / 1'000'000
6237 : : * = (secs_from_day_month * 1'000'000 + interval->time) / 1'000'000
6238 : : */
6239 : :
6240 : : /*
6241 : : * Try the computation inside int64; if it overflows, do it in
6242 : : * numeric (slower). This overflow happens around 10^9 days, so
6243 : : * not common in practice.
6244 : : */
1614 6245 [ + + ]: 66 : if (!pg_mul_s64_overflow(secs_from_day_month, 1000000, &val) &&
6246 [ + - ]: 63 : !pg_add_s64_overflow(val, interval->time, &val))
6247 : 63 : result = int64_div_fast_to_numeric(val, 6);
6248 : : else
6249 : : result =
1 michael@paquier.xyz 6250 :GNC 3 : numeric_add_safe(int64_div_fast_to_numeric(interval->time, 6),
6251 : : int64_to_numeric(secs_from_day_month),
6252 : : NULL);
6253 : :
1614 peter@eisentraut.org 6254 :CBC 66 : PG_RETURN_NUMERIC(result);
6255 : : }
6256 : : else
6257 : : {
6258 : : float8 result;
6259 : :
6260 : 30 : result = interval->time / 1000000.0;
6261 : 30 : result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
6262 : 30 : result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
6263 : 30 : result += ((double) SECS_PER_DAY) * interval->day;
6264 : :
6265 : 30 : PG_RETURN_FLOAT8(result);
6266 : : }
6267 : : }
6268 : : else
6269 : : {
8077 tgl@sss.pgh.pa.us 6270 [ + - ]: 3 : ereport(ERROR,
6271 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6272 : : errmsg("unit \"%s\" not recognized for type %s",
6273 : : lowunits, format_type_be(INTERVALOID))));
6274 : : intresult = 0;
6275 : : }
6276 : :
1614 peter@eisentraut.org 6277 [ + + ]: 714 : if (retnumeric)
6278 : 684 : PG_RETURN_NUMERIC(int64_to_numeric(intresult));
6279 : : else
6280 : 30 : PG_RETURN_FLOAT8(intresult);
6281 : : }
6282 : :
6283 : : Datum
6284 : 144 : interval_part(PG_FUNCTION_ARGS)
6285 : : {
6286 : 144 : return interval_part_common(fcinfo, false);
6287 : : }
6288 : :
6289 : : Datum
6290 : 1044 : extract_interval(PG_FUNCTION_ARGS)
6291 : : {
6292 : 1044 : return interval_part_common(fcinfo, true);
6293 : : }
6294 : :
6295 : :
6296 : : /* timestamp_zone()
6297 : : * Encode timestamp type with specified time zone.
6298 : : * This function is just timestamp2timestamptz() except instead of
6299 : : * shifting to the global timezone, we shift to the specified timezone.
6300 : : * This is different from the other AT TIME ZONE cases because instead
6301 : : * of shifting _to_ a new time zone, it sets the time to _be_ the
6302 : : * specified timezone.
6303 : : */
6304 : : Datum
9220 tgl@sss.pgh.pa.us 6305 : 84 : timestamp_zone(PG_FUNCTION_ARGS)
6306 : : {
6374 6307 : 84 : text *zone = PG_GETARG_TEXT_PP(0);
7302 6308 : 84 : Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
6309 : : TimestampTz result;
6310 : : int tz;
6311 : : char tzname[TZ_STRLEN_MAX + 1];
6312 : : int type,
6313 : : val;
6314 : : pg_tz *tzp;
6315 : : struct pg_tm tm;
6316 : : fsec_t fsec;
6317 : :
8744 lockhart@fourpalms.o 6318 [ + - - + ]: 84 : if (TIMESTAMP_NOT_FINITE(timestamp))
8744 lockhart@fourpalms.o 6319 :UBC 0 : PG_RETURN_TIMESTAMPTZ(timestamp);
6320 : :
6321 : : /*
6322 : : * Look up the requested timezone.
6323 : : */
6374 tgl@sss.pgh.pa.us 6324 :CBC 84 : text_to_cstring_buffer(zone, tzname, sizeof(tzname));
6325 : :
904 6326 : 84 : type = DecodeTimezoneName(tzname, &val, &tzp);
6327 : :
6328 [ - + ]: 84 : if (type == TZNAME_FIXED_OFFSET)
6329 : : {
6330 : : /* fixed-offset abbreviation */
3978 tgl@sss.pgh.pa.us 6331 :UBC 0 : tz = val;
6332 : 0 : result = dt2local(timestamp, tz);
6333 : : }
904 tgl@sss.pgh.pa.us 6334 [ + + ]:CBC 84 : else if (type == TZNAME_DYNTZ)
6335 : : {
6336 : : /* dynamic-offset abbreviation, resolve using specified time */
3978 6337 [ - + ]: 42 : if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, tzp) != 0)
3978 tgl@sss.pgh.pa.us 6338 [ # # ]:UBC 0 : ereport(ERROR,
6339 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6340 : : errmsg("timestamp out of range")));
3978 tgl@sss.pgh.pa.us 6341 :CBC 42 : tz = -DetermineTimeZoneAbbrevOffset(&tm, tzname, tzp);
6270 6342 : 42 : result = dt2local(timestamp, tz);
6343 : : }
6344 : : else
6345 : : {
6346 : : /* full zone name, rotate to that zone */
904 6347 [ - + ]: 42 : if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, tzp) != 0)
7302 tgl@sss.pgh.pa.us 6348 [ # # ]:UBC 0 : ereport(ERROR,
6349 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6350 : : errmsg("timestamp out of range")));
904 tgl@sss.pgh.pa.us 6351 :CBC 42 : tz = DetermineTimeZoneOffset(&tm, tzp);
6352 [ - + ]: 42 : if (tm2timestamp(&tm, fsec, &tz, &result) != 0)
904 tgl@sss.pgh.pa.us 6353 [ # # ]:UBC 0 : ereport(ERROR,
6354 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6355 : : errmsg("timestamp out of range")));
6356 : : }
6357 : :
3461 tgl@sss.pgh.pa.us 6358 [ + - - + ]:CBC 84 : if (!IS_VALID_TIMESTAMP(result))
3461 tgl@sss.pgh.pa.us 6359 [ # # ]:UBC 0 : ereport(ERROR,
6360 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6361 : : errmsg("timestamp out of range")));
6362 : :
7351 bruce@momjian.us 6363 :CBC 84 : PG_RETURN_TIMESTAMPTZ(result);
6364 : : }
6365 : :
6366 : : /* timestamp_izone()
6367 : : * Encode timestamp type with specified time interval as time zone.
6368 : : */
6369 : : Datum
8744 lockhart@fourpalms.o 6370 : 6 : timestamp_izone(PG_FUNCTION_ARGS)
6371 : : {
6372 : 6 : Interval *zone = PG_GETARG_INTERVAL_P(0);
7266 bruce@momjian.us 6373 : 6 : Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
6374 : : TimestampTz result;
6375 : : int tz;
6376 : :
8744 lockhart@fourpalms.o 6377 [ + - - + ]: 6 : if (TIMESTAMP_NOT_FINITE(timestamp))
8744 lockhart@fourpalms.o 6378 :UBC 0 : PG_RETURN_TIMESTAMPTZ(timestamp);
6379 : :
662 dean.a.rasheed@gmail 6380 [ + + + - :CBC 6 : if (INTERVAL_NOT_FINITE(zone))
- + + - +
- + - ]
6381 [ + - ]: 6 : ereport(ERROR,
6382 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6383 : : errmsg("interval time zone \"%s\" must be finite",
6384 : : DatumGetCString(DirectFunctionCall1(interval_out,
6385 : : PointerGetDatum(zone))))));
6386 : :
4601 tgl@sss.pgh.pa.us 6387 [ # # # # ]:UBC 0 : if (zone->month != 0 || zone->day != 0)
8077 6388 [ # # ]: 0 : ereport(ERROR,
6389 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6390 : : errmsg("interval time zone \"%s\" must not include months or days",
6391 : : DatumGetCString(DirectFunctionCall1(interval_out,
6392 : : PointerGetDatum(zone))))));
6393 : :
7411 bruce@momjian.us 6394 : 0 : tz = zone->time / USECS_PER_SEC;
6395 : :
8539 lockhart@fourpalms.o 6396 : 0 : result = dt2local(timestamp, tz);
6397 : :
3461 tgl@sss.pgh.pa.us 6398 [ # # # # ]: 0 : if (!IS_VALID_TIMESTAMP(result))
6399 [ # # ]: 0 : ereport(ERROR,
6400 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6401 : : errmsg("timestamp out of range")));
6402 : :
8744 lockhart@fourpalms.o 6403 : 0 : PG_RETURN_TIMESTAMPTZ(result);
6404 : : } /* timestamp_izone() */
6405 : :
6406 : : /* TimestampTimestampTzRequiresRewrite()
6407 : : *
6408 : : * Returns false if the TimeZone GUC setting causes timestamp_timestamptz and
6409 : : * timestamptz_timestamp to be no-ops, where the return value has the same
6410 : : * bits as the argument. Since project convention is to assume a GUC changes
6411 : : * no more often than STABLE functions change, the answer is valid that long.
6412 : : */
6413 : : bool
2374 noah@leadboat.com 6414 :CBC 9 : TimestampTimestampTzRequiresRewrite(void)
6415 : : {
6416 : : long offset;
6417 : :
6418 [ + + + - ]: 9 : if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
2267 6419 : 6 : return false;
6420 : 3 : return true;
6421 : : }
6422 : :
6423 : : /* timestamp_timestamptz()
6424 : : * Convert local timestamp to timestamp at GMT
6425 : : */
6426 : : Datum
8744 lockhart@fourpalms.o 6427 : 111 : timestamp_timestamptz(PG_FUNCTION_ARGS)
6428 : : {
7266 bruce@momjian.us 6429 : 111 : Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
6430 : :
7838 tgl@sss.pgh.pa.us 6431 : 111 : PG_RETURN_TIMESTAMPTZ(timestamp2timestamptz(timestamp));
6432 : : }
6433 : :
6434 : : /*
6435 : : * Convert timestamp to timestamp with time zone.
6436 : : *
6437 : : * On successful conversion, *overflow is set to zero if it's not NULL.
6438 : : *
6439 : : * If the timestamp is finite but out of the valid range for timestamptz, then:
6440 : : * if overflow is NULL, we throw an out-of-range error.
6441 : : * if overflow is not NULL, we store +1 or -1 there to indicate the sign
6442 : : * of the overflow, and return the appropriate timestamptz infinity.
6443 : : */
6444 : : TimestampTz
2147 akorotkov@postgresql 6445 : 8160 : timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
6446 : : {
6447 : : TimestampTz result;
6448 : : struct pg_tm tt,
8744 lockhart@fourpalms.o 6449 : 8160 : *tm = &tt;
6450 : : fsec_t fsec;
6451 : : int tz;
6452 : :
1795 tgl@sss.pgh.pa.us 6453 [ + + ]: 8160 : if (overflow)
6454 : 8043 : *overflow = 0;
6455 : :
8744 lockhart@fourpalms.o 6456 [ + + + + ]: 8160 : if (TIMESTAMP_NOT_FINITE(timestamp))
2173 akorotkov@postgresql 6457 :GBC 17 : return timestamp;
6458 : :
6459 : : /* timestamp2tm should not fail on valid timestamps, but cope */
1795 tgl@sss.pgh.pa.us 6460 [ + - ]:CBC 8143 : if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
6461 : : {
6608 6462 : 8143 : tz = DetermineTimeZoneOffset(tm, session_timezone);
6463 : :
2147 akorotkov@postgresql 6464 : 8143 : result = dt2local(timestamp, -tz);
6465 : :
6466 [ + + + - ]: 8143 : if (IS_VALID_TIMESTAMP(result))
2173 6467 : 8137 : return result;
6468 : : }
6469 : :
65 tgl@sss.pgh.pa.us 6470 [ + - ]:GNC 6 : if (overflow)
6471 : : {
6472 [ + - ]: 6 : if (timestamp < 0)
6473 : : {
6474 : 6 : *overflow = -1;
6475 : 6 : TIMESTAMP_NOBEGIN(result);
6476 : : }
6477 : : else
6478 : : {
65 tgl@sss.pgh.pa.us 6479 :UNC 0 : *overflow = 1;
6480 : 0 : TIMESTAMP_NOEND(result);
6481 : : }
65 tgl@sss.pgh.pa.us 6482 :GNC 6 : return result;
6483 : : }
6484 : :
2147 akorotkov@postgresql 6485 [ # # ]:UBC 0 : ereport(ERROR,
6486 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6487 : : errmsg("timestamp out of range")));
6488 : :
6489 : : return 0;
6490 : : }
6491 : :
6492 : : /*
6493 : : * Promote timestamp to timestamptz, throwing error for overflow.
6494 : : */
6495 : : static TimestampTz
2173 akorotkov@postgresql 6496 :CBC 117 : timestamp2timestamptz(Timestamp timestamp)
6497 : : {
2147 6498 : 117 : return timestamp2timestamptz_opt_overflow(timestamp, NULL);
6499 : : }
6500 : :
6501 : : /* timestamptz_timestamp()
6502 : : * Convert timestamp at GMT to local timestamp
6503 : : */
6504 : : Datum
8744 lockhart@fourpalms.o 6505 : 31052 : timestamptz_timestamp(PG_FUNCTION_ARGS)
6506 : : {
8153 tgl@sss.pgh.pa.us 6507 : 31052 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0);
6508 : :
3308 6509 : 31052 : PG_RETURN_TIMESTAMP(timestamptz2timestamp(timestamp));
6510 : : }
6511 : :
6512 : : /*
6513 : : * Convert timestamptz to timestamp, throwing error for overflow.
6514 : : */
6515 : : static Timestamp
6516 : 31085 : timestamptz2timestamp(TimestampTz timestamp)
6517 : : {
65 tgl@sss.pgh.pa.us 6518 :GNC 31085 : return timestamptz2timestamp_opt_overflow(timestamp, NULL);
6519 : : }
6520 : :
6521 : : /*
6522 : : * Convert timestamp with time zone to timestamp.
6523 : : *
6524 : : * On successful conversion, *overflow is set to zero if it's not NULL.
6525 : : *
6526 : : * If the timestamptz is finite but out of the valid range for timestamp, then:
6527 : : * if overflow is NULL, we throw an out-of-range error.
6528 : : * if overflow is not NULL, we store +1 or -1 there to indicate the sign
6529 : : * of the overflow, and return the appropriate timestamp infinity.
6530 : : */
6531 : : Timestamp
6532 : 31105 : timestamptz2timestamp_opt_overflow(TimestampTz timestamp, int *overflow)
6533 : : {
6534 : : Timestamp result;
6535 : : struct pg_tm tt,
8744 lockhart@fourpalms.o 6536 :CBC 31105 : *tm = &tt;
6537 : : fsec_t fsec;
6538 : : int tz;
6539 : :
65 tgl@sss.pgh.pa.us 6540 [ + + ]:GNC 31105 : if (overflow)
6541 : 20 : *overflow = 0;
6542 : :
8744 lockhart@fourpalms.o 6543 [ + + + + ]:CBC 31105 : if (TIMESTAMP_NOT_FINITE(timestamp))
8744 lockhart@fourpalms.o 6544 :GBC 12 : result = timestamp;
6545 : : else
6546 : : {
4923 peter_e@gmx.net 6547 [ - + ]:CBC 31093 : if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
6548 : : {
65 tgl@sss.pgh.pa.us 6549 [ # # ]:UNC 0 : if (overflow)
6550 : : {
6551 [ # # ]: 0 : if (timestamp < 0)
6552 : : {
6553 : 0 : *overflow = -1;
6554 : 0 : TIMESTAMP_NOBEGIN(result);
6555 : : }
6556 : : else
6557 : : {
6558 : 0 : *overflow = 1;
6559 : 0 : TIMESTAMP_NOEND(result);
6560 : : }
6561 : 0 : return result;
6562 : : }
8077 tgl@sss.pgh.pa.us 6563 [ # # ]:UBC 0 : ereport(ERROR,
6564 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6565 : : errmsg("timestamp out of range")));
6566 : : }
8744 lockhart@fourpalms.o 6567 [ + + ]:CBC 31093 : if (tm2timestamp(tm, fsec, NULL, &result) != 0)
6568 : : {
65 tgl@sss.pgh.pa.us 6569 [ + - ]:GNC 2 : if (overflow)
6570 : : {
6571 [ + - ]: 2 : if (timestamp < 0)
6572 : : {
6573 : 2 : *overflow = -1;
6574 : 2 : TIMESTAMP_NOBEGIN(result);
6575 : : }
6576 : : else
6577 : : {
65 tgl@sss.pgh.pa.us 6578 :UNC 0 : *overflow = 1;
6579 : 0 : TIMESTAMP_NOEND(result);
6580 : : }
65 tgl@sss.pgh.pa.us 6581 :GNC 2 : return result;
6582 : : }
8077 tgl@sss.pgh.pa.us 6583 [ # # ]:UBC 0 : ereport(ERROR,
6584 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6585 : : errmsg("timestamp out of range")));
6586 : : }
6587 : : }
3308 tgl@sss.pgh.pa.us 6588 :CBC 31103 : return result;
6589 : : }
6590 : :
6591 : : /* timestamptz_zone()
6592 : : * Evaluate timestamp with time zone type at the specified time zone.
6593 : : * Returns a timestamp without time zone.
6594 : : */
6595 : : Datum
8744 lockhart@fourpalms.o 6596 : 117 : timestamptz_zone(PG_FUNCTION_ARGS)
6597 : : {
6374 tgl@sss.pgh.pa.us 6598 : 117 : text *zone = PG_GETARG_TEXT_PP(0);
8153 6599 : 117 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
6600 : : Timestamp result;
6601 : : int tz;
6602 : : char tzname[TZ_STRLEN_MAX + 1];
6603 : : int type,
6604 : : val;
6605 : : pg_tz *tzp;
6606 : :
9220 6607 [ + + + + ]: 117 : if (TIMESTAMP_NOT_FINITE(timestamp))
7302 6608 : 12 : PG_RETURN_TIMESTAMP(timestamp);
6609 : :
6610 : : /*
6611 : : * Look up the requested timezone.
6612 : : */
6374 6613 : 105 : text_to_cstring_buffer(zone, tzname, sizeof(tzname));
6614 : :
904 6615 : 105 : type = DecodeTimezoneName(tzname, &val, &tzp);
6616 : :
6617 [ + + ]: 102 : if (type == TZNAME_FIXED_OFFSET)
6618 : : {
6619 : : /* fixed-offset abbreviation */
3978 6620 : 24 : tz = -val;
6621 : 24 : result = dt2local(timestamp, tz);
6622 : : }
904 6623 [ + + ]: 78 : else if (type == TZNAME_DYNTZ)
6624 : : {
6625 : : /* dynamic-offset abbreviation, resolve using specified time */
6626 : : int isdst;
6627 : :
3978 6628 : 36 : tz = DetermineTimeZoneAbbrevOffsetTS(timestamp, tzname, tzp, &isdst);
6270 6629 : 36 : result = dt2local(timestamp, tz);
6630 : : }
6631 : : else
6632 : : {
6633 : : /* full zone name, rotate from that zone */
6634 : : struct pg_tm tm;
6635 : : fsec_t fsec;
6636 : :
904 6637 [ - + ]: 42 : if (timestamp2tm(timestamp, &tz, &tm, &fsec, NULL, tzp) != 0)
7302 tgl@sss.pgh.pa.us 6638 [ # # ]:UBC 0 : ereport(ERROR,
6639 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6640 : : errmsg("timestamp out of range")));
904 tgl@sss.pgh.pa.us 6641 [ - + ]:CBC 42 : if (tm2timestamp(&tm, fsec, NULL, &result) != 0)
904 tgl@sss.pgh.pa.us 6642 [ # # ]:UBC 0 : ereport(ERROR,
6643 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6644 : : errmsg("timestamp out of range")));
6645 : : }
6646 : :
3461 tgl@sss.pgh.pa.us 6647 [ + - - + ]:CBC 102 : if (!IS_VALID_TIMESTAMP(result))
3461 tgl@sss.pgh.pa.us 6648 [ # # ]:UBC 0 : ereport(ERROR,
6649 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6650 : : errmsg("timestamp out of range")));
6651 : :
8539 lockhart@fourpalms.o 6652 :CBC 102 : PG_RETURN_TIMESTAMP(result);
6653 : : }
6654 : :
6655 : : /* timestamptz_izone()
6656 : : * Encode timestamp with time zone type with specified time interval as time zone.
6657 : : * Returns a timestamp without time zone.
6658 : : */
6659 : : Datum
8744 6660 : 6 : timestamptz_izone(PG_FUNCTION_ARGS)
6661 : : {
9070 6662 : 6 : Interval *zone = PG_GETARG_INTERVAL_P(0);
8153 tgl@sss.pgh.pa.us 6663 : 6 : TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1);
6664 : : Timestamp result;
6665 : : int tz;
6666 : :
9070 lockhart@fourpalms.o 6667 [ + - - + ]: 6 : if (TIMESTAMP_NOT_FINITE(timestamp))
7302 tgl@sss.pgh.pa.us 6668 :UBC 0 : PG_RETURN_TIMESTAMP(timestamp);
6669 : :
662 dean.a.rasheed@gmail 6670 [ + + + - :CBC 6 : if (INTERVAL_NOT_FINITE(zone))
- + + - +
- + - ]
6671 [ + - ]: 6 : ereport(ERROR,
6672 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6673 : : errmsg("interval time zone \"%s\" must be finite",
6674 : : DatumGetCString(DirectFunctionCall1(interval_out,
6675 : : PointerGetDatum(zone))))));
6676 : :
4601 tgl@sss.pgh.pa.us 6677 [ # # # # ]:UBC 0 : if (zone->month != 0 || zone->day != 0)
8077 6678 [ # # ]: 0 : ereport(ERROR,
6679 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6680 : : errmsg("interval time zone \"%s\" must not include months or days",
6681 : : DatumGetCString(DirectFunctionCall1(interval_out,
6682 : : PointerGetDatum(zone))))));
6683 : :
7411 bruce@momjian.us 6684 : 0 : tz = -(zone->time / USECS_PER_SEC);
6685 : :
8539 lockhart@fourpalms.o 6686 : 0 : result = dt2local(timestamp, tz);
6687 : :
3461 tgl@sss.pgh.pa.us 6688 [ # # # # ]: 0 : if (!IS_VALID_TIMESTAMP(result))
6689 [ # # ]: 0 : ereport(ERROR,
6690 : : (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6691 : : errmsg("timestamp out of range")));
6692 : :
8539 lockhart@fourpalms.o 6693 : 0 : PG_RETURN_TIMESTAMP(result);
6694 : : }
6695 : :
6696 : : /* generate_series_timestamp()
6697 : : * Generate the set of timestamps from start to finish by step
6698 : : */
6699 : : Datum
6334 tgl@sss.pgh.pa.us 6700 :CBC 342 : generate_series_timestamp(PG_FUNCTION_ARGS)
6701 : : {
6702 : : FuncCallContext *funcctx;
6703 : : generate_series_timestamp_fctx *fctx;
6704 : : Timestamp result;
6705 : :
6706 : : /* stuff done only on the first call of the function */
6707 [ + + ]: 342 : if (SRF_IS_FIRSTCALL())
6708 : : {
5931 bruce@momjian.us 6709 : 21 : Timestamp start = PG_GETARG_TIMESTAMP(0);
6710 : 21 : Timestamp finish = PG_GETARG_TIMESTAMP(1);
6711 : 21 : Interval *step = PG_GETARG_INTERVAL_P(2);
6712 : : MemoryContext oldcontext;
6713 : :
6714 : : /* create a function context for cross-call persistence */
6334 tgl@sss.pgh.pa.us 6715 : 21 : funcctx = SRF_FIRSTCALL_INIT();
6716 : :
6717 : : /*
6718 : : * switch to memory context appropriate for multiple function calls
6719 : : */
6720 : 21 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
6721 : :
6722 : : /* allocate memory for user context */
6723 : : fctx = (generate_series_timestamp_fctx *)
6724 : 21 : palloc(sizeof(generate_series_timestamp_fctx));
6725 : :
6726 : : /*
6727 : : * Use fctx to keep state from call to call. Seed current with the
6728 : : * original start value
6729 : : */
6730 : 21 : fctx->current = start;
6731 : 21 : fctx->finish = finish;
6732 : 21 : fctx->step = *step;
6733 : :
6734 : : /* Determine sign of the interval */
662 dean.a.rasheed@gmail 6735 : 21 : fctx->step_sign = interval_sign(&fctx->step);
6736 : :
6334 tgl@sss.pgh.pa.us 6737 [ + + ]: 21 : if (fctx->step_sign == 0)
6738 [ + - ]: 3 : ereport(ERROR,
6739 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6740 : : errmsg("step size cannot equal zero")));
6741 : :
662 dean.a.rasheed@gmail 6742 [ + + + - : 18 : if (INTERVAL_NOT_FINITE((&fctx->step)))
- + + + +
- + - ]
6743 [ + - ]: 6 : ereport(ERROR,
6744 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6745 : : errmsg("step size cannot be infinite")));
6746 : :
6334 tgl@sss.pgh.pa.us 6747 : 12 : funcctx->user_fctx = fctx;
6748 : 12 : MemoryContextSwitchTo(oldcontext);
6749 : : }
6750 : :
6751 : : /* stuff done on every call of the function */
6752 : 333 : funcctx = SRF_PERCALL_SETUP();
6753 : :
6754 : : /*
6755 : : * get the saved state and use current as the result for this iteration
6756 : : */
6757 : 333 : fctx = funcctx->user_fctx;
6758 : 333 : result = fctx->current;
6759 : :
6760 [ + - + + ]: 666 : if (fctx->step_sign > 0 ?
6761 : 333 : timestamp_cmp_internal(result, fctx->finish) <= 0 :
6334 tgl@sss.pgh.pa.us 6762 :UBC 0 : timestamp_cmp_internal(result, fctx->finish) >= 0)
6763 : : {
6764 : : /* increment current in preparation for next iteration */
2046 alvherre@alvh.no-ip. 6765 :CBC 324 : fctx->current = DatumGetTimestamp(DirectFunctionCall2(timestamp_pl_interval,
6766 : : TimestampGetDatum(fctx->current),
6767 : : PointerGetDatum(&fctx->step)));
6768 : :
6769 : : /* do when there is more left to send */
6334 tgl@sss.pgh.pa.us 6770 : 324 : SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
6771 : : }
6772 : : else
6773 : : {
6774 : : /* do when there is no more left */
6775 : 9 : SRF_RETURN_DONE(funcctx);
6776 : : }
6777 : : }
6778 : :
6779 : : /* generate_series_timestamptz()
6780 : : * Generate the set of timestamps from start to finish by step,
6781 : : * doing arithmetic in the specified or session timezone.
6782 : : */
6783 : : static Datum
903 6784 : 31341 : generate_series_timestamptz_internal(FunctionCallInfo fcinfo)
6785 : : {
6786 : : FuncCallContext *funcctx;
6787 : : generate_series_timestamptz_fctx *fctx;
6788 : : TimestampTz result;
6789 : :
6790 : : /* stuff done only on the first call of the function */
6334 6791 [ + + ]: 31341 : if (SRF_IS_FIRSTCALL())
6792 : : {
6793 : 46 : TimestampTz start = PG_GETARG_TIMESTAMPTZ(0);
6794 : 46 : TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5931 bruce@momjian.us 6795 : 46 : Interval *step = PG_GETARG_INTERVAL_P(2);
903 tgl@sss.pgh.pa.us 6796 [ + + ]: 46 : text *zone = (PG_NARGS() == 4) ? PG_GETARG_TEXT_PP(3) : NULL;
6797 : : MemoryContext oldcontext;
6798 : :
6799 : : /* create a function context for cross-call persistence */
6334 6800 : 46 : funcctx = SRF_FIRSTCALL_INIT();
6801 : :
6802 : : /*
6803 : : * switch to memory context appropriate for multiple function calls
6804 : : */
6805 : 46 : oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
6806 : :
6807 : : /* allocate memory for user context */
6808 : : fctx = (generate_series_timestamptz_fctx *)
6809 : 46 : palloc(sizeof(generate_series_timestamptz_fctx));
6810 : :
6811 : : /*
6812 : : * Use fctx to keep state from call to call. Seed current with the
6813 : : * original start value
6814 : : */
6815 : 46 : fctx->current = start;
6816 : 46 : fctx->finish = finish;
6817 : 46 : fctx->step = *step;
903 6818 [ + + ]: 46 : fctx->attimezone = zone ? lookup_timezone(zone) : session_timezone;
6819 : :
6820 : : /* Determine sign of the interval */
662 dean.a.rasheed@gmail 6821 : 46 : fctx->step_sign = interval_sign(&fctx->step);
6822 : :
6334 tgl@sss.pgh.pa.us 6823 [ + + ]: 46 : if (fctx->step_sign == 0)
6824 [ + - ]: 6 : ereport(ERROR,
6825 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6826 : : errmsg("step size cannot equal zero")));
6827 : :
662 dean.a.rasheed@gmail 6828 [ + + + - : 40 : if (INTERVAL_NOT_FINITE((&fctx->step)))
- + + + +
- + - ]
6829 [ + - ]: 6 : ereport(ERROR,
6830 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6831 : : errmsg("step size cannot be infinite")));
6832 : :
6334 tgl@sss.pgh.pa.us 6833 : 34 : funcctx->user_fctx = fctx;
6834 : 34 : MemoryContextSwitchTo(oldcontext);
6835 : : }
6836 : :
6837 : : /* stuff done on every call of the function */
6838 : 31329 : funcctx = SRF_PERCALL_SETUP();
6839 : :
6840 : : /*
6841 : : * get the saved state and use current as the result for this iteration
6842 : : */
6843 : 31329 : fctx = funcctx->user_fctx;
6844 : 31329 : result = fctx->current;
6845 : :
6846 [ + + + + ]: 62658 : if (fctx->step_sign > 0 ?
6847 : 31194 : timestamp_cmp_internal(result, fctx->finish) <= 0 :
6848 : 135 : timestamp_cmp_internal(result, fctx->finish) >= 0)
6849 : : {
6850 : : /* increment current in preparation for next iteration */
903 6851 : 31298 : fctx->current = timestamptz_pl_interval_internal(fctx->current,
6852 : : &fctx->step,
6853 : : fctx->attimezone);
6854 : :
6855 : : /* do when there is more left to send */
6334 6856 : 31298 : SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
6857 : : }
6858 : : else
6859 : : {
6860 : : /* do when there is no more left */
6861 : 31 : SRF_RETURN_DONE(funcctx);
6862 : : }
6863 : : }
6864 : :
6865 : : Datum
903 6866 : 31206 : generate_series_timestamptz(PG_FUNCTION_ARGS)
6867 : : {
6868 : 31206 : return generate_series_timestamptz_internal(fcinfo);
6869 : : }
6870 : :
6871 : : Datum
6872 : 135 : generate_series_timestamptz_at_zone(PG_FUNCTION_ARGS)
6873 : : {
6874 : 135 : return generate_series_timestamptz_internal(fcinfo);
6875 : : }
6876 : :
6877 : : /*
6878 : : * Planner support function for generate_series(timestamp, timestamp, interval)
6879 : : */
6880 : : Datum
424 drowley@postgresql.o 6881 : 198 : generate_series_timestamp_support(PG_FUNCTION_ARGS)
6882 : : {
6883 : 198 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
6884 : 198 : Node *ret = NULL;
6885 : :
6886 [ + + ]: 198 : if (IsA(rawreq, SupportRequestRows))
6887 : : {
6888 : : /* Try to estimate the number of rows returned */
6889 : 66 : SupportRequestRows *req = (SupportRequestRows *) rawreq;
6890 : :
6891 [ + - ]: 66 : if (is_funcclause(req->node)) /* be paranoid */
6892 : : {
6893 : 66 : List *args = ((FuncExpr *) req->node)->args;
6894 : : Node *arg1,
6895 : : *arg2,
6896 : : *arg3;
6897 : :
6898 : : /* We can use estimated argument values here */
6899 : 66 : arg1 = estimate_expression_value(req->root, linitial(args));
6900 : 66 : arg2 = estimate_expression_value(req->root, lsecond(args));
6901 : 66 : arg3 = estimate_expression_value(req->root, lthird(args));
6902 : :
6903 : : /*
6904 : : * If any argument is constant NULL, we can safely assume that
6905 : : * zero rows are returned. Otherwise, if they're all non-NULL
6906 : : * constants, we can calculate the number of rows that will be
6907 : : * returned.
6908 : : */
6909 [ + + + - ]: 66 : if ((IsA(arg1, Const) && ((Const *) arg1)->constisnull) ||
6910 [ + + + - ]: 66 : (IsA(arg2, Const) && ((Const *) arg2)->constisnull) ||
6911 [ + - - + ]: 66 : (IsA(arg3, Const) && ((Const *) arg3)->constisnull))
6912 : : {
424 drowley@postgresql.o 6913 :UBC 0 : req->rows = 0;
6914 : 0 : ret = (Node *) req;
6915 : : }
424 drowley@postgresql.o 6916 [ + + + - :CBC 66 : else if (IsA(arg1, Const) && IsA(arg2, Const) && IsA(arg3, Const))
+ - ]
6917 : : {
6918 : : Timestamp start,
6919 : : finish;
6920 : : Interval *step;
6921 : : Datum diff;
6922 : : double dstep;
6923 : : int64 dummy;
6924 : :
6925 : 65 : start = DatumGetTimestamp(((Const *) arg1)->constvalue);
6926 : 65 : finish = DatumGetTimestamp(((Const *) arg2)->constvalue);
6927 : 65 : step = DatumGetIntervalP(((Const *) arg3)->constvalue);
6928 : :
6929 : : /*
6930 : : * Perform some prechecks which could cause timestamp_mi to
6931 : : * raise an ERROR. It's much better to just return some
6932 : : * default estimate than error out in a support function.
6933 : : */
6934 [ + + + - : 65 : if (!TIMESTAMP_NOT_FINITE(start) && !TIMESTAMP_NOT_FINITE(finish) &&
+ - + + ]
6935 [ + - ]: 56 : !pg_sub_s64_overflow(finish, start, &dummy))
6936 : : {
6937 : 56 : diff = DirectFunctionCall2(timestamp_mi,
6938 : : TimestampGetDatum(finish),
6939 : : TimestampGetDatum(start));
6940 : :
6941 : : #define INTERVAL_TO_MICROSECONDS(i) ((((double) (i)->month * DAYS_PER_MONTH + (i)->day)) * USECS_PER_DAY + (i)->time)
6942 : :
6943 : 56 : dstep = INTERVAL_TO_MICROSECONDS(step);
6944 : :
6945 : : /* This equation works for either sign of step */
6946 [ + + ]: 56 : if (dstep != 0.0)
6947 : : {
6948 : 47 : Interval *idiff = DatumGetIntervalP(diff);
6949 : 47 : double ddiff = INTERVAL_TO_MICROSECONDS(idiff);
6950 : :
6951 : 47 : req->rows = floor(ddiff / dstep + 1.0);
6952 : 47 : ret = (Node *) req;
6953 : : }
6954 : : #undef INTERVAL_TO_MICROSECONDS
6955 : : }
6956 : : }
6957 : : }
6958 : : }
6959 : :
6960 : 198 : PG_RETURN_POINTER(ret);
6961 : : }
6962 : :
6963 : :
6964 : : /* timestamp_at_local()
6965 : : * timestamptz_at_local()
6966 : : *
6967 : : * The regression tests do not like two functions with the same proargs and
6968 : : * prosrc but different proname, but the grammar for AT LOCAL needs an
6969 : : * overloaded name to handle both types of timestamp, so we make simple
6970 : : * wrappers for it.
6971 : : */
6972 : : Datum
694 michael@paquier.xyz 6973 : 12 : timestamp_at_local(PG_FUNCTION_ARGS)
6974 : : {
6975 : 12 : return timestamp_timestamptz(fcinfo);
6976 : : }
6977 : :
6978 : : Datum
6979 : 12 : timestamptz_at_local(PG_FUNCTION_ARGS)
6980 : : {
6981 : 12 : return timestamptz_timestamp(fcinfo);
6982 : : }
|