PostgreSQL Source Code  git master
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/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.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/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
 

Macros

#define SAMESIGN(a, b)   (((a) < 0) == ((b) < 0))
 
#define TIMESTAMP_GT(t1, t2)    DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
 
#define TIMESTAMP_LT(t1, t2)    DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
 

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 int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
static char * anytimestamp_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)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const char * timestamptz_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)
 
static int timestamp_fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum timestamp_sortsupport (PG_FUNCTION_ARGS)
 
Datum timestamp_hash (PG_FUNCTION_ARGS)
 
Datum timestamp_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)
 
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)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
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)
 
Datum interval_accum (PG_FUNCTION_ARGS)
 
Datum interval_combine (PG_FUNCTION_ARGS)
 
Datum interval_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (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 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_opt_overflow (Timestamp timestamp, int *overflow)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
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)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ SAMESIGN

#define SAMESIGN (   a,
  b 
)    (((a) < 0) == ((b) < 0))

Definition at line 50 of file timestamp.c.

◆ 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))

Function Documentation

◆ AdjustIntervalForTypmod()

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

Definition at line 1330 of file timestamp.c.

1332 {
1333  static const int64 IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
1334  INT64CONST(1000000),
1335  INT64CONST(100000),
1336  INT64CONST(10000),
1337  INT64CONST(1000),
1338  INT64CONST(100),
1339  INT64CONST(10),
1340  INT64CONST(1)
1341  };
1342 
1343  static const int64 IntervalOffsets[MAX_INTERVAL_PRECISION + 1] = {
1344  INT64CONST(500000),
1345  INT64CONST(50000),
1346  INT64CONST(5000),
1347  INT64CONST(500),
1348  INT64CONST(50),
1349  INT64CONST(5),
1350  INT64CONST(0)
1351  };
1352 
1353  /*
1354  * Unspecified range and precision? Then not necessary to adjust. Setting
1355  * typmod to -1 is the convention for all data types.
1356  */
1357  if (typmod >= 0)
1358  {
1359  int range = INTERVAL_RANGE(typmod);
1360  int precision = INTERVAL_PRECISION(typmod);
1361 
1362  /*
1363  * Our interpretation of intervals with a limited set of fields is
1364  * that fields to the right of the last one specified are zeroed out,
1365  * but those to the left of it remain valid. Thus for example there
1366  * is no operational difference between INTERVAL YEAR TO MONTH and
1367  * INTERVAL MONTH. In some cases we could meaningfully enforce that
1368  * higher-order fields are zero; for example INTERVAL DAY could reject
1369  * nonzero "month" field. However that seems a bit pointless when we
1370  * can't do it consistently. (We cannot enforce a range limit on the
1371  * highest expected field, since we do not have any equivalent of
1372  * SQL's <interval leading field precision>.) If we ever decide to
1373  * revisit this, interval_support will likely require adjusting.
1374  *
1375  * Note: before PG 8.4 we interpreted a limited set of fields as
1376  * actually causing a "modulo" operation on a given value, potentially
1377  * losing high-order as well as low-order information. But there is
1378  * no support for such behavior in the standard, and it seems fairly
1379  * undesirable on data consistency grounds anyway. Now we only
1380  * perform truncation or rounding of low-order fields.
1381  */
1382  if (range == INTERVAL_FULL_RANGE)
1383  {
1384  /* Do nothing... */
1385  }
1386  else if (range == INTERVAL_MASK(YEAR))
1387  {
1389  interval->day = 0;
1390  interval->time = 0;
1391  }
1392  else if (range == INTERVAL_MASK(MONTH))
1393  {
1394  interval->day = 0;
1395  interval->time = 0;
1396  }
1397  /* YEAR TO MONTH */
1398  else if (range == (INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH)))
1399  {
1400  interval->day = 0;
1401  interval->time = 0;
1402  }
1403  else if (range == INTERVAL_MASK(DAY))
1404  {
1405  interval->time = 0;
1406  }
1407  else if (range == INTERVAL_MASK(HOUR))
1408  {
1411  }
1412  else if (range == INTERVAL_MASK(MINUTE))
1413  {
1416  }
1417  else if (range == INTERVAL_MASK(SECOND))
1418  {
1419  /* fractional-second rounding will be dealt with below */
1420  }
1421  /* DAY TO HOUR */
1422  else if (range == (INTERVAL_MASK(DAY) |
1423  INTERVAL_MASK(HOUR)))
1424  {
1427  }
1428  /* DAY TO MINUTE */
1429  else if (range == (INTERVAL_MASK(DAY) |
1430  INTERVAL_MASK(HOUR) |
1432  {
1435  }
1436  /* DAY TO SECOND */
1437  else if (range == (INTERVAL_MASK(DAY) |
1438  INTERVAL_MASK(HOUR) |
1441  {
1442  /* fractional-second rounding will be dealt with below */
1443  }
1444  /* HOUR TO MINUTE */
1445  else if (range == (INTERVAL_MASK(HOUR) |
1447  {
1450  }
1451  /* HOUR TO SECOND */
1452  else if (range == (INTERVAL_MASK(HOUR) |
1455  {
1456  /* fractional-second rounding will be dealt with below */
1457  }
1458  /* MINUTE TO SECOND */
1459  else if (range == (INTERVAL_MASK(MINUTE) |
1461  {
1462  /* fractional-second rounding will be dealt with below */
1463  }
1464  else
1465  elog(ERROR, "unrecognized interval typmod: %d", typmod);
1466 
1467  /* Need to adjust sub-second precision? */
1468  if (precision != INTERVAL_FULL_PRECISION)
1469  {
1470  if (precision < 0 || precision > MAX_INTERVAL_PRECISION)
1471  ereturn(escontext, false,
1472  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1473  errmsg("interval(%d) precision must be between %d and %d",
1474  precision, 0, MAX_INTERVAL_PRECISION)));
1475 
1476  if (interval->time >= INT64CONST(0))
1477  {
1478  interval->time = ((interval->time +
1479  IntervalOffsets[precision]) /
1480  IntervalScales[precision]) *
1481  IntervalScales[precision];
1482  }
1483  else
1484  {
1485  interval->time = -(((-interval->time +
1486  IntervalOffsets[precision]) /
1487  IntervalScales[precision]) *
1488  IntervalScales[precision]);
1489  }
1490  }
1491  }
1492 
1493  return true;
1494 }
#define USECS_PER_HOUR
Definition: timestamp.h:131
#define MONTHS_PER_YEAR
Definition: timestamp.h:108
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:93
#define USECS_PER_MINUTE
Definition: timestamp.h:132
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
#define ERROR
Definition: elog.h:39
#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 struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
#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, HOUR, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_PRECISION, INTERVAL_RANGE, MAX_INTERVAL_PRECISION, MINUTE, MONTH, interval::month, MONTHS_PER_YEAR, 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 350 of file timestamp.c.

351 {
352  static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
353  INT64CONST(1000000),
354  INT64CONST(100000),
355  INT64CONST(10000),
356  INT64CONST(1000),
357  INT64CONST(100),
358  INT64CONST(10),
359  INT64CONST(1)
360  };
361 
362  static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
363  INT64CONST(500000),
364  INT64CONST(50000),
365  INT64CONST(5000),
366  INT64CONST(500),
367  INT64CONST(50),
368  INT64CONST(5),
369  INT64CONST(0)
370  };
371 
372  if (!TIMESTAMP_NOT_FINITE(*time)
373  && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
374  {
375  if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
376  ereturn(escontext, false,
377  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
378  errmsg("timestamp(%d) precision must be between %d and %d",
379  typmod, 0, MAX_TIMESTAMP_PRECISION)));
380 
381  if (*time >= INT64CONST(0))
382  {
383  *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
384  TimestampScales[typmod];
385  }
386  else
387  {
388  *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
389  * TimestampScales[typmod]);
390  }
391  }
392 
393  return true;
394 }
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:92
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:168

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

Referenced by 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 107 of file timestamp.c.

108 {
109  if (typmod < 0)
110  ereport(ERROR,
111  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
112  errmsg("TIMESTAMP(%d)%s precision must not be negative",
113  typmod, (istz ? " WITH TIME ZONE" : ""))));
114  if (typmod > MAX_TIMESTAMP_PRECISION)
115  {
117  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
118  errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
119  typmod, (istz ? " WITH TIME ZONE" : ""),
121  typmod = MAX_TIMESTAMP_PRECISION;
122  }
123 
124  return typmod;
125 }
#define WARNING
Definition: elog.h:36
#define ereport(elevel,...)
Definition: elog.h:149

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 86 of file timestamp.c.

87 {
88  int32 *tl;
89  int n;
90 
91  tl = ArrayGetIntegerTypmods(ta, &n);
92 
93  /*
94  * we're not too tense about good error message here because grammar
95  * shouldn't allow wrong number of modifiers for TIMESTAMP
96  */
97  if (n != 1)
98  ereport(ERROR,
99  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
100  errmsg("invalid type modifier")));
101 
102  return anytimestamp_typmod_check(istz, tl[0]);
103 }
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:254
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:107
signed int int32
Definition: c.h:483

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

◆ anytimestamp_typmodout()

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

Definition at line 129 of file timestamp.c.

130 {
131  const char *tz = istz ? " with time zone" : " without time zone";
132 
133  if (typmod >= 0)
134  return psprintf("(%d)%s", (int) typmod, tz);
135  else
136  return pstrdup(tz);
137 }
char * pstrdup(const char *in)
Definition: mcxt.c:1644
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References psprintf(), and pstrdup().

Referenced by timestamptypmodout(), and timestamptztypmodout().

◆ clock_timestamp()

Datum clock_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1559 of file timestamp.c.

1560 {
1562 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
#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 4510 of file timestamp.c.

4511 {
4512  float8 result;
4513  int day0,
4514  day4,
4515  dayn;
4516 
4517  /* current day */
4518  dayn = date2j(year, mon, mday);
4519 
4520  /* fourth day of current year */
4521  day4 = date2j(year, 1, 4);
4522 
4523  /* day0 == offset to first day of week (Monday) */
4524  day0 = j2day(day4 - 1);
4525 
4526  /*
4527  * We need the first week containing a Thursday, otherwise this day falls
4528  * into the previous year for purposes of counting weeks
4529  */
4530  if (dayn < day4 - day0)
4531  {
4532  day4 = date2j(year - 1, 1, 4);
4533 
4534  /* day0 == offset to first day of week (Monday) */
4535  day0 = j2day(day4 - 1);
4536  }
4537 
4538  result = (dayn - (day4 - day0)) / 7 + 1;
4539 
4540  /*
4541  * Sometimes the last few days in a year will fall into the first week of
4542  * the next year, so check for this.
4543  */
4544  if (result >= 52)
4545  {
4546  day4 = date2j(year + 1, 1, 4);
4547 
4548  /* day0 == offset to first day of week (Monday) */
4549  day0 = j2day(day4 - 1);
4550 
4551  if (dayn >= day4 - day0)
4552  result = (dayn - (day4 - day0)) / 7 + 1;
4553  }
4554 
4555  return (int) result;
4556 }
int j2day(int date)
Definition: datetime.c:346
int date2j(int year, int month, int day)
Definition: datetime.c:288
double float8
Definition: c.h:619

References date2j(), 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 4565 of file timestamp.c.

4566 {
4567  float8 result;
4568  int day0,
4569  day4,
4570  dayn;
4571 
4572  /* current day */
4573  dayn = date2j(year, mon, mday);
4574 
4575  /* fourth day of current year */
4576  day4 = date2j(year, 1, 4);
4577 
4578  /* day0 == offset to first day of week (Monday) */
4579  day0 = j2day(day4 - 1);
4580 
4581  /*
4582  * We need the first week containing a Thursday, otherwise this day falls
4583  * into the previous year for purposes of counting weeks
4584  */
4585  if (dayn < day4 - day0)
4586  {
4587  day4 = date2j(year - 1, 1, 4);
4588 
4589  /* day0 == offset to first day of week (Monday) */
4590  day0 = j2day(day4 - 1);
4591 
4592  year--;
4593  }
4594 
4595  result = (dayn - (day4 - day0)) / 7 + 1;
4596 
4597  /*
4598  * Sometimes the last few days in a year will fall into the first week of
4599  * the next year, so check for this.
4600  */
4601  if (result >= 52)
4602  {
4603  day4 = date2j(year + 1, 1, 4);
4604 
4605  /* day0 == offset to first day of week (Monday) */
4606  day0 = j2day(day4 - 1);
4607 
4608  if (dayn >= day4 - day0)
4609  year++;
4610  }
4611 
4612  return year;
4613 }

References date2j(), 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 4622 of file timestamp.c.

4623 {
4624  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4625 }
int isoweek2j(int year, int week)
Definition: timestamp.c:4459
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4565

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

Referenced by DCH_to_char().

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 2051 of file timestamp.c.

2052 {
2053  dt -= (timezone * USECS_PER_SEC);
2054  return dt;
2055 }
#define USECS_PER_SEC
Definition: timestamp.h:133

References USECS_PER_SEC.

Referenced by make_timestamptz_at_timezone(), timestamp2timestamptz_opt_overflow(), timestamp_izone(), timestamp_zone(), timestamptz_izone(), timestamptz_zone(), and tm2timestamp().

◆ dt2time()

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

Definition at line 1803 of file timestamp.c.

1804 {
1805  TimeOffset time;
1806 
1807  time = jd;
1808 
1809  *hour = time / USECS_PER_HOUR;
1810  time -= (*hour) * USECS_PER_HOUR;
1811  *min = time / USECS_PER_MINUTE;
1812  time -= (*min) * USECS_PER_MINUTE;
1813  *sec = time / USECS_PER_SEC;
1814  *fsec = time - (*sec * USECS_PER_SEC);
1815 } /* dt2time() */
int64 TimeOffset
Definition: timestamp.h:40

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ EncodeSpecialTimestamp()

void EncodeSpecialTimestamp ( Timestamp  dt,
char *  str 
)

Definition at line 1536 of file timestamp.c.

1537 {
1538  if (TIMESTAMP_IS_NOBEGIN(dt))
1539  strcpy(str, EARLY);
1540  else if (TIMESTAMP_IS_NOEND(dt))
1541  strcpy(str, LATE);
1542  else /* shouldn't happen */
1543  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1544 }
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:166
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:161
#define EARLY
Definition: datetime.h:39
#define LATE
Definition: datetime.h:40

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

Referenced by JsonEncodeDateTime(), PGTYPEStimestamp_to_asc(), timestamp_out(), timestamptz_out(), and timestamptz_to_str().

◆ extract_interval()

Datum extract_interval ( PG_FUNCTION_ARGS  )

Definition at line 5413 of file timestamp.c.

5414 {
5415  return interval_part_common(fcinfo, true);
5416 }
static Datum interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:5242

References interval_part_common().

◆ extract_timestamp()

Datum extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 4960 of file timestamp.c.

4961 {
4962  return timestamp_part_common(fcinfo, true);
4963 }
static Datum timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4696

References timestamp_part_common().

◆ extract_timestamptz()

Datum extract_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5232 of file timestamp.c.

5233 {
5234  return timestamptz_part_common(fcinfo, true);
5235 }
static Datum timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4969

References timestamptz_part_common().

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 719 of file timestamp.c.

720 {
721  float8 seconds = PG_GETARG_FLOAT8(0);
722  TimestampTz result;
723 
724  /* Deal with NaN and infinite inputs ... */
725  if (isnan(seconds))
726  ereport(ERROR,
727  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
728  errmsg("timestamp cannot be NaN")));
729 
730  if (isinf(seconds))
731  {
732  if (seconds < 0)
733  TIMESTAMP_NOBEGIN(result);
734  else
735  TIMESTAMP_NOEND(result);
736  }
737  else
738  {
739  /* Out of range? */
740  if (seconds <
742  || seconds >=
744  ereport(ERROR,
745  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
746  errmsg("timestamp out of range: \"%g\"", seconds)));
747 
748  /* Convert UNIX epoch to Postgres epoch */
750 
751  seconds = rint(seconds * USECS_PER_SEC);
752  result = (int64) seconds;
753 
754  /* Recheck in case roundoff produces something just out of range */
755  if (!IS_VALID_TIMESTAMP(result))
756  ereport(ERROR,
757  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
758  errmsg("timestamp out of range: \"%g\"",
759  PG_GETARG_FLOAT8(0))));
760  }
761 
762  PG_RETURN_TIMESTAMP(result);
763 }
#define DATETIME_MIN_JULIAN
Definition: timestamp.h:225
int64 TimestampTz
Definition: timestamp.h:39
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:158
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:227
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:241
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:208
#define SECS_PER_DAY
Definition: timestamp.h:125
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:209
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:163
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:67

References DATETIME_MIN_JULIAN, ereport, errcode(), errmsg(), ERROR, 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 5756 of file timestamp.c.

5757 {
5758  FuncCallContext *funcctx;
5760  Timestamp result;
5761 
5762  /* stuff done only on the first call of the function */
5763  if (SRF_IS_FIRSTCALL())
5764  {
5765  Timestamp start = PG_GETARG_TIMESTAMP(0);
5766  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5767  Interval *step = PG_GETARG_INTERVAL_P(2);
5768  MemoryContext oldcontext;
5769  const Interval interval_zero = {0};
5770 
5771  /* create a function context for cross-call persistence */
5772  funcctx = SRF_FIRSTCALL_INIT();
5773 
5774  /*
5775  * switch to memory context appropriate for multiple function calls
5776  */
5777  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5778 
5779  /* allocate memory for user context */
5782 
5783  /*
5784  * Use fctx to keep state from call to call. Seed current with the
5785  * original start value
5786  */
5787  fctx->current = start;
5788  fctx->finish = finish;
5789  fctx->step = *step;
5790 
5791  /* Determine sign of the interval */
5792  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5793 
5794  if (fctx->step_sign == 0)
5795  ereport(ERROR,
5796  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5797  errmsg("step size cannot equal zero")));
5798 
5799  funcctx->user_fctx = fctx;
5800  MemoryContextSwitchTo(oldcontext);
5801  }
5802 
5803  /* stuff done on every call of the function */
5804  funcctx = SRF_PERCALL_SETUP();
5805 
5806  /*
5807  * get the saved state and use current as the result for this iteration
5808  */
5809  fctx = funcctx->user_fctx;
5810  result = fctx->current;
5811 
5812  if (fctx->step_sign > 0 ?
5813  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5814  timestamp_cmp_internal(result, fctx->finish) >= 0)
5815  {
5816  /* increment current in preparation for next iteration */
5818  TimestampGetDatum(fctx->current),
5819  PointerGetDatum(&fctx->step)));
5820 
5821  /* do when there is more left to send */
5822  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5823  }
5824  else
5825  {
5826  /* do when there is no more left */
5827  SRF_RETURN_DONE(funcctx);
5828  }
5829 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2125
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2914
static int interval_cmp_internal(const Interval *interval1, const Interval *interval2)
Definition: timestamp.c:2420
int64 Timestamp
Definition: timestamp.h:38
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#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
void * palloc(Size size)
Definition: mcxt.c:1226
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
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 generate_series_timestamp_fctx::current, DatumGetTimestamp(), DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, generate_series_timestamp_fctx::finish, interval_cmp_internal(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_timestamp_fctx::step, generate_series_timestamp_fctx::step_sign, timestamp_cmp_internal(), timestamp_pl_interval(), TimestampGetDatum(), and FuncCallContext::user_fctx.

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5914 of file timestamp.c.

5915 {
5916  return generate_series_timestamptz_internal(fcinfo);
5917 }
static Datum generate_series_timestamptz_internal(FunctionCallInfo fcinfo)
Definition: timestamp.c:5836

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_at_zone()

Datum generate_series_timestamptz_at_zone ( PG_FUNCTION_ARGS  )

Definition at line 5920 of file timestamp.c.

5921 {
5922  return generate_series_timestamptz_internal(fcinfo);
5923 }

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_internal()

static Datum generate_series_timestamptz_internal ( FunctionCallInfo  fcinfo)
static

Definition at line 5836 of file timestamp.c.

5837 {
5838  FuncCallContext *funcctx;
5840  TimestampTz result;
5841 
5842  /* stuff done only on the first call of the function */
5843  if (SRF_IS_FIRSTCALL())
5844  {
5846  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5847  Interval *step = PG_GETARG_INTERVAL_P(2);
5848  text *zone = (PG_NARGS() == 4) ? PG_GETARG_TEXT_PP(3) : NULL;
5849  MemoryContext oldcontext;
5850  const Interval interval_zero = {0};
5851 
5852  /* create a function context for cross-call persistence */
5853  funcctx = SRF_FIRSTCALL_INIT();
5854 
5855  /*
5856  * switch to memory context appropriate for multiple function calls
5857  */
5858  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5859 
5860  /* allocate memory for user context */
5863 
5864  /*
5865  * Use fctx to keep state from call to call. Seed current with the
5866  * original start value
5867  */
5868  fctx->current = start;
5869  fctx->finish = finish;
5870  fctx->step = *step;
5872 
5873  /* Determine sign of the interval */
5874  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5875 
5876  if (fctx->step_sign == 0)
5877  ereport(ERROR,
5878  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5879  errmsg("step size cannot equal zero")));
5880 
5881  funcctx->user_fctx = fctx;
5882  MemoryContextSwitchTo(oldcontext);
5883  }
5884 
5885  /* stuff done on every call of the function */
5886  funcctx = SRF_PERCALL_SETUP();
5887 
5888  /*
5889  * get the saved state and use current as the result for this iteration
5890  */
5891  fctx = funcctx->user_fctx;
5892  result = fctx->current;
5893 
5894  if (fctx->step_sign > 0 ?
5895  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5896  timestamp_cmp_internal(result, fctx->finish) >= 0)
5897  {
5898  /* increment current in preparation for next iteration */
5900  &fctx->step,
5901  fctx->attimezone);
5902 
5903  /* do when there is more left to send */
5904  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5905  }
5906  else
5907  {
5908  /* do when there is no more left */
5909  SRF_RETURN_DONE(funcctx);
5910  }
5911 }
static pg_tz * lookup_timezone(text *zone)
Definition: timestamp.c:542
static TimestampTz timestamptz_pl_interval_internal(TimestampTz timestamp, Interval *span, pg_tz *attimezone)
Definition: timestamp.c:3021
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_NARGS()
Definition: fmgr.h:203
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
Definition: c.h:676
Definition: zic.c:94
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:64

References generate_series_timestamptz_fctx::attimezone, generate_series_timestamptz_fctx::current, ereport, errcode(), errmsg(), ERROR, generate_series_timestamptz_fctx::finish, interval_cmp_internal(), lookup_timezone(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), 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, generate_series_timestamptz_fctx::step, generate_series_timestamptz_fctx::step_sign, timestamp_cmp_internal(), timestamptz_pl_interval_internal(), TimestampTzGetDatum(), and FuncCallContext::user_fctx.

Referenced by generate_series_timestamptz(), and generate_series_timestamptz_at_zone().

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1583 of file timestamp.c.

1584 {
1585  TimestampTz result;
1586  struct timeval tp;
1587 
1588  gettimeofday(&tp, NULL);
1589 
1590  result = (TimestampTz) tp.tv_sec -
1592  result = (result * USECS_PER_SEC) + tp.tv_usec;
1593 
1594  return result;
1595 }
int gettimeofday(struct timeval *tp, void *tzp)

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

Referenced by ApplyLauncherMain(), asyncQueueFillWarning(), autoprewarm_main(), AutoVacLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), check_log_duration(), CheckPointGuts(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), enable_startup_progress_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), initialize_drandom_seed(), InitProcessGlobals(), KnownAssignedXidsCompress(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), pa_send_data(), PerformWalRecovery(), 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_stat(), pgstat_report_vacuum(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_counters(), pgstat_reset_of_kind(), pgstat_reset_slru(), PostgresSingleUserMain(), PostmasterMain(), PrepareTransaction(), process_syncing_tables_for_apply(), ProcessConfigFileInternal(), ProcessPendingWrites(), ProcessRepliesIfAny(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), SetupApplyOrSyncWorker(), StartTransaction(), test_pattern(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), 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 2083 of file timestamp.c.

2084 {
2085  struct pg_tm *t0;
2086  pg_time_t epoch = 0;
2087 
2088  t0 = pg_gmtime(&epoch);
2089 
2090  if (t0 == NULL)
2091  elog(ERROR, "could not convert epoch to timestamp: %m");
2092 
2093  tm->tm_year = t0->tm_year;
2094  tm->tm_mon = t0->tm_mon;
2095  tm->tm_mday = t0->tm_mday;
2096  tm->tm_hour = t0->tm_hour;
2097  tm->tm_min = t0->tm_min;
2098  tm->tm_sec = t0->tm_sec;
2099 
2100  tm->tm_year += 1900;
2101  tm->tm_mon++;
2102 }
static struct pg_tm tm
Definition: localtime.c:104
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1389
int64 pg_time_t
Definition: pgtime.h:23
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, 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(), and SetEpochTimestamp().

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1601 of file timestamp.c.

1602 {
1603  TimestampTz ts;
1604 
1606  if (typmod >= 0)
1607  AdjustTimestampForTypmod(&ts, typmod, NULL);
1608  return ts;
1609 }
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition: timestamp.c:350
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:855

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1615 of file timestamp.c.

1616 {
1617  Timestamp ts;
1618 
1620  if (typmod >= 0)
1621  AdjustTimestampForTypmod(&ts, typmod, NULL);
1622  return ts;
1623 }
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5630

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

Referenced by ExecEvalSQLValueFunction().

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

Definition at line 3501 of file timestamp.c.

3502 {
3504  Interval *base = PG_GETARG_INTERVAL_P(1);
3505  Interval *offset = PG_GETARG_INTERVAL_P(2);
3506  bool sub = PG_GETARG_BOOL(3);
3507  bool less = PG_GETARG_BOOL(4);
3508  Interval *sum;
3509 
3510  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3511  ereport(ERROR,
3512  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3513  errmsg("invalid preceding or following size in window function")));
3514 
3515  /* We don't currently bother to avoid overflow hazards here */
3516  if (sub)
3518  IntervalPGetDatum(base),
3519  IntervalPGetDatum(offset)));
3520  else
3522  IntervalPGetDatum(base),
3523  IntervalPGetDatum(offset)));
3524 
3525  if (less)
3527  else
3529 }
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2398
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3264
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3230
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
long val
Definition: informix.c:664
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
static Datum IntervalPGetDatum(const Interval *X)
Definition: timestamp.h:58
static Interval * DatumGetIntervalP(Datum X)
Definition: timestamp.h:40

References DatumGetIntervalP(), DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, int128_compare(), int64_to_int128(), interval_cmp_internal(), interval_cmp_value(), interval_mi(), interval_pl(), 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 3470 of file timestamp.c.

3471 {
3473  Timestamp base = PG_GETARG_TIMESTAMP(1);
3474  Interval *offset = PG_GETARG_INTERVAL_P(2);
3475  bool sub = PG_GETARG_BOOL(3);
3476  bool less = PG_GETARG_BOOL(4);
3477  Timestamp sum;
3478 
3479  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3480  ereport(ERROR,
3481  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3482  errmsg("invalid preceding or following size in window function")));
3483 
3484  /* We don't currently bother to avoid overflow hazards here */
3485  if (sub)
3487  TimestampGetDatum(base),
3488  IntervalPGetDatum(offset)));
3489  else
3491  TimestampGetDatum(base),
3492  IntervalPGetDatum(offset)));
3493 
3494  if (less)
3495  PG_RETURN_BOOL(val <= sum);
3496  else
3497  PG_RETURN_BOOL(val >= sum);
3498 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2993

References DatumGetTimestamp(), DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, int128_compare(), int64_to_int128(), interval_cmp_value(), IntervalPGetDatum(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, 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 3443 of file timestamp.c.

3444 {
3447  Interval *offset = PG_GETARG_INTERVAL_P(2);
3448  bool sub = PG_GETARG_BOOL(3);
3449  bool less = PG_GETARG_BOOL(4);
3450  TimestampTz sum;
3451 
3452  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3453  ereport(ERROR,
3454  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3455  errmsg("invalid preceding or following size in window function")));
3456 
3457  /* We don't currently bother to avoid overflow hazards here */
3458  if (sub)
3459  sum = timestamptz_mi_interval_internal(base, offset, NULL);
3460  else
3461  sum = timestamptz_pl_interval_internal(base, offset, NULL);
3462 
3463  if (less)
3464  PG_RETURN_BOOL(val <= sum);
3465  else
3466  PG_RETURN_BOOL(val >= sum);
3467 }
static TimestampTz timestamptz_mi_interval_internal(TimestampTz timestamp, Interval *span, pg_tz *attimezone)
Definition: timestamp.c:3112

References ereport, errcode(), errmsg(), ERROR, int128_compare(), int64_to_int128(), interval_cmp_value(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, timestamptz_mi_interval_internal(), timestamptz_pl_interval_internal(), and val.

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 1976 of file timestamp.c.

1977 {
1978  TimeOffset time;
1979  TimeOffset tfrac;
1980 
1981  itm->tm_year = span.month / MONTHS_PER_YEAR;
1982  itm->tm_mon = span.month % MONTHS_PER_YEAR;
1983  itm->tm_mday = span.day;
1984  time = span.time;
1985 
1986  tfrac = time / USECS_PER_HOUR;
1987  time -= tfrac * USECS_PER_HOUR;
1988  itm->tm_hour = tfrac;
1989  tfrac = time / USECS_PER_MINUTE;
1990  time -= tfrac * USECS_PER_MINUTE;
1991  itm->tm_min = (int) tfrac;
1992  tfrac = time / USECS_PER_SEC;
1993  time -= tfrac * USECS_PER_SEC;
1994  itm->tm_sec = (int) tfrac;
1995  itm->tm_usec = (int) time;
1996 }
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
int64 tm_hour
Definition: timestamp.h:70
int tm_year
Definition: timestamp.h:73
int tm_mon
Definition: timestamp.h:72
int tm_mday
Definition: timestamp.h:71
int tm_sec
Definition: timestamp.h:68
int tm_min
Definition: timestamp.h:69
int tm_usec
Definition: timestamp.h:67

References Interval::day, Interval::month, MONTHS_PER_YEAR, Interval::time, pg_itm::tm_hour, pg_itm::tm_mday, pg_itm::tm_min, pg_itm::tm_mon, pg_itm::tm_sec, pg_itm::tm_usec, pg_itm::tm_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_accum()

Datum interval_accum ( PG_FUNCTION_ARGS  )

Definition at line 3543 of file timestamp.c.

3544 {
3545  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3547  Datum *transdatums;
3548  int ndatums;
3549  Interval sumX,
3550  N;
3551  Interval *newsum;
3552  ArrayType *result;
3553 
3554  deconstruct_array(transarray,
3555  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3556  &transdatums, NULL, &ndatums);
3557  if (ndatums != 2)
3558  elog(ERROR, "expected 2-element interval array");
3559 
3560  sumX = *(DatumGetIntervalP(transdatums[0]));
3561  N = *(DatumGetIntervalP(transdatums[1]));
3562 
3564  IntervalPGetDatum(&sumX),
3566  N.time += 1;
3567 
3568  transdatums[0] = IntervalPGetDatum(newsum);
3569  transdatums[1] = IntervalPGetDatum(&N);
3570 
3571  result = construct_array(transdatums, 2,
3572  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3573 
3574  PG_RETURN_ARRAYTYPE_P(result);
3575 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3320
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3578
#define newval
uintptr_t Datum
Definition: postgres.h:64

References construct_array(), DatumGetIntervalP(), deconstruct_array(), DirectFunctionCall2, elog(), ERROR, interval_pl(), IntervalPGetDatum(), newval, PG_GETARG_ARRAYTYPE_P, PG_GETARG_INTERVAL_P, PG_RETURN_ARRAYTYPE_P, and Interval::time.

◆ interval_accum_inv()

Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3627 of file timestamp.c.

3628 {
3629  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3631  Datum *transdatums;
3632  int ndatums;
3633  Interval sumX,
3634  N;
3635  Interval *newsum;
3636  ArrayType *result;
3637 
3638  deconstruct_array(transarray,
3639  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3640  &transdatums, NULL, &ndatums);
3641  if (ndatums != 2)
3642  elog(ERROR, "expected 2-element interval array");
3643 
3644  sumX = *(DatumGetIntervalP(transdatums[0]));
3645  N = *(DatumGetIntervalP(transdatums[1]));
3646 
3648  IntervalPGetDatum(&sumX),
3650  N.time -= 1;
3651 
3652  transdatums[0] = IntervalPGetDatum(newsum);
3653  transdatums[1] = IntervalPGetDatum(&N);
3654 
3655  result = construct_array(transdatums, 2,
3656  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3657 
3658  PG_RETURN_ARRAYTYPE_P(result);
3659 }

References construct_array(), DatumGetIntervalP(), deconstruct_array(), DirectFunctionCall2, elog(), ERROR, interval_mi(), IntervalPGetDatum(), newval, PG_GETARG_ARRAYTYPE_P, PG_GETARG_INTERVAL_P, PG_RETURN_ARRAYTYPE_P, and Interval::time.

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 3662 of file timestamp.c.

3663 {
3664  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3665  Datum *transdatums;
3666  int ndatums;
3667  Interval sumX,
3668  N;
3669 
3670  deconstruct_array(transarray,
3671  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3672  &transdatums, NULL, &ndatums);
3673  if (ndatums != 2)
3674  elog(ERROR, "expected 2-element interval array");
3675 
3676  sumX = *(DatumGetIntervalP(transdatums[0]));
3677  N = *(DatumGetIntervalP(transdatums[1]));
3678 
3679  /* SQL defines AVG of no values to be NULL */
3680  if (N.time == 0)
3681  PG_RETURN_NULL();
3682 
3684  IntervalPGetDatum(&sumX),
3685  Float8GetDatum((double) N.time));
3686 }
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3391
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1799
#define PG_RETURN_NULL()
Definition: fmgr.h:345

References DatumGetIntervalP(), deconstruct_array(), DirectFunctionCall2, elog(), ERROR, Float8GetDatum(), interval_div(), IntervalPGetDatum(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_NULL, and Interval::time.

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2483 of file timestamp.c.

2484 {
2485  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2486  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2487 
2488  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2489 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp().

◆ interval_cmp_internal()

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

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2398 of file timestamp.c.

2399 {
2400  INT128 span;
2401  int64 days;
2402 
2403  /*
2404  * Combine the month and day fields into an integral number of days.
2405  * Because the inputs are int32, int64 arithmetic suffices here.
2406  */
2407  days = interval->month * INT64CONST(30);
2408  days += interval->day;
2409 
2410  /* Widen time field to 128 bits */
2411  span = int64_to_int128(interval->time);
2412 
2413  /* Scale up days to microseconds, forming a 128-bit product */
2415 
2416  return span;
2417 }
const char *const days[]
Definition: datetime.c:86
#define USECS_PER_DAY
Definition: timestamp.h:130
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
Definition: int128.h:177

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

Referenced by in_range_interval_interval(), in_range_timestamp_interval(), in_range_timestamptz_interval(), interval_cmp_internal(), interval_hash(), and interval_hash_extended().

◆ interval_combine()

Datum interval_combine ( PG_FUNCTION_ARGS  )

Definition at line 3578 of file timestamp.c.

3579 {
3580  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3581  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3582  Datum *transdatums1;
3583  Datum *transdatums2;
3584  int ndatums1;
3585  int ndatums2;
3586  Interval sum1,
3587  N1;
3588  Interval sum2,
3589  N2;
3590 
3591  Interval *newsum;
3592  ArrayType *result;
3593 
3594  deconstruct_array(transarray1,
3595  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3596  &transdatums1, NULL, &ndatums1);
3597  if (ndatums1 != 2)
3598  elog(ERROR, "expected 2-element interval array");
3599 
3600  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3601  N1 = *(DatumGetIntervalP(transdatums1[1]));
3602 
3603  deconstruct_array(transarray2,
3604  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3605  &transdatums2, NULL, &ndatums2);
3606  if (ndatums2 != 2)
3607  elog(ERROR, "expected 2-element interval array");
3608 
3609  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3610  N2 = *(DatumGetIntervalP(transdatums2[1]));
3611 
3613  IntervalPGetDatum(&sum1),
3614  IntervalPGetDatum(&sum2)));
3615  N1.time += N2.time;
3616 
3617  transdatums1[0] = IntervalPGetDatum(newsum);
3618  transdatums1[1] = IntervalPGetDatum(&N1);
3619 
3620  result = construct_array(transdatums1, 2,
3621  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3622 
3623  PG_RETURN_ARRAYTYPE_P(result);
3624 }

References construct_array(), DatumGetIntervalP(), deconstruct_array(), DirectFunctionCall2, elog(), ERROR, interval_pl(), IntervalPGetDatum(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_ARRAYTYPE_P, and Interval::time.

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3391 of file timestamp.c.

3392 {
3393  Interval *span = PG_GETARG_INTERVAL_P(0);
3394  float8 factor = PG_GETARG_FLOAT8(1);
3395  double month_remainder_days,
3396  sec_remainder;
3397  int32 orig_month = span->month,
3398  orig_day = span->day;
3399  Interval *result;
3400 
3401  result = (Interval *) palloc(sizeof(Interval));
3402 
3403  if (factor == 0.0)
3404  ereport(ERROR,
3405  (errcode(ERRCODE_DIVISION_BY_ZERO),
3406  errmsg("division by zero")));
3407 
3408  result->month = (int32) (span->month / factor);
3409  result->day = (int32) (span->day / factor);
3410 
3411  /*
3412  * Fractional months full days into days. See comment in interval_mul().
3413  */
3414  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3415  month_remainder_days = TSROUND(month_remainder_days);
3416  sec_remainder = (orig_day / factor - result->day +
3417  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3418  sec_remainder = TSROUND(sec_remainder);
3419  if (fabs(sec_remainder) >= SECS_PER_DAY)
3420  {
3421  result->day += (int) (sec_remainder / SECS_PER_DAY);
3422  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3423  }
3424 
3425  /* cascade units down */
3426  result->day += (int32) month_remainder_days;
3427  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3428 
3429  PG_RETURN_INTERVAL_P(result);
3430 }
#define DAYS_PER_MONTH
Definition: timestamp.h:116
#define TSROUND(j)
Definition: timestamp.h:100
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:69

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), PG_GETARG_FLOAT8, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_avg().

◆ interval_eq()

Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2429 of file timestamp.c.

2430 {
2431  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2432  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2433 
2434  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2435 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 2072 of file timestamp.c.

2073 {
2074  PG_RETURN_BOOL(true);
2075 }

References PG_RETURN_BOOL.

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2474 of file timestamp.c.

2475 {
2476  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2477  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2478 
2479  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2480 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

◆ interval_gt()

Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2456 of file timestamp.c.

2457 {
2458  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2459  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2460 
2461  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2462 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2499 of file timestamp.c.

2500 {
2503  int64 span64;
2504 
2505  /*
2506  * Use only the least significant 64 bits for hashing. The upper 64 bits
2507  * seldom add any useful information, and besides we must do it like this
2508  * for compatibility with hashes calculated before use of INT128 was
2509  * introduced.
2510  */
2511  span64 = int128_to_int64(span);
2512 
2514 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:85
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define Int64GetDatumFast(X)
Definition: postgres.h:554

References DirectFunctionCall1, 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 2517 of file timestamp.c.

2518 {
2521  int64 span64;
2522 
2523  /* Same approach as interval_hash */
2524  span64 = int128_to_int64(span);
2525 
2527  PG_GETARG_DATUM(1));
2528 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:105

References DirectFunctionCall2, 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 884 of file timestamp.c.

885 {
886  char *str = PG_GETARG_CSTRING(0);
887 #ifdef NOT_USED
888  Oid typelem = PG_GETARG_OID(1);
889 #endif
890  int32 typmod = PG_GETARG_INT32(2);
891  Node *escontext = fcinfo->context;
892  Interval *result;
893  struct pg_itm_in tt,
894  *itm_in = &tt;
895  int dtype;
896  int nf;
897  int range;
898  int dterr;
899  char *field[MAXDATEFIELDS];
900  int ftype[MAXDATEFIELDS];
901  char workbuf[256];
902  DateTimeErrorExtra extra;
903 
904  itm_in->tm_year = 0;
905  itm_in->tm_mon = 0;
906  itm_in->tm_mday = 0;
907  itm_in->tm_usec = 0;
908 
909  if (typmod >= 0)
910  range = INTERVAL_RANGE(typmod);
911  else
913 
914  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
915  ftype, MAXDATEFIELDS, &nf);
916  if (dterr == 0)
917  dterr = DecodeInterval(field, ftype, nf, range,
918  &dtype, itm_in);
919 
920  /* if those functions think it's a bad format, try ISO8601 style */
921  if (dterr == DTERR_BAD_FORMAT)
922  dterr = DecodeISO8601Interval(str,
923  &dtype, itm_in);
924 
925  if (dterr != 0)
926  {
927  if (dterr == DTERR_FIELD_OVERFLOW)
928  dterr = DTERR_INTERVAL_OVERFLOW;
929  DateTimeParseError(dterr, &extra, str, "interval", escontext);
930  PG_RETURN_NULL();
931  }
932 
933  result = (Interval *) palloc(sizeof(Interval));
934 
935  switch (dtype)
936  {
937  case DTK_DELTA:
938  if (itmin2interval(itm_in, result) != 0)
939  ereturn(escontext, (Datum) 0,
940  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
941  errmsg("interval out of range")));
942  break;
943 
944  default:
945  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
946  dtype, str);
947  }
948 
949  AdjustIntervalForTypmod(result, typmod, escontext);
950 
951  PG_RETURN_INTERVAL_P(result);
952 }
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:756
void DateTimeParseError(int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, Node *escontext)
Definition: datetime.c:3981
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_itm_in *itm_in)
Definition: datetime.c:3276
int DecodeISO8601Interval(char *str, int *dtype, struct pg_itm_in *itm_in)
Definition: datetime.c:3718
int itmin2interval(struct pg_itm_in *itm_in, Interval *span)
Definition: timestamp.c:2032
static bool AdjustIntervalForTypmod(Interval *interval, int32 typmod, Node *escontext)
Definition: timestamp.c:1330
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#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 DTERR_BAD_FORMAT
Definition: datetime.h:282
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:283
unsigned int Oid
Definition: postgres_ext.h:31
Definition: nodes.h:129
int tm_mon
Definition: timestamp.h:86
int tm_year
Definition: timestamp.h:87
int tm_mday
Definition: timestamp.h:85
int64 tm_usec
Definition: timestamp.h:84

References AdjustIntervalForTypmod(), DateTimeParseError(), DecodeInterval(), DecodeISO8601Interval(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTK_DELTA, elog(), ereturn, errcode(), errmsg(), ERROR, INTERVAL_FULL_RANGE, INTERVAL_RANGE, itmin2interval(), MAXDATEFIELDS, palloc(), ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, range(), generate_unaccent_rules::str, pg_itm_in::tm_mday, pg_itm_in::tm_mon, pg_itm_in::tm_usec, and pg_itm_in::tm_year.

Referenced by check_timezone(), and flatten_set_variable_args().

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2871 of file timestamp.c.

2872 {
2873  Interval *span = PG_GETARG_INTERVAL_P(0);
2874  Interval *result;
2875  int32 wholemonth;
2876 
2877  result = (Interval *) palloc(sizeof(Interval));
2878  result->month = span->month;
2879  result->day = span->day;
2880  result->time = span->time;
2881 
2882  wholemonth = result->day / DAYS_PER_MONTH;
2883  result->day -= wholemonth * DAYS_PER_MONTH;
2884  if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2885  ereport(ERROR,
2886  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2887  errmsg("interval out of range")));
2888 
2889  if (result->month > 0 && result->day < 0)
2890  {
2891  result->day += DAYS_PER_MONTH;
2892  result->month--;
2893  }
2894  else if (result->month < 0 && result->day > 0)
2895  {
2896  result->day -= DAYS_PER_MONTH;
2897  result->month++;
2898  }
2899 
2900  PG_RETURN_INTERVAL_P(result);
2901 }
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), 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 2833 of file timestamp.c.

2834 {
2835  Interval *span = PG_GETARG_INTERVAL_P(0);
2836  Interval *result;
2837  TimeOffset wholeday;
2838 
2839  result = (Interval *) palloc(sizeof(Interval));
2840  result->month = span->month;
2841  result->day = span->day;
2842  result->time = span->time;
2843 
2844  TMODULO(result->time, wholeday, USECS_PER_DAY);
2845  if (pg_add_s32_overflow(result->day, wholeday, &result->day))
2846  ereport(ERROR,
2847  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2848  errmsg("interval out of range")));
2849 
2850  if (result->day > 0 && result->time < 0)
2851  {
2852  result->time += USECS_PER_DAY;
2853  result->day--;
2854  }
2855  else if (result->day < 0 && result->time > 0)
2856  {
2857  result->time -= USECS_PER_DAY;
2858  result->day++;
2859  }
2860 
2861  PG_RETURN_INTERVAL_P(result);
2862 }
#define TMODULO(t, q, u)
Definition: datetime.h:248

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), 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 2757 of file timestamp.c.

2758 {
2759  Interval *span = PG_GETARG_INTERVAL_P(0);
2760  Interval *result;
2761  TimeOffset wholeday;
2762  int32 wholemonth;
2763 
2764  result = (Interval *) palloc(sizeof(Interval));
2765  result->month = span->month;
2766  result->day = span->day;
2767  result->time = span->time;
2768 
2769  /* pre-justify days if it might prevent overflow */
2770  if ((result->day > 0 && result->time > 0) ||
2771  (result->day < 0 && result->time < 0))
2772  {
2773  wholemonth = result->day / DAYS_PER_MONTH;
2774  result->day -= wholemonth * DAYS_PER_MONTH;
2775  if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2776  ereport(ERROR,
2777  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2778  errmsg("interval out of range")));
2779  }
2780 
2781  /*
2782  * Since TimeOffset is int64, abs(wholeday) can't exceed about 1.07e8. If
2783  * we pre-justified then abs(result->day) is less than DAYS_PER_MONTH, so
2784  * this addition can't overflow. If we didn't pre-justify, then day and
2785  * time are of different signs, so it still can't overflow.
2786  */
2787  TMODULO(result->time, wholeday, USECS_PER_DAY);
2788  result->day += wholeday;
2789 
2790  wholemonth = result->day / DAYS_PER_MONTH;
2791  result->day -= wholemonth * DAYS_PER_MONTH;
2792  if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2793  ereport(ERROR,
2794  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2795  errmsg("interval out of range")));
2796 
2797  if (result->month > 0 &&
2798  (result->day < 0 || (result->day == 0 && result->time < 0)))
2799  {
2800  result->day += DAYS_PER_MONTH;
2801  result->month--;
2802  }
2803  else if (result->month < 0 &&
2804  (result->day > 0 || (result->day == 0 && result->time > 0)))
2805  {
2806  result->day -= DAYS_PER_MONTH;
2807  result->month++;
2808  }
2809 
2810  if (result->day > 0 && result->time < 0)
2811  {
2812  result->time += USECS_PER_DAY;
2813  result->day--;
2814  }
2815  else if (result->day < 0 && result->time > 0)
2816  {
2817  result->time -= USECS_PER_DAY;
2818  result->day++;
2819  }
2820 
2821  PG_RETURN_INTERVAL_P(result);
2822 }

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), 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 3216 of file timestamp.c.

3217 {
3218  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3219  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3220  Interval *result;
3221 
3222  if (interval_cmp_internal(interval1, interval2) > 0)
3223  result = interval1;
3224  else
3225  result = interval2;
3226  PG_RETURN_INTERVAL_P(result);
3227 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2465 of file timestamp.c.

2466 {
2467  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2468  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2469 
2470  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2471 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

◆ interval_lt()

Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2447 of file timestamp.c.

2448 {
2449  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2450  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2451 
2452  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2453 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3264 of file timestamp.c.

3265 {
3266  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3267  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3268  Interval *result;
3269 
3270  result = (Interval *) palloc(sizeof(Interval));
3271 
3272  result->month = span1->month - span2->month;
3273  /* overflow check copied from int4mi */
3274  if (!SAMESIGN(span1->month, span2->month) &&
3275  !SAMESIGN(result->month, span1->month))
3276  ereport(ERROR,
3277  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3278  errmsg("interval out of range")));
3279 
3280  result->day = span1->day - span2->day;
3281  if (!SAMESIGN(span1->day, span2->day) &&
3282  !SAMESIGN(result->day, span1->day))
3283  ereport(ERROR,
3284  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3285  errmsg("interval out of range")));
3286 
3287  result->time = span1->time - span2->time;
3288  if (!SAMESIGN(span1->time, span2->time) &&
3289  !SAMESIGN(result->time, span1->time))
3290  ereport(ERROR,
3291  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3292  errmsg("interval out of range")));
3293 
3294  PG_RETURN_INTERVAL_P(result);
3295 }
#define SAMESIGN(a, b)
Definition: timestamp.c:50

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, SAMESIGN, and Interval::time.

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

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3304 of file timestamp.c.

3305 {
3306  Interval *span = PG_GETARG_INTERVAL_P(0);
3307  float8 factor = PG_GETARG_FLOAT8(1);
3308  double month_remainder_days,
3309  sec_remainder,
3310  result_double;
3311  int32 orig_month = span->month,
3312  orig_day = span->day;
3313  Interval *result;
3314 
3315  result = (Interval *) palloc(sizeof(Interval));
3316 
3317  result_double = span->month * factor;
3318  if (isnan(result_double) ||
3319  result_double > INT_MAX || result_double < INT_MIN)
3320  ereport(ERROR,
3321  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3322  errmsg("interval out of range")));
3323  result->month = (int32) result_double;
3324 
3325  result_double = span->day * factor;
3326  if (isnan(result_double) ||
3327  result_double > INT_MAX || result_double < INT_MIN)
3328  ereport(ERROR,
3329  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3330  errmsg("interval out of range")));
3331  result->day = (int32) result_double;
3332 
3333  /*
3334  * The above correctly handles the whole-number part of the month and day
3335  * products, but we have to do something with any fractional part
3336  * resulting when the factor is non-integral. We cascade the fractions
3337  * down to lower units using the conversion factors DAYS_PER_MONTH and
3338  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3339  * so by the representation. The user can choose to cascade up later,
3340  * using justify_hours and/or justify_days.
3341  */
3342 
3343  /*
3344  * Fractional months full days into days.
3345  *
3346  * Floating point calculation are inherently imprecise, so these
3347  * calculations are crafted to produce the most reliable result possible.
3348  * TSROUND() is needed to more accurately produce whole numbers where
3349  * appropriate.
3350  */
3351  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3352  month_remainder_days = TSROUND(month_remainder_days);
3353  sec_remainder = (orig_day * factor - result->day +
3354  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3355  sec_remainder = TSROUND(sec_remainder);
3356 
3357  /*
3358  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3359  * cascade from months and days. It might still be >24 if the combination
3360  * of cascade and the seconds factor operation itself.
3361  */
3362  if (fabs(sec_remainder) >= SECS_PER_DAY)
3363  {
3364  result->day += (int) (sec_remainder / SECS_PER_DAY);
3365  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3366  }
3367 
3368  /* cascade units down */
3369  result->day += (int32) month_remainder_days;
3370  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3371  if (isnan(result_double) || !FLOAT8_FITS_IN_INT64(result_double))
3372  ereport(ERROR,
3373  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3374  errmsg("interval out of range")));
3375  result->time = (int64) result_double;
3376 
3377  PG_RETURN_INTERVAL_P(result);
3378 }
#define FLOAT8_FITS_IN_INT64(num)
Definition: c.h:1105

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg(), ERROR, FLOAT8_FITS_IN_INT64, Interval::month, palloc(), PG_GETARG_FLOAT8, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_lerp(), and mul_d_interval().

◆ interval_ne()

Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2438 of file timestamp.c.

2439 {
2440  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2441  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2442 
2443  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2444 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 958 of file timestamp.c.

959 {
960  Interval *span = PG_GETARG_INTERVAL_P(0);
961  char *result;
962  struct pg_itm tt,
963  *itm = &tt;
964  char buf[MAXDATELEN + 1];
965 
966  interval2itm(*span, itm);
968 
969  result = pstrdup(buf);
970  PG_RETURN_CSTRING(result);
971 }
void EncodeInterval(struct pg_itm *itm, int style, char *str)
Definition: datetime.c:4474
void interval2itm(Interval span, struct pg_itm *itm)
Definition: timestamp.c:1976
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int IntervalStyle
Definition: globals.c:121
#define MAXDATELEN
Definition: datetime.h:200
static char * buf
Definition: pg_test_fsync.c:67

References buf, EncodeInterval(), interval2itm(), 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 5407 of file timestamp.c.

5408 {
5409  return interval_part_common(fcinfo, false);
5410 }

References interval_part_common().

◆ interval_part_common()

static Datum interval_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5242 of file timestamp.c.

5243 {
5244  text *units = PG_GETARG_TEXT_PP(0);
5246  int64 intresult;
5247  int type,
5248  val;
5249  char *lowunits;
5250  struct pg_itm tt,
5251  *tm = &tt;
5252 
5253  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
5254  VARSIZE_ANY_EXHDR(units),
5255  false);
5256 
5257  type = DecodeUnits(0, lowunits, &val);
5258  if (type == UNKNOWN_FIELD)
5259  type = DecodeSpecial(0, lowunits, &val);
5260 
5261  if (type == UNITS)
5262  {
5264  switch (val)
5265  {
5266  case DTK_MICROSEC:
5267  intresult = tm->tm_sec * INT64CONST(1000000) + tm->tm_usec;
5268  break;
5269 
5270  case DTK_MILLISEC:
5271  if (retnumeric)
5272  /*---
5273  * tm->tm_sec * 1000 + fsec / 1000
5274  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5275  */
5276  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + tm->tm_usec, 3));
5277  else
5278  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + tm->tm_usec / 1000.0);
5279  break;
5280 
5281  case DTK_SECOND:
5282  if (retnumeric)
5283  /*---
5284  * tm->tm_sec + fsec / 1'000'000
5285  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5286  */
5287  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + tm->tm_usec, 6));
5288  else
5289  PG_RETURN_FLOAT8(tm->tm_sec + tm->tm_usec / 1000000.0);
5290  break;
5291 
5292  case DTK_MINUTE:
5293  intresult = tm->tm_min;
5294  break;
5295 
5296  case DTK_HOUR:
5297  intresult = tm->tm_hour;
5298  break;
5299 
5300  case DTK_DAY:
5301  intresult = tm->tm_mday;
5302  break;
5303 
5304  case DTK_MONTH:
5305  intresult = tm->tm_mon;
5306  break;
5307 
5308  case DTK_QUARTER:
5309  intresult = (tm->tm_mon / 3) + 1;
5310  break;
5311 
5312  case DTK_YEAR:
5313  intresult = tm->tm_year;
5314  break;
5315 
5316  case DTK_DECADE:
5317  /* caution: C division may have negative remainder */
5318  intresult = tm->tm_year / 10;
5319  break;
5320 
5321  case DTK_CENTURY:
5322  /* caution: C division may have negative remainder */
5323  intresult = tm->tm_year / 100;
5324  break;
5325 
5326  case DTK_MILLENNIUM:
5327  /* caution: C division may have negative remainder */
5328  intresult = tm->tm_year / 1000;
5329  break;
5330 
5331  default:
5332  ereport(ERROR,
5333  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5334  errmsg("unit \"%s\" not supported for type %s",
5335  lowunits, format_type_be(INTERVALOID))));
5336  intresult = 0;
5337  }
5338  }
5339  else if (type == RESERV && val == DTK_EPOCH)
5340  {
5341  if (retnumeric)
5342  {
5343  Numeric result;
5344  int64 secs_from_day_month;
5345  int64 val;
5346 
5347  /*
5348  * To do this calculation in integer arithmetic even though
5349  * DAYS_PER_YEAR is fractional, multiply everything by 4 and then
5350  * divide by 4 again at the end. This relies on DAYS_PER_YEAR
5351  * being a multiple of 0.25 and on SECS_PER_DAY being a multiple
5352  * of 4.
5353  */
5354  secs_from_day_month = ((int64) (4 * DAYS_PER_YEAR) * (interval->month / MONTHS_PER_YEAR) +
5355  (int64) (4 * DAYS_PER_MONTH) * (interval->month % MONTHS_PER_YEAR) +
5356  (int64) 4 * interval->day) * (SECS_PER_DAY / 4);
5357 
5358  /*---
5359  * result = secs_from_day_month + interval->time / 1'000'000
5360  * = (secs_from_day_month * 1'000'000 + interval->time) / 1'000'000
5361  */
5362 
5363  /*
5364  * Try the computation inside int64; if it overflows, do it in
5365  * numeric (slower). This overflow happens around 10^9 days, so
5366  * not common in practice.
5367  */
5368  if (!pg_mul_s64_overflow(secs_from_day_month, 1000000, &val) &&
5370  result = int64_div_fast_to_numeric(val, 6);
5371  else
5372  result =
5374  int64_to_numeric(secs_from_day_month),
5375  NULL);
5376 
5377  PG_RETURN_NUMERIC(result);
5378  }
5379  else
5380  {
5381  float8 result;
5382 
5383  result = interval->time / 1000000.0;
5384  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
5385  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
5386  result += ((double) SECS_PER_DAY) * interval->day;
5387 
5388  PG_RETURN_FLOAT8(result);
5389  }
5390  }
5391  else
5392  {
5393  ereport(ERROR,
5394  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5395  errmsg("unit \"%s\" not recognized for type %s",
5396  lowunits, format_type_be(INTERVALOID))));
5397  intresult = 0;
5398  }
5399 
5400  if (retnumeric)
5401  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
5402  else
5403  PG_RETURN_FLOAT8(intresult);
5404 }
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition: datetime.c:3936
int DecodeSpecial(int field, const char *lowtoken, int *val)
Definition: datetime.c:3139
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4232
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4253
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2866
#define DAYS_PER_YEAR
Definition: timestamp.h:107
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#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_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:215
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:161
#define PG_RETURN_NUMERIC(x)
Definition: numeric.h:79
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
const char * type

References DAYS_PER_MONTH, DAYS_PER_YEAR, DecodeSpecial(), DecodeUnits(), 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_YEAR, ereport, errcode(), errmsg(), ERROR, format_type_be(), int64_div_fast_to_numeric(), int64_to_numeric(), interval2itm(), interval::month, MONTHS_PER_YEAR, numeric_add_opt_error(), pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, pg_mul_s64_overflow(), PG_RETURN_FLOAT8, 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 3230 of file timestamp.c.

3231 {
3232  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3233  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3234  Interval *result;
3235 
3236  result = (Interval *) palloc(sizeof(Interval));
3237 
3238  result->month = span1->month + span2->month;
3239  /* overflow check copied from int4pl */
3240  if (SAMESIGN(span1->month, span2->month) &&
3241  !SAMESIGN(result->month, span1->month))
3242  ereport(ERROR,
3243  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3244  errmsg("interval out of range")));
3245 
3246  result->day = span1->day + span2->day;
3247  if (SAMESIGN(span1->day, span2->day) &&
3248  !SAMESIGN(result->day, span1->day))
3249  ereport(ERROR,
3250  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3251  errmsg("interval out of range")));
3252 
3253  result->time = span1->time + span2->time;
3254  if (SAMESIGN(span1->time, span2->time) &&
3255  !SAMESIGN(result->time, span1->time))
3256  ereport(ERROR,
3257  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3258  errmsg("interval out of range")));
3259 
3260  PG_RETURN_INTERVAL_P(result);
3261 }

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, SAMESIGN, and Interval::time.

Referenced by in_range_interval_interval(), interval_accum(), interval_combine(), and interval_lerp().

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

Definition at line 977 of file timestamp.c.

978 {
980 
981 #ifdef NOT_USED
982  Oid typelem = PG_GETARG_OID(1);
983 #endif
984  int32 typmod = PG_GETARG_INT32(2);
986 
987  interval = (Interval *) palloc(sizeof(Interval));
988 
990  interval->day = pq_getmsgint(buf, sizeof(interval->day));
992 
993  AdjustIntervalForTypmod(interval, typmod, NULL);
994 
996 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:418
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:456
StringInfoData * StringInfo
Definition: stringinfo.h:44

References AdjustIntervalForTypmod(), buf, interval::month, palloc(), 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 1308 of file timestamp.c.

1309 {
1311  int32 typmod = PG_GETARG_INT32(1);
1312  Interval *result;
1313 
1314  result = palloc(sizeof(Interval));
1315  *result = *interval;
1316 
1317  AdjustIntervalForTypmod(result, typmod, NULL);
1318 
1319  PG_RETURN_INTERVAL_P(result);
1320 }

References AdjustIntervalForTypmod(), palloc(), PG_GETARG_INT32, PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_send()

Datum interval_send ( PG_FUNCTION_ARGS  )

Definition at line 1002 of file timestamp.c.

1003 {
1006 
1007  pq_begintypsend(&buf);
1009  pq_sendint32(&buf, interval->day);
1012 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153

References buf, interval::month, PG_GETARG_INTERVAL_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint32(), pq_sendint64(), and interval::time.

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3201 of file timestamp.c.

3202 {
3203  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3204  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3205  Interval *result;
3206 
3207  /* use interval_cmp_internal to be sure this agrees with comparisons */
3208  if (interval_cmp_internal(interval1, interval2) < 0)
3209  result = interval1;
3210  else
3211  result = interval2;
3212  PG_RETURN_INTERVAL_P(result);
3213 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1245 of file timestamp.c.

1246 {
1247  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1248  Node *ret = NULL;
1249 
1250  if (IsA(rawreq, SupportRequestSimplify))
1251  {
1253  FuncExpr *expr = req->fcall;
1254  Node *typmod;
1255 
1256  Assert(list_length(expr->args) >= 2);
1257 
1258  typmod = (Node *) lsecond(expr->args);
1259 
1260  if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
1261  {
1262  Node *source = (Node *) linitial(expr->args);
1263  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1264  bool noop;
1265 
1266  if (new_typmod < 0)
1267  noop = true;
1268  else
1269  {
1270  int32 old_typmod = exprTypmod(source);
1271  int old_least_field;
1272  int new_least_field;
1273  int old_precis;
1274  int new_precis;
1275 
1276  old_least_field = intervaltypmodleastfield(old_typmod);
1277  new_least_field = intervaltypmodleastfield(new_typmod);
1278  if (old_typmod < 0)
1279  old_precis = INTERVAL_FULL_PRECISION;
1280  else
1281  old_precis = INTERVAL_PRECISION(old_typmod);
1282  new_precis = INTERVAL_PRECISION(new_typmod);
1283 
1284  /*
1285  * Cast is a no-op if least field stays the same or decreases
1286  * while precision stays the same or increases. But
1287  * precision, which is to say, sub-second precision, only
1288  * affects ranges that include SECOND.
1289  */
1290  noop = (new_least_field <= old_least_field) &&
1291  (old_least_field > 0 /* SECOND */ ||
1292  new_precis >= MAX_INTERVAL_PRECISION ||
1293  new_precis >= old_precis);
1294  }
1295  if (noop)
1296  ret = relabel_to_typmod(source, new_typmod);
1297  }
1298  }
1299 
1300  PG_RETURN_POINTER(ret);
1301 }
static int intervaltypmodleastfield(int32 typmod)
Definition: timestamp.c:1192
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
Assert(fmt[strlen(fmt) - 1] !='\n')
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:282
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:654
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static rewind_source * source
Definition: pg_rewind.c:89
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
List * args
Definition: primnodes.h:695

References FuncExpr::args, Assert(), DatumGetInt32(), exprTypmod(), SupportRequestSimplify::fcall, 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 4366 of file timestamp.c.

4367 {
4368  text *units = PG_GETARG_TEXT_PP(0);
4370  Interval *result;
4371  int type,
4372  val;
4373  char *lowunits;
4374  struct pg_itm tt,
4375  *tm = &tt;
4376 
4377  result = (Interval *) palloc(sizeof(Interval));
4378 
4379  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4380  VARSIZE_ANY_EXHDR(units),
4381  false);
4382 
4383  type = DecodeUnits(0, lowunits, &val);
4384 
4385  if (type == UNITS)
4386  {
4388  switch (val)
4389  {
4390  case DTK_MILLENNIUM:
4391  /* caution: C division may have negative remainder */
4392  tm->tm_year = (tm->tm_year / 1000) * 1000;
4393  /* FALL THRU */
4394  case DTK_CENTURY:
4395  /* caution: C division may have negative remainder */
4396  tm->tm_year = (tm->tm_year / 100) * 100;
4397  /* FALL THRU */
4398  case DTK_DECADE:
4399  /* caution: C division may have negative remainder */
4400  tm->tm_year = (tm->tm_year / 10) * 10;
4401  /* FALL THRU */
4402  case DTK_YEAR:
4403  tm->tm_mon = 0;
4404  /* FALL THRU */
4405  case DTK_QUARTER:
4406  tm->tm_mon = 3 * (tm->tm_mon / 3);
4407  /* FALL THRU */
4408  case DTK_MONTH:
4409  tm->tm_mday = 0;
4410  /* FALL THRU */
4411  case DTK_DAY:
4412  tm->tm_hour = 0;
4413  /* FALL THRU */
4414  case DTK_HOUR:
4415  tm->tm_min = 0;
4416  /* FALL THRU */
4417  case DTK_MINUTE:
4418  tm->tm_sec = 0;
4419  /* FALL THRU */
4420  case DTK_SECOND:
4421  tm->tm_usec = 0;
4422  break;
4423  case DTK_MILLISEC:
4424  tm->tm_usec = (tm->tm_usec / 1000) * 1000;
4425  break;
4426  case DTK_MICROSEC:
4427  break;
4428 
4429  default:
4430  ereport(ERROR,
4431  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4432  errmsg("unit \"%s\" not supported for type %s",
4433  lowunits, format_type_be(INTERVALOID)),
4434  (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
4435  }
4436 
4437  if (itm2interval(tm, result) != 0)
4438  ereport(ERROR,
4439  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4440  errmsg("interval out of range")));
4441  }
4442  else
4443  {
4444  ereport(ERROR,
4445  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4446  errmsg("unit \"%s\" not recognized for type %s",
4447  lowunits, format_type_be(INTERVALOID))));
4448  }
4449 
4450  PG_RETURN_INTERVAL_P(result);
4451 }
int itm2interval(struct pg_itm *itm, Interval *span)
Definition: timestamp.c:2003
int errdetail(const char *fmt,...)
Definition: elog.c:1202
#define DTK_WEEK
Definition: datetime.h:164

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, format_type_be(), interval2itm(), itm2interval(), palloc(), 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 3172 of file timestamp.c.

3173 {
3175  Interval *result;
3176 
3177  result = (Interval *) palloc(sizeof(Interval));
3178 
3179  result->time = -interval->time;
3180  /* overflow check copied from int4um */
3181  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3182  ereport(ERROR,
3183  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3184  errmsg("interval out of range")));
3185  result->day = -interval->day;
3186  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3187  ereport(ERROR,
3188  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3189  errmsg("interval out of range")));
3190  result->month = -interval->month;
3191  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3192  ereport(ERROR,
3193  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3194  errmsg("interval out of range")));
3195 
3196  PG_RETURN_INTERVAL_P(result);
3197 }

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, SAMESIGN, Interval::time, and interval::time.

Referenced by abs_interval().

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1027 of file timestamp.c.

1028 {
1030  int32 *tl;
1031  int n;
1032  int32 typmod;
1033 
1034  tl = ArrayGetIntegerTypmods(ta, &n);
1035 
1036  /*
1037  * tl[0] - interval range (fields bitmask) tl[1] - precision (optional)
1038  *
1039  * Note we must validate tl[0] even though it's normally guaranteed
1040  * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
1041  */
1042  if (n > 0)
1043  {
1044  switch (tl[0])
1045  {
1046  case INTERVAL_MASK(YEAR):
1047  case INTERVAL_MASK(MONTH):
1048  case INTERVAL_MASK(DAY):
1049  case INTERVAL_MASK(HOUR):
1050  case INTERVAL_MASK(MINUTE):
1051  case INTERVAL_MASK(SECOND):
1059  case INTERVAL_FULL_RANGE:
1060  /* all OK */
1061  break;
1062  default:
1063  ereport(ERROR,
1064  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1065  errmsg("invalid INTERVAL type modifier")));
1066  }
1067  }
1068 
1069  if (n == 1)
1070  {
1071  if (tl[0] != INTERVAL_FULL_RANGE)
1072  typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, tl[0]);
1073  else
1074  typmod = -1;
1075  }
1076  else if (n == 2)
1077  {
1078  if (tl[1] < 0)
1079  ereport(ERROR,
1080  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1081  errmsg("INTERVAL(%d) precision must not be negative",
1082  tl[1])));
1083  if (tl[1] > MAX_INTERVAL_PRECISION)
1084  {
1085  ereport(WARNING,
1086  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1087  errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1088  tl[1], MAX_INTERVAL_PRECISION)));
1089  typmod = INTERVAL_TYPMOD(MAX_INTERVAL_PRECISION, tl[0]);
1090  }
1091  else
1092  typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
1093  }
1094  else
1095  {
1096  ereport(ERROR,
1097  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1098  errmsg("invalid INTERVAL type modifier")));
1099  typmod = 0; /* keep compiler quiet */
1100  }
1101 
1102  PG_RETURN_INT32(typmod);
1103 }
#define INTERVAL_TYPMOD(p, r)
Definition: timestamp.h:80

References ArrayGetIntegerTypmods(), DAY, ereport, errcode(), errmsg(), ERROR, 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 1192 of file timestamp.c.

1193 {
1194  if (typmod < 0)
1195  return 0; /* SECOND */
1196 
1197  switch (INTERVAL_RANGE(typmod))
1198  {
1199  case INTERVAL_MASK(YEAR):
1200  return 5; /* YEAR */
1201  case INTERVAL_MASK(MONTH):
1202  return 4; /* MONTH */
1203  case INTERVAL_MASK(DAY):
1204  return 3; /* DAY */
1205  case INTERVAL_MASK(HOUR):
1206  return 2; /* HOUR */
1207  case INTERVAL_MASK(MINUTE):
1208  return 1; /* MINUTE */
1209  case INTERVAL_MASK(SECOND):
1210  return 0; /* SECOND */
1212  return 4; /* MONTH */
1214  return 2; /* HOUR */
1216  return 1; /* MINUTE */
1218  return 0; /* SECOND */
1220  return 1; /* MINUTE */
1222  return 0; /* SECOND */
1224  return 0; /* SECOND */
1225  case INTERVAL_FULL_RANGE:
1226  return 0; /* SECOND */
1227  default:
1228  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1229  break;
1230  }
1231  return 0; /* can't get here, but keep compiler quiet */
1232 }

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 1106 of file timestamp.c.

1107 {
1108  int32 typmod = PG_GETARG_INT32(0);
1109  char *res = (char *) palloc(64);
1110  int fields;
1111  int precision;
1112  const char *fieldstr;
1113 
1114  if (typmod < 0)
1115  {
1116  *res = '\0';
1118  }
1119 
1120  fields = INTERVAL_RANGE(typmod);
1121  precision = INTERVAL_PRECISION(typmod);
1122 
1123  switch (fields)
1124  {
1125  case INTERVAL_MASK(YEAR):
1126  fieldstr = " year";
1127  break;
1128  case INTERVAL_MASK(MONTH):
1129  fieldstr = " month";
1130  break;
1131  case INTERVAL_MASK(DAY):
1132  fieldstr = " day";
1133  break;
1134  case INTERVAL_MASK(HOUR):
1135  fieldstr = " hour";
1136  break;
1137  case INTERVAL_MASK(MINUTE):
1138  fieldstr = " minute";
1139  break;
1140  case INTERVAL_MASK(SECOND):
1141  fieldstr = " second";
1142  break;
1144  fieldstr = " year to month";
1145  break;
1147  fieldstr = " day to hour";
1148  break;
1150  fieldstr = " day to minute";
1151  break;
1153  fieldstr = " day to second";
1154  break;
1156  fieldstr = " hour to minute";
1157  break;
1159  fieldstr = " hour to second";
1160  break;
1162  fieldstr = " minute to second";
1163  break;
1164  case INTERVAL_FULL_RANGE:
1165  fieldstr = "";
1166  break;
1167  default:
1168  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1169  fieldstr = "";
1170  break;
1171  }
1172 
1173  if (precision != INTERVAL_FULL_PRECISION)
1174  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1175  else
1176  snprintf(res, 64, "%s", fieldstr);
1177 
1179 }
#define snprintf
Definition: port.h:238

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

◆ isoweek2date()

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

Definition at line 4479 of file timestamp.c.

4480 {
4481  j2date(isoweek2j(*year, woy), year, mon, mday);
4482 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:313

References isoweek2j(), and j2date().

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

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4459 of file timestamp.c.

4460 {
4461  int day0,
4462  day4;
4463 
4464  /* fourth day of current year */
4465  day4 = date2j(year, 1, 4);
4466 
4467  /* day0 == offset to first day of week (Monday) */
4468  day0 = j2day(day4 - 1);
4469 
4470  return ((week - 1) * 7) + (day4 - day0);
4471 }

References date2j(), 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 4492 of file timestamp.c.

4493 {
4494  int jday;
4495 
4496  jday = isoweek2j(*year, isoweek);
4497  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4498  if (wday > 1)
4499  jday += wday - 2;
4500  else
4501  jday += 6;
4502  j2date(jday, year, mon, mday);
4503 }

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 2003 of file timestamp.c.

2004 {
2005  int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2006 
2007  if (total_months > INT_MAX || total_months < INT_MIN)
2008  return -1;
2009  span->month = (int32) total_months;
2010  span->day = itm->tm_mday;
2012  &span->time))
2013  return -1;
2014  /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2015  if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2016  &span->time))
2017  return -1;
2018  if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2019  &span->time))
2020  return -1;
2021  if (pg_add_s64_overflow(span->time, itm->tm_usec,
2022  &span->time))
2023  return -1;
2024  return 0;
2025 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77

References Interval::day, if(), Interval::month, MONTHS_PER_YEAR, pg_add_s64_overflow(), pg_mul_s64_overflow(), Interval::time, pg_itm::tm_hour, pg_itm::tm_mday, pg_itm::tm_min, pg_itm::tm_mon, pg_itm::tm_sec, pg_itm::tm_usec, pg_itm::tm_year, 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 2032 of file timestamp.c.

2033 {
2034  int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2035 
2036  if (total_months > INT_MAX || total_months < INT_MIN)
2037  return -1;
2038  span->month = (int32) total_months;
2039  span->day = itm_in->tm_mday;
2040  span->time = itm_in->tm_usec;
2041  return 0;
2042 }

References Interval::day, if(), Interval::month, MONTHS_PER_YEAR, Interval::time, pg_itm_in::tm_mday, pg_itm_in::tm_mon, pg_itm_in::tm_usec, and pg_itm_in::tm_year.

Referenced by interval_in(), and pg_timezone_abbrevs().

◆ lookup_timezone()

static pg_tz* lookup_timezone ( text zone)
static

Definition at line 542 of file timestamp.c.

543 {
544  char tzname[TZ_STRLEN_MAX + 1];
545 
546  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
547 
548  return DecodeTimezoneNameToTz(tzname);
549 }
pg_tz * DecodeTimezoneNameToTz(const char *tzname)
Definition: datetime.c:3236
#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:246

References DecodeTimezoneNameToTz(), 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 1500 of file timestamp.c.

1501 {
1502  int32 years = PG_GETARG_INT32(0);
1504  int32 weeks = PG_GETARG_INT32(2);
1505  int32 days = PG_GETARG_INT32(3);
1506  int32 hours = PG_GETARG_INT32(4);
1507  int32 mins = PG_GETARG_INT32(5);
1508  double secs = PG_GETARG_FLOAT8(6);
1509  Interval *result;
1510 
1511  /*
1512  * Reject out-of-range inputs. We really ought to check the integer
1513  * inputs as well, but it's not entirely clear what limits to apply.
1514  */
1515  if (isinf(secs) || isnan(secs))
1516  ereport(ERROR,
1517  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1518  errmsg("interval out of range")));
1519 
1520  result = (Interval *) palloc(sizeof(Interval));
1521  result->month = years * MONTHS_PER_YEAR + months;
1522  result->day = weeks * 7 + days;
1523 
1524  secs = rint(secs * USECS_PER_SEC);
1525  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1526  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1527  (int64) secs;
1528 
1529  PG_RETURN_INTERVAL_P(result);
1530 }
const char *const months[]
Definition: datetime.c:83
#define SECS_PER_HOUR
Definition: timestamp.h:126
#define SECS_PER_MINUTE
Definition: timestamp.h:127

References Interval::day, days, ereport, errcode(), errmsg(), ERROR, Interval::month, months, MONTHS_PER_YEAR, palloc(), PG_GETARG_FLOAT8, PG_GETARG_INT32, PG_RETURN_INTERVAL_P, SECS_PER_HOUR, SECS_PER_MINUTE, Interval::time, and USECS_PER_SEC.

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 638 of file timestamp.c.

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

References 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 556 of file timestamp.c.

558 {
559  struct pg_tm tm;
561  TimeOffset time;
562  int dterr;
563  bool bc = false;
564  Timestamp result;
565 
566  tm.tm_year = year;
567  tm.tm_mon = month;
568  tm.tm_mday = day;
569 
570  /* Handle negative years as BC */
571  if (tm.tm_year < 0)
572  {
573  bc = true;
574  tm.tm_year = -tm.tm_year;
575  }
576 
577  dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
578 
579  if (dterr != 0)
580  ereport(ERROR,
581  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
582  errmsg("date field value out of range: %d-%02d-%02d",
583  year, month, day)));
584 
586  ereport(ERROR,
587  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
588  errmsg("date out of range: %d-%02d-%02d",
589  year, month, day)));
590 
592 
593  /* Check for time overflow */
594  if (float_time_overflows(hour, min, sec))
595  ereport(ERROR,
596  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
597  errmsg("time field value out of range: %d:%02d:%02g",
598  hour, min, sec)));
599 
600  /* This should match tm2time */
601  time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
602  * USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
603 
604  result = date * USECS_PER_DAY + time;
605  /* check for major overflow */
606  if ((result - time) / USECS_PER_DAY != date)
607  ereport(ERROR,
608  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
609  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
610  year, month, day,
611  hour, min, sec)));
612 
613  /* check for just-barely overflow (okay except time-of-day wraps) */
614  /* caution: we want to allow 1999-12-31 24:00:00 */
615  if ((result < 0 && date > 0) ||
616  (result > 0 && date < -1))
617  ereport(ERROR,
618  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
619  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
620  year, month, day,
621  hour, min, sec)));
622 
623  /* final range check catches just-out-of-range timestamps */
624  if (!IS_VALID_TIMESTAMP(result))
625  ereport(ERROR,
626  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
627  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
628  year, month, day,
629  hour, min, sec)));
630 
631  return result;
632 }
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2499
#define MINS_PER_HOUR
Definition: timestamp.h:128
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:201
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1450
#define DTK_DATE_M
Definition: datetime.h:191
long date
Definition: pgtypes_date.h:9

References date2j(), DTK_DATE_M, ereport, errcode(), errmsg(), ERROR, float_time_overflows(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, MINS_PER_HOUR, POSTGRES_EPOCH_JDATE, SECS_PER_MINUTE, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, 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 658 of file timestamp.c.

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

References 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 679 of file timestamp.c.

680 {
681  int32 year = PG_GETARG_INT32(0);
682  int32 month = PG_GETARG_INT32(1);
683  int32 mday = PG_GETARG_INT32(2);
684  int32 hour = PG_GETARG_INT32(3);
685  int32 min = PG_GETARG_INT32(4);
686  float8 sec = PG_GETARG_FLOAT8(5);
688  TimestampTz result;
690  struct pg_tm tt;
691  int tz;
692  fsec_t fsec;
693 
694  timestamp = make_timestamp_internal(year, month, mday,
695  hour, min, sec);
696 
697  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
698  ereport(ERROR,
699  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
700  errmsg("timestamp out of range")));
701 
702  tz = parse_sane_timezone(&tt, zone);
703 
704  result = dt2local(timestamp, -tz);
705 
706  if (!IS_VALID_TIMESTAMP(result))
707  ereport(ERROR,
708  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
709  errmsg("timestamp out of range")));
710 
711  PG_RETURN_TIMESTAMPTZ(result);
712 }
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2051
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1830
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:473
int32 fsec_t
Definition: timestamp.h:41
int64 timestamp

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

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3381 of file timestamp.c.

3382 {
3383  /* Args are float8 and Interval *, but leave them as generic Datum */
3384  Datum factor = PG_GETARG_DATUM(0);
3385  Datum span = PG_GETARG_DATUM(1);
3386 
3387  return DirectFunctionCall2(interval_mul, span, factor);
3388 }
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3304

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

◆ NonFiniteTimestampTzPart()

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

Definition at line 4639 of file timestamp.c.

4641 {
4642  if ((type != UNITS) && (type != RESERV))
4643  ereport(ERROR,
4644  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4645  errmsg("unit \"%s\" not recognized for type %s",
4646  lowunits,
4647  format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
4648 
4649  switch (unit)
4650  {
4651  /* Oscillating units */
4652  case DTK_MICROSEC:
4653  case DTK_MILLISEC:
4654  case DTK_SECOND:
4655  case DTK_MINUTE:
4656  case DTK_HOUR:
4657  case DTK_DAY:
4658  case DTK_MONTH:
4659  case DTK_QUARTER:
4660  case DTK_WEEK:
4661  case DTK_DOW:
4662  case DTK_ISODOW:
4663  case DTK_DOY:
4664  case DTK_TZ:
4665  case DTK_TZ_MINUTE:
4666  case DTK_TZ_HOUR:
4667  return 0.0;
4668 
4669  /* Monotonically-increasing units */
4670  case DTK_YEAR:
4671  case DTK_DECADE:
4672  case DTK_CENTURY:
4673  case DTK_MILLENNIUM:
4674  case DTK_JULIAN:
4675  case DTK_ISOYEAR:
4676  case DTK_EPOCH:
4677  if (isNegative)
4678  return -get_float8_infinity();
4679  else
4680  return get_float8_infinity();
4681 
4682  default:
4683  ereport(ERROR,
4684  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4685  errmsg("unit \"%s\" not supported for type %s",
4686  lowunits,
4687  format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
4688  return 0.0; /* keep compiler quiet */
4689  }
4690 }
static float8 get_float8_infinity(void)
Definition: float.h:94
#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, format_type_be(), get_float8_infinity(), RESERV, type, and UNITS.

Referenced by timestamp_part_common(), and timestamptz_part_common().

◆ now()

Definition at line 1547 of file timestamp.c.

References GetCurrentTransactionStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

Referenced by advanceConnectionState(), ApplyLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), CalculateCopyStreamSleeptime(), CheckArchiveTimeout(), CheckpointerMain(), doLog(), doRetry(), dumpTimestamp(), enable_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), executeMetaCommand(), flushAndSendFeedback(), handle_sig_alarm(), HandleCopyStream(), has_startup_progress_timeout_expired(), initialize_drandom_seed(), IsCheckpointOnSchedule(), LagTrackerRead(), launch_worker(), 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(), pgfdw_get_cleanup_result(), pgstat_report_archiver(), pgstat_report_stat(), pqSocketPoll(), pqTraceFormatTimestamp(), printProgressReport(), printVerboseErrorMessages(), process_syncing_tables_for_apply(), ProcessKeepaliveMsg(), ProcessStandbyReplyMessage(), processXactStats(), ProcSleep(), progress_report(), RequestXLogStreaming(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), schedule_alarm(), send_feedback(), sendFeedback(), ServerLoop(), set_next_rotation_time(), StreamLogicalLog(), SysLoggerMain(), threadRun(), timetz_zone(), WaitForWALToBecomeAvailable(), WalRcvComputeNextWakeup(), WalRcvRunning(), WalRcvStreaming(), WalReceiverMain(), WalSndComputeSleeptime(), WalSndUpdateProgress(), WalSndWriteData(), xid_age(), XLogBackgroundFlush(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2537 of file timestamp.c.

2538 {
2539  /*
2540  * The arguments are Timestamps, but we leave them as generic Datums to
2541  * avoid unnecessary conversions between value and reference forms --- not
2542  * to mention possible dereferences of null pointers.
2543  */
2544  Datum ts1 = PG_GETARG_DATUM(0);
2545  Datum te1 = PG_GETARG_DATUM(1);
2546  Datum ts2 = PG_GETARG_DATUM(2);
2547  Datum te2 = PG_GETARG_DATUM(3);
2548  bool ts1IsNull = PG_ARGISNULL(0);
2549  bool te1IsNull = PG_ARGISNULL(1);
2550  bool ts2IsNull = PG_ARGISNULL(2);
2551  bool te2IsNull = PG_ARGISNULL(3);
2552 
2553 #define TIMESTAMP_GT(t1,t2) \
2554  DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2555 #define TIMESTAMP_LT(t1,t2) \
2556  DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2557 
2558  /*
2559  * If both endpoints of interval 1 are null, the result is null (unknown).
2560  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2561  * take ts1 as the lesser endpoint.
2562  */
2563  if (ts1IsNull)
2564  {
2565  if (te1IsNull)
2566  PG_RETURN_NULL();
2567  /* swap null for non-null */
2568  ts1 = te1;
2569  te1IsNull = true;
2570  }
2571  else if (!te1IsNull)
2572  {
2573  if (TIMESTAMP_GT(ts1, te1))
2574  {
2575  Datum tt = ts1;
2576 
2577  ts1 = te1;
2578  te1 = tt;
2579  }
2580  }
2581 
2582  /* Likewise for interval 2. */
2583  if (ts2IsNull)
2584  {
2585  if (te2IsNull)
2586  PG_RETURN_NULL();
2587  /* swap null for non-null */
2588  ts2 = te2;
2589  te2IsNull = true;
2590  }
2591  else if (!te2IsNull)
2592  {
2593  if (TIMESTAMP_GT(ts2, te2))
2594  {
2595  Datum tt = ts2;
2596 
2597  ts2 = te2;
2598  te2 = tt;
2599  }
2600  }
2601 
2602  /*
2603  * At this point neither ts1 nor ts2 is null, so we can consider three
2604  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2605  */
2606  if (TIMESTAMP_GT(ts1, ts2))
2607  {
2608  /*
2609  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2610  * in the presence of nulls it's not quite completely so.
2611  */
2612  if (te2IsNull)
2613  PG_RETURN_NULL();
2614  if (TIMESTAMP_LT(ts1, te2))
2615  PG_RETURN_BOOL(true);
2616  if (te1IsNull)
2617  PG_RETURN_NULL();
2618 
2619  /*
2620  * If te1 is not null then we had ts1 <= te1 above, and we just found
2621  * ts1 >= te2, hence te1 >= te2.
2622  */
2623  PG_RETURN_BOOL(false);
2624  }
2625  else if (TIMESTAMP_LT(ts1, ts2))
2626  {
2627  /* This case is ts2 < te1 OR te2 < te1 */
2628  if (te1IsNull)
2629  PG_RETURN_NULL();
2630  if (TIMESTAMP_LT(ts2, te1))
2631  PG_RETURN_BOOL(true);
2632  if (te2IsNull)
2633  PG_RETURN_NULL();
2634 
2635  /*
2636  * If te2 is not null then we had ts2 <= te2 above, and we just found
2637  * ts2 >= te1, hence te2 >= te1.
2638  */
2639  PG_RETURN_BOOL(false);
2640  }
2641  else
2642  {
2643  /*
2644  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2645  * rather silly way of saying "true if both are non-null, else null".
2646  */
2647  if (te1IsNull || te2IsNull)
2648  PG_RETURN_NULL();
2649  PG_RETURN_BOOL(true);
2650  }
2651 
2652 #undef TIMESTAMP_GT
2653 #undef TIMESTAMP_LT
2654 }
#define TIMESTAMP_GT(t1, t2)
#define TIMESTAMP_LT(t1, t2)
#define PG_ARGISNULL(n)
Definition: fmgr.h:209

References 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 473 of file timestamp.c.

474 {
475  char tzname[TZ_STRLEN_MAX + 1];
476  int dterr;
477  int tz;
478 
479  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
480 
481  /*
482  * Look up the requested timezone. First we try to interpret it as a
483  * numeric timezone specification; if DecodeTimezone decides it doesn't
484  * like the format, we try timezone abbreviations and names.
485  *
486  * Note pg_tzset happily parses numeric input that DecodeTimezone would
487  * reject. To avoid having it accept input that would otherwise be seen
488  * as invalid, it's enough to disallow having a digit in the first
489  * position of our input string.
490  */
491  if (isdigit((unsigned char) *tzname))
492  ereport(ERROR,
493  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
494  errmsg("invalid input syntax for type %s: \"%s\"",
495  "numeric time zone", tzname),
496  errhint("Numeric time zones must have \"-\" or \"+\" as first character.")));
497 
498  dterr = DecodeTimezone(tzname, &tz);
499  if (dterr != 0)
500  {
501  int type,
502  val;
503  pg_tz *tzp;
504 
505  if (dterr == DTERR_TZDISP_OVERFLOW)
506  ereport(ERROR,
507  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
508  errmsg("numeric time zone \"%s\" out of range", tzname)));
509  else if (dterr != DTERR_BAD_FORMAT)
510  ereport(ERROR,
511  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
512  errmsg("time zone \"%s\" not recognized", tzname)));
513 
514  type = DecodeTimezoneName(tzname, &val, &tzp);
515 
516  if (type == TZNAME_FIXED_OFFSET)
517  {
518  /* fixed-offset abbreviation */
519  tz = -val;
520  }
521  else if (type == TZNAME_DYNTZ)
522  {
523  /* dynamic-offset abbreviation, resolve using specified time */
524  tz = DetermineTimeZoneAbbrevOffset(tm, tzname, tzp);
525  }
526  else
527  {
528  /* full zone name */
529  tz = DetermineTimeZoneOffset(tm, tzp);
530  }
531  }
532 
533  return tz;
534 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1587
int DecodeTimezone(const char *str, int *tzp)
Definition: datetime.c:2998
int DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
Definition: datetime.c:3181
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1748
int errhint(const char *fmt,...)
Definition: elog.c:1316
#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, 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 1571 of file timestamp.c.

1572 {
1574 }
TimestampTz PgReloadTime
Definition: timestamp.c:56

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

◆ pg_postmaster_start_time()

Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1565 of file timestamp.c.

1566 {
1568 }
TimestampTz PgStartTime
Definition: timestamp.c:53

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2105 of file timestamp.c.

2106 {
2107  Timestamp dt;
2108  struct pg_tm tt,
2109  *tm = &tt;
2110 
2111  GetEpochTime(tm);
2112  /* we don't bother to test for failure ... */
2113  tm2timestamp(tm, 0, NULL, &dt);
2114 
2115  return dt;
2116 } /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2083
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1926

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

Referenced by dttofmtasc_replace(), PGTYPEStimestamp_from_asc(), timestamp_in(), timestamp_part_common(), timestamptz_in(), and timestamptz_part_common().

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1553 of file timestamp.c.

1554 {
1556 }
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:864

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 2045 of file timestamp.c.

2046 {
2047  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2048 }

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1740 of file timestamp.c.

1741 {
1742  TimestampTz result;
1743 
1744  result = (TimestampTz) tm -
1746  result *= USECS_PER_SEC;
1747 
1748  return result;
1749 }

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 1629 of file timestamp.c.

1630 {
1631  struct timeval tp;
1632  char templ[128];
1633  char buf[128];
1634  pg_time_t tt;
1635 
1636  gettimeofday(&tp, NULL);
1637  tt = (pg_time_t) tp.tv_sec;
1638  pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1640  snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1641 
1643 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1344
size_t pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t)
Definition: strftime.c:128
Definition: lexi.c:60
text * cstring_to_text(const char *s)
Definition: varlena.c:182

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

◆ timestamp2timestamptz()

static TimestampTz timestamp2timestamptz ( Timestamp  timestamp)
static

Definition at line 5613 of file timestamp.c.

5614 {
5616 }
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5561

References timestamp2timestamptz_opt_overflow().

Referenced by make_timestamptz(), and timestamp_timestamptz().

◆ timestamp2timestamptz_opt_overflow()

TimestampTz timestamp2timestamptz_opt_overflow ( Timestamp  timestamp,
int *  overflow 
)

Definition at line 5561 of file timestamp.c.

5562 {
5563  TimestampTz result;
5564  struct pg_tm tt,
5565  *tm = &tt;
5566  fsec_t fsec;
5567  int tz;
5568 
5569  if (overflow)
5570  *overflow = 0;
5571 
5573  return timestamp;
5574 
5575  /* We don't expect this to fail, but check it pro forma */
5576  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
5577  {
5579 
5580  result = dt2local(timestamp, -tz);
5581 
5582  if (IS_VALID_TIMESTAMP(result))
5583  {
5584  return result;
5585  }
5586  else if (overflow)
5587  {
5588  if (result < MIN_TIMESTAMP)
5589  {
5590  *overflow = -1;
5591  TIMESTAMP_NOBEGIN(result);
5592  }
5593  else
5594  {
5595  *overflow = 1;
5596  TIMESTAMP_NOEND(result);
5597  }
5598  return result;
5599  }
5600  }
5601 
5602  ereport(ERROR,
5603  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5604  errmsg("timestamp out of range")));
5605 
5606  return 0;
5607 }
#define MIN_TIMESTAMP
Definition: timestamp.h:230

References DetermineTimeZoneOffset(), dt2local(), ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, MIN_TIMESTAMP, session_timezone, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, and tm.

Referenced by 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 1830 of file timestamp.c.

1831 {
1832  Timestamp date;
1833  Timestamp time;
1834  pg_time_t utime;
1835 
1836  /* Use session timezone if caller asks for default */
1837  if (attimezone == NULL)
1838  attimezone = session_timezone;
1839 
1840  time = dt;
1841  TMODULO(time, date, USECS_PER_DAY);
1842 
1843  if (time < INT64CONST(0))
1844  {
1845  time += USECS_PER_DAY;
1846  date -= 1;
1847  }
1848 
1849  /* add offset to go from J2000 back to standard Julian date */
1851 
1852  /* Julian day routine does not work for negative Julian days */
1853  if (date < 0 || date > (Timestamp) INT_MAX)
1854  return -1;
1855 
1856  j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1857  dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1858 
1859  /* Done if no TZ conversion wanted */
1860  if (tzp == NULL)
1861  {
1862  tm->tm_isdst = -1;
1863  tm->tm_gmtoff = 0;
1864  tm->tm_zone = NULL;
1865  if (tzn != NULL)
1866  *tzn = NULL;
1867  return 0;
1868  }
1869 
1870  /*
1871  * If the time falls within the range of pg_time_t, use pg_localtime() to
1872  * rotate to the local time zone.
1873  *
1874  * First, convert to an integral timestamp, avoiding possibly
1875  * platform-specific roundoff-in-wrong-direction errors, and adjust to
1876  * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1877  * coding avoids hardwiring any assumptions about the width of pg_time_t,
1878  * so it should behave sanely on machines without int64.
1879  */
1880  dt = (dt - *fsec) / USECS_PER_SEC +
1882  utime = (pg_time_t) dt;
1883  if ((Timestamp) utime == dt)
1884  {
1885  struct pg_tm *tx = pg_localtime(&utime, attimezone);
1886 
1887  tm->tm_year = tx->tm_year + 1900;
1888  tm->tm_mon = tx->tm_mon + 1;
1889  tm->tm_mday = tx->tm_mday;
1890  tm->tm_hour = tx->tm_hour;
1891  tm->tm_min = tx->tm_min;
1892  tm->tm_sec = tx->tm_sec;
1893  tm->tm_isdst = tx->tm_isdst;
1894  tm->tm_gmtoff = tx->tm_gmtoff;
1895  tm->tm_zone = tx->tm_zone;
1896  *tzp = -tm->tm_gmtoff;
1897  if (tzn != NULL)
1898  *tzn = tm->tm_zone;
1899  }
1900  else
1901  {
1902  /*
1903  * When out of range of pg_time_t, treat as GMT
1904  */
1905  *tzp = 0;
1906  /* Mark this as *no* time zone available */
1907  tm->tm_isdst = -1;
1908  tm->tm_gmtoff = 0;
1909  tm->tm_zone = NULL;
1910  if (tzn != NULL)
1911  *tzn = NULL;
1912  }
1913 
1914  return 0;
1915 }
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1803
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(), 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(), GetCurrentTimeUsec(), JsonEncodeDateTime(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_fmt_asc(), PGTYPEStimestamp_to_asc(), timestamp2timestamptz_opt_overflow(), timestamp_age(), timestamp_date(), timestamp_out(), timestamp_part_common(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_age(), timestamptz_date(), timestamptz_out(), timestamptz_part_common(), timestamptz_pl_interval_internal(), timestamptz_recv(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), timestamptz_to_str(), timestamptz_trunc_internal(), timestamptz_zone(), and timetz_zone().

◆ timestamp_age()

Datum timestamp_age ( PG_FUNCTION_ARGS  )

Definition at line 3696 of file timestamp.c.

3697 {
3698  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
3699  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
3700  Interval *result;
3701  fsec_t fsec1,
3702  fsec2;
3703  struct pg_itm tt,
3704  *tm = &tt;
3705  struct pg_tm tt1,
3706  *tm1 = &tt1;
3707  struct pg_tm tt2,
3708  *tm2 = &tt2;
3709 
3710  result = (Interval *) palloc(sizeof(Interval));
3711 
3712  if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
3713  timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
3714  {
3715  /* form the symbolic difference */
3716  tm->tm_usec = fsec1 - fsec2;
3717  tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3718  tm->tm_min = tm1->tm_min - tm2->tm_min;
3719  tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3720  tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3721  tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3722  tm->tm_year = tm1->tm_year - tm2->tm_year;
3723 
3724  /* flip sign if necessary... */
3725  if (dt1 < dt2)
3726  {
3727  tm->tm_usec = -tm->tm_usec;
3728  tm->tm_sec = -tm->tm_sec;
3729  tm->tm_min = -tm->tm_min;
3730  tm->tm_hour = -tm->tm_hour;
3731  tm->tm_mday = -tm->tm_mday;
3732  tm->tm_mon = -tm->tm_mon;
3733  tm->tm_year = -tm->tm_year;
3734  }
3735 
3736  /* propagate any negative fields into the next higher field */
3737  while (tm->tm_usec < 0)
3738  {
3739  tm->tm_usec += USECS_PER_SEC;
3740  tm->tm_sec--;
3741  }
3742 
3743  while (tm->tm_sec < 0)
3744  {
3746  tm->tm_min--;
3747  }
3748 
3749  while (tm->tm_min < 0)
3750  {
3751  tm->tm_min += MINS_PER_HOUR;
3752  tm->tm_hour--;
3753  }
3754 
3755  while (tm->tm_hour < 0)
3756  {
3757  tm->tm_hour += HOURS_PER_DAY;
3758  tm->tm_mday--;
3759  }
3760 
3761  while (tm->tm_mday < 0)
3762  {
3763  if (dt1 < dt2)
3764  {
3765  tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
3766  tm->tm_mon--;
3767  }
3768  else
3769  {
3770  tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
3771  tm->tm_mon--;
3772  }
3773  }
3774 
3775  while (tm->tm_mon < 0)
3776  {
3778  tm->tm_year--;
3779  }
3780 
3781  /* recover sign if necessary... */
3782  if (dt1 < dt2)
3783  {
3784  tm->tm_usec = -tm->tm_usec;
3785  tm->tm_sec = -tm->tm_sec;
3786  tm->tm_min = -tm->tm_min;
3787  tm->tm_hour = -tm->tm_hour;
3788  tm->tm_mday = -tm->tm_mday;
3789  tm->tm_mon = -tm->tm_mon;
3790  tm->tm_year = -tm->tm_year;
3791  }
3792 
3793  if (itm2interval(tm, result) != 0)
3794  ereport(ERROR,
3795  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3796  errmsg("interval out of range")));
3797  }
3798  else
3799  ereport(ERROR,
3800  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3801  errmsg("timestamp out of range")));
3802 
3803  PG_RETURN_INTERVAL_P(result);
3804 }
const int day_tab[2][13]
Definition: datetime.c:77
#define HOURS_PER_DAY
Definition: timestamp.h:117
#define isleap(y)
Definition: datetime.h:271

References day_tab, ereport, errcode(), errmsg(), ERROR, HOURS_PER_DAY, isleap, itm2interval(), MINS_PER_HOUR, MONTHS_PER_YEAR, palloc(), PG_GETARG_TIMESTAMP, PG_RETURN_INTERVAL_P, SECS_PER_MINUTE, timestamp2tm(), 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_bin()

Datum timestamp_bin ( PG_FUNCTION_ARGS  )

Definition at line 3940 of file timestamp.c.

3941 {
3942  Interval *stride = PG_GETARG_INTERVAL_P(0);
3944  Timestamp origin = PG_GETARG_TIMESTAMP(2);
3945  Timestamp result,
3946  tm_diff,
3947  stride_usecs,
3948  tm_delta;
3949 
3952 
3953  if (TIMESTAMP_NOT_FINITE(origin))
3954  ereport(ERROR,
3955  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3956  errmsg("origin out of range")));
3957 
3958  if (stride->month != 0)
3959  ereport(ERROR,
3960  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3961  errmsg("timestamps cannot be binned into intervals containing months or years")));
3962 
3963  stride_usecs = stride->day * USECS_PER_DAY + stride->time;
3964 
3965  if (stride_usecs <= 0)
3966  ereport(ERROR,
3967  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3968  errmsg("stride must be greater than zero")));
3969 
3970  tm_diff = timestamp - origin;
3971  tm_delta = tm_diff - tm_diff % stride_usecs;
3972 
3973  /*
3974  * Make sure the returned timestamp is at the start of the bin, even if
3975  * the origin is in the future.
3976  */
3977  if (origin > timestamp && stride_usecs > 1)
3978  tm_delta -= stride_usecs;
3979 
3980  result = origin + tm_delta;
3981 
3982  PG_RETURN_TIMESTAMP(result);
3983 }

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, Interval::time, TIMESTAMP_NOT_FINITE, and USECS_PER_DAY.

◆ timestamp_cmp()

Datum timestamp_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2185 of file timestamp.c.

2186 {
2187  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2188  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2189 
2191 }

References PG_GETARG_TIMESTAMP, PG_RETURN_INT32, and timestamp_cmp_internal().

Referenced by compareDatetime(), and gbt_tskey_cmp().

◆ timestamp_cmp_internal()

int timestamp_cmp_internal ( Timestamp  dt1,
Timestamp  dt2 
)

◆ timestamp_cmp_timestamptz()

Datum timestamp_cmp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2315 of file timestamp.c.

2316 {
2317  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2319 
2321 }
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition: timestamp.c:2240

References 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 2240 of file timestamp.c.

2241 {
2242  TimestampTz dt1;
2243  int overflow;
2244 
2245  dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2246  if (overflow > 0)
2247  {
2248  /* dt1 is larger than any finite timestamp, but less than infinity */
2249  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2250  }
2251  if (overflow < 0)
2252  {
2253  /* dt1 is less than any finite timestamp, but more than -infinity */
2254  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2255  }
2256 
2257  return timestamptz_cmp_internal(dt1, dt2);
2258 }
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:131

References timestamp2timestamptz_opt_overflow(), 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_eq()

Datum timestamp_eq ( PG_FUNCTION_ARGS  )

Definition at line 2131 of file timestamp.c.

2132 {
2133  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2134  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2135 
2136  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
2137 }

References PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tseq().

◆ timestamp_eq_timestamptz()

Datum timestamp_eq_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2261 of file timestamp.c.

2262 {
2263  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2265 
2266  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) == 0);
2267 }

References PG_GETARG_TIMESTAMP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamp_cmp_timestamptz_internal().

◆ timestamp_fastcmp()

static int timestamp_fastcmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 2196 of file timestamp.c.

2197 {
2200 
2201  return timestamp_cmp_internal(a, b);
2202 }
int y
Definition: isn.c:72
int b
Definition: isn.c:70
int x
Definition: isn.c:71
int a
Definition: isn.c:69

References a, b, DatumGetTimestamp(), timestamp_cmp_internal(), x, and y.

Referenced by timestamp_sortsupport().

◆ timestamp_finite()

Datum timestamp_finite ( PG_FUNCTION_ARGS  )

Definition at line 2064 of file timestamp.c.

2065 {
2067 
2069 }

References PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and TIMESTAMP_NOT_FINITE.

◆ timestamp_ge()

Datum timestamp_ge ( PG_FUNCTION_ARGS  )

Definition at line 2176 of file timestamp.c.

2177 {
2178  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2179  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2180 
2181  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
2182 }

References PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsge().

◆ timestamp_ge_timestamptz()

Datum timestamp_ge_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2306 of file timestamp.c.

2307 {
2308  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2310 
2311  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) >= 0);
2312 }

References PG_GETARG_TIMESTAMP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamp_cmp_timestamptz_internal().

◆ timestamp_gt()

Datum timestamp_gt ( PG_FUNCTION_ARGS  )

Definition at line 2158 of file timestamp.c.

2159 {
2160  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2161  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2162 
2163  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
2164 }

References PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsgt().

◆ timestamp_gt_timestamptz()

Datum timestamp_gt_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2288 of file timestamp.c.

2289 {
2290  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2292 
2293  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) > 0);
2294 }

References PG_GETARG_TIMESTAMP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, and timestamp_cmp_timestamptz_internal().

◆ timestamp_hash()

Datum timestamp_hash ( PG_FUNCTION_ARGS  )

Definition at line 2224 of file timestamp.c.

2225 {
2226  return hashint8(fcinfo);
2227 }

References hashint8().

◆ timestamp_hash_extended()

Datum timestamp_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2230 of file timestamp.c.

2231 {
2232  return hashint8extended(fcinfo);
2233 }

References hashint8extended().

◆ timestamp_in()

Datum timestamp_in ( PG_FUNCTION_ARGS  )

Definition at line 148 of file timestamp.c.

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

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

Referenced by moddatetime().

◆ timestamp_izone()

Datum timestamp_izone ( PG_FUNCTION_ARGS  )

Definition at line 5493 of file timestamp.c.

5494 {
5497  TimestampTz result;
5498  int tz;
5499 
5502 
5503  if (zone->month != 0 || zone->day != 0)
5504  ereport(ERROR,
5505  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5506  errmsg("interval time zone \"%s\" must not include months or days",
5508