PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
timestamp.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <sys/time.h>
#include "access/hash.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
Include dependency graph for timestamp.c:

Go to the source code of this file.

Data Structures

struct  generate_series_timestamp_fctx
 
struct  generate_series_timestamptz_fctx
 

Macros

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

Functions

static TimeOffset time2t (const int hour, const int min, const int sec, const fsec_t fsec)
 
static Timestamp dt2local (Timestamp dt, int timezone)
 
static void AdjustTimestampForTypmod (Timestamp *time, int32 typmod)
 
static void AdjustIntervalForTypmod (Interval *interval, int32 typmod)
 
static TimestampTz timestamp2timestamptz (Timestamp timestamp)
 
static Timestamp timestamptz2timestamp (TimestampTz timestamp)
 
static int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
static char * anytimestamp_typmodout (bool istz, int32 typmod)
 
Datum timestamp_in (PG_FUNCTION_ARGS)
 
Datum timestamp_out (PG_FUNCTION_ARGS)
 
Datum timestamp_recv (PG_FUNCTION_ARGS)
 
Datum timestamp_send (PG_FUNCTION_ARGS)
 
Datum timestamptypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptypmodout (PG_FUNCTION_ARGS)
 
Datum timestamp_transform (PG_FUNCTION_ARGS)
 
Datum timestamp_scale (PG_FUNCTION_ARGS)
 
Datum timestamptz_in (PG_FUNCTION_ARGS)
 
static int parse_sane_timezone (struct pg_tm *tm, text *zone)
 
static Timestamp make_timestamp_internal (int year, int month, int day, int hour, int min, double sec)
 
Datum make_timestamp (PG_FUNCTION_ARGS)
 
Datum make_timestamptz (PG_FUNCTION_ARGS)
 
Datum make_timestamptz_at_timezone (PG_FUNCTION_ARGS)
 
Datum float8_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_out (PG_FUNCTION_ARGS)
 
Datum timestamptz_recv (PG_FUNCTION_ARGS)
 
Datum timestamptz_send (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodout (PG_FUNCTION_ARGS)
 
Datum timestamptz_scale (PG_FUNCTION_ARGS)
 
Datum interval_in (PG_FUNCTION_ARGS)
 
Datum interval_out (PG_FUNCTION_ARGS)
 
Datum interval_recv (PG_FUNCTION_ARGS)
 
Datum interval_send (PG_FUNCTION_ARGS)
 
Datum intervaltypmodin (PG_FUNCTION_ARGS)
 
Datum intervaltypmodout (PG_FUNCTION_ARGS)
 
static int intervaltypmodleastfield (int32 typmod)
 
Datum interval_transform (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
int64 GetCurrentIntegerTimestamp (void)
 
TimestampTz IntegerTimestampToTimestampTz (int64 timestamp)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const char * timestamptz_to_str (TimestampTz t)
 
void dt2time (Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
int tm2timestamp (struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
 
int interval2tm (Interval span, struct pg_tm *tm, fsec_t *fsec)
 
int tm2interval (struct pg_tm *tm, fsec_t fsec, Interval *span)
 
Datum timestamp_finite (PG_FUNCTION_ARGS)
 
Datum interval_finite (PG_FUNCTION_ARGS)
 
void GetEpochTime (struct pg_tm *tm)
 
Timestamp SetEpochTimestamp (void)
 
int timestamp_cmp_internal (Timestamp dt1, Timestamp dt2)
 
Datum timestamp_eq (PG_FUNCTION_ARGS)
 
Datum timestamp_ne (PG_FUNCTION_ARGS)
 
Datum timestamp_lt (PG_FUNCTION_ARGS)
 
Datum timestamp_gt (PG_FUNCTION_ARGS)
 
Datum timestamp_le (PG_FUNCTION_ARGS)
 
Datum timestamp_ge (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp (PG_FUNCTION_ARGS)
 
static int timestamp_fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum timestamp_sortsupport (PG_FUNCTION_ARGS)
 
Datum timestamp_hash (PG_FUNCTION_ARGS)
 
Datum timestamp_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 TimeOffset 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 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 interval_accum (PG_FUNCTION_ARGS)
 
Datum interval_combine (PG_FUNCTION_ARGS)
 
Datum interval_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (PG_FUNCTION_ARGS)
 
Datum timestamp_age (PG_FUNCTION_ARGS)
 
Datum timestamptz_age (PG_FUNCTION_ARGS)
 
Datum timestamp_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc (PG_FUNCTION_ARGS)
 
Datum interval_trunc (PG_FUNCTION_ARGS)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
static float8 NonFiniteTimestampTzPart (int type, int unit, char *lowunits, bool isNegative, bool isTz)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum timestamp_zone_transform (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone_transform (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
Datum timestamp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

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

Definition at line 45 of file timestamp.c.

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

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

Referenced by overlaps_timestamp().

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

Referenced by overlaps_timestamp().

Function Documentation

static void AdjustIntervalForTypmod ( Interval interval,
int32  typmod 
)
static

Definition at line 1386 of file timestamp.c.

References Interval::day, DAY, elog, ereport, errcode(), errmsg(), ERROR, HOUR, INT64CONST, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_PRECISION, INTERVAL_RANGE, MAX_INTERVAL_PRECISION, MINUTE, Interval::month, MONTH, MONTHS_PER_YEAR, range(), rint(), SECOND, SECS_PER_HOUR, SECS_PER_MINUTE, Interval::time, USECS_PER_HOUR, USECS_PER_MINUTE, and YEAR.

Referenced by interval_in(), interval_recv(), and interval_scale().

1387 {
1388 #ifdef HAVE_INT64_TIMESTAMP
1389  static const int64 IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
1390  INT64CONST(1000000),
1391  INT64CONST(100000),
1392  INT64CONST(10000),
1393  INT64CONST(1000),
1394  INT64CONST(100),
1395  INT64CONST(10),
1396  INT64CONST(1)
1397  };
1398 
1399  static const int64 IntervalOffsets[MAX_INTERVAL_PRECISION + 1] = {
1400  INT64CONST(500000),
1401  INT64CONST(50000),
1402  INT64CONST(5000),
1403  INT64CONST(500),
1404  INT64CONST(50),
1405  INT64CONST(5),
1406  INT64CONST(0)
1407  };
1408 #else
1409  static const double IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
1410  1,
1411  10,
1412  100,
1413  1000,
1414  10000,
1415  100000,
1416  1000000
1417  };
1418 #endif
1419 
1420  /*
1421  * Unspecified range and precision? Then not necessary to adjust. Setting
1422  * typmod to -1 is the convention for all data types.
1423  */
1424  if (typmod >= 0)
1425  {
1426  int range = INTERVAL_RANGE(typmod);
1427  int precision = INTERVAL_PRECISION(typmod);
1428 
1429  /*
1430  * Our interpretation of intervals with a limited set of fields is
1431  * that fields to the right of the last one specified are zeroed out,
1432  * but those to the left of it remain valid. Thus for example there
1433  * is no operational difference between INTERVAL YEAR TO MONTH and
1434  * INTERVAL MONTH. In some cases we could meaningfully enforce that
1435  * higher-order fields are zero; for example INTERVAL DAY could reject
1436  * nonzero "month" field. However that seems a bit pointless when we
1437  * can't do it consistently. (We cannot enforce a range limit on the
1438  * highest expected field, since we do not have any equivalent of
1439  * SQL's <interval leading field precision>.) If we ever decide to
1440  * revisit this, interval_transform will likely require adjusting.
1441  *
1442  * Note: before PG 8.4 we interpreted a limited set of fields as
1443  * actually causing a "modulo" operation on a given value, potentially
1444  * losing high-order as well as low-order information. But there is
1445  * no support for such behavior in the standard, and it seems fairly
1446  * undesirable on data consistency grounds anyway. Now we only
1447  * perform truncation or rounding of low-order fields.
1448  */
1449  if (range == INTERVAL_FULL_RANGE)
1450  {
1451  /* Do nothing... */
1452  }
1453  else if (range == INTERVAL_MASK(YEAR))
1454  {
1455  interval->month = (interval->month / MONTHS_PER_YEAR) * MONTHS_PER_YEAR;
1456  interval->day = 0;
1457  interval->time = 0;
1458  }
1459  else if (range == INTERVAL_MASK(MONTH))
1460  {
1461  interval->day = 0;
1462  interval->time = 0;
1463  }
1464  /* YEAR TO MONTH */
1465  else if (range == (INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH)))
1466  {
1467  interval->day = 0;
1468  interval->time = 0;
1469  }
1470  else if (range == INTERVAL_MASK(DAY))
1471  {
1472  interval->time = 0;
1473  }
1474  else if (range == INTERVAL_MASK(HOUR))
1475  {
1476 #ifdef HAVE_INT64_TIMESTAMP
1477  interval->time = (interval->time / USECS_PER_HOUR) *
1479 #else
1480  interval->time = ((int) (interval->time / SECS_PER_HOUR)) * (double) SECS_PER_HOUR;
1481 #endif
1482  }
1483  else if (range == INTERVAL_MASK(MINUTE))
1484  {
1485 #ifdef HAVE_INT64_TIMESTAMP
1486  interval->time = (interval->time / USECS_PER_MINUTE) *
1488 #else
1489  interval->time = ((int) (interval->time / SECS_PER_MINUTE)) * (double) SECS_PER_MINUTE;
1490 #endif
1491  }
1492  else if (range == INTERVAL_MASK(SECOND))
1493  {
1494  /* fractional-second rounding will be dealt with below */
1495  }
1496  /* DAY TO HOUR */
1497  else if (range == (INTERVAL_MASK(DAY) |
1498  INTERVAL_MASK(HOUR)))
1499  {
1500 #ifdef HAVE_INT64_TIMESTAMP
1501  interval->time = (interval->time / USECS_PER_HOUR) *
1503 #else
1504  interval->time = ((int) (interval->time / SECS_PER_HOUR)) * (double) SECS_PER_HOUR;
1505 #endif
1506  }
1507  /* DAY TO MINUTE */
1508  else if (range == (INTERVAL_MASK(DAY) |
1509  INTERVAL_MASK(HOUR) |
1511  {
1512 #ifdef HAVE_INT64_TIMESTAMP
1513  interval->time = (interval->time / USECS_PER_MINUTE) *
1515 #else
1516  interval->time = ((int) (interval->time / SECS_PER_MINUTE)) * (double) SECS_PER_MINUTE;
1517 #endif
1518  }
1519  /* DAY TO SECOND */
1520  else if (range == (INTERVAL_MASK(DAY) |
1521  INTERVAL_MASK(HOUR) |
1524  {
1525  /* fractional-second rounding will be dealt with below */
1526  }
1527  /* HOUR TO MINUTE */
1528  else if (range == (INTERVAL_MASK(HOUR) |
1530  {
1531 #ifdef HAVE_INT64_TIMESTAMP
1532  interval->time = (interval->time / USECS_PER_MINUTE) *
1534 #else
1535  interval->time = ((int) (interval->time / SECS_PER_MINUTE)) * (double) SECS_PER_MINUTE;
1536 #endif
1537  }
1538  /* HOUR TO SECOND */
1539  else if (range == (INTERVAL_MASK(HOUR) |
1542  {
1543  /* fractional-second rounding will be dealt with below */
1544  }
1545  /* MINUTE TO SECOND */
1546  else if (range == (INTERVAL_MASK(MINUTE) |
1548  {
1549  /* fractional-second rounding will be dealt with below */
1550  }
1551  else
1552  elog(ERROR, "unrecognized interval typmod: %d", typmod);
1553 
1554  /* Need to adjust sub-second precision? */
1555  if (precision != INTERVAL_FULL_PRECISION)
1556  {
1557  if (precision < 0 || precision > MAX_INTERVAL_PRECISION)
1558  ereport(ERROR,
1559  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1560  errmsg("interval(%d) precision must be between %d and %d",
1561  precision, 0, MAX_INTERVAL_PRECISION)));
1562 
1563  /*
1564  * Note: this round-to-nearest code is not completely consistent
1565  * about rounding values that are exactly halfway between integral
1566  * values. On most platforms, rint() will implement
1567  * round-to-nearest-even, but the integer code always rounds up
1568  * (away from zero). Is it worth trying to be consistent?
1569  */
1570 #ifdef HAVE_INT64_TIMESTAMP
1571  if (interval->time >= INT64CONST(0))
1572  {
1573  interval->time = ((interval->time +
1574  IntervalOffsets[precision]) /
1575  IntervalScales[precision]) *
1576  IntervalScales[precision];
1577  }
1578  else
1579  {
1580  interval->time = -(((-interval->time +
1581  IntervalOffsets[precision]) /
1582  IntervalScales[precision]) *
1583  IntervalScales[precision]);
1584  }
1585 #else
1586  interval->time = rint(((double) interval->time) *
1587  IntervalScales[precision]) /
1588  IntervalScales[precision];
1589 #endif
1590  }
1591  }
1592 }
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
#define USECS_PER_MINUTE
Definition: timestamp.h:105
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:69
int32 day
Definition: timestamp.h:60
#define MONTHS_PER_YEAR
Definition: timestamp.h:81
#define ERROR
Definition: elog.h:43
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:66
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:71
#define SECS_PER_MINUTE
Definition: timestamp.h:100
#define USECS_PER_HOUR
Definition: timestamp.h:104
TimeOffset time
Definition: timestamp.h:58
#define MINUTE
Definition: datetime.h:102
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:99
#define MONTH
Definition: datetime.h:92
int32 month
Definition: timestamp.h:61
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:74
#define INTERVAL_RANGE(t)
Definition: timestamp.h:75
#define INT64CONST(x)
Definition: c.h:307
#define INTERVAL_MASK(b)
Definition: timestamp.h:66
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
#define HOUR
Definition: datetime.h:101
static void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)
static

Definition at line 349 of file timestamp.c.

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

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

350 {
351 #ifdef HAVE_INT64_TIMESTAMP
352  static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
353  INT64CONST(1000000),
354  INT64CONST(100000),
355  INT64CONST(10000),
356  INT64CONST(1000),
357  INT64CONST(100),
358  INT64CONST(10),
359  INT64CONST(1)
360  };
361 
362  static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
363  INT64CONST(500000),
364  INT64CONST(50000),
365  INT64CONST(5000),
366  INT64CONST(500),
367  INT64CONST(50),
368  INT64CONST(5),
369  INT64CONST(0)
370  };
371 #else
372  static const double TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
373  1,
374  10,
375  100,
376  1000,
377  10000,
378  100000,
379  1000000
380  };
381 #endif
382 
383  if (!TIMESTAMP_NOT_FINITE(*time)
384  && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
385  {
386  if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
387  ereport(ERROR,
388  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
389  errmsg("timestamp(%d) precision must be between %d and %d",
390  typmod, 0, MAX_TIMESTAMP_PRECISION)));
391 
392  /*
393  * Note: this round-to-nearest code is not completely consistent about
394  * rounding values that are exactly halfway between integral values.
395  * On most platforms, rint() will implement round-to-nearest-even, but
396  * the integer code always rounds up (away from zero). Is it worth
397  * trying to be consistent?
398  */
399 #ifdef HAVE_INT64_TIMESTAMP
400  if (*time >= INT64CONST(0))
401  {
402  *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
403  TimestampScales[typmod];
404  }
405  else
406  {
407  *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
408  * TimestampScales[typmod]);
409  }
410 #else
411  *time = rint((double) *time * TimestampScales[typmod]) / TimestampScales[typmod];
412 #endif
413  }
414 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:144
#define ERROR
Definition: elog.h:43
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:65
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define INT64CONST(x)
Definition: c.h:307
int errmsg(const char *fmt,...)
Definition: elog.c:797
int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 101 of file timestamp.c.

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

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

Definition at line 80 of file timestamp.c.

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

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

Definition at line 123 of file timestamp.c.

References psprintf().

Referenced by timestamptypmodout(), and timestamptztypmodout().

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

Definition at line 1663 of file timestamp.c.

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1664 {
1666 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:60
int date2isoweek ( int  year,
int  mon,
int  mday 
)

Definition at line 4433 of file timestamp.c.

References date2j(), and j2day().

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

4434 {
4435  float8 result;
4436  int day0,
4437  day4,
4438  dayn;
4439 
4440  /* current day */
4441  dayn = date2j(year, mon, mday);
4442 
4443  /* fourth day of current year */
4444  day4 = date2j(year, 1, 4);
4445 
4446  /* day0 == offset to first day of week (Monday) */
4447  day0 = j2day(day4 - 1);
4448 
4449  /*
4450  * We need the first week containing a Thursday, otherwise this day falls
4451  * into the previous year for purposes of counting weeks
4452  */
4453  if (dayn < day4 - day0)
4454  {
4455  day4 = date2j(year - 1, 1, 4);
4456 
4457  /* day0 == offset to first day of week (Monday) */
4458  day0 = j2day(day4 - 1);
4459  }
4460 
4461  result = (dayn - (day4 - day0)) / 7 + 1;
4462 
4463  /*
4464  * Sometimes the last few days in a year will fall into the first week of
4465  * the next year, so check for this.
4466  */
4467  if (result >= 52)
4468  {
4469  day4 = date2j(year + 1, 1, 4);
4470 
4471  /* day0 == offset to first day of week (Monday) */
4472  day0 = j2day(day4 - 1);
4473 
4474  if (dayn >= day4 - day0)
4475  result = (dayn - (day4 - day0)) / 7 + 1;
4476  }
4477 
4478  return (int) result;
4479 }
double float8
Definition: c.h:377
int j2day(int date)
Definition: datetime.c:357
int date2j(int y, int m, int d)
Definition: datetime.c:297
int date2isoyear ( int  year,
int  mon,
int  mday 
)

Definition at line 4487 of file timestamp.c.

References date2j(), and j2day().

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

4488 {
4489  float8 result;
4490  int day0,
4491  day4,
4492  dayn;
4493 
4494  /* current day */
4495  dayn = date2j(year, mon, mday);
4496 
4497  /* fourth day of current year */
4498  day4 = date2j(year, 1, 4);
4499 
4500  /* day0 == offset to first day of week (Monday) */
4501  day0 = j2day(day4 - 1);
4502 
4503  /*
4504  * We need the first week containing a Thursday, otherwise this day falls
4505  * into the previous year for purposes of counting weeks
4506  */
4507  if (dayn < day4 - day0)
4508  {
4509  day4 = date2j(year - 1, 1, 4);
4510 
4511  /* day0 == offset to first day of week (Monday) */
4512  day0 = j2day(day4 - 1);
4513 
4514  year--;
4515  }
4516 
4517  result = (dayn - (day4 - day0)) / 7 + 1;
4518 
4519  /*
4520  * Sometimes the last few days in a year will fall into the first week of
4521  * the next year, so check for this.
4522  */
4523  if (result >= 52)
4524  {
4525  day4 = date2j(year + 1, 1, 4);
4526 
4527  /* day0 == offset to first day of week (Monday) */
4528  day0 = j2day(day4 - 1);
4529 
4530  if (dayn >= day4 - day0)
4531  year++;
4532  }
4533 
4534  return year;
4535 }
double float8
Definition: c.h:377
int j2day(int date)
Definition: datetime.c:357
int date2j(int y, int m, int d)
Definition: datetime.c:297
int date2isoyearday ( int  year,
int  mon,
int  mday 
)

Definition at line 4544 of file timestamp.c.

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

Referenced by DCH_to_char().

4545 {
4546  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4547 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4487
int isoweek2j(int year, int week)
Definition: timestamp.c:4382
int date2j(int y, int m, int d)
Definition: datetime.c:297
static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 2218 of file timestamp.c.

References USECS_PER_SEC.

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

2219 {
2220 #ifdef HAVE_INT64_TIMESTAMP
2221  dt -= (tz * USECS_PER_SEC);
2222 #else
2223  dt -= tz;
2224 #endif
2225  return dt;
2226 }
#define USECS_PER_SEC
Definition: timestamp.h:106
void dt2time ( Timestamp  jd,
int *  hour,
int *  min,
int *  sec,
fsec_t fsec 
)

Definition at line 1914 of file timestamp.c.

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

1915 {
1916  TimeOffset time;
1917 
1918  time = jd;
1919 
1920 #ifdef HAVE_INT64_TIMESTAMP
1921  *hour = time / USECS_PER_HOUR;
1922  time -= (*hour) * USECS_PER_HOUR;
1923  *min = time / USECS_PER_MINUTE;
1924  time -= (*min) * USECS_PER_MINUTE;
1925  *sec = time / USECS_PER_SEC;
1926  *fsec = time - (*sec * USECS_PER_SEC);
1927 #else
1928  *hour = time / SECS_PER_HOUR;
1929  time -= (*hour) * SECS_PER_HOUR;
1930  *min = time / SECS_PER_MINUTE;
1931  time -= (*min) * SECS_PER_MINUTE;
1932  *sec = time;
1933  *fsec = time - *sec;
1934 #endif
1935 } /* dt2time() */
#define USECS_PER_SEC
Definition: timestamp.h:106
#define USECS_PER_MINUTE
Definition: timestamp.h:105
#define SECS_PER_MINUTE
Definition: timestamp.h:100
#define USECS_PER_HOUR
Definition: timestamp.h:104
double TimeOffset
Definition: timestamp.h:52
#define SECS_PER_HOUR
Definition: timestamp.h:99
void EncodeSpecialTimestamp ( Timestamp  dt,
char *  str 
)

Definition at line 1640 of file timestamp.c.

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

Referenced by datum_to_json(), datum_to_jsonb(), PGTYPEStimestamp_to_asc(), timestamp_out(), timestamptz_out(), and timestamptz_to_str().

1641 {
1642  if (TIMESTAMP_IS_NOBEGIN(dt))
1643  strcpy(str, EARLY);
1644  else if (TIMESTAMP_IS_NOEND(dt))
1645  strcpy(str, LATE);
1646  else /* shouldn't happen */
1647  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1648 }
#define LATE
Definition: datetime.h:41
#define ERROR
Definition: elog.h:43
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:142
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:137
#define elog
Definition: elog.h:219
#define EARLY
Definition: datetime.h:40
Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 754 of file timestamp.c.

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

755 {
756  float8 seconds = PG_GETARG_FLOAT8(0);
757  TimestampTz result;
758 
759  /* Deal with NaN and infinite inputs ... */
760  if (isnan(seconds))
761  ereport(ERROR,
762  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
763  errmsg("timestamp cannot be NaN")));
764 
765  if (isinf(seconds))
766  {
767  if (seconds < 0)
768  TIMESTAMP_NOBEGIN(result);
769  else
770  TIMESTAMP_NOEND(result);
771  }
772  else
773  {
774  /* Out of range? */
775  if (seconds <
777  || seconds >=
779  ereport(ERROR,
780  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
781  errmsg("timestamp out of range: \"%g\"", seconds)));
782 
783  /* Convert UNIX epoch to Postgres epoch */
785 
786 #ifdef HAVE_INT64_TIMESTAMP
787  seconds = rint(seconds * USECS_PER_SEC);
788  result = (int64) seconds;
789 #else
790  result = seconds;
791 #endif
792 
793  /* Recheck in case roundoff produces something just out of range */
794  if (!IS_VALID_TIMESTAMP(result))
795  ereport(ERROR,
796  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
797  errmsg("timestamp out of range: \"%g\"",
798  PG_GETARG_FLOAT8(0))));
799  }
800 
801  PG_RETURN_TIMESTAMP(result);
802 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:139
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:208
#define USECS_PER_SEC
Definition: timestamp.h:106
int errcode(int sqlerrcode)
Definition: elog.c:575
double TimestampTz
Definition: timestamp.h:51
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:377
#define SECS_PER_DAY
Definition: timestamp.h:98
int isinf(double x)
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:134
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:230
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:59
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:184
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:185
#define DATETIME_MIN_JULIAN
Definition: timestamp.h:203
Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

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

5563 {
5564  FuncCallContext *funcctx;
5566  Timestamp result;
5567 
5568  /* stuff done only on the first call of the function */
5569  if (SRF_IS_FIRSTCALL())
5570  {
5571  Timestamp start = PG_GETARG_TIMESTAMP(0);
5572  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5573  Interval *step = PG_GETARG_INTERVAL_P(2);
5574  MemoryContext oldcontext;
5575  Interval interval_zero;
5576 
5577  /* create a function context for cross-call persistence */
5578  funcctx = SRF_FIRSTCALL_INIT();
5579 
5580  /*
5581  * switch to memory context appropriate for multiple function calls
5582  */
5583  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5584 
5585  /* allocate memory for user context */
5588 
5589  /*
5590  * Use fctx to keep state from call to call. Seed current with the
5591  * original start value
5592  */
5593  fctx->current = start;
5594  fctx->finish = finish;
5595  fctx->step = *step;
5596 
5597  /* Determine sign of the interval */
5598  MemSet(&interval_zero, 0, sizeof(Interval));
5599  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5600 
5601  if (fctx->step_sign == 0)
5602  ereport(ERROR,
5603  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5604  errmsg("step size cannot equal zero")));
5605 
5606  funcctx->user_fctx = fctx;
5607  MemoryContextSwitchTo(oldcontext);
5608  }
5609 
5610  /* stuff done on every call of the function */
5611  funcctx = SRF_PERCALL_SETUP();
5612 
5613  /*
5614  * get the saved state and use current as the result for this iteration
5615  */
5616  fctx = funcctx->user_fctx;
5617  result = fctx->current;
5618 
5619  if (fctx->step_sign > 0 ?
5620  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5621  timestamp_cmp_internal(result, fctx->finish) >= 0)
5622  {
5623  /* increment current in preparation for next iteration */
5624  fctx->current = DatumGetTimestamp(
5626  TimestampGetDatum(fctx->current),
5627  PointerGetDatum(&fctx->step)));
5628 
5629  /* do when there is more left to send */
5630  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5631  }
5632  else
5633  {
5634  /* do when there is no more left */
5635  SRF_RETURN_DONE(funcctx);
5636  }
5637 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2295
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:55
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:852
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3091
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define TimestampGetDatum(X)
Definition: timestamp.h:51
double Timestamp
Definition: timestamp.h:50
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define DatumGetTimestamp(X)
Definition: timestamp.h:47
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

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

5644 {
5645  FuncCallContext *funcctx;
5647  TimestampTz result;
5648 
5649  /* stuff done only on the first call of the function */
5650  if (SRF_IS_FIRSTCALL())
5651  {
5653  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5654  Interval *step = PG_GETARG_INTERVAL_P(2);
5655  MemoryContext oldcontext;
5656  Interval interval_zero;
5657 
5658  /* create a function context for cross-call persistence */
5659  funcctx = SRF_FIRSTCALL_INIT();
5660 
5661  /*
5662  * switch to memory context appropriate for multiple function calls
5663  */
5664  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5665 
5666  /* allocate memory for user context */
5669 
5670  /*
5671  * Use fctx to keep state from call to call. Seed current with the
5672  * original start value
5673  */
5674  fctx->current = start;
5675  fctx->finish = finish;
5676  fctx->step = *step;
5677 
5678  /* Determine sign of the interval */
5679  MemSet(&interval_zero, 0, sizeof(Interval));
5680  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5681 
5682  if (fctx->step_sign == 0)
5683  ereport(ERROR,
5684  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5685  errmsg("step size cannot equal zero")));
5686 
5687  funcctx->user_fctx = fctx;
5688  MemoryContextSwitchTo(oldcontext);
5689  }
5690 
5691  /* stuff done on every call of the function */
5692  funcctx = SRF_PERCALL_SETUP();
5693 
5694  /*
5695  * get the saved state and use current as the result for this iteration
5696  */
5697  fctx = funcctx->user_fctx;
5698  result = fctx->current;
5699 
5700  if (fctx->step_sign > 0 ?
5701  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5702  timestamp_cmp_internal(result, fctx->finish) >= 0)
5703  {
5704  /* increment current in preparation for next iteration */
5705  fctx->current = DatumGetTimestampTz(
5708  PointerGetDatum(&fctx->step)));
5709 
5710  /* do when there is more left to send */
5711  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5712  }
5713  else
5714  {
5715  /* do when there is no more left */
5716  SRF_RETURN_DONE(funcctx);
5717  }
5718 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2295
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:852
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
double TimestampTz
Definition: timestamp.h:51
#define ERROR
Definition: elog.h:43
#define TimestampTzGetDatum(X)
Definition: timestamp.h:52
#define DatumGetTimestampTz(X)
Definition: timestamp.h:48
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3196
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:56
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
int64 GetCurrentIntegerTimestamp ( void  )

Definition at line 1715 of file timestamp.c.

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

Referenced by GetSnapshotCurrentTimestamp(), perform_base_backup(), throttle(), WalSndKeepalive(), WalSndWriteData(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1716 {
1717  int64 result;
1718  struct timeval tp;
1719 
1720  gettimeofday(&tp, NULL);
1721 
1722  result = (int64) tp.tv_sec -
1724 
1725  result = (result * USECS_PER_SEC) + tp.tv_usec;
1726 
1727  return result;
1728 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define USECS_PER_SEC
Definition: timestamp.h:106
#define SECS_PER_DAY
Definition: timestamp.h:98
#define NULL
Definition: c.h:226
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:184
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:185
TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1687 of file timestamp.c.

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

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

1688 {
1689  TimestampTz result;
1690  struct timeval tp;
1691 
1692  gettimeofday(&tp, NULL);
1693 
1694  result = (TimestampTz) tp.tv_sec -
1696 
1697 #ifdef HAVE_INT64_TIMESTAMP
1698  result = (result * USECS_PER_SEC) + tp.tv_usec;
1699 #else
1700  result = result + (tp.tv_usec / 1000000.0);
1701 #endif
1702 
1703  return result;
1704 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define USECS_PER_SEC
Definition: timestamp.h:106
double TimestampTz
Definition: timestamp.h:51
#define SECS_PER_DAY
Definition: timestamp.h:98
#define NULL
Definition: c.h:226
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:184
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:185
void GetEpochTime ( struct pg_tm tm)

Definition at line 2254 of file timestamp.c.

References epoch, pg_gmtime(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by date_in(), PGTYPESdate_from_asc(), and SetEpochTimestamp().

2255 {
2256  struct pg_tm *t0;
2257  pg_time_t epoch = 0;
2258 
2259  t0 = pg_gmtime(&epoch);
2260 
2261  tm->tm_year = t0->tm_year;
2262  tm->tm_mon = t0->tm_mon;
2263  tm->tm_mday = t0->tm_mday;
2264  tm->tm_hour = t0->tm_hour;
2265  tm->tm_min = t0->tm_min;
2266  tm->tm_sec = t0->tm_sec;
2267 
2268  tm->tm_year += 1900;
2269  tm->tm_mon++;
2270 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1295
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1754 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1755 {
1756  TimestampTz ts;
1757 
1759  if (typmod >= 0)
1760  AdjustTimestampForTypmod(&ts, typmod);
1761  return ts;
1762 }
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:349
double TimestampTz
Definition: timestamp.h:51
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:707
Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1768 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1769 {
1770  Timestamp ts;
1771 
1773  if (typmod >= 0)
1774  AdjustTimestampForTypmod(&ts, typmod);
1775  return ts;
1776 }
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:349
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5409
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:707
double Timestamp
Definition: timestamp.h:50
TimestampTz IntegerTimestampToTimestampTz ( int64  timestamp)

Definition at line 1739 of file timestamp.c.

References USECS_PER_SEC.

Referenced by ApplyLoop(), and XLogWalRcvProcessMsg().

1740 {
1741  TimestampTz result;
1742 
1743  result = timestamp / USECS_PER_SEC;
1744  result += (timestamp % USECS_PER_SEC) / 1000000.0;
1745 
1746  return result;
1747 }
#define USECS_PER_SEC
Definition: timestamp.h:106
double TimestampTz
Definition: timestamp.h:51
double timestamp
int interval2tm ( Interval  span,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 2140 of file timestamp.c.

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, MONTHS_PER_YEAR, SAMESIGN, SECS_PER_HOUR, SECS_PER_MINUTE, 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, TMODULO, TSROUND, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

2141 {
2142  TimeOffset time;
2143  TimeOffset tfrac;
2144 
2145  tm->tm_year = span.month / MONTHS_PER_YEAR;
2146  tm->tm_mon = span.month % MONTHS_PER_YEAR;
2147  tm->tm_mday = span.day;
2148  time = span.time;
2149 
2150 #ifdef HAVE_INT64_TIMESTAMP
2151  tfrac = time / USECS_PER_HOUR;
2152  time -= tfrac * USECS_PER_HOUR;
2153  tm->tm_hour = tfrac;
2154  if (!SAMESIGN(tm->tm_hour, tfrac))
2155  ereport(ERROR,
2156  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2157  errmsg("interval out of range")));
2158  tfrac = time / USECS_PER_MINUTE;
2159  time -= tfrac * USECS_PER_MINUTE;
2160  tm->tm_min = tfrac;
2161  tfrac = time / USECS_PER_SEC;
2162  *fsec = time - (tfrac * USECS_PER_SEC);
2163  tm->tm_sec = tfrac;
2164 #else
2165 recalc:
2166  TMODULO(time, tfrac, (double) SECS_PER_HOUR);
2167  tm->tm_hour = tfrac; /* could overflow ... */
2168  TMODULO(time, tfrac, (double) SECS_PER_MINUTE);
2169  tm->tm_min = tfrac;
2170  TMODULO(time, tfrac, 1.0);
2171  tm->tm_sec = tfrac;
2172  time = TSROUND(time);
2173  /* roundoff may need to propagate to higher-order fields */
2174  if (time >= 1.0)
2175  {
2176  time = ceil(span.time);
2177  goto recalc;
2178  }
2179  *fsec = time;
2180 #endif
2181 
2182  return 0;
2183 }
#define USECS_PER_SEC
Definition: timestamp.h:106
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:105
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:60
#define MONTHS_PER_YEAR
Definition: timestamp.h:81
#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:100
#define USECS_PER_HOUR
Definition: timestamp.h:104
TimeOffset time
Definition: timestamp.h:58
double TimeOffset
Definition: timestamp.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:99
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:61
#define TSROUND(j)
Definition: timestamp.h:73
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
#define TMODULO(t, q, u)
Definition: datetime.h:258
Datum interval_accum ( PG_FUNCTION_ARGS  )

Definition at line 3582 of file timestamp.c.

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

3583 {
3584  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3586  Datum *transdatums;
3587  int ndatums;
3588  Interval sumX,
3589  N;
3590  Interval *newsum;
3591  ArrayType *result;
3592 
3593  deconstruct_array(transarray,
3594  INTERVALOID, sizeof(Interval), false, 'd',
3595  &transdatums, NULL, &ndatums);
3596  if (ndatums != 2)
3597  elog(ERROR, "expected 2-element interval array");
3598 
3599  sumX = *(DatumGetIntervalP(transdatums[0]));
3600  N = *(DatumGetIntervalP(transdatums[1]));
3601 
3603  IntervalPGetDatum(&sumX),
3604  IntervalPGetDatum(newval)));
3605  N.time += 1;
3606 
3607  transdatums[0] = IntervalPGetDatum(newsum);
3608  transdatums[1] = IntervalPGetDatum(&N);
3609 
3610  result = construct_array(transdatums, 2,
3611  INTERVALOID, sizeof(Interval), false, 'd');
3612 
3613  PG_RETURN_ARRAYTYPE_P(result);
3614 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define DatumGetIntervalP(X)
Definition: timestamp.h:49
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3355
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:53
#define INTERVALOID
Definition: pg_type.h:517
TimeOffset time
Definition: timestamp.h:58
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3666 of file timestamp.c.

References construct_array(), DatumGetIntervalP, deconstruct_array(), DirectFunctionCall2, elog, ERROR, interval_mi(), INTERVALOID, IntervalPGetDatum, newval, NULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_INTERVAL_P, PG_RETURN_ARRAYTYPE_P, and Interval::time.

3667 {
3668  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3670  Datum *transdatums;
3671  int ndatums;
3672  Interval sumX,
3673  N;
3674  Interval *newsum;
3675  ArrayType *result;
3676 
3677  deconstruct_array(transarray,
3678  INTERVALOID, sizeof(Interval), false, 'd',
3679  &transdatums, NULL, &ndatums);
3680  if (ndatums != 2)
3681  elog(ERROR, "expected 2-element interval array");
3682 
3683  sumX = *(DatumGetIntervalP(transdatums[0]));
3684  N = *(DatumGetIntervalP(transdatums[1]));
3685 
3687  IntervalPGetDatum(&sumX),
3688  IntervalPGetDatum(newval)));
3689  N.time -= 1;
3690 
3691  transdatums[0] = IntervalPGetDatum(newsum);
3692  transdatums[1] = IntervalPGetDatum(&N);
3693 
3694  result = construct_array(transdatums, 2,
3695  INTERVALOID, sizeof(Interval), false, 'd');
3696 
3697  PG_RETURN_ARRAYTYPE_P(result);
3698 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define DatumGetIntervalP(X)
Definition: timestamp.h:49
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3389
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:53
#define INTERVALOID
Definition: pg_type.h:517
TimeOffset time
Definition: timestamp.h:58
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 3701 of file timestamp.c.

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

3702 {
3703  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3704  Datum *transdatums;
3705  int ndatums;
3706  Interval sumX,
3707  N;
3708 
3709  deconstruct_array(transarray,
3710  INTERVALOID, sizeof(Interval), false, 'd',
3711  &transdatums, NULL, &ndatums);
3712  if (ndatums != 2)
3713  elog(ERROR, "expected 2-element interval array");
3714 
3715  sumX = *(DatumGetIntervalP(transdatums[0]));
3716  N = *(DatumGetIntervalP(transdatums[1]));
3717 
3718  /* SQL defines AVG of no values to be NULL */
3719  if (N.time == 0)
3720  PG_RETURN_NULL();
3721 
3723  IntervalPGetDatum(&sumX),
3724  Float8GetDatum((double) N.time));
3725 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:49
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:2126
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:53
#define INTERVALOID
Definition: pg_type.h:517
TimeOffset time
Definition: timestamp.h:58
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3520
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2685 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp().

2686 {
2687  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2688  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2689 
2690  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2691 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

Definition at line 2622 of file timestamp.c.

References interval_cmp_value().

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

2623 {
2624  TimeOffset span1 = interval_cmp_value(interval1);
2625  TimeOffset span2 = interval_cmp_value(interval2);
2626 
2627  return ((span1 < span2) ? -1 : (span1 > span2) ? 1 : 0);
2628 }
static TimeOffset interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2604
double TimeOffset
Definition: timestamp.h:52
static TimeOffset interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2604 of file timestamp.c.

References Interval::day, DAYS_PER_MONTH, HOURS_PER_DAY, INT64CONST, Interval::month, SECS_PER_DAY, SECS_PER_HOUR, Interval::time, USECS_PER_DAY, and USECS_PER_HOUR.

Referenced by interval_cmp_internal(), and interval_hash().

2605 {
2606  TimeOffset span;
2607 
2608  span = interval->time;
2609 
2610 #ifdef HAVE_INT64_TIMESTAMP
2611  span += interval->month * INT64CONST(30) * USECS_PER_DAY;
2612  span += interval->day * INT64CONST(24) * USECS_PER_HOUR;
2613 #else
2614  span += interval->month * ((double) DAYS_PER_MONTH * SECS_PER_DAY);
2615  span += interval->day * ((double) HOURS_PER_DAY * SECS_PER_HOUR);
2616 #endif
2617 
2618  return span;
2619 }
int32 day
Definition: timestamp.h:60
#define SECS_PER_DAY
Definition: timestamp.h:98
#define HOURS_PER_DAY
Definition: timestamp.h:90
#define USECS_PER_HOUR
Definition: timestamp.h:104
TimeOffset time
Definition: timestamp.h:58
double TimeOffset
Definition: timestamp.h:52
#define USECS_PER_DAY
Definition: timestamp.h:103
#define SECS_PER_HOUR
Definition: timestamp.h:99
int32 month
Definition: timestamp.h:61
#define DAYS_PER_MONTH
Definition: timestamp.h:89
#define INT64CONST(x)
Definition: c.h:307
Datum interval_combine ( PG_FUNCTION_ARGS  )

Definition at line 3617 of file timestamp.c.

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

3618 {
3619  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3620  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3621  Datum *transdatums1;
3622  Datum *transdatums2;
3623  int ndatums1;
3624  int ndatums2;
3625  Interval sum1,
3626  N1;
3627  Interval sum2,
3628  N2;
3629 
3630  Interval *newsum;
3631  ArrayType *result;
3632 
3633  deconstruct_array(transarray1,
3634  INTERVALOID, sizeof(Interval), false, 'd',
3635  &transdatums1, NULL, &ndatums1);
3636  if (ndatums1 != 2)
3637  elog(ERROR, "expected 2-element interval array");
3638 
3639  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3640  N1 = *(DatumGetIntervalP(transdatums1[1]));
3641 
3642  deconstruct_array(transarray2,
3643  INTERVALOID, sizeof(Interval), false, 'd',
3644  &transdatums2, NULL, &ndatums2);
3645  if (ndatums2 != 2)
3646  elog(ERROR, "expected 2-element interval array");
3647 
3648  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3649  N2 = *(DatumGetIntervalP(transdatums2[1]));
3650 
3652  IntervalPGetDatum(&sum1),
3653  IntervalPGetDatum(&sum2)));
3654  N1.time += N2.time;
3655 
3656  transdatums1[0] = IntervalPGetDatum(newsum);
3657  transdatums1[1] = IntervalPGetDatum(&N1);
3658 
3659  result = construct_array(transdatums1, 2,
3660  INTERVALOID, sizeof(Interval), false, 'd');
3661 
3662  PG_RETURN_ARRAYTYPE_P(result);
3663 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:49
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3355
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:53
#define INTERVALOID
Definition: pg_type.h:517
TimeOffset time
Definition: timestamp.h:58
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3520 of file timestamp.c.

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

Referenced by interval_avg().

3521 {
3522  Interval *span = PG_GETARG_INTERVAL_P(0);
3523  float8 factor = PG_GETARG_FLOAT8(1);
3524  double month_remainder_days,
3525  sec_remainder;
3526  int32 orig_month = span->month,
3527  orig_day = span->day;
3528  Interval *result;
3529 
3530  result = (Interval *) palloc(sizeof(Interval));
3531 
3532  if (factor == 0.0)
3533  ereport(ERROR,
3534  (errcode(ERRCODE_DIVISION_BY_ZERO),
3535  errmsg("division by zero")));
3536 
3537  result->month = (int32) (span->month / factor);
3538  result->day = (int32) (span->day / factor);
3539 
3540  /*
3541  * Fractional months full days into days. See comment in interval_mul().
3542  */
3543  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3544  month_remainder_days = TSROUND(month_remainder_days);
3545  sec_remainder = (orig_day / factor - result->day +
3546  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3547  sec_remainder = TSROUND(sec_remainder);
3548  if (Abs(sec_remainder) >= SECS_PER_DAY)
3549  {
3550  result->day += (int) (sec_remainder / SECS_PER_DAY);
3551  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3552  }
3553 
3554  /* cascade units down */
3555  result->day += (int32) month_remainder_days;
3556 #ifdef HAVE_INT64_TIMESTAMP
3557  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3558 #else
3559  /* See TSROUND comment in interval_mul(). */
3560  result->time = span->time / factor + sec_remainder;
3561 #endif
3562 
3563  PG_RETURN_INTERVAL_P(result);
3564 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
#define USECS_PER_SEC
Definition: timestamp.h:106
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:253
int32 day
Definition: timestamp.h:60
#define Abs(x)
Definition: c.h:807
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:377
#define SECS_PER_DAY
Definition: timestamp.h:98
TimeOffset time
Definition: timestamp.h:58
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:61
#define DAYS_PER_MONTH
Definition: timestamp.h:89
#define TSROUND(j)
Definition: timestamp.h:73
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2631 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

2632 {
2633  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2634  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2635 
2636  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2637 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 2243 of file timestamp.c.

References PG_RETURN_BOOL.

2244 {
2245  PG_RETURN_BOOL(true);
2246 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2676 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

2677 {
2678  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2679  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2680 
2681  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2682 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2658 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2659 {
2660  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2661  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2662 
2663  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2664 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2701 of file timestamp.c.

References DirectFunctionCall1, Float8GetDatumFast, hashfloat8(), hashint8(), Int64GetDatumFast, interval_cmp_value(), and PG_GETARG_INTERVAL_P.

2702 {
2704  TimeOffset span = interval_cmp_value(interval);
2705 
2706 #ifdef HAVE_INT64_TIMESTAMP
2708 #else
2710 #endif
2711 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
static TimeOffset interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2604
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:62
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
double TimeOffset
Definition: timestamp.h:52
Datum hashfloat8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:120
#define Int64GetDatumFast(X)
Definition: postgres.h:783
#define Float8GetDatumFast(X)
Definition: postgres.h:784
Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 934 of file timestamp.c.

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

935 {
936  char *str = PG_GETARG_CSTRING(0);
937 
938 #ifdef NOT_USED
939  Oid typelem = PG_GETARG_OID(1);
940 #endif
941  int32 typmod = PG_GETARG_INT32(2);
942  Interval *result;
943  fsec_t fsec;
944  struct pg_tm tt,
945  *tm = &tt;
946  int dtype;
947  int nf;
948  int range;
949  int dterr;
950  char *field[MAXDATEFIELDS];
951  int ftype[MAXDATEFIELDS];
952  char workbuf[256];
953 
954  tm->tm_year = 0;
955  tm->tm_mon = 0;
956  tm->tm_mday = 0;
957  tm->tm_hour = 0;
958  tm->tm_min = 0;
959  tm->tm_sec = 0;
960  fsec = 0;
961 
962  if (typmod >= 0)
963  range = INTERVAL_RANGE(typmod);
964  else
965  range = INTERVAL_FULL_RANGE;
966 
967  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
968  ftype, MAXDATEFIELDS, &nf);
969  if (dterr == 0)
970  dterr = DecodeInterval(field, ftype, nf, range,
971  &dtype, tm, &fsec);
972 
973  /* if those functions think it's a bad format, try ISO8601 style */
974  if (dterr == DTERR_BAD_FORMAT)
975  dterr = DecodeISO8601Interval(str,
976  &dtype, tm, &fsec);
977 
978  if (dterr != 0)
979  {
980  if (dterr == DTERR_FIELD_OVERFLOW)
981  dterr = DTERR_INTERVAL_OVERFLOW;
982  DateTimeParseError(dterr, str, "interval");
983  }
984 
985  result = (Interval *) palloc(sizeof(Interval));
986 
987  switch (dtype)
988  {
989  case DTK_DELTA:
990  if (tm2interval(tm, fsec, result) != 0)
991  ereport(ERROR,
992  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
993  errmsg("interval out of range")));
994  break;
995 
996  case DTK_INVALID:
997  ereport(ERROR,
998  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
999  errmsg("date/time value \"%s\" is no longer supported", str)));
1000  break;
1001 
1002  default:
1003  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
1004  dtype, str);
1005  }
1006 
1007  AdjustIntervalForTypmod(result, typmod);
1008 
1009  PG_RETURN_INTERVAL_P(result);
1010 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:290
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3871
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
double fsec_t
Definition: timestamp.h:53
#define DTK_DELTA
Definition: datetime.h:162
int tm_hour
Definition: pgtime.h:29
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:69
#define DTK_INVALID
Definition: datetime.h:151
signed int int32
Definition: c.h:253
static struct pg_tm tm
Definition: localtime.c:103
#define ERROR
Definition: elog.h:43
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:291
#define DTERR_INTERVAL_OVERFLOW
Definition: datetime.h:293
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1386
int tm_mday
Definition: pgtime.h:30
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
int tm_mon
Definition: pgtime.h:31
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
#define ereport(elevel, rest)
Definition: elog.h:122
int DecodeISO8601Interval(char *str, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3627
#define INTERVAL_RANGE(t)
Definition: timestamp.h:75
#define MAXDATEFIELDS
Definition: datetime.h:205
int tm_year
Definition: pgtime.h:32
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3185
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:233
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:626
int tm_min
Definition: pgtime.h:28
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:2186
Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 3051 of file timestamp.c.

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

3052 {
3053  Interval *span = PG_GETARG_INTERVAL_P(0);
3054  Interval *result;
3055  int32 wholemonth;
3056 
3057  result = (Interval *) palloc(sizeof(Interval));
3058  result->month = span->month;
3059  result->day = span->day;
3060  result->time = span->time;
3061 
3062  wholemonth = result->day / DAYS_PER_MONTH;
3063  result->day -= wholemonth * DAYS_PER_MONTH;
3064  result->month += wholemonth;
3065 
3066  if (result->month > 0 && result->day < 0)
3067  {
3068  result->day += DAYS_PER_MONTH;
3069  result->month--;
3070  }
3071  else if (result->month < 0 && result->day > 0)
3072  {
3073  result->day -= DAYS_PER_MONTH;
3074  result->month++;
3075  }
3076 
3077  PG_RETURN_INTERVAL_P(result);
3078 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
signed int int32
Definition: c.h:253
int32 day
Definition: timestamp.h:60
TimeOffset time
Definition: timestamp.h:58
int32 month
Definition: timestamp.h:61
#define DAYS_PER_MONTH
Definition: timestamp.h:89
void * palloc(Size size)
Definition: mcxt.c:891
Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

Definition at line 3004 of file timestamp.c.

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

Referenced by timestamp_mi().

3005 {
3006  Interval *span = PG_GETARG_INTERVAL_P(0);
3007  Interval *result;
3008  TimeOffset wholeday;
3009 
3010  result = (Interval *) palloc(sizeof(Interval));
3011  result->month = span->month;
3012  result->day = span->day;
3013  result->time = span->time;
3014 
3015 #ifdef HAVE_INT64_TIMESTAMP
3016  TMODULO(result->time, wholeday, USECS_PER_DAY);
3017 #else
3018  TMODULO(result->time, wholeday, (double) SECS_PER_DAY);
3019 #endif
3020  result->day += wholeday; /* could overflow... */
3021 
3022  if (result->day > 0 && result->time < 0)
3023  {
3024 #ifdef HAVE_INT64_TIMESTAMP
3025  result->time += USECS_PER_DAY;
3026 #else
3027  result->time += (double) SECS_PER_DAY;
3028 #endif
3029  result->day--;
3030  }
3031  else if (result->day < 0 && result->time > 0)
3032  {
3033 #ifdef HAVE_INT64_TIMESTAMP
3034  result->time -= USECS_PER_DAY;
3035 #else
3036  result->time -= (double) SECS_PER_DAY;
3037 #endif
3038  result->day++;
3039  }
3040 
3041  PG_RETURN_INTERVAL_P(result);
3042 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
int32 day
Definition: timestamp.h:60
#define SECS_PER_DAY
Definition: timestamp.h:98
TimeOffset time
Definition: timestamp.h:58
double TimeOffset
Definition: timestamp.h:52
#define USECS_PER_DAY
Definition: timestamp.h:103
int32 month
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:891
#define TMODULO(t, q, u)
Definition: datetime.h:258
Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2937 of file timestamp.c.

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

2938 {
2939  Interval *span = PG_GETARG_INTERVAL_P(0);
2940  Interval *result;
2941  TimeOffset wholeday;
2942  int32 wholemonth;
2943 
2944  result = (Interval *) palloc(sizeof(Interval));
2945  result->month = span->month;
2946  result->day = span->day;
2947  result->time = span->time;
2948 
2949 #ifdef HAVE_INT64_TIMESTAMP
2950  TMODULO(result->time, wholeday, USECS_PER_DAY);
2951 #else
2952  TMODULO(result->time, wholeday, (double) SECS_PER_DAY);
2953 #endif
2954  result->day += wholeday; /* could overflow... */
2955 
2956  wholemonth = result->day / DAYS_PER_MONTH;
2957  result->day -= wholemonth * DAYS_PER_MONTH;
2958  result->month += wholemonth;
2959 
2960  if (result->month > 0 &&
2961  (result->day < 0 || (result->day == 0 && result->time < 0)))
2962  {
2963  result->day += DAYS_PER_MONTH;
2964  result->month--;
2965  }
2966  else if (result->month < 0 &&
2967  (result->day > 0 || (result->day == 0 && result->time > 0)))
2968  {
2969  result->day -= DAYS_PER_MONTH;
2970  result->month++;
2971  }
2972 
2973  if (result->day > 0 && result->time < 0)
2974  {
2975 #ifdef HAVE_INT64_TIMESTAMP
2976  result->time += USECS_PER_DAY;
2977 #else
2978  result->time += (double) SECS_PER_DAY;
2979 #endif
2980  result->day--;
2981  }
2982  else if (result->day < 0 && result->time > 0)
2983  {
2984 #ifdef HAVE_INT64_TIMESTAMP
2985  result->time -= USECS_PER_DAY;
2986 #else
2987  result->time -= (double) SECS_PER_DAY;
2988 #endif
2989  result->day++;
2990  }
2991 
2992  PG_RETURN_INTERVAL_P(result);
2993 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
signed int int32
Definition: c.h:253
int32 day
Definition: timestamp.h:60
#define SECS_PER_DAY
Definition: timestamp.h:98
TimeOffset time
Definition: timestamp.h:58
double TimeOffset
Definition: timestamp.h:52
#define USECS_PER_DAY
Definition: timestamp.h:103
int32 month
Definition: timestamp.h:61
#define DAYS_PER_MONTH
Definition: timestamp.h:89
void * palloc(Size size)
Definition: mcxt.c:891
#define TMODULO(t, q, u)
Definition: datetime.h:258
Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3341 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3342 {
3343  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3344  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3345  Interval *result;
3346 
3347  if (interval_cmp_internal(interval1, interval2) > 0)
3348  result = interval1;
3349  else
3350  result = interval2;
3351  PG_RETURN_INTERVAL_P(result);
3352 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2667 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

2668 {
2669  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2670  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2671 
2672  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2673 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2649 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2650 {
2651  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2652  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2653 
2654  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2655 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3389 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 interval_accum_inv(), interval_dist(), and interval_lerp().

3390 {
3391  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3392  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3393  Interval *result;
3394 
3395  result = (Interval *) palloc(sizeof(Interval));
3396 
3397  result->month = span1->month - span2->month;
3398  /* overflow check copied from int4mi */
3399  if (!SAMESIGN(span1->month, span2->month) &&
3400  !SAMESIGN(result->month, span1->month))
3401  ereport(ERROR,
3402  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3403  errmsg("interval out of range")));
3404 
3405  result->day = span1->day - span2->day;
3406  if (!SAMESIGN(span1->day, span2->day) &&
3407  !SAMESIGN(result->day, span1->day))
3408  ereport(ERROR,
3409  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3410  errmsg("interval out of range")));
3411 
3412  result->time = span1->time - span2->time;
3413  if (!SAMESIGN(span1->time, span2->time) &&
3414  !SAMESIGN(result->time, span1->time))
3415  ereport(ERROR,
3416  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3417  errmsg("interval out of range")));
3418 
3419  PG_RETURN_INTERVAL_P(result);
3420 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:60
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:58
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3429 of file timestamp.c.

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

Referenced by interval_lerp(), and mul_d_interval().

3430 {
3431  Interval *span = PG_GETARG_INTERVAL_P(0);
3432  float8 factor = PG_GETARG_FLOAT8(1);
3433  double month_remainder_days,
3434  sec_remainder,
3435  result_double;
3436  int32 orig_month = span->month,
3437  orig_day = span->day;
3438  Interval *result;
3439 
3440  result = (Interval *) palloc(sizeof(Interval));
3441 
3442  result_double = span->month * factor;
3443  if (isnan(result_double) ||
3444  result_double > INT_MAX || result_double < INT_MIN)
3445  ereport(ERROR,
3446  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3447  errmsg("interval out of range")));
3448  result->month = (int32) result_double;
3449 
3450  result_double = span->day * factor;
3451  if (isnan(result_double) ||
3452  result_double > INT_MAX || result_double < INT_MIN)
3453  ereport(ERROR,
3454  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3455  errmsg("interval out of range")));
3456  result->day = (int32) result_double;
3457 
3458  /*
3459  * The above correctly handles the whole-number part of the month and day
3460  * products, but we have to do something with any fractional part
3461  * resulting when the factor is non-integral. We cascade the fractions
3462  * down to lower units using the conversion factors DAYS_PER_MONTH and
3463  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3464  * so by the representation. The user can choose to cascade up later,
3465  * using justify_hours and/or justify_days.
3466  */
3467 
3468  /*
3469  * Fractional months full days into days.
3470  *
3471  * Floating point calculation are inherently imprecise, so these
3472  * calculations are crafted to produce the most reliable result possible.
3473  * TSROUND() is needed to more accurately produce whole numbers where
3474  * appropriate.
3475  */
3476  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3477  month_remainder_days = TSROUND(month_remainder_days);
3478  sec_remainder = (orig_day * factor - result->day +
3479  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3480  sec_remainder = TSROUND(sec_remainder);
3481 
3482  /*
3483  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3484  * cascade from months and days. It might still be >24 if the combination
3485  * of cascade and the seconds factor operation itself.
3486  */
3487  if (Abs(sec_remainder) >= SECS_PER_DAY)
3488  {
3489  result->day += (int) (sec_remainder / SECS_PER_DAY);
3490  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3491  }
3492 
3493  /* cascade units down */
3494  result->day += (int32) month_remainder_days;
3495 #ifdef HAVE_INT64_TIMESTAMP
3496  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3497  if (result_double > PG_INT64_MAX || result_double < PG_INT64_MIN)
3498  ereport(ERROR,
3499  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3500  errmsg("interval out of range")));
3501  result->time = (int64) result_double;
3502 #else
3503  result->time = span->time * factor + sec_remainder;
3504 #endif
3505 
3506  PG_RETURN_INTERVAL_P(result);
3507 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_INT64_MAX
Definition: c.h:340
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
#define USECS_PER_SEC
Definition: timestamp.h:106
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:253
int32 day
Definition: timestamp.h:60
#define Abs(x)
Definition: c.h:807
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:377
#define SECS_PER_DAY
Definition: timestamp.h:98
#define PG_INT64_MIN
Definition: c.h:339
TimeOffset time
Definition: timestamp.h:58
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:61
#define DAYS_PER_MONTH
Definition: timestamp.h:89
#define TSROUND(j)
Definition: timestamp.h:73
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2640 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2641 {
2642  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2643  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2644 
2645  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2646 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_out ( PG_FUNCTION_ARGS  )

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

1017 {
1018  Interval *span = PG_GETARG_INTERVAL_P(0);
1019  char *result;
1020  struct pg_tm tt,
1021  *tm = &tt;
1022  fsec_t fsec;
1023  char buf[MAXDATELEN + 1];
1024 
1025  if (interval2tm(*span, tm, &fsec) != 0)
1026  elog(ERROR, "could not convert interval to tm");
1027 
1028  EncodeInterval(tm, fsec, IntervalStyle, buf);
1029 
1030  result = pstrdup(buf);
1031  PG_RETURN_CSTRING(result);
1032 }
#define MAXDATELEN
Definition: datetime.h:203
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
double fsec_t
Definition: timestamp.h:53
char * pstrdup(const char *in)
Definition: mcxt.c:1165
int IntervalStyle
Definition: globals.c:108
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:103
#define ERROR
Definition: elog.h:43
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:2140
static char * buf
Definition: pg_test_fsync.c:65
void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str)
Definition: datetime.c:4347
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:306
#define elog
Definition: elog.h:219
Datum interval_part ( PG_FUNCTION_ARGS  )

Definition at line 5075 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, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

5076 {
5077  text *units = PG_GETARG_TEXT_PP(0);
5079  float8 result;
5080  int type,
5081  val;
5082  char *lowunits;
5083  fsec_t fsec;
5084  struct pg_tm tt,
5085  *tm = &tt;
5086 
5087  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
5088  VARSIZE_ANY_EXHDR(units),
5089  false);
5090 
5091  type = DecodeUnits(0, lowunits, &val);
5092  if (type == UNKNOWN_FIELD)
5093  type = DecodeSpecial(0, lowunits, &val);
5094 
5095  if (type == UNITS)
5096  {
5097  if (interval2tm(*interval, tm, &fsec) == 0)
5098  {
5099  switch (val)
5100  {
5101  case DTK_MICROSEC:
5102 #ifdef HAVE_INT64_TIMESTAMP
5103  result = tm->tm_sec * 1000000.0 + fsec;
5104 #else
5105  result = (tm->tm_sec + fsec) * 1000000;
5106 #endif
5107  break;
5108 
5109  case DTK_MILLISEC:
5110 #ifdef HAVE_INT64_TIMESTAMP
5111  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
5112 #else
5113  result = (tm->tm_sec + fsec) * 1000;
5114 #endif
5115  break;
5116 
5117  case DTK_SECOND:
5118 #ifdef HAVE_INT64_TIMESTAMP
5119  result = tm->tm_sec + fsec / 1000000.0;
5120 #else
5121  result = tm->tm_sec + fsec;
5122 #endif
5123  break;
5124 
5125  case DTK_MINUTE:
5126  result = tm->tm_min;
5127  break;
5128 
5129  case DTK_HOUR:
5130  result = tm->tm_hour;
5131  break;
5132 
5133  case DTK_DAY:
5134  result = tm->tm_mday;
5135  break;
5136 
5137  case DTK_MONTH:
5138  result = tm->tm_mon;
5139  break;
5140 
5141  case DTK_QUARTER:
5142  result = (tm->tm_mon / 3) + 1;
5143  break;
5144 
5145  case DTK_YEAR:
5146  result = tm->tm_year;
5147  break;
5148 
5149  case DTK_DECADE:
5150  /* caution: C division may have negative remainder */
5151  result = tm->tm_year / 10;
5152  break;
5153 
5154  case DTK_CENTURY:
5155  /* caution: C division may have negative remainder */
5156  result = tm->tm_year / 100;
5157  break;
5158 
5159  case DTK_MILLENNIUM:
5160  /* caution: C division may have negative remainder */
5161  result = tm->tm_year / 1000;
5162  break;
5163 
5164  default:
5165  ereport(ERROR,
5166  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5167  errmsg("interval units \"%s\" not supported",
5168  lowunits)));
5169  result = 0;
5170  }
5171 
5172  }
5173  else
5174  {
5175  elog(ERROR, "could not convert interval to tm");
5176  result = 0;
5177  }
5178  }
5179  else if (type == RESERV && val == DTK_EPOCH)
5180  {
5181 #ifdef HAVE_INT64_TIMESTAMP
5182  result = interval->time / 1000000.0;
5183 #else
5184  result = interval->time;
5185 #endif
5186  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
5187  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
5188  result += ((double) SECS_PER_DAY) * interval->day;
5189  }
5190  else
5191  {
5192  ereport(ERROR,
5193  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5194  errmsg("interval units \"%s\" not recognized",
5195  lowunits)));
5196  result = 0;
5197  }
5198 
5199  PG_RETURN_FLOAT8(result);
5200 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
double fsec_t
Definition: timestamp.h:53
#define DTK_YEAR
Definition: datetime.h:170
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:169
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:310
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3834
Definition: pgtime.h:25
int32 day
Definition: timestamp.h:60
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static struct pg_tm tm
Definition: localtime.c:103
#define DTK_MONTH
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:174
#define MONTHS_PER_YEAR
Definition: timestamp.h:81
#define DTK_DECADE
Definition: datetime.h:171
#define DAYS_PER_YEAR
Definition: timestamp.h:80
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:377
#define SECS_PER_DAY
Definition: timestamp.h:98
#define DTK_SECOND
Definition: datetime.h:163
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:2140
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
TimeOffset time
Definition: timestamp.h:58
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTK_MINUTE
Definition: datetime.h:164
int32 month
Definition: timestamp.h:61
#define DTK_MICROSEC
Definition: datetime.h:175
#define DAYS_PER_MONTH
Definition: timestamp.h:89
#define DTK_DAY
Definition: datetime.h:166
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3129
#define DTK_EPOCH
Definition: datetime.h:155
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:434
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:689
Datum interval_pl ( PG_FUNCTION_ARGS  )

Definition at line 3355 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 interval_accum(), interval_combine(), and interval_lerp().

3356 {
3357  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3358  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3359  Interval *result;
3360 
3361  result = (Interval *) palloc(sizeof(Interval));
3362 
3363  result->month = span1->month + span2->month;
3364  /* overflow check copied from int4pl */
3365  if (SAMESIGN(span1->month, span2->month) &&
3366  !SAMESIGN(result->month, span1->month))
3367  ereport(ERROR,
3368  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3369  errmsg("interval out of range")));
3370 
3371  result->day = span1->day + span2->day;
3372  if (SAMESIGN(span1->day, span2->day) &&
3373  !SAMESIGN(result->day, span1->day))
3374  ereport(ERROR,
3375  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3376  errmsg("interval out of range")));
3377 
3378  result->time = span1->time + span2->time;
3379  if (SAMESIGN(span1->time, span2->time) &&
3380  !SAMESIGN(result->time, span1->time))
3381  ereport(ERROR,
3382  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3383  errmsg("interval out of range")));
3384 
3385  PG_RETURN_INTERVAL_P(result);
3386 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:60
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:58
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_recv ( PG_FUNCTION_ARGS  )

Definition at line 1038 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_getmsgfloat8(), pq_getmsgint(), pq_getmsgint64(), and Interval::time.

1039 {
1041 
1042 #ifdef NOT_USED
1043  Oid typelem = PG_GETARG_OID(1);
1044 #endif
1045  int32 typmod = PG_GETARG_INT32(2);
1046  Interval *interval;
1047 
1048  interval = (Interval *) palloc(sizeof(Interval));
1049 
1050 #ifdef HAVE_INT64_TIMESTAMP
1051  interval->time = pq_getmsgint64(buf);
1052 #else
1053  interval->time = pq_getmsgfloat8(buf);
1054 #endif
1055  interval->day = pq_getmsgint(buf, sizeof(interval->day));
1056  interval->month = pq_getmsgint(buf, sizeof(interval->month));
1057 
1058  AdjustIntervalForTypmod(interval, typmod);
1059 
1060  PG_RETURN_INTERVAL_P(interval);
1061 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:253
int32 day
Definition: timestamp.h:60
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1386
static char * buf
Definition: pg_test_fsync.c:65
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
TimeOffset time
Definition: timestamp.h:58
int32 month
Definition: timestamp.h:61
float8 pq_getmsgfloat8(StringInfo msg)
Definition: pqformat.c:530
void * palloc(Size size)
Definition: mcxt.c:891
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:486
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1367 of file timestamp.c.

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

1368 {
1370  int32 typmod = PG_GETARG_INT32(1);
1371  Interval *result;
1372 
1373  result = palloc(sizeof(Interval));
1374  *result = *interval;
1375 
1376  AdjustIntervalForTypmod(result, typmod);
1377 
1378  PG_RETURN_INTERVAL_P(result);
1379 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
signed int int32
Definition: c.h:253
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1386
void * palloc(Size size)
Definition: mcxt.c:891
Datum interval_send ( PG_FUNCTION_ARGS  )

Definition at line 1067 of file timestamp.c.

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

1068 {
1071 
1072  pq_begintypsend(&buf);
1073 #ifdef HAVE_INT64_TIMESTAMP
1074  pq_sendint64(&buf, interval->time);
1075 #else
1076  pq_sendfloat8(&buf, interval->time);
1077 #endif
1078  pq_sendint(&buf, interval->day, sizeof(interval->day));
1079  pq_sendint(&buf, interval->month, sizeof(interval->month));
1081 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
void pq_sendfloat8(StringInfo buf, float8 f)
Definition: pqformat.c:324
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:313
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
int32 day
Definition: timestamp.h:60
static char * buf
Definition: pg_test_fsync.c:65
TimeOffset time
Definition: timestamp.h:58
int32 month
Definition: timestamp.h:61
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.c:271
Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3326 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3327 {
3328  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3329  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3330  Interval *result;
3331 
3332  /* use interval_cmp_internal to be sure this agrees with comparisons */
3333  if (interval_cmp_internal(interval1, interval2) < 0)
3334  result = interval1;
3335  else
3336  result = interval2;
3337  PG_RETURN_INTERVAL_P(result);
3338 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2622
Datum interval_transform ( PG_FUNCTION_ARGS  )

Definition at line 1310 of file timestamp.c.

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

1311 {
1313  Node *ret = NULL;
1314  Node *typmod;
1315 
1316  Assert(list_length(expr->args) >= 2);
1317 
1318  typmod = (Node *) lsecond(expr->args);
1319 
1320  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
1321  {
1322  Node *source = (Node *) linitial(expr->args);
1323  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1324  bool noop;
1325 
1326  if (new_typmod < 0)
1327  noop = true;
1328  else
1329  {
1330  int32 old_typmod = exprTypmod(source);
1331  int old_least_field;
1332  int new_least_field;
1333  int old_precis;
1334  int new_precis;
1335 
1336  old_least_field = intervaltypmodleastfield(old_typmod);
1337  new_least_field = intervaltypmodleastfield(new_typmod);
1338  if (old_typmod < 0)
1339  old_precis = INTERVAL_FULL_PRECISION;
1340  else
1341  old_precis = INTERVAL_PRECISION(old_typmod);
1342  new_precis = INTERVAL_PRECISION(new_typmod);
1343 
1344  /*
1345  * Cast is a no-op if least field stays the same or decreases
1346  * while precision stays the same or increases. But precision,
1347  * which is to say, sub-second precision, only affects ranges that
1348  * include SECOND.
1349  */
1350  noop = (new_least_field <= old_least_field) &&
1351  (old_least_field > 0 /* SECOND */ ||
1352  new_precis >= MAX_INTERVAL_PRECISION ||
1353  new_precis >= old_precis);
1354  }
1355  if (noop)
1356  ret = relabel_to_typmod(source, new_typmod);
1357  }
1358 
1359  PG_RETURN_POINTER(ret);
1360 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
List * args
Definition: primnodes.h:434
#define DatumGetInt32(X)
Definition: postgres.h:480
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
Definition: nodes.h:508
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
#define lsecond(l)
Definition: pg_list.h:114
signed int int32
Definition: c.h:253
#define linitial(l)
Definition: pg_list.h:110
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:66
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:71
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:74
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static int list_length(const List *l)
Definition: pg_list.h:89
static int intervaltypmodleastfield(int32 typmod)
Definition: timestamp.c:1261
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:585
Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 4278 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, UNITS, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4279 {
4280  text *units = PG_GETARG_TEXT_PP(0);
4282  Interval *result;
4283  int type,
4284  val;
4285  char *lowunits;
4286  fsec_t fsec;
4287  struct pg_tm tt,
4288  *tm = &tt;
4289 
4290  result = (Interval *) palloc(sizeof(Interval));
4291 
4292  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4293  VARSIZE_ANY_EXHDR(units),
4294  false);
4295 
4296  type = DecodeUnits(0, lowunits, &val);
4297 
4298  if (type == UNITS)
4299  {
4300  if (interval2tm(*interval, tm, &fsec) == 0)
4301  {
4302  switch (val)
4303  {
4304  /* fall through */
4305  case DTK_MILLENNIUM:
4306  /* caution: C division may have negative remainder */
4307  tm->tm_year = (tm->tm_year / 1000) * 1000;
4308  case DTK_CENTURY:
4309  /* caution: C division may have negative remainder */
4310  tm->tm_year = (tm->tm_year / 100) * 100;
4311  case DTK_DECADE:
4312  /* caution: C division may have negative remainder */
4313  tm->tm_year = (tm->tm_year / 10) * 10;
4314  case DTK_YEAR:
4315  tm->tm_mon = 0;
4316  case DTK_QUARTER:
4317  tm->tm_mon = 3 * (tm->tm_mon / 3);
4318  case DTK_MONTH:
4319  tm->tm_mday = 0;
4320  case DTK_DAY:
4321  tm->tm_hour = 0;
4322  case DTK_HOUR:
4323  tm->tm_min = 0;
4324  case DTK_MINUTE:
4325  tm->tm_sec = 0;
4326  case DTK_SECOND:
4327  fsec = 0;
4328  break;
4329 
4330  case DTK_MILLISEC:
4331 #ifdef HAVE_INT64_TIMESTAMP
4332  fsec = (fsec / 1000) * 1000;
4333 #else
4334  fsec = floor(fsec * 1000) / 1000;
4335 #endif
4336  break;
4337  case DTK_MICROSEC:
4338 #ifndef HAVE_INT64_TIMESTAMP
4339  fsec = floor(fsec * 1000000) / 1000000;
4340 #endif
4341  break;
4342 
4343  default:
4344  if (val == DTK_WEEK)
4345  ereport(ERROR,
4346  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4347  errmsg("interval units \"%s\" not supported "
4348  "because months usually have fractional weeks",
4349  lowunits)));
4350  else
4351  ereport(ERROR,
4352  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4353  errmsg("interval units \"%s\" not supported",
4354  lowunits)));
4355  }
4356 
4357  if (tm2interval(tm, fsec, result) != 0)
4358  ereport(ERROR,
4359  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4360  errmsg("interval out of range")));
4361  }
4362  else
4363  elog(ERROR, "could not convert interval to tm");
4364  }
4365  else
4366  {
4367  ereport(ERROR,
4368  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4369  errmsg("interval units \"%s\" not recognized",
4370  lowunits)));
4371  }
4372 
4373  PG_RETURN_INTERVAL_P(result);
4374 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
double fsec_t
Definition: timestamp.h:53
#define DTK_WEEK
Definition: datetime.h:167
#define DTK_YEAR
Definition: datetime.h:170
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:169
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3834
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
static struct pg_tm tm
Definition: localtime.c:103
#define DTK_MONTH
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:174
#define DTK_DECADE
Definition: datetime.h:171
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
#define DTK_SECOND
Definition: datetime.h:163
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:2140
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTK_MINUTE
Definition: datetime.h:164
#define DTK_MICROSEC
Definition: datetime.h:175
#define DTK_DAY
Definition: datetime.h:166
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:434
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:689
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:2186
Datum interval_um ( PG_FUNCTION_ARGS  )

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

3298 {
3300  Interval *result;
3301 
3302  result = (Interval *) palloc(sizeof(Interval));
3303 
3304  result->time = -interval->time;
3305  /* overflow check copied from int4um */
3306  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3307  ereport(ERROR,
3308  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3309  errmsg("interval out of range")));
3310  result->day = -interval->day;
3311  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3312  ereport(ERROR,
3313  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3314  errmsg("interval out of range")));
3315  result->month = -interval->month;
3316  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3317  ereport(ERROR,
3318  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3319  errmsg("interval out of range")));
3320 
3321  PG_RETURN_INTERVAL_P(result);
3322 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:57
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:60
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:58
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

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

1097 {
1099  int32 *tl;
1100  int n;
1101  int32 typmod;
1102 
1103  tl = ArrayGetIntegerTypmods(ta, &n);
1104 
1105  /*
1106  * tl[0] - interval range (fields bitmask) tl[1] - precision (optional)
1107  *
1108  * Note we must validate tl[0] even though it's normally guaranteed
1109  * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
1110  */
1111  if (n > 0)
1112  {
1113  switch (tl[0])
1114  {
1115  case INTERVAL_MASK(YEAR):
1116  case INTERVAL_MASK(MONTH):
1117  case INTERVAL_MASK(DAY):
1118  case INTERVAL_MASK(HOUR):
1119  case INTERVAL_MASK(MINUTE):
1120  case INTERVAL_MASK(SECOND):
1128  case INTERVAL_FULL_RANGE:
1129  /* all OK */
1130  break;
1131  default:
1132  ereport(ERROR,
1133  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1134  errmsg("invalid INTERVAL type modifier")));
1135  }
1136  }
1137 
1138  if (n == 1)
1139  {
1140  if (tl[0] != INTERVAL_FULL_RANGE)
1141  typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, tl[0]);
1142  else
1143  typmod = -1;
1144  }
1145  else if (n == 2)
1146  {
1147  if (tl[1] < 0)
1148  ereport(ERROR,
1149  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1150  errmsg("INTERVAL(%d) precision must not be negative",
1151  tl[1])));
1152  if (tl[1] > MAX_INTERVAL_PRECISION)
1153  {
1154  ereport(WARNING,
1155  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1156  errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1157  tl[1], MAX_INTERVAL_PRECISION)));
1158  typmod = INTERVAL_TYPMOD(MAX_INTERVAL_PRECISION, tl[0]);
1159  }
1160  else
1161  typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
1162  }
1163  else
1164  {
1165  ereport(ERROR,
1166  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1167  errmsg("invalid INTERVAL type modifier")));
1168  typmod = 0; /* keep compiler quiet */
1169  }
1170 
1171  PG_RETURN_INT32(typmod);
1172 }
#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:298
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:69
signed int int32
Definition: c.h:253
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:66
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:71
#define INTERVAL_TYPMOD(p, r)
Definition: timestamp.h:73
#define MINUTE
Definition: datetime.h:102
#define ereport(elevel, rest)
Definition: elog.h:122
#define MONTH
Definition: datetime.h:92
#define WARNING
Definition: elog.h:40
#define INTERVAL_MASK(b)
Definition: timestamp.h:66
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HOUR
Definition: datetime.h:101
static int intervaltypmodleastfield ( int32  typmod)
static

Definition at line 1261 of file timestamp.c.

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

Referenced by interval_transform().

1262 {
1263  if (typmod < 0)
1264  return 0; /* SECOND */
1265 
1266  switch (INTERVAL_RANGE(typmod))
1267  {
1268  case INTERVAL_MASK(YEAR):
1269  return 5; /* YEAR */
1270  case INTERVAL_MASK(MONTH):
1271  return 4; /* MONTH */
1272  case INTERVAL_MASK(DAY):
1273  return 3; /* DAY */
1274  case INTERVAL_MASK(HOUR):
1275  return 2; /* HOUR */
1276  case INTERVAL_MASK(MINUTE):
1277  return 1; /* MINUTE */
1278  case INTERVAL_MASK(SECOND):
1279  return 0; /* SECOND */
1281  return 4; /* MONTH */
1283  return 2; /* HOUR */
1285  return 1; /* MINUTE */
1287  return 0; /* SECOND */
1289  return 1; /* MINUTE */
1291  return 0; /* SECOND */
1293  return 0; /* SECOND */
1294  case INTERVAL_FULL_RANGE:
1295  return 0; /* SECOND */
1296  default:
1297  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1298  break;
1299  }
1300  return 0; /* can't get here, but keep compiler quiet */
1301 }
#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:69
#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:75
#define INTERVAL_MASK(b)
Definition: timestamp.h:66
#define elog
Definition: elog.h:219
#define HOUR
Definition: datetime.h:101
Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

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

1176 {
1177  int32 typmod = PG_GETARG_INT32(0);
1178  char *res = (char *) palloc(64);
1179  int fields;
1180  int precision;
1181  const char *fieldstr;
1182 
1183  if (typmod < 0)
1184  {
1185  *res = '\0';
1186  PG_RETURN_CSTRING(res);
1187  }
1188 
1189  fields = INTERVAL_RANGE(typmod);
1190  precision = INTERVAL_PRECISION(typmod);
1191 
1192  switch (fields)
1193  {
1194  case INTERVAL_MASK(YEAR):
1195  fieldstr = " year";
1196  break;
1197  case INTERVAL_MASK(MONTH):
1198  fieldstr = " month";
1199  break;
1200  case INTERVAL_MASK(DAY):
1201  fieldstr = " day";
1202  break;
1203  case INTERVAL_MASK(HOUR):
1204  fieldstr = " hour";
1205  break;
1206  case INTERVAL_MASK(MINUTE):
1207  fieldstr = " minute";
1208  break;
1209  case INTERVAL_MASK(SECOND):
1210  fieldstr = " second";
1211  break;
1213  fieldstr = " year to month";
1214  break;
1216  fieldstr = " day to hour";
1217  break;
1219  fieldstr = " day to minute";
1220  break;
1222  fieldstr = " day to second";
1223  break;
1225  fieldstr = " hour to minute";
1226  break;
1228  fieldstr = " hour to second";
1229  break;
1231  fieldstr = " minute to second";
1232  break;
1233  case INTERVAL_FULL_RANGE:
1234  fieldstr = "";
1235  break;
1236  default:
1237  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1238  fieldstr = "";
1239  break;
1240  }
1241 
1242  if (precision != INTERVAL_FULL_PRECISION)
1243  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1244  else
1245  snprintf(res, 64, "%s", fieldstr);
1246 
1247  PG_RETURN_CSTRING(res);
1248 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define SECOND
Definition: datetime.h:103
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:69
signed int int32
Definition: c.h:253
#define ERROR
Definition: elog.h:43
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:71
#define MINUTE
Definition: datetime.h:102
#define MONTH
Definition: datetime.h:92
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:74
#define INTERVAL_RANGE(t)
Definition: timestamp.h:75
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:306
#define INTERVAL_MASK(b)
Definition: timestamp.h:66
void * palloc(Size size)
Definition: mcxt.c:891
#define elog
Definition: elog.h:219
#define HOUR
Definition: datetime.h:101
void isoweek2date ( int  woy,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4402 of file timestamp.c.

References isoweek2j(), and j2date().

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

4403 {
4404  j2date(isoweek2j(*year, woy), year, mon, mday);
4405 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:322
int isoweek2j(int year, int week)
Definition: timestamp.c:4382
int isoweek2j ( int  year,
int  week 
)

Definition at line 4382 of file timestamp.c.

References date2j(), and j2day().

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

4383 {
4384  int day0,
4385  day4;
4386 
4387  /* fourth day of current year */
4388  day4 = date2j(year, 1, 4);
4389 
4390  /* day0 == offset to first day of week (Monday) */
4391  day0 = j2day(day4 - 1);
4392 
4393  return ((week - 1) * 7) + (day4 - day0);
4394 }
int j2day(int date)
Definition: datetime.c:357
int date2j(int y, int m, int d)
Definition: datetime.c:297
void isoweekdate2date ( int  isoweek,
int  wday,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4415 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4416 {
4417  int jday;
4418 
4419  jday = isoweek2j(*year, isoweek);
4420  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4421  if (wday > 1)
4422  jday += wday - 2;
4423  else
4424  jday += 6;
4425  j2date(jday, year, mon, mday);
4426 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:322
int isoweek2j(int year, int week)
Definition: timestamp.c:4382
Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1598 of file timestamp.c.

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

1599 {
1600  int32 years = PG_GETARG_INT32(0);
1602  int32 weeks = PG_GETARG_INT32(2);
1603  int32 days = PG_GETARG_INT32(3);
1604  int32 hours = PG_GETARG_INT32(4);
1605  int32 mins = PG_GETARG_INT32(5);
1606  double secs = PG_GETARG_FLOAT8(6);
1607  Interval *result;
1608 
1609  /*
1610  * Reject out-of-range inputs. We really ought to check the integer
1611  * inputs as well, but it's not entirely clear what limits to apply.
1612  */
1613  if (isinf(secs) || isnan(secs))
1614  ereport(ERROR,
1615  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1616  errmsg("interval out of range")));
1617 
1618  result = (Interval *) palloc(sizeof(Interval));
1619  result->month = years * MONTHS_PER_YEAR + months;
1620  result->day = weeks * 7 + days;
1621 
1622 #ifdef HAVE_INT64_TIMESTAMP
1623  secs = rint(secs * USECS_PER_SEC);
1624  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1625  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1626  (int64) secs;
1627 #else
1628  result->time = hours * (double) SECS_PER_HOUR +
1629  mins * (double) SECS_PER_MINUTE +
1630  secs;
1631 #endif
1632 
1633  PG_RETURN_INTERVAL_P(result);
1634 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:61
#define USECS_PER_SEC
Definition: timestamp.h:106
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:253
int32 day
Definition: timestamp.h:60
#define MONTHS_PER_YEAR
Definition: timestamp.h:81
const char *const months[]
Definition: datetime.c:71
#define ERROR
Definition: elog.h:43
int isinf(double x)
#define SECS_PER_MINUTE
Definition: timestamp.h:100
TimeOffset time
Definition: timestamp.h:58
const char *const days[]
Definition: datetime.c:74
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:99
int32 month
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 673 of file timestamp.c.

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

674 {
675  int32 year = PG_GETARG_INT32(0);
676  int32 month = PG_GETARG_INT32(1);
677  int32 mday = PG_GETARG_INT32(2);
678  int32 hour = PG_GETARG_INT32(3);
679  int32 min = PG_GETARG_INT32(4);
680  float8 sec = PG_GETARG_FLOAT8(5);
681  Timestamp result;
682 
683  result = make_timestamp_internal(year, month, mday,
684  hour, min, sec);
685 
686  PG_RETURN_TIMESTAMP(result);
687 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:580
signed int int32
Definition: c.h:253
double float8
Definition: c.h:377
double Timestamp
Definition: timestamp.h:50
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:59
static Timestamp make_timestamp_internal ( int  year,
int  month,
int  day,
int  hour,
int  min,
double  sec 
)
static

Definition at line 580 of file timestamp.c.

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

582 {
583  struct pg_tm tm;
585  TimeOffset time;
586  int dterr;
587  Timestamp result;
588 
589  tm.tm_year = year;
590  tm.tm_mon = month;
591  tm.tm_mday = day;
592 
593  /*
594  * Note: we'll reject zero or negative year values. Perhaps negatives
595  * should be allowed to represent BC years?
596  */
597  dterr = ValidateDate(DTK_DATE_M, false, false, false, &tm);
598 
599  if (dterr != 0)
600  ereport(ERROR,
601  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
602  errmsg("date field value out of range: %d-%02d-%02d",
603  year, month, day)));
604 
606  ereport(ERROR,
607  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
608  errmsg("date out of range: %d-%02d-%02d",
609  year, month, day)));
610 
612 
613  /*
614  * This should match the checks in DecodeTimeOnly, except that since we're
615  * dealing with a float "sec" value, we also explicitly reject NaN. (An
616  * infinity input should get rejected by the range comparisons, but we
617  * can't be sure how those will treat a NaN.)
618  */
619  if (hour < 0 || min < 0 || min > MINS_PER_HOUR - 1 ||
620  isnan(sec) ||
621  sec < 0 || sec > SECS_PER_MINUTE ||
622  hour > HOURS_PER_DAY ||
623  /* test for > 24:00:00 */
624  (hour == HOURS_PER_DAY && (min > 0 || sec > 0)))
625  ereport(ERROR,
626  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
627  errmsg("time field value out of range: %d:%02d:%02g",
628  hour, min, sec)));
629 
630  /* This should match tm2time */
631 #ifdef HAVE_INT64_TIMESTAMP
632  time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
633  * USECS_PER_SEC) + rint(sec * USECS_PER_SEC);
634 
635  result = date * USECS_PER_DAY + time;
636  /* check for major overflow */
637  if ((result - time) / USECS_PER_DAY != date)
638  ereport(ERROR,
639  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
640  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
641  year, month, day,
642  hour, min, sec)));
643 
644  /* check for just-barely overflow (okay except time-of-day wraps) */
645  /* caution: we want to allow 1999-12-31 24:00:00 */
646  if ((result < 0 && date > 0) ||
647  (result > 0 && date < -1))
648  ereport(ERROR,
649  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
650  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
651  year, month, day,
652  hour, min, sec)));
653 #else
654  time = ((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE) + sec;
655  result = date * SECS_PER_DAY + time;
656 #endif
657 
658  /* final range check catches just-out-of-range timestamps */
659  if (!IS_VALID_TIMESTAMP(result))
660  ereport(ERROR,
661  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
662  errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
663  year, month, day,
664  hour, min, sec)));
665 
666  return result;
667 }
#define USECS_PER_SEC
Definition: timestamp.h:106
int errcode(int sqlerrcode)
Definition: elog.c:575
long date
Definition: pgtypes_date.h:8
Definition: pgtime.h:25
#define DTK_DATE_M
Definition: datetime.h:194
#define MINS_PER_HOUR
Definition: timestamp.h:101
static struct pg_tm tm
Definition: localtime.c:103
#define ERROR
Definition: elog.h:43
#define SECS_PER_DAY
Definition: timestamp.h:98
int tm_mday
Definition: pgtime.h:30
#define HOURS_PER_DAY
Definition: timestamp.h:90
int tm_mon
Definition: pgtime.h:31
#define SECS_PER_MINUTE
Definition: timestamp.h:100
double TimeOffset
Definition: timestamp.h:52
double rint(double x)
Definition: rint.c:22
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:177
#define USECS_PER_DAY
Definition: timestamp.h:103
#define ereport(elevel, rest)
Definition: elog.h:122
double Timestamp
Definition: timestamp.h:50
int date2j(int y, int m, int d)
Definition: datetime.c:297
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:230
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:185
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2558
Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 693 of file timestamp.c.

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

694 {
695  int32 year = PG_GETARG_INT32(0);
696  int32 month = PG_GETARG_INT32(1);
697  int32 mday = PG_GETARG_INT32(2);
698  int32 hour = PG_GETARG_INT32(3);
699  int32 min = PG_GETARG_INT32(4);
700  float8 sec = PG_GETARG_FLOAT8(5);
701  Timestamp result;
702 
703  result = make_timestamp_internal(year, month, mday,
704  hour, min, sec);
705 
707 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:5369
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:580
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:60
signed int int32
Definition: c.h:253
double float8
Definition: c.h:377
double Timestamp
Definition: timestamp.h:50
Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 714 of file timestamp.c.

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

715 {
716  int32 year = PG_GETARG_INT32(0);
717  int32 month = PG_GETARG_INT32(1);
718  int32 mday = PG_GETARG_INT32(2);
719  int32 hour = PG_GETARG_INT32(3);
720  int32 min = PG_GETARG_INT32(4);
721  float8 sec = PG_GETARG_FLOAT8(5);
723  TimestampTz result;
725  struct pg_tm tt;
726  int tz;
727  fsec_t fsec;
728 
729  timestamp = make_timestamp_internal(year, month, mday,
730  hour, min, sec);
731 
732  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
733  ereport(ERROR,
734  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
735  errmsg("timestamp out of range")));
736 
737  tz = parse_sane_timezone(&tt, zone);
738 
739  result = dt2local(timestamp, -tz);
740 
741  if (!IS_VALID_TIMESTAMP(result))
742  ereport(ERROR,
743  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
744  errmsg("timestamp out of range")));
745 
746  PG_RETURN_TIMESTAMPTZ(result);
747 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
double fsec_t
Definition: timestamp.h:53
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:580
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1950
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:60
signed int int32
Definition: c.h:253
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
double TimestampTz
Definition: timestamp.h:51
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:377
#define ereport(elevel, rest)
Definition: elog.h:122
double Timestamp
Definition: timestamp.h:50
#define NULL
Definition: c.h:226
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:230
double timestamp
Definition: zic.c:91
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:434
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2218
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:496
Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3510 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3511 {
3512  /* Args are float8 and Interval *, but leave them as generic Datum */
3513  Datum factor = PG_GETARG_DATUM(0);
3514  Datum span = PG_GETARG_DATUM(1);
3515 
3516  return DirectFunctionCall2(interval_mul, span, factor);
3517 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
uintptr_t Datum
Definition: postgres.h:374
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3429
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
static float8 NonFiniteTimestampTzPart ( int  type,
int  unit,
char *  lowunits,
bool  isNegative,
bool  isTz 
)
static

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

4563 {
4564  if ((type != UNITS) && (type != RESERV))
4565  {
4566  if (isTz)
4567  ereport(ERROR,
4568  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4569  errmsg("timestamp with time zone units \"%s\" not recognized",
4570  lowunits)));
4571  else
4572  ereport(ERROR,
4573  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4574  errmsg("timestamp units \"%s\" not recognized",
4575  lowunits)));
4576  }
4577 
4578  switch (unit)
4579  {
4580  /* Oscillating units */
4581  case DTK_MICROSEC:
4582  case DTK_MILLISEC:
4583  case DTK_SECOND:
4584  case DTK_MINUTE:
4585  case DTK_HOUR:
4586  case DTK_DAY:
4587  case DTK_MONTH:
4588  case DTK_QUARTER:
4589  case DTK_WEEK:
4590  case DTK_DOW:
4591  case DTK_ISODOW:
4592  case DTK_DOY:
4593  case DTK_TZ:
4594  case DTK_TZ_MINUTE:
4595  case DTK_TZ_HOUR:
4596  return 0.0;
4597 
4598  /* Monotonically-increasing units */
4599  case DTK_YEAR:
4600  case DTK_DECADE:
4601  case DTK_CENTURY:
4602  case DTK_MILLENNIUM:
4603  case DTK_JULIAN:
4604  case DTK_ISOYEAR:
4605  case DTK_EPOCH:
4606  if (isNegative)
4607  return -get_float8_infinity();
4608  else
4609  return get_float8_infinity();
4610 
4611  default:
4612  if (isTz)
4613  ereport(ERROR,
4614  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4615  errmsg("timestamp with time zone units \"%s\" not supported",
4616  lowunits)));
4617  else
4618  ereport(ERROR,
4619  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4620  errmsg("timestamp units \"%s\" not supported",
4621  lowunits)));
4622  return 0.0; /* keep compiler quiet */
4623  }
4624 }
#define DTK_TZ_HOUR
Definition: datetime.h:180
#define DTK_CENTURY
Definition: datetime.h:172
#define UNITS
Definition: datetime.h:108
#define DTK_JULIAN
Definition: datetime.h:176
#define DTK_WEEK
Definition: datetime.h:167
#define DTK_YEAR
Definition: datetime.h:170
#define DTK_QUARTER
Definition: datetime.h:169
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DTK_ISODOW
Definition: datetime.h:183
#define DTK_MONTH
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:174
#define DTK_DECADE
Definition: datetime.h:171
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
#define DTK_SECOND
Definition: datetime.h:163
#define DTK_ISOYEAR
Definition: datetime.h:182
#define DTK_TZ_MINUTE
Definition: datetime.h:181
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTK_MINUTE
Definition: datetime.h:164
double get_float8_infinity(void)
Definition: float.c:121
#define DTK_DOW
Definition: datetime.h:178
#define DTK_MICROSEC
Definition: datetime.h:175
#define DTK_DOY
Definition: datetime.h:179
#define DTK_DAY
Definition: datetime.h:166
#define RESERV
Definition: datetime.h:91
#define DTK_EPOCH
Definition: datetime.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2720 of file timestamp.c.

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

2721 {
2722  /*
2723  * The arguments are Timestamps, but we leave them as generic Datums to
2724  * avoid unnecessary conversions between value and reference forms --- not
2725  * to mention possible dereferences of null pointers.
2726  */
2727  Datum ts1 = PG_GETARG_DATUM(0);
2728  Datum te1 = PG_GETARG_DATUM(1);
2729  Datum ts2 = PG_GETARG_DATUM(2);
2730  Datum te2 = PG_GETARG_DATUM(3);
2731  bool ts1IsNull = PG_ARGISNULL(0);
2732  bool te1IsNull = PG_ARGISNULL(1);
2733  bool ts2IsNull = PG_ARGISNULL(2);
2734  bool te2IsNull = PG_ARGISNULL(3);
2735 
2736 #define TIMESTAMP_GT(t1,t2) \
2737  DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2738 #define TIMESTAMP_LT(t1,t2) \
2739  DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2740 
2741  /*
2742  * If both endpoints of interval 1 are null, the result is null (unknown).
2743  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2744  * take ts1 as the lesser endpoint.
2745  */
2746  if (ts1IsNull)
2747  {
2748  if (te1IsNull)
2749  PG_RETURN_NULL();
2750  /* swap null for non-null */
2751  ts1 = te1;
2752  te1IsNull = true;
2753  }
2754  else if (!te1IsNull)
2755  {
2756  if (TIMESTAMP_GT(ts1, te1))
2757  {
2758  Datum tt = ts1;
2759 
2760  ts1 = te1;
2761  te1 = tt;
2762  }
2763  }
2764 
2765  /* Likewise for interval 2. */
2766  if (ts2IsNull)
2767  {
2768  if (te2IsNull)
2769  PG_RETURN_NULL();
2770  /* swap null for non-null */
2771  ts2 = te2;
2772  te2IsNull = true;
2773  }
2774  else if (!te2IsNull)
2775  {
2776  if (TIMESTAMP_GT(ts2, te2))
2777  {
2778  Datum tt = ts2;
2779 
2780  ts2 = te2;
2781  te2 = tt;
2782  }
2783  }
2784 
2785  /*
2786  * At this point neither ts1 nor ts2 is null, so we can consider three
2787  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2788  */
2789  if (TIMESTAMP_GT(ts1, ts2))
2790  {
2791  /*
2792  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2793  * in the presence of nulls it's not quite completely so.
2794  */
2795  if (te2IsNull)
2796  PG_RETURN_NULL();
2797  if (TIMESTAMP_LT(ts1, te2))
2798  PG_RETURN_BOOL(true);
2799  if (te1IsNull)
2800  PG_RETURN_NULL();
2801 
2802  /*
2803  * If te1 is not null then we had ts1 <= te1 above, and we just found
2804  * ts1 >= te2, hence te1 >= te2.
2805  */
2806  PG_RETURN_BOOL(false);
2807  }
2808  else if (TIMESTAMP_LT(ts1, ts2))
2809  {
2810  /* This case is ts2 < te1 OR te2 < te1 */
2811  if (te1IsNull)
2812  PG_RETURN_NULL();
2813  if (TIMESTAMP_LT(ts2, te1))
2814  PG_RETURN_BOOL(true);
2815  if (te2IsNull)
2816  PG_RETURN_NULL();
2817 
2818  /*
2819  * If te2 is not null then we had ts2 <= te2 above, and we just found
2820  * ts2 >= te1, hence te2 >= te1.
2821  */
2822  PG_RETURN_BOOL(false);
2823  }
2824  else
2825  {
2826  /*
2827  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2828  * rather silly way of saying "true if both are non-null, else null".
2829  */
2830  if (te1IsNull || te2IsNull)
2831  PG_RETURN_NULL();
2832  PG_RETURN_BOOL(true);
2833  }
2834 
2835 #undef TIMESTAMP_GT
2836 #undef TIMESTAMP_LT
2837 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:224
#define TIMESTAMP_LT(t1, t2)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define TIMESTAMP_GT(t1, t2)
#define PG_RETURN_NULL()
Definition: fmgr.h:289
static int parse_sane_timezone ( struct pg_tm tm,
text zone 
)
static

Definition at line 496 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(), TZ, TZ_STRLEN_MAX, and val.

Referenced by make_timestamptz_at_timezone().

497 {
498  char tzname[TZ_STRLEN_MAX + 1];
499  int rt;
500  int tz;
501 
502  text_to_cstring_buffer(zone, tzname, sizeof(tzname));
503 
504  /*
505  * Look up the requested timezone. First we try to interpret it as a
506  * numeric timezone specification; if DecodeTimezone decides it doesn't
507  * like the format, we look in the timezone abbreviation table (to handle
508  * cases like "EST"), and if that also fails, we look in the timezone
509  * database (to handle cases like "America/New_York"). (This matches the
510  * order in which timestamp input checks the cases; it's important because
511  * the timezone database unwisely uses a few zone names that are identical
512  * to offset abbreviations.)
513  *
514  * Note pg_tzset happily parses numeric input that DecodeTimezone would
515  * reject. To avoid having it accept input that would otherwise be seen
516  * as invalid, it's enough to disallow having a digit in the first
517  * position of our input string.
518  */
519  if (isdigit((unsigned char) *tzname))
520  ereport(ERROR,
521  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
522  errmsg("invalid input syntax for numeric time zone: \"%s\"",
523  tzname),
524  errhint("Numeric time zones must have \"-\" or \"+\" as first character.")));
525 
526  rt = DecodeTimezone(tzname, &tz);
527  if (rt != 0)
528  {
529  char *lowzone;
530  int type,
531  val;
532  pg_tz *tzp;
533 
534  if (rt == DTERR_TZDISP_OVERFLOW)
535  ereport(ERROR,
536  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
537  errmsg("numeric time zone \"%s\" out of range", tzname)));
538  else if (rt != DTERR_BAD_FORMAT)
539  ereport(ERROR,
540  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
541  errmsg("time zone \"%s\" not recognized", tzname)));
542 
543  /* DecodeTimezoneAbbrev requires lowercase input */
544  lowzone = downcase_truncate_identifier(tzname,
545  strlen(tzname),
546  false);
547  type = DecodeTimezoneAbbrev(0, lowzone, &val, &tzp);
548 
549  if (type == TZ || type == DTZ)
550  {
551  /* fixed-offset abbreviation */
552  tz = -val;
553  }
554  else if (type == DYNTZ)
555  {
556  /* dynamic-offset abbreviation, resolve using specified time */
557  tz = DetermineTimeZoneAbbrevOffset(tm, tzname, tzp);
558  }
559  else
560  {
561  /* try it as a full zone name */
562  tzp = pg_tzset(tzname);
563  if (tzp)
564  tz = DetermineTimeZoneOffset(tm, tzp);
565  else
566  ereport(ERROR,
567  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
568  errmsg("time zone \"%s\" not recognized", tzname)));
569  }
570  }
571 
572  return tz;
573 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:290
int errhint(const char *fmt,...)
Definition: elog.c:987
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
int errcode(int sqlerrcode)
Definition: elog.c:575
#define TZ
Definition: datetime.h:96
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:215
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:218
#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:3074
int DecodeTimezone(char *str, int *tzp)
Definition: datetime.c:2996
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1540
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:294
Definition: pgtz.h:59
#define DTZ
Definition: datetime.h:97
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DYNTZ
Definition: datetime.h:98
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1698
long val
Definition: informix.c:689
Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1675 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

1676 {
1678 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:60
TimestampTz PgReloadTime
Definition: timestamp.c:51
Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1669 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

1670 {
1672 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:60
TimestampTz PgStartTime
Definition: timestamp.c:48
Timestamp SetEpochTimestamp ( void  )

Definition at line 2273 of file timestamp.c.

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

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

2274 {
2275  Timestamp dt;
2276  struct pg_tm tt,
2277  *tm = &tt;
2278 
2279  GetEpochTime(tm);
2280  /* we don't bother to test for failure ... */
2281  tm2timestamp(tm, 0, NULL, &dt);
2282 
2283  return dt;
2284 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:103
double Timestamp
Definition: timestamp.h:50
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:2088
#define NULL
Definition: c.h:226
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2254
Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1657 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1658 {
1660 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:60
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:716
static TimeOffset time2t ( const int  hour,
const int  min,
const int  sec,
const fsec_t  fsec 
)
static

Definition at line 2208 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

2209 {
2210 #ifdef HAVE_INT64_TIMESTAMP
2211  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2212 #else
2213  return (((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec + fsec;
2214 #endif
2215 }
#define USECS_PER_SEC
Definition: timestamp.h:106
#define MINS_PER_HOUR
Definition: timestamp.h:101
#define SECS_PER_MINUTE
Definition: timestamp.h:100
TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1845 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(), and pg_stat_file().

1846 {
1847  TimestampTz result;
1848 
1849  result = (TimestampTz) tm -
1851 
1852 #ifdef HAVE_INT64_TIMESTAMP
1853  result *= USECS_PER_SEC;
1854 #endif
1855 
1856  return result;
1857 }
#define USECS_PER_SEC
Definition: timestamp.h:106
static struct pg_tm tm
Definition: localtime.c:103
double TimestampTz
Definition: timestamp.h:51
#define SECS_PER_DAY
Definition: timestamp.h:98
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:184
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:185
static TimestampTz timestamp2timestamptz ( Timestamp  timestamp)
static

Definition at line 5369 of file timestamp.c.

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

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

5370 {
5371  TimestampTz result;
5372  struct pg_tm tt,
5373  *tm = &tt;
5374  fsec_t fsec;
5375  int tz;
5376 
5378  result = timestamp;
5379  else
5380  {
5381  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
5382  ereport(ERROR,
5383  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5384  errmsg("timestamp out of range")));
5385 
5387 
5388  if (tm2timestamp(tm, fsec, &tz, &result) != 0)
5389  ereport(ERROR,
5390  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5391  errmsg("timestamp out of range")));
5392  }
5393 
5394  return result;
5395 }
double fsec_t
Definition: timestamp.h:53
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1950
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:103
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:144
double TimestampTz
Definition: timestamp.h:51
#define ERROR
Definition: elog.h:43
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1540
#define ereport(elevel, rest)
Definition: elog.h:122
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:2088
#define NULL
Definition: c.h:226
double timestamp
int errmsg(const char *fmt,...)
Definition: elog.c:797
pg_tz * session_timezone
Definition: pgtz.c:27
int timestamp2tm ( Timestamp  dt,
int *  tzp,
struct pg_tm tm,
fsec_t fsec,
const char **  tzn,
pg_tz attimezone 
)

Definition at line 1950 of file timestamp.c.

References dt2time(), INT64CONST, j2date(), NULL, pg_localtime(), POSTGRES_EPOCH_JDATE, rint(), SECS_PER_DAY, session_timezone, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, pg_tm::tm_zone, TMODULO, TSROUND, UNIX_EPOCH_JDATE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by datum_to_json(), datum_to_jsonb(), DetermineTimeZoneAbbrevOffsetTS(), GetCurrentDateTime(), GetCurrentTimeUsec(), GetSQLCurrentDate(), GetSQLCurrentTime(), GetSQLLocalTime(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), pg_timezone_names(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_fmt_asc(), PGTYPEStimestamp_to_asc(), timestamp2timestamptz(), timestamp_abstime(), timestamp_age(), timestamp_date(), timestamp_out(), timestamp_part(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_abstime(), timestamptz_age(), timestamptz_date(), timestamptz_out(), timestamptz_part(), timestamptz_pl_interval(), timestamptz_recv(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), timestamptz_to_str(), timestamptz_trunc(), and timestamptz_zone().

1951 {
1952  Timestamp date;
1953  Timestamp time;
1954  pg_time_t utime;
1955 
1956  /* Use session timezone if caller asks for default */
1957  if (attimezone == NULL)
1958  attimezone = session_timezone;
1959 
1960 #ifdef HAVE_INT64_TIMESTAMP
1961  time = dt;
1962  TMODULO(time, date, USECS_PER_DAY);
1963 
1964