PostgreSQL Source Code git master
Loading...
Searching...
No Matches
timestamp.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <sys/time.h>
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/int.h"
#include "common/int128.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/optimizer.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/float.h"
#include "utils/numeric.h"
#include "utils/skipsupport.h"
#include "utils/sortsupport.h"
Include dependency graph for timestamp.c:

Go to the source code of this file.

Data Structures

struct  generate_series_timestamp_fctx
 
struct  generate_series_timestamptz_fctx
 
struct  IntervalAggState
 

Macros

#define IA_TOTAL_COUNT(ia)    ((ia)->N + (ia)->pInfcount + (ia)->nInfcount)
 
#define TIMESTAMP_GT(t1, t2)    DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
 
#define TIMESTAMP_LT(t1, t2)    DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
 
#define INTERVAL_TO_MICROSECONDS(i)   ((((double) (i)->month * DAYS_PER_MONTH + (i)->day)) * USECS_PER_DAY + (i)->time)
 

Typedefs

typedef struct IntervalAggState IntervalAggState
 

Functions

static TimeOffset time2t (const int hour, const int min, const int sec, const fsec_t fsec)
 
static Timestamp dt2local (Timestamp dt, int timezone)
 
static bool AdjustIntervalForTypmod (Interval *interval, int32 typmod, Node *escontext)
 
static TimestampTz timestamp2timestamptz (Timestamp timestamp)
 
static Timestamp timestamptz2timestamp (TimestampTz timestamp)
 
static void EncodeSpecialInterval (const Interval *interval, char *str)
 
static void interval_um_internal (const Interval *interval, Interval *result)
 
static int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
static charanytimestamp_typmodout (bool istz, int32 typmod)
 
Datum timestamp_in (PG_FUNCTION_ARGS)
 
Datum timestamp_out (PG_FUNCTION_ARGS)
 
Datum timestamp_recv (PG_FUNCTION_ARGS)
 
Datum timestamp_send (PG_FUNCTION_ARGS)
 
Datum timestamptypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptypmodout (PG_FUNCTION_ARGS)
 
Datum timestamp_support (PG_FUNCTION_ARGS)
 
Datum timestamp_scale (PG_FUNCTION_ARGS)
 
bool AdjustTimestampForTypmod (Timestamp *time, int32 typmod, Node *escontext)
 
Datum timestamptz_in (PG_FUNCTION_ARGS)
 
static int parse_sane_timezone (struct pg_tm *tm, text *zone)
 
static pg_tzlookup_timezone (text *zone)
 
static Timestamp make_timestamp_internal (int year, int month, int day, int hour, int min, double sec)
 
Datum make_timestamp (PG_FUNCTION_ARGS)
 
Datum make_timestamptz (PG_FUNCTION_ARGS)
 
Datum make_timestamptz_at_timezone (PG_FUNCTION_ARGS)
 
Datum float8_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_out (PG_FUNCTION_ARGS)
 
Datum timestamptz_recv (PG_FUNCTION_ARGS)
 
Datum timestamptz_send (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodout (PG_FUNCTION_ARGS)
 
Datum timestamptz_scale (PG_FUNCTION_ARGS)
 
Datum interval_in (PG_FUNCTION_ARGS)
 
Datum interval_out (PG_FUNCTION_ARGS)
 
Datum interval_recv (PG_FUNCTION_ARGS)
 
Datum interval_send (PG_FUNCTION_ARGS)
 
Datum intervaltypmodin (PG_FUNCTION_ARGS)
 
Datum intervaltypmodout (PG_FUNCTION_ARGS)
 
static int intervaltypmodleastfield (int32 typmod)
 
Datum interval_support (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
Datum timeofday (PG_FUNCTION_ARGS)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
long TimestampDifferenceMilliseconds (TimestampTz start_time, TimestampTz stop_time)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
bool TimestampDifferenceExceedsSeconds (TimestampTz start_time, TimestampTz stop_time, int threshold_sec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const chartimestamptz_to_str (TimestampTz t)
 
void dt2time (Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
int tm2timestamp (struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
 
void interval2itm (Interval span, struct pg_itm *itm)
 
int itm2interval (struct pg_itm *itm, Interval *span)
 
int itmin2interval (struct pg_itm_in *itm_in, Interval *span)
 
Datum timestamp_finite (PG_FUNCTION_ARGS)
 
Datum interval_finite (PG_FUNCTION_ARGS)
 
void GetEpochTime (struct pg_tm *tm)
 
Timestamp SetEpochTimestamp (void)
 
int timestamp_cmp_internal (Timestamp dt1, Timestamp dt2)
 
Datum timestamp_eq (PG_FUNCTION_ARGS)
 
Datum timestamp_ne (PG_FUNCTION_ARGS)
 
Datum timestamp_lt (PG_FUNCTION_ARGS)
 
Datum timestamp_gt (PG_FUNCTION_ARGS)
 
Datum timestamp_le (PG_FUNCTION_ARGS)
 
Datum timestamp_ge (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp (PG_FUNCTION_ARGS)
 
Datum timestamp_sortsupport (PG_FUNCTION_ARGS)
 
static Datum timestamp_decrement (Relation rel, Datum existing, bool *underflow)
 
static Datum timestamp_increment (Relation rel, Datum existing, bool *overflow)
 
Datum timestamp_skipsupport (PG_FUNCTION_ARGS)
 
Datum timestamp_hash (PG_FUNCTION_ARGS)
 
Datum timestamp_hash_extended (PG_FUNCTION_ARGS)
 
Datum timestamptz_hash (PG_FUNCTION_ARGS)
 
Datum timestamptz_hash_extended (PG_FUNCTION_ARGS)
 
int32 timestamp_cmp_timestamptz_internal (Timestamp timestampVal, TimestampTz dt2)
 
Datum timestamp_eq_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ne_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_lt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_gt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_le_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ge_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_eq_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ne_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_lt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_gt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_le_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ge_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_cmp_timestamp (PG_FUNCTION_ARGS)
 
static INT128 interval_cmp_value (const Interval *interval)
 
static int interval_cmp_internal (const Interval *interval1, const Interval *interval2)
 
static int interval_sign (const Interval *interval)
 
Datum interval_eq (PG_FUNCTION_ARGS)
 
Datum interval_ne (PG_FUNCTION_ARGS)
 
Datum interval_lt (PG_FUNCTION_ARGS)
 
Datum interval_gt (PG_FUNCTION_ARGS)
 
Datum interval_le (PG_FUNCTION_ARGS)
 
Datum interval_ge (PG_FUNCTION_ARGS)
 
Datum interval_cmp (PG_FUNCTION_ARGS)
 
Datum interval_hash (PG_FUNCTION_ARGS)
 
Datum interval_hash_extended (PG_FUNCTION_ARGS)
 
Datum overlaps_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_smaller (PG_FUNCTION_ARGS)
 
Datum timestamp_larger (PG_FUNCTION_ARGS)
 
Datum timestamp_mi (PG_FUNCTION_ARGS)
 
Datum interval_justify_interval (PG_FUNCTION_ARGS)
 
Datum interval_justify_hours (PG_FUNCTION_ARGS)
 
Datum interval_justify_days (PG_FUNCTION_ARGS)
 
Datum timestamp_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_mi_interval (PG_FUNCTION_ARGS)
 
static TimestampTz timestamptz_pl_interval_internal (TimestampTz timestamp, Interval *span, pg_tz *attimezone)
 
static TimestampTz timestamptz_mi_interval_internal (TimestampTz timestamp, Interval *span, pg_tz *attimezone)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_pl_interval_at_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval_at_zone (PG_FUNCTION_ARGS)
 
Datum interval_um (PG_FUNCTION_ARGS)
 
Datum interval_smaller (PG_FUNCTION_ARGS)
 
Datum interval_larger (PG_FUNCTION_ARGS)
 
static void finite_interval_pl (const Interval *span1, const Interval *span2, Interval *result)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
static void finite_interval_mi (const Interval *span1, const Interval *span2, Interval *result)
 
Datum interval_mi (PG_FUNCTION_ARGS)
 
Datum interval_mul (PG_FUNCTION_ARGS)
 
Datum mul_d_interval (PG_FUNCTION_ARGS)
 
Datum interval_div (PG_FUNCTION_ARGS)
 
Datum in_range_timestamptz_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timestamp_interval (PG_FUNCTION_ARGS)
 
Datum in_range_interval_interval (PG_FUNCTION_ARGS)
 
static IntervalAggStatemakeIntervalAggState (FunctionCallInfo fcinfo)
 
static void do_interval_accum (IntervalAggState *state, Interval *newval)
 
static void do_interval_discard (IntervalAggState *state, Interval *newval)
 
Datum interval_avg_accum (PG_FUNCTION_ARGS)
 
Datum interval_avg_combine (PG_FUNCTION_ARGS)
 
Datum interval_avg_serialize (PG_FUNCTION_ARGS)
 
Datum interval_avg_deserialize (PG_FUNCTION_ARGS)
 
Datum interval_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (PG_FUNCTION_ARGS)
 
Datum interval_sum (PG_FUNCTION_ARGS)
 
Datum timestamp_age (PG_FUNCTION_ARGS)
 
Datum timestamptz_age (PG_FUNCTION_ARGS)
 
Datum timestamp_bin (PG_FUNCTION_ARGS)
 
Datum timestamp_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_bin (PG_FUNCTION_ARGS)
 
static TimestampTz timestamptz_trunc_internal (text *units, TimestampTz timestamp, pg_tz *tzp)
 
Datum timestamptz_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc_zone (PG_FUNCTION_ARGS)
 
Datum interval_trunc (PG_FUNCTION_ARGS)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
static float8 NonFiniteTimestampTzPart (int type, int unit, char *lowunits, bool isNegative, bool isTz)
 
static Datum timestamp_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamp (PG_FUNCTION_ARGS)
 
static Datum timestamptz_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamptz (PG_FUNCTION_ARGS)
 
static float8 NonFiniteIntervalPart (int type, int unit, char *lowunits, bool isNegative)
 
static Datum interval_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum extract_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
bool TimestampTimestampTzRequiresRewrite (void)
 
Datum timestamp_timestamptz (PG_FUNCTION_ARGS)
 
TimestampTz timestamp2timestamptz_safe (Timestamp timestamp, Node *escontext)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Timestamp timestamptz2timestamp_safe (TimestampTz timestamp, Node *escontext)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
static Datum generate_series_timestamptz_internal (FunctionCallInfo fcinfo)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz_at_zone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp_support (PG_FUNCTION_ARGS)
 
Datum timestamp_at_local (PG_FUNCTION_ARGS)
 
Datum timestamptz_at_local (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ IA_TOTAL_COUNT

#define IA_TOTAL_COUNT (   ia)     ((ia)->N + (ia)->pInfcount + (ia)->nInfcount)

Definition at line 80 of file timestamp.c.

◆ INTERVAL_TO_MICROSECONDS

#define INTERVAL_TO_MICROSECONDS (   i)    ((((double) (i)->month * DAYS_PER_MONTH + (i)->day)) * USECS_PER_DAY + (i)->time)

◆ TIMESTAMP_GT

#define TIMESTAMP_GT (   t1,
  t2 
)     DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))

◆ TIMESTAMP_LT

#define TIMESTAMP_LT (   t1,
  t2 
)     DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))

Typedef Documentation

◆ IntervalAggState

Function Documentation

◆ AdjustIntervalForTypmod()

static bool AdjustIntervalForTypmod ( Interval interval,
int32  typmod,
Node escontext 
)
static

Definition at line 1341 of file timestamp.c.

1343{
1344 static const int64 IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
1345 INT64CONST(1000000),
1346 INT64CONST(100000),
1347 INT64CONST(10000),
1348 INT64CONST(1000),
1349 INT64CONST(100),
1350 INT64CONST(10),
1351 INT64CONST(1)
1352 };
1353
1354 static const int64 IntervalOffsets[MAX_INTERVAL_PRECISION + 1] = {
1355 INT64CONST(500000),
1356 INT64CONST(50000),
1357 INT64CONST(5000),
1358 INT64CONST(500),
1359 INT64CONST(50),
1360 INT64CONST(5),
1361 INT64CONST(0)
1362 };
1363
1364 /* Typmod has no effect on infinite intervals */
1366 return true;
1367
1368 /*
1369 * Unspecified range and precision? Then not necessary to adjust. Setting
1370 * typmod to -1 is the convention for all data types.
1371 */
1372 if (typmod >= 0)
1373 {
1374 int range = INTERVAL_RANGE(typmod);
1375 int precision = INTERVAL_PRECISION(typmod);
1376
1377 /*
1378 * Our interpretation of intervals with a limited set of fields is
1379 * that fields to the right of the last one specified are zeroed out,
1380 * but those to the left of it remain valid. Thus for example there
1381 * is no operational difference between INTERVAL YEAR TO MONTH and
1382 * INTERVAL MONTH. In some cases we could meaningfully enforce that
1383 * higher-order fields are zero; for example INTERVAL DAY could reject
1384 * nonzero "month" field. However that seems a bit pointless when we
1385 * can't do it consistently. (We cannot enforce a range limit on the
1386 * highest expected field, since we do not have any equivalent of
1387 * SQL's <interval leading field precision>.) If we ever decide to
1388 * revisit this, interval_support will likely require adjusting.
1389 *
1390 * Note: before PG 8.4 we interpreted a limited set of fields as
1391 * actually causing a "modulo" operation on a given value, potentially
1392 * losing high-order as well as low-order information. But there is
1393 * no support for such behavior in the standard, and it seems fairly
1394 * undesirable on data consistency grounds anyway. Now we only
1395 * perform truncation or rounding of low-order fields.
1396 */
1398 {
1399 /* Do nothing... */
1400 }
1401 else if (range == INTERVAL_MASK(YEAR))
1402 {
1404 interval->day = 0;
1405 interval->time = 0;
1406 }
1407 else if (range == INTERVAL_MASK(MONTH))
1408 {
1409 interval->day = 0;
1410 interval->time = 0;
1411 }
1412 /* YEAR TO MONTH */
1413 else if (range == (INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH)))
1414 {
1415 interval->day = 0;
1416 interval->time = 0;
1417 }
1418 else if (range == INTERVAL_MASK(DAY))
1419 {
1420 interval->time = 0;
1421 }
1422 else if (range == INTERVAL_MASK(HOUR))
1423 {
1426 }
1427 else if (range == INTERVAL_MASK(MINUTE))
1428 {
1431 }
1432 else if (range == INTERVAL_MASK(SECOND))
1433 {
1434 /* fractional-second rounding will be dealt with below */
1435 }
1436 /* DAY TO HOUR */
1437 else if (range == (INTERVAL_MASK(DAY) |
1439 {
1442 }
1443 /* DAY TO MINUTE */
1444 else if (range == (INTERVAL_MASK(DAY) |
1447 {
1450 }
1451 /* DAY TO SECOND */
1452 else if (range == (INTERVAL_MASK(DAY) |
1456 {
1457 /* fractional-second rounding will be dealt with below */
1458 }
1459 /* HOUR TO MINUTE */
1460 else if (range == (INTERVAL_MASK(HOUR) |
1462 {
1465 }
1466 /* HOUR TO SECOND */
1467 else if (range == (INTERVAL_MASK(HOUR) |
1470 {
1471 /* fractional-second rounding will be dealt with below */
1472 }
1473 /* MINUTE TO SECOND */
1474 else if (range == (INTERVAL_MASK(MINUTE) |
1476 {
1477 /* fractional-second rounding will be dealt with below */
1478 }
1479 else
1480 elog(ERROR, "unrecognized interval typmod: %d", typmod);
1481
1482 /* Need to adjust sub-second precision? */
1483 if (precision != INTERVAL_FULL_PRECISION)
1484 {
1486 ereturn(escontext, false,
1488 errmsg("interval(%d) precision must be between %d and %d",
1489 precision, 0, MAX_INTERVAL_PRECISION)));
1490
1491 if (interval->time >= INT64CONST(0))
1492 {
1494 IntervalOffsets[precision],
1495 &interval->time))
1496 ereturn(escontext, false,
1498 errmsg("interval out of range")));
1499 interval->time -= interval->time % IntervalScales[precision];
1500 }
1501 else
1502 {
1504 IntervalOffsets[precision],
1505 &interval->time))
1506 ereturn(escontext, false,
1508 errmsg("interval out of range")));
1509 interval->time -= interval->time % IntervalScales[precision];
1510 }
1511 }
1512 }
1513
1514 return true;
1515}
#define INT64CONST(x)
Definition c.h:629
int64_t int64
Definition c.h:612
#define INTERVAL_NOT_FINITE(i)
Definition timestamp.h:195
#define USECS_PER_HOUR
Definition timestamp.h:132
#define MONTHS_PER_YEAR
Definition timestamp.h:108
#define MAX_INTERVAL_PRECISION
Definition timestamp.h:93
#define USECS_PER_MINUTE
Definition timestamp.h:133
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereturn(context, dummy_value,...)
Definition elog.h:278
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define MONTH
Definition datetime.h:91
#define HOUR
Definition datetime.h:100
#define DAY
Definition datetime.h:93
#define YEAR
Definition datetime.h:92
#define SECOND
Definition datetime.h:102
#define MINUTE
Definition datetime.h:101
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:262
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235
static char * errmsg
static int fb(int x)
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
#define INTERVAL_FULL_RANGE
Definition timestamp.h:76
#define INTERVAL_PRECISION(t)
Definition timestamp.h:81
#define INTERVAL_RANGE(t)
Definition timestamp.h:82
#define INTERVAL_MASK(b)
Definition timestamp.h:73
#define INTERVAL_FULL_PRECISION
Definition timestamp.h:78

References DAY, elog, ereturn, errcode(), errmsg, ERROR, fb(), HOUR, INT64CONST, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_NOT_FINITE, INTERVAL_PRECISION, INTERVAL_RANGE, MAX_INTERVAL_PRECISION, MINUTE, MONTH, interval::month, MONTHS_PER_YEAR, pg_add_s64_overflow(), pg_sub_s64_overflow(), range(), SECOND, interval::time, USECS_PER_HOUR, USECS_PER_MINUTE, and YEAR.

Referenced by interval_in(), interval_recv(), and interval_scale().

◆ AdjustTimestampForTypmod()

bool AdjustTimestampForTypmod ( Timestamp time,
int32  typmod,
Node escontext 
)

Definition at line 359 of file timestamp.c.

360{
361 static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
362 INT64CONST(1000000),
363 INT64CONST(100000),
364 INT64CONST(10000),
365 INT64CONST(1000),
366 INT64CONST(100),
367 INT64CONST(10),
368 INT64CONST(1)
369 };
370
372 INT64CONST(500000),
373 INT64CONST(50000),
374 INT64CONST(5000),
375 INT64CONST(500),
376 INT64CONST(50),
377 INT64CONST(5),
378 INT64CONST(0)
379 };
380
381 if (!TIMESTAMP_NOT_FINITE(*time)
382 && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
383 {
385 ereturn(escontext, false,
387 errmsg("timestamp(%d) precision must be between %d and %d",
388 typmod, 0, MAX_TIMESTAMP_PRECISION)));
389
390 if (*time >= INT64CONST(0))
391 {
392 *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
393 TimestampScales[typmod];
394 }
395 else
396 {
397 *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
398 * TimestampScales[typmod]);
399 }
400 }
401
402 return true;
403}
#define MAX_TIMESTAMP_PRECISION
Definition timestamp.h:92
#define TIMESTAMP_NOT_FINITE(j)
Definition timestamp.h:169

References ereturn, errcode(), errmsg, fb(), INT64CONST, MAX_TIMESTAMP_PRECISION, and TIMESTAMP_NOT_FINITE.

Referenced by executeDateTimeMethod(), GetSQLCurrentTimestamp(), GetSQLLocalTimestamp(), parse_datetime(), timestamp_in(), timestamp_recv(), timestamp_scale(), timestamptz_in(), timestamptz_recv(), timestamptz_scale(), and to_timestamp().

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 116 of file timestamp.c.

117{
118 if (typmod < 0)
121 errmsg("TIMESTAMP(%d)%s precision must not be negative",
122 typmod, (istz ? " WITH TIME ZONE" : ""))));
123 if (typmod > MAX_TIMESTAMP_PRECISION)
124 {
127 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
128 typmod, (istz ? " WITH TIME ZONE" : ""),
131 }
132
133 return typmod;
134}
#define WARNING
Definition elog.h:36
#define ereport(elevel,...)
Definition elog.h:150

References ereport, errcode(), errmsg, ERROR, fb(), MAX_TIMESTAMP_PRECISION, and WARNING.

Referenced by anytimestamp_typmodin(), executeDateTimeMethod(), and transformSQLValueFunction().

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 95 of file timestamp.c.

96{
97 int32 *tl;
98 int n;
99
101
102 /*
103 * we're not too tense about good error message here because grammar
104 * shouldn't allow wrong number of modifiers for TIMESTAMP
105 */
106 if (n != 1)
109 errmsg("invalid type modifier")));
110
112}
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition arrayutils.c:233
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition timestamp.c:116
int32_t int32
Definition c.h:611

References anytimestamp_typmod_check(), ArrayGetIntegerTypmods(), ereport, errcode(), errmsg, ERROR, and fb().

Referenced by timestamptypmodin(), and timestamptztypmodin().

◆ anytimestamp_typmodout()

static char * anytimestamp_typmodout ( bool  istz,
int32  typmod 
)
static

Definition at line 138 of file timestamp.c.

139{
140 const char *tz = istz ? " with time zone" : " without time zone";
141
142 if (typmod >= 0)
143 return psprintf("(%d)%s", (int) typmod, tz);
144 else
145 return pstrdup(tz);
146}
char * pstrdup(const char *in)
Definition mcxt.c:1781
char * psprintf(const char *fmt,...)
Definition psprintf.c:43

References fb(), psprintf(), and pstrdup().

Referenced by timestamptypmodout(), and timestamptztypmodout().

◆ clock_timestamp()

Datum clock_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1612 of file timestamp.c.

1613{
1615}
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1636
#define PG_RETURN_TIMESTAMPTZ(x)
Definition timestamp.h:68

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

◆ date2isoweek()

int date2isoweek ( int  year,
int  mon,
int  mday 
)

Definition at line 5286 of file timestamp.c.

5287{
5288 int day0,
5289 day4,
5290 dayn,
5291 week;
5292
5293 /* current day */
5294 dayn = date2j(year, mon, mday);
5295
5296 /* fourth day of current year */
5297 day4 = date2j(year, 1, 4);
5298
5299 /* day0 == offset to first day of week (Monday) */
5300 day0 = j2day(day4 - 1);
5301
5302 /*
5303 * We need the first week containing a Thursday, otherwise this day falls
5304 * into the previous year for purposes of counting weeks
5305 */
5306 if (dayn < day4 - day0)
5307 {
5308 day4 = date2j(year - 1, 1, 4);
5309
5310 /* day0 == offset to first day of week (Monday) */
5311 day0 = j2day(day4 - 1);
5312 }
5313
5314 week = (dayn - (day4 - day0)) / 7 + 1;
5315
5316 /*
5317 * Sometimes the last few days in a year will fall into the first week of
5318 * the next year, so check for this.
5319 */
5320 if (week >= 52)
5321 {
5322 day4 = date2j(year + 1, 1, 4);
5323
5324 /* day0 == offset to first day of week (Monday) */
5325 day0 = j2day(day4 - 1);
5326
5327 if (dayn >= day4 - day0)
5328 week = (dayn - (day4 - day0)) / 7 + 1;
5329 }
5330
5331 return week;
5332}
int j2day(int date)
Definition datetime.c:355
int date2j(int year, int month, int day)
Definition datetime.c:297

References date2j(), fb(), and j2day().

Referenced by DCH_to_char(), extract_date(), timestamp_part_common(), timestamp_trunc(), timestamptz_part_common(), and timestamptz_trunc_internal().

◆ date2isoyear()

int date2isoyear ( int  year,
int  mon,
int  mday 
)

Definition at line 5341 of file timestamp.c.

5342{
5343 int day0,
5344 day4,
5345 dayn,
5346 week;
5347
5348 /* current day */
5349 dayn = date2j(year, mon, mday);
5350
5351 /* fourth day of current year */
5352 day4 = date2j(year, 1, 4);
5353
5354 /* day0 == offset to first day of week (Monday) */
5355 day0 = j2day(day4 - 1);
5356
5357 /*
5358 * We need the first week containing a Thursday, otherwise this day falls
5359 * into the previous year for purposes of counting weeks
5360 */
5361 if (dayn < day4 - day0)
5362 {
5363 day4 = date2j(year - 1, 1, 4);
5364
5365 /* day0 == offset to first day of week (Monday) */
5366 day0 = j2day(day4 - 1);
5367
5368 year--;
5369 }
5370
5371 week = (dayn - (day4 - day0)) / 7 + 1;
5372
5373 /*
5374 * Sometimes the last few days in a year will fall into the first week of
5375 * the next year, so check for this.
5376 */
5377 if (week >= 52)
5378 {
5379 day4 = date2j(year + 1, 1, 4);
5380
5381 /* day0 == offset to first day of week (Monday) */
5382 day0 = j2day(day4 - 1);
5383
5384 if (dayn >= day4 - day0)
5385 year++;
5386 }
5387
5388 return year;
5389}

References date2j(), fb(), and j2day().

Referenced by date2isoyearday(), DCH_to_char(), extract_date(), timestamp_part_common(), and timestamptz_part_common().

◆ date2isoyearday()

int date2isoyearday ( int  year,
int  mon,
int  mday 
)

Definition at line 5398 of file timestamp.c.

5399{
5400 return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
5401}
int isoweek2j(int year, int week)
Definition timestamp.c:5235
int date2isoyear(int year, int mon, int mday)
Definition timestamp.c:5341

References date2isoyear(), date2j(), fb(), and isoweek2j().

Referenced by DCH_to_char().

◆ do_interval_accum()

static void do_interval_accum ( IntervalAggState state,
Interval newval 
)
static

Definition at line 3980 of file timestamp.c.

3981{
3982 /* Infinite inputs are counted separately, and do not affect "N" */
3984 {
3985 state->nInfcount++;
3986 return;
3987 }
3988
3990 {
3991 state->pInfcount++;
3992 return;
3993 }
3994
3995 finite_interval_pl(&state->sumX, newval, &state->sumX);
3996 state->N++;
3997}
static void finite_interval_pl(const Interval *span1, const Interval *span2, Interval *result)
Definition timestamp.c:3479
#define INTERVAL_IS_NOBEGIN(i)
Definition timestamp.h:182
#define INTERVAL_IS_NOEND(i)
Definition timestamp.h:192
#define newval

References finite_interval_pl(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, and newval.

Referenced by interval_avg_accum().

◆ do_interval_discard()

static void do_interval_discard ( IntervalAggState state,
Interval newval 
)
static

Definition at line 4003 of file timestamp.c.

4004{
4005 /* Infinite inputs are counted separately, and do not affect "N" */
4007 {
4008 state->nInfcount--;
4009 return;
4010 }
4011
4013 {
4014 state->pInfcount--;
4015 return;
4016 }
4017
4018 /* Handle the to-be-discarded finite value. */
4019 state->N--;
4020 if (state->N > 0)
4021 finite_interval_mi(&state->sumX, newval, &state->sumX);
4022 else
4023 {
4024 /* All values discarded, reset the state */
4025 Assert(state->N == 0);
4026 memset(&state->sumX, 0, sizeof(state->sumX));
4027 }
4028}
static void finite_interval_mi(const Interval *span1, const Interval *span2, Interval *result)
Definition timestamp.c:3535
#define Assert(condition)
Definition c.h:942

References Assert, fb(), finite_interval_mi(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, and newval.

Referenced by interval_avg_accum_inv().

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 2125 of file timestamp.c.

2126{
2127 dt -= (timezone * USECS_PER_SEC);
2128 return dt;
2129}
#define USECS_PER_SEC
Definition timestamp.h:134

References fb(), and USECS_PER_SEC.

◆ dt2time()

void dt2time ( Timestamp  jd,
int hour,
int min,
int sec,
fsec_t fsec 
)

Definition at line 1874 of file timestamp.c.

1875{
1876 TimeOffset time;
1877
1878 time = jd;
1879
1880 *hour = time / USECS_PER_HOUR;
1881 time -= (*hour) * USECS_PER_HOUR;
1882 *min = time / USECS_PER_MINUTE;
1883 time -= (*min) * USECS_PER_MINUTE;
1884 *sec = time / USECS_PER_SEC;
1885 *fsec = time - (*sec * USECS_PER_SEC);
1886} /* dt2time() */
int64 TimeOffset
Definition timestamp.h:40

References fb(), USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by DecodeDateTime(), DecodeTimeOnly(), timestamp2tm(), and timestamp2tm().

◆ EncodeSpecialInterval()

static void EncodeSpecialInterval ( const Interval interval,
char str 
)
static

Definition at line 1589 of file timestamp.c.

1590{
1592 strcpy(str, EARLY);
1593 else if (INTERVAL_IS_NOEND(interval))
1594 strcpy(str, LATE);
1595 else /* shouldn't happen */
1596 elog(ERROR, "invalid argument for EncodeSpecialInterval");
1597}
const char * str
#define EARLY
Definition datetime.h:39
#define LATE
Definition datetime.h:40

References EARLY, elog, ERROR, fb(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, LATE, and str.

Referenced by interval_out().

◆ EncodeSpecialTimestamp()

void EncodeSpecialTimestamp ( Timestamp  dt,
char str 
)

Definition at line 1578 of file timestamp.c.

1579{
1581 strcpy(str, EARLY);
1582 else if (TIMESTAMP_IS_NOEND(dt))
1583 strcpy(str, LATE);
1584 else /* shouldn't happen */
1585 elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1586}
#define TIMESTAMP_IS_NOEND(j)
Definition timestamp.h:167
#define TIMESTAMP_IS_NOBEGIN(j)
Definition timestamp.h:162

References EARLY, elog, ERROR, fb(), LATE, str, TIMESTAMP_IS_NOBEGIN, and TIMESTAMP_IS_NOEND.

Referenced by JsonEncodeDateTime().

◆ extract_interval()

Datum extract_interval ( PG_FUNCTION_ARGS  )

Definition at line 6284 of file timestamp.c.

6285{
6286 return interval_part_common(fcinfo, true);
6287}
static Datum interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition timestamp.c:6071

References interval_part_common().

◆ extract_timestamp()

Datum extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 5736 of file timestamp.c.

5737{
5738 return timestamp_part_common(fcinfo, true);
5739}
static Datum timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition timestamp.c:5472

References timestamp_part_common().

◆ extract_timestamptz()

Datum extract_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 6008 of file timestamp.c.

6009{
6010 return timestamptz_part_common(fcinfo, true);
6011}
static Datum timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition timestamp.c:5745

References timestamptz_part_common().

◆ finite_interval_mi()

static void finite_interval_mi ( const Interval span1,
const Interval span2,
Interval result 
)
static

Definition at line 3535 of file timestamp.c.

3536{
3539
3540 if (pg_sub_s32_overflow(span1->month, span2->month, &result->month) ||
3541 pg_sub_s32_overflow(span1->day, span2->day, &result->day) ||
3542 pg_sub_s64_overflow(span1->time, span2->time, &result->time) ||
3543 INTERVAL_NOT_FINITE(result))
3544 ereport(ERROR,
3546 errmsg("interval out of range")));
3547}
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:169
int32 day
Definition timestamp.h:51
int32 month
Definition timestamp.h:52
TimeOffset time
Definition timestamp.h:49

References Assert, Interval::day, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, Interval::month, pg_sub_s32_overflow(), pg_sub_s64_overflow(), and Interval::time.

Referenced by do_interval_discard(), and interval_mi().

◆ finite_interval_pl()

static void finite_interval_pl ( const Interval span1,
const Interval span2,
Interval result 
)
static

Definition at line 3479 of file timestamp.c.

3480{
3483
3484 if (pg_add_s32_overflow(span1->month, span2->month, &result->month) ||
3485 pg_add_s32_overflow(span1->day, span2->day, &result->day) ||
3486 pg_add_s64_overflow(span1->time, span2->time, &result->time) ||
3487 INTERVAL_NOT_FINITE(result))
3488 ereport(ERROR,
3490 errmsg("interval out of range")));
3491}
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:151

References Assert, Interval::day, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, Interval::month, pg_add_s32_overflow(), pg_add_s64_overflow(), and Interval::time.

Referenced by do_interval_accum(), interval_avg_combine(), and interval_pl().

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 717 of file timestamp.c.

718{
720 TimestampTz result;
721
722 /* Deal with NaN and infinite inputs ... */
723 if (isnan(seconds))
726 errmsg("timestamp cannot be NaN")));
727
728 if (isinf(seconds))
729 {
730 if (seconds < 0)
731 TIMESTAMP_NOBEGIN(result);
732 else
733 TIMESTAMP_NOEND(result);
734 }
735 else
736 {
737 /* Out of range? */
738 if (seconds <
740 || seconds >=
744 errmsg("timestamp out of range: \"%g\"", seconds)));
745
746 /* Convert UNIX epoch to Postgres epoch */
748
750 result = (int64) seconds;
751
752 /* Recheck in case roundoff produces something just out of range */
753 if (!IS_VALID_TIMESTAMP(result))
756 errmsg("timestamp out of range: \"%g\"",
757 PG_GETARG_FLOAT8(0))));
758 }
759
760 PG_RETURN_TIMESTAMP(result);
761}
double float8
Definition c.h:713
#define DATETIME_MIN_JULIAN
Definition timestamp.h:251
int64 TimestampTz
Definition timestamp.h:39
#define TIMESTAMP_NOBEGIN(j)
Definition timestamp.h:159
#define TIMESTAMP_END_JULIAN
Definition timestamp.h:253
#define IS_VALID_TIMESTAMP(t)
Definition timestamp.h:267
#define UNIX_EPOCH_JDATE
Definition timestamp.h:234
#define SECS_PER_DAY
Definition timestamp.h:126
#define POSTGRES_EPOCH_JDATE
Definition timestamp.h:235
#define TIMESTAMP_NOEND(j)
Definition timestamp.h:164
#define PG_GETARG_FLOAT8(n)
Definition fmgr.h:283
#define PG_RETURN_TIMESTAMP(x)
Definition timestamp.h:67

References DATETIME_MIN_JULIAN, ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_FLOAT8, PG_RETURN_TIMESTAMP, POSTGRES_EPOCH_JDATE, SECS_PER_DAY, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

◆ generate_series_timestamp()

Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 6658 of file timestamp.c.

6659{
6662 Timestamp result;
6663
6664 /* stuff done only on the first call of the function */
6665 if (SRF_IS_FIRSTCALL())
6666 {
6668 Timestamp finish = PG_GETARG_TIMESTAMP(1);
6669 Interval *step = PG_GETARG_INTERVAL_P(2);
6670 MemoryContext oldcontext;
6671
6672 /* create a function context for cross-call persistence */
6674
6675 /*
6676 * switch to memory context appropriate for multiple function calls
6677 */
6678 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
6679
6680 /* allocate memory for user context */
6682
6683 /*
6684 * Use fctx to keep state from call to call. Seed current with the
6685 * original start value
6686 */
6687 fctx->current = start;
6688 fctx->finish = finish;
6689 fctx->step = *step;
6690
6691 /* Determine sign of the interval */
6692 fctx->step_sign = interval_sign(&fctx->step);
6693
6694 if (fctx->step_sign == 0)
6695 ereport(ERROR,
6697 errmsg("step size cannot equal zero")));
6698
6699 if (INTERVAL_NOT_FINITE((&fctx->step)))
6700 ereport(ERROR,
6702 errmsg("step size cannot be infinite")));
6703
6704 funcctx->user_fctx = fctx;
6705 MemoryContextSwitchTo(oldcontext);
6706 }
6707
6708 /* stuff done on every call of the function */
6710
6711 /*
6712 * get the saved state and use current as the result for this iteration
6713 */
6714 fctx = funcctx->user_fctx;
6715 result = fctx->current;
6716
6717 if (fctx->step_sign > 0 ?
6718 timestamp_cmp_internal(result, fctx->finish) <= 0 :
6719 timestamp_cmp_internal(result, fctx->finish) >= 0)
6720 {
6721 /* increment current in preparation for next iteration */
6723 TimestampGetDatum(fctx->current),
6724 PointerGetDatum(&fctx->step)));
6725
6726 /* do when there is more left to send */
6728 }
6729 else
6730 {
6731 /* do when there is no more left */
6733 }
6734}
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition timestamp.c:2201
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition timestamp.c:3081
static int interval_sign(const Interval *interval)
Definition timestamp.c:2546
int64 Timestamp
Definition timestamp.h:38
#define palloc_object(type)
Definition fe_memutils.h:74
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
#define SRF_IS_FIRSTCALL()
Definition funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition funcapi.h:328
return str start
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Datum TimestampGetDatum(Timestamp X)
Definition timestamp.h:46
#define PG_GETARG_TIMESTAMP(n)
Definition timestamp.h:63
#define PG_GETARG_INTERVAL_P(n)
Definition timestamp.h:65
static Timestamp DatumGetTimestamp(Datum X)
Definition timestamp.h:28

References DatumGetTimestamp(), DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_sign(), MemoryContextSwitchTo(), palloc_object, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, start, timestamp_cmp_internal(), timestamp_pl_interval(), and TimestampGetDatum().

◆ generate_series_timestamp_support()

Datum generate_series_timestamp_support ( PG_FUNCTION_ARGS  )

Definition at line 6837 of file timestamp.c.

6838{
6840 Node *ret = NULL;
6841
6843 {
6844 /* Try to estimate the number of rows returned */
6846
6847 if (is_funcclause(req->node)) /* be paranoid */
6848 {
6849 List *args = ((FuncExpr *) req->node)->args;
6850 Node *arg1,
6851 *arg2,
6852 *arg3;
6853
6854 /* We can use estimated argument values here */
6856 arg2 = estimate_expression_value(req->root, lsecond(args));
6857 arg3 = estimate_expression_value(req->root, lthird(args));
6858
6859 /*
6860 * If any argument is constant NULL, we can safely assume that
6861 * zero rows are returned. Otherwise, if they're all non-NULL
6862 * constants, we can calculate the number of rows that will be
6863 * returned.
6864 */
6865 if ((IsA(arg1, Const) && ((Const *) arg1)->constisnull) ||
6866 (IsA(arg2, Const) && ((Const *) arg2)->constisnull) ||
6867 (IsA(arg3, Const) && ((Const *) arg3)->constisnull))
6868 {
6869 req->rows = 0;
6870 ret = (Node *) req;
6871 }
6872 else if (IsA(arg1, Const) && IsA(arg2, Const) && IsA(arg3, Const))
6873 {
6875 finish;
6876 Interval *step;
6877 Datum diff;
6878 double dstep;
6879 int64 dummy;
6880
6882 finish = DatumGetTimestamp(((Const *) arg2)->constvalue);
6883 step = DatumGetIntervalP(((Const *) arg3)->constvalue);
6884
6885 /*
6886 * Perform some prechecks which could cause timestamp_mi to
6887 * raise an ERROR. It's much better to just return some
6888 * default estimate than error out in a support function.
6889 */
6891 !pg_sub_s64_overflow(finish, start, &dummy))
6892 {
6894 TimestampGetDatum(finish),
6896
6897#define INTERVAL_TO_MICROSECONDS(i) ((((double) (i)->month * DAYS_PER_MONTH + (i)->day)) * USECS_PER_DAY + (i)->time)
6898
6900
6901 /* This equation works for either sign of step */
6902 if (dstep != 0.0)
6903 {
6906
6907 req->rows = floor(ddiff / dstep + 1.0);
6908 ret = (Node *) req;
6909 }
6910#undef INTERVAL_TO_MICROSECONDS
6911 }
6912 }
6913 }
6914 }
6915
6916 PG_RETURN_POINTER(ret);
6917}
Datum timestamp_mi(PG_FUNCTION_ARGS)
Definition timestamp.c:2818
#define INTERVAL_TO_MICROSECONDS(i)
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition clauses.c:2639
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
static bool is_funcclause(const void *clause)
Definition nodeFuncs.h:69
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define lthird(l)
Definition pg_list.h:188
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
uint64_t Datum
Definition postgres.h:70
Definition pg_list.h:54
Definition nodes.h:135
static Interval * DatumGetIntervalP(Datum X)
Definition timestamp.h:40

References DatumGetIntervalP(), DatumGetTimestamp(), DirectFunctionCall2, estimate_expression_value(), fb(), INTERVAL_TO_MICROSECONDS, is_funcclause(), IsA, linitial, lsecond, lthird, PG_GETARG_POINTER, PG_RETURN_POINTER, pg_sub_s64_overflow(), start, timestamp_mi(), TIMESTAMP_NOT_FINITE, and TimestampGetDatum().

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 6822 of file timestamp.c.

6823{
6825}
static Datum generate_series_timestamptz_internal(FunctionCallInfo fcinfo)
Definition timestamp.c:6741

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_at_zone()

Datum generate_series_timestamptz_at_zone ( PG_FUNCTION_ARGS  )

Definition at line 6828 of file timestamp.c.

6829{
6831}

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_internal()

static Datum generate_series_timestamptz_internal ( FunctionCallInfo  fcinfo)
static

Definition at line 6741 of file timestamp.c.

6742{
6745 TimestampTz result;
6746
6747 /* stuff done only on the first call of the function */
6748 if (SRF_IS_FIRSTCALL())
6749 {
6752 Interval *step = PG_GETARG_INTERVAL_P(2);
6753 text *zone = (PG_NARGS() == 4) ? PG_GETARG_TEXT_PP(3) : NULL;
6754 MemoryContext oldcontext;
6755
6756 /* create a function context for cross-call persistence */
6758
6759 /*
6760 * switch to memory context appropriate for multiple function calls
6761 */
6762 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
6763
6764 /* allocate memory for user context */
6766
6767 /*
6768 * Use fctx to keep state from call to call. Seed current with the
6769 * original start value
6770 */
6771 fctx->current = start;
6772 fctx->finish = finish;
6773 fctx->step = *step;
6774 fctx->attimezone = zone ? lookup_timezone(zone) : session_timezone;
6775
6776 /* Determine sign of the interval */
6777 fctx->step_sign = interval_sign(&fctx->step);
6778
6779 if (fctx->step_sign == 0)
6780 ereport(ERROR,
6782 errmsg("step size cannot equal zero")));
6783
6784 if (INTERVAL_NOT_FINITE((&fctx->step)))
6785 ereport(ERROR,
6787 errmsg("step size cannot be infinite")));
6788
6789 funcctx->user_fctx = fctx;
6790 MemoryContextSwitchTo(oldcontext);
6791 }
6792
6793 /* stuff done on every call of the function */
6795
6796 /*
6797 * get the saved state and use current as the result for this iteration
6798 */
6799 fctx = funcctx->user_fctx;
6800 result = fctx->current;
6801
6802 if (fctx->step_sign > 0 ?
6803 timestamp_cmp_internal(result, fctx->finish) <= 0 :
6804 timestamp_cmp_internal(result, fctx->finish) >= 0)
6805 {
6806 /* increment current in preparation for next iteration */
6807 fctx->current = timestamptz_pl_interval_internal(fctx->current,
6808 &fctx->step,
6809 fctx->attimezone);
6810
6811 /* do when there is more left to send */
6813 }
6814 else
6815 {
6816 /* do when there is no more left */
6818 }
6819}
static pg_tz * lookup_timezone(text *zone)
Definition timestamp.c:551
static TimestampTz timestamptz_pl_interval_internal(TimestampTz timestamp, Interval *span, pg_tz *attimezone)
Definition timestamp.c:3224
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_NARGS()
Definition fmgr.h:203
PGDLLIMPORT pg_tz * session_timezone
Definition pgtz.c:28
Definition c.h:775
Definition zic.c:99
static Datum TimestampTzGetDatum(TimestampTz X)
Definition timestamp.h:52
#define PG_GETARG_TIMESTAMPTZ(n)
Definition timestamp.h:64

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_sign(), lookup_timezone(), MemoryContextSwitchTo(), palloc_object, PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_NARGS, session_timezone, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, start, timestamp_cmp_internal(), timestamptz_pl_interval_internal(), and TimestampTzGetDatum().

Referenced by generate_series_timestamptz(), and generate_series_timestamptz_at_zone().

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1636 of file timestamp.c.

1637{
1638 TimestampTz result;
1639 struct timeval tp;
1640
1641 gettimeofday(&tp, NULL);
1642
1643 result = (TimestampTz) tp.tv_sec -
1645 result = (result * USECS_PER_SEC) + tp.tv_usec;
1646
1647 return result;
1648}
int gettimeofday(struct timeval *tp, void *tzp)

References fb(), gettimeofday(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by ApplyLauncherMain(), asyncQueueFillWarning(), autoprewarm_main(), AutoVacLauncherMain(), BackendStartup(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), check_log_duration(), CheckPointGuts(), CleanupBackend(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), dblink_cancel_query(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_worker(), enable_startup_progress_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_alloc(), entry_reset(), get_candidate_xid(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), initialize_prng(), InitProcessGlobals(), InvalidatePossiblyObsoleteSlot(), KnownAssignedXidsCompress(), launch_sync_worker(), launcher_determine_sleep(), libpqsrv_cancel(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), pa_send_data(), PerformAuthentication(), PerformWalRecovery(), pg_sleep(), pgfdw_abort_cleanup_begin(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_finish_abort_cleanup(), pgfdw_get_cleanup_result(), pgss_shmem_startup(), pgstat_build_snapshot(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_archiver(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_replslotsync(), pgstat_report_stat(), pgstat_report_vacuum(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_counters(), pgstat_reset_of_kind(), pgstat_reset_slru(), PostgresMain(), PostgresSingleUserMain(), postmaster_child_launch(), PostmasterMain(), PrepareTransaction(), ProcessConfigFileInternal(), ProcessPendingWrites(), ProcessRepliesIfAny(), ProcessStandbyPSRequestMessage(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), ReplicationSlotRelease(), request_publisher_status(), reschedule_timeouts(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), RestoreSlotFromDisk(), send_feedback(), SetCurrentStatementStartTimestamp(), SetupApplyOrSyncWorker(), should_stop_conflict_info_retention(), StartBackgroundWorker(), StartTransaction(), test_pattern(), test_random(), test_random_operations(), throttle(), update_synced_slots_inactive_since(), UpdateWorkerStats(), wait_for_local_flush(), WaitExceedsMaxStandbyDelay(), WaitForLSN(), WaitForWalSummarization(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogPrefetchResetStats(), XLogPrefetchShmemInit(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

Definition at line 2159 of file timestamp.c.

2160{
2161 struct pg_tm *t0;
2162 pg_time_t epoch = 0;
2163
2164 t0 = pg_gmtime(&epoch);
2165
2166 if (t0 == NULL)
2167 elog(ERROR, "could not convert epoch to timestamp: %m");
2168
2169 tm->tm_year = t0->tm_year;
2170 tm->tm_mon = t0->tm_mon;
2171 tm->tm_mday = t0->tm_mday;
2172 tm->tm_hour = t0->tm_hour;
2173 tm->tm_min = t0->tm_min;
2174 tm->tm_sec = t0->tm_sec;
2175
2176 tm->tm_year += 1900;
2177 tm->tm_mon++;
2178}
static struct pg_tm tm
Definition localtime.c:104
int64 pg_time_t
Definition pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition localtime.c:1390
Definition pgtime.h:35
int tm_hour
Definition pgtime.h:38
int tm_mday
Definition pgtime.h:39
int tm_mon
Definition pgtime.h:40
int tm_min
Definition pgtime.h:37
int tm_sec
Definition pgtime.h:36
int tm_year
Definition pgtime.h:41
static const unsigned __int64 epoch

References elog, epoch, ERROR, fb(), pg_gmtime(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by date_in(), PGTYPESdate_from_asc(), SetEpochTimestamp(), and SetEpochTimestamp().

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1654 of file timestamp.c.

1655{
1656 TimestampTz ts;
1657
1659 if (typmod >= 0)
1660 AdjustTimestampForTypmod(&ts, typmod, NULL);
1661 return ts;
1662}
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition timestamp.c:359
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition xact.c:872

References AdjustTimestampForTypmod(), fb(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1668 of file timestamp.c.

1669{
1670 Timestamp ts;
1671
1673 if (typmod >= 0)
1674 AdjustTimestampForTypmod(&ts, typmod, NULL);
1675 return ts;
1676}
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition timestamp.c:6495

References AdjustTimestampForTypmod(), fb(), GetCurrentTransactionStartTimestamp(), and timestamptz2timestamp().

Referenced by ExecEvalSQLValueFunction().

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

Definition at line 3908 of file timestamp.c.

3909{
3911 Interval *base = PG_GETARG_INTERVAL_P(1);
3912 Interval *offset = PG_GETARG_INTERVAL_P(2);
3913 bool sub = PG_GETARG_BOOL(3);
3914 bool less = PG_GETARG_BOOL(4);
3915 Interval *sum;
3916
3917 if (interval_sign(offset) < 0)
3918 ereport(ERROR,
3920 errmsg("invalid preceding or following size in window function")));
3921
3922 /*
3923 * Deal with cases where both base and offset are infinite, and computing
3924 * base +/- offset would cause an error. As for float and numeric types,
3925 * we assume that all values infinitely precede +infinity and infinitely
3926 * follow -infinity. See in_range_float8_float8() for reasoning.
3927 */
3928 if (INTERVAL_IS_NOEND(offset) &&
3929 (sub ? INTERVAL_IS_NOEND(base) : INTERVAL_IS_NOBEGIN(base)))
3931
3932 /* We don't currently bother to avoid overflow hazards here */
3933 if (sub)
3935 IntervalPGetDatum(base),
3936 IntervalPGetDatum(offset)));
3937 else
3939 IntervalPGetDatum(base),
3940 IntervalPGetDatum(offset)));
3941
3942 if (less)
3944 else
3946}
Datum interval_mi(PG_FUNCTION_ARGS)
Definition timestamp.c:3550
Datum interval_pl(PG_FUNCTION_ARGS)
Definition timestamp.c:3494
static int interval_cmp_internal(const Interval *interval1, const Interval *interval2)
Definition timestamp.c:2537
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
long val
Definition informix.c:689
return true
Definition isn.c:130
static Datum IntervalPGetDatum(const Interval *X)
Definition timestamp.h:58

References DatumGetIntervalP(), DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), interval_cmp_internal(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, interval_mi(), interval_pl(), interval_sign(), IntervalPGetDatum(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_RETURN_BOOL, and val.

◆ in_range_timestamp_interval()

Datum in_range_timestamp_interval ( PG_FUNCTION_ARGS  )

Definition at line 3867 of file timestamp.c.

3868{
3871 Interval *offset = PG_GETARG_INTERVAL_P(2);
3872 bool sub = PG_GETARG_BOOL(3);
3873 bool less = PG_GETARG_BOOL(4);
3874 Timestamp sum;
3875
3876 if (interval_sign(offset) < 0)
3877 ereport(ERROR,
3879 errmsg("invalid preceding or following size in window function")));
3880
3881 /*
3882 * Deal with cases where both base and offset are infinite, and computing
3883 * base +/- offset would cause an error. As for float and numeric types,
3884 * we assume that all values infinitely precede +infinity and infinitely
3885 * follow -infinity. See in_range_float8_float8() for reasoning.
3886 */
3887 if (INTERVAL_IS_NOEND(offset) &&
3888 (sub ? TIMESTAMP_IS_NOEND(base) : TIMESTAMP_IS_NOBEGIN(base)))
3890
3891 /* We don't currently bother to avoid overflow hazards here */
3892 if (sub)
3894 TimestampGetDatum(base),
3895 IntervalPGetDatum(offset)));
3896 else
3898 TimestampGetDatum(base),
3899 IntervalPGetDatum(offset)));
3900
3901 if (less)
3902 PG_RETURN_BOOL(val <= sum);
3903 else
3904 PG_RETURN_BOOL(val >= sum);
3905}
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition timestamp.c:3198

References DatumGetTimestamp(), DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOEND, interval_sign(), IntervalPGetDatum(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, timestamp_mi_interval(), timestamp_pl_interval(), TimestampGetDatum(), and val.

Referenced by in_range_date_interval().

◆ in_range_timestamptz_interval()

Datum in_range_timestamptz_interval ( PG_FUNCTION_ARGS  )

Definition at line 3830 of file timestamp.c.

3831{
3834 Interval *offset = PG_GETARG_INTERVAL_P(2);
3835 bool sub = PG_GETARG_BOOL(3);
3836 bool less = PG_GETARG_BOOL(4);
3837 TimestampTz sum;
3838
3839 if (interval_sign(offset) < 0)
3840 ereport(ERROR,
3842 errmsg("invalid preceding or following size in window function")));
3843
3844 /*
3845 * Deal with cases where both base and offset are infinite, and computing
3846 * base +/- offset would cause an error. As for float and numeric types,
3847 * we assume that all values infinitely precede +infinity and infinitely
3848 * follow -infinity. See in_range_float8_float8() for reasoning.
3849 */
3850 if (INTERVAL_IS_NOEND(offset) &&
3851 (sub ? TIMESTAMP_IS_NOEND(base) : TIMESTAMP_IS_NOBEGIN(base)))
3853
3854 /* We don't currently bother to avoid overflow hazards here */
3855 if (sub)
3856 sum = timestamptz_mi_interval_internal(base, offset, NULL);
3857 else
3858 sum = timestamptz_pl_interval_internal(base, offset, NULL);
3859
3860 if (less)
3861 PG_RETURN_BOOL(val <= sum);
3862 else
3863 PG_RETURN_BOOL(val >= sum);
3864}
static TimestampTz timestamptz_mi_interval_internal(TimestampTz timestamp, Interval *span, pg_tz *attimezone)
Definition timestamp.c:3356

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOEND, interval_sign(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, timestamptz_mi_interval_internal(), timestamptz_pl_interval_internal(), and val.

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 2038 of file timestamp.c.

2039{
2040 TimeOffset time;
2042
2043 itm->tm_year = span.month / MONTHS_PER_YEAR;
2044 itm->tm_mon = span.month % MONTHS_PER_YEAR;
2045 itm->tm_mday = span.day;
2046 time = span.time;
2047
2048 tfrac = time / USECS_PER_HOUR;
2049 time -= tfrac * USECS_PER_HOUR;
2050 itm->tm_hour = tfrac;
2051 tfrac = time / USECS_PER_MINUTE;
2052 time -= tfrac * USECS_PER_MINUTE;
2053 itm->tm_min = (int) tfrac;
2054 tfrac = time / USECS_PER_SEC;
2055 time -= tfrac * USECS_PER_SEC;
2056 itm->tm_sec = (int) tfrac;
2057 itm->tm_usec = (int) time;
2058}

References fb(), MONTHS_PER_YEAR, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by interval_out(), interval_part_common(), interval_to_char(), and interval_trunc().

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 4199 of file timestamp.c.

4200{
4202
4204
4205 /* If there were no non-null inputs, return NULL */
4206 if (state == NULL || IA_TOTAL_COUNT(state) == 0)
4208
4209 /*
4210 * Aggregating infinities that all have the same sign produces infinity
4211 * with that sign. Aggregating infinities with different signs results in
4212 * an error.
4213 */
4214 if (state->pInfcount > 0 || state->nInfcount > 0)
4215 {
4216 Interval *result;
4217
4218 if (state->pInfcount > 0 && state->nInfcount > 0)
4219 ereport(ERROR,
4221 errmsg("interval out of range")));
4222
4223 result = palloc_object(Interval);
4224 if (state->pInfcount > 0)
4225 INTERVAL_NOEND(result);
4226 else
4227 INTERVAL_NOBEGIN(result);
4228
4229 PG_RETURN_INTERVAL_P(result);
4230 }
4231
4233 IntervalPGetDatum(&state->sumX),
4234 Float8GetDatum((double) state->N));
4235}
#define IA_TOTAL_COUNT(ia)
Definition timestamp.c:80
Datum interval_div(PG_FUNCTION_ARGS)
Definition timestamp.c:3729
#define INTERVAL_NOEND(i)
Definition timestamp.h:185
#define INTERVAL_NOBEGIN(i)
Definition timestamp.h:175
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_RETURN_NULL()
Definition fmgr.h:346
static Datum Float8GetDatum(float8 X)
Definition postgres.h:502
#define PG_RETURN_INTERVAL_P(x)
Definition timestamp.h:69

References DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), Float8GetDatum(), IA_TOTAL_COUNT, interval_div(), INTERVAL_NOBEGIN, INTERVAL_NOEND, IntervalPGetDatum(), palloc_object, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_INTERVAL_P, and PG_RETURN_NULL.

◆ interval_avg_accum()

Datum interval_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 4034 of file timestamp.c.

4035{
4037
4039
4040 /* Create the state data on the first call */
4041 if (state == NULL)
4042 state = makeIntervalAggState(fcinfo);
4043
4044 if (!PG_ARGISNULL(1))
4046
4048}
static void do_interval_accum(IntervalAggState *state, Interval *newval)
Definition timestamp.c:3980
static IntervalAggState * makeIntervalAggState(FunctionCallInfo fcinfo)
Definition timestamp.c:3958

References do_interval_accum(), fb(), makeIntervalAggState(), PG_ARGISNULL, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ interval_avg_accum_inv()

Datum interval_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4181 of file timestamp.c.

4182{
4184
4186
4187 /* Should not get here with no state */
4188 if (state == NULL)
4189 elog(ERROR, "interval_avg_accum_inv called with NULL state");
4190
4191 if (!PG_ARGISNULL(1))
4193
4195}
static void do_interval_discard(IntervalAggState *state, Interval *newval)
Definition timestamp.c:4003

References do_interval_discard(), elog, ERROR, fb(), PG_ARGISNULL, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ interval_avg_combine()

Datum interval_avg_combine ( PG_FUNCTION_ARGS  )

Definition at line 4057 of file timestamp.c.

4058{
4061
4064
4065 if (state2 == NULL)
4067
4068 if (state1 == NULL)
4069 {
4070 /* manually copy all fields from state2 to state1 */
4071 state1 = makeIntervalAggState(fcinfo);
4072
4073 state1->N = state2->N;
4074 state1->pInfcount = state2->pInfcount;
4075 state1->nInfcount = state2->nInfcount;
4076
4077 state1->sumX.day = state2->sumX.day;
4078 state1->sumX.month = state2->sumX.month;
4079 state1->sumX.time = state2->sumX.time;
4080
4082 }
4083
4084 state1->N += state2->N;
4085 state1->pInfcount += state2->pInfcount;
4086 state1->nInfcount += state2->nInfcount;
4087
4088 /* Accumulate finite interval values, if any. */
4089 if (state2->N > 0)
4090 finite_interval_pl(&state1->sumX, &state2->sumX, &state1->sumX);
4091
4093}

References fb(), finite_interval_pl(), makeIntervalAggState(), PG_ARGISNULL, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ interval_avg_deserialize()

Datum interval_avg_deserialize ( PG_FUNCTION_ARGS  )

Definition at line 4138 of file timestamp.c.

4139{
4140 bytea *sstate;
4141 IntervalAggState *result;
4143
4144 if (!AggCheckCallContext(fcinfo, NULL))
4145 elog(ERROR, "aggregate function called in non-aggregate context");
4146
4147 sstate = PG_GETARG_BYTEA_PP(0);
4148
4149 /*
4150 * Initialize a StringInfo so that we can "receive" it using the standard
4151 * recv-function infrastructure.
4152 */
4154 VARSIZE_ANY_EXHDR(sstate));
4155
4157
4158 /* N */
4159 result->N = pq_getmsgint64(&buf);
4160
4161 /* sumX */
4162 result->sumX.time = pq_getmsgint64(&buf);
4163 result->sumX.day = pq_getmsgint(&buf, 4);
4164 result->sumX.month = pq_getmsgint(&buf, 4);
4165
4166 /* pInfcount */
4167 result->pInfcount = pq_getmsgint64(&buf);
4168
4169 /* nInfcount */
4170 result->nInfcount = pq_getmsgint64(&buf);
4171
4172 pq_getmsgend(&buf);
4173
4174 PG_RETURN_POINTER(result);
4175}
#define palloc0_object(type)
Definition fe_memutils.h:75
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition nodeAgg.c:4609
static char buf[DEFAULT_XLOG_SEG_SIZE]
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
void pq_getmsgend(StringInfo msg)
Definition pqformat.c:634
int64 pq_getmsgint64(StringInfo msg)
Definition pqformat.c:452
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
Definition stringinfo.h:157
Interval sumX
Definition timestamp.c:74
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

References AggCheckCallContext(), buf, Interval::day, elog, ERROR, fb(), initReadOnlyStringInfo(), Interval::month, IntervalAggState::N, IntervalAggState::nInfcount, palloc0_object, PG_GETARG_BYTEA_PP, PG_RETURN_POINTER, IntervalAggState::pInfcount, pq_getmsgend(), pq_getmsgint(), pq_getmsgint64(), IntervalAggState::sumX, Interval::time, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ interval_avg_serialize()

Datum interval_avg_serialize ( PG_FUNCTION_ARGS  )

Definition at line 4100 of file timestamp.c.

4101{
4104 bytea *result;
4105
4106 /* Ensure we disallow calling when not in aggregate context */
4107 if (!AggCheckCallContext(fcinfo, NULL))
4108 elog(ERROR, "aggregate function called in non-aggregate context");
4109
4111
4113
4114 /* N */
4115 pq_sendint64(&buf, state->N);
4116
4117 /* sumX */
4118 pq_sendint64(&buf, state->sumX.time);
4119 pq_sendint32(&buf, state->sumX.day);
4120 pq_sendint32(&buf, state->sumX.month);
4121
4122 /* pInfcount */
4123 pq_sendint64(&buf, state->pInfcount);
4124
4125 /* nInfcount */
4126 pq_sendint64(&buf, state->nInfcount);
4127
4128 result = pq_endtypsend(&buf);
4129
4130 PG_RETURN_BYTEA_P(result);
4131}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144
static void pq_sendint64(StringInfo buf, uint64 i)
Definition pqformat.h:152

References AggCheckCallContext(), buf, elog, ERROR, fb(), PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint32(), and pq_sendint64().

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

◆ interval_cmp_internal()

static int interval_cmp_internal ( const Interval interval1,
const Interval interval2 
)
static

Definition at line 2537 of file timestamp.c.

2538{
2541
2542 return int128_compare(span1, span2);
2543}
static INT128 interval_cmp_value(const Interval *interval)
Definition timestamp.c:2515
static int int128_compare(INT128 x, INT128 y)
Definition int128.h:425

References fb(), int128_compare(), and interval_cmp_value().

Referenced by in_range_interval_interval(), interval_cmp(), interval_eq(), interval_ge(), interval_gt(), interval_larger(), interval_le(), interval_lt(), interval_ne(), and interval_smaller().

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2515 of file timestamp.c.

2516{
2517 INT128 span;
2518 int64 days;
2519
2520 /*
2521 * Combine the month and day fields into an integral number of days.
2522 * Because the inputs are int32, int64 arithmetic suffices here.
2523 */
2524 days = interval->month * INT64CONST(30);
2525 days += interval->day;
2526
2527 /* Widen time field to 128 bits */
2529
2530 /* Scale up days to microseconds, forming a 128-bit product */
2532
2533 return span;
2534}
const char *const days[]
Definition datetime.c:85
#define USECS_PER_DAY
Definition timestamp.h:131
static INT128 int64_to_int128(int64 v)
Definition int128.h:450
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
Definition int128.h:203

References days, fb(), int128_add_int64_mul_int64(), int64_to_int128(), INT64CONST, interval::month, interval::time, and USECS_PER_DAY.

Referenced by interval_cmp_internal(), interval_hash(), interval_hash_extended(), and interval_sign().

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3729 of file timestamp.c.

3730{
3733 double month_remainder_days,
3736 int32 orig_month = span->month,
3737 orig_day = span->day;
3738 Interval *result;
3739
3740 result = palloc_object(Interval);
3741
3742 if (factor == 0.0)
3743 ereport(ERROR,
3745 errmsg("division by zero")));
3746
3747 /*
3748 * Handle NaN and infinities.
3749 *
3750 * We treat "infinity / infinity" as an error, since the interval type has
3751 * nothing equivalent to NaN. Otherwise, dividing by infinity is handled
3752 * by the regular division code, causing all fields to be set to zero.
3753 */
3754 if (isnan(factor))
3755 goto out_of_range;
3756
3758 {
3759 if (isinf(factor))
3760 goto out_of_range;
3761
3762 if (factor < 0.0)
3763 interval_um_internal(span, result);
3764 else
3765 memcpy(result, span, sizeof(Interval));
3766
3767 PG_RETURN_INTERVAL_P(result);
3768 }
3769
3770 result_double = span->month / factor;
3772 goto out_of_range;
3773 result->month = (int32) result_double;
3774
3775 result_double = span->day / factor;
3777 goto out_of_range;
3778 result->day = (int32) result_double;
3779
3780 /*
3781 * Fractional months full days into days. See comment in interval_mul().
3782 */
3785 sec_remainder = (orig_day / factor - result->day +
3789 {
3790 if (pg_add_s32_overflow(result->day,
3791 (int) (sec_remainder / SECS_PER_DAY),
3792 &result->day))
3793 goto out_of_range;
3795 }
3796
3797 /* cascade units down */
3799 &result->day))
3800 goto out_of_range;
3803 goto out_of_range;
3804 result->time = (int64) result_double;
3805
3806 if (INTERVAL_NOT_FINITE(result))
3807 goto out_of_range;
3808
3809 PG_RETURN_INTERVAL_P(result);
3810
3812 ereport(ERROR,
3814 errmsg("interval out of range"));
3815
3816 PG_RETURN_NULL(); /* keep compiler quiet */
3817}
static void interval_um_internal(const Interval *interval, Interval *result)
Definition timestamp.c:3417
#define FLOAT8_FITS_IN_INT32(num)
Definition c.h:1176
#define FLOAT8_FITS_IN_INT64(num)
Definition c.h:1178
#define DAYS_PER_MONTH
Definition timestamp.h:116
#define TSROUND(j)
Definition timestamp.h:100

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), FLOAT8_FITS_IN_INT32, FLOAT8_FITS_IN_INT64, INTERVAL_NOT_FINITE, interval_um_internal(), Interval::month, palloc_object, pg_add_s32_overflow(), PG_GETARG_FLOAT8, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_avg().

◆ interval_eq()

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

◆ interval_ge()

◆ interval_gt()

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2625 of file timestamp.c.

2626{
2629 int64 span64;
2630
2631 /*
2632 * Use only the least significant 64 bits for hashing. The upper 64 bits
2633 * seldom add any useful information, and besides we must do it like this
2634 * for compatibility with hashes calculated before use of INT128 was
2635 * introduced.
2636 */
2638
2640}
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
Datum hashint8(PG_FUNCTION_ARGS)
Definition hashfunc.c:84
static int64 int128_to_int64(INT128 val)
Definition int128.h:468
#define Int64GetDatumFast(X)
Definition postgres.h:525

References DirectFunctionCall1, fb(), hashint8(), int128_to_int64(), Int64GetDatumFast, interval_cmp_value(), and PG_GETARG_INTERVAL_P.

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2643 of file timestamp.c.

2644{
2647 int64 span64;
2648
2649 /* Same approach as interval_hash */
2651
2653 PG_GETARG_DATUM(1));
2654}
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition hashfunc.c:104

References DirectFunctionCall2, fb(), hashint8extended(), int128_to_int64(), Int64GetDatumFast, interval_cmp_value(), PG_GETARG_DATUM, and PG_GETARG_INTERVAL_P.

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 882 of file timestamp.c.

883{
884 char *str = PG_GETARG_CSTRING(0);
885#ifdef NOT_USED
886 Oid typelem = PG_GETARG_OID(1);
887#endif
888 int32 typmod = PG_GETARG_INT32(2);
889 Node *escontext = fcinfo->context;
890 Interval *result;
891 struct pg_itm_in tt,
892 *itm_in = &tt;
893 int dtype;
894 int nf;
895 int range;
896 int dterr;
897 char *field[MAXDATEFIELDS];
898 int ftype[MAXDATEFIELDS];
899 char workbuf[256];
900 DateTimeErrorExtra extra;
901
902 itm_in->tm_year = 0;
903 itm_in->tm_mon = 0;
904 itm_in->tm_mday = 0;
905 itm_in->tm_usec = 0;
906
907 if (typmod >= 0)
908 range = INTERVAL_RANGE(typmod);
909 else
911
912 dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
913 ftype, MAXDATEFIELDS, &nf);
914 if (dterr == 0)
915 dterr = DecodeInterval(field, ftype, nf, range,
916 &dtype, itm_in);
917
918 /* if those functions think it's a bad format, try ISO8601 style */
919 if (dterr == DTERR_BAD_FORMAT)
921 &dtype, itm_in);
922
923 if (dterr != 0)
924 {
927 DateTimeParseError(dterr, &extra, str, "interval", escontext);
929 }
930
931 result = palloc_object(Interval);
932
933 switch (dtype)
934 {
935 case DTK_DELTA:
936 if (itmin2interval(itm_in, result) != 0)
937 ereturn(escontext, (Datum) 0,
939 errmsg("interval out of range")));
940 break;
941
942 case DTK_LATE:
943 INTERVAL_NOEND(result);
944 break;
945
946 case DTK_EARLY:
947 INTERVAL_NOBEGIN(result);
948 break;
949
950 default:
951 elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
952 dtype, str);
953 }
954
955 AdjustIntervalForTypmod(result, typmod, escontext);
956
957 PG_RETURN_INTERVAL_P(result);
958}
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition datetime.c:774
void DateTimeParseError(int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, Node *escontext)
Definition datetime.c:4215
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_itm_in *itm_in)
Definition datetime.c:3487
int DecodeISO8601Interval(char *str, int *dtype, struct pg_itm_in *itm_in)
Definition datetime.c:3952
int itmin2interval(struct pg_itm_in *itm_in, Interval *span)
Definition timestamp.c:2106
static bool AdjustIntervalForTypmod(Interval *interval, int32 typmod, Node *escontext)
Definition timestamp.c:1341
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define MAXDATEFIELDS
Definition datetime.h:202
#define DTERR_INTERVAL_OVERFLOW
Definition datetime.h:285
#define DTK_DELTA
Definition datetime.h:159
#define DTK_LATE
Definition datetime.h:151
#define DTERR_BAD_FORMAT
Definition datetime.h:282
#define DTK_EARLY
Definition datetime.h:150
#define DTERR_FIELD_OVERFLOW
Definition datetime.h:283
unsigned int Oid

References AdjustIntervalForTypmod(), DateTimeParseError(), DecodeInterval(), DecodeISO8601Interval(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTK_DELTA, DTK_EARLY, DTK_LATE, elog, ereturn, errcode(), errmsg, ERROR, fb(), INTERVAL_FULL_RANGE, INTERVAL_NOBEGIN, INTERVAL_NOEND, INTERVAL_RANGE, itmin2interval(), MAXDATEFIELDS, palloc_object, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, range(), and str.

Referenced by check_timezone(), and flatten_set_variable_args().

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 3034 of file timestamp.c.

3035{
3037 Interval *result;
3039
3040 result = palloc_object(Interval);
3041 result->month = span->month;
3042 result->day = span->day;
3043 result->time = span->time;
3044
3045 /* do nothing for infinite intervals */
3046 if (INTERVAL_NOT_FINITE(result))
3047 PG_RETURN_INTERVAL_P(result);
3048
3049 wholemonth = result->day / DAYS_PER_MONTH;
3050 result->day -= wholemonth * DAYS_PER_MONTH;
3051 if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
3052 ereport(ERROR,
3054 errmsg("interval out of range")));
3055
3056 if (result->month > 0 && result->day < 0)
3057 {
3058 result->day += DAYS_PER_MONTH;
3059 result->month--;
3060 }
3061 else if (result->month < 0 && result->day > 0)
3062 {
3063 result->day -= DAYS_PER_MONTH;
3064 result->month++;
3065 }
3066
3067 PG_RETURN_INTERVAL_P(result);
3068}

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, Interval::month, palloc_object, pg_add_s32_overflow(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, and Interval::time.

◆ interval_justify_hours()

Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

Definition at line 2992 of file timestamp.c.

2993{
2995 Interval *result;
2997
2998 result = palloc_object(Interval);
2999 result->month = span->month;
3000 result->day = span->day;
3001 result->time = span->time;
3002
3003 /* do nothing for infinite intervals */
3004 if (INTERVAL_NOT_FINITE(result))
3005 PG_RETURN_INTERVAL_P(result);
3006
3008 if (pg_add_s32_overflow(result->day, wholeday, &result->day))
3009 ereport(ERROR,
3011 errmsg("interval out of range")));
3012
3013 if (result->day > 0 && result->time < 0)
3014 {
3015 result->time += USECS_PER_DAY;
3016 result->day--;
3017 }
3018 else if (result->day < 0 && result->time > 0)
3019 {
3020 result->time -= USECS_PER_DAY;
3021 result->day++;
3022 }
3023
3024 PG_RETURN_INTERVAL_P(result);
3025}
#define TMODULO(t, q, u)
Definition datetime.h:248

References Interval::day, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, Interval::month, palloc_object, pg_add_s32_overflow(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, Interval::time, TMODULO, and USECS_PER_DAY.

Referenced by timestamp_mi().

◆ interval_justify_interval()

Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2912 of file timestamp.c.

2913{
2915 Interval *result;
2918
2919 result = palloc_object(Interval);
2920 result->month = span->month;
2921 result->day = span->day;
2922 result->time = span->time;
2923
2924 /* do nothing for infinite intervals */
2925 if (INTERVAL_NOT_FINITE(result))
2926 PG_RETURN_INTERVAL_P(result);
2927
2928 /* pre-justify days if it might prevent overflow */
2929 if ((result->day > 0 && result->time > 0) ||
2930 (result->day < 0 && result->time < 0))
2931 {
2932 wholemonth = result->day / DAYS_PER_MONTH;
2933 result->day -= wholemonth * DAYS_PER_MONTH;
2934 if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2935 ereport(ERROR,
2937 errmsg("interval out of range")));
2938 }
2939
2940 /*
2941 * Since TimeOffset is int64, abs(wholeday) can't exceed about 1.07e8. If
2942 * we pre-justified then abs(result->day) is less than DAYS_PER_MONTH, so
2943 * this addition can't overflow. If we didn't pre-justify, then day and
2944 * time are of different signs, so it still can't overflow.
2945 */
2947 result->day += wholeday;
2948
2949 wholemonth = result->day / DAYS_PER_MONTH;
2950 result->day -= wholemonth * DAYS_PER_MONTH;
2951 if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2952 ereport(ERROR,
2954 errmsg("interval out of range")));
2955
2956 if (result->month > 0 &&
2957 (result->day < 0 || (result->day == 0 && result->time < 0)))
2958 {
2959 result->day += DAYS_PER_MONTH;
2960 result->month--;
2961 }
2962 else if (result->month < 0 &&
2963 (result->day > 0 || (result->day == 0 && result->time > 0)))
2964 {
2965 result->day -= DAYS_PER_MONTH;
2966 result->month++;
2967 }
2968
2969 if (result->day > 0 && result->time < 0)
2970 {
2971 result->time += USECS_PER_DAY;
2972 result->day--;
2973 }
2974 else if (result->day < 0 && result->time > 0)
2975 {
2976 result->time -= USECS_PER_DAY;
2977 result->day++;
2978 }
2979
2980 PG_RETURN_INTERVAL_P(result);
2981}

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, Interval::month, palloc_object, pg_add_s32_overflow(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, Interval::time, TMODULO, and USECS_PER_DAY.

◆ interval_larger()

Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3465 of file timestamp.c.

3466{
3469 Interval *result;
3470
3472 result = interval1;
3473 else
3474 result = interval2;
3475 PG_RETURN_INTERVAL_P(result);
3476}

References fb(), interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_le()

◆ interval_lt()

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3550 of file timestamp.c.

3551{
3554 Interval *result;
3555
3556 result = palloc_object(Interval);
3557
3558 /*
3559 * Handle infinities.
3560 *
3561 * We treat anything that amounts to "infinity - infinity" as an error,
3562 * since the interval type has nothing equivalent to NaN.
3563 */
3565 {
3567 ereport(ERROR,
3569 errmsg("interval out of range")));
3570 else
3571 INTERVAL_NOBEGIN(result);
3572 }
3573 else if (INTERVAL_IS_NOEND(span1))
3574 {
3576 ereport(ERROR,
3578 errmsg("interval out of range")));
3579 else
3580 INTERVAL_NOEND(result);
3581 }
3582 else if (INTERVAL_IS_NOBEGIN(span2))
3583 INTERVAL_NOEND(result);
3584 else if (INTERVAL_IS_NOEND(span2))
3585 INTERVAL_NOBEGIN(result);
3586 else
3587 finite_interval_mi(span1, span2, result);
3588
3589 PG_RETURN_INTERVAL_P(result);
3590}

References ereport, errcode(), errmsg, ERROR, fb(), finite_interval_mi(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, INTERVAL_NOBEGIN, INTERVAL_NOEND, palloc_object, PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

Referenced by in_range_interval_interval(), interval_dist(), and interval_lerp().

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3599 of file timestamp.c.

3600{
3603 double month_remainder_days,
3606 int32 orig_month = span->month,
3607 orig_day = span->day;
3608 Interval *result;
3609
3610 result = palloc_object(Interval);
3611
3612 /*
3613 * Handle NaN and infinities.
3614 *
3615 * We treat "0 * infinity" and "infinity * 0" as errors, since the
3616 * interval type has nothing equivalent to NaN.
3617 */
3618 if (isnan(factor))
3619 goto out_of_range;
3620
3622 {
3623 if (factor == 0.0)
3624 goto out_of_range;
3625
3626 if (factor < 0.0)
3627 interval_um_internal(span, result);
3628 else
3629 memcpy(result, span, sizeof(Interval));
3630
3631 PG_RETURN_INTERVAL_P(result);
3632 }
3633 if (isinf(factor))
3634 {
3635 int isign = interval_sign(span);
3636
3637 if (isign == 0)
3638 goto out_of_range;
3639
3640 if (factor * isign < 0)
3641 INTERVAL_NOBEGIN(result);
3642 else
3643 INTERVAL_NOEND(result);
3644
3645 PG_RETURN_INTERVAL_P(result);
3646 }
3647
3648 result_double = span->month * factor;
3650 goto out_of_range;
3651 result->month = (int32) result_double;
3652
3653 result_double = span->day * factor;
3655 goto out_of_range;
3656 result->day = (int32) result_double;
3657
3658 /*
3659 * The above correctly handles the whole-number part of the month and day
3660 * products, but we have to do something with any fractional part
3661 * resulting when the factor is non-integral. We cascade the fractions
3662 * down to lower units using the conversion factors DAYS_PER_MONTH and
3663 * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3664 * so by the representation. The user can choose to cascade up later,
3665 * using justify_hours and/or justify_days.
3666 */
3667
3668 /*
3669 * Fractional months full days into days.
3670 *
3671 * Floating point calculation are inherently imprecise, so these
3672 * calculations are crafted to produce the most reliable result possible.
3673 * TSROUND() is needed to more accurately produce whole numbers where
3674 * appropriate.
3675 */
3678 sec_remainder = (orig_day * factor - result->day +
3681
3682 /*
3683 * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3684 * cascade from months and days. It might still be >24 if the combination
3685 * of cascade and the seconds factor operation itself.
3686 */
3688 {
3689 if (pg_add_s32_overflow(result->day,
3690 (int) (sec_remainder / SECS_PER_DAY),
3691 &result->day))
3692 goto out_of_range;
3694 }
3695
3696 /* cascade units down */
3698 &result->day))
3699 goto out_of_range;
3702 goto out_of_range;
3703 result->time = (int64) result_double;
3704
3705 if (INTERVAL_NOT_FINITE(result))
3706 goto out_of_range;
3707
3708 PG_RETURN_INTERVAL_P(result);
3709
3711 ereport(ERROR,
3713 errmsg("interval out of range"));
3714
3715 PG_RETURN_NULL(); /* keep compiler quiet */
3716}

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), FLOAT8_FITS_IN_INT32, FLOAT8_FITS_IN_INT64, INTERVAL_NOBEGIN, INTERVAL_NOEND, INTERVAL_NOT_FINITE, interval_sign(), interval_um_internal(), Interval::month, palloc_object, pg_add_s32_overflow(), PG_GETARG_FLOAT8, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_lerp(), and mul_d_interval().

◆ interval_ne()

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 964 of file timestamp.c.

965{
967 char *result;
968 struct pg_itm tt,
969 *itm = &tt;
970 char buf[MAXDATELEN + 1];
971
974 else
975 {
978 }
979
980 result = pstrdup(buf);
981 PG_RETURN_CSTRING(result);
982}
void EncodeInterval(struct pg_itm *itm, int style, char *str)
Definition datetime.c:4708
static void EncodeSpecialInterval(const Interval *interval, char *str)
Definition timestamp.c:1589
void interval2itm(Interval span, struct pg_itm *itm)
Definition timestamp.c:2038
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
int IntervalStyle
Definition globals.c:127
#define MAXDATELEN
Definition datetime.h:200

References buf, EncodeInterval(), EncodeSpecialInterval(), fb(), interval2itm(), INTERVAL_NOT_FINITE, IntervalStyle, MAXDATELEN, PG_GETARG_INTERVAL_P, PG_RETURN_CSTRING, and pstrdup().

Referenced by flatten_set_variable_args(), timestamp_izone(), timestamptz_izone(), and timetz_izone().

◆ interval_part()

Datum interval_part ( PG_FUNCTION_ARGS  )

Definition at line 6278 of file timestamp.c.

6279{
6280 return interval_part_common(fcinfo, false);
6281}

References interval_part_common().

◆ interval_part_common()

static Datum interval_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 6071 of file timestamp.c.

6072{
6076 int type,
6077 val;
6078 char *lowunits;
6079 struct pg_itm tt,
6080 *tm = &tt;
6081
6084 false);
6085
6086 type = DecodeUnits(0, lowunits, &val);
6087 if (type == UNKNOWN_FIELD)
6089
6091 {
6094
6095 if (r != 0.0)
6096 {
6097 if (retnumeric)
6098 {
6099 if (r < 0)
6101 CStringGetDatum("-Infinity"),
6103 Int32GetDatum(-1));
6104 else if (r > 0)
6106 CStringGetDatum("Infinity"),
6108 Int32GetDatum(-1));
6109 }
6110 else
6112 }
6113 else
6115 }
6116
6117 if (type == UNITS)
6118 {
6120 switch (val)
6121 {
6122 case DTK_MICROSEC:
6123 intresult = tm->tm_sec * INT64CONST(1000000) + tm->tm_usec;
6124 break;
6125
6126 case DTK_MILLISEC:
6127 if (retnumeric)
6128 /*---
6129 * tm->tm_sec * 1000 + fsec / 1000
6130 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
6131 */
6133 else
6134 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + tm->tm_usec / 1000.0);
6135 break;
6136
6137 case DTK_SECOND:
6138 if (retnumeric)
6139 /*---
6140 * tm->tm_sec + fsec / 1'000'000
6141 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
6142 */
6144 else
6145 PG_RETURN_FLOAT8(tm->tm_sec + tm->tm_usec / 1000000.0);
6146 break;
6147
6148 case DTK_MINUTE:
6149 intresult = tm->tm_min;
6150 break;
6151
6152 case DTK_HOUR:
6153 intresult = tm->tm_hour;
6154 break;
6155
6156 case DTK_DAY:
6157 intresult = tm->tm_mday;
6158 break;
6159
6160 case DTK_WEEK:
6161 intresult = tm->tm_mday / 7;
6162 break;
6163
6164 case DTK_MONTH:
6165 intresult = tm->tm_mon;
6166 break;
6167
6168 case DTK_QUARTER:
6169
6170 /*
6171 * We want to maintain the rule that a field extracted from a
6172 * negative interval is the negative of the field's value for
6173 * the sign-reversed interval. The broken-down tm_year and
6174 * tm_mon aren't very helpful for that, so work from
6175 * interval->month.
6176 */
6177 if (interval->month >= 0)
6178 intresult = (tm->tm_mon / 3) + 1;
6179 else
6180 intresult = -(((-interval->month % MONTHS_PER_YEAR) / 3) + 1);
6181 break;
6182
6183 case DTK_YEAR:
6184 intresult = tm->tm_year;
6185 break;
6186
6187 case DTK_DECADE:
6188 /* caution: C division may have negative remainder */
6189 intresult = tm->tm_year / 10;
6190 break;
6191
6192 case DTK_CENTURY:
6193 /* caution: C division may have negative remainder */
6194 intresult = tm->tm_year / 100;
6195 break;
6196
6197 case DTK_MILLENNIUM:
6198 /* caution: C division may have negative remainder */
6199 intresult = tm->tm_year / 1000;
6200 break;
6201
6202 default:
6203 ereport(ERROR,
6205 errmsg("unit \"%s\" not supported for type %s",
6207 intresult = 0;
6208 }
6209 }
6210 else if (type == RESERV && val == DTK_EPOCH)
6211 {
6212 if (retnumeric)
6213 {
6214 Numeric result;
6216 int64 val;
6217
6218 /*
6219 * To do this calculation in integer arithmetic even though
6220 * DAYS_PER_YEAR is fractional, multiply everything by 4 and then
6221 * divide by 4 again at the end. This relies on DAYS_PER_YEAR
6222 * being a multiple of 0.25 and on SECS_PER_DAY being a multiple
6223 * of 4.
6224 */
6227 (int64) 4 * interval->day) * (SECS_PER_DAY / 4);
6228
6229 /*---
6230 * result = secs_from_day_month + interval->time / 1'000'000
6231 * = (secs_from_day_month * 1'000'000 + interval->time) / 1'000'000
6232 */
6233
6234 /*
6235 * Try the computation inside int64; if it overflows, do it in
6236 * numeric (slower). This overflow happens around 10^9 days, so
6237 * not common in practice.
6238 */
6239 if (!pg_mul_s64_overflow(secs_from_day_month, 1000000, &val) &&
6241 result = int64_div_fast_to_numeric(val, 6);
6242 else
6243 result =
6246 NULL);
6247
6248 PG_RETURN_NUMERIC(result);
6249 }
6250 else
6251 {
6252 float8 result;
6253
6254 result = interval->time / 1000000.0;
6257 result += ((double) SECS_PER_DAY) * interval->day;
6258
6259 PG_RETURN_FLOAT8(result);
6260 }
6261 }
6262 else
6263 {
6264 ereport(ERROR,
6266 errmsg("unit \"%s\" not recognized for type %s",
6268 intresult = 0;
6269 }
6270
6271 if (retnumeric)
6273 else
6275}
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition datetime.c:4170
int DecodeSpecial(int field, const char *lowtoken, int *val)
Definition datetime.c:3247
Numeric int64_to_numeric(int64 val)
Definition numeric.c:4259
Numeric numeric_add_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:2881
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition numeric.c:4280
Datum numeric_in(PG_FUNCTION_ARGS)
Definition numeric.c:626
static float8 NonFiniteIntervalPart(int type, int unit, char *lowunits, bool isNegative)
Definition timestamp.c:6025
#define DAYS_PER_YEAR
Definition timestamp.h:107
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition fmgr.h:688
char * format_type_be(Oid type_oid)
#define DTK_EPOCH
Definition datetime.h:152
#define UNKNOWN_FIELD
Definition datetime.h:124
#define DTK_DECADE
Definition datetime.h:168
#define DTK_SECOND
Definition datetime.h:160
#define DTK_QUARTER
Definition datetime.h:166
#define DTK_CENTURY
Definition datetime.h:169
#define DTK_DAY
Definition datetime.h:163
#define RESERV
Definition datetime.h:90
#define DTK_MILLENNIUM
Definition datetime.h:170
#define DTK_HOUR
Definition datetime.h:162
#define DTK_WEEK
Definition datetime.h:164
#define DTK_MICROSEC
Definition datetime.h:172
#define DTK_YEAR
Definition datetime.h:167
#define DTK_MILLISEC
Definition datetime.h:171
#define DTK_MONTH
Definition datetime.h:165
#define DTK_MINUTE
Definition datetime.h:161
#define UNITS
Definition datetime.h:107
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:293
#define PG_RETURN_NUMERIC(x)
Definition numeric.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
#define InvalidOid
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition scansup.c:38
const char * type

References CStringGetDatum(), DAYS_PER_MONTH, DAYS_PER_YEAR, DecodeSpecial(), DecodeUnits(), DirectFunctionCall3, downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_WEEK, DTK_YEAR, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), Int32GetDatum(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, interval2itm(), INTERVAL_IS_NOBEGIN, INTERVAL_NOT_FINITE, InvalidOid, interval::month, MONTHS_PER_YEAR, NonFiniteIntervalPart(), numeric_add_safe(), numeric_in(), ObjectIdGetDatum(), pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, pg_mul_s64_overflow(), PG_RETURN_FLOAT8, PG_RETURN_NULL, PG_RETURN_NUMERIC, RESERV, SECS_PER_DAY, interval::time, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by extract_interval(), and interval_part().

◆ interval_pl()

Datum interval_pl ( PG_FUNCTION_ARGS  )

Definition at line 3494 of file timestamp.c.

3495{
3498 Interval *result;
3499
3500 result = palloc_object(Interval);
3501
3502 /*
3503 * Handle infinities.
3504 *
3505 * We treat anything that amounts to "infinity - infinity" as an error,
3506 * since the interval type has nothing equivalent to NaN.
3507 */
3509 {
3511 ereport(ERROR,
3513 errmsg("interval out of range")));
3514 else
3515 INTERVAL_NOBEGIN(result);
3516 }
3517 else if (INTERVAL_IS_NOEND(span1))
3518 {
3520 ereport(ERROR,
3522 errmsg("interval out of range")));
3523 else
3524 INTERVAL_NOEND(result);
3525 }
3526 else if (INTERVAL_NOT_FINITE(span2))
3527 memcpy(result, span2, sizeof(Interval));
3528 else
3529 finite_interval_pl(span1, span2, result);
3530
3531 PG_RETURN_INTERVAL_P(result);
3532}

References ereport, errcode(), errmsg, ERROR, fb(), finite_interval_pl(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, INTERVAL_NOBEGIN, INTERVAL_NOEND, INTERVAL_NOT_FINITE, palloc_object, PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

Referenced by in_range_interval_interval(), and interval_lerp().

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

Definition at line 988 of file timestamp.c.

989{
991
992#ifdef NOT_USED
993 Oid typelem = PG_GETARG_OID(1);
994#endif
995 int32 typmod = PG_GETARG_INT32(2);
997
999
1001 interval->day = pq_getmsgint(buf, sizeof(interval->day));
1003
1005
1007}
struct StringInfoData * StringInfo
Definition string.h:15

References AdjustIntervalForTypmod(), buf, fb(), interval::month, palloc_object, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_INTERVAL_P, pq_getmsgint(), pq_getmsgint64(), and interval::time.

◆ interval_scale()

Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1319 of file timestamp.c.

1320{
1322 int32 typmod = PG_GETARG_INT32(1);
1323 Interval *result;
1324
1325 result = palloc_object(Interval);
1326 *result = *interval;
1327
1328 AdjustIntervalForTypmod(result, typmod, NULL);
1329
1330 PG_RETURN_INTERVAL_P(result);
1331}

References AdjustIntervalForTypmod(), fb(), palloc_object, PG_GETARG_INT32, PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_send()

◆ interval_sign()

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3450 of file timestamp.c.

3451{
3454 Interval *result;
3455
3456 /* use interval_cmp_internal to be sure this agrees with comparisons */
3458 result = interval1;
3459 else
3460 result = interval2;
3461 PG_RETURN_INTERVAL_P(result);
3462}

References fb(), interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_sum()

Datum interval_sum ( PG_FUNCTION_ARGS  )

Definition at line 4239 of file timestamp.c.

4240{
4242 Interval *result;
4243
4245
4246 /* If there were no non-null inputs, return NULL */
4247 if (state == NULL || IA_TOTAL_COUNT(state) == 0)
4249
4250 /*
4251 * Aggregating infinities that all have the same sign produces infinity
4252 * with that sign. Aggregating infinities with different signs results in
4253 * an error.
4254 */
4255 if (state->pInfcount > 0 && state->nInfcount > 0)
4256 ereport(ERROR,
4258 errmsg("interval out of range")));
4259
4260 result = palloc_object(Interval);
4261
4262 if (state->pInfcount > 0)
4263 INTERVAL_NOEND(result);
4264 else if (state->nInfcount > 0)
4265 INTERVAL_NOBEGIN(result);
4266 else
4267 memcpy(result, &state->sumX, sizeof(Interval));
4268
4269 PG_RETURN_INTERVAL_P(result);
4270}

References ereport, errcode(), errmsg, ERROR, fb(), IA_TOTAL_COUNT, INTERVAL_NOBEGIN, INTERVAL_NOEND, palloc_object, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_INTERVAL_P, and PG_RETURN_NULL.

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1256 of file timestamp.c.

1257{
1259 Node *ret = NULL;
1260
1262 {
1264 FuncExpr *expr = req->fcall;
1265 Node *typmod;
1266
1267 Assert(list_length(expr->args) >= 2);
1268
1269 typmod = (Node *) lsecond(expr->args);
1270
1271 if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
1272 {
1273 Node *source = (Node *) linitial(expr->args);
1275 bool noop;
1276
1277 if (new_typmod < 0)
1278 noop = true;
1279 else
1280 {
1282 int old_least_field;
1283 int new_least_field;
1284 int old_precis;
1285 int new_precis;
1286
1289 if (old_typmod < 0)
1291 else
1294
1295 /*
1296 * Cast is a no-op if least field stays the same or decreases
1297 * while precision stays the same or increases. But
1298 * precision, which is to say, sub-second precision, only
1299 * affects ranges that include SECOND.
1300 */
1302 (old_least_field > 0 /* SECOND */ ||
1305 }
1306 if (noop)
1308 }
1309 }
1310
1311 PG_RETURN_POINTER(ret);
1312}
static int intervaltypmodleastfield(int32 typmod)
Definition timestamp.c:1203
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition nodeFuncs.c:694
static int list_length(const List *l)
Definition pg_list.h:152
static rewind_source * source
Definition pg_rewind.c:89
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
List * args
Definition primnodes.h:801

References FuncExpr::args, Assert, DatumGetInt32(), exprTypmod(), fb(), INTERVAL_FULL_PRECISION, INTERVAL_PRECISION, intervaltypmodleastfield(), IsA, linitial, list_length(), lsecond, MAX_INTERVAL_PRECISION, PG_GETARG_POINTER, PG_RETURN_POINTER, relabel_to_typmod(), and source.

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 5103 of file timestamp.c.

5104{
5107 Interval *result;
5108 int type,
5109 val;
5110 char *lowunits;
5111 struct pg_itm tt,
5112 *tm = &tt;
5113
5114 result = palloc_object(Interval);
5115
5118 false);
5119
5120 type = DecodeUnits(0, lowunits, &val);
5121
5122 if (type == UNITS)
5123 {
5125 {
5126 /*
5127 * Errors thrown here for invalid units should exactly match those
5128 * below, else there will be unexpected discrepancies between
5129 * finite- and infinite-input cases.
5130 */
5131 switch (val)
5132 {
5133 case DTK_MILLENNIUM:
5134 case DTK_CENTURY:
5135 case DTK_DECADE:
5136 case DTK_YEAR:
5137 case DTK_QUARTER:
5138 case DTK_MONTH:
5139 case DTK_DAY:
5140 case DTK_HOUR:
5141 case DTK_MINUTE:
5142 case DTK_SECOND:
5143 case DTK_MILLISEC:
5144 case DTK_MICROSEC:
5145 memcpy(result, interval, sizeof(Interval));
5146 PG_RETURN_INTERVAL_P(result);
5147 break;
5148
5149 default:
5150 ereport(ERROR,
5152 errmsg("unit \"%s\" not supported for type %s",
5154 (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
5155 result = NULL;
5156 }
5157 }
5158
5160 switch (val)
5161 {
5162 case DTK_MILLENNIUM:
5163 /* caution: C division may have negative remainder */
5164 tm->tm_year = (tm->tm_year / 1000) * 1000;
5166 case DTK_CENTURY:
5167 /* caution: C division may have negative remainder */
5168 tm->tm_year = (tm->tm_year / 100) * 100;
5170 case DTK_DECADE:
5171 /* caution: C division may have negative remainder */
5172 tm->tm_year = (tm->tm_year / 10) * 10;
5174 case DTK_YEAR:
5175 tm->tm_mon = 0;
5177 case DTK_QUARTER:
5178 tm->tm_mon = 3 * (tm->tm_mon / 3);
5180 case DTK_MONTH:
5181 tm->tm_mday = 0;
5183 case DTK_DAY:
5184 tm->tm_hour = 0;
5186 case DTK_HOUR:
5187 tm->tm_min = 0;
5189 case DTK_MINUTE:
5190 tm->tm_sec = 0;
5192 case DTK_SECOND:
5193 tm->tm_usec = 0;
5194 break;
5195 case DTK_MILLISEC:
5196 tm->tm_usec = (tm->tm_usec / 1000) * 1000;
5197 break;
5198 case DTK_MICROSEC:
5199 break;
5200
5201 default:
5202 ereport(ERROR,
5204 errmsg("unit \"%s\" not supported for type %s",
5206 (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
5207 }
5208
5209 if (itm2interval(tm, result) != 0)
5210 ereport(ERROR,
5212 errmsg("interval out of range")));
5213 }
5214 else
5215 {
5216 ereport(ERROR,
5218 errmsg("unit \"%s\" not recognized for type %s",
5220 }
5221
5222 PG_RETURN_INTERVAL_P(result);
5223}
int itm2interval(struct pg_itm *itm, Interval *span)
Definition timestamp.c:2068
#define pg_fallthrough
Definition c.h:152
int errdetail(const char *fmt,...) pg_attribute_printf(1

References DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_WEEK, DTK_YEAR, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), format_type_be(), interval2itm(), INTERVAL_NOT_FINITE, itm2interval(), palloc_object, pg_fallthrough, PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_RETURN_INTERVAL_P, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ interval_um()

Datum interval_um ( PG_FUNCTION_ARGS  )

Definition at line 3437 of file timestamp.c.

3438{
3440 Interval *result;
3441
3442 result = palloc_object(Interval);
3444
3445 PG_RETURN_INTERVAL_P(result);
3446}

References interval_um_internal(), palloc_object, PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

Referenced by abs_interval().

◆ interval_um_internal()

static void interval_um_internal ( const Interval interval,
Interval result 
)
static

Definition at line 3417 of file timestamp.c.

3418{
3420 INTERVAL_NOEND(result);
3421 else if (INTERVAL_IS_NOEND(interval))
3422 INTERVAL_NOBEGIN(result);
3423 else
3424 {
3425 /* Negate each field, guarding against overflow */
3426 if (pg_sub_s64_overflow(INT64CONST(0), interval->time, &result->time) ||
3427 pg_sub_s32_overflow(0, interval->day, &result->day) ||
3428 pg_sub_s32_overflow(0, interval->month, &result->month) ||
3429 INTERVAL_NOT_FINITE(result))
3430 ereport(ERROR,
3432 errmsg("interval out of range")));
3433 }
3434}

References Interval::day, ereport, errcode(), errmsg, ERROR, fb(), INT64CONST, INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, INTERVAL_NOBEGIN, INTERVAL_NOEND, INTERVAL_NOT_FINITE, Interval::month, interval::month, pg_sub_s32_overflow(), pg_sub_s64_overflow(), Interval::time, and interval::time.

Referenced by interval_div(), interval_mul(), interval_um(), timestamp_mi_interval(), and timestamptz_mi_interval_internal().

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1038 of file timestamp.c.

1039{
1041 int32 *tl;
1042 int n;
1043 int32 typmod;
1044
1046
1047 /*
1048 * tl[0] - interval range (fields bitmask) tl[1] - precision (optional)
1049 *
1050 * Note we must validate tl[0] even though it's normally guaranteed
1051 * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
1052 */
1053 if (n > 0)
1054 {
1055 switch (tl[0])
1056 {
1057 case INTERVAL_MASK(YEAR):
1071 /* all OK */
1072 break;
1073 default:
1074 ereport(ERROR,
1076 errmsg("invalid INTERVAL type modifier")));
1077 }
1078 }
1079
1080 if (n == 1)
1081 {
1082 if (tl[0] != INTERVAL_FULL_RANGE)
1084 else
1085 typmod = -1;
1086 }
1087 else if (n == 2)
1088 {
1089 if (tl[1] < 0)
1090 ereport(ERROR,
1092 errmsg("INTERVAL(%d) precision must not be negative",
1093 tl[1])));
1094 if (tl[1] > MAX_INTERVAL_PRECISION)
1095 {
1098 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1101 }
1102 else
1103 typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
1104 }
1105 else
1106 {
1107 ereport(ERROR,
1109 errmsg("invalid INTERVAL type modifier")));
1110 typmod = 0; /* keep compiler quiet */
1111 }
1112
1113 PG_RETURN_INT32(typmod);
1114}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
#define INTERVAL_TYPMOD(p, r)
Definition timestamp.h:80

References ArrayGetIntegerTypmods(), DAY, ereport, errcode(), errmsg, ERROR, fb(), HOUR, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_TYPMOD, MAX_INTERVAL_PRECISION, MINUTE, MONTH, PG_GETARG_ARRAYTYPE_P, PG_RETURN_INT32, SECOND, WARNING, and YEAR.

◆ intervaltypmodleastfield()

static int intervaltypmodleastfield ( int32  typmod)
static

Definition at line 1203 of file timestamp.c.

1204{
1205 if (typmod < 0)
1206 return 0; /* SECOND */
1207
1208 switch (INTERVAL_RANGE(typmod))
1209 {
1210 case INTERVAL_MASK(YEAR):
1211 return 5; /* YEAR */
1212 case INTERVAL_MASK(MONTH):
1213 return 4; /* MONTH */
1214 case INTERVAL_MASK(DAY):
1215 return 3; /* DAY */
1216 case INTERVAL_MASK(HOUR):
1217 return 2; /* HOUR */
1218 case INTERVAL_MASK(MINUTE):
1219 return 1; /* MINUTE */
1220 case INTERVAL_MASK(SECOND):
1221 return 0; /* SECOND */
1223 return 4; /* MONTH */
1225 return 2; /* HOUR */
1227 return 1; /* MINUTE */
1229 return 0; /* SECOND */
1231 return 1; /* MINUTE */
1233 return 0; /* SECOND */
1235 return 0; /* SECOND */
1237 return 0; /* SECOND */
1238 default:
1239 elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1240 break;
1241 }
1242 return 0; /* can't get here, but keep compiler quiet */
1243}

References DAY, elog, ERROR, HOUR, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_RANGE, MINUTE, MONTH, SECOND, and YEAR.

Referenced by interval_support().

◆ intervaltypmodout()

Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1117 of file timestamp.c.

1118{
1119 int32 typmod = PG_GETARG_INT32(0);
1120 char *res = (char *) palloc(64);
1121 int fields;
1122 int precision;
1123 const char *fieldstr;
1124
1125 if (typmod < 0)
1126 {
1127 *res = '\0';
1128 PG_RETURN_CSTRING(res);
1129 }
1130
1131 fields = INTERVAL_RANGE(typmod);
1132 precision = INTERVAL_PRECISION(typmod);
1133
1134 switch (fields)
1135 {
1136 case INTERVAL_MASK(YEAR):
1137 fieldstr = " year";
1138 break;
1139 case INTERVAL_MASK(MONTH):
1140 fieldstr = " month";
1141 break;
1142 case INTERVAL_MASK(DAY):
1143 fieldstr = " day";
1144 break;
1145 case INTERVAL_MASK(HOUR):
1146 fieldstr = " hour";
1147 break;
1148 case INTERVAL_MASK(MINUTE):
1149 fieldstr = " minute";
1150 break;
1151 case INTERVAL_MASK(SECOND):
1152 fieldstr = " second";
1153 break;
1155 fieldstr = " year to month";
1156 break;
1158 fieldstr = " day to hour";
1159 break;
1161 fieldstr = " day to minute";
1162 break;
1164 fieldstr = " day to second";
1165 break;
1167 fieldstr = " hour to minute";
1168 break;
1170 fieldstr = " hour to second";
1171 break;
1173 fieldstr = " minute to second";
1174 break;
1176 fieldstr = "";
1177 break;
1178 default:
1179 elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1180 fieldstr = "";
1181 break;
1182 }
1183
1184 if (precision != INTERVAL_FULL_PRECISION)
1185 snprintf(res, 64, "%s(%d)", fieldstr, precision);
1186 else
1187 snprintf(res, 64, "%s", fieldstr);
1188
1189 PG_RETURN_CSTRING(res);
1190}
void * palloc(Size size)
Definition mcxt.c:1387
#define snprintf
Definition port.h:260

References DAY, elog, ERROR, fb(), HOUR, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_PRECISION, INTERVAL_RANGE, MINUTE, MONTH, palloc(), PG_GETARG_INT32, PG_RETURN_CSTRING, SECOND, snprintf, and YEAR.

◆ isoweek2date()

void isoweek2date ( int  woy,
int year,
int mon,
int mday 
)

Definition at line 5255 of file timestamp.c.

5256{
5257 j2date(isoweek2j(*year, woy), year, mon, mday);
5258}
void j2date(int jd, int *year, int *month, int *day)
Definition datetime.c:322

References fb(), isoweek2j(), and j2date().

Referenced by do_to_timestamp(), timestamp_trunc(), and timestamptz_trunc_internal().

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 5235 of file timestamp.c.

5236{
5237 int day0,
5238 day4;
5239
5240 /* fourth day of current year */
5241 day4 = date2j(year, 1, 4);
5242
5243 /* day0 == offset to first day of week (Monday) */
5244 day0 = j2day(day4 - 1);
5245
5246 return ((week - 1) * 7) + (day4 - day0);
5247}

References date2j(), fb(), and j2day().

Referenced by date2isoyearday(), do_to_timestamp(), isoweek2date(), and isoweekdate2date().

◆ isoweekdate2date()

void isoweekdate2date ( int  isoweek,
int  wday,
int year,
int mon,
int mday 
)

Definition at line 5268 of file timestamp.c.

5269{
5270 int jday;
5271
5272 jday = isoweek2j(*year, isoweek);
5273 /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
5274 if (wday > 1)
5275 jday += wday - 2;
5276 else
5277 jday += 6;
5278 j2date(jday, year, mon, mday);
5279}

References fb(), isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 2068 of file timestamp.c.

2069{
2070 int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2071
2073 return -1;
2074 span->month = (int32) total_months;
2075 span->day = itm->tm_mday;
2077 &span->time))
2078 return -1;
2079 /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2080 if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2081 &span->time))
2082 return -1;
2083 if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2084 &span->time))
2085 return -1;
2086 if (pg_add_s64_overflow(span->time, itm->tm_usec,
2087 &span->time))
2088 return -1;
2090 return -1;
2091 return 0;
2092}

References fb(), INTERVAL_NOT_FINITE, MONTHS_PER_YEAR, pg_add_s64_overflow(), pg_mul_s64_overflow(), USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by interval_trunc(), timestamp_age(), and timestamptz_age().

◆ itmin2interval()

int itmin2interval ( struct pg_itm_in itm_in,
Interval span 
)

Definition at line 2106 of file timestamp.c.

2107{
2108 int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2109
2111 return -1;
2112 span->month = (int32) total_months;
2113 span->day = itm_in->tm_mday;
2114 span->time = itm_in->tm_usec;
2115 return 0;
2116}

References fb(), and MONTHS_PER_YEAR.

Referenced by interval_in(), pg_timezone_abbrevs_abbrevs(), pg_timezone_abbrevs_zone(), and pg_timezone_names().

◆ lookup_timezone()

static pg_tz * lookup_timezone ( text zone)
static

Definition at line 551 of file timestamp.c.

552{
553 char tzname[TZ_STRLEN_MAX + 1];
554
556
558}
pg_tz * DecodeTimezoneNameToTz(const char *tzname)
Definition datetime.c:3344
#define TZ_STRLEN_MAX
Definition pgtime.h:54
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition varlena.c:248

References DecodeTimezoneNameToTz(), fb(), text_to_cstring_buffer(), and TZ_STRLEN_MAX.

Referenced by generate_series_timestamptz_internal(), timestamptz_mi_interval_at_zone(), timestamptz_pl_interval_at_zone(), and timestamptz_trunc_zone().

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1521 of file timestamp.c.

1522{
1529 double secs = PG_GETARG_FLOAT8(6);
1530 Interval *result;
1531
1532 /*
1533 * Reject out-of-range inputs. We reject any input values that cause
1534 * integer overflow of the corresponding interval fields.
1535 */
1536 if (isinf(secs) || isnan(secs))
1537 goto out_of_range;
1538
1539 result = palloc_object(Interval);
1540
1541 /* years and months -> months */
1543 pg_add_s32_overflow(result->month, months, &result->month))
1544 goto out_of_range;
1545
1546 /* weeks and days -> days */
1547 if (pg_mul_s32_overflow(weeks, DAYS_PER_WEEK, &result->day) ||
1548 pg_add_s32_overflow(result->day, days, &result->day))
1549 goto out_of_range;
1550
1551 /* hours and mins -> usecs (cannot overflow 64-bit) */
1553
1554 /* secs -> usecs */
1556 if (!FLOAT8_FITS_IN_INT64(secs) ||
1557 pg_add_s64_overflow(result->time, (int64) secs, &result->time))
1558 goto out_of_range;
1559
1560 /* make sure that the result is finite */
1561 if (INTERVAL_NOT_FINITE(result))
1562 goto out_of_range;
1563
1564 PG_RETURN_INTERVAL_P(result);
1565
1567 ereport(ERROR,
1569 errmsg("interval out of range"));
1570
1571 PG_RETURN_NULL(); /* keep compiler quiet */
1572}
const char *const months[]
Definition datetime.c:82
#define DAYS_PER_WEEK
Definition timestamp.h:117
static float8 float8_mul(const float8 val1, const float8 val2)
Definition float.h:163
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:187

References Interval::day, days, DAYS_PER_WEEK, ereport, errcode(), errmsg, ERROR, fb(), FLOAT8_FITS_IN_INT64, float8_mul(), INTERVAL_NOT_FINITE, Interval::month, months, MONTHS_PER_YEAR, palloc_object, pg_add_s32_overflow(), pg_add_s64_overflow(), PG_GETARG_FLOAT8, PG_GETARG_INT32, pg_mul_s32_overflow(), PG_RETURN_INTERVAL_P, PG_RETURN_NULL, Interval::time, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 636 of file timestamp.c.

637{
638 int32 year = PG_GETARG_INT32(0);
639 int32 month = PG_GETARG_INT32(1);
642 int32 min = PG_GETARG_INT32(4);
643 float8 sec = PG_GETARG_FLOAT8(5);
644 Timestamp result;
645
646 result = make_timestamp_internal(year, month, mday,
647 hour, min, sec);
648
649 PG_RETURN_TIMESTAMP(result);
650}
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition timestamp.c:565

References fb(), make_timestamp_internal(), PG_GETARG_FLOAT8, PG_GETARG_INT32, and PG_RETURN_TIMESTAMP.

◆ make_timestamp_internal()

static Timestamp make_timestamp_internal ( int  year,
int  month,
int  day,
int  hour,
int  min,
double  sec 
)
static

Definition at line 565 of file timestamp.c.

567{
568 struct pg_tm tm;
570 TimeOffset time;
571 int dterr;
572 bool bc = false;
573 Timestamp result;
574
575 tm.tm_year = year;
576 tm.tm_mon = month;
577 tm.tm_mday = day;
578
579 /* Handle negative years as BC */
580 if (tm.tm_year < 0)
581 {
582 bc = true;
583 tm.tm_year = -tm.tm_year;
584 }
585
586 dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
587
588 if (dterr != 0)
591 errmsg("date field value out of range: %d-%02d-%02d",
592 year, month, day)));
593
597 errmsg("date out of range: %d-%02d-%02d",
598 year, month, day)));
599
601
602 /* Check for time overflow */
603 if (float_time_overflows(hour, min, sec))
606 errmsg("time field value out of range: %d:%02d:%02g",
607 hour, min, sec)));
608
609 /* This should match tm2time */
610 time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
612
614 pg_add_s64_overflow(result, time, &result)))
617 errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
618 year, month, day,
619 hour, min, sec)));
620
621 /* final range check catches just-out-of-range timestamps */
622 if (!IS_VALID_TIMESTAMP(result))
625 errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
626 year, month, day,
627 hour, min, sec)));
628
629 return result;
630}
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition datetime.c:2562
#define unlikely(x)
Definition c.h:432
#define MINS_PER_HOUR
Definition timestamp.h:129
#define IS_VALID_JULIAN(y, m, d)
Definition timestamp.h:227
#define SECS_PER_MINUTE
Definition timestamp.h:128
bool float_time_overflows(int hour, int min, double sec)
Definition date.c:1539
#define DTK_DATE_M
Definition datetime.h:191
long date
Definition pgtypes_date.h:9

References date2j(), DTK_DATE_M, ereport, errcode(), errmsg, ERROR, fb(), float_time_overflows(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, MINS_PER_HOUR, pg_add_s64_overflow(), pg_mul_s64_overflow(), POSTGRES_EPOCH_JDATE, SECS_PER_MINUTE, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, unlikely, USECS_PER_DAY, USECS_PER_SEC, and ValidateDate().

Referenced by make_timestamp(), make_timestamptz(), and make_timestamptz_at_timezone().

◆ make_timestamptz()

Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 656 of file timestamp.c.

657{
658 int32 year = PG_GETARG_INT32(0);
659 int32 month = PG_GETARG_INT32(1);
662 int32 min = PG_GETARG_INT32(4);
663 float8 sec = PG_GETARG_FLOAT8(5);
664 Timestamp result;
665
666 result = make_timestamp_internal(year, month, mday,
667 hour, min, sec);
668
670}
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition timestamp.c:6475

References fb(), make_timestamp_internal(), PG_GETARG_FLOAT8, PG_GETARG_INT32, PG_RETURN_TIMESTAMPTZ, and timestamp2timestamptz().

◆ make_timestamptz_at_timezone()

Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 677 of file timestamp.c.

678{
679 int32 year = PG_GETARG_INT32(0);
680 int32 month = PG_GETARG_INT32(1);
683 int32 min = PG_GETARG_INT32(4);
684 float8 sec = PG_GETARG_FLOAT8(5);
686 TimestampTz result;
688 struct pg_tm tt;
689 int tz;
690 fsec_t fsec;
691
693 hour, min, sec);
694
695 if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
698 errmsg("timestamp out of range")));
699
701
702 result = dt2local(timestamp, -tz);
703
704 if (!IS_VALID_TIMESTAMP(result))
707 errmsg("timestamp out of range")));
708
709 PG_RETURN_TIMESTAMPTZ(result);
710}
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition timestamp.c:482
int32 fsec_t
Definition timestamp.h:41
static int timestamp2tm(timestamp dt, int *tzp, struct tm *tm, fsec_t *fsec, const char **tzn)
Definition timestamp.c:88
static timestamp dt2local(timestamp dt, int tz)
Definition timestamp.c:23
int64 timestamp

References dt2local(), ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, make_timestamp_internal(), parse_sane_timezone(), PG_GETARG_FLOAT8, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TIMESTAMPTZ, and timestamp2tm().

◆ makeIntervalAggState()

static IntervalAggState * makeIntervalAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 3958 of file timestamp.c.

3959{
3961 MemoryContext agg_context;
3963
3964 if (!AggCheckCallContext(fcinfo, &agg_context))
3965 elog(ERROR, "aggregate function called in non-aggregate context");
3966
3967 old_context = MemoryContextSwitchTo(agg_context);
3968
3970
3972
3973 return state;
3974}

References AggCheckCallContext(), elog, ERROR, fb(), MemoryContextSwitchTo(), and palloc0_object.

Referenced by interval_avg_accum(), and interval_avg_combine().

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3719 of file timestamp.c.

3720{
3721 /* Args are float8 and Interval *, but leave them as generic Datum */
3724
3726}
Datum interval_mul(PG_FUNCTION_ARGS)
Definition timestamp.c:3599

References DirectFunctionCall2, fb(), interval_mul(), and PG_GETARG_DATUM.

◆ NonFiniteIntervalPart()

static float8 NonFiniteIntervalPart ( int  type,
int  unit,
char lowunits,
bool  isNegative 
)
static

Definition at line 6025 of file timestamp.c.

6026{
6027 if ((type != UNITS) && (type != RESERV))
6028 ereport(ERROR,
6030 errmsg("unit \"%s\" not recognized for type %s",
6032
6033 switch (unit)
6034 {
6035 /* Oscillating units */
6036 case DTK_MICROSEC:
6037 case DTK_MILLISEC:
6038 case DTK_SECOND:
6039 case DTK_MINUTE:
6040 case DTK_WEEK:
6041 case DTK_MONTH:
6042 case DTK_QUARTER:
6043 return 0.0;
6044
6045 /* Monotonically-increasing units */
6046 case DTK_HOUR:
6047 case DTK_DAY:
6048 case DTK_YEAR:
6049 case DTK_DECADE:
6050 case DTK_CENTURY:
6051 case DTK_MILLENNIUM:
6052 case DTK_EPOCH:
6053 if (isNegative)
6054 return -get_float8_infinity();
6055 else
6056 return get_float8_infinity();
6057
6058 default:
6059 ereport(ERROR,
6061 errmsg("unit \"%s\" not supported for type %s",
6063 return 0.0; /* keep compiler quiet */
6064 }
6065}
static float8 get_float8_infinity(void)
Definition float.h:65

References DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_WEEK, DTK_YEAR, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), get_float8_infinity(), RESERV, type, and UNITS.

Referenced by interval_part_common().

◆ NonFiniteTimestampTzPart()

static float8 NonFiniteTimestampTzPart ( int  type,
int  unit,
char lowunits,
bool  isNegative,
bool  isTz 
)
static

Definition at line 5415 of file timestamp.c.

5417{
5418 if ((type != UNITS) && (type != RESERV))
5419 ereport(ERROR,
5421 errmsg("unit \"%s\" not recognized for type %s",
5422 lowunits,
5424
5425 switch (unit)
5426 {
5427 /* Oscillating units */
5428 case DTK_MICROSEC:
5429 case DTK_MILLISEC:
5430 case DTK_SECOND:
5431 case DTK_MINUTE:
5432 case DTK_HOUR:
5433 case DTK_DAY:
5434 case DTK_MONTH:
5435 case DTK_QUARTER:
5436 case DTK_WEEK:
5437 case DTK_DOW:
5438 case DTK_ISODOW:
5439 case DTK_DOY:
5440 case DTK_TZ:
5441 case DTK_TZ_MINUTE:
5442 case DTK_TZ_HOUR:
5443 return 0.0;
5444
5445 /* Monotonically-increasing units */
5446 case DTK_YEAR:
5447 case DTK_DECADE:
5448 case DTK_CENTURY:
5449 case DTK_MILLENNIUM:
5450 case DTK_JULIAN:
5451 case DTK_ISOYEAR:
5452 case DTK_EPOCH:
5453 if (isNegative)
5454 return -get_float8_infinity();
5455 else
5456 return get_float8_infinity();
5457
5458 default:
5459 ereport(ERROR,
5461 errmsg("unit \"%s\" not supported for type %s",
5462 lowunits,
5464 return 0.0; /* keep compiler quiet */
5465 }
5466}
#define DTK_JULIAN
Definition datetime.h:173
#define DTK_TZ_HOUR
Definition datetime.h:177
#define DTK_TZ_MINUTE
Definition datetime.h:178
#define DTK_ISODOW
Definition datetime.h:180
#define DTK_ISOYEAR
Definition datetime.h:179
#define DTK_DOY
Definition datetime.h:176
#define DTK_TZ
Definition datetime.h:146
#define DTK_DOW
Definition datetime.h:175

References DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_DOW, DTK_DOY, DTK_EPOCH, DTK_HOUR, DTK_ISODOW, DTK_ISOYEAR, DTK_JULIAN, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_WEEK, DTK_YEAR, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), get_float8_infinity(), RESERV, type, and UNITS.

Referenced by timestamp_part_common(), and timestamptz_part_common().

◆ now()

Definition at line 1600 of file timestamp.c.

References GetCurrentTransactionStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

Referenced by advanceConnectionState(), ApplyLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), CalculateCopyStreamSleeptime(), CheckArchiveTimeout(), CheckpointerMain(), DetermineSlotInvalidationCause(), doLog(), doRetry(), dumpTimestamp(), enable_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), executeMetaCommand(), flushAndSendFeedback(), get_candidate_xid(), get_role_password(), handle_sig_alarm(), HandleCopyStream(), has_startup_progress_timeout_expired(), initialize_prng(), InvalidatePossiblyObsoleteSlot(), IsCheckpointOnSchedule(), LagTrackerRead(), launch_sync_worker(), launch_worker(), libpqsrv_cancel(), LockBufferForCleanup(), logicalrep_worker_launch(), LogicalRepApplyLoop(), LogRecoveryConflict(), maybe_start_bgworkers(), mxid_age(), OutputFsync(), PerformRadiusTransaction(), pg_clock_gettime_ns(), pg_time_now(), pg_time_now_lazy(), pg_timezone_abbrevs_abbrevs(), pg_timezone_abbrevs_zone(), pgfdw_cancel_query(), pgfdw_finish_abort_cleanup(), pgfdw_get_cleanup_result(), pgstat_report_archiver(), pgstat_report_stat(), PQsocketPoll(), pqTraceFormatTimestamp(), printProgressReport(), printVerboseErrorMessages(), ProcessKeepaliveMsg(), ProcessStandbyReplyMessage(), processXactStats(), ProcSleep(), progress_report(), progress_report(), progress_report(), progress_report(), progress_report(), ReplicationSlotRelease(), RequestXLogStreaming(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), RestoreSlotFromDisk(), schedule_alarm(), send_feedback(), sendFeedback(), sendFeedback(), ServerLoop(), set_next_rotation_time(), should_stop_conflict_info_retention(), StreamLogicalLog(), SysLoggerMain(), threadRun(), timetz_zone(), update_synced_slots_inactive_since(), wait_for_local_flush(), WaitForWALToBecomeAvailable(), WalRcvComputeNextWakeup(), WalRcvRunning(), WalRcvStreaming(), WalReceiverMain(), WalSndComputeSleeptime(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), xid_age(), XLogBackgroundFlush(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2663 of file timestamp.c.

2664{
2665 /*
2666 * The arguments are Timestamps, but we leave them as generic Datums to
2667 * avoid unnecessary conversions between value and reference forms --- not
2668 * to mention possible dereferences of null pointers.
2669 */
2674 bool ts1IsNull = PG_ARGISNULL(0);
2675 bool te1IsNull = PG_ARGISNULL(1);
2676 bool ts2IsNull = PG_ARGISNULL(2);
2677 bool te2IsNull = PG_ARGISNULL(3);
2678
2679#define TIMESTAMP_GT(t1,t2) \
2680 DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2681#define TIMESTAMP_LT(t1,t2) \
2682 DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2683
2684 /*
2685 * If both endpoints of interval 1 are null, the result is null (unknown).
2686 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2687 * take ts1 as the lesser endpoint.
2688 */
2689 if (ts1IsNull)
2690 {
2691 if (te1IsNull)
2693 /* swap null for non-null */
2694 ts1 = te1;
2695 te1IsNull = true;
2696 }
2697 else if (!te1IsNull)
2698 {
2699 if (TIMESTAMP_GT(ts1, te1))
2700 {
2701 Datum tt = ts1;
2702
2703 ts1 = te1;
2704 te1 = tt;
2705 }
2706 }
2707
2708 /* Likewise for interval 2. */
2709 if (ts2IsNull)
2710 {
2711 if (te2IsNull)
2713 /* swap null for non-null */
2714 ts2 = te2;
2715 te2IsNull = true;
2716 }
2717 else if (!te2IsNull)
2718 {
2719 if (TIMESTAMP_GT(ts2, te2))
2720 {
2721 Datum tt = ts2;
2722
2723 ts2 = te2;
2724 te2 = tt;
2725 }
2726 }
2727
2728 /*
2729 * At this point neither ts1 nor ts2 is null, so we can consider three
2730 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2731 */
2732 if (TIMESTAMP_GT(ts1, ts2))
2733 {
2734 /*
2735 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2736 * in the presence of nulls it's not quite completely so.
2737 */
2738 if (te2IsNull)
2740 if (TIMESTAMP_LT(ts1, te2))
2741 PG_RETURN_BOOL(true);
2742 if (te1IsNull)
2744
2745 /*
2746 * If te1 is not null then we had ts1 <= te1 above, and we just found
2747 * ts1 >= te2, hence te1 >= te2.
2748 */
2749 PG_RETURN_BOOL(false);
2750 }
2751 else if (TIMESTAMP_LT(ts1, ts2))
2752 {
2753 /* This case is ts2 < te1 OR te2 < te1 */
2754 if (te1IsNull)
2756 if (TIMESTAMP_LT(ts2, te1))
2757 PG_RETURN_BOOL(true);
2758 if (te2IsNull)
2760
2761 /*
2762 * If te2 is not null then we had ts2 <= te2 above, and we just found
2763 * ts2 >= te1, hence te2 >= te1.
2764 */
2765 PG_RETURN_BOOL(false);
2766 }
2767 else
2768 {
2769 /*
2770 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2771 * rather silly way of saying "true if both are non-null, else null".
2772 */
2773 if (te1IsNull || te2IsNull)
2775 PG_RETURN_BOOL(true);
2776 }
2777
2778#undef TIMESTAMP_GT
2779#undef TIMESTAMP_LT
2780}
#define TIMESTAMP_GT(t1, t2)
#define TIMESTAMP_LT(t1, t2)

References fb(), PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_BOOL, PG_RETURN_NULL, TIMESTAMP_GT, and TIMESTAMP_LT.

◆ parse_sane_timezone()

static int parse_sane_timezone ( struct pg_tm tm,
text zone 
)
static

Definition at line 482 of file timestamp.c.

483{
484 char tzname[TZ_STRLEN_MAX + 1];
485 int dterr;
486 int tz;
487
489
490 /*
491 * Look up the requested timezone. First we try to interpret it as a
492 * numeric timezone specification; if DecodeTimezone decides it doesn't
493 * like the format, we try timezone abbreviations and names.
494 *
495 * Note pg_tzset happily parses numeric input that DecodeTimezone would
496 * reject. To avoid having it accept input that would otherwise be seen
497 * as invalid, it's enough to disallow having a digit in the first
498 * position of our input string.
499 */
500 if (isdigit((unsigned char) *tzname))
503 errmsg("invalid input syntax for type %s: \"%s\"",
504 "numeric time zone", tzname),
505 errhint("Numeric time zones must have \"-\" or \"+\" as first character.")));
506
508 if (dterr != 0)
509 {
510 int type,
511 val;
512 pg_tz *tzp;
513
517 errmsg("numeric time zone \"%s\" out of range", tzname)));
518 else if (dterr != DTERR_BAD_FORMAT)
521 errmsg("time zone \"%s\" not recognized", tzname)));
522
524
526 {
527 /* fixed-offset abbreviation */
528 tz = -val;
529 }
530 else if (type == TZNAME_DYNTZ)
531 {
532 /* dynamic-offset abbreviation, resolve using specified time */
534 }
535 else
536 {
537 /* full zone name */
538 tz = DetermineTimeZoneOffset(tm, tzp);
539 }
540 }
541
542 return tz;
543}
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition datetime.c:1605
int DecodeTimezone(const char *str, int *tzp)
Definition datetime.c:3058
int DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
Definition datetime.c:3289
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition datetime.c:1766
int errhint(const char *fmt,...) pg_attribute_printf(1
#define TZNAME_FIXED_OFFSET
Definition datetime.h:299
#define TZNAME_DYNTZ
Definition datetime.h:300
#define DTERR_TZDISP_OVERFLOW
Definition datetime.h:286
Definition pgtz.h:66

References DecodeTimezone(), DecodeTimezoneName(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), DTERR_BAD_FORMAT, DTERR_TZDISP_OVERFLOW, ereport, errcode(), errhint(), errmsg, ERROR, fb(), text_to_cstring_buffer(), tm, type, TZ_STRLEN_MAX, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, and val.

Referenced by make_timestamptz_at_timezone().

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1624 of file timestamp.c.

1625{
1627}
TimestampTz PgReloadTime
Definition timestamp.c:48

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

◆ pg_postmaster_start_time()

Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1618 of file timestamp.c.

1619{
1621}
TimestampTz PgStartTime
Definition timestamp.c:45

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2181 of file timestamp.c.

2182{
2183 Timestamp dt;
2184 struct pg_tm tt,
2185 *tm = &tt;
2186
2188 /* we don't bother to test for failure ... */
2189 tm2timestamp(tm, 0, NULL, &dt);
2190
2191 return dt;
2192} /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition timestamp.c:2159
int tm2timestamp(struct tm *tm, fsec_t fsec, int *tzp, timestamp *result)
Definition timestamp.c:37

References fb(), GetEpochTime(), tm, and tm2timestamp().

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1606 of file timestamp.c.

1607{
1609}
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition xact.c:881

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

◆ time2t()

static TimeOffset time2t ( const int  hour,
const int  min,
const int  sec,
const fsec_t  fsec 
)
static

Definition at line 2119 of file timestamp.c.

2120{
2121 return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2122}

References fb(), MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1811 of file timestamp.c.

1812{
1813 TimestampTz result;
1814
1815 result = (TimestampTz) tm -
1817 result *= USECS_PER_SEC;
1818
1819 return result;
1820}

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, tm, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by pg_control_checkpoint(), pg_control_system(), pg_ls_dir_files(), and pg_stat_file().

◆ timeofday()

Datum timeofday ( PG_FUNCTION_ARGS  )

Definition at line 1682 of file timestamp.c.

1683{
1684 struct timeval tp;
1685 char templ[128];
1686 char buf[128];
1687 pg_time_t tt;
1688
1689 gettimeofday(&tp, NULL);
1690 tt = (pg_time_t) tp.tv_sec;
1691 pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1693 snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1694
1696}
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
size_t pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t)
Definition strftime.c:128
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition localtime.c:1345
Definition lexi.c:60
text * cstring_to_text(const char *s)
Definition varlena.c:184

References buf, cstring_to_text(), fb(), gettimeofday(), pg_localtime(), PG_RETURN_TEXT_P, pg_strftime(), session_timezone, and snprintf.

◆ timestamp2timestamptz()

static TimestampTz timestamp2timestamptz ( Timestamp  timestamp)
static

Definition at line 6475 of file timestamp.c.

6476{
6478}
TimestampTz timestamp2timestamptz_safe(Timestamp timestamp, Node *escontext)
Definition timestamp.c:6439

References fb(), and timestamp2timestamptz_safe().

Referenced by make_timestamptz(), and timestamp_timestamptz().

◆ timestamp2timestamptz_safe()

TimestampTz timestamp2timestamptz_safe ( Timestamp  timestamp,
Node escontext 
)

Definition at line 6439 of file timestamp.c.

6440{
6441 TimestampTz result;
6442 struct pg_tm tt,
6443 *tm = &tt;
6444 fsec_t fsec;
6445 int tz;
6446
6448 return timestamp;
6449
6450 /* timestamp2tm should not fail on valid timestamps, but cope */
6451 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
6452 {
6454
6455 result = dt2local(timestamp, -tz);
6456
6457 if (IS_VALID_TIMESTAMP(result))
6458 return result;
6459 }
6460
6461 if (timestamp < 0)
6462 TIMESTAMP_NOBEGIN(result);
6463 else
6464 TIMESTAMP_NOEND(result);
6465
6466 ereturn(escontext, result,
6468 errmsg("timestamp out of range")));
6469}

References DetermineTimeZoneOffset(), dt2local(), ereturn, errcode(), errmsg, fb(), IS_VALID_TIMESTAMP, session_timezone, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, and tm.

Referenced by cvt_timestamp_timestamptz(), timestamp2timestamptz(), and timestamp_cmp_timestamptz_internal().

◆ timestamp2tm()

int timestamp2tm ( Timestamp  dt,
int tzp,
struct pg_tm tm,
fsec_t fsec,
const char **  tzn,
pg_tz attimezone 
)

Definition at line 1901 of file timestamp.c.

1902{
1904 Timestamp time;
1906
1907 /* Use session timezone if caller asks for default */
1908 if (attimezone == NULL)
1909 attimezone = session_timezone;
1910
1911 time = dt;
1912 TMODULO(time, date, USECS_PER_DAY);
1913
1914 if (time < INT64CONST(0))
1915 {
1916 time += USECS_PER_DAY;
1917 date -= 1;
1918 }
1919
1920 /* add offset to go from J2000 back to standard Julian date */
1922
1923 /* Julian day routine does not work for negative Julian days */
1925 return -1;
1926
1927 j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1928 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1929
1930 /* Done if no TZ conversion wanted */
1931 if (tzp == NULL)
1932 {
1933 tm->tm_isdst = -1;
1934 tm->tm_gmtoff = 0;
1935 tm->tm_zone = NULL;
1936 if (tzn != NULL)
1937 *tzn = NULL;
1938 return 0;
1939 }
1940
1941 /*
1942 * If the time falls within the range of pg_time_t, use pg_localtime() to
1943 * rotate to the local time zone.
1944 *
1945 * First, convert to an integral timestamp, avoiding possibly
1946 * platform-specific roundoff-in-wrong-direction errors, and adjust to
1947 * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1948 * coding avoids hardwiring any assumptions about the width of pg_time_t,
1949 * so it should behave sanely on machines without int64.
1950 */
1951 dt = (dt - *fsec) / USECS_PER_SEC +
1953 utime = (pg_time_t) dt;
1954 if ((Timestamp) utime == dt)
1955 {
1956 struct pg_tm *tx = pg_localtime(&utime, attimezone);
1957
1958 tm->tm_year = tx->tm_year + 1900;
1959 tm->tm_mon = tx->tm_mon + 1;
1960 tm->tm_mday = tx->tm_mday;
1961 tm->tm_hour = tx->tm_hour;
1962 tm->tm_min = tx->tm_min;
1963 tm->tm_sec = tx->tm_sec;
1964 tm->tm_isdst = tx->tm_isdst;
1965 tm->tm_gmtoff = tx->tm_gmtoff;
1966 tm->tm_zone = tx->tm_zone;
1967 *tzp = -tm->tm_gmtoff;
1968 if (tzn != NULL)
1969 *tzn = tm->tm_zone;
1970 }
1971 else
1972 {
1973 /*
1974 * When out of range of pg_time_t, treat as GMT
1975 */
1976 *tzp = 0;
1977 /* Mark this as *no* time zone available */
1978 tm->tm_isdst = -1;
1979 tm->tm_gmtoff = 0;
1980 tm->tm_zone = NULL;
1981 if (tzn != NULL)
1982 *tzn = NULL;
1983 }
1984
1985 return 0;
1986}
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition timestamp.c:1874
const char * tm_zone
Definition pgtime.h:46
int tm_isdst
Definition pgtime.h:44
long int tm_gmtoff
Definition pgtime.h:45

References dt2time(), fb(), INT64CONST, j2date(), pg_localtime(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, session_timezone, tm, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, pg_tm::tm_zone, TMODULO, UNIX_EPOCH_JDATE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by DetermineTimeZoneAbbrevOffsetTS(), executeDateTimeMethod(), GetCurrentTimeUsec(), JsonEncodeDateTime(), map_sql_value_to_xml_value(), pg_timezone_names(), timestamp2date_safe(), timestamp_time(), timestamp_to_char(), timestamptz2date_safe(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), and timetz_zone().

◆ timestamp_age()

Datum timestamp_age ( PG_FUNCTION_ARGS  )

Definition at line 4279 of file timestamp.c.

4280{
4283 Interval *result;
4284 fsec_t fsec1,
4285 fsec2;
4286 struct pg_itm tt,
4287 *tm = &tt;
4288 struct pg_tm tt1,
4289 *tm1 = &tt1;
4290 struct pg_tm tt2,
4291 *tm2 = &tt2;
4292
4293 result = palloc_object(Interval);
4294
4295 /*
4296 * Handle infinities.
4297 *
4298 * We treat anything that amounts to "infinity - infinity" as an error,
4299 * since the interval type has nothing equivalent to NaN.
4300 */
4302 {
4304 ereport(ERROR,
4306 errmsg("interval out of range")));
4307 else
4308 INTERVAL_NOBEGIN(result);
4309 }
4310 else if (TIMESTAMP_IS_NOEND(dt1))
4311 {
4313 ereport(ERROR,
4315 errmsg("interval out of range")));
4316 else
4317 INTERVAL_NOEND(result);
4318 }
4319 else if (TIMESTAMP_IS_NOBEGIN(dt2))
4320 INTERVAL_NOEND(result);
4321 else if (TIMESTAMP_IS_NOEND(dt2))
4322 INTERVAL_NOBEGIN(result);
4323 else if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
4324 timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
4325 {
4326 /* form the symbolic difference */
4327 tm->tm_usec = fsec1 - fsec2;
4328 tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
4329 tm->tm_min = tm1->tm_min - tm2->tm_min;
4330 tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
4331 tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
4332 tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
4333 tm->tm_year = tm1->tm_year - tm2->tm_year;
4334
4335 /* flip sign if necessary... */
4336 if (dt1 < dt2)
4337 {
4338 tm->tm_usec = -tm->tm_usec;
4339 tm->tm_sec = -tm->tm_sec;
4340 tm->tm_min = -tm->tm_min;
4341 tm->tm_hour = -tm->tm_hour;
4342 tm->tm_mday = -tm->tm_mday;
4343 tm->tm_mon = -tm->tm_mon;
4344 tm->tm_year = -tm->tm_year;
4345 }
4346
4347 /* propagate any negative fields into the next higher field */
4348 while (tm->tm_usec < 0)
4349 {
4350 tm->tm_usec += USECS_PER_SEC;
4351 tm->tm_sec--;
4352 }
4353
4354 while (tm->tm_sec < 0)
4355 {
4357 tm->tm_min--;
4358 }
4359
4360 while (tm->tm_min < 0)
4361 {
4363 tm->tm_hour--;
4364 }
4365
4366 while (tm->tm_hour < 0)
4367 {
4369 tm->tm_mday--;
4370 }
4371
4372 while (tm->tm_mday < 0)
4373 {
4374 if (dt1 < dt2)
4375 {
4376 tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
4377 tm->tm_mon--;
4378 }
4379 else
4380 {
4381 tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
4382 tm->tm_mon--;
4383 }
4384 }
4385
4386 while (tm->tm_mon < 0)
4387 {
4389 tm->tm_year--;
4390 }
4391
4392 /* recover sign if necessary... */
4393 if (dt1 < dt2)
4394 {
4395 tm->tm_usec = -tm->tm_usec;
4396 tm->tm_sec = -tm->tm_sec;
4397 tm->tm_min = -tm->tm_min;
4398 tm->tm_hour = -tm->tm_hour;
4399 tm->tm_mday = -tm->tm_mday;
4400 tm->tm_mon = -tm->tm_mon;
4401 tm->tm_year = -tm->tm_year;
4402 }
4403
4404 if (itm2interval(tm, result) != 0)
4405 ereport(ERROR,
4407 errmsg("interval out of range")));
4408 }
4409 else
4410 ereport(ERROR,
4412 errmsg("timestamp out of range")));
4413
4414 PG_RETURN_INTERVAL_P(result);
4415}
const int day_tab[2][13]
Definition datetime.c:76
#define HOURS_PER_DAY
Definition timestamp.h:118
#define isleap(y)
Definition datetime.h:271

References day_tab, ereport, errcode(), errmsg, ERROR, fb(), HOURS_PER_DAY, INTERVAL_NOBEGIN, INTERVAL_NOEND, isleap, itm2interval(), MINS_PER_HOUR, MONTHS_PER_YEAR, palloc_object, PG_GETARG_TIMESTAMP, PG_RETURN_INTERVAL_P, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, and USECS_PER_SEC.

◆ timestamp_at_local()

Datum timestamp_at_local ( PG_FUNCTION_ARGS  )

Definition at line 6929 of file timestamp.c.

6930{
6931 return timestamp_timestamptz(fcinfo);
6932}
Datum timestamp_timestamptz(PG_FUNCTION_ARGS)
Definition timestamp.c:6421

References timestamp_timestamptz().

◆ timestamp_bin()

Datum timestamp_bin ( PG_FUNCTION_ARGS  )

Definition at line 4579 of file timestamp.c.

4580{
4583 Timestamp origin = PG_GETARG_TIMESTAMP(2);
4584 Timestamp result,
4586 tm_diff,
4587 tm_modulo,
4588 tm_delta;
4589
4592
4593 if (TIMESTAMP_NOT_FINITE(origin))
4594 ereport(ERROR,
4596 errmsg("origin out of range")));
4597
4599 ereport(ERROR,
4601 errmsg("timestamps cannot be binned into infinite intervals")));
4602
4603 if (stride->month != 0)
4604 ereport(ERROR,
4606 errmsg("timestamps cannot be binned into intervals containing months or years")));
4607
4610 ereport(ERROR,
4612 errmsg("interval out of range")));
4613
4614 if (stride_usecs <= 0)
4615 ereport(ERROR,
4617 errmsg("stride must be greater than zero")));
4618
4620 ereport(ERROR,
4622 errmsg("interval out of range")));
4623
4624 /* These calculations cannot overflow */
4627 result = origin + tm_delta;
4628
4629 /*
4630 * We want to round towards -infinity, not 0, when tm_diff is negative and
4631 * not a multiple of stride_usecs. This adjustment *can* cause overflow,
4632 * since the result might now be out of the range origin .. timestamp.
4633 */
4634 if (tm_modulo < 0)
4635 {
4636 if (unlikely(pg_sub_s64_overflow(result, stride_usecs, &result)) ||
4637 !IS_VALID_TIMESTAMP(result))
4638 ereport(ERROR,
4640 errmsg("timestamp out of range")));
4641 }
4642
4643 PG_RETURN_TIMESTAMP(result);
4644}

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, pg_mul_s64_overflow(), PG_RETURN_TIMESTAMP, pg_sub_s64_overflow(), TIMESTAMP_NOT_FINITE, unlikely, and USECS_PER_DAY.

◆ timestamp_cmp()

◆ timestamp_cmp_internal()

◆ timestamp_cmp_timestamptz()

Datum timestamp_cmp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2432 of file timestamp.c.

2433{
2436
2438}
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition timestamp.c:2354

References fb(), PG_GETARG_TIMESTAMP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_INT32, and timestamp_cmp_timestamptz_internal().

◆ timestamp_cmp_timestamptz_internal()

int32 timestamp_cmp_timestamptz_internal ( Timestamp  timestampVal,
TimestampTz  dt2 
)

Definition at line 2354 of file timestamp.c.

2355{
2358
2360 if (escontext.error_occurred)
2361 {
2363 {
2364 /* dt1 is larger than any finite timestamp, but less than infinity */
2365 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2366 }
2368 {
2369 /* dt1 is less than any finite timestamp, but more than -infinity */
2370 return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2371 }
2372 }
2373
2375}
#define timestamptz_cmp_internal(dt1, dt2)
Definition timestamp.h:143

References ErrorSaveContext::error_occurred, fb(), timestamp2timestamptz_safe(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, and timestamptz_cmp_internal.

Referenced by cmpTimestampToTimestampTz(), timestamp_cmp_timestamptz(), timestamp_eq_timestamptz(), timestamp_ge_timestamptz(), timestamp_gt_timestamptz(), timestamp_le_timestamptz(), timestamp_lt_timestamptz(), timestamp_ne_timestamptz(), timestamptz_cmp_timestamp(), timestamptz_eq_timestamp(), timestamptz_ge_timestamp(), timestamptz_gt_timestamp(), timestamptz_le_timestamp(), timestamptz_lt_timestamp(), and timestamptz_ne_timestamp().

◆ timestamp_decrement()

static Datum timestamp_decrement ( Relation  rel,
Datum  existing,
bool underflow 
)
static

Definition at line 2280 of file timestamp.c.

2281{
2283
2284 if (texisting == PG_INT64_MIN)
2285 {
2286 /* return value is undefined */
2287 *underflow = true;
2288 return (Datum) 0;
2289 }
2290
2291 *underflow = false;
2292 return TimestampGetDatum(texisting - 1);
2293}
#define PG_INT64_MIN
Definition c.h:674

References DatumGetTimestamp(), fb(), PG_INT64_MIN, and TimestampGetDatum().

Referenced by timestamp_skipsupport().

◆ timestamp_eq()

Datum timestamp_eq ( PG_FUNCTION_ARGS  )

Definition at line 2207 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tseq().

◆ timestamp_eq_timestamptz()

◆ timestamp_finite()

Datum timestamp_finite ( PG_FUNCTION_ARGS  )

◆ timestamp_ge()

Datum timestamp_ge ( PG_FUNCTION_ARGS  )

Definition at line 2252 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsge().

◆ timestamp_ge_timestamptz()

◆ timestamp_gt()

Datum timestamp_gt ( PG_FUNCTION_ARGS  )

Definition at line 2234 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsgt().

◆ timestamp_gt_timestamptz()

◆ timestamp_hash()

Datum timestamp_hash ( PG_FUNCTION_ARGS  )

Definition at line 2326 of file timestamp.c.

2327{
2328 return hashint8(fcinfo);
2329}

References hashint8().

◆ timestamp_hash_extended()

Datum timestamp_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2332 of file timestamp.c.

2333{
2334 return hashint8extended(fcinfo);
2335}

References hashint8extended().

◆ timestamp_in()

Datum timestamp_in ( PG_FUNCTION_ARGS  )

Definition at line 157 of file timestamp.c.

158{
159 char *str = PG_GETARG_CSTRING(0);
160#ifdef NOT_USED
161 Oid typelem = PG_GETARG_OID(1);
162#endif
163 int32 typmod = PG_GETARG_INT32(2);
164 Node *escontext = fcinfo->context;
165 Timestamp result;
166 fsec_t fsec;
167 struct pg_tm tt,
168 *tm = &tt;
169 int tz;
170 int dtype;
171 int nf;
172 int dterr;
173 char *field[MAXDATEFIELDS];
174 int ftype[MAXDATEFIELDS];
176 DateTimeErrorExtra extra;
177
179 field, ftype, MAXDATEFIELDS, &nf);
180 if (dterr == 0)
181 dterr = DecodeDateTime(field, ftype, nf,
182 &dtype, tm, &fsec, &tz, &extra);
183 if (dterr != 0)
184 {
185 DateTimeParseError(dterr, &extra, str, "timestamp", escontext);
187 }
188
189 switch (dtype)
190 {
191 case DTK_DATE:
192 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
193 ereturn(escontext, (Datum) 0,
195 errmsg("timestamp out of range: \"%s\"", str)));
196 break;
197
198 case DTK_EPOCH:
199 result = SetEpochTimestamp();
200 break;
201
202 case DTK_LATE:
203 TIMESTAMP_NOEND(result);
204 break;
205
206 case DTK_EARLY:
207 TIMESTAMP_NOBEGIN(result);
208 break;
209
210 default:
211 elog(ERROR, "unexpected dtype %d while parsing timestamp \"%s\"",
212 dtype, str);
213 TIMESTAMP_NOEND(result);
214 }
215
216 AdjustTimestampForTypmod(&result, typmod, escontext);
217
218 PG_RETURN_TIMESTAMP(result);
219}
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition datetime.c:998
#define DTK_DATE
Definition datetime.h:144
static timestamp SetEpochTimestamp(void)
Definition timestamp.c:62

References AdjustTimestampForTypmod(), DateTimeParseError(), DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, elog, ereturn, errcode(), errmsg, ERROR, fb(), MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMP, SetEpochTimestamp(), str, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, and tm2timestamp().

Referenced by moddatetime().

◆ timestamp_increment()

static Datum timestamp_increment ( Relation  rel,
Datum  existing,
bool overflow 
)
static

Definition at line 2297 of file timestamp.c.

2298{
2300
2301 if (texisting == PG_INT64_MAX)
2302 {
2303 /* return value is undefined */
2304 *overflow = true;
2305 return (Datum) 0;
2306 }
2307
2308 *overflow = false;
2309 return TimestampGetDatum(texisting + 1);
2310}
#define PG_INT64_MAX
Definition c.h:675

References DatumGetTimestamp(), fb(), PG_INT64_MAX, and TimestampGetDatum().

Referenced by timestamp_skipsupport().

◆ timestamp_izone()

Datum timestamp_izone ( PG_FUNCTION_ARGS  )

Definition at line 6364 of file timestamp.c.

6365{
6368 TimestampTz result;
6369 int tz;
6370
6373
6375 ereport(ERROR,
6377 errmsg("interval time zone \"%s\" must be finite",
6379 PointerGetDatum(zone))))));
6380
6381 if (zone->month != 0 || zone->day != 0)
6382 ereport(ERROR,
6384 errmsg("interval time zone \"%s\" must not include months or days",
6386 PointerGetDatum(zone))))));
6387
6388 tz = zone->time / USECS_PER_SEC;
6389
6390 result = dt2local(timestamp, tz);
6391
6392 if (!IS_VALID_TIMESTAMP(result))
6393 ereport(ERROR,
6395 errmsg("timestamp out of range")));
6396
6397 PG_RETURN_TIMESTAMPTZ(result);
6398} /* timestamp_izone() */
Datum interval_out(PG_FUNCTION_ARGS)
Definition timestamp.c:964
static char * DatumGetCString(Datum X)
Definition postgres.h:355

References DatumGetCString(), DirectFunctionCall1, dt2local(), ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_out(), IS_VALID_TIMESTAMP, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMPTZ, PointerGetDatum(), TIMESTAMP_NOT_FINITE, and USECS_PER_SEC.

◆ timestamp_larger()

Datum timestamp_larger ( PG_FUNCTION_ARGS  )

Definition at line 2803 of file timestamp.c.

2804{
2807 Timestamp result;
2808
2809 if (timestamp_cmp_internal(dt1, dt2) > 0)
2810 result = dt1;
2811 else
2812 result = dt2;
2813 PG_RETURN_TIMESTAMP(result);
2814}

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, and timestamp_cmp_internal().

◆ timestamp_le()

Datum timestamp_le ( PG_FUNCTION_ARGS  )

Definition at line 2243 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsle().

◆ timestamp_le_timestamptz()

◆ timestamp_lt()

Datum timestamp_lt ( PG_FUNCTION_ARGS  )

Definition at line 2225 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tslt().

◆ timestamp_lt_timestamptz()

◆ timestamp_mi()

Datum timestamp_mi ( PG_FUNCTION_ARGS  )

Definition at line 2818 of file timestamp.c.

2819{
2822 Interval *result;
2823
2824 result = palloc_object(Interval);
2825
2826 /*
2827 * Handle infinities.
2828 *
2829 * We treat anything that amounts to "infinity - infinity" as an error,
2830 * since the interval type has nothing equivalent to NaN.
2831 */
2833 {
2835 {
2837 ereport(ERROR,
2839 errmsg("interval out of range")));
2840 else
2841 INTERVAL_NOBEGIN(result);
2842 }
2843 else if (TIMESTAMP_IS_NOEND(dt1))
2844 {
2846 ereport(ERROR,
2848 errmsg("interval out of range")));
2849 else
2850 INTERVAL_NOEND(result);
2851 }
2852 else if (TIMESTAMP_IS_NOBEGIN(dt2))
2853 INTERVAL_NOEND(result);
2854 else /* TIMESTAMP_IS_NOEND(dt2) */
2855 INTERVAL_NOBEGIN(result);
2856
2857 PG_RETURN_INTERVAL_P(result);
2858 }
2859
2860 if (unlikely(pg_sub_s64_overflow(dt1, dt2, &result->time)))
2861 ereport(ERROR,
2863 errmsg("interval out of range")));
2864
2865 result->month = 0;
2866 result->day = 0;
2867
2868 /*----------
2869 * This is wrong, but removing it breaks a lot of regression tests.
2870 * For example:
2871 *
2872 * test=> SET timezone = 'EST5EDT';
2873 * test=> SELECT
2874 * test-> ('2005-10-30 13:22:00-05'::timestamptz -
2875 * test(> '2005-10-29 13:22:00-04'::timestamptz);
2876 * ?column?
2877 * ----------------
2878 * 1 day 01:00:00
2879 * (1 row)
2880 *
2881 * so adding that to the first timestamp gets:
2882 *
2883 * test=> SELECT
2884 * test-> ('2005-10-29 13:22:00-04'::timestamptz +
2885 * test(> ('2005-10-30 13:22:00-05'::timestamptz -
2886 * test(> '2005-10-29 13:22:00-04'::timestamptz)) at time zone 'EST';
2887 * timezone
2888 * --------------------
2889 * 2005-10-30 14:22:00
2890 * (1 row)
2891 *----------
2892 */
2894 IntervalPGetDatum(result)));
2895
2896 PG_RETURN_INTERVAL_P(result);
2897}
Datum interval_justify_hours(PG_FUNCTION_ARGS)
Definition timestamp.c:2992

References DatumGetIntervalP(), Interval::day, DirectFunctionCall1, ereport, errcode(), errmsg, ERROR, fb(), interval_justify_hours(), INTERVAL_NOBEGIN, INTERVAL_NOEND, IntervalPGetDatum(), Interval::month, palloc_object, PG_GETARG_TIMESTAMP, PG_RETURN_INTERVAL_P, pg_sub_s64_overflow(), Interval::time, TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, TIMESTAMP_NOT_FINITE, and unlikely.

Referenced by gbt_ts_dist(), generate_series_timestamp_support(), ts_dist(), and tstz_dist().

◆ timestamp_mi_interval()

◆ timestamp_ne()

Datum timestamp_ne ( PG_FUNCTION_ARGS  )

◆ timestamp_ne_timestamptz()

◆ timestamp_out()

Datum timestamp_out ( PG_FUNCTION_ARGS  )

Definition at line 225 of file timestamp.c.

226{
228 char *result;
229 struct pg_tm tt,
230 *tm = &tt;
231 fsec_t fsec;
232 char buf[MAXDATELEN + 1];
233
236 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
237 EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf);
238 else
241 errmsg("timestamp out of range")));
242
243 result = pstrdup(buf);
244 PG_RETURN_CSTRING(result);
245}
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition datetime.c:4465
int DateStyle
Definition globals.c:125
static void EncodeSpecialTimestamp(timestamp dt, char *str)
Definition timestamp.c:187

References buf, DateStyle, EncodeDateTime(), EncodeSpecialTimestamp(), ereport, errcode(), errmsg, ERROR, fb(), MAXDATELEN, PG_GETARG_TIMESTAMP, PG_RETURN_CSTRING, pstrdup(), timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

Referenced by ExecGetJsonValueItemString().

◆ timestamp_part()

Datum timestamp_part ( PG_FUNCTION_ARGS  )

Definition at line 5730 of file timestamp.c.

5731{
5732 return timestamp_part_common(fcinfo, false);
5733}

References timestamp_part_common().

◆ timestamp_part_common()

static Datum timestamp_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5472 of file timestamp.c.

5473{
5478 int type,
5479 val;
5480 char *lowunits;
5481 fsec_t fsec;
5482 struct pg_tm tt,
5483 *tm = &tt;
5484
5487 false);
5488
5489 type = DecodeUnits(0, lowunits, &val);
5490 if (type == UNKNOWN_FIELD)
5492
5494 {
5497 false);
5498
5499 if (r != 0.0)
5500 {
5501 if (retnumeric)
5502 {
5503 if (r < 0)
5505 CStringGetDatum("-Infinity"),
5507 Int32GetDatum(-1));
5508 else if (r > 0)
5510 CStringGetDatum("Infinity"),
5512 Int32GetDatum(-1));
5513 }
5514 else
5516 }
5517 else
5519 }
5520
5521 if (type == UNITS)
5522 {
5523 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
5524 ereport(ERROR,
5526 errmsg("timestamp out of range")));
5527
5528 switch (val)
5529 {
5530 case DTK_MICROSEC:
5531 intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
5532 break;
5533
5534 case DTK_MILLISEC:
5535 if (retnumeric)
5536 /*---
5537 * tm->tm_sec * 1000 + fsec / 1000
5538 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5539 */
5541 else
5542 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
5543 break;
5544
5545 case DTK_SECOND:
5546 if (retnumeric)
5547 /*---
5548 * tm->tm_sec + fsec / 1'000'000
5549 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5550 */
5552 else
5553 PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
5554 break;
5555
5556 case DTK_MINUTE:
5557 intresult = tm->tm_min;
5558 break;
5559
5560 case DTK_HOUR:
5561 intresult = tm->tm_hour;
5562 break;
5563
5564 case DTK_DAY:
5565 intresult = tm->tm_mday;
5566 break;
5567
5568 case DTK_MONTH:
5569 intresult = tm->tm_mon;
5570 break;
5571
5572 case DTK_QUARTER:
5573 intresult = (tm->tm_mon - 1) / 3 + 1;
5574 break;
5575
5576 case DTK_WEEK:
5578 break;
5579
5580 case DTK_YEAR:
5581 if (tm->tm_year > 0)
5582 intresult = tm->tm_year;
5583 else
5584 /* there is no year 0, just 1 BC and 1 AD */
5585 intresult = tm->tm_year - 1;
5586 break;
5587
5588 case DTK_DECADE:
5589
5590 /*
5591 * what is a decade wrt dates? let us assume that decade 199
5592 * is 1990 thru 1999... decade 0 starts on year 1 BC, and -1
5593 * is 11 BC thru 2 BC...
5594 */
5595 if (tm->tm_year >= 0)
5596 intresult = tm->tm_year / 10;
5597 else
5598 intresult = -((8 - (tm->tm_year - 1)) / 10);
5599 break;
5600
5601 case DTK_CENTURY:
5602
5603 /* ----
5604 * centuries AD, c>0: year in [ (c-1)* 100 + 1 : c*100 ]
5605 * centuries BC, c<0: year in [ c*100 : (c+1) * 100 - 1]
5606 * there is no number 0 century.
5607 * ----
5608 */
5609 if (tm->tm_year > 0)
5610 intresult = (tm->tm_year + 99) / 100;
5611 else
5612 /* caution: C division may have negative remainder */
5613 intresult = -((99 - (tm->tm_year - 1)) / 100);
5614 break;
5615
5616 case DTK_MILLENNIUM:
5617 /* see comments above. */
5618 if (tm->tm_year > 0)
5619 intresult = (tm->tm_year + 999) / 1000;
5620 else
5621 intresult = -((999 - (tm->tm_year - 1)) / 1000);
5622 break;
5623
5624 case DTK_JULIAN:
5625 if (retnumeric)
5629 NULL),
5630 NULL));
5631 else
5634 tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY);
5635 break;
5636
5637 case DTK_ISOYEAR:
5639 /* Adjust BC years */
5640 if (intresult <= 0)
5641 intresult -= 1;
5642 break;
5643
5644 case DTK_DOW:
5645 case DTK_ISODOW:
5647 if (val == DTK_ISODOW && intresult == 0)
5648 intresult = 7;
5649 break;
5650
5651 case DTK_DOY:
5653 - date2j(tm->tm_year, 1, 1) + 1);
5654 break;
5655
5656 case DTK_TZ:
5657 case DTK_TZ_MINUTE:
5658 case DTK_TZ_HOUR:
5659 default:
5660 ereport(ERROR,
5662 errmsg("unit \"%s\" not supported for type %s",
5664 intresult = 0;
5665 }
5666 }
5667 else if (type == RESERV)
5668 {
5669 switch (val)
5670 {
5671 case DTK_EPOCH:
5673 /* (timestamp - epoch) / 1000000 */
5674 if (retnumeric)
5675 {
5676 Numeric result;
5677
5678 if (timestamp < (PG_INT64_MAX + epoch))
5680 else
5681 {
5684 NULL),
5685 int64_to_numeric(1000000),
5686 NULL);
5688 NumericGetDatum(result),
5689 Int32GetDatum(6)));
5690 }
5691 PG_RETURN_NUMERIC(result);
5692 }
5693 else
5694 {
5695 float8 result;
5696
5697 /* try to avoid precision loss in subtraction */
5698 if (timestamp < (PG_INT64_MAX + epoch))
5699 result = (timestamp - epoch) / 1000000.0;
5700 else
5701 result = ((float8) timestamp - epoch) / 1000000.0;
5702 PG_RETURN_FLOAT8(result);
5703 }
5704 break;
5705
5706 default:
5707 ereport(ERROR,
5709 errmsg("unit \"%s\" not supported for type %s",
5711 intresult = 0;
5712 }
5713 }
5714 else
5715 {
5716 ereport(ERROR,
5718 errmsg("unit \"%s\" not recognized for type %s",
5720 intresult = 0;
5721 }
5722
5723 if (retnumeric)
5725 else
5727}
Datum numeric_round(PG_FUNCTION_ARGS)
Definition numeric.c:1526
Numeric numeric_div_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:3152
Numeric numeric_sub_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:2957
static float8 NonFiniteTimestampTzPart(int type, int unit, char *lowunits, bool isNegative, bool isTz)
Definition timestamp.c:5415
int date2isoweek(int year, int mon, int mday)
Definition timestamp.c:5286
static Numeric DatumGetNumeric(Datum X)
Definition numeric.h:64
static Datum NumericGetDatum(Numeric X)
Definition numeric.h:76

References CStringGetDatum(), date2isoweek(), date2isoyear(), date2j(), DatumGetNumeric(), DecodeSpecial(), DecodeUnits(), DirectFunctionCall2, DirectFunctionCall3, downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_DOW, DTK_DOY, DTK_EPOCH, DTK_HOUR, DTK_ISODOW, DTK_ISOYEAR, DTK_JULIAN, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_WEEK, DTK_YEAR, epoch, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), Int32GetDatum(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, InvalidOid, j2day(), MINS_PER_HOUR, NonFiniteTimestampTzPart(), numeric_add_safe(), numeric_div_safe(), numeric_in(), numeric_round(), numeric_sub_safe(), NumericGetDatum(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMP, PG_INT64_MAX, PG_RETURN_FLOAT8, PG_RETURN_NULL, PG_RETURN_NUMERIC, RESERV, SECS_PER_DAY, SECS_PER_MINUTE, SetEpochTimestamp(), timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by extract_timestamp(), and timestamp_part().

◆ timestamp_pl_interval()

Datum timestamp_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 3081 of file timestamp.c.

3082{
3085 Timestamp result;
3086
3087 /*
3088 * Handle infinities.
3089 *
3090 * We treat anything that amounts to "infinity - infinity" as an error,
3091 * since the timestamp type has nothing equivalent to NaN.
3092 */
3094 {
3096 ereport(ERROR,
3098 errmsg("timestamp out of range")));
3099 else
3100 TIMESTAMP_NOBEGIN(result);
3101 }
3102 else if (INTERVAL_IS_NOEND(span))
3103 {
3105 ereport(ERROR,
3107 errmsg("timestamp out of range")));
3108 else
3109 TIMESTAMP_NOEND(result);
3110 }
3112 result = timestamp;
3113 else
3114 {
3115 if (span->month != 0)
3116 {
3117 struct pg_tm tt,
3118 *tm = &tt;
3119 fsec_t fsec;
3120
3121 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3122 ereport(ERROR,
3124 errmsg("timestamp out of range")));
3125
3126 if (pg_add_s32_overflow(tm->tm_mon, span->month, &tm->tm_mon))
3127 ereport(ERROR,
3129 errmsg("timestamp out of range")));
3130 if (tm->tm_mon > MONTHS_PER_YEAR)
3131 {
3132 tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
3133 tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
3134 }
3135 else if (tm->tm_mon < 1)
3136 {
3137 tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
3139 }
3140
3141 /* adjust for end of month boundary problems... */
3142 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
3143 tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
3144
3145 if (tm2timestamp(tm, fsec, NULL, &timestamp) != 0)
3146 ereport(ERROR,
3148 errmsg("timestamp out of range")));
3149 }
3150
3151 if (span->day != 0)
3152 {
3153 struct pg_tm tt,
3154 *tm = &tt;
3155 fsec_t fsec;
3156 int julian;
3157
3158 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3159 ereport(ERROR,
3161 errmsg("timestamp out of range")));
3162
3163 /*
3164 * Add days by converting to and from Julian. We need an overflow
3165 * check here since j2date expects a non-negative integer input.
3166 */
3168 if (pg_add_s32_overflow(julian, span->day, &julian) ||
3169 julian < 0)
3170 ereport(ERROR,
3172 errmsg("timestamp out of range")));
3174
3175 if (tm2timestamp(tm, fsec, NULL, &timestamp) != 0)
3176 ereport(ERROR,
3178 errmsg("timestamp out of range")));
3179 }
3180
3182 ereport(ERROR,
3184 errmsg("timestamp out of range")));
3185
3187 ereport(ERROR,
3189 errmsg("timestamp out of range")));
3190
3191 result = timestamp;
3192 }
3193
3194 PG_RETURN_TIMESTAMP(result);
3195}

References date2j(), day_tab, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, IS_VALID_TIMESTAMP, isleap, j2date(), MONTHS_PER_YEAR, pg_add_s32_overflow(), pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by date_pl_interval(), generate_series_timestamp(), in_range_timestamp_interval(), and timestamp_mi_interval().

◆ timestamp_recv()

Datum timestamp_recv ( PG_FUNCTION_ARGS  )

Definition at line 251 of file timestamp.c.

252{
254
255#ifdef NOT_USED
256 Oid typelem = PG_GETARG_OID(1);
257#endif
258 int32 typmod = PG_GETARG_INT32(2);
260 struct pg_tm tt,
261 *tm = &tt;
262 fsec_t fsec;
263
265
266 /* range check: see if timestamp_out would like it */
268 /* ok */ ;
269 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0 ||
273 errmsg("timestamp out of range")));
274
276
278}

References AdjustTimestampForTypmod(), buf, ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_TIMESTAMP, pq_getmsgint64(), timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

◆ timestamp_scale()

Datum timestamp_scale ( PG_FUNCTION_ARGS  )

Definition at line 338 of file timestamp.c.

339{
341 int32 typmod = PG_GETARG_INT32(1);
342 Timestamp result;
343
344 result = timestamp;
345
346 AdjustTimestampForTypmod(&result, typmod, NULL);
347
348 PG_RETURN_TIMESTAMP(result);
349}

References AdjustTimestampForTypmod(), fb(), PG_GETARG_INT32, PG_GETARG_TIMESTAMP, and PG_RETURN_TIMESTAMP.

◆ timestamp_send()

◆ timestamp_skipsupport()

Datum timestamp_skipsupport ( PG_FUNCTION_ARGS  )

Definition at line 2313 of file timestamp.c.

2314{
2316
2321
2323}
static Datum timestamp_increment(Relation rel, Datum existing, bool *overflow)
Definition timestamp.c:2297
static Datum timestamp_decrement(Relation rel, Datum existing, bool *underflow)
Definition timestamp.c:2280
#define PG_RETURN_VOID()
Definition fmgr.h:350
struct SkipSupportData * SkipSupport
Definition skipsupport.h:50
SkipSupportIncDec decrement
Definition skipsupport.h:91
SkipSupportIncDec increment
Definition skipsupport.h:92

References SkipSupportData::decrement, SkipSupportData::high_elem, SkipSupportData::increment, SkipSupportData::low_elem, PG_GETARG_POINTER, PG_INT64_MAX, PG_INT64_MIN, PG_RETURN_VOID, timestamp_decrement(), timestamp_increment(), and TimestampGetDatum().

◆ timestamp_smaller()

Datum timestamp_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2788 of file timestamp.c.

2789{
2792 Timestamp result;
2793
2794 /* use timestamp_cmp_internal to be sure this agrees with comparisons */
2795 if (timestamp_cmp_internal(dt1, dt2) < 0)
2796 result = dt1;
2797 else
2798 result = dt2;
2799 PG_RETURN_TIMESTAMP(result);
2800}

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, and timestamp_cmp_internal().

◆ timestamp_sortsupport()

Datum timestamp_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 2270 of file timestamp.c.

2271{
2273
2276}
struct SortSupportData * SortSupport
Definition execnodes.h:60
int(* comparator)(Datum x, Datum y, SortSupport ssup)
int ssup_datum_signed_cmp(Datum x, Datum y, SortSupport ssup)
Definition tuplesort.c:3422

References SortSupportData::comparator, PG_GETARG_POINTER, PG_RETURN_VOID, and ssup_datum_signed_cmp().

◆ timestamp_support()

Datum timestamp_support ( PG_FUNCTION_ARGS  )

Definition at line 318 of file timestamp.c.

319{
321 Node *ret = NULL;
322
324 {
326
328 }
329
331}
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition datetime.c:4963

References fb(), IsA, MAX_TIMESTAMP_PRECISION, PG_GETARG_POINTER, PG_RETURN_POINTER, and TemporalSimplify().

◆ timestamp_timestamptz()

◆ timestamp_trunc()

Datum timestamp_trunc ( PG_FUNCTION_ARGS  )

Definition at line 4650 of file timestamp.c.

4651{
4654 Timestamp result;
4655 int type,
4656 val;
4657 char *lowunits;
4658 fsec_t fsec;
4659 struct pg_tm tt,
4660 *tm = &tt;
4661
4664 false);
4665
4666 type = DecodeUnits(0, lowunits, &val);
4667
4668 if (type == UNITS)
4669 {
4671 {
4672 /*
4673 * Errors thrown here for invalid units should exactly match those
4674 * below, else there will be unexpected discrepancies between
4675 * finite- and infinite-input cases.
4676 */
4677 switch (val)
4678 {
4679 case DTK_WEEK:
4680 case DTK_MILLENNIUM:
4681 case DTK_CENTURY:
4682 case DTK_DECADE:
4683 case DTK_YEAR:
4684 case DTK_QUARTER:
4685 case DTK_MONTH:
4686 case DTK_DAY:
4687 case DTK_HOUR:
4688 case DTK_MINUTE:
4689 case DTK_SECOND:
4690 case DTK_MILLISEC:
4691 case DTK_MICROSEC:
4693 break;
4694 default:
4695 ereport(ERROR,
4697 errmsg("unit \"%s\" not supported for type %s",
4699 result = 0;
4700 }
4701 }
4702
4703 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4704 ereport(ERROR,
4706 errmsg("timestamp out of range")));
4707
4708 switch (val)
4709 {
4710 case DTK_WEEK:
4711 {
4712 int woy;
4713
4715
4716 /*
4717 * If it is week 52/53 and the month is January, then the
4718 * week must belong to the previous year. Also, some
4719 * December dates belong to the next year.
4720 */
4721 if (woy >= 52 && tm->tm_mon == 1)
4722 --tm->tm_year;
4724 ++tm->tm_year;
4725 isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
4726 tm->tm_hour = 0;
4727 tm->tm_min = 0;
4728 tm->tm_sec = 0;
4729 fsec = 0;
4730 break;
4731 }
4732 case DTK_MILLENNIUM:
4733 /* see comments in timestamptz_trunc */
4734 if (tm->tm_year > 0)
4735 tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
4736 else
4737 tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
4739 case DTK_CENTURY:
4740 /* see comments in timestamptz_trunc */
4741 if (tm->tm_year > 0)
4742 tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
4743 else
4744 tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
4746 case DTK_DECADE:
4747 /* see comments in timestamptz_trunc */
4748 if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
4749 {
4750 if (tm->tm_year > 0)
4751 tm->tm_year = (tm->tm_year / 10) * 10;
4752 else
4753 tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
4754 }
4756 case DTK_YEAR:
4757 tm->tm_mon = 1;
4759 case DTK_QUARTER:
4760 tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
4762 case DTK_MONTH:
4763 tm->tm_mday = 1;
4765 case DTK_DAY:
4766 tm->tm_hour = 0;
4768 case DTK_HOUR:
4769 tm->tm_min = 0;
4771 case DTK_MINUTE:
4772 tm->tm_sec = 0;
4774 case DTK_SECOND:
4775 fsec = 0;
4776 break;
4777
4778 case DTK_MILLISEC:
4779 fsec = (fsec / 1000) * 1000;
4780 break;
4781
4782 case DTK_MICROSEC:
4783 break;
4784
4785 default:
4786 ereport(ERROR,
4788 errmsg("unit \"%s\" not supported for type %s",
4790 result = 0;
4791 }
4792
4793 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
4794 ereport(ERROR,
4796 errmsg("timestamp out of range")));
4797 }
4798 else
4799 {
4800 ereport(ERROR,
4802 errmsg("unit \"%s\" not recognized for type %s",
4804 result = 0;
4805 }
4806
4807 PG_RETURN_TIMESTAMP(result);
4808}
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition timestamp.c:5255

References date2isoweek(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_WEEK, DTK_YEAR, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), isoweek2date(), MONTHS_PER_YEAR, pg_fallthrough, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ timestamp_zone()

Datum timestamp_zone ( PG_FUNCTION_ARGS  )

Definition at line 6299 of file timestamp.c.

6300{
6303 TimestampTz result;
6304 int tz;
6305 char tzname[TZ_STRLEN_MAX + 1];
6306 int type,
6307 val;
6308 pg_tz *tzp;
6309 struct pg_tm tm;
6310 fsec_t fsec;
6311
6314
6315 /*
6316 * Look up the requested timezone.
6317 */
6319
6320 type = DecodeTimezoneName(tzname, &val, &tzp);
6321
6323 {
6324 /* fixed-offset abbreviation */
6325 tz = val;
6326 result = dt2local(timestamp, tz);
6327 }
6328 else if (type == TZNAME_DYNTZ)
6329 {
6330 /* dynamic-offset abbreviation, resolve using specified time */
6331 if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, tzp) != 0)
6332 ereport(ERROR,
6334 errmsg("timestamp out of range")));
6336 result = dt2local(timestamp, tz);
6337 }
6338 else
6339 {
6340 /* full zone name, rotate to that zone */
6341 if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, tzp) != 0)
6342 ereport(ERROR,
6344 errmsg("timestamp out of range")));
6345 tz = DetermineTimeZoneOffset(&tm, tzp);
6346 if (tm2timestamp(&tm, fsec, &tz, &result) != 0)
6347 ereport(ERROR,
6349 errmsg("timestamp out of range")));
6350 }
6351
6352 if (!IS_VALID_TIMESTAMP(result))
6353 ereport(ERROR,
6355 errmsg("timestamp out of range")));
6356
6357 PG_RETURN_TIMESTAMPTZ(result);
6358}

References DecodeTimezoneName(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), dt2local(), ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMPTZ, text_to_cstring_buffer(), timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), type, TZ_STRLEN_MAX, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, and val.

◆ TimestampDifference()

void TimestampDifference ( TimestampTz  start_time,
TimestampTz  stop_time,
long secs,
int microsecs 
)

◆ TimestampDifferenceExceeds()

◆ TimestampDifferenceExceedsSeconds()

bool TimestampDifferenceExceedsSeconds ( TimestampTz  start_time,
TimestampTz  stop_time,
int  threshold_sec 
)

Definition at line 1786 of file timestamp.c.

1789{
1790 long secs;
1791 int usecs;
1792
1793 /* Calculate the difference in seconds */
1795
1796 return (secs >= threshold_sec);
1797}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1712

References fb(), start_time, and TimestampDifference().

Referenced by DetermineSlotInvalidationCause().

◆ TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)

Definition at line 1748 of file timestamp.c.

1749{
1751
1752 /* Deal with zero or negative elapsed time quickly. */
1753 if (start_time >= stop_time)
1754 return 0;
1755 /* To not fail with timestamp infinities, we must detect overflow. */
1757 return (long) INT_MAX;
1758 if (diff >= (INT_MAX * INT64CONST(1000) - 999))
1759 return (long) INT_MAX;
1760 else
1761 return (long) ((diff + 999) / 1000);
1762}

References fb(), INT64CONST, pg_sub_s64_overflow(), and start_time.

Referenced by ApplyLauncherMain(), autoprewarm_main(), bbsink_copystream_archive_contents(), DetermineSleepTime(), do_analyze_rel(), GetReplicationApplyDelay(), GetReplicationTransferLatency(), libpqsrv_cancel(), LogCheckpointEnd(), pgfdw_get_cleanup_result(), pgstat_report_analyze(), pgstat_report_vacuum(), recoveryApplyDelay(), wait_for_local_flush(), WaitForLSN(), WaitForWalSummarization(), WaitForWALToBecomeAvailable(), WalReceiverMain(), and WalSndComputeSleeptime().

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 6408 of file timestamp.c.

6409{
6410 long offset;
6411
6412 if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
6413 return false;
6414 return true;
6415}
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition localtime.c:1966

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

◆ timestamptypmodin()

Datum timestamptypmodin ( PG_FUNCTION_ARGS  )

Definition at line 295 of file timestamp.c.

296{
298
300}
static int32 anytimestamp_typmodin(bool istz, ArrayType *ta)
Definition timestamp.c:95

References anytimestamp_typmodin(), fb(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timestamptypmodout()

Datum timestamptypmodout ( PG_FUNCTION_ARGS  )

Definition at line 303 of file timestamp.c.

304{
305 int32 typmod = PG_GETARG_INT32(0);
306
308}
static char * anytimestamp_typmodout(bool istz, int32 typmod)
Definition timestamp.c:138

References anytimestamp_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ timestamptz2timestamp()

static Timestamp timestamptz2timestamp ( TimestampTz  timestamp)
static

Definition at line 6495 of file timestamp.c.

6496{
6498}
Timestamp timestamptz2timestamp_safe(TimestampTz timestamp, Node *escontext)
Definition timestamp.c:6511

References fb(), and timestamptz2timestamp_safe().

Referenced by GetSQLLocalTimestamp(), and timestamptz_timestamp().

◆ timestamptz2timestamp_safe()

Timestamp timestamptz2timestamp_safe ( TimestampTz  timestamp,
Node escontext 
)

Definition at line 6511 of file timestamp.c.

6512{
6513 Timestamp result;
6514 struct pg_tm tt,
6515 *tm = &tt;
6516 fsec_t fsec;
6517 int tz;
6518
6520 result = timestamp;
6521 else
6522 {
6523 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
6524 {
6525 if (timestamp < 0)
6526 TIMESTAMP_NOBEGIN(result);
6527 else
6528 TIMESTAMP_NOEND(result);
6529
6530 ereturn(escontext, result,
6532 errmsg("timestamp out of range")));
6533 }
6534 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
6535 {
6536 if (timestamp < 0)
6537 TIMESTAMP_NOBEGIN(result);
6538 else
6539 TIMESTAMP_NOEND(result);
6540
6541 ereturn(escontext, result,
6543 errmsg("timestamp out of range")));
6544 }
6545 }
6546 return result;
6547}

References ereturn, errcode(), errmsg, fb(), timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, and tm2timestamp().

Referenced by cvt_timestamptz_timestamp(), and timestamptz2timestamp().

◆ timestamptz_age()

Datum timestamptz_age ( PG_FUNCTION_ARGS  )

Definition at line 4425 of file timestamp.c.

4426{
4429 Interval *result;
4430 fsec_t fsec1,
4431 fsec2;
4432 struct pg_itm tt,
4433 *tm = &tt;
4434 struct pg_tm tt1,
4435 *tm1 = &tt1;
4436 struct pg_tm tt2,
4437 *tm2 = &tt2;
4438 int tz1;
4439 int tz2;
4440
4441 result = palloc_object(Interval);
4442
4443 /*
4444 * Handle infinities.
4445 *
4446 * We treat anything that amounts to "infinity - infinity" as an error,
4447 * since the interval type has nothing equivalent to NaN.
4448 */
4450 {
4452 ereport(ERROR,
4454 errmsg("interval out of range")));
4455 else
4456 INTERVAL_NOBEGIN(result);
4457 }
4458 else if (TIMESTAMP_IS_NOEND(dt1))
4459 {
4461 ereport(ERROR,
4463 errmsg("interval out of range")));
4464 else
4465 INTERVAL_NOEND(result);
4466 }
4467 else if (TIMESTAMP_IS_NOBEGIN(dt2))
4468 INTERVAL_NOEND(result);
4469 else if (TIMESTAMP_IS_NOEND(dt2))
4470 INTERVAL_NOBEGIN(result);
4471 else if (timestamp2tm(dt1, &tz1, tm1, &fsec1, NULL, NULL) == 0 &&
4472 timestamp2tm(dt2, &tz2, tm2, &fsec2, NULL, NULL) == 0)
4473 {
4474 /* form the symbolic difference */
4475 tm->tm_usec = fsec1 - fsec2;
4476 tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
4477 tm->tm_min = tm1->tm_min - tm2->tm_min;
4478 tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
4479 tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
4480 tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
4481 tm->tm_year = tm1->tm_year - tm2->tm_year;
4482
4483 /* flip sign if necessary... */
4484 if (dt1 < dt2)
4485 {
4486 tm->tm_usec = -tm->tm_usec;
4487 tm->tm_sec = -tm->tm_sec;
4488 tm->tm_min = -tm->tm_min;
4489 tm->tm_hour = -tm->tm_hour;
4490 tm->tm_mday = -tm->tm_mday;
4491 tm->tm_mon = -tm->tm_mon;
4492 tm->tm_year = -tm->tm_year;
4493 }
4494
4495 /* propagate any negative fields into the next higher field */
4496 while (tm->tm_usec < 0)
4497 {
4498 tm->tm_usec += USECS_PER_SEC;
4499 tm->tm_sec--;
4500 }
4501
4502 while (tm->tm_sec < 0)
4503 {
4505 tm->tm_min--;
4506 }
4507
4508 while (tm->tm_min < 0)
4509 {
4511 tm->tm_hour--;
4512 }
4513
4514 while (tm->tm_hour < 0)
4515 {
4517 tm->tm_mday--;
4518 }
4519
4520 while (tm->tm_mday < 0)
4521 {
4522 if (dt1 < dt2)
4523 {
4524 tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
4525 tm->tm_mon--;
4526 }
4527 else
4528 {
4529 tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
4530 tm->tm_mon--;
4531 }
4532 }
4533
4534 while (tm->tm_mon < 0)
4535 {
4537 tm->tm_year--;
4538 }
4539
4540 /*
4541 * Note: we deliberately ignore any difference between tz1 and tz2.
4542 */
4543
4544 /* recover sign if necessary... */
4545 if (dt1 < dt2)
4546 {
4547 tm->tm_usec = -tm->tm_usec;
4548 tm->tm_sec = -tm->tm_sec;
4549 tm->tm_min = -tm->tm_min;
4550 tm->tm_hour = -tm->tm_hour;
4551 tm->tm_mday = -tm->tm_mday;
4552 tm->tm_mon = -tm->tm_mon;
4553 tm->tm_year = -tm->tm_year;
4554 }
4555
4556 if (itm2interval(tm, result) != 0)
4557 ereport(ERROR,
4559 errmsg("interval out of range")));
4560 }
4561 else
4562 ereport(ERROR,
4564 errmsg("timestamp out of range")));
4565
4566 PG_RETURN_INTERVAL_P(result);
4567}

References day_tab, ereport, errcode(), errmsg, ERROR, fb(), HOURS_PER_DAY, INTERVAL_NOBEGIN, INTERVAL_NOEND, isleap, itm2interval(), MINS_PER_HOUR, MONTHS_PER_YEAR, palloc_object, PG_GETARG_TIMESTAMPTZ, PG_RETURN_INTERVAL_P, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, and USECS_PER_SEC.

◆ timestamptz_at_local()

Datum timestamptz_at_local ( PG_FUNCTION_ARGS  )

Definition at line 6935 of file timestamp.c.

6936{
6937 return timestamptz_timestamp(fcinfo);
6938}
Datum timestamptz_timestamp(PG_FUNCTION_ARGS)
Definition timestamp.c:6484

References timestamptz_timestamp().

◆ timestamptz_bin()

Datum timestamptz_bin ( PG_FUNCTION_ARGS  )

Definition at line 4814 of file timestamp.c.

4815{
4819 TimestampTz result,
4821 tm_diff,
4822 tm_modulo,
4823 tm_delta;
4824
4827
4828 if (TIMESTAMP_NOT_FINITE(origin))
4829 ereport(ERROR,
4831 errmsg("origin out of range")));
4832
4834 ereport(ERROR,
4836 errmsg("timestamps cannot be binned into infinite intervals")));
4837
4838 if (stride->month != 0)
4839 ereport(ERROR,
4841 errmsg("timestamps cannot be binned into intervals containing months or years")));
4842
4845 ereport(ERROR,
4847 errmsg("interval out of range")));
4848
4849 if (stride_usecs <= 0)
4850 ereport(ERROR,
4852 errmsg("stride must be greater than zero")));
4853
4855 ereport(ERROR,
4857 errmsg("interval out of range")));
4858
4859 /* These calculations cannot overflow */
4862 result = origin + tm_delta;
4863
4864 /*
4865 * We want to round towards -infinity, not 0, when tm_diff is negative and
4866 * not a multiple of stride_usecs. This adjustment *can* cause overflow,
4867 * since the result might now be out of the range origin .. timestamp.
4868 */
4869 if (tm_modulo < 0)
4870 {
4871 if (unlikely(pg_sub_s64_overflow(result, stride_usecs, &result)) ||
4872 !IS_VALID_TIMESTAMP(result))
4873 ereport(ERROR,
4875 errmsg("timestamp out of range")));
4876 }
4877
4878 PG_RETURN_TIMESTAMPTZ(result);
4879}

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, pg_mul_s64_overflow(), PG_RETURN_TIMESTAMPTZ, pg_sub_s64_overflow(), TIMESTAMP_NOT_FINITE, unlikely, and USECS_PER_DAY.

◆ timestamptz_cmp_timestamp()

◆ timestamptz_eq_timestamp()

◆ timestamptz_ge_timestamp()

◆ timestamptz_gt_timestamp()

◆ timestamptz_hash()

Datum timestamptz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2338 of file timestamp.c.

2339{
2340 return hashint8(fcinfo);
2341}

References hashint8().

◆ timestamptz_hash_extended()

Datum timestamptz_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2344 of file timestamp.c.

2345{
2346 return hashint8extended(fcinfo);
2347}

References hashint8extended().

◆ timestamptz_in()

Datum timestamptz_in ( PG_FUNCTION_ARGS  )

Definition at line 409 of file timestamp.c.

410{
411 char *str = PG_GETARG_CSTRING(0);
412#ifdef NOT_USED
413 Oid typelem = PG_GETARG_OID(1);
414#endif
415 int32 typmod = PG_GETARG_INT32(2);
416 Node *escontext = fcinfo->context;
417 TimestampTz result;
418 fsec_t fsec;
419 struct pg_tm tt,
420 *tm = &tt;
421 int tz;
422 int dtype;
423 int nf;
424 int dterr;
425 char *field[MAXDATEFIELDS];
426 int ftype[MAXDATEFIELDS];
428 DateTimeErrorExtra extra;
429
431 field, ftype, MAXDATEFIELDS, &nf);
432 if (dterr == 0)
433 dterr = DecodeDateTime(field, ftype, nf,
434 &dtype, tm, &fsec, &tz, &extra);
435 if (dterr != 0)
436 {
437 DateTimeParseError(dterr, &extra, str, "timestamp with time zone",
438 escontext);
440 }
441
442 switch (dtype)
443 {
444 case DTK_DATE:
445 if (tm2timestamp(tm, fsec, &tz, &result) != 0)
446 ereturn(escontext, (Datum) 0,
448 errmsg("timestamp out of range: \"%s\"", str)));
449 break;
450
451 case DTK_EPOCH:
452 result = SetEpochTimestamp();
453 break;
454
455 case DTK_LATE:
456 TIMESTAMP_NOEND(result);
457 break;
458
459 case DTK_EARLY:
460 TIMESTAMP_NOBEGIN(result);
461 break;
462
463 default:
464 elog(ERROR, "unexpected dtype %d while parsing timestamptz \"%s\"",
465 dtype, str);
466 TIMESTAMP_NOEND(result);
467 }
468
469 AdjustTimestampForTypmod(&result, typmod, escontext);
470
471 PG_RETURN_TIMESTAMPTZ(result);
472}

References AdjustTimestampForTypmod(), DateTimeParseError(), DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, elog, ereturn, errcode(), errmsg, ERROR, fb(), MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, SetEpochTimestamp(), str, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, and tm2timestamp().

Referenced by AlterRole(), CreateRole(), moddatetime(), and validateRecoveryParameters().

◆ timestamptz_izone()

Datum timestamptz_izone ( PG_FUNCTION_ARGS  )

Definition at line 6618 of file timestamp.c.

6619{
6622 Timestamp result;
6623 int tz;
6624
6627
6629 ereport(ERROR,
6631 errmsg("interval time zone \"%s\" must be finite",
6633 PointerGetDatum(zone))))));
6634
6635 if (zone->month != 0 || zone->day != 0)
6636 ereport(ERROR,
6638 errmsg("interval time zone \"%s\" must not include months or days",
6640 PointerGetDatum(zone))))));
6641
6642 tz = -(zone->time / USECS_PER_SEC);
6643
6644 result = dt2local(timestamp, tz);
6645
6646 if (!IS_VALID_TIMESTAMP(result))
6647 ereport(ERROR,
6649 errmsg("timestamp out of range")));
6650
6651 PG_RETURN_TIMESTAMP(result);
6652}

References DatumGetCString(), DirectFunctionCall1, dt2local(), ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_out(), IS_VALID_TIMESTAMP, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMP, PointerGetDatum(), TIMESTAMP_NOT_FINITE, and USECS_PER_SEC.

◆ timestamptz_le_timestamp()

◆ timestamptz_lt_timestamp()

◆ timestamptz_mi_interval()

◆ timestamptz_mi_interval_at_zone()

◆ timestamptz_mi_interval_internal()

static TimestampTz timestamptz_mi_interval_internal ( TimestampTz  timestamp,
Interval span,
pg_tz attimezone 
)
static

◆ timestamptz_ne_timestamp()

◆ timestamptz_out()

Datum timestamptz_out ( PG_FUNCTION_ARGS  )

Definition at line 767 of file timestamp.c.

768{
770 char *result;
771 int tz;
772 struct pg_tm tt,
773 *tm = &tt;
774 fsec_t fsec;
775 const char *tzn;
776 char buf[MAXDATELEN + 1];
777
780 else if (timestamp2tm(dt, &tz, tm, &fsec, &tzn, NULL) == 0)
781 EncodeDateTime(tm, fsec, true, tz, tzn, DateStyle, buf);
782 else
785 errmsg("timestamp out of range")));
786
787 result = pstrdup(buf);
788 PG_RETURN_CSTRING(result);
789}

References buf, DateStyle, EncodeDateTime(), EncodeSpecialTimestamp(), ereport, errcode(), errmsg, ERROR, fb(), MAXDATELEN, PG_GETARG_TIMESTAMPTZ, PG_RETURN_CSTRING, pstrdup(), timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

Referenced by ExecGetJsonValueItemString().

◆ timestamptz_part()

Datum timestamptz_part ( PG_FUNCTION_ARGS  )

Definition at line 6002 of file timestamp.c.

6003{
6004 return timestamptz_part_common(fcinfo, false);
6005}

References timestamptz_part_common().

◆ timestamptz_part_common()

static Datum timestamptz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5745 of file timestamp.c.

5746{
5751 int tz;
5752 int type,
5753 val;
5754 char *lowunits;
5755 fsec_t fsec;
5756 struct pg_tm tt,
5757 *tm = &tt;
5758
5761 false);
5762
5763 type = DecodeUnits(0, lowunits, &val);
5764 if (type == UNKNOWN_FIELD)
5766
5768 {
5771 true);
5772
5773 if (r != 0.0)
5774 {
5775 if (retnumeric)
5776 {
5777 if (r < 0)
5779 CStringGetDatum("-Infinity"),
5781 Int32GetDatum(-1));
5782 else if (r > 0)
5784 CStringGetDatum("Infinity"),
5786 Int32GetDatum(-1));
5787 }
5788 else
5790 }
5791 else
5793 }
5794
5795 if (type == UNITS)
5796 {
5797 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
5798 ereport(ERROR,
5800 errmsg("timestamp out of range")));
5801
5802 switch (val)
5803 {
5804 case DTK_TZ:
5805 intresult = -tz;
5806 break;
5807
5808 case DTK_TZ_MINUTE:
5810 break;
5811
5812 case DTK_TZ_HOUR:
5813 intresult = -tz / SECS_PER_HOUR;
5814 break;
5815
5816 case DTK_MICROSEC:
5817 intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
5818 break;
5819
5820 case DTK_MILLISEC:
5821 if (retnumeric)
5822 /*---
5823 * tm->tm_sec * 1000 + fsec / 1000
5824 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5825 */
5827 else
5828 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
5829 break;
5830
5831 case DTK_SECOND:
5832 if (retnumeric)
5833 /*---
5834 * tm->tm_sec + fsec / 1'000'000
5835 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5836 */
5838 else
5839 PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
5840 break;
5841
5842 case DTK_MINUTE:
5843 intresult = tm->tm_min;
5844 break;
5845
5846 case DTK_HOUR:
5847 intresult = tm->tm_hour;
5848 break;
5849
5850 case DTK_DAY:
5851 intresult = tm->tm_mday;
5852 break;
5853
5854 case DTK_MONTH:
5855 intresult = tm->tm_mon;
5856 break;
5857
5858 case DTK_QUARTER:
5859 intresult = (tm->tm_mon - 1) / 3 + 1;
5860 break;
5861
5862 case DTK_WEEK:
5864 break;
5865
5866 case DTK_YEAR:
5867 if (tm->tm_year > 0)
5868 intresult = tm->tm_year;
5869 else
5870 /* there is no year 0, just 1 BC and 1 AD */
5871 intresult = tm->tm_year - 1;
5872 break;
5873
5874 case DTK_DECADE:
5875 /* see comments in timestamp_part */
5876 if (tm->tm_year > 0)
5877 intresult = tm->tm_year / 10;
5878 else
5879 intresult = -((8 - (tm->tm_year - 1)) / 10);
5880 break;
5881
5882 case DTK_CENTURY:
5883 /* see comments in timestamp_part */
5884 if (tm->tm_year > 0)
5885 intresult = (tm->tm_year + 99) / 100;
5886 else
5887 intresult = -((99 - (tm->tm_year - 1)) / 100);
5888 break;
5889
5890 case DTK_MILLENNIUM:
5891 /* see comments in timestamp_part */
5892 if (tm->tm_year > 0)
5893 intresult = (tm->tm_year + 999) / 1000;
5894 else
5895 intresult = -((999 - (tm->tm_year - 1)) / 1000);
5896 break;
5897
5898 case DTK_JULIAN:
5899 if (retnumeric)
5903 NULL),
5904 NULL));
5905 else
5908 tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY);
5909 break;
5910
5911 case DTK_ISOYEAR:
5913 /* Adjust BC years */
5914 if (intresult <= 0)
5915 intresult -= 1;
5916 break;
5917
5918 case DTK_DOW:
5919 case DTK_ISODOW:
5921 if (val == DTK_ISODOW && intresult == 0)
5922 intresult = 7;
5923 break;
5924
5925 case DTK_DOY:
5927 - date2j(tm->tm_year, 1, 1) + 1);
5928 break;
5929
5930 default:
5931 ereport(ERROR,
5933 errmsg("unit \"%s\" not supported for type %s",
5935 intresult = 0;
5936 }
5937 }
5938 else if (type == RESERV)
5939 {
5940 switch (val)
5941 {
5942 case DTK_EPOCH:
5944 /* (timestamp - epoch) / 1000000 */
5945 if (retnumeric)
5946 {
5947 Numeric result;
5948
5949 if (timestamp < (PG_INT64_MAX + epoch))
5951 else
5952 {
5955 NULL),
5956 int64_to_numeric(1000000),
5957 NULL);
5959 NumericGetDatum(result),
5960 Int32GetDatum(6)));
5961 }
5962 PG_RETURN_NUMERIC(result);
5963 }
5964 else
5965 {
5966 float8 result;
5967
5968 /* try to avoid precision loss in subtraction */
5969 if (timestamp < (PG_INT64_MAX + epoch))
5970 result = (timestamp - epoch) / 1000000.0;
5971 else
5972 result = ((float8) timestamp - epoch) / 1000000.0;
5973 PG_RETURN_FLOAT8(result);
5974 }
5975 break;
5976
5977 default:
5978 ereport(ERROR,
5980 errmsg("unit \"%s\" not supported for type %s",
5982 intresult = 0;
5983 }
5984 }
5985 else
5986 {
5987 ereport(ERROR,
5989 errmsg("unit \"%s\" not recognized for type %s",
5991
5992 intresult = 0;
5993 }
5994
5995 if (retnumeric)
5997 else
5999}
#define SECS_PER_HOUR
Definition timestamp.h:127

References CStringGetDatum(), date2isoweek(), date2isoyear(), date2j(), DatumGetNumeric(), DecodeSpecial(), DecodeUnits(), DirectFunctionCall2, DirectFunctionCall3, downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_DOW, DTK_DOY, DTK_EPOCH, DTK_HOUR, DTK_ISODOW, DTK_ISOYEAR, DTK_JULIAN, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_TZ, DTK_TZ_HOUR, DTK_TZ_MINUTE, DTK_WEEK, DTK_YEAR, epoch, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), Int32GetDatum(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, InvalidOid, j2day(), MINS_PER_HOUR, NonFiniteTimestampTzPart(), numeric_add_safe(), numeric_div_safe(), numeric_in(), numeric_round(), numeric_sub_safe(), NumericGetDatum(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_INT64_MAX, PG_RETURN_FLOAT8, PG_RETURN_NULL, PG_RETURN_NUMERIC, RESERV, SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, SetEpochTimestamp(), timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_NOT_FINITE, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by extract_timestamptz(), and timestamptz_part().

◆ timestamptz_pl_interval()

◆ timestamptz_pl_interval_at_zone()

◆ timestamptz_pl_interval_internal()

static TimestampTz timestamptz_pl_interval_internal ( TimestampTz  timestamp,
Interval span,
pg_tz attimezone 
)
static

Definition at line 3224 of file timestamp.c.

3227{
3228 TimestampTz result;
3229 int tz;
3230
3231 /*
3232 * Handle infinities.
3233 *
3234 * We treat anything that amounts to "infinity - infinity" as an error,
3235 * since the timestamptz type has nothing equivalent to NaN.
3236 */
3238 {
3240 ereport(ERROR,
3242 errmsg("timestamp out of range")));
3243 else
3244 TIMESTAMP_NOBEGIN(result);
3245 }
3246 else if (INTERVAL_IS_NOEND(span))
3247 {
3249 ereport(ERROR,
3251 errmsg("timestamp out of range")));
3252 else
3253 TIMESTAMP_NOEND(result);
3254 }
3256 result = timestamp;
3257 else
3258 {
3259 /* Use session timezone if caller asks for default */
3260 if (attimezone == NULL)
3261 attimezone = session_timezone;
3262
3263 if (span->month != 0)
3264 {
3265 struct pg_tm tt,
3266 *tm = &tt;
3267 fsec_t fsec;
3268
3269 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, attimezone) != 0)
3270 ereport(ERROR,
3272 errmsg("timestamp out of range")));
3273
3274 if (pg_add_s32_overflow(tm->tm_mon, span->month, &tm->tm_mon))
3275 ereport(ERROR,
3277 errmsg("timestamp out of range")));
3278 if (tm->tm_mon > MONTHS_PER_YEAR)
3279 {
3280 tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
3281 tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
3282 }
3283 else if (tm->tm_mon < 1)
3284 {
3285 tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
3287 }
3288
3289 /* adjust for end of month boundary problems... */
3290 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
3291 tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
3292
3293 tz = DetermineTimeZoneOffset(tm, attimezone);
3294
3295 if (tm2timestamp(tm, fsec, &tz, &timestamp) != 0)
3296 ereport(ERROR,
3298 errmsg("timestamp out of range")));
3299 }
3300
3301 if (span->day != 0)
3302 {
3303 struct pg_tm tt,
3304 *tm = &tt;
3305 fsec_t fsec;
3306 int julian;
3307
3308 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, attimezone) != 0)
3309 ereport(ERROR,
3311 errmsg("timestamp out of range")));
3312
3313 /*
3314 * Add days by converting to and from Julian. We need an overflow
3315 * check here since j2date expects a non-negative integer input.
3316 * In practice though, it will give correct answers for small
3317 * negative Julian dates; we should allow -1 to avoid
3318 * timezone-dependent failures, as discussed in timestamp.h.
3319 */
3321 if (pg_add_s32_overflow(julian, span->day, &julian) ||
3322 julian < -1)
3323 ereport(ERROR,
3325 errmsg("timestamp out of range")));
3327
3328 tz = DetermineTimeZoneOffset(tm, attimezone);
3329
3330 if (tm2timestamp(tm, fsec, &tz, &timestamp) != 0)
3331 ereport(ERROR,
3333 errmsg("timestamp out of range")));
3334 }
3335
3337 ereport(ERROR,
3339 errmsg("timestamp out of range")));
3340
3342 ereport(ERROR,
3344 errmsg("timestamp out of range")));
3345
3346 result = timestamp;
3347 }
3348
3349 return result;
3350}

References date2j(), day_tab, DetermineTimeZoneOffset(), ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, IS_VALID_TIMESTAMP, isleap, j2date(), MONTHS_PER_YEAR, pg_add_s32_overflow(), pg_add_s64_overflow(), session_timezone, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by generate_series_timestamptz_internal(), in_range_timestamptz_interval(), timestamptz_mi_interval_internal(), timestamptz_pl_interval(), and timestamptz_pl_interval_at_zone().

◆ timestamptz_recv()

Datum timestamptz_recv ( PG_FUNCTION_ARGS  )

Definition at line 795 of file timestamp.c.

796{
798
799#ifdef NOT_USED
800 Oid typelem = PG_GETARG_OID(1);
801#endif
802 int32 typmod = PG_GETARG_INT32(2);
804 int tz;
805 struct pg_tm tt,
806 *tm = &tt;
807 fsec_t fsec;
808
810
811 /* range check: see if timestamptz_out would like it */
813 /* ok */ ;
814 else if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0 ||
818 errmsg("timestamp out of range")));
819
821
823}

References AdjustTimestampForTypmod(), buf, ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_TIMESTAMPTZ, pq_getmsgint64(), timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

◆ timestamptz_scale()

Datum timestamptz_scale ( PG_FUNCTION_ARGS  )

Definition at line 861 of file timestamp.c.

862{
864 int32 typmod = PG_GETARG_INT32(1);
865 TimestampTz result;
866
867 result = timestamp;
868
869 AdjustTimestampForTypmod(&result, typmod, NULL);
870
871 PG_RETURN_TIMESTAMPTZ(result);
872}

References AdjustTimestampForTypmod(), fb(), PG_GETARG_INT32, PG_GETARG_TIMESTAMPTZ, and PG_RETURN_TIMESTAMPTZ.

◆ timestamptz_send()

◆ timestamptz_timestamp()

◆ timestamptz_to_str()

const char * timestamptz_to_str ( TimestampTz  t)

◆ timestamptz_to_time_t()

pg_time_t timestamptz_to_time_t ( TimestampTz  t)

Definition at line 1833 of file timestamp.c.

1834{
1835 pg_time_t result;
1836
1837 result = (pg_time_t) (t / USECS_PER_SEC +
1839
1840 return result;
1841}

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by DetermineTimeZoneAbbrevOffsetTS(), InitProcessGlobals(), and pg_timezone_abbrevs_zone().

◆ timestamptz_trunc()

Datum timestamptz_trunc ( PG_FUNCTION_ARGS  )

Definition at line 5066 of file timestamp.c.

5067{
5070 TimestampTz result;
5071
5073
5074 PG_RETURN_TIMESTAMPTZ(result);
5075}
static TimestampTz timestamptz_trunc_internal(text *units, TimestampTz timestamp, pg_tz *tzp)
Definition timestamp.c:4888

References fb(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMPTZ, session_timezone, and timestamptz_trunc_internal().

◆ timestamptz_trunc_internal()

static TimestampTz timestamptz_trunc_internal ( text units,
TimestampTz  timestamp,
pg_tz tzp 
)
static

Definition at line 4888 of file timestamp.c.

4889{
4890 TimestampTz result;
4891 int tz;
4892 int type,
4893 val;
4894 bool redotz = false;
4895 char *lowunits;
4896 fsec_t fsec;
4897 struct pg_tm tt,
4898 *tm = &tt;
4899
4902 false);
4903
4904 type = DecodeUnits(0, lowunits, &val);
4905
4906 if (type == UNITS)
4907 {
4909 {
4910 /*
4911 * Errors thrown here for invalid units should exactly match those
4912 * below, else there will be unexpected discrepancies between
4913 * finite- and infinite-input cases.
4914 */
4915 switch (val)
4916 {
4917 case DTK_WEEK:
4918 case DTK_MILLENNIUM:
4919 case DTK_CENTURY:
4920 case DTK_DECADE:
4921 case DTK_YEAR:
4922 case DTK_QUARTER:
4923 case DTK_MONTH:
4924 case DTK_DAY:
4925 case DTK_HOUR:
4926 case DTK_MINUTE:
4927 case DTK_SECOND:
4928 case DTK_MILLISEC:
4929 case DTK_MICROSEC:
4930 return timestamp;
4931 break;
4932
4933 default:
4934 ereport(ERROR,
4936 errmsg("unit \"%s\" not supported for type %s",
4938 result = 0;
4939 }
4940 }
4941
4942 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, tzp) != 0)
4943 ereport(ERROR,
4945 errmsg("timestamp out of range")));
4946
4947 switch (val)
4948 {
4949 case DTK_WEEK:
4950 {
4951 int woy;
4952
4954
4955 /*
4956 * If it is week 52/53 and the month is January, then the
4957 * week must belong to the previous year. Also, some
4958 * December dates belong to the next year.
4959 */
4960 if (woy >= 52 && tm->tm_mon == 1)
4961 --tm->tm_year;
4963 ++tm->tm_year;
4964 isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
4965 tm->tm_hour = 0;
4966 tm->tm_min = 0;
4967 tm->tm_sec = 0;
4968 fsec = 0;
4969 redotz = true;
4970 break;
4971 }
4972 /* one may consider DTK_THOUSAND and DTK_HUNDRED... */
4973 case DTK_MILLENNIUM:
4974
4975 /*
4976 * truncating to the millennium? what is this supposed to
4977 * mean? let us put the first year of the millennium... i.e.
4978 * -1000, 1, 1001, 2001...
4979 */
4980 if (tm->tm_year > 0)
4981 tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
4982 else
4983 tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
4985 case DTK_CENTURY:
4986 /* truncating to the century? as above: -100, 1, 101... */
4987 if (tm->tm_year > 0)
4988 tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
4989 else
4990 tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
4992 case DTK_DECADE:
4993
4994 /*
4995 * truncating to the decade? first year of the decade. must
4996 * not be applied if year was truncated before!
4997 */
4998 if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
4999 {
5000 if (tm->tm_year > 0)
5001 tm->tm_year = (tm->tm_year / 10) * 10;
5002 else
5003 tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
5004 }
5006 case DTK_YEAR:
5007 tm->tm_mon = 1;
5009 case DTK_QUARTER:
5010 tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
5012 case DTK_MONTH:
5013 tm->tm_mday = 1;
5015 case DTK_DAY:
5016 tm->tm_hour = 0;
5017 redotz = true; /* for all cases >= DAY */
5019 case DTK_HOUR:
5020 tm->tm_min = 0;
5022 case DTK_MINUTE:
5023 tm->tm_sec = 0;
5025 case DTK_SECOND:
5026 fsec = 0;
5027 break;
5028 case DTK_MILLISEC:
5029 fsec = (fsec / 1000) * 1000;
5030 break;
5031 case DTK_MICROSEC:
5032 break;
5033
5034 default:
5035 ereport(ERROR,
5037 errmsg("unit \"%s\" not supported for type %s",
5039 result = 0;
5040 }
5041
5042 if (redotz)
5043 tz = DetermineTimeZoneOffset(tm, tzp);
5044
5045 if (tm2timestamp(tm, fsec, &tz, &result) != 0)
5046 ereport(ERROR,
5048 errmsg("timestamp out of range")));
5049 }
5050 else
5051 {
5052 ereport(ERROR,
5054 errmsg("unit \"%s\" not recognized for type %s",
5056 result = 0;
5057 }
5058
5059 return result;
5060}

References date2isoweek(), DecodeUnits(), DetermineTimeZoneOffset(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_WEEK, DTK_YEAR, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), isoweek2date(), MONTHS_PER_YEAR, pg_fallthrough, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by timestamptz_trunc(), and timestamptz_trunc_zone().

◆ timestamptz_trunc_zone()

Datum timestamptz_trunc_zone ( PG_FUNCTION_ARGS  )

Definition at line 5081 of file timestamp.c.

5082{
5086 TimestampTz result;
5087 pg_tz *tzp;
5088
5089 /*
5090 * Look up the requested timezone.
5091 */
5092 tzp = lookup_timezone(zone);
5093
5095
5096 PG_RETURN_TIMESTAMPTZ(result);
5097}

References fb(), lookup_timezone(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMPTZ, and timestamptz_trunc_internal().

◆ timestamptz_zone()

Datum timestamptz_zone ( PG_FUNCTION_ARGS  )

Definition at line 6554 of file timestamp.c.

6555{
6558 Timestamp result;
6559 int tz;
6560 char tzname[TZ_STRLEN_MAX + 1];
6561 int type,
6562 val;
6563 pg_tz *tzp;
6564
6567
6568 /*
6569 * Look up the requested timezone.
6570 */
6572
6573 type = DecodeTimezoneName(tzname, &val, &tzp);
6574
6576 {
6577 /* fixed-offset abbreviation */
6578 tz = -val;
6579 result = dt2local(timestamp, tz);
6580 }
6581 else if (type == TZNAME_DYNTZ)
6582 {
6583 /* dynamic-offset abbreviation, resolve using specified time */
6584 int isdst;
6585
6587 result = dt2local(timestamp, tz);
6588 }
6589 else
6590 {
6591 /* full zone name, rotate from that zone */
6592 struct pg_tm tm;
6593 fsec_t fsec;
6594
6595 if (timestamp2tm(timestamp, &tz, &tm, &fsec, NULL, tzp) != 0)
6596 ereport(ERROR,
6598 errmsg("timestamp out of range")));
6599 if (tm2timestamp(&tm, fsec, NULL, &result) != 0)
6600 ereport(ERROR,
6602 errmsg("timestamp out of range")));
6603 }
6604
6605 if (!IS_VALID_TIMESTAMP(result))
6606 ereport(ERROR,
6608 errmsg("timestamp out of range")));
6609
6610 PG_RETURN_TIMESTAMP(result);
6611}
int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr, pg_tz *tzp, int *isdst)
Definition datetime.c:1804

References DecodeTimezoneName(), DetermineTimeZoneAbbrevOffsetTS(), dt2local(), ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMP, text_to_cstring_buffer(), timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), type, TZ_STRLEN_MAX, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, and val.

◆ timestamptztypmodin()

Datum timestamptztypmodin ( PG_FUNCTION_ARGS  )

Definition at line 840 of file timestamp.c.

References anytimestamp_typmodin(), fb(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timestamptztypmodout()

Datum timestamptztypmodout ( PG_FUNCTION_ARGS  )

Definition at line 848 of file timestamp.c.

849{
850 int32 typmod = PG_GETARG_INT32(0);
851
853}

References anytimestamp_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ tm2timestamp()

int tm2timestamp ( struct pg_tm tm,
fsec_t  fsec,
int tzp,
Timestamp result 
)

Definition at line 1997 of file timestamp.c.

1998{
2000 TimeOffset time;
2001
2002 /* Prevent overflow in Julian-day routines */
2004 {
2005 *result = 0; /* keep compiler quiet */
2006 return -1;
2007 }
2008
2010 time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
2011
2013 pg_add_s64_overflow(*result, time, result)))
2014 {
2015 *result = 0; /* keep compiler quiet */
2016 return -1;
2017 }
2018 if (tzp != NULL)
2019 *result = dt2local(*result, -(*tzp));
2020
2021 /* final range check catches just-out-of-range timestamps */
2022 if (!IS_VALID_TIMESTAMP(*result))
2023 {
2024 *result = 0; /* keep compiler quiet */
2025 return -1;
2026 }
2027
2028 return 0;
2029}
static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition timestamp.c:17

References date2j(), dt2local(), fb(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), pg_mul_s64_overflow(), POSTGRES_EPOCH_JDATE, time2t(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, unlikely, and USECS_PER_DAY.

Referenced by check_recovery_target_time(), parse_datetime(), PGTYPEStimestamp_defmt_scan(), and to_timestamp().

Variable Documentation

◆ PgReloadTime

TimestampTz PgReloadTime

Definition at line 48 of file timestamp.c.

Referenced by pg_conf_load_time(), and ProcessConfigFileInternal().

◆ PgStartTime

TimestampTz PgStartTime

Definition at line 45 of file timestamp.c.

Referenced by pg_postmaster_start_time(), PostgresSingleUserMain(), and PostmasterMain().