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/date.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 AdjustIntervalForTypmod (Interval *interval, int32 typmod)
 
static TimestampTz timestamp2timestamptz (Timestamp timestamp)
 
static Timestamp timestamptz2timestamp (TimestampTz timestamp)
 
static int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
static char * anytimestamp_typmodout (bool istz, int32 typmod)
 
Datum timestamp_in (PG_FUNCTION_ARGS)
 
Datum timestamp_out (PG_FUNCTION_ARGS)
 
Datum timestamp_recv (PG_FUNCTION_ARGS)
 
Datum timestamp_send (PG_FUNCTION_ARGS)
 
Datum timestamptypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptypmodout (PG_FUNCTION_ARGS)
 
Datum timestamp_support (PG_FUNCTION_ARGS)
 
Datum timestamp_scale (PG_FUNCTION_ARGS)
 
bool AdjustTimestampForTypmodError (Timestamp *time, int32 typmod, bool *error)
 
void AdjustTimestampForTypmod (Timestamp *time, int32 typmod)
 
Datum timestamptz_in (PG_FUNCTION_ARGS)
 
static int parse_sane_timezone (struct pg_tm *tm, text *zone)
 
static Timestamp make_timestamp_internal (int year, int month, int day, int hour, int min, double sec)
 
Datum make_timestamp (PG_FUNCTION_ARGS)
 
Datum make_timestamptz (PG_FUNCTION_ARGS)
 
Datum make_timestamptz_at_timezone (PG_FUNCTION_ARGS)
 
Datum float8_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_out (PG_FUNCTION_ARGS)
 
Datum timestamptz_recv (PG_FUNCTION_ARGS)
 
Datum timestamptz_send (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodout (PG_FUNCTION_ARGS)
 
Datum timestamptz_scale (PG_FUNCTION_ARGS)
 
Datum interval_in (PG_FUNCTION_ARGS)
 
Datum interval_out (PG_FUNCTION_ARGS)
 
Datum interval_recv (PG_FUNCTION_ARGS)
 
Datum interval_send (PG_FUNCTION_ARGS)
 
Datum intervaltypmodin (PG_FUNCTION_ARGS)
 
Datum intervaltypmodout (PG_FUNCTION_ARGS)
 
static int intervaltypmodleastfield (int32 typmod)
 
Datum interval_support (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
Datum timeofday (PG_FUNCTION_ARGS)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
long TimestampDifferenceMilliseconds (TimestampTz start_time, TimestampTz stop_time)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const char * timestamptz_to_str (TimestampTz t)
 
void dt2time (Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
int tm2timestamp (struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
 
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)
 
int32 timestamp_cmp_timestamptz_internal (Timestamp timestampVal, TimestampTz dt2)
 
Datum timestamp_eq_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ne_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_lt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_gt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_le_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ge_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_eq_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ne_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_lt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_gt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_le_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ge_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_cmp_timestamp (PG_FUNCTION_ARGS)
 
static INT128 interval_cmp_value (const Interval *interval)
 
static int interval_cmp_internal (Interval *interval1, Interval *interval2)
 
Datum interval_eq (PG_FUNCTION_ARGS)
 
Datum interval_ne (PG_FUNCTION_ARGS)
 
Datum interval_lt (PG_FUNCTION_ARGS)
 
Datum interval_gt (PG_FUNCTION_ARGS)
 
Datum interval_le (PG_FUNCTION_ARGS)
 
Datum interval_ge (PG_FUNCTION_ARGS)
 
Datum interval_cmp (PG_FUNCTION_ARGS)
 
Datum interval_hash (PG_FUNCTION_ARGS)
 
Datum interval_hash_extended (PG_FUNCTION_ARGS)
 
Datum overlaps_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_smaller (PG_FUNCTION_ARGS)
 
Datum timestamp_larger (PG_FUNCTION_ARGS)
 
Datum timestamp_mi (PG_FUNCTION_ARGS)
 
Datum interval_justify_interval (PG_FUNCTION_ARGS)
 
Datum interval_justify_hours (PG_FUNCTION_ARGS)
 
Datum interval_justify_days (PG_FUNCTION_ARGS)
 
Datum timestamp_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_mi_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (PG_FUNCTION_ARGS)
 
Datum interval_um (PG_FUNCTION_ARGS)
 
Datum interval_smaller (PG_FUNCTION_ARGS)
 
Datum interval_larger (PG_FUNCTION_ARGS)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
Datum interval_mi (PG_FUNCTION_ARGS)
 
Datum interval_mul (PG_FUNCTION_ARGS)
 
Datum mul_d_interval (PG_FUNCTION_ARGS)
 
Datum interval_div (PG_FUNCTION_ARGS)
 
Datum in_range_timestamptz_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timestamp_interval (PG_FUNCTION_ARGS)
 
Datum in_range_interval_interval (PG_FUNCTION_ARGS)
 
Datum interval_accum (PG_FUNCTION_ARGS)
 
Datum interval_combine (PG_FUNCTION_ARGS)
 
Datum interval_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (PG_FUNCTION_ARGS)
 
Datum timestamp_age (PG_FUNCTION_ARGS)
 
Datum timestamptz_age (PG_FUNCTION_ARGS)
 
Datum timestamp_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)
 
TimestampTz timestamp2timestamptz_opt_overflow (Timestamp timestamp, int *overflow)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ SAMESIGN

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

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

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

◆ AdjustTimestampForTypmod()

void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)

Definition at line 392 of file timestamp.c.

References AdjustTimestampForTypmodError().

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

393 {
394  (void) AdjustTimestampForTypmodError(time, typmod, NULL);
395 }
bool AdjustTimestampForTypmodError(Timestamp *time, int32 typmod, bool *error)
Definition: timestamp.c:337

◆ AdjustTimestampForTypmodError()

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

Definition at line 337 of file timestamp.c.

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

Referenced by AdjustTimestampForTypmod().

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  {
364  if (error)
365  {
366  *error = true;
367  return false;
368  }
369 
370  ereport(ERROR,
371  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
372  errmsg("timestamp(%d) precision must be between %d and %d",
373  typmod, 0, MAX_TIMESTAMP_PRECISION)));
374  }
375 
376  if (*time >= INT64CONST(0))
377  {
378  *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
379  TimestampScales[typmod];
380  }
381  else
382  {
383  *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
384  * TimestampScales[typmod]);
385  }
386  }
387 
388  return true;
389 }
static void error(void)
Definition: sql-dyntest.c:147
int errcode(int sqlerrcode)
Definition: elog.c:704
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:45
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ 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:704
#define ERROR
Definition: elog.h:45
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ 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:704
signed int int32
Definition: c.h:417
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:102
int errmsg(const char *fmt,...)
Definition: elog.c:915

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

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1555 {
1557 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1578
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40

◆ date2isoweek()

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

Definition at line 4332 of file timestamp.c.

References date2j(), and j2day().

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

4333 {
4334  float8 result;
4335  int day0,
4336  day4,
4337  dayn;
4338 
4339  /* current day */
4340  dayn = date2j(year, mon, mday);
4341 
4342  /* fourth day of current year */
4343  day4 = date2j(year, 1, 4);
4344 
4345  /* day0 == offset to first day of week (Monday) */
4346  day0 = j2day(day4 - 1);
4347 
4348  /*
4349  * We need the first week containing a Thursday, otherwise this day falls
4350  * into the previous year for purposes of counting weeks
4351  */
4352  if (dayn < day4 - day0)
4353  {
4354  day4 = date2j(year - 1, 1, 4);
4355 
4356  /* day0 == offset to first day of week (Monday) */
4357  day0 = j2day(day4 - 1);
4358  }
4359 
4360  result = (dayn - (day4 - day0)) / 7 + 1;
4361 
4362  /*
4363  * Sometimes the last few days in a year will fall into the first week of
4364  * the next year, so check for this.
4365  */
4366  if (result >= 52)
4367  {
4368  day4 = date2j(year + 1, 1, 4);
4369 
4370  /* day0 == offset to first day of week (Monday) */
4371  day0 = j2day(day4 - 1);
4372 
4373  if (dayn >= day4 - day0)
4374  result = (dayn - (day4 - day0)) / 7 + 1;
4375  }
4376 
4377  return (int) result;
4378 }
double float8
Definition: c.h:553
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ date2isoyear()

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

Definition at line 4387 of file timestamp.c.

References date2j(), and j2day().

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

4388 {
4389  float8 result;
4390  int day0,
4391  day4,
4392  dayn;
4393 
4394  /* current day */
4395  dayn = date2j(year, mon, mday);
4396 
4397  /* fourth day of current year */
4398  day4 = date2j(year, 1, 4);
4399 
4400  /* day0 == offset to first day of week (Monday) */
4401  day0 = j2day(day4 - 1);
4402 
4403  /*
4404  * We need the first week containing a Thursday, otherwise this day falls
4405  * into the previous year for purposes of counting weeks
4406  */
4407  if (dayn < day4 - day0)
4408  {
4409  day4 = date2j(year - 1, 1, 4);
4410 
4411  /* day0 == offset to first day of week (Monday) */
4412  day0 = j2day(day4 - 1);
4413 
4414  year--;
4415  }
4416 
4417  result = (dayn - (day4 - day0)) / 7 + 1;
4418 
4419  /*
4420  * Sometimes the last few days in a year will fall into the first week of
4421  * the next year, so check for this.
4422  */
4423  if (result >= 52)
4424  {
4425  day4 = date2j(year + 1, 1, 4);
4426 
4427  /* day0 == offset to first day of week (Monday) */
4428  day0 = j2day(day4 - 1);
4429 
4430  if (dayn >= day4 - day0)
4431  year++;
4432  }
4433 
4434  return year;
4435 }
double float8
Definition: c.h:553
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ date2isoyearday()

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

Definition at line 4444 of file timestamp.c.

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

Referenced by DCH_to_char().

4445 {
4446  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4447 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4387
int isoweek2j(int year, int week)
Definition: timestamp.c:4281
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 2014 of file timestamp.c.

References USECS_PER_SEC.

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

2015 {
2016  dt -= (tz * USECS_PER_SEC);
2017  return dt;
2018 }
#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 1793 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1794 {
1795  TimeOffset time;
1796 
1797  time = jd;
1798 
1799  *hour = time / USECS_PER_HOUR;
1800  time -= (*hour) * USECS_PER_HOUR;
1801  *min = time / USECS_PER_MINUTE;
1802  time -= (*min) * USECS_PER_MINUTE;
1803  *sec = time / USECS_PER_SEC;
1804  *fsec = time - (*sec * USECS_PER_SEC);
1805 } /* 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 1531 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().

1532 {
1533  if (TIMESTAMP_IS_NOBEGIN(dt))
1534  strcpy(str, EARLY);
1535  else if (TIMESTAMP_IS_NOEND(dt))
1536  strcpy(str, LATE);
1537  else /* shouldn't happen */
1538  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1539 }
#define LATE
Definition: datetime.h:41
#define ERROR
Definition: elog.h:45
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define elog(elevel,...)
Definition: elog.h:228
#define EARLY
Definition: datetime.h:40

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 715 of file timestamp.c.

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

716 {
717  float8 seconds = PG_GETARG_FLOAT8(0);
718  TimestampTz result;
719 
720  /* Deal with NaN and infinite inputs ... */
721  if (isnan(seconds))
722  ereport(ERROR,
723  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
724  errmsg("timestamp cannot be NaN")));
725 
726  if (isinf(seconds))
727  {
728  if (seconds < 0)
729  TIMESTAMP_NOBEGIN(result);
730  else
731  TIMESTAMP_NOEND(result);
732  }
733  else
734  {
735  /* Out of range? */
736  if (seconds <
738  || seconds >=
740  ereport(ERROR,
741  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
742  errmsg("timestamp out of range: \"%g\"", seconds)));
743 
744  /* Convert UNIX epoch to Postgres epoch */
746 
747  seconds = rint(seconds * USECS_PER_SEC);
748  result = (int64) seconds;
749 
750  /* Recheck in case roundoff produces something just out of range */
751  if (!IS_VALID_TIMESTAMP(result))
752  ereport(ERROR,
753  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
754  errmsg("timestamp out of range: \"%g\"",
755  PG_GETARG_FLOAT8(0))));
756  }
757 
758  PG_RETURN_TIMESTAMP(result);
759 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#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:704
#define ERROR
Definition: elog.h:45
double float8
Definition: c.h:553
#define SECS_PER_DAY
Definition: timestamp.h:86
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
#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 5418 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.

5419 {
5420  FuncCallContext *funcctx;
5422  Timestamp result;
5423 
5424  /* stuff done only on the first call of the function */
5425  if (SRF_IS_FIRSTCALL())
5426  {
5427  Timestamp start = PG_GETARG_TIMESTAMP(0);
5428  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5429  Interval *step = PG_GETARG_INTERVAL_P(2);
5430  MemoryContext oldcontext;
5431  Interval interval_zero;
5432 
5433  /* create a function context for cross-call persistence */
5434  funcctx = SRF_FIRSTCALL_INIT();
5435 
5436  /*
5437  * switch to memory context appropriate for multiple function calls
5438  */
5439  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5440 
5441  /* allocate memory for user context */
5444 
5445  /*
5446  * Use fctx to keep state from call to call. Seed current with the
5447  * original start value
5448  */
5449  fctx->current = start;
5450  fctx->finish = finish;
5451  fctx->step = *step;
5452 
5453  /* Determine sign of the interval */
5454  MemSet(&interval_zero, 0, sizeof(Interval));
5455  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5456 
5457  if (fctx->step_sign == 0)
5458  ereport(ERROR,
5459  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5460  errmsg("step size cannot equal zero")));
5461 
5462  funcctx->user_fctx = fctx;
5463  MemoryContextSwitchTo(oldcontext);
5464  }
5465 
5466  /* stuff done on every call of the function */
5467  funcctx = SRF_PERCALL_SETUP();
5468 
5469  /*
5470  * get the saved state and use current as the result for this iteration
5471  */
5472  fctx = funcctx->user_fctx;
5473  result = fctx->current;
5474 
5475  if (fctx->step_sign > 0 ?
5476  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5477  timestamp_cmp_internal(result, fctx->finish) >= 0)
5478  {
5479  /* increment current in preparation for next iteration */
5481  TimestampGetDatum(fctx->current),
5482  PointerGetDatum(&fctx->step)));
5483 
5484  /* do when there is more left to send */
5485  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5486  }
5487  else
5488  {
5489  /* do when there is no more left */
5490  SRF_RETURN_DONE(funcctx);
5491  }
5492 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2088
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
#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:704
#define MemSet(start, val, len)
Definition: c.h:996
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2839
#define ERROR
Definition: elog.h:45
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:155
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

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

5499 {
5500  FuncCallContext *funcctx;
5502  TimestampTz result;
5503 
5504  /* stuff done only on the first call of the function */
5505  if (SRF_IS_FIRSTCALL())
5506  {
5508  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5509  Interval *step = PG_GETARG_INTERVAL_P(2);
5510  MemoryContext oldcontext;
5511  Interval interval_zero;
5512 
5513  /* create a function context for cross-call persistence */
5514  funcctx = SRF_FIRSTCALL_INIT();
5515 
5516  /*
5517  * switch to memory context appropriate for multiple function calls
5518  */
5519  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5520 
5521  /* allocate memory for user context */
5524 
5525  /*
5526  * Use fctx to keep state from call to call. Seed current with the
5527  * original start value
5528  */
5529  fctx->current = start;
5530  fctx->finish = finish;
5531  fctx->step = *step;
5532 
5533  /* Determine sign of the interval */
5534  MemSet(&interval_zero, 0, sizeof(Interval));
5535  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5536 
5537  if (fctx->step_sign == 0)
5538  ereport(ERROR,
5539  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5540  errmsg("step size cannot equal zero")));
5541 
5542  funcctx->user_fctx = fctx;
5543  MemoryContextSwitchTo(oldcontext);
5544  }
5545 
5546  /* stuff done on every call of the function */
5547  funcctx = SRF_PERCALL_SETUP();
5548 
5549  /*
5550  * get the saved state and use current as the result for this iteration
5551  */
5552  fctx = funcctx->user_fctx;
5553  result = fctx->current;
5554 
5555  if (fctx->step_sign > 0 ?
5556  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5557  timestamp_cmp_internal(result, fctx->finish) >= 0)
5558  {
5559  /* increment current in preparation for next iteration */
5562  PointerGetDatum(&fctx->step)));
5563 
5564  /* do when there is more left to send */
5565  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5566  }
5567  else
5568  {
5569  /* do when there is no more left */
5570  SRF_RETURN_DONE(funcctx);
5571  }
5572 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2088
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
#define PointerGetDatum(X)
Definition: postgres.h:556
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:704
#define MemSet(start, val, len)
Definition: c.h:996
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
#define ERROR
Definition: elog.h:45
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define ereport(elevel,...)
Definition: elog.h:155
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2944
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1578 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(), CheckPointGuts(), 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(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), heap_vacuum_rel(), InitProcessGlobals(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), perform_base_backup(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), pgss_shmem_startup(), pgstat_read_statsfiles(), pgstat_recv_inquiry(), pgstat_recv_resetreplslotcounter(), pgstat_recv_resetsharedcounter(), pgstat_recv_resetsinglecounter(), pgstat_recv_resetslrucounter(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_vacuum(), pgstat_send_archiver(), pgstat_send_connstats(), 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().

1579 {
1580  TimestampTz result;
1581  struct timeval tp;
1582 
1583  gettimeofday(&tp, NULL);
1584 
1585  result = (TimestampTz) tp.tv_sec -
1587  result = (result * USECS_PER_SEC) + tp.tv_usec;
1588 
1589  return result;
1590 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104
#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 2046 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().

2047 {
2048  struct pg_tm *t0;
2049  pg_time_t epoch = 0;
2050 
2051  t0 = pg_gmtime(&epoch);
2052 
2053  if (t0 == NULL)
2054  elog(ERROR, "could not convert epoch to timestamp: %m");
2055 
2056  tm->tm_year = t0->tm_year;
2057  tm->tm_mon = t0->tm_mon;
2058  tm->tm_mday = t0->tm_mday;
2059  tm->tm_hour = t0->tm_hour;
2060  tm->tm_min = t0->tm_min;
2061  tm->tm_sec = t0->tm_sec;
2062 
2063  tm->tm_year += 1900;
2064  tm->tm_mon++;
2065 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1387
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
#define ERROR
Definition: elog.h:45
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:228
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 1596 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1597 {
1598  TimestampTz ts;
1599 
1601  if (typmod >= 0)
1602  AdjustTimestampForTypmod(&ts, typmod);
1603  return ts;
1604 }
int64 TimestampTz
Definition: timestamp.h:39
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:392
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1610 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1611 {
1612  Timestamp ts;
1613 
1615  if (typmod >= 0)
1616  AdjustTimestampForTypmod(&ts, typmod);
1617  return ts;
1618 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:392
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5269
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

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

3379 {
3381  Interval *base = PG_GETARG_INTERVAL_P(1);
3382  Interval *offset = PG_GETARG_INTERVAL_P(2);
3383  bool sub = PG_GETARG_BOOL(3);
3384  bool less = PG_GETARG_BOOL(4);
3385  Interval *sum;
3386 
3387  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3388  ereport(ERROR,
3389  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3390  errmsg("invalid preceding or following size in window function")));
3391 
3392  /* We don't currently bother to avoid overflow hazards here */
3393  if (sub)
3395  IntervalPGetDatum(base),
3396  IntervalPGetDatum(offset)));
3397  else
3399  IntervalPGetDatum(base),
3400  IntervalPGetDatum(offset)));
3401 
3402  if (less)
3403  PG_RETURN_BOOL(interval_cmp_internal(val, sum) <= 0);
3404  else
3405  PG_RETURN_BOOL(interval_cmp_internal(val, sum) >= 0);
3406 }
#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:704
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3137
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3103
#define ERROR
Definition: elog.h:45
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
#define ereport(elevel,...)
Definition: elog.h:155
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375
int errmsg(const char *fmt,...)
Definition: elog.c:915
long val
Definition: informix.c:664
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2350

◆ in_range_timestamp_interval()

Datum in_range_timestamp_interval ( PG_FUNCTION_ARGS  )

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

3348 {
3350  Timestamp base = PG_GETARG_TIMESTAMP(1);
3351  Interval *offset = PG_GETARG_INTERVAL_P(2);
3352  bool sub = PG_GETARG_BOOL(3);
3353  bool less = PG_GETARG_BOOL(4);
3354  Timestamp sum;
3355 
3356  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3357  ereport(ERROR,
3358  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3359  errmsg("invalid preceding or following size in window function")));
3360 
3361  /* We don't currently bother to avoid overflow hazards here */
3362  if (sub)
3364  TimestampGetDatum(base),
3365  IntervalPGetDatum(offset)));
3366  else
3368  TimestampGetDatum(base),
3369  IntervalPGetDatum(offset)));
3370 
3371  if (less)
3372  PG_RETURN_BOOL(val <= sum);
3373  else
3374  PG_RETURN_BOOL(val >= sum);
3375 }
#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:704
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2918
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2839
#define ERROR
Definition: elog.h:45
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
long val
Definition: informix.c:664
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2350
#define DatumGetTimestamp(X)
Definition: timestamp.h:27

◆ in_range_timestamptz_interval()

Datum in_range_timestamptz_interval ( PG_FUNCTION_ARGS  )

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

3317 {
3320  Interval *offset = PG_GETARG_INTERVAL_P(2);
3321  bool sub = PG_GETARG_BOOL(3);
3322  bool less = PG_GETARG_BOOL(4);
3323  TimestampTz sum;
3324 
3325  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3326  ereport(ERROR,
3327  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3328  errmsg("invalid preceding or following size in window function")));
3329 
3330  /* We don't currently bother to avoid overflow hazards here */
3331  if (sub)
3333  TimestampTzGetDatum(base),
3334  IntervalPGetDatum(offset)));
3335  else
3337  TimestampTzGetDatum(base),
3338  IntervalPGetDatum(offset)));
3339 
3340  if (less)
3341  PG_RETURN_BOOL(val <= sum);
3342  else
3343  PG_RETURN_BOOL(val >= sum);
3344 }
#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:704
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define ERROR
Definition: elog.h:45
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
#define ereport(elevel,...)
Definition: elog.h:155
Datum timestamptz_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3028
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2944
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
long val
Definition: informix.c:664
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2350

◆ interval2tm()

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

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

1965 {
1966  TimeOffset time;
1967  TimeOffset tfrac;
1968 
1969  tm->tm_year = span.month / MONTHS_PER_YEAR;
1970  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1971  tm->tm_mday = span.day;
1972  time = span.time;
1973 
1974  tfrac = time / USECS_PER_HOUR;
1975  time -= tfrac * USECS_PER_HOUR;
1976  tm->tm_hour = tfrac;
1977  if (!SAMESIGN(tm->tm_hour, tfrac))
1978  ereport(ERROR,
1979  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1980  errmsg("interval out of range")));
1981  tfrac = time / USECS_PER_MINUTE;
1982  time -= tfrac * USECS_PER_MINUTE;
1983  tm->tm_min = tfrac;
1984  tfrac = time / USECS_PER_SEC;
1985  *fsec = time - (tfrac * USECS_PER_SEC);
1986  tm->tm_sec = tfrac;
1987 
1988  return 0;
1989 }
#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:704
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define ERROR
Definition: elog.h:45
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 SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define ereport(elevel,...)
Definition: elog.h:155
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:915
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 3420 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.

3421 {
3422  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3424  Datum *transdatums;
3425  int ndatums;
3426  Interval sumX,
3427  N;
3428  Interval *newsum;
3429  ArrayType *result;
3430 
3431  deconstruct_array(transarray,
3432  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3433  &transdatums, NULL, &ndatums);
3434  if (ndatums != 2)
3435  elog(ERROR, "expected 2-element interval array");
3436 
3437  sumX = *(DatumGetIntervalP(transdatums[0]));
3438  N = *(DatumGetIntervalP(transdatums[1]));
3439 
3441  IntervalPGetDatum(&sumX),
3442  IntervalPGetDatum(newval)));
3443  N.time += 1;
3444 
3445  transdatums[0] = IntervalPGetDatum(newsum);
3446  transdatums[1] = IntervalPGetDatum(&N);
3447 
3448  result = construct_array(transdatums, 2,
3449  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3450 
3451  PG_RETURN_ARRAYTYPE_P(result);
3452 }
#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:3318
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3103
#define ERROR
Definition: elog.h:45
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
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:3488
#define elog(elevel,...)
Definition: elog.h:228
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ interval_accum_inv()

Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

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

3505 {
3506  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3508  Datum *transdatums;
3509  int ndatums;
3510  Interval sumX,
3511  N;
3512  Interval *newsum;
3513  ArrayType *result;
3514 
3515  deconstruct_array(transarray,
3516  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3517  &transdatums, NULL, &ndatums);
3518  if (ndatums != 2)
3519  elog(ERROR, "expected 2-element interval array");
3520 
3521  sumX = *(DatumGetIntervalP(transdatums[0]));
3522  N = *(DatumGetIntervalP(transdatums[1]));
3523 
3525  IntervalPGetDatum(&sumX),
3526  IntervalPGetDatum(newval)));
3527  N.time -= 1;
3528 
3529  transdatums[0] = IntervalPGetDatum(newsum);
3530  transdatums[1] = IntervalPGetDatum(&N);
3531 
3532  result = construct_array(transdatums, 2,
3533  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3534 
3535  PG_RETURN_ARRAYTYPE_P(result);
3536 }
#define PG_GETARG_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:3318
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3137
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ERROR
Definition: elog.h:45
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
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:3488
#define elog(elevel,...)
Definition: elog.h:228
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

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

3540 {
3541  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3542  Datum *transdatums;
3543  int ndatums;
3544  Interval sumX,
3545  N;
3546 
3547  deconstruct_array(transarray,
3548  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3549  &transdatums, NULL, &ndatums);
3550  if (ndatums != 2)
3551  elog(ERROR, "expected 2-element interval array");
3552 
3553  sumX = *(DatumGetIntervalP(transdatums[0]));
3554  N = *(DatumGetIntervalP(transdatums[1]));
3555 
3556  /* SQL defines AVG of no values to be NULL */
3557  if (N.time == 0)
3558  PG_RETURN_NULL();
3559 
3561  IntervalPGetDatum(&sumX),
3562  Float8GetDatum((double) N.time));
3563 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1709
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ERROR
Definition: elog.h:45
#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:3264
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
#define elog(elevel,...)
Definition: elog.h:228
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2438 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp(), and leftmostvalue_interval().

2439 {
2440  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2441  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2442 
2443  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2444 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375

◆ interval_cmp_internal()

static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

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

2376 {
2377  INT128 span1 = interval_cmp_value(interval1);
2378  INT128 span2 = interval_cmp_value(interval2);
2379 
2380  return int128_compare(span1, span2);
2381 }
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:2350

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

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

2351 {
2352  INT128 span;
2353  int64 dayfraction;
2354  int64 days;
2355 
2356  /*
2357  * Separate time field into days and dayfraction, then add the month and
2358  * day fields to the days part. We cannot overflow int64 days here.
2359  */
2360  dayfraction = interval->time % USECS_PER_DAY;
2361  days = interval->time / USECS_PER_DAY;
2362  days += interval->month * INT64CONST(30);
2363  days += interval->day;
2364 
2365  /* Widen dayfraction to 128 bits */
2366  span = int64_to_int128(dayfraction);
2367 
2368  /* Scale up days to microseconds, forming a 128-bit product */
2370 
2371  return span;
2372 }
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:68
#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 3455 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.

3456 {
3457  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3458  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3459  Datum *transdatums1;
3460  Datum *transdatums2;
3461  int ndatums1;
3462  int ndatums2;
3463  Interval sum1,
3464  N1;
3465  Interval sum2,
3466  N2;
3467 
3468  Interval *newsum;
3469  ArrayType *result;
3470 
3471  deconstruct_array(transarray1,
3472  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3473  &transdatums1, NULL, &ndatums1);
3474  if (ndatums1 != 2)
3475  elog(ERROR, "expected 2-element interval array");
3476 
3477  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3478  N1 = *(DatumGetIntervalP(transdatums1[1]));
3479 
3480  deconstruct_array(transarray2,
3481  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3482  &transdatums2, NULL, &ndatums2);
3483  if (ndatums2 != 2)
3484  elog(ERROR, "expected 2-element interval array");
3485 
3486  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3487  N2 = *(DatumGetIntervalP(transdatums2[1]));
3488 
3490  IntervalPGetDatum(&sum1),
3491  IntervalPGetDatum(&sum2)));
3492  N1.time += N2.time;
3493 
3494  transdatums1[0] = IntervalPGetDatum(newsum);
3495  transdatums1[1] = IntervalPGetDatum(&N1);
3496 
3497  result = construct_array(transdatums1, 2,
3498  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3499 
3500  PG_RETURN_ARRAYTYPE_P(result);
3501 }
#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:3318
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3103
#define ERROR
Definition: elog.h:45
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:258
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:3488
#define elog(elevel,...)
Definition: elog.h:228
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3264 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, SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_avg().

3265 {
3266  Interval *span = PG_GETARG_INTERVAL_P(0);
3267  float8 factor = PG_GETARG_FLOAT8(1);
3268  double month_remainder_days,
3269  sec_remainder;
3270  int32 orig_month = span->month,
3271  orig_day = span->day;
3272  Interval *result;
3273 
3274  result = (Interval *) palloc(sizeof(Interval));
3275 
3276  if (factor == 0.0)
3277  ereport(ERROR,
3278  (errcode(ERRCODE_DIVISION_BY_ZERO),
3279  errmsg("division by zero")));
3280 
3281  result->month = (int32) (span->month / factor);
3282  result->day = (int32) (span->day / factor);
3283 
3284  /*
3285  * Fractional months full days into days. See comment in interval_mul().
3286  */
3287  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3288  month_remainder_days = TSROUND(month_remainder_days);
3289  sec_remainder = (orig_day / factor - result->day +
3290  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3291  sec_remainder = TSROUND(sec_remainder);
3292  if (Abs(sec_remainder) >= SECS_PER_DAY)
3293  {
3294  result->day += (int) (sec_remainder / SECS_PER_DAY);
3295  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3296  }
3297 
3298  /* cascade units down */
3299  result->day += (int32) month_remainder_days;
3300  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3301 
3302  PG_RETURN_INTERVAL_P(result);
3303 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#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:704
signed int int32
Definition: c.h:417
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:980
#define ERROR
Definition: elog.h:45
double float8
Definition: c.h:553
#define SECS_PER_DAY
Definition: timestamp.h:86
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define ereport(elevel,...)
Definition: elog.h:155
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ interval_eq()

Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2384 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

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

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 2035 of file timestamp.c.

References PG_RETURN_BOOL.

2036 {
2037  PG_RETURN_BOOL(true);
2038 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2429 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

2430 {
2431  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2432  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2433 
2434  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2435 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375

◆ interval_gt()

Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2411 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2412 {
2413  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2414  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2415 
2416  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2417 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2454 of file timestamp.c.

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

2455 {
2457  INT128 span = interval_cmp_value(interval);
2458  int64 span64;
2459 
2460  /*
2461  * Use only the least significant 64 bits for hashing. The upper 64 bits
2462  * seldom add any useful information, and besides we must do it like this
2463  * for compatibility with hashes calculated before use of INT128 was
2464  * introduced.
2465  */
2466  span64 = int128_to_int64(span);
2467 
2469 }
#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:626
#define Int64GetDatumFast(X)
Definition: postgres.h:760
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:2350

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2472 of file timestamp.c.

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

2473 {
2475  INT128 span = interval_cmp_value(interval);
2476  int64 span64;
2477 
2478  /* Same approach as interval_hash */
2479  span64 = int128_to_int64(span);
2480 
2482  PG_GETARG_DATUM(1));
2483 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
#define Int64GetDatumFast(X)
Definition: postgres.h:760
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2350

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

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

881 {
882  char *str = PG_GETARG_CSTRING(0);
883 
884 #ifdef NOT_USED
885  Oid typelem = PG_GETARG_OID(1);
886 #endif
887  int32 typmod = PG_GETARG_INT32(2);
888  Interval *result;
889  fsec_t fsec;
890  struct pg_tm tt,
891  *tm = &tt;
892  int dtype;
893  int nf;
894  int range;
895  int dterr;
896  char *field[MAXDATEFIELDS];
897  int ftype[MAXDATEFIELDS];
898  char workbuf[256];
899 
900  tm->tm_year = 0;
901  tm->tm_mon = 0;
902  tm->tm_mday = 0;
903  tm->tm_hour = 0;
904  tm->tm_min = 0;
905  tm->tm_sec = 0;
906  fsec = 0;
907 
908  if (typmod >= 0)
909  range = INTERVAL_RANGE(typmod);
910  else
911  range = INTERVAL_FULL_RANGE;
912 
913  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
914  ftype, MAXDATEFIELDS, &nf);
915  if (dterr == 0)
916  dterr = DecodeInterval(field, ftype, nf, range,
917  &dtype, tm, &fsec);
918 
919  /* if those functions think it's a bad format, try ISO8601 style */
920  if (dterr == DTERR_BAD_FORMAT)
921  dterr = DecodeISO8601Interval(str,
922  &dtype, tm, &fsec);
923 
924  if (dterr != 0)
925  {
926  if (dterr == DTERR_FIELD_OVERFLOW)
927  dterr = DTERR_INTERVAL_OVERFLOW;
928  DateTimeParseError(dterr, str, "interval");
929  }
930 
931  result = (Interval *) palloc(sizeof(Interval));
932 
933  switch (dtype)
934  {
935  case DTK_DELTA:
936  if (tm2interval(tm, fsec, result) != 0)
937  ereport(ERROR,
938  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
939  errmsg("interval out of range")));
940  break;
941 
942  default:
943  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
944  dtype, str);
945  }
946 
947  AdjustIntervalForTypmod(result, typmod);
948 
949  PG_RETURN_INTERVAL_P(result);
950 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3768
#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:704
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:417
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:45
#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:1328
int tm_mday
Definition: pgtime.h:30
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
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
int DecodeISO8601Interval(char *str, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3522
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define ereport(elevel,...)
Definition: elog.h:155
#define MAXDATEFIELDS
Definition: datetime.h:203
int tm_year
Definition: pgtime.h:32
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3096
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
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:582
int tm_min
Definition: pgtime.h:28
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1992

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2799 of file timestamp.c.

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

2800 {
2801  Interval *span = PG_GETARG_INTERVAL_P(0);
2802  Interval *result;
2803  int32 wholemonth;
2804 
2805  result = (Interval *) palloc(sizeof(Interval));
2806  result->month = span->month;
2807  result->day = span->day;
2808  result->time = span->time;
2809 
2810  wholemonth = result->day / DAYS_PER_MONTH;
2811  result->day -= wholemonth * DAYS_PER_MONTH;
2812  result->month += wholemonth;
2813 
2814  if (result->month > 0 && result->day < 0)
2815  {
2816  result->day += DAYS_PER_MONTH;
2817  result->month--;
2818  }
2819  else if (result->month < 0 && result->day > 0)
2820  {
2821  result->day -= DAYS_PER_MONTH;
2822  result->month++;
2823  }
2824 
2825  PG_RETURN_INTERVAL_P(result);
2826 }
#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:417
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:950

◆ interval_justify_hours()

Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

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

2765 {
2766  Interval *span = PG_GETARG_INTERVAL_P(0);
2767  Interval *result;
2768  TimeOffset wholeday;
2769 
2770  result = (Interval *) palloc(sizeof(Interval));
2771  result->month = span->month;
2772  result->day = span->day;
2773  result->time = span->time;
2774 
2775  TMODULO(result->time, wholeday, USECS_PER_DAY);
2776  result->day += wholeday; /* could overflow... */
2777 
2778  if (result->day > 0 && result->time < 0)
2779  {
2780  result->time += USECS_PER_DAY;
2781  result->day--;
2782  }
2783  else if (result->day < 0 && result->time > 0)
2784  {
2785  result->time -= USECS_PER_DAY;
2786  result->day++;
2787  }
2788 
2789  PG_RETURN_INTERVAL_P(result);
2790 }
#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:950
#define TMODULO(t, q, u)
Definition: datetime.h:249

◆ interval_justify_interval()

Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

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

2710 {
2711  Interval *span = PG_GETARG_INTERVAL_P(0);
2712  Interval *result;
2713  TimeOffset wholeday;
2714  int32 wholemonth;
2715 
2716  result = (Interval *) palloc(sizeof(Interval));
2717  result->month = span->month;
2718  result->day = span->day;
2719  result->time = span->time;
2720 
2721  TMODULO(result->time, wholeday, USECS_PER_DAY);
2722  result->day += wholeday; /* could overflow... */
2723 
2724  wholemonth = result->day / DAYS_PER_MONTH;
2725  result->day -= wholemonth * DAYS_PER_MONTH;
2726  result->month += wholemonth;
2727 
2728  if (result->month > 0 &&
2729  (result->day < 0 || (result->day == 0 && result->time < 0)))
2730  {
2731  result->day += DAYS_PER_MONTH;
2732  result->month--;
2733  }
2734  else if (result->month < 0 &&
2735  (result->day > 0 || (result->day == 0 && result->time > 0)))
2736  {
2737  result->day -= DAYS_PER_MONTH;
2738  result->month++;
2739  }
2740 
2741  if (result->day > 0 && result->time < 0)
2742  {
2743  result->time += USECS_PER_DAY;
2744  result->day--;
2745  }
2746  else if (result->day < 0 && result->time > 0)
2747  {
2748  result->time -= USECS_PER_DAY;
2749  result->day++;
2750  }
2751 
2752  PG_RETURN_INTERVAL_P(result);
2753 }
#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:417
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:950
#define TMODULO(t, q, u)
Definition: datetime.h:249

◆ interval_larger()

Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3089 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3090 {
3091  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3092  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3093  Interval *result;
3094 
3095  if (interval_cmp_internal(interval1, interval2) > 0)
3096  result = interval1;
3097  else
3098  result = interval2;
3099  PG_RETURN_INTERVAL_P(result);
3100 }
#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:2375

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2420 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

2421 {
2422  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2423  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2424 
2425  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2426 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375

◆ interval_lt()

Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2402 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2403 {
2404  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2405  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2406 
2407  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2408 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

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

3138 {
3139  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3140  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3141  Interval *result;
3142 
3143  result = (Interval *) palloc(sizeof(Interval));
3144 
3145  result->month = span1->month - span2->month;
3146  /* overflow check copied from int4mi */
3147  if (!SAMESIGN(span1->month, span2->month) &&
3148  !SAMESIGN(result->month, span1->month))
3149  ereport(ERROR,
3150  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3151  errmsg("interval out of range")));
3152 
3153  result->day = span1->day - span2->day;
3154  if (!SAMESIGN(span1->day, span2->day) &&
3155  !SAMESIGN(result->day, span1->day))
3156  ereport(ERROR,
3157  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3158  errmsg("interval out of range")));
3159 
3160  result->time = span1->time - span2->time;
3161  if (!SAMESIGN(span1->time, span2->time) &&
3162  !SAMESIGN(result->time, span1->time))
3163  ereport(ERROR,
3164  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3165  errmsg("interval out of range")));
3166 
3167  PG_RETURN_INTERVAL_P(result);
3168 }
#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:704
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:45
TimeOffset time
Definition: timestamp.h:45
#define SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:155
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3177 of file timestamp.c.

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

Referenced by interval_lerp(), and mul_d_interval().

3178 {
3179  Interval *span = PG_GETARG_INTERVAL_P(0);
3180  float8 factor = PG_GETARG_FLOAT8(1);
3181  double month_remainder_days,
3182  sec_remainder,
3183  result_double;
3184  int32 orig_month = span->month,
3185  orig_day = span->day;
3186  Interval *result;
3187 
3188  result = (Interval *) palloc(sizeof(Interval));
3189 
3190  result_double = span->month * factor;
3191  if (isnan(result_double) ||
3192  result_double > INT_MAX || result_double < INT_MIN)
3193  ereport(ERROR,
3194  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3195  errmsg("interval out of range")));
3196  result->month = (int32) result_double;
3197 
3198  result_double = span->day * factor;
3199  if (isnan(result_double) ||
3200  result_double > INT_MAX || result_double < INT_MIN)
3201  ereport(ERROR,
3202  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3203  errmsg("interval out of range")));
3204  result->day = (int32) result_double;
3205 
3206  /*
3207  * The above correctly handles the whole-number part of the month and day
3208  * products, but we have to do something with any fractional part
3209  * resulting when the factor is non-integral. We cascade the fractions
3210  * down to lower units using the conversion factors DAYS_PER_MONTH and
3211  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3212  * so by the representation. The user can choose to cascade up later,
3213  * using justify_hours and/or justify_days.
3214  */
3215 
3216  /*
3217  * Fractional months full days into days.
3218  *
3219  * Floating point calculation are inherently imprecise, so these
3220  * calculations are crafted to produce the most reliable result possible.
3221  * TSROUND() is needed to more accurately produce whole numbers where
3222  * appropriate.
3223  */
3224  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3225  month_remainder_days = TSROUND(month_remainder_days);
3226  sec_remainder = (orig_day * factor - result->day +
3227  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3228  sec_remainder = TSROUND(sec_remainder);
3229 
3230  /*
3231  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3232  * cascade from months and days. It might still be >24 if the combination
3233  * of cascade and the seconds factor operation itself.
3234  */
3235  if (Abs(sec_remainder) >= SECS_PER_DAY)
3236  {
3237  result->day += (int) (sec_remainder / SECS_PER_DAY);
3238  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3239  }
3240 
3241  /* cascade units down */
3242  result->day += (int32) month_remainder_days;
3243  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3244  if (isnan(result_double) || !FLOAT8_FITS_IN_INT64(result_double))
3245  ereport(ERROR,
3246  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3247  errmsg("interval out of range")));
3248  result->time = (int64) result_double;
3249 
3250  PG_RETURN_INTERVAL_P(result);
3251 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#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:704
signed int int32
Definition: c.h:417
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:980
#define ERROR
Definition: elog.h:45
double float8
Definition: c.h:553
#define SECS_PER_DAY
Definition: timestamp.h:86
#define FLOAT8_FITS_IN_INT64(num)
Definition: c.h:1095
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define ereport(elevel,...)
Definition: elog.h:155
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ interval_ne()

Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2393 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2394 {
2395  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2396  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2397 
2398  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2399 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2375

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

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

957 {
958  Interval *span = PG_GETARG_INTERVAL_P(0);
959  char *result;
960  struct pg_tm tt,
961  *tm = &tt;
962  fsec_t fsec;
963  char buf[MAXDATELEN + 1];
964 
965  if (interval2tm(*span, tm, &fsec) != 0)
966  elog(ERROR, "could not convert interval to tm");
967 
968  EncodeInterval(tm, fsec, IntervalStyle, buf);
969 
970  result = pstrdup(buf);
971  PG_RETURN_CSTRING(result);
972 }
#define MAXDATELEN
Definition: datetime.h:201
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
char * pstrdup(const char *in)
Definition: mcxt.c:1187
int IntervalStyle
Definition: globals.c:118
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:45
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1964
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:4245
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define elog(elevel,...)
Definition: elog.h:228

◆ interval_part()

Datum interval_part ( PG_FUNCTION_ARGS  )

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

4924 {
4925  text *units = PG_GETARG_TEXT_PP(0);
4927  float8 result;
4928  int type,
4929  val;
4930  char *lowunits;
4931  fsec_t fsec;
4932  struct pg_tm tt,
4933  *tm = &tt;
4934 
4935  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4936  VARSIZE_ANY_EXHDR(units),
4937  false);
4938 
4939  type = DecodeUnits(0, lowunits, &val);
4940  if (type == UNKNOWN_FIELD)
4941  type = DecodeSpecial(0, lowunits, &val);
4942 
4943  if (type == UNITS)
4944  {
4945  if (interval2tm(*interval, tm, &fsec) == 0)
4946  {
4947  switch (val)
4948  {
4949  case DTK_MICROSEC:
4950  result = tm->tm_sec * 1000000.0 + fsec;
4951  break;
4952 
4953  case DTK_MILLISEC:
4954  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4955  break;
4956 
4957  case DTK_SECOND:
4958  result = tm->tm_sec + fsec / 1000000.0;
4959  break;
4960 
4961  case DTK_MINUTE:
4962  result = tm->tm_min;
4963  break;
4964 
4965  case DTK_HOUR:
4966  result = tm->tm_hour;
4967  break;
4968 
4969  case DTK_DAY:
4970  result = tm->tm_mday;
4971  break;
4972 
4973  case DTK_MONTH:
4974  result = tm->tm_mon;
4975  break;
4976 
4977  case DTK_QUARTER:
4978  result = (tm->tm_mon / 3) + 1;
4979  break;
4980 
4981  case DTK_YEAR:
4982  result = tm->tm_year;
4983  break;
4984 
4985  case DTK_DECADE:
4986  /* caution: C division may have negative remainder */
4987  result = tm->tm_year / 10;
4988  break;
4989 
4990  case DTK_CENTURY:
4991  /* caution: C division may have negative remainder */
4992  result = tm->tm_year / 100;
4993  break;
4994 
4995  case DTK_MILLENNIUM:
4996  /* caution: C division may have negative remainder */
4997  result = tm->tm_year / 1000;
4998  break;
4999 
5000  default:
5001  ereport(ERROR,
5002  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5003  errmsg("interval units \"%s\" not supported",
5004  lowunits)));
5005  result = 0;
5006  }
5007 
5008  }
5009  else
5010  {
5011  elog(ERROR, "could not convert interval to tm");
5012  result = 0;
5013  }
5014  }
5015  else if (type == RESERV && val == DTK_EPOCH)
5016  {
5017  result = interval->time / 1000000.0;
5018  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
5019  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
5020  result += ((double) SECS_PER_DAY) * interval->day;
5021  }
5022  else
5023  {
5024  ereport(ERROR,
5025  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5026  errmsg("interval units \"%s\" not recognized",
5027  lowunits)));
5028  result = 0;
5029  }
5030 
5031  PG_RETURN_FLOAT8(result);
5032 }
#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:37
#define DTK_QUARTER
Definition: datetime.h:167
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:704
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3731
Definition: pgtime.h:25
int32 day
Definition: timestamp.h:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static struct pg_tm tm
Definition: localtime.c:102
#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:45
double float8
Definition: c.h:553
#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:1964
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 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 ereport(elevel,...)
Definition: elog.h:155
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3040
#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:915
#define elog(elevel,...)
Definition: elog.h:228
Definition: c.h:609
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:664

◆ interval_pl()

Datum interval_pl ( PG_FUNCTION_ARGS  )

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

3104 {
3105  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3106  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3107  Interval *result;
3108 
3109  result = (Interval *) palloc(sizeof(Interval));
3110 
3111  result->month = span1->month + span2->month;
3112  /* overflow check copied from int4pl */
3113  if (SAMESIGN(span1->month, span2->month) &&
3114  !SAMESIGN(result->month, span1->month))
3115  ereport(ERROR,
3116  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3117  errmsg("interval out of range")));
3118 
3119  result->day = span1->day + span2->day;
3120  if (SAMESIGN(span1->day, span2->day) &&
3121  !SAMESIGN(result->day, span1->day))
3122  ereport(ERROR,
3123  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3124  errmsg("interval out of range")));
3125 
3126  result->time = span1->time + span2->time;
3127  if (SAMESIGN(span1->time, span2->time) &&
3128  !SAMESIGN(result->time, span1->time))
3129  ereport(ERROR,
3130  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3131  errmsg("interval out of range")));
3132 
3133  PG_RETURN_INTERVAL_P(result);
3134 }
#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:704
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:45
TimeOffset time
Definition: timestamp.h:45
#define SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:155
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

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

979 {
981 
982 #ifdef NOT_USED
983  Oid typelem = PG_GETARG_OID(1);
984 #endif
985  int32 typmod = PG_GETARG_INT32(2);
987 
988  interval = (Interval *) palloc(sizeof(Interval));
989 
990  interval->time = pq_getmsgint64(buf);
991  interval->day = pq_getmsgint(buf, sizeof(interval->day));
992  interval->month = pq_getmsgint(buf, sizeof(interval->month));
993 
994  AdjustIntervalForTypmod(interval, typmod);
995 
996  PG_RETURN_INTERVAL_P(interval);
997 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:417
int32 day
Definition: timestamp.h:47
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1328
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:950
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 1309 of file timestamp.c.

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

1310 {
1312  int32 typmod = PG_GETARG_INT32(1);
1313  Interval *result;
1314 
1315  result = palloc(sizeof(Interval));
1316  *result = *interval;
1317 
1318  AdjustIntervalForTypmod(result, typmod);
1319 
1320  PG_RETURN_INTERVAL_P(result);
1321 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#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:417
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1328
void * palloc(Size size)
Definition: mcxt.c:950

◆ interval_send()

Datum interval_send ( PG_FUNCTION_ARGS  )

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

1004 {
1007 
1008  pq_begintypsend(&buf);
1009  pq_sendint64(&buf, interval->time);
1010  pq_sendint32(&buf, interval->day);
1011  pq_sendint32(&buf, interval->month);
1013 }
#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:371
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 3074 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3075 {
3076  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3077  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3078  Interval *result;
3079 
3080  /* use interval_cmp_internal to be sure this agrees with comparisons */
3081  if (interval_cmp_internal(interval1, interval2) < 0)
3082  result = interval1;
3083  else
3084  result = interval2;
3085  PG_RETURN_INTERVAL_P(result);
3086 }
#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:2375

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1246 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, relabel_to_typmod(), and source.

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

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

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

4178 {
4179  text *units = PG_GETARG_TEXT_PP(0);
4181  Interval *result;
4182  int type,
4183  val;
4184  char *lowunits;
4185  fsec_t fsec;
4186  struct pg_tm tt,
4187  *tm = &tt;
4188 
4189  result = (Interval *) palloc(sizeof(Interval));
4190 
4191  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4192  VARSIZE_ANY_EXHDR(units),
4193  false);
4194 
4195  type = DecodeUnits(0, lowunits, &val);
4196 
4197  if (type == UNITS)
4198  {
4199  if (interval2tm(*interval, tm, &fsec) == 0)
4200  {
4201  switch (val)
4202  {
4203  case DTK_MILLENNIUM:
4204  /* caution: C division may have negative remainder */
4205  tm->tm_year = (tm->tm_year / 1000) * 1000;
4206  /* FALL THRU */
4207  case DTK_CENTURY:
4208  /* caution: C division may have negative remainder */
4209  tm->tm_year = (tm->tm_year / 100) * 100;
4210  /* FALL THRU */
4211  case DTK_DECADE:
4212  /* caution: C division may have negative remainder */
4213  tm->tm_year = (tm->tm_year / 10) * 10;
4214  /* FALL THRU */
4215  case DTK_YEAR:
4216  tm->tm_mon = 0;
4217  /* FALL THRU */
4218  case DTK_QUARTER:
4219  tm->tm_mon = 3 * (tm->tm_mon / 3);
4220  /* FALL THRU */
4221  case DTK_MONTH:
4222  tm->tm_mday = 0;
4223  /* FALL THRU */
4224  case DTK_DAY:
4225  tm->tm_hour = 0;
4226  /* FALL THRU */
4227  case DTK_HOUR:
4228  tm->tm_min = 0;
4229  /* FALL THRU */
4230  case DTK_MINUTE:
4231  tm->tm_sec = 0;
4232  /* FALL THRU */
4233  case DTK_SECOND:
4234  fsec = 0;
4235  break;
4236  case DTK_MILLISEC:
4237  fsec = (fsec / 1000) * 1000;
4238  break;
4239  case DTK_MICROSEC:
4240  break;
4241 
4242  default:
4243  if (val == DTK_WEEK)
4244  ereport(ERROR,
4245  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4246  errmsg("interval units \"%s\" not supported "
4247  "because months usually have fractional weeks",
4248  lowunits)));
4249  else
4250  ereport(ERROR,
4251  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4252  errmsg("interval units \"%s\" not supported",
4253  lowunits)));
4254  }
4255 
4256  if (tm2interval(tm, fsec, result) != 0)
4257  ereport(ERROR,
4258  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4259  errmsg("interval out of range")));
4260  }
4261  else
4262  elog(ERROR, "could not convert interval to tm");
4263  }
4264  else
4265  {
4266  ereport(ERROR,
4267  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4268  errmsg("interval units \"%s\" not recognized",
4269  lowunits)));
4270  }
4271 
4272  PG_RETURN_INTERVAL_P(result);
4273 }
#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:37
#define DTK_QUARTER
Definition: datetime.h:167
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:704
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3731
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
static struct pg_tm tm
Definition: localtime.c:102
#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:45
#define DTK_SECOND
Definition: datetime.h:161
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1964
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
#define ereport(elevel,...)
Definition: elog.h:155
#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:950
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
Definition: c.h:609
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:664
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1992

◆ interval_um()

Datum interval_um ( PG_FUNCTION_ARGS  )

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

3046 {
3048  Interval *result;
3049 
3050  result = (Interval *) palloc(sizeof(Interval));
3051 
3052  result->time = -interval->time;
3053  /* overflow check copied from int4um */
3054  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3055  ereport(ERROR,
3056  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3057  errmsg("interval out of range")));
3058  result->day = -interval->day;
3059  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3060  ereport(ERROR,
3061  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3062  errmsg("interval out of range")));
3063  result->month = -interval->month;
3064  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3065  ereport(ERROR,
3066  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3067  errmsg("interval out of range")));
3068 
3069  PG_RETURN_INTERVAL_P(result);
3070 }
#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:704
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:45
TimeOffset time
Definition: timestamp.h:45
#define SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:155
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

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

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

◆ intervaltypmodleastfield()

static int intervaltypmodleastfield ( int32  typmod)
static

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

1194 {
1195  if (typmod < 0)
1196  return 0; /* SECOND */
1197 
1198  switch (INTERVAL_RANGE(typmod))
1199  {
1200  case INTERVAL_MASK(YEAR):
1201  return 5; /* YEAR */
1202  case INTERVAL_MASK(MONTH):
1203  return 4; /* MONTH */
1204  case INTERVAL_MASK(DAY):
1205  return 3; /* DAY */
1206  case INTERVAL_MASK(HOUR):
1207  return 2; /* HOUR */
1208  case INTERVAL_MASK(MINUTE):
1209  return 1; /* MINUTE */
1210  case INTERVAL_MASK(SECOND):
1211  return 0; /* SECOND */
1213  return 4; /* MONTH */
1215  return 2; /* HOUR */
1217  return 1; /* MINUTE */
1219  return 0; /* SECOND */
1221  return 1; /* MINUTE */
1223  return 0; /* SECOND */
1225  return 0; /* SECOND */
1226  case INTERVAL_FULL_RANGE:
1227  return 0; /* SECOND */
1228  default:
1229  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1230  break;
1231  }
1232  return 0; /* can't get here, but keep compiler quiet */
1233 }
#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:45
#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:228
#define HOUR
Definition: datetime.h:101

◆ intervaltypmodout()

Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

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

1108 {
1109  int32 typmod = PG_GETARG_INT32(0);
1110  char *res = (char *) palloc(64);
1111  int fields;
1112  int precision;
1113  const char *fieldstr;
1114 
1115  if (typmod < 0)
1116  {
1117  *res = '\0';
1118  PG_RETURN_CSTRING(res);
1119  }
1120 
1121  fields = INTERVAL_RANGE(typmod);
1122  precision = INTERVAL_PRECISION(typmod);
1123 
1124  switch (fields)
1125  {
1126  case INTERVAL_MASK(YEAR):
1127  fieldstr = " year";
1128  break;
1129  case INTERVAL_MASK(MONTH):
1130  fieldstr = " month";
1131  break;
1132  case INTERVAL_MASK(DAY):
1133  fieldstr = " day";
1134  break;
1135  case INTERVAL_MASK(HOUR):
1136  fieldstr = " hour";
1137  break;
1138  case INTERVAL_MASK(MINUTE):
1139  fieldstr = " minute";
1140  break;
1141  case INTERVAL_MASK(SECOND):
1142  fieldstr = " second";
1143  break;
1145  fieldstr = " year to month";
1146  break;
1148  fieldstr = " day to hour";
1149  break;
1151  fieldstr = " day to minute";
1152  break;
1154  fieldstr = " day to second";
1155  break;
1157  fieldstr = " hour to minute";
1158  break;
1160  fieldstr = " hour to second";
1161  break;
1163  fieldstr = " minute to second";
1164  break;
1165  case INTERVAL_FULL_RANGE:
1166  fieldstr = "";
1167  break;
1168  default:
1169  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1170  fieldstr = "";
1171  break;
1172  }
1173 
1174  if (precision != INTERVAL_FULL_PRECISION)
1175  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1176  else
1177  snprintf(res, 64, "%s", fieldstr);
1178 
1179  PG_RETURN_CSTRING(res);
1180 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#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:417
#define ERROR
Definition: elog.h:45
#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:362
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
#define HOUR
Definition: datetime.h:101
#define snprintf
Definition: port.h:215

◆ isoweek2date()

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

Definition at line 4301 of file timestamp.c.

References isoweek2j(), and j2date().

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

4302 {
4303  j2date(isoweek2j(*year, woy), year, mon, mday);
4304 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4281

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4281 of file timestamp.c.

References date2j(), and j2day().

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

4282 {
4283  int day0,
4284  day4;
4285 
4286  /* fourth day of current year */
4287  day4 = date2j(year, 1, 4);
4288 
4289  /* day0 == offset to first day of week (Monday) */
4290  day0 = j2day(day4 - 1);
4291 
4292  return ((week - 1) * 7) + (day4 - day0);
4293 }
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ isoweekdate2date()

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

Definition at line 4314 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4315 {
4316  int jday;
4317 
4318  jday = isoweek2j(*year, isoweek);
4319  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4320  if (wday > 1)
4321  jday += wday - 2;
4322  else
4323  jday += 6;
4324  j2date(jday, year, mon, mday);
4325 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4281

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1495 of file timestamp.c.

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

1496 {
1497  int32 years = PG_GETARG_INT32(0);
1499  int32 weeks = PG_GETARG_INT32(2);
1500  int32 days = PG_GETARG_INT32(3);
1501  int32 hours = PG_GETARG_INT32(4);
1502  int32 mins = PG_GETARG_INT32(5);
1503  double secs = PG_GETARG_FLOAT8(6);
1504  Interval *result;
1505 
1506  /*
1507  * Reject out-of-range inputs. We really ought to check the integer
1508  * inputs as well, but it's not entirely clear what limits to apply.
1509  */
1510  if (isinf(secs) || isnan(secs))
1511  ereport(ERROR,
1512  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1513  errmsg("interval out of range")));
1514 
1515  result = (Interval *) palloc(sizeof(Interval));
1516  result->month = years * MONTHS_PER_YEAR + months;
1517  result->day = weeks * 7 + days;
1518 
1519  secs = rint(secs * USECS_PER_SEC);
1520  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1521  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1522  (int64) secs;
1523 
1524  PG_RETURN_INTERVAL_P(result);
1525 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#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:704
signed int int32
Definition: c.h:417
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
const char *const months[]
Definition: datetime.c:65
#define ERROR
Definition: elog.h:45
#define SECS_PER_MINUTE
Definition: timestamp.h:88
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:68
#define SECS_PER_HOUR
Definition: timestamp.h:87
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:155
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 634 of file timestamp.c.

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

635 {
636  int32 year = PG_GETARG_INT32(0);
637  int32 month = PG_GETARG_INT32(1);
638  int32 mday = PG_GETARG_INT32(2);
639  int32 hour = PG_GETARG_INT32(3);
640  int32 min = PG_GETARG_INT32(4);
641  float8 sec = PG_GETARG_FLOAT8(5);
642  Timestamp result;
643 
644  result = make_timestamp_internal(year, month, mday,
645  hour, min, sec);
646 
647  PG_RETURN_TIMESTAMP(result);
648 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:552
signed int int32
Definition: c.h:417
double float8
Definition: c.h:553
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 552 of file timestamp.c.

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

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

◆ make_timestamptz()

Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 654 of file timestamp.c.

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

655 {
656  int32 year = PG_GETARG_INT32(0);
657  int32 month = PG_GETARG_INT32(1);
658  int32 mday = PG_GETARG_INT32(2);
659  int32 hour = PG_GETARG_INT32(3);
660  int32 min = PG_GETARG_INT32(4);
661  float8 sec = PG_GETARG_FLOAT8(5);
662  Timestamp result;
663 
664  result = make_timestamp_internal(year, month, mday,
665  hour, min, sec);
666 
668 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:5252
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:552
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:417
double float8
Definition: c.h:553
int64 Timestamp
Definition: timestamp.h:38

◆ make_timestamptz_at_timezone()

Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

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

676 {
677  int32 year = PG_GETARG_INT32(0);
678  int32 month = PG_GETARG_INT32(1);
679  int32 mday = PG_GETARG_INT32(2);
680  int32 hour = PG_GETARG_INT32(3);
681  int32 min = PG_GETARG_INT32(4);
682  float8 sec = PG_GETARG_FLOAT8(5);
684  TimestampTz result;
686  struct pg_tm tt;
687  int tz;
688  fsec_t fsec;
689 
690  timestamp = make_timestamp_internal(year, month, mday,
691  hour, min, sec);
692 
693  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
694  ereport(ERROR,
695  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
696  errmsg("timestamp out of range")));
697 
698  tz = parse_sane_timezone(&tt, zone);
699 
700  result = dt2local(timestamp, -tz);
701 
702  if (!IS_VALID_TIMESTAMP(result))
703  ereport(ERROR,
704  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
705  errmsg("timestamp out of range")));
706 
707  PG_RETURN_TIMESTAMPTZ(result);
708 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
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:552
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1820
int errcode(int sqlerrcode)
Definition: elog.c:704
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:417
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define ERROR
Definition: elog.h:45
double float8
Definition: c.h:553
int32 fsec_t
Definition: timestamp.h:41
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:155
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Definition: zic.c:93
int errmsg(const char *fmt,...)
Definition: elog.c:915
Definition: c.h:609
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2014
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:468

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3254 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3255 {
3256  /* Args are float8 and Interval *, but leave them as generic Datum */
3257  Datum factor = PG_GETARG_DATUM(0);
3258  Datum span = PG_GETARG_DATUM(1);
3259 
3260  return DirectFunctionCall2(interval_mul, span, factor);
3261 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uintptr_t Datum
Definition: postgres.h:367
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3177
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ NonFiniteTimestampTzPart()

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

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

4463 {
4464  if ((type != UNITS) && (type != RESERV))
4465  {
4466  if (isTz)
4467  ereport(ERROR,
4468  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4469  errmsg("timestamp with time zone units \"%s\" not recognized",
4470  lowunits)));
4471  else
4472  ereport(ERROR,
4473  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4474  errmsg("timestamp units \"%s\" not recognized",
4475  lowunits)));
4476  }
4477 
4478  switch (unit)
4479  {
4480  /* Oscillating units */
4481  case DTK_MICROSEC:
4482  case DTK_MILLISEC:
4483  case DTK_SECOND:
4484  case DTK_MINUTE:
4485  case DTK_HOUR:
4486  case DTK_DAY:
4487  case DTK_MONTH:
4488  case DTK_QUARTER:
4489  case DTK_WEEK:
4490  case DTK_DOW:
4491  case DTK_ISODOW:
4492  case DTK_DOY:
4493  case DTK_TZ:
4494  case DTK_TZ_MINUTE:
4495  case DTK_TZ_HOUR:
4496  return 0.0;
4497 
4498  /* Monotonically-increasing units */
4499  case DTK_YEAR:
4500  case DTK_DECADE:
4501  case DTK_CENTURY:
4502  case DTK_MILLENNIUM:
4503  case DTK_JULIAN:
4504  case DTK_ISOYEAR:
4505  case DTK_EPOCH:
4506  if (isNegative)
4507  return -get_float8_infinity();
4508  else
4509  return get_float8_infinity();
4510 
4511  default:
4512  if (isTz)
4513  ereport(ERROR,
4514  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4515  errmsg("timestamp with time zone units \"%s\" not supported",
4516  lowunits)));
4517  else
4518  ereport(ERROR,
4519  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4520  errmsg("timestamp units \"%s\" not supported",
4521  lowunits)));
4522  return 0.0; /* keep compiler quiet */
4523  }
4524 }
#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:93
#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:704
#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:45
#define DTK_SECOND
Definition: datetime.h:161
#define DTK_ISOYEAR
Definition: datetime.h:180
#define DTK_TZ_MINUTE
Definition: datetime.h:179
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_DOW
Definition: datetime.h:176
#define DTK_MICROSEC
Definition: datetime.h:173
#define ereport(elevel,...)
Definition: elog.h:155
#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:915

◆ now()

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2492 of file timestamp.c.

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

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

◆ parse_sane_timezone()

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

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

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

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1566 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

1567 {
1569 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgReloadTime
Definition: timestamp.c:53

◆ pg_postmaster_start_time()

Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1560 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

1561 {
1563 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgStartTime
Definition: timestamp.c:50

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

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

2069 {
2070  Timestamp dt;
2071  struct pg_tm tt,
2072  *tm = &tt;
2073 
2074  GetEpochTime(tm);
2075  /* we don't bother to test for failure ... */
2076  tm2timestamp(tm, 0, NULL, &dt);
2077 
2078  return dt;
2079 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1916
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2046

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1548 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1549 {
1551 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:807

◆ time2t()

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

Definition at line 2008 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

2009 {
2010  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2011 }
#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 1730 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().

1731 {
1732  TimestampTz result;
1733 
1734  result = (TimestampTz) tm -
1736  result *= USECS_PER_SEC;
1737 
1738  return result;
1739 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:102
#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 1624 of file timestamp.c.

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

1625 {
1626  struct timeval tp;
1627  char templ[128];
1628  char buf[128];
1629  pg_time_t tt;
1630 
1631  gettimeofday(&tp, NULL);
1632  tt = (pg_time_t) tp.tv_sec;