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 pg_tzlookup_timezone (text *zone)
 
static Timestamp make_timestamp_internal (int year, int month, int day, int hour, int min, double sec)
 
Datum make_timestamp (PG_FUNCTION_ARGS)
 
Datum make_timestamptz (PG_FUNCTION_ARGS)
 
Datum make_timestamptz_at_timezone (PG_FUNCTION_ARGS)
 
Datum float8_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_out (PG_FUNCTION_ARGS)
 
Datum timestamptz_recv (PG_FUNCTION_ARGS)
 
Datum timestamptz_send (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodout (PG_FUNCTION_ARGS)
 
Datum timestamptz_scale (PG_FUNCTION_ARGS)
 
Datum interval_in (PG_FUNCTION_ARGS)
 
Datum interval_out (PG_FUNCTION_ARGS)
 
Datum interval_recv (PG_FUNCTION_ARGS)
 
Datum interval_send (PG_FUNCTION_ARGS)
 
Datum intervaltypmodin (PG_FUNCTION_ARGS)
 
Datum intervaltypmodout (PG_FUNCTION_ARGS)
 
static int intervaltypmodleastfield (int32 typmod)
 
Datum interval_support (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
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)
 
static TimestampTz timestamptz_pl_interval_internal (TimestampTz timestamp, Interval *span, pg_tz *attimezone)
 
static TimestampTz timestamptz_mi_interval_internal (TimestampTz timestamp, Interval *span, pg_tz *attimezone)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_pl_interval_at_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval_at_zone (PG_FUNCTION_ARGS)
 
Datum interval_um (PG_FUNCTION_ARGS)
 
Datum interval_smaller (PG_FUNCTION_ARGS)
 
Datum interval_larger (PG_FUNCTION_ARGS)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
Datum interval_mi (PG_FUNCTION_ARGS)
 
Datum interval_mul (PG_FUNCTION_ARGS)
 
Datum mul_d_interval (PG_FUNCTION_ARGS)
 
Datum interval_div (PG_FUNCTION_ARGS)
 
Datum in_range_timestamptz_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timestamp_interval (PG_FUNCTION_ARGS)
 
Datum in_range_interval_interval (PG_FUNCTION_ARGS)
 
Datum interval_accum (PG_FUNCTION_ARGS)
 
Datum interval_combine (PG_FUNCTION_ARGS)
 
Datum interval_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (PG_FUNCTION_ARGS)
 
Datum timestamp_age (PG_FUNCTION_ARGS)
 
Datum timestamptz_age (PG_FUNCTION_ARGS)
 
Datum timestamp_bin (PG_FUNCTION_ARGS)
 
Datum timestamp_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_bin (PG_FUNCTION_ARGS)
 
static TimestampTz timestamptz_trunc_internal (text *units, TimestampTz timestamp, pg_tz *tzp)
 
Datum timestamptz_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc_zone (PG_FUNCTION_ARGS)
 
Datum interval_trunc (PG_FUNCTION_ARGS)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
static float8 NonFiniteTimestampTzPart (int type, int unit, char *lowunits, bool isNegative, bool isTz)
 
static Datum timestamp_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamp (PG_FUNCTION_ARGS)
 
static Datum timestamptz_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamptz (PG_FUNCTION_ARGS)
 
static Datum interval_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum extract_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
bool TimestampTimestampTzRequiresRewrite (void)
 
Datum timestamp_timestamptz (PG_FUNCTION_ARGS)
 
TimestampTz timestamp2timestamptz_opt_overflow (Timestamp timestamp, int *overflow)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
static Datum generate_series_timestamptz_internal (FunctionCallInfo fcinfo)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz_at_zone (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ SAMESIGN

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

Definition at line 50 of file timestamp.c.

◆ TIMESTAMP_GT

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

◆ TIMESTAMP_LT

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

Function Documentation

◆ AdjustIntervalForTypmod()

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

Definition at line 1329 of file timestamp.c.

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

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

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

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

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

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

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

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

1559 {
1561 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1582
#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 1600 of file timestamp.c.

1601 {
1602  TimestampTz ts;
1603  int32 typmod = -1;
1604 
1605  if (!PG_ARGISNULL(0))
1606  typmod = anytimestamp_typmod_check(true, PG_GETARG_INT32(0));
1607 
1609  if (typmod >= 0)
1610  AdjustTimestampForTypmod(&ts, typmod, NULL);
1611  return TimestampTzGetDatum(ts);
1612 }
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition: timestamp.c:349
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:855

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

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

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

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

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

4631 {
4632  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4633 }
int isoweek2j(int year, int week)
Definition: timestamp.c:4467
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4573

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

Referenced by DCH_to_char().

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 2059 of file timestamp.c.

2060 {
2061  dt -= (timezone * USECS_PER_SEC);
2062  return dt;
2063 }
#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 1811 of file timestamp.c.

1812 {
1813  TimeOffset time;
1814 
1815  time = jd;
1816 
1817  *hour = time / USECS_PER_HOUR;
1818  time -= (*hour) * USECS_PER_HOUR;
1819  *min = time / USECS_PER_MINUTE;
1820  time -= (*min) * USECS_PER_MINUTE;
1821  *sec = time / USECS_PER_SEC;
1822  *fsec = time - (*sec * USECS_PER_SEC);
1823 } /* 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 1535 of file timestamp.c.

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

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

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

◆ extract_interval()

Datum extract_interval ( PG_FUNCTION_ARGS  )

Definition at line 5421 of file timestamp.c.

5422 {
5423  return interval_part_common(fcinfo, true);
5424 }
static Datum interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:5250

References interval_part_common().

◆ extract_timestamp()

Datum extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 4968 of file timestamp.c.

4969 {
4970  return timestamp_part_common(fcinfo, true);
4971 }
static Datum timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4704

References timestamp_part_common().

◆ extract_timestamptz()

Datum extract_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5240 of file timestamp.c.

5241 {
5242  return timestamptz_part_common(fcinfo, true);
5243 }
static Datum timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4977

References timestamptz_part_common().

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 718 of file timestamp.c.

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

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

◆ generate_series_timestamp()

Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 5764 of file timestamp.c.

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

References generate_series_timestamp_fctx::current, DatumGetTimestamp(), DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, generate_series_timestamp_fctx::finish, interval_cmp_internal(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_timestamp_fctx::step, generate_series_timestamp_fctx::step_sign, timestamp_cmp_internal(), timestamp_pl_interval(), TimestampGetDatum(), and FuncCallContext::user_fctx.

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5922 of file timestamp.c.

5923 {
5924  return generate_series_timestamptz_internal(fcinfo);
5925 }
static Datum generate_series_timestamptz_internal(FunctionCallInfo fcinfo)
Definition: timestamp.c:5844

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_at_zone()

Datum generate_series_timestamptz_at_zone ( PG_FUNCTION_ARGS  )

Definition at line 5928 of file timestamp.c.

5929 {
5930  return generate_series_timestamptz_internal(fcinfo);
5931 }

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_internal()

static Datum generate_series_timestamptz_internal ( FunctionCallInfo  fcinfo)
static

Definition at line 5844 of file timestamp.c.

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

References generate_series_timestamptz_fctx::attimezone, generate_series_timestamptz_fctx::current, ereport, errcode(), errmsg(), ERROR, generate_series_timestamptz_fctx::finish, interval_cmp_internal(), lookup_timezone(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_NARGS, session_timezone, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_timestamptz_fctx::step, generate_series_timestamptz_fctx::step_sign, timestamp_cmp_internal(), timestamptz_pl_interval_internal(), TimestampTzGetDatum(), and FuncCallContext::user_fctx.

Referenced by generate_series_timestamptz(), and generate_series_timestamptz_at_zone().

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1582 of file timestamp.c.

1583 {
1584  TimestampTz result;
1585  struct timeval tp;
1586 
1587  gettimeofday(&tp, NULL);
1588 
1589  result = (TimestampTz) tp.tv_sec -
1591  result = (result * USECS_PER_SEC) + tp.tv_usec;
1592 
1593  return result;
1594 }
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(), check_log_duration(), CheckPointGuts(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), enable_startup_progress_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), initialize_drandom_seed(), InitProcessGlobals(), KnownAssignedXidsCompress(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), pa_send_data(), PerformWalRecovery(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_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 2091 of file timestamp.c.

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

3510 {
3512  Interval *base = PG_GETARG_INTERVAL_P(1);
3513  Interval *offset = PG_GETARG_INTERVAL_P(2);
3514  bool sub = PG_GETARG_BOOL(3);
3515  bool less = PG_GETARG_BOOL(4);
3516  Interval *sum;
3517 
3518  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3519  ereport(ERROR,
3520  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3521  errmsg("invalid preceding or following size in window function")));
3522 
3523  /* We don't currently bother to avoid overflow hazards here */
3524  if (sub)
3526  IntervalPGetDatum(base),
3527  IntervalPGetDatum(offset)));
3528  else
3530  IntervalPGetDatum(base),
3531  IntervalPGetDatum(offset)));
3532 
3533  if (less)
3535  else
3537 }
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2406
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3272
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3238
#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 3478 of file timestamp.c.

3479 {
3481  Timestamp base = PG_GETARG_TIMESTAMP(1);
3482  Interval *offset = PG_GETARG_INTERVAL_P(2);
3483  bool sub = PG_GETARG_BOOL(3);
3484  bool less = PG_GETARG_BOOL(4);
3485  Timestamp sum;
3486 
3487  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3488  ereport(ERROR,
3489  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3490  errmsg("invalid preceding or following size in window function")));
3491 
3492  /* We don't currently bother to avoid overflow hazards here */
3493  if (sub)
3495  TimestampGetDatum(base),
3496  IntervalPGetDatum(offset)));
3497  else
3499  TimestampGetDatum(base),
3500  IntervalPGetDatum(offset)));
3501 
3502  if (less)
3503  PG_RETURN_BOOL(val <= sum);
3504  else
3505  PG_RETURN_BOOL(val >= sum);
3506 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3001

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

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

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

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 1984 of file timestamp.c.

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

3552 {
3553  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3555  Datum *transdatums;
3556  int ndatums;
3557  Interval sumX,
3558  N;
3559  Interval *newsum;
3560  ArrayType *result;
3561 
3562  deconstruct_array(transarray,
3563  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3564  &transdatums, NULL, &ndatums);
3565  if (ndatums != 2)
3566  elog(ERROR, "expected 2-element interval array");
3567 
3568  sumX = *(DatumGetIntervalP(transdatums[0]));
3569  N = *(DatumGetIntervalP(transdatums[1]));
3570 
3572  IntervalPGetDatum(&sumX),
3574  N.time += 1;
3575 
3576  transdatums[0] = IntervalPGetDatum(newsum);
3577  transdatums[1] = IntervalPGetDatum(&N);
3578 
3579  result = construct_array(transdatums, 2,
3580  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3581 
3582  PG_RETURN_ARRAYTYPE_P(result);
3583 }
#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:3341
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3600
#define newval
uintptr_t Datum
Definition: postgres.h:64

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

◆ interval_accum_inv()

Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3635 of file timestamp.c.

3636 {
3637  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3639  Datum *transdatums;
3640  int ndatums;
3641  Interval sumX,
3642  N;
3643  Interval *newsum;
3644  ArrayType *result;
3645 
3646  deconstruct_array(transarray,
3647  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3648  &transdatums, NULL, &ndatums);
3649  if (ndatums != 2)
3650  elog(ERROR, "expected 2-element interval array");
3651 
3652  sumX = *(DatumGetIntervalP(transdatums[0]));
3653  N = *(DatumGetIntervalP(transdatums[1]));
3654 
3656  IntervalPGetDatum(&sumX),
3658  N.time -= 1;
3659 
3660  transdatums[0] = IntervalPGetDatum(newsum);
3661  transdatums[1] = IntervalPGetDatum(&N);
3662 
3663  result = construct_array(transdatums, 2,
3664  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3665 
3666  PG_RETURN_ARRAYTYPE_P(result);
3667 }

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

3671 {
3672  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3673  Datum *transdatums;
3674  int ndatums;
3675  Interval sumX,
3676  N;
3677 
3678  deconstruct_array(transarray,
3679  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3680  &transdatums, NULL, &ndatums);
3681  if (ndatums != 2)
3682  elog(ERROR, "expected 2-element interval array");
3683 
3684  sumX = *(DatumGetIntervalP(transdatums[0]));
3685  N = *(DatumGetIntervalP(transdatums[1]));
3686 
3687  /* SQL defines AVG of no values to be NULL */
3688  if (N.time == 0)
3689  PG_RETURN_NULL();
3690 
3692  IntervalPGetDatum(&sumX),
3693  Float8GetDatum((double) N.time));
3694 }
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3399
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1788
#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 2491 of file timestamp.c.

2492 {
2493  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2494  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2495 
2496  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2497 }
#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 2406 of file timestamp.c.

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

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

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

◆ interval_combine()

Datum interval_combine ( PG_FUNCTION_ARGS  )

Definition at line 3586 of file timestamp.c.

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

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

3400 {
3401  Interval *span = PG_GETARG_INTERVAL_P(0);
3402  float8 factor = PG_GETARG_FLOAT8(1);
3403  double month_remainder_days,
3404  sec_remainder;
3405  int32 orig_month = span->month,
3406  orig_day = span->day;
3407  Interval *result;
3408 
3409  result = (Interval *) palloc(sizeof(Interval));
3410 
3411  if (factor == 0.0)
3412  ereport(ERROR,
3413  (errcode(ERRCODE_DIVISION_BY_ZERO),
3414  errmsg("division by zero")));
3415 
3416  result->month = (int32) (span->month / factor);
3417  result->day = (int32) (span->day / factor);
3418 
3419  /*
3420  * Fractional months full days into days. See comment in interval_mul().
3421  */
3422  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3423  month_remainder_days = TSROUND(month_remainder_days);
3424  sec_remainder = (orig_day / factor - result->day +
3425  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3426  sec_remainder = TSROUND(sec_remainder);
3427  if (fabs(sec_remainder) >= SECS_PER_DAY)
3428  {
3429  result->day += (int) (sec_remainder / SECS_PER_DAY);
3430  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3431  }
3432 
3433  /* cascade units down */
3434  result->day += (int32) month_remainder_days;
3435  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3436 
3437  PG_RETURN_INTERVAL_P(result);
3438 }
#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 2437 of file timestamp.c.

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

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

2081 {
2082  PG_RETURN_BOOL(true);
2083 }

References PG_RETURN_BOOL.

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2482 of file timestamp.c.

2483 {
2484  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2485  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2486 
2487  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2488 }

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

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

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

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

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

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2525 of file timestamp.c.

2526 {
2529  int64 span64;
2530 
2531  /* Same approach as interval_hash */
2532  span64 = int128_to_int64(span);
2533 
2535  PG_GETARG_DATUM(1));
2536 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:105

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

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 883 of file timestamp.c.

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

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

Referenced by check_timezone(), and flatten_set_variable_args().

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2879 of file timestamp.c.

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

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

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

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

3225 {
3226  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3227  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3228  Interval *result;
3229 
3230  if (interval_cmp_internal(interval1, interval2) > 0)
3231  result = interval1;
3232  else
3233  result = interval2;
3234  PG_RETURN_INTERVAL_P(result);
3235 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2473 of file timestamp.c.

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

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

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

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

3273 {
3274  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3275  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3276  Interval *result;
3277 
3278  result = (Interval *) palloc(sizeof(Interval));
3279 
3280  result->month = span1->month - span2->month;
3281  /* overflow check copied from int4mi */
3282  if (!SAMESIGN(span1->month, span2->month) &&
3283  !SAMESIGN(result->month, span1->month))
3284  ereport(ERROR,
3285  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3286  errmsg("interval out of range")));
3287 
3288  result->day = span1->day - span2->day;
3289  if (!SAMESIGN(span1->day, span2->day) &&
3290  !SAMESIGN(result->day, span1->day))
3291  ereport(ERROR,
3292  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3293  errmsg("interval out of range")));
3294 
3295  result->time = span1->time - span2->time;
3296  if (!SAMESIGN(span1->time, span2->time) &&
3297  !SAMESIGN(result->time, span1->time))
3298  ereport(ERROR,
3299  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3300  errmsg("interval out of range")));
3301 
3302  PG_RETURN_INTERVAL_P(result);
3303 }
#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 3312 of file timestamp.c.

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

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

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

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 957 of file timestamp.c.

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

5416 {
5417  return interval_part_common(fcinfo, false);
5418 }

References interval_part_common().

◆ interval_part_common()

static Datum interval_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5250 of file timestamp.c.

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

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

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

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

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

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

◆ interval_scale()

Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1307 of file timestamp.c.

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

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

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

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

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3209 of file timestamp.c.

3210 {
3211  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3212  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3213  Interval *result;
3214 
3215  /* use interval_cmp_internal to be sure this agrees with comparisons */
3216  if (interval_cmp_internal(interval1, interval2) < 0)
3217  result = interval1;
3218  else
3219  result = interval2;
3220  PG_RETURN_INTERVAL_P(result);
3221 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1244 of file timestamp.c.

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

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

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 4374 of file timestamp.c.

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

3181 {
3183  Interval *result;
3184 
3185  result = (Interval *) palloc(sizeof(Interval));
3186 
3187  result->time = -interval->time;
3188  /* overflow check copied from int4um */
3189  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3190  ereport(ERROR,
3191  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3192  errmsg("interval out of range")));
3193  result->day = -interval->day;
3194  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3195  ereport(ERROR,
3196  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3197  errmsg("interval out of range")));
3198  result->month = -interval->month;
3199  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3200  ereport(ERROR,
3201  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3202  errmsg("interval out of range")));
3203 
3204  PG_RETURN_INTERVAL_P(result);
3205 }

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

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

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

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

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

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

References isoweek2j(), and j2date().

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

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4467 of file timestamp.c.

4468 {
4469  int day0,
4470  day4;
4471 
4472  /* fourth day of current year */
4473  day4 = date2j(year, 1, 4);
4474 
4475  /* day0 == offset to first day of week (Monday) */
4476  day0 = j2day(day4 - 1);
4477 
4478  return ((week - 1) * 7) + (day4 - day0);
4479 }

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

4501 {
4502  int jday;
4503 
4504  jday = isoweek2j(*year, isoweek);
4505  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4506  if (wday > 1)
4507  jday += wday - 2;
4508  else
4509  jday += 6;
4510  j2date(jday, year, mon, mday);
4511 }

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 2011 of file timestamp.c.

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

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

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

Referenced by interval_in(), and pg_timezone_abbrevs().

◆ lookup_timezone()

static pg_tz* lookup_timezone ( text zone)
static

Definition at line 541 of file timestamp.c.

542 {
543  char tzname[TZ_STRLEN_MAX + 1];
544 
545  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
546 
547  return DecodeTimezoneNameToTz(tzname);
548 }
pg_tz * DecodeTimezoneNameToTz(const char *tzname)
Definition: datetime.c:3236
#define TZ_STRLEN_MAX
Definition: pgtime.h:54
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:246

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

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

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1499 of file timestamp.c.

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

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

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 637 of file timestamp.c.

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

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

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

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

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

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

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

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3389 of file timestamp.c.

3390 {
3391  /* Args are float8 and Interval *, but leave them as generic Datum */
3392  Datum factor = PG_GETARG_DATUM(0);
3393  Datum span = PG_GETARG_DATUM(1);
3394 
3395  return DirectFunctionCall2(interval_mul, span, factor);
3396 }
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3312

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

4649 {
4650  if ((type != UNITS) && (type != RESERV))
4651  ereport(ERROR,
4652  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4653  errmsg("unit \"%s\" not recognized for type %s",
4654  lowunits,
4655  format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
4656 
4657  switch (unit)
4658  {
4659  /* Oscillating units */
4660  case DTK_MICROSEC:
4661  case DTK_MILLISEC:
4662  case DTK_SECOND:
4663  case DTK_MINUTE:
4664  case DTK_HOUR:
4665  case DTK_DAY:
4666  case DTK_MONTH:
4667  case DTK_QUARTER:
4668  case DTK_WEEK:
4669  case DTK_DOW:
4670  case DTK_ISODOW:
4671  case DTK_DOY:
4672  case DTK_TZ:
4673  case DTK_TZ_MINUTE:
4674  case DTK_TZ_HOUR:
4675  return 0.0;
4676 
4677  /* Monotonically-increasing units */
4678  case DTK_YEAR:
4679  case DTK_DECADE:
4680  case DTK_CENTURY:
4681  case DTK_MILLENNIUM:
4682  case DTK_JULIAN:
4683  case DTK_ISOYEAR:
4684  case DTK_EPOCH:
4685  if (isNegative)
4686  return -get_float8_infinity();
4687  else
4688  return get_float8_infinity();
4689 
4690  default:
4691  ereport(ERROR,
4692  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4693  errmsg("unit \"%s\" not supported for type %s",
4694  lowunits,
4695  format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
4696  return 0.0; /* keep compiler quiet */
4697  }
4698 }
static float8 get_float8_infinity(void)
Definition: float.h:94
#define DTK_JULIAN
Definition: datetime.h:173
#define DTK_TZ_HOUR
Definition: datetime.h:177
#define DTK_TZ_MINUTE
Definition: datetime.h:178
#define DTK_ISODOW
Definition: datetime.h:180
#define DTK_ISOYEAR
Definition: datetime.h:179
#define DTK_DOY
Definition: datetime.h:176
#define DTK_TZ
Definition: datetime.h:146
#define DTK_DOW
Definition: datetime.h:175

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

Referenced by timestamp_part_common(), and timestamptz_part_common().

◆ now()

Definition at line 1546 of file timestamp.c.

References GetCurrentTransactionStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

Referenced by advanceConnectionState(), ApplyLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), CalculateCopyStreamSleeptime(), CheckArchiveTimeout(), CheckpointerMain(), doLog(), doRetry(), dumpTimestamp(), enable_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), executeMetaCommand(), flushAndSendFeedback(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), HandleCopyStream(), has_startup_progress_timeout_expired(), initialize_drandom_seed(), IsCheckpointOnSchedule(), LagTrackerRead(), launch_worker(), LockBufferForCleanup(), logicalrep_worker_launch(), LogicalRepApplyLoop(), LogRecoveryConflict(), maybe_start_bgworkers(), mxid_age(), OutputFsync(), PerformRadiusTransaction(), pg_clock_gettime_ns(), pg_time_now(), pg_time_now_lazy(), pg_timezone_abbrevs(), pgfdw_get_cleanup_result(), pgstat_report_archiver(), pgstat_report_stat(), pqSocketPoll(), pqTraceFormatTimestamp(), printProgressReport(), printVerboseErrorMessages(), process_syncing_tables_for_apply(), ProcessKeepaliveMsg(), ProcessStandbyReplyMessage(), processXactStats(), ProcSleep(), progress_report(), RequestXLogStreaming(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), schedule_alarm(), send_feedback(), sendFeedback(), ServerLoop(), set_next_rotation_time(), StreamLogicalLog(), SysLoggerMain(), threadRun(), timetz_zone(), WaitForWALToBecomeAvailable(), WalRcvComputeNextWakeup(), WalRcvRunning(), WalRcvStreaming(), WalReceiverMain(), WalSndComputeSleeptime(), WalSndUpdateProgress(), WalSndWriteData(), xid_age(), XLogBackgroundFlush(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2545 of file timestamp.c.

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

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

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

Referenced by make_timestamptz_at_timezone().

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1570 of file timestamp.c.

1571 {
1573 }
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 1564 of file timestamp.c.

1565 {
1567 }
TimestampTz PgStartTime
Definition: timestamp.c:53

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2113 of file timestamp.c.

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

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

1619 {
1620  Timestamp ts;
1621  int32 typmod = -1;
1622 
1623  if (!PG_ARGISNULL(0))
1624  typmod = anytimestamp_typmod_check(false, PG_GETARG_INT32(0));
1625 
1627  if (typmod >= 0)
1628  AdjustTimestampForTypmod(&ts, typmod, NULL);
1629  return TimestampGetDatum(ts);
1630 }
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5638

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

1553 {
1555 }
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:864

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

◆ time2t()

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

Definition at line 2053 of file timestamp.c.

2054 {
2055  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2056 }

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

1749 {
1750  TimestampTz result;
1751 
1752  result = (TimestampTz) tm -
1754  result *= USECS_PER_SEC;
1755 
1756  return result;
1757 }

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

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

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

◆ timestamp2timestamptz()

static TimestampTz timestamp2timestamptz ( Timestamp  timestamp)
static

Definition at line 5621 of file timestamp.c.

5622 {
5624 }
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5569

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

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

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

Referenced by timestamp2timestamptz(), and timestamp_cmp_timestamptz_internal().

◆ timestamp2tm()

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

Definition at line 1838 of file timestamp.c.

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

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

Referenced by DetermineTimeZoneAbbrevOffsetTS(), GetCurrentTimeUsec(), JsonEncodeDateTime(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_fmt_asc(), PGTYPEStimestamp_to_asc(), timestamp2timestamptz_opt_overflow(), timestamp_age(), timestamp_date(), timestamp_out(), timestamp_part_common(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_age(), timestamptz_date(), timestamptz_out(), timestamptz_part_common(), timestamptz_pl_interval_internal(), timestamptz_recv(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), timestamptz_to_str(), timestamptz_trunc_internal(), timestamptz_zone(), and timetz_zone().

◆ timestamp_age()

Datum timestamp_age ( PG_FUNCTION_ARGS  )

Definition at line 3704 of file timestamp.c.

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

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

◆ timestamp_bin()

Datum timestamp_bin ( PG_FUNCTION_ARGS  )

Definition at line 3948 of file timestamp.c.

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

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

2194 {
2195  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2196  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2197 
2199 }

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

2324 {
2325  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2327 
2329 }
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition: timestamp.c:2248

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

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

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

2140 {
2141  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2142  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2143 
2144  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
2145 }

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

2270 {
2271  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2273 
2274  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) == 0);
2275 }

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

2205 {
2208 
2209  return timestamp_cmp_internal(a, b);
2210 }
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 2072 of file timestamp.c.

2073 {
2075 
2077 }

References PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and TIMESTAMP_NOT_FINITE.

◆ timestamp_ge()

Datum timestamp_ge ( PG_FUNCTION_ARGS  )

Definition at line 2184 of file timestamp.c.

2185 {
2186  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2187  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2188 
2189  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
2190 }

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

2315 {
2316  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2318 
2319  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) >= 0);
2320 }

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

2167 {
2168  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2169  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2170 
2171  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) > 0);
2172 }

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

2297 {
2298  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2300 
2301  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) > 0);
2302 }

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

2233 {
2234  return hashint8(fcinfo);
2235 }

References hashint8().

◆ timestamp_hash_extended()

Datum timestamp_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2238 of file timestamp.c.

2239 {
2240  return hashint8extended(fcinfo);
2241 }

References hashint8extended().

◆ timestamp_in()

Datum timestamp_in ( PG_FUNCTION_ARGS  )

Definition at line 147 of file timestamp.c.

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

5502 {
5504