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

Go to the source code of this file.

Data Structures

struct  generate_series_timestamp_fctx
 
struct  generate_series_timestamptz_fctx
 

Macros

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

Functions

static TimeOffset time2t (const int hour, const int min, const int sec, const fsec_t fsec)
 
static Timestamp dt2local (Timestamp dt, int timezone)
 
static void AdjustIntervalForTypmod (Interval *interval, int32 typmod)
 
static TimestampTz timestamp2timestamptz (Timestamp timestamp)
 
static Timestamp timestamptz2timestamp (TimestampTz timestamp)
 
static int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
static char * anytimestamp_typmodout (bool istz, int32 typmod)
 
Datum timestamp_in (PG_FUNCTION_ARGS)
 
Datum timestamp_out (PG_FUNCTION_ARGS)
 
Datum timestamp_recv (PG_FUNCTION_ARGS)
 
Datum timestamp_send (PG_FUNCTION_ARGS)
 
Datum timestamptypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptypmodout (PG_FUNCTION_ARGS)
 
Datum timestamp_support (PG_FUNCTION_ARGS)
 
Datum timestamp_scale (PG_FUNCTION_ARGS)
 
bool AdjustTimestampForTypmodError (Timestamp *time, int32 typmod, bool *error)
 
void AdjustTimestampForTypmod (Timestamp *time, int32 typmod)
 
Datum timestamptz_in (PG_FUNCTION_ARGS)
 
static int parse_sane_timezone (struct pg_tm *tm, text *zone)
 
static Timestamp make_timestamp_internal (int year, int month, int day, int hour, int min, double sec)
 
Datum make_timestamp (PG_FUNCTION_ARGS)
 
Datum make_timestamptz (PG_FUNCTION_ARGS)
 
Datum make_timestamptz_at_timezone (PG_FUNCTION_ARGS)
 
Datum float8_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_out (PG_FUNCTION_ARGS)
 
Datum timestamptz_recv (PG_FUNCTION_ARGS)
 
Datum timestamptz_send (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodout (PG_FUNCTION_ARGS)
 
Datum timestamptz_scale (PG_FUNCTION_ARGS)
 
Datum interval_in (PG_FUNCTION_ARGS)
 
Datum interval_out (PG_FUNCTION_ARGS)
 
Datum interval_recv (PG_FUNCTION_ARGS)
 
Datum interval_send (PG_FUNCTION_ARGS)
 
Datum intervaltypmodin (PG_FUNCTION_ARGS)
 
Datum intervaltypmodout (PG_FUNCTION_ARGS)
 
static int intervaltypmodleastfield (int32 typmod)
 
Datum interval_support (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
Datum timeofday (PG_FUNCTION_ARGS)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
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_overflow (Timestamp timestamp, int *overflow)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ SAMESIGN

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

Definition at line 47 of file timestamp.c.

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

◆ TIMESTAMP_GT

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

Referenced by overlaps_timestamp().

◆ TIMESTAMP_LT

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

Referenced by overlaps_timestamp().

Function Documentation

◆ AdjustIntervalForTypmod()

static void AdjustIntervalForTypmod ( Interval interval,
int32  typmod 
)
static

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

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

◆ AdjustTimestampForTypmod()

void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)

Definition at line 392 of file timestamp.c.

References AdjustTimestampForTypmodError().

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

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

◆ AdjustTimestampForTypmodError()

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

Definition at line 337 of file timestamp.c.

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

Referenced by AdjustTimestampForTypmod().

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

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 102 of file timestamp.c.

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

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

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 81 of file timestamp.c.

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

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

◆ anytimestamp_typmodout()

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

Definition at line 124 of file timestamp.c.

References psprintf().

Referenced by timestamptypmodout(), and timestamptztypmodout().

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

◆ clock_timestamp()

Datum clock_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1550 of file timestamp.c.

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

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

◆ date2isoweek()

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

Definition at line 4317 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyear()

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

Definition at line 4372 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyearday()

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

Definition at line 4429 of file timestamp.c.

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

Referenced by DCH_to_char().

4430 {
4431  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4432 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4372
int isoweek2j(int year, int week)
Definition: timestamp.c:4266
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 1978 of file timestamp.c.

References USECS_PER_SEC.

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

1979 {
1980  dt -= (tz * USECS_PER_SEC);
1981  return dt;
1982 }
#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 1757 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1758 {
1759  TimeOffset time;
1760 
1761  time = jd;
1762 
1763  *hour = time / USECS_PER_HOUR;
1764  time -= (*hour) * USECS_PER_HOUR;
1765  *min = time / USECS_PER_MINUTE;
1766  time -= (*min) * USECS_PER_MINUTE;
1767  *sec = time / USECS_PER_SEC;
1768  *fsec = time - (*sec * USECS_PER_SEC);
1769 } /* 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 1527 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().

1528 {
1529  if (TIMESTAMP_IS_NOBEGIN(dt))
1530  strcpy(str, EARLY);
1531  else if (TIMESTAMP_IS_NOEND(dt))
1532  strcpy(str, LATE);
1533  else /* shouldn't happen */
1534  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1535 }
#define 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:214
#define EARLY
Definition: datetime.h:40

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 711 of file timestamp.c.

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

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

5394 {
5395  FuncCallContext *funcctx;
5397  Timestamp result;
5398 
5399  /* stuff done only on the first call of the function */
5400  if (SRF_IS_FIRSTCALL())
5401  {
5402  Timestamp start = PG_GETARG_TIMESTAMP(0);
5403  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5404  Interval *step = PG_GETARG_INTERVAL_P(2);
5405  MemoryContext oldcontext;
5406  Interval interval_zero;
5407 
5408  /* create a function context for cross-call persistence */
5409  funcctx = SRF_FIRSTCALL_INIT();
5410 
5411  /*
5412  * switch to memory context appropriate for multiple function calls
5413  */
5414  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5415 
5416  /* allocate memory for user context */
5419 
5420  /*
5421  * Use fctx to keep state from call to call. Seed current with the
5422  * original start value
5423  */
5424  fctx->current = start;
5425  fctx->finish = finish;
5426  fctx->step = *step;
5427 
5428  /* Determine sign of the interval */
5429  MemSet(&interval_zero, 0, sizeof(Interval));
5430  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5431 
5432  if (fctx->step_sign == 0)
5433  ereport(ERROR,
5434  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5435  errmsg("step size cannot equal zero")));
5436 
5437  funcctx->user_fctx = fctx;
5438  MemoryContextSwitchTo(oldcontext);
5439  }
5440 
5441  /* stuff done on every call of the function */
5442  funcctx = SRF_PERCALL_SETUP();
5443 
5444  /*
5445  * get the saved state and use current as the result for this iteration
5446  */
5447  fctx = funcctx->user_fctx;
5448  result = fctx->current;
5449 
5450  if (fctx->step_sign > 0 ?
5451  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5452  timestamp_cmp_internal(result, fctx->finish) >= 0)
5453  {
5454  /* increment current in preparation for next iteration */
5456  TimestampGetDatum(fctx->current),
5457  PointerGetDatum(&fctx->step)));
5458 
5459  /* do when there is more left to send */
5460  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5461  }
5462  else
5463  {
5464  /* do when there is no more left */
5465  SRF_RETURN_DONE(funcctx);
5466  }
5467 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2052
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#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:610
#define MemSet(start, val, len)
Definition: c.h:971
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2824
#define ERROR
Definition: elog.h:43
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:144
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

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

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

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1574 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_recv_resetslrucounter(), 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().

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

2011 {
2012  struct pg_tm *t0;
2013  pg_time_t epoch = 0;
2014 
2015  t0 = pg_gmtime(&epoch);
2016 
2017  if (t0 == NULL)
2018  elog(ERROR, "could not convert epoch to timestamp: %m");
2019 
2020  tm->tm_year = t0->tm_year;
2021  tm->tm_mon = t0->tm_mon;
2022  tm->tm_mday = t0->tm_mday;
2023  tm->tm_hour = t0->tm_hour;
2024  tm->tm_min = t0->tm_min;
2025  tm->tm_sec = t0->tm_sec;
2026 
2027  tm->tm_year += 1900;
2028  tm->tm_mon++;
2029 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1387
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
#define ERROR
Definition: elog.h: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:214
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 1592 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

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

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1606 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1607 {
1608  Timestamp ts;
1609 
1611  if (typmod >= 0)
1612  AdjustTimestampForTypmod(&ts, typmod);
1613  return ts;
1614 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:392
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5244
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 3363 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.

3364 {
3366  Interval *base = PG_GETARG_INTERVAL_P(1);
3367  Interval *offset = PG_GETARG_INTERVAL_P(2);
3368  bool sub = PG_GETARG_BOOL(3);
3369  bool less = PG_GETARG_BOOL(4);
3370  Interval *sum;
3371 
3372  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3373  ereport(ERROR,
3374  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3375  errmsg("invalid preceding or following size in window function")));
3376 
3377  /* We don't currently bother to avoid overflow hazards here */
3378  if (sub)
3380  IntervalPGetDatum(base),
3381  IntervalPGetDatum(offset)));
3382  else
3384  IntervalPGetDatum(base),
3385  IntervalPGetDatum(offset)));
3386 
3387  if (less)
3388  PG_RETURN_BOOL(interval_cmp_internal(val, sum) <= 0);
3389  else
3390  PG_RETURN_BOOL(interval_cmp_internal(val, sum) >= 0);
3391 }
#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:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3122
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3088
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
#define ereport(elevel,...)
Definition: elog.h:144
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360
int errmsg(const char *fmt,...)
Definition: elog.c:824
long val
Definition: informix.c:664
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2335

◆ in_range_timestamp_interval()

Datum in_range_timestamp_interval ( PG_FUNCTION_ARGS  )

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

3333 {
3335  Timestamp base = PG_GETARG_TIMESTAMP(1);
3336  Interval *offset = PG_GETARG_INTERVAL_P(2);
3337  bool sub = PG_GETARG_BOOL(3);
3338  bool less = PG_GETARG_BOOL(4);
3339  Timestamp sum;
3340 
3341  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3342  ereport(ERROR,
3343  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3344  errmsg("invalid preceding or following size in window function")));
3345 
3346  /* We don't currently bother to avoid overflow hazards here */
3347  if (sub)
3349  TimestampGetDatum(base),
3350  IntervalPGetDatum(offset)));
3351  else
3353  TimestampGetDatum(base),
3354  IntervalPGetDatum(offset)));
3355 
3356  if (less)
3357  PG_RETURN_BOOL(val <= sum);
3358  else
3359  PG_RETURN_BOOL(val >= sum);
3360 }
#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:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2903
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2824
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
long val
Definition: informix.c:664
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2335
#define DatumGetTimestamp(X)
Definition: timestamp.h:27

◆ in_range_timestamptz_interval()

Datum in_range_timestamptz_interval ( PG_FUNCTION_ARGS  )

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

3302 {
3305  Interval *offset = PG_GETARG_INTERVAL_P(2);
3306  bool sub = PG_GETARG_BOOL(3);
3307  bool less = PG_GETARG_BOOL(4);
3308  TimestampTz sum;
3309 
3310  if (int128_compare(interval_cmp_value(offset), int64_to_int128(0)) < 0)
3311  ereport(ERROR,
3312  (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
3313  errmsg("invalid preceding or following size in window function")));
3314 
3315  /* We don't currently bother to avoid overflow hazards here */
3316  if (sub)
3318  TimestampTzGetDatum(base),
3319  IntervalPGetDatum(offset)));
3320  else
3322  TimestampTzGetDatum(base),
3323  IntervalPGetDatum(offset)));
3324 
3325  if (less)
3326  PG_RETURN_BOOL(val <= sum);
3327  else
3328  PG_RETURN_BOOL(val >= sum);
3329 }
#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:610
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#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 PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
#define ereport(elevel,...)
Definition: elog.h:144
Datum timestamptz_mi_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3013
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2929
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
long val
Definition: informix.c:664
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2335

◆ interval2tm()

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

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

1929 {
1930  TimeOffset time;
1931  TimeOffset tfrac;
1932 
1933  tm->tm_year = span.month / MONTHS_PER_YEAR;
1934  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1935  tm->tm_mday = span.day;
1936  time = span.time;
1937 
1938  tfrac = time / USECS_PER_HOUR;
1939  time -= tfrac * USECS_PER_HOUR;
1940  tm->tm_hour = tfrac;
1941  if (!SAMESIGN(tm->tm_hour, tfrac))
1942  ereport(ERROR,
1943  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1944  errmsg("interval out of range")));
1945  tfrac = time / USECS_PER_MINUTE;
1946  time -= tfrac * USECS_PER_MINUTE;
1947  tm->tm_min = tfrac;
1948  tfrac = time / USECS_PER_SEC;
1949  *fsec = time - (tfrac * USECS_PER_SEC);
1950  tm->tm_sec = tfrac;
1951 
1952  return 0;
1953 }
#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:610
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 SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define ereport(elevel,...)
Definition: elog.h:144
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:824
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 3405 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.

3406 {
3407  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3409  Datum *transdatums;
3410  int ndatums;
3411  Interval sumX,
3412  N;
3413  Interval *newsum;
3414  ArrayType *result;
3415 
3416  deconstruct_array(transarray,
3417  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3418  &transdatums, NULL, &ndatums);
3419  if (ndatums != 2)
3420  elog(ERROR, "expected 2-element interval array");
3421 
3422  sumX = *(DatumGetIntervalP(transdatums[0]));
3423  N = *(DatumGetIntervalP(transdatums[1]));
3424 
3426  IntervalPGetDatum(&sumX),
3427  IntervalPGetDatum(newval)));
3428  N.time += 1;
3429 
3430  transdatums[0] = IntervalPGetDatum(newsum);
3431  transdatums[1] = IntervalPGetDatum(&N);
3432 
3433  result = construct_array(transdatums, 2,
3434  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3435 
3436  PG_RETURN_ARRAYTYPE_P(result);
3437 }
#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:3292
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3088
#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:3462
#define elog(elevel,...)
Definition: elog.h:214
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ interval_accum_inv()

Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

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

3490 {
3491  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3493  Datum *transdatums;
3494  int ndatums;
3495  Interval sumX,
3496  N;
3497  Interval *newsum;
3498  ArrayType *result;
3499 
3500  deconstruct_array(transarray,
3501  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3502  &transdatums, NULL, &ndatums);
3503  if (ndatums != 2)
3504  elog(ERROR, "expected 2-element interval array");
3505 
3506  sumX = *(DatumGetIntervalP(transdatums[0]));
3507  N = *(DatumGetIntervalP(transdatums[1]));
3508 
3510  IntervalPGetDatum(&sumX),
3511  IntervalPGetDatum(newval)));
3512  N.time -= 1;
3513 
3514  transdatums[0] = IntervalPGetDatum(newsum);
3515  transdatums[1] = IntervalPGetDatum(&N);
3516 
3517  result = construct_array(transdatums, 2,
3518  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3519 
3520  PG_RETURN_ARRAYTYPE_P(result);
3521 }
#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:3292
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3122
#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:3462
#define elog(elevel,...)
Definition: elog.h:214
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

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

3525 {
3526  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3527  Datum *transdatums;
3528  int ndatums;
3529  Interval sumX,
3530  N;
3531 
3532  deconstruct_array(transarray,
3533  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3534  &transdatums, NULL, &ndatums);
3535  if (ndatums != 2)
3536  elog(ERROR, "expected 2-element interval array");
3537 
3538  sumX = *(DatumGetIntervalP(transdatums[0]));
3539  N = *(DatumGetIntervalP(transdatums[1]));
3540 
3541  /* SQL defines AVG of no values to be NULL */
3542  if (N.time == 0)
3543  PG_RETURN_NULL();
3544 
3546  IntervalPGetDatum(&sumX),
3547  Float8GetDatum((double) N.time));
3548 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1710
#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:3249
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3462
#define elog(elevel,...)
Definition: elog.h:214
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2423 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp(), and leftmostvalue_interval().

2424 {
2425  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2426  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2427 
2428  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2429 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:353
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360

◆ interval_cmp_internal()

static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

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

2361 {
2362  INT128 span1 = interval_cmp_value(interval1);
2363  INT128 span2 = interval_cmp_value(interval2);
2364 
2365  return int128_compare(span1, span2);
2366 }
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:2335

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

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

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

◆ interval_combine()

Datum interval_combine ( PG_FUNCTION_ARGS  )

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

3441 {
3442  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3443  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3444  Datum *transdatums1;
3445  Datum *transdatums2;
3446  int ndatums1;
3447  int ndatums2;
3448  Interval sum1,
3449  N1;
3450  Interval sum2,
3451  N2;
3452 
3453  Interval *newsum;
3454  ArrayType *result;
3455 
3456  deconstruct_array(transarray1,
3457  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3458  &transdatums1, NULL, &ndatums1);
3459  if (ndatums1 != 2)
3460  elog(ERROR, "expected 2-element interval array");
3461 
3462  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3463  N1 = *(DatumGetIntervalP(transdatums1[1]));
3464 
3465  deconstruct_array(transarray2,
3466  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE,
3467  &transdatums2, NULL, &ndatums2);
3468  if (ndatums2 != 2)
3469  elog(ERROR, "expected 2-element interval array");
3470 
3471  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3472  N2 = *(DatumGetIntervalP(transdatums2[1]));
3473 
3475  IntervalPGetDatum(&sum1),
3476  IntervalPGetDatum(&sum2)));
3477  N1.time += N2.time;
3478 
3479  transdatums1[0] = IntervalPGetDatum(newsum);
3480  transdatums1[1] = IntervalPGetDatum(&N1);
3481 
3482  result = construct_array(transdatums1, 2,
3483  INTERVALOID, sizeof(Interval), false, TYPALIGN_DOUBLE);
3484 
3485  PG_RETURN_ARRAYTYPE_P(result);
3486 }
#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:3292
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3088
#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:3462
#define elog(elevel,...)
Definition: elog.h:214
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3249 of file timestamp.c.

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

Referenced by interval_avg().

3250 {
3251  Interval *span = PG_GETARG_INTERVAL_P(0);
3252  float8 factor = PG_GETARG_FLOAT8(1);
3253  double month_remainder_days,
3254  sec_remainder;
3255  int32 orig_month = span->month,
3256  orig_day = span->day;
3257  Interval *result;
3258 
3259  result = (Interval *) palloc(sizeof(Interval));
3260 
3261  if (factor == 0.0)
3262  ereport(ERROR,
3263  (errcode(ERRCODE_DIVISION_BY_ZERO),
3264  errmsg("division by zero")));
3265 
3266  result->month = (int32) (span->month / factor);
3267  result->day = (int32) (span->day / factor);
3268 
3269  /*
3270  * Fractional months full days into days. See comment in interval_mul().
3271  */
3272  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3273  month_remainder_days = TSROUND(month_remainder_days);
3274  sec_remainder = (orig_day / factor - result->day +
3275  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3276  sec_remainder = TSROUND(sec_remainder);
3277  if (Abs(sec_remainder) >= SECS_PER_DAY)
3278  {
3279  result->day += (int) (sec_remainder / SECS_PER_DAY);
3280  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3281  }
3282 
3283  /* cascade units down */
3284  result->day += (int32) month_remainder_days;
3285  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3286 
3287  PG_RETURN_INTERVAL_P(result);
3288 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:281
#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:610
signed int int32
Definition: c.h:355
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:926
#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
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define ereport(elevel,...)
Definition: elog.h:144
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ interval_eq()

Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2369 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

2370 {
2371  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2372  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2373 
2374  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2375 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 1999 of file timestamp.c.

References PG_RETURN_BOOL.

2000 {
2001  PG_RETURN_BOOL(true);
2002 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358

◆ interval_ge()

Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2414 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

2415 {
2416  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2417  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2418 
2419  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2420 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360

◆ interval_gt()

Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2396 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2397 {
2398  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2399  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2400 
2401  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2402 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2439 of file timestamp.c.

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

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

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2457 of file timestamp.c.

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

2458 {
2460  INT128 span = interval_cmp_value(interval);
2461  int64 span64;
2462 
2463  /* Same approach as interval_hash */
2464  span64 = int128_to_int64(span);
2465 
2467  PG_GETARG_DATUM(1));
2468 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:103
#define Int64GetDatumFast(X)
Definition: postgres.h:760
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2335

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 876 of file timestamp.c.

References AdjustIntervalForTypmod(), DateTimeParseError(), DecodeInterval(), DecodeISO8601Interval(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTK_DELTA, elog, ereport, errcode(), errmsg(), ERROR, INTERVAL_FULL_RANGE, INTERVAL_RANGE, MAXDATEFIELDS, palloc(), ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_INTERVAL_P, range(), generate_unaccent_rules::str, tm, tm2interval(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by check_timezone(), and flatten_set_variable_args().

877 {
878  char *str = PG_GETARG_CSTRING(0);
879 
880 #ifdef NOT_USED
881  Oid typelem = PG_GETARG_OID(1);
882 #endif
883  int32 typmod = PG_GETARG_INT32(2);
884  Interval *result;
885  fsec_t fsec;
886  struct pg_tm tt,
887  *tm = &tt;
888  int dtype;
889  int nf;
890  int range;
891  int dterr;
892  char *field[MAXDATEFIELDS];
893  int ftype[MAXDATEFIELDS];
894  char workbuf[256];
895 
896  tm->tm_year = 0;
897  tm->tm_mon = 0;
898  tm->tm_mday = 0;
899  tm->tm_hour = 0;
900  tm->tm_min = 0;
901  tm->tm_sec = 0;
902  fsec = 0;
903 
904  if (typmod >= 0)
905  range = INTERVAL_RANGE(typmod);
906  else
907  range = INTERVAL_FULL_RANGE;
908 
909  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
910  ftype, MAXDATEFIELDS, &nf);
911  if (dterr == 0)
912  dterr = DecodeInterval(field, ftype, nf, range,
913  &dtype, tm, &fsec);
914 
915  /* if those functions think it's a bad format, try ISO8601 style */
916  if (dterr == DTERR_BAD_FORMAT)
917  dterr = DecodeISO8601Interval(str,
918  &dtype, tm, &fsec);
919 
920  if (dterr != 0)
921  {
922  if (dterr == DTERR_FIELD_OVERFLOW)
923  dterr = DTERR_INTERVAL_OVERFLOW;
924  DateTimeParseError(dterr, str, "interval");
925  }
926 
927  result = (Interval *) palloc(sizeof(Interval));
928 
929  switch (dtype)
930  {
931  case DTK_DELTA:
932  if (tm2interval(tm, fsec, result) != 0)
933  ereport(ERROR,
934  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
935  errmsg("interval out of range")));
936  break;
937 
938  default:
939  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
940  dtype, str);
941  }
942 
943  AdjustIntervalForTypmod(result, typmod);
944 
945  PG_RETURN_INTERVAL_P(result);
946 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3723
#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:610
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:355
static struct pg_tm tm
Definition: localtime.c:102
#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:1324
int tm_mday
Definition: pgtime.h:30
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
int DecodeISO8601Interval(char *str, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3477
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define ereport(elevel,...)
Definition: elog.h:144
#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:824
#define elog(elevel,...)
Definition: elog.h:214
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3051
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
int tm_sec
Definition: pgtime.h:27
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:537
int tm_min
Definition: pgtime.h:28
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1956

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2784 of file timestamp.c.

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

2785 {
2786  Interval *span = PG_GETARG_INTERVAL_P(0);
2787  Interval *result;
2788  int32 wholemonth;
2789 
2790  result = (Interval *) palloc(sizeof(Interval));
2791  result->month = span->month;
2792  result->day = span->day;
2793  result->time = span->time;
2794 
2795  wholemonth = result->day / DAYS_PER_MONTH;
2796  result->day -= wholemonth * DAYS_PER_MONTH;
2797  result->month += wholemonth;
2798 
2799  if (result->month > 0 && result->day < 0)
2800  {
2801  result->day += DAYS_PER_MONTH;
2802  result->month--;
2803  }
2804  else if (result->month < 0 && result->day > 0)
2805  {
2806  result->day -= DAYS_PER_MONTH;
2807  result->month++;
2808  }
2809 
2810  PG_RETURN_INTERVAL_P(result);
2811 }
#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:355
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 2749 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().

2750 {
2751  Interval *span = PG_GETARG_INTERVAL_P(0);
2752  Interval *result;
2753  TimeOffset wholeday;
2754 
2755  result = (Interval *) palloc(sizeof(Interval));
2756  result->month = span->month;
2757  result->day = span->day;
2758  result->time = span->time;
2759 
2760  TMODULO(result->time, wholeday, USECS_PER_DAY);
2761  result->day += wholeday; /* could overflow... */
2762 
2763  if (result->day > 0 && result->time < 0)
2764  {
2765  result->time += USECS_PER_DAY;
2766  result->day--;
2767  }
2768  else if (result->day < 0 && result->time > 0)
2769  {
2770  result->time -= USECS_PER_DAY;
2771  result->day++;
2772  }
2773 
2774  PG_RETURN_INTERVAL_P(result);
2775 }
#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 2694 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.

2695 {
2696  Interval *span = PG_GETARG_INTERVAL_P(0);
2697  Interval *result;
2698  TimeOffset wholeday;
2699  int32 wholemonth;
2700 
2701  result = (Interval *) palloc(sizeof(Interval));
2702  result->month = span->month;
2703  result->day = span->day;
2704  result->time = span->time;
2705 
2706  TMODULO(result->time, wholeday, USECS_PER_DAY);
2707  result->day += wholeday; /* could overflow... */
2708 
2709  wholemonth = result->day / DAYS_PER_MONTH;
2710  result->day -= wholemonth * DAYS_PER_MONTH;
2711  result->month += wholemonth;
2712 
2713  if (result->month > 0 &&
2714  (result->day < 0 || (result->day == 0 && result->time < 0)))
2715  {
2716  result->day += DAYS_PER_MONTH;
2717  result->month--;
2718  }
2719  else if (result->month < 0 &&
2720  (result->day > 0 || (result->day == 0 && result->time > 0)))
2721  {
2722  result->day -= DAYS_PER_MONTH;
2723  result->month++;
2724  }
2725 
2726  if (result->day > 0 && result->time < 0)
2727  {
2728  result->time += USECS_PER_DAY;
2729  result->day--;
2730  }
2731  else if (result->day < 0 && result->time > 0)
2732  {
2733  result->time -= USECS_PER_DAY;
2734  result->day++;
2735  }
2736 
2737  PG_RETURN_INTERVAL_P(result);
2738 }
#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:355
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 3074 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3075 {
3076  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3077  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3078  Interval *result;
3079 
3080  if (interval_cmp_internal(interval1, interval2) > 0)
3081  result = interval1;
3082  else
3083  result = interval2;
3084  PG_RETURN_INTERVAL_P(result);
3085 }
#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:2360

◆ interval_le()

Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2405 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

2406 {
2407  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2408  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2409 
2410  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2411 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360

◆ interval_lt()

Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2387 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2388 {
2389  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2390  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2391 
2392  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2393 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

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

3123 {
3124  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3125  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3126  Interval *result;
3127 
3128  result = (Interval *) palloc(sizeof(Interval));
3129 
3130  result->month = span1->month - span2->month;
3131  /* overflow check copied from int4mi */
3132  if (!SAMESIGN(span1->month, span2->month) &&
3133  !SAMESIGN(result->month, span1->month))
3134  ereport(ERROR,
3135  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3136  errmsg("interval out of range")));
3137 
3138  result->day = span1->day - span2->day;
3139  if (!SAMESIGN(span1->day, span2->day) &&
3140  !SAMESIGN(result->day, span1->day))
3141  ereport(ERROR,
3142  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3143  errmsg("interval out of range")));
3144 
3145  result->time = span1->time - span2->time;
3146  if (!SAMESIGN(span1->time, span2->time) &&
3147  !SAMESIGN(result->time, span1->time))
3148  ereport(ERROR,
3149  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3150  errmsg("interval out of range")));
3151 
3152  PG_RETURN_INTERVAL_P(result);
3153 }
#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:610
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:144
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3162 of file timestamp.c.

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

Referenced by interval_lerp(), and mul_d_interval().

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

◆ interval_ne()

Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2378 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2379 {
2380  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2381  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2382 
2383  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2384 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2360

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

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

953 {
954  Interval *span = PG_GETARG_INTERVAL_P(0);
955  char *result;
956  struct pg_tm tt,
957  *tm = &tt;
958  fsec_t fsec;
959  char buf[MAXDATELEN + 1];
960 
961  if (interval2tm(*span, tm, &fsec) != 0)
962  elog(ERROR, "could not convert interval to tm");
963 
964  EncodeInterval(tm, fsec, IntervalStyle, buf);
965 
966  result = pstrdup(buf);
967  PG_RETURN_CSTRING(result);
968 }
#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:102
#define ERROR
Definition: elog.h:43
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1928
static char * buf
Definition: pg_test_fsync.c:67
int32 fsec_t
Definition: timestamp.h:41
void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str)
Definition: datetime.c:4200
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
#define elog(elevel,...)
Definition: elog.h:214

◆ interval_part()

Datum interval_part ( PG_FUNCTION_ARGS  )

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

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

◆ interval_pl()

Datum interval_pl ( PG_FUNCTION_ARGS  )

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

3089 {
3090  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3091  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3092  Interval *result;
3093 
3094  result = (Interval *) palloc(sizeof(Interval));
3095 
3096  result->month = span1->month + span2->month;
3097  /* overflow check copied from int4pl */
3098  if (SAMESIGN(span1->month, span2->month) &&
3099  !SAMESIGN(result->month, span1->month))
3100  ereport(ERROR,
3101  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3102  errmsg("interval out of range")));
3103 
3104  result->day = span1->day + span2->day;
3105  if (SAMESIGN(span1->day, span2->day) &&
3106  !SAMESIGN(result->day, span1->day))
3107  ereport(ERROR,
3108  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3109  errmsg("interval out of range")));
3110 
3111  result->time = span1->time + span2->time;
3112  if (SAMESIGN(span1->time, span2->time) &&
3113  !SAMESIGN(result->time, span1->time))
3114  ereport(ERROR,
3115  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3116  errmsg("interval out of range")));
3117 
3118  PG_RETURN_INTERVAL_P(result);
3119 }
#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:610
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:144
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

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

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

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

1306 {
1308  int32 typmod = PG_GETARG_INT32(1);
1309  Interval *result;
1310 
1311  result = palloc(sizeof(Interval));
1312  *result = *interval;
1313 
1314  AdjustIntervalForTypmod(result, typmod);
1315 
1316  PG_RETURN_INTERVAL_P(result);
1317 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:355
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1324
void * palloc(Size size)
Definition: mcxt.c:949

◆ interval_send()

Datum interval_send ( PG_FUNCTION_ARGS  )

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

1000 {
1003 
1004  pq_begintypsend(&buf);
1005  pq_sendint64(&buf, interval->time);
1006  pq_sendint32(&buf, interval->day);
1007  pq_sendint32(&buf, interval->month);
1009 }
#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:369
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:67
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3059 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3060 {
3061  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3062  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3063  Interval *result;
3064 
3065  /* use interval_cmp_internal to be sure this agrees with comparisons */
3066  if (interval_cmp_internal(interval1, interval2) < 0)
3067  result = interval1;
3068  else
3069  result = interval2;
3070  PG_RETURN_INTERVAL_P(result);
3071 }
#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:2360

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

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

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

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

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

4163 {
4164  text *units = PG_GETARG_TEXT_PP(0);
4166  Interval *result;
4167  int type,
4168  val;
4169  char *lowunits;
4170  fsec_t fsec;
4171  struct pg_tm tt,
4172  *tm = &tt;
4173 
4174  result = (Interval *) palloc(sizeof(Interval));
4175 
4176  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4177  VARSIZE_ANY_EXHDR(units),
4178  false);
4179 
4180  type = DecodeUnits(0, lowunits, &val);
4181 
4182  if (type == UNITS)
4183  {
4184  if (interval2tm(*interval, tm, &fsec) == 0)
4185  {
4186  switch (val)
4187  {
4188  case DTK_MILLENNIUM:
4189  /* caution: C division may have negative remainder */
4190  tm->tm_year = (tm->tm_year / 1000) * 1000;
4191  /* FALL THRU */
4192  case DTK_CENTURY:
4193  /* caution: C division may have negative remainder */
4194  tm->tm_year = (tm->tm_year / 100) * 100;
4195  /* FALL THRU */
4196  case DTK_DECADE:
4197  /* caution: C division may have negative remainder */
4198  tm->tm_year = (tm->tm_year / 10) * 10;
4199  /* FALL THRU */
4200  case DTK_YEAR:
4201  tm->tm_mon = 0;
4202  /* FALL THRU */
4203  case DTK_QUARTER:
4204  tm->tm_mon = 3 * (tm->tm_mon / 3);
4205  /* FALL THRU */
4206  case DTK_MONTH:
4207  tm->tm_mday = 0;
4208  /* FALL THRU */
4209  case DTK_DAY:
4210  tm->tm_hour = 0;
4211  /* FALL THRU */
4212  case DTK_HOUR:
4213  tm->tm_min = 0;
4214  /* FALL THRU */
4215  case DTK_MINUTE:
4216  tm->tm_sec = 0;
4217  /* FALL THRU */
4218  case DTK_SECOND:
4219  fsec = 0;
4220  break;
4221  case DTK_MILLISEC:
4222  fsec = (fsec / 1000) * 1000;
4223  break;
4224  case DTK_MICROSEC:
4225  break;
4226 
4227  default:
4228  if (val == DTK_WEEK)
4229  ereport(ERROR,
4230  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4231  errmsg("interval units \"%s\" not supported "
4232  "because months usually have fractional weeks",
4233  lowunits)));
4234  else
4235  ereport(ERROR,
4236  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4237  errmsg("interval units \"%s\" not supported",
4238  lowunits)));
4239  }
4240 
4241  if (tm2interval(tm, fsec, result) != 0)
4242  ereport(ERROR,
4243  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4244  errmsg("interval out of range")));
4245  }
4246  else
4247  elog(ERROR, "could not convert interval to tm");
4248  }
4249  else
4250  {
4251  ereport(ERROR,
4252  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4253  errmsg("interval units \"%s\" not recognized",
4254  lowunits)));
4255  }
4256 
4257  PG_RETURN_INTERVAL_P(result);
4258 }
#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:130
#define DTK_QUARTER
Definition: datetime.h:167
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:610
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3686
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static struct pg_tm tm
Definition: localtime.c:102
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:43
#define DTK_SECOND
Definition: datetime.h:161
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1928
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
#define ereport(elevel,...)
Definition: elog.h:144
#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:824
#define elog(elevel,...)
Definition: elog.h:214
Definition: c.h:555
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:664
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1956

◆ interval_um()

Datum interval_um ( PG_FUNCTION_ARGS  )

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

3031 {
3033  Interval *result;
3034 
3035  result = (Interval *) palloc(sizeof(Interval));
3036 
3037  result->time = -interval->time;
3038  /* overflow check copied from int4um */
3039  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3040  ereport(ERROR,
3041  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3042  errmsg("interval out of range")));
3043  result->day = -interval->day;
3044  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3045  ereport(ERROR,
3046  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3047  errmsg("interval out of range")));
3048  result->month = -interval->month;
3049  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3050  ereport(ERROR,
3051  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3052  errmsg("interval out of range")));
3053 
3054  PG_RETURN_INTERVAL_P(result);
3055 }
#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:610
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
#define ereport(elevel,...)
Definition: elog.h:144
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

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

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

◆ intervaltypmodleastfield()

static int intervaltypmodleastfield ( int32  typmod)
static

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

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

◆ intervaltypmodout()

Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

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

1104 {
1105  int32 typmod = PG_GETARG_INT32(0);
1106  char *res = (char *) palloc(64);
1107  int fields;
1108  int precision;
1109  const char *fieldstr;
1110 
1111  if (typmod < 0)
1112  {
1113  *res = '\0';
1114  PG_RETURN_CSTRING(res);
1115  }
1116 
1117  fields = INTERVAL_RANGE(typmod);
1118  precision = INTERVAL_PRECISION(typmod);
1119 
1120  switch (fields)
1121  {
1122  case INTERVAL_MASK(YEAR):
1123  fieldstr = " year";
1124  break;
1125  case INTERVAL_MASK(MONTH):
1126  fieldstr = " month";
1127  break;
1128  case INTERVAL_MASK(DAY):
1129  fieldstr = " day";
1130  break;
1131  case INTERVAL_MASK(HOUR):
1132  fieldstr = " hour";
1133  break;
1134  case INTERVAL_MASK(MINUTE):
1135  fieldstr = " minute";
1136  break;
1137  case INTERVAL_MASK(SECOND):
1138  fieldstr = " second";
1139  break;
1141  fieldstr = " year to month";
1142  break;
1144  fieldstr = " day to hour";
1145  break;
1147  fieldstr = " day to minute";
1148  break;
1150  fieldstr = " day to second";
1151  break;
1153  fieldstr = " hour to minute";
1154  break;
1156  fieldstr = " hour to second";
1157  break;
1159  fieldstr = " minute to second";
1160  break;
1161  case INTERVAL_FULL_RANGE:
1162  fieldstr = "";
1163  break;
1164  default:
1165  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1166  fieldstr = "";
1167  break;
1168  }
1169 
1170  if (precision != INTERVAL_FULL_PRECISION)
1171  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1172  else
1173  snprintf(res, 64, "%s", fieldstr);
1174 
1175  PG_RETURN_CSTRING(res);
1176 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
signed int int32
Definition: c.h:355
#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:361
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
#define HOUR
Definition: datetime.h:101
#define snprintf
Definition: port.h:193

◆ isoweek2date()

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

Definition at line 4286 of file timestamp.c.

References isoweek2j(), and j2date().

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

4287 {
4288  j2date(isoweek2j(*year, woy), year, mon, mday);
4289 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4266

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4266 of file timestamp.c.

References date2j(), and j2day().

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

4267 {
4268  int day0,
4269  day4;
4270 
4271  /* fourth day of current year */
4272  day4 = date2j(year, 1, 4);
4273 
4274  /* day0 == offset to first day of week (Monday) */
4275  day0 = j2day(day4 - 1);
4276 
4277  return ((week - 1) * 7) + (day4 - day0);
4278 }
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ isoweekdate2date()

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

Definition at line 4299 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4300 {
4301  int jday;
4302 
4303  jday = isoweek2j(*year, isoweek);
4304  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4305  if (wday > 1)
4306  jday += wday - 2;
4307  else
4308  jday += 6;
4309  j2date(jday, year, mon, mday);
4310 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4266

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1491 of file timestamp.c.

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

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

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 630 of file timestamp.c.

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

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

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

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

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

◆ make_timestamptz()

Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 650 of file timestamp.c.

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

651 {
652  int32 year = PG_GETARG_INT32(0);
653  int32 month = PG_GETARG_INT32(1);
654  int32 mday = PG_GETARG_INT32(2);
655  int32 hour = PG_GETARG_INT32(3);
656  int32 min = PG_GETARG_INT32(4);
657  float8 sec = PG_GETARG_FLOAT8(5);
658  Timestamp result;
659 
660  result = make_timestamp_internal(year, month, mday,
661  hour, min, sec);
662 
664 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:281
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:5227
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:552
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:355
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 671 of file timestamp.c.

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

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

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3239 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3240 {
3241  /* Args are float8 and Interval *, but leave them as generic Datum */
3242  Datum factor = PG_GETARG_DATUM(0);
3243  Datum span = PG_GETARG_DATUM(1);
3244 
3245  return DirectFunctionCall2(interval_mul, span, factor);
3246 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uintptr_t Datum
Definition: postgres.h:367
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3162
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:626

◆ NonFiniteTimestampTzPart()

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

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

4448 {
4449  if ((type != UNITS) && (type != RESERV))
4450  {
4451  if (isTz)
4452  ereport(ERROR,
4453  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4454  errmsg("timestamp with time zone units \"%s\" not recognized",
4455  lowunits)));
4456  else
4457  ereport(ERROR,
4458  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4459  errmsg("timestamp units \"%s\" not recognized",
4460  lowunits)));
4461  }
4462 
4463  switch (unit)
4464  {
4465  /* Oscillating units */
4466  case DTK_MICROSEC:
4467  case DTK_MILLISEC:
4468  case DTK_SECOND:
4469  case DTK_MINUTE:
4470  case DTK_HOUR:
4471  case DTK_DAY:
4472  case DTK_MONTH:
4473  case DTK_QUARTER:
4474  case DTK_WEEK:
4475  case DTK_DOW:
4476  case DTK_ISODOW:
4477  case DTK_DOY:
4478  case DTK_TZ:
4479  case DTK_TZ_MINUTE:
4480  case DTK_TZ_HOUR:
4481  return 0.0;
4482 
4483  /* Monotonically-increasing units */
4484  case DTK_YEAR:
4485  case DTK_DECADE:
4486  case DTK_CENTURY:
4487  case DTK_MILLENNIUM:
4488  case DTK_JULIAN:
4489  case DTK_ISOYEAR:
4490  case DTK_EPOCH:
4491  if (isNegative)
4492  return -get_float8_infinity();
4493  else
4494  return get_float8_infinity();
4495 
4496  default:
4497  if (isTz)
4498  ereport(ERROR,
4499  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4500  errmsg("timestamp with time zone units \"%s\" not supported",
4501  lowunits)));
4502  else
4503  ereport(ERROR,
4504  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4505  errmsg("timestamp units \"%s\" not supported",
4506  lowunits)));
4507  return 0.0; /* keep compiler quiet */
4508  }
4509 }
#define DTK_TZ_HOUR
Definition: datetime.h:178
#define DTK_CENTURY
Definition: datetime.h:170
#define UNITS
Definition: datetime.h:108
static float8 get_float8_infinity(void)
Definition: float.h:93
#define DTK_JULIAN
Definition: datetime.h:174
#define DTK_WEEK
Definition: datetime.h:165
#define DTK_YEAR
Definition: datetime.h:168
#define DTK_QUARTER
Definition: datetime.h:167
#define DTK_MILLENNIUM
Definition: datetime.h:171
int errcode(int sqlerrcode)
Definition: elog.c:610
#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 DTK_MINUTE
Definition: datetime.h:162
#define DTK_DOW
Definition: datetime.h:176
#define DTK_MICROSEC
Definition: datetime.h:173
#define ereport(elevel,...)
Definition: elog.h:144
#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:824

◆ now()

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2477 of file timestamp.c.

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

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

◆ parse_sane_timezone()

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

Definition at line 468 of file timestamp.c.

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

Referenced by make_timestamptz_at_timezone().

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

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1562 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

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

◆ pg_postmaster_start_time()

Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1556 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

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

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

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

2033 {
2034  Timestamp dt;
2035  struct pg_tm tt,
2036  *tm = &tt;
2037 
2038  GetEpochTime(tm);
2039  /* we don't bother to test for failure ... */
2040  tm2timestamp(tm, 0, NULL, &dt);
2041 
2042  return dt;
2043 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1880
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2010

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1544 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1545 {
1547 }
#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 1972 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

1973 {
1974  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
1975 }
#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 1694 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().

1695 {
1696  TimestampTz result;
1697 
1698  result = (TimestampTz) tm -
1700  result *= USECS_PER_SEC;
1701 
1702  return result;
1703 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:102
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ timeofday()

Datum timeofday ( PG_FUNCTION_ARGS  )

Definition at line 1620 of file timestamp.c.

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

1621 {
1622  struct timeval tp;
1623  char templ[128];
1624  char buf[128];
1625  pg_time_t tt;
1626 
1627  gettimeofday(&tp, NULL);
1628  tt = (pg_time_t) tp.tv_sec;
1629  pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
1631  snprintf(buf, sizeof(buf), templ, tp.tv_usec);
1632 
1634 }
size_t pg_strftime(char *s, size_t max, const char *format, const struct pg_tm *tm)
Definition: strftime.c:128
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104
int64 pg_time_t
Definition: pgtime.h:23
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
static char * buf
Definition: pg_test_fsync.c:67
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:370