PostgreSQL Source Code  git master
timestamp.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <sys/time.h>
#include "access/hash.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/int128.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datetime.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 AdjustTimestampForTypmod (Timestamp *time, int32 typmod)
 
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_transform (PG_FUNCTION_ARGS)
 
Datum timestamp_scale (PG_FUNCTION_ARGS)
 
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_transform (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)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
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)
 
int interval2tm (Interval span, struct pg_tm *tm, fsec_t *fsec)
 
int tm2interval (struct pg_tm *tm, fsec_t fsec, 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)
 
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_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc (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)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum timestamp_zone_transform (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone_transform (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
Datum timestamp_timestamptz (PG_FUNCTION_ARGS)
 
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,
 
)    (((a) < 0) == ((b) < 0))

Definition at line 46 of file timestamp.c.

Referenced by interval2tm(), interval_mi(), interval_pl(), and interval_um().

◆ TIMESTAMP_GT

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

Referenced by overlaps_timestamp().

◆ TIMESTAMP_LT

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

Referenced by overlaps_timestamp().

Function Documentation

◆ AdjustIntervalForTypmod()

static void AdjustIntervalForTypmod ( Interval interval,
int32  typmod 
)
static

Definition at line 1320 of file timestamp.c.

References Interval::day, DAY, elog, ereport, errcode(), errmsg(), ERROR, HOUR, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_PRECISION, INTERVAL_RANGE, MAX_INTERVAL_PRECISION, MINUTE, Interval::month, 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().

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

◆ AdjustTimestampForTypmod()

static void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)
static

Definition at line 334 of file timestamp.c.

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

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

335 {
336  static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
337  INT64CONST(1000000),
338  INT64CONST(100000),
339  INT64CONST(10000),
340  INT64CONST(1000),
341  INT64CONST(100),
342  INT64CONST(10),
343  INT64CONST(1)
344  };
345 
346  static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
347  INT64CONST(500000),
348  INT64CONST(50000),
349  INT64CONST(5000),
350  INT64CONST(500),
351  INT64CONST(50),
352  INT64CONST(5),
353  INT64CONST(0)
354  };
355 
356  if (!TIMESTAMP_NOT_FINITE(*time)
357  && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
358  {
359  if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
360  ereport(ERROR,
361  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
362  errmsg("timestamp(%d) precision must be between %d and %d",
363  typmod, 0, MAX_TIMESTAMP_PRECISION)));
364 
365  if (*time >= INT64CONST(0))
366  {
367  *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
368  TimestampScales[typmod];
369  }
370  else
371  {
372  *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
373  * TimestampScales[typmod]);
374  }
375  }
376 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 102 of file timestamp.c.

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

103 {
104  if (typmod < 0)
105  ereport(ERROR,
106  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
107  errmsg("TIMESTAMP(%d)%s precision must not be negative",
108  typmod, (istz ? " WITH TIME ZONE" : ""))));
109  if (typmod > MAX_TIMESTAMP_PRECISION)
110  {
112  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
113  errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
114  typmod, (istz ? " WITH TIME ZONE" : ""),
116  typmod = MAX_TIMESTAMP_PRECISION;
117  }
118 
119  return typmod;
120 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 81 of file timestamp.c.

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

82 {
83  int32 *tl;
84  int n;
85 
86  tl = ArrayGetIntegerTypmods(ta, &n);
87 
88  /*
89  * we're not too tense about good error message here because grammar
90  * shouldn't allow wrong number of modifiers for TIMESTAMP
91  */
92  if (n != 1)
93  ereport(ERROR,
94  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
95  errmsg("invalid type modifier")));
96 
97  return anytimestamp_typmod_check(istz, tl[0]);
98 }
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:200
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:102
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ anytimestamp_typmodout()

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

Definition at line 124 of file timestamp.c.

References psprintf().

Referenced by timestamptypmodout(), and timestamptztypmodout().

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

◆ clock_timestamp()

Datum clock_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1546 of file timestamp.c.

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1547 {
1549 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40

◆ date2isoweek()

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

Definition at line 4196 of file timestamp.c.

References date2j(), and j2day().

Referenced by DCH_to_char(), timestamp_part(), timestamp_trunc(), timestamptz_part(), and timestamptz_trunc().

4197 {
4198  float8 result;
4199  int day0,
4200  day4,
4201  dayn;
4202 
4203  /* current day */
4204  dayn = date2j(year, mon, mday);
4205 
4206  /* fourth day of current year */
4207  day4 = date2j(year, 1, 4);
4208 
4209  /* day0 == offset to first day of week (Monday) */
4210  day0 = j2day(day4 - 1);
4211 
4212  /*
4213  * We need the first week containing a Thursday, otherwise this day falls
4214  * into the previous year for purposes of counting weeks
4215  */
4216  if (dayn < day4 - day0)
4217  {
4218  day4 = date2j(year - 1, 1, 4);
4219 
4220  /* day0 == offset to first day of week (Monday) */
4221  day0 = j2day(day4 - 1);
4222  }
4223 
4224  result = (dayn - (day4 - day0)) / 7 + 1;
4225 
4226  /*
4227  * Sometimes the last few days in a year will fall into the first week of
4228  * the next year, so check for this.
4229  */
4230  if (result >= 52)
4231  {
4232  day4 = date2j(year + 1, 1, 4);
4233 
4234  /* day0 == offset to first day of week (Monday) */
4235  day0 = j2day(day4 - 1);
4236 
4237  if (dayn >= day4 - day0)
4238  result = (dayn - (day4 - day0)) / 7 + 1;
4239  }
4240 
4241  return (int) result;
4242 }
double float8
Definition: c.h:458
int j2day(int date)
Definition: datetime.c:336
int date2j(int y, int m, int d)
Definition: datetime.c:276

◆ date2isoyear()

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

Definition at line 4250 of file timestamp.c.

References date2j(), and j2day().

Referenced by date2isoyearday(), DCH_to_char(), timestamp_part(), and timestamptz_part().

4251 {
4252  float8 result;
4253  int day0,
4254  day4,
4255  dayn;
4256 
4257  /* current day */
4258  dayn = date2j(year, mon, mday);
4259 
4260  /* fourth day of current year */
4261  day4 = date2j(year, 1, 4);
4262 
4263  /* day0 == offset to first day of week (Monday) */
4264  day0 = j2day(day4 - 1);
4265 
4266  /*
4267  * We need the first week containing a Thursday, otherwise this day falls
4268  * into the previous year for purposes of counting weeks
4269  */
4270  if (dayn < day4 - day0)
4271  {
4272  day4 = date2j(year - 1, 1, 4);
4273 
4274  /* day0 == offset to first day of week (Monday) */
4275  day0 = j2day(day4 - 1);
4276 
4277  year--;
4278  }
4279 
4280  result = (dayn - (day4 - day0)) / 7 + 1;
4281 
4282  /*
4283  * Sometimes the last few days in a year will fall into the first week of
4284  * the next year, so check for this.
4285  */
4286  if (result >= 52)
4287  {
4288  day4 = date2j(year + 1, 1, 4);
4289 
4290  /* day0 == offset to first day of week (Monday) */
4291  day0 = j2day(day4 - 1);
4292 
4293  if (dayn >= day4 - day0)
4294  year++;
4295  }
4296 
4297  return year;
4298 }
double float8
Definition: c.h:458
int j2day(int date)
Definition: datetime.c:336
int date2j(int y, int m, int d)
Definition: datetime.c:276

◆ date2isoyearday()

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

Definition at line 4307 of file timestamp.c.

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

Referenced by DCH_to_char().

4308 {
4309  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4310 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4250
int isoweek2j(int year, int week)
Definition: timestamp.c:4145
int date2j(int y, int m, int d)
Definition: datetime.c:276

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 1952 of file timestamp.c.

References USECS_PER_SEC.

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

1953 {
1954  dt -= (tz * USECS_PER_SEC);
1955  return dt;
1956 }
#define USECS_PER_SEC
Definition: timestamp.h:94

◆ dt2time()

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

Definition at line 1731 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1732 {
1733  TimeOffset time;
1734 
1735  time = jd;
1736 
1737  *hour = time / USECS_PER_HOUR;
1738  time -= (*hour) * USECS_PER_HOUR;
1739  *min = time / USECS_PER_MINUTE;
1740  time -= (*min) * USECS_PER_MINUTE;
1741  *sec = time / USECS_PER_SEC;
1742  *fsec = time - (*sec * USECS_PER_SEC);
1743 } /* dt2time() */
#define USECS_PER_SEC
Definition: timestamp.h:94
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int64 TimeOffset
Definition: timestamp.h:40

◆ EncodeSpecialTimestamp()

void EncodeSpecialTimestamp ( Timestamp  dt,
char *  str 
)

Definition at line 1523 of file timestamp.c.

References EARLY, elog, ERROR, LATE, TIMESTAMP_IS_NOBEGIN, and TIMESTAMP_IS_NOEND.

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

1524 {
1525  if (TIMESTAMP_IS_NOBEGIN(dt))
1526  strcpy(str, EARLY);
1527  else if (TIMESTAMP_IS_NOEND(dt))
1528  strcpy(str, LATE);
1529  else /* shouldn't happen */
1530  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1531 }
#define LATE
Definition: datetime.h:41
#define ERROR
Definition: elog.h:43
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define elog
Definition: elog.h:219
#define EARLY
Definition: datetime.h:40

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 711 of file timestamp.c.

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

712 {
713  float8 seconds = PG_GETARG_FLOAT8(0);
714  TimestampTz result;
715 
716  /* Deal with NaN and infinite inputs ... */
717  if (isnan(seconds))
718  ereport(ERROR,
719  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
720  errmsg("timestamp cannot be NaN")));
721 
722  if (isinf(seconds))
723  {
724  if (seconds < 0)
725  TIMESTAMP_NOBEGIN(result);
726  else
727  TIMESTAMP_NOEND(result);
728  }
729  else
730  {
731  /* Out of range? */
732  if (seconds <
734  || seconds >=
736  ereport(ERROR,
737  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
738  errmsg("timestamp out of range: \"%g\"", seconds)));
739 
740  /* Convert UNIX epoch to Postgres epoch */
742 
743  seconds = rint(seconds * USECS_PER_SEC);
744  result = (int64) seconds;
745 
746  /* Recheck in case roundoff produces something just out of range */
747  if (!IS_VALID_TIMESTAMP(result))
748  ereport(ERROR,
749  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
750  errmsg("timestamp out of range: \"%g\"",
751  PG_GETARG_FLOAT8(0))));
752  }
753 
754  PG_RETURN_TIMESTAMP(result);
755 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define SECS_PER_DAY
Definition: timestamp.h:86
int isinf(double x)
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define DATETIME_MIN_JULIAN
Definition: timestamp.h:179

◆ generate_series_timestamp()

Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 5259 of file timestamp.c.

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.

5260 {
5261  FuncCallContext *funcctx;
5263  Timestamp result;
5264 
5265  /* stuff done only on the first call of the function */
5266  if (SRF_IS_FIRSTCALL())
5267  {
5268  Timestamp start = PG_GETARG_TIMESTAMP(0);
5269  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5270  Interval *step = PG_GETARG_INTERVAL_P(2);
5271  MemoryContext oldcontext;
5272  Interval interval_zero;
5273 
5274  /* create a function context for cross-call persistence */
5275  funcctx = SRF_FIRSTCALL_INIT();
5276 
5277  /*
5278  * switch to memory context appropriate for multiple function calls
5279  */
5280  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5281 
5282  /* allocate memory for user context */
5285 
5286  /*
5287  * Use fctx to keep state from call to call. Seed current with the
5288  * original start value
5289  */
5290  fctx->current = start;
5291  fctx->finish = finish;
5292  fctx->step = *step;
5293 
5294  /* Determine sign of the interval */
5295  MemSet(&interval_zero, 0, sizeof(Interval));
5296  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5297 
5298  if (fctx->step_sign == 0)
5299  ereport(ERROR,
5300  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5301  errmsg("step size cannot equal zero")));
5302 
5303  funcctx->user_fctx = fctx;
5304  MemoryContextSwitchTo(oldcontext);
5305  }
5306 
5307  /* stuff done on every call of the function */
5308  funcctx = SRF_PERCALL_SETUP();
5309 
5310  /*
5311  * get the saved state and use current as the result for this iteration
5312  */
5313  fctx = funcctx->user_fctx;
5314  result = fctx->current;
5315 
5316  if (fctx->step_sign > 0 ?
5317  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5318  timestamp_cmp_internal(result, fctx->finish) >= 0)
5319  {
5320  /* increment current in preparation for next iteration */
5321  fctx->current = DatumGetTimestamp(
5323  TimestampGetDatum(fctx->current),
5324  PointerGetDatum(&fctx->step)));
5325 
5326  /* do when there is more left to send */
5327  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5328  }
5329  else
5330  {
5331  /* do when there is no more left */
5332  SRF_RETURN_DONE(funcctx);
5333  }
5334 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2023
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
#define PointerGetDatum(X)
Definition: postgres.h:539
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:908
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2795
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:110
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331
void * user_fctx
Definition: funcapi.h:91
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5340 of file timestamp.c.

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.

5341 {
5342  FuncCallContext *funcctx;
5344  TimestampTz result;
5345 
5346  /* stuff done only on the first call of the function */
5347  if (SRF_IS_FIRSTCALL())
5348  {
5350  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5351  Interval *step = PG_GETARG_INTERVAL_P(2);
5352  MemoryContext oldcontext;
5353  Interval interval_zero;
5354 
5355  /* create a function context for cross-call persistence */
5356  funcctx = SRF_FIRSTCALL_INIT();
5357 
5358  /*
5359  * switch to memory context appropriate for multiple function calls
5360  */
5361  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5362 
5363  /* allocate memory for user context */
5366 
5367  /*
5368  * Use fctx to keep state from call to call. Seed current with the
5369  * original start value
5370  */
5371  fctx->current = start;
5372  fctx->finish = finish;
5373  fctx->step = *step;
5374 
5375  /* Determine sign of the interval */
5376  MemSet(&interval_zero, 0, sizeof(Interval));
5377  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5378 
5379  if (fctx->step_sign == 0)
5380  ereport(ERROR,
5381  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5382  errmsg("step size cannot equal zero")));
5383 
5384  funcctx->user_fctx = fctx;
5385  MemoryContextSwitchTo(oldcontext);
5386  }
5387 
5388  /* stuff done on every call of the function */
5389  funcctx = SRF_PERCALL_SETUP();
5390 
5391  /*
5392  * get the saved state and use current as the result for this iteration
5393  */
5394  fctx = funcctx->user_fctx;
5395  result = fctx->current;
5396 
5397  if (fctx->step_sign > 0 ?
5398  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5399  timestamp_cmp_internal(result, fctx->finish) >= 0)
5400  {
5401  /* increment current in preparation for next iteration */
5402  fctx->current = DatumGetTimestampTz(
5405  PointerGetDatum(&fctx->step)));
5406 
5407  /* do when there is more left to send */
5408  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5409  }
5410  else
5411  {
5412  /* do when there is no more left */
5413  SRF_RETURN_DONE(funcctx);
5414  }
5415 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2023
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
#define PointerGetDatum(X)
Definition: postgres.h:539
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:908
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
#define ERROR
Definition: elog.h:43
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:110
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2900
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331
void * user_fctx
Definition: funcapi.h:91
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1570 of file timestamp.c.

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

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), asyncQueueFillWarning(), autoprewarm_main(), autovac_refresh_stats(), AutoVacLauncherMain(), backend_read_statsfile(), BackendInitialize(), BackgroundWriterMain(), check_log_duration(), CheckPointBuffers(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), enable_timeout_after(), enable_timeout_at(), enable_timeouts(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), launcher_determine_sleep(), lazy_vacuum_rel(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), perform_base_backup(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), pgstat_bestart(), pgstat_read_statsfiles(), pgstat_recv_inquiry(), pgstat_recv_resetsharedcounter(), pgstat_recv_resetsinglecounter(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_vacuum(), pgstat_send_archiver(), pgstat_write_statsfiles(), PostgresMain(), PostmasterMain(), PrepareTransaction(), process_syncing_tables_for_apply(), ProcessRepliesIfAny(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), reset_dbentry_counters(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), SetCurrentTransactionStopTimestamp(), StartupXLOG(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1571 {
1572  TimestampTz result;
1573  struct timeval tp;
1574 
1575  gettimeofday(&tp, NULL);
1576 
1577  result = (TimestampTz) tp.tv_sec -
1579  result = (result * USECS_PER_SEC) + tp.tv_usec;
1580 
1581  return result;
1582 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

Definition at line 1984 of file timestamp.c.

References epoch, pg_gmtime(), 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().

1985 {
1986  struct pg_tm *t0;
1987  pg_time_t epoch = 0;
1988 
1989  t0 = pg_gmtime(&epoch);
1990 
1991  tm->tm_year = t0->tm_year;
1992  tm->tm_mon = t0->tm_mon;
1993  tm->tm_mday = t0->tm_mday;
1994  tm->tm_hour = t0->tm_hour;
1995  tm->tm_min = t0->tm_min;
1996  tm->tm_sec = t0->tm_sec;
1997 
1998  tm->tm_year += 1900;
1999  tm->tm_mon++;
2000 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1355
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1588 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1589 {
1590  TimestampTz ts;
1591 
1593  if (typmod >= 0)
1594  AdjustTimestampForTypmod(&ts, typmod);
1595  return ts;
1596 }
int64 TimestampTz
Definition: timestamp.h:39
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:334
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:700

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1602 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1603 {
1604  Timestamp ts;
1605 
1607  if (typmod >= 0)
1608  AdjustTimestampForTypmod(&ts, typmod);
1609  return ts;
1610 }
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:334
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5110
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:700

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

Definition at line 3334 of file timestamp.c.

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.

3335 {
3337  Interval *base = PG_GETARG_INTERVAL_P(1);
3338  Interval *offset = PG_GETARG_INTERVAL_P(2);
3339  bool sub = PG_GETARG_BOOL(3);
3340  bool less = PG_GETARG_BOOL(4);
3341  Interval *sum;
3342 
3343  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3344  ereport(ERROR,
3345  (errcode(ERRCODE_INVALID_PRECEDING_FOLLOWING_SIZE),
3346  errmsg("invalid preceding or following size in window function")));
3347 
3348  /* We don't currently bother to avoid overflow hazards here */
3349  if (sub)
3351  IntervalPGetDatum(base),
3352  IntervalPGetDatum(offset)));
3353  else
3355  IntervalPGetDatum(base),
3356  IntervalPGetDatum(offset)));
3357 
3358  if (less)
3359  PG_RETURN_BOOL(interval_cmp_internal(val, sum) <= 0);
3360  else
3361  PG_RETURN_BOOL(interval_cmp_internal(val, sum) >= 0);
3362 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:244
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3093
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3059
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306

◆ in_range_timestamp_interval()

Datum in_range_timestamp_interval ( PG_FUNCTION_ARGS  )

Definition at line 3303 of file timestamp.c.

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

3304 {
3306  Timestamp base = PG_GETARG_TIMESTAMP(1);
3307  Interval *offset = PG_GETARG_INTERVAL_P(2);
3308  bool sub = PG_GETARG_BOOL(3);
3309  bool less = PG_GETARG_BOOL(4);
3310  Timestamp sum;
3311 
3312  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3313  ereport(ERROR,
3314  (errcode(ERRCODE_INVALID_PRECEDING_FOLLOWING_SIZE),
3315  errmsg("invalid preceding or following size in window function")));
3316 
3317  /* We don't currently bother to avoid overflow hazards here */
3318  if (sub)
3320  TimestampGetDatum(base),
3321  IntervalPGetDatum(offset)));
3322  else
3324  TimestampGetDatum(base),
3325  IntervalPGetDatum(offset)));
3326 
3327  if (less)
3328  PG_RETURN_BOOL(val <= sum);
3329  else
3330  PG_RETURN_BOOL(val >= sum);
3331 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:244
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2874
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2795
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define ereport(elevel, rest)
Definition: elog.h:122
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306
#define DatumGetTimestamp(X)
Definition: timestamp.h:27

◆ in_range_timestamptz_interval()

Datum in_range_timestamptz_interval ( PG_FUNCTION_ARGS  )

Definition at line 3272 of file timestamp.c.

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.

3273 {
3276  Interval *offset = PG_GETARG_INTERVAL_P(2);
3277  bool sub = PG_GETARG_BOOL(3);
3278  bool less = PG_GETARG_BOOL(4);
3279  TimestampTz sum;
3280 
3281  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3282  ereport(ERROR,
3283  (errcode(ERRCODE_INVALID_PRECEDING_FOLLOWING_SIZE),
3284  errmsg("invalid preceding or following size in window function")));
3285 
3286  /* We don't currently bother to avoid overflow hazards here */
3287  if (sub)
3289  TimestampTzGetDatum(base),
3290  IntervalPGetDatum(offset)));
3291  else
3293  TimestampTzGetDatum(base),
3294  IntervalPGetDatum(offset)));
3295 
3296  if (less)
3297  PG_RETURN_BOOL(val <= sum);
3298  else
3299  PG_RETURN_BOOL(val >= sum);
3300 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int64 TimestampTz
Definition: timestamp.h:39
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:244
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
Datum timestamptz_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2984
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2900
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
long val
Definition: informix.c:689
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306

◆ interval2tm()

int interval2tm ( Interval  span,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 1902 of file timestamp.c.

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, MONTHS_PER_YEAR, SAMESIGN, Interval::time, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1903 {
1904  TimeOffset time;
1905  TimeOffset tfrac;
1906 
1907  tm->tm_year = span.month / MONTHS_PER_YEAR;
1908  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1909  tm->tm_mday = span.day;
1910  time = span.time;
1911 
1912  tfrac = time / USECS_PER_HOUR;
1913  time -= tfrac * USECS_PER_HOUR;
1914  tm->tm_hour = tfrac;
1915  if (!SAMESIGN(tm->tm_hour, tfrac))
1916  ereport(ERROR,
1917  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1918  errmsg("interval out of range")));
1919  tfrac = time / USECS_PER_MINUTE;
1920  time -= tfrac * USECS_PER_MINUTE;
1921  tm->tm_min = tfrac;
1922  tfrac = time / USECS_PER_SEC;
1923  *fsec = time - (tfrac * USECS_PER_SEC);
1924  tm->tm_sec = tfrac;
1925 
1926  return 0;
1927 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USECS_PER_HOUR
Definition: timestamp.h:92
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ interval_accum()

Datum interval_accum ( PG_FUNCTION_ARGS  )

Definition at line 3376 of file timestamp.c.

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.

3377 {
3378  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3380  Datum *transdatums;
3381  int ndatums;
3382  Interval sumX,
3383  N;
3384  Interval *newsum;
3385  ArrayType *result;
3386 
3387  deconstruct_array(transarray,
3388  INTERVALOID, sizeof(Interval), false, 'd',
3389  &transdatums, NULL, &ndatums);
3390  if (ndatums != 2)
3391  elog(ERROR, "expected 2-element interval array");
3392 
3393  sumX = *(DatumGetIntervalP(transdatums[0]));
3394  N = *(DatumGetIntervalP(transdatums[1]));
3395 
3397  IntervalPGetDatum(&sumX),
3398  IntervalPGetDatum(newval)));
3399  N.time += 1;
3400 
3401  transdatums[0] = IntervalPGetDatum(newsum);
3402  transdatums[1] = IntervalPGetDatum(&N);
3403 
3404  result = construct_array(transdatums, 2,
3405  INTERVALOID, sizeof(Interval), false, 'd');
3406 
3407  PG_RETURN_ARRAYTYPE_P(result);
3408 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3059
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:365
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592

◆ interval_accum_inv()

Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3460 of file timestamp.c.

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.

3461 {
3462  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3464  Datum *transdatums;
3465  int ndatums;
3466  Interval sumX,
3467  N;
3468  Interval *newsum;
3469  ArrayType *result;
3470 
3471  deconstruct_array(transarray,
3472  INTERVALOID, sizeof(Interval), false, 'd',
3473  &transdatums, NULL, &ndatums);
3474  if (ndatums != 2)
3475  elog(ERROR, "expected 2-element interval array");
3476 
3477  sumX = *(DatumGetIntervalP(transdatums[0]));
3478  N = *(DatumGetIntervalP(transdatums[1]));
3479 
3481  IntervalPGetDatum(&sumX),
3482  IntervalPGetDatum(newval)));
3483  N.time -= 1;
3484 
3485  transdatums[0] = IntervalPGetDatum(newsum);
3486  transdatums[1] = IntervalPGetDatum(&N);
3487 
3488  result = construct_array(transdatums, 2,
3489  INTERVALOID, sizeof(Interval), false, 'd');
3490 
3491  PG_RETURN_ARRAYTYPE_P(result);
3492 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3093
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:365
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 3495 of file timestamp.c.

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

3496 {
3497  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3498  Datum *transdatums;
3499  int ndatums;
3500  Interval sumX,
3501  N;
3502 
3503  deconstruct_array(transarray,
3504  INTERVALOID, sizeof(Interval), false, 'd',
3505  &transdatums, NULL, &ndatums);
3506  if (ndatums != 2)
3507  elog(ERROR, "expected 2-element interval array");
3508 
3509  sumX = *(DatumGetIntervalP(transdatums[0]));
3510  N = *(DatumGetIntervalP(transdatums[1]));
3511 
3512  /* SQL defines AVG of no values to be NULL */
3513  if (N.time == 0)
3514  PG_RETURN_NULL();
3515 
3517  IntervalPGetDatum(&sumX),
3518  Float8GetDatum((double) N.time));
3519 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1900
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
uintptr_t Datum
Definition: postgres.h:365
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3220
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2394 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp(), and leftmostvalue_interval().

2395 {
2396  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2397  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2398 
2399  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2400 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_cmp_internal()

static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

Definition at line 2331 of file timestamp.c.

References int128_compare(), and interval_cmp_value().

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

2332 {
2333  INT128 span1 = interval_cmp_value(interval1);
2334  INT128 span2 = interval_cmp_value(interval2);
2335 
2336  return int128_compare(span1, span2);
2337 }
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
Definition: int128.h:107
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2306 of file timestamp.c.

References Interval::day, 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().

2307 {
2308  INT128 span;
2309  int64 dayfraction;
2310  int64 days;
2311 
2312  /*
2313  * Separate time field into days and dayfraction, then add the month and
2314  * day fields to the days part. We cannot overflow int64 days here.
2315  */
2316  dayfraction = interval->time % USECS_PER_DAY;
2317  days = interval->time / USECS_PER_DAY;
2318  days += interval->month * INT64CONST(30);
2319  days += interval->day;
2320 
2321  /* Widen dayfraction to 128 bits */
2322  span = int64_to_int128(dayfraction);
2323 
2324  /* Scale up days to microseconds, forming a 128-bit product */
2326 
2327  return span;
2328 }
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:70
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
Definition: int128.h:107
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
Definition: int128.h:177

◆ interval_combine()

Datum interval_combine ( PG_FUNCTION_ARGS  )

Definition at line 3411 of file timestamp.c.

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

3412 {
3413  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3414  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3415  Datum *transdatums1;
3416  Datum *transdatums2;
3417  int ndatums1;
3418  int ndatums2;
3419  Interval sum1,
3420  N1;
3421  Interval sum2,
3422  N2;
3423 
3424  Interval *newsum;
3425  ArrayType *result;
3426 
3427  deconstruct_array(transarray1,
3428  INTERVALOID, sizeof(Interval), false, 'd',
3429  &transdatums1, NULL, &ndatums1);
3430  if (ndatums1 != 2)
3431  elog(ERROR, "expected 2-element interval array");
3432 
3433  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3434  N1 = *(DatumGetIntervalP(transdatums1[1]));
3435 
3436  deconstruct_array(transarray2,
3437  INTERVALOID, sizeof(Interval), false, 'd',
3438  &transdatums2, NULL, &ndatums2);
3439  if (ndatums2 != 2)
3440  elog(ERROR, "expected 2-element interval array");
3441 
3442  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3443  N2 = *(DatumGetIntervalP(transdatums2[1]));
3444 
3446  IntervalPGetDatum(&sum1),
3447  IntervalPGetDatum(&sum2)));
3448  N1.time += N2.time;
3449 
3450  transdatums1[0] = IntervalPGetDatum(newsum);
3451  transdatums1[1] = IntervalPGetDatum(&N1);
3452 
3453  result = construct_array(transdatums1, 2,
3454  INTERVALOID, sizeof(Interval), false, 'd');
3455 
3456  PG_RETURN_ARRAYTYPE_P(result);
3457 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3059
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:365
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3220 of file timestamp.c.

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, rint(), SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_avg().

3221 {
3222  Interval *span = PG_GETARG_INTERVAL_P(0);
3223  float8 factor = PG_GETARG_FLOAT8(1);
3224  double month_remainder_days,
3225  sec_remainder;
3226  int32 orig_month = span->month,
3227  orig_day = span->day;
3228  Interval *result;
3229 
3230  result = (Interval *) palloc(sizeof(Interval));
3231 
3232  if (factor == 0.0)
3233  ereport(ERROR,
3234  (errcode(ERRCODE_DIVISION_BY_ZERO),
3235  errmsg("division by zero")));
3236 
3237  result->month = (int32) (span->month / factor);
3238  result->day = (int32) (span->day / factor);
3239 
3240  /*
3241  * Fractional months full days into days. See comment in interval_mul().
3242  */
3243  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3244  month_remainder_days = TSROUND(month_remainder_days);
3245  sec_remainder = (orig_day / factor - result->day +
3246  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3247  sec_remainder = TSROUND(sec_remainder);
3248  if (Abs(sec_remainder) >= SECS_PER_DAY)
3249  {
3250  result->day += (int) (sec_remainder / SECS_PER_DAY);
3251  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3252  }
3253 
3254  /* cascade units down */
3255  result->day += (int32) month_remainder_days;
3256  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3257 
3258  PG_RETURN_INTERVAL_P(result);
3259 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:863
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define SECS_PER_DAY
Definition: timestamp.h:86
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ interval_eq()

Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2340 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

2341 {
2342  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2343  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2344 
2345  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2346 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 1973 of file timestamp.c.

References PG_RETURN_BOOL.

1974 {
1975  PG_RETURN_BOOL(true);
1976 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2385 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

2386 {
2387  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2388  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2389 
2390  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2391 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_gt()

Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2367 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2368 {
2369  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2370  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2371 
2372  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2373 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2410 of file timestamp.c.

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

2411 {
2413  INT128 span = interval_cmp_value(interval);
2414  int64 span64;
2415 
2416  /*
2417  * Use only the least significant 64 bits for hashing. The upper 64 bits
2418  * seldom add any useful information, and besides we must do it like this
2419  * for compatibility with hashes calculated before use of INT128 was
2420  * introduced.
2421  */
2422  span64 = int128_to_int64(span);
2423 
2425 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:80
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:590
#define Int64GetDatumFast(X)
Definition: postgres.h:758
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2428 of file timestamp.c.

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

2429 {
2431  INT128 span = interval_cmp_value(interval);
2432  int64 span64;
2433 
2434  /* Same approach as interval_hash */
2435  span64 = int128_to_int64(span);
2436 
2438  PG_GETARG_DATUM(1));
2439 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:100
#define Int64GetDatumFast(X)
Definition: postgres.h:758
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 876 of file timestamp.c.

References AdjustIntervalForTypmod(), DateTimeParseError(), DecodeInterval(), DecodeISO8601Interval(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTK_DELTA, DTK_INVALID, elog, ereport, errcode(), errmsg(), ERROR, INTERVAL_FULL_RANGE, INTERVAL_RANGE, MAXDATEFIELDS, palloc(), ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_INTERVAL_P, range(), generate_unaccent_rules::str, tm, tm2interval(), 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 check_timezone(), and flatten_set_variable_args().

877 {
878  char *str = PG_GETARG_CSTRING(0);
879 
880 #ifdef NOT_USED
881  Oid typelem = PG_GETARG_OID(1);
882 #endif
883  int32 typmod = PG_GETARG_INT32(2);
884  Interval *result;
885  fsec_t fsec;
886  struct pg_tm tt,
887  *tm = &tt;
888  int dtype;
889  int nf;
890  int range;
891  int dterr;
892  char *field[MAXDATEFIELDS];
893  int ftype[MAXDATEFIELDS];
894  char workbuf[256];
895 
896  tm->tm_year = 0;
897  tm->tm_mon = 0;
898  tm->tm_mday = 0;
899  tm->tm_hour = 0;
900  tm->tm_min = 0;
901  tm->tm_sec = 0;
902  fsec = 0;
903 
904  if (typmod >= 0)
905  range = INTERVAL_RANGE(typmod);
906  else
907  range = INTERVAL_FULL_RANGE;
908 
909  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
910  ftype, MAXDATEFIELDS, &nf);
911  if (dterr == 0)
912  dterr = DecodeInterval(field, ftype, nf, range,
913  &dtype, tm, &fsec);
914 
915  /* if those functions think it's a bad format, try ISO8601 style */
916  if (dterr == DTERR_BAD_FORMAT)
917  dterr = DecodeISO8601Interval(str,
918  &dtype, tm, &fsec);
919 
920  if (dterr != 0)
921  {
922  if (dterr == DTERR_FIELD_OVERFLOW)
923  dterr = DTERR_INTERVAL_OVERFLOW;
924  DateTimeParseError(dterr, str, "interval");
925  }
926 
927  result = (Interval *) palloc(sizeof(Interval));
928 
929  switch (dtype)
930  {
931  case DTK_DELTA:
932  if (tm2interval(tm, fsec, result) != 0)
933  ereport(ERROR,
934  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
935  errmsg("interval out of range")));
936  break;
937 
938  case DTK_INVALID:
939  ereport(ERROR,
940  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
941  errmsg("date/time value \"%s\" is no longer supported", str)));
942  break;
943 
944  default:
945  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
946  dtype, str);
947  }
948 
949  AdjustIntervalForTypmod(result, typmod);
950 
951  PG_RETURN_INTERVAL_P(result);
952 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:282
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3749
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define DTK_DELTA
Definition: datetime.h:162
int tm_hour
Definition: pgtime.h:29
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define DTK_INVALID
Definition: datetime.h:151
signed int int32
Definition: c.h:313
static struct pg_tm tm
Definition: localtime.c:107
#define ERROR
Definition: elog.h:43
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:283
#define DTERR_INTERVAL_OVERFLOW
Definition: datetime.h:285
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1320
int tm_mday
Definition: pgtime.h:30
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
#define ereport(elevel, rest)
Definition: elog.h:122
int DecodeISO8601Interval(char *str, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3505
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define MAXDATEFIELDS
Definition: datetime.h:205
int tm_year
Definition: pgtime.h:32
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3079
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:247
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:546
int tm_min
Definition: pgtime.h:28
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1930

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2755 of file timestamp.c.

References Interval::day, DAYS_PER_MONTH, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, and Interval::time.

2756 {
2757  Interval *span = PG_GETARG_INTERVAL_P(0);
2758  Interval *result;
2759  int32 wholemonth;
2760 
2761  result = (Interval *) palloc(sizeof(Interval));
2762  result->month = span->month;
2763  result->day = span->day;
2764  result->time = span->time;
2765 
2766  wholemonth = result->day / DAYS_PER_MONTH;
2767  result->day -= wholemonth * DAYS_PER_MONTH;
2768  result->month += wholemonth;
2769 
2770  if (result->month > 0 && result->day < 0)
2771  {
2772  result->day += DAYS_PER_MONTH;
2773  result->month--;
2774  }
2775  else if (result->month < 0 && result->day > 0)
2776  {
2777  result->day -= DAYS_PER_MONTH;
2778  result->month++;
2779  }
2780 
2781  PG_RETURN_INTERVAL_P(result);
2782 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:313
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:924

◆ interval_justify_hours()

Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

Definition at line 2720 of file timestamp.c.

References Interval::day, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, Interval::time, TMODULO, and USECS_PER_DAY.

Referenced by timestamp_mi().

2721 {
2722  Interval *span = PG_GETARG_INTERVAL_P(0);
2723  Interval *result;
2724  TimeOffset wholeday;
2725 
2726  result = (Interval *) palloc(sizeof(Interval));
2727  result->month = span->month;
2728  result->day = span->day;
2729  result->time = span->time;
2730 
2731  TMODULO(result->time, wholeday, USECS_PER_DAY);
2732  result->day += wholeday; /* could overflow... */
2733 
2734  if (result->day > 0 && result->time < 0)
2735  {
2736  result->time += USECS_PER_DAY;
2737  result->day--;
2738  }
2739  else if (result->day < 0 && result->time > 0)
2740  {
2741  result->time -= USECS_PER_DAY;
2742  result->day++;
2743  }
2744 
2745  PG_RETURN_INTERVAL_P(result);
2746 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
void * palloc(Size size)
Definition: mcxt.c:924
#define TMODULO(t, q, u)
Definition: datetime.h:251

◆ interval_justify_interval()

Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2665 of file timestamp.c.

References Interval::day, DAYS_PER_MONTH, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, Interval::time, TMODULO, and USECS_PER_DAY.

2666 {
2667  Interval *span = PG_GETARG_INTERVAL_P(0);
2668  Interval *result;
2669  TimeOffset wholeday;
2670  int32 wholemonth;
2671 
2672  result = (Interval *) palloc(sizeof(Interval));
2673  result->month = span->month;
2674  result->day = span->day;
2675  result->time = span->time;
2676 
2677  TMODULO(result->time, wholeday, USECS_PER_DAY);
2678  result->day += wholeday; /* could overflow... */
2679 
2680  wholemonth = result->day / DAYS_PER_MONTH;
2681  result->day -= wholemonth * DAYS_PER_MONTH;
2682  result->month += wholemonth;
2683 
2684  if (result->month > 0 &&
2685  (result->day < 0 || (result->day == 0 && result->time < 0)))
2686  {
2687  result->day += DAYS_PER_MONTH;
2688  result->month--;
2689  }
2690  else if (result->month < 0 &&
2691  (result->day > 0 || (result->day == 0 && result->time > 0)))
2692  {
2693  result->day -= DAYS_PER_MONTH;
2694  result->month++;
2695  }
2696 
2697  if (result->day > 0 && result->time < 0)
2698  {
2699  result->time += USECS_PER_DAY;
2700  result->day--;
2701  }
2702  else if (result->day < 0 && result->time > 0)
2703  {
2704  result->time -= USECS_PER_DAY;
2705  result->day++;
2706  }
2707 
2708  PG_RETURN_INTERVAL_P(result);
2709 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:313
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:924
#define TMODULO(t, q, u)
Definition: datetime.h:251

◆ interval_larger()

Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3045 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3046 {
3047  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3048  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3049  Interval *result;
3050 
3051  if (interval_cmp_internal(interval1, interval2) > 0)
3052  result = interval1;
3053  else
3054  result = interval2;
3055  PG_RETURN_INTERVAL_P(result);
3056 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2376 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

2377 {
2378  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2379  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2380 
2381  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2382 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_lt()

Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2358 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2359 {
2360  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2361  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2362 
2363  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2364 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3093 of file timestamp.c.

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

3094 {
3095  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3096  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3097  Interval *result;
3098 
3099  result = (Interval *) palloc(sizeof(Interval));
3100 
3101  result->month = span1->month - span2->month;
3102  /* overflow check copied from int4mi */
3103  if (!SAMESIGN(span1->month, span2->month) &&
3104  !SAMESIGN(result->month, span1->month))
3105  ereport(ERROR,
3106  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3107  errmsg("interval out of range")));
3108 
3109  result->day = span1->day - span2->day;
3110  if (!SAMESIGN(span1->day, span2->day) &&
3111  !SAMESIGN(result->day, span1->day))
3112  ereport(ERROR,
3113  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3114  errmsg("interval out of range")));
3115 
3116  result->time = span1->time - span2->time;
3117  if (!SAMESIGN(span1->time, span2->time) &&
3118  !SAMESIGN(result->time, span1->time))
3119  ereport(ERROR,
3120  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3121  errmsg("interval out of range")));
3122 
3123  PG_RETURN_INTERVAL_P(result);
3124 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3133 of file timestamp.c.

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

Referenced by interval_lerp(), and mul_d_interval().

3134 {
3135  Interval *span = PG_GETARG_INTERVAL_P(0);
3136  float8 factor = PG_GETARG_FLOAT8(1);
3137  double month_remainder_days,
3138  sec_remainder,
3139  result_double;
3140  int32 orig_month = span->month,
3141  orig_day = span->day;
3142  Interval *result;
3143 
3144  result = (Interval *) palloc(sizeof(Interval));
3145 
3146  result_double = span->month * factor;
3147  if (isnan(result_double) ||
3148  result_double > INT_MAX || result_double < INT_MIN)
3149  ereport(ERROR,
3150  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3151  errmsg("interval out of range")));
3152  result->month = (int32) result_double;
3153 
3154  result_double = span->day * factor;
3155  if (isnan(result_double) ||
3156  result_double > INT_MAX || result_double < INT_MIN)
3157  ereport(ERROR,
3158  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3159  errmsg("interval out of range")));
3160  result->day = (int32) result_double;
3161 
3162  /*
3163  * The above correctly handles the whole-number part of the month and day
3164  * products, but we have to do something with any fractional part
3165  * resulting when the factor is non-integral. We cascade the fractions
3166  * down to lower units using the conversion factors DAYS_PER_MONTH and
3167  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3168  * so by the representation. The user can choose to cascade up later,
3169  * using justify_hours and/or justify_days.
3170  */
3171 
3172  /*
3173  * Fractional months full days into days.
3174  *
3175  * Floating point calculation are inherently imprecise, so these
3176  * calculations are crafted to produce the most reliable result possible.
3177  * TSROUND() is needed to more accurately produce whole numbers where
3178  * appropriate.
3179  */
3180  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3181  month_remainder_days = TSROUND(month_remainder_days);
3182  sec_remainder = (orig_day * factor - result->day +
3183  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3184  sec_remainder = TSROUND(sec_remainder);
3185 
3186  /*
3187  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3188  * cascade from months and days. It might still be >24 if the combination
3189  * of cascade and the seconds factor operation itself.
3190  */
3191  if (Abs(sec_remainder) >= SECS_PER_DAY)
3192  {
3193  result->day += (int) (sec_remainder / SECS_PER_DAY);
3194  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3195  }
3196 
3197  /* cascade units down */
3198  result->day += (int32) month_remainder_days;
3199  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3200  if (result_double > PG_INT64_MAX || result_double < PG_INT64_MIN)
3201  ereport(ERROR,
3202  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3203  errmsg("interval out of range")));
3204  result->time = (int64) result_double;
3205 
3206  PG_RETURN_INTERVAL_P(result);
3207 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_INT64_MAX
Definition: c.h:411
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:863
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define SECS_PER_DAY
Definition: timestamp.h:86
#define PG_INT64_MIN
Definition: c.h:410
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ interval_ne()

Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2349 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2350 {
2351  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2352  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2353 
2354  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2355 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 958 of file timestamp.c.

References buf, elog, EncodeInterval(), ERROR, interval2tm(), IntervalStyle, MAXDATELEN, PG_GETARG_INTERVAL_P, PG_RETURN_CSTRING, pstrdup(), and tm.

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

959 {
960  Interval *span = PG_GETARG_INTERVAL_P(0);
961  char *result;
962  struct pg_tm tt,
963  *tm = &tt;
964  fsec_t fsec;
965  char buf[MAXDATELEN + 1];
966 
967  if (interval2tm(*span, tm, &fsec) != 0)
968  elog(ERROR, "could not convert interval to tm");
969 
970  EncodeInterval(tm, fsec, IntervalStyle, buf);
971 
972  result = pstrdup(buf);
973  PG_RETURN_CSTRING(result);
974 }
#define MAXDATELEN
Definition: datetime.h:203
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int IntervalStyle
Definition: globals.c:118
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
#define ERROR
Definition: elog.h:43
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1902
static char * buf
Definition: pg_test_fsync.c:67
int32 fsec_t
Definition: timestamp.h:41
void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str)
Definition: datetime.c:4225
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:327
#define elog
Definition: elog.h:219

◆ interval_part()

Datum interval_part ( PG_FUNCTION_ARGS  )

Definition at line 4796 of file timestamp.c.

References Interval::day, 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, elog, ereport, errcode(), errmsg(), ERROR, interval2tm(), Interval::month, MONTHS_PER_YEAR, PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT8, 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.

4797 {
4798  text *units = PG_GETARG_TEXT_PP(0);
4800  float8 result;
4801  int type,
4802  val;
4803  char *lowunits;
4804  fsec_t fsec;
4805  struct pg_tm tt,
4806  *tm = &tt;
4807 
4808  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4809  VARSIZE_ANY_EXHDR(units),
4810  false);
4811 
4812  type = DecodeUnits(0, lowunits, &val);
4813  if (type == UNKNOWN_FIELD)
4814  type = DecodeSpecial(0, lowunits, &val);
4815 
4816  if (type == UNITS)
4817  {
4818  if (interval2tm(*interval, tm, &fsec) == 0)
4819  {
4820  switch (val)
4821  {
4822  case DTK_MICROSEC:
4823  result = tm->tm_sec * 1000000.0 + fsec;
4824  break;
4825 
4826  case DTK_MILLISEC:
4827  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4828  break;
4829 
4830  case DTK_SECOND:
4831  result = tm->tm_sec + fsec / 1000000.0;
4832  break;
4833 
4834  case DTK_MINUTE:
4835  result = tm->tm_min;
4836  break;
4837 
4838  case DTK_HOUR:
4839  result = tm->tm_hour;
4840  break;
4841 
4842  case DTK_DAY:
4843  result = tm->tm_mday;
4844  break;
4845 
4846  case DTK_MONTH:
4847  result = tm->tm_mon;
4848  break;
4849 
4850  case DTK_QUARTER:
4851  result = (tm->tm_mon / 3) + 1;
4852  break;
4853 
4854  case DTK_YEAR:
4855  result = tm->tm_year;
4856  break;
4857 
4858  case DTK_DECADE:
4859  /* caution: C division may have negative remainder */
4860  result = tm->tm_year / 10;
4861  break;
4862 
4863  case DTK_CENTURY:
4864  /* caution: C division may have negative remainder */
4865  result = tm->tm_year / 100;
4866  break;
4867 
4868  case DTK_MILLENNIUM:
4869  /* caution: C division may have negative remainder */
4870  result = tm->tm_year / 1000;
4871  break;
4872 
4873  default:
4874  ereport(ERROR,
4875  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4876  errmsg("interval units \"%s\" not supported",
4877  lowunits)));
4878  result = 0;
4879  }
4880 
4881  }
4882  else
4883  {
4884  elog(ERROR, "could not convert interval to tm");
4885  result = 0;
4886  }
4887  }
4888  else if (type == RESERV && val == DTK_EPOCH)
4889  {
4890  result = interval->time / 1000000.0;
4891  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
4892  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
4893  result += ((double) SECS_PER_DAY) * interval->day;
4894  }
4895  else
4896  {
4897  ereport(ERROR,
4898  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4899  errmsg("interval units \"%s\" not recognized",
4900  lowunits)));
4901  result = 0;
4902  }
4903 
4904  PG_RETURN_FLOAT8(result);
4905 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:346
#define DTK_YEAR
Definition: datetime.h:170
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:169
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3712
Definition: pgtime.h:25
int32 day
Definition: timestamp.h:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static struct pg_tm tm
Definition: localtime.c:107
#define DTK_MONTH
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:174
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define DTK_DECADE
Definition: datetime.h:171
#define DAYS_PER_YEAR
Definition: timestamp.h:68
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define SECS_PER_DAY
Definition: timestamp.h:86
#define DTK_SECOND
Definition: datetime.h:163
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1902
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
TimeOffset time
Definition: timestamp.h:45
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTK_MINUTE
Definition: datetime.h:164
int32 month
Definition: timestamp.h:48
#define DTK_MICROSEC
Definition: datetime.h:175
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define DTK_DAY
Definition: datetime.h:166
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3023
#define DTK_EPOCH
Definition: datetime.h:155
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:339
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:689

◆ interval_pl()

Datum interval_pl ( PG_FUNCTION_ARGS  )

Definition at line 3059 of file timestamp.c.

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

3060 {
3061  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3062  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3063  Interval *result;
3064 
3065  result = (Interval *) palloc(sizeof(Interval));
3066 
3067  result->month = span1->month + span2->month;
3068  /* overflow check copied from int4pl */
3069  if (SAMESIGN(span1->month, span2->month) &&
3070  !SAMESIGN(result->month, span1->month))
3071  ereport(ERROR,
3072  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3073  errmsg("interval out of range")));
3074 
3075  result->day = span1->day + span2->day;
3076  if (SAMESIGN(span1->day, span2->day) &&
3077  !SAMESIGN(result->day, span1->day))
3078  ereport(ERROR,
3079  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3080  errmsg("interval out of range")));
3081 
3082  result->time = span1->time + span2->time;
3083  if (SAMESIGN(span1->time, span2->time) &&
3084  !SAMESIGN(result->time, span1->time))
3085  ereport(ERROR,
3086  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3087  errmsg("interval out of range")));
3088 
3089  PG_RETURN_INTERVAL_P(result);
3090 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

Definition at line 980 of file timestamp.c.

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

981 {
983 
984 #ifdef NOT_USED
985  Oid typelem = PG_GETARG_OID(1);
986 #endif
987  int32 typmod = PG_GETARG_INT32(2);
989 
990  interval = (Interval *) palloc(sizeof(Interval));
991 
992  interval->time = pq_getmsgint64(buf);
993  interval->day = pq_getmsgint(buf, sizeof(interval->day));
994  interval->month = pq_getmsgint(buf, sizeof(interval->month));
995 
996  AdjustIntervalForTypmod(interval, typmod);
997 
998  PG_RETURN_INTERVAL_P(interval);
999 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:313
int32 day
Definition: timestamp.h:47
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1320
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:924
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ interval_scale()

Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1301 of file timestamp.c.

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

1302 {
1304  int32 typmod = PG_GETARG_INT32(1);
1305  Interval *result;
1306 
1307  result = palloc(sizeof(Interval));
1308  *result = *interval;
1309 
1310  AdjustIntervalForTypmod(result, typmod);
1311 
1312  PG_RETURN_INTERVAL_P(result);
1313 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:313
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1320
void * palloc(Size size)
Definition: mcxt.c:924

◆ interval_send()

Datum interval_send ( PG_FUNCTION_ARGS  )

Definition at line 1005 of file timestamp.c.

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

1006 {
1009 
1010  pq_begintypsend(&buf);
1011  pq_sendint64(&buf, interval->time);
1012  pq_sendint32(&buf, interval->day);
1013  pq_sendint32(&buf, interval->month);
1015 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
static void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.h:156
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
int32 day
Definition: timestamp.h:47
static char * buf
Definition: pg_test_fsync.c:67
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3030 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3031 {
3032  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3033  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3034  Interval *result;
3035 
3036  /* use interval_cmp_internal to be sure this agrees with comparisons */
3037  if (interval_cmp_internal(interval1, interval2) < 0)
3038  result = interval1;
3039  else
3040  result = interval2;
3041  PG_RETURN_INTERVAL_P(result);
3042 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331

◆ interval_transform()

Datum interval_transform ( PG_FUNCTION_ARGS  )

Definition at line 1244 of file timestamp.c.

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

1245 {
1247  Node *ret = NULL;
1248  Node *typmod;
1249 
1250  Assert(list_length(expr->args) >= 2);
1251 
1252  typmod = (Node *) lsecond(expr->args);
1253 
1254  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
1255  {
1256  Node *source = (Node *) linitial(expr->args);
1257  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1258  bool noop;
1259 
1260  if (new_typmod < 0)
1261  noop = true;
1262  else
1263  {
1264  int32 old_typmod = exprTypmod(source);
1265  int old_least_field;
1266  int new_least_field;
1267  int old_precis;
1268  int new_precis;
1269 
1270  old_least_field = intervaltypmodleastfield(old_typmod);
1271  new_least_field = intervaltypmodleastfield(new_typmod);
1272  if (old_typmod < 0)
1273  old_precis = INTERVAL_FULL_PRECISION;
1274  else
1275  old_precis = INTERVAL_PRECISION(old_typmod);
1276  new_precis = INTERVAL_PRECISION(new_typmod);
1277 
1278  /*
1279  * Cast is a no-op if least field stays the same or decreases
1280  * while precision stays the same or increases. But precision,
1281  * which is to say, sub-second precision, only affects ranges that
1282  * include SECOND.
1283  */
1284  noop = (new_least_field <= old_least_field) &&
1285  (old_least_field > 0 /* SECOND */ ||
1286  new_precis >= MAX_INTERVAL_PRECISION ||
1287  new_precis >= old_precis);
1288  }
1289  if (noop)
1290  ret = relabel_to_typmod(source, new_typmod);
1291  }
1292 
1293  PG_RETURN_POINTER(ret);
1294 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:326
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
List * args
Definition: primnodes.h:458
#define DatumGetInt32(X)
Definition: postgres.h:455
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:517
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
#define lsecond(l)
Definition: pg_list.h:116
signed int int32
Definition: c.h:313
#define linitial(l)
Definition: pg_list.h:111
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:54
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:53
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
static int intervaltypmodleastfield(int32 typmod)
Definition: timestamp.c:1195
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:588

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 4049 of file timestamp.c.

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, elog, ereport, errcode(), errmsg(), ERROR, interval2tm(), palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_RETURN_INTERVAL_P, tm, tm2interval(), 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.

4050 {
4051  text *units = PG_GETARG_TEXT_PP(0);
4053  Interval *result;
4054  int type,
4055  val;
4056  char *lowunits;
4057  fsec_t fsec;
4058  struct pg_tm tt,
4059  *tm = &tt;
4060 
4061  result = (Interval *) palloc(sizeof(Interval));
4062 
4063  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4064  VARSIZE_ANY_EXHDR(units),
4065  false);
4066 
4067  type = DecodeUnits(0, lowunits, &val);
4068 
4069  if (type == UNITS)
4070  {
4071  if (interval2tm(*interval, tm, &fsec) == 0)
4072  {
4073  switch (val)
4074  {
4075  /* fall through */
4076  case DTK_MILLENNIUM:
4077  /* caution: C division may have negative remainder */
4078  tm->tm_year = (tm->tm_year / 1000) * 1000;
4079  case DTK_CENTURY:
4080  /* caution: C division may have negative remainder */
4081  tm->tm_year = (tm->tm_year / 100) * 100;
4082  case DTK_DECADE:
4083  /* caution: C division may have negative remainder */
4084  tm->tm_year = (tm->tm_year / 10) * 10;
4085  case DTK_YEAR:
4086  tm->tm_mon = 0;
4087  case DTK_QUARTER:
4088  tm->tm_mon = 3 * (tm->tm_mon / 3);
4089  case DTK_MONTH:
4090  tm->tm_mday = 0;
4091  case DTK_DAY:
4092  tm->tm_hour = 0;
4093  case DTK_HOUR:
4094  tm->tm_min = 0;
4095  case DTK_MINUTE:
4096  tm->tm_sec = 0;
4097  case DTK_SECOND:
4098  fsec = 0;
4099  break;
4100  case DTK_MILLISEC:
4101  fsec = (fsec / 1000) * 1000;
4102  break;
4103  case DTK_MICROSEC:
4104  break;
4105 
4106  default:
4107  if (val == DTK_WEEK)
4108  ereport(ERROR,
4109  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4110  errmsg("interval units \"%s\" not supported "
4111  "because months usually have fractional weeks",
4112  lowunits)));
4113  else
4114  ereport(ERROR,
4115  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4116  errmsg("interval units \"%s\" not supported",
4117  lowunits)));
4118  }
4119 
4120  if (tm2interval(tm, fsec, result) != 0)
4121  ereport(ERROR,
4122  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4123  errmsg("interval out of range")));
4124  }
4125  else
4126  elog(ERROR, "could not convert interval to tm");
4127  }
4128  else
4129  {
4130  ereport(ERROR,
4131  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4132  errmsg("interval units \"%s\" not recognized",
4133  lowunits)));
4134  }
4135 
4136  PG_RETURN_INTERVAL_P(result);
4137 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:346
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define DTK_WEEK
Definition: datetime.h:167
#define DTK_YEAR
Definition: datetime.h:170
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:169
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3712
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
static struct pg_tm tm
Definition: localtime.c:107
#define DTK_MONTH
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:174
#define DTK_DECADE
Definition: datetime.h:171
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
#define DTK_SECOND
Definition: datetime.h:163
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1902
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTK_MINUTE
Definition: datetime.h:164
#define DTK_MICROSEC
Definition: datetime.h:175
#define DTK_DAY
Definition: datetime.h:166
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:339
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:689
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1930

◆ interval_um()

Datum interval_um ( PG_FUNCTION_ARGS  )

Definition at line 3001 of file timestamp.c.

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

Referenced by abs_interval().

3002 {
3004  Interval *result;
3005 
3006  result = (Interval *) palloc(sizeof(Interval));
3007 
3008  result->time = -interval->time;
3009  /* overflow check copied from int4um */
3010  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3011  ereport(ERROR,
3012  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3013  errmsg("interval out of range")));
3014  result->day = -interval->day;
3015  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3016  ereport(ERROR,
3017  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3018  errmsg("interval out of range")));
3019  result->month = -interval->month;
3020  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3021  ereport(ERROR,
3022  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3023  errmsg("interval out of range")));
3024 
3025  PG_RETURN_INTERVAL_P(result);
3026 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1030 of file timestamp.c.

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.

1031 {
1033  int32 *tl;
1034  int n;
1035  int32 typmod;
1036 
1037  tl = ArrayGetIntegerTypmods(ta, &n);
1038 
1039  /*
1040  * tl[0] - interval range (fields bitmask) tl[1] - precision (optional)
1041  *
1042  * Note we must validate tl[0] even though it's normally guaranteed
1043  * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
1044  */
1045  if (n > 0)
1046  {
1047  switch (tl[0])
1048  {
1049  case INTERVAL_MASK(YEAR):
1050  case INTERVAL_MASK(MONTH):
1051  case INTERVAL_MASK(DAY):
1052  case INTERVAL_MASK(HOUR):
1053  case INTERVAL_MASK(MINUTE):
1054  case INTERVAL_MASK(SECOND):
1062  case INTERVAL_FULL_RANGE:
1063  /* all OK */
1064  break;
1065  default:
1066  ereport(ERROR,
1067  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1068  errmsg("invalid INTERVAL type modifier")));
1069  }
1070  }
1071 
1072  if (n == 1)
1073  {
1074  if (tl[0] != INTERVAL_FULL_RANGE)
1075  typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, tl[0]);
1076  else
1077  typmod = -1;
1078  }
1079  else if (n == 2)
1080  {
1081  if (tl[1] < 0)
1082  ereport(ERROR,
1083  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1084  errmsg("INTERVAL(%d) precision must not be negative",
1085  tl[1])));
1086  if (tl[1] > MAX_INTERVAL_PRECISION)
1087  {
1088  ereport(WARNING,
1089  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1090  errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1091  tl[1], MAX_INTERVAL_PRECISION)));
1092  typmod = INTERVAL_TYPMOD(MAX_INTERVAL_PRECISION, tl[0]);
1093  }
1094  else
1095  typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
1096  }
1097  else
1098  {
1099  ereport(ERROR,
1100  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1101  errmsg("invalid INTERVAL type modifier")));
1102  typmod = 0; /* keep compiler quiet */
1103  }
1104 
1105  PG_RETURN_INT32(typmod);
1106 }
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:200
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
signed int int32
Definition: c.h:313
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:54
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define INTERVAL_TYPMOD(p, r)
Definition: timestamp.h:52
#define MINUTE
Definition: datetime.h:102
#define ereport(elevel, rest)
Definition: elog.h:122
#define MONTH
Definition: datetime.h:92
#define WARNING
Definition: elog.h:40
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HOUR
Definition: datetime.h:101

◆ intervaltypmodleastfield()

static int intervaltypmodleastfield ( int32  typmod)
static

Definition at line 1195 of file timestamp.c.

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

Referenced by interval_transform().

1196 {
1197  if (typmod < 0)
1198  return 0; /* SECOND */
1199 
1200  switch (INTERVAL_RANGE(typmod))
1201  {
1202  case INTERVAL_MASK(YEAR):
1203  return 5; /* YEAR */
1204  case INTERVAL_MASK(MONTH):
1205  return 4; /* MONTH */
1206  case INTERVAL_MASK(DAY):
1207  return 3; /* DAY */
1208  case INTERVAL_MASK(HOUR):
1209  return 2; /* HOUR */
1210  case INTERVAL_MASK(MINUTE):
1211  return 1; /* MINUTE */
1212  case INTERVAL_MASK(SECOND):
1213  return 0; /* SECOND */
1215  return 4; /* MONTH */
1217  return 2; /* HOUR */
1219  return 1; /* MINUTE */
1221  return 0; /* SECOND */
1223  return 1; /* MINUTE */
1225  return 0; /* SECOND */
1227  return 0; /* SECOND */
1228  case INTERVAL_FULL_RANGE:
1229  return 0; /* SECOND */
1230  default:
1231  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1232  break;
1233  }
1234  return 0; /* can't get here, but keep compiler quiet */
1235 }
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define ERROR
Definition: elog.h:43
#define MINUTE
Definition: datetime.h:102
#define MONTH
Definition: datetime.h:92
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
#define elog
Definition: elog.h:219
#define HOUR
Definition: datetime.h:101

◆ intervaltypmodout()

Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1109 of file timestamp.c.

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, SECOND, snprintf(), and YEAR.

1110 {
1111  int32 typmod = PG_GETARG_INT32(0);
1112  char *res = (char *) palloc(64);
1113  int fields;
1114  int precision;
1115  const char *fieldstr;
1116 
1117  if (typmod < 0)
1118  {
1119  *res = '\0';
1120  PG_RETURN_CSTRING(res);
1121  }
1122 
1123  fields = INTERVAL_RANGE(typmod);
1124  precision = INTERVAL_PRECISION(typmod);
1125 
1126  switch (fields)
1127  {
1128  case INTERVAL_MASK(YEAR):
1129  fieldstr = " year";
1130  break;
1131  case INTERVAL_MASK(MONTH):
1132  fieldstr = " month";
1133  break;
1134  case INTERVAL_MASK(DAY):
1135  fieldstr = " day";
1136  break;
1137  case INTERVAL_MASK(HOUR):
1138  fieldstr = " hour";
1139  break;
1140  case INTERVAL_MASK(MINUTE):
1141  fieldstr = " minute";
1142  break;
1143  case INTERVAL_MASK(SECOND):
1144  fieldstr = " second";
1145  break;
1147  fieldstr = " year to month";
1148  break;
1150  fieldstr = " day to hour";
1151  break;
1153  fieldstr = " day to minute";
1154  break;
1156  fieldstr = " day to second";
1157  break;
1159  fieldstr = " hour to minute";
1160  break;
1162  fieldstr = " hour to second";
1163  break;
1165  fieldstr = " minute to second";
1166  break;
1167  case INTERVAL_FULL_RANGE:
1168  fieldstr = "";
1169  break;
1170  default:
1171  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1172  fieldstr = "";
1173  break;
1174  }
1175 
1176  if (precision != INTERVAL_FULL_PRECISION)
1177  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1178  else
1179  snprintf(res, 64, "%s", fieldstr);
1180 
1181  PG_RETURN_CSTRING(res);
1182 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define MINUTE
Definition: datetime.h:102
#define MONTH
Definition: datetime.h:92
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:53
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:327
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
void * palloc(Size size)
Definition: mcxt.c:924
#define elog
Definition: elog.h:219
#define HOUR
Definition: datetime.h:101

◆ isoweek2date()

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

Definition at line 4165 of file timestamp.c.

References isoweek2j(), and j2date().

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

4166 {
4167  j2date(isoweek2j(*year, woy), year, mon, mday);
4168 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:301
int isoweek2j(int year, int week)
Definition: timestamp.c:4145

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4145 of file timestamp.c.

References date2j(), and j2day().

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

4146 {
4147  int day0,
4148  day4;
4149 
4150  /* fourth day of current year */
4151  day4 = date2j(year, 1, 4);
4152 
4153  /* day0 == offset to first day of week (Monday) */
4154  day0 = j2day(day4 - 1);
4155 
4156  return ((week - 1) * 7) + (day4 - day0);
4157 }
int j2day(int date)
Definition: datetime.c:336
int date2j(int y, int m, int d)
Definition: datetime.c:276

◆ isoweekdate2date()

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

Definition at line 4178 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4179 {
4180  int jday;
4181 
4182  jday = isoweek2j(*year, isoweek);
4183  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4184  if (wday > 1)
4185  jday += wday - 2;
4186  else
4187  jday += 6;
4188  j2date(jday, year, mon, mday);
4189 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:301
int isoweek2j(int year, int week)
Definition: timestamp.c:4145

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1487 of file timestamp.c.

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

1488 {
1489  int32 years = PG_GETARG_INT32(0);
1491  int32 weeks = PG_GETARG_INT32(2);
1492  int32 days = PG_GETARG_INT32(3);
1493  int32 hours = PG_GETARG_INT32(4);
1494  int32 mins = PG_GETARG_INT32(5);
1495  double secs = PG_GETARG_FLOAT8(6);
1496  Interval *result;
1497 
1498  /*
1499  * Reject out-of-range inputs. We really ought to check the integer
1500  * inputs as well, but it's not entirely clear what limits to apply.
1501  */
1502  if (isinf(secs) || isnan(secs))
1503  ereport(ERROR,
1504  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1505  errmsg("interval out of range")));
1506 
1507  result = (Interval *) palloc(sizeof(Interval));
1508  result->month = years * MONTHS_PER_YEAR + months;
1509  result->day = weeks * 7 + days;
1510 
1511  secs = rint(secs * USECS_PER_SEC);
1512  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1513  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1514  (int64) secs;
1515 
1516  PG_RETURN_INTERVAL_P(result);
1517 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
const char *const months[]
Definition: datetime.c:67
#define ERROR
Definition: elog.h:43
int isinf(double x)
#define SECS_PER_MINUTE
Definition: timestamp.h:88
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:70
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:87
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 630 of file timestamp.c.

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

631 {
632  int32 year = PG_GETARG_INT32(0);
633  int32 month = PG_GETARG_INT32(1);
634  int32 mday = PG_GETARG_INT32(2);
635  int32 hour = PG_GETARG_INT32(3);
636  int32 min = PG_GETARG_INT32(4);
637  float8 sec = PG_GETARG_FLOAT8(5);
638  Timestamp result;
639 
640  result = make_timestamp_internal(year, month, mday,
641  hour, min, sec);
642 
643  PG_RETURN_TIMESTAMP(result);
644 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:542
signed int int32
Definition: c.h:313
double float8
Definition: c.h:458
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39

◆ make_timestamp_internal()

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

Definition at line 542 of file timestamp.c.

References date2j(), DTK_DATE_M, ereport, errcode(), errmsg(), ERROR, HOURS_PER_DAY, IS_VALID_JULIAN, IS_VALID_TIMESTAMP, MINS_PER_HOUR, POSTGRES_EPOCH_JDATE, rint(), SECS_PER_MINUTE, 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().

544 {
545  struct pg_tm tm;
547  TimeOffset time;
548  int dterr;
549  Timestamp result;
550 
551  tm.tm_year = year;
552  tm.tm_mon = month;
553  tm.tm_mday = day;
554 
555  /*
556  * Note: we'll reject zero or negative year values. Perhaps negatives
557  * should be allowed to represent BC years?
558  */
559  dterr = ValidateDate(DTK_DATE_M, false, false, false, &tm);
560 
561  if (dterr != 0)
562  ereport(ERROR,
563  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
564  errmsg("date field value out of range: %d-%02d-%02d",
565  year, month, day)));
566 
568  ereport(ERROR,
569  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
570  errmsg("date out of range: %d-%02d-%02d",
571  year, month, day)));
572 
574 
575  /*
576  * This should match the checks in DecodeTimeOnly, except that since we're
577  * dealing with a float "sec" value, we also explicitly reject NaN. (An
578  * infinity input should get rejected by the range comparisons, but we
579  * can't be sure how those will treat a NaN.)
580  */
581  if (hour < 0 || min < 0 || min > MINS_PER_HOUR - 1 ||
582  isnan(sec) ||
583  sec < 0 || sec > SECS_PER_MINUTE ||
584  hour > HOURS_PER_DAY ||
585  /* test for > 24:00:00 */
586  (hour == HOURS_PER_DAY && (min > 0 || sec > 0)))
587  ereport(ERROR,
588  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
589  errmsg("time field value out of range: %d:%02d:%02g",
590  hour, min, sec)));
591 
592  /* This should match tm2time */
593  time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
594  * USECS_PER_SEC) + rint(sec * USECS_PER_SEC);
595 
596  result = date * USECS_PER_DAY + time;
597  /* check for major overflow */
598  if ((result - time) / USECS_PER_DAY != date)
599  ereport(ERROR,
600  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
601  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
602  year, month, day,
603  hour, min, sec)));
604 
605  /* check for just-barely overflow (okay except time-of-day wraps) */
606  /* caution: we want to allow 1999-12-31 24:00:00 */
607  if ((result < 0 && date > 0) ||
608  (result > 0 && date < -1))
609  ereport(ERROR,
610  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
611  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
612  year, month, day,
613  hour, min, sec)));
614 
615  /* final range check catches just-out-of-range timestamps */
616  if (!IS_VALID_TIMESTAMP(result))
617  ereport(ERROR,
618  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
619  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
620  year, month, day,
621  hour, min, sec)));
622 
623  return result;
624 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
long date
Definition: pgtypes_date.h:8
Definition: pgtime.h:25
#define DTK_DATE_M
Definition: datetime.h:194
#define MINS_PER_HOUR
Definition: timestamp.h:89
static struct pg_tm tm
Definition: localtime.c:107
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
#define HOURS_PER_DAY
Definition: timestamp.h:78
int tm_mon
Definition: pgtime.h:31
#define SECS_PER_MINUTE
Definition: timestamp.h:88
double rint(double x)
Definition: rint.c:22
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
#define USECS_PER_DAY
Definition: timestamp.h:91
#define ereport(elevel, rest)
Definition: elog.h:122
int64 Timestamp
Definition: timestamp.h:38
int64 TimeOffset
Definition: timestamp.h:40
int date2j(int y, int m, int d)
Definition: datetime.c:276
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2463

◆ make_timestamptz()

Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 650 of file timestamp.c.

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

651 {
652  int32 year = PG_GETARG_INT32(0);
653  int32 month = PG_GETARG_INT32(1);
654  int32 mday = PG_GETARG_INT32(2);
655  int32 hour = PG_GETARG_INT32(3);
656  int32 min = PG_GETARG_INT32(4);
657  float8 sec = PG_GETARG_FLOAT8(5);
658  Timestamp result;
659 
660  result = make_timestamp_internal(year, month, mday,
661  hour, min, sec);
662 
664 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:5070
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:542
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:313
double float8
Definition: c.h:458
int64 Timestamp
Definition: timestamp.h:38

◆ make_timestamptz_at_timezone()

Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 671 of file timestamp.c.

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

672 {
673  int32 year = PG_GETARG_INT32(0);
674  int32 month = PG_GETARG_INT32(1);
675  int32 mday = PG_GETARG_INT32(2);
676  int32 hour = PG_GETARG_INT32(3);
677  int32 min = PG_GETARG_INT32(4);
678  float8 sec = PG_GETARG_FLOAT8(5);
680  TimestampTz result;
682  struct pg_tm tt;
683  int tz;
684  fsec_t fsec;
685 
686  timestamp = make_timestamp_internal(year, month, mday,
687  hour, min, sec);
688 
689  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
690  ereport(ERROR,
691  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
692  errmsg("timestamp out of range")));
693 
694  tz = parse_sane_timezone(&tt, zone);
695 
696  result = dt2local(timestamp, -tz);
697 
698  if (!IS_VALID_TIMESTAMP(result))
699  ereport(ERROR,
700  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
701  errmsg("timestamp out of range")));
702 
703  PG_RETURN_TIMESTAMPTZ(result);
704 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:542
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:313
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
int64 Timestamp
Definition: timestamp.h:38
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Definition: zic.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:516
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:1952
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:458

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3210 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3211 {
3212  /* Args are float8 and Interval *, but leave them as generic Datum */
3213  Datum factor = PG_GETARG_DATUM(0);
3214  Datum span = PG_GETARG_DATUM(1);
3215 
3216  return DirectFunctionCall2(interval_mul, span, factor);
3217 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
uintptr_t Datum
Definition: postgres.h:365
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3133
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:592

◆ NonFiniteTimestampTzPart()

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

Definition at line 4324 of file timestamp.c.

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, get_float8_infinity(), RESERV, and UNITS.

Referenced by timestamp_part(), and timestamptz_part().

4326 {
4327  if ((type != UNITS) && (type != RESERV))
4328  {
4329  if (isTz)
4330  ereport(ERROR,
4331  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4332  errmsg("timestamp with time zone units \"%s\" not recognized",
4333  lowunits)));
4334  else
4335  ereport(ERROR,
4336  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4337  errmsg("timestamp units \"%s\" not recognized",
4338  lowunits)));
4339  }
4340 
4341  switch (unit)
4342  {
4343  /* Oscillating units */
4344  case DTK_MICROSEC:
4345  case DTK_MILLISEC:
4346  case DTK_SECOND:
4347  case DTK_MINUTE:
4348  case DTK_HOUR:
4349  case DTK_DAY:
4350  case DTK_MONTH:
4351  case DTK_QUARTER:
4352  case DTK_WEEK:
4353  case DTK_DOW:
4354  case DTK_ISODOW:
4355  case DTK_DOY:
4356  case DTK_TZ:
4357  case DTK_TZ_MINUTE:
4358  case DTK_TZ_HOUR:
4359  return 0.0;
4360 
4361  /* Monotonically-increasing units */
4362  case DTK_YEAR:
4363  case DTK_DECADE:
4364  case DTK_CENTURY:
4365  case DTK_MILLENNIUM:
4366  case DTK_JULIAN:
4367  case DTK_ISOYEAR:
4368  case DTK_EPOCH:
4369  if (isNegative)
4370  return -get_float8_infinity();
4371  else
4372  return get_float8_infinity();
4373 
4374  default:
4375  if (isTz)
4376  ereport(ERROR,
4377  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4378  errmsg("timestamp with time zone units \"%s\" not supported",
4379  lowunits)));
4380  else
4381  ereport(ERROR,
4382  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4383  errmsg("timestamp units \"%s\" not supported",
4384  lowunits)));
4385  return 0.0; /* keep compiler quiet */
4386  }
4387 }
#define DTK_TZ_HOUR
Definition: datetime.h:180
#define DTK_CENTURY
Definition: datetime.h:172
#define UNITS
Definition: datetime.h:108
#define DTK_JULIAN
Definition: datetime.h:176
#define DTK_WEEK
Definition: datetime.h:167
#define DTK_YEAR
Definition: datetime.h:170
#define DTK_QUARTER
Definition: datetime.h:169
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DTK_ISODOW
Definition: datetime.h:183
#define DTK_MONTH
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:174
#define DTK_DECADE
Definition: datetime.h:171
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
#define DTK_SECOND
Definition: datetime.h:163
#define DTK_ISOYEAR
Definition: datetime.h:182
#define DTK_TZ_MINUTE
Definition: datetime.h:181
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTK_MINUTE
Definition: datetime.h:164
double get_float8_infinity(void)
Definition: float.c:118
#define DTK_DOW
Definition: datetime.h:178
#define DTK_MICROSEC
Definition: datetime.h:175
#define DTK_DOY
Definition: datetime.h:179
#define DTK_DAY
Definition: datetime.h:166
#define RESERV
Definition: datetime.h:91
#define DTK_EPOCH
Definition: datetime.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ now()

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2448 of file timestamp.c.

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

2449 {
2450  /*
2451  * The arguments are Timestamps, but we leave them as generic Datums to
2452  * avoid unnecessary conversions between value and reference forms --- not
2453  * to mention possible dereferences of null pointers.
2454  */
2455  Datum ts1 = PG_GETARG_DATUM(0);
2456  Datum te1 = PG_GETARG_DATUM(1);
2457  Datum ts2 = PG_GETARG_DATUM(2);
2458  Datum te2 = PG_GETARG_DATUM(3);
2459  bool ts1IsNull = PG_ARGISNULL(0);
2460  bool te1IsNull = PG_ARGISNULL(1);
2461  bool ts2IsNull = PG_ARGISNULL(2);
2462  bool te2IsNull = PG_ARGISNULL(3);
2463 
2464 #define TIMESTAMP_GT(t1,t2) \
2465  DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2466 #define TIMESTAMP_LT(t1,t2) \
2467  DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2468 
2469  /*
2470  * If both endpoints of interval 1 are null, the result is null (unknown).
2471  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2472  * take ts1 as the lesser endpoint.
2473  */
2474  if (ts1IsNull)
2475  {
2476  if (te1IsNull)
2477  PG_RETURN_NULL();
2478  /* swap null for non-null */
2479  ts1 = te1;
2480  te1IsNull = true;
2481  }
2482  else if (!te1IsNull)
2483  {
2484  if (TIMESTAMP_GT(ts1, te1))
2485  {
2486  Datum tt = ts1;
2487 
2488  ts1 = te1;
2489  te1 = tt;
2490  }
2491  }
2492 
2493  /* Likewise for interval 2. */
2494  if (ts2IsNull)
2495  {
2496  if (te2IsNull)
2497  PG_RETURN_NULL();
2498  /* swap null for non-null */
2499  ts2 = te2;
2500  te2IsNull = true;
2501  }
2502  else if (!te2IsNull)
2503  {
2504  if (TIMESTAMP_GT(ts2, te2))
2505  {
2506  Datum tt = ts2;
2507 
2508  ts2 = te2;
2509  te2 = tt;
2510  }
2511  }
2512 
2513  /*
2514  * At this point neither ts1 nor ts2 is null, so we can consider three
2515  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2516  */
2517  if (TIMESTAMP_GT(ts1, ts2))
2518  {
2519  /*
2520  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2521  * in the presence of nulls it's not quite completely so.
2522  */
2523  if (te2IsNull)
2524  PG_RETURN_NULL();
2525  if (TIMESTAMP_LT(ts1, te2))
2526  PG_RETURN_BOOL(true);
2527  if (te1IsNull)
2528  PG_RETURN_NULL();
2529 
2530  /*
2531  * If te1 is not null then we had ts1 <= te1 above, and we just found
2532  * ts1 >= te2, hence te1 >= te2.
2533  */
2534  PG_RETURN_BOOL(false);
2535  }
2536  else if (TIMESTAMP_LT(ts1, ts2))
2537  {
2538  /* This case is ts2 < te1 OR te2 < te1 */
2539  if (te1IsNull)
2540  PG_RETURN_NULL();
2541  if (TIMESTAMP_LT(ts2, te1))
2542  PG_RETURN_BOOL(true);
2543  if (te2IsNull)
2544  PG_RETURN_NULL();
2545 
2546  /*
2547  * If te2 is not null then we had ts2 <= te2 above, and we just found
2548  * ts2 >= te1, hence te2 >= te1.
2549  */
2550  PG_RETURN_BOOL(false);
2551  }
2552  else
2553  {
2554  /*
2555  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2556  * rather silly way of saying "true if both are non-null, else null".
2557  */
2558  if (te1IsNull || te2IsNull)
2559  PG_RETURN_NULL();
2560  PG_RETURN_BOOL(true);
2561  }
2562 
2563 #undef TIMESTAMP_GT
2564 #undef TIMESTAMP_LT
2565 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
#define TIMESTAMP_LT(t1, t2)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
uintptr_t Datum
Definition: postgres.h:365
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
#define TIMESTAMP_GT(t1, t2)
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ parse_sane_timezone()

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

Definition at line 458 of file timestamp.c.

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(), generate_unaccent_rules::type, TZ, TZ_STRLEN_MAX, and val.

Referenced by make_timestamptz_at_timezone().

459 {
460  char tzname[TZ_STRLEN_MAX + 1];
461  int rt;
462  int tz;
463 
464  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
465 
466  /*
467  * Look up the requested timezone. First we try to interpret it as a
468  * numeric timezone specification; if DecodeTimezone decides it doesn't
469  * like the format, we look in the timezone abbreviation table (to handle
470  * cases like "EST"), and if that also fails, we look in the timezone
471  * database (to handle cases like "America/New_York"). (This matches the
472  * order in which timestamp input checks the cases; it's important because
473  * the timezone database unwisely uses a few zone names that are identical
474  * to offset abbreviations.)
475  *
476  * Note pg_tzset happily parses numeric input that DecodeTimezone would
477  * reject. To avoid having it accept input that would otherwise be seen
478  * as invalid, it's enough to disallow having a digit in the first
479  * position of our input string.
480  */
481  if (isdigit((unsigned char) *tzname))
482  ereport(ERROR,
483  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
484  errmsg("invalid input syntax for numeric time zone: \"%s\"",
485  tzname),
486  errhint("Numeric time zones must have \"-\" or \"+\" as first character.")));
487 
488  rt = DecodeTimezone(tzname, &tz);
489  if (rt != 0)
490  {
491  char *lowzone;
492  int type,
493  val;
494  pg_tz *tzp;
495 
496  if (rt == DTERR_TZDISP_OVERFLOW)
497  ereport(ERROR,
498  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
499  errmsg("numeric time zone \"%s\" out of range", tzname)));
500  else if (rt != DTERR_BAD_FORMAT)
501  ereport(ERROR,
502  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
503  errmsg("time zone \"%s\" not recognized", tzname)));
504 
505  /* DecodeTimezoneAbbrev requires lowercase input */
506  lowzone = downcase_truncate_identifier(tzname,
507  strlen(tzname),
508  false);
509  type = DecodeTimezoneAbbrev(0, lowzone, &val, &tzp);
510 
511  if (type == TZ || type == DTZ)
512  {
513  /* fixed-offset abbreviation */
514  tz = -val;
515  }
516  else if (type == DYNTZ)
517  {
518  /* dynamic-offset abbreviation, resolve using specified time */
519  tz = DetermineTimeZoneAbbrevOffset(tm, tzname, tzp);
520  }
521  else
522  {
523  /* try it as a full zone name */
524  tzp = pg_tzset(tzname);
525  if (tzp)
526  tz = DetermineTimeZoneOffset(tm, tzp);
527  else
528  ereport(ERROR,
529  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
530  errmsg("time zone \"%s\" not recognized", tzname)));
531  }
532  }
533 
534  return tz;
535 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:282
int errhint(const char *fmt,...)
Definition: elog.c:987
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
int errcode(int sqlerrcode)
Definition: elog.c:575
#define TZ
Definition: datetime.h:96
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:213
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:236
#define TZ_STRLEN_MAX
Definition: pgtime.h:44
#define ERROR
Definition: elog.h:43
int DecodeTimezoneAbbrev(int field, char *lowtoken, int *offset, pg_tz **tz)
Definition: datetime.c:2968
int DecodeTimezone(char *str, int *tzp)
Definition: datetime.c:2890
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1455
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:286
Definition: pgtz.h:59
#define DTZ
Definition: datetime.h:97
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DYNTZ
Definition: datetime.h:98
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1613
long val
Definition: informix.c:689

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1558 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

1559 {
1561 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgReloadTime
Definition: timestamp.c:52

◆ pg_postmaster_start_time()

Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1552 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

1553 {
1555 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgStartTime
Definition: timestamp.c:49

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2003 of file timestamp.c.

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

Referenced by dttofmtasc_replace(), PGTYPEStimestamp_from_asc(), timestamp_in(), timestamp_part(), timestamptz_in(), and timestamptz_part().

2004 {
2005  Timestamp dt;
2006  struct pg_tm tt,
2007  *tm = &tt;
2008 
2009  GetEpochTime(tm);
2010  /* we don't bother to test for failure ... */
2011  tm2timestamp(tm, 0, NULL, &dt);
2012 
2013  return dt;
2014 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1854
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:1984

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1540 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1541 {
1543 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:709

◆ time2t()

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

Definition at line 1946 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

1947 {
1948  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
1949 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define SECS_PER_MINUTE
Definition: timestamp.h:88

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1670 of file timestamp.c.

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

1671 {
1672  TimestampTz result;
1673 
1674  result = (TimestampTz) tm -
1676  result *= USECS_PER_SEC;
1677 
1678  return result;
1679 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:107
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ timestamp2timestamptz()

static TimestampTz timestamp2timestamptz ( Timestamp  timestamp)
static

Definition at line 5070 of file timestamp.c.

References DetermineTimeZoneOffset(), ereport, errcode(), errmsg(), ERROR, session_timezone, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, and tm2timestamp().

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

5071 {
5072  TimestampTz result;
5073  struct pg_tm tt,
5074  *tm = &tt;
5075  fsec_t fsec;
5076  int tz;
5077 
5079  result = timestamp;
5080  else
5081  {
5082  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
5083  ereport(ERROR,
5084  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5085  errmsg("timestamp out of range")));
5086 
5088 
5089  if (tm2timestamp(tm, fsec, &tz, &result) != 0)
5090  ereport(ERROR,
5091  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5092  errmsg("timestamp out of range")));
5093  }
5094 
5095  return result;
5096 }
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
int errcode(int sqlerrcode)
Definition: