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_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 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, INT64CONST, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_PRECISION, INTERVAL_RANGE, MAX_INTERVAL_PRECISION, MINUTE, Interval::month, MONTH, MONTHS_PER_YEAR, range(), SECOND, Interval::time, USECS_PER_HOUR, USECS_PER_MINUTE, and YEAR.

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

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 INT64CONST(x)
Definition: c.h:310
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
#define HOUR
Definition: datetime.h:101
static void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)
static

Definition at line 334 of file timestamp.c.

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

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

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
#define INT64CONST(x)
Definition: c.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:797
int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 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:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c: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 4073 of file timestamp.c.

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

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

4074 {
4075  float8 result;
4076  int day0,
4077  day4,
4078  dayn;
4079 
4080  /* current day */
4081  dayn = date2j(year, mon, mday);
4082 
4083  /* fourth day of current year */
4084  day4 = date2j(year, 1, 4);
4085 
4086  /* day0 == offset to first day of week (Monday) */
4087  day0 = j2day(day4 - 1);
4088 
4089  /*
4090  * We need the first week containing a Thursday, otherwise this day falls
4091  * into the previous year for purposes of counting weeks
4092  */
4093  if (dayn < day4 - day0)
4094  {
4095  day4 = date2j(year - 1, 1, 4);
4096 
4097  /* day0 == offset to first day of week (Monday) */
4098  day0 = j2day(day4 - 1);
4099  }
4100 
4101  result = (dayn - (day4 - day0)) / 7 + 1;
4102 
4103  /*
4104  * Sometimes the last few days in a year will fall into the first week of
4105  * the next year, so check for this.
4106  */
4107  if (result >= 52)
4108  {
4109  day4 = date2j(year + 1, 1, 4);
4110 
4111  /* day0 == offset to first day of week (Monday) */
4112  day0 = j2day(day4 - 1);
4113 
4114  if (dayn >= day4 - day0)
4115  result = (dayn - (day4 - day0)) / 7 + 1;
4116  }
4117 
4118  return (int) result;
4119 }
return result
Definition: formatting.c:1632
double float8
Definition: c.h:381
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
int date2isoyear ( int  year,
int  mon,
int  mday 
)

Definition at line 4127 of file timestamp.c.

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

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

4128 {
4129  float8 result;
4130  int day0,
4131  day4,
4132  dayn;
4133 
4134  /* current day */
4135  dayn = date2j(year, mon, mday);
4136 
4137  /* fourth day of current year */
4138  day4 = date2j(year, 1, 4);
4139 
4140  /* day0 == offset to first day of week (Monday) */
4141  day0 = j2day(day4 - 1);
4142 
4143  /*
4144  * We need the first week containing a Thursday, otherwise this day falls
4145  * into the previous year for purposes of counting weeks
4146  */
4147  if (dayn < day4 - day0)
4148  {
4149  day4 = date2j(year - 1, 1, 4);
4150 
4151  /* day0 == offset to first day of week (Monday) */
4152  day0 = j2day(day4 - 1);
4153 
4154  year--;
4155  }
4156 
4157  result = (dayn - (day4 - day0)) / 7 + 1;
4158 
4159  /*
4160  * Sometimes the last few days in a year will fall into the first week of
4161  * the next year, so check for this.
4162  */
4163  if (result >= 52)
4164  {
4165  day4 = date2j(year + 1, 1, 4);
4166 
4167  /* day0 == offset to first day of week (Monday) */
4168  day0 = j2day(day4 - 1);
4169 
4170  if (dayn >= day4 - day0)
4171  year++;
4172  }
4173 
4174  return year;
4175 }
return result
Definition: formatting.c:1632
double float8
Definition: c.h:381
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
int date2isoyearday ( int  year,
int  mon,
int  mday 
)

Definition at line 4184 of file timestamp.c.

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

Referenced by DCH_to_char().

4185 {
4186  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4187 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4127
int isoweek2j(int year, int week)
Definition: timestamp.c:4022
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, result, 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);
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
return result
Definition: formatting.c:1632
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
int isinf(double x)
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
#define DATETIME_MIN_JULIAN
Definition: timestamp.h:179
Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 5136 of file timestamp.c.

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

5137 {
5138  FuncCallContext *funcctx;
5140  Timestamp result;
5141 
5142  /* stuff done only on the first call of the function */
5143  if (SRF_IS_FIRSTCALL())
5144  {
5145  Timestamp start = PG_GETARG_TIMESTAMP(0);
5146  Timestamp finish = PG_GETARG_TIMESTAMP(1);
5147  Interval *step = PG_GETARG_INTERVAL_P(2);
5148  MemoryContext oldcontext;
5149  Interval interval_zero;
5150 
5151  /* create a function context for cross-call persistence */
5152  funcctx = SRF_FIRSTCALL_INIT();
5153 
5154  /*
5155  * switch to memory context appropriate for multiple function calls
5156  */
5157  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5158 
5159  /* allocate memory for user context */
5162 
5163  /*
5164  * Use fctx to keep state from call to call. Seed current with the
5165  * original start value
5166  */
5167  fctx->current = start;
5168  fctx->finish = finish;
5169  fctx->step = *step;
5170 
5171  /* Determine sign of the interval */
5172  MemSet(&interval_zero, 0, sizeof(Interval));
5173  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5174 
5175  if (fctx->step_sign == 0)
5176  ereport(ERROR,
5177  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5178  errmsg("step size cannot equal zero")));
5179 
5180  funcctx->user_fctx = fctx;
5181  MemoryContextSwitchTo(oldcontext);
5182  }
5183 
5184  /* stuff done on every call of the function */
5185  funcctx = SRF_PERCALL_SETUP();
5186 
5187  /*
5188  * get the saved state and use current as the result for this iteration
5189  */
5190  fctx = funcctx->user_fctx;
5191  result = fctx->current;
5192 
5193  if (fctx->step_sign > 0 ?
5194  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5195  timestamp_cmp_internal(result, fctx->finish) >= 0)
5196  {
5197  /* increment current in preparation for next iteration */
5198  fctx->current = DatumGetTimestamp(
5200  TimestampGetDatum(fctx->current),
5201  PointerGetDatum(&fctx->step)));
5202 
5203  /* do when there is more left to send */
5204  SRF_RETURN_NEXT(funcctx, TimestampGetDatum(result));
5205  }
5206  else
5207  {
5208  /* do when there is no more left */
5209  SRF_RETURN_DONE(funcctx);
5210  }
5211 }
#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:857
return result
Definition: formatting.c:1632
#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:2776
#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:2326
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 5217 of file timestamp.c.

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

5218 {
5219  FuncCallContext *funcctx;
5222 
5223  /* stuff done only on the first call of the function */
5224  if (SRF_IS_FIRSTCALL())
5225  {
5227  TimestampTz finish = PG_GETARG_TIMESTAMPTZ(1);
5228  Interval *step = PG_GETARG_INTERVAL_P(2);
5229  MemoryContext oldcontext;
5230  Interval interval_zero;
5231 
5232  /* create a function context for cross-call persistence */
5233  funcctx = SRF_FIRSTCALL_INIT();
5234 
5235  /*
5236  * switch to memory context appropriate for multiple function calls
5237  */
5238  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
5239 
5240  /* allocate memory for user context */
5243 
5244  /*
5245  * Use fctx to keep state from call to call. Seed current with the
5246  * original start value
5247  */
5248  fctx->current = start;
5249  fctx->finish = finish;
5250  fctx->step = *step;
5251 
5252  /* Determine sign of the interval */
5253  MemSet(&interval_zero, 0, sizeof(Interval));
5254  fctx->step_sign = interval_cmp_internal(&fctx->step, &interval_zero);
5255 
5256  if (fctx->step_sign == 0)
5257  ereport(ERROR,
5258  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5259  errmsg("step size cannot equal zero")));
5260 
5261  funcctx->user_fctx = fctx;
5262  MemoryContextSwitchTo(oldcontext);
5263  }
5264 
5265  /* stuff done on every call of the function */
5266  funcctx = SRF_PERCALL_SETUP();
5267 
5268  /*
5269  * get the saved state and use current as the result for this iteration
5270  */
5271  fctx = funcctx->user_fctx;
5272  result = fctx->current;
5273 
5274  if (fctx->step_sign > 0 ?
5275  timestamp_cmp_internal(result, fctx->finish) <= 0 :
5276  timestamp_cmp_internal(result, fctx->finish) >= 0)
5277  {
5278  /* increment current in preparation for next iteration */
5279  fctx->current = DatumGetTimestampTz(
5282  PointerGetDatum(&fctx->step)));
5283 
5284  /* do when there is more left to send */
5285  SRF_RETURN_NEXT(funcctx, TimestampTzGetDatum(result));
5286  }
5287  else
5288  {
5289  /* do when there is no more left */
5290  SRF_RETURN_DONE(funcctx);
5291  }
5292 }
#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:857
return result
Definition: formatting.c:1632
#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:2881
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2326
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1570 of file timestamp.c.

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

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), asyncQueueFillWarning(), autovac_refresh_stats(), AutoVacLauncherMain(), backend_read_statsfile(), BackendInitialize(), BackgroundWriterMain(), check_log_duration(), CheckPointBuffers(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_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(), 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 {
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
return result
Definition: formatting.c:1632
#define SECS_PER_DAY
Definition: timestamp.h:86
#define NULL
Definition: c.h:229
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
void GetEpochTime ( struct pg_tm tm)

Definition at line 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:708
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:4987
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:708
int interval2tm ( Interval  span,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 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 3253 of file timestamp.c.

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

3254 {
3255  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3257  Datum *transdatums;
3258  int ndatums;
3259  Interval sumX,
3260  N;
3261  Interval *newsum;
3262  ArrayType *result;
3263 
3264  deconstruct_array(transarray,
3265  INTERVALOID, sizeof(Interval), false, 'd',
3266  &transdatums, NULL, &ndatums);
3267  if (ndatums != 2)
3268  elog(ERROR, "expected 2-element interval array");
3269 
3270  sumX = *(DatumGetIntervalP(transdatums[0]));
3271  N = *(DatumGetIntervalP(transdatums[1]));
3272 
3274  IntervalPGetDatum(&sumX),
3275  IntervalPGetDatum(newval)));
3276  N.time += 1;
3277 
3278  transdatums[0] = IntervalPGetDatum(newsum);
3279  transdatums[1] = IntervalPGetDatum(&N);
3280 
3281  result = construct_array(transdatums, 2,
3282  INTERVALOID, sizeof(Interval), false, 'd');
3283 
3284  PG_RETURN_ARRAYTYPE_P(result);
3285 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
return result
Definition: formatting.c:1632
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3040
#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:246
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum interval_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 3337 of file timestamp.c.

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

3338 {
3339  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3341  Datum *transdatums;
3342  int ndatums;
3343  Interval sumX,
3344  N;
3345  Interval *newsum;
3346  ArrayType *result;
3347 
3348  deconstruct_array(transarray,
3349  INTERVALOID, sizeof(Interval), false, 'd',
3350  &transdatums, NULL, &ndatums);
3351  if (ndatums != 2)
3352  elog(ERROR, "expected 2-element interval array");
3353 
3354  sumX = *(DatumGetIntervalP(transdatums[0]));
3355  N = *(DatumGetIntervalP(transdatums[1]));
3356 
3358  IntervalPGetDatum(&sumX),
3359  IntervalPGetDatum(newval)));
3360  N.time -= 1;
3361 
3362  transdatums[0] = IntervalPGetDatum(newsum);
3363  transdatums[1] = IntervalPGetDatum(&N);
3364 
3365  result = construct_array(transdatums, 2,
3366  INTERVALOID, sizeof(Interval), false, 'd');
3367 
3368  PG_RETURN_ARRAYTYPE_P(result);
3369 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
return result
Definition: formatting.c:1632
Datum interval_mi(PG_FUNCTION_ARGS)
Definition: timestamp.c:3074
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:529
TimeOffset time
Definition: timestamp.h:45
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
#define newval
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 3372 of file timestamp.c.

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

3373 {
3374  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3375  Datum *transdatums;
3376  int ndatums;
3377  Interval sumX,
3378  N;
3379 
3380  deconstruct_array(transarray,
3381  INTERVALOID, sizeof(Interval), false, 'd',
3382  &transdatums, NULL, &ndatums);
3383  if (ndatums != 2)
3384  elog(ERROR, "expected 2-element interval array");
3385 
3386  sumX = *(DatumGetIntervalP(transdatums[0]));
3387  N = *(DatumGetIntervalP(transdatums[1]));
3388 
3389  /* SQL defines AVG of no values to be NULL */
3390  if (N.time == 0)
3391  PG_RETURN_NULL();
3392 
3394  IntervalPGetDatum(&sumX),
3395  Float8GetDatum((double) N.time));
3396 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1815
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define IntervalPGetDatum(X)
Definition: timestamp.h:33
#define INTERVALOID
Definition: pg_type.h:529
TimeOffset time
Definition: timestamp.h:45
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
Datum interval_div(PG_FUNCTION_ARGS)
Definition: timestamp.c:3201
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum interval_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2389 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_INT32.

Referenced by gbt_intvkey_cmp().

2390 {
2391  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2392  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2393 
2394  PG_RETURN_INT32(interval_cmp_internal(interval1, interval2));
2395 }
#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:2326
static int interval_cmp_internal ( Interval interval1,
Interval interval2 
)
static

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

2327 {
2328  INT128 span1 = interval_cmp_value(interval1);
2329  INT128 span2 = interval_cmp_value(interval2);
2330 
2331  return int128_compare(span1, span2);
2332 }
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:2301
static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2301 of file timestamp.c.

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

Referenced by interval_cmp_internal(), and interval_hash().

2302 {
2303  INT128 span;
2304  int64 dayfraction;
2305  int64 days;
2306 
2307  /*
2308  * Separate time field into days and dayfraction, then add the month and
2309  * day fields to the days part. We cannot overflow int64 days here.
2310  */
2311  dayfraction = interval->time % USECS_PER_DAY;
2312  days = interval->time / USECS_PER_DAY;
2313  days += interval->month * INT64CONST(30);
2314  days += interval->day;
2315 
2316  /* Widen dayfraction to 128 bits */
2317  span = int64_to_int128(dayfraction);
2318 
2319  /* Scale up days to microseconds, forming a 128-bit product */
2321 
2322  return span;
2323 }
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
#define INT64CONST(x)
Definition: c.h:310
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 3288 of file timestamp.c.

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

3289 {
3290  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3291  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3292  Datum *transdatums1;
3293  Datum *transdatums2;
3294  int ndatums1;
3295  int ndatums2;
3296  Interval sum1,
3297  N1;
3298  Interval sum2,
3299  N2;
3300 
3301  Interval *newsum;
3302  ArrayType *result;
3303 
3304  deconstruct_array(transarray1,
3305  INTERVALOID, sizeof(Interval), false, 'd',
3306  &transdatums1, NULL, &ndatums1);
3307  if (ndatums1 != 2)
3308  elog(ERROR, "expected 2-element interval array");
3309 
3310  sum1 = *(DatumGetIntervalP(transdatums1[0]));
3311  N1 = *(DatumGetIntervalP(transdatums1[1]));
3312 
3313  deconstruct_array(transarray2,
3314  INTERVALOID, sizeof(Interval), false, 'd',
3315  &transdatums2, NULL, &ndatums2);
3316  if (ndatums2 != 2)
3317  elog(ERROR, "expected 2-element interval array");
3318 
3319  sum2 = *(DatumGetIntervalP(transdatums2[0]));
3320  N2 = *(DatumGetIntervalP(transdatums2[1]));
3321 
3323  IntervalPGetDatum(&sum1),
3324  IntervalPGetDatum(&sum2)));
3325  N1.time += N2.time;
3326 
3327  transdatums1[0] = IntervalPGetDatum(newsum);
3328  transdatums1[1] = IntervalPGetDatum(&N1);
3329 
3330  result = construct_array(transdatums1, 2,
3331  INTERVALOID, sizeof(Interval), false, 'd');
3332 
3333  PG_RETURN_ARRAYTYPE_P(result);
3334 }
#define DatumGetIntervalP(X)
Definition: timestamp.h:29
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
return result
Definition: formatting.c:1632
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
Datum interval_pl(PG_FUNCTION_ARGS)
Definition: timestamp.c:3040
#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:246
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define elog
Definition: elog.h:219
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3201 of file timestamp.c.

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

Referenced by interval_avg().

3202 {
3203  Interval *span = PG_GETARG_INTERVAL_P(0);
3204  float8 factor = PG_GETARG_FLOAT8(1);
3205  double month_remainder_days,
3206  sec_remainder;
3207  int32 orig_month = span->month,
3208  orig_day = span->day;
3209  Interval *result;
3210 
3211  result = (Interval *) palloc(sizeof(Interval));
3212 
3213  if (factor == 0.0)
3214  ereport(ERROR,
3215  (errcode(ERRCODE_DIVISION_BY_ZERO),
3216  errmsg("division by zero")));
3217 
3218  result->month = (int32) (span->month / factor);
3219  result->day = (int32) (span->day / factor);
3220 
3221  /*
3222  * Fractional months full days into days. See comment in interval_mul().
3223  */
3224  month_remainder_days = (orig_month / factor - result->month) * DAYS_PER_MONTH;
3225  month_remainder_days = TSROUND(month_remainder_days);
3226  sec_remainder = (orig_day / factor - result->day +
3227  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3228  sec_remainder = TSROUND(sec_remainder);
3229  if (Abs(sec_remainder) >= SECS_PER_DAY)
3230  {
3231  result->day += (int) (sec_remainder / SECS_PER_DAY);
3232  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3233  }
3234 
3235  /* cascade units down */
3236  result->day += (int32) month_remainder_days;
3237  result->time = rint(span->time / factor + sec_remainder * USECS_PER_SEC);
3238 
3239  PG_RETURN_INTERVAL_P(result);
3240 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:812
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_eq ( PG_FUNCTION_ARGS  )

Definition at line 2335 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intveq().

2336 {
2337  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2338  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2339 
2340  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) == 0);
2341 }
#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:2326
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 2380 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvge().

2381 {
2382  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2383  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2384 
2385  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) >= 0);
2386 }
#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:2326
Datum interval_gt ( PG_FUNCTION_ARGS  )

Definition at line 2362 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvgt().

2363 {
2364  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2365  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2366 
2367  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) > 0);
2368 }
#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:2326
Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2405 of file timestamp.c.

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

2406 {
2408  INT128 span = interval_cmp_value(interval);
2409  int64 span64;
2410 
2411  /*
2412  * Use only the least significant 64 bits for hashing. The upper 64 bits
2413  * seldom add any useful information, and besides we must do it like this
2414  * for compatibility with hashes calculated before use of INT128 was
2415  * introduced.
2416  */
2417  span64 = int128_to_int64(span);
2418 
2420 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
Datum hashint8(PG_FUNCTION_ARGS)
Definition: hashfunc.c:62
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#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:2301
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(), result, tm, tm2interval(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by check_timezone(), and flatten_set_variable_args().

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
return result
Definition: formatting.c:1632
Definition: pgtime.h:25
unsigned int Oid
Definition: postgres_ext.h:31
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define DTK_INVALID
Definition: datetime.h:151
signed int int32
Definition: c.h:256
static struct pg_tm tm
Definition: localtime.c:111
#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:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3095
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition: datetime.c:562
int tm_min
Definition: pgtime.h:28
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1930
Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 2736 of file timestamp.c.

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

2737 {
2738  Interval *span = PG_GETARG_INTERVAL_P(0);
2739  Interval *result;
2740  int32 wholemonth;
2741 
2742  result = (Interval *) palloc(sizeof(Interval));
2743  result->month = span->month;
2744  result->day = span->day;
2745  result->time = span->time;
2746 
2747  wholemonth = result->day / DAYS_PER_MONTH;
2748  result->day -= wholemonth * DAYS_PER_MONTH;
2749  result->month += wholemonth;
2750 
2751  if (result->month > 0 && result->day < 0)
2752  {
2753  result->day += DAYS_PER_MONTH;
2754  result->month--;
2755  }
2756  else if (result->month < 0 && result->day > 0)
2757  {
2758  result->day -= DAYS_PER_MONTH;
2759  result->month++;
2760  }
2761 
2762  PG_RETURN_INTERVAL_P(result);
2763 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:849
Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

Definition at line 2701 of file timestamp.c.

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

Referenced by timestamp_mi().

2702 {
2703  Interval *span = PG_GETARG_INTERVAL_P(0);
2704  Interval *result;
2705  TimeOffset wholeday;
2706 
2707  result = (Interval *) palloc(sizeof(Interval));
2708  result->month = span->month;
2709  result->day = span->day;
2710  result->time = span->time;
2711 
2712  TMODULO(result->time, wholeday, USECS_PER_DAY);
2713  result->day += wholeday; /* could overflow... */
2714 
2715  if (result->day > 0 && result->time < 0)
2716  {
2717  result->time += USECS_PER_DAY;
2718  result->day--;
2719  }
2720  else if (result->day < 0 && result->time > 0)
2721  {
2722  result->time -= USECS_PER_DAY;
2723  result->day++;
2724  }
2725 
2726  PG_RETURN_INTERVAL_P(result);
2727 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1632
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
void * palloc(Size size)
Definition: mcxt.c:849
#define TMODULO(t, q, u)
Definition: datetime.h:251
Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2646 of file timestamp.c.

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

2647 {
2648  Interval *span = PG_GETARG_INTERVAL_P(0);
2649  Interval *result;
2650  TimeOffset wholeday;
2651  int32 wholemonth;
2652 
2653  result = (Interval *) palloc(sizeof(Interval));
2654  result->month = span->month;
2655  result->day = span->day;
2656  result->time = span->time;
2657 
2658  TMODULO(result->time, wholeday, USECS_PER_DAY);
2659  result->day += wholeday; /* could overflow... */
2660 
2661  wholemonth = result->day / DAYS_PER_MONTH;
2662  result->day -= wholemonth * DAYS_PER_MONTH;
2663  result->month += wholemonth;
2664 
2665  if (result->month > 0 &&
2666  (result->day < 0 || (result->day == 0 && result->time < 0)))
2667  {
2668  result->day += DAYS_PER_MONTH;
2669  result->month--;
2670  }
2671  else if (result->month < 0 &&
2672  (result->day > 0 || (result->day == 0 && result->time > 0)))
2673  {
2674  result->day -= DAYS_PER_MONTH;
2675  result->month++;
2676  }
2677 
2678  if (result->day > 0 && result->time < 0)
2679  {
2680  result->time += USECS_PER_DAY;
2681  result->day--;
2682  }
2683  else if (result->day < 0 && result->time > 0)
2684  {
2685  result->time -= USECS_PER_DAY;
2686  result->day++;
2687  }
2688 
2689  PG_RETURN_INTERVAL_P(result);
2690 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
TimeOffset time
Definition: timestamp.h:45
#define USECS_PER_DAY
Definition: timestamp.h:91
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define DAYS_PER_MONTH
Definition: timestamp.h:77
void * palloc(Size size)
Definition: mcxt.c:849
#define TMODULO(t, q, u)
Definition: datetime.h:251
Datum interval_larger ( PG_FUNCTION_ARGS  )

Definition at line 3026 of file timestamp.c.

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

3027 {
3028  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3029  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3030  Interval *result;
3031 
3032  if (interval_cmp_internal(interval1, interval2) > 0)
3033  result = interval1;
3034  else
3035  result = interval2;
3036  PG_RETURN_INTERVAL_P(result);
3037 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1632
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2326
Datum interval_le ( PG_FUNCTION_ARGS  )

Definition at line 2371 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by gbt_intvle().

2372 {
2373  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2374  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2375 
2376  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) <= 0);
2377 }
#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:2326
Datum interval_lt ( PG_FUNCTION_ARGS  )

Definition at line 2353 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

Referenced by abs_interval(), and gbt_intvlt().

2354 {
2355  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2356  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2357 
2358  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) < 0);
2359 }
#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:2326
Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3074 of file timestamp.c.

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

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

3075 {
3076  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3077  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3078  Interval *result;
3079 
3080  result = (Interval *) palloc(sizeof(Interval));
3081 
3082  result->month = span1->month - span2->month;
3083  /* overflow check copied from int4mi */
3084  if (!SAMESIGN(span1->month, span2->month) &&
3085  !SAMESIGN(result->month, span1->month))
3086  ereport(ERROR,
3087  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3088  errmsg("interval out of range")));
3089 
3090  result->day = span1->day - span2->day;
3091  if (!SAMESIGN(span1->day, span2->day) &&
3092  !SAMESIGN(result->day, span1->day))
3093  ereport(ERROR,
3094  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3095  errmsg("interval out of range")));
3096 
3097  result->time = span1->time - span2->time;
3098  if (!SAMESIGN(span1->time, span2->time) &&
3099  !SAMESIGN(result->time, span1->time))
3100  ereport(ERROR,
3101  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3102  errmsg("interval out of range")));
3103 
3104  PG_RETURN_INTERVAL_P(result);
3105 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
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:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3114 of file timestamp.c.

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

Referenced by interval_lerp(), and mul_d_interval().

3115 {
3116  Interval *span = PG_GETARG_INTERVAL_P(0);
3117  float8 factor = PG_GETARG_FLOAT8(1);
3118  double month_remainder_days,
3119  sec_remainder,
3120  result_double;
3121  int32 orig_month = span->month,
3122  orig_day = span->day;
3123  Interval *result;
3124 
3125  result = (Interval *) palloc(sizeof(Interval));
3126 
3127  result_double = span->month * factor;
3128  if (isnan(result_double) ||
3129  result_double > INT_MAX || result_double < INT_MIN)
3130  ereport(ERROR,
3131  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3132  errmsg("interval out of range")));
3133  result->month = (int32) result_double;
3134 
3135  result_double = span->day * factor;
3136  if (isnan(result_double) ||
3137  result_double > INT_MAX || result_double < INT_MIN)
3138  ereport(ERROR,
3139  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3140  errmsg("interval out of range")));
3141  result->day = (int32) result_double;
3142 
3143  /*
3144  * The above correctly handles the whole-number part of the month and day
3145  * products, but we have to do something with any fractional part
3146  * resulting when the factor is non-integral. We cascade the fractions
3147  * down to lower units using the conversion factors DAYS_PER_MONTH and
3148  * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3149  * so by the representation. The user can choose to cascade up later,
3150  * using justify_hours and/or justify_days.
3151  */
3152 
3153  /*
3154  * Fractional months full days into days.
3155  *
3156  * Floating point calculation are inherently imprecise, so these
3157  * calculations are crafted to produce the most reliable result possible.
3158  * TSROUND() is needed to more accurately produce whole numbers where
3159  * appropriate.
3160  */
3161  month_remainder_days = (orig_month * factor - result->month) * DAYS_PER_MONTH;
3162  month_remainder_days = TSROUND(month_remainder_days);
3163  sec_remainder = (orig_day * factor - result->day +
3164  month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
3165  sec_remainder = TSROUND(sec_remainder);
3166 
3167  /*
3168  * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3169  * cascade from months and days. It might still be >24 if the combination
3170  * of cascade and the seconds factor operation itself.
3171  */
3172  if (Abs(sec_remainder) >= SECS_PER_DAY)
3173  {
3174  result->day += (int) (sec_remainder / SECS_PER_DAY);
3175  sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
3176  }
3177 
3178  /* cascade units down */
3179  result->day += (int32) month_remainder_days;
3180  result_double = rint(span->time * factor + sec_remainder * USECS_PER_SEC);
3181  if (result_double > PG_INT64_MAX || result_double < PG_INT64_MIN)
3182  ereport(ERROR,
3183  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3184  errmsg("interval out of range")));
3185  result->time = (int64) result_double;
3186 
3187  PG_RETURN_INTERVAL_P(result);
3188 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:246
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_INT64_MAX
Definition: c.h:343
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
#define Abs(x)
Definition: c.h:812
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
#define PG_INT64_MIN
Definition: c.h:342
TimeOffset time
Definition: timestamp.h:45
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int32 month
Definition: timestamp.h:48
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define TSROUND(j)
Definition: timestamp.h:61
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum interval_ne ( PG_FUNCTION_ARGS  )

Definition at line 2344 of file timestamp.c.

References interval_cmp_internal(), PG_GETARG_INTERVAL_P, and PG_RETURN_BOOL.

2345 {
2346  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
2347  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
2348 
2349  PG_RETURN_BOOL(interval_cmp_internal(interval1, interval2) != 0);
2350 }
#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:2326
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(), result, 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:1077
int IntervalStyle
Definition: globals.c:108
return result
Definition: formatting.c:1632
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:111
#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:66
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 4673 of file timestamp.c.

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

4674 {
4675  text *units = PG_GETARG_TEXT_PP(0);
4677  float8 result;
4678  int type,
4679  val;
4680  char *lowunits;
4681  fsec_t fsec;
4682  struct pg_tm tt,
4683  *tm = &tt;
4684 
4685  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
4686  VARSIZE_ANY_EXHDR(units),
4687  false);
4688 
4689  type = DecodeUnits(0, lowunits, &val);
4690  if (type == UNKNOWN_FIELD)
4691  type = DecodeSpecial(0, lowunits, &val);
4692 
4693  if (type == UNITS)
4694  {
4695  if (interval2tm(*interval, tm, &fsec) == 0)
4696  {
4697  switch (val)
4698  {
4699  case DTK_MICROSEC:
4700  result = tm->tm_sec * 1000000.0 + fsec;
4701  break;
4702 
4703  case DTK_MILLISEC:
4704  result = tm->tm_sec * 1000.0 + fsec / 1000.0;
4705  break;
4706 
4707  case DTK_SECOND:
4708  result = tm->tm_sec + fsec / 1000000.0;
4709  break;
4710 
4711  case DTK_MINUTE:
4712  result = tm->tm_min;
4713  break;
4714 
4715  case DTK_HOUR:
4716  result = tm->tm_hour;
4717  break;
4718 
4719  case DTK_DAY:
4720  result = tm->tm_mday;
4721  break;
4722 
4723  case DTK_MONTH:
4724  result = tm->tm_mon;
4725  break;
4726 
4727  case DTK_QUARTER:
4728  result = (tm->tm_mon / 3) + 1;
4729  break;
4730 
4731  case DTK_YEAR:
4732  result = tm->tm_year;
4733  break;
4734 
4735  case DTK_DECADE:
4736  /* caution: C division may have negative remainder */
4737  result = tm->tm_year / 10;
4738  break;
4739 
4740  case DTK_CENTURY:
4741  /* caution: C division may have negative remainder */
4742  result = tm->tm_year / 100;
4743  break;
4744 
4745  case DTK_MILLENNIUM:
4746  /* caution: C division may have negative remainder */
4747  result = tm->tm_year / 1000;
4748  break;
4749 
4750  default:
4751  ereport(ERROR,
4752  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4753  errmsg("interval units \"%s\" not supported",
4754  lowunits)));
4755  result = 0;
4756  }
4757 
4758  }
4759  else
4760  {
4761  elog(ERROR, "could not convert interval to tm");
4762  result = 0;
4763  }
4764  }
4765  else if (type == RESERV && val == DTK_EPOCH)
4766  {
4767  result = interval->time / 1000000.0;
4768  result += ((double) DAYS_PER_YEAR * SECS_PER_DAY) * (interval->month / MONTHS_PER_YEAR);
4769  result += ((double) DAYS_PER_MONTH * SECS_PER_DAY) * (interval->month % MONTHS_PER_YEAR);
4770  result += ((double) SECS_PER_DAY) * interval->day;
4771  }
4772  else
4773  {
4774  ereport(ERROR,
4775  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4776  errmsg("interval units \"%s\" not recognized",
4777  lowunits)));
4778  result = 0;
4779  }
4780 
4781  PG_RETURN_FLOAT8(result);
4782 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define DTK_YEAR
Definition: datetime.h:170
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:169
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
#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:111
#define DTK_MONTH
Definition: datetime.h:168
#define DTK_MILLISEC
Definition: datetime.h:174
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define DTK_DECADE
Definition: datetime.h:171
#define DAYS_PER_YEAR
Definition: timestamp.h:68
#define DTK_HOUR
Definition: datetime.h:165
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
#define SECS_PER_DAY
Definition: timestamp.h:86
#define DTK_SECOND
Definition: datetime.h:163
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c: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:439
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:689
Datum interval_pl ( PG_FUNCTION_ARGS  )

Definition at line 3040 of file timestamp.c.

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

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

3041 {
3042  Interval *span1 = PG_GETARG_INTERVAL_P(0);
3043  Interval *span2 = PG_GETARG_INTERVAL_P(1);
3044  Interval *result;
3045 
3046  result = (Interval *) palloc(sizeof(Interval));
3047 
3048  result->month = span1->month + span2->month;
3049  /* overflow check copied from int4pl */
3050  if (SAMESIGN(span1->month, span2->month) &&
3051  !SAMESIGN(result->month, span1->month))
3052  ereport(ERROR,
3053  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3054  errmsg("interval out of range")));
3055 
3056  result->day = span1->day + span2->day;
3057  if (SAMESIGN(span1->day, span2->day) &&
3058  !SAMESIGN(result->day, span1->day))
3059  ereport(ERROR,
3060  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3061  errmsg("interval out of range")));
3062 
3063  result->time = span1->time + span2->time;
3064  if (SAMESIGN(span1->time, span2->time) &&
3065  !SAMESIGN(result->time, span1->time))
3066  ereport(ERROR,
3067  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3068  errmsg("interval out of range")));
3069 
3070  PG_RETURN_INTERVAL_P(result);
3071 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
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:849
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:256
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:66
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:849
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:486
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1301 of file timestamp.c.

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

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
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod)
Definition: timestamp.c:1320
void * palloc(Size size)
Definition: mcxt.c:849
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_sendint(), pq_sendint64(), and Interval::time.

1006 {
1009 
1010  pq_begintypsend(&buf);
1011  pq_sendint64(&buf, interval->time);
1012  pq_sendint(&buf, interval->day, sizeof(interval->day));
1013  pq_sendint(&buf, interval->month, sizeof(interval->month));
1015 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
int32 day
Definition: timestamp.h:47
static char * buf
Definition: pg_test_fsync.c:66
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_sendint64(StringInfo buf, int64 i)
Definition: pqformat.c:271
Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3011 of file timestamp.c.

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

3012 {
3013  Interval *interval1 = PG_GETARG_INTERVAL_P(0);
3014  Interval *interval2 = PG_GETARG_INTERVAL_P(1);
3015  Interval *result;
3016 
3017  /* use interval_cmp_internal to be sure this agrees with comparisons */
3018  if (interval_cmp_internal(interval1, interval2) < 0)
3019  result = interval1;
3020  else
3021  result = interval2;
3022  PG_RETURN_INTERVAL_P(result);
3023 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
return result
Definition: formatting.c:1632
static int interval_cmp_internal(Interval *interval1, Interval *interval2)
Definition: timestamp.c:2326
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, NULL, 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:560
List * args
Definition: primnodes.h:456
#define DatumGetInt32(X)
Definition: postgres.h:478
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:509
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define lsecond(l)
Definition: pg_list.h:116
signed int int32
Definition: c.h:256
#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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
static int intervaltypmodleastfield(int32 typmod)
Definition: timestamp.c:1195
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:588
Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 3926 of file timestamp.c.

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

3927 {
3928  text *units = PG_GETARG_TEXT_PP(0);
3930  Interval *result;
3931  int type,
3932  val;
3933  char *lowunits;
3934  fsec_t fsec;
3935  struct pg_tm tt,
3936  *tm = &tt;
3937 
3938  result = (Interval *) palloc(sizeof(Interval));
3939 
3940  lowunits = downcase_truncate_identifier(VARDATA_ANY(units),
3941  VARSIZE_ANY_EXHDR(units),
3942  false);
3943 
3944  type = DecodeUnits(0, lowunits, &val);
3945 
3946  if (type == UNITS)
3947  {
3948  if (interval2tm(*interval, tm, &fsec) == 0)
3949  {
3950  switch (val)
3951  {
3952  /* fall through */
3953  case DTK_MILLENNIUM:
3954  /* caution: C division may have negative remainder */
3955  tm->tm_year = (tm->tm_year / 1000) * 1000;
3956  case DTK_CENTURY:
3957  /* caution: C division may have negative remainder */
3958  tm->tm_year = (tm->tm_year / 100) * 100;
3959  case DTK_DECADE:
3960  /* caution: C division may have negative remainder */
3961  tm->tm_year = (tm->tm_year / 10) * 10;
3962  case DTK_YEAR:
3963  tm->tm_mon = 0;
3964  case DTK_QUARTER:
3965  tm->tm_mon = 3 * (tm->tm_mon / 3);
3966  case DTK_MONTH:
3967  tm->tm_mday = 0;
3968  case DTK_DAY:
3969  tm->tm_hour = 0;
3970  case DTK_HOUR:
3971  tm->tm_min = 0;
3972  case DTK_MINUTE:
3973  tm->tm_sec = 0;
3974  case DTK_SECOND:
3975  fsec = 0;
3976  break;
3977  case DTK_MILLISEC:
3978  fsec = (fsec / 1000) * 1000;
3979  break;
3980  case DTK_MICROSEC:
3981  break;
3982 
3983  default:
3984  if (val == DTK_WEEK)
3985  ereport(ERROR,
3986  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3987  errmsg("interval units \"%s\" not supported "
3988  "because months usually have fractional weeks",
3989  lowunits)));
3990  else
3991  ereport(ERROR,
3992  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3993  errmsg("interval units \"%s\" not supported",
3994  lowunits)));
3995  }
3996 
3997  if (tm2interval(tm, fsec, result) != 0)
3998  ereport(ERROR,
3999  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4000  errmsg("interval out of range")));
4001  }
4002  else
4003  elog(ERROR, "could not convert interval to tm");
4004  }
4005  else
4006  {
4007  ereport(ERROR,
4008  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4009  errmsg("interval units \"%s\" not recognized",
4010  lowunits)));
4011  }
4012 
4013  PG_RETURN_INTERVAL_P(result);
4014 }
#define DTK_CENTURY
Definition: datetime.h:172
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define UNITS
Definition: datetime.h:108
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
#define DTK_WEEK
Definition: datetime.h:167
#define DTK_YEAR
Definition: datetime.h:170
int tm_hour
Definition: pgtime.h:29
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:131
#define DTK_QUARTER
Definition: datetime.h:169
#define DTK_MILLENNIUM
Definition: datetime.h:173
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
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:111
#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:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#define elog
Definition: elog.h:219
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:689
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1930
Datum interval_um ( PG_FUNCTION_ARGS  )

Definition at line 2982 of file timestamp.c.

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

Referenced by abs_interval().

2983 {
2985  Interval *result;
2986 
2987  result = (Interval *) palloc(sizeof(Interval));
2988 
2989  result->time = -interval->time;
2990  /* overflow check copied from int4um */
2991  if (interval->time != 0 && SAMESIGN(result->time, interval->time))
2992  ereport(ERROR,
2993  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2994  errmsg("interval out of range")));
2995  result->day = -interval->day;
2996  if (interval->day != 0 && SAMESIGN(result->day, interval->day))
2997  ereport(ERROR,
2998  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2999  errmsg("interval out of range")));
3000  result->month = -interval->month;
3001  if (interval->month != 0 && SAMESIGN(result->month, interval->month))
3002  ereport(ERROR,
3003  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3004  errmsg("interval out of range")));
3005 
3006  PG_RETURN_INTERVAL_P(result);
3007 }
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:37
#define PG_RETURN_INTERVAL_P(x)
Definition: timestamp.h:41
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1632
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:849
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:256
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:54
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define INTERVAL_TYPMOD(p, r)
Definition: timestamp.h:52
#define MINUTE
Definition: datetime.h:102
#define ereport(elevel, rest)
Definition: elog.h:122
#define MONTH
Definition: datetime.h:92
#define WARNING
Definition: elog.h:40
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HOUR
Definition: datetime.h:101
static int intervaltypmodleastfield ( int32  typmod)
static

Definition at line 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:256
#define ERROR
Definition: elog.h:43
#define INTERVAL_FULL_PRECISION
Definition: timestamp.h:50
#define MINUTE
Definition: datetime.h:102
#define MONTH
Definition: datetime.h:92
#define INTERVAL_PRECISION(t)
Definition: timestamp.h:53
#define INTERVAL_RANGE(t)
Definition: timestamp.h:54
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
void * palloc(Size size)
Definition: mcxt.c:849
#define elog
Definition: elog.h:219
#define HOUR
Definition: datetime.h:101
void isoweek2date ( int  woy,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4042 of file timestamp.c.

References isoweek2j(), and j2date().

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

4043 {
4044  j2date(isoweek2j(*year, woy), year, mon, mday);
4045 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:4022
int isoweek2j ( int  year,
int  week 
)

Definition at line 4022 of file timestamp.c.

References date2j(), and j2day().

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

4023 {
4024  int day0,
4025  day4;
4026 
4027  /* fourth day of current year */
4028  day4 = date2j(year, 1, 4);
4029 
4030  /* day0 == offset to first day of week (Monday) */
4031  day0 = j2day(day4 - 1);
4032 
4033  return ((week - 1) * 7) + (day4 - day0);
4034 }
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 4055 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4056 {
4057  int jday;
4058 
4059  jday = isoweek2j(*year, isoweek);
4060  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4061  if (wday > 1)
4062  jday += wday - 2;
4063  else
4064  jday += 6;
4065  j2date(jday, year, mon, mday);
4066 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:4022
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, result, 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
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
const char *const months[]
Definition: datetime.c:66
#define ERROR
Definition: elog.h:43
int isinf(double x)
#define SECS_PER_MINUTE
Definition: timestamp.h:88
TimeOffset time
Definition: timestamp.h:45
const char *const days[]
Definition: datetime.c:69
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define SECS_PER_HOUR
Definition: timestamp.h:87
int32 month
Definition: timestamp.h:48
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 630 of file timestamp.c.

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

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);
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
return result
Definition: formatting.c:1632
signed int int32
Definition: c.h:256
double float8
Definition: c.h:381
int64 Timestamp
Definition: timestamp.h:38
#define PG_RETURN_TIMESTAMP(x)
Definition: timestamp.h:39
static Timestamp make_timestamp_internal ( int  year,
int  month,
int  day,
int  hour,
int  min,
double  sec 
)
static

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

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

544 {
545  struct pg_tm tm;
547  TimeOffset time;
548  int dterr;
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
return result
Definition: formatting.c:1632
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:111
#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, result, 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);
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:4947
#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
return result
Definition: formatting.c:1632
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:256
double float8
Definition: c.h:381
int64 Timestamp
Definition: timestamp.h:38
Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 671 of file timestamp.c.

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

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);
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
return result
Definition: formatting.c:1632
Definition: pgtime.h:25
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:40
signed int int32
Definition: c.h:256
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
int64 Timestamp
Definition: timestamp.h:38
#define NULL
Definition: c.h:229
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
Definition: zic.c:90
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c: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 3191 of file timestamp.c.

References DirectFunctionCall2, interval_mul(), and PG_GETARG_DATUM.

3192 {
3193  /* Args are float8 and Interval *, but leave them as generic Datum */
3194  Datum factor = PG_GETARG_DATUM(0);
3195  Datum span = PG_GETARG_DATUM(1);
3196 
3197  return DirectFunctionCall2(interval_mul, span, factor);
3198 }
#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:3114
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
static float8 NonFiniteTimestampTzPart ( int  type,
int  unit,
char *  lowunits,
bool  isNegative,
bool  isTz 
)
static

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

4203 {
4204  if ((type != UNITS) && (type != RESERV))
4205  {
4206  if (isTz)
4207  ereport(ERROR,
4208  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4209  errmsg("timestamp with time zone units \"%s\" not recognized",
4210  lowunits)));
4211  else
4212  ereport(ERROR,
4213  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4214  errmsg("timestamp units \"%s\" not recognized",
4215  lowunits)));
4216  }
4217 
4218  switch (unit)
4219  {
4220  /* Oscillating units */
4221  case DTK_MICROSEC:
4222  case DTK_MILLISEC:
4223  case DTK_SECOND:
4224  case DTK_MINUTE:
4225  case DTK_HOUR:
4226  case DTK_DAY:
4227  case DTK_MONTH:
4228  case DTK_QUARTER:
4229  case DTK_WEEK:
4230  case DTK_DOW:
4231  case DTK_ISODOW:
4232  case DTK_DOY:
4233  case DTK_TZ:
4234  case DTK_TZ_MINUTE:
4235  case DTK_TZ_HOUR:
4236  return 0.0;
4237 
4238  /* Monotonically-increasing units */
4239  case DTK_YEAR:
4240  case DTK_DECADE:
4241  case DTK_CENTURY:
4242  case DTK_MILLENNIUM:
4243  case DTK_JULIAN:
4244  case DTK_ISOYEAR:
4245  case DTK_EPOCH:
4246  if (isNegative)
4247  return -get_float8_infinity();
4248  else
4249  return get_float8_infinity();
4250 
4251  default:
4252  if (isTz)
4253  ereport(ERROR,
4254  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4255  errmsg("timestamp with time zone units \"%s\" not supported",
4256  lowunits)));
4257  else
4258  ereport(ERROR,
4259  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4260  errmsg("timestamp units \"%s\" not supported",
4261  lowunits)));
4262  return 0.0; /* keep compiler quiet */
4263  }
4264 }
#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 2429 of file timestamp.c.

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

2430 {
2431  /*
2432  * The arguments are Timestamps, but we leave them as generic Datums to
2433  * avoid unnecessary conversions between value and reference forms --- not
2434  * to mention possible dereferences of null pointers.
2435  */
2436  Datum ts1 = PG_GETARG_DATUM(0);
2437  Datum te1 = PG_GETARG_DATUM(1);
2438  Datum ts2 = PG_GETARG_DATUM(2);
2439  Datum te2 = PG_GETARG_DATUM(3);
2440  bool ts1IsNull = PG_ARGISNULL(0);
2441  bool te1IsNull = PG_ARGISNULL(1);
2442  bool ts2IsNull = PG_ARGISNULL(2);
2443  bool te2IsNull = PG_ARGISNULL(3);
2444 
2445 #define TIMESTAMP_GT(t1,t2) \
2446  DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2447 #define TIMESTAMP_LT(t1,t2) \
2448  DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2449 
2450  /*
2451  * If both endpoints of interval 1 are null, the result is null (unknown).
2452  * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2453  * take ts1 as the lesser endpoint.
2454  */
2455  if (ts1IsNull)
2456  {
2457  if (te1IsNull)
2458  PG_RETURN_NULL();
2459  /* swap null for non-null */
2460  ts1 = te1;
2461  te1IsNull = true;
2462  }
2463  else if (!te1IsNull)
2464  {
2465  if (TIMESTAMP_GT(ts1, te1))
2466  {
2467  Datum tt = ts1;
2468 
2469  ts1 = te1;
2470  te1 = tt;
2471  }
2472  }
2473 
2474  /* Likewise for interval 2. */
2475  if (ts2IsNull)
2476  {
2477  if (te2IsNull)
2478  PG_RETURN_NULL();
2479  /* swap null for non-null */
2480  ts2 = te2;
2481  te2IsNull = true;
2482  }
2483  else if (!te2IsNull)
2484  {
2485  if (TIMESTAMP_GT(ts2, te2))
2486  {
2487  Datum tt = ts2;
2488 
2489  ts2 = te2;
2490  te2 = tt;
2491  }
2492  }
2493 
2494  /*
2495  * At this point neither ts1 nor ts2 is null, so we can consider three
2496  * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2497  */
2498  if (TIMESTAMP_GT(ts1, ts2))
2499  {
2500  /*
2501  * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2502  * in the presence of nulls it's not quite completely so.
2503  */
2504  if (te2IsNull)
2505  PG_RETURN_NULL();
2506  if (TIMESTAMP_LT(ts1, te2))
2507  PG_RETURN_BOOL(true);
2508  if (te1IsNull)
2509  PG_RETURN_NULL();
2510 
2511  /*
2512  * If te1 is not null then we had ts1 <= te1 above, and we just found
2513  * ts1 >= te2, hence te1 >= te2.
2514  */
2515  PG_RETURN_BOOL(false);
2516  }
2517  else if (TIMESTAMP_LT(ts1, ts2))
2518  {
2519  /* This case is ts2 < te1 OR te2 < te1 */
2520  if (te1IsNull)
2521  PG_RETURN_NULL();
2522  if (TIMESTAMP_LT(ts2, te1))
2523  PG_RETURN_BOOL(true);
2524  if (te2IsNull)
2525  PG_RETURN_NULL();
2526 
2527  /*
2528  * If te2 is not null then we had ts2 <= te2 above, and we just found
2529  * ts2 >= te1, hence te2 >= te1.
2530  */
2531  PG_RETURN_BOOL(false);
2532  }
2533  else
2534  {
2535  /*
2536  * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2537  * rather silly way of saying "true if both are non-null, else null".
2538  */
2539  if (te1IsNull || te2IsNull)
2540  PG_RETURN_NULL();
2541  PG_RETURN_BOOL(true);
2542  }
2543 
2544 #undef TIMESTAMP_GT
2545 #undef TIMESTAMP_LT
2546 }
#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(), NULL, 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:111
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1854
#define NULL
Definition: c.h:229
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:717
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, result, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

1671 {
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
return result
Definition: formatting.c:1632
static struct pg_tm tm
Definition: localtime.c:111
#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 4947 of file timestamp.c.

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

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

4948 {
4950  struct pg_tm tt,
4951  *tm = &tt;
4952  fsec_t fsec;
4953  int tz;
4954 
4956  result = timestamp;
4957  else
4958  {
4959  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4960  ereport(ERROR,
4961  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4962  errmsg("timestamp out of range")));
4963 
4965 
4966  if (tm2timestamp(tm, fsec, &tz, &result) != 0)
4967  ereport(ERROR,
4968  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4969  errmsg("timestamp out of range")));
4970  }
4971 
4972  return result;
4973 }
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
return result
Definition: formatting.c:1632
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:111
#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
#define NULL
Definition: c.h:229
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(), INT64CONST, j2date(), NULL, pg_localtime(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, session_timezone, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, pg_tm::tm_zone, TMODULO, UNIX_EPOCH_JDATE, USECS_PER_DAY, and USECS_PER_SEC.

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

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
#define NULL
Definition: c.h:229
#define INT64CONST(x)
Definition: c.h:310
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c: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 3406 of file timestamp.c.

References day_tab, ereport, errcode(), errmsg(), ERROR, HOURS_PER_DAY, isleap, MINS_PER_HOUR, MONTHS_PER_YEAR, NULL, palloc(), PG_GETARG_TIMESTAMP, PG_RETURN_INTERVAL_P, result, SECS_PER_MINUTE, timestamp2tm(), tm, tm2interval(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, and USECS_PER_SEC.

3407 {
3408  Timestamp dt1 = PG_GETARG_TIMESTAMP(0);
3409  Timestamp dt2 = PG_GETARG_TIMESTAMP(1);
3410  Interval *result;
3411  fsec_t fsec,
3412  fsec1,
3413  fsec2;
3414  struct pg_tm tt,
3415  *tm = &tt;
3416  struct pg_tm tt1,
3417  *tm1 = &tt1;
3418  struct pg_tm tt2,
3419  *tm2 = &tt2;
3420 
3421  result = (Interval *) palloc(sizeof(Interval));
3422 
3423  if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
3424  timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
3425  {
3426  /* form the symbolic difference */
3427  fsec = fsec1 - fsec2;
3428  tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
3429  tm->tm_min = tm1->tm_min - tm2->tm_min;
3430  tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
3431  tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
3432  tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
3433  tm->tm_year = tm1->tm_year - tm2->tm_year;
3434 
3435  /* flip sign if necessary... */
3436  if (dt1 < dt2)
3437  {
3438  fsec = -fsec;
3439  tm->tm_sec = -tm->tm_sec;
3440  tm->tm_min = -tm->tm_min;
3441  tm->tm_hour = -tm->