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 void AdjustIntervalForTypmod (Interval *interval, int32 typmod)
 
static TimestampTz timestamp2timestamptz (Timestamp timestamp)
 
static Timestamp timestamptz2timestamp (TimestampTz timestamp)
 
static int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
static char * anytimestamp_typmodout (bool istz, int32 typmod)
 
Datum timestamp_in (PG_FUNCTION_ARGS)
 
Datum timestamp_out (PG_FUNCTION_ARGS)
 
Datum timestamp_recv (PG_FUNCTION_ARGS)
 
Datum timestamp_send (PG_FUNCTION_ARGS)
 
Datum timestamptypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptypmodout (PG_FUNCTION_ARGS)
 
Datum timestamp_support (PG_FUNCTION_ARGS)
 
Datum timestamp_scale (PG_FUNCTION_ARGS)
 
bool AdjustTimestampForTypmodError (Timestamp *time, int32 typmod, bool *error)
 
void AdjustTimestampForTypmod (Timestamp *time, int32 typmod)
 
Datum timestamptz_in (PG_FUNCTION_ARGS)
 
static int parse_sane_timezone (struct pg_tm *tm, text *zone)
 
static Timestamp make_timestamp_internal (int year, int month, int day, int hour, int min, double sec)
 
Datum make_timestamp (PG_FUNCTION_ARGS)
 
Datum make_timestamptz (PG_FUNCTION_ARGS)
 
Datum make_timestamptz_at_timezone (PG_FUNCTION_ARGS)
 
Datum float8_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_out (PG_FUNCTION_ARGS)
 
Datum timestamptz_recv (PG_FUNCTION_ARGS)
 
Datum timestamptz_send (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodout (PG_FUNCTION_ARGS)
 
Datum timestamptz_scale (PG_FUNCTION_ARGS)
 
Datum interval_in (PG_FUNCTION_ARGS)
 
Datum interval_out (PG_FUNCTION_ARGS)
 
Datum interval_recv (PG_FUNCTION_ARGS)
 
Datum interval_send (PG_FUNCTION_ARGS)
 
Datum intervaltypmodin (PG_FUNCTION_ARGS)
 
Datum intervaltypmodout (PG_FUNCTION_ARGS)
 
static int intervaltypmodleastfield (int32 typmod)
 
Datum interval_support (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
Datum timeofday (PG_FUNCTION_ARGS)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
long TimestampDifferenceMilliseconds (TimestampTz start_time, TimestampTz stop_time)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const char * timestamptz_to_str (TimestampTz t)
 
void dt2time (Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
int tm2timestamp (struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
 
void interval2itm (Interval span, struct pg_itm *itm)
 
int itm2interval (struct pg_itm *itm, Interval *span)
 
int itmin2interval (struct pg_itm_in *itm_in, Interval *span)
 
Datum timestamp_finite (PG_FUNCTION_ARGS)
 
Datum interval_finite (PG_FUNCTION_ARGS)
 
void GetEpochTime (struct pg_tm *tm)
 
Timestamp SetEpochTimestamp (void)
 
int timestamp_cmp_internal (Timestamp dt1, Timestamp dt2)
 
Datum timestamp_eq (PG_FUNCTION_ARGS)
 
Datum timestamp_ne (PG_FUNCTION_ARGS)
 
Datum timestamp_lt (PG_FUNCTION_ARGS)
 
Datum timestamp_gt (PG_FUNCTION_ARGS)
 
Datum timestamp_le (PG_FUNCTION_ARGS)
 
Datum timestamp_ge (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp (PG_FUNCTION_ARGS)
 
static int timestamp_fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum timestamp_sortsupport (PG_FUNCTION_ARGS)
 
Datum timestamp_hash (PG_FUNCTION_ARGS)
 
Datum timestamp_hash_extended (PG_FUNCTION_ARGS)
 
int32 timestamp_cmp_timestamptz_internal (Timestamp timestampVal, TimestampTz dt2)
 
Datum timestamp_eq_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ne_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_lt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_gt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_le_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ge_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_eq_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ne_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_lt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_gt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_le_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ge_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_cmp_timestamp (PG_FUNCTION_ARGS)
 
static INT128 interval_cmp_value (const Interval *interval)
 
static int interval_cmp_internal (Interval *interval1, Interval *interval2)
 
Datum interval_eq (PG_FUNCTION_ARGS)
 
Datum interval_ne (PG_FUNCTION_ARGS)
 
Datum interval_lt (PG_FUNCTION_ARGS)
 
Datum interval_gt (PG_FUNCTION_ARGS)
 
Datum interval_le (PG_FUNCTION_ARGS)
 
Datum interval_ge (PG_FUNCTION_ARGS)
 
Datum interval_cmp (PG_FUNCTION_ARGS)
 
Datum interval_hash (PG_FUNCTION_ARGS)
 
Datum interval_hash_extended (PG_FUNCTION_ARGS)
 
Datum overlaps_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_smaller (PG_FUNCTION_ARGS)
 
Datum timestamp_larger (PG_FUNCTION_ARGS)
 
Datum timestamp_mi (PG_FUNCTION_ARGS)
 
Datum interval_justify_interval (PG_FUNCTION_ARGS)
 
Datum interval_justify_hours (PG_FUNCTION_ARGS)
 
Datum interval_justify_days (PG_FUNCTION_ARGS)
 
Datum timestamp_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_mi_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (PG_FUNCTION_ARGS)
 
Datum interval_um (PG_FUNCTION_ARGS)
 
Datum interval_smaller (PG_FUNCTION_ARGS)
 
Datum interval_larger (PG_FUNCTION_ARGS)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
Datum interval_mi (PG_FUNCTION_ARGS)
 
Datum interval_mul (PG_FUNCTION_ARGS)
 
Datum mul_d_interval (PG_FUNCTION_ARGS)
 
Datum interval_div (PG_FUNCTION_ARGS)
 
Datum in_range_timestamptz_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timestamp_interval (PG_FUNCTION_ARGS)
 
Datum in_range_interval_interval (PG_FUNCTION_ARGS)
 
Datum interval_accum (PG_FUNCTION_ARGS)
 
Datum interval_combine (PG_FUNCTION_ARGS)
 
Datum interval_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (PG_FUNCTION_ARGS)
 
Datum timestamp_age (PG_FUNCTION_ARGS)
 
Datum timestamptz_age (PG_FUNCTION_ARGS)
 
Datum timestamp_bin (PG_FUNCTION_ARGS)
 
Datum timestamp_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_bin (PG_FUNCTION_ARGS)
 
static TimestampTz timestamptz_trunc_internal (text *units, TimestampTz timestamp, pg_tz *tzp)
 
Datum timestamptz_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc_zone (PG_FUNCTION_ARGS)
 
Datum interval_trunc (PG_FUNCTION_ARGS)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
static float8 NonFiniteTimestampTzPart (int type, int unit, char *lowunits, bool isNegative, bool isTz)
 
static Datum timestamp_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamp (PG_FUNCTION_ARGS)
 
static Datum timestamptz_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamptz (PG_FUNCTION_ARGS)
 
static Datum interval_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum extract_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
bool TimestampTimestampTzRequiresRewrite (void)
 
Datum timestamp_timestamptz (PG_FUNCTION_ARGS)
 
TimestampTz timestamp2timestamptz_opt_overflow (Timestamp timestamp, int *overflow)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ SAMESIGN

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

Definition at line 50 of file timestamp.c.

◆ TIMESTAMP_GT

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

◆ TIMESTAMP_LT

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

Function Documentation

◆ AdjustIntervalForTypmod()

static void AdjustIntervalForTypmod ( Interval interval,
int32  typmod 
)
static

Definition at line 1324 of file timestamp.c.

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

References DAY, elog, ereport, 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()

void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)

Definition at line 395 of file timestamp.c.

396 {
397  (void) AdjustTimestampForTypmodError(time, typmod, NULL);
398 }
bool AdjustTimestampForTypmodError(Timestamp *time, int32 typmod, bool *error)
Definition: timestamp.c:340

References AdjustTimestampForTypmodError().

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

◆ AdjustTimestampForTypmodError()

bool AdjustTimestampForTypmodError ( Timestamp time,
int32  typmod,
bool error 
)

Definition at line 340 of file timestamp.c.

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

References ereport, errcode(), errmsg(), ERROR, error(), MAX_TIMESTAMP_PRECISION, and TIMESTAMP_NOT_FINITE.

Referenced by AdjustTimestampForTypmod().

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 105 of file timestamp.c.

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

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 84 of file timestamp.c.

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

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

◆ anytimestamp_typmodout()

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

Definition at line 127 of file timestamp.c.

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

References psprintf().

Referenced by timestamptypmodout(), and timestamptztypmodout().

◆ clock_timestamp()

Datum clock_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1550 of file timestamp.c.

1551 {
1553 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

◆ date2isoweek()

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

Definition at line 4475 of file timestamp.c.

4476 {
4477  float8 result;
4478  int day0,
4479  day4,
4480  dayn;
4481 
4482  /* current day */
4483  dayn = date2j(year, mon, mday);
4484 
4485  /* fourth day of current year */
4486  day4 = date2j(year, 1, 4);
4487 
4488  /* day0 == offset to first day of week (Monday) */
4489  day0 = j2day(day4 - 1);
4490 
4491  /*
4492  * We need the first week containing a Thursday, otherwise this day falls
4493  * into the previous year for purposes of counting weeks
4494  */
4495  if (dayn < day4 - day0)
4496  {
4497  day4 = date2j(year - 1, 1, 4);
4498 
4499  /* day0 == offset to first day of week (Monday) */
4500  day0 = j2day(day4 - 1);
4501  }
4502 
4503  result = (dayn - (day4 - day0)) / 7 + 1;
4504 
4505  /*
4506  * Sometimes the last few days in a year will fall into the first week of
4507  * the next year, so check for this.
4508  */
4509  if (result >= 52)
4510  {
4511  day4 = date2j(year + 1, 1, 4);
4512 
4513  /* day0 == offset to first day of week (Monday) */
4514  day0 = j2day(day4 - 1);
4515 
4516  if (dayn >= day4 - day0)
4517  result = (dayn - (day4 - day0)) / 7 + 1;
4518  }
4519 
4520  return (int) result;
4521 }
int j2day(int date)
Definition: datetime.c:342
int date2j(int y, int m, int d)
Definition: datetime.c:284
double float8
Definition: c.h:565

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

4531 {
4532  float8 result;
4533  int day0,
4534  day4,
4535  dayn;
4536 
4537  /* current day */
4538  dayn = date2j(year, mon, mday);
4539 
4540  /* fourth day of current year */
4541  day4 = date2j(year, 1, 4);
4542 
4543  /* day0 == offset to first day of week (Monday) */
4544  day0 = j2day(day4 - 1);
4545 
4546  /*
4547  * We need the first week containing a Thursday, otherwise this day falls
4548  * into the previous year for purposes of counting weeks
4549  */
4550  if (dayn < day4 - day0)
4551  {
4552  day4 = date2j(year - 1, 1, 4);
4553 
4554  /* day0 == offset to first day of week (Monday) */
4555  day0 = j2day(day4 - 1);
4556 
4557  year--;
4558  }
4559 
4560  result = (dayn - (day4 - day0)) / 7 + 1;
4561 
4562  /*
4563  * Sometimes the last few days in a year will fall into the first week of
4564  * the next year, so check for this.
4565  */
4566  if (result >= 52)
4567  {
4568  day4 = date2j(year + 1, 1, 4);
4569 
4570  /* day0 == offset to first day of week (Monday) */
4571  day0 = j2day(day4 - 1);
4572 
4573  if (dayn >= day4 - day0)
4574  year++;
4575  }
4576 
4577  return year;
4578 }

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

4588 {
4589  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4590 }
int isoweek2j(int year, int week)
Definition: timestamp.c:4424
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4530

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

Referenced by DCH_to_char().

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 2037 of file timestamp.c.

2038 {
2039  dt -= (tz * USECS_PER_SEC);
2040  return dt;
2041 }
#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 1789 of file timestamp.c.

1790 {
1791  TimeOffset time;
1792 
1793  time = jd;
1794 
1795  *hour = time / USECS_PER_HOUR;
1796  time -= (*hour) * USECS_PER_HOUR;
1797  *min = time / USECS_PER_MINUTE;
1798  time -= (*min) * USECS_PER_MINUTE;
1799  *sec = time / USECS_PER_SEC;
1800  *fsec = time - (*sec * USECS_PER_SEC);
1801 } /* 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 1527 of file timestamp.c.

1528 {
1529  if (TIMESTAMP_IS_NOBEGIN(dt))
1530  strcpy(str, EARLY);
1531  else if (TIMESTAMP_IS_NOEND(dt))
1532  strcpy(str, LATE);
1533  else /* shouldn't happen */
1534  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1535 }
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:159
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:154
#define EARLY
Definition: datetime.h:40
#define LATE
Definition: datetime.h:41

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

5379 {
5380  return interval_part_common(fcinfo, true);
5381 }
static Datum interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:5207

References interval_part_common().

◆ extract_timestamp()

Datum extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 4925 of file timestamp.c.

4926 {
4927  return timestamp_part_common(fcinfo, true);
4928 }
static Datum timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4661

References timestamp_part_common().

◆ extract_timestamptz()

Datum extract_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5197 of file timestamp.c.

5198 {
5199  return timestamptz_part_common(fcinfo, true);
5200 }
static Datum timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition: timestamp.c:4934

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:218
int64 TimestampTz
Definition: timestamp.h:39
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:220
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:234
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:201
#define SECS_PER_DAY
Definition: timestamp.h:125
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:202
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:156
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39

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

5768 {
5769  FuncCallContext *funcctx;
5771  Timestamp result;
5772 
5773  /* stuff done only on the first call of the function */
5774  if (SRF_IS_FIRSTCALL())
5775  {
5776  Timestamp start = PG_GETARG_TIMESTAMP(0);
5777  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5778  Interval *step = PG_GETARG_INTERVAL_P(2);
5779  MemoryContext oldcontext;
5780  Interval interval_zero;
5781 
5782  /* create a function context for cross-call persistence */
5783  funcctx = SRF_FIRSTCALL_INIT();
5784 
5785  /*
5786  * switch to memory context appropriate for multiple function calls
5787  */
5788  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5789 
5790  /* allocate memory for user context */
5793 
5794  /*
5795  * Use fctx to keep state from call to call. Seed current with the
5796  * original start value
5797  */
5798  fctx->current = start;
5799  fctx->finish = finish;
5800  fctx->step = *step;
5801 
5802  /* Determine sign of the interval */
5803  MemSet(&interval_zero, 0, sizeof(Interval));
5804  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5805 
5806  if (fctx->step_sign == 0)
5807  ereport(ERROR,
5808  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5809  errmsg("step size cannot equal zero")));
5810 
5811  funcctx->user_fctx = fctx;
5812  MemoryContextSwitchTo(oldcontext);
5813  }
5814 
5815  /* stuff done on every call of the function */
5816  funcctx = SRF_PERCALL_SETUP();
5817 
5818  /*
5819  * get the saved state and use current as the result for this iteration
5820  */
5821  fctx = funcctx->user_fctx;
5822  result = fctx->current;
5823 
5824  if (fctx->step_sign > 0 ?
5825  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5826  timestamp_cmp_internal(result, fctx->finish) >= 0)
5827  {
5828  /* increment current in preparation for next iteration */
5830  TimestampGetDatum(fctx->current),
5831  PointerGetDatum(&fctx->step)));
5832 
5833  /* do when there is more left to send */
5834  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5835  }
5836  else
5837  {
5838  /* do when there is no more left */
5839  SRF_RETURN_DONE(funcctx);
5840  }
5841 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2111
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2897
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2406
#define MemSet(start, val, len)
Definition: c.h:1008
int64 Timestamp
Definition: timestamp.h:38
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:633
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:299
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:305
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:301
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:323
void * palloc(Size size)
Definition: mcxt.c:1068
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PointerGetDatum(X)
Definition: postgres.h:600
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define TimestampGetDatum(X)
Definition: timestamp.h:31
#define DatumGetTimestamp(X)
Definition: timestamp.h:27

References generate_series_timestamp_fctx::current, DatumGetTimestamp, DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, generate_series_timestamp_fctx::finish, interval_cmp_internal(), MemoryContextSwitchTo(), MemSet, 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 5847 of file timestamp.c.

5848 {
5849  FuncCallContext *funcctx;
5851  TimestampTz result;
5852 
5853  /* stuff done only on the first call of the function */
5854  if (SRF_IS_FIRSTCALL())
5855  {
5857  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5858  Interval *step = PG_GETARG_INTERVAL_P(2);
5859  MemoryContext oldcontext;
5860  Interval interval_zero;
5861 
5862  /* create a function context for cross-call persistence */
5863  funcctx = SRF_FIRSTCALL_INIT();
5864 
5865  /*
5866  * switch to memory context appropriate for multiple function calls
5867  */
5868  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5869 
5870  /* allocate memory for user context */
5873 
5874  /*
5875  * Use fctx to keep state from call to call. Seed current with the
5876  * original start value
5877  */
5878  fctx->current = start;
5879  fctx->finish = finish;
5880  fctx->step = *step;
5881 
5882  /* Determine sign of the interval */
5883  MemSet(&interval_zero, 0, sizeof(Interval));
5884  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5885 
5886  if (fctx->step_sign == 0)
5887  ereport(ERROR,
5888  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5889  errmsg("step size cannot equal zero")));
5890 
5891  funcctx->user_fctx = fctx;
5892  MemoryContextSwitchTo(oldcontext);
5893  }
5894 
5895  /* stuff done on every call of the function */
5896  funcctx = SRF_PERCALL_SETUP();
5897 
5898  /*
5899  * get the saved state and use current as the result for this iteration
5900  */
5901  fctx = funcctx->user_fctx;
5902  result = fctx->current;
5903 
5904  if (fctx->step_sign > 0 ?
5905  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5906  timestamp_cmp_internal(result, fctx->finish) >= 0)
5907  {
5908  /* increment current in preparation for next iteration */
5911  PointerGetDatum(&fctx->step)));
5912 
5913  /* do when there is more left to send */
5914  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5915  }
5916  else
5917  {
5918  /* do when there is no more left */
5919  SRF_RETURN_DONE(funcctx);
5920  }
5921 }
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3002
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28

References generate_series_timestamptz_fctx::current, DatumGetTimestampTz, DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, generate_series_timestamptz_fctx::finish, interval_cmp_internal(), MemoryContextSwitchTo(), MemSet, FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PointerGetDatum, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_timestamptz_fctx::step, generate_series_timestamptz_fctx::step_sign, timestamp_cmp_internal(), timestamptz_pl_interval(), TimestampTzGetDatum, and FuncCallContext::user_fctx.

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1574 of file timestamp.c.

1575 {
1576  TimestampTz result;
1577  struct timeval tp;
1578 
1579  gettimeofday(&tp, NULL);
1580 
1581  result = (TimestampTz) tp.tv_sec -
1583  result = (result * USECS_PER_SEC) + tp.tv_usec;
1584 
1585  return result;
1586 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104

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

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), asyncQueueFillWarning(), autoprewarm_main(), AutoVacLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), begin_startup_progress_phase(), check_log_duration(), CheckPointGuts(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), drandom(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), InitProcessGlobals(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), PerformWalRecovery(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), pgss_shmem_startup(), pgstat_build_snapshot(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_archiver(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_vacuum(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_counters(), pgstat_reset_of_kind(), pgstat_reset_slru(), PostgresSingleUserMain(), PostmasterMain(), PrepareTransaction(), process_syncing_tables_for_apply(), ProcessPendingWrites(), ProcessRepliesIfAny(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), SetCurrentTransactionStopTimestamp(), 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 2069 of file timestamp.c.

2070 {
2071  struct pg_tm *t0;
2072  pg_time_t epoch = 0;
2073 
2074  t0 = pg_gmtime(&epoch);
2075 
2076  if (t0 == NULL)
2077  elog(ERROR, "could not convert epoch to timestamp: %m");
2078 
2079  tm->tm_year = t0->tm_year;
2080  tm->tm_mon = t0->tm_mon;
2081  tm->tm_mday = t0->tm_mday;
2082  tm->tm_hour = t0->tm_hour;
2083  tm->tm_min = t0->tm_min;
2084  tm->tm_sec = t0->tm_sec;
2085 
2086  tm->tm_year += 1900;
2087  tm->tm_mon++;
2088 }
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
static struct pg_tm tm
Definition: localtime.c:102
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1387
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

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

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

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1592 of file timestamp.c.

1593 {
1594  TimestampTz ts;
1595 
1597  if (typmod >= 0)
1598  AdjustTimestampForTypmod(&ts, typmod);
1599  return ts;
1600 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:395
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:851

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1606 of file timestamp.c.

1607 {
1608  Timestamp ts;
1609 
1611  if (typmod >= 0)
1612  AdjustTimestampForTypmod(&ts, typmod);
1613  return ts;
1614 }
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5618

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

Referenced by ExecEvalSQLValueFunction().

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

Definition at line 3436 of file timestamp.c.

3437 {
3439  Interval *base = PG_GETARG_INTERVAL_P(1);
3440  Interval *offset = PG_GETARG_INTERVAL_P(2);
3441  bool sub = PG_GETARG_BOOL(3);
3442  bool less = PG_GETARG_BOOL(4);
3443  Interval *sum;
3444 
3445  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3446  ereport(ERROR,
3447  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3448  errmsg("invalid preceding or following size in window function")));
3449 
3450  /* We don't currently bother to avoid overflow hazards here */
3451  if (sub)
3453  IntervalPGetDatum(base),
3454  IntervalPGetDatum(offset)));
3455  else
3457  IntervalPGetDatum(base),
3458  IntervalPGetDatum(offset)));
3459 
3460  if (less)
3462  else
3464 }
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2384
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3195
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3161
#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
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
#define IntervalPGetDatum(X)
Definition: timestamp.h:33

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

3406 {
3408  Timestamp base = PG_GETARG_TIMESTAMP(1);
3409  Interval *offset = PG_GETARG_INTERVAL_P(2);
3410  bool sub = PG_GETARG_BOOL(3);
3411  bool less = PG_GETARG_BOOL(4);
3412  Timestamp sum;
3413 
3414  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3415  ereport(ERROR,
3416  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3417  errmsg("invalid preceding or following size in window function")));
3418 
3419  /* We don't currently bother to avoid overflow hazards here */
3420  if (sub)
3422  TimestampGetDatum(base),
3423  IntervalPGetDatum(offset)));
3424  else
3426  TimestampGetDatum(base),
3427  IntervalPGetDatum(offset)));
3428 
3429  if (less)
3430  PG_RETURN_BOOL(val <= sum);
3431  else
3432  PG_RETURN_BOOL(val >= sum);
3433 }
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2976

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

3375 {
3378  Interval *offset = PG_GETARG_INTERVAL_P(2);
3379  bool sub = PG_GETARG_BOOL(3);
3380  bool less = PG_GETARG_BOOL(4);
3381  TimestampTz sum;
3382 
3383  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3384  ereport(ERROR,
3385  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3386  errmsg("invalid preceding or following size in window function")));
3387 
3388  /* We don't currently bother to avoid overflow hazards here */
3389  if (sub)
3391  TimestampTzGetDatum(base),
3392  IntervalPGetDatum(offset)));
3393  else
3395  TimestampTzGetDatum(base),
3396  IntervalPGetDatum(offset)));
3397 
3398  if (less)
3399  PG_RETURN_BOOL(val <= sum);
3400  else
3401  PG_RETURN_BOOL(val >= sum);
3402 }
Datum timestamptz_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3086

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

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 1962 of file timestamp.c.

1963 {
1964  TimeOffset time;
1965  TimeOffset tfrac;
1966 
1967  itm->tm_year = span.month / MONTHS_PER_YEAR;
1968  itm->tm_mon = span.month % MONTHS_PER_YEAR;
1969  itm->tm_mday = span.day;
1970  time = span.time;
1971 
1972  tfrac = time / USECS_PER_HOUR;
1973  time -= tfrac * USECS_PER_HOUR;
1974  itm->tm_hour = tfrac;
1975  tfrac = time / USECS_PER_MINUTE;
1976  time -= tfrac * USECS_PER_MINUTE;
1977  itm->tm_min = (int) tfrac;
1978  tfrac = time / USECS_PER_SEC;
1979  time -= tfrac * USECS_PER_SEC;
1980  itm->tm_sec = (int) tfrac;
1981  itm->tm_usec = (int) time;
1982 }
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 3478 of file timestamp.c.

3479 {
3480  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3482  Datum *transdatums;
3483  int ndatums;
3484  Interval sumX,
3485  N;
3486  Interval *newsum;
3487  ArrayType *result;
3488 
3489  deconstruct_array(transarray,
3490  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3491  &transdatums, NULL, &ndatums);
3492  if (ndatums != 2)
3493  elog(ERROR, "expected 2-element interval array");
3494 
3495  sumX = *(DatumGetIntervalP(transdatums[0]));
3496  N = *(DatumGetIntervalP(transdatums[1]));
3497 
3499  IntervalPGetDatum(&sumX),
3501  N.time += 1;
3502 
3503  transdatums[0] = IntervalPGetDatum(newsum);
3504  transdatums[1] = IntervalPGetDatum(&N);
3505 
3506  result = construct_array(transdatums, 2,
3507  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3508 
3509  PG_RETURN_ARRAYTYPE_P(result);
3510 }
#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:3319
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3491
#define newval
uintptr_t Datum
Definition: postgres.h:411

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

3563 {
3564  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3566  Datum *transdatums;
3567  int ndatums;
3568  Interval sumX,
3569  N;
3570  Interval *newsum;
3571  ArrayType *result;
3572 
3573  deconstruct_array(transarray,
3574  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3575  &transdatums, NULL, &ndatums);
3576  if (ndatums != 2)
3577  elog(ERROR, "expected 2-element interval array");
3578 
3579  sumX = *(DatumGetIntervalP(transdatums[0]));
3580  N = *(DatumGetIntervalP(transdatums[1]));
3581 
3583  IntervalPGetDatum(&sumX),
3585  N.time -= 1;
3586 
3587  transdatums[0] = IntervalPGetDatum(newsum);
3588  transdatums[1] = IntervalPGetDatum(&N);
3589 
3590  result = construct_array(transdatums, 2,
3591  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3592 
3593  PG_RETURN_ARRAYTYPE_P(result);
3594 }

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

3598 {
3599  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3600  Datum *transdatums;
3601  int ndatums;
3602  Interval sumX,
3603  N;
3604 
3605  deconstruct_array(transarray,
3606  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3607  &transdatums, NULL, &ndatums);
3608  if (ndatums != 2)
3609  elog(ERROR, "expected 2-element interval array");
3610 
3611  sumX = *(DatumGetIntervalP(transdatums[0]));
3612  N = *(DatumGetIntervalP(transdatums[1]));
3613 
3614  /* SQL defines AVG of no values to be NULL */
3615  if (N.time == 0)
3616  PG_RETURN_NULL();
3617 
3619  IntervalPGetDatum(&sumX),
3620  Float8GetDatum((double) N.time));
3621 }
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3322
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1692
#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 2469 of file timestamp.c.

2470 {
2471  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2472  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2473 
2474  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2475 }
#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 ( Interval interval1,
Interval interval2 
)
static

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2384 of file timestamp.c.

2385 {
2386  INT128 span;
2387  int64 days;
2388 
2389  /*
2390  * Combine the month and day fields into an integral number of days.
2391  * Because the inputs are int32, int64 arithmetic suffices here.
2392  */
2393  days = interval->month * INT64CONST(30);
2394  days += interval->day;
2395 
2396  /* Widen time field to 128 bits */
2397  span = int64_to_int128(interval->time);
2398 
2399  /* Scale up days to microseconds, forming a 128-bit product */
2401 
2402  return span;
2403 }
const char *const days[]
Definition: datetime.c:83
#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 3513 of file timestamp.c.

3514 {
3515  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3516  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3517  Datum *transdatums1;
3518  Datum *transdatums2;
3519  int ndatums1;
3520  int ndatums2;
3521  Interval sum1,
3522  N1;
3523  Interval sum2,
3524  N2;
3525 
3526  Interval *newsum;
3527  ArrayType *result;
3528 
3529  deconstruct_array(transarray1,
3530  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3531  &transdatums1, NULL, &ndatums1);
3532  if (ndatums1 != 2)
3533  elog(ERROR, "expected 2-element interval array");
3534 
3535  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3536  N1 = *(DatumGetIntervalP(transdatums1[1]));
3537 
3538  deconstruct_array(transarray2,
3539  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3540  &transdatums2, NULL, &ndatums2);
3541  if (ndatums2 != 2)
3542  elog(ERROR, "expected 2-element interval array");
3543 
3544  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3545  N2 = *(DatumGetIntervalP(transdatums2[1]));
3546 
3548  IntervalPGetDatum(&sum1),
3549  IntervalPGetDatum(&sum2)));
3550  N1.time += N2.time;
3551 
3552  transdatums1[0] = IntervalPGetDatum(newsum);
3553  transdatums1[1] = IntervalPGetDatum(&N1);
3554 
3555  result = construct_array(transdatums1, 2,
3556  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3557 
3558  PG_RETURN_ARRAYTYPE_P(result);
3559 }

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

3323 {
3324  Interval *span = PG_GETARG_INTERVAL_P(0);
3325  float8 factor = PG_GETARG_FLOAT8(1);
3326  double month_remainder_days,
3327  sec_remainder;
3328  int32 orig_month = span->month,
3329  orig_day = span->day;
3330  Interval *result;
3331 
3332  result = (Interval *) palloc(sizeof(Interval));
3333 
3334  if (factor == 0.0)
3335  ereport(ERROR,
3336  (errcode(ERRCODE_DIVISION_BY_ZERO),
3337  errmsg("division by zero")));
3338 
3339  result->month = (int32) (span->month / factor);
3340  result->day = (int32) (span->day / factor);
3341 
3342  /*
3343  * Fractional months full days into days. See comment in interval_mul().
3344  */
3345  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3346  month_remainder_days = TSROUND(month_remainder_days);
3347  sec_remainder = (orig_day / factor - result->day +
3348  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3349  sec_remainder = TSROUND(sec_remainder);
3350  if (Abs(sec_remainder) >= SECS_PER_DAY)
3351  {
3352  result->day += (int) (sec_remainder / SECS_PER_DAY);
3353  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3354  }
3355 
3356  /* cascade units down */
3357  result->day += (int32) month_remainder_days;
3358  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3359 
3360  PG_RETURN_INTERVAL_P(result);
3361 }
#define Abs(x)
Definition: c.h:992
#define DAYS_PER_MONTH
Definition: timestamp.h:116
#define TSROUND(j)
Definition: timestamp.h:100
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41

References Abs, 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 2415 of file timestamp.c.

2416 {
2417  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2418  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2419 
2420  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2421 }

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

2059 {
2060  PG_RETURN_BOOL(true);
2061 }

References PG_RETURN_BOOL.

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2460 of file timestamp.c.

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

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

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

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

2486 {
2489  int64 span64;
2490 
2491  /*
2492  * Use only the least significant 64 bits for hashing. The upper 64 bits
2493  * seldom add any useful information, and besides we must do it like this
2494  * for compatibility with hashes calculated before use of INT128 was
2495  * introduced.
2496  */
2497  span64 = int128_to_int64(span);
2498 
2500 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:84
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define Int64GetDatumFast(X)
Definition: postgres.h:804

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

2504 {
2507  int64 span64;
2508 
2509  /* Same approach as interval_hash */
2510  span64 = int128_to_int64(span);
2511 
2513  PG_GETARG_DATUM(1));
2514 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:104

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

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 883 of file timestamp.c.

884 {
885  char *str = PG_GETARG_CSTRING(0);
886 
887 #ifdef NOT_USED
888  Oid typelem = PG_GETARG_OID(1);
889 #endif
890  int32 typmod = PG_GETARG_INT32(2);
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 
902  itm_in->tm_year = 0;
903  itm_in->tm_mon = 0;
904  itm_in->tm_mday = 0;
905  itm_in->tm_usec = 0;
906 
907  if (typmod >= 0)
908  range = INTERVAL_RANGE(typmod);
909  else
911 
912  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
913  ftype, MAXDATEFIELDS, &nf);
914  if (dterr == 0)
915  dterr = DecodeInterval(field, ftype, nf, range,
916  &dtype, itm_in);
917 
918  /* if those functions think it's a bad format, try ISO8601 style */
919  if (dterr == DTERR_BAD_FORMAT)
920  dterr = DecodeISO8601Interval(str,
921  &dtype, itm_in);
922 
923  if (dterr != 0)
924  {
925  if (dterr == DTERR_FIELD_OVERFLOW)
926  dterr = DTERR_INTERVAL_OVERFLOW;
927  DateTimeParseError(dterr, str, "interval");
928  }
929 
930  result = (Interval *) palloc(sizeof(Interval));
931 
932  switch (dtype)
933  {
934  case DTK_DELTA:
935  if (itmin2interval(itm_in, result) != 0)
936  ereport(ERROR,
937  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
938  errmsg("interval out of range")));
939  break;
940 
941  default:
942  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
943  dtype, str);
944  }
945 
946  AdjustIntervalForTypmod(result, typmod);
947 
948  PG_RETURN_INTERVAL_P(result);
949 }
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:752
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_itm_in *itm_in)
Definition: datetime.c:3333
int DecodeISO8601Interval(char *str, int *dtype, struct pg_itm_in *itm_in)
Definition: datetime.c:3769
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:4024
int itmin2interval(struct pg_itm_in *itm_in, Interval *span)
Definition: timestamp.c:2018
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1324
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define MAXDATEFIELDS
Definition: datetime.h:203
#define DTERR_INTERVAL_OVERFLOW
Definition: datetime.h:284
#define DTK_DELTA
Definition: datetime.h:160
#define DTERR_BAD_FORMAT
Definition: datetime.h:281
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:282
unsigned int Oid
Definition: postgres_ext.h:31
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, ereport, 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, 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 2854 of file timestamp.c.

2855 {
2856  Interval *span = PG_GETARG_INTERVAL_P(0);
2857  Interval *result;
2858  int32 wholemonth;
2859 
2860  result = (Interval *) palloc(sizeof(Interval));
2861  result->month = span->month;
2862  result->day = span->day;
2863  result->time = span->time;
2864 
2865  wholemonth = result->day / DAYS_PER_MONTH;
2866  result->day -= wholemonth * DAYS_PER_MONTH;
2867  if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2868  ereport(ERROR,
2869  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2870  errmsg("interval out of range")));
2871 
2872  if (result->month > 0 && result->day < 0)
2873  {
2874  result->day += DAYS_PER_MONTH;
2875  result->month--;
2876  }
2877  else if (result->month < 0 && result->day > 0)
2878  {
2879  result->day -= DAYS_PER_MONTH;
2880  result->month++;
2881  }
2882 
2883  PG_RETURN_INTERVAL_P(result);
2884 }
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 2816 of file timestamp.c.

2817 {
2818  Interval *span = PG_GETARG_INTERVAL_P(0);
2819  Interval *result;
2820  TimeOffset wholeday;
2821 
2822  result = (Interval *) palloc(sizeof(Interval));
2823  result->month = span->month;
2824  result->day = span->day;
2825  result->time = span->time;
2826 
2827  TMODULO(result->time, wholeday, USECS_PER_DAY);
2828  if (pg_add_s32_overflow(result->day, wholeday, &result->day))
2829  ereport(ERROR,
2830  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2831  errmsg("interval out of range")));
2832 
2833  if (result->day > 0 && result->time < 0)
2834  {
2835  result->time += USECS_PER_DAY;
2836  result->day--;
2837  }
2838  else if (result->day < 0 && result->time > 0)
2839  {
2840  result->time -= USECS_PER_DAY;
2841  result->day++;
2842  }
2843 
2844  PG_RETURN_INTERVAL_P(result);
2845 }
#define TMODULO(t, q, u)
Definition: datetime.h:249

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

2741 {
2742  Interval *span = PG_GETARG_INTERVAL_P(0);
2743  Interval *result;
2744  TimeOffset wholeday;
2745  int32 wholemonth;
2746 
2747  result = (Interval *) palloc(sizeof(Interval));
2748  result->month = span->month;
2749  result->day = span->day;
2750  result->time = span->time;
2751 
2752  /* pre-justify days if it might prevent overflow */
2753  if ((result->day > 0 && result->time > 0) ||
2754  (result->day < 0 && result->time < 0))
2755  {
2756  wholemonth = result->day / DAYS_PER_MONTH;
2757  result->day -= wholemonth * DAYS_PER_MONTH;
2758  if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2759  ereport(ERROR,
2760  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2761  errmsg("interval out of range")));
2762  }
2763 
2764  /*
2765  * Since TimeOffset is int64, abs(wholeday) can't exceed about 1.07e8. If
2766  * we pre-justified then abs(result->day) is less than DAYS_PER_MONTH, so
2767  * this addition can't overflow. If we didn't pre-justify, then day and
2768  * time are of different signs, so it still can't overflow.
2769  */
2770  TMODULO(result->time, wholeday, USECS_PER_DAY);
2771  result->day += wholeday;
2772 
2773  wholemonth = result->day / DAYS_PER_MONTH;
2774  result->day -= wholemonth * DAYS_PER_MONTH;
2775  if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2776  ereport(ERROR,
2777  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2778  errmsg("interval out of range")));
2779 
2780  if (result->month > 0 &&
2781  (result->day < 0 || (result->day == 0 && result->time < 0)))
2782  {
2783  result->day += DAYS_PER_MONTH;
2784  result->month--;
2785  }
2786  else if (result->month < 0 &&
2787  (result->day > 0 || (result->day == 0 && result->time > 0)))
2788  {
2789  result->day -= DAYS_PER_MONTH;
2790  result->month++;
2791  }
2792 
2793  if (result->day > 0 && result->time < 0)
2794  {
2795  result->time += USECS_PER_DAY;
2796  result->day--;
2797  }
2798  else if (result->day < 0 && result->time > 0)
2799  {
2800  result->time -= USECS_PER_DAY;
2801  result->day++;
2802  }
2803 
2804  PG_RETURN_INTERVAL_P(result);
2805 }

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

3148 {
3149  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3150  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3151  Interval *result;
3152 
3153  if (interval_cmp_internal(interval1, interval2) > 0)
3154  result = interval1;
3155  else
3156  result = interval2;
3157  PG_RETURN_INTERVAL_P(result);
3158 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2451 of file timestamp.c.

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

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

2434 {
2435  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2436  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2437 
2438  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2439 }

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

3196 {
3197  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3198  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3199  Interval *result;
3200 
3201  result = (Interval *) palloc(sizeof(Interval));
3202 
3203  result->month = span1->month - span2->month;
3204  /* overflow check copied from int4mi */
3205  if (!SAMESIGN(span1->month, span2->month) &&
3206  !SAMESIGN(result->month, span1->month))
3207  ereport(ERROR,
3208  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3209  errmsg("interval out of range")));
3210 
3211  result->day = span1->day - span2->day;
3212  if (!SAMESIGN(span1->day, span2->day) &&
3213  !SAMESIGN(result->day, span1->day))
3214  ereport(ERROR,
3215  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3216  errmsg("interval out of range")));
3217 
3218  result->time = span1->time - span2->time;
3219  if (!SAMESIGN(span1->time, span2->time) &&
3220  !SAMESIGN(result->time, span1->time))
3221  ereport(ERROR,
3222  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3223  errmsg("interval out of range")));
3224 
3225  PG_RETURN_INTERVAL_P(result);
3226 }
#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 3235 of file timestamp.c.

3236 {
3237  Interval *span = PG_GETARG_INTERVAL_P(0);
3238  float8 factor = PG_GETARG_FLOAT8(1);
3239  double month_remainder_days,
3240  sec_remainder,
3241  result_double;
3242  int32 orig_month = span->month,
3243  orig_day = span->day;
3244  Interval *result;
3245 
3246  result = (Interval *) palloc(sizeof(Interval));
3247 
3248  result_double = span->month * factor;
3249  if (isnan(result_double) ||
3250  result_double > INT_MAX || result_double < INT_MIN)
3251  ereport(ERROR,
3252  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3253  errmsg("interval out of range")));
3254  result->month = (int32) result_double;
3255 
3256  result_double = span->day * factor;
3257  if (isnan(result_double) ||
3258  result_double > INT_MAX || result_double < INT_MIN)
3259  ereport(ERROR,
3260  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3261  errmsg("interval out of range")));
3262  result->day = (int32) result_double;
3263 
3264  /*
3265  * The above correctly handles the whole-number part of the month and day
3266  * products, but we have to do something with any fractional part
3267  * resulting when the factor is non-integral. We cascade the fractions
3268  * down to lower units using the conversion factors DAYS_PER_MONTH and
3269  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3270  * so by the representation. The user can choose to cascade up later,
3271  * using justify_hours and/or justify_days.
3272  */
3273 
3274  /*
3275  * Fractional months full days into days.
3276  *
3277  * Floating point calculation are inherently imprecise, so these
3278  * calculations are crafted to produce the most reliable result possible.
3279  * TSROUND() is needed to more accurately produce whole numbers where
3280  * appropriate.
3281  */
3282  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3283  month_remainder_days = TSROUND(month_remainder_days);
3284  sec_remainder = (orig_day * factor - result->day +
3285  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3286  sec_remainder = TSROUND(sec_remainder);
3287 
3288  /*
3289  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3290  * cascade from months and days. It might still be >24 if the combination
3291  * of cascade and the seconds factor operation itself.
3292  */
3293  if (Abs(sec_remainder) >= SECS_PER_DAY)
3294  {
3295  result->day += (int) (sec_remainder / SECS_PER_DAY);
3296  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3297  }
3298 
3299  /* cascade units down */
3300  result->day += (int32) month_remainder_days;
3301  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3302  if (isnan(result_double) || !FLOAT8_FITS_IN_INT64(result_double))
3303  ereport(ERROR,
3304  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3305  errmsg("interval out of range")));
3306  result->time = (int64) result_double;
3307 
3308  PG_RETURN_INTERVAL_P(result);
3309 }
#define FLOAT8_FITS_IN_INT64(num)
Definition: c.h:1104

References Abs, 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 2424 of file timestamp.c.

2425 {
2426  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2427  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2428 
2429  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2430 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 955 of file timestamp.c.

956 {
957  Interval *span = PG_GETARG_INTERVAL_P(0);
958  char *result;
959  struct pg_itm tt,
960  *itm = &tt;
961  char buf[MAXDATELEN + 1];
962 
963  interval2itm(*span, itm);
965 
966  result = pstrdup(buf);
967  PG_RETURN_CSTRING(result);
968 }
void EncodeInterval(struct pg_itm *itm, int style, char *str)
Definition: datetime.c:4501
void interval2itm(Interval span, struct pg_itm *itm)
Definition: timestamp.c:1962
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int IntervalStyle
Definition: globals.c:121
#define MAXDATELEN
Definition: datetime.h:201
char * pstrdup(const char *in)
Definition: mcxt.c:1305
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 5372 of file timestamp.c.

5373 {
5374  return interval_part_common(fcinfo, false);
5375 }

References interval_part_common().

◆ interval_part_common()

static Datum interval_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5207 of file timestamp.c.

5208 {
5209  text *units = PG_GETARG_TEXT_PP(0);
5211  int64 intresult;
5212  int type,
5213  val;
5214  char *lowunits;
5215  struct pg_itm tt,
5216  *tm = &tt;
5217 
5218  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
5219  VARSIZE_ANY_EXHDR(units),
5220  false);
5221 
5222  type = DecodeUnits(0, lowunits, &val);
5223  if (type == UNKNOWN_FIELD)
5224  type = DecodeSpecial(0, lowunits, &val);
5225 
5226  if (type == UNITS)
5227  {
5229  switch (val)
5230  {
5231  case DTK_MICROSEC:
5232  intresult = tm->tm_sec * INT64CONST(1000000) + tm->tm_usec;
5233  break;
5234 
5235  case DTK_MILLISEC:
5236  if (retnumeric)
5237  /*---
5238  * tm->tm_sec * 1000 + fsec / 1000
5239  * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5240  */
5241  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + tm->tm_usec, 3));
5242  else
5243  PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + tm->tm_usec / 1000.0);
5244  break;
5245 
5246  case DTK_SECOND:
5247  if (retnumeric)
5248  /*---
5249  * tm->tm_sec + fsec / 1'000'000
5250  * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5251  */
5252  PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + tm->tm_usec, 6));
5253  else
5254  PG_RETURN_FLOAT8(tm->tm_sec + tm->tm_usec / 1000000.0);
5255  break;
5256 
5257  case DTK_MINUTE:
5258  intresult = tm->tm_min;
5259  break;
5260 
5261  case DTK_HOUR:
5262  intresult = tm->tm_hour;
5263  break;
5264 
5265  case DTK_DAY:
5266  intresult = tm->tm_mday;
5267  break;
5268 
5269  case DTK_MONTH:
5270  intresult = tm->tm_mon;
5271  break;
5272 
5273  case DTK_QUARTER:
5274  intresult = (tm->tm_mon / 3) + 1;
5275  break;
5276 
5277  case DTK_YEAR:
5278  intresult = tm->tm_year;
5279  break;
5280 
5281  case DTK_DECADE:
5282  /* caution: C division may have negative remainder */
5283  intresult = tm->tm_year / 10;
5284  break;
5285 
5286  case DTK_CENTURY:
5287  /* caution: C division may have negative remainder */
5288  intresult = tm->tm_year / 100;
5289  break;
5290 
5291  case DTK_MILLENNIUM:
5292  /* caution: C division may have negative remainder */
5293  intresult = tm->tm_year / 1000;
5294  break;
5295 
5296  default:
5297  ereport(ERROR,
5298  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5299  errmsg("unit \"%s\" not supported for type %s",
5300  lowunits, format_type_be(INTERVALOID))));
5301  intresult = 0;
5302  }
5303  }
5304  else if (type == RESERV && val == DTK_EPOCH)
5305  {
5306  if (retnumeric)
5307  {
5308  Numeric result;
5309  int64 secs_from_day_month;
5310  int64 val;
5311 
5312  /*
5313  * To do this calculation in integer arithmetic even though
5314  * DAYS_PER_YEAR is fractional, multiply everything by 4 and then
5315  * divide by 4 again at the end. This relies on DAYS_PER_YEAR
5316  * being a multiple of 0.25 and on SECS_PER_DAY being a multiple
5317  * of 4.
5318  */
5319  secs_from_day_month = ((int64) (4 * DAYS_PER_YEAR) * (interval->month / MONTHS_PER_YEAR) +
5320  (int64) (4 * DAYS_PER_MONTH) * (interval->month % MONTHS_PER_YEAR) +
5321  (int64) 4 * interval->day) * (SECS_PER_DAY / 4);
5322 
5323  /*---
5324  * result = secs_from_day_month + interval->time / 1'000'000
5325  * = (secs_from_day_month * 1'000'000 + interval->time) / 1'000'000
5326  */
5327 
5328  /*
5329  * Try the computation inside int64; if it overflows, do it in
5330  * numeric (slower). This overflow happens around 10^9 days, so
5331  * not common in practice.
5332  */
5333  if (!pg_mul_s64_overflow(secs_from_day_month, 1000000, &val) &&
5335  result = int64_div_fast_to_numeric(val, 6);
5336  else
5337  result =
5339  int64_to_numeric(secs_from_day_month),
5340  NULL);
5341 
5342  PG_RETURN_NUMERIC(result);
5343  }
5344  else
5345  {
5346  float8 result;
5347 
5348  result = interval->time / 1000000.0;
5349  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
5350  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
5351  result += ((double) SECS_PER_DAY) * interval->day;
5352 
5353  PG_RETURN_FLOAT8(result);
5354  }
5355  }
5356  else
5357  {
5358  ereport(ERROR,
5359  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5360  errmsg("unit \"%s\" not recognized for type %s",
5361  lowunits, format_type_be(INTERVALOID))));
5362  intresult = 0;
5363  }
5364 
5365  if (retnumeric)
5366  PG_RETURN_NUMERIC(int64_to_numeric(intresult));
5367  else
5368  PG_RETURN_FLOAT8(intresult);
5369 }
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3280
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3987
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4137
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4158
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2784
#define DAYS_PER_YEAR
Definition: timestamp.h:107
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define DTK_EPOCH
Definition: datetime.h:153
#define UNKNOWN_FIELD
Definition: datetime.h:125
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_SECOND
Definition: datetime.h:161
#define DTK_QUARTER
Definition: datetime.h:167
#define DTK_CENTURY
Definition: datetime.h:170
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
#define DTK_MILLENNIUM
Definition: datetime.h:171
#define DTK_HOUR
Definition: datetime.h:163
#define DTK_MICROSEC
Definition: datetime.h:173
#define DTK_YEAR
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MINUTE
Definition: datetime.h:162
#define UNITS
Definition: datetime.h:108
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:64
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37
Definition: c.h:622

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

3162 {
3163  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3164  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3165  Interval *result;
3166 
3167  result = (Interval *) palloc(sizeof(Interval));
3168 
3169  result->month = span1->month + span2->month;
3170  /* overflow check copied from int4pl */
3171  if (SAMESIGN(span1->month, span2->month) &&
3172  !SAMESIGN(result->month, span1->month))
3173  ereport(ERROR,
3174  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3175  errmsg("interval out of range")));
3176 
3177  result->day = span1->day + span2->day;
3178  if (SAMESIGN(span1->day, span2->day) &&
3179  !SAMESIGN(result->day, span1->day))
3180  ereport(ERROR,
3181  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3182  errmsg("interval out of range")));
3183 
3184  result->time = span1->time + span2->time;
3185  if (SAMESIGN(span1->time, span2->time) &&
3186  !SAMESIGN(result->time, span1->time))
3187  ereport(ERROR,
3188  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3189  errmsg("interval out of range")));
3190 
3191  PG_RETURN_INTERVAL_P(result);
3192 }

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

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

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

◆ interval_scale()

Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1305 of file timestamp.c.

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

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

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

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

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3132 of file timestamp.c.

3133 {
3134  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3135  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3136  Interval *result;
3137 
3138  /* use interval_cmp_internal to be sure this agrees with comparisons */
3139  if (interval_cmp_internal(interval1, interval2) < 0)
3140  result = interval1;
3141  else
3142  result = interval2;
3143  PG_RETURN_INTERVAL_P(result);
3144 }

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1242 of file timestamp.c.

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

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

4332 {
4333  text *units = PG_GETARG_TEXT_PP(0);
4335  Interval *result;
4336  int type,
4337  val;
4338  char *lowunits;
4339  struct pg_itm tt,
4340  *tm = &tt;
4341 
4342  result = (Interval *) palloc(sizeof(Interval));
4343 
4344  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4345  VARSIZE_ANY_EXHDR(units),
4346  false);
4347 
4348  type = DecodeUnits(0, lowunits, &val);
4349 
4350  if (type == UNITS)
4351  {
4353  switch (val)
4354  {
4355  case DTK_MILLENNIUM:
4356  /* caution: C division may have negative remainder */
4357  tm->tm_year = (tm->tm_year / 1000) * 1000;
4358  /* FALL THRU */
4359  case DTK_CENTURY:
4360  /* caution: C division may have negative remainder */
4361  tm->tm_year = (tm->tm_year / 100) * 100;
4362  /* FALL THRU */
4363  case DTK_DECADE:
4364  /* caution: C division may have negative remainder */
4365  tm->tm_year = (tm->tm_year / 10) * 10;
4366  /* FALL THRU */
4367  case DTK_YEAR:
4368  tm->tm_mon = 0;
4369  /* FALL THRU */
4370  case DTK_QUARTER:
4371  tm->tm_mon = 3 * (tm->tm_mon / 3);
4372  /* FALL THRU */
4373  case DTK_MONTH:
4374  tm->tm_mday = 0;
4375  /* FALL THRU */
4376  case DTK_DAY:
4377  tm->tm_hour = 0;
4378  /* FALL THRU */
4379  case DTK_HOUR:
4380  tm->tm_min = 0;
4381  /* FALL THRU */
4382  case DTK_MINUTE:
4383  tm->tm_sec = 0;
4384  /* FALL THRU */
4385  case DTK_SECOND:
4386  tm->tm_usec = 0;
4387  break;
4388  case DTK_MILLISEC:
4389  tm->tm_usec = (tm->tm_usec / 1000) * 1000;
4390  break;
4391  case DTK_MICROSEC:
4392  break;
4393 
4394  default:
4395  ereport(ERROR,
4396  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4397  errmsg("unit \"%s\" not supported for type %s",
4398  lowunits, format_type_be(INTERVALOID)),
4399  (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
4400  }
4401 
4402  if (itm2interval(tm, result) != 0)
4403  ereport(ERROR,
4404  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4405  errmsg("interval out of range")));
4406  }
4407  else
4408  {
4409  ereport(ERROR,
4410  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4411  errmsg("unit \"%s\" not recognized for type %s",
4412  lowunits, format_type_be(INTERVALOID))));
4413  }
4414 
4415  PG_RETURN_INTERVAL_P(result);
4416 }
int itm2interval(struct pg_itm *itm, Interval *span)
Definition: timestamp.c:1989
int errdetail(const char *fmt,...)
Definition: elog.c:1037
#define DTK_WEEK
Definition: datetime.h:165

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

3104 {
3106  Interval *result;
3107 
3108  result = (Interval *) palloc(sizeof(Interval));
3109 
3110  result->time = -interval->time;
3111  /* overflow check copied from int4um */
3112  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3113  ereport(ERROR,
3114  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3115  errmsg("interval out of range")));
3116  result->day = -interval->day;
3117  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3118  ereport(ERROR,
3119  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3120  errmsg("interval out of range")));
3121  result->month = -interval->month;
3122  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3123  ereport(ERROR,
3124  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3125  errmsg("interval out of range")));
3126 
3127  PG_RETURN_INTERVAL_P(result);
3128 }

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

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

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

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

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

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

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

4445 {
4446  j2date(isoweek2j(*year, woy), year, mon, mday);
4447 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:309

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

4425 {
4426  int day0,
4427  day4;
4428 
4429  /* fourth day of current year */
4430  day4 = date2j(year, 1, 4);
4431 
4432  /* day0 == offset to first day of week (Monday) */
4433  day0 = j2day(day4 - 1);
4434 
4435  return ((week - 1) * 7) + (day4 - day0);
4436 }

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

4458 {
4459  int jday;
4460 
4461  jday = isoweek2j(*year, isoweek);
4462  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4463  if (wday > 1)
4464  jday += wday - 2;
4465  else
4466  jday += 6;
4467  j2date(jday, year, mon, mday);
4468 }

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 1989 of file timestamp.c.

1990 {
1991  int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
1992 
1993  if (total_months > INT_MAX || total_months < INT_MIN)
1994  return -1;
1995  span->month = (int32) total_months;
1996  span->day = itm->tm_mday;
1998  &span->time))
1999  return -1;
2000  /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2001  if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2002  &span->time))
2003  return -1;
2004  if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2005  &span->time))
2006  return -1;
2007  if (pg_add_s64_overflow(span->time, itm->tm_usec,
2008  &span->time))
2009  return -1;
2010  return 0;
2011 }

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

2019 {
2020  int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2021 
2022  if (total_months > INT_MAX || total_months < INT_MIN)
2023  return -1;
2024  span->month = (int32) total_months;
2025  span->day = itm_in->tm_mday;
2026  span->time = itm_in->tm_usec;
2027  return 0;
2028 }

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(), pg_timezone_abbrevs(), and pg_timezone_names().

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1491 of file timestamp.c.

1492 {
1493  int32 years = PG_GETARG_INT32(0);
1495  int32 weeks = PG_GETARG_INT32(2);
1496  int32 days = PG_GETARG_INT32(3);
1497  int32 hours = PG_GETARG_INT32(4);
1498  int32 mins = PG_GETARG_INT32(5);
1499  double secs = PG_GETARG_FLOAT8(6);
1500  Interval *result;
1501 
1502  /*
1503  * Reject out-of-range inputs. We really ought to check the integer
1504  * inputs as well, but it's not entirely clear what limits to apply.
1505  */
1506  if (isinf(secs) || isnan(secs))
1507  ereport(ERROR,
1508  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1509  errmsg("interval out of range")));
1510 
1511  result = (Interval *) palloc(sizeof(Interval));
1512  result->month = years * MONTHS_PER_YEAR + months;
1513  result->day = weeks * 7 + days;
1514 
1515  secs = rint(secs * USECS_PER_SEC);
1516  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1517  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1518  (int64) secs;
1519 
1520  PG_RETURN_INTERVAL_P(result);
1521 }
const char *const months[]
Definition: datetime.c:80
#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:2648
#define MINS_PER_HOUR
Definition: timestamp.h:128
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:194
bool float_time_overflows(int hour, int min, double sec)
Definition: date.c:1439
#define DTK_DATE_M
Definition: datetime.h:192
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:5601

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:2037
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1816
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:471
int32 fsec_t
Definition: timestamp.h:41
int64 timestamp
Definition: zic.c:94

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

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3312 of file timestamp.c.

3313 {
3314  /* Args are float8 and Interval *, but leave them as generic Datum */
3315  Datum factor = PG_GETARG_DATUM(0);
3316  Datum span = PG_GETARG_DATUM(1);
3317 
3318  return DirectFunctionCall2(interval_mul, span, factor);
3319 }
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3235

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

4606 {
4607  if ((type != UNITS) && (type != RESERV))
4608  ereport(ERROR,
4609  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4610  errmsg("unit \"%s\" not recognized for type %s",
4611  lowunits,
4612  format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
4613 
4614  switch (unit)
4615  {
4616  /* Oscillating units */
4617  case DTK_MICROSEC:
4618  case DTK_MILLISEC:
4619  case DTK_SECOND:
4620  case DTK_MINUTE:
4621  case DTK_HOUR:
4622  case DTK_DAY:
4623  case DTK_MONTH:
4624  case DTK_QUARTER:
4625  case DTK_WEEK:
4626  case DTK_DOW:
4627  case DTK_ISODOW:
4628  case DTK_DOY:
4629  case DTK_TZ:
4630  case DTK_TZ_MINUTE:
4631  case DTK_TZ_HOUR:
4632  return 0.0;
4633 
4634  /* Monotonically-increasing units */
4635  case DTK_YEAR:
4636  case DTK_DECADE:
4637  case DTK_CENTURY:
4638  case DTK_MILLENNIUM:
4639  case DTK_JULIAN:
4640  case DTK_ISOYEAR:
4641  case DTK_EPOCH:
4642  if (isNegative)
4643  return -get_float8_infinity();
4644  else
4645  return get_float8_infinity();
4646 
4647  default:
4648  ereport(ERROR,
4649  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4650  errmsg("unit \"%s\" not supported for type %s",
4651  lowunits,
4652  format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
4653  return 0.0; /* keep compiler quiet */
4654  }
4655 }
static float8 get_float8_infinity(void)
Definition: float.h:93
#define DTK_JULIAN
Definition: datetime.h:174
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DTK_TZ_MINUTE
Definition: datetime.h:179
#define DTK_ISODOW
Definition: datetime.h:181
#define DTK_ISOYEAR
Definition: datetime.h:180
#define DTK_DOY
Definition: datetime.h:177
#define DTK_TZ
Definition: datetime.h:147
#define DTK_DOW
Definition: datetime.h:176

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

References GetCurrentTransactionStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

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

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2523 of file timestamp.c.

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

References PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_BOOL, PG_RETURN_NULL, TIMESTAMP_GT, and TIMESTAMP_LT.

◆ parse_sane_timezone()

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

Definition at line 471 of file timestamp.c.

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

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

Referenced by make_timestamptz_at_timezone().

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1562 of file timestamp.c.

1563 {
1565 }
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 1556 of file timestamp.c.

1557 {
1559 }
TimestampTz PgStartTime
Definition: timestamp.c:53

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2091 of file timestamp.c.

2092 {
2093  Timestamp dt;
2094  struct pg_tm tt,
2095  *tm = &tt;
2096 
2097  GetEpochTime(tm);
2098  /* we don't bother to test for failure ... */
2099  tm2timestamp(tm, 0, NULL, &dt);
2100 
2101  return dt;
2102 } /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2069
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1912

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

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

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1544 of file timestamp.c.

1545 {
1547 }
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:860

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

2032 {
2033  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2034 }

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

1727 {
1728  TimestampTz result;
1729 
1730  result = (TimestampTz) tm -
1732  result *= USECS_PER_SEC;
1733 
1734  return result;
1735 }

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

1621 {
1622  struct timeval tp;
1623  char templ[128];
1624  char buf[128];
1625  pg_time_t tt;
1626 
1627  gettimeofday(&tp, NULL);
1628  tt = (pg_time_t) tp.tv_sec;
1629  pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1631  snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1632 
1634 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:128
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1342
text * cstring_to_text(const char *s)
Definition: varlena.c:188

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

5602 {
5604 }
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5549

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

5550 {
5551  TimestampTz result;
5552  struct pg_tm tt,
5553  *tm = &tt;
5554  fsec_t fsec;
5555  int tz;
5556 
5557  if (overflow)
5558  *overflow = 0;
5559 
5561  return timestamp;
5562 
5563  /* We don't expect this to fail, but check it pro forma */
5564  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
5565  {
5567 
5568  result = dt2local(timestamp, -tz);
5569 
5570  if (IS_VALID_TIMESTAMP(result))
5571  {
5572  return result;
5573  }
5574  else if (overflow)
5575  {
5576  if (result < MIN_TIMESTAMP)
5577  {
5578  *overflow = -1;
5579  TIMESTAMP_NOBEGIN(result);
5580  }
5581  else
5582  {
5583  *overflow = 1;
5584  TIMESTAMP_NOEND(result);
5585  }
5586  return result;
5587  }
5588  }
5589 
5590  ereport(ERROR,
5591  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5592  errmsg("timestamp out of range")));
5593 
5594  return 0;
5595 }
#define MIN_TIMESTAMP
Definition: timestamp.h:223

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

Referenced by timestamp2timestamptz(), and timestamp_cmp_timestamptz_internal().

◆ timestamp2tm()

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

Definition at line 1816 of file timestamp.c.

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

◆ timestamp_age()

Datum timestamp_age ( PG_FUNCTION_ARGS  )

Definition at line 3631 of file timestamp.c.

3632 {
3633  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
3634  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
3635  Interval *result;
3636  fsec_t fsec1,
3637  fsec2;
3638  struct pg_itm tt,
3639  *tm = &tt;
3640  struct pg_tm tt1,
3641  *tm1 = &tt1;
3642  struct pg_tm tt2,
3643  *tm2 = &tt2;
3644 
3645  result = (Interval *) palloc(sizeof(Interval));
3646 
3647  if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
3648  timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
3649  {
3650  /* form the symbolic difference */
3651  tm->tm_usec = fsec1 - fsec2;
3652  tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3653  tm->tm_min = tm1->tm_min - tm2->tm_min;
3654  tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3655  tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3656  tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3657  tm->tm_year = tm1->tm_year - tm2->tm_year;
3658 
3659  /* flip sign if necessary... */
3660  if (dt1 < dt2)
3661  {
3662  tm->tm_usec = -tm->tm_usec;
3663  tm->tm_sec = -tm->tm_sec;
3664  tm->tm_min = -tm->tm_min;
3665  tm->tm_hour = -tm->tm_hour;
3666  tm->tm_mday = -tm->tm_mday;
3667  tm->tm_mon = -tm->tm_mon;
3668  tm->tm_year = -tm->tm_year;
3669  }
3670 
3671  /* propagate any negative fields into the next higher field */
3672  while (tm->tm_usec < 0)
3673  {
3674  tm->tm_usec += USECS_PER_SEC;
3675  tm->tm_sec--;
3676  }
3677 
3678  while (tm->tm_sec < 0)
3679  {
3681  tm->tm_min--;
3682  }
3683 
3684  while (tm->tm_min < 0)
3685  {
3686  tm->tm_min += MINS_PER_HOUR;
3687  tm->tm_hour--;
3688  }
3689 
3690  while (tm->tm_hour < 0)
3691  {
3692  tm->tm_hour += HOURS_PER_DAY;
3693  tm->tm_mday--;
3694  }
3695 
3696  while (tm->tm_mday < 0)
3697  {
3698  if (dt1 < dt2)
3699  {
3700  tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
3701  tm->tm_mon--;
3702  }
3703  else
3704  {
3705  tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
3706  tm->tm_mon--;
3707  }
3708  }
3709 
3710  while (tm->tm_mon < 0)
3711  {
3713  tm->tm_year--;
3714  }
3715 
3716  /* recover sign if necessary... */
3717  if (dt1 < dt2)
3718  {
3719  tm->tm_usec = -tm->tm_usec;
3720  tm->tm_sec = -tm->tm_sec;
3721  tm->tm_min = -tm->tm_min;
3722  tm->tm_hour = -tm->tm_hour;
3723  tm->tm_mday = -tm->tm_mday;
3724  tm->tm_mon = -tm->tm_mon;
3725  tm->tm_year = -tm->tm_year;
3726  }
3727 
3728  if (itm2interval(tm, result) != 0)
3729  ereport(ERROR,
3730  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3731  errmsg("interval out of range")));
3732  }
3733  else
3734  ereport(ERROR,
3735  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3736  errmsg("timestamp out of range")));
3737 
3738  PG_RETURN_INTERVAL_P(result);
3739 }
const int day_tab[2][13]
Definition: datetime.c:74
#define HOURS_PER_DAY
Definition: timestamp.h:117
#define isleap(y)
Definition: datetime.h:272

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

3876 {
3877  Interval *stride = PG_GETARG_INTERVAL_P(0);
3879  Timestamp origin = PG_GETARG_TIMESTAMP(2);
3880  Timestamp result,
3881  tm_diff,
3882  stride_usecs,
3883  tm_delta;
3884 
3887 
3888  if (TIMESTAMP_NOT_FINITE(origin))
3889  ereport(ERROR,
3890  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3891  errmsg("origin out of range")));
3892 
3893  if (stride->month != 0)
3894  ereport(ERROR,
3895  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3896  errmsg("timestamps cannot be binned into intervals containing months or years")));
3897 
3898  stride_usecs = stride->day * USECS_PER_DAY + stride->time;
3899 
3900  if (stride_usecs <= 0)
3901  ereport(ERROR,
3902  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3903  errmsg("stride must be greater than zero")));
3904 
3905  tm_diff = timestamp - origin;
3906  tm_delta = tm_diff - tm_diff % stride_usecs;
3907 
3908  /*
3909  * Make sure the returned timestamp is at the start of the bin, even if
3910  * the origin is in the future.
3911  */
3912  if (origin > timestamp && stride_usecs > 1)
3913  tm_delta -= stride_usecs;
3914 
3915  result = origin + tm_delta;
3916 
3917  PG_RETURN_TIMESTAMP(result);
3918 }

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

2172 {
2173  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2174  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2175 
2177 }

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

2302 {
2303  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2305 
2307 }
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition: timestamp.c:2226

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

2227 {
2228  TimestampTz dt1;
2229  int overflow;
2230 
2231  dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2232  if (overflow > 0)
2233  {
2234  /* dt1 is larger than any finite timestamp, but less than infinity */
2235  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2236  }
2237  if (overflow < 0)
2238  {
2239  /* dt1 is less than any finite timestamp, but more than -infinity */
2240  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2241  }
2242 
2243  return timestamptz_cmp_internal(dt1, dt2);
2244 }
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:101

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

2118 {
2119  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2120  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2121 
2122  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) == 0);
2123 }

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

2248 {
2249  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2251 
2252  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) == 0);
2253 }

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

2183 {
2186 
2187  return timestamp_cmp_internal(a, b);
2188 }
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 2050 of file timestamp.c.

2051 {
2053 
2055 }

References PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and TIMESTAMP_NOT_FINITE.

◆ timestamp_ge()

Datum timestamp_ge ( PG_FUNCTION_ARGS  )

Definition at line 2162 of file timestamp.c.

2163 {
2164  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
2165  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
2166 
2167  PG_RETURN_BOOL(timestamp_cmp_internal(dt1, dt2) >= 0);
2168 }

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

2293 {
2294  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2296 
2297  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) >= 0);
2298 }

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

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

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

2275 {
2276  Timestamp timestampVal = PG_GETARG_TIMESTAMP(0);
2278 
2279  PG_RETURN_BOOL(timestamp_cmp_timestamptz_internal(timestampVal, dt2) > 0);
2280 }

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

2211 {
2212  return hashint8(fcinfo);
2213 }

References hashint8().

◆ timestamp_hash_extended()

Datum timestamp_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2216 of file timestamp.c.

2217 {
2218  return hashint8extended(fcinfo);
2219 }

References hashint8extended().

◆ timestamp_in()

Datum timestamp_in ( PG_FUNCTION_ARGS  )

Definition at line 146 of file timestamp.c.

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

References AdjustTimestampForTypmod(), DateTimeParseError(), DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, elog, ereport, errcode(), errmsg(), ERROR, MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, 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 5481 of file timestamp.c.

5482 {
5485  TimestampTz result;
5486  int tz;
5487 
5490 
5491  if (zone->month != 0 || zone->day != 0)
5492  ereport(ERROR,
5493  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5494  errmsg("interval time zone \"%s\" must not include months or days",
5496  PointerGetDatum(zone))))));
5497 
5498  tz = zone->time / USECS_PER_SEC;
5499 
5500  result = dt2local(timestamp, tz);
5501 
5502  if (!IS_VALID_TIMESTAMP(result))
5503  ereport(ERROR,
5504  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5505  errmsg("timestamp out of range")));
5506 
5507  PG_RETURN_TIMESTAMPTZ(result);
5508 } /* timestamp_izone() */
Datum interval_out(PG_FUNCTION_ARGS)
Definition: timestamp.c:955
#define DatumGetCString(X)
Definition: postgres.h:610

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

◆ timestamp_larger()

Datum timestamp_larger ( PG_FUNCTION_ARGS  )

Definition at line 2663 of file timestamp.c.

2664 {
2665  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
<