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_typmod_check (bool istz, int32 typmod)
 
static int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
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 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)
 
Datum current_timestamp (PG_FUNCTION_ARGS)
 
Datum sql_localtimestamp (PG_FUNCTION_ARGS)
 
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)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (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)
 
Datum generate_series_timestamptz (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 1332 of file timestamp.c.

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

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

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

Referenced by current_timestamp(), parse_datetime(), sql_localtimestamp(), timestamp_in(), timestamp_recv(), timestamp_scale(), timestamptz_in(), timestamptz_recv(), timestamptz_scale(), and to_timestamp().

◆ anytimestamp_typmod_check()

static int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)
static

Definition at line 85 of file timestamp.c.

86 {
87  if (typmod < 0)
88  ereport(ERROR,
89  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
90  errmsg("TIMESTAMP(%d)%s precision must not be negative",
91  typmod, (istz ? " WITH TIME ZONE" : ""))));
92  if (typmod > MAX_TIMESTAMP_PRECISION)
93  {
95  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
96  errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
97  typmod, (istz ? " WITH TIME ZONE" : ""),
99  typmod = MAX_TIMESTAMP_PRECISION;
100  }
101 
102  return typmod;
103 }
#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(), current_timestamp(), and sql_localtimestamp().

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 106 of file timestamp.c.

107 {
108  int32 *tl;
109  int n;
110 
111  tl = ArrayGetIntegerTypmods(ta, &n);
112 
113  /*
114  * we're not too tense about good error message here because grammar
115  * shouldn't allow wrong number of modifiers for TIMESTAMP
116  */
117  if (n != 1)
118  ereport(ERROR,
119  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
120  errmsg("invalid type modifier")));
121 
122  return anytimestamp_typmod_check(istz, tl[0]);
123 }
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:254
static int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:85
signed int int32
Definition: c.h:430

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

128 {
129  const char *tz = istz ? " with time zone" : " without time zone";
130 
131  if (typmod >= 0)
132  return psprintf("(%d)%s", (int) typmod, tz);
133  else
134  return pstrdup(tz);
135 }
char * pstrdup(const char *in)
Definition: mcxt.c:1483
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 1561 of file timestamp.c.

1562 {
1564 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1585
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

◆ current_timestamp()

Datum current_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1603 of file timestamp.c.

1604 {
1605  TimestampTz ts;
1606  int32 typmod = -1;
1607 
1608  if (!PG_ARGISNULL(0))
1609  {
1610  typmod = PG_GETARG_INT32(0);
1611  anytimestamp_typmod_check(true, typmod);
1612  }
1613 
1615  if (typmod >= 0)
1616  AdjustTimestampForTypmod(&ts, typmod, NULL);
1617  return TimestampTzGetDatum(ts);
1618 }
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition: timestamp.c:348
int64 TimestampTz
Definition: timestamp.h:39
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:854

References AdjustTimestampForTypmod(), anytimestamp_typmod_check(), GetCurrentTransactionStartTimestamp(), PG_ARGISNULL, PG_GETARG_INT32, and TimestampTzGetDatum().

Referenced by GetSnapshotCurrentTimestamp(), and pgstat_report_activity().

◆ date2isoweek()

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

Definition at line 4505 of file timestamp.c.

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

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

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

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

4618 {
4619  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4620 }
int isoweek2j(int year, int week)
Definition: timestamp.c:4454
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4560

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

Referenced by DCH_to_char().

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 2063 of file timestamp.c.

2064 {
2065  dt -= (timezone * USECS_PER_SEC);
2066  return dt;
2067 }
#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 1815 of file timestamp.c.

1816 {
1817  TimeOffset time;
1818 
1819  time = jd;
1820 
1821  *hour = time / USECS_PER_HOUR;
1822  time -= (*hour) * USECS_PER_HOUR;
1823  *min = time / USECS_PER_MINUTE;
1824  time -= (*min) * USECS_PER_MINUTE;
1825  *sec = time / USECS_PER_SEC;
1826  *fsec = time - (*sec * USECS_PER_SEC);
1827 } /* 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 1538 of file timestamp.c.

1539 {
1540  if (TIMESTAMP_IS_NOBEGIN(dt))
1541  strcpy(str, EARLY);
1542  else if (TIMESTAMP_IS_NOEND(dt))
1543  strcpy(str, LATE);
1544  else /* shouldn't happen */
1545  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1546 }
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:159
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:154
#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 5408 of file timestamp.c.

5409 {
5410  return interval_part_common(fcinfo, true);
5411 }
static Datum interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:5237

References interval_part_common().

◆ extract_timestamp()

Datum extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 4955 of file timestamp.c.

4956 {
4957  return timestamp_part_common(fcinfo, true);
4958 }
static Datum timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4691

References timestamp_part_common().

◆ extract_timestamptz()

Datum extract_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5227 of file timestamp.c.

5228 {
5229  return timestamptz_part_common(fcinfo, true);
5230 }
static Datum timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4964

References timestamptz_part_common().

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 721 of file timestamp.c.

722 {
723  float8 seconds = PG_GETARG_FLOAT8(0);
724  TimestampTz result;
725 
726  /* Deal with NaN and infinite inputs ... */
727  if (isnan(seconds))
728  ereport(ERROR,
729  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
730  errmsg("timestamp cannot be NaN")));
731 
732  if (isinf(seconds))
733  {
734  if (seconds < 0)
735  TIMESTAMP_NOBEGIN(result);
736  else
737  TIMESTAMP_NOEND(result);
738  }
739  else
740  {
741  /* Out of range? */
742  if (seconds <
744  || seconds >=
746  ereport(ERROR,
747  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
748  errmsg("timestamp out of range: \"%g\"", seconds)));
749 
750  /* Convert UNIX epoch to Postgres epoch */
752 
753  seconds = rint(seconds * USECS_PER_SEC);
754  result = (int64) seconds;
755 
756  /* Recheck in case roundoff produces something just out of range */
757  if (!IS_VALID_TIMESTAMP(result))
758  ereport(ERROR,
759  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
760  errmsg("timestamp out of range: \"%g\"",
761  PG_GETARG_FLOAT8(0))));
762  }
763 
764  PG_RETURN_TIMESTAMP(result);
765 }
#define DATETIME_MIN_JULIAN
Definition: timestamp.h:218
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:220
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:234
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:201
#define SECS_PER_DAY
Definition: timestamp.h:125
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:202
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:156
#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 5805 of file timestamp.c.

5806 {
5807  FuncCallContext *funcctx;
5809  Timestamp result;
5810 
5811  /* stuff done only on the first call of the function */
5812  if (SRF_IS_FIRSTCALL())
5813  {
5814  Timestamp start = PG_GETARG_TIMESTAMP(0);
5815  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5816  Interval *step = PG_GETARG_INTERVAL_P(2);
5817  MemoryContext oldcontext;
5818  const Interval interval_zero = {0};
5819 
5820  /* create a function context for cross-call persistence */
5821  funcctx = SRF_FIRSTCALL_INIT();
5822 
5823  /*
5824  * switch to memory context appropriate for multiple function calls
5825  */
5826  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5827 
5828  /* allocate memory for user context */
5831 
5832  /*
5833  * Use fctx to keep state from call to call. Seed current with the
5834  * original start value
5835  */
5836  fctx->current = start;
5837  fctx->finish = finish;
5838  fctx->step = *step;
5839 
5840  /* Determine sign of the interval */
5841  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5842 
5843  if (fctx->step_sign == 0)
5844  ereport(ERROR,
5845  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5846  errmsg("step size cannot equal zero")));
5847 
5848  funcctx->user_fctx = fctx;
5849  MemoryContextSwitchTo(oldcontext);
5850  }
5851 
5852  /* stuff done on every call of the function */
5853  funcctx = SRF_PERCALL_SETUP();
5854 
5855  /*
5856  * get the saved state and use current as the result for this iteration
5857  */
5858  fctx = funcctx->user_fctx;
5859  result = fctx->current;
5860 
5861  if (fctx->step_sign > 0 ?
5862  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5863  timestamp_cmp_internal(result, fctx->finish) >= 0)
5864  {
5865  /* increment current in preparation for next iteration */
5867  TimestampGetDatum(fctx->current),
5868  PointerGetDatum(&fctx->step)));
5869 
5870  /* do when there is more left to send */
5871  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5872  }
5873  else
5874  {
5875  /* do when there is no more left */
5876  SRF_RETURN_DONE(funcctx);
5877  }
5878 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2137
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2923
static int interval_cmp_internal(const Interval *interval1, const Interval *interval2)
Definition: timestamp.c:2432
int64 Timestamp
Definition: timestamp.h:38
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:644
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:303
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:307
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:305
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:327
void * palloc(Size size)
Definition: mcxt.c:1199
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
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 5884 of file timestamp.c.

5885 {
5886  FuncCallContext *funcctx;
5888  TimestampTz result;
5889 
5890  /* stuff done only on the first call of the function */
5891  if (SRF_IS_FIRSTCALL())
5892  {
5894  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5895  Interval *step = PG_GETARG_INTERVAL_P(2);
5896  MemoryContext oldcontext;
5897  const Interval interval_zero = {0};
5898 
5899  /* create a function context for cross-call persistence */
5900  funcctx = SRF_FIRSTCALL_INIT();
5901 
5902  /*
5903  * switch to memory context appropriate for multiple function calls
5904  */
5905  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5906 
5907  /* allocate memory for user context */
5910 
5911  /*
5912  * Use fctx to keep state from call to call. Seed current with the
5913  * original start value
5914  */
5915  fctx->current = start;
5916  fctx->finish = finish;
5917  fctx->step = *step;
5918 
5919  /* Determine sign of the interval */
5920  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5921 
5922  if (fctx->step_sign == 0)
5923  ereport(ERROR,
5924  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5925  errmsg("step size cannot equal zero")));
5926 
5927  funcctx->user_fctx = fctx;
5928  MemoryContextSwitchTo(oldcontext);
5929  }
5930 
5931  /* stuff done on every call of the function */
5932  funcctx = SRF_PERCALL_SETUP();
5933 
5934  /*
5935  * get the saved state and use current as the result for this iteration
5936  */
5937  fctx = funcctx->user_fctx;
5938  result = fctx->current;
5939 
5940  if (fctx->step_sign > 0 ?
5941  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5942  timestamp_cmp_internal(result, fctx->finish) >= 0)
5943  {
5944  /* increment current in preparation for next iteration */
5947  PointerGetDatum(&fctx->step)));
5948 
5949  /* do when there is more left to send */
5950  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5951  }
5952  else
5953  {
5954  /* do when there is no more left */
5955  SRF_RETURN_DONE(funcctx);
5956  }
5957 }
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3028
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:64
static TimestampTz DatumGetTimestampTz(Datum X)
Definition: timestamp.h:34

References generate_series_timestamptz_fctx::current, DatumGetTimestampTz(), DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, generate_series_timestamptz_fctx::finish, interval_cmp_internal(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PointerGetDatum(), 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(), TimestampTzGetDatum(), and FuncCallContext::user_fctx.

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1585 of file timestamp.c.

1586 {
1587  TimestampTz result;
1588  struct timeval tp;
1589 
1590  gettimeofday(&tp, NULL);
1591 
1592  result = (TimestampTz) tp.tv_sec -
1594  result = (result * USECS_PER_SEC) + tp.tv_usec;
1595 
1596  return result;
1597 }
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(), ApplyWorkerMain(), asyncQueueFillWarning(), autoprewarm_main(), AutoVacLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), begin_startup_progress_phase(), 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(), drandom(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), InitProcessGlobals(), KnownAssignedXidsCompress(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), PerformWalRecovery(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), 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_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(), 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 2095 of file timestamp.c.

2096 {
2097  struct pg_tm *t0;
2098  pg_time_t epoch = 0;
2099 
2100  t0 = pg_gmtime(&epoch);
2101 
2102  if (t0 == NULL)
2103  elog(ERROR, "could not convert epoch to timestamp: %m");
2104 
2105  tm->tm_year = t0->tm_year;
2106  tm->tm_mon = t0->tm_mon;
2107  tm->tm_mday = t0->tm_mday;
2108  tm->tm_hour = t0->tm_hour;
2109  tm->tm_min = t0->tm_min;
2110  tm->tm_sec = t0->tm_sec;
2111 
2112  tm->tm_year += 1900;
2113  tm->tm_mon++;
2114 }
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().

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

Definition at line 3462 of file timestamp.c.

3463 {
3465  Interval *base = PG_GETARG_INTERVAL_P(1);
3466  Interval *offset = PG_GETARG_INTERVAL_P(2);
3467  bool sub = PG_GETARG_BOOL(3);
3468  bool less = PG_GETARG_BOOL(4);
3469  Interval *sum;
3470 
3471  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3472  ereport(ERROR,
3473  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3474  errmsg("invalid preceding or following size in window function")));
3475 
3476  /* We don't currently bother to avoid overflow hazards here */
3477  if (sub)
3479  IntervalPGetDatum(base),
3480  IntervalPGetDatum(offset)));
3481  else
3483  IntervalPGetDatum(base),
3484  IntervalPGetDatum(offset)));
3485 
3486  if (less)
3488  else
3490 }
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2410
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3221
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3187
#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 3431 of file timestamp.c.

3432 {
3434  Timestamp base = PG_GETARG_TIMESTAMP(1);
3435  Interval *offset = PG_GETARG_INTERVAL_P(2);
3436  bool sub = PG_GETARG_BOOL(3);
3437  bool less = PG_GETARG_BOOL(4);
3438  Timestamp sum;
3439 
3440  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3441  ereport(ERROR,
3442  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3443  errmsg("invalid preceding or following size in window function")));
3444 
3445  /* We don't currently bother to avoid overflow hazards here */
3446  if (sub)
3448  TimestampGetDatum(base),
3449  IntervalPGetDatum(offset)));
3450  else
3452  TimestampGetDatum(base),
3453  IntervalPGetDatum(offset)));
3454 
3455  if (less)
3456  PG_RETURN_BOOL(val <= sum);
3457  else
3458  PG_RETURN_BOOL(val >= sum);
3459 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3002

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

3401 {
3404  Interval *offset = PG_GETARG_INTERVAL_P(2);
3405  bool sub = PG_GETARG_BOOL(3);
3406  bool less = PG_GETARG_BOOL(4);
3407  TimestampTz sum;
3408 
3409  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3410  ereport(ERROR,
3411  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3412  errmsg("invalid preceding or following size in window function")));
3413 
3414  /* We don't currently bother to avoid overflow hazards here */
3415  if (sub)
3417  TimestampTzGetDatum(base),
3418  IntervalPGetDatum(offset)));
3419  else
3421  TimestampTzGetDatum(base),
3422  IntervalPGetDatum(offset)));
3423 
3424  if (less)
3425  PG_RETURN_BOOL(val <= sum);
3426  else
3427  PG_RETURN_BOOL(val >= sum);
3428 }
Datum timestamptz_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3112

References DatumGetTimestampTz(), DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, int128_compare(), int64_to_int128(), interval_cmp_value(), IntervalPGetDatum(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, timestamptz_mi_interval(), timestamptz_pl_interval(), TimestampTzGetDatum(), and val.

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 1988 of file timestamp.c.

1989 {
1990  TimeOffset time;
1991  TimeOffset tfrac;
1992 
1993  itm->tm_year = span.month / MONTHS_PER_YEAR;
1994  itm->tm_mon = span.month % MONTHS_PER_YEAR;
1995  itm->tm_mday = span.day;
1996  time = span.time;
1997 
1998  tfrac = time / USECS_PER_HOUR;
1999  time -= tfrac * USECS_PER_HOUR;
2000  itm->tm_hour = tfrac;
2001  tfrac = time / USECS_PER_MINUTE;
2002  time -= tfrac * USECS_PER_MINUTE;
2003  itm->tm_min = (int) tfrac;
2004  tfrac = time / USECS_PER_SEC;
2005  time -= tfrac * USECS_PER_SEC;
2006  itm->tm_sec = (int) tfrac;
2007  itm->tm_usec = (int) time;
2008 }
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 3504 of file timestamp.c.

3505 {
3506  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3508  Datum *transdatums;
3509  int ndatums;
3510  Interval sumX,
3511  N;
3512  Interval *newsum;
3513  ArrayType *result;
3514 
3515  deconstruct_array(transarray,
3516  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3517  &transdatums, NULL, &ndatums);
3518  if (ndatums != 2)
3519  elog(ERROR, "expected 2-element interval array");
3520 
3521  sumX = *(DatumGetIntervalP(transdatums[0]));
3522  N = *(DatumGetIntervalP(transdatums[1]));
3523 
3525  IntervalPGetDatum(&sumX),
3527  N.time += 1;
3528 
3529  transdatums[0] = IntervalPGetDatum(newsum);
3530  transdatums[1] = IntervalPGetDatum(&N);
3531 
3532  result = construct_array(transdatums, 2,
3533  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3534 
3535  PG_RETURN_ARRAYTYPE_P(result);
3536 }
#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:3343
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3602
#define newval
uintptr_t Datum
Definition: postgres.h:412

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

3589 {
3590  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3592  Datum *transdatums;
3593  int ndatums;
3594  Interval sumX,
3595  N;
3596  Interval *newsum;
3597  ArrayType *result;
3598 
3599  deconstruct_array(transarray,
3600  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3601  &transdatums, NULL, &ndatums);
3602  if (ndatums != 2)
3603  elog(ERROR, "expected 2-element interval array");
3604 
3605  sumX = *(DatumGetIntervalP(transdatums[0]));
3606  N = *(DatumGetIntervalP(transdatums[1]));
3607 
3609  IntervalPGetDatum(&sumX),
3611  N.time -= 1;
3612 
3613  transdatums[0] = IntervalPGetDatum(newsum);
3614  transdatums[1] = IntervalPGetDatum(&N);
3615 
3616  result = construct_array(transdatums, 2,
3617  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3618 
3619  PG_RETURN_ARRAYTYPE_P(result);
3620 }

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

3624 {
3625  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3626  Datum *transdatums;
3627  int ndatums;
3628  Interval sumX,
3629  N;
3630 
3631  deconstruct_array(transarray,
3632  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3633  &transdatums, NULL, &ndatums);
3634  if (ndatums != 2)
3635  elog(ERROR, "expected 2-element interval array");
3636 
3637  sumX = *(DatumGetIntervalP(transdatums[0]));
3638  N = *(DatumGetIntervalP(transdatums[1]));
3639 
3640  /* SQL defines AVG of no values to be NULL */
3641  if (N.time == 0)
3642  PG_RETURN_NULL();
3643 
3645  IntervalPGetDatum(&sumX),
3646  Float8GetDatum((double) N.time));
3647 }
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3348
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1758
#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 2495 of file timestamp.c.

2496 {
2497  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2498  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2499 
2500  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2501 }
#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 2410 of file timestamp.c.

2411 {
2412  INT128 span;
2413  int64 days;
2414 
2415  /*
2416  * Combine the month and day fields into an integral number of days.
2417  * Because the inputs are int32, int64 arithmetic suffices here.
2418  */
2419  days = interval->month * INT64CONST(30);
2420  days += interval->day;
2421 
2422  /* Widen time field to 128 bits */
2423  span = int64_to_int128(interval->time);
2424 
2425  /* Scale up days to microseconds, forming a 128-bit product */
2427 
2428  return span;
2429 }
const char *const days[]
Definition: datetime.c:85
#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 3539 of file timestamp.c.

3540 {
3541  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3542  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3543  Datum *transdatums1;
3544  Datum *transdatums2;
3545  int ndatums1;
3546  int ndatums2;
3547  Interval sum1,
3548  N1;
3549  Interval sum2,
3550  N2;
3551 
3552  Interval *newsum;
3553  ArrayType *result;
3554 
3555  deconstruct_array(transarray1,
3556  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3557  &transdatums1, NULL, &ndatums1);
3558  if (ndatums1 != 2)
3559  elog(ERROR, "expected 2-element interval array");
3560 
3561  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3562  N1 = *(DatumGetIntervalP(transdatums1[1]));
3563 
3564  deconstruct_array(transarray2,
3565  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3566  &transdatums2, NULL, &ndatums2);
3567  if (ndatums2 != 2)
3568  elog(ERROR, "expected 2-element interval array");
3569 
3570  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3571  N2 = *(DatumGetIntervalP(transdatums2[1]));
3572 
3574  IntervalPGetDatum(&sum1),
3575  IntervalPGetDatum(&sum2)));
3576  N1.time += N2.time;
3577 
3578  transdatums1[0] = IntervalPGetDatum(newsum);
3579  transdatums1[1] = IntervalPGetDatum(&N1);
3580 
3581  result = construct_array(transdatums1, 2,
3582  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3583 
3584  PG_RETURN_ARRAYTYPE_P(result);
3585 }

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

3349 {
3350  Interval *span = PG_GETARG_INTERVAL_P(0);
3351  float8 factor = PG_GETARG_FLOAT8(1);
3352  double month_remainder_days,
3353  sec_remainder;
3354  int32 orig_month = span->month,
3355  orig_day = span->day;
3356  Interval *result;
3357 
3358  result = (Interval *) palloc(sizeof(Interval));
3359 
3360  if (factor == 0.0)
3361  ereport(ERROR,
3362  (errcode(ERRCODE_DIVISION_BY_ZERO),
3363  errmsg("division by zero")));
3364 
3365  result->month = (int32) (span->month / factor);
3366  result->day = (int32) (span->day / factor);
3367 
3368  /*
3369  * Fractional months full days into days. See comment in interval_mul().
3370  */
3371  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3372  month_remainder_days = TSROUND(month_remainder_days);
3373  sec_remainder = (orig_day / factor - result->day +
3374  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3375  sec_remainder = TSROUND(sec_remainder);
3376  if (fabs(sec_remainder) >= SECS_PER_DAY)
3377  {
3378  result->day += (int) (sec_remainder / SECS_PER_DAY);
3379  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3380  }
3381 
3382  /* cascade units down */
3383  result->day += (int32) month_remainder_days;
3384  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3385 
3386  PG_RETURN_INTERVAL_P(result);
3387 }
#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 2441 of file timestamp.c.

2442 {
2443  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2444  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2445 
2446  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2447 }

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

2085 {
2086  PG_RETURN_BOOL(true);
2087 }

References PG_RETURN_BOOL.

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2486 of file timestamp.c.

2487 {
2488  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2489  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2490 
2491  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2492 }

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

2469 {
2470  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2471  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2472 
2473  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2474 }

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

2512 {
2515  int64 span64;
2516 
2517  /*
2518  * Use only the least significant 64 bits for hashing. The upper 64 bits
2519  * seldom add any useful information, and besides we must do it like this
2520  * for compatibility with hashes calculated before use of INT128 was
2521  * introduced.
2522  */
2523  span64 = int128_to_int64(span);
2524 
2526 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:84
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define Int64GetDatumFast(X)
Definition: postgres.h:902

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

2530 {
2533  int64 span64;
2534 
2535  /* Same approach as interval_hash */
2536  span64 = int128_to_int64(span);
2537 
2539  PG_GETARG_DATUM(1));
2540 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:104

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

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

2881 {
2882  Interval *span = PG_GETARG_INTERVAL_P(0);
2883  Interval *result;
2884  int32 wholemonth;
2885 
2886  result = (Interval *) palloc(sizeof(Interval));
2887  result->month = span->month;
2888  result->day = span->day;
2889  result->time = span->time;
2890 
2891  wholemonth = result->day / DAYS_PER_MONTH;
2892  result->day -= wholemonth * DAYS_PER_MONTH;
2893  if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2894  ereport(ERROR,
2895  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2896  errmsg("interval out of range")));
2897 
2898  if (result->month > 0 && result->day < 0)
2899  {
2900  result->day += DAYS_PER_MONTH;
2901  result->month--;
2902  }
2903  else if (result->month < 0 && result->day > 0)
2904  {
2905  result->day -= DAYS_PER_MONTH;
2906  result->month++;
2907  }
2908 
2909  PG_RETURN_INTERVAL_P(result);
2910 }
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 2842 of file timestamp.c.

2843 {
2844  Interval *span = PG_GETARG_INTERVAL_P(0);
2845  Interval *result;
2846  TimeOffset wholeday;
2847 
2848  result = (Interval *) palloc(sizeof(Interval));
2849  result->month = span->month;
2850  result->day = span->day;
2851  result->time = span->time;
2852 
2853  TMODULO(result->time, wholeday, USECS_PER_DAY);
2854  if (pg_add_s32_overflow(result->day, wholeday, &result->day))
2855  ereport(ERROR,
2856  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2857  errmsg("interval out of range")));
2858 
2859  if (result->day > 0 && result->time < 0)
2860  {
2861  result->time += USECS_PER_DAY;
2862  result->day--;
2863  }
2864  else if (result->day < 0 && result->time > 0)
2865  {
2866  result->time -= USECS_PER_DAY;
2867  result->day++;
2868  }
2869 
2870  PG_RETURN_INTERVAL_P(result);
2871 }
#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 2766 of file timestamp.c.

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

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

3174 {
3175  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3176  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3177  Interval *result;
3178 
3179  if (interval_cmp_internal(interval1, interval2) > 0)
3180  result = interval1;
3181  else
3182  result = interval2;
3183  PG_RETURN_INTERVAL_P(result);
3184 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2477 of file timestamp.c.

2478 {
2479  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2480  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2481 
2482  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2483 }

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

2460 {
2461  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2462  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2463 
2464  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2465 }

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

3222 {
3223  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3224  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3225  Interval *result;
3226 
3227  result = (Interval *) palloc(sizeof(Interval));
3228 
3229  result->month = span1->month - span2->month;
3230  /* overflow check copied from int4mi */
3231  if (!SAMESIGN(span1->month, span2->month) &&
3232  !SAMESIGN(result->month, span1->month))
3233  ereport(ERROR,
3234  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3235  errmsg("interval out of range")));
3236 
3237  result->day = span1->day - span2->day;
3238  if (!SAMESIGN(span1->day, span2->day) &&
3239  !SAMESIGN(result->day, span1->day))
3240  ereport(ERROR,
3241  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3242  errmsg("interval out of range")));
3243 
3244  result->time = span1->time - span2->time;
3245  if (!SAMESIGN(span1->time, span2->time) &&
3246  !SAMESIGN(result->time, span1->time))
3247  ereport(ERROR,
3248  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3249  errmsg("interval out of range")));
3250 
3251  PG_RETURN_INTERVAL_P(result);
3252 }
#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 3261 of file timestamp.c.

3262 {
3263  Interval *span = PG_GETARG_INTERVAL_P(0);
3264  float8 factor = PG_GETARG_FLOAT8(1);
3265  double month_remainder_days,
3266  sec_remainder,
3267  result_double;
3268  int32 orig_month = span->month,
3269  orig_day = span->day;
3270  Interval *result;
3271 
3272  result = (Interval *) palloc(sizeof(Interval));
3273 
3274  result_double = span->month * factor;
3275  if (isnan(result_double) ||
3276  result_double > INT_MAX || result_double < INT_MIN)
3277  ereport(ERROR,
3278  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3279  errmsg("interval out of range")));
3280  result->month = (int32) result_double;
3281 
3282  result_double = span->day * factor;
3283  if (isnan(result_double) ||
3284  result_double > INT_MAX || result_double < INT_MIN)
3285  ereport(ERROR,
3286  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3287  errmsg("interval out of range")));
3288  result->day = (int32) result_double;
3289 
3290  /*
3291  * The above correctly handles the whole-number part of the month and day
3292  * products, but we have to do something with any fractional part
3293  * resulting when the factor is non-integral. We cascade the fractions
3294  * down to lower units using the conversion factors DAYS_PER_MONTH and
3295  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3296  * so by the representation. The user can choose to cascade up later,
3297  * using justify_hours and/or justify_days.
3298  */
3299 
3300  /*
3301  * Fractional months full days into days.
3302  *
3303  * Floating point calculation are inherently imprecise, so these
3304  * calculations are crafted to produce the most reliable result possible.
3305  * TSROUND() is needed to more accurately produce whole numbers where
3306  * appropriate.
3307  */
3308  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3309  month_remainder_days = TSROUND(month_remainder_days);
3310  sec_remainder = (orig_day * factor - result->day +
3311  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3312  sec_remainder = TSROUND(sec_remainder);
3313 
3314  /*
3315  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3316  * cascade from months and days. It might still be >24 if the combination
3317  * of cascade and the seconds factor operation itself.
3318  */
3319  if (fabs(sec_remainder) >= SECS_PER_DAY)
3320  {
3321  result->day += (int) (sec_remainder / SECS_PER_DAY);
3322  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3323  }
3324 
3325  /* cascade units down */
3326  result->day += (int32) month_remainder_days;
3327  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3328  if (isnan(result_double) || !FLOAT8_FITS_IN_INT64(result_double))
3329  ereport(ERROR,
3330  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3331  errmsg("interval out of range")));
3332  result->time = (int64) result_double;
3333 
3334  PG_RETURN_INTERVAL_P(result);
3335 }
#define FLOAT8_FITS_IN_INT64(num)
Definition: c.h:1049

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

2451 {
2452  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2453  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2454 
2455  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2456 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 960 of file timestamp.c.

961 {
962  Interval *span = PG_GETARG_INTERVAL_P(0);
963  char *result;
964  struct pg_itm tt,
965  *itm = &tt;
966  char buf[MAXDATELEN + 1];
967 
968  interval2itm(*span, itm);
970 
971  result = pstrdup(buf);
972  PG_RETURN_CSTRING(result);
973 }
void EncodeInterval(struct pg_itm *itm, int style, char *str)
Definition: datetime.c:4535
void interval2itm(Interval span, struct pg_itm *itm)
Definition: timestamp.c:1988
#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 5402 of file timestamp.c.

5403 {
5404  return interval_part_common(fcinfo, false);
5405 }

References interval_part_common().

◆ interval_part_common()

static Datum interval_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5237 of file timestamp.c.

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

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, generate_unaccent_rules::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 3187 of file timestamp.c.

3188 {
3189  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3190  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3191  Interval *result;
3192 
3193  result = (Interval *) palloc(sizeof(Interval));
3194 
3195  result->month = span1->month + span2->month;
3196  /* overflow check copied from int4pl */
3197  if (SAMESIGN(span1->month, span2->month) &&
3198  !SAMESIGN(result->month, span1->month))
3199  ereport(ERROR,
3200  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3201  errmsg("interval out of range")));
3202 
3203  result->day = span1->day + span2->day;
3204  if (SAMESIGN(span1->day, span2->day) &&
3205  !SAMESIGN(result->day, span1->day))
3206  ereport(ERROR,
3207  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3208  errmsg("interval out of range")));
3209 
3210  result->time = span1->time + span2->time;
3211  if (SAMESIGN(span1->time, span2->time) &&
3212  !SAMESIGN(result->time, span1->time))
3213  ereport(ERROR,
3214  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3215  errmsg("interval out of range")));
3216 
3217  PG_RETURN_INTERVAL_P(result);
3218 }

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

980 {
982 
983 #ifdef NOT_USED
984  Oid typelem = PG_GETARG_OID(1);
985 #endif
986  int32 typmod = PG_GETARG_INT32(2);
988 
989  interval = (Interval *) palloc(sizeof(Interval));
990 
992  interval->day = pq_getmsgint(buf, sizeof(interval->day));
994 
995  AdjustIntervalForTypmod(interval, typmod, NULL);
996 
998 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
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 1310 of file timestamp.c.

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

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

1005 {
1008 
1009  pq_begintypsend(&buf);
1011  pq_sendint32(&buf, interval->day);
1014 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
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 3158 of file timestamp.c.

3159 {
3160  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3161  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3162  Interval *result;
3163 
3164  /* use interval_cmp_internal to be sure this agrees with comparisons */
3165  if (interval_cmp_internal(interval1, interval2) < 0)
3166  result = interval1;
3167  else
3168  result = interval2;
3169  PG_RETURN_INTERVAL_P(result);
3170 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1247 of file timestamp.c.

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

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

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

◆ interval_um()

Datum interval_um ( PG_FUNCTION_ARGS  )

Definition at line 3129 of file timestamp.c.

3130 {
3132  Interval *result;
3133 
3134  result = (Interval *) palloc(sizeof(Interval));
3135 
3136  result->time = -interval->time;
3137  /* overflow check copied from int4um */
3138  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3139  ereport(ERROR,
3140  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3141  errmsg("interval out of range")));
3142  result->day = -interval->day;
3143  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3144  ereport(ERROR,
3145  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3146  errmsg("interval out of range")));
3147  result->month = -interval->month;
3148  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3149  ereport(ERROR,
3150  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3151  errmsg("interval out of range")));
3152 
3153  PG_RETURN_INTERVAL_P(result);
3154 }

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

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

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

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

1109 {
1110  int32 typmod = PG_GETARG_INT32(0);
1111  char *res = (char *) palloc(64);
1112  int fields;
1113  int precision;
1114  const char *fieldstr;
1115 
1116  if (typmod < 0)
1117  {
1118  *res = '\0';
1120  }
1121 
1122  fields = INTERVAL_RANGE(typmod);
1123  precision = INTERVAL_PRECISION(typmod);
1124 
1125  switch (fields)
1126  {
1127  case INTERVAL_MASK(YEAR):
1128  fieldstr = " year";
1129  break;
1130  case INTERVAL_MASK(MONTH):
1131  fieldstr = " month";
1132  break;
1133  case INTERVAL_MASK(DAY):
1134  fieldstr = " day";
1135  break;
1136  case INTERVAL_MASK(HOUR):
1137  fieldstr = " hour";
1138  break;
1139  case INTERVAL_MASK(MINUTE):
1140  fieldstr = " minute";
1141  break;
1142  case INTERVAL_MASK(SECOND):
1143  fieldstr = " second";
1144  break;
1146  fieldstr = " year to month";
1147  break;
1149  fieldstr = " day to hour";
1150  break;
1152  fieldstr = " day to minute";
1153  break;
1155  fieldstr = " day to second";
1156  break;
1158  fieldstr = " hour to minute";
1159  break;
1161  fieldstr = " hour to second";
1162  break;
1164  fieldstr = " minute to second";
1165  break;
1166  case INTERVAL_FULL_RANGE:
1167  fieldstr = "";
1168  break;
1169  default:
1170  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1171  fieldstr = "";
1172  break;
1173  }
1174 
1175  if (precision != INTERVAL_FULL_PRECISION)
1176  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1177  else
1178  snprintf(res, 64, "%s", fieldstr);
1179 
1181 }
#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 4474 of file timestamp.c.

4475 {
4476  j2date(isoweek2j(*year, woy), year, mon, mday);
4477 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:311

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

4455 {
4456  int day0,
4457  day4;
4458 
4459  /* fourth day of current year */
4460  day4 = date2j(year, 1, 4);
4461 
4462  /* day0 == offset to first day of week (Monday) */
4463  day0 = j2day(day4 - 1);
4464 
4465  return ((week - 1) * 7) + (day4 - day0);
4466 }

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

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

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 2015 of file timestamp.c.

2016 {
2017  int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2018 
2019  if (total_months > INT_MAX || total_months < INT_MIN)
2020  return -1;
2021  span->month = (int32) total_months;
2022  span->day = itm->tm_mday;
2024  &span->time))
2025  return -1;
2026  /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2027  if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2028  &span->time))
2029  return -1;
2030  if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2031  &span->time))
2032  return -1;
2033  if (pg_add_s64_overflow(span->time, itm->tm_usec,
2034  &span->time))
2035  return -1;
2036  return 0;
2037 }
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 2044 of file timestamp.c.

2045 {
2046  int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2047 
2048  if (total_months > INT_MAX || total_months < INT_MIN)
2049  return -1;
2050  span->month = (int32) total_months;
2051  span->day = itm_in->tm_mday;
2052  span->time = itm_in->tm_usec;
2053  return 0;
2054 }

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().

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1502 of file timestamp.c.

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

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

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

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

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

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

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

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

3339 {
3340  /* Args are float8 and Interval *, but leave them as generic Datum */
3341  Datum factor = PG_GETARG_DATUM(0);
3342  Datum span = PG_GETARG_DATUM(1);
3343 
3344  return DirectFunctionCall2(interval_mul, span, factor);
3345 }
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3261

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

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

Referenced by timestamp_part_common(), and timestamptz_part_common().

◆ now()

Definition at line 1549 of file timestamp.c.

References GetCurrentTransactionStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

Referenced by advanceConnectionState(), ApplyLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), CalculateCopyStreamSleeptime(), CheckArchiveTimeout(), CheckpointerMain(), doLog(), doRetry(), drandom(), dumpTimestamp(), enable_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), executeMetaCommand(), flushAndSendFeedback(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), HandleCopyStream(), has_startup_progress_timeout_expired(), IsCheckpointOnSchedule(), LagTrackerRead(), launch_worker(), LockBufferForCleanup(), logicalrep_worker_launch(), LogicalRepApplyLoop(), LogRecoveryConflict(), maybe_start_bgworkers(), mxid_age(), OutputFsync(), PerformRadiusTransaction(), 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 2549 of file timestamp.c.

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

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

472 {
473  char tzname[TZ_STRLEN_MAX + 1];
474  int dterr;
475  int tz;
476 
477  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
478 
479  /*
480  * Look up the requested timezone. First we try to interpret it as a
481  * numeric timezone specification; if DecodeTimezone decides it doesn't
482  * like the format, we look in the timezone abbreviation table (to handle
483  * cases like "EST"), and if that also fails, we look in the timezone
484  * database (to handle cases like "America/New_York"). (This matches the
485  * order in which timestamp input checks the cases; it's important because
486  * the timezone database unwisely uses a few zone names that are identical
487  * to offset abbreviations.)
488  *
489  * Note pg_tzset happily parses numeric input that DecodeTimezone would
490  * reject. To avoid having it accept input that would otherwise be seen
491  * as invalid, it's enough to disallow having a digit in the first
492  * position of our input string.
493  */
494  if (isdigit((unsigned char) *tzname))
495  ereport(ERROR,
496  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
497  errmsg("invalid input syntax for type %s: \"%s\"",
498  "numeric time zone", tzname),
499  errhint("Numeric time zones must have \"-\" or \"+\" as first character.")));
500 
501  dterr = DecodeTimezone(tzname, &tz);
502  if (dterr != 0)
503  {
504  char *lowzone;
505  int type,
506  val;
507  pg_tz *tzp;
508  DateTimeErrorExtra extra;
509 
510  if (dterr == DTERR_TZDISP_OVERFLOW)
511  ereport(ERROR,
512  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
513  errmsg("numeric time zone \"%s\" out of range", tzname)));
514  else if (dterr != DTERR_BAD_FORMAT)
515  ereport(ERROR,
516  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
517  errmsg("time zone \"%s\" not recognized", tzname)));
518 
519  /* DecodeTimezoneAbbrev requires lowercase input */
520  lowzone = downcase_truncate_identifier(tzname,
521  strlen(tzname),
522  false);
523  dterr = DecodeTimezoneAbbrev(0, lowzone, &type, &val, &tzp, &extra);
524  if (dterr)
525  DateTimeParseError(dterr, &extra, NULL, NULL, NULL);
526 
527  if (type == TZ || type == DTZ)
528  {
529  /* fixed-offset abbreviation */
530  tz = -val;
531  }
532  else if (type == DYNTZ)
533  {
534  /* dynamic-offset abbreviation, resolve using specified time */
535  tz = DetermineTimeZoneAbbrevOffset(tm, tzname, tzp);
536  }
537  else
538  {
539  /* try it as a full zone name */
540  tzp = pg_tzset(tzname);
541  if (tzp)
542  tz = DetermineTimeZoneOffset(tm, tzp);
543  else
544  ereport(ERROR,
545  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
546  errmsg("time zone \"%s\" not recognized", tzname)));
547  }
548  }
549 
550  return tz;
551 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1649
int DecodeTimezoneAbbrev(int field, const char *lowtoken, int *ftype, int *offset, pg_tz **tz, DateTimeErrorExtra *extra)
Definition: datetime.c:3233
int DecodeTimezone(const char *str, int *tzp)
Definition: datetime.c:3149
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1810
int errhint(const char *fmt,...)
Definition: elog.c:1316
#define TZ
Definition: datetime.h:95
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:286
#define DTZ
Definition: datetime.h:96
#define DYNTZ
Definition: datetime.h:97
#define TZ_STRLEN_MAX
Definition: pgtime.h:54
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:234
Definition: pgtz.h:66
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:253

References DateTimeParseError(), DecodeTimezone(), DecodeTimezoneAbbrev(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), downcase_truncate_identifier(), DTERR_BAD_FORMAT, DTERR_TZDISP_OVERFLOW, DTZ, DYNTZ, ereport, errcode(), errhint(), errmsg(), ERROR, pg_tzset(), text_to_cstring_buffer(), tm, generate_unaccent_rules::type, TZ, TZ_STRLEN_MAX, and val.

Referenced by make_timestamptz_at_timezone().

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1573 of file timestamp.c.

1574 {
1576 }
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 1567 of file timestamp.c.

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

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2117 of file timestamp.c.

2118 {
2119  Timestamp dt;
2120  struct pg_tm tt,
2121  *tm = &tt;
2122 
2123  GetEpochTime(tm);
2124  /* we don't bother to test for failure ... */
2125  tm2timestamp(tm, 0, NULL, &dt);
2126 
2127  return dt;
2128 } /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2095
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1938

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

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

◆ sql_localtimestamp()

Datum sql_localtimestamp ( PG_FUNCTION_ARGS  )

Definition at line 1624 of file timestamp.c.

1625 {
1626  Timestamp ts;
1627  int32 typmod = -1;
1628 
1629  if (!PG_ARGISNULL(0))
1630  {
1631  typmod = PG_GETARG_INT32(0);
1632  anytimestamp_typmod_check(false, typmod);
1633  }
1634 
1636  if (typmod >= 0)
1637  AdjustTimestampForTypmod(&ts, typmod, NULL);
1638  return TimestampGetDatum(ts);
1639 }
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5652

References AdjustTimestampForTypmod(), anytimestamp_typmod_check(), GetCurrentTransactionStartTimestamp(), PG_ARGISNULL, PG_GETARG_INT32, TimestampGetDatum(), and timestamptz2timestamp().

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1555 of file timestamp.c.

1556 {
1558 }
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:863

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

2058 {
2059  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2060 }

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

1753 {
1754  TimestampTz result;
1755 
1756  result = (TimestampTz) tm -
1758  result *= USECS_PER_SEC;
1759 
1760  return result;
1761 }

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

1647 {
1648  struct timeval tp;
1649  char templ[128];
1650  char buf[128];
1651  pg_time_t tt;
1652 
1653  gettimeofday(&tp, NULL);
1654  tt = (pg_time_t) tp.tv_sec;
1655  pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1657  snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1658 
1660 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
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
text * cstring_to_text(const char *s)
Definition: varlena.c:189

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

5636 {
5638 }
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5583

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

5584 {
5585  TimestampTz result;
5586  struct pg_tm tt,
5587  *tm = &tt;
5588  fsec_t fsec;
5589  int tz;
5590 
5591  if (overflow)
5592  *overflow = 0;
5593 
5595  return timestamp;
5596 
5597  /* We don't expect this to fail, but check it pro forma */
5598  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
5599  {
5601 
5602  result = dt2local(timestamp, -tz);
5603 
5604  if (IS_VALID_TIMESTAMP(result))
5605  {
5606  return result;
5607  }
5608  else if (overflow)
5609  {
5610  if (result < MIN_TIMESTAMP)
5611  {
5612  *overflow = -1;
5613  TIMESTAMP_NOBEGIN(result);
5614  }
5615  else
5616  {
5617  *overflow = 1;
5618  TIMESTAMP_NOEND(result);
5619  }
5620  return result;
5621  }
5622  }
5623 
5624  ereport(ERROR,
5625  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5626  errmsg("timestamp out of range")));
5627 
5628  return 0;
5629 }
#define MIN_TIMESTAMP
Definition: timestamp.h:223

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

1843 {
1844  Timestamp date;
1845  Timestamp time;
1846  pg_time_t utime;
1847 
1848  /* Use session timezone if caller asks for default */
1849  if (attimezone == NULL)
1850  attimezone = session_timezone;
1851 
1852  time = dt;
1853  TMODULO(time, date, USECS_PER_DAY);
1854 
1855  if (time < INT64CONST(0))
1856  {
1857  time += USECS_PER_DAY;
1858  date -= 1;
1859  }
1860 
1861  /* add offset to go from J2000 back to standard Julian date */
1863 
1864  /* Julian day routine does not work for negative Julian days */
1865  if (date < 0 || date > (Timestamp) INT_MAX)
1866  return -1;
1867 
1868  j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1869  dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1870 
1871  /* Done if no TZ conversion wanted */
1872  if (tzp == NULL)
1873  {
1874  tm->tm_isdst = -1;
1875  tm->tm_gmtoff = 0;
1876  tm->tm_zone = NULL;
1877  if (tzn != NULL)
1878  *tzn = NULL;
1879  return 0;
1880  }
1881 
1882  /*
1883  * If the time falls within the range of pg_time_t, use pg_localtime() to
1884  * rotate to the local time zone.
1885  *
1886  * First, convert to an integral timestamp, avoiding possibly
1887  * platform-specific roundoff-in-wrong-direction errors, and adjust to
1888  * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1889  * coding avoids hardwiring any assumptions about the width of pg_time_t,
1890  * so it should behave sanely on machines without int64.
1891  */
1892  dt = (dt - *fsec) / USECS_PER_SEC +
1894  utime = (pg_time_t) dt;
1895  if ((Timestamp) utime == dt)
1896  {
1897  struct pg_tm *tx = pg_localtime(&utime, attimezone);
1898 
1899  tm->tm_year = tx->tm_year + 1900;
1900  tm->tm_mon = tx->tm_mon + 1;
1901  tm->tm_mday = tx->tm_mday;
1902  tm->tm_hour = tx->tm_hour;
1903  tm->tm_min = tx->tm_min;
1904  tm->tm_sec = tx->tm_sec;
1905  tm->tm_isdst = tx->tm_isdst;
1906  tm->tm_gmtoff = tx->tm_gmtoff;
1907  tm->tm_zone = tx->tm_zone;
1908  *tzp = -tm->tm_gmtoff;
1909  if (tzn != NULL)
1910  *tzn = tm->tm_zone;
1911  }
1912  else
1913  {
1914  /*
1915  * When out of range of pg_time_t, treat as GMT
1916  */
1917  *tzp = 0;
1918  /* Mark this as *no* time zone available */
1919  tm->tm_isdst = -1;
1920  tm->tm_gmtoff = 0;
1921  tm->tm_zone = NULL;
1922  if (tzn != NULL)
1923  *tzn = NULL;
1924  }
1925 
1926  return 0;
1927 }
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1815
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(), 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 3657 of file timestamp.c.

3658 {
3659  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
3660  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
3661  Interval *result;
3662  fsec_t fsec1,
3663  fsec2;
3664  struct pg_itm tt,
3665  *tm = &tt;
3666  struct pg_tm tt1,
3667  *tm1 = &tt1;
3668  struct pg_tm tt2,
3669  *tm2 = &tt2;
3670 
3671  result = (Interval *) palloc(sizeof(Interval));
3672 
3673  if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
3674  timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
3675  {
3676  /* form the symbolic difference */
3677  tm->tm_usec = fsec1 - fsec2;
3678  tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3679  tm->tm_min = tm1->tm_min - tm2->tm_min;
3680  tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3681  tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3682  tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3683  tm->tm_year = tm1->tm_year - tm2->tm_year;
3684 
3685  /* flip sign if necessary... */
3686  if (dt1 < dt2)
3687  {
3688  tm->tm_usec = -tm->tm_usec;
3689  tm->tm_sec = -tm->tm_sec;
3690  tm->tm_min = -tm->tm_min;
3691  tm->tm_hour = -tm->tm_hour;
3692  tm->tm_mday = -tm->tm_mday;
3693  tm->tm_mon = -tm->tm_mon;
3694  tm->tm_year = -tm->tm_year;
3695  }
3696 
3697  /* propagate any negative fields into the next higher field */
3698  while (tm->tm_usec < 0)
3699  {
3700  tm->tm_usec += USECS_PER_SEC;
3701  tm->tm_sec--;
3702  }
3703 
3704  while (tm->tm_sec < 0)
3705  {
3707  tm->tm_min--;
3708  }
3709 
3710  while (tm->tm_min < 0)
3711  {
3712  tm->tm_min += MINS_PER_HOUR;
3713  tm->tm_hour--;
3714  }
3715 
3716  while (tm->tm_hour < 0)
3717  {
3718  tm->tm_hour += HOURS_PER_DAY;
3719  tm->tm_mday--;
3720  }
3721 
3722  while (tm->tm_mday < 0)
3723  {
3724  if (dt1 < dt2)
3725  {
3726  tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
3727  tm->tm_mon--;
3728  }
3729  else
3730  {
3731  tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
3732  tm->tm_mon--;
3733  }
3734  }
3735 
3736  while (tm->tm_mon < 0)
3737  {
3739  tm->tm_year--;
3740  }
3741 
3742  /* recover sign if necessary... */
3743  if (dt1 < dt2)
3744  {
3745  tm->tm_usec = -tm->tm_usec;
3746  tm->tm_sec = -tm->tm_sec;
3747  tm->tm_min = -tm->tm_min;
3748  tm->tm_hour = -tm->tm_hour;
3749  tm->tm_mday = -tm->tm_mday;
3750  tm->tm_mon = -tm->tm_mon;
3751  tm->tm_year = -tm->tm_year;
3752  }
3753 
3754  if (itm2interval(tm, result) != 0)
3755  ereport(ERROR,
3756  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3757  errmsg("interval out of range")));
3758  }
3759  else
3760  ereport(ERROR,
3761  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3762  errmsg("timestamp out of range")));
3763 
3764  PG_RETURN_INTERVAL_P(result);
3765 }
const int day_tab[2][13]
Definition: datetime.c:76
#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 3901 of file timestamp.c.

3902 {
3903  Interval *stride = PG_GETARG_INTERVAL_P(0);
3905  Timestamp origin = PG_GETARG_TIMESTAMP(2);
3906  Timestamp result,
3907  tm_diff,
3908  stride_usecs,
3909  tm_delta;
3910 
3913 
3914  if (TIMESTAMP_NOT_FINITE(origin))
3915  ereport(ERROR,
3916  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3917  errmsg("origin out of range")));
3918 
3919  if (stride->month != 0)
3920  ereport(ERROR,
3921  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3922  errmsg("timestamps cannot be binned into intervals containing months or years")));
3923 
3924  stride_usecs = stride->day * USECS_PER_DAY + stride->time;
3925 
3926  if (stride_usecs <= 0)
3927  ereport(ERROR,
3928  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3929  errmsg("stride must be greater than zero")));
3930 
3931  tm_diff = timestamp - origin;
3932  tm_delta = tm_diff - tm_diff % stride_usecs;
3933 
3934  /*
3935  * Make sure the returned timestamp is at the start of the bin, even if
3936  * the origin is in the future.
3937  */
3938  if (origin > timestamp && stride_usecs > 1)
3939  tm_delta -= stride_usecs;
3940 
3941  result = origin + tm_delta;
3942 
3943  PG_RETURN_TIMESTAMP(result);
3944 }

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

2198 {
2199  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2200  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2201 
2203 }

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

2328 {
2329  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2331 
2333 }
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition: timestamp.c:2252

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

2253 {
2254  TimestampTz dt1;
2255  int overflow;
2256 
2257  dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2258  if (overflow > 0)
2259  {
2260  /* dt1 is larger than any finite timestamp, but less than infinity */
2261  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2262  }
2263  if (overflow < 0)
2264  {
2265  /* dt1 is less than any finite timestamp, but more than -infinity */
2266  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2267  }
2268 
2269  return timestamptz_cmp_internal(dt1, dt2);
2270 }
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:125

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

2144 {
2145  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2146  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2147 
2148  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
2149 }

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

2274 {
2275  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2277 
2278  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) == 0);
2279 }

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

2209 {
2212 
2213  return timestamp_cmp_internal(a, b);
2214 }
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 2076 of file timestamp.c.

2077 {
2079 
2081 }

References PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and TIMESTAMP_NOT_FINITE.

◆ timestamp_ge()

Datum timestamp_ge ( PG_FUNCTION_ARGS  )

Definition at line 2188 of file timestamp.c.

2189 {
2190  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2191  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2192 
2193  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
2194 }

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

2319 {
2320  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2322 
2323  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) >= 0);
2324 }

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

2171 {
2172  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2173  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2174 
2175  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
2176 }

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

2301 {
2302  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2304 
2305  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) > 0);
2306 }

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

2237 {
2238  return hashint8(fcinfo);
2239 }

References hashint8().

◆ timestamp_hash_extended()

Datum timestamp_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2242 of file timestamp.c.

2243 {
2244  return hashint8extended(fcinfo);
2245 }

References hashint8extended().

◆ timestamp_in()

Datum timestamp_in ( PG_FUNCTION_ARGS  )

Definition at line 146 of file timestamp.c.

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

5516 {
5519  TimestampTz result;
5520  int tz;
5521 
5524 
5525  if (zone->month != 0 || zone->day != 0)
5526  ereport(ERROR,
5527  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5528  errmsg("interval time zone \"%s\" must not include months or days",
5530  PointerGetDatum(zone))))));
5531 
5532  tz = zone->time / USECS_PER_SEC;
5533 
5534  result = dt2local(timestamp, tz);
5535 
5536  if (!IS_VALID_TIMESTAMP(result))
5537  ereport(ERROR,
5538  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5539  errmsg("timestamp out of range")));
5540 
5541  PG_RETURN_TIMESTAMPTZ(result);
5542 } /* timestamp_izone() */
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:960
static char * DatumGetCString(Datum X)
Definition: postgres.h:683

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