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 "common/int128.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_hash_extended (PG_FUNCTION_ARGS)
 
Datum timestamp_eq_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ne_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_lt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_gt_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_le_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_ge_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamp_cmp_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_eq_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ne_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_lt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_gt_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_le_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_ge_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamptz_cmp_timestamp (PG_FUNCTION_ARGS)
 
static INT128 interval_cmp_value (const Interval *interval)
 
static int interval_cmp_internal (Interval *interval1, Interval *interval2)
 
Datum interval_eq (PG_FUNCTION_ARGS)
 
Datum interval_ne (PG_FUNCTION_ARGS)
 
Datum interval_lt (PG_FUNCTION_ARGS)
 
Datum interval_gt (PG_FUNCTION_ARGS)
 
Datum interval_le (PG_FUNCTION_ARGS)
 
Datum interval_ge (PG_FUNCTION_ARGS)
 
Datum interval_cmp (PG_FUNCTION_ARGS)
 
Datum interval_hash (PG_FUNCTION_ARGS)
 
Datum interval_hash_extended (PG_FUNCTION_ARGS)
 
Datum overlaps_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_smaller (PG_FUNCTION_ARGS)
 
Datum timestamp_larger (PG_FUNCTION_ARGS)
 
Datum timestamp_mi (PG_FUNCTION_ARGS)
 
Datum interval_justify_interval (PG_FUNCTION_ARGS)
 
Datum interval_justify_hours (PG_FUNCTION_ARGS)
 
Datum interval_justify_days (PG_FUNCTION_ARGS)
 
Datum timestamp_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_mi_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (PG_FUNCTION_ARGS)
 
Datum interval_um (PG_FUNCTION_ARGS)
 
Datum interval_smaller (PG_FUNCTION_ARGS)
 
Datum interval_larger (PG_FUNCTION_ARGS)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
Datum interval_mi (PG_FUNCTION_ARGS)
 
Datum interval_mul (PG_FUNCTION_ARGS)
 
Datum mul_d_interval (PG_FUNCTION_ARGS)
 
Datum interval_div (PG_FUNCTION_ARGS)
 
Datum 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 46 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 1320 of file timestamp.c.

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

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

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

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

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

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

Definition at line 102 of file timestamp.c.

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

103 {
104  if (typmod < 0)
105  ereport(ERROR,
106  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
107  errmsg("TIMESTAMP(%d)%s precision must not be negative",
108  typmod, (istz ? " WITH TIME ZONE" : ""))));
109  if (typmod > MAX_TIMESTAMP_PRECISION)
110  {
112  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
113  errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
114  typmod, (istz ? " WITH TIME ZONE" : ""),
116  typmod = MAX_TIMESTAMP_PRECISION;
117  }
118 
119  return typmod;
120 }
int errcode(int sqlerrcode)
Definition: elog.c: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 81 of file timestamp.c.

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

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

Definition at line 124 of file timestamp.c.

References psprintf().

Referenced by timestamptypmodout(), and timestamptztypmodout().

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

Definition at line 1546 of file timestamp.c.

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

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

Definition at line 4092 of file timestamp.c.

References date2j(), and j2day().

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

4093 {
4094  float8 result;
4095  int day0,
4096  day4,
4097  dayn;
4098 
4099  /* current day */
4100  dayn = date2j(year, mon, mday);
4101 
4102  /* fourth day of current year */
4103  day4 = date2j(year, 1, 4);
4104 
4105  /* day0 == offset to first day of week (Monday) */
4106  day0 = j2day(day4 - 1);
4107 
4108  /*
4109  * We need the first week containing a Thursday, otherwise this day falls
4110  * into the previous year for purposes of counting weeks
4111  */
4112  if (dayn < day4 - day0)
4113  {
4114  day4 = date2j(year - 1, 1, 4);
4115 
4116  /* day0 == offset to first day of week (Monday) */
4117  day0 = j2day(day4 - 1);
4118  }
4119 
4120  result = (dayn - (day4 - day0)) / 7 + 1;
4121 
4122  /*
4123  * Sometimes the last few days in a year will fall into the first week of
4124  * the next year, so check for this.
4125  */
4126  if (result >= 52)
4127  {
4128  day4 = date2j(year + 1, 1, 4);
4129 
4130  /* day0 == offset to first day of week (Monday) */
4131  day0 = j2day(day4 - 1);
4132 
4133  if (dayn >= day4 - day0)
4134  result = (dayn - (day4 - day0)) / 7 + 1;
4135  }
4136 
4137  return (int) result;
4138 }
double float8
Definition: c.h:375
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 4146 of file timestamp.c.

References date2j(), and j2day().

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

4147 {
4148  float8 result;
4149  int day0,
4150  day4,
4151  dayn;
4152 
4153  /* current day */
4154  dayn = date2j(year, mon, mday);
4155 
4156  /* fourth day of current year */
4157  day4 = date2j(year, 1, 4);
4158 
4159  /* day0 == offset to first day of week (Monday) */
4160  day0 = j2day(day4 - 1);
4161 
4162  /*
4163  * We need the first week containing a Thursday, otherwise this day falls
4164  * into the previous year for purposes of counting weeks
4165  */
4166  if (dayn < day4 - day0)
4167  {
4168  day4 = date2j(year - 1, 1, 4);
4169 
4170  /* day0 == offset to first day of week (Monday) */
4171  day0 = j2day(day4 - 1);
4172 
4173  year--;
4174  }
4175 
4176  result = (dayn - (day4 - day0)) / 7 + 1;
4177 
4178  /*
4179  * Sometimes the last few days in a year will fall into the first week of
4180  * the next year, so check for this.
4181  */
4182  if (result >= 52)
4183  {
4184  day4 = date2j(year + 1, 1, 4);
4185 
4186  /* day0 == offset to first day of week (Monday) */
4187  day0 = j2day(day4 - 1);
4188 
4189  if (dayn >= day4 - day0)
4190  year++;
4191  }
4192 
4193  return year;
4194 }
double float8
Definition: c.h:375
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 4203 of file timestamp.c.

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

Referenced by DCH_to_char().

4204 {
4205  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4206 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4146
int isoweek2j(int year, int week)
Definition: timestamp.c:4041
int date2j(int y, int m, int d)
Definition: datetime.c:292
static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

Definition at line 1952 of file timestamp.c.

References USECS_PER_SEC.

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

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

Definition at line 1731 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1732 {
1733  TimeOffset time;
1734 
1735  time = jd;
1736 
1737  *hour = time / USECS_PER_HOUR;
1738  time -= (*hour) * USECS_PER_HOUR;
1739  *min = time / USECS_PER_MINUTE;
1740  time -= (*min) * USECS_PER_MINUTE;
1741  *sec = time / USECS_PER_SEC;
1742  *fsec = time - (*sec * USECS_PER_SEC);
1743 } /* 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 1523 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().

1524 {
1525  if (TIMESTAMP_IS_NOBEGIN(dt))
1526  strcpy(str, EARLY);
1527  else if (TIMESTAMP_IS_NOEND(dt))
1528  strcpy(str, LATE);
1529  else /* shouldn't happen */
1530  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1531 }
#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 711 of file timestamp.c.

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

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

References generate_series_timestamp_fctx::current, DatumGetTimestamp, DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, generate_series_timestamp_fctx::finish, interval_cmp_internal(), MemoryContextSwitchTo(), MemSet, FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PointerGetDatum, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_timestamp_fctx::step, generate_series_timestamp_fctx::step_sign, timestamp_cmp_internal(), timestamp_pl_interval(), TimestampGetDatum, and FuncCallContext::user_fctx.

5156 {
5157  FuncCallContext *funcctx;
5159  Timestamp result;
5160 
5161  /* stuff done only on the first call of the function */
5162  if (SRF_IS_FIRSTCALL())
5163  {
5164  Timestamp start = PG_GETARG_TIMESTAMP(0);
5165  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5166  Interval *step = PG_GETARG_INTERVAL_P(2);
5167  MemoryContext oldcontext;
5168  Interval interval_zero;
5169 
5170  /* create a function context for cross-call persistence */
5171  funcctx = SRF_FIRSTCALL_INIT();
5172 
5173  /*
5174  * switch to memory context appropriate for multiple function calls
5175  */
5176  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5177 
5178  /* allocate memory for user context */
5181 
5182  /*
5183  * Use fctx to keep state from call to call. Seed current with the
5184  * original start value
5185  */
5186  fctx->current = start;
5187  fctx->finish = finish;
5188  fctx->step = *step;
5189 
5190  /* Determine sign of the interval */
5191  MemSet(&interval_zero, 0, sizeof(Interval));
5192  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5193 
5194  if (fctx->step_sign == 0)
5195  ereport(ERROR,
5196  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5197  errmsg("step size cannot equal zero")));
5198 
5199  funcctx->user_fctx = fctx;
5200  MemoryContextSwitchTo(oldcontext);
5201  }
5202 
5203  /* stuff done on every call of the function */
5204  funcctx = SRF_PERCALL_SETUP();
5205 
5206  /*
5207  * get the saved state and use current as the result for this iteration
5208  */
5209  fctx = funcctx->user_fctx;
5210  result = fctx->current;
5211 
5212  if (fctx->step_sign > 0 ?
5213  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5214  timestamp_cmp_internal(result, fctx->finish) >= 0)
5215  {
5216  /* increment current in preparation for next iteration */
5217  fctx->current = DatumGetTimestamp(
5219  TimestampGetDatum(fctx->current),
5220  PointerGetDatum(&fctx->step)));
5221 
5222  /* do when there is more left to send */
5223  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5224  }
5225  else
5226  {
5227  /* do when there is no more left */
5228  SRF_RETURN_DONE(funcctx);
5229  }
5230 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2023
#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:863
#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:2795
#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:2331
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
#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 5236 of file timestamp.c.

References generate_series_timestamptz_fctx::current, DatumGetTimestampTz, DirectFunctionCall2, ereport, errcode(), errmsg(), ERROR, generate_series_timestamptz_fctx::finish, interval_cmp_internal(), MemoryContextSwitchTo(), MemSet, FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PointerGetDatum, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, generate_series_timestamptz_fctx::step, generate_series_timestamptz_fctx::step_sign, timestamp_cmp_internal(), timestamptz_pl_interval(), TimestampTzGetDatum, and FuncCallContext::user_fctx.

5237 {
5238  FuncCallContext *funcctx;
5240  TimestampTz result;
5241 
5242  /* stuff done only on the first call of the function */
5243  if (SRF_IS_FIRSTCALL())
5244  {
5246  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5247  Interval *step = PG_GETARG_INTERVAL_P(2);
5248  MemoryContext oldcontext;
5249  Interval interval_zero;
5250 
5251  /* create a function context for cross-call persistence */
5252  funcctx = SRF_FIRSTCALL_INIT();
5253 
5254  /*
5255  * switch to memory context appropriate for multiple function calls
5256  */
5257  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5258 
5259  /* allocate memory for user context */
5262 
5263  /*
5264  * Use fctx to keep state from call to call. Seed current with the
5265  * original start value
5266  */
5267  fctx->current = start;
5268  fctx->finish = finish;
5269  fctx->step = *step;
5270 
5271  /* Determine sign of the interval */
5272  MemSet(&interval_zero, 0, sizeof(Interval));
5273  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5274 
5275  if (fctx->step_sign == 0)
5276  ereport(ERROR,
5277  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5278  errmsg("step size cannot equal zero")));
5279 
5280  funcctx->user_fctx = fctx;
5281  MemoryContextSwitchTo(oldcontext);
5282  }
5283 
5284  /* stuff done on every call of the function */
5285  funcctx = SRF_PERCALL_SETUP();
5286 
5287  /*
5288  * get the saved state and use current as the result for this iteration
5289  */
5290  fctx = funcctx->user_fctx;
5291  result = fctx->current;
5292 
5293  if (fctx->step_sign > 0 ?
5294  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5295  timestamp_cmp_internal(result, fctx->finish) >= 0)
5296  {
5297  /* increment current in preparation for next iteration */
5298  fctx->current = DatumGetTimestampTz(
5301  PointerGetDatum(&fctx->step)));
5302 
5303  /* do when there is more left to send */
5304  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5305  }
5306  else
5307  {
5308  /* do when there is no more left */
5309  SRF_RETURN_DONE(funcctx);
5310  }
5311 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2023
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:863
#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:2900
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:848
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:587
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1570 of file timestamp.c.

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

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), asyncQueueFillWarning(), autoprewarm_main(), autovac_refresh_stats(), AutoVacLauncherMain(), backend_read_statsfile(), BackendInitialize(), BackgroundWriterMain(), check_log_duration(), CheckPointBuffers(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), 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(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), perform_base_backup(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), 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(), process_syncing_tables_for_apply(), ProcessRepliesIfAny(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), reset_dbentry_counters(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), SetCurrentTransactionStopTimestamp(), StartupXLOG(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

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

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

1985 {
1986  struct pg_tm *t0;
1987  pg_time_t epoch = 0;
1988 
1989  t0 = pg_gmtime(&epoch);
1990 
1991  tm->tm_year = t0->tm_year;
1992  tm->tm_mon = t0->tm_mon;
1993  tm->tm_mday = t0->tm_mday;
1994  tm->tm_hour = t0->tm_hour;
1995  tm->tm_min = t0->tm_min;
1996  tm->tm_sec = t0->tm_sec;
1997 
1998  tm->tm_year += 1900;
1999  tm->tm_mon++;
2000 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1355
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 1588 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

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

Definition at line 1602 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

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

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

1903 {
1904  TimeOffset time;
1905  TimeOffset tfrac;
1906 
1907  tm->tm_year = span.month / MONTHS_PER_YEAR;
1908  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1909  tm->tm_mday = span.day;
1910  time = span.time;
1911 
1912  tfrac = time / USECS_PER_HOUR;
1913  time -= tfrac * USECS_PER_HOUR;
1914  tm->tm_hour = tfrac;
1915  if (!SAMESIGN(tm->tm_hour, tfrac))
1916  ereport(ERROR,
1917  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1918  errmsg("interval out of range")));
1919  tfrac = time / USECS_PER_MINUTE;
1920  time -= tfrac * USECS_PER_MINUTE;
1921  tm->tm_min = tfrac;
1922  tfrac = time / USECS_PER_SEC;
1923  *fsec = time - (tfrac * USECS_PER_SEC);
1924  tm->tm_sec = tfrac;
1925 
1926  return 0;
1927 }
#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:46
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:797
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
Datum interval_accum ( PG_FUNCTION_ARGS  )

Definition at line 3272 of file timestamp.c.

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

3273 {
3274  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3276  Datum *transdatums;
3277  int ndatums;
3278  Interval sumX,
3279  N;
3280  Interval *newsum;
3281  ArrayType *result;
3282 
3283  deconstruct_array(transarray,
3284  INTERVALOID, sizeof(Interval), false, 'd',
3285  &transdatums, NULL, &ndatums);
3286  if (ndatums != 2)
3287  elog(ERROR, "expected 2-element interval array");
3288 
3289  sumX = *(DatumGetIntervalP(transdatums[0]));
3290  N = *(DatumGetIntervalP(transdatums[1]));
3291 
3293  IntervalPGetDatum(&sumX),
3294  IntervalPGetDatum(newval)));
3295  N.time += 1;
3296 
3297  transdatums[0] = IntervalPGetDatum(newsum);
3298  transdatums[1] = IntervalPGetDatum(&N);
3299 
3300  result = construct_array(transdatums, 2,
3301  INTERVALOID, sizeof(Interval), false, 'd');
3302 
3303  PG_RETURN_ARRAYTYPE_P(result);
3304 }
#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:3279
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3059
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:529
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:372
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3356 of file timestamp.c.

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

3357 {
3358  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3360  Datum *transdatums;
3361  int ndatums;
3362  Interval sumX,
3363  N;
3364  Interval *newsum;
3365  ArrayType *result;
3366 
3367  deconstruct_array(transarray,
3368  INTERVALOID, sizeof(Interval), false, 'd',
3369  &transdatums, NULL, &ndatums);
3370  if (ndatums != 2)
3371  elog(ERROR, "expected 2-element interval array");
3372 
3373  sumX = *(DatumGetIntervalP(transdatums[0]));
3374  N = *(DatumGetIntervalP(transdatums[1]));
3375 
3377  IntervalPGetDatum(&sumX),
3378  IntervalPGetDatum(newval)));
3379  N.time -= 1;
3380 
3381  transdatums[0] = IntervalPGetDatum(newsum);
3382  transdatums[1] = IntervalPGetDatum(&N);
3383 
3384  result = construct_array(transdatums, 2,
3385  INTERVALOID, sizeof(Interval), false, 'd');
3386 
3387  PG_RETURN_ARRAYTYPE_P(result);
3388 }
#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:3279
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3093
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:529
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:372
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 3391 of file timestamp.c.

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

3392 {
3393  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3394  Datum *transdatums;
3395  int ndatums;
3396  Interval sumX,
3397  N;
3398 
3399  deconstruct_array(transarray,
3400  INTERVALOID, sizeof(Interval), false, 'd',
3401  &transdatums, NULL, &ndatums);
3402  if (ndatums != 2)
3403  elog(ERROR, "expected 2-element interval array");
3404 
3405  sumX = *(DatumGetIntervalP(transdatums[0]));
3406  N = *(DatumGetIntervalP(transdatums[1]));
3407 
3408  /* SQL defines AVG of no values to be NULL */
3409  if (N.time == 0)
3410  PG_RETURN_NULL();
3411 
3413  IntervalPGetDatum(&sumX),
3414  Float8GetDatum((double) N.time));
3415 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1810
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:529
TimeOffset time
Definition: timestamp.h:45
uintptr_t Datum
Definition: postgres.h:372
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3220
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2394 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp().

2395 {
2396  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2397  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2398 
2399  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2400 }
#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:2331
static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

Definition at line 2331 of file timestamp.c.

References int128_compare(), and 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().

2332 {
2333  INT128 span1 = interval_cmp_value(interval1);
2334  INT128 span2 = interval_cmp_value(interval2);
2335 
2336  return int128_compare(span1, span2);
2337 }
static int int128_compare(INT128 x, INT128 y)
Definition: int128.h:238
Definition: int128.h:107
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306
static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2306 of file timestamp.c.

References Interval::day, days, int128_add_int64_mul_int64(), int64_to_int128(), Interval::month, Interval::time, and USECS_PER_DAY.

Referenced by interval_cmp_internal(), interval_hash(), and interval_hash_extended().

2307 {
2308  INT128 span;
2309  int64 dayfraction;
2310  int64 days;
2311 
2312  /*
2313  * Separate time field into days and dayfraction, then add the month and
2314  * day fields to the days part. We cannot overflow int64 days here.
2315  */
2316  dayfraction = interval->time % USECS_PER_DAY;
2317  days = interval->time / USECS_PER_DAY;
2318  days += interval->month * INT64CONST(30);
2319  days += interval->day;
2320 
2321  /* Widen dayfraction to 128 bits */
2322  span = int64_to_int128(dayfraction);
2323 
2324  /* Scale up days to microseconds, forming a 128-bit product */
2326 
2327  return span;
2328 }
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:69
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
static INT128 int64_to_int128(int64 v)
Definition: int128.h:255
Definition: int128.h:107
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
Definition: int128.h:177
Datum interval_combine ( PG_FUNCTION_ARGS  )

Definition at line 3307 of file timestamp.c.

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

3308 {
3309  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3310  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3311  Datum *transdatums1;
3312  Datum *transdatums2;
3313  int ndatums1;
3314  int ndatums2;
3315  Interval sum1,
3316  N1;
3317  Interval sum2,
3318  N2;
3319 
3320  Interval *newsum;
3321  ArrayType *result;
3322 
3323  deconstruct_array(transarray1,
3324  INTERVALOID, sizeof(Interval), false, 'd',
3325  &transdatums1, NULL, &ndatums1);
3326  if (ndatums1 != 2)
3327  elog(ERROR, "expected 2-element interval array");
3328 
3329  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3330  N1 = *(DatumGetIntervalP(transdatums1[1]));
3331 
3332  deconstruct_array(transarray2,
3333  INTERVALOID, sizeof(Interval), false, 'd',
3334  &transdatums2, NULL, &ndatums2);
3335  if (ndatums2 != 2)
3336  elog(ERROR, "expected 2-element interval array");
3337 
3338  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3339  N2 = *(DatumGetIntervalP(transdatums2[1]));
3340 
3342  IntervalPGetDatum(&sum1),
3343  IntervalPGetDatum(&sum2)));
3344  N1.time += N2.time;
3345 
3346  transdatums1[0] = IntervalPGetDatum(newsum);
3347  transdatums1[1] = IntervalPGetDatum(&N1);
3348 
3349  result = construct_array(transdatums1, 2,
3350  INTERVALOID, sizeof(Interval), false, 'd');
3351 
3352  PG_RETURN_ARRAYTYPE_P(result);
3353 }
#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:3279
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3059
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:529
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:372
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3220 of file timestamp.c.

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

Referenced by interval_avg().

3221 {
3222  Interval *span = PG_GETARG_INTERVAL_P(0);
3223  float8 factor = PG_GETARG_FLOAT8(1);
3224  double month_remainder_days,
3225  sec_remainder;
3226  int32 orig_month = span->month,
3227  orig_day = span->day;
3228  Interval *result;
3229 
3230  result = (Interval *) palloc(sizeof(Interval));
3231 
3232  if (factor == 0.0)
3233  ereport(ERROR,
3234  (errcode(ERRCODE_DIVISION_BY_ZERO),
3235  errmsg("division by zero")));
3236 
3237  result->month = (int32) (span->month / factor);
3238  result->day = (int32) (span->day / factor);
3239 
3240  /*
3241  * Fractional months full days into days. See comment in interval_mul().
3242  */
3243  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3244  month_remainder_days = TSROUND(month_remainder_days);
3245  sec_remainder = (orig_day / factor - result->day +
3246  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3247  sec_remainder = TSROUND(sec_remainder);
3248  if (Abs(sec_remainder) >= SECS_PER_DAY)
3249  {
3250  result->day += (int) (sec_remainder / SECS_PER_DAY);
3251  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3252  }
3253 
3254  /* cascade units down */
3255  result->day += (int32) month_remainder_days;
3256  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3257 
3258  PG_RETURN_INTERVAL_P(result);
3259 }
#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
signed int int32
Definition: c.h:246
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:818
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:375
#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:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2340 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

2341 {
2342  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2343  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2344 
2345  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2346 }
#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:2331
Datum interval_finite ( PG_FUNCTION_ARGS  )

Definition at line 1973 of file timestamp.c.

References PG_RETURN_BOOL.

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

Definition at line 2385 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

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

Definition at line 2367 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2368 {
2369  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2370  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2371 
2372  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2373 }
#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:2331
Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2410 of file timestamp.c.

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

2411 {
2413  INT128 span = interval_cmp_value(interval);
2414  int64 span64;
2415 
2416  /*
2417  * Use only the least significant 64 bits for hashing. The upper 64 bits
2418  * seldom add any useful information, and besides we must do it like this
2419  * for compatibility with hashes calculated before use of INT128 was
2420  * introduced.
2421  */
2422  span64 = int128_to_int64(span);
2423 
2425 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:80
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
#define Int64GetDatumFast(X)
Definition: postgres.h:781
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306
Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2428 of file timestamp.c.

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

2429 {
2431  INT128 span = interval_cmp_value(interval);
2432  int64 span64;
2433 
2434  /* Same approach as interval_hash */
2435  span64 = int128_to_int64(span);
2436 
2438  PG_GETARG_DATUM(1));
2439 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:100
#define Int64GetDatumFast(X)
Definition: postgres.h:781
Definition: int128.h:107
static int64 int128_to_int64(INT128 val)
Definition: int128.h:269
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
static INT128 interval_cmp_value(const Interval *interval)
Definition: timestamp.c:2306
Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 876 of file timestamp.c.

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

Referenced by check_timezone(), and flatten_set_variable_args().

877 {
878  char *str = PG_GETARG_CSTRING(0);
879 
880 #ifdef NOT_USED
881  Oid typelem = PG_GETARG_OID(1);
882 #endif
883  int32 typmod = PG_GETARG_INT32(2);
884  Interval *result;
885  fsec_t fsec;
886  struct pg_tm tt,
887  *tm = &tt;
888  int dtype;
889  int nf;
890  int range;
891  int dterr;
892  char *field[MAXDATEFIELDS];
893  int ftype[MAXDATEFIELDS];
894  char workbuf[256];
895 
896  tm->tm_year = 0;
897  tm->tm_mon = 0;
898  tm->tm_mday = 0;
899  tm->tm_hour = 0;
900  tm->tm_min = 0;
901  tm->tm_sec = 0;
902  fsec = 0;
903 
904  if (typmod >= 0)
905  range = INTERVAL_RANGE(typmod);
906  else
907  range = INTERVAL_FULL_RANGE;
908 
909  dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
910  ftype, MAXDATEFIELDS, &nf);
911  if (dterr == 0)
912  dterr = DecodeInterval(field, ftype, nf, range,
913  &dtype, tm, &fsec);
914 
915  /* if those functions think it's a bad format, try ISO8601 style */
916  if (dterr == DTERR_BAD_FORMAT)
917  dterr = DecodeISO8601Interval(str,
918  &dtype, tm, &fsec);
919 
920  if (dterr != 0)
921  {
922  if (dterr == DTERR_FIELD_OVERFLOW)
923  dterr = DTERR_INTERVAL_OVERFLOW;
924  DateTimeParseError(dterr, str, "interval");
925  }
926 
927  result = (Interval *) palloc(sizeof(Interval));
928 
929  switch (dtype)
930  {
931  case DTK_DELTA:
932  if (tm2interval(tm, fsec, result) != 0)
933  ereport(ERROR,
934  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
935  errmsg("interval out of range")));
936  break;
937 
938  case DTK_INVALID:
939  ereport(ERROR,
940  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
941  errmsg("date/time value \"%s\" is no longer supported", str)));
942  break;
943 
944  default:
945  elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
946  dtype, str);
947  }
948 
949  AdjustIntervalForTypmod(result, typmod);
950 
951  PG_RETURN_INTERVAL_P(result);
952 }
#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
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:246
static struct pg_tm tm
Definition: localtime.c:107
#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:1320
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:848
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:1930
Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2755 of file timestamp.c.

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

2756 {
2757  Interval *span = PG_GETARG_INTERVAL_P(0);
2758  Interval *result;
2759  int32 wholemonth;
2760 
2761  result = (Interval *) palloc(sizeof(Interval));
2762  result->month = span->month;
2763  result->day = span->day;
2764  result->time = span->time;
2765 
2766  wholemonth = result->day / DAYS_PER_MONTH;
2767  result->day -= wholemonth * DAYS_PER_MONTH;
2768  result->month += wholemonth;
2769 
2770  if (result->month > 0 && result->day < 0)
2771  {
2772  result->day += DAYS_PER_MONTH;
2773  result->month--;
2774  }
2775  else if (result->month < 0 && result->day > 0)
2776  {
2777  result->day -= DAYS_PER_MONTH;
2778  result->month++;
2779  }
2780 
2781  PG_RETURN_INTERVAL_P(result);
2782 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:246
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:848
Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

Definition at line 2720 of file timestamp.c.

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

Referenced by timestamp_mi().

2721 {
2722  Interval *span = PG_GETARG_INTERVAL_P(0);
2723  Interval *result;
2724  TimeOffset wholeday;
2725 
2726  result = (Interval *) palloc(sizeof(Interval));
2727  result->month = span->month;
2728  result->day = span->day;
2729  result->time = span->time;
2730 
2731  TMODULO(result->time, wholeday, USECS_PER_DAY);
2732  result->day += wholeday; /* could overflow... */
2733 
2734  if (result->day > 0 && result->time < 0)
2735  {
2736  result->time += USECS_PER_DAY;
2737  result->day--;
2738  }
2739  else if (result->day < 0 && result->time > 0)
2740  {
2741  result->time -= USECS_PER_DAY;
2742  result->day++;
2743  }
2744 
2745  PG_RETURN_INTERVAL_P(result);
2746 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
void * palloc(Size size)
Definition: mcxt.c:848
#define TMODULO(t, q, u)
Definition: datetime.h:251
Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2665 of file timestamp.c.

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

2666 {
2667  Interval *span = PG_GETARG_INTERVAL_P(0);
2668  Interval *result;
2669  TimeOffset wholeday;
2670  int32 wholemonth;
2671 
2672  result = (Interval *) palloc(sizeof(Interval));
2673  result->month = span->month;
2674  result->day = span->day;
2675  result->time = span->time;
2676 
2677  TMODULO(result->time, wholeday, USECS_PER_DAY);
2678  result->day += wholeday; /* could overflow... */
2679 
2680  wholemonth = result->day / DAYS_PER_MONTH;
2681  result->day -= wholemonth * DAYS_PER_MONTH;
2682  result->month += wholemonth;
2683 
2684  if (result->month > 0 &&
2685  (result->day < 0 || (result->day == 0 && result->time < 0)))
2686  {
2687  result->day += DAYS_PER_MONTH;
2688  result->month--;
2689  }
2690  else if (result->month < 0 &&
2691  (result->day > 0 || (result->day == 0 && result->time > 0)))
2692  {
2693  result->day -= DAYS_PER_MONTH;
2694  result->month++;
2695  }
2696 
2697  if (result->day > 0 && result->time < 0)
2698  {
2699  result->time += USECS_PER_DAY;
2700  result->day--;
2701  }
2702  else if (result->day < 0 && result->time > 0)
2703  {
2704  result->time -= USECS_PER_DAY;
2705  result->day++;
2706  }
2707 
2708  PG_RETURN_INTERVAL_P(result);
2709 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
signed int int32
Definition: c.h:246
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:848
#define TMODULO(t, q, u)
Definition: datetime.h:251
Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3045 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3046 {
3047  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3048  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3049  Interval *result;
3050 
3051  if (interval_cmp_internal(interval1, interval2) > 0)
3052  result = interval1;
3053  else
3054  result = interval2;
3055  PG_RETURN_INTERVAL_P(result);
3056 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331
Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2376 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

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

Definition at line 2358 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2359 {
2360  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2361  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2362 
2363  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2364 }
#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:2331
Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3093 of file timestamp.c.

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

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

3094 {
3095  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3096  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3097  Interval *result;
3098 
3099  result = (Interval *) palloc(sizeof(Interval));
3100 
3101  result->month = span1->month - span2->month;
3102  /* overflow check copied from int4mi */
3103  if (!SAMESIGN(span1->month, span2->month) &&
3104  !SAMESIGN(result->month, span1->month))
3105  ereport(ERROR,
3106  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3107  errmsg("interval out of range")));
3108 
3109  result->day = span1->day - span2->day;
3110  if (!SAMESIGN(span1->day, span2->day) &&
3111  !SAMESIGN(result->day, span1->day))
3112  ereport(ERROR,
3113  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3114  errmsg("interval out of range")));
3115 
3116  result->time = span1->time - span2->time;
3117  if (!SAMESIGN(span1->time, span2->time) &&
3118  !SAMESIGN(result->time, span1->time))
3119  ereport(ERROR,
3120  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3121  errmsg("interval out of range")));
3122 
3123  PG_RETURN_INTERVAL_P(result);
3124 }
#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
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:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3133 of file timestamp.c.

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

Referenced by interval_lerp(), and mul_d_interval().

3134 {
3135  Interval *span = PG_GETARG_INTERVAL_P(0);
3136  float8 factor = PG_GETARG_FLOAT8(1);
3137  double month_remainder_days,
3138  sec_remainder,
3139  result_double;
3140  int32 orig_month = span->month,
3141  orig_day = span->day;
3142  Interval *result;
3143 
3144  result = (Interval *) palloc(sizeof(Interval));
3145 
3146  result_double = span->month * factor;
3147  if (isnan(result_double) ||
3148  result_double > INT_MAX || result_double < INT_MIN)
3149  ereport(ERROR,
3150  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3151  errmsg("interval out of range")));
3152  result->month = (int32) result_double;
3153 
3154  result_double = span->day * factor;
3155  if (isnan(result_double) ||
3156  result_double > INT_MAX || result_double < INT_MIN)
3157  ereport(ERROR,
3158  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3159  errmsg("interval out of range")));
3160  result->day = (int32) result_double;
3161 
3162  /*
3163  * The above correctly handles the whole-number part of the month and day
3164  * products, but we have to do something with any fractional part
3165  * resulting when the factor is non-integral. We cascade the fractions
3166  * down to lower units using the conversion factors DAYS_PER_MONTH and
3167  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3168  * so by the representation. The user can choose to cascade up later,
3169  * using justify_hours and/or justify_days.
3170  */
3171 
3172  /*
3173  * Fractional months full days into days.
3174  *
3175  * Floating point calculation are inherently imprecise, so these
3176  * calculations are crafted to produce the most reliable result possible.
3177  * TSROUND() is needed to more accurately produce whole numbers where
3178  * appropriate.
3179  */
3180  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3181  month_remainder_days = TSROUND(month_remainder_days);
3182  sec_remainder = (orig_day * factor - result->day +
3183  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3184  sec_remainder = TSROUND(sec_remainder);
3185 
3186  /*
3187  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3188  * cascade from months and days. It might still be >24 if the combination
3189  * of cascade and the seconds factor operation itself.
3190  */
3191  if (Abs(sec_remainder) >= SECS_PER_DAY)
3192  {
3193  result->day += (int) (sec_remainder / SECS_PER_DAY);
3194  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3195  }
3196 
3197  /* cascade units down */
3198  result->day += (int32) month_remainder_days;
3199  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3200  if (result_double > PG_INT64_MAX || result_double < PG_INT64_MIN)
3201  ereport(ERROR,
3202  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3203  errmsg("interval out of range")));
3204  result->time = (int64) result_double;
3205 
3206  PG_RETURN_INTERVAL_P(result);
3207 }
#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:328
#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
signed int int32
Definition: c.h:246
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:818
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:375
#define SECS_PER_DAY
Definition: timestamp.h:86
#define PG_INT64_MIN
Definition: c.h:327
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:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2349 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2350 {
2351  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2352  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2353 
2354  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2355 }
#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:2331
Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 958 of file timestamp.c.

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

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

959 {
960  Interval *span = PG_GETARG_INTERVAL_P(0);
961  char *result;
962  struct pg_tm tt,
963  *tm = &tt;
964  fsec_t fsec;
965  char buf[MAXDATELEN + 1];
966 
967  if (interval2tm(*span, tm, &fsec) != 0)
968  elog(ERROR, "could not convert interval to tm");
969 
970  EncodeInterval(tm, fsec, IntervalStyle, buf);
971 
972  result = pstrdup(buf);
973  PG_RETURN_CSTRING(result);
974 }
#define MAXDATELEN
Definition: datetime.h:203
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int IntervalStyle
Definition: globals.c:109
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
#define ERROR
Definition: elog.h:43
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1902
static char * buf
Definition: pg_test_fsync.c:67
int32 fsec_t
Definition: timestamp.h:41
void EncodeInterval(struct pg_tm *tm, fsec_t fsec, int style, char *str)
Definition: datetime.c: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 4692 of file timestamp.c.

References Interval::day, DAYS_PER_MONTH, DAYS_PER_YEAR, DecodeSpecial(), DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_EPOCH, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_YEAR, elog, ereport, errcode(), errmsg(), ERROR, interval2tm(), Interval::month, MONTHS_PER_YEAR, PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT8, RESERV, SECS_PER_DAY, Interval::time, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

4693 {
4694  text *units = PG_GETARG_TEXT_PP(0);
4696  float8 result;
4697  int type,
4698  val;
4699  char *lowunits;
4700  fsec_t fsec;
4701  struct pg_tm tt,
4702  *tm = &tt;
4703 
4704  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4705  VARSIZE_ANY_EXHDR(units),
4706  false);
4707 
4708  type = DecodeUnits(0, lowunits, &val);
4709  if (type == UNKNOWN_FIELD)
4710  type = DecodeSpecial(0, lowunits, &val);
4711 
4712  if (type == UNITS)
4713  {
4714  if (interval2tm(*interval, tm, &fsec) == 0)
4715  {
4716  switch (val)
4717  {
4718  case DTK_MICROSEC:
4719  result = tm->tm_sec * 1000000.0 + fsec;
4720  break;
4721 
4722  case DTK_MILLISEC:
4723  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4724  break;
4725 
4726  case DTK_SECOND:
4727  result = tm->tm_sec + fsec / 1000000.0;
4728  break;
4729 
4730  case DTK_MINUTE:
4731  result = tm->tm_min;
4732  break;
4733 
4734  case DTK_HOUR:
4735  result = tm->tm_hour;
4736  break;
4737 
4738  case DTK_DAY:
4739  result = tm->tm_mday;
4740  break;
4741 
4742  case DTK_MONTH:
4743  result = tm->tm_mon;
4744  break;
4745 
4746  case DTK_QUARTER:
4747  result = (tm->tm_mon / 3) + 1;
4748  break;
4749 
4750  case DTK_YEAR:
4751  result = tm->tm_year;
4752  break;
4753 
4754  case DTK_DECADE:
4755  /* caution: C division may have negative remainder */
4756  result = tm->tm_year / 10;
4757  break;
4758 
4759  case DTK_CENTURY:
4760  /* caution: C division may have negative remainder */
4761  result = tm->tm_year / 100;
4762  break;
4763 
4764  case DTK_MILLENNIUM:
4765  /* caution: C division may have negative remainder */
4766  result = tm->tm_year / 1000;
4767  break;
4768 
4769  default:
4770  ereport(ERROR,
4771  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4772  errmsg("interval units \"%s\" not supported",
4773  lowunits)));
4774  result = 0;
4775  }
4776 
4777  }
4778  else
4779  {
4780  elog(ERROR, "could not convert interval to tm");
4781  result = 0;
4782  }
4783  }
4784  else if (type == RESERV && val == DTK_EPOCH)
4785  {
4786  result = interval->time / 1000000.0;
4787  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
4788  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
4789  result += ((double) SECS_PER_DAY) * interval->day;
4790  }
4791  else
4792  {
4793  ereport(ERROR,
4794  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4795  errmsg("interval units \"%s\" not recognized",
4796  lowunits)));
4797  result = 0;
4798  }
4799 
4800  PG_RETURN_FLOAT8(result);
4801 }
#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
#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:107
#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:375
#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:1902
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:433
#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 3059 of file timestamp.c.

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

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

3060 {
3061  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3062  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3063  Interval *result;
3064 
3065  result = (Interval *) palloc(sizeof(Interval));
3066 
3067  result->month = span1->month + span2->month;
3068  /* overflow check copied from int4pl */
3069  if (SAMESIGN(span1->month, span2->month) &&
3070  !SAMESIGN(result->month, span1->month))
3071  ereport(ERROR,
3072  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3073  errmsg("interval out of range")));
3074 
3075  result->day = span1->day + span2->day;
3076  if (SAMESIGN(span1->day, span2->day) &&
3077  !SAMESIGN(result->day, span1->day))
3078  ereport(ERROR,
3079  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3080  errmsg("interval out of range")));
3081 
3082  result->time = span1->time + span2->time;
3083  if (SAMESIGN(span1->time, span2->time) &&
3084  !SAMESIGN(result->time, span1->time))
3085  ereport(ERROR,
3086  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3087  errmsg("interval out of range")));
3088 
3089  PG_RETURN_INTERVAL_P(result);
3090 }
#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
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:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_recv ( PG_FUNCTION_ARGS  )

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

981 {
983 
984 #ifdef NOT_USED
985  Oid typelem = PG_GETARG_OID(1);
986 #endif
987  int32 typmod = PG_GETARG_INT32(2);
989 
990  interval = (Interval *) palloc(sizeof(Interval));
991 
992  interval->time = pq_getmsgint64(buf);
993  interval->day = pq_getmsgint(buf, sizeof(interval->day));
994  interval->month = pq_getmsgint(buf, sizeof(interval->month));
995 
996  AdjustIntervalForTypmod(interval, typmod);
997 
998  PG_RETURN_INTERVAL_P(interval);
999 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:246
int32 day
Definition: timestamp.h:47
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1320
static char * buf
Definition: pg_test_fsync.c:67
#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:848
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1301 of file timestamp.c.

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

1302 {
1304  int32 typmod = PG_GETARG_INT32(1);
1305  Interval *result;
1306 
1307  result = palloc(sizeof(Interval));
1308  *result = *interval;
1309 
1310  AdjustIntervalForTypmod(result, typmod);
1311 
1312  PG_RETURN_INTERVAL_P(result);
1313 }
#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
signed int int32
Definition: c.h:246
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1320
void * palloc(Size size)
Definition: mcxt.c:848
Datum interval_send ( PG_FUNCTION_ARGS  )

Definition at line 1005 of file timestamp.c.

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

1006 {
1009 
1010  pq_begintypsend(&buf);
1011  pq_sendint64(&buf, interval->time);
1012  pq_sendint32(&buf, interval->day);
1013  pq_sendint32(&buf, interval->month);
1015 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
static void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.h:156
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
int32 day
Definition: timestamp.h:47
static char * buf
Definition: pg_test_fsync.c:67
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3030 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INTERVAL_P.

3031 {
3032  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3033  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3034  Interval *result;
3035 
3036  /* use interval_cmp_internal to be sure this agrees with comparisons */
3037  if (interval_cmp_internal(interval1, interval2) < 0)
3038  result = interval1;
3039  else
3040  result = interval2;
3041  PG_RETURN_INTERVAL_P(result);
3042 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2331
Datum interval_transform ( PG_FUNCTION_ARGS  )

Definition at line 1244 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, PG_GETARG_POINTER, PG_RETURN_POINTER, and relabel_to_typmod().

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

Definition at line 3945 of file timestamp.c.

References DecodeUnits(), downcase_truncate_identifier(), DTK_CENTURY, DTK_DAY, DTK_DECADE, DTK_HOUR, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_QUARTER, DTK_SECOND, DTK_WEEK, DTK_YEAR, elog, ereport, errcode(), errmsg(), ERROR, interval2tm(), palloc(), PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_RETURN_INTERVAL_P, tm, tm2interval(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, UNITS, val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

3946 {
3947  text *units = PG_GETARG_TEXT_PP(0);
3949  Interval *result;
3950  int type,
3951  val;
3952  char *lowunits;
3953  fsec_t fsec;
3954  struct pg_tm tt,
3955  *tm = &tt;
3956 
3957  result = (Interval *) palloc(sizeof(Interval));
3958 
3959  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
3960  VARSIZE_ANY_EXHDR(units),
3961  false);
3962 
3963  type = DecodeUnits(0, lowunits, &val);
3964 
3965  if (type == UNITS)
3966  {
3967  if (interval2tm(*interval, tm, &fsec) == 0)
3968  {
3969  switch (val)
3970  {
3971  /* fall through */
3972  case DTK_MILLENNIUM:
3973  /* caution: C division may have negative remainder */
3974  tm->tm_year = (tm->tm_year / 1000) * 1000;
3975  case DTK_CENTURY:
3976  /* caution: C division may have negative remainder */
3977  tm->tm_year = (tm->tm_year / 100) * 100;
3978  case DTK_DECADE:
3979  /* caution: C division may have negative remainder */
3980  tm->tm_year = (tm->tm_year / 10) * 10;
3981  case DTK_YEAR:
3982  tm->tm_mon = 0;
3983  case DTK_QUARTER:
3984  tm->tm_mon = 3 * (tm->tm_mon / 3);
3985  case DTK_MONTH:
3986  tm->tm_mday = 0;
3987  case DTK_DAY:
3988  tm->tm_hour = 0;
3989  case DTK_HOUR:
3990  tm->tm_min = 0;
3991  case DTK_MINUTE:
3992  tm->tm_sec = 0;
3993  case DTK_SECOND:
3994  fsec = 0;
3995  break;
3996  case DTK_MILLISEC:
3997  fsec = (fsec / 1000) * 1000;
3998  break;
3999  case DTK_MICROSEC:
4000  break;
4001 
4002  default:
4003  if (val == DTK_WEEK)
4004  ereport(ERROR,
4005  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4006  errmsg("interval units \"%s\" not supported "
4007  "because months usually have fractional weeks",
4008  lowunits)));
4009  else
4010  ereport(ERROR,
4011  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4012  errmsg("interval units \"%s\" not supported",
4013  lowunits)));
4014  }
4015 
4016  if (tm2interval(tm, fsec, result) != 0)
4017  ereport(ERROR,
4018  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4019  errmsg("interval out of range")));
4020  }
4021  else
4022  elog(ERROR, "could not convert interval to tm");
4023  }
4024  else
4025  {
4026  ereport(ERROR,
4027  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4028  errmsg("interval units \"%s\" not recognized",
4029  lowunits)));
4030  }
4031 
4032  PG_RETURN_INTERVAL_P(result);
4033 }
#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
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:107
#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:1902
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:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
#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:1930
Datum interval_um ( PG_FUNCTION_ARGS  )

Definition at line 3001 of file timestamp.c.

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

Referenced by abs_interval().

3002 {
3004  Interval *result;
3005 
3006  result = (Interval *) palloc(sizeof(Interval));
3007 
3008  result->time = -interval->time;
3009  /* overflow check copied from int4um */
3010  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
3011  ereport(ERROR,
3012  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3013  errmsg("interval out of range")));
3014  result->day = -interval->day;
3015  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
3016  ereport(ERROR,
3017  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3018  errmsg("interval out of range")));
3019  result->month = -interval->month;
3020  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3021  ereport(ERROR,
3022  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3023  errmsg("interval out of range")));
3024 
3025  PG_RETURN_INTERVAL_P(result);
3026 }
#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
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:46
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

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

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

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

1110 {
1111  int32 typmod = PG_GETARG_INT32(0);
1112  char *res = (char *) palloc(64);
1113  int fields;
1114  int precision;
1115  const char *fieldstr;
1116 
1117  if (typmod < 0)
1118  {
1119  *res = '\0';
1120  PG_RETURN_CSTRING(res);
1121  }
1122 
1123  fields = INTERVAL_RANGE(typmod);
1124  precision = INTERVAL_PRECISION(typmod);
1125 
1126  switch (fields)
1127  {
1128  case INTERVAL_MASK(YEAR):
1129  fieldstr = " year";
1130  break;
1131  case INTERVAL_MASK(MONTH):
1132  fieldstr = " month";
1133  break;
1134  case INTERVAL_MASK(DAY):
1135  fieldstr = " day";
1136  break;
1137  case INTERVAL_MASK(HOUR):
1138  fieldstr = " hour";
1139  break;
1140  case INTERVAL_MASK(MINUTE):
1141  fieldstr = " minute";
1142  break;
1143  case INTERVAL_MASK(SECOND):
1144  fieldstr = " second";
1145  break;
1147  fieldstr = " year to month";
1148  break;
1150  fieldstr = " day to hour";
1151  break;
1153  fieldstr = " day to minute";
1154  break;
1156  fieldstr = " day to second";
1157  break;
1159  fieldstr = " hour to minute";
1160  break;
1162  fieldstr = " hour to second";
1163  break;
1165  fieldstr = " minute to second";
1166  break;
1167  case INTERVAL_FULL_RANGE:
1168  fieldstr = "";
1169  break;
1170  default:
1171  elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1172  fieldstr = "";
1173  break;
1174  }
1175 
1176  if (precision != INTERVAL_FULL_PRECISION)
1177  snprintf(res, 64, "%s(%d)", fieldstr, precision);
1178  else
1179  snprintf(res, 64, "%s", fieldstr);
1180 
1181  PG_RETURN_CSTRING(res);
1182 }
#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:246
#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:848
#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 4061 of file timestamp.c.

References isoweek2j(), and j2date().

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

4062 {
4063  j2date(isoweek2j(*year, woy), year, mon, mday);
4064 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:4041
int isoweek2j ( int  year,
int  week 
)

Definition at line 4041 of file timestamp.c.

References date2j(), and j2day().

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

4042 {
4043  int day0,
4044  day4;
4045 
4046  /* fourth day of current year */
4047  day4 = date2j(year, 1, 4);
4048 
4049  /* day0 == offset to first day of week (Monday) */
4050  day0 = j2day(day4 - 1);
4051 
4052  return ((week - 1) * 7) + (day4 - day0);
4053 }
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 4074 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4075 {
4076  int jday;
4077 
4078  jday = isoweek2j(*year, isoweek);
4079  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4080  if (wday > 1)
4081  jday += wday - 2;
4082  else
4083  jday += 6;
4084  j2date(jday, year, mon, mday);
4085 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:4041
Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1487 of file timestamp.c.

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

1488 {
1489  int32 years = PG_GETARG_INT32(0);
1491  int32 weeks = PG_GETARG_INT32(2);
1492  int32 days = PG_GETARG_INT32(3);
1493  int32 hours = PG_GETARG_INT32(4);
1494  int32 mins = PG_GETARG_INT32(5);
1495  double secs = PG_GETARG_FLOAT8(6);
1496  Interval *result;
1497 
1498  /*
1499  * Reject out-of-range inputs. We really ought to check the integer
1500  * inputs as well, but it's not entirely clear what limits to apply.
1501  */
1502  if (isinf(secs) || isnan(secs))
1503  ereport(ERROR,
1504  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1505  errmsg("interval out of range")));
1506 
1507  result = (Interval *) palloc(sizeof(Interval));
1508  result->month = years * MONTHS_PER_YEAR + months;
1509  result->day = weeks * 7 + days;
1510 
1511  secs = rint(secs * USECS_PER_SEC);
1512  result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1513  mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1514  (int64) secs;
1515 
1516  PG_RETURN_INTERVAL_P(result);
1517 }
#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
signed int int32
Definition: c.h:246
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:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 630 of file timestamp.c.

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

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

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

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

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

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

651 {
652  int32 year = PG_GETARG_INT32(0);
653  int32 month = PG_GETARG_INT32(1);
654  int32 mday = PG_GETARG_INT32(2);
655  int32 hour = PG_GETARG_INT32(3);
656  int32 min = PG_GETARG_INT32(4);
657  float8 sec = PG_GETARG_FLOAT8(5);
658  Timestamp result;
659 
660  result = make_timestamp_internal(year, month, mday,
661  hour, min, sec);
662 
664 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition: timestamp.c:4966
#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:542
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:246
double float8
Definition: c.h:375
int64 Timestamp
Definition: timestamp.h:38
Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 671 of file timestamp.c.

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

672 {
673  int32 year = PG_GETARG_INT32(0);
674  int32 month = PG_GETARG_INT32(1);
675  int32 mday = PG_GETARG_INT32(2);
676  int32 hour = PG_GETARG_INT32(3);
677  int32 min = PG_GETARG_INT32(4);
678  float8 sec = PG_GETARG_FLOAT8(5);
680  TimestampTz result;
682  struct pg_tm tt;
683  int tz;
684  fsec_t fsec;
685 
686  timestamp = make_timestamp_internal(year, month, mday,
687  hour, min, sec);
688 
689  if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
690  ereport(ERROR,
691  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
692  errmsg("timestamp out of range")));
693 
694  tz = parse_sane_timezone(&tt, zone);
695 
696  result = dt2local(timestamp, -tz);
697 
698  if (!IS_VALID_TIMESTAMP(result))
699  ereport(ERROR,
700  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
701  errmsg("timestamp out of range")));
702 
703  PG_RETURN_TIMESTAMPTZ(result);
704 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h: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:542
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:246
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:375
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
int64 Timestamp
Definition: timestamp.h:38
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Definition: zic.c:92
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:1952
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition: timestamp.c:458
Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3210 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3211 {
3212  /* Args are float8 and Interval *, but leave them as generic Datum */
3213  Datum factor = PG_GETARG_DATUM(0);
3214  Datum span = PG_GETARG_DATUM(1);
3215 
3216  return DirectFunctionCall2(interval_mul, span, factor);
3217 }
#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:3133
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587
static float8 NonFiniteTimestampTzPart ( int  type,
int  unit,
char *  lowunits,
bool  isNegative,
bool  isTz 
)
static

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

4222 {
4223  if ((type != UNITS) && (type != RESERV))
4224  {
4225  if (isTz)
4226  ereport(ERROR,
4227  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4228  errmsg("timestamp with time zone units \"%s\" not recognized",
4229  lowunits)));
4230  else
4231  ereport(ERROR,
4232  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4233  errmsg("timestamp units \"%s\" not recognized",
4234  lowunits)));
4235  }
4236 
4237  switch (unit)
4238  {
4239  /* Oscillating units */
4240  case DTK_MICROSEC:
4241  case DTK_MILLISEC:
4242  case DTK_SECOND:
4243  case DTK_MINUTE:
4244  case DTK_HOUR:
4245  case DTK_DAY:
4246  case DTK_MONTH:
4247  case DTK_QUARTER:
4248  case DTK_WEEK:
4249  case DTK_DOW:
4250  case DTK_ISODOW:
4251  case DTK_DOY:
4252  case DTK_TZ:
4253  case DTK_TZ_MINUTE:
4254  case DTK_TZ_HOUR:
4255  return 0.0;
4256 
4257  /* Monotonically-increasing units */
4258  case DTK_YEAR:
4259  case DTK_DECADE:
4260  case DTK_CENTURY:
4261  case DTK_MILLENNIUM:
4262  case DTK_JULIAN:
4263  case DTK_ISOYEAR:
4264  case DTK_EPOCH:
4265  if (isNegative)
4266  return -get_float8_infinity();
4267  else
4268  return get_float8_infinity();
4269 
4270  default:
4271  if (isTz)
4272  ereport(ERROR,
4273  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4274  errmsg("timestamp with time zone units \"%s\" not supported",
4275  lowunits)));
4276  else
4277  ereport(ERROR,
4278  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4279  errmsg("timestamp units \"%s\" not supported",
4280  lowunits)));
4281  return 0.0; /* keep compiler quiet */
4282  }
4283 }
#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 2448 of file timestamp.c.

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

2449 {
2450  /*
2451  * The arguments are Timestamps, but we leave them as generic Datums to
2452  * avoid unnecessary conversions between value and reference forms --- not
2453  * to mention possible dereferences of null pointers.
2454  */
2455  Datum ts1 = PG_GETARG_DATUM(0);
2456  Datum te1 = PG_GETARG_DATUM(1);
2457  Datum ts2 = PG_GETARG_DATUM(2);
2458  Datum te2 = PG_GETARG_DATUM(3);
2459  bool ts1IsNull = PG_ARGISNULL(0);
2460  bool te1IsNull = PG_ARGISNULL(1);
2461  bool ts2IsNull = PG_ARGISNULL(2);
2462  bool te2IsNull = PG_ARGISNULL(3);
2463 
2464 #define TIMESTAMP_GT(t1,t2) \
2465  DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2466 #define TIMESTAMP_LT(t1,t2) \
2467  DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2468 
2469  /*
2470  * If both endpoints of interval 1 are null, the result is null (unknown).
2471  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2472  * take ts1 as the lesser endpoint.
2473  */
2474  if (ts1IsNull)
2475  {
2476  if (te1IsNull)
2477  PG_RETURN_NULL();
2478  /* swap null for non-null */
2479  ts1 = te1;
2480  te1IsNull = true;
2481  }
2482  else if (!te1IsNull)
2483  {
2484  if (TIMESTAMP_GT(ts1, te1))
2485  {
2486  Datum tt = ts1;
2487 
2488  ts1 = te1;
2489  te1 = tt;
2490  }
2491  }
2492 
2493  /* Likewise for interval 2. */
2494  if (ts2IsNull)
2495  {
2496  if (te2IsNull)
2497  PG_RETURN_NULL();
2498  /* swap null for non-null */
2499  ts2 = te2;
2500  te2IsNull = true;
2501  }
2502  else if (!te2IsNull)
2503  {
2504  if (TIMESTAMP_GT(ts2, te2))
2505  {
2506  Datum tt = ts2;
2507 
2508  ts2 = te2;
2509  te2 = tt;
2510  }
2511  }
2512 
2513  /*
2514  * At this point neither ts1 nor ts2 is null, so we can consider three
2515  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2516  */
2517  if (TIMESTAMP_GT(ts1, ts2))
2518  {
2519  /*
2520  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2521  * in the presence of nulls it's not quite completely so.
2522  */
2523  if (te2IsNull)
2524  PG_RETURN_NULL();
2525  if (TIMESTAMP_LT(ts1, te2))
2526  PG_RETURN_BOOL(true);
2527  if (te1IsNull)
2528  PG_RETURN_NULL();
2529 
2530  /*
2531  * If te1 is not null then we had ts1 <= te1 above, and we just found
2532  * ts1 >= te2, hence te1 >= te2.
2533  */
2534  PG_RETURN_BOOL(false);
2535  }
2536  else if (TIMESTAMP_LT(ts1, ts2))
2537  {
2538  /* This case is ts2 < te1 OR te2 < te1 */
2539  if (te1IsNull)
2540  PG_RETURN_NULL();
2541  if (TIMESTAMP_LT(ts2, te1))
2542  PG_RETURN_BOOL(true);
2543  if (te2IsNull)
2544  PG_RETURN_NULL();
2545 
2546  /*
2547  * If te2 is not null then we had ts2 <= te2 above, and we just found
2548  * ts2 >= te1, hence te2 >= te1.
2549  */
2550  PG_RETURN_BOOL(false);
2551  }
2552  else
2553  {
2554  /*
2555  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2556  * rather silly way of saying "true if both are non-null, else null".
2557  */
2558  if (te1IsNull || te2IsNull)
2559  PG_RETURN_NULL();
2560  PG_RETURN_BOOL(true);
2561  }
2562 
2563 #undef TIMESTAMP_GT
2564 #undef TIMESTAMP_LT
2565 }
#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 458 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().

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

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

1559 {
1561 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgReloadTime
Definition: timestamp.c:52
Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1552 of file timestamp.c.

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

1553 {
1555 }
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
TimestampTz PgStartTime
Definition: timestamp.c:49
Timestamp SetEpochTimestamp ( void  )

Definition at line 2003 of file timestamp.c.

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

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

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

Definition at line 1540 of file timestamp.c.

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

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

Definition at line 1946 of file timestamp.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp().

1947 {
1948  return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
1949 }
#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 1670 of file timestamp.c.

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

1671 {
1672  TimestampTz result;
1673 
1674  result = (TimestampTz) tm -
1676  result *= USECS_PER_SEC;
1677 
1678  return result;
1679 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:107
#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 4966 of file timestamp.c.

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

4967 {
4968  TimestampTz result;
4969  struct pg_tm tt,
4970  *tm = &tt;
4971  fsec_t fsec;
4972  int tz;
4973 
4975  result = timestamp;
4976  else
4977  {
4978  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4979  ereport(ERROR,
4980  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4981  errmsg("timestamp out of range")));
4982 
4984 
4985  if (tm2timestamp(tm, fsec, &tz, &result) != 0)
4986  ereport(ERROR,
4987  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4988  errmsg("timestamp out of range")));
4989  }
4990 
4991  return result;
4992 }
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:1758
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
#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:1854
int errmsg(const char *fmt,...)
Definition: elog.c:797
pg_tz * session_timezone
Definition: pgtz.c:28
int timestamp2tm ( Timestamp  dt,
int *  tzp,
struct pg_tm tm,
fsec_t fsec,
const char **  tzn,
pg_tz attimezone 
)

Definition at line 1758 of file timestamp.c.

References dt2time(), j2date(), 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().

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

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

3426 {
3427  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
3428  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
3429  Interval *result;
3430  fsec_t fsec,
3431  fsec1,
3432  fsec2;
3433  struct pg_tm tt,
3434  *tm = &tt;
3435  struct pg_tm tt1,
3436  *tm1 = &tt1;
3437  struct pg_tm tt2,
3438  *tm2 = &tt2;
3439 
3440  result = (Interval *) palloc(sizeof(Interval));
3441 
3442  if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
3443  timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
3444  {
3445  /* form the symbolic difference */
3446  fsec = fsec1 - fsec2;
3447  tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3448  tm->tm_min = tm1->tm_min - tm2->tm_min;
3449  tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3450  tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3451  tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3452  tm->tm_year = tm1->tm_year - tm2->tm_year;
3453 
3454  /* flip sign if necessary... */
3455  if (dt1 < dt2)
3456  {
3457  fsec = -fsec;
3458  tm->tm_sec = -tm->tm_sec;
3459  tm->tm_min = -tm->tm_min;
3460  tm->tm_hour = -tm->tm_hour;
3461  tm->tm_mday = -tm->tm_mday;
3462  tm->tm_mon = -tm->tm_mon;
3463  tm->tm_year = -tm->tm_year;
3464  }
3465 
3466  /* propagate any negative fields into the next higher field */
3467  while (fsec < 0)
3468  {
3469  fsec += USECS_PER_SEC;
3470  tm->tm_sec--;
3471  }
3472 
3473  while (tm->tm_sec < 0)
3474  {
3475  tm->tm_sec += SECS_PER_MINUTE;
3476  tm->tm_min--;
3477  }
3478 
3479  while (tm->tm_min < 0)
3480  {
3481  tm->tm_min += MINS_PER_HOUR;
3482  tm->tm_hour--;
3483  }
3484 
3485  while (tm->tm_hour < 0)
3486  {
3487  tm->tm_hour += HOURS_PER_DAY;
3488  tm->tm_mday--;
3489  }
3490 
3491  while (tm->tm_mday < 0)
3492  {
3493  if (dt1 < dt2)
3494  {
3495  tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
3496  tm->tm_mon--;
3497  }
3498  else
3499  {
3500  tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
3501  tm->tm_mon--;
3502  }
3503  }
3504 
3505  while (tm->tm_mon < 0)
3506  {
3507  tm->tm_mon += MONTHS_PER_YEAR;
3508  tm->tm_year--;
3509  }
3510 
3511  /* recover sign if necessary... */
3512  if (dt1 < dt2)
3513  {
3514  fsec = -fsec;
3515  tm->tm_sec = -tm->tm_sec;
3516  tm->tm_min = -tm->tm_min;
3517  tm->tm_hour = -tm->tm_hour;
3518  tm->tm_mday = -tm->tm_mday;
3519  tm->tm_mon = -tm->tm_mon;
3520  tm->tm_year = -tm->tm_year;
3521  }
3522 
3523  if (tm2interval(tm, fsec, result) != 0)
3524  ereport(ERROR,
3525  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3526  errmsg("interval out of range")));
3527  }
3528  else
3529  ereport(ERROR,
3530  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3531  errmsg("timestamp out of range")));
3532 
3533  PG_RETURN_INTERVAL_P(result);
3534 }
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:35
#define isleap(y)
Definition: datetime.h:273
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758