PostgreSQL Source Code  git master
timestamp.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <sys/time.h>
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/int128.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
#include "utils/float.h"
Include dependency graph for timestamp.c:

Go to the source code of this file.

Data Structures

struct  generate_series_timestamp_fctx
 
struct  generate_series_timestamptz_fctx
 

Macros

#define SAMESIGN(a, b)   (((a) < 0) == ((b) < 0))
 
#define TIMESTAMP_GT(t1, t2)   DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
 
#define TIMESTAMP_LT(t1, t2)   DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
 

Functions

static TimeOffset time2t (const int hour, const int min, const int sec, const fsec_t fsec)
 
static Timestamp dt2local (Timestamp dt, int timezone)
 
static void 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)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const char * timestamptz_to_str (TimestampTz t)
 
void dt2time (Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
int tm2timestamp (struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
 
int interval2tm (Interval span, struct pg_tm *tm, fsec_t *fsec)
 
int tm2interval (struct pg_tm *tm, fsec_t fsec, Interval *span)
 
Datum timestamp_finite (PG_FUNCTION_ARGS)
 
Datum interval_finite (PG_FUNCTION_ARGS)
 
void GetEpochTime (struct pg_tm *tm)
 
Timestamp SetEpochTimestamp (void)
 
int timestamp_cmp_internal (Timestamp dt1, Timestamp dt2)
 
Datum timestamp_eq (PG_FUNCTION_ARGS)
 
Datum timestamp_ne (PG_FUNCTION_ARGS)
 
Datum timestamp_lt (PG_FUNCTION_ARGS)
 
Datum timestamp_gt (PG_FUNCTION_ARGS)
 
Datum timestamp_le (PG_FUNCTION_ARGS)
 
Datum timestamp_ge (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp (PG_FUNCTION_ARGS)
 
static int timestamp_fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum timestamp_sortsupport (PG_FUNCTION_ARGS)
 
Datum timestamp_hash (PG_FUNCTION_ARGS)
 
Datum timestamp_hash_extended (PG_FUNCTION_ARGS)
 
Datum timestamp_eq_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ne_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_lt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_gt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_le_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ge_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_eq_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ne_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_lt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_gt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_le_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ge_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_cmp_timestamp (PG_FUNCTION_ARGS)
 
static INT128 interval_cmp_value (const Interval *interval)
 
static int interval_cmp_internal (Interval *interval1, Interval *interval2)
 
Datum interval_eq (PG_FUNCTION_ARGS)
 
Datum interval_ne (PG_FUNCTION_ARGS)
 
Datum interval_lt (PG_FUNCTION_ARGS)
 
Datum interval_gt (PG_FUNCTION_ARGS)
 
Datum interval_le (PG_FUNCTION_ARGS)
 
Datum interval_ge (PG_FUNCTION_ARGS)
 
Datum interval_cmp (PG_FUNCTION_ARGS)
 
Datum interval_hash (PG_FUNCTION_ARGS)
 
Datum interval_hash_extended (PG_FUNCTION_ARGS)
 
Datum overlaps_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_smaller (PG_FUNCTION_ARGS)
 
Datum timestamp_larger (PG_FUNCTION_ARGS)
 
Datum timestamp_mi (PG_FUNCTION_ARGS)
 
Datum interval_justify_interval (PG_FUNCTION_ARGS)
 
Datum interval_justify_hours (PG_FUNCTION_ARGS)
 
Datum interval_justify_days (PG_FUNCTION_ARGS)
 
Datum timestamp_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_mi_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (PG_FUNCTION_ARGS)
 
Datum interval_um (PG_FUNCTION_ARGS)
 
Datum interval_smaller (PG_FUNCTION_ARGS)
 
Datum interval_larger (PG_FUNCTION_ARGS)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
Datum interval_mi (PG_FUNCTION_ARGS)
 
Datum interval_mul (PG_FUNCTION_ARGS)
 
Datum mul_d_interval (PG_FUNCTION_ARGS)
 
Datum interval_div (PG_FUNCTION_ARGS)
 
Datum in_range_timestamptz_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timestamp_interval (PG_FUNCTION_ARGS)
 
Datum in_range_interval_interval (PG_FUNCTION_ARGS)
 
Datum interval_accum (PG_FUNCTION_ARGS)
 
Datum interval_combine (PG_FUNCTION_ARGS)
 
Datum interval_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (PG_FUNCTION_ARGS)
 
Datum timestamp_age (PG_FUNCTION_ARGS)
 
Datum timestamptz_age (PG_FUNCTION_ARGS)
 
Datum timestamp_trunc (PG_FUNCTION_ARGS)
 
static TimestampTz timestamptz_trunc_internal (text *units, TimestampTz timestamp, pg_tz *tzp)
 
Datum timestamptz_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc_zone (PG_FUNCTION_ARGS)
 
Datum interval_trunc (PG_FUNCTION_ARGS)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
static float8 NonFiniteTimestampTzPart (int type, int unit, char *lowunits, bool isNegative, bool isTz)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
bool TimestampTimestampTzRequiresRewrite (void)
 
Datum timestamp_timestamptz (PG_FUNCTION_ARGS)
 
TimestampTz timestamp2timestamptz_opt_error (Timestamp timestamp, bool *have_error)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ SAMESIGN

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

Definition at line 46 of file timestamp.c.

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

◆ TIMESTAMP_GT

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

Referenced by overlaps_timestamp().

◆ TIMESTAMP_LT

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

Referenced by overlaps_timestamp().

Function Documentation

◆ AdjustIntervalForTypmod()

static void AdjustIntervalForTypmod ( Interval interval,
int32  typmod 
)
static

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

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

◆ AdjustTimestampForTypmod()

void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)

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

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

◆ AdjustTimestampForTypmodError()

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

Definition at line 336 of file timestamp.c.

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

Referenced by AdjustTimestampForTypmod().

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

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 101 of file timestamp.c.

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

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

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 80 of file timestamp.c.

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

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

◆ anytimestamp_typmodout()

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

Definition at line 123 of file timestamp.c.

References psprintf().

Referenced by timestamptypmodout(), and timestamptztypmodout().

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

◆ clock_timestamp()

Datum clock_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1559 of file timestamp.c.

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1560 {
1562 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40

◆ date2isoweek()

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

Definition at line 4324 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyear()

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

Definition at line 4378 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyearday()

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

Definition at line 4435 of file timestamp.c.

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

Referenced by DCH_to_char().

4436 {
4437  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4438 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4378
int isoweek2j(int year, int week)
Definition: timestamp.c:4273
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 1985 of file timestamp.c.

References USECS_PER_SEC.

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

1986 {
1987  dt -= (tz * USECS_PER_SEC);
1988  return dt;
1989 }
#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 1764 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1765 {
1766  TimeOffset time;
1767 
1768  time = jd;
1769 
1770  *hour = time / USECS_PER_HOUR;
1771  time -= (*hour) * USECS_PER_HOUR;
1772  *min = time / USECS_PER_MINUTE;
1773  time -= (*min) * USECS_PER_MINUTE;
1774  *sec = time / USECS_PER_SEC;
1775  *fsec = time - (*sec * USECS_PER_SEC);
1776 } /* 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 1536 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().

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

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 720 of file timestamp.c.

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

721 {
722  float8 seconds = PG_GETARG_FLOAT8(0);
723  TimestampTz result;
724 
725  /* Deal with NaN and infinite inputs ... */
726  if (isnan(seconds))
727  ereport(ERROR,
728  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
729  errmsg("timestamp cannot be NaN")));
730 
731  if (isinf(seconds))
732  {
733  if (seconds < 0)
734  TIMESTAMP_NOBEGIN(result);
735  else
736  TIMESTAMP_NOEND(result);
737  }
738  else
739  {
740  /* Out of range? */
741  if (seconds <
743  || seconds >=
745  ereport(ERROR,
746  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
747  errmsg("timestamp out of range: \"%g\"", seconds)));
748 
749  /* Convert UNIX epoch to Postgres epoch */
751 
752  seconds = rint(seconds * USECS_PER_SEC);
753  result = (int64) seconds;
754 
755  /* Recheck in case roundoff produces something just out of range */
756  if (!IS_VALID_TIMESTAMP(result))
757  ereport(ERROR,
758  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
759  errmsg("timestamp out of range: \"%g\"",
760  PG_GETARG_FLOAT8(0))));
761  }
762 
763  PG_RETURN_TIMESTAMP(result);
764 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#define SECS_PER_DAY
Definition: timestamp.h:86
int isinf(double x)
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define DATETIME_MIN_JULIAN
Definition: timestamp.h:179

◆ generate_series_timestamp()

Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

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

5398 {
5399  FuncCallContext *funcctx;
5401  Timestamp result;
5402 
5403  /* stuff done only on the first call of the function */
5404  if (SRF_IS_FIRSTCALL())
5405  {
5406  Timestamp start = PG_GETARG_TIMESTAMP(0);
5407  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5408  Interval *step = PG_GETARG_INTERVAL_P(2);
5409  MemoryContext oldcontext;
5410  Interval interval_zero;
5411 
5412  /* create a function context for cross-call persistence */
5413  funcctx = SRF_FIRSTCALL_INIT();
5414 
5415  /*
5416  * switch to memory context appropriate for multiple function calls
5417  */
5418  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5419 
5420  /* allocate memory for user context */
5423 
5424  /*
5425  * Use fctx to keep state from call to call. Seed current with the
5426  * original start value
5427  */
5428  fctx->current = start;
5429  fctx->finish = finish;
5430  fctx->step = *step;
5431 
5432  /* Determine sign of the interval */
5433  MemSet(&interval_zero, 0, sizeof(Interval));
5434  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5435 
5436  if (fctx->step_sign == 0)
5437  ereport(ERROR,
5438  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5439  errmsg("step size cannot equal zero")));
5440 
5441  funcctx->user_fctx = fctx;
5442  MemoryContextSwitchTo(oldcontext);
5443  }
5444 
5445  /* stuff done on every call of the function */
5446  funcctx = SRF_PERCALL_SETUP();
5447 
5448  /*
5449  * get the saved state and use current as the result for this iteration
5450  */
5451  fctx = funcctx->user_fctx;
5452  result = fctx->current;
5453 
5454  if (fctx->step_sign > 0 ?
5455  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5456  timestamp_cmp_internal(result, fctx->finish) >= 0)
5457  {
5458  /* increment current in preparation for next iteration */
5459  fctx->current = DatumGetTimestamp(
5461  TimestampGetDatum(fctx->current),
5462  PointerGetDatum(&fctx->step)));
5463 
5464  /* do when there is more left to send */
5465  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5466  }
5467  else
5468  {
5469  /* do when there is no more left */
5470  SRF_RETURN_DONE(funcctx);
5471  }
5472 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2059
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
#define MemSet(start, val, len)
Definition: c.h:955
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2831
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2367
void * user_fctx
Definition: funcapi.h:83
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

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

5479 {
5480  FuncCallContext *funcctx;
5482  TimestampTz result;
5483 
5484  /* stuff done only on the first call of the function */
5485  if (SRF_IS_FIRSTCALL())
5486  {
5488  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5489  Interval *step = PG_GETARG_INTERVAL_P(2);
5490  MemoryContext oldcontext;
5491  Interval interval_zero;
5492 
5493  /* create a function context for cross-call persistence */
5494  funcctx = SRF_FIRSTCALL_INIT();
5495 
5496  /*
5497  * switch to memory context appropriate for multiple function calls
5498  */
5499  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5500 
5501  /* allocate memory for user context */
5504 
5505  /*
5506  * Use fctx to keep state from call to call. Seed current with the
5507  * original start value
5508  */
5509  fctx->current = start;
5510  fctx->finish = finish;
5511  fctx->step = *step;
5512 
5513  /* Determine sign of the interval */
5514  MemSet(&interval_zero, 0, sizeof(Interval));
5515  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5516 
5517  if (fctx->step_sign == 0)
5518  ereport(ERROR,
5519  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5520  errmsg("step size cannot equal zero")));
5521 
5522  funcctx->user_fctx = fctx;
5523  MemoryContextSwitchTo(oldcontext);
5524  }
5525 
5526  /* stuff done on every call of the function */
5527  funcctx = SRF_PERCALL_SETUP();
5528 
5529  /*
5530  * get the saved state and use current as the result for this iteration
5531  */
5532  fctx = funcctx->user_fctx;
5533  result = fctx->current;
5534 
5535  if (fctx->step_sign > 0 ?
5536  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5537  timestamp_cmp_internal(result, fctx->finish) >= 0)
5538  {
5539  /* increment current in preparation for next iteration */
5540  fctx->current = DatumGetTimestampTz(
5543  PointerGetDatum(&fctx->step)));
5544 
5545  /* do when there is more left to send */
5546  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5547  }
5548  else
5549  {
5550  /* do when there is no more left */
5551  SRF_RETURN_DONE(funcctx);
5552  }
5553 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2059
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
#define PointerGetDatum(X)
Definition: postgres.h:556
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
#define MemSet(start, val, len)
Definition: c.h:955
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
#define ERROR
Definition: elog.h:43
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2936
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2367
void * user_fctx
Definition: funcapi.h:83
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1583 of file timestamp.c.

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

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

1584 {
1585  TimestampTz result;
1586  struct timeval tp;
1587 
1588  gettimeofday(&tp, NULL);
1589 
1590  result = (TimestampTz) tp.tv_sec -
1592  result = (result * USECS_PER_SEC) + tp.tv_usec;
1593 
1594  return result;
1595 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

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

2018 {
2019  struct pg_tm *t0;
2020  pg_time_t epoch = 0;
2021 
2022  t0 = pg_gmtime(&epoch);
2023 
2024  if (t0 == NULL)
2025  elog(ERROR, "could not convert epoch to timestamp: %m");
2026 
2027  tm->tm_year = t0->tm_year;
2028  tm->tm_mon = t0->tm_mon;
2029  tm->tm_mday = t0->tm_mday;
2030  tm->tm_hour = t0->tm_hour;
2031  tm->tm_min = t0->tm_min;
2032  tm->tm_sec = t0->tm_sec;
2033 
2034  tm->tm_year += 1900;
2035  tm->tm_mon++;
2036 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1420
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32
#define elog(elevel,...)
Definition: elog.h:226
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1601 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

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

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1615 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1616 {
1617  Timestamp ts;
1618 
1620  if (typmod >= 0)
1621  AdjustTimestampForTypmod(&ts, typmod);
1622  return ts;
1623 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:391
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5248
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

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

3371 {
3373  Interval *base = PG_GETARG_INTERVAL_P(1);
3374  Interval *offset = PG_GETARG_INTERVAL_P(2);
3375  bool sub = PG_GETARG_BOOL(3);
3376  bool less = PG_GETARG_BOOL(4);
3377  Interval *sum;
3378 
3379  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3380  ereport(ERROR,
3381  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3382  errmsg("invalid preceding or following size in window function")));
3383 
3384  /* We don't currently bother to avoid overflow hazards here */
3385  if (sub)
3387  IntervalPGetDatum(base),
3388  IntervalPGetDatum(offset)));
3389  else
3391  IntervalPGetDatum(base),
3392  IntervalPGetDatum(offset)));
3393 
3394  if (less)
3395  PG_RETURN_BOOL(interval_cmp_internal(val, sum) <= 0);
3396  else
3397  PG_RETURN_BOOL(interval_cmp_internal(val, sum) >= 0);
3398 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3129
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3095
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2367
int errmsg(const char *fmt,...)
Definition: elog.c:784
long val
Definition: informix.c:684
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2342

◆ in_range_timestamp_interval()

Datum in_range_timestamp_interval ( PG_FUNCTION_ARGS  )

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

3340 {
3342  Timestamp base = PG_GETARG_TIMESTAMP(1);
3343  Interval *offset = PG_GETARG_INTERVAL_P(2);
3344  bool sub = PG_GETARG_BOOL(3);
3345  bool less = PG_GETARG_BOOL(4);
3346  Timestamp sum;
3347 
3348  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3349  ereport(ERROR,
3350  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3351  errmsg("invalid preceding or following size in window function")));
3352 
3353  /* We don't currently bother to avoid overflow hazards here */
3354  if (sub)
3356  TimestampGetDatum(base),
3357  IntervalPGetDatum(offset)));
3358  else
3360  TimestampGetDatum(base),
3361  IntervalPGetDatum(offset)));
3362 
3363  if (less)
3364  PG_RETURN_BOOL(val <= sum);
3365  else
3366  PG_RETURN_BOOL(val >= sum);
3367 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2910
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2831
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define ereport(elevel, rest)
Definition: elog.h:141
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
int errmsg(const char *fmt,...)
Definition: elog.c:784
long val
Definition: informix.c:684
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2342
#define DatumGetTimestamp(X)
Definition: timestamp.h:27

◆ in_range_timestamptz_interval()

Datum in_range_timestamptz_interval ( PG_FUNCTION_ARGS  )

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

3309 {
3312  Interval *offset = PG_GETARG_INTERVAL_P(2);
3313  bool sub = PG_GETARG_BOOL(3);
3314  bool less = PG_GETARG_BOOL(4);
3315  TimestampTz sum;
3316 
3317  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3318  ereport(ERROR,
3319  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3320  errmsg("invalid preceding or following size in window function")));
3321 
3322  /* We don't currently bother to avoid overflow hazards here */
3323  if (sub)
3325  TimestampTzGetDatum(base),
3326  IntervalPGetDatum(offset)));
3327  else
3329  TimestampTzGetDatum(base),
3330  IntervalPGetDatum(offset)));
3331 
3332  if (less)
3333  PG_RETURN_BOOL(val <= sum);
3334  else
3335  PG_RETURN_BOOL(val >= sum);
3336 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int64 TimestampTz
Definition: timestamp.h:39
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
Datum timestamptz_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3020
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2936
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
long val
Definition: informix.c:684
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2342

◆ interval2tm()

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

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

1936 {
1937  TimeOffset time;
1938  TimeOffset tfrac;
1939 
1940  tm->tm_year = span.month / MONTHS_PER_YEAR;
1941  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1942  tm->tm_mday = span.day;
1943  time = span.time;
1944 
1945  tfrac = time / USECS_PER_HOUR;
1946  time -= tfrac * USECS_PER_HOUR;
1947  tm->tm_hour = tfrac;
1948  if (!SAMESIGN(tm->tm_hour, tfrac))
1949  ereport(ERROR,
1950  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1951  errmsg("interval out of range")));
1952  tfrac = time / USECS_PER_MINUTE;
1953  time -= tfrac * USECS_PER_MINUTE;
1954  tm->tm_min = tfrac;
1955  tfrac = time / USECS_PER_SEC;
1956  *fsec = time - (tfrac * USECS_PER_SEC);
1957  tm->tm_sec = tfrac;
1958 
1959  return 0;
1960 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int errcode(int sqlerrcode)
Definition: elog.c:570
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USECS_PER_HOUR
Definition: timestamp.h:92
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:141
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:784
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ interval_accum()

Datum interval_accum ( PG_FUNCTION_ARGS  )

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

3413 {
3414  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3416  Datum *transdatums;
3417  int ndatums;
3418  Interval sumX,
3419  N;
3420  Interval *newsum;
3421  ArrayType *result;
3422 
3423  deconstruct_array(transarray,
3424  INTERVALOID, sizeof(Interval), false, 'd',
3425  &transdatums, NULL, &ndatums);
3426  if (ndatums != 2)
3427  elog(ERROR, "expected 2-element interval array");
3428 
3429  sumX = *(DatumGetIntervalP(transdatums[0]));
3430  N = *(DatumGetIntervalP(transdatums[1]));
3431 
3433  IntervalPGetDatum(&sumX),
3434  IntervalPGetDatum(newval)));
3435  N.time += 1;
3436 
3437  transdatums[0] = IntervalPGetDatum(newsum);
3438  transdatums[1] = IntervalPGetDatum(&N);
3439 
3440  result = construct_array(transdatums, 2,
3441  INTERVALOID, sizeof(Interval), false, 'd');
3442 
3443  PG_RETURN_ARRAYTYPE_P(result);
3444 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3095
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
uintptr_t Datum
Definition: postgres.h:367
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619

◆ interval_accum_inv()

Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

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

3497 {
3498  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3500  Datum *transdatums;
3501  int ndatums;
3502  Interval sumX,
3503  N;
3504  Interval *newsum;
3505  ArrayType *result;
3506 
3507  deconstruct_array(transarray,
3508  INTERVALOID, sizeof(Interval), false, 'd',
3509  &transdatums, NULL, &ndatums);
3510  if (ndatums != 2)
3511  elog(ERROR, "expected 2-element interval array");
3512 
3513  sumX = *(DatumGetIntervalP(transdatums[0]));
3514  N = *(DatumGetIntervalP(transdatums[1]));
3515 
3517  IntervalPGetDatum(&sumX),
3518  IntervalPGetDatum(newval)));
3519  N.time -= 1;
3520 
3521  transdatums[0] = IntervalPGetDatum(newsum);
3522  transdatums[1] = IntervalPGetDatum(&N);
3523 
3524  result = construct_array(transdatums, 2,
3525  INTERVALOID, sizeof(Interval), false, 'd');
3526 
3527  PG_RETURN_ARRAYTYPE_P(result);
3528 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3129
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
uintptr_t Datum
Definition: postgres.h:367
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

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

3532 {
3533  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3534  Datum *transdatums;
3535  int ndatums;
3536  Interval sumX,
3537  N;
3538 
3539  deconstruct_array(transarray,
3540  INTERVALOID, sizeof(Interval), false, 'd',
3541  &transdatums, NULL, &ndatums);
3542  if (ndatums != 2)
3543  elog(ERROR, "expected 2-element interval array");
3544 
3545  sumX = *(DatumGetIntervalP(transdatums[0]));
3546  N = *(DatumGetIntervalP(transdatums[1]));
3547 
3548  /* SQL defines AVG of no values to be NULL */
3549  if (N.time == 0)
3550  PG_RETURN_NULL();
3551 
3553  IntervalPGetDatum(&sumX),
3554  Float8GetDatum((double) N.time));
3555 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1723
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
uintptr_t Datum
Definition: postgres.h:367
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3256
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2430 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp(), and leftmostvalue_interval().

2431 {
2432  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2433  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2434 
2435  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2436 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2367

◆ interval_cmp_internal()

static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

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

2368 {
2369  INT128 span1 = interval_cmp_value(interval1);
2370  INT128 span2 = interval_cmp_value(interval2);
2371 
2372  return int128_compare(span1, span2);
2373 }
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:2342

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

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

2343 {
2344  INT128 span;
2345  int64 dayfraction;
2346  int64 days;
2347 
2348  /*
2349  * Separate time field into days and dayfraction, then add the month and
2350  * day fields to the days part. We cannot overflow int64 days here.
2351  */
2352  dayfraction = interval->time % USECS_PER_DAY;
2353  days = interval->time / USECS_PER_DAY;
2354  days += interval->month * INT64CONST(30);
2355  days += interval->day;
2356 
2357  /* Widen dayfraction to 128 bits */
2358  span = int64_to_int128(dayfraction);
2359 
2360  /* Scale up days to microseconds, forming a 128-bit product */
2362 
2363  return span;
2364 }
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:69
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
Definition: int128.h:107
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
Definition: int128.h:177

◆ interval_combine()

Datum interval_combine ( PG_FUNCTION_ARGS  )

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

3448 {
3449  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3450  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3451  Datum *transdatums1;
3452  Datum *transdatums2;
3453  int ndatums1;
3454  int ndatums2;
3455  Interval sum1,
3456  N1;
3457  Interval sum2,
3458  N2;
3459 
3460  Interval *newsum;
3461  ArrayType *result;
3462 
3463  deconstruct_array(transarray1,
3464  INTERVALOID, sizeof(Interval), false, 'd',
3465  &transdatums1, NULL, &ndatums1);
3466  if (ndatums1 != 2)
3467  elog(ERROR, "expected 2-element interval array");
3468 
3469  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3470  N1 = *(DatumGetIntervalP(transdatums1[1]));
3471 
3472  deconstruct_array(transarray2,
3473  INTERVALOID, sizeof(Interval), false, 'd',
3474  &transdatums2, NULL, &ndatums2);
3475  if (ndatums2 != 2)
3476  elog(ERROR, "expected 2-element interval array");
3477 
3478  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3479  N2 = *(DatumGetIntervalP(transdatums2[1]));
3480 
3482  IntervalPGetDatum(&sum1),
3483  IntervalPGetDatum(&sum2)));
3484  N1.time += N2.time;
3485 
3486  transdatums1[0] = IntervalPGetDatum(newsum);
3487  transdatums1[1] = IntervalPGetDatum(&N1);
3488 
3489  result = construct_array(transdatums1, 2,
3490  INTERVALOID, sizeof(Interval), false, 'd');
3491 
3492  PG_RETURN_ARRAYTYPE_P(result);
3493 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3095
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
uintptr_t Datum
Definition: postgres.h:367
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
#define elog(elevel,...)
Definition: elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3256 of file timestamp.c.

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

Referenced by interval_avg().

3257 {
3258  Interval *span = PG_GETARG_INTERVAL_P(0);
3259  float8 factor = PG_GETARG_FLOAT8(1);
3260  double month_remainder_days,
3261  sec_remainder;
3262  int32 orig_month = span->month,
3263  orig_day = span->day;
3264  Interval *result;
3265 
3266  result = (Interval *) palloc(sizeof(Interval));
3267 
3268  if (factor == 0.0)
3269  ereport(ERROR,
3270  (errcode(ERRCODE_DIVISION_BY_ZERO),
3271  errmsg("division by zero")));
3272 
3273  result->month = (int32) (span->month / factor);
3274  result->day = (int32) (span->day / factor);
3275 
3276  /*
3277  * Fractional months full days into days. See comment in interval_mul().
3278  */
3279  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3280  month_remainder_days = TSROUND(month_remainder_days);
3281  sec_remainder = (orig_day / factor - result->day +
3282  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3283  sec_remainder = TSROUND(sec_remainder);
3284  if (Abs(sec_remainder) >= SECS_PER_DAY)
3285  {
3286  result->day += (int) (sec_remainder / SECS_PER_DAY);
3287  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3288  }
3289 
3290  /* cascade units down */
3291  result->day += (int32) month_remainder_days;
3292  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3293 
3294  PG_RETURN_INTERVAL_P(result);
3295 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:570
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:910
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
#define SECS_PER_DAY
Definition: timestamp.h:86
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ interval_eq()

Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2376 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

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

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 2006 of file timestamp.c.

References PG_RETURN_BOOL.

2007 {
2008  PG_RETURN_BOOL(true);
2009 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2421 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

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

◆ interval_gt()

Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2403 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

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

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2446 of file timestamp.c.

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

2447 {
2449  INT128 span = interval_cmp_value(interval);
2450  int64 span64;
2451 
2452  /*
2453  * Use only the least significant 64 bits for hashing. The upper 64 bits
2454  * seldom add any useful information, and besides we must do it like this
2455  * for compatibility with hashes calculated before use of INT128 was
2456  * introduced.
2457  */
2458  span64 = int128_to_int64(span);
2459 
2461 }
#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:617
#define Int64GetDatumFast(X)
Definition: postgres.h:775
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2342

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2464 of file timestamp.c.

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

2465 {
2467  INT128 span = interval_cmp_value(interval);
2468  int64 span64;
2469 
2470  /* Same approach as interval_hash */
2471  span64 = int128_to_int64(span);
2472 
2474  PG_GETARG_DATUM(1));
2475 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
#define Int64GetDatumFast(X)
Definition: postgres.h:775
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2342

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

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

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

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2791 of file timestamp.c.

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

2792 {
2793  Interval *span = PG_GETARG_INTERVAL_P(0);
2794  Interval *result;
2795  int32 wholemonth;
2796 
2797  result = (Interval *) palloc(sizeof(Interval));
2798  result->month = span->month;
2799  result->day = span->day;
2800  result->time = span->time;
2801 
2802  wholemonth = result->day / DAYS_PER_MONTH;
2803  result->day -= wholemonth * DAYS_PER_MONTH;
2804  result->month += wholemonth;
2805 
2806  if (result->month > 0 && result->day < 0)
2807  {
2808  result->day += DAYS_PER_MONTH;
2809  result->month--;
2810  }
2811  else if (result->month < 0 && result->day > 0)
2812  {
2813  result->day -= DAYS_PER_MONTH;
2814  result->month++;
2815  }
2816 
2817  PG_RETURN_INTERVAL_P(result);
2818 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:949

◆ interval_justify_hours()

Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

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

2757 {
2758  Interval *span = PG_GETARG_INTERVAL_P(0);
2759  Interval *result;
2760  TimeOffset wholeday;
2761 
2762  result = (Interval *) palloc(sizeof(Interval));
2763  result->month = span->month;
2764  result->day = span->day;
2765  result->time = span->time;
2766 
2767  TMODULO(result->time, wholeday, USECS_PER_DAY);
2768  result->day += wholeday; /* could overflow... */
2769 
2770  if (result->day > 0 && result->time < 0)
2771  {
2772  result->time += USECS_PER_DAY;
2773  result->day--;
2774  }
2775  else if (result->day < 0 && result->time > 0)
2776  {
2777  result->time -= USECS_PER_DAY;
2778  result->day++;
2779  }
2780 
2781  PG_RETURN_INTERVAL_P(result);
2782 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
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:949
#define TMODULO(t, q, u)
Definition: datetime.h:249

◆ interval_justify_interval()

Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

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

2702 {
2703  Interval *span = PG_GETARG_INTERVAL_P(0);
2704  Interval *result;
2705  TimeOffset wholeday;
2706  int32 wholemonth;
2707 
2708  result = (Interval *) palloc(sizeof(Interval));
2709  result->month = span->month;
2710  result->day = span->day;
2711  result->time = span->time;
2712 
2713  TMODULO(result->time, wholeday, USECS_PER_DAY);
2714  result->day += wholeday; /* could overflow... */
2715 
2716  wholemonth = result->day / DAYS_PER_MONTH;
2717  result->day -= wholemonth * DAYS_PER_MONTH;
2718  result->month += wholemonth;
2719 
2720  if (result->month > 0 &&
2721  (result->day < 0 || (result->day == 0 && result->time < 0)))
2722  {
2723  result->day += DAYS_PER_MONTH;
2724  result->month--;
2725  }
2726  else if (result->month < 0 &&
2727  (result->day > 0 || (result->day == 0 && result->time > 0)))
2728  {
2729  result->day -= DAYS_PER_MONTH;
2730  result->month++;
2731  }
2732 
2733  if (result->day > 0 && result->time < 0)
2734  {
2735  result->time += USECS_PER_DAY;
2736  result->day--;
2737  }
2738  else if (result->day < 0 && result->time > 0)
2739  {
2740  result->time -= USECS_PER_DAY;
2741  result->day++;
2742  }
2743 
2744  PG_RETURN_INTERVAL_P(result);
2745 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:949
#define TMODULO(t, q, u)
Definition: datetime.h:249

◆ interval_larger()

Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3081 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3082 {
3083  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3084  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3085  Interval *result;
3086 
3087  if (interval_cmp_internal(interval1, interval2) > 0)
3088  result = interval1;
3089  else
3090  result = interval2;
3091  PG_RETURN_INTERVAL_P(result);
3092 }
#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:2367

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2412 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

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

◆ interval_lt()

Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2394 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

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

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

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

3130 {
3131  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3132  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3133  Interval *result;
3134 
3135  result = (Interval *) palloc(sizeof(Interval));
3136 
3137  result->month = span1->month - span2->month;
3138  /* overflow check copied from int4mi */
3139  if (!SAMESIGN(span1->month, span2->month) &&
3140  !SAMESIGN(result->month, span1->month))
3141  ereport(ERROR,
3142  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3143  errmsg("interval out of range")));
3144 
3145  result->day = span1->day - span2->day;
3146  if (!SAMESIGN(span1->day, span2->day) &&
3147  !SAMESIGN(result->day, span1->day))
3148  ereport(ERROR,
3149  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3150  errmsg("interval out of range")));
3151 
3152  result->time = span1->time - span2->time;
3153  if (!SAMESIGN(span1->time, span2->time) &&
3154  !SAMESIGN(result->time, span1->time))
3155  ereport(ERROR,
3156  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3157  errmsg("interval out of range")));
3158 
3159  PG_RETURN_INTERVAL_P(result);
3160 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:570
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:141
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3169 of file timestamp.c.

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

Referenced by interval_lerp(), and mul_d_interval().

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

◆ interval_ne()

Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2385 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

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

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

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

962 {
963  Interval *span = PG_GETARG_INTERVAL_P(0);
964  char *result;
965  struct pg_tm tt,
966  *tm = &tt;
967  fsec_t fsec;
968  char buf[MAXDATELEN + 1];
969 
970  if (interval2tm(*span, tm, &fsec) != 0)
971  elog(ERROR, "could not convert interval to tm");
972 
973  EncodeInterval(tm, fsec, IntervalStyle, buf);
974 
975  result = pstrdup(buf);
976  PG_RETURN_CSTRING(result);
977 }
#define MAXDATELEN
Definition: datetime.h:201
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int IntervalStyle
Definition: globals.c:117
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define ERROR
Definition: elog.h:43
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1935
static char * buf
Definition: pg_test_fsync.c:68
int32 fsec_t
Definition: timestamp.h:41
void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str)
Definition: datetime.c:4215
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
#define elog(elevel,...)
Definition: elog.h:226

◆ interval_part()

Datum interval_part ( PG_FUNCTION_ARGS  )

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

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

◆ interval_pl()

Datum interval_pl ( PG_FUNCTION_ARGS  )

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

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

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

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

984 {
986 
987 #ifdef NOT_USED
988  Oid typelem = PG_GETARG_OID(1);
989 #endif
990  int32 typmod = PG_GETARG_INT32(2);
992 
993  interval = (Interval *) palloc(sizeof(Interval));
994 
995  interval->time = pq_getmsgint64(buf);
996  interval->day = pq_getmsgint(buf, sizeof(interval->day));
997  interval->month = pq_getmsgint(buf, sizeof(interval->month));
998 
999  AdjustIntervalForTypmod(interval, typmod);
1000 
1001  PG_RETURN_INTERVAL_P(interval);
1002 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1333
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:949
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 1314 of file timestamp.c.

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

1315 {
1317  int32 typmod = PG_GETARG_INT32(1);
1318  Interval *result;
1319 
1320  result = palloc(sizeof(Interval));
1321  *result = *interval;
1322 
1323  AdjustIntervalForTypmod(result, typmod);
1324 
1325  PG_RETURN_INTERVAL_P(result);
1326 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:346
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1333
void * palloc(Size size)
Definition: mcxt.c:949

◆ interval_send()

Datum interval_send ( PG_FUNCTION_ARGS  )

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

1009 {
1012 
1013  pq_begintypsend(&buf);
1014  pq_sendint64(&buf, interval->time);
1015  pq_sendint32(&buf, interval->day);
1016  pq_sendint32(&buf, interval->month);
1018 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
int32 day
Definition: timestamp.h:47
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:68
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3066 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3067 {
3068  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3069  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3070  Interval *result;
3071 
3072  /* use interval_cmp_internal to be sure this agrees with comparisons */
3073  if (interval_cmp_internal(interval1, interval2) < 0)
3074  result = interval1;
3075  else
3076  result = interval2;
3077  PG_RETURN_INTERVAL_P(result);
3078 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2367

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1251 of file timestamp.c.

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

1252 {
1253  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
1254  Node *ret = NULL;
1255 
1256  if (IsA(rawreq, SupportRequestSimplify))
1257  {
1259  FuncExpr *expr = req->fcall;
1260  Node *typmod;
1261 
1262  Assert(list_length(expr->args) >= 2);
1263 
1264  typmod = (Node *) lsecond(expr->args);
1265 
1266  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
1267  {
1268  Node *source = (Node *) linitial(expr->args);
1269  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1270  bool noop;
1271 
1272  if (new_typmod < 0)
1273  noop = true;
1274  else
1275  {
1276  int32 old_typmod = exprTypmod(source);
1277  int old_least_field;
1278  int new_least_field;
1279  int old_precis;
1280  int new_precis;
1281 
1282  old_least_field = intervaltypmodleastfield(old_typmod);
1283  new_least_field = intervaltypmodleastfield(new_typmod);
1284  if (old_typmod < 0)
1285  old_precis = INTERVAL_FULL_PRECISION;
1286  else
1287  old_precis = INTERVAL_PRECISION(old_typmod);
1288  new_precis = INTERVAL_PRECISION(new_typmod);
1289 
1290  /*
1291  * Cast is a no-op if least field stays the same or decreases
1292  * while precision stays the same or increases. But
1293  * precision, which is to say, sub-second precision, only
1294  * affects ranges that include SECOND.
1295  */
1296  noop = (new_least_field <= old_least_field) &&
1297  (old_least_field > 0 /* SECOND */ ||
1298  new_precis >= MAX_INTERVAL_PRECISION ||
1299  new_precis >= old_precis);
1300  }
1301  if (noop)
1302  ret = relabel_to_typmod(source, new_typmod);
1303  }
1304  }
1305 
1306  PG_RETURN_POINTER(ret);
1307 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
List * args
Definition: primnodes.h:463
#define DatumGetInt32(X)
Definition: postgres.h:472
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:525
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define lsecond(l)
Definition: pg_list.h:200
signed int int32
Definition: c.h:346
#define linitial(l)
Definition: pg_list.h:195
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:54
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:53
#define Assert(condition)
Definition: c.h:732
static int list_length(const List *l)
Definition: pg_list.h:169
static int intervaltypmodleastfield(int32 typmod)
Definition: timestamp.c:1198
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:588

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

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

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

◆ interval_um()

Datum interval_um ( PG_FUNCTION_ARGS  )

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

3038 {
3040  Interval *result;
3041 
3042  result = (Interval *) palloc(sizeof(Interval));
3043 
3044  result->time = -interval->time;
3045  /* overflow check copied from int4um */
3046  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3047  ereport(ERROR,
3048  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3049  errmsg("interval out of range")));
3050  result->day = -interval->day;
3051  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3052  ereport(ERROR,
3053  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3054  errmsg("interval out of range")));
3055  result->month = -interval->month;
3056  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3057  ereport(ERROR,
3058  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3059  errmsg("interval out of range")));
3060 
3061  PG_RETURN_INTERVAL_P(result);
3062 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:570
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:141
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

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

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

◆ intervaltypmodleastfield()

static int intervaltypmodleastfield ( int32  typmod)
static

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

1199 {
1200  if (typmod < 0)
1201  return 0; /* SECOND */
1202 
1203  switch (INTERVAL_RANGE(typmod))
1204  {
1205  case INTERVAL_MASK(YEAR):
1206  return 5; /* YEAR */
1207  case INTERVAL_MASK(MONTH):
1208  return 4; /* MONTH */
1209  case INTERVAL_MASK(DAY):
1210  return 3; /* DAY */
1211  case INTERVAL_MASK(HOUR):
1212  return 2; /* HOUR */
1213  case INTERVAL_MASK(MINUTE):
1214  return 1; /* MINUTE */
1215  case INTERVAL_MASK(SECOND):
1216  return 0; /* SECOND */
1218  return 4; /* MONTH */
1220  return 2; /* HOUR */
1222  return 1; /* MINUTE */
1224  return 0; /* SECOND */
1226  return 1; /* MINUTE */
1228  return 0; /* SECOND */
1230  return 0; /* SECOND */
1231  case INTERVAL_FULL_RANGE:
1232  return 0; /* SECOND */
1233  default:
1234  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1235  break;
1236  }
1237  return 0; /* can't get here, but keep compiler quiet */
1238 }
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define ERROR
Definition: elog.h:43
#define MINUTE
Definition: datetime.h:102
#define MONTH
Definition: datetime.h:92
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
#define elog(elevel,...)
Definition: elog.h:226
#define HOUR
Definition: datetime.h:101

◆ intervaltypmodout()

Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

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

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

◆ isoweek2date()

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

Definition at line 4293 of file timestamp.c.

References isoweek2j(), and j2date().

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

4294 {
4295  j2date(isoweek2j(*year, woy), year, mon, mday);
4296 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
int isoweek2j(int year, int week)
Definition: timestamp.c:4273

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4273 of file timestamp.c.

References date2j(), and j2day().

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

4274 {
4275  int day0,
4276  day4;
4277 
4278  /* fourth day of current year */
4279  day4 = date2j(year, 1, 4);
4280 
4281  /* day0 == offset to first day of week (Monday) */
4282  day0 = j2day(day4 - 1);
4283 
4284  return ((week - 1) * 7) + (day4 - day0);
4285 }
int j2day(int date)
Definition: datetime.c:330
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ isoweekdate2date()

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

Definition at line 4306 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4307 {
4308  int jday;
4309 
4310  jday = isoweek2j(*year, isoweek);
4311  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4312  if (wday > 1)
4313  jday += wday - 2;
4314  else
4315  jday += 6;
4316  j2date(jday, year, mon, mday);
4317 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
int isoweek2j(int year, int week)
Definition: timestamp.c:4273

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1500 of file timestamp.c.

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

1501 {
1502  int32 years = PG_GETARG_INT32(0);
1504  int32 weeks = PG_GETARG_INT32(2);
1505  int32 days = PG_GETARG_INT32(3);
1506  int32 hours = PG_GETARG_INT32(4);
1507  int32 mins = PG_GETARG_INT32(5);
1508  double secs = PG_GETARG_FLOAT8(6);
1509  Interval *result;
1510 
1511  /*
1512  * Reject out-of-range inputs. We really ought to check the integer
1513  * inputs as well, but it's not entirely clear what limits to apply.
1514  */
1515  if (isinf(secs) || isnan(secs))
1516  ereport(ERROR,
1517  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1518  errmsg("interval out of range")));
1519 
1520  result = (Interval *) palloc(sizeof(Interval));
1521  result->month = years * MONTHS_PER_YEAR + months;
1522  result->day = weeks * 7 + days;
1523 
1524  secs = rint(secs * USECS_PER_SEC);
1525  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1526  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1527  (int64) secs;
1528 
1529  PG_RETURN_INTERVAL_P(result);
1530 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:570
signed int int32
Definition: c.h:346
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
const char *const months[]
Definition: datetime.c:66
#define ERROR
Definition: elog.h:43
int isinf(double x)
#define SECS_PER_MINUTE
Definition: timestamp.h:88
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:69
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
#define SECS_PER_HOUR
Definition: timestamp.h:87
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 639 of file timestamp.c.

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

640 {
641  int32 year = PG_GETARG_INT32(0);
642  int32 month = PG_GETARG_INT32(1);
643  int32 mday = PG_GETARG_INT32(2);
644  int32 hour = PG_GETARG_INT32(3);
645  int32 min = PG_GETARG_INT32(4);
646  float8 sec = PG_GETARG_FLOAT8(5);
647  Timestamp result;
648 
649  result = make_timestamp_internal(year, month, mday,
650  hour, min, sec);
651 
652  PG_RETURN_TIMESTAMP(result);
653 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:551
signed int int32
Definition: c.h:346
double float8
Definition: c.h:491
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39

◆ make_timestamp_internal()

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

Definition at line 551 of file timestamp.c.

References date2j(), DTK_DATE_M, ereport, errcode(), errmsg(), ERROR, HOURS_PER_DAY, IS_VALID_JULIAN, IS_VALID_TIMESTAMP, MINS_PER_HOUR, POSTGRES_EPOCH_JDATE, rint(), SECS_PER_MINUTE, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, USECS_PER_DAY, USECS_PER_SEC, and ValidateDate().

Referenced by make_timestamp(), make_timestamptz(), and make_timestamptz_at_timezone().

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

◆ make_timestamptz()

Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 659 of file timestamp.c.

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

660 {
661  int32 year = PG_GETARG_INT32(0);
662  int32 month = PG_GETARG_INT32(1);
663  int32 mday = PG_GETARG_INT32(2);
664  int32 hour = PG_GETARG_INT32(3);
665  int32 min = PG_GETARG_INT32(4);
666  float8 sec = PG_GETARG_FLOAT8(5);
667  Timestamp result;
668 
669  result = make_timestamp_internal(year, month, mday,
670  hour, min, sec);
671 
673 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:5231
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:551
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:346
double float8
Definition: c.h:491
int64 Timestamp
Definition: timestamp.h:38

◆ make_timestamptz_at_timezone()

Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

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

681 {
682  int32 year = PG_GETARG_INT32(0);
683  int32 month = PG_GETARG_INT32(1);
684  int32 mday = PG_GETARG_INT32(2);
685  int32 hour = PG_GETARG_INT32(3);
686  int32 min = PG_GETARG_INT32(4);
687  float8 sec = PG_GETARG_FLOAT8(5);
689  TimestampTz result;
691  struct pg_tm tt;
692  int tz;
693  fsec_t fsec;
694 
695  timestamp = make_timestamp_internal(year, month, mday,
696  hour, min, sec);
697 
698  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
699  ereport(ERROR,
700  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
701  errmsg("timestamp out of range")));
702 
703  tz = parse_sane_timezone(&tt, zone);
704 
705  result = dt2local(timestamp, -tz);
706 
707  if (!IS_VALID_TIMESTAMP(result))
708  ereport(ERROR,
709  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
710  errmsg("timestamp out of range")));
711 
712  PG_RETURN_TIMESTAMPTZ(result);
713 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:551
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1791
int errcode(int sqlerrcode)
Definition: elog.c:570
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:346
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:141
int64 Timestamp
Definition: timestamp.h:38
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Definition: zic.c:98
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:1985
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:467

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3246 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3247 {
3248  /* Args are float8 and Interval *, but leave them as generic Datum */
3249  Datum factor = PG_GETARG_DATUM(0);
3250  Datum span = PG_GETARG_DATUM(1);
3251 
3252  return DirectFunctionCall2(interval_mul, span, factor);
3253 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
uintptr_t Datum
Definition: postgres.h:367
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3169
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:619

◆ NonFiniteTimestampTzPart()

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

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

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

◆ now()

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2484 of file timestamp.c.

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

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

◆ parse_sane_timezone()

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

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

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

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1571 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

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

◆ pg_postmaster_start_time()

Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1565 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

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

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

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

2040 {
2041  Timestamp dt;
2042  struct pg_tm tt,
2043  *tm = &tt;
2044 
2045  GetEpochTime(tm);
2046  /* we don't bother to test for failure ... */
2047  tm2timestamp(tm, 0, NULL, &dt);
2048 
2049  return dt;
2050 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1887
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2017

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1553 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

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

◆ time2t()

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

Definition at line 1979 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

1980 {
1981  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
1982 }
#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 1703 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().

1704 {
1705  TimestampTz result;
1706 
1707  result = (TimestampTz) tm -
1709  result *= USECS_PER_SEC;
1710 
1711  return result;
1712 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:108
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ timeofday()

Datum timeofday ( PG_FUNCTION_ARGS  )

Definition at line