PostgreSQL Source Code  git master
timestamp.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <sys/time.h>
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/int128.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
#include "utils/float.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_support (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_support (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
Datum timeofday (PG_FUNCTION_ARGS)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
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)
 
static TimestampTz timestamptz_trunc_internal (text *units, TimestampTz timestamp, pg_tz *tzp)
 
Datum timestamptz_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc_zone (PG_FUNCTION_ARGS)
 
Datum interval_trunc (PG_FUNCTION_ARGS)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
static float8 NonFiniteTimestampTzPart (int type, int unit, char *lowunits, bool isNegative, bool isTz)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
bool TimestampTimestampTzRequiresRewrite (void)
 
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 1319 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().

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

◆ AdjustTimestampForTypmod()

static void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)
static

Definition at line 337 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().

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

◆ 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:570
#define ERROR
Definition: elog.h:43
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ 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:570
signed int int32
Definition: c.h:346
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:102
int errmsg(const char *fmt,...)
Definition: elog.c:784

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

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1546 {
1548 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1569
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40

◆ date2isoweek()

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

Definition at line 4310 of file timestamp.c.

References date2j(), and j2day().

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

4311 {
4312  float8 result;
4313  int day0,
4314  day4,
4315  dayn;
4316 
4317  /* current day */
4318  dayn = date2j(year, mon, mday);
4319 
4320  /* fourth day of current year */
4321  day4 = date2j(year, 1, 4);
4322 
4323  /* day0 == offset to first day of week (Monday) */
4324  day0 = j2day(day4 - 1);
4325 
4326  /*
4327  * We need the first week containing a Thursday, otherwise this day falls
4328  * into the previous year for purposes of counting weeks
4329  */
4330  if (dayn < day4 - day0)
4331  {
4332  day4 = date2j(year - 1, 1, 4);
4333 
4334  /* day0 == offset to first day of week (Monday) */
4335  day0 = j2day(day4 - 1);
4336  }
4337 
4338  result = (dayn - (day4 - day0)) / 7 + 1;
4339 
4340  /*
4341  * Sometimes the last few days in a year will fall into the first week of
4342  * the next year, so check for this.
4343  */
4344  if (result >= 52)
4345  {
4346  day4 = date2j(year + 1, 1, 4);
4347 
4348  /* day0 == offset to first day of week (Monday) */
4349  day0 = j2day(day4 - 1);
4350 
4351  if (dayn >= day4 - day0)
4352  result = (dayn - (day4 - day0)) / 7 + 1;
4353  }
4354 
4355  return (int) result;
4356 }
double float8
Definition: c.h:491
int j2day(int date)
Definition: datetime.c:330
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ date2isoyear()

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

Definition at line 4364 of file timestamp.c.

References date2j(), and j2day().

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

4365 {
4366  float8 result;
4367  int day0,
4368  day4,
4369  dayn;
4370 
4371  /* current day */
4372  dayn = date2j(year, mon, mday);
4373 
4374  /* fourth day of current year */
4375  day4 = date2j(year, 1, 4);
4376 
4377  /* day0 == offset to first day of week (Monday) */
4378  day0 = j2day(day4 - 1);
4379 
4380  /*
4381  * We need the first week containing a Thursday, otherwise this day falls
4382  * into the previous year for purposes of counting weeks
4383  */
4384  if (dayn < day4 - day0)
4385  {
4386  day4 = date2j(year - 1, 1, 4);
4387 
4388  /* day0 == offset to first day of week (Monday) */
4389  day0 = j2day(day4 - 1);
4390 
4391  year--;
4392  }
4393 
4394  result = (dayn - (day4 - day0)) / 7 + 1;
4395 
4396  /*
4397  * Sometimes the last few days in a year will fall into the first week of
4398  * the next year, so check for this.
4399  */
4400  if (result >= 52)
4401  {
4402  day4 = date2j(year + 1, 1, 4);
4403 
4404  /* day0 == offset to first day of week (Monday) */
4405  day0 = j2day(day4 - 1);
4406 
4407  if (dayn >= day4 - day0)
4408  year++;
4409  }
4410 
4411  return year;
4412 }
double float8
Definition: c.h:491
int j2day(int date)
Definition: datetime.c:330
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ date2isoyearday()

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

Definition at line 4421 of file timestamp.c.

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

Referenced by DCH_to_char().

4422 {
4423  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4424 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4364
int isoweek2j(int year, int week)
Definition: timestamp.c:4259
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 1971 of file timestamp.c.

References USECS_PER_SEC.

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

1972 {
1973  dt -= (tz * USECS_PER_SEC);
1974  return dt;
1975 }
#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 1750 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1751 {
1752  TimeOffset time;
1753 
1754  time = jd;
1755 
1756  *hour = time / USECS_PER_HOUR;
1757  time -= (*hour) * USECS_PER_HOUR;
1758  *min = time / USECS_PER_MINUTE;
1759  time -= (*min) * USECS_PER_MINUTE;
1760  *sec = time / USECS_PER_SEC;
1761  *fsec = time - (*sec * USECS_PER_SEC);
1762 } /* 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 1522 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().

1523 {
1524  if (TIMESTAMP_IS_NOBEGIN(dt))
1525  strcpy(str, EARLY);
1526  else if (TIMESTAMP_IS_NOEND(dt))
1527  strcpy(str, LATE);
1528  else /* shouldn't happen */
1529  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1530 }
#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(elevel,...)
Definition: elog.h:226
#define EARLY
Definition: datetime.h:40

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 706 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.

707 {
708  float8 seconds = PG_GETARG_FLOAT8(0);
709  TimestampTz result;
710 
711  /* Deal with NaN and infinite inputs ... */
712  if (isnan(seconds))
713  ereport(ERROR,
714  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
715  errmsg("timestamp cannot be NaN")));
716 
717  if (isinf(seconds))
718  {
719  if (seconds < 0)
720  TIMESTAMP_NOBEGIN(result);
721  else
722  TIMESTAMP_NOEND(result);
723  }
724  else
725  {
726  /* Out of range? */
727  if (seconds <
729  || seconds >=
731  ereport(ERROR,
732  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
733  errmsg("timestamp out of range: \"%g\"", seconds)));
734 
735  /* Convert UNIX epoch to Postgres epoch */
737 
738  seconds = rint(seconds * USECS_PER_SEC);
739  result = (int64) seconds;
740 
741  /* Recheck in case roundoff produces something just out of range */
742  if (!IS_VALID_TIMESTAMP(result))
743  ereport(ERROR,
744  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
745  errmsg("timestamp out of range: \"%g\"",
746  PG_GETARG_FLOAT8(0))));
747  }
748 
749  PG_RETURN_TIMESTAMP(result);
750 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#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:570
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#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:21
#define ereport(elevel, rest)
Definition: elog.h:141
#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:784
#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 5366 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.

5367 {
5368  FuncCallContext *funcctx;
5370  Timestamp result;
5371 
5372  /* stuff done only on the first call of the function */
5373  if (SRF_IS_FIRSTCALL())
5374  {
5375  Timestamp start = PG_GETARG_TIMESTAMP(0);
5376  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5377  Interval *step = PG_GETARG_INTERVAL_P(2);
5378  MemoryContext oldcontext;
5379  Interval interval_zero;
5380 
5381  /* create a function context for cross-call persistence */
5382  funcctx = SRF_FIRSTCALL_INIT();
5383 
5384  /*
5385  * switch to memory context appropriate for multiple function calls
5386  */
5387  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5388 
5389  /* allocate memory for user context */
5392 
5393  /*
5394  * Use fctx to keep state from call to call. Seed current with the
5395  * original start value
5396  */
5397  fctx->current = start;
5398  fctx->finish = finish;
5399  fctx->step = *step;
5400 
5401  /* Determine sign of the interval */
5402  MemSet(&interval_zero, 0, sizeof(Interval));
5403  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5404 
5405  if (fctx->step_sign == 0)
5406  ereport(ERROR,
5407  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5408  errmsg("step size cannot equal zero")));
5409 
5410  funcctx->user_fctx = fctx;
5411  MemoryContextSwitchTo(oldcontext);
5412  }
5413 
5414  /* stuff done on every call of the function */
5415  funcctx = SRF_PERCALL_SETUP();
5416 
5417  /*
5418  * get the saved state and use current as the result for this iteration
5419  */
5420  fctx = funcctx->user_fctx;
5421  result = fctx->current;
5422 
5423  if (fctx->step_sign > 0 ?
5424  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5425  timestamp_cmp_internal(result, fctx->finish) >= 0)
5426  {
5427  /* increment current in preparation for next iteration */
5428  fctx->current = DatumGetTimestamp(
5430  TimestampGetDatum(fctx->current),
5431  PointerGetDatum(&fctx->step)));
5432 
5433  /* do when there is more left to send */
5434  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5435  }
5436  else
5437  {
5438  /* do when there is no more left */
5439  SRF_RETURN_DONE(funcctx);
5440  }
5441 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
#define PointerGetDatum(X)
Definition: postgres.h:556
#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:570
#define MemSet(start, val, len)
Definition: c.h:955
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2817
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353
void * user_fctx
Definition: funcapi.h:83
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5447 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.

5448 {
5449  FuncCallContext *funcctx;
5451  TimestampTz result;
5452 
5453  /* stuff done only on the first call of the function */
5454  if (SRF_IS_FIRSTCALL())
5455  {
5457  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5458  Interval *step = PG_GETARG_INTERVAL_P(2);
5459  MemoryContext oldcontext;
5460  Interval interval_zero;
5461 
5462  /* create a function context for cross-call persistence */
5463  funcctx = SRF_FIRSTCALL_INIT();
5464 
5465  /*
5466  * switch to memory context appropriate for multiple function calls
5467  */
5468  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5469 
5470  /* allocate memory for user context */
5473 
5474  /*
5475  * Use fctx to keep state from call to call. Seed current with the
5476  * original start value
5477  */
5478  fctx->current = start;
5479  fctx->finish = finish;
5480  fctx->step = *step;
5481 
5482  /* Determine sign of the interval */
5483  MemSet(&interval_zero, 0, sizeof(Interval));
5484  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5485 
5486  if (fctx->step_sign == 0)
5487  ereport(ERROR,
5488  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5489  errmsg("step size cannot equal zero")));
5490 
5491  funcctx->user_fctx = fctx;
5492  MemoryContextSwitchTo(oldcontext);
5493  }
5494 
5495  /* stuff done on every call of the function */
5496  funcctx = SRF_PERCALL_SETUP();
5497 
5498  /*
5499  * get the saved state and use current as the result for this iteration
5500  */
5501  fctx = funcctx->user_fctx;
5502  result = fctx->current;
5503 
5504  if (fctx->step_sign > 0 ?
5505  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5506  timestamp_cmp_internal(result, fctx->finish) >= 0)
5507  {
5508  /* increment current in preparation for next iteration */
5509  fctx->current = DatumGetTimestampTz(
5512  PointerGetDatum(&fctx->step)));
5513 
5514  /* do when there is more left to send */
5515  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5516  }
5517  else
5518  {
5519  /* do when there is no more left */
5520  SRF_RETURN_DONE(funcctx);
5521  }
5522 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2045
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
#define PointerGetDatum(X)
Definition: postgres.h:556
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
#define MemSet(start, val, len)
Definition: c.h:955
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
#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:141
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2922
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353
void * user_fctx
Definition: funcapi.h:83
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1569 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(), 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(), drandom(), enable_timeout_after(), enable_timeout_at(), enable_timeouts(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), heap_vacuum_rel(), InitProcessGlobals(), launcher_determine_sleep(), 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_read_statsfiles(), pgstat_recv_inquiry(), pgstat_recv_resetsharedcounter(), pgstat_recv_resetsinglecounter(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), 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(), StartTransaction(), StartupXLOG(), test_pattern(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1570 {
1571  TimestampTz result;
1572  struct timeval tp;
1573 
1574  gettimeofday(&tp, NULL);
1575 
1576  result = (TimestampTz) tp.tv_sec -
1578  result = (result * USECS_PER_SEC) + tp.tv_usec;
1579 
1580  return result;
1581 }
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 2003 of file timestamp.c.

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

2004 {
2005  struct pg_tm *t0;
2006  pg_time_t epoch = 0;
2007 
2008  t0 = pg_gmtime(&epoch);
2009 
2010  if (t0 == NULL)
2011  elog(ERROR, "could not convert epoch to timestamp: %m");
2012 
2013  tm->tm_year = t0->tm_year;
2014  tm->tm_mon = t0->tm_mon;
2015  tm->tm_mday = t0->tm_mday;
2016  tm->tm_hour = t0->tm_hour;
2017  tm->tm_min = t0->tm_min;
2018  tm->tm_sec = t0->tm_sec;
2019 
2020  tm->tm_year += 1900;
2021  tm->tm_mon++;
2022 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1420
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32
#define elog(elevel,...)
Definition: elog.h:226
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 1587 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

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

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1601 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1602 {
1603  Timestamp ts;
1604 
1606  if (typmod >= 0)
1607  AdjustTimestampForTypmod(&ts, typmod);
1608  return ts;
1609 }
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:337
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5217
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

Definition at line 3356 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.

3357 {
3359  Interval *base = PG_GETARG_INTERVAL_P(1);
3360  Interval *offset = PG_GETARG_INTERVAL_P(2);
3361  bool sub = PG_GETARG_BOOL(3);
3362  bool less = PG_GETARG_BOOL(4);
3363  Interval *sum;
3364 
3365  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3366  ereport(ERROR,
3367  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3368  errmsg("invalid preceding or following size in window function")));
3369 
3370  /* We don't currently bother to avoid overflow hazards here */
3371  if (sub)
3373  IntervalPGetDatum(base),
3374  IntervalPGetDatum(offset)));
3375  else
3377  IntervalPGetDatum(base),
3378  IntervalPGetDatum(offset)));
3379 
3380  if (less)
3381  PG_RETURN_BOOL(interval_cmp_internal(val, sum) <= 0);
3382  else
3383  PG_RETURN_BOOL(interval_cmp_internal(val, sum) >= 0);
3384 }
#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:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3115
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3081
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353
int errmsg(const char *fmt,...)
Definition: elog.c:784
long val
Definition: informix.c:684
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2328

◆ in_range_timestamp_interval()

Datum in_range_timestamp_interval ( PG_FUNCTION_ARGS  )

Definition at line 3325 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().

3326 {
3328  Timestamp base = PG_GETARG_TIMESTAMP(1);
3329  Interval *offset = PG_GETARG_INTERVAL_P(2);
3330  bool sub = PG_GETARG_BOOL(3);
3331  bool less = PG_GETARG_BOOL(4);
3332  Timestamp sum;
3333 
3334  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3335  ereport(ERROR,
3336  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3337  errmsg("invalid preceding or following size in window function")));
3338 
3339  /* We don't currently bother to avoid overflow hazards here */
3340  if (sub)
3342  TimestampGetDatum(base),
3343  IntervalPGetDatum(offset)));
3344  else
3346  TimestampGetDatum(base),
3347  IntervalPGetDatum(offset)));
3348 
3349  if (less)
3350  PG_RETURN_BOOL(val <= sum);
3351  else
3352  PG_RETURN_BOOL(val >= sum);
3353 }
#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:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2896
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2817
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define ereport(elevel, rest)
Definition: elog.h:141
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
int errmsg(const char *fmt,...)
Definition: elog.c:784
long val
Definition: informix.c:684
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2328
#define DatumGetTimestamp(X)
Definition: timestamp.h:27

◆ in_range_timestamptz_interval()

Datum in_range_timestamptz_interval ( PG_FUNCTION_ARGS  )

Definition at line 3294 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.

3295 {
3298  Interval *offset = PG_GETARG_INTERVAL_P(2);
3299  bool sub = PG_GETARG_BOOL(3);
3300  bool less = PG_GETARG_BOOL(4);
3301  TimestampTz sum;
3302 
3303  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3304  ereport(ERROR,
3305  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3306  errmsg("invalid preceding or following size in window function")));
3307 
3308  /* We don't currently bother to avoid overflow hazards here */
3309  if (sub)
3311  TimestampTzGetDatum(base),
3312  IntervalPGetDatum(offset)));
3313  else
3315  TimestampTzGetDatum(base),
3316  IntervalPGetDatum(offset)));
3317 
3318  if (less)
3319  PG_RETURN_BOOL(val <= sum);
3320  else
3321  PG_RETURN_BOOL(val >= sum);
3322 }
#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:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#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:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
Datum timestamptz_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3006
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2922
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
long val
Definition: informix.c:684
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2328

◆ interval2tm()

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

Definition at line 1921 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().

1922 {
1923  TimeOffset time;
1924  TimeOffset tfrac;
1925 
1926  tm->tm_year = span.month / MONTHS_PER_YEAR;
1927  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1928  tm->tm_mday = span.day;
1929  time = span.time;
1930 
1931  tfrac = time / USECS_PER_HOUR;
1932  time -= tfrac * USECS_PER_HOUR;
1933  tm->tm_hour = tfrac;
1934  if (!SAMESIGN(tm->tm_hour, tfrac))
1935  ereport(ERROR,
1936  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1937  errmsg("interval out of range")));
1938  tfrac = time / USECS_PER_MINUTE;
1939  time -= tfrac * USECS_PER_MINUTE;
1940  tm->tm_min = tfrac;
1941  tfrac = time / USECS_PER_SEC;
1942  *fsec = time - (tfrac * USECS_PER_SEC);
1943  tm->tm_sec = tfrac;
1944 
1945  return 0;
1946 }
#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:570
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:141
#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:784
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 3398 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.

3399 {
3400  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3402  Datum *transdatums;
3403  int ndatums;
3404  Interval sumX,
3405  N;
3406  Interval *newsum;
3407  ArrayType *result;
3408 
3409  deconstruct_array(transarray,
3410  INTERVALOID, sizeof(Interval), false, 'd',
3411  &transdatums, NULL, &ndatums);
3412  if (ndatums != 2)
3413  elog(ERROR, "expected 2-element interval array");
3414 
3415  sumX = *(DatumGetIntervalP(transdatums[0]));
3416  N = *(DatumGetIntervalP(transdatums[1]));
3417 
3419  IntervalPGetDatum(&sumX),
3420  IntervalPGetDatum(newval)));
3421  N.time += 1;
3422 
3423  transdatums[0] = IntervalPGetDatum(newsum);
3424  transdatums[1] = IntervalPGetDatum(&N);
3425 
3426  result = construct_array(transdatums, 2,
3427  INTERVALOID, sizeof(Interval), false, 'd');
3428 
3429  PG_RETURN_ARRAYTYPE_P(result);
3430 }
#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:3291
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3081
#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:253
uintptr_t Datum
Definition: postgres.h:367
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ interval_accum_inv()

Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3482 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.

3483 {
3484  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3486  Datum *transdatums;
3487  int ndatums;
3488  Interval sumX,
3489  N;
3490  Interval *newsum;
3491  ArrayType *result;
3492 
3493  deconstruct_array(transarray,
3494  INTERVALOID, sizeof(Interval), false, 'd',
3495  &transdatums, NULL, &ndatums);
3496  if (ndatums != 2)
3497  elog(ERROR, "expected 2-element interval array");
3498 
3499  sumX = *(DatumGetIntervalP(transdatums[0]));
3500  N = *(DatumGetIntervalP(transdatums[1]));
3501 
3503  IntervalPGetDatum(&sumX),
3504  IntervalPGetDatum(newval)));
3505  N.time -= 1;
3506 
3507  transdatums[0] = IntervalPGetDatum(newsum);
3508  transdatums[1] = IntervalPGetDatum(&N);
3509 
3510  result = construct_array(transdatums, 2,
3511  INTERVALOID, sizeof(Interval), false, 'd');
3512 
3513  PG_RETURN_ARRAYTYPE_P(result);
3514 }
#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:3291
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3115
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#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:253
uintptr_t Datum
Definition: postgres.h:367
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 3517 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.

3518 {
3519  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3520  Datum *transdatums;
3521  int ndatums;
3522  Interval sumX,
3523  N;
3524 
3525  deconstruct_array(transarray,
3526  INTERVALOID, sizeof(Interval), false, 'd',
3527  &transdatums, NULL, &ndatums);
3528  if (ndatums != 2)
3529  elog(ERROR, "expected 2-element interval array");
3530 
3531  sumX = *(DatumGetIntervalP(transdatums[0]));
3532  N = *(DatumGetIntervalP(transdatums[1]));
3533 
3534  /* SQL defines AVG of no values to be NULL */
3535  if (N.time == 0)
3536  PG_RETURN_NULL();
3537 
3539  IntervalPGetDatum(&sumX),
3540  Float8GetDatum((double) N.time));
3541 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1723
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#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:367
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3242
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2416 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp(), and leftmostvalue_interval().

2417 {
2418  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2419  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2420 
2421  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2422 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353

◆ interval_cmp_internal()

static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

Definition at line 2353 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().

2354 {
2355  INT128 span1 = interval_cmp_value(interval1);
2356  INT128 span2 = interval_cmp_value(interval2);
2357 
2358  return int128_compare(span1, span2);
2359 }
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:2328

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2328 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().

2329 {
2330  INT128 span;
2331  int64 dayfraction;
2332  int64 days;
2333 
2334  /*
2335  * Separate time field into days and dayfraction, then add the month and
2336  * day fields to the days part. We cannot overflow int64 days here.
2337  */
2338  dayfraction = interval->time % USECS_PER_DAY;
2339  days = interval->time / USECS_PER_DAY;
2340  days += interval->month * INT64CONST(30);
2341  days += interval->day;
2342 
2343  /* Widen dayfraction to 128 bits */
2344  span = int64_to_int128(dayfraction);
2345 
2346  /* Scale up days to microseconds, forming a 128-bit product */
2348 
2349  return span;
2350 }
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:69
#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 3433 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.

3434 {
3435  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3436  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3437  Datum *transdatums1;
3438  Datum *transdatums2;
3439  int ndatums1;
3440  int ndatums2;
3441  Interval sum1,
3442  N1;
3443  Interval sum2,
3444  N2;
3445 
3446  Interval *newsum;
3447  ArrayType *result;
3448 
3449  deconstruct_array(transarray1,
3450  INTERVALOID, sizeof(Interval), false, 'd',
3451  &transdatums1, NULL, &ndatums1);
3452  if (ndatums1 != 2)
3453  elog(ERROR, "expected 2-element interval array");
3454 
3455  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3456  N1 = *(DatumGetIntervalP(transdatums1[1]));
3457 
3458  deconstruct_array(transarray2,
3459  INTERVALOID, sizeof(Interval), false, 'd',
3460  &transdatums2, NULL, &ndatums2);
3461  if (ndatums2 != 2)
3462  elog(ERROR, "expected 2-element interval array");
3463 
3464  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3465  N2 = *(DatumGetIntervalP(transdatums2[1]));
3466 
3468  IntervalPGetDatum(&sum1),
3469  IntervalPGetDatum(&sum2)));
3470  N1.time += N2.time;
3471 
3472  transdatums1[0] = IntervalPGetDatum(newsum);
3473  transdatums1[1] = IntervalPGetDatum(&N1);
3474 
3475  result = construct_array(transdatums1, 2,
3476  INTERVALOID, sizeof(Interval), false, 'd');
3477 
3478  PG_RETURN_ARRAYTYPE_P(result);
3479 }
#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:3291
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3081
#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:253
uintptr_t Datum
Definition: postgres.h:367
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3242 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().

3243 {
3244  Interval *span = PG_GETARG_INTERVAL_P(0);
3245  float8 factor = PG_GETARG_FLOAT8(1);
3246  double month_remainder_days,
3247  sec_remainder;
3248  int32 orig_month = span->month,
3249  orig_day = span->day;
3250  Interval *result;
3251 
3252  result = (Interval *) palloc(sizeof(Interval));
3253 
3254  if (factor == 0.0)
3255  ereport(ERROR,
3256  (errcode(ERRCODE_DIVISION_BY_ZERO),
3257  errmsg("division by zero")));
3258 
3259  result->month = (int32) (span->month / factor);
3260  result->day = (int32) (span->day / factor);
3261 
3262  /*
3263  * Fractional months full days into days. See comment in interval_mul().
3264  */
3265  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3266  month_remainder_days = TSROUND(month_remainder_days);
3267  sec_remainder = (orig_day / factor - result->day +
3268  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3269  sec_remainder = TSROUND(sec_remainder);
3270  if (Abs(sec_remainder) >= SECS_PER_DAY)
3271  {
3272  result->day += (int) (sec_remainder / SECS_PER_DAY);
3273  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3274  }
3275 
3276  /* cascade units down */
3277  result->day += (int32) month_remainder_days;
3278  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3279 
3280  PG_RETURN_INTERVAL_P(result);
3281 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#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:570
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:910
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#define SECS_PER_DAY
Definition: timestamp.h:86
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
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:784

◆ interval_eq()

Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2362 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

2363 {
2364  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2365  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2366 
2367  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2368 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 1992 of file timestamp.c.

References PG_RETURN_BOOL.

1993 {
1994  PG_RETURN_BOOL(true);
1995 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2407 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

2408 {
2409  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2410  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2411 
2412  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2413 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353

◆ interval_gt()

Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2389 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2390 {
2391  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2392  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2393 
2394  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2395 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2432 of file timestamp.c.

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

2433 {
2435  INT128 span = interval_cmp_value(interval);
2436  int64 span64;
2437 
2438  /*
2439  * Use only the least significant 64 bits for hashing. The upper 64 bits
2440  * seldom add any useful information, and besides we must do it like this
2441  * for compatibility with hashes calculated before use of INT128 was
2442  * introduced.
2443  */
2444  span64 = int128_to_int64(span);
2445 
2447 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:83
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
#define Int64GetDatumFast(X)
Definition: postgres.h:775
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:2328

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2450 of file timestamp.c.

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

2451 {
2453  INT128 span = interval_cmp_value(interval);
2454  int64 span64;
2455 
2456  /* Same approach as interval_hash */
2457  span64 = int128_to_int64(span);
2458 
2460  PG_GETARG_DATUM(1));
2461 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
#define Int64GetDatumFast(X)
Definition: postgres.h:775
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2328

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 871 of file timestamp.c.

References AdjustIntervalForTypmod(), DateTimeParseError(), DecodeInterval(), DecodeISO8601Interval(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTK_DELTA, elog, ereport, errcode(), errmsg(), ERROR, INTERVAL_FULL_RANGE, INTERVAL_RANGE, 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().

872 {
873  char *str = PG_GETARG_CSTRING(0);
874 
875 #ifdef NOT_USED
876  Oid typelem = PG_GETARG_OID(1);
877 #endif
878  int32 typmod = PG_GETARG_INT32(2);
879  Interval *result;
880  fsec_t fsec;
881  struct pg_tm tt,
882  *tm = &tt;
883  int dtype;
884  int nf;
885  int range;
886  int dterr;
887  char *field[MAXDATEFIELDS];
888  int ftype[MAXDATEFIELDS];
889  char workbuf[256];
890 
891  tm->tm_year = 0;
892  tm->tm_mon = 0;
893  tm->tm_mday = 0;
894  tm->tm_hour = 0;
895  tm->tm_min = 0;
896  tm->tm_sec = 0;
897  fsec = 0;
898 
899  if (typmod >= 0)
900  range = INTERVAL_RANGE(typmod);
901  else
902  range = INTERVAL_FULL_RANGE;
903 
904  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
905  ftype, MAXDATEFIELDS, &nf);
906  if (dterr == 0)
907  dterr = DecodeInterval(field, ftype, nf, range,
908  &dtype, tm, &fsec);
909 
910  /* if those functions think it's a bad format, try ISO8601 style */
911  if (dterr == DTERR_BAD_FORMAT)
912  dterr = DecodeISO8601Interval(str,
913  &dtype, tm, &fsec);
914 
915  if (dterr != 0)
916  {
917  if (dterr == DTERR_FIELD_OVERFLOW)
918  dterr = DTERR_INTERVAL_OVERFLOW;
919  DateTimeParseError(dterr, str, "interval");
920  }
921 
922  result = (Interval *) palloc(sizeof(Interval));
923 
924  switch (dtype)
925  {
926  case DTK_DELTA:
927  if (tm2interval(tm, fsec, result) != 0)
928  ereport(ERROR,
929  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
930  errmsg("interval out of range")));
931  break;
932 
933  default:
934  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
935  dtype, str);
936  }
937 
938  AdjustIntervalForTypmod(result, typmod);
939 
940  PG_RETURN_INTERVAL_P(result);
941 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3739
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define DTK_DELTA
Definition: datetime.h:160
int tm_hour
Definition: pgtime.h:29
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
signed int int32
Definition: c.h:346
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
#define DTERR_INTERVAL_OVERFLOW
Definition: datetime.h:283
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1319
int tm_mday
Definition: pgtime.h:30
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
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:141
int DecodeISO8601Interval(char *str, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3493
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define MAXDATEFIELDS
Definition: datetime.h:203
int tm_year
Definition: pgtime.h:32
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3067
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
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:540
int tm_min
Definition: pgtime.h:28
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1949

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2777 of file timestamp.c.

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

2778 {
2779  Interval *span = PG_GETARG_INTERVAL_P(0);
2780  Interval *result;
2781  int32 wholemonth;
2782 
2783  result = (Interval *) palloc(sizeof(Interval));
2784  result->month = span->month;
2785  result->day = span->day;
2786  result->time = span->time;
2787 
2788  wholemonth = result->day / DAYS_PER_MONTH;
2789  result->day -= wholemonth * DAYS_PER_MONTH;
2790  result->month += wholemonth;
2791 
2792  if (result->month > 0 && result->day < 0)
2793  {
2794  result->day += DAYS_PER_MONTH;
2795  result->month--;
2796  }
2797  else if (result->month < 0 && result->day > 0)
2798  {
2799  result->day -= DAYS_PER_MONTH;
2800  result->month++;
2801  }
2802 
2803  PG_RETURN_INTERVAL_P(result);
2804 }
#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:346
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 2742 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().

2743 {
2744  Interval *span = PG_GETARG_INTERVAL_P(0);
2745  Interval *result;
2746  TimeOffset wholeday;
2747 
2748  result = (Interval *) palloc(sizeof(Interval));
2749  result->month = span->month;
2750  result->day = span->day;
2751  result->time = span->time;
2752 
2753  TMODULO(result->time, wholeday, USECS_PER_DAY);
2754  result->day += wholeday; /* could overflow... */
2755 
2756  if (result->day > 0 && result->time < 0)
2757  {
2758  result->time += USECS_PER_DAY;
2759  result->day--;
2760  }
2761  else if (result->day < 0 && result->time > 0)
2762  {
2763  result->time -= USECS_PER_DAY;
2764  result->day++;
2765  }
2766 
2767  PG_RETURN_INTERVAL_P(result);
2768 }
#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:249

◆ interval_justify_interval()

Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2687 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.

2688 {
2689  Interval *span = PG_GETARG_INTERVAL_P(0);
2690  Interval *result;
2691  TimeOffset wholeday;
2692  int32 wholemonth;
2693 
2694  result = (Interval *) palloc(sizeof(Interval));
2695  result->month = span->month;
2696  result->day = span->day;
2697  result->time = span->time;
2698 
2699  TMODULO(result->time, wholeday, USECS_PER_DAY);
2700  result->day += wholeday; /* could overflow... */
2701 
2702  wholemonth = result->day / DAYS_PER_MONTH;
2703  result->day -= wholemonth * DAYS_PER_MONTH;
2704  result->month += wholemonth;
2705 
2706  if (result->month > 0 &&
2707  (result->day < 0 || (result->day == 0 && result->time < 0)))
2708  {
2709  result->day += DAYS_PER_MONTH;
2710  result->month--;
2711  }
2712  else if (result->month < 0 &&
2713  (result->day > 0 || (result->day == 0 && result->time > 0)))
2714  {
2715  result->day -= DAYS_PER_MONTH;
2716  result->month++;
2717  }
2718 
2719  if (result->day > 0 && result->time < 0)
2720  {
2721  result->time += USECS_PER_DAY;
2722  result->day--;
2723  }
2724  else if (result->day < 0 && result->time > 0)
2725  {
2726  result->time -= USECS_PER_DAY;
2727  result->day++;
2728  }
2729 
2730  PG_RETURN_INTERVAL_P(result);
2731 }
#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:346
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:249

◆ interval_larger()

Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3067 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3068 {
3069  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3070  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3071  Interval *result;
3072 
3073  if (interval_cmp_internal(interval1, interval2) > 0)
3074  result = interval1;
3075  else
3076  result = interval2;
3077  PG_RETURN_INTERVAL_P(result);
3078 }
#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:2353

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2398 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

2399 {
2400  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2401  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2402 
2403  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2404 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353

◆ interval_lt()

Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2380 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2381 {
2382  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2383  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2384 
2385  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2386 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3115 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().

3116 {
3117  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3118  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3119  Interval *result;
3120 
3121  result = (Interval *) palloc(sizeof(Interval));
3122 
3123  result->month = span1->month - span2->month;
3124  /* overflow check copied from int4mi */
3125  if (!SAMESIGN(span1->month, span2->month) &&
3126  !SAMESIGN(result->month, span1->month))
3127  ereport(ERROR,
3128  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3129  errmsg("interval out of range")));
3130 
3131  result->day = span1->day - span2->day;
3132  if (!SAMESIGN(span1->day, span2->day) &&
3133  !SAMESIGN(result->day, span1->day))
3134  ereport(ERROR,
3135  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3136  errmsg("interval out of range")));
3137 
3138  result->time = span1->time - span2->time;
3139  if (!SAMESIGN(span1->time, span2->time) &&
3140  !SAMESIGN(result->time, span1->time))
3141  ereport(ERROR,
3142  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3143  errmsg("interval out of range")));
3144 
3145  PG_RETURN_INTERVAL_P(result);
3146 }
#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:570
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:141
#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:784

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3155 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().

3156 {
3157  Interval *span = PG_GETARG_INTERVAL_P(0);
3158  float8 factor = PG_GETARG_FLOAT8(1);
3159  double month_remainder_days,
3160  sec_remainder,
3161  result_double;
3162  int32 orig_month = span->month,
3163  orig_day = span->day;
3164  Interval *result;
3165 
3166  result = (Interval *) palloc(sizeof(Interval));
3167 
3168  result_double = span->month * factor;
3169  if (isnan(result_double) ||
3170  result_double > INT_MAX || result_double < INT_MIN)
3171  ereport(ERROR,
3172  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3173  errmsg("interval out of range")));
3174  result->month = (int32) result_double;
3175 
3176  result_double = span->day * factor;
3177  if (isnan(result_double) ||
3178  result_double > INT_MAX || result_double < INT_MIN)
3179  ereport(ERROR,
3180  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3181  errmsg("interval out of range")));
3182  result->day = (int32) result_double;
3183 
3184  /*
3185  * The above correctly handles the whole-number part of the month and day
3186  * products, but we have to do something with any fractional part
3187  * resulting when the factor is non-integral. We cascade the fractions
3188  * down to lower units using the conversion factors DAYS_PER_MONTH and
3189  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3190  * so by the representation. The user can choose to cascade up later,
3191  * using justify_hours and/or justify_days.
3192  */
3193 
3194  /*
3195  * Fractional months full days into days.
3196  *
3197  * Floating point calculation are inherently imprecise, so these
3198  * calculations are crafted to produce the most reliable result possible.
3199  * TSROUND() is needed to more accurately produce whole numbers where
3200  * appropriate.
3201  */
3202  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3203  month_remainder_days = TSROUND(month_remainder_days);
3204  sec_remainder = (orig_day * factor - result->day +
3205  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3206  sec_remainder = TSROUND(sec_remainder);
3207 
3208  /*
3209  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3210  * cascade from months and days. It might still be >24 if the combination
3211  * of cascade and the seconds factor operation itself.
3212  */
3213  if (Abs(sec_remainder) >= SECS_PER_DAY)
3214  {
3215  result->day += (int) (sec_remainder / SECS_PER_DAY);
3216  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3217  }
3218 
3219  /* cascade units down */
3220  result->day += (int32) month_remainder_days;
3221  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3222  if (result_double > PG_INT64_MAX || result_double < PG_INT64_MIN)
3223  ereport(ERROR,
3224  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3225  errmsg("interval out of range")));
3226  result->time = (int64) result_double;
3227 
3228  PG_RETURN_INTERVAL_P(result);
3229 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_INT64_MAX
Definition: c.h:444
#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:570
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:910
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#define SECS_PER_DAY
Definition: timestamp.h:86
#define PG_INT64_MIN
Definition: c.h:443
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
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:784

◆ interval_ne()

Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2371 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2372 {
2373  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2374  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2375 
2376  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2377 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2353

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 947 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().

948 {
949  Interval *span = PG_GETARG_INTERVAL_P(0);
950  char *result;
951  struct pg_tm tt,
952  *tm = &tt;
953  fsec_t fsec;
954  char buf[MAXDATELEN + 1];
955 
956  if (interval2tm(*span, tm, &fsec) != 0)
957  elog(ERROR, "could not convert interval to tm");
958 
959  EncodeInterval(tm, fsec, IntervalStyle, buf);
960 
961  result = pstrdup(buf);
962  PG_RETURN_CSTRING(result);
963 }
#define MAXDATELEN
Definition: datetime.h:201
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int IntervalStyle
Definition: globals.c:117
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1921
static char * buf
Definition: pg_test_fsync.c:68
int32 fsec_t
Definition: timestamp.h:41
void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str)
Definition: datetime.c:4215
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
#define elog(elevel,...)
Definition: elog.h:226

◆ interval_part()

Datum interval_part ( PG_FUNCTION_ARGS  )

Definition at line 4910 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.

4911 {
4912  text *units = PG_GETARG_TEXT_PP(0);
4914  float8 result;
4915  int type,
4916  val;
4917  char *lowunits;
4918  fsec_t fsec;
4919  struct pg_tm tt,
4920  *tm = &tt;
4921 
4922  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4923  VARSIZE_ANY_EXHDR(units),
4924  false);
4925 
4926  type = DecodeUnits(0, lowunits, &val);
4927  if (type == UNKNOWN_FIELD)
4928  type = DecodeSpecial(0, lowunits, &val);
4929 
4930  if (type == UNITS)
4931  {
4932  if (interval2tm(*interval, tm, &fsec) == 0)
4933  {
4934  switch (val)
4935  {
4936  case DTK_MICROSEC:
4937  result = tm->tm_sec * 1000000.0 + fsec;
4938  break;
4939 
4940  case DTK_MILLISEC:
4941  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4942  break;
4943 
4944  case DTK_SECOND:
4945  result = tm->tm_sec + fsec / 1000000.0;
4946  break;
4947 
4948  case DTK_MINUTE:
4949  result = tm->tm_min;
4950  break;
4951 
4952  case DTK_HOUR:
4953  result = tm->tm_hour;
4954  break;
4955 
4956  case DTK_DAY:
4957  result = tm->tm_mday;
4958  break;
4959 
4960  case DTK_MONTH:
4961  result = tm->tm_mon;
4962  break;
4963 
4964  case DTK_QUARTER:
4965  result = (tm->tm_mon / 3) + 1;
4966  break;
4967 
4968  case DTK_YEAR:
4969  result = tm->tm_year;
4970  break;
4971 
4972  case DTK_DECADE:
4973  /* caution: C division may have negative remainder */
4974  result = tm->tm_year / 10;
4975  break;
4976 
4977  case DTK_CENTURY:
4978  /* caution: C division may have negative remainder */
4979  result = tm->tm_year / 100;
4980  break;
4981 
4982  case DTK_MILLENNIUM:
4983  /* caution: C division may have negative remainder */
4984  result = tm->tm_year / 1000;
4985  break;
4986 
4987  default:
4988  ereport(ERROR,
4989  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4990  errmsg("interval units \"%s\" not supported",
4991  lowunits)));
4992  result = 0;
4993  }
4994 
4995  }
4996  else
4997  {
4998  elog(ERROR, "could not convert interval to tm");
4999  result = 0;
5000  }
5001  }
5002  else if (type == RESERV && val == DTK_EPOCH)
5003  {
5004  result = interval->time / 1000000.0;
5005  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
5006  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
5007  result += ((double) SECS_PER_DAY) * interval->day;
5008  }
5009  else
5010  {
5011  ereport(ERROR,
5012  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5013  errmsg("interval units \"%s\" not recognized",
5014  lowunits)));
5015  result = 0;
5016  }
5017 
5018  PG_RETURN_FLOAT8(result);
5019 }
#define DTK_CENTURY
Definition: datetime.h:170
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define DTK_YEAR
Definition: datetime.h:168
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:167
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:356
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:570
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3702
Definition: pgtime.h:25
int32 day
Definition: timestamp.h:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static struct pg_tm tm
Definition: localtime.c:108
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define DTK_DECADE
Definition: datetime.h:169
#define DAYS_PER_YEAR
Definition: timestamp.h:68
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#define SECS_PER_DAY
Definition: timestamp.h:86
#define DTK_SECOND
Definition: datetime.h:161
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1921
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:141
#define DTK_MINUTE
Definition: datetime.h:162
int32 month
Definition: timestamp.h:48
#define DTK_MICROSEC
Definition: datetime.h:173
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3011
#define DTK_EPOCH
Definition: datetime.h:153
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
Definition: c.h:549
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:684

◆ interval_pl()

Datum interval_pl ( PG_FUNCTION_ARGS  )

Definition at line 3081 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().

3082 {
3083  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3084  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3085  Interval *result;
3086 
3087  result = (Interval *) palloc(sizeof(Interval));
3088 
3089  result->month = span1->month + span2->month;
3090  /* overflow check copied from int4pl */
3091  if (SAMESIGN(span1->month, span2->month) &&
3092  !SAMESIGN(result->month, span1->month))
3093  ereport(ERROR,
3094  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3095  errmsg("interval out of range")));
3096 
3097  result->day = span1->day + span2->day;
3098  if (SAMESIGN(span1->day, span2->day) &&
3099  !SAMESIGN(result->day, span1->day))
3100  ereport(ERROR,
3101  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3102  errmsg("interval out of range")));
3103 
3104  result->time = span1->time + span2->time;
3105  if (SAMESIGN(span1->time, span2->time) &&
3106  !SAMESIGN(result->time, span1->time))
3107  ereport(ERROR,
3108  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3109  errmsg("interval out of range")));
3110 
3111  PG_RETURN_INTERVAL_P(result);
3112 }
#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:570
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:141
#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:784

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

Definition at line 969 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.

970 {
972 
973 #ifdef NOT_USED
974  Oid typelem = PG_GETARG_OID(1);
975 #endif
976  int32 typmod = PG_GETARG_INT32(2);
978 
979  interval = (Interval *) palloc(sizeof(Interval));
980 
981  interval->time = pq_getmsgint64(buf);
982  interval->day = pq_getmsgint(buf, sizeof(interval->day));
983  interval->month = pq_getmsgint(buf, sizeof(interval->month));
984 
985  AdjustIntervalForTypmod(interval, typmod);
986 
987  PG_RETURN_INTERVAL_P(interval);
988 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1319
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
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 1300 of file timestamp.c.

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

1301 {
1303  int32 typmod = PG_GETARG_INT32(1);
1304  Interval *result;
1305 
1306  result = palloc(sizeof(Interval));
1307  *result = *interval;
1308 
1309  AdjustIntervalForTypmod(result, typmod);
1310 
1311  PG_RETURN_INTERVAL_P(result);
1312 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#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:346
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1319
void * palloc(Size size)
Definition: mcxt.c:924

◆ interval_send()

Datum interval_send ( PG_FUNCTION_ARGS  )

Definition at line 994 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.

995 {
998 
999  pq_begintypsend(&buf);
1000  pq_sendint64(&buf, interval->time);
1001  pq_sendint32(&buf, interval->day);
1002  pq_sendint32(&buf, interval->month);
1004 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
int32 day
Definition: timestamp.h:47
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:68
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3052 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3053 {
3054  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3055  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3056  Interval *result;
3057 
3058  /* use interval_cmp_internal to be sure this agrees with comparisons */
3059  if (interval_cmp_internal(interval1, interval2) < 0)
3060  result = interval1;
3061  else
3062  result = interval2;
3063  PG_RETURN_INTERVAL_P(result);
3064 }
#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:2353

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1237 of file timestamp.c.

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

1238 {
1239  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1240  Node *ret = NULL;
1241 
1242  if (IsA(rawreq, SupportRequestSimplify))
1243  {
1245  FuncExpr *expr = req->fcall;
1246  Node *typmod;
1247 
1248  Assert(list_length(expr->args) >= 2);
1249 
1250  typmod = (Node *) lsecond(expr->args);
1251 
1252  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
1253  {
1254  Node *source = (Node *) linitial(expr->args);
1255  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1256  bool noop;
1257 
1258  if (new_typmod < 0)
1259  noop = true;
1260  else
1261  {
1262  int32 old_typmod = exprTypmod(source);
1263  int old_least_field;
1264  int new_least_field;
1265  int old_precis;
1266  int new_precis;
1267 
1268  old_least_field = intervaltypmodleastfield(old_typmod);
1269  new_least_field = intervaltypmodleastfield(new_typmod);
1270  if (old_typmod < 0)
1271  old_precis = INTERVAL_FULL_PRECISION;
1272  else
1273  old_precis = INTERVAL_PRECISION(old_typmod);
1274  new_precis = INTERVAL_PRECISION(new_typmod);
1275 
1276  /*
1277  * Cast is a no-op if least field stays the same or decreases
1278  * while precision stays the same or increases. But
1279  * precision, which is to say, sub-second precision, only
1280  * affects ranges that include SECOND.
1281  */
1282  noop = (new_least_field <= old_least_field) &&
1283  (old_least_field > 0 /* SECOND */ ||
1284  new_precis >= MAX_INTERVAL_PRECISION ||
1285  new_precis >= old_precis);
1286  }
1287  if (noop)
1288  ret = relabel_to_typmod(source, new_typmod);
1289  }
1290  }
1291 
1292  PG_RETURN_POINTER(ret);
1293 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
List * args
Definition: primnodes.h:463
#define DatumGetInt32(X)
Definition: postgres.h:472
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:524
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define lsecond(l)
Definition: pg_list.h:200
signed int int32
Definition: c.h:346
#define linitial(l)
Definition: pg_list.h:195
#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:732
static int list_length(const List *l)
Definition: pg_list.h:169
static int intervaltypmodleastfield(int32 typmod)
Definition: timestamp.c:1184
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:588

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 4155 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.

4156 {
4157  text *units = PG_GETARG_TEXT_PP(0);
4159  Interval *result;
4160  int type,
4161  val;
4162  char *lowunits;
4163  fsec_t fsec;
4164  struct pg_tm tt,
4165  *tm = &tt;
4166 
4167  result = (Interval *) palloc(sizeof(Interval));
4168 
4169  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4170  VARSIZE_ANY_EXHDR(units),
4171  false);
4172 
4173  type = DecodeUnits(0, lowunits, &val);
4174 
4175  if (type == UNITS)
4176  {
4177  if (interval2tm(*interval, tm, &fsec) == 0)
4178  {
4179  switch (val)
4180  {
4181  case DTK_MILLENNIUM:
4182  /* caution: C division may have negative remainder */
4183  tm->tm_year = (tm->tm_year / 1000) * 1000;
4184  /* FALL THRU */
4185  case DTK_CENTURY:
4186  /* caution: C division may have negative remainder */
4187  tm->tm_year = (tm->tm_year / 100) * 100;
4188  /* FALL THRU */
4189  case DTK_DECADE:
4190  /* caution: C division may have negative remainder */
4191  tm->tm_year = (tm->tm_year / 10) * 10;
4192  /* FALL THRU */
4193  case DTK_YEAR:
4194  tm->tm_mon = 0;
4195  /* FALL THRU */
4196  case DTK_QUARTER:
4197  tm->tm_mon = 3 * (tm->tm_mon / 3);
4198  /* FALL THRU */
4199  case DTK_MONTH:
4200  tm->tm_mday = 0;
4201  /* FALL THRU */
4202  case DTK_DAY:
4203  tm->tm_hour = 0;
4204  /* FALL THRU */
4205  case DTK_HOUR:
4206  tm->tm_min = 0;
4207  /* FALL THRU */
4208  case DTK_MINUTE:
4209  tm->tm_sec = 0;
4210  /* FALL THRU */
4211  case DTK_SECOND:
4212  fsec = 0;
4213  break;
4214  case DTK_MILLISEC:
4215  fsec = (fsec / 1000) * 1000;
4216  break;
4217  case DTK_MICROSEC:
4218  break;
4219 
4220  default:
4221  if (val == DTK_WEEK)
4222  ereport(ERROR,
4223  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4224  errmsg("interval units \"%s\" not supported "
4225  "because months usually have fractional weeks",
4226  lowunits)));
4227  else
4228  ereport(ERROR,
4229  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4230  errmsg("interval units \"%s\" not supported",
4231  lowunits)));
4232  }
4233 
4234  if (tm2interval(tm, fsec, result) != 0)
4235  ereport(ERROR,
4236  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4237  errmsg("interval out of range")));
4238  }
4239  else
4240  elog(ERROR, "could not convert interval to tm");
4241  }
4242  else
4243  {
4244  ereport(ERROR,
4245  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4246  errmsg("interval units \"%s\" not recognized",
4247  lowunits)));
4248  }
4249 
4250  PG_RETURN_INTERVAL_P(result);
4251 }
#define DTK_CENTURY
Definition: datetime.h:170
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define DTK_WEEK
Definition: datetime.h:165
#define DTK_YEAR
Definition: datetime.h:168
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:167
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:570
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3702
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static struct pg_tm tm
Definition: localtime.c:108
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:43
#define DTK_SECOND
Definition: datetime.h:161
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1921
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:141
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
#define DTK_DAY
Definition: datetime.h:164
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
Definition: c.h:549
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:684
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1949

◆ interval_um()

Datum interval_um ( PG_FUNCTION_ARGS  )

Definition at line 3023 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().

3024 {
3026  Interval *result;
3027 
3028  result = (Interval *) palloc(sizeof(Interval));
3029 
3030  result->time = -interval->time;
3031  /* overflow check copied from int4um */
3032  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3033  ereport(ERROR,
3034  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3035  errmsg("interval out of range")));
3036  result->day = -interval->day;
3037  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3038  ereport(ERROR,
3039  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3040  errmsg("interval out of range")));
3041  result->month = -interval->month;
3042  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3043  ereport(ERROR,
3044  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3045  errmsg("interval out of range")));
3046 
3047  PG_RETURN_INTERVAL_P(result);
3048 }
#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:570
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:141
#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:784

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1019 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.

1020 {
1022  int32 *tl;
1023  int n;
1024  int32 typmod;
1025 
1026  tl = ArrayGetIntegerTypmods(ta, &n);
1027 
1028  /*
1029  * tl[0] - interval range (fields bitmask) tl[1] - precision (optional)
1030  *
1031  * Note we must validate tl[0] even though it's normally guaranteed
1032  * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
1033  */
1034  if (n > 0)
1035  {
1036  switch (tl[0])
1037  {
1038  case INTERVAL_MASK(YEAR):
1039  case INTERVAL_MASK(MONTH):
1040  case INTERVAL_MASK(DAY):
1041  case INTERVAL_MASK(HOUR):
1042  case INTERVAL_MASK(MINUTE):
1043  case INTERVAL_MASK(SECOND):
1051  case INTERVAL_FULL_RANGE:
1052  /* all OK */
1053  break;
1054  default:
1055  ereport(ERROR,
1056  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1057  errmsg("invalid INTERVAL type modifier")));
1058  }
1059  }
1060 
1061  if (n == 1)
1062  {
1063  if (tl[0] != INTERVAL_FULL_RANGE)
1064  typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, tl[0]);
1065  else
1066  typmod = -1;
1067  }
1068  else if (n == 2)
1069  {
1070  if (tl[1] < 0)
1071  ereport(ERROR,
1072  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1073  errmsg("INTERVAL(%d) precision must not be negative",
1074  tl[1])));
1075  if (tl[1] > MAX_INTERVAL_PRECISION)
1076  {
1077  ereport(WARNING,
1078  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1079  errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1080  tl[1], MAX_INTERVAL_PRECISION)));
1081  typmod = INTERVAL_TYPMOD(MAX_INTERVAL_PRECISION, tl[0]);
1082  }
1083  else
1084  typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
1085  }
1086  else
1087  {
1088  ereport(ERROR,
1089  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1090  errmsg("invalid INTERVAL type modifier")));
1091  typmod = 0; /* keep compiler quiet */
1092  }
1093 
1094  PG_RETURN_INT32(typmod);
1095 }
#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:344
int errcode(int sqlerrcode)
Definition: elog.c:570
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
signed int int32
Definition: c.h:346
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#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:141
#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:784
#define HOUR
Definition: datetime.h:101

◆ intervaltypmodleastfield()

static int intervaltypmodleastfield ( int32  typmod)
static

Definition at line 1184 of file timestamp.c.

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

Referenced by interval_support().

1185 {
1186  if (typmod < 0)
1187  return 0; /* SECOND */
1188 
1189  switch (INTERVAL_RANGE(typmod))
1190  {
1191  case INTERVAL_MASK(YEAR):
1192  return 5; /* YEAR */
1193  case INTERVAL_MASK(MONTH):
1194  return 4; /* MONTH */
1195  case INTERVAL_MASK(DAY):
1196  return 3; /* DAY */
1197  case INTERVAL_MASK(HOUR):
1198  return 2; /* HOUR */
1199  case INTERVAL_MASK(MINUTE):
1200  return 1; /* MINUTE */
1201  case INTERVAL_MASK(SECOND):
1202  return 0; /* SECOND */
1204  return 4; /* MONTH */
1206  return 2; /* HOUR */
1208  return 1; /* MINUTE */
1210  return 0; /* SECOND */
1212  return 1; /* MINUTE */
1214  return 0; /* SECOND */
1216  return 0; /* SECOND */
1217  case INTERVAL_FULL_RANGE:
1218  return 0; /* SECOND */
1219  default:
1220  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1221  break;
1222  }
1223  return 0; /* can't get here, but keep compiler quiet */
1224 }
#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(elevel,...)
Definition: elog.h:226
#define HOUR
Definition: datetime.h:101

◆ intervaltypmodout()

Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1098 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.

1099 {
1100  int32 typmod = PG_GETARG_INT32(0);
1101  char *res = (char *) palloc(64);
1102  int fields;
1103  int precision;
1104  const char *fieldstr;
1105 
1106  if (typmod < 0)
1107  {
1108  *res = '\0';
1109  PG_RETURN_CSTRING(res);
1110  }
1111 
1112  fields = INTERVAL_RANGE(typmod);
1113  precision = INTERVAL_PRECISION(typmod);
1114 
1115  switch (fields)
1116  {
1117  case INTERVAL_MASK(YEAR):
1118  fieldstr = " year";
1119  break;
1120  case INTERVAL_MASK(MONTH):
1121  fieldstr = " month";
1122  break;
1123  case INTERVAL_MASK(DAY):
1124  fieldstr = " day";
1125  break;
1126  case INTERVAL_MASK(HOUR):
1127  fieldstr = " hour";
1128  break;
1129  case INTERVAL_MASK(MINUTE):
1130  fieldstr = " minute";
1131  break;
1132  case INTERVAL_MASK(SECOND):
1133  fieldstr = " second";
1134  break;
1136  fieldstr = " year to month";
1137  break;
1139  fieldstr = " day to hour";
1140  break;
1142  fieldstr = " day to minute";
1143  break;
1145  fieldstr = " day to second";
1146  break;
1148  fieldstr = " hour to minute";
1149  break;
1151  fieldstr = " hour to second";
1152  break;
1154  fieldstr = " minute to second";
1155  break;
1156  case INTERVAL_FULL_RANGE:
1157  fieldstr = "";
1158  break;
1159  default:
1160  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1161  fieldstr = "";
1162  break;
1163  }
1164 
1165  if (precision != INTERVAL_FULL_PRECISION)
1166  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1167  else
1168  snprintf(res, 64, "%s", fieldstr);
1169 
1170  PG_RETURN_CSTRING(res);
1171 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#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
signed int int32
Definition: c.h:346
#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:352
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
void * palloc(Size size)
Definition: mcxt.c:924
#define elog(elevel,...)
Definition: elog.h:226
#define HOUR
Definition: datetime.h:101
#define snprintf
Definition: port.h:192

◆ isoweek2date()

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

Definition at line 4279 of file timestamp.c.

References isoweek2j(), and j2date().

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

4280 {
4281  j2date(isoweek2j(*year, woy), year, mon, mday);
4282 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
int isoweek2j(int year, int week)
Definition: timestamp.c:4259

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4259 of file timestamp.c.

References date2j(), and j2day().

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

4260 {
4261  int day0,
4262  day4;
4263 
4264  /* fourth day of current year */
4265  day4 = date2j(year, 1, 4);
4266 
4267  /* day0 == offset to first day of week (Monday) */
4268  day0 = j2day(day4 - 1);
4269 
4270  return ((week - 1) * 7) + (day4 - day0);
4271 }
int j2day(int date)
Definition: datetime.c:330
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ isoweekdate2date()

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

Definition at line 4292 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4293 {
4294  int jday;
4295 
4296  jday = isoweek2j(*year, isoweek);
4297  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4298  if (wday > 1)
4299  jday += wday - 2;
4300  else
4301  jday += 6;
4302  j2date(jday, year, mon, mday);
4303 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
int isoweek2j(int year, int week)
Definition: timestamp.c:4259

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1486 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.

1487 {
1488  int32 years = PG_GETARG_INT32(0);
1490  int32 weeks = PG_GETARG_INT32(2);
1491  int32 days = PG_GETARG_INT32(3);
1492  int32 hours = PG_GETARG_INT32(4);
1493  int32 mins = PG_GETARG_INT32(5);
1494  double secs = PG_GETARG_FLOAT8(6);
1495  Interval *result;
1496 
1497  /*
1498  * Reject out-of-range inputs. We really ought to check the integer
1499  * inputs as well, but it's not entirely clear what limits to apply.
1500  */
1501  if (isinf(secs) || isnan(secs))
1502  ereport(ERROR,
1503  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1504  errmsg("interval out of range")));
1505 
1506  result = (Interval *) palloc(sizeof(Interval));
1507  result->month = years * MONTHS_PER_YEAR + months;
1508  result->day = weeks * 7 + days;
1509 
1510  secs = rint(secs * USECS_PER_SEC);
1511  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1512  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1513  (int64) secs;
1514 
1515  PG_RETURN_INTERVAL_P(result);
1516 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#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:570
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
const char *const months[]
Definition: datetime.c:66
#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:69
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
#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:784

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 625 of file timestamp.c.

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

626 {
627  int32 year = PG_GETARG_INT32(0);
628  int32 month = PG_GETARG_INT32(1);
629  int32 mday = PG_GETARG_INT32(2);
630  int32 hour = PG_GETARG_INT32(3);
631  int32 min = PG_GETARG_INT32(4);
632  float8 sec = PG_GETARG_FLOAT8(5);
633  Timestamp result;
634 
635  result = make_timestamp_internal(year, month, mday,
636  hour, min, sec);
637 
638  PG_RETURN_TIMESTAMP(result);
639 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:537
signed int int32
Definition: c.h:346
double float8
Definition: c.h:491
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 537 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().

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

◆ make_timestamptz()

Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 645 of file timestamp.c.

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

646 {
647  int32 year = PG_GETARG_INT32(0);
648  int32 month = PG_GETARG_INT32(1);
649  int32 mday = PG_GETARG_INT32(2);
650  int32 hour = PG_GETARG_INT32(3);
651  int32 min = PG_GETARG_INT32(4);
652  float8 sec = PG_GETARG_FLOAT8(5);
653  Timestamp result;
654 
655  result = make_timestamp_internal(year, month, mday,
656  hour, min, sec);
657 
659 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:5177
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:537
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:346
double float8
Definition: c.h:491
int64 Timestamp
Definition: timestamp.h:38

◆ make_timestamptz_at_timezone()

Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 666 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().

667 {
668  int32 year = PG_GETARG_INT32(0);
669  int32 month = PG_GETARG_INT32(1);
670  int32 mday = PG_GETARG_INT32(2);
671  int32 hour = PG_GETARG_INT32(3);
672  int32 min = PG_GETARG_INT32(4);
673  float8 sec = PG_GETARG_FLOAT8(5);
675  TimestampTz result;
677  struct pg_tm tt;
678  int tz;
679  fsec_t fsec;
680 
681  timestamp = make_timestamp_internal(year, month, mday,
682  hour, min, sec);
683 
684  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
685  ereport(ERROR,
686  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
687  errmsg("timestamp out of range")));
688 
689  tz = parse_sane_timezone(&tt, zone);
690 
691  result = dt2local(timestamp, -tz);
692 
693  if (!IS_VALID_TIMESTAMP(result))
694  ereport(ERROR,
695  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
696  errmsg("timestamp out of range")));
697 
698  PG_RETURN_TIMESTAMPTZ(result);
699 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
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:537
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1777
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:346
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:141
int64 Timestamp
Definition: timestamp.h:38
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Definition: zic.c:98
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:1971
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:453

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3232 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3233 {
3234  /* Args are float8 and Interval *, but leave them as generic Datum */
3235  Datum factor = PG_GETARG_DATUM(0);
3236  Datum span = PG_GETARG_DATUM(1);
3237 
3238  return DirectFunctionCall2(interval_mul, span, factor);
3239 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
uintptr_t Datum
Definition: postgres.h:367
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3155
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ NonFiniteTimestampTzPart()

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

Definition at line 4438 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().

4440 {
4441  if ((type != UNITS) && (type != RESERV))
4442  {
4443  if (isTz)
4444  ereport(ERROR,
4445  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4446  errmsg("timestamp with time zone units \"%s\" not recognized",
4447  lowunits)));
4448  else
4449  ereport(ERROR,
4450  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4451  errmsg("timestamp units \"%s\" not recognized",
4452  lowunits)));
4453  }
4454 
4455  switch (unit)
4456  {
4457  /* Oscillating units */
4458  case DTK_MICROSEC:
4459  case DTK_MILLISEC:
4460  case DTK_SECOND:
4461  case DTK_MINUTE:
4462  case DTK_HOUR:
4463  case DTK_DAY:
4464  case DTK_MONTH:
4465  case DTK_QUARTER:
4466  case DTK_WEEK:
4467  case DTK_DOW:
4468  case DTK_ISODOW:
4469  case DTK_DOY:
4470  case DTK_TZ:
4471  case DTK_TZ_MINUTE:
4472  case DTK_TZ_HOUR:
4473  return 0.0;
4474 
4475  /* Monotonically-increasing units */
4476  case DTK_YEAR:
4477  case DTK_DECADE:
4478  case DTK_CENTURY:
4479  case DTK_MILLENNIUM:
4480  case DTK_JULIAN:
4481  case DTK_ISOYEAR:
4482  case DTK_EPOCH:
4483  if (isNegative)
4484  return -get_float8_infinity();
4485  else
4486  return get_float8_infinity();
4487 
4488  default:
4489  if (isTz)
4490  ereport(ERROR,
4491  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4492  errmsg("timestamp with time zone units \"%s\" not supported",
4493  lowunits)));
4494  else
4495  ereport(ERROR,
4496  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4497  errmsg("timestamp units \"%s\" not supported",
4498  lowunits)));
4499  return 0.0; /* keep compiler quiet */
4500  }
4501 }
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DTK_CENTURY
Definition: datetime.h:170
#define UNITS
Definition: datetime.h:108
static float8 get_float8_infinity(void)
Definition: float.h:90
#define DTK_JULIAN
Definition: datetime.h:174
#define DTK_WEEK
Definition: datetime.h:165
#define DTK_YEAR
Definition: datetime.h:168
#define DTK_QUARTER
Definition: datetime.h:167
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:570
#define DTK_ISODOW
Definition: datetime.h:181
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:43
#define DTK_SECOND
Definition: datetime.h:161
#define DTK_ISOYEAR
Definition: datetime.h:180
#define DTK_TZ_MINUTE
Definition: datetime.h:179
#define ereport(elevel, rest)
Definition: elog.h:141
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_DOW
Definition: datetime.h:176
#define DTK_MICROSEC
Definition: datetime.h:173
#define DTK_DOY
Definition: datetime.h:177
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
#define DTK_EPOCH
Definition: datetime.h:153
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ now()

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2470 of file timestamp.c.

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

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

◆ parse_sane_timezone()

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

Definition at line 453 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().

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

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1557 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

1558 {
1560 }
#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 1551 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

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

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2025 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().

2026 {
2027  Timestamp dt;
2028  struct pg_tm tt,
2029  *tm = &tt;
2030 
2031  GetEpochTime(tm);
2032  /* we don't bother to test for failure ... */
2033  tm2timestamp(tm, 0, NULL, &dt);
2034 
2035  return dt;
2036 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1873
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2003

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1539 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1540 {
1542 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:792

◆ time2t()

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

Definition at line 1965 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

1966 {
1967  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
1968 }
#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 1689 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().

1690 {
1691  TimestampTz result;
1692 
1693  result = (TimestampTz) tm -
1695  result *= USECS_PER_SEC;
1696 
1697  return result;
1698 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:108
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ timeofday()

Datum timeofday ( PG_FUNCTION_ARGS  )

Definition at line 1615 of file timestamp.c.

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

1616 {
1617  struct timeval tp;
1618  char templ[128];
1619  char buf[128];
1620  pg_time_t tt;
1621 
1622  gettimeofday(&tp, NULL);
1623  tt = (pg_time_t) tp.tv_sec;
1624  pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1626  snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1627 
1629 }
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:122
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
int64 pg_time_t
Definition: pgtime.h:23
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
static char * buf
Definition: pg_test_fsync.c:68
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
struct pg_tm * pg_localtime(const pg_time_