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)
 
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 1319 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(), SECOND, Interval::time, USECS_PER_HOUR, USECS_PER_MINUTE, and YEAR.

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

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

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

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

334 {
335  static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
336  INT64CONST(1000000),
337  INT64CONST(100000),
338  INT64CONST(10000),
339  INT64CONST(1000),
340  INT64CONST(100),
341  INT64CONST(10),
342  INT64CONST(1)
343  };
344 
345  static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
346  INT64CONST(500000),
347  INT64CONST(50000),
348  INT64CONST(5000),
349  INT64CONST(500),
350  INT64CONST(50),
351  INT64CONST(5),
352  INT64CONST(0)
353  };
354 
355  if (!TIMESTAMP_NOT_FINITE(*time)
356  && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
357  {
358  if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
359  ereport(ERROR,
360  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
361  errmsg("timestamp(%d) precision must be between %d and %d",
362  typmod, 0, MAX_TIMESTAMP_PRECISION)));
363 
364  if (*time >= INT64CONST(0))
365  {
366  *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
367  TimestampScales[typmod];
368  }
369  else
370  {
371  *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
372  * TimestampScales[typmod]);
373  }
374  }
375 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel, rest)
Definition: elog.h:122
#define INT64CONST(x)
Definition: c.h:310
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:53
#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:256
#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 1545 of file timestamp.c.

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1546 {
1548 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1569
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
int date2isoweek ( int  year,
int  mon,
int  mday 
)

Definition at line 4043 of file timestamp.c.

References date2j(), j2day(), and result.

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

4044 {
4045  float8 result;
4046  int day0,
4047  day4,
4048  dayn;
4049 
4050  /* current day */
4051  dayn = date2j(year, mon, mday);
4052 
4053  /* fourth day of current year */
4054  day4 = date2j(year, 1, 4);
4055 
4056  /* day0 == offset to first day of week (Monday) */
4057  day0 = j2day(day4 - 1);
4058 
4059  /*
4060  * We need the first week containing a Thursday, otherwise this day falls
4061  * into the previous year for purposes of counting weeks
4062  */
4063  if (dayn < day4 - day0)
4064  {
4065  day4 = date2j(year - 1, 1, 4);
4066 
4067  /* day0 == offset to first day of week (Monday) */
4068  day0 = j2day(day4 - 1);
4069  }
4070 
4071  result = (dayn - (day4 - day0)) / 7 + 1;
4072 
4073  /*
4074  * Sometimes the last few days in a year will fall into the first week of
4075  * the next year, so check for this.
4076  */
4077  if (result >= 52)
4078  {
4079  day4 = date2j(year + 1, 1, 4);
4080 
4081  /* day0 == offset to first day of week (Monday) */
4082  day0 = j2day(day4 - 1);
4083 
4084  if (dayn >= day4 - day0)
4085  result = (dayn - (day4 - day0)) / 7 + 1;
4086  }
4087 
4088  return (int) result;
4089 }
return result
Definition: formatting.c:1618
double float8
Definition: c.h:381
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
int date2isoyear ( int  year,
int  mon,
int  mday 
)

Definition at line 4097 of file timestamp.c.

References date2j(), j2day(), and result.

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

4098 {
4099  float8 result;
4100  int day0,
4101  day4,
4102  dayn;
4103 
4104  /* current day */
4105  dayn = date2j(year, mon, mday);
4106 
4107  /* fourth day of current year */
4108  day4 = date2j(year, 1, 4);
4109 
4110  /* day0 == offset to first day of week (Monday) */
4111  day0 = j2day(day4 - 1);
4112 
4113  /*
4114  * We need the first week containing a Thursday, otherwise this day falls
4115  * into the previous year for purposes of counting weeks
4116  */
4117  if (dayn < day4 - day0)
4118  {
4119  day4 = date2j(year - 1, 1, 4);
4120 
4121  /* day0 == offset to first day of week (Monday) */
4122  day0 = j2day(day4 - 1);
4123 
4124  year--;
4125  }
4126 
4127  result = (dayn - (day4 - day0)) / 7 + 1;
4128 
4129  /*
4130  * Sometimes the last few days in a year will fall into the first week of
4131  * the next year, so check for this.
4132  */
4133  if (result >= 52)
4134  {
4135  day4 = date2j(year + 1, 1, 4);
4136 
4137  /* day0 == offset to first day of week (Monday) */
4138  day0 = j2day(day4 - 1);
4139 
4140  if (dayn >= day4 - day0)
4141  year++;
4142  }
4143 
4144  return year;
4145 }
return result
Definition: formatting.c:1618
double float8
Definition: c.h:381
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
int date2isoyearday ( int  year,
int  mon,
int  mday 
)

Definition at line 4154 of file timestamp.c.

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

Referenced by DCH_to_char().

4155 {
4156  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4157 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4097
int isoweek2j(int year, int week)
Definition: timestamp.c:3992
int date2j(int y, int m, int d)
Definition: datetime.c:292
static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 1951 of file timestamp.c.

References USECS_PER_SEC.

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

1952 {
1953  dt -= (tz * USECS_PER_SEC);
1954  return dt;
1955 }
#define USECS_PER_SEC
Definition: timestamp.h:94
void dt2time ( Timestamp  jd,
int *  hour,
int *  min,
int *  sec,
fsec_t fsec 
)

Definition at line 1730 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1731 {
1732  TimeOffset time;
1733 
1734  time = jd;
1735 
1736  *hour = time / USECS_PER_HOUR;
1737  time -= (*hour) * USECS_PER_HOUR;
1738  *min = time / USECS_PER_MINUTE;
1739  time -= (*min) * USECS_PER_MINUTE;
1740  *sec = time / USECS_PER_SEC;
1741  *fsec = time - (*sec * USECS_PER_SEC);
1742 } /* dt2time() */
#define USECS_PER_SEC
Definition: timestamp.h:94
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int64 TimeOffset
Definition: timestamp.h:40
void EncodeSpecialTimestamp ( Timestamp  dt,
char *  str 
)

Definition at line 1522 of file timestamp.c.

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

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

1523 {
1524  if (TIMESTAMP_IS_NOBEGIN(dt))
1525  strcpy(str, EARLY);
1526  else if (TIMESTAMP_IS_NOEND(dt))
1527  strcpy(str, LATE);
1528  else /* shouldn't happen */
1529  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1530 }
#define LATE
Definition: datetime.h:41
#define ERROR
Definition: elog.h:43
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define elog
Definition: elog.h:219
#define EARLY
Definition: datetime.h:40
Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 710 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, result, rint(), SECS_PER_DAY, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

711 {
712  float8 seconds = PG_GETARG_FLOAT8(0);
714 
715  /* Deal with NaN and infinite inputs ... */
716  if (isnan(seconds))
717  ereport(ERROR,
718  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
719  errmsg("timestamp cannot be NaN")));
720 
721  if (isinf(seconds))
722  {
723  if (seconds < 0)
724  TIMESTAMP_NOBEGIN(result);
725  else
726  TIMESTAMP_NOEND(result);
727  }
728  else
729  {
730  /* Out of range? */
731  if (seconds <
733  || seconds >=
735  ereport(ERROR,
736  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
737  errmsg("timestamp out of range: \"%g\"", seconds)));
738 
739  /* Convert UNIX epoch to Postgres epoch */
741 
742  seconds = rint(seconds * USECS_PER_SEC);
743  result = (int64) seconds;
744 
745  /* Recheck in case roundoff produces something just out of range */
746  if (!IS_VALID_TIMESTAMP(result))
747  ereport(ERROR,
748  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
749  errmsg("timestamp out of range: \"%g\"",
750  PG_GETARG_FLOAT8(0))));
751  }
752 
753  PG_RETURN_TIMESTAMP(result);
754 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
int isinf(double x)
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define DATETIME_MIN_JULIAN
Definition: timestamp.h:179
Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 5106 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, result, 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.

5107 {
5108  FuncCallContext *funcctx;
5110  Timestamp result;
5111 
5112  /* stuff done only on the first call of the function */
5113  if (SRF_IS_FIRSTCALL())
5114  {
5115  Timestamp start = PG_GETARG_TIMESTAMP(0);
5116  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5117  Interval *step = PG_GETARG_INTERVAL_P(2);
5118  MemoryContext oldcontext;
5119  Interval interval_zero;
5120 
5121  /* create a function context for cross-call persistence */
5122  funcctx = SRF_FIRSTCALL_INIT();
5123 
5124  /*
5125  * switch to memory context appropriate for multiple function calls
5126  */
5127  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5128 
5129  /* allocate memory for user context */
5132 
5133  /*
5134  * Use fctx to keep state from call to call. Seed current with the
5135  * original start value
5136  */
5137  fctx->current = start;
5138  fctx->finish = finish;
5139  fctx->step = *step;
5140 
5141  /* Determine sign of the interval */
5142  MemSet(&interval_zero, 0, sizeof(Interval));
5143  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5144 
5145  if (fctx->step_sign == 0)
5146  ereport(ERROR,
5147  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5148  errmsg("step size cannot equal zero")));
5149 
5150  funcctx->user_fctx = fctx;
5151  MemoryContextSwitchTo(oldcontext);
5152  }
5153 
5154  /* stuff done on every call of the function */
5155  funcctx = SRF_PERCALL_SETUP();
5156 
5157  /*
5158  * get the saved state and use current as the result for this iteration
5159  */
5160  fctx = funcctx->user_fctx;
5161  result = fctx->current;
5162 
5163  if (fctx->step_sign > 0 ?
5164  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5165  timestamp_cmp_internal(result, fctx->finish) >= 0)
5166  {
5167  /* increment current in preparation for next iteration */
5168  fctx->current = DatumGetTimestamp(
5170  TimestampGetDatum(fctx->current),
5171  PointerGetDatum(&fctx->step)));
5172 
5173  /* do when there is more left to send */
5174  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5175  }
5176  else
5177  {
5178  /* do when there is no more left */
5179  SRF_RETURN_DONE(funcctx);
5180  }
5181 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2022
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:857
return result
Definition: formatting.c:1618
#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:2746
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define TimestampGetDatum(X)
Definition: timestamp.h:31
int64 Timestamp
Definition: timestamp.h:38
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5187 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, result, 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.

5188 {
5189  FuncCallContext *funcctx;
5192 
5193  /* stuff done only on the first call of the function */
5194  if (SRF_IS_FIRSTCALL())
5195  {
5197  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5198  Interval *step = PG_GETARG_INTERVAL_P(2);
5199  MemoryContext oldcontext;
5200  Interval interval_zero;
5201 
5202  /* create a function context for cross-call persistence */
5203  funcctx = SRF_FIRSTCALL_INIT();
5204 
5205  /*
5206  * switch to memory context appropriate for multiple function calls
5207  */
5208  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5209 
5210  /* allocate memory for user context */
5213 
5214  /*
5215  * Use fctx to keep state from call to call. Seed current with the
5216  * original start value
5217  */
5218  fctx->current = start;
5219  fctx->finish = finish;
5220  fctx->step = *step;
5221 
5222  /* Determine sign of the interval */
5223  MemSet(&interval_zero, 0, sizeof(Interval));
5224  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5225 
5226  if (fctx->step_sign == 0)
5227  ereport(ERROR,
5228  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5229  errmsg("step size cannot equal zero")));
5230 
5231  funcctx->user_fctx = fctx;
5232  MemoryContextSwitchTo(oldcontext);
5233  }
5234 
5235  /* stuff done on every call of the function */
5236  funcctx = SRF_PERCALL_SETUP();
5237 
5238  /*
5239  * get the saved state and use current as the result for this iteration
5240  */
5241  fctx = funcctx->user_fctx;
5242  result = fctx->current;
5243 
5244  if (fctx->step_sign > 0 ?
5245  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5246  timestamp_cmp_internal(result, fctx->finish) >= 0)
5247  {
5248  /* increment current in preparation for next iteration */
5249  fctx->current = DatumGetTimestampTz(
5252  PointerGetDatum(&fctx->step)));
5253 
5254  /* do when there is more left to send */
5255  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5256  }
5257  else
5258  {
5259  /* do when there is no more left */
5260  SRF_RETURN_DONE(funcctx);
5261  }
5262 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2022
int64 TimestampTz
Definition: timestamp.h:39
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:562
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:857
return result
Definition: formatting.c:1618
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define ERROR
Definition: elog.h:43
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:2851
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1569 of file timestamp.c.

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

Referenced by ApplyLauncherMain(), 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(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), launcher_determine_sleep(), lazy_vacuum_rel(), log_disconnections(), LogCheckpointEnd(), LogicalRepApplyLoop(), maybe_start_bgworker(), perform_base_backup(), 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(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), reset_dbentry_counters(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), SetCurrentTransactionStopTimestamp(), StartupXLOG(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1570 {
1572  struct timeval tp;
1573 
1574  gettimeofday(&tp, NULL);
1575 
1576  result = (TimestampTz) tp.tv_sec -
1578  result = (result * USECS_PER_SEC) + tp.tv_usec;
1579 
1580  return result;
1581 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
return result
Definition: formatting.c:1618
#define SECS_PER_DAY
Definition: timestamp.h:86
#define NULL
Definition: c.h:229
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
void GetEpochTime ( struct pg_tm tm)

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

1984 {
1985  struct pg_tm *t0;
1986  pg_time_t epoch = 0;
1987 
1988  t0 = pg_gmtime(&epoch);
1989 
1990  tm->tm_year = t0->tm_year;
1991  tm->tm_mon = t0->tm_mon;
1992  tm->tm_mday = t0->tm_mday;
1993  tm->tm_hour = t0->tm_hour;
1994  tm->tm_min = t0->tm_min;
1995  tm->tm_sec = t0->tm_sec;
1996 
1997  tm->tm_year += 1900;
1998  tm->tm_mon++;
1999 }
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 1587 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

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

Definition at line 1601 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1602 {
1603  Timestamp ts;
1604 
1606  if (typmod >= 0)
1607  AdjustTimestampForTypmod(&ts, typmod);
1608  return ts;
1609 }
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:333
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:4957
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:708
int interval2tm ( Interval  span,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 1901 of file timestamp.c.

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, MONTHS_PER_YEAR, SAMESIGN, Interval::time, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1902 {
1903  TimeOffset time;
1904  TimeOffset tfrac;
1905 
1906  tm->tm_year = span.month / MONTHS_PER_YEAR;
1907  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1908  tm->tm_mday = span.day;
1909  time = span.time;
1910 
1911  tfrac = time / USECS_PER_HOUR;
1912  time -= tfrac * USECS_PER_HOUR;
1913  tm->tm_hour = tfrac;
1914  if (!SAMESIGN(tm->tm_hour, tfrac))
1915  ereport(ERROR,
1916  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1917  errmsg("interval out of range")));
1918  tfrac = time / USECS_PER_MINUTE;
1919  time -= tfrac * USECS_PER_MINUTE;
1920  tm->tm_min = tfrac;
1921  tfrac = time / USECS_PER_SEC;
1922  *fsec = time - (tfrac * USECS_PER_SEC);
1923  tm->tm_sec = tfrac;
1924 
1925  return 0;
1926 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int errcode(int sqlerrcode)
Definition: elog.c:575
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USECS_PER_HOUR
Definition: timestamp.h:92
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
Datum interval_accum ( PG_FUNCTION_ARGS  )

Definition at line 3223 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, result, and Interval::time.

3224 {
3225  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3227  Datum *transdatums;
3228  int ndatums;
3229  Interval sumX,
3230  N;
3231  Interval *newsum;
3232  ArrayType *result;
3233 
3234  deconstruct_array(transarray,
3235  INTERVALOID, sizeof(Interval), false, 'd',
3236  &transdatums, NULL, &ndatums);
3237  if (ndatums != 2)
3238  elog(ERROR, "expected 2-element interval array");
3239 
3240  sumX = *(DatumGetIntervalP(transdatums[0]));
3241  N = *(DatumGetIntervalP(transdatums[1]));
3242 
3244  IntervalPGetDatum(&sumX),
3245  IntervalPGetDatum(newval)));
3246  N.time += 1;
3247 
3248  transdatums[0] = IntervalPGetDatum(newsum);
3249  transdatums[1] = IntervalPGetDatum(&N);
3250 
3251  result = construct_array(transdatums, 2,
3252  INTERVALOID, sizeof(Interval), false, 'd');
3253 
3254  PG_RETURN_ARRAYTYPE_P(result);
3255 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
return result
Definition: formatting.c:1618
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3010
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:525
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
#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:586
Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3307 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, result, and Interval::time.

3308 {
3309  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3311  Datum *transdatums;
3312  int ndatums;
3313  Interval sumX,
3314  N;
3315  Interval *newsum;
3316  ArrayType *result;
3317 
3318  deconstruct_array(transarray,
3319  INTERVALOID, sizeof(Interval), false, 'd',
3320  &transdatums, NULL, &ndatums);
3321  if (ndatums != 2)
3322  elog(ERROR, "expected 2-element interval array");
3323 
3324  sumX = *(DatumGetIntervalP(transdatums[0]));
3325  N = *(DatumGetIntervalP(transdatums[1]));
3326 
3328  IntervalPGetDatum(&sumX),
3329  IntervalPGetDatum(newval)));
3330  N.time -= 1;
3331 
3332  transdatums[0] = IntervalPGetDatum(newsum);
3333  transdatums[1] = IntervalPGetDatum(&N);
3334 
3335  result = construct_array(transdatums, 2,
3336  INTERVALOID, sizeof(Interval), false, 'd');
3337 
3338  PG_RETURN_ARRAYTYPE_P(result);
3339 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
return result
Definition: formatting.c:1618
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3044
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:525
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
#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:586
Datum interval_avg ( PG_FUNCTION_ARGS  )

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

3343 {
3344  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3345  Datum *transdatums;
3346  int ndatums;
3347  Interval sumX,
3348  N;
3349 
3350  deconstruct_array(transarray,
3351  INTERVALOID, sizeof(Interval), false, 'd',
3352  &transdatums, NULL, &ndatums);
3353  if (ndatums != 2)
3354  elog(ERROR, "expected 2-element interval array");
3355 
3356  sumX = *(DatumGetIntervalP(transdatums[0]));
3357  N = *(DatumGetIntervalP(transdatums[1]));
3358 
3359  /* SQL defines AVG of no values to be NULL */
3360  if (N.time == 0)
3361  PG_RETURN_NULL();
3362 
3364  IntervalPGetDatum(&sumX),
3365  Float8GetDatum((double) N.time));
3366 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:2176
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:525
TimeOffset time
Definition: timestamp.h:45
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3171
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:586
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2368 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp().

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

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

2306 {
2307  TimeOffset span1 = interval_cmp_value(interval1);
2308  TimeOffset span2 = interval_cmp_value(interval2);
2309 
2310  return ((span1 < span2) ? -1 : (span1 > span2) ? 1 : 0);
2311 }
static TimeOffset interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2293
int64 TimeOffset
Definition: timestamp.h:40
static TimeOffset interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2293 of file timestamp.c.

References Interval::day, INT64CONST, Interval::month, Interval::time, USECS_PER_DAY, and USECS_PER_HOUR.

Referenced by interval_cmp_internal(), and interval_hash().

2294 {
2295  TimeOffset span;
2296 
2297  span = interval->time;
2298  span += interval->month * INT64CONST(30) * USECS_PER_DAY;
2299  span += interval->day * INT64CONST(24) * USECS_PER_HOUR;
2300 
2301  return span;
2302 }
int32 day
Definition: timestamp.h:47
#define USECS_PER_HOUR
Definition: timestamp.h:92
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define INT64CONST(x)
Definition: c.h:310
Datum interval_combine ( PG_FUNCTION_ARGS  )

Definition at line 3258 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, result, and Interval::time.

3259 {
3260  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3261  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3262  Datum *transdatums1;
3263  Datum *transdatums2;
3264  int ndatums1;
3265  int ndatums2;
3266  Interval sum1,
3267  N1;
3268  Interval sum2,
3269  N2;
3270 
3271  Interval *newsum;
3272  ArrayType *result;
3273 
3274  deconstruct_array(transarray1,
3275  INTERVALOID, sizeof(Interval), false, 'd',
3276  &transdatums1, NULL, &ndatums1);
3277  if (ndatums1 != 2)
3278  elog(ERROR, "expected 2-element interval array");
3279 
3280  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3281  N1 = *(DatumGetIntervalP(transdatums1[1]));
3282 
3283  deconstruct_array(transarray2,
3284  INTERVALOID, sizeof(Interval), false, 'd',
3285  &transdatums2, NULL, &ndatums2);
3286  if (ndatums2 != 2)
3287  elog(ERROR, "expected 2-element interval array");
3288 
3289  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3290  N2 = *(DatumGetIntervalP(transdatums2[1]));
3291 
3293  IntervalPGetDatum(&sum1),
3294  IntervalPGetDatum(&sum2)));
3295  N1.time += N2.time;
3296 
3297  transdatums1[0] = IntervalPGetDatum(newsum);
3298  transdatums1[1] = IntervalPGetDatum(&N1);
3299 
3300  result = construct_array(transdatums1, 2,
3301  INTERVALOID, sizeof(Interval), false, 'd');
3302 
3303  PG_RETURN_ARRAYTYPE_P(result);
3304 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
return result
Definition: formatting.c:1618
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3010
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:525
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
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:586
Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3171 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, result, rint(), SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_avg().

3172 {
3173  Interval *span = PG_GETARG_INTERVAL_P(0);
3174  float8 factor = PG_GETARG_FLOAT8(1);
3175  double month_remainder_days,
3176  sec_remainder;
3177  int32 orig_month = span->month,
3178  orig_day = span->day;
3179  Interval *result;
3180 
3181  result = (Interval *) palloc(sizeof(Interval));
3182 
3183  if (factor == 0.0)
3184  ereport(ERROR,
3185  (errcode(ERRCODE_DIVISION_BY_ZERO),
3186  errmsg("division by zero")));
3187 
3188  result->month = (int32) (span->month / factor);
3189  result->day = (int32) (span->day / factor);
3190 
3191  /*
3192  * Fractional months full days into days. See comment in interval_mul().
3193  */
3194  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3195  month_remainder_days = TSROUND(month_remainder_days);
3196  sec_remainder = (orig_day / factor - result->day +
3197  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3198  sec_remainder = TSROUND(sec_remainder);
3199  if (Abs(sec_remainder) >= SECS_PER_DAY)
3200  {
3201  result->day += (int) (sec_remainder / SECS_PER_DAY);
3202  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3203  }
3204 
3205  /* cascade units down */
3206  result->day += (int32) month_remainder_days;
3207  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3208 
3209  PG_RETURN_INTERVAL_P(result);
3210 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:812
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2314 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

2315 {
2316  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2317  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2318 
2319  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2320 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 1972 of file timestamp.c.

References PG_RETURN_BOOL.

1973 {
1974  PG_RETURN_BOOL(true);
1975 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum interval_ge ( PG_FUNCTION_ARGS  )

Definition at line 2359 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

2360 {
2361  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2362  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2363 
2364  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2365 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2341 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2342 {
2343  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2344  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2345 
2346  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2347 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2384 of file timestamp.c.

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

2385 {
2387  TimeOffset span = interval_cmp_value(interval);
2388 
2390 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
static TimeOffset interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2293
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:62
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
int64 TimeOffset
Definition: timestamp.h:40
#define Int64GetDatumFast(X)
Definition: postgres.h:781
Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 875 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(), result, 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().

876 {
877  char *str = PG_GETARG_CSTRING(0);
878 
879 #ifdef NOT_USED
880  Oid typelem = PG_GETARG_OID(1);
881 #endif
882  int32 typmod = PG_GETARG_INT32(2);
883  Interval *result;
884  fsec_t fsec;
885  struct pg_tm tt,
886  *tm = &tt;
887  int dtype;
888  int nf;
889  int range;
890  int dterr;
891  char *field[MAXDATEFIELDS];
892  int ftype[MAXDATEFIELDS];
893  char workbuf[256];
894 
895  tm->tm_year = 0;
896  tm->tm_mon = 0;
897  tm->tm_mday = 0;
898  tm->tm_hour = 0;
899  tm->tm_min = 0;
900  tm->tm_sec = 0;
901  fsec = 0;
902 
903  if (typmod >= 0)
904  range = INTERVAL_RANGE(typmod);
905  else
906  range = INTERVAL_FULL_RANGE;
907 
908  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
909  ftype, MAXDATEFIELDS, &nf);
910  if (dterr == 0)
911  dterr = DecodeInterval(field, ftype, nf, range,
912  &dtype, tm, &fsec);
913 
914  /* if those functions think it's a bad format, try ISO8601 style */
915  if (dterr == DTERR_BAD_FORMAT)
916  dterr = DecodeISO8601Interval(str,
917  &dtype, tm, &fsec);
918 
919  if (dterr != 0)
920  {
921  if (dterr == DTERR_FIELD_OVERFLOW)
922  dterr = DTERR_INTERVAL_OVERFLOW;
923  DateTimeParseError(dterr, str, "interval");
924  }
925 
926  result = (Interval *) palloc(sizeof(Interval));
927 
928  switch (dtype)
929  {
930  case DTK_DELTA:
931  if (tm2interval(tm, fsec, result) != 0)
932  ereport(ERROR,
933  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
934  errmsg("interval out of range")));
935  break;
936 
937  case DTK_INVALID:
938  ereport(ERROR,
939  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
940  errmsg("date/time value \"%s\" is no longer supported", str)));
941  break;
942 
943  default:
944  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
945  dtype, str);
946  }
947 
948  AdjustIntervalForTypmod(result, typmod);
949 
950  PG_RETURN_INTERVAL_P(result);
951 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:282
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
void DateTimeParseError(int dterr, const char *str, const char *datatype)
Definition: datetime.c:3765
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define DTK_DELTA
Definition: datetime.h:162
int tm_hour
Definition: pgtime.h:29
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define DTK_INVALID
Definition: datetime.h:151
signed int int32
Definition: c.h:256
static struct pg_tm tm
Definition: localtime.c:103
#define ERROR
Definition: elog.h:43
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:283
#define DTERR_INTERVAL_OVERFLOW
Definition: datetime.h:285
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1319
int tm_mday
Definition: pgtime.h:30
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
#define ereport(elevel, rest)
Definition: elog.h:122
int DecodeISO8601Interval(char *str, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3521
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define MAXDATEFIELDS
Definition: datetime.h:205
int tm_year
Definition: pgtime.h:32
void * palloc(Size size)
Definition: mcxt.c:849
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:3095
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#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:562
int tm_min
Definition: pgtime.h:28
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1929
Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2706 of file timestamp.c.

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

2707 {
2708  Interval *span = PG_GETARG_INTERVAL_P(0);
2709  Interval *result;
2710  int32 wholemonth;
2711 
2712  result = (Interval *) palloc(sizeof(Interval));
2713  result->month = span->month;
2714  result->day = span->day;
2715  result->time = span->time;
2716 
2717  wholemonth = result->day / DAYS_PER_MONTH;
2718  result->day -= wholemonth * DAYS_PER_MONTH;
2719  result->month += wholemonth;
2720 
2721  if (result->month > 0 && result->day < 0)
2722  {
2723  result->day += DAYS_PER_MONTH;
2724  result->month--;
2725  }
2726  else if (result->month < 0 && result->day > 0)
2727  {
2728  result->day -= DAYS_PER_MONTH;
2729  result->month++;
2730  }
2731 
2732  PG_RETURN_INTERVAL_P(result);
2733 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:849
Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

Definition at line 2671 of file timestamp.c.

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

Referenced by timestamp_mi().

2672 {
2673  Interval *span = PG_GETARG_INTERVAL_P(0);
2674  Interval *result;
2675  TimeOffset wholeday;
2676 
2677  result = (Interval *) palloc(sizeof(Interval));
2678  result->month = span->month;
2679  result->day = span->day;
2680  result->time = span->time;
2681 
2682  TMODULO(result->time, wholeday, USECS_PER_DAY);
2683  result->day += wholeday; /* could overflow... */
2684 
2685  if (result->day > 0 && result->time < 0)
2686  {
2687  result->time += USECS_PER_DAY;
2688  result->day--;
2689  }
2690  else if (result->day < 0 && result->time > 0)
2691  {
2692  result->time -= USECS_PER_DAY;
2693  result->day++;
2694  }
2695 
2696  PG_RETURN_INTERVAL_P(result);
2697 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1618
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
void * palloc(Size size)
Definition: mcxt.c:849
#define TMODULO(t, q, u)
Definition: datetime.h:251
Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2616 of file timestamp.c.

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

2617 {
2618  Interval *span = PG_GETARG_INTERVAL_P(0);
2619  Interval *result;
2620  TimeOffset wholeday;
2621  int32 wholemonth;
2622 
2623  result = (Interval *) palloc(sizeof(Interval));
2624  result->month = span->month;
2625  result->day = span->day;
2626  result->time = span->time;
2627 
2628  TMODULO(result->time, wholeday, USECS_PER_DAY);
2629  result->day += wholeday; /* could overflow... */
2630 
2631  wholemonth = result->day / DAYS_PER_MONTH;
2632  result->day -= wholemonth * DAYS_PER_MONTH;
2633  result->month += wholemonth;
2634 
2635  if (result->month > 0 &&
2636  (result->day < 0 || (result->day == 0 && result->time < 0)))
2637  {
2638  result->day += DAYS_PER_MONTH;
2639  result->month--;
2640  }
2641  else if (result->month < 0 &&
2642  (result->day > 0 || (result->day == 0 && result->time > 0)))
2643  {
2644  result->day -= DAYS_PER_MONTH;
2645  result->month++;
2646  }
2647 
2648  if (result->day > 0 && result->time < 0)
2649  {
2650  result->time += USECS_PER_DAY;
2651  result->day--;
2652  }
2653  else if (result->day < 0 && result->time > 0)
2654  {
2655  result->time -= USECS_PER_DAY;
2656  result->day++;
2657  }
2658 
2659  PG_RETURN_INTERVAL_P(result);
2660 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:849
#define TMODULO(t, q, u)
Definition: datetime.h:251
Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 2996 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, and result.

2997 {
2998  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2999  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3000  Interval *result;
3001 
3002  if (interval_cmp_internal(interval1, interval2) > 0)
3003  result = interval1;
3004  else
3005  result = interval2;
3006  PG_RETURN_INTERVAL_P(result);
3007 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1618
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2350 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

2351 {
2352  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2353  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2354 
2355  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2356 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2332 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2333 {
2334  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2335  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2336 
2337  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2338 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3044 of file timestamp.c.

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, result, SAMESIGN, and Interval::time.

Referenced by interval_accum_inv(), interval_dist(), and interval_lerp().

3045 {
3046  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3047  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3048  Interval *result;
3049 
3050  result = (Interval *) palloc(sizeof(Interval));
3051 
3052  result->month = span1->month - span2->month;
3053  /* overflow check copied from int4mi */
3054  if (!SAMESIGN(span1->month, span2->month) &&
3055  !SAMESIGN(result->month, span1->month))
3056  ereport(ERROR,
3057  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3058  errmsg("interval out of range")));
3059 
3060  result->day = span1->day - span2->day;
3061  if (!SAMESIGN(span1->day, span2->day) &&
3062  !SAMESIGN(result->day, span1->day))
3063  ereport(ERROR,
3064  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3065  errmsg("interval out of range")));
3066 
3067  result->time = span1->time - span2->time;
3068  if (!SAMESIGN(span1->time, span2->time) &&
3069  !SAMESIGN(result->time, span1->time))
3070  ereport(ERROR,
3071  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3072  errmsg("interval out of range")));
3073 
3074  PG_RETURN_INTERVAL_P(result);
3075 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3084 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, result, rint(), SECS_PER_DAY, Interval::time, TSROUND, and USECS_PER_SEC.

Referenced by interval_lerp(), and mul_d_interval().

3085 {
3086  Interval *span = PG_GETARG_INTERVAL_P(0);
3087  float8 factor = PG_GETARG_FLOAT8(1);
3088  double month_remainder_days,
3089  sec_remainder,
3090  result_double;
3091  int32 orig_month = span->month,
3092  orig_day = span->day;
3093  Interval *result;
3094 
3095  result = (Interval *) palloc(sizeof(Interval));
3096 
3097  result_double = span->month * factor;
3098  if (isnan(result_double) ||
3099  result_double > INT_MAX || result_double < INT_MIN)
3100  ereport(ERROR,
3101  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3102  errmsg("interval out of range")));
3103  result->month = (int32) result_double;
3104 
3105  result_double = span->day * factor;
3106  if (isnan(result_double) ||
3107  result_double > INT_MAX || result_double < INT_MIN)
3108  ereport(ERROR,
3109  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3110  errmsg("interval out of range")));
3111  result->day = (int32) result_double;
3112 
3113  /*
3114  * The above correctly handles the whole-number part of the month and day
3115  * products, but we have to do something with any fractional part
3116  * resulting when the factor is non-integral. We cascade the fractions
3117  * down to lower units using the conversion factors DAYS_PER_MONTH and
3118  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3119  * so by the representation. The user can choose to cascade up later,
3120  * using justify_hours and/or justify_days.
3121  */
3122 
3123  /*
3124  * Fractional months full days into days.
3125  *
3126  * Floating point calculation are inherently imprecise, so these
3127  * calculations are crafted to produce the most reliable result possible.
3128  * TSROUND() is needed to more accurately produce whole numbers where
3129  * appropriate.
3130  */
3131  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3132  month_remainder_days = TSROUND(month_remainder_days);
3133  sec_remainder = (orig_day * factor - result->day +
3134  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3135  sec_remainder = TSROUND(sec_remainder);
3136 
3137  /*
3138  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3139  * cascade from months and days. It might still be >24 if the combination
3140  * of cascade and the seconds factor operation itself.
3141  */
3142  if (Abs(sec_remainder) >= SECS_PER_DAY)
3143  {
3144  result->day += (int) (sec_remainder / SECS_PER_DAY);
3145  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3146  }
3147 
3148  /* cascade units down */
3149  result->day += (int32) month_remainder_days;
3150  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3151  if (result_double > PG_INT64_MAX || result_double < PG_INT64_MIN)
3152  ereport(ERROR,
3153  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3154  errmsg("interval out of range")));
3155  result->time = (int64) result_double;
3156 
3157  PG_RETURN_INTERVAL_P(result);
3158 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_INT64_MAX
Definition: c.h:343
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:812
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
#define PG_INT64_MIN
Definition: c.h:342
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2323 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2324 {
2325  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2326  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2327 
2328  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2329 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 957 of file timestamp.c.

References buf, elog, EncodeInterval(), ERROR, interval2tm(), IntervalStyle, MAXDATELEN, PG_GETARG_INTERVAL_P, PG_RETURN_CSTRING, pstrdup(), result, and tm.

Referenced by flatten_set_variable_args(), timestamp_izone(), timestamptz_izone(), and timetz_izone().

958 {
959  Interval *span = PG_GETARG_INTERVAL_P(0);
960  char *result;
961  struct pg_tm tt,
962  *tm = &tt;
963  fsec_t fsec;
964  char buf[MAXDATELEN + 1];
965 
966  if (interval2tm(*span, tm, &fsec) != 0)
967  elog(ERROR, "could not convert interval to tm");
968 
969  EncodeInterval(tm, fsec, IntervalStyle, buf);
970 
971  result = pstrdup(buf);
972  PG_RETURN_CSTRING(result);
973 }
#define MAXDATELEN
Definition: datetime.h:203
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int IntervalStyle
Definition: globals.c:108
return result
Definition: formatting.c:1618
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:1901
static char * buf
Definition: pg_test_fsync.c:65
int32 fsec_t
Definition: timestamp.h:41
void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str)
Definition: datetime.c:4241
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
#define elog
Definition: elog.h:219
Datum interval_part ( PG_FUNCTION_ARGS  )

Definition at line 4643 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, result, 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.

4644 {
4645  text *units = PG_GETARG_TEXT_PP(0);
4647  float8 result;
4648  int type,
4649  val;
4650  char *lowunits;
4651  fsec_t fsec;
4652  struct pg_tm tt,
4653  *tm = &tt;
4654 
4655  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4656  VARSIZE_ANY_EXHDR(units),
4657  false);
4658 
4659  type = DecodeUnits(0, lowunits, &val);
4660  if (type == UNKNOWN_FIELD)
4661  type = DecodeSpecial(0, lowunits, &val);
4662 
4663  if (type == UNITS)
4664  {
4665  if (interval2tm(*interval, tm, &fsec) == 0)
4666  {
4667  switch (val)
4668  {
4669  case DTK_MICROSEC:
4670  result = tm->tm_sec * 1000000.0 + fsec;
4671  break;
4672 
4673  case DTK_MILLISEC:
4674  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4675  break;
4676 
4677  case DTK_SECOND:
4678  result = tm->tm_sec + fsec / 1000000.0;
4679  break;
4680 
4681  case DTK_MINUTE:
4682  result = tm->tm_min;
4683  break;
4684 
4685  case DTK_HOUR:
4686  result = tm->tm_hour;
4687  break;
4688 
4689  case DTK_DAY:
4690  result = tm->tm_mday;
4691  break;
4692 
4693  case DTK_MONTH:
4694  result = tm->tm_mon;
4695  break;
4696 
4697  case DTK_QUARTER:
4698  result = (tm->tm_mon / 3) + 1;
4699  break;
4700 
4701  case DTK_YEAR:
4702  result = tm->tm_year;
4703  break;
4704 
4705  case DTK_DECADE:
4706  /* caution: C division may have negative remainder */
4707  result = tm->tm_year / 10;
4708  break;
4709 
4710  case DTK_CENTURY:
4711  /* caution: C division may have negative remainder */
4712  result = tm->tm_year / 100;
4713  break;
4714 
4715  case DTK_MILLENNIUM:
4716  /* caution: C division may have negative remainder */
4717  result = tm->tm_year / 1000;
4718  break;
4719 
4720  default:
4721  ereport(ERROR,
4722  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4723  errmsg("interval units \"%s\" not supported",
4724  lowunits)));
4725  result = 0;
4726  }
4727 
4728  }
4729  else
4730  {
4731  elog(ERROR, "could not convert interval to tm");
4732  result = 0;
4733  }
4734  }
4735  else if (type == RESERV && val == DTK_EPOCH)
4736  {
4737  result = interval->time / 1000000.0;
4738  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
4739  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
4740  result += ((double) SECS_PER_DAY) * interval->day;
4741  }
4742  else
4743  {
4744  ereport(ERROR,
4745  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4746  errmsg("interval units \"%s\" not recognized",
4747  lowunits)));
4748  result = 0;
4749  }
4750 
4751  PG_RETURN_FLOAT8(result);
4752 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#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:326
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define UNKNOWN_FIELD
Definition: datetime.h:125
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3728
Definition: pgtime.h:25
int32 day
Definition: timestamp.h:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
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:69
#define DTK_DECADE
Definition: datetime.h:171
#define DAYS_PER_YEAR
Definition: timestamp.h:68
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
#define DTK_SECOND
Definition: datetime.h:163
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1901
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
TimeOffset time
Definition: timestamp.h:45
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
#define DTK_MINUTE
Definition: datetime.h:164
int32 month
Definition: timestamp.h:48
#define DTK_MICROSEC
Definition: datetime.h:175
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define DTK_DAY
Definition: datetime.h:166
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3039
#define DTK_EPOCH
Definition: datetime.h:155
int tm_year
Definition: pgtime.h:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#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 3010 of file timestamp.c.

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, result, SAMESIGN, and Interval::time.

Referenced by interval_accum(), interval_combine(), and interval_lerp().

3011 {
3012  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3013  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3014  Interval *result;
3015 
3016  result = (Interval *) palloc(sizeof(Interval));
3017 
3018  result->month = span1->month + span2->month;
3019  /* overflow check copied from int4pl */
3020  if (SAMESIGN(span1->month, span2->month) &&
3021  !SAMESIGN(result->month, span1->month))
3022  ereport(ERROR,
3023  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3024  errmsg("interval out of range")));
3025 
3026  result->day = span1->day + span2->day;
3027  if (SAMESIGN(span1->day, span2->day) &&
3028  !SAMESIGN(result->day, span1->day))
3029  ereport(ERROR,
3030  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3031  errmsg("interval out of range")));
3032 
3033  result->time = span1->time + span2->time;
3034  if (SAMESIGN(span1->time, span2->time) &&
3035  !SAMESIGN(result->time, span1->time))
3036  ereport(ERROR,
3037  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3038  errmsg("interval out of range")));
3039 
3040  PG_RETURN_INTERVAL_P(result);
3041 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_recv ( PG_FUNCTION_ARGS  )

Definition at line 979 of file timestamp.c.

References AdjustIntervalForTypmod(), buf, Interval::day, Interval::month, palloc(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_INTERVAL_P, pq_getmsgint(), pq_getmsgint64(), and Interval::time.

980 {
982 
983 #ifdef NOT_USED
984  Oid typelem = PG_GETARG_OID(1);
985 #endif
986  int32 typmod = PG_GETARG_INT32(2);
988 
989  interval = (Interval *) palloc(sizeof(Interval));
990 
991  interval->time = pq_getmsgint64(buf);
992  interval->day = pq_getmsgint(buf, sizeof(interval->day));
993  interval->month = pq_getmsgint(buf, sizeof(interval->month));
994 
995  AdjustIntervalForTypmod(interval, typmod);
996 
997  PG_RETURN_INTERVAL_P(interval);
998 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1319
static char * buf
Definition: pg_test_fsync.c:65
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:849
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 1300 of file timestamp.c.

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

1301 {
1303  int32 typmod = PG_GETARG_INT32(1);
1304  Interval *result;
1305 
1306  result = palloc(sizeof(Interval));
1307  *result = *interval;
1308 
1309  AdjustIntervalForTypmod(result, typmod);
1310 
1311  PG_RETURN_INTERVAL_P(result);
1312 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1319
void * palloc(Size size)
Definition: mcxt.c:849
Datum interval_send ( PG_FUNCTION_ARGS  )

Definition at line 1004 of file timestamp.c.

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

1005 {
1008 
1009  pq_begintypsend(&buf);
1010  pq_sendint64(&buf, interval->time);
1011  pq_sendint(&buf, interval->day, sizeof(interval->day));
1012  pq_sendint(&buf, interval->month, sizeof(interval->month));
1014 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
int32 day
Definition: timestamp.h:47
static char * buf
Definition: pg_test_fsync.c:65
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
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 2981 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, and result.

2982 {
2983  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2984  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2985  Interval *result;
2986 
2987  /* use interval_cmp_internal to be sure this agrees with comparisons */
2988  if (interval_cmp_internal(interval1, interval2) < 0)
2989  result = interval1;
2990  else
2991  result = interval2;
2992  PG_RETURN_INTERVAL_P(result);
2993 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1618
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2305
Datum interval_transform ( PG_FUNCTION_ARGS  )

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

1244 {
1246  Node *ret = NULL;
1247  Node *typmod;
1248 
1249  Assert(list_length(expr->args) >= 2);
1250 
1251  typmod = (Node *) lsecond(expr->args);
1252 
1253  if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
1254  {
1255  Node *source = (Node *) linitial(expr->args);
1256  int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
1257  bool noop;
1258 
1259  if (new_typmod < 0)
1260  noop = true;
1261  else
1262  {
1263  int32 old_typmod = exprTypmod(source);
1264  int old_least_field;
1265  int new_least_field;
1266  int old_precis;
1267  int new_precis;
1268 
1269  old_least_field = intervaltypmodleastfield(old_typmod);
1270  new_least_field = intervaltypmodleastfield(new_typmod);
1271  if (old_typmod < 0)
1272  old_precis = INTERVAL_FULL_PRECISION;
1273  else
1274  old_precis = INTERVAL_PRECISION(old_typmod);
1275  new_precis = INTERVAL_PRECISION(new_typmod);
1276 
1277  /*
1278  * Cast is a no-op if least field stays the same or decreases
1279  * while precision stays the same or increases. But precision,
1280  * which is to say, sub-second precision, only affects ranges that
1281  * include SECOND.
1282  */
1283  noop = (new_least_field <= old_least_field) &&
1284  (old_least_field > 0 /* SECOND */ ||
1285  new_precis >= MAX_INTERVAL_PRECISION ||
1286  new_precis >= old_precis);
1287  }
1288  if (noop)
1289  ret = relabel_to_typmod(source, new_typmod);
1290  }
1291 
1292  PG_RETURN_POINTER(ret);
1293 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
List * args
Definition: primnodes.h:456
#define DatumGetInt32(X)
Definition: postgres.h:478
#define castNode(_type_, nodeptr)
Definition: nodes.h:575
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:273
Definition: nodes.h:506
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define lsecond(l)
Definition: pg_list.h:114
signed int int32
Definition: c.h:256
#define linitial(l)
Definition: pg_list.h:110
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:54
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:53
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
static int intervaltypmodleastfield(int32 typmod)
Definition: timestamp.c:1194
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:585
Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 3896 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, result, 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.

3897 {
3898  text *units = PG_GETARG_TEXT_PP(0);
3900  Interval *result;
3901  int type,
3902  val;
3903  char *lowunits;
3904  fsec_t fsec;
3905  struct pg_tm tt,
3906  *tm = &tt;
3907 
3908  result = (Interval *) palloc(sizeof(Interval));
3909 
3910  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
3911  VARSIZE_ANY_EXHDR(units),
3912  false);
3913 
3914  type = DecodeUnits(0, lowunits, &val);
3915 
3916  if (type == UNITS)
3917  {
3918  if (interval2tm(*interval, tm, &fsec) == 0)
3919  {
3920  switch (val)
3921  {
3922  /* fall through */
3923  case DTK_MILLENNIUM:
3924  /* caution: C division may have negative remainder */
3925  tm->tm_year = (tm->tm_year / 1000) * 1000;
3926  case DTK_CENTURY:
3927  /* caution: C division may have negative remainder */
3928  tm->tm_year = (tm->tm_year / 100) * 100;
3929  case DTK_DECADE:
3930  /* caution: C division may have negative remainder */
3931  tm->tm_year = (tm->tm_year / 10) * 10;
3932  case DTK_YEAR:
3933  tm->tm_mon = 0;
3934  case DTK_QUARTER:
3935  tm->tm_mon = 3 * (tm->tm_mon / 3);
3936  case DTK_MONTH:
3937  tm->tm_mday = 0;
3938  case DTK_DAY:
3939  tm->tm_hour = 0;
3940  case DTK_HOUR:
3941  tm->tm_min = 0;
3942  case DTK_MINUTE:
3943  tm->tm_sec = 0;
3944  case DTK_SECOND:
3945  fsec = 0;
3946  break;
3947  case DTK_MILLISEC:
3948  fsec = (fsec / 1000) * 1000;
3949  break;
3950  case DTK_MICROSEC:
3951  break;
3952 
3953  default:
3954  if (val == DTK_WEEK)
3955  ereport(ERROR,
3956  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3957  errmsg("interval units \"%s\" not supported "
3958  "because months usually have fractional weeks",
3959  lowunits)));
3960  else
3961  ereport(ERROR,
3962  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3963  errmsg("interval units \"%s\" not supported",
3964  lowunits)));
3965  }
3966 
3967  if (tm2interval(tm, fsec, result) != 0)
3968  ereport(ERROR,
3969  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3970  errmsg("interval out of range")));
3971  }
3972  else
3973  elog(ERROR, "could not convert interval to tm");
3974  }
3975  else
3976  {
3977  ereport(ERROR,
3978  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3979  errmsg("interval units \"%s\" not recognized",
3980  lowunits)));
3981  }
3982 
3983  PG_RETURN_INTERVAL_P(result);
3984 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#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
return result
Definition: formatting.c:1618
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3728
Definition: pgtime.h:25
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
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:1901
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h: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:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#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:1929
Datum interval_um ( PG_FUNCTION_ARGS  )

Definition at line 2952 of file timestamp.c.

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, palloc(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, result, SAMESIGN, and Interval::time.

Referenced by abs_interval().

2953 {
2955  Interval *result;
2956 
2957  result = (Interval *) palloc(sizeof(Interval));
2958 
2959  result->time = -interval->time;
2960  /* overflow check copied from int4um */
2961  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
2962  ereport(ERROR,
2963  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2964  errmsg("interval out of range")));
2965  result->day = -interval->day;
2966  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
2967  ereport(ERROR,
2968  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2969  errmsg("interval out of range")));
2970  result->month = -interval->month;
2971  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
2972  ereport(ERROR,
2973  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2974  errmsg("interval out of range")));
2975 
2976  PG_RETURN_INTERVAL_P(result);
2977 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
int32 day
Definition: timestamp.h:47
#define ERROR
Definition: elog.h:43
TimeOffset time
Definition: timestamp.h:45
#define ereport(elevel, rest)
Definition: elog.h:122
#define SAMESIGN(a, b)
Definition: timestamp.c:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

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

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

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

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

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

1109 {
1110  int32 typmod = PG_GETARG_INT32(0);
1111  char *res = (char *) palloc(64);
1112  int fields;
1113  int precision;
1114  const char *fieldstr;
1115 
1116  if (typmod < 0)
1117  {
1118  *res = '\0';
1119  PG_RETURN_CSTRING(res);
1120  }
1121 
1122  fields = INTERVAL_RANGE(typmod);
1123  precision = INTERVAL_PRECISION(typmod);
1124 
1125  switch (fields)
1126  {
1127  case INTERVAL_MASK(YEAR):
1128  fieldstr = " year";
1129  break;
1130  case INTERVAL_MASK(MONTH):
1131  fieldstr = " month";
1132  break;
1133  case INTERVAL_MASK(DAY):
1134  fieldstr = " day";
1135  break;
1136  case INTERVAL_MASK(HOUR):
1137  fieldstr = " hour";
1138  break;
1139  case INTERVAL_MASK(MINUTE):
1140  fieldstr = " minute";
1141  break;
1142  case INTERVAL_MASK(SECOND):
1143  fieldstr = " second";
1144  break;
1146  fieldstr = " year to month";
1147  break;
1149  fieldstr = " day to hour";
1150  break;
1152  fieldstr = " day to minute";
1153  break;
1155  fieldstr = " day to second";
1156  break;
1158  fieldstr = " hour to minute";
1159  break;
1161  fieldstr = " hour to second";
1162  break;
1164  fieldstr = " minute to second";
1165  break;
1166  case INTERVAL_FULL_RANGE:
1167  fieldstr = "";
1168  break;
1169  default:
1170  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1171  fieldstr = "";
1172  break;
1173  }
1174 
1175  if (precision != INTERVAL_FULL_PRECISION)
1176  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1177  else
1178  snprintf(res, 64, "%s", fieldstr);
1179 
1180  PG_RETURN_CSTRING(res);
1181 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#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:48
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define MINUTE
Definition: datetime.h:102
#define MONTH
Definition: datetime.h:92
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:53
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
void * palloc(Size size)
Definition: mcxt.c:849
#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 4012 of file timestamp.c.

References isoweek2j(), and j2date().

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

4013 {
4014  j2date(isoweek2j(*year, woy), year, mon, mday);
4015 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:3992
int isoweek2j ( int  year,
int  week 
)

Definition at line 3992 of file timestamp.c.

References date2j(), and j2day().

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

3993 {
3994  int day0,
3995  day4;
3996 
3997  /* fourth day of current year */
3998  day4 = date2j(year, 1, 4);
3999 
4000  /* day0 == offset to first day of week (Monday) */
4001  day0 = j2day(day4 - 1);
4002 
4003  return ((week - 1) * 7) + (day4 - day0);
4004 }
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
void isoweekdate2date ( int  isoweek,
int  wday,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4025 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4026 {
4027  int jday;
4028 
4029  jday = isoweek2j(*year, isoweek);
4030  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4031  if (wday > 1)
4032  jday += wday - 2;
4033  else
4034  jday += 6;
4035  j2date(jday, year, mon, mday);
4036 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:3992
Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1486 of file timestamp.c.

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

1487 {
1488  int32 years = PG_GETARG_INT32(0);
1490  int32 weeks = PG_GETARG_INT32(2);
1491  int32 days = PG_GETARG_INT32(3);
1492  int32 hours = PG_GETARG_INT32(4);
1493  int32 mins = PG_GETARG_INT32(5);
1494  double secs = PG_GETARG_FLOAT8(6);
1495  Interval *result;
1496 
1497  /*
1498  * Reject out-of-range inputs. We really ought to check the integer
1499  * inputs as well, but it's not entirely clear what limits to apply.
1500  */
1501  if (isinf(secs) || isnan(secs))
1502  ereport(ERROR,
1503  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1504  errmsg("interval out of range")));
1505 
1506  result = (Interval *) palloc(sizeof(Interval));
1507  result->month = years * MONTHS_PER_YEAR + months;
1508  result->day = weeks * 7 + days;
1509 
1510  secs = rint(secs * USECS_PER_SEC);
1511  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1512  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1513  (int64) secs;
1514 
1515  PG_RETURN_INTERVAL_P(result);
1516 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
const char *const months[]
Definition: datetime.c:66
#define ERROR
Definition: elog.h:43
int isinf(double x)
#define SECS_PER_MINUTE
Definition: timestamp.h:88
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:69
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:87
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 629 of file timestamp.c.

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

630 {
631  int32 year = PG_GETARG_INT32(0);
632  int32 month = PG_GETARG_INT32(1);
633  int32 mday = PG_GETARG_INT32(2);
634  int32 hour = PG_GETARG_INT32(3);
635  int32 min = PG_GETARG_INT32(4);
636  float8 sec = PG_GETARG_FLOAT8(5);
638 
639  result = make_timestamp_internal(year, month, mday,
640  hour, min, sec);
641 
642  PG_RETURN_TIMESTAMP(result);
643 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:541
return result
Definition: formatting.c:1618
signed int int32
Definition: c.h:256
double float8
Definition: c.h:381
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
static Timestamp make_timestamp_internal ( int  year,
int  month,
int  day,
int  hour,
int  min,
double  sec 
)
static

Definition at line 541 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, result, rint(), SECS_PER_MINUTE, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, USECS_PER_DAY, USECS_PER_SEC, and ValidateDate().

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

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

Definition at line 649 of file timestamp.c.

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

650 {
651  int32 year = PG_GETARG_INT32(0);
652  int32 month = PG_GETARG_INT32(1);
653  int32 mday = PG_GETARG_INT32(2);
654  int32 hour = PG_GETARG_INT32(3);
655  int32 min = PG_GETARG_INT32(4);
656  float8 sec = PG_GETARG_FLOAT8(5);
658 
659  result = make_timestamp_internal(year, month, mday,
660  hour, min, sec);
661 
663 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:4917
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:541
return result
Definition: formatting.c:1618
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:256
double float8
Definition: c.h:381
int64 Timestamp
Definition: timestamp.h:38
Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 670 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, result, and timestamp2tm().

671 {
672  int32 year = PG_GETARG_INT32(0);
673  int32 month = PG_GETARG_INT32(1);
674  int32 mday = PG_GETARG_INT32(2);
675  int32 hour = PG_GETARG_INT32(3);
676  int32 min = PG_GETARG_INT32(4);
677  float8 sec = PG_GETARG_FLOAT8(5);
681  struct pg_tm tt;
682  int tz;
683  fsec_t fsec;
684 
685  timestamp = make_timestamp_internal(year, month, mday,
686  hour, min, sec);
687 
688  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
689  ereport(ERROR,
690  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
691  errmsg("timestamp out of range")));
692 
693  tz = parse_sane_timezone(&tt, zone);
694 
695  result = dt2local(timestamp, -tz);
696 
697  if (!IS_VALID_TIMESTAMP(result))
698  ereport(ERROR,
699  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
700  errmsg("timestamp out of range")));
701 
702  PG_RETURN_TIMESTAMPTZ(result);
703 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition: timestamp.c:541
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1757
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:256
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
int64 Timestamp
Definition: timestamp.h:38
#define NULL
Definition: c.h:229
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Definition: zic.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:1951
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:457
Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3161 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3162 {
3163  /* Args are float8 and Interval *, but leave them as generic Datum */
3164  Datum factor = PG_GETARG_DATUM(0);
3165  Datum span = PG_GETARG_DATUM(1);
3166 
3167  return DirectFunctionCall2(interval_mul, span, factor);
3168 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
uintptr_t Datum
Definition: postgres.h:372
Datum interval_mul(PG_FUNCTION_ARGS)
Definition: timestamp.c:3084
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static float8 NonFiniteTimestampTzPart ( int  type,
int  unit,
char *  lowunits,
bool  isNegative,
bool  isTz 
)
static

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

4173 {
4174  if ((type != UNITS) && (type != RESERV))
4175  {
4176  if (isTz)
4177  ereport(ERROR,
4178  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4179  errmsg("timestamp with time zone units \"%s\" not recognized",
4180  lowunits)));
4181  else
4182  ereport(ERROR,
4183  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4184  errmsg("timestamp units \"%s\" not recognized",
4185  lowunits)));
4186  }
4187 
4188  switch (unit)
4189  {
4190  /* Oscillating units */
4191  case DTK_MICROSEC:
4192  case DTK_MILLISEC:
4193  case DTK_SECOND:
4194  case DTK_MINUTE:
4195  case DTK_HOUR:
4196  case DTK_DAY:
4197  case DTK_MONTH:
4198  case DTK_QUARTER:
4199  case DTK_WEEK:
4200  case DTK_DOW:
4201  case DTK_ISODOW:
4202  case DTK_DOY:
4203  case DTK_TZ:
4204  case DTK_TZ_MINUTE:
4205  case DTK_TZ_HOUR:
4206  return 0.0;
4207 
4208  /* Monotonically-increasing units */
4209  case DTK_YEAR:
4210  case DTK_DECADE:
4211  case DTK_CENTURY:
4212  case DTK_MILLENNIUM:
4213  case DTK_JULIAN:
4214  case DTK_ISOYEAR:
4215  case DTK_EPOCH:
4216  if (isNegative)
4217  return -get_float8_infinity();
4218  else
4219  return get_float8_infinity();
4220 
4221  default:
4222  if (isTz)
4223  ereport(ERROR,
4224  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4225  errmsg("timestamp with time zone units \"%s\" not supported",
4226  lowunits)));
4227  else
4228  ereport(ERROR,
4229  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4230  errmsg("timestamp units \"%s\" not supported",
4231  lowunits)));
4232  return 0.0; /* keep compiler quiet */
4233  }
4234 }
#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 2399 of file timestamp.c.

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

2400 {
2401  /*
2402  * The arguments are Timestamps, but we leave them as generic Datums to
2403  * avoid unnecessary conversions between value and reference forms --- not
2404  * to mention possible dereferences of null pointers.
2405  */
2406  Datum ts1 = PG_GETARG_DATUM(0);
2407  Datum te1 = PG_GETARG_DATUM(1);
2408  Datum ts2 = PG_GETARG_DATUM(2);
2409  Datum te2 = PG_GETARG_DATUM(3);
2410  bool ts1IsNull = PG_ARGISNULL(0);
2411  bool te1IsNull = PG_ARGISNULL(1);
2412  bool ts2IsNull = PG_ARGISNULL(2);
2413  bool te2IsNull = PG_ARGISNULL(3);
2414 
2415 #define TIMESTAMP_GT(t1,t2) \
2416  DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2417 #define TIMESTAMP_LT(t1,t2) \
2418  DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2419 
2420  /*
2421  * If both endpoints of interval 1 are null, the result is null (unknown).
2422  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2423  * take ts1 as the lesser endpoint.
2424  */
2425  if (ts1IsNull)
2426  {
2427  if (te1IsNull)
2428  PG_RETURN_NULL();
2429  /* swap null for non-null */
2430  ts1 = te1;
2431  te1IsNull = true;
2432  }
2433  else if (!te1IsNull)
2434  {
2435  if (TIMESTAMP_GT(ts1, te1))
2436  {
2437  Datum tt = ts1;
2438 
2439  ts1 = te1;
2440  te1 = tt;
2441  }
2442  }
2443 
2444  /* Likewise for interval 2. */
2445  if (ts2IsNull)
2446  {
2447  if (te2IsNull)
2448  PG_RETURN_NULL();
2449  /* swap null for non-null */
2450  ts2 = te2;
2451  te2IsNull = true;
2452  }
2453  else if (!te2IsNull)
2454  {
2455  if (TIMESTAMP_GT(ts2, te2))
2456  {
2457  Datum tt = ts2;
2458 
2459  ts2 = te2;
2460  te2 = tt;
2461  }
2462  }
2463 
2464  /*
2465  * At this point neither ts1 nor ts2 is null, so we can consider three
2466  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2467  */
2468  if (TIMESTAMP_GT(ts1, ts2))
2469  {
2470  /*
2471  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2472  * in the presence of nulls it's not quite completely so.
2473  */
2474  if (te2IsNull)
2475  PG_RETURN_NULL();
2476  if (TIMESTAMP_LT(ts1, te2))
2477  PG_RETURN_BOOL(true);
2478  if (te1IsNull)
2479  PG_RETURN_NULL();
2480 
2481  /*
2482  * If te1 is not null then we had ts1 <= te1 above, and we just found
2483  * ts1 >= te2, hence te1 >= te2.
2484  */
2485  PG_RETURN_BOOL(false);
2486  }
2487  else if (TIMESTAMP_LT(ts1, ts2))
2488  {
2489  /* This case is ts2 < te1 OR te2 < te1 */
2490  if (te1IsNull)
2491  PG_RETURN_NULL();
2492  if (TIMESTAMP_LT(ts2, te1))
2493  PG_RETURN_BOOL(true);
2494  if (te2IsNull)
2495  PG_RETURN_NULL();
2496 
2497  /*
2498  * If te2 is not null then we had ts2 <= te2 above, and we just found
2499  * ts2 >= te1, hence te2 >= te1.
2500  */
2501  PG_RETURN_BOOL(false);
2502  }
2503  else
2504  {
2505  /*
2506  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2507  * rather silly way of saying "true if both are non-null, else null".
2508  */
2509  if (te1IsNull || te2IsNull)
2510  PG_RETURN_NULL();
2511  PG_RETURN_BOOL(true);
2512  }
2513 
2514 #undef TIMESTAMP_GT
2515 #undef TIMESTAMP_LT
2516 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define TIMESTAMP_LT(t1, t2)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define TIMESTAMP_GT(t1, t2)
#define PG_RETURN_NULL()
Definition: fmgr.h:305
static int parse_sane_timezone ( struct pg_tm tm,
text zone 
)
static

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

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

Definition at line 1557 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

1558 {
1560 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgReloadTime
Definition: timestamp.c:51
Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1551 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

1552 {
1554 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgStartTime
Definition: timestamp.c:48
Timestamp SetEpochTimestamp ( void  )

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

2003 {
2004  Timestamp dt;
2005  struct pg_tm tt,
2006  *tm = &tt;
2007 
2008  GetEpochTime(tm);
2009  /* we don't bother to test for failure ... */
2010  tm2timestamp(tm, 0, NULL, &dt);
2011 
2012  return dt;
2013 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:103
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1853
#define NULL
Definition: c.h:229
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:1983
Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1539 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

1540 {
1542 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:717
static TimeOffset time2t ( const int  hour,
const int  min,
const int  sec,
const fsec_t  fsec 
)
static

Definition at line 1945 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

1946 {
1947  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
1948 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define SECS_PER_MINUTE
Definition: timestamp.h:88
TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1669 of file timestamp.c.

References POSTGRES_EPOCH_JDATE, result, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by pg_control_checkpoint(), pg_control_system(), pg_ls_dir_files(), and pg_stat_file().

1670 {
1672 
1673  result = (TimestampTz) tm -
1675  result *= USECS_PER_SEC;
1676 
1677  return result;
1678 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
return result
Definition: formatting.c:1618
static struct pg_tm tm
Definition: localtime.c:103
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
static TimestampTz timestamp2timestamptz ( Timestamp  timestamp)
static

Definition at line 4917 of file timestamp.c.

References DetermineTimeZoneOffset(), ereport, errcode(), errmsg(), ERROR, NULL, result, 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().

4918 {
4920  struct pg_tm tt,
4921  *tm = &tt;
4922  fsec_t fsec;
4923  int tz;
4924 
4926  result = timestamp;
4927  else
4928  {
4929  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4930  ereport(ERROR,
4931  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4932  errmsg("timestamp out of range")));
4933 
4935 
4936  if (tm2timestamp(tm, fsec, &tz, &result) != 0)
4937  ereport(ERROR,
4938  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4939  errmsg("timestamp out of range")));
4940  }
4941 
4942  return result;
4943 }
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1757
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:103
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1471
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1853
#define NULL
Definition: c.h:229
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 1757 of file timestamp.c.

References dt2time(), INT64CONST, j2date(), NULL, pg_localtime(), POSTGRES_EPOCH_JDATE, 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, 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().

1758 {
1759  Timestamp date;
1760  Timestamp time;
1761  pg_time_t utime;
1762 
1763  /* Use session timezone if caller asks for default */
1764  if (attimezone == NULL)
1765  attimezone = session_timezone;
1766 
1767  time = dt;
1768  TMODULO(time, date, USECS_PER_DAY);
1769 
1770  if (time < INT64CONST(0))
1771  {
1772  time += USECS_PER_DAY;
1773  date -= 1;
1774  }
1775 
1776  /* add offset to go from J2000 back to standard Julian date */
1777  date += POSTGRES_EPOCH_JDATE;
1778 
1779  /* Julian day routine does not work for negative Julian days */
1780  if (date < 0 || date > (Timestamp) INT_MAX)
1781  return -1;
1782 
1783  j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1784  dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1785 
1786  /* Done if no TZ conversion wanted */
1787  if (tzp == NULL)
1788  {
1789  tm->tm_isdst = -1;
1790  tm->tm_gmtoff = 0;
1791  tm->tm_zone = NULL;
1792  if (tzn != NULL)
1793  *tzn = NULL;
1794  return 0;
1795  }
1796 
1797  /*
1798  * If the time falls within the range of pg_time_t, use pg_localtime() to
1799  * rotate to the local time zone.
1800  *
1801  * First, convert to an integral timestamp, avoiding possibly
1802  * platform-specific roundoff-in-wrong-direction errors, and adjust to
1803  * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1804  * coding avoids hardwiring any assumptions about the width of pg_time_t,
1805  * so it should behave sanely on machines without int64.
1806  */
1807  dt = (dt - *fsec) / USECS_PER_SEC +
1809  utime = (pg_time_t) dt;
1810  if ((Timestamp) utime == dt)
1811  {
1812  struct pg_tm *tx = pg_localtime(&utime, attimezone);
1813 
1814  tm->tm_year = tx->tm_year + 1900;
1815  tm->tm_mon = tx->tm_mon + 1;
1816  tm->tm_mday = tx->tm_mday;
1817  tm->tm_hour = tx->tm_hour;
1818  tm->tm_min = tx->tm_min;
1819  tm->tm_sec = tx->tm_sec;
1820  tm->tm_isdst = tx->tm_isdst;
1821  tm->tm_gmtoff = tx->tm_gmtoff;
1822  tm->tm_zone = tx->tm_zone;
1823  *tzp = -tm->tm_gmtoff;
1824  if (tzn != NULL)
1825  *tzn = tm->tm_zone;
1826  }
1827  else
1828  {
1829  /*
1830  * When out of range of pg_time_t, treat as GMT
1831  */
1832  *tzp = 0;
1833  /* Mark this as *no* time zone available */
1834  tm->tm_isdst = -1;
1835  tm->tm_gmtoff = 0;
1836  tm->tm_zone = NULL;
1837  if (tzn != NULL)
1838  *tzn = NULL;
1839  }
1840 
1841  return 0;
1842 }
int64 pg_time_t
Definition: pgtime.h:23
int tm_isdst
Definition: pgtime.h:35
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
long date
Definition: pgtypes_date.h:8
long int tm_gmtoff
Definition: pgtime.h:36
Definition: pgtime.h:25
#define SECS_PER_DAY
Definition: timestamp.h:86
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USECS_PER_DAY
Definition: timestamp.h:91
const char * tm_zone
Definition: pgtime.h:37
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int64 Timestamp
Definition: timestamp.h:38
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1730
#define NULL
Definition: c.h:229
#define INT64CONST(x)
Definition: c.h:310
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1254
int tm_year
Definition: pgtime.h:32
pg_tz * session_timezone
Definition: pgtz.c:27
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
#define TMODULO(t, q, u)
Definition: datetime.h:251
Datum timestamp_age ( PG_FUNCTION_ARGS  )

Definition at line 3376 of file timestamp.c.

References day_tab, ereport, errcode(), errmsg(), ERROR, HOURS_PER_DAY, isleap, MINS_PER_HOUR, MONTHS_PER_YEAR, NULL, palloc(), PG_GETARG_TIMESTAMP, PG_RETURN_INTERVAL_P, result, SECS_PER_MINUTE, timestamp2tm(), 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, and USECS_PER_SEC.

3377 {
3378  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
3379  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
3380  Interval *result;
3381  fsec_t fsec,
3382  fsec1,
3383  fsec2;
3384  struct pg_tm tt,
3385  *tm = &tt;
3386  struct pg_tm tt1,
3387  *tm1 = &tt1;
3388  struct pg_tm tt2,
3389  *tm2 = &tt2;
3390 
3391  result = (Interval *) palloc(sizeof(Interval));
3392 
3393  if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
3394  timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
3395  {
3396  /* form the symbolic difference */
3397  fsec = fsec1 - fsec2;
3398  tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3399  tm->tm_min = tm1->tm_min - tm2->tm_min;
3400  tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3401  tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3402  tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3403  tm->tm_year = tm1->tm_year - tm2->tm_year;
3404 
3405  /* flip sign if necessary... */
3406  if (dt1 < dt2)
3407  {
3408  fsec = -fsec;
3409  tm->tm_sec = -tm->tm_sec;
3410  tm->tm_min = -tm->tm_min;
3411  tm->tm_hour = -tm->tm_hour;
3412  tm->tm_mday = -tm->tm_mday;
3413  tm->tm_mon = -tm->tm_mon;
3414  tm->tm_year = -tm->tm_year;
3415  }
3416 
3417  /* propagate any negative fields into the next higher field */
3418  while (fsec < 0)
3419  {
3420  fsec += USECS_PER_SEC;
3421  tm->tm_sec--;
3422  }
3423 
3424  while (tm->tm_sec < 0)
3425  {
3426  tm->tm_sec += SECS_PER_MINUTE;
3427  tm->tm_min--;
3428  }
3429 
3430  while (tm->tm_min < 0)
3431  {
3432  tm->tm_min += MINS_PER_HOUR;
3433  tm->tm_hour--;
3434  }
3435 
3436  while (tm->tm_hour < 0)
3437  {
3438  tm->tm_hour += HOURS_PER_DAY;
3439  tm->tm_mday--;
3440  }
3441 
3442