PostgreSQL Source Code git master
Loading...
Searching...
No Matches
timestamp.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <sys/time.h>
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/int.h"
#include "common/int128.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/optimizer.h"
#include "parser/scansup.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/float.h"
#include "utils/numeric.h"
#include "utils/skipsupport.h"
#include "utils/sortsupport.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
 
struct  IntervalAggState
 

Macros

#define IA_TOTAL_COUNT(ia)    ((ia)->N + (ia)->pInfcount + (ia)->nInfcount)
 
#define TIMESTAMP_GT(t1, t2)    DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
 
#define TIMESTAMP_LT(t1, t2)    DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
 
#define INTERVAL_TO_MICROSECONDS(i)   ((((double) (i)->month * DAYS_PER_MONTH + (i)->day)) * USECS_PER_DAY + (i)->time)
 

Typedefs

typedef struct IntervalAggState IntervalAggState
 

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 bool AdjustIntervalForTypmod (Interval *interval, int32 typmod, Node *escontext)
 
static TimestampTz timestamp2timestamptz (Timestamp timestamp)
 
static Timestamp timestamptz2timestamp (TimestampTz timestamp)
 
static void EncodeSpecialInterval (const Interval *interval, char *str)
 
static void interval_um_internal (const Interval *interval, Interval *result)
 
static int32 anytimestamp_typmodin (bool istz, ArrayType *ta)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
static charanytimestamp_typmodout (bool istz, int32 typmod)
 
Datum timestamp_in (PG_FUNCTION_ARGS)
 
Datum timestamp_out (PG_FUNCTION_ARGS)
 
Datum timestamp_recv (PG_FUNCTION_ARGS)
 
Datum timestamp_send (PG_FUNCTION_ARGS)
 
Datum timestamptypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptypmodout (PG_FUNCTION_ARGS)
 
Datum timestamp_support (PG_FUNCTION_ARGS)
 
Datum timestamp_scale (PG_FUNCTION_ARGS)
 
bool AdjustTimestampForTypmod (Timestamp *time, int32 typmod, Node *escontext)
 
Datum timestamptz_in (PG_FUNCTION_ARGS)
 
static int parse_sane_timezone (struct pg_tm *tm, text *zone)
 
static pg_tzlookup_timezone (text *zone)
 
static Timestamp make_timestamp_internal (int year, int month, int day, int hour, int min, double sec)
 
Datum make_timestamp (PG_FUNCTION_ARGS)
 
Datum make_timestamptz (PG_FUNCTION_ARGS)
 
Datum make_timestamptz_at_timezone (PG_FUNCTION_ARGS)
 
Datum float8_timestamptz (PG_FUNCTION_ARGS)
 
Datum timestamptz_out (PG_FUNCTION_ARGS)
 
Datum timestamptz_recv (PG_FUNCTION_ARGS)
 
Datum timestamptz_send (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodin (PG_FUNCTION_ARGS)
 
Datum timestamptztypmodout (PG_FUNCTION_ARGS)
 
Datum timestamptz_scale (PG_FUNCTION_ARGS)
 
Datum interval_in (PG_FUNCTION_ARGS)
 
Datum interval_out (PG_FUNCTION_ARGS)
 
Datum interval_recv (PG_FUNCTION_ARGS)
 
Datum interval_send (PG_FUNCTION_ARGS)
 
Datum intervaltypmodin (PG_FUNCTION_ARGS)
 
Datum intervaltypmodout (PG_FUNCTION_ARGS)
 
static int intervaltypmodleastfield (int32 typmod)
 
Datum interval_support (PG_FUNCTION_ARGS)
 
Datum interval_scale (PG_FUNCTION_ARGS)
 
Datum make_interval (PG_FUNCTION_ARGS)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
Datum now (PG_FUNCTION_ARGS)
 
Datum statement_timestamp (PG_FUNCTION_ARGS)
 
Datum clock_timestamp (PG_FUNCTION_ARGS)
 
Datum pg_postmaster_start_time (PG_FUNCTION_ARGS)
 
Datum pg_conf_load_time (PG_FUNCTION_ARGS)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
Datum timeofday (PG_FUNCTION_ARGS)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
long TimestampDifferenceMilliseconds (TimestampTz start_time, TimestampTz stop_time)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
bool TimestampDifferenceExceedsSeconds (TimestampTz start_time, TimestampTz stop_time, int threshold_sec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const chartimestamptz_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)
 
void interval2itm (Interval span, struct pg_itm *itm)
 
int itm2interval (struct pg_itm *itm, Interval *span)
 
int itmin2interval (struct pg_itm_in *itm_in, 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)
 
Datum timestamp_sortsupport (PG_FUNCTION_ARGS)
 
static Datum timestamp_decrement (Relation rel, Datum existing, bool *underflow)
 
static Datum timestamp_increment (Relation rel, Datum existing, bool *overflow)
 
Datum timestamp_skipsupport (PG_FUNCTION_ARGS)
 
Datum timestamp_hash (PG_FUNCTION_ARGS)
 
Datum timestamp_hash_extended (PG_FUNCTION_ARGS)
 
Datum timestamptz_hash (PG_FUNCTION_ARGS)
 
Datum timestamptz_hash_extended (PG_FUNCTION_ARGS)
 
int32 timestamp_cmp_timestamptz_internal (Timestamp timestampVal, TimestampTz dt2)
 
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 (const Interval *interval1, const Interval *interval2)
 
static int interval_sign (const Interval *interval)
 
Datum interval_eq (PG_FUNCTION_ARGS)
 
Datum interval_ne (PG_FUNCTION_ARGS)
 
Datum interval_lt (PG_FUNCTION_ARGS)
 
Datum interval_gt (PG_FUNCTION_ARGS)
 
Datum interval_le (PG_FUNCTION_ARGS)
 
Datum interval_ge (PG_FUNCTION_ARGS)
 
Datum interval_cmp (PG_FUNCTION_ARGS)
 
Datum interval_hash (PG_FUNCTION_ARGS)
 
Datum interval_hash_extended (PG_FUNCTION_ARGS)
 
Datum overlaps_timestamp (PG_FUNCTION_ARGS)
 
Datum timestamp_smaller (PG_FUNCTION_ARGS)
 
Datum timestamp_larger (PG_FUNCTION_ARGS)
 
Datum timestamp_mi (PG_FUNCTION_ARGS)
 
Datum interval_justify_interval (PG_FUNCTION_ARGS)
 
Datum interval_justify_hours (PG_FUNCTION_ARGS)
 
Datum interval_justify_days (PG_FUNCTION_ARGS)
 
Datum timestamp_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_mi_interval (PG_FUNCTION_ARGS)
 
static TimestampTz timestamptz_pl_interval_internal (TimestampTz timestamp, Interval *span, pg_tz *attimezone)
 
static TimestampTz timestamptz_mi_interval_internal (TimestampTz timestamp, Interval *span, pg_tz *attimezone)
 
Datum timestamptz_pl_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval (PG_FUNCTION_ARGS)
 
Datum timestamptz_pl_interval_at_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_mi_interval_at_zone (PG_FUNCTION_ARGS)
 
Datum interval_um (PG_FUNCTION_ARGS)
 
Datum interval_smaller (PG_FUNCTION_ARGS)
 
Datum interval_larger (PG_FUNCTION_ARGS)
 
static void finite_interval_pl (const Interval *span1, const Interval *span2, Interval *result)
 
Datum interval_pl (PG_FUNCTION_ARGS)
 
static void finite_interval_mi (const Interval *span1, const Interval *span2, Interval *result)
 
Datum interval_mi (PG_FUNCTION_ARGS)
 
Datum interval_mul (PG_FUNCTION_ARGS)
 
Datum mul_d_interval (PG_FUNCTION_ARGS)
 
Datum interval_div (PG_FUNCTION_ARGS)
 
Datum in_range_timestamptz_interval (PG_FUNCTION_ARGS)
 
Datum in_range_timestamp_interval (PG_FUNCTION_ARGS)
 
Datum in_range_interval_interval (PG_FUNCTION_ARGS)
 
static IntervalAggStatemakeIntervalAggState (FunctionCallInfo fcinfo)
 
static void do_interval_accum (IntervalAggState *state, Interval *newval)
 
static void do_interval_discard (IntervalAggState *state, Interval *newval)
 
Datum interval_avg_accum (PG_FUNCTION_ARGS)
 
Datum interval_avg_combine (PG_FUNCTION_ARGS)
 
Datum interval_avg_serialize (PG_FUNCTION_ARGS)
 
Datum interval_avg_deserialize (PG_FUNCTION_ARGS)
 
Datum interval_avg_accum_inv (PG_FUNCTION_ARGS)
 
Datum interval_avg (PG_FUNCTION_ARGS)
 
Datum interval_sum (PG_FUNCTION_ARGS)
 
Datum timestamp_age (PG_FUNCTION_ARGS)
 
Datum timestamptz_age (PG_FUNCTION_ARGS)
 
Datum timestamp_bin (PG_FUNCTION_ARGS)
 
Datum timestamp_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_bin (PG_FUNCTION_ARGS)
 
static TimestampTz timestamptz_trunc_internal (text *units, TimestampTz timestamp, pg_tz *tzp)
 
Datum timestamptz_trunc (PG_FUNCTION_ARGS)
 
Datum timestamptz_trunc_zone (PG_FUNCTION_ARGS)
 
Datum interval_trunc (PG_FUNCTION_ARGS)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
static float8 NonFiniteTimestampTzPart (int type, int unit, char *lowunits, bool isNegative, bool isTz)
 
static Datum timestamp_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamp_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamp (PG_FUNCTION_ARGS)
 
static Datum timestamptz_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum timestamptz_part (PG_FUNCTION_ARGS)
 
Datum extract_timestamptz (PG_FUNCTION_ARGS)
 
static float8 NonFiniteIntervalPart (int type, int unit, char *lowunits, bool isNegative)
 
static Datum interval_part_common (PG_FUNCTION_ARGS, bool retnumeric)
 
Datum interval_part (PG_FUNCTION_ARGS)
 
Datum extract_interval (PG_FUNCTION_ARGS)
 
Datum timestamp_zone (PG_FUNCTION_ARGS)
 
Datum timestamp_izone (PG_FUNCTION_ARGS)
 
bool TimestampTimestampTzRequiresRewrite (void)
 
Datum timestamp_timestamptz (PG_FUNCTION_ARGS)
 
TimestampTz timestamp2timestamptz_safe (Timestamp timestamp, Node *escontext)
 
Datum timestamptz_timestamp (PG_FUNCTION_ARGS)
 
Timestamp timestamptz2timestamp_safe (TimestampTz timestamp, Node *escontext)
 
Datum timestamptz_zone (PG_FUNCTION_ARGS)
 
Datum timestamptz_izone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp (PG_FUNCTION_ARGS)
 
static Datum generate_series_timestamptz_internal (FunctionCallInfo fcinfo)
 
Datum generate_series_timestamptz (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamptz_at_zone (PG_FUNCTION_ARGS)
 
Datum generate_series_timestamp_support (PG_FUNCTION_ARGS)
 
Datum timestamp_at_local (PG_FUNCTION_ARGS)
 
Datum timestamptz_at_local (PG_FUNCTION_ARGS)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ IA_TOTAL_COUNT

#define IA_TOTAL_COUNT (   ia)     ((ia)->N + (ia)->pInfcount + (ia)->nInfcount)

Definition at line 80 of file timestamp.c.

◆ INTERVAL_TO_MICROSECONDS

#define INTERVAL_TO_MICROSECONDS (   i)    ((((double) (i)->month * DAYS_PER_MONTH + (i)->day)) * USECS_PER_DAY + (i)->time)

◆ TIMESTAMP_GT

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

◆ TIMESTAMP_LT

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

Typedef Documentation

◆ IntervalAggState

Function Documentation

◆ AdjustIntervalForTypmod()

static bool AdjustIntervalForTypmod ( Interval interval,
int32  typmod,
Node escontext 
)
static

Definition at line 1353 of file timestamp.c.

1355{
1356 static const int64 IntervalScales[MAX_INTERVAL_PRECISION + 1] = {
1357 INT64CONST(1000000),
1358 INT64CONST(100000),
1359 INT64CONST(10000),
1360 INT64CONST(1000),
1361 INT64CONST(100),
1362 INT64CONST(10),
1363 INT64CONST(1)
1364 };
1365
1366 static const int64 IntervalOffsets[MAX_INTERVAL_PRECISION + 1] = {
1367 INT64CONST(500000),
1368 INT64CONST(50000),
1369 INT64CONST(5000),
1370 INT64CONST(500),
1371 INT64CONST(50),
1372 INT64CONST(5),
1373 INT64CONST(0)
1374 };
1375
1376 /* Typmod has no effect on infinite intervals */
1378 return true;
1379
1380 /*
1381 * Unspecified range and precision? Then not necessary to adjust. Setting
1382 * typmod to -1 is the convention for all data types.
1383 */
1384 if (typmod >= 0)
1385 {
1386 int range = INTERVAL_RANGE(typmod);
1387 int precision = INTERVAL_PRECISION(typmod);
1388
1389 /*
1390 * Our interpretation of intervals with a limited set of fields is
1391 * that fields to the right of the last one specified are zeroed out,
1392 * but those to the left of it remain valid. Thus for example there
1393 * is no operational difference between INTERVAL YEAR TO MONTH and
1394 * INTERVAL MONTH. In some cases we could meaningfully enforce that
1395 * higher-order fields are zero; for example INTERVAL DAY could reject
1396 * nonzero "month" field. However that seems a bit pointless when we
1397 * can't do it consistently. (We cannot enforce a range limit on the
1398 * highest expected field, since we do not have any equivalent of
1399 * SQL's <interval leading field precision>.) If we ever decide to
1400 * revisit this, interval_support will likely require adjusting.
1401 *
1402 * Note: before PG 8.4 we interpreted a limited set of fields as
1403 * actually causing a "modulo" operation on a given value, potentially
1404 * losing high-order as well as low-order information. But there is
1405 * no support for such behavior in the standard, and it seems fairly
1406 * undesirable on data consistency grounds anyway. Now we only
1407 * perform truncation or rounding of low-order fields.
1408 */
1410 {
1411 /* Do nothing... */
1412 }
1413 else if (range == INTERVAL_MASK(YEAR))
1414 {
1416 interval->day = 0;
1417 interval->time = 0;
1418 }
1419 else if (range == INTERVAL_MASK(MONTH))
1420 {
1421 interval->day = 0;
1422 interval->time = 0;
1423 }
1424 /* YEAR TO MONTH */
1425 else if (range == (INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH)))
1426 {
1427 interval->day = 0;
1428 interval->time = 0;
1429 }
1430 else if (range == INTERVAL_MASK(DAY))
1431 {
1432 interval->time = 0;
1433 }
1434 else if (range == INTERVAL_MASK(HOUR))
1435 {
1438 }
1439 else if (range == INTERVAL_MASK(MINUTE))
1440 {
1443 }
1444 else if (range == INTERVAL_MASK(SECOND))
1445 {
1446 /* fractional-second rounding will be dealt with below */
1447 }
1448 /* DAY TO HOUR */
1449 else if (range == (INTERVAL_MASK(DAY) |
1451 {
1454 }
1455 /* DAY TO MINUTE */
1456 else if (range == (INTERVAL_MASK(DAY) |
1459 {
1462 }
1463 /* DAY TO SECOND */
1464 else if (range == (INTERVAL_MASK(DAY) |
1468 {
1469 /* fractional-second rounding will be dealt with below */
1470 }
1471 /* HOUR TO MINUTE */
1472 else if (range == (INTERVAL_MASK(HOUR) |
1474 {
1477 }
1478 /* HOUR TO SECOND */
1479 else if (range == (INTERVAL_MASK(HOUR) |
1482 {
1483 /* fractional-second rounding will be dealt with below */
1484 }
1485 /* MINUTE TO SECOND */
1486 else if (range == (INTERVAL_MASK(MINUTE) |
1488 {
1489 /* fractional-second rounding will be dealt with below */
1490 }
1491 else
1492 elog(ERROR, "unrecognized interval typmod: %d", typmod);
1493
1494 /* Need to adjust sub-second precision? */
1495 if (precision != INTERVAL_FULL_PRECISION)
1496 {
1498 ereturn(escontext, false,
1500 errmsg("interval(%d) precision must be between %d and %d",
1501 precision, 0, MAX_INTERVAL_PRECISION)));
1502
1503 if (interval->time >= INT64CONST(0))
1504 {
1506 IntervalOffsets[precision],
1507 &interval->time))
1508 ereturn(escontext, false,
1510 errmsg("interval out of range")));
1511 interval->time -= interval->time % IntervalScales[precision];
1512 }
1513 else
1514 {
1516 IntervalOffsets[precision],
1517 &interval->time))
1518 ereturn(escontext, false,
1520 errmsg("interval out of range")));
1521 interval->time -= interval->time % IntervalScales[precision];
1522 }
1523 }
1524 }
1525
1526 return true;
1527}
#define INT64CONST(x)
Definition c.h:630
int64_t int64
Definition c.h:621
#define INTERVAL_NOT_FINITE(i)
Definition timestamp.h:195
#define USECS_PER_HOUR
Definition timestamp.h:132
#define MONTHS_PER_YEAR
Definition timestamp.h:108
#define MAX_INTERVAL_PRECISION
Definition timestamp.h:93
#define USECS_PER_MINUTE
Definition timestamp.h:133
int errcode(int sqlerrcode)
Definition elog.c:875
#define ereturn(context, dummy_value,...)
Definition elog.h:280
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define MONTH
Definition datetime.h:91
#define HOUR
Definition datetime.h:100
#define DAY
Definition datetime.h:93
#define YEAR
Definition datetime.h:92
#define SECOND
Definition datetime.h:102
#define MINUTE
Definition datetime.h:101
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:262
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235
static char * errmsg
static int fb(int x)
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
#define INTERVAL_FULL_RANGE
Definition timestamp.h:76
#define INTERVAL_PRECISION(t)
Definition timestamp.h:81
#define INTERVAL_RANGE(t)
Definition timestamp.h:82
#define INTERVAL_MASK(b)
Definition timestamp.h:73
#define INTERVAL_FULL_PRECISION
Definition timestamp.h:78

References DAY, elog, ereturn, errcode(), errmsg, ERROR, fb(), HOUR, INT64CONST, INTERVAL_FULL_PRECISION, INTERVAL_FULL_RANGE, INTERVAL_MASK, INTERVAL_NOT_FINITE, INTERVAL_PRECISION, INTERVAL_RANGE, MAX_INTERVAL_PRECISION, MINUTE, MONTH, interval::month, MONTHS_PER_YEAR, pg_add_s64_overflow(), pg_sub_s64_overflow(), range(), SECOND, interval::time, USECS_PER_HOUR, USECS_PER_MINUTE, and YEAR.

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

◆ AdjustTimestampForTypmod()

bool AdjustTimestampForTypmod ( Timestamp time,
int32  typmod,
Node escontext 
)

Definition at line 363 of file timestamp.c.

364{
365 static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
366 INT64CONST(1000000),
367 INT64CONST(100000),
368 INT64CONST(10000),
369 INT64CONST(1000),
370 INT64CONST(100),
371 INT64CONST(10),
372 INT64CONST(1)
373 };
374
376 INT64CONST(500000),
377 INT64CONST(50000),
378 INT64CONST(5000),
379 INT64CONST(500),
380 INT64CONST(50),
381 INT64CONST(5),
382 INT64CONST(0)
383 };
384
385 if (!TIMESTAMP_NOT_FINITE(*time)
386 && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
387 {
389 ereturn(escontext, false,
391 errmsg("timestamp(%d) precision must be between %d and %d",
392 typmod, 0, MAX_TIMESTAMP_PRECISION)));
393
394 if (*time >= INT64CONST(0))
395 {
396 *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
397 TimestampScales[typmod];
398 }
399 else
400 {
401 *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
402 * TimestampScales[typmod]);
403 }
404 }
405
406 return true;
407}
#define MAX_TIMESTAMP_PRECISION
Definition timestamp.h:92
#define TIMESTAMP_NOT_FINITE(j)
Definition timestamp.h:169

References ereturn, errcode(), errmsg, fb(), INT64CONST, MAX_TIMESTAMP_PRECISION, and TIMESTAMP_NOT_FINITE.

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

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 116 of file timestamp.c.

117{
118 if (typmod < 0)
121 errmsg("TIMESTAMP(%d)%s precision must not be negative",
122 typmod, (istz ? " WITH TIME ZONE" : ""))));
123 if (typmod > MAX_TIMESTAMP_PRECISION)
124 {
127 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
128 typmod, (istz ? " WITH TIME ZONE" : ""),
131 }
132
133 return typmod;
134}
#define WARNING
Definition elog.h:37
#define ereport(elevel,...)
Definition elog.h:152

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

Referenced by anytimestamp_typmodin(), executeDateTimeMethod(), and transformSQLValueFunction().

◆ anytimestamp_typmodin()

static int32 anytimestamp_typmodin ( bool  istz,
ArrayType ta 
)
static

Definition at line 95 of file timestamp.c.

96{
97 int32 *tl;
98 int n;
99
101
102 /*
103 * we're not too tense about good error message here because grammar
104 * shouldn't allow wrong number of modifiers for TIMESTAMP
105 */
106 if (n != 1)
109 errmsg("invalid type modifier")));
110
112}
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition arrayutils.c:233
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition timestamp.c:116
int32_t int32
Definition c.h:620

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

Referenced by timestamptypmodin(), and timestamptztypmodin().

◆ anytimestamp_typmodout()

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

Definition at line 138 of file timestamp.c.

139{
140 const char *tz = istz ? " with time zone" : " without time zone";
141
142 if (typmod >= 0)
143 return psprintf("(%d)%s", (int) typmod, tz);
144 else
145 return pstrdup(tz);
146}
char * pstrdup(const char *in)
Definition mcxt.c:1910
char * psprintf(const char *fmt,...)
Definition psprintf.c:43

References fb(), psprintf(), and pstrdup().

Referenced by timestamptypmodout(), and timestamptztypmodout().

◆ clock_timestamp()

Datum clock_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1625 of file timestamp.c.

1626{
1628}
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1649
#define PG_RETURN_TIMESTAMPTZ(x)
Definition timestamp.h:68

References GetCurrentTimestamp(), and PG_RETURN_TIMESTAMPTZ.

◆ date2isoweek()

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

Definition at line 5326 of file timestamp.c.

5327{
5328 int day0,
5329 day4,
5330 dayn,
5331 week;
5332
5333 /* current day */
5334 dayn = date2j(year, mon, mday);
5335
5336 /* fourth day of current year */
5337 day4 = date2j(year, 1, 4);
5338
5339 /* day0 == offset to first day of week (Monday) */
5340 day0 = j2day(day4 - 1);
5341
5342 /*
5343 * We need the first week containing a Thursday, otherwise this day falls
5344 * into the previous year for purposes of counting weeks
5345 */
5346 if (dayn < day4 - day0)
5347 {
5348 day4 = date2j(year - 1, 1, 4);
5349
5350 /* day0 == offset to first day of week (Monday) */
5351 day0 = j2day(day4 - 1);
5352 }
5353
5354 week = (dayn - (day4 - day0)) / 7 + 1;
5355
5356 /*
5357 * Sometimes the last few days in a year will fall into the first week of
5358 * the next year, so check for this.
5359 */
5360 if (week >= 52)
5361 {
5362 day4 = date2j(year + 1, 1, 4);
5363
5364 /* day0 == offset to first day of week (Monday) */
5365 day0 = j2day(day4 - 1);
5366
5367 if (dayn >= day4 - day0)
5368 week = (dayn - (day4 - day0)) / 7 + 1;
5369 }
5370
5371 return week;
5372}
int j2day(int date)
Definition datetime.c:355
int date2j(int year, int month, int day)
Definition datetime.c:297

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

Referenced by DCH_to_char(), extract_date(), timestamp_part_common(), timestamp_trunc(), timestamptz_part_common(), and timestamptz_trunc_internal().

◆ date2isoyear()

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

Definition at line 5382 of file timestamp.c.

5383{
5384 int day0,
5385 day4,
5386 dayn,
5387 week;
5388
5389 /* current day */
5390 dayn = date2j(year, mon, mday);
5391
5392 /* fourth day of current year */
5393 day4 = date2j(year, 1, 4);
5394
5395 /* day0 == offset to first day of week (Monday) */
5396 day0 = j2day(day4 - 1);
5397
5398 /*
5399 * We need the first week containing a Thursday, otherwise this day falls
5400 * into the previous year for purposes of counting weeks
5401 */
5402 if (dayn < day4 - day0)
5403 {
5404 day4 = date2j(year - 1, 1, 4);
5405
5406 /* day0 == offset to first day of week (Monday) */
5407 day0 = j2day(day4 - 1);
5408
5409 year--;
5410 }
5411
5412 week = (dayn - (day4 - day0)) / 7 + 1;
5413
5414 /*
5415 * Sometimes the last few days in a year will fall into the first week of
5416 * the next year, so check for this.
5417 */
5418 if (week >= 52)
5419 {
5420 day4 = date2j(year + 1, 1, 4);
5421
5422 /* day0 == offset to first day of week (Monday) */
5423 day0 = j2day(day4 - 1);
5424
5425 if (dayn >= day4 - day0)
5426 year++;
5427 }
5428
5429 return year;
5430}

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

Referenced by date2isoyearday(), DCH_to_char(), extract_date(), timestamp_part_common(), and timestamptz_part_common().

◆ date2isoyearday()

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

Definition at line 5440 of file timestamp.c.

5441{
5442 return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
5443}
int isoweek2j(int year, int week)
Definition timestamp.c:5272
int date2isoyear(int year, int mon, int mday)
Definition timestamp.c:5382

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

Referenced by DCH_to_char().

◆ do_interval_accum()

static void do_interval_accum ( IntervalAggState state,
Interval newval 
)
static

Definition at line 4008 of file timestamp.c.

4009{
4010 /* Infinite inputs are counted separately, and do not affect "N" */
4012 {
4013 state->nInfcount++;
4014 return;
4015 }
4016
4018 {
4019 state->pInfcount++;
4020 return;
4021 }
4022
4023 finite_interval_pl(&state->sumX, newval, &state->sumX);
4024 state->N++;
4025}
static void finite_interval_pl(const Interval *span1, const Interval *span2, Interval *result)
Definition timestamp.c:3507
#define INTERVAL_IS_NOBEGIN(i)
Definition timestamp.h:182
#define INTERVAL_IS_NOEND(i)
Definition timestamp.h:192
#define newval

References finite_interval_pl(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, and newval.

Referenced by interval_avg_accum().

◆ do_interval_discard()

static void do_interval_discard ( IntervalAggState state,
Interval newval 
)
static

Definition at line 4031 of file timestamp.c.

4032{
4033 /* Infinite inputs are counted separately, and do not affect "N" */
4035 {
4036 state->nInfcount--;
4037 return;
4038 }
4039
4041 {
4042 state->pInfcount--;
4043 return;
4044 }
4045
4046 /* Handle the to-be-discarded finite value. */
4047 state->N--;
4048 if (state->N > 0)
4049 finite_interval_mi(&state->sumX, newval, &state->sumX);
4050 else
4051 {
4052 /* All values discarded, reset the state */
4053 Assert(state->N == 0);
4054 memset(&state->sumX, 0, sizeof(state->sumX));
4055 }
4056}
static void finite_interval_mi(const Interval *span1, const Interval *span2, Interval *result)
Definition timestamp.c:3563
#define Assert(condition)
Definition c.h:943

References Assert, fb(), finite_interval_mi(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, and newval.

Referenced by interval_avg_accum_inv().

◆ dt2local()

static Timestamp dt2local ( Timestamp  dt,
int  timezone 
)
static

◆ dt2time()

void dt2time ( Timestamp  jd,
int hour,
int min,
int sec,
fsec_t fsec 
)

Definition at line 1891 of file timestamp.c.

1892{
1893 TimeOffset time;
1894
1895 time = jd;
1896
1897 *hour = time / USECS_PER_HOUR;
1898 time -= (*hour) * USECS_PER_HOUR;
1899 *min = time / USECS_PER_MINUTE;
1900 time -= (*min) * USECS_PER_MINUTE;
1901 *sec = time / USECS_PER_SEC;
1902 *fsec = time - (*sec * USECS_PER_SEC);
1903} /* dt2time() */
int64 TimeOffset
Definition timestamp.h:40

References fb(), USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ EncodeSpecialInterval()

static void EncodeSpecialInterval ( const Interval interval,
char str 
)
static

Definition at line 1602 of file timestamp.c.

1603{
1605 strcpy(str, EARLY);
1606 else if (INTERVAL_IS_NOEND(interval))
1607 strcpy(str, LATE);
1608 else /* shouldn't happen */
1609 elog(ERROR, "invalid argument for EncodeSpecialInterval");
1610}
const char * str
#define EARLY
Definition datetime.h:39
#define LATE
Definition datetime.h:40

References EARLY, elog, ERROR, fb(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, LATE, and str.

Referenced by interval_out().

◆ EncodeSpecialTimestamp()

void EncodeSpecialTimestamp ( Timestamp  dt,
char str 
)

Definition at line 1591 of file timestamp.c.

1592{
1594 strcpy(str, EARLY);
1595 else if (TIMESTAMP_IS_NOEND(dt))
1596 strcpy(str, LATE);
1597 else /* shouldn't happen */
1598 elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1599}
#define TIMESTAMP_IS_NOEND(j)
Definition timestamp.h:167
#define TIMESTAMP_IS_NOBEGIN(j)
Definition timestamp.h:162

References EARLY, elog, ERROR, fb(), LATE, str, TIMESTAMP_IS_NOBEGIN, and TIMESTAMP_IS_NOEND.

Referenced by JsonEncodeDateTime(), pg_get_role_ddl_internal(), PGTYPEStimestamp_to_asc(), timestamp_out(), timestamptz_out(), and timestamptz_to_str().

◆ extract_interval()

Datum extract_interval ( PG_FUNCTION_ARGS  )

Definition at line 6329 of file timestamp.c.

6330{
6331 return interval_part_common(fcinfo, true);
6332}
static Datum interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition timestamp.c:6116

References interval_part_common().

◆ extract_timestamp()

Datum extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 5779 of file timestamp.c.

5780{
5781 return timestamp_part_common(fcinfo, true);
5782}
static Datum timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition timestamp.c:5515

References timestamp_part_common().

◆ extract_timestamptz()

Datum extract_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 6052 of file timestamp.c.

6053{
6054 return timestamptz_part_common(fcinfo, true);
6055}
static Datum timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
Definition timestamp.c:5789

References timestamptz_part_common().

◆ finite_interval_mi()

static void finite_interval_mi ( const Interval span1,
const Interval span2,
Interval result 
)
static

Definition at line 3563 of file timestamp.c.

3564{
3567
3568 if (pg_sub_s32_overflow(span1->month, span2->month, &result->month) ||
3569 pg_sub_s32_overflow(span1->day, span2->day, &result->day) ||
3570 pg_sub_s64_overflow(span1->time, span2->time, &result->time) ||
3572 ereport(ERROR,
3574 errmsg("interval out of range")));
3575}
uint32 result
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:169

References Assert, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, pg_sub_s32_overflow(), pg_sub_s64_overflow(), and result.

Referenced by do_interval_discard(), and interval_mi().

◆ finite_interval_pl()

static void finite_interval_pl ( const Interval span1,
const Interval span2,
Interval result 
)
static

Definition at line 3507 of file timestamp.c.

3508{
3511
3512 if (pg_add_s32_overflow(span1->month, span2->month, &result->month) ||
3513 pg_add_s32_overflow(span1->day, span2->day, &result->day) ||
3514 pg_add_s64_overflow(span1->time, span2->time, &result->time) ||
3516 ereport(ERROR,
3518 errmsg("interval out of range")));
3519}
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:151

References Assert, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, pg_add_s32_overflow(), pg_add_s64_overflow(), and result.

Referenced by do_interval_accum(), interval_avg_combine(), and interval_pl().

◆ float8_timestamptz()

Datum float8_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 722 of file timestamp.c.

723{
726
727 /* Deal with NaN and infinite inputs ... */
728 if (isnan(seconds))
731 errmsg("timestamp cannot be NaN")));
732
733 if (isinf(seconds))
734 {
735 if (seconds < 0)
737 else
739 }
740 else
741 {
742 /* Out of range? */
743 if (seconds <
745 || seconds >=
749 errmsg("timestamp out of range: \"%g\"", seconds)));
750
751 /* Convert UNIX epoch to Postgres epoch */
753
755 result = (int64) seconds;
756
757 /* Recheck in case roundoff produces something just out of range */
761 errmsg("timestamp out of range: \"%g\"",
762 PG_GETARG_FLOAT8(0))));
763 }
764
766}
double float8
Definition c.h:714
#define DATETIME_MIN_JULIAN
Definition timestamp.h:251
int64 TimestampTz
Definition timestamp.h:39
#define TIMESTAMP_NOBEGIN(j)
Definition timestamp.h:159
#define TIMESTAMP_END_JULIAN
Definition timestamp.h:253
#define IS_VALID_TIMESTAMP(t)
Definition timestamp.h:267
#define UNIX_EPOCH_JDATE
Definition timestamp.h:234
#define SECS_PER_DAY
Definition timestamp.h:126
#define POSTGRES_EPOCH_JDATE
Definition timestamp.h:235
#define TIMESTAMP_NOEND(j)
Definition timestamp.h:164
#define PG_GETARG_FLOAT8(n)
Definition fmgr.h:283
#define PG_RETURN_TIMESTAMP(x)
Definition timestamp.h:67

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

◆ generate_series_timestamp()

Datum generate_series_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 6721 of file timestamp.c.

6722{
6726
6727 /* stuff done only on the first call of the function */
6728 if (SRF_IS_FIRSTCALL())
6729 {
6731 Timestamp finish = PG_GETARG_TIMESTAMP(1);
6732 Interval *step = PG_GETARG_INTERVAL_P(2);
6733 MemoryContext oldcontext;
6734
6735 /* create a function context for cross-call persistence */
6737
6738 /*
6739 * switch to memory context appropriate for multiple function calls
6740 */
6741 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
6742
6743 /* allocate memory for user context */
6745
6746 /*
6747 * Use fctx to keep state from call to call. Seed current with the
6748 * original start value
6749 */
6750 fctx->current = start;
6751 fctx->finish = finish;
6752 fctx->step = *step;
6753
6754 /* Determine sign of the interval */
6755 fctx->step_sign = interval_sign(&fctx->step);
6756
6757 if (fctx->step_sign == 0)
6758 ereport(ERROR,
6760 errmsg("step size cannot equal zero")));
6761
6762 if (INTERVAL_NOT_FINITE((&fctx->step)))
6763 ereport(ERROR,
6765 errmsg("step size cannot be infinite")));
6766
6767 funcctx->user_fctx = fctx;
6768 MemoryContextSwitchTo(oldcontext);
6769 }
6770
6771 /* stuff done on every call of the function */
6773
6774 /*
6775 * get the saved state and use current as the result for this iteration
6776 */
6777 fctx = funcctx->user_fctx;
6778 result = fctx->current;
6779
6780 if (fctx->step_sign > 0 ?
6781 timestamp_cmp_internal(result, fctx->finish) <= 0 :
6782 timestamp_cmp_internal(result, fctx->finish) >= 0)
6783 {
6784 /* increment current in preparation for next iteration */
6786 TimestampGetDatum(fctx->current),
6787 PointerGetDatum(&fctx->step)));
6788
6789 /* do when there is more left to send */
6791 }
6792 else
6793 {
6794 /* do when there is no more left */
6796 }
6797}
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition timestamp.c:2222
Datum timestamp_pl_interval(PG_FUNCTION_ARGS)
Definition timestamp.c:3104
static int interval_sign(const Interval *interval)
Definition timestamp.c:2567
int64 Timestamp
Definition timestamp.h:38
#define palloc_object(type)
Definition fe_memutils.h:89
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:690
#define SRF_IS_FIRSTCALL()
Definition funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition funcapi.h:328
return str start
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
#define PointerGetDatum(X)
Definition postgres.h:354
static Datum TimestampGetDatum(Timestamp X)
Definition timestamp.h:46
#define PG_GETARG_TIMESTAMP(n)
Definition timestamp.h:63
#define PG_GETARG_INTERVAL_P(n)
Definition timestamp.h:65
static Timestamp DatumGetTimestamp(Datum X)
Definition timestamp.h:28

References DatumGetTimestamp(), DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_sign(), MemoryContextSwitchTo(), palloc_object, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PointerGetDatum, result, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, start, timestamp_cmp_internal(), timestamp_pl_interval(), and TimestampGetDatum().

◆ generate_series_timestamp_support()

Datum generate_series_timestamp_support ( PG_FUNCTION_ARGS  )

Definition at line 6901 of file timestamp.c.

6902{
6904 Node *ret = NULL;
6905
6907 {
6908 /* Try to estimate the number of rows returned */
6910
6911 if (is_funcclause(req->node)) /* be paranoid */
6912 {
6913 List *args = ((FuncExpr *) req->node)->args;
6914 Node *arg1,
6915 *arg2,
6916 *arg3;
6917
6918 /* We can use estimated argument values here */
6920 arg2 = estimate_expression_value(req->root, lsecond(args));
6921 arg3 = estimate_expression_value(req->root, lthird(args));
6922
6923 /*
6924 * If any argument is constant NULL, we can safely assume that
6925 * zero rows are returned. Otherwise, if they're all non-NULL
6926 * constants, we can calculate the number of rows that will be
6927 * returned.
6928 */
6929 if ((IsA(arg1, Const) && ((Const *) arg1)->constisnull) ||
6930 (IsA(arg2, Const) && ((Const *) arg2)->constisnull) ||
6931 (IsA(arg3, Const) && ((Const *) arg3)->constisnull))
6932 {
6933 req->rows = 0;
6934 ret = (Node *) req;
6935 }
6936 else if (IsA(arg1, Const) && IsA(arg2, Const) && IsA(arg3, Const))
6937 {
6939 finish;
6940 Interval *step;
6941 Datum diff;
6942 double dstep;
6943 int64 dummy;
6944
6946 finish = DatumGetTimestamp(((Const *) arg2)->constvalue);
6947 step = DatumGetIntervalP(((Const *) arg3)->constvalue);
6948
6949 /*
6950 * Perform some prechecks which could cause timestamp_mi to
6951 * raise an ERROR. It's much better to just return some
6952 * default estimate than error out in a support function.
6953 */
6955 !pg_sub_s64_overflow(finish, start, &dummy))
6956 {
6958 TimestampGetDatum(finish),
6960
6961#define INTERVAL_TO_MICROSECONDS(i) ((((double) (i)->month * DAYS_PER_MONTH + (i)->day)) * USECS_PER_DAY + (i)->time)
6962
6964
6965 /* This equation works for either sign of step */
6966 if (dstep != 0.0)
6967 {
6970
6971 req->rows = floor(ddiff / dstep + 1.0);
6972 ret = (Node *) req;
6973 }
6974#undef INTERVAL_TO_MICROSECONDS
6975 }
6976 }
6977 }
6978 }
6979
6980 PG_RETURN_POINTER(ret);
6981}
Datum timestamp_mi(PG_FUNCTION_ARGS)
Definition timestamp.c:2840
#define INTERVAL_TO_MICROSECONDS(i)
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition clauses.c:2641
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
static bool is_funcclause(const void *clause)
Definition nodeFuncs.h:69
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define lthird(l)
Definition pg_list.h:188
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
uint64_t Datum
Definition postgres.h:70
Definition pg_list.h:54
Definition nodes.h:135
static Interval * DatumGetIntervalP(Datum X)
Definition timestamp.h:40

References DatumGetIntervalP(), DatumGetTimestamp(), DirectFunctionCall2, estimate_expression_value(), fb(), INTERVAL_TO_MICROSECONDS, is_funcclause(), IsA, linitial, lsecond, lthird, PG_GETARG_POINTER, PG_RETURN_POINTER, pg_sub_s64_overflow(), start, timestamp_mi(), TIMESTAMP_NOT_FINITE, and TimestampGetDatum().

◆ generate_series_timestamptz()

Datum generate_series_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 6886 of file timestamp.c.

6887{
6889}
static Datum generate_series_timestamptz_internal(FunctionCallInfo fcinfo)
Definition timestamp.c:6805

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_at_zone()

Datum generate_series_timestamptz_at_zone ( PG_FUNCTION_ARGS  )

Definition at line 6892 of file timestamp.c.

6893{
6895}

References generate_series_timestamptz_internal().

◆ generate_series_timestamptz_internal()

static Datum generate_series_timestamptz_internal ( FunctionCallInfo  fcinfo)
static

Definition at line 6805 of file timestamp.c.

6806{
6810
6811 /* stuff done only on the first call of the function */
6812 if (SRF_IS_FIRSTCALL())
6813 {
6816 Interval *step = PG_GETARG_INTERVAL_P(2);
6817 text *zone = (PG_NARGS() == 4) ? PG_GETARG_TEXT_PP(3) : NULL;
6818 MemoryContext oldcontext;
6819
6820 /* create a function context for cross-call persistence */
6822
6823 /*
6824 * switch to memory context appropriate for multiple function calls
6825 */
6826 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
6827
6828 /* allocate memory for user context */
6830
6831 /*
6832 * Use fctx to keep state from call to call. Seed current with the
6833 * original start value
6834 */
6835 fctx->current = start;
6836 fctx->finish = finish;
6837 fctx->step = *step;
6838 fctx->attimezone = zone ? lookup_timezone(zone) : session_timezone;
6839
6840 /* Determine sign of the interval */
6841 fctx->step_sign = interval_sign(&fctx->step);
6842
6843 if (fctx->step_sign == 0)
6844 ereport(ERROR,
6846 errmsg("step size cannot equal zero")));
6847
6848 if (INTERVAL_NOT_FINITE((&fctx->step)))
6849 ereport(ERROR,
6851 errmsg("step size cannot be infinite")));
6852
6853 funcctx->user_fctx = fctx;
6854 MemoryContextSwitchTo(oldcontext);
6855 }
6856
6857 /* stuff done on every call of the function */
6859
6860 /*
6861 * get the saved state and use current as the result for this iteration
6862 */
6863 fctx = funcctx->user_fctx;
6864 result = fctx->current;
6865
6866 if (fctx->step_sign > 0 ?
6867 timestamp_cmp_internal(result, fctx->finish) <= 0 :
6868 timestamp_cmp_internal(result, fctx->finish) >= 0)
6869 {
6870 /* increment current in preparation for next iteration */
6871 fctx->current = timestamptz_pl_interval_internal(fctx->current,
6872 &fctx->step,
6873 fctx->attimezone);
6874
6875 /* do when there is more left to send */
6877 }
6878 else
6879 {
6880 /* do when there is no more left */
6882 }
6883}
static pg_tz * lookup_timezone(text *zone)
Definition timestamp.c:556
static TimestampTz timestamptz_pl_interval_internal(TimestampTz timestamp, Interval *span, pg_tz *attimezone)
Definition timestamp.c:3248
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_NARGS()
Definition fmgr.h:203
PGDLLIMPORT pg_tz * session_timezone
Definition pgtz.c:28
Definition c.h:776
Definition zic.c:101
static Datum TimestampTzGetDatum(TimestampTz X)
Definition timestamp.h:52
#define PG_GETARG_TIMESTAMPTZ(n)
Definition timestamp.h:64

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_sign(), lookup_timezone(), MemoryContextSwitchTo(), palloc_object, PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_NARGS, result, session_timezone, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, start, timestamp_cmp_internal(), timestamptz_pl_interval_internal(), and TimestampTzGetDatum().

Referenced by generate_series_timestamptz(), and generate_series_timestamptz_at_zone().

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1649 of file timestamp.c.

1650{
1652 struct timeval tp;
1653
1654 gettimeofday(&tp, NULL);
1655
1656 result = (TimestampTz) tp.tv_sec -
1658 result = (result * USECS_PER_SEC) + tp.tv_usec;
1659
1660 return result;
1661}
int gettimeofday(struct timeval *tp, void *tzp)

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

Referenced by ApplyLauncherMain(), asyncQueueFillWarning(), autoprewarm_main(), AutoVacLauncherMain(), BackendStartup(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), check_log_duration(), CheckPointGuts(), CleanupBackend(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), dblink_cancel_query(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_worker(), enable_startup_progress_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_alloc(), entry_reset(), get_candidate_xid(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), initialize_prng(), InitProcessGlobals(), InvalidatePossiblyObsoleteSlot(), IoWorkerMain(), KnownAssignedXidsCompress(), launch_sync_worker(), launcher_determine_sleep(), libpqsrv_cancel(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), maybe_start_io_workers(), pa_send_data(), PerformAuthentication(), PerformWalRecovery(), pg_promote(), pg_sleep(), pg_stash_advice_worker_main(), pgfdw_abort_cleanup_begin(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_finish_abort_cleanup(), pgfdw_get_cleanup_result(), pgss_shmem_init(), pgstat_build_snapshot(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_archiver(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_replslotsync(), pgstat_report_stat(), pgstat_report_vacuum(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_counters(), pgstat_reset_of_kind(), pgstat_reset_slru(), PostgresMain(), PostgresSingleUserMain(), postmaster_child_launch(), PostmasterMain(), PrepareTransaction(), ProcessConfigFileInternal(), ProcessPendingWrites(), ProcessRepliesIfAny(), ProcessStandbyPSRequestMessage(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), ReplicationSlotRelease(), request_publisher_status(), reschedule_timeouts(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), RestoreSlotFromDisk(), send_feedback(), SetCurrentStatementStartTimestamp(), SetupApplyOrSyncWorker(), should_stop_conflict_info_retention(), StartBackgroundWorker(), StartTransaction(), test_pattern(), test_random(), test_random_operations(), throttle(), update_synced_slots_inactive_since(), UpdateWorkerStats(), wait_for_local_flush(), WaitExceedsMaxStandbyDelay(), WaitForLSN(), WaitForWalSummarization(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndCheckShutdownTimeout(), WalSndDone(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogPrefetchResetStats(), XLogPrefetchShmemInit(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

Definition at line 2180 of file timestamp.c.

2181{
2182 struct pg_tm *t0;
2183 pg_time_t epoch = 0;
2184
2185 t0 = pg_gmtime(&epoch);
2186
2187 if (t0 == NULL)
2188 elog(ERROR, "could not convert epoch to timestamp: %m");
2189
2190 tm->tm_year = t0->tm_year;
2191 tm->tm_mon = t0->tm_mon;
2192 tm->tm_mday = t0->tm_mday;
2193 tm->tm_hour = t0->tm_hour;
2194 tm->tm_min = t0->tm_min;
2195 tm->tm_sec = t0->tm_sec;
2196
2197 tm->tm_year += 1900;
2198 tm->tm_mon++;
2199}
static struct pg_tm tm
Definition localtime.c:104
int64 pg_time_t
Definition pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition localtime.c:1392
Definition pgtime.h:35
int tm_hour
Definition pgtime.h:38
int tm_mday
Definition pgtime.h:39
int tm_mon
Definition pgtime.h:40
int tm_min
Definition pgtime.h:37
int tm_sec
Definition pgtime.h:36
int tm_year
Definition pgtime.h:41
static const unsigned __int64 epoch

References elog, epoch, ERROR, fb(), pg_gmtime(), tm, 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(), SetEpochTimestamp(), and SetEpochTimestamp().

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1667 of file timestamp.c.

1668{
1669 TimestampTz ts;
1670
1672 if (typmod >= 0)
1673 AdjustTimestampForTypmod(&ts, typmod, NULL);
1674 return ts;
1675}
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition timestamp.c:363
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition xact.c:872

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1681 of file timestamp.c.

1682{
1683 Timestamp ts;
1684
1686 if (typmod >= 0)
1687 AdjustTimestampForTypmod(&ts, typmod, NULL);
1688 return ts;
1689}
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition timestamp.c:6555

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

Referenced by ExecEvalSQLValueFunction().

◆ in_range_interval_interval()

Datum in_range_interval_interval ( PG_FUNCTION_ARGS  )

Definition at line 3936 of file timestamp.c.

3937{
3939 Interval *base = PG_GETARG_INTERVAL_P(1);
3940 Interval *offset = PG_GETARG_INTERVAL_P(2);
3941 bool sub = PG_GETARG_BOOL(3);
3942 bool less = PG_GETARG_BOOL(4);
3943 Interval *sum;
3944
3945 if (interval_sign(offset) < 0)
3946 ereport(ERROR,
3948 errmsg("invalid preceding or following size in window function")));
3949
3950 /*
3951 * Deal with cases where both base and offset are infinite, and computing
3952 * base +/- offset would cause an error. As for float and numeric types,
3953 * we assume that all values infinitely precede +infinity and infinitely
3954 * follow -infinity. See in_range_float8_float8() for reasoning.
3955 */
3956 if (INTERVAL_IS_NOEND(offset) &&
3957 (sub ? INTERVAL_IS_NOEND(base) : INTERVAL_IS_NOBEGIN(base)))
3959
3960 /* We don't currently bother to avoid overflow hazards here */
3961 if (sub)
3963 IntervalPGetDatum(base),
3964 IntervalPGetDatum(offset)));
3965 else
3967 IntervalPGetDatum(base),
3968 IntervalPGetDatum(offset)));
3969
3970 if (less)
3972 else
3974}
Datum interval_mi(PG_FUNCTION_ARGS)
Definition timestamp.c:3578
Datum interval_pl(PG_FUNCTION_ARGS)
Definition timestamp.c:3522
static int interval_cmp_internal(const Interval *interval1, const Interval *interval2)
Definition timestamp.c:2558
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
long val
Definition informix.c:689
return true
Definition isn.c:130
static Datum IntervalPGetDatum(const Interval *X)
Definition timestamp.h:58

References DatumGetIntervalP(), DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), interval_cmp_internal(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, interval_mi(), interval_pl(), interval_sign(), IntervalPGetDatum(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_RETURN_BOOL, and val.

◆ in_range_timestamp_interval()

Datum in_range_timestamp_interval ( PG_FUNCTION_ARGS  )

Definition at line 3895 of file timestamp.c.

3896{
3899 Interval *offset = PG_GETARG_INTERVAL_P(2);
3900 bool sub = PG_GETARG_BOOL(3);
3901 bool less = PG_GETARG_BOOL(4);
3902 Timestamp sum;
3903
3904 if (interval_sign(offset) < 0)
3905 ereport(ERROR,
3907 errmsg("invalid preceding or following size in window function")));
3908
3909 /*
3910 * Deal with cases where both base and offset are infinite, and computing
3911 * base +/- offset would cause an error. As for float and numeric types,
3912 * we assume that all values infinitely precede +infinity and infinitely
3913 * follow -infinity. See in_range_float8_float8() for reasoning.
3914 */
3915 if (INTERVAL_IS_NOEND(offset) &&
3916 (sub ? TIMESTAMP_IS_NOEND(base) : TIMESTAMP_IS_NOBEGIN(base)))
3918
3919 /* We don't currently bother to avoid overflow hazards here */
3920 if (sub)
3922 TimestampGetDatum(base),
3923 IntervalPGetDatum(offset)));
3924 else
3926 TimestampGetDatum(base),
3927 IntervalPGetDatum(offset)));
3928
3929 if (less)
3930 PG_RETURN_BOOL(val <= sum);
3931 else
3932 PG_RETURN_BOOL(val >= sum);
3933}
Datum timestamp_mi_interval(PG_FUNCTION_ARGS)
Definition timestamp.c:3221

References DatumGetTimestamp(), DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOEND, interval_sign(), IntervalPGetDatum(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, timestamp_mi_interval(), timestamp_pl_interval(), TimestampGetDatum(), and val.

Referenced by in_range_date_interval().

◆ in_range_timestamptz_interval()

Datum in_range_timestamptz_interval ( PG_FUNCTION_ARGS  )

Definition at line 3858 of file timestamp.c.

3859{
3862 Interval *offset = PG_GETARG_INTERVAL_P(2);
3863 bool sub = PG_GETARG_BOOL(3);
3864 bool less = PG_GETARG_BOOL(4);
3865 TimestampTz sum;
3866
3867 if (interval_sign(offset) < 0)
3868 ereport(ERROR,
3870 errmsg("invalid preceding or following size in window function")));
3871
3872 /*
3873 * Deal with cases where both base and offset are infinite, and computing
3874 * base +/- offset would cause an error. As for float and numeric types,
3875 * we assume that all values infinitely precede +infinity and infinitely
3876 * follow -infinity. See in_range_float8_float8() for reasoning.
3877 */
3878 if (INTERVAL_IS_NOEND(offset) &&
3879 (sub ? TIMESTAMP_IS_NOEND(base) : TIMESTAMP_IS_NOBEGIN(base)))
3881
3882 /* We don't currently bother to avoid overflow hazards here */
3883 if (sub)
3884 sum = timestamptz_mi_interval_internal(base, offset, NULL);
3885 else
3886 sum = timestamptz_pl_interval_internal(base, offset, NULL);
3887
3888 if (less)
3889 PG_RETURN_BOOL(val <= sum);
3890 else
3891 PG_RETURN_BOOL(val >= sum);
3892}
static TimestampTz timestamptz_mi_interval_internal(TimestampTz timestamp, Interval *span, pg_tz *attimezone)
Definition timestamp.c:3381

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOEND, interval_sign(), PG_GETARG_BOOL, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PG_RETURN_BOOL, TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, timestamptz_mi_interval_internal(), timestamptz_pl_interval_internal(), and val.

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 2057 of file timestamp.c.

2058{
2059 TimeOffset time;
2061
2062 itm->tm_year = span.month / MONTHS_PER_YEAR;
2063 itm->tm_mon = span.month % MONTHS_PER_YEAR;
2064 itm->tm_mday = span.day;
2065 time = span.time;
2066
2067 tfrac = time / USECS_PER_HOUR;
2068 time -= tfrac * USECS_PER_HOUR;
2069 itm->tm_hour = tfrac;
2070 tfrac = time / USECS_PER_MINUTE;
2071 time -= tfrac * USECS_PER_MINUTE;
2072 itm->tm_min = (int) tfrac;
2073 tfrac = time / USECS_PER_SEC;
2074 time -= tfrac * USECS_PER_SEC;
2075 itm->tm_sec = (int) tfrac;
2076 itm->tm_usec = (int) time;
2077}

References fb(), MONTHS_PER_YEAR, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ interval_avg()

Datum interval_avg ( PG_FUNCTION_ARGS  )

Definition at line 4227 of file timestamp.c.

4228{
4230
4232
4233 /* If there were no non-null inputs, return NULL */
4234 if (state == NULL || IA_TOTAL_COUNT(state) == 0)
4236
4237 /*
4238 * Aggregating infinities that all have the same sign produces infinity
4239 * with that sign. Aggregating infinities with different signs results in
4240 * an error.
4241 */
4242 if (state->pInfcount > 0 || state->nInfcount > 0)
4243 {
4245
4246 if (state->pInfcount > 0 && state->nInfcount > 0)
4247 ereport(ERROR,
4249 errmsg("interval out of range")));
4250
4252 if (state->pInfcount > 0)
4254 else
4256
4258 }
4259
4261 IntervalPGetDatum(&state->sumX),
4262 Float8GetDatum((double) state->N));
4263}
#define IA_TOTAL_COUNT(ia)
Definition timestamp.c:80
Datum interval_div(PG_FUNCTION_ARGS)
Definition timestamp.c:3757
#define INTERVAL_NOEND(i)
Definition timestamp.h:185
#define INTERVAL_NOBEGIN(i)
Definition timestamp.h:175
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_RETURN_NULL()
Definition fmgr.h:346
static Datum Float8GetDatum(float8 X)
Definition postgres.h:515
#define PG_RETURN_INTERVAL_P(x)
Definition timestamp.h:69

References DirectFunctionCall2, ereport, errcode(), errmsg, ERROR, fb(), Float8GetDatum(), IA_TOTAL_COUNT, interval_div(), INTERVAL_NOBEGIN, INTERVAL_NOEND, IntervalPGetDatum(), palloc_object, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, and result.

◆ interval_avg_accum()

Datum interval_avg_accum ( PG_FUNCTION_ARGS  )

Definition at line 4062 of file timestamp.c.

4063{
4065
4067
4068 /* Create the state data on the first call */
4069 if (state == NULL)
4070 state = makeIntervalAggState(fcinfo);
4071
4072 if (!PG_ARGISNULL(1))
4074
4076}
static void do_interval_accum(IntervalAggState *state, Interval *newval)
Definition timestamp.c:4008
static IntervalAggState * makeIntervalAggState(FunctionCallInfo fcinfo)
Definition timestamp.c:3986

References do_interval_accum(), fb(), makeIntervalAggState(), PG_ARGISNULL, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ interval_avg_accum_inv()

Datum interval_avg_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 4209 of file timestamp.c.

4210{
4212
4214
4215 /* Should not get here with no state */
4216 if (state == NULL)
4217 elog(ERROR, "interval_avg_accum_inv called with NULL state");
4218
4219 if (!PG_ARGISNULL(1))
4221
4223}
static void do_interval_discard(IntervalAggState *state, Interval *newval)
Definition timestamp.c:4031

References do_interval_discard(), elog, ERROR, fb(), PG_ARGISNULL, PG_GETARG_INTERVAL_P, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ interval_avg_combine()

Datum interval_avg_combine ( PG_FUNCTION_ARGS  )

Definition at line 4085 of file timestamp.c.

4086{
4089
4092
4093 if (state2 == NULL)
4095
4096 if (state1 == NULL)
4097 {
4098 /* manually copy all fields from state2 to state1 */
4099 state1 = makeIntervalAggState(fcinfo);
4100
4101 state1->N = state2->N;
4102 state1->pInfcount = state2->pInfcount;
4103 state1->nInfcount = state2->nInfcount;
4104
4105 state1->sumX.day = state2->sumX.day;
4106 state1->sumX.month = state2->sumX.month;
4107 state1->sumX.time = state2->sumX.time;
4108
4110 }
4111
4112 state1->N += state2->N;
4113 state1->pInfcount += state2->pInfcount;
4114 state1->nInfcount += state2->nInfcount;
4115
4116 /* Accumulate finite interval values, if any. */
4117 if (state2->N > 0)
4118 finite_interval_pl(&state1->sumX, &state2->sumX, &state1->sumX);
4119
4121}

References fb(), finite_interval_pl(), makeIntervalAggState(), PG_ARGISNULL, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ interval_avg_deserialize()

Datum interval_avg_deserialize ( PG_FUNCTION_ARGS  )

Definition at line 4166 of file timestamp.c.

4167{
4168 bytea *sstate;
4171
4172 if (!AggCheckCallContext(fcinfo, NULL))
4173 elog(ERROR, "aggregate function called in non-aggregate context");
4174
4175 sstate = PG_GETARG_BYTEA_PP(0);
4176
4177 /*
4178 * Initialize a StringInfo so that we can "receive" it using the standard
4179 * recv-function infrastructure.
4180 */
4182 VARSIZE_ANY_EXHDR(sstate));
4183
4185
4186 /* N */
4187 result->N = pq_getmsgint64(&buf);
4188
4189 /* sumX */
4190 result->sumX.time = pq_getmsgint64(&buf);
4191 result->sumX.day = pq_getmsgint(&buf, 4);
4192 result->sumX.month = pq_getmsgint(&buf, 4);
4193
4194 /* pInfcount */
4195 result->pInfcount = pq_getmsgint64(&buf);
4196
4197 /* nInfcount */
4198 result->nInfcount = pq_getmsgint64(&buf);
4199
4200 pq_getmsgend(&buf);
4201
4203}
#define palloc0_object(type)
Definition fe_memutils.h:90
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition nodeAgg.c:4609
static char buf[DEFAULT_XLOG_SEG_SIZE]
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
void pq_getmsgend(StringInfo msg)
Definition pqformat.c:634
int64 pq_getmsgint64(StringInfo msg)
Definition pqformat.c:452
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
Definition stringinfo.h:157
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

References AggCheckCallContext(), buf, elog, ERROR, fb(), initReadOnlyStringInfo(), palloc0_object, PG_GETARG_BYTEA_PP, PG_RETURN_POINTER, pq_getmsgend(), pq_getmsgint(), pq_getmsgint64(), result, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ interval_avg_serialize()

Datum interval_avg_serialize ( PG_FUNCTION_ARGS  )

Definition at line 4128 of file timestamp.c.

4129{
4132 bytea *result;
4133
4134 /* Ensure we disallow calling when not in aggregate context */
4135 if (!AggCheckCallContext(fcinfo, NULL))
4136 elog(ERROR, "aggregate function called in non-aggregate context");
4137
4139
4141
4142 /* N */
4143 pq_sendint64(&buf, state->N);
4144
4145 /* sumX */
4146 pq_sendint64(&buf, state->sumX.time);
4147 pq_sendint32(&buf, state->sumX.day);
4148 pq_sendint32(&buf, state->sumX.month);
4149
4150 /* pInfcount */
4151 pq_sendint64(&buf, state->pInfcount);
4152
4153 /* nInfcount */
4154 pq_sendint64(&buf, state->nInfcount);
4155
4157
4159}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144
static void pq_sendint64(StringInfo buf, uint64 i)
Definition pqformat.h:152

References AggCheckCallContext(), buf, elog, ERROR, fb(), PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint32(), pq_sendint64(), and result.

◆ interval_cmp()

Datum interval_cmp ( PG_FUNCTION_ARGS  )

◆ interval_cmp_internal()

static int interval_cmp_internal ( const Interval interval1,
const Interval interval2 
)
static

Definition at line 2558 of file timestamp.c.

2559{
2562
2563 return int128_compare(span1, span2);
2564}
static INT128 interval_cmp_value(const Interval *interval)
Definition timestamp.c:2536
static int int128_compare(INT128 x, INT128 y)
Definition int128.h:425

References fb(), int128_compare(), and interval_cmp_value().

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

◆ interval_cmp_value()

static INT128 interval_cmp_value ( const Interval interval)
inlinestatic

Definition at line 2536 of file timestamp.c.

2537{
2538 INT128 span;
2539 int64 days;
2540
2541 /*
2542 * Combine the month and day fields into an integral number of days.
2543 * Because the inputs are int32, int64 arithmetic suffices here.
2544 */
2545 days = interval->month * INT64CONST(30);
2546 days += interval->day;
2547
2548 /* Widen time field to 128 bits */
2550
2551 /* Scale up days to microseconds, forming a 128-bit product */
2553
2554 return span;
2555}
const char *const days[]
Definition datetime.c:85
#define USECS_PER_DAY
Definition timestamp.h:131
static INT128 int64_to_int128(int64 v)
Definition int128.h:450
static void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y)
Definition int128.h:203

References days, fb(), int128_add_int64_mul_int64(), int64_to_int128(), INT64CONST, interval::month, interval::time, and USECS_PER_DAY.

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

◆ interval_div()

Datum interval_div ( PG_FUNCTION_ARGS  )

Definition at line 3757 of file timestamp.c.

3758{
3761 double month_remainder_days,
3764 int32 orig_month = span->month,
3765 orig_day = span->day;
3767
3769
3770 if (factor == 0.0)
3771 ereport(ERROR,
3773 errmsg("division by zero")));
3774
3775 /*
3776 * Handle NaN and infinities.
3777 *
3778 * We treat "infinity / infinity" as an error, since the interval type has
3779 * nothing equivalent to NaN. Otherwise, dividing by infinity is handled
3780 * by the regular division code, causing all fields to be set to zero.
3781 */
3782 if (isnan(factor))
3783 goto out_of_range;
3784
3786 {
3787 if (isinf(factor))
3788 goto out_of_range;
3789
3790 if (factor < 0.0)
3792 else
3793 memcpy(result, span, sizeof(Interval));
3794
3796 }
3797
3798 result_double = span->month / factor;
3800 goto out_of_range;
3801 result->month = (int32) result_double;
3802
3803 result_double = span->day / factor;
3805 goto out_of_range;
3806 result->day = (int32) result_double;
3807
3808 /*
3809 * Fractional months full days into days. See comment in interval_mul().
3810 */
3813 sec_remainder = (orig_day / factor - result->day +
3817 {
3818 if (pg_add_s32_overflow(result->day,
3819 (int) (sec_remainder / SECS_PER_DAY),
3820 &result->day))
3821 goto out_of_range;
3823 }
3824
3825 /* cascade units down */
3827 &result->day))
3828 goto out_of_range;
3831 goto out_of_range;
3832 result->time = (int64) result_double;
3833
3835 goto out_of_range;
3836
3838
3840 ereport(ERROR,
3842 errmsg("interval out of range"));
3843
3844 PG_RETURN_NULL(); /* keep compiler quiet */
3845}
static void interval_um_internal(const Interval *interval, Interval *result)
Definition timestamp.c:3445
#define FLOAT8_FITS_IN_INT32(num)
Definition c.h:1177
#define FLOAT8_FITS_IN_INT64(num)
Definition c.h:1179
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define DAYS_PER_MONTH
Definition timestamp.h:116
#define TSROUND(j)
Definition timestamp.h:100

References DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), FLOAT8_FITS_IN_INT32, FLOAT8_FITS_IN_INT64, INTERVAL_NOT_FINITE, interval_um_internal(), memcpy(), palloc_object, pg_add_s32_overflow(), PG_GETARG_FLOAT8, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, result, SECS_PER_DAY, TSROUND, and USECS_PER_SEC.

Referenced by interval_avg().

◆ interval_eq()

◆ interval_finite()

Datum interval_finite ( PG_FUNCTION_ARGS  )

◆ interval_ge()

◆ interval_gt()

◆ interval_hash()

Datum interval_hash ( PG_FUNCTION_ARGS  )

Definition at line 2646 of file timestamp.c.

2647{
2650 int64 span64;
2651
2652 /*
2653 * Use only the least significant 64 bits for hashing. The upper 64 bits
2654 * seldom add any useful information, and besides we must do it like this
2655 * for compatibility with hashes calculated before use of INT128 was
2656 * introduced.
2657 */
2659
2661}
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:688
Datum hashint8(PG_FUNCTION_ARGS)
Definition hashfunc.c:84
static int64 int128_to_int64(INT128 val)
Definition int128.h:468
#define Int64GetDatumFast(X)
Definition postgres.h:538

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

◆ interval_hash_extended()

Datum interval_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2664 of file timestamp.c.

2665{
2668 int64 span64;
2669
2670 /* Same approach as interval_hash */
2672
2674 PG_GETARG_DATUM(1));
2675}
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
Datum hashint8extended(PG_FUNCTION_ARGS)
Definition hashfunc.c:104

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

◆ interval_in()

Datum interval_in ( PG_FUNCTION_ARGS  )

Definition at line 891 of file timestamp.c.

892{
893 char *str = PG_GETARG_CSTRING(0);
894#ifdef NOT_USED
895 Oid typelem = PG_GETARG_OID(1);
896#endif
897 int32 typmod = PG_GETARG_INT32(2);
898 Node *escontext = fcinfo->context;
900 struct pg_itm_in tt,
901 *itm_in = &tt;
902 int dtype;
903 int nf;
904 int range;
905 int dterr;
906 char *field[MAXDATEFIELDS];
907 int ftype[MAXDATEFIELDS];
908 char workbuf[256];
909 DateTimeErrorExtra extra;
910
911 itm_in->tm_year = 0;
912 itm_in->tm_mon = 0;
913 itm_in->tm_mday = 0;
914 itm_in->tm_usec = 0;
915
916 if (typmod >= 0)
917 range = INTERVAL_RANGE(typmod);
918 else
920
921 dterr = ParseDateTime(str, workbuf, sizeof(workbuf), field,
922 ftype, MAXDATEFIELDS, &nf);
923 if (dterr == 0)
924 dterr = DecodeInterval(field, ftype, nf, range,
925 &dtype, itm_in);
926
927 /* if those functions think it's a bad format, try ISO8601 style */
928 if (dterr == DTERR_BAD_FORMAT)
930 &dtype, itm_in);
931
932 if (dterr != 0)
933 {
936 DateTimeParseError(dterr, &extra, str, "interval", escontext);
938 }
939
941
942 switch (dtype)
943 {
944 case DTK_DELTA:
945 if (itmin2interval(itm_in, result) != 0)
946 ereturn(escontext, (Datum) 0,
948 errmsg("interval out of range")));
949 break;
950
951 case DTK_LATE:
953 break;
954
955 case DTK_EARLY:
957 break;
958
959 default:
960 elog(ERROR, "unexpected dtype %d while parsing interval \"%s\"",
961 dtype, str);
962 }
963
964 AdjustIntervalForTypmod(result, typmod, escontext);
965
967}
int ParseDateTime(const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
Definition datetime.c:775
void DateTimeParseError(int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, Node *escontext)
Definition datetime.c:4241
int DecodeInterval(char **field, int *ftype, int nf, int range, int *dtype, struct pg_itm_in *itm_in)
Definition datetime.c:3511
int DecodeISO8601Interval(char *str, int *dtype, struct pg_itm_in *itm_in)
Definition datetime.c:3977
int itmin2interval(struct pg_itm_in *itm_in, Interval *span)
Definition timestamp.c:2127
static bool AdjustIntervalForTypmod(Interval *interval, int32 typmod, Node *escontext)
Definition timestamp.c:1353
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define MAXDATEFIELDS
Definition datetime.h:202
#define DTERR_INTERVAL_OVERFLOW
Definition datetime.h:287
#define DTK_DELTA
Definition datetime.h:159
#define DTK_LATE
Definition datetime.h:151
#define DTERR_BAD_FORMAT
Definition datetime.h:284
#define DTK_EARLY
Definition datetime.h:150
#define DTERR_FIELD_OVERFLOW
Definition datetime.h:285
unsigned int Oid

References AdjustIntervalForTypmod(), DateTimeParseError(), DecodeInterval(), DecodeISO8601Interval(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTK_DELTA, DTK_EARLY, DTK_LATE, elog, ereturn, errcode(), errmsg, ERROR, fb(), INTERVAL_FULL_RANGE, INTERVAL_NOBEGIN, INTERVAL_NOEND, INTERVAL_RANGE, itmin2interval(), MAXDATEFIELDS, palloc_object, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, range(), result, and str.

Referenced by check_timezone(), and flatten_set_variable_args().

◆ interval_justify_days()

Datum interval_justify_days ( PG_FUNCTION_ARGS  )

Definition at line 3056 of file timestamp.c.

3057{
3061
3063 result->month = span->month;
3064 result->day = span->day;
3065 result->time = span->time;
3066
3067 /* do nothing for infinite intervals */
3070
3073 if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
3074 ereport(ERROR,
3076 errmsg("interval out of range")));
3077
3078 if (result->month > 0 && result->day < 0)
3079 {
3080 result->day += DAYS_PER_MONTH;
3081 result->month--;
3082 }
3083 else if (result->month < 0 && result->day > 0)
3084 {
3085 result->day -= DAYS_PER_MONTH;
3086 result->month++;
3087 }
3088
3090}

References DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, palloc_object, pg_add_s32_overflow(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, and result.

◆ interval_justify_hours()

Datum interval_justify_hours ( PG_FUNCTION_ARGS  )

Definition at line 3014 of file timestamp.c.

3015{
3019
3021 result->month = span->month;
3022 result->day = span->day;
3023 result->time = span->time;
3024
3025 /* do nothing for infinite intervals */
3028
3030 if (pg_add_s32_overflow(result->day, wholeday, &result->day))
3031 ereport(ERROR,
3033 errmsg("interval out of range")));
3034
3035 if (result->day > 0 && result->time < 0)
3036 {
3037 result->time += USECS_PER_DAY;
3038 result->day--;
3039 }
3040 else if (result->day < 0 && result->time > 0)
3041 {
3042 result->time -= USECS_PER_DAY;
3043 result->day++;
3044 }
3045
3047}
#define TMODULO(t, q, u)
Definition datetime.h:250

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, palloc_object, pg_add_s32_overflow(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, result, TMODULO, and USECS_PER_DAY.

Referenced by timestamp_mi().

◆ interval_justify_interval()

Datum interval_justify_interval ( PG_FUNCTION_ARGS  )

Definition at line 2934 of file timestamp.c.

2935{
2940
2942 result->month = span->month;
2943 result->day = span->day;
2944 result->time = span->time;
2945
2946 /* do nothing for infinite intervals */
2949
2950 /* pre-justify days if it might prevent overflow */
2951 if ((result->day > 0 && result->time > 0) ||
2952 (result->day < 0 && result->time < 0))
2953 {
2956 if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2957 ereport(ERROR,
2959 errmsg("interval out of range")));
2960 }
2961
2962 /*
2963 * Since TimeOffset is int64, abs(wholeday) can't exceed about 1.07e8. If
2964 * we pre-justified then abs(result->day) is less than DAYS_PER_MONTH, so
2965 * this addition can't overflow. If we didn't pre-justify, then day and
2966 * time are of different signs, so it still can't overflow.
2967 */
2969 result->day += wholeday;
2970
2973 if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
2974 ereport(ERROR,
2976 errmsg("interval out of range")));
2977
2978 if (result->month > 0 &&
2979 (result->day < 0 || (result->day == 0 && result->time < 0)))
2980 {
2981 result->day += DAYS_PER_MONTH;
2982 result->month--;
2983 }
2984 else if (result->month < 0 &&
2985 (result->day > 0 || (result->day == 0 && result->time > 0)))
2986 {
2987 result->day -= DAYS_PER_MONTH;
2988 result->month++;
2989 }
2990
2991 if (result->day > 0 && result->time < 0)
2992 {
2993 result->time += USECS_PER_DAY;
2994 result->day--;
2995 }
2996 else if (result->day < 0 && result->time > 0)
2997 {
2998 result->time -= USECS_PER_DAY;
2999 result->day++;
3000 }
3001
3003}

References DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, palloc_object, pg_add_s32_overflow(), PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, result, TMODULO, and USECS_PER_DAY.

◆ interval_larger()

◆ interval_le()

◆ interval_lt()

◆ interval_mi()

Datum interval_mi ( PG_FUNCTION_ARGS  )

Definition at line 3578 of file timestamp.c.

3579{
3583
3585
3586 /*
3587 * Handle infinities.
3588 *
3589 * We treat anything that amounts to "infinity - infinity" as an error,
3590 * since the interval type has nothing equivalent to NaN.
3591 */
3593 {
3595 ereport(ERROR,
3597 errmsg("interval out of range")));
3598 else
3600 }
3601 else if (INTERVAL_IS_NOEND(span1))
3602 {
3604 ereport(ERROR,
3606 errmsg("interval out of range")));
3607 else
3609 }
3610 else if (INTERVAL_IS_NOBEGIN(span2))
3612 else if (INTERVAL_IS_NOEND(span2))
3614 else
3616
3618}

References ereport, errcode(), errmsg, ERROR, fb(), finite_interval_mi(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, INTERVAL_NOBEGIN, INTERVAL_NOEND, palloc_object, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, and result.

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

◆ interval_mul()

Datum interval_mul ( PG_FUNCTION_ARGS  )

Definition at line 3627 of file timestamp.c.

3628{
3631 double month_remainder_days,
3634 int32 orig_month = span->month,
3635 orig_day = span->day;
3637
3639
3640 /*
3641 * Handle NaN and infinities.
3642 *
3643 * We treat "0 * infinity" and "infinity * 0" as errors, since the
3644 * interval type has nothing equivalent to NaN.
3645 */
3646 if (isnan(factor))
3647 goto out_of_range;
3648
3650 {
3651 if (factor == 0.0)
3652 goto out_of_range;
3653
3654 if (factor < 0.0)
3656 else
3657 memcpy(result, span, sizeof(Interval));
3658
3660 }
3661 if (isinf(factor))
3662 {
3663 int isign = interval_sign(span);
3664
3665 if (isign == 0)
3666 goto out_of_range;
3667
3668 if (factor * isign < 0)
3670 else
3672
3674 }
3675
3676 result_double = span->month * factor;
3678 goto out_of_range;
3679 result->month = (int32) result_double;
3680
3681 result_double = span->day * factor;
3683 goto out_of_range;
3684 result->day = (int32) result_double;
3685
3686 /*
3687 * The above correctly handles the whole-number part of the month and day
3688 * products, but we have to do something with any fractional part
3689 * resulting when the factor is non-integral. We cascade the fractions
3690 * down to lower units using the conversion factors DAYS_PER_MONTH and
3691 * SECS_PER_DAY. Note we do NOT cascade up, since we are not forced to do
3692 * so by the representation. The user can choose to cascade up later,
3693 * using justify_hours and/or justify_days.
3694 */
3695
3696 /*
3697 * Fractional months full days into days.
3698 *
3699 * Floating point calculation are inherently imprecise, so these
3700 * calculations are crafted to produce the most reliable result possible.
3701 * TSROUND() is needed to more accurately produce whole numbers where
3702 * appropriate.
3703 */
3706 sec_remainder = (orig_day * factor - result->day +
3709
3710 /*
3711 * Might have 24:00:00 hours due to rounding, or >24 hours because of time
3712 * cascade from months and days. It might still be >24 if the combination
3713 * of cascade and the seconds factor operation itself.
3714 */
3716 {
3717 if (pg_add_s32_overflow(result->day,
3718 (int) (sec_remainder / SECS_PER_DAY),
3719 &result->day))
3720 goto out_of_range;
3722 }
3723
3724 /* cascade units down */
3726 &result->day))
3727 goto out_of_range;
3730 goto out_of_range;
3731 result->time = (int64) result_double;
3732
3734 goto out_of_range;
3735
3737
3739 ereport(ERROR,
3741 errmsg("interval out of range"));
3742
3743 PG_RETURN_NULL(); /* keep compiler quiet */
3744}

References DAYS_PER_MONTH, ereport, errcode(), errmsg, ERROR, fb(), FLOAT8_FITS_IN_INT32, FLOAT8_FITS_IN_INT64, INTERVAL_NOBEGIN, INTERVAL_NOEND, INTERVAL_NOT_FINITE, interval_sign(), interval_um_internal(), memcpy(), palloc_object, pg_add_s32_overflow(), PG_GETARG_FLOAT8, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, result, SECS_PER_DAY, TSROUND, and USECS_PER_SEC.

Referenced by interval_lerp(), and mul_d_interval().

◆ interval_ne()

◆ interval_out()

Datum interval_out ( PG_FUNCTION_ARGS  )

Definition at line 974 of file timestamp.c.

975{
977 char *result;
978 struct pg_itm tt,
979 *itm = &tt;
980 char buf[MAXDATELEN + 1];
981
984 else
985 {
988 }
989
990 result = pstrdup(buf);
992}
void EncodeInterval(struct pg_itm *itm, int style, char *str)
Definition datetime.c:4740
static void EncodeSpecialInterval(const Interval *interval, char *str)
Definition timestamp.c:1602
void interval2itm(Interval span, struct pg_itm *itm)
Definition timestamp.c:2057
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
int IntervalStyle
Definition globals.c:129
#define MAXDATELEN
Definition datetime.h:200

References buf, EncodeInterval(), EncodeSpecialInterval(), fb(), interval2itm(), INTERVAL_NOT_FINITE, IntervalStyle, MAXDATELEN, PG_GETARG_INTERVAL_P, PG_RETURN_CSTRING, pstrdup(), and result.

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

◆ interval_part()

Datum interval_part ( PG_FUNCTION_ARGS  )

Definition at line 6323 of file timestamp.c.

6324{
6325 return interval_part_common(fcinfo, false);
6326}

References interval_part_common().

◆ interval_part_common()

static Datum interval_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 6116 of file timestamp.c.

6117{
6121 int type,
6122 val;
6123 char *lowunits;
6124 struct pg_itm tt,
6125 *tm = &tt;
6126
6129 false);
6130
6131 type = DecodeUnits(0, lowunits, &val);
6132 if (type == UNKNOWN_FIELD)
6134
6136 {
6139
6140 if (r != 0.0)
6141 {
6142 if (retnumeric)
6143 {
6144 if (r < 0)
6146 CStringGetDatum("-Infinity"),
6148 Int32GetDatum(-1));
6149 else if (r > 0)
6151 CStringGetDatum("Infinity"),
6153 Int32GetDatum(-1));
6154 }
6155 else
6157 }
6158 else
6160 }
6161
6162 if (type == UNITS)
6163 {
6165 switch (val)
6166 {
6167 case DTK_MICROSEC:
6168 intresult = tm->tm_sec * INT64CONST(1000000) + tm->tm_usec;
6169 break;
6170
6171 case DTK_MILLISEC:
6172 if (retnumeric)
6173 /*---
6174 * tm->tm_sec * 1000 + fsec / 1000
6175 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
6176 */
6178 else
6179 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + tm->tm_usec / 1000.0);
6180 break;
6181
6182 case DTK_SECOND:
6183 if (retnumeric)
6184 /*---
6185 * tm->tm_sec + fsec / 1'000'000
6186 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
6187 */
6189 else
6190 PG_RETURN_FLOAT8(tm->tm_sec + tm->tm_usec / 1000000.0);
6191 break;
6192
6193 case DTK_MINUTE:
6194 intresult = tm->tm_min;
6195 break;
6196
6197 case DTK_HOUR:
6198 intresult = tm->tm_hour;
6199 break;
6200
6201 case DTK_DAY:
6202 intresult = tm->tm_mday;
6203 break;
6204
6205 case DTK_WEEK:
6206 intresult = tm->tm_mday / 7;
6207 break;
6208
6209 case DTK_MONTH:
6210 intresult = tm->tm_mon;
6211 break;
6212
6213 case DTK_QUARTER:
6214
6215 /*
6216 * We want to maintain the rule that a field extracted from a
6217 * negative interval is the negative of the field's value for
6218 * the sign-reversed interval. The broken-down tm_year and
6219 * tm_mon aren't very helpful for that, so work from
6220 * interval->month.
6221 */
6222 if (interval->month >= 0)
6223 intresult = (tm->tm_mon / 3) + 1;
6224 else
6225 intresult = -(((-interval->month % MONTHS_PER_YEAR) / 3) + 1);
6226 break;
6227
6228 case DTK_YEAR:
6229 intresult = tm->tm_year;
6230 break;
6231
6232 case DTK_DECADE:
6233 /* caution: C division may have negative remainder */
6234 intresult = tm->tm_year / 10;
6235 break;
6236
6237 case DTK_CENTURY:
6238 /* caution: C division may have negative remainder */
6239 intresult = tm->tm_year / 100;
6240 break;
6241
6242 case DTK_MILLENNIUM:
6243 /* caution: C division may have negative remainder */
6244 intresult = tm->tm_year / 1000;
6245 break;
6246
6247 default:
6248 ereport(ERROR,
6250 errmsg("unit \"%s\" not supported for type %s",
6252 intresult = 0;
6253 }
6254 }
6255 else if (type == RESERV && val == DTK_EPOCH)
6256 {
6257 if (retnumeric)
6258 {
6261 int64 val;
6262
6263 /*
6264 * To do this calculation in integer arithmetic even though
6265 * DAYS_PER_YEAR is fractional, multiply everything by 4 and then
6266 * divide by 4 again at the end. This relies on DAYS_PER_YEAR
6267 * being a multiple of 0.25 and on SECS_PER_DAY being a multiple
6268 * of 4.
6269 */
6272 (int64) 4 * interval->day) * (SECS_PER_DAY / 4);
6273
6274 /*---
6275 * result = secs_from_day_month + interval->time / 1'000'000
6276 * = (secs_from_day_month * 1'000'000 + interval->time) / 1'000'000
6277 */
6278
6279 /*
6280 * Try the computation inside int64; if it overflows, do it in
6281 * numeric (slower). This overflow happens around 10^9 days, so
6282 * not common in practice.
6283 */
6284 if (!pg_mul_s64_overflow(secs_from_day_month, 1000000, &val) &&
6287 else
6288 result =
6291 NULL);
6292
6294 }
6295 else
6296 {
6297 float8 result;
6298
6299 result = interval->time / 1000000.0;
6302 result += ((double) SECS_PER_DAY) * interval->day;
6303
6305 }
6306 }
6307 else
6308 {
6309 ereport(ERROR,
6311 errmsg("unit \"%s\" not recognized for type %s",
6313 intresult = 0;
6314 }
6315
6316 if (retnumeric)
6318 else
6320}
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition datetime.c:4196
int DecodeSpecial(int field, const char *lowtoken, int *val)
Definition datetime.c:3266
Numeric int64_to_numeric(int64 val)
Definition numeric.c:4264
Numeric numeric_add_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:2883
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition numeric.c:4285
Datum numeric_in(PG_FUNCTION_ARGS)
Definition numeric.c:626
static float8 NonFiniteIntervalPart(int type, int unit, char *lowunits, bool isNegative)
Definition timestamp.c:6069
#define DAYS_PER_YEAR
Definition timestamp.h:107
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition fmgr.h:692
char * format_type_be(Oid type_oid)
#define DTK_EPOCH
Definition datetime.h:152
#define UNKNOWN_FIELD
Definition datetime.h:124
#define DTK_DECADE
Definition datetime.h:168
#define DTK_SECOND
Definition datetime.h:160
#define DTK_QUARTER
Definition datetime.h:166
#define DTK_CENTURY
Definition datetime.h:169
#define DTK_DAY
Definition datetime.h:163
#define RESERV
Definition datetime.h:90
#define DTK_MILLENNIUM
Definition datetime.h:170
#define DTK_HOUR
Definition datetime.h:162
#define DTK_WEEK
Definition datetime.h:164
#define DTK_MICROSEC
Definition datetime.h:172
#define DTK_YEAR
Definition datetime.h:167
#define DTK_MILLISEC
Definition datetime.h:171
#define DTK_MONTH
Definition datetime.h:165
#define DTK_MINUTE
Definition datetime.h:161
#define UNITS
Definition datetime.h:107
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:293
#define PG_RETURN_NUMERIC(x)
Definition numeric.h:83
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition postgres.h:383
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
#define InvalidOid
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition scansup.c:38
const char * type

References CStringGetDatum(), DAYS_PER_MONTH, DAYS_PER_YEAR, DecodeSpecial(), DecodeUnits(), DirectFunctionCall3, 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_WEEK, DTK_YEAR, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), Int32GetDatum(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, interval2itm(), INTERVAL_IS_NOBEGIN, INTERVAL_NOT_FINITE, InvalidOid, interval::month, MONTHS_PER_YEAR, NonFiniteIntervalPart(), numeric_add_safe(), numeric_in(), ObjectIdGetDatum(), pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, pg_mul_s64_overflow(), PG_RETURN_FLOAT8, PG_RETURN_NULL, PG_RETURN_NUMERIC, 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, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by extract_interval(), and interval_part().

◆ interval_pl()

Datum interval_pl ( PG_FUNCTION_ARGS  )

Definition at line 3522 of file timestamp.c.

3523{
3527
3529
3530 /*
3531 * Handle infinities.
3532 *
3533 * We treat anything that amounts to "infinity - infinity" as an error,
3534 * since the interval type has nothing equivalent to NaN.
3535 */
3537 {
3539 ereport(ERROR,
3541 errmsg("interval out of range")));
3542 else
3544 }
3545 else if (INTERVAL_IS_NOEND(span1))
3546 {
3548 ereport(ERROR,
3550 errmsg("interval out of range")));
3551 else
3553 }
3554 else if (INTERVAL_NOT_FINITE(span2))
3555 memcpy(result, span2, sizeof(Interval));
3556 else
3558
3560}

References ereport, errcode(), errmsg, ERROR, fb(), finite_interval_pl(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, INTERVAL_NOBEGIN, INTERVAL_NOEND, INTERVAL_NOT_FINITE, memcpy(), palloc_object, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, and result.

Referenced by in_range_interval_interval(), and interval_lerp().

◆ interval_recv()

Datum interval_recv ( PG_FUNCTION_ARGS  )

Definition at line 998 of file timestamp.c.

999{
1001
1002#ifdef NOT_USED
1003 Oid typelem = PG_GETARG_OID(1);
1004#endif
1005 int32 typmod = PG_GETARG_INT32(2);
1007
1009
1011 interval->day = pq_getmsgint(buf, sizeof(interval->day));
1013
1015
1017}
struct StringInfoData * StringInfo
Definition string.h:15

References AdjustIntervalForTypmod(), buf, fb(), interval::month, palloc_object, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_INTERVAL_P, pq_getmsgint(), pq_getmsgint64(), and interval::time.

◆ interval_scale()

Datum interval_scale ( PG_FUNCTION_ARGS  )

Definition at line 1330 of file timestamp.c.

1331{
1333 int32 typmod = PG_GETARG_INT32(1);
1335
1337 *result = *interval;
1338
1339 if (!AdjustIntervalForTypmod(result, typmod, fcinfo->context))
1341
1343}

References AdjustIntervalForTypmod(), palloc_object, PG_GETARG_INT32, PG_GETARG_INTERVAL_P, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, and result.

◆ interval_send()

◆ interval_sign()

◆ interval_smaller()

Datum interval_smaller ( PG_FUNCTION_ARGS  )

Definition at line 3478 of file timestamp.c.

3479{
3483
3484 /* use interval_cmp_internal to be sure this agrees with comparisons */
3486 result = interval1;
3487 else
3488 result = interval2;
3490}

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

◆ interval_sum()

Datum interval_sum ( PG_FUNCTION_ARGS  )

Definition at line 4267 of file timestamp.c.

4268{
4271
4273
4274 /* If there were no non-null inputs, return NULL */
4275 if (state == NULL || IA_TOTAL_COUNT(state) == 0)
4277
4278 /*
4279 * Aggregating infinities that all have the same sign produces infinity
4280 * with that sign. Aggregating infinities with different signs results in
4281 * an error.
4282 */
4283 if (state->pInfcount > 0 && state->nInfcount > 0)
4284 ereport(ERROR,
4286 errmsg("interval out of range")));
4287
4289
4290 if (state->pInfcount > 0)
4292 else if (state->nInfcount > 0)
4294 else
4295 memcpy(result, &state->sumX, sizeof(Interval));
4296
4298}

References ereport, errcode(), errmsg, ERROR, fb(), IA_TOTAL_COUNT, INTERVAL_NOBEGIN, INTERVAL_NOEND, memcpy(), palloc_object, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_INTERVAL_P, PG_RETURN_NULL, and result.

◆ interval_support()

Datum interval_support ( PG_FUNCTION_ARGS  )

Definition at line 1266 of file timestamp.c.

1267{
1269 Node *ret = NULL;
1270
1272 {
1274 FuncExpr *expr = req->fcall;
1275 Node *typmod;
1276
1277 Assert(list_length(expr->args) >= 2);
1278
1279 typmod = (Node *) lsecond(expr->args);
1280
1281 if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
1282 {
1283 Node *source = (Node *) linitial(expr->args);
1285 bool noop;
1286
1287 if (new_typmod < 0)
1288 noop = true;
1289 else
1290 {
1292 int old_least_field;
1293 int new_least_field;
1294 int old_precis;
1295 int new_precis;
1296
1299 if (old_typmod < 0)
1301 else
1304
1305 /*
1306 * Cast is a no-op if least field stays the same or decreases
1307 * while precision stays the same or increases. But
1308 * precision, which is to say, sub-second precision, only
1309 * affects ranges that include SECOND.
1310 */
1312 (old_least_field > 0 /* SECOND */ ||
1315 }
1316 if (noop)
1318 }
1319 }
1320
1321 PG_RETURN_POINTER(ret);
1322}
static int intervaltypmodleastfield(int32 typmod)
Definition timestamp.c:1213
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition nodeFuncs.c:694
static int list_length(const List *l)
Definition pg_list.h:152
static rewind_source * source
Definition pg_rewind.c:89
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
List * args
Definition primnodes.h:801

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

◆ interval_trunc()

Datum interval_trunc ( PG_FUNCTION_ARGS  )

Definition at line 5139 of file timestamp.c.

5140{
5144 int type,
5145 val;
5146 char *lowunits;
5147 struct pg_itm tt,
5148 *tm = &tt;
5149
5151
5154 false);
5155
5156 type = DecodeUnits(0, lowunits, &val);
5157
5158 if (type == UNITS)
5159 {
5161 {
5162 /*
5163 * Errors thrown here for invalid units should exactly match those
5164 * below, else there will be unexpected discrepancies between
5165 * finite- and infinite-input cases.
5166 */
5167 switch (val)
5168 {
5169 case DTK_MILLENNIUM:
5170 case DTK_CENTURY:
5171 case DTK_DECADE:
5172 case DTK_YEAR:
5173 case DTK_QUARTER:
5174 case DTK_MONTH:
5175 case DTK_DAY:
5176 case DTK_HOUR:
5177 case DTK_MINUTE:
5178 case DTK_SECOND:
5179 case DTK_MILLISEC:
5180 case DTK_MICROSEC:
5181 memcpy(result, interval, sizeof(Interval));
5183 break;
5184
5185 default:
5186 ereport(ERROR,
5188 errmsg("unit \"%s\" not supported for type %s",
5190 (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
5191 result = NULL;
5192 }
5193 }
5194
5196 switch (val)
5197 {
5198 case DTK_MILLENNIUM:
5199 /* caution: C division may have negative remainder */
5200 tm->tm_year = (tm->tm_year / 1000) * 1000;
5202 case DTK_CENTURY:
5203 /* caution: C division may have negative remainder */
5204 tm->tm_year = (tm->tm_year / 100) * 100;
5206 case DTK_DECADE:
5207 /* caution: C division may have negative remainder */
5208 tm->tm_year = (tm->tm_year / 10) * 10;
5210 case DTK_YEAR:
5211 tm->tm_mon = 0;
5213 case DTK_QUARTER:
5214 tm->tm_mon = 3 * (tm->tm_mon / 3);
5216 case DTK_MONTH:
5217 tm->tm_mday = 0;
5219 case DTK_DAY:
5220 tm->tm_hour = 0;
5222 case DTK_HOUR:
5223 tm->tm_min = 0;
5225 case DTK_MINUTE:
5226 tm->tm_sec = 0;
5228 case DTK_SECOND:
5229 tm->tm_usec = 0;
5230 break;
5231 case DTK_MILLISEC:
5232 tm->tm_usec = (tm->tm_usec / 1000) * 1000;
5233 break;
5234 case DTK_MICROSEC:
5235 break;
5236
5237 default:
5238 ereport(ERROR,
5240 errmsg("unit \"%s\" not supported for type %s",
5242 (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
5243 }
5244
5245 if (itm2interval(tm, result) != 0)
5246 ereport(ERROR,
5248 errmsg("interval out of range")));
5249 }
5250 else
5251 {
5252 ereport(ERROR,
5254 errmsg("unit \"%s\" not recognized for type %s",
5256 }
5257
5259}
int itm2interval(struct pg_itm *itm, Interval *span)
Definition timestamp.c:2088
#define pg_fallthrough
Definition c.h:161
int errdetail(const char *fmt,...) pg_attribute_printf(1

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, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), format_type_be(), interval2itm(), INTERVAL_NOT_FINITE, itm2interval(), memcpy(), palloc_object, pg_fallthrough, PG_GETARG_INTERVAL_P, PG_GETARG_TEXT_PP, PG_RETURN_INTERVAL_P, result, 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, type, UNITS, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ interval_um()

◆ interval_um_internal()

static void interval_um_internal ( const Interval interval,
Interval result 
)
static

◆ intervaltypmodin()

Datum intervaltypmodin ( PG_FUNCTION_ARGS  )

Definition at line 1048 of file timestamp.c.

1049{
1051 int32 *tl;
1052 int n;
1053 int32 typmod;
1054
1056
1057 /*
1058 * tl[0] - interval range (fields bitmask) tl[1] - precision (optional)
1059 *
1060 * Note we must validate tl[0] even though it's normally guaranteed
1061 * correct by the grammar --- consider SELECT 'foo'::"interval"(1000).
1062 */
1063 if (n > 0)
1064 {
1065 switch (tl[0])
1066 {
1067 case INTERVAL_MASK(YEAR):
1081 /* all OK */
1082 break;
1083 default:
1084 ereport(ERROR,
1086 errmsg("invalid INTERVAL type modifier")));
1087 }
1088 }
1089
1090 if (n == 1)
1091 {
1092 if (tl[0] != INTERVAL_FULL_RANGE)
1094 else
1095 typmod = -1;
1096 }
1097 else if (n == 2)
1098 {
1099 if (tl[1] < 0)
1100 ereport(ERROR,
1102 errmsg("INTERVAL(%d) precision must not be negative",
1103 tl[1])));
1104 if (tl[1] > MAX_INTERVAL_PRECISION)
1105 {
1108 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1111 }
1112 else
1113 typmod = INTERVAL_TYPMOD(tl[1], tl[0]);
1114 }
1115 else
1116 {
1117 ereport(ERROR,
1119 errmsg("invalid INTERVAL type modifier")));
1120 typmod = 0; /* keep compiler quiet */
1121 }
1122
1123 PG_RETURN_INT32(typmod);
1124}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
#define INTERVAL_TYPMOD(p, r)
Definition timestamp.h:80

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

◆ intervaltypmodleastfield()

static int intervaltypmodleastfield ( int32  typmod)
static

Definition at line 1213 of file timestamp.c.

1214{
1215 if (typmod < 0)
1216 return 0; /* SECOND */
1217
1218 switch (INTERVAL_RANGE(typmod))
1219 {
1220 case INTERVAL_MASK(YEAR):
1221 return 5; /* YEAR */
1222 case INTERVAL_MASK(MONTH):
1223 return 4; /* MONTH */
1224 case INTERVAL_MASK(DAY):
1225 return 3; /* DAY */
1226 case INTERVAL_MASK(HOUR):
1227 return 2; /* HOUR */
1228 case INTERVAL_MASK(MINUTE):
1229 return 1; /* MINUTE */
1230 case INTERVAL_MASK(SECOND):
1231 return 0; /* SECOND */
1233 return 4; /* MONTH */
1235 return 2; /* HOUR */
1237 return 1; /* MINUTE */
1239 return 0; /* SECOND */
1241 return 1; /* MINUTE */
1243 return 0; /* SECOND */
1245 return 0; /* SECOND */
1247 return 0; /* SECOND */
1248 default:
1249 elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1250 break;
1251 }
1252 return 0; /* can't get here, but keep compiler quiet */
1253}

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

Referenced by interval_support().

◆ intervaltypmodout()

Datum intervaltypmodout ( PG_FUNCTION_ARGS  )

Definition at line 1127 of file timestamp.c.

1128{
1129 int32 typmod = PG_GETARG_INT32(0);
1130 char *res = (char *) palloc(64);
1131 int fields;
1132 int precision;
1133 const char *fieldstr;
1134
1135 if (typmod < 0)
1136 {
1137 *res = '\0';
1138 PG_RETURN_CSTRING(res);
1139 }
1140
1141 fields = INTERVAL_RANGE(typmod);
1142 precision = INTERVAL_PRECISION(typmod);
1143
1144 switch (fields)
1145 {
1146 case INTERVAL_MASK(YEAR):
1147 fieldstr = " year";
1148 break;
1149 case INTERVAL_MASK(MONTH):
1150 fieldstr = " month";
1151 break;
1152 case INTERVAL_MASK(DAY):
1153 fieldstr = " day";
1154 break;
1155 case INTERVAL_MASK(HOUR):
1156 fieldstr = " hour";
1157 break;
1158 case INTERVAL_MASK(MINUTE):
1159 fieldstr = " minute";
1160 break;
1161 case INTERVAL_MASK(SECOND):
1162 fieldstr = " second";
1163 break;
1165 fieldstr = " year to month";
1166 break;
1168 fieldstr = " day to hour";
1169 break;
1171 fieldstr = " day to minute";
1172 break;
1174 fieldstr = " day to second";
1175 break;
1177 fieldstr = " hour to minute";
1178 break;
1180 fieldstr = " hour to second";
1181 break;
1183 fieldstr = " minute to second";
1184 break;
1186 fieldstr = "";
1187 break;
1188 default:
1189 elog(ERROR, "invalid INTERVAL typmod: 0x%x", typmod);
1190 fieldstr = "";
1191 break;
1192 }
1193
1194 if (precision != INTERVAL_FULL_PRECISION)
1195 snprintf(res, 64, "%s(%d)", fieldstr, precision);
1196 else
1197 snprintf(res, 64, "%s", fieldstr);
1198
1199 PG_RETURN_CSTRING(res);
1200}
void * palloc(Size size)
Definition mcxt.c:1390
#define snprintf
Definition port.h:261

References DAY, elog, ERROR, fb(), 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.

◆ isoweek2date()

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

Definition at line 5293 of file timestamp.c.

5294{
5295 j2date(isoweek2j(*year, woy), year, mon, mday);
5296}
void j2date(int jd, int *year, int *month, int *day)
Definition datetime.c:322

References fb(), isoweek2j(), and j2date().

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

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 5272 of file timestamp.c.

5273{
5274 int day0,
5275 day4;
5276
5277 /* fourth day of current year */
5278 day4 = date2j(year, 1, 4);
5279
5280 /* day0 == offset to first day of week (Monday) */
5281 day0 = j2day(day4 - 1);
5282
5283 return ((week - 1) * 7) + (day4 - day0);
5284}

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

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

◆ isoweekdate2date()

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

Definition at line 5307 of file timestamp.c.

5308{
5309 int jday;
5310
5311 jday = isoweek2j(*year, isoweek);
5312 /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
5313 if (wday > 1)
5314 jday += wday - 2;
5315 else
5316 jday += 6;
5317 j2date(jday, year, mon, mday);
5318}

References fb(), isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 2088 of file timestamp.c.

2089{
2090 int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2091
2093 return -1;
2094 span->month = (int32) total_months;
2095 span->day = itm->tm_mday;
2097 &span->time))
2098 return -1;
2099 /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2100 if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2101 &span->time))
2102 return -1;
2103 if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2104 &span->time))
2105 return -1;
2106 if (pg_add_s64_overflow(span->time, itm->tm_usec,
2107 &span->time))
2108 return -1;
2110 return -1;
2111 return 0;
2112}

References fb(), INTERVAL_NOT_FINITE, MONTHS_PER_YEAR, pg_add_s64_overflow(), pg_mul_s64_overflow(), USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by interval_trunc(), timestamp_age(), and timestamptz_age().

◆ itmin2interval()

int itmin2interval ( struct pg_itm_in itm_in,
Interval span 
)

Definition at line 2127 of file timestamp.c.

2128{
2129 int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2130
2132 return -1;
2133 span->month = (int32) total_months;
2134 span->day = itm_in->tm_mday;
2135 span->time = itm_in->tm_usec;
2136 return 0;
2137}

References fb(), and MONTHS_PER_YEAR.

Referenced by interval_in(), pg_timezone_abbrevs_abbrevs(), pg_timezone_abbrevs_zone(), and pg_timezone_names().

◆ lookup_timezone()

static pg_tz * lookup_timezone ( text zone)
static

Definition at line 556 of file timestamp.c.

557{
558 char tzname[TZ_STRLEN_MAX + 1];
559
561
563}
pg_tz * DecodeTimezoneNameToTz(const char *tzname)
Definition datetime.c:3365
#define TZ_STRLEN_MAX
Definition pgtime.h:54
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition varlena.c:248

References DecodeTimezoneNameToTz(), fb(), text_to_cstring_buffer(), and TZ_STRLEN_MAX.

Referenced by generate_series_timestamptz_internal(), timestamptz_mi_interval_at_zone(), timestamptz_pl_interval_at_zone(), and timestamptz_trunc_zone().

◆ make_interval()

Datum make_interval ( PG_FUNCTION_ARGS  )

Definition at line 1533 of file timestamp.c.

1534{
1541 double secs = PG_GETARG_FLOAT8(6);
1543
1544 /*
1545 * Reject out-of-range inputs. We reject any input values that cause
1546 * integer overflow of the corresponding interval fields.
1547 */
1548 if (isinf(secs) || isnan(secs))
1549 goto out_of_range;
1550
1552
1553 /* years and months -> months */
1555 pg_add_s32_overflow(result->month, months, &result->month))
1556 goto out_of_range;
1557
1558 /* weeks and days -> days */
1560 pg_add_s32_overflow(result->day, days, &result->day))
1561 goto out_of_range;
1562
1563 /* hours and mins -> usecs (cannot overflow 64-bit) */
1565
1566 /* secs -> usecs */
1568 if (!FLOAT8_FITS_IN_INT64(secs) ||
1569 pg_add_s64_overflow(result->time, (int64) secs, &result->time))
1570 goto out_of_range;
1571
1572 /* make sure that the result is finite */
1574 goto out_of_range;
1575
1577
1579 ereport(ERROR,
1581 errmsg("interval out of range"));
1582
1583 PG_RETURN_NULL(); /* keep compiler quiet */
1584}
const char *const months[]
Definition datetime.c:82
#define DAYS_PER_WEEK
Definition timestamp.h:117
static float8 float8_mul(const float8 val1, const float8 val2)
Definition float.h:192
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:187

References days, DAYS_PER_WEEK, ereport, errcode(), errmsg, ERROR, fb(), FLOAT8_FITS_IN_INT64, float8_mul(), INTERVAL_NOT_FINITE, months, MONTHS_PER_YEAR, palloc_object, pg_add_s32_overflow(), pg_add_s64_overflow(), PG_GETARG_FLOAT8, PG_GETARG_INT32, pg_mul_s32_overflow(), PG_RETURN_INTERVAL_P, PG_RETURN_NULL, result, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

◆ make_timestamp()

Datum make_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 641 of file timestamp.c.

642{
643 int32 year = PG_GETARG_INT32(0);
644 int32 month = PG_GETARG_INT32(1);
647 int32 min = PG_GETARG_INT32(4);
648 float8 sec = PG_GETARG_FLOAT8(5);
650
651 result = make_timestamp_internal(year, month, mday,
652 hour, min, sec);
653
655}
static Timestamp make_timestamp_internal(int year, int month, int day, int hour, int min, double sec)
Definition timestamp.c:570

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

◆ make_timestamp_internal()

static Timestamp make_timestamp_internal ( int  year,
int  month,
int  day,
int  hour,
int  min,
double  sec 
)
static

Definition at line 570 of file timestamp.c.

572{
573 struct pg_tm tm;
575 TimeOffset time;
576 int dterr;
577 bool bc = false;
579
580 tm.tm_year = year;
581 tm.tm_mon = month;
582 tm.tm_mday = day;
583
584 /* Handle negative years as BC */
585 if (tm.tm_year < 0)
586 {
587 bc = true;
588 tm.tm_year = -tm.tm_year;
589 }
590
591 dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
592
593 if (dterr != 0)
596 errmsg("date field value out of range: %d-%02d-%02d",
597 year, month, day)));
598
602 errmsg("date out of range: %d-%02d-%02d",
603 year, month, day)));
604
606
607 /* Check for time overflow */
608 if (float_time_overflows(hour, min, sec))
611 errmsg("time field value out of range: %d:%02d:%02g",
612 hour, min, sec)));
613
614 /* This should match tm2time */
615 time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
617
622 errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
623 year, month, day,
624 hour, min, sec)));
625
626 /* final range check catches just-out-of-range timestamps */
630 errmsg("timestamp out of range: %d-%02d-%02d %d:%02d:%02g",
631 year, month, day,
632 hour, min, sec)));
633
634 return result;
635}
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition datetime.c:2573
#define unlikely(x)
Definition c.h:438
#define MINS_PER_HOUR
Definition timestamp.h:129
#define IS_VALID_JULIAN(y, m, d)
Definition timestamp.h:227
#define SECS_PER_MINUTE
Definition timestamp.h:128
bool float_time_overflows(int hour, int min, double sec)
Definition date.c:1555
#define DTK_DATE_M
Definition datetime.h:191
long date
Definition pgtypes_date.h:9

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

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

◆ make_timestamptz()

Datum make_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 661 of file timestamp.c.

662{
663 int32 year = PG_GETARG_INT32(0);
664 int32 month = PG_GETARG_INT32(1);
667 int32 min = PG_GETARG_INT32(4);
668 float8 sec = PG_GETARG_FLOAT8(5);
670
671 result = make_timestamp_internal(year, month, mday,
672 hour, min, sec);
673
675}
static TimestampTz timestamp2timestamptz(Timestamp timestamp)
Definition timestamp.c:6529

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

◆ make_timestamptz_at_timezone()

Datum make_timestamptz_at_timezone ( PG_FUNCTION_ARGS  )

Definition at line 682 of file timestamp.c.

683{
684 int32 year = PG_GETARG_INT32(0);
685 int32 month = PG_GETARG_INT32(1);
688 int32 min = PG_GETARG_INT32(4);
689 float8 sec = PG_GETARG_FLOAT8(5);
693 struct pg_tm tt;
694 int tz;
695 fsec_t fsec;
696
698 hour, min, sec);
699
700 if (timestamp2tm(timestamp, NULL, &tt, &fsec, NULL, NULL) != 0)
703 errmsg("timestamp out of range")));
704
706
707 result = dt2local(timestamp, -tz);
708
712 errmsg("timestamp out of range")));
713
715}
static Timestamp dt2local(Timestamp dt, int timezone)
Definition timestamp.c:2146
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition timestamp.c:1918
static int parse_sane_timezone(struct pg_tm *tm, text *zone)
Definition timestamp.c:487
int32 fsec_t
Definition timestamp.h:41
int64 timestamp

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

◆ makeIntervalAggState()

static IntervalAggState * makeIntervalAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 3986 of file timestamp.c.

3987{
3989 MemoryContext agg_context;
3991
3992 if (!AggCheckCallContext(fcinfo, &agg_context))
3993 elog(ERROR, "aggregate function called in non-aggregate context");
3994
3995 old_context = MemoryContextSwitchTo(agg_context);
3996
3998
4000
4001 return state;
4002}

References AggCheckCallContext(), elog, ERROR, fb(), MemoryContextSwitchTo(), and palloc0_object.

Referenced by interval_avg_accum(), and interval_avg_combine().

◆ mul_d_interval()

Datum mul_d_interval ( PG_FUNCTION_ARGS  )

Definition at line 3747 of file timestamp.c.

3748{
3749 /* Args are float8 and Interval *, but leave them as generic Datum */
3752
3754}
Datum interval_mul(PG_FUNCTION_ARGS)
Definition timestamp.c:3627

References DirectFunctionCall2, fb(), interval_mul(), and PG_GETARG_DATUM.

◆ NonFiniteIntervalPart()

static float8 NonFiniteIntervalPart ( int  type,
int  unit,
char lowunits,
bool  isNegative 
)
static

Definition at line 6069 of file timestamp.c.

6070{
6071 if ((type != UNITS) && (type != RESERV))
6072 ereport(ERROR,
6074 errmsg("unit \"%s\" not recognized for type %s",
6076
6077 switch (unit)
6078 {
6079 /* Oscillating units */
6080 case DTK_MICROSEC:
6081 case DTK_MILLISEC:
6082 case DTK_SECOND:
6083 case DTK_MINUTE:
6084 case DTK_WEEK:
6085 case DTK_MONTH:
6086 case DTK_QUARTER:
6087 return 0.0;
6088
6089 /* Monotonically-increasing units */
6090 case DTK_HOUR:
6091 case DTK_DAY:
6092 case DTK_YEAR:
6093 case DTK_DECADE:
6094 case DTK_CENTURY:
6095 case DTK_MILLENNIUM:
6096 case DTK_EPOCH:
6097 if (isNegative)
6098 return -get_float8_infinity();
6099 else
6100 return get_float8_infinity();
6101
6102 default:
6103 ereport(ERROR,
6105 errmsg("unit \"%s\" not supported for type %s",
6107 return 0.0; /* keep compiler quiet */
6108 }
6109}
static float8 get_float8_infinity(void)
Definition float.h:68

References 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_WEEK, DTK_YEAR, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), get_float8_infinity(), RESERV, type, and UNITS.

Referenced by interval_part_common().

◆ NonFiniteTimestampTzPart()

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

Definition at line 5457 of file timestamp.c.

5459{
5460 if ((type != UNITS) && (type != RESERV))
5461 ereport(ERROR,
5463 errmsg("unit \"%s\" not recognized for type %s",
5464 lowunits,
5466
5467 switch (unit)
5468 {
5469 /* Oscillating units */
5470 case DTK_MICROSEC:
5471 case DTK_MILLISEC:
5472 case DTK_SECOND:
5473 case DTK_MINUTE:
5474 case DTK_HOUR:
5475 case DTK_DAY:
5476 case DTK_MONTH:
5477 case DTK_QUARTER:
5478 case DTK_WEEK:
5479 case DTK_DOW:
5480 case DTK_ISODOW:
5481 case DTK_DOY:
5482 case DTK_TZ:
5483 case DTK_TZ_MINUTE:
5484 case DTK_TZ_HOUR:
5485 return 0.0;
5486
5487 /* Monotonically-increasing units */
5488 case DTK_YEAR:
5489 case DTK_DECADE:
5490 case DTK_CENTURY:
5491 case DTK_MILLENNIUM:
5492 case DTK_JULIAN:
5493 case DTK_ISOYEAR:
5494 case DTK_EPOCH:
5495 if (isNegative)
5496 return -get_float8_infinity();
5497 else
5498 return get_float8_infinity();
5499
5500 default:
5501 ereport(ERROR,
5503 errmsg("unit \"%s\" not supported for type %s",
5504 lowunits,
5506 return 0.0; /* keep compiler quiet */
5507 }
5508}
#define DTK_JULIAN
Definition datetime.h:173
#define DTK_TZ_HOUR
Definition datetime.h:177
#define DTK_TZ_MINUTE
Definition datetime.h:178
#define DTK_ISODOW
Definition datetime.h:180
#define DTK_ISOYEAR
Definition datetime.h:179
#define DTK_DOY
Definition datetime.h:176
#define DTK_TZ
Definition datetime.h:146
#define DTK_DOW
Definition datetime.h:175

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, fb(), format_type_be(), get_float8_infinity(), RESERV, type, and UNITS.

Referenced by timestamp_part_common(), and timestamptz_part_common().

◆ now()

Definition at line 1613 of file timestamp.c.

References GetCurrentTransactionStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

Referenced by advanceConnectionState(), ApplyLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), CalculateCopyStreamSleeptime(), CheckArchiveTimeout(), CheckpointerMain(), DetermineSlotInvalidationCause(), doLog(), doRetry(), dumpTimestamp(), enable_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), executeMetaCommand(), flushAndSendFeedback(), get_candidate_xid(), get_role_password(), handle_sig_alarm(), HandleCopyStream(), has_startup_progress_timeout_expired(), initialize_prng(), InvalidatePossiblyObsoleteSlot(), IoWorkerMain(), IsCheckpointOnSchedule(), LagTrackerRead(), launch_sync_worker(), launch_worker(), libpqsrv_cancel(), LockBufferForCleanup(), logicalrep_worker_launch(), LogicalRepApplyLoop(), LogRecoveryConflict(), make_output_dirs(), maybe_start_bgworkers(), maybe_start_io_workers(), mxid_age(), OutputFsync(), pg_get_ticks_system(), pg_time_now(), pg_time_now_lazy(), pg_timezone_abbrevs_abbrevs(), pg_timezone_abbrevs_zone(), pgfdw_cancel_query(), pgfdw_finish_abort_cleanup(), pgfdw_get_cleanup_result(), pgstat_report_archiver(), pgstat_report_stat(), PQsocketPoll(), pqTraceFormatTimestamp(), printProgressReport(), printVerboseErrorMessages(), ProcessKeepaliveMsg(), ProcessStandbyReplyMessage(), processXactStats(), ProcSleep(), progress_report(), progress_report(), progress_report(), progress_report(), progress_report(), ReplicationSlotRelease(), RequestXLogStreaming(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), RestoreSlotFromDisk(), schedule_alarm(), send_feedback(), sendFeedback(), sendFeedback(), ServerLoop(), set_next_rotation_time(), should_stop_conflict_info_retention(), StreamLogicalLog(), SysLoggerMain(), threadRun(), timetz_zone(), update_synced_slots_inactive_since(), wait_for_local_flush(), WaitForWALToBecomeAvailable(), WalRcvComputeNextWakeup(), WalRcvRunning(), WalRcvStreaming(), WalReceiverMain(), WalSndCheckShutdownTimeout(), WalSndComputeSleeptime(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), xid_age(), XLogBackgroundFlush(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ overlaps_timestamp()

Datum overlaps_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2685 of file timestamp.c.

2686{
2687 /*
2688 * The arguments are Timestamps, but we leave them as generic Datums to
2689 * avoid unnecessary conversions between value and reference forms --- not
2690 * to mention possible dereferences of null pointers.
2691 */
2696 bool ts1IsNull = PG_ARGISNULL(0);
2697 bool te1IsNull = PG_ARGISNULL(1);
2698 bool ts2IsNull = PG_ARGISNULL(2);
2699 bool te2IsNull = PG_ARGISNULL(3);
2700
2701#define TIMESTAMP_GT(t1,t2) \
2702 DatumGetBool(DirectFunctionCall2(timestamp_gt,t1,t2))
2703#define TIMESTAMP_LT(t1,t2) \
2704 DatumGetBool(DirectFunctionCall2(timestamp_lt,t1,t2))
2705
2706 /*
2707 * If both endpoints of interval 1 are null, the result is null (unknown).
2708 * If just one endpoint is null, take ts1 as the non-null one. Otherwise,
2709 * take ts1 as the lesser endpoint.
2710 */
2711 if (ts1IsNull)
2712 {
2713 if (te1IsNull)
2715 /* swap null for non-null */
2716 ts1 = te1;
2717 te1IsNull = true;
2718 }
2719 else if (!te1IsNull)
2720 {
2721 if (TIMESTAMP_GT(ts1, te1))
2722 {
2723 Datum tt = ts1;
2724
2725 ts1 = te1;
2726 te1 = tt;
2727 }
2728 }
2729
2730 /* Likewise for interval 2. */
2731 if (ts2IsNull)
2732 {
2733 if (te2IsNull)
2735 /* swap null for non-null */
2736 ts2 = te2;
2737 te2IsNull = true;
2738 }
2739 else if (!te2IsNull)
2740 {
2741 if (TIMESTAMP_GT(ts2, te2))
2742 {
2743 Datum tt = ts2;
2744
2745 ts2 = te2;
2746 te2 = tt;
2747 }
2748 }
2749
2750 /*
2751 * At this point neither ts1 nor ts2 is null, so we can consider three
2752 * cases: ts1 > ts2, ts1 < ts2, ts1 = ts2
2753 */
2754 if (TIMESTAMP_GT(ts1, ts2))
2755 {
2756 /*
2757 * This case is ts1 < te2 OR te1 < te2, which may look redundant but
2758 * in the presence of nulls it's not quite completely so.
2759 */
2760 if (te2IsNull)
2762 if (TIMESTAMP_LT(ts1, te2))
2763 PG_RETURN_BOOL(true);
2764 if (te1IsNull)
2766
2767 /*
2768 * If te1 is not null then we had ts1 <= te1 above, and we just found
2769 * ts1 >= te2, hence te1 >= te2.
2770 */
2771 PG_RETURN_BOOL(false);
2772 }
2773 else if (TIMESTAMP_LT(ts1, ts2))
2774 {
2775 /* This case is ts2 < te1 OR te2 < te1 */
2776 if (te1IsNull)
2778 if (TIMESTAMP_LT(ts2, te1))
2779 PG_RETURN_BOOL(true);
2780 if (te2IsNull)
2782
2783 /*
2784 * If te2 is not null then we had ts2 <= te2 above, and we just found
2785 * ts2 >= te1, hence te2 >= te1.
2786 */
2787 PG_RETURN_BOOL(false);
2788 }
2789 else
2790 {
2791 /*
2792 * For ts1 = ts2 the spec says te1 <> te2 OR te1 = te2, which is a
2793 * rather silly way of saying "true if both are non-null, else null".
2794 */
2795 if (te1IsNull || te2IsNull)
2797 PG_RETURN_BOOL(true);
2798 }
2799
2800#undef TIMESTAMP_GT
2801#undef TIMESTAMP_LT
2802}
#define TIMESTAMP_GT(t1, t2)
#define TIMESTAMP_LT(t1, t2)

References fb(), PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_BOOL, PG_RETURN_NULL, TIMESTAMP_GT, and TIMESTAMP_LT.

◆ parse_sane_timezone()

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

Definition at line 487 of file timestamp.c.

488{
489 char tzname[TZ_STRLEN_MAX + 1];
490 int dterr;
491 int tz;
492
494
495 /*
496 * Look up the requested timezone. First we try to interpret it as a
497 * numeric timezone specification; if DecodeTimezone decides it doesn't
498 * like the format, we try timezone abbreviations and names.
499 *
500 * Note pg_tzset happily parses numeric input that DecodeTimezone would
501 * reject. To avoid having it accept input that would otherwise be seen
502 * as invalid, it's enough to disallow having a digit in the first
503 * position of our input string.
504 */
505 if (isdigit((unsigned char) *tzname))
508 errmsg("invalid input syntax for type %s: \"%s\"",
509 "numeric time zone", tzname),
510 errhint("Numeric time zones must have \"-\" or \"+\" as first character.")));
511
513 if (dterr != 0)
514 {
515 int type,
516 val;
517 pg_tz *tzp;
518
522 errmsg("numeric time zone \"%s\" out of range", tzname)));
523 else if (dterr != DTERR_BAD_FORMAT)
526 errmsg("time zone \"%s\" not recognized", tzname)));
527
529
531 {
532 /* fixed-offset abbreviation */
533 tz = -val;
534 }
535 else if (type == TZNAME_DYNTZ)
536 {
537 /* dynamic-offset abbreviation, resolve using specified time */
539 }
540 else
541 {
542 /* full zone name */
543 tz = DetermineTimeZoneOffset(tm, tzp);
544 }
545 }
546
547 return tz;
548}
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition datetime.c:1608
int DecodeTimezone(const char *str, int *tzp)
Definition datetime.c:3075
int DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
Definition datetime.c:3309
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition datetime.c:1771
int errhint(const char *fmt,...) pg_attribute_printf(1
#define TZNAME_FIXED_OFFSET
Definition datetime.h:301
#define TZNAME_DYNTZ
Definition datetime.h:302
#define DTERR_TZDISP_OVERFLOW
Definition datetime.h:288
Definition pgtz.h:66

References DecodeTimezone(), DecodeTimezoneName(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), DTERR_BAD_FORMAT, DTERR_TZDISP_OVERFLOW, ereport, errcode(), errhint(), errmsg, ERROR, fb(), text_to_cstring_buffer(), tm, type, TZ_STRLEN_MAX, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, and val.

Referenced by make_timestamptz_at_timezone().

◆ pg_conf_load_time()

Datum pg_conf_load_time ( PG_FUNCTION_ARGS  )

Definition at line 1637 of file timestamp.c.

1638{
1640}
TimestampTz PgReloadTime
Definition timestamp.c:48

References PG_RETURN_TIMESTAMPTZ, and PgReloadTime.

◆ pg_postmaster_start_time()

Datum pg_postmaster_start_time ( PG_FUNCTION_ARGS  )

Definition at line 1631 of file timestamp.c.

1632{
1634}
TimestampTz PgStartTime
Definition timestamp.c:45

References PG_RETURN_TIMESTAMPTZ, and PgStartTime.

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2202 of file timestamp.c.

2203{
2204 Timestamp dt;
2205 struct pg_tm tt,
2206 *tm = &tt;
2207
2209 /* we don't bother to test for failure ... */
2210 tm2timestamp(tm, 0, NULL, &dt);
2211
2212 return dt;
2213} /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition timestamp.c:2180
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition timestamp.c:2015

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

Referenced by dttofmtasc_replace(), PGTYPEStimestamp_from_asc(), timestamp_in(), timestamp_part_common(), timestamptz_in(), and timestamptz_part_common().

◆ statement_timestamp()

Datum statement_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 1619 of file timestamp.c.

1620{
1622}
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition xact.c:881

References GetCurrentStatementStartTimestamp(), and PG_RETURN_TIMESTAMPTZ.

◆ time2t()

static TimeOffset time2t ( const int  hour,
const int  min,
const int  sec,
const fsec_t  fsec 
)
static

Definition at line 2140 of file timestamp.c.

2141{
2142 return (((((hour * MINS_PER_HOUR) + min) * SECS_PER_MINUTE) + sec) * USECS_PER_SEC) + fsec;
2143}

References fb(), MINS_PER_HOUR, SECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by tm2timestamp(), and tm2timestamp().

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

◆ timeofday()

Datum timeofday ( PG_FUNCTION_ARGS  )

Definition at line 1695 of file timestamp.c.

1696{
1697 struct timeval tp;
1698 pg_time_t tt;
1699 struct pg_tm *tm;
1700 char part1[128];
1701 char part2[128];
1702 char buf[128 + 128 + 10];
1703
1704 gettimeofday(&tp, NULL);
1705 tt = (pg_time_t) tp.tv_sec;
1707
1708 pg_strftime(part1, sizeof(part1), "%a %b %d %H:%M:%S", tm);
1709 pg_strftime(part2, sizeof(part2), "%Y %Z", tm);
1710 snprintf(buf, sizeof(buf), "%s.%06d %s", part1, (int) tp.tv_usec, part2);
1711
1713}
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
size_t pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t)
Definition strftime.c:135
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition localtime.c:1347
text * cstring_to_text(const char *s)
Definition varlena.c:184

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

◆ timestamp2timestamptz()

static TimestampTz timestamp2timestamptz ( Timestamp  timestamp)
static

Definition at line 6529 of file timestamp.c.

6530{
6532}
TimestampTz timestamp2timestamptz_safe(Timestamp timestamp, Node *escontext)
Definition timestamp.c:6493

References fb(), and timestamp2timestamptz_safe().

Referenced by make_timestamptz().

◆ timestamp2timestamptz_safe()

TimestampTz timestamp2timestamptz_safe ( Timestamp  timestamp,
Node escontext 
)

Definition at line 6493 of file timestamp.c.

6494{
6496 struct pg_tm tt,
6497 *tm = &tt;
6498 fsec_t fsec;
6499 int tz;
6500
6502 return timestamp;
6503
6504 /* timestamp2tm should not fail on valid timestamps, but cope */
6505 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
6506 {
6508
6509 result = dt2local(timestamp, -tz);
6510
6512 return result;
6513 }
6514
6515 if (timestamp < 0)
6517 else
6519
6520 ereturn(escontext, result,
6522 errmsg("timestamp out of range")));
6523}

References DetermineTimeZoneOffset(), dt2local(), ereturn, errcode(), errmsg, fb(), IS_VALID_TIMESTAMP, result, session_timezone, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, and tm.

Referenced by cvt_timestamp_timestamptz(), timestamp2timestamptz(), timestamp_cmp_timestamptz_internal(), and timestamp_timestamptz().

◆ timestamp2tm()

int timestamp2tm ( Timestamp  dt,
int tzp,
struct pg_tm tm,
fsec_t fsec,
const char **  tzn,
pg_tz attimezone 
)

Definition at line 1918 of file timestamp.c.

1919{
1921 Timestamp time;
1923
1924 /* Use session timezone if caller asks for default */
1925 if (attimezone == NULL)
1926 attimezone = session_timezone;
1927
1928 time = dt;
1929 TMODULO(time, date, USECS_PER_DAY);
1930
1931 if (time < INT64CONST(0))
1932 {
1933 time += USECS_PER_DAY;
1934 date -= 1;
1935 }
1936
1937 /* add offset to go from J2000 back to standard Julian date */
1939
1940 /* Julian day routine does not work for negative Julian days */
1942 return -1;
1943
1944 j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1945 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1946
1947 /* Done if no TZ conversion wanted */
1948 if (tzp == NULL)
1949 {
1950 tm->tm_isdst = -1;
1951 tm->tm_gmtoff = 0;
1952 tm->tm_zone = NULL;
1953 if (tzn != NULL)
1954 *tzn = NULL;
1955 return 0;
1956 }
1957
1958 /*
1959 * If the time falls within the range of pg_time_t, use pg_localtime() to
1960 * rotate to the local time zone.
1961 *
1962 * First, convert to an integral timestamp, avoiding possibly
1963 * platform-specific roundoff-in-wrong-direction errors, and adjust to
1964 * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1965 * coding avoids hardwiring any assumptions about the width of pg_time_t,
1966 * so it should behave sanely on machines without int64.
1967 */
1968 dt = (dt - *fsec) / USECS_PER_SEC +
1970 utime = (pg_time_t) dt;
1971 if ((Timestamp) utime == dt)
1972 {
1973 struct pg_tm *tx = pg_localtime(&utime, attimezone);
1974
1975 tm->tm_year = tx->tm_year + 1900;
1976 tm->tm_mon = tx->tm_mon + 1;
1977 tm->tm_mday = tx->tm_mday;
1978 tm->tm_hour = tx->tm_hour;
1979 tm->tm_min = tx->tm_min;
1980 tm->tm_sec = tx->tm_sec;
1981 tm->tm_isdst = tx->tm_isdst;
1982 tm->tm_gmtoff = tx->tm_gmtoff;
1983 tm->tm_zone = tx->tm_zone;
1984 *tzp = -tm->tm_gmtoff;
1985 if (tzn != NULL)
1986 *tzn = tm->tm_zone;
1987 }
1988 else
1989 {
1990 /*
1991 * When out of range of pg_time_t, treat as GMT
1992 */
1993 *tzp = 0;
1994 /* Mark this as *no* time zone available */
1995 tm->tm_isdst = -1;
1996 tm->tm_gmtoff = 0;
1997 tm->tm_zone = NULL;
1998 if (tzn != NULL)
1999 *tzn = NULL;
2000 }
2001
2002 return 0;
2003}
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition timestamp.c:1891
const char * tm_zone
Definition pgtime.h:46
int tm_isdst
Definition pgtime.h:44
long int tm_gmtoff
Definition pgtime.h:45

References dt2time(), fb(), INT64CONST, j2date(), pg_localtime(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, session_timezone, tm, 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 DetermineTimeZoneAbbrevOffsetTS(), executeDateTimeMethod(), GetCurrentTimeUsec(), JsonEncodeDateTime(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), pg_get_role_ddl_internal(), pg_timezone_names(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_fmt_asc(), PGTYPEStimestamp_to_asc(), timestamp2date_safe(), timestamp2timestamptz_safe(), timestamp_age(), timestamp_out(), timestamp_part_common(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2date_safe(), timestamptz2timestamp_safe(), timestamptz_age(), timestamptz_out(), timestamptz_part_common(), timestamptz_pl_interval_internal(), timestamptz_recv(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), timestamptz_to_str(), timestamptz_trunc_internal(), timestamptz_zone(), and timetz_zone().

◆ timestamp_age()

Datum timestamp_age ( PG_FUNCTION_ARGS  )

Definition at line 4308 of file timestamp.c.

4309{
4313 fsec_t fsec1,
4314 fsec2;
4315 struct pg_itm tt,
4316 *tm = &tt;
4317 struct pg_tm tt1,
4318 *tm1 = &tt1;
4319 struct pg_tm tt2,
4320 *tm2 = &tt2;
4321
4323
4324 /*
4325 * Handle infinities.
4326 *
4327 * We treat anything that amounts to "infinity - infinity" as an error,
4328 * since the interval type has nothing equivalent to NaN.
4329 */
4331 {
4333 ereport(ERROR,
4335 errmsg("interval out of range")));
4336 else
4338 }
4339 else if (TIMESTAMP_IS_NOEND(dt1))
4340 {
4342 ereport(ERROR,
4344 errmsg("interval out of range")));
4345 else
4347 }
4348 else if (TIMESTAMP_IS_NOBEGIN(dt2))
4350 else if (TIMESTAMP_IS_NOEND(dt2))
4352 else if (timestamp2tm(dt1, NULL, tm1, &fsec1, NULL, NULL) == 0 &&
4353 timestamp2tm(dt2, NULL, tm2, &fsec2, NULL, NULL) == 0)
4354 {
4355 /* form the symbolic difference */
4356 tm->tm_usec = fsec1 - fsec2;
4357 tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
4358 tm->tm_min = tm1->tm_min - tm2->tm_min;
4359 tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
4360 tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
4361 tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
4362 tm->tm_year = tm1->tm_year - tm2->tm_year;
4363
4364 /* flip sign if necessary... */
4365 if (dt1 < dt2)
4366 {
4367 tm->tm_usec = -tm->tm_usec;
4368 tm->tm_sec = -tm->tm_sec;
4369 tm->tm_min = -tm->tm_min;
4370 tm->tm_hour = -tm->tm_hour;
4371 tm->tm_mday = -tm->tm_mday;
4372 tm->tm_mon = -tm->tm_mon;
4373 tm->tm_year = -tm->tm_year;
4374 }
4375
4376 /* propagate any negative fields into the next higher field */
4377 while (tm->tm_usec < 0)
4378 {
4379 tm->tm_usec += USECS_PER_SEC;
4380 tm->tm_sec--;
4381 }
4382
4383 while (tm->tm_sec < 0)
4384 {
4386 tm->tm_min--;
4387 }
4388
4389 while (tm->tm_min < 0)
4390 {
4392 tm->tm_hour--;
4393 }
4394
4395 while (tm->tm_hour < 0)
4396 {
4398 tm->tm_mday--;
4399 }
4400
4401 while (tm->tm_mday < 0)
4402 {
4403 if (dt1 < dt2)
4404 {
4405 tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
4406 tm->tm_mon--;
4407 }
4408 else
4409 {
4410 tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
4411 tm->tm_mon--;
4412 }
4413 }
4414
4415 while (tm->tm_mon < 0)
4416 {
4418 tm->tm_year--;
4419 }
4420
4421 /* recover sign if necessary... */
4422 if (dt1 < dt2)
4423 {
4424 tm->tm_usec = -tm->tm_usec;
4425 tm->tm_sec = -tm->tm_sec;
4426 tm->tm_min = -tm->tm_min;
4427 tm->tm_hour = -tm->tm_hour;
4428 tm->tm_mday = -tm->tm_mday;
4429 tm->tm_mon = -tm->tm_mon;
4430 tm->tm_year = -tm->tm_year;
4431 }
4432
4433 if (itm2interval(tm, result) != 0)
4434 ereport(ERROR,
4436 errmsg("interval out of range")));
4437 }
4438 else
4439 ereport(ERROR,
4441 errmsg("timestamp out of range")));
4442
4444}
const int day_tab[2][13]
Definition datetime.c:76
#define HOURS_PER_DAY
Definition timestamp.h:118
#define isleap(y)
Definition datetime.h:273

References day_tab, ereport, errcode(), errmsg, ERROR, fb(), HOURS_PER_DAY, INTERVAL_NOBEGIN, INTERVAL_NOEND, isleap, itm2interval(), MINS_PER_HOUR, MONTHS_PER_YEAR, palloc_object, PG_GETARG_TIMESTAMP, PG_RETURN_INTERVAL_P, result, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, 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, and USECS_PER_SEC.

◆ timestamp_at_local()

Datum timestamp_at_local ( PG_FUNCTION_ARGS  )

Definition at line 6994 of file timestamp.c.

6995{
6996 return timestamp_timestamptz(fcinfo);
6997}
Datum timestamp_timestamptz(PG_FUNCTION_ARGS)
Definition timestamp.c:6470

References timestamp_timestamptz().

◆ timestamp_bin()

Datum timestamp_bin ( PG_FUNCTION_ARGS  )

Definition at line 4610 of file timestamp.c.

4611{
4614 Timestamp origin = PG_GETARG_TIMESTAMP(2);
4617 tm_diff,
4618 tm_modulo,
4619 tm_delta;
4620
4623
4624 if (TIMESTAMP_NOT_FINITE(origin))
4625 ereport(ERROR,
4627 errmsg("origin out of range")));
4628
4630 ereport(ERROR,
4632 errmsg("timestamps cannot be binned into infinite intervals")));
4633
4634 if (stride->month != 0)
4635 ereport(ERROR,
4637 errmsg("timestamps cannot be binned into intervals containing months or years")));
4638
4641 ereport(ERROR,
4643 errmsg("interval out of range")));
4644
4645 if (stride_usecs <= 0)
4646 ereport(ERROR,
4648 errmsg("stride must be greater than zero")));
4649
4651 ereport(ERROR,
4653 errmsg("interval out of range")));
4654
4655 /* These calculations cannot overflow */
4658 result = origin + tm_delta;
4659
4660 /*
4661 * We want to round towards -infinity, not 0, when tm_diff is negative and
4662 * not a multiple of stride_usecs. This adjustment *can* cause overflow,
4663 * since the result might now be out of the range origin .. timestamp.
4664 */
4665 if (tm_modulo < 0)
4666 {
4669 ereport(ERROR,
4671 errmsg("timestamp out of range")));
4672 }
4673
4675}

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, pg_mul_s64_overflow(), PG_RETURN_TIMESTAMP, pg_sub_s64_overflow(), result, TIMESTAMP_NOT_FINITE, unlikely, and USECS_PER_DAY.

◆ timestamp_cmp()

◆ timestamp_cmp_internal()

◆ timestamp_cmp_timestamptz()

Datum timestamp_cmp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 2453 of file timestamp.c.

2454{
2457
2459}
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition timestamp.c:2375

References fb(), PG_GETARG_TIMESTAMP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_INT32, and timestamp_cmp_timestamptz_internal().

◆ timestamp_cmp_timestamptz_internal()

int32 timestamp_cmp_timestamptz_internal ( Timestamp  timestampVal,
TimestampTz  dt2 
)

Definition at line 2375 of file timestamp.c.

2376{
2379
2381 if (escontext.error_occurred)
2382 {
2384 {
2385 /* dt1 is larger than any finite timestamp, but less than infinity */
2386 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2387 }
2389 {
2390 /* dt1 is less than any finite timestamp, but more than -infinity */
2391 return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2392 }
2393 }
2394
2396}
#define timestamptz_cmp_internal(dt1, dt2)
Definition timestamp.h:143

References ErrorSaveContext::error_occurred, fb(), timestamp2timestamptz_safe(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, and timestamptz_cmp_internal.

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

◆ timestamp_decrement()

static Datum timestamp_decrement ( Relation  rel,
Datum  existing,
bool underflow 
)
static

Definition at line 2301 of file timestamp.c.

2302{
2304
2305 if (texisting == PG_INT64_MIN)
2306 {
2307 /* return value is undefined */
2308 *underflow = true;
2309 return (Datum) 0;
2310 }
2311
2312 *underflow = false;
2313 return TimestampGetDatum(texisting - 1);
2314}
#define PG_INT64_MIN
Definition c.h:675

References DatumGetTimestamp(), fb(), PG_INT64_MIN, and TimestampGetDatum().

Referenced by timestamp_skipsupport().

◆ timestamp_eq()

Datum timestamp_eq ( PG_FUNCTION_ARGS  )

Definition at line 2228 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tseq().

◆ timestamp_eq_timestamptz()

◆ timestamp_finite()

Datum timestamp_finite ( PG_FUNCTION_ARGS  )

◆ timestamp_ge()

Datum timestamp_ge ( PG_FUNCTION_ARGS  )

Definition at line 2273 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsge().

◆ timestamp_ge_timestamptz()

◆ timestamp_gt()

Datum timestamp_gt ( PG_FUNCTION_ARGS  )

Definition at line 2255 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsgt().

◆ timestamp_gt_timestamptz()

◆ timestamp_hash()

Datum timestamp_hash ( PG_FUNCTION_ARGS  )

Definition at line 2347 of file timestamp.c.

2348{
2349 return hashint8(fcinfo);
2350}

References hashint8().

◆ timestamp_hash_extended()

Datum timestamp_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2353 of file timestamp.c.

2354{
2355 return hashint8extended(fcinfo);
2356}

References hashint8extended().

◆ timestamp_in()

Datum timestamp_in ( PG_FUNCTION_ARGS  )

Definition at line 158 of file timestamp.c.

159{
160 char *str = PG_GETARG_CSTRING(0);
161#ifdef NOT_USED
162 Oid typelem = PG_GETARG_OID(1);
163#endif
164 int32 typmod = PG_GETARG_INT32(2);
165 Node *escontext = fcinfo->context;
167 fsec_t fsec;
168 struct pg_tm tt,
169 *tm = &tt;
170 int tz;
171 int dtype;
172 int nf;
173 int dterr;
174 char *field[MAXDATEFIELDS];
175 int ftype[MAXDATEFIELDS];
177 DateTimeErrorExtra extra;
178
180 field, ftype, MAXDATEFIELDS, &nf);
181 if (dterr == 0)
182 dterr = DecodeDateTime(field, ftype, nf,
183 &dtype, tm, &fsec, &tz, &extra);
184 if (dterr != 0)
185 {
186 DateTimeParseError(dterr, &extra, str, "timestamp", escontext);
188 }
189
190 switch (dtype)
191 {
192 case DTK_DATE:
193 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
194 ereturn(escontext, (Datum) 0,
196 errmsg("timestamp out of range: \"%s\"", str)));
197 break;
198
199 case DTK_EPOCH:
201 break;
202
203 case DTK_LATE:
205 break;
206
207 case DTK_EARLY:
209 break;
210
211 default:
212 elog(ERROR, "unexpected dtype %d while parsing timestamp \"%s\"",
213 dtype, str);
215 }
216
217 AdjustTimestampForTypmod(&result, typmod, escontext);
218
220}
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
Definition datetime.c:1000
Timestamp SetEpochTimestamp(void)
Definition timestamp.c:2202
#define DTK_DATE
Definition datetime.h:144

References AdjustTimestampForTypmod(), DateTimeParseError(), DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, elog, ereturn, errcode(), errmsg, ERROR, fb(), MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMP, result, SetEpochTimestamp(), str, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, and tm2timestamp().

Referenced by moddatetime().

◆ timestamp_increment()

static Datum timestamp_increment ( Relation  rel,
Datum  existing,
bool overflow 
)
static

Definition at line 2318 of file timestamp.c.

2319{
2321
2322 if (texisting == PG_INT64_MAX)
2323 {
2324 /* return value is undefined */
2325 *overflow = true;
2326 return (Datum) 0;
2327 }
2328
2329 *overflow = false;
2330 return TimestampGetDatum(texisting + 1);
2331}
#define PG_INT64_MAX
Definition c.h:676

References DatumGetTimestamp(), fb(), PG_INT64_MAX, and TimestampGetDatum().

Referenced by timestamp_skipsupport().

◆ timestamp_izone()

Datum timestamp_izone ( PG_FUNCTION_ARGS  )

Definition at line 6411 of file timestamp.c.

6412{
6416 int tz;
6417
6420
6422 ereport(ERROR,
6424 errmsg("interval time zone \"%s\" must be finite",
6426 PointerGetDatum(zone))))));
6427
6428 if (zone->month != 0 || zone->day != 0)
6429 ereport(ERROR,
6431 errmsg("interval time zone \"%s\" must not include months or days",
6433 PointerGetDatum(zone))))));
6434
6435 tz = zone->time / USECS_PER_SEC;
6436
6437 result = dt2local(timestamp, tz);
6438
6440 ereport(ERROR,
6442 errmsg("timestamp out of range")));
6443
6445} /* timestamp_izone() */
Datum interval_out(PG_FUNCTION_ARGS)
Definition timestamp.c:974
static char * DatumGetCString(Datum X)
Definition postgres.h:365

References DatumGetCString(), DirectFunctionCall1, dt2local(), ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_out(), IS_VALID_TIMESTAMP, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMPTZ, PointerGetDatum, result, TIMESTAMP_NOT_FINITE, and USECS_PER_SEC.

◆ timestamp_larger()

Datum timestamp_larger ( PG_FUNCTION_ARGS  )

Definition at line 2825 of file timestamp.c.

2826{
2830
2831 if (timestamp_cmp_internal(dt1, dt2) > 0)
2832 result = dt1;
2833 else
2834 result = dt2;
2836}

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, result, and timestamp_cmp_internal().

◆ timestamp_le()

Datum timestamp_le ( PG_FUNCTION_ARGS  )

Definition at line 2264 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tsle().

◆ timestamp_le_timestamptz()

◆ timestamp_lt()

Datum timestamp_lt ( PG_FUNCTION_ARGS  )

Definition at line 2246 of file timestamp.c.

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_BOOL, and timestamp_cmp_internal().

Referenced by gbt_tslt().

◆ timestamp_lt_timestamptz()

◆ timestamp_mi()

Datum timestamp_mi ( PG_FUNCTION_ARGS  )

Definition at line 2840 of file timestamp.c.

2841{
2845
2847
2848 /*
2849 * Handle infinities.
2850 *
2851 * We treat anything that amounts to "infinity - infinity" as an error,
2852 * since the interval type has nothing equivalent to NaN.
2853 */
2855 {
2857 {
2859 ereport(ERROR,
2861 errmsg("interval out of range")));
2862 else
2864 }
2865 else if (TIMESTAMP_IS_NOEND(dt1))
2866 {
2868 ereport(ERROR,
2870 errmsg("interval out of range")));
2871 else
2873 }
2874 else if (TIMESTAMP_IS_NOBEGIN(dt2))
2876 else /* TIMESTAMP_IS_NOEND(dt2) */
2878
2880 }
2881
2882 if (unlikely(pg_sub_s64_overflow(dt1, dt2, &result->time)))
2883 ereport(ERROR,
2885 errmsg("interval out of range")));
2886
2887 result->month = 0;
2888 result->day = 0;
2889
2890 /*----------
2891 * This is wrong, but removing it breaks a lot of regression tests.
2892 * For example:
2893 *
2894 * test=> SET timezone = 'EST5EDT';
2895 * test=> SELECT
2896 * test-> ('2005-10-30 13:22:00-05'::timestamptz -
2897 * test(> '2005-10-29 13:22:00-04'::timestamptz);
2898 * ?column?
2899 * ----------------
2900 * 1 day 01:00:00
2901 * (1 row)
2902 *
2903 * so adding that to the first timestamp gets:
2904 *
2905 * test=> SELECT
2906 * test-> ('2005-10-29 13:22:00-04'::timestamptz +
2907 * test(> ('2005-10-30 13:22:00-05'::timestamptz -
2908 * test(> '2005-10-29 13:22:00-04'::timestamptz)) at time zone 'EST';
2909 * timezone
2910 * --------------------
2911 * 2005-10-30 14:22:00
2912 * (1 row)
2913 *----------
2914 */
2917
2919}
Datum interval_justify_hours(PG_FUNCTION_ARGS)
Definition timestamp.c:3014

References DatumGetIntervalP(), DirectFunctionCall1, ereport, errcode(), errmsg, ERROR, fb(), interval_justify_hours(), INTERVAL_NOBEGIN, INTERVAL_NOEND, IntervalPGetDatum(), palloc_object, PG_GETARG_TIMESTAMP, PG_RETURN_INTERVAL_P, pg_sub_s64_overflow(), result, TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, TIMESTAMP_NOT_FINITE, and unlikely.

Referenced by gbt_ts_dist(), generate_series_timestamp_support(), ts_dist(), and tstz_dist().

◆ timestamp_mi_interval()

◆ timestamp_ne()

Datum timestamp_ne ( PG_FUNCTION_ARGS  )

◆ timestamp_ne_timestamptz()

◆ timestamp_out()

Datum timestamp_out ( PG_FUNCTION_ARGS  )

Definition at line 227 of file timestamp.c.

228{
230 char *result;
231 struct pg_tm tt,
232 *tm = &tt;
233 fsec_t fsec;
234 char buf[MAXDATELEN + 1];
235
238 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
239 EncodeDateTime(tm, fsec, false, 0, NULL, DateStyle, buf);
240 else
243 errmsg("timestamp out of range")));
244
245 result = pstrdup(buf);
247}
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition datetime.c:4496
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition timestamp.c:1591
int DateStyle
Definition globals.c:127

References buf, DateStyle, EncodeDateTime(), EncodeSpecialTimestamp(), ereport, errcode(), errmsg, ERROR, fb(), MAXDATELEN, PG_GETARG_TIMESTAMP, PG_RETURN_CSTRING, pstrdup(), result, timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

Referenced by ExecGetJsonValueItemString().

◆ timestamp_part()

Datum timestamp_part ( PG_FUNCTION_ARGS  )

Definition at line 5773 of file timestamp.c.

5774{
5775 return timestamp_part_common(fcinfo, false);
5776}

References timestamp_part_common().

◆ timestamp_part_common()

static Datum timestamp_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5515 of file timestamp.c.

5516{
5521 int type,
5522 val;
5523 char *lowunits;
5524 fsec_t fsec;
5525 struct pg_tm tt,
5526 *tm = &tt;
5527
5530 false);
5531
5532 type = DecodeUnits(0, lowunits, &val);
5533 if (type == UNKNOWN_FIELD)
5535
5537 {
5540 false);
5541
5542 if (r != 0.0)
5543 {
5544 if (retnumeric)
5545 {
5546 if (r < 0)
5548 CStringGetDatum("-Infinity"),
5550 Int32GetDatum(-1));
5551 else if (r > 0)
5553 CStringGetDatum("Infinity"),
5555 Int32GetDatum(-1));
5556 }
5557 else
5559 }
5560 else
5562 }
5563
5564 if (type == UNITS)
5565 {
5566 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
5567 ereport(ERROR,
5569 errmsg("timestamp out of range")));
5570
5571 switch (val)
5572 {
5573 case DTK_MICROSEC:
5574 intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
5575 break;
5576
5577 case DTK_MILLISEC:
5578 if (retnumeric)
5579 /*---
5580 * tm->tm_sec * 1000 + fsec / 1000
5581 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5582 */
5584 else
5585 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
5586 break;
5587
5588 case DTK_SECOND:
5589 if (retnumeric)
5590 /*---
5591 * tm->tm_sec + fsec / 1'000'000
5592 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5593 */
5595 else
5596 PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
5597 break;
5598
5599 case DTK_MINUTE:
5600 intresult = tm->tm_min;
5601 break;
5602
5603 case DTK_HOUR:
5604 intresult = tm->tm_hour;
5605 break;
5606
5607 case DTK_DAY:
5608 intresult = tm->tm_mday;
5609 break;
5610
5611 case DTK_MONTH:
5612 intresult = tm->tm_mon;
5613 break;
5614
5615 case DTK_QUARTER:
5616 intresult = (tm->tm_mon - 1) / 3 + 1;
5617 break;
5618
5619 case DTK_WEEK:
5621 break;
5622
5623 case DTK_YEAR:
5624 if (tm->tm_year > 0)
5625 intresult = tm->tm_year;
5626 else
5627 /* there is no year 0, just 1 BC and 1 AD */
5628 intresult = tm->tm_year - 1;
5629 break;
5630
5631 case DTK_DECADE:
5632
5633 /*
5634 * what is a decade wrt dates? let us assume that decade 199
5635 * is 1990 thru 1999... decade 0 starts on year 1 BC, and -1
5636 * is 11 BC thru 2 BC...
5637 */
5638 if (tm->tm_year >= 0)
5639 intresult = tm->tm_year / 10;
5640 else
5641 intresult = -((8 - (tm->tm_year - 1)) / 10);
5642 break;
5643
5644 case DTK_CENTURY:
5645
5646 /* ----
5647 * centuries AD, c>0: year in [ (c-1)* 100 + 1 : c*100 ]
5648 * centuries BC, c<0: year in [ c*100 : (c+1) * 100 - 1]
5649 * there is no number 0 century.
5650 * ----
5651 */
5652 if (tm->tm_year > 0)
5653 intresult = (tm->tm_year + 99) / 100;
5654 else
5655 /* caution: C division may have negative remainder */
5656 intresult = -((99 - (tm->tm_year - 1)) / 100);
5657 break;
5658
5659 case DTK_MILLENNIUM:
5660 /* see comments above. */
5661 if (tm->tm_year > 0)
5662 intresult = (tm->tm_year + 999) / 1000;
5663 else
5664 intresult = -((999 - (tm->tm_year - 1)) / 1000);
5665 break;
5666
5667 case DTK_JULIAN:
5668 if (retnumeric)
5672 NULL),
5673 NULL));
5674 else
5677 tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY);
5678 break;
5679
5680 case DTK_ISOYEAR:
5682 /* Adjust BC years */
5683 if (intresult <= 0)
5684 intresult -= 1;
5685 break;
5686
5687 case DTK_DOW:
5688 case DTK_ISODOW:
5690 if (val == DTK_ISODOW && intresult == 0)
5691 intresult = 7;
5692 break;
5693
5694 case DTK_DOY:
5696 - date2j(tm->tm_year, 1, 1) + 1);
5697 break;
5698
5699 case DTK_TZ:
5700 case DTK_TZ_MINUTE:
5701 case DTK_TZ_HOUR:
5702 default:
5703 ereport(ERROR,
5705 errmsg("unit \"%s\" not supported for type %s",
5707 intresult = 0;
5708 }
5709 }
5710 else if (type == RESERV)
5711 {
5712 switch (val)
5713 {
5714 case DTK_EPOCH:
5716 /* (timestamp - epoch) / 1000000 */
5717 if (retnumeric)
5718 {
5720
5721 if (timestamp < (PG_INT64_MAX + epoch))
5723 else
5724 {
5727 NULL),
5728 int64_to_numeric(1000000),
5729 NULL);
5732 Int32GetDatum(6)));
5733 }
5735 }
5736 else
5737 {
5738 float8 result;
5739
5740 /* try to avoid precision loss in subtraction */
5741 if (timestamp < (PG_INT64_MAX + epoch))
5742 result = (timestamp - epoch) / 1000000.0;
5743 else
5744 result = ((float8) timestamp - epoch) / 1000000.0;
5746 }
5747 break;
5748
5749 default:
5750 ereport(ERROR,
5752 errmsg("unit \"%s\" not supported for type %s",
5754 intresult = 0;
5755 }
5756 }
5757 else
5758 {
5759 ereport(ERROR,
5761 errmsg("unit \"%s\" not recognized for type %s",
5763 intresult = 0;
5764 }
5765
5766 if (retnumeric)
5768 else
5770}
Datum numeric_round(PG_FUNCTION_ARGS)
Definition numeric.c:1528
Numeric numeric_div_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:3157
Numeric numeric_sub_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:2959
static float8 NonFiniteTimestampTzPart(int type, int unit, char *lowunits, bool isNegative, bool isTz)
Definition timestamp.c:5457
int date2isoweek(int year, int mon, int mday)
Definition timestamp.c:5326
static Numeric DatumGetNumeric(Datum X)
Definition numeric.h:64
static Datum NumericGetDatum(Numeric X)
Definition numeric.h:76

References CStringGetDatum(), date2isoweek(), date2isoyear(), date2j(), DatumGetNumeric(), DecodeSpecial(), DecodeUnits(), DirectFunctionCall2, DirectFunctionCall3, downcase_truncate_identifier(), 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, epoch, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), Int32GetDatum(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, InvalidOid, j2day(), MINS_PER_HOUR, NonFiniteTimestampTzPart(), numeric_add_safe(), numeric_div_safe(), numeric_in(), numeric_round(), numeric_sub_safe(), NumericGetDatum(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMP, PG_INT64_MAX, PG_RETURN_FLOAT8, PG_RETURN_NULL, PG_RETURN_NUMERIC, RESERV, result, SECS_PER_DAY, SECS_PER_MINUTE, SetEpochTimestamp(), timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_NOT_FINITE, 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, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by extract_timestamp(), and timestamp_part().

◆ timestamp_pl_interval()

Datum timestamp_pl_interval ( PG_FUNCTION_ARGS  )

Definition at line 3104 of file timestamp.c.

3105{
3109
3110 /*
3111 * Handle infinities.
3112 *
3113 * We treat anything that amounts to "infinity - infinity" as an error,
3114 * since the timestamp type has nothing equivalent to NaN.
3115 */
3117 {
3119 ereport(ERROR,
3121 errmsg("timestamp out of range")));
3122 else
3124 }
3125 else if (INTERVAL_IS_NOEND(span))
3126 {
3128 ereport(ERROR,
3130 errmsg("timestamp out of range")));
3131 else
3133 }
3135 result = timestamp;
3136 else
3137 {
3138 if (span->month != 0)
3139 {
3140 struct pg_tm tt,
3141 *tm = &tt;
3142 fsec_t fsec;
3143
3144 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3145 ereport(ERROR,
3147 errmsg("timestamp out of range")));
3148
3149 if (pg_add_s32_overflow(tm->tm_mon, span->month, &tm->tm_mon))
3150 ereport(ERROR,
3152 errmsg("timestamp out of range")));
3153 if (tm->tm_mon > MONTHS_PER_YEAR)
3154 {
3155 tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
3156 tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
3157 }
3158 else if (tm->tm_mon < 1)
3159 {
3160 tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
3162 }
3163
3164 /* adjust for end of month boundary problems... */
3165 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
3166 tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
3167
3168 if (tm2timestamp(tm, fsec, NULL, &timestamp) != 0)
3169 ereport(ERROR,
3171 errmsg("timestamp out of range")));
3172 }
3173
3174 if (span->day != 0)
3175 {
3176 struct pg_tm tt,
3177 *tm = &tt;
3178 fsec_t fsec;
3179 int julian;
3180
3181 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3182 ereport(ERROR,
3184 errmsg("timestamp out of range")));
3185
3186 /*
3187 * Add days by converting to and from Julian. We need an overflow
3188 * check here since j2date expects a non-negative integer input.
3189 */
3191 if (pg_add_s32_overflow(julian, span->day, &julian) ||
3192 julian < 0)
3193 ereport(ERROR,
3195 errmsg("timestamp out of range")));
3197
3198 if (tm2timestamp(tm, fsec, NULL, &timestamp) != 0)
3199 ereport(ERROR,
3201 errmsg("timestamp out of range")));
3202 }
3203
3205 ereport(ERROR,
3207 errmsg("timestamp out of range")));
3208
3210 ereport(ERROR,
3212 errmsg("timestamp out of range")));
3213
3214 result = timestamp;
3215 }
3216
3218}

References date2j(), day_tab, ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, IS_VALID_TIMESTAMP, isleap, j2date(), MONTHS_PER_YEAR, pg_add_s32_overflow(), pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, result, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by date_pl_interval(), generate_series_timestamp(), in_range_timestamp_interval(), and timestamp_mi_interval().

◆ timestamp_recv()

Datum timestamp_recv ( PG_FUNCTION_ARGS  )

Definition at line 253 of file timestamp.c.

254{
256
257#ifdef NOT_USED
258 Oid typelem = PG_GETARG_OID(1);
259#endif
260 int32 typmod = PG_GETARG_INT32(2);
262 struct pg_tm tt,
263 *tm = &tt;
264 fsec_t fsec;
265
267
268 /* range check: see if timestamp_out would like it */
270 /* ok */ ;
271 else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0 ||
275 errmsg("timestamp out of range")));
276
278
280}

References AdjustTimestampForTypmod(), buf, ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_TIMESTAMP, pq_getmsgint64(), timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

◆ timestamp_scale()

Datum timestamp_scale ( PG_FUNCTION_ARGS  )

Definition at line 341 of file timestamp.c.

342{
344 int32 typmod = PG_GETARG_INT32(1);
346
348
349 if (!AdjustTimestampForTypmod(&result, typmod, fcinfo->context))
351
353}

References AdjustTimestampForTypmod(), PG_GETARG_INT32, PG_GETARG_TIMESTAMP, PG_RETURN_NULL, PG_RETURN_TIMESTAMP, and result.

◆ timestamp_send()

◆ timestamp_skipsupport()

Datum timestamp_skipsupport ( PG_FUNCTION_ARGS  )

Definition at line 2334 of file timestamp.c.

2335{
2337
2342
2344}
static Datum timestamp_increment(Relation rel, Datum existing, bool *overflow)
Definition timestamp.c:2318
static Datum timestamp_decrement(Relation rel, Datum existing, bool *underflow)
Definition timestamp.c:2301
#define PG_RETURN_VOID()
Definition fmgr.h:350
struct SkipSupportData * SkipSupport
Definition skipsupport.h:50
SkipSupportIncDec decrement
Definition skipsupport.h:91
SkipSupportIncDec increment
Definition skipsupport.h:92

References SkipSupportData::decrement, SkipSupportData::high_elem, SkipSupportData::increment, SkipSupportData::low_elem, PG_GETARG_POINTER, PG_INT64_MAX, PG_INT64_MIN, PG_RETURN_VOID, timestamp_decrement(), timestamp_increment(), and TimestampGetDatum().

◆ timestamp_smaller()

Datum timestamp_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2810 of file timestamp.c.

2811{
2815
2816 /* use timestamp_cmp_internal to be sure this agrees with comparisons */
2817 if (timestamp_cmp_internal(dt1, dt2) < 0)
2818 result = dt1;
2819 else
2820 result = dt2;
2822}

References fb(), PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, result, and timestamp_cmp_internal().

◆ timestamp_sortsupport()

Datum timestamp_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 2291 of file timestamp.c.

2292{
2294
2297}
struct SortSupportData * SortSupport
Definition execnodes.h:61
int(* comparator)(Datum x, Datum y, SortSupport ssup)
int ssup_datum_signed_cmp(Datum x, Datum y, SortSupport ssup)
Definition tuplesort.c:3461

References SortSupportData::comparator, PG_GETARG_POINTER, PG_RETURN_VOID, and ssup_datum_signed_cmp().

◆ timestamp_support()

Datum timestamp_support ( PG_FUNCTION_ARGS  )

Definition at line 320 of file timestamp.c.

321{
323 Node *ret = NULL;
324
326 {
328
330 }
331
333}
Node * TemporalSimplify(int32 max_precis, Node *node)
Definition datetime.c:4995

References fb(), IsA, MAX_TIMESTAMP_PRECISION, PG_GETARG_POINTER, PG_RETURN_POINTER, and TemporalSimplify().

◆ timestamp_timestamptz()

Datum timestamp_timestamptz ( PG_FUNCTION_ARGS  )

Definition at line 6470 of file timestamp.c.

6471{
6474
6475 result = timestamp2timestamptz_safe(timestamp, fcinfo->context);
6476 if (SOFT_ERROR_OCCURRED(fcinfo->context))
6478
6480}
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53

References PG_GETARG_TIMESTAMP, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, result, SOFT_ERROR_OCCURRED, and timestamp2timestamptz_safe().

Referenced by executeDateTimeMethod(), and timestamp_at_local().

◆ timestamp_trunc()

Datum timestamp_trunc ( PG_FUNCTION_ARGS  )

Definition at line 4682 of file timestamp.c.

4683{
4687 int type,
4688 val;
4689 char *lowunits;
4690 fsec_t fsec;
4691 struct pg_tm tt,
4692 *tm = &tt;
4693
4696 false);
4697
4698 type = DecodeUnits(0, lowunits, &val);
4699
4700 if (type == UNITS)
4701 {
4703 {
4704 /*
4705 * Errors thrown here for invalid units should exactly match those
4706 * below, else there will be unexpected discrepancies between
4707 * finite- and infinite-input cases.
4708 */
4709 switch (val)
4710 {
4711 case DTK_WEEK:
4712 case DTK_MILLENNIUM:
4713 case DTK_CENTURY:
4714 case DTK_DECADE:
4715 case DTK_YEAR:
4716 case DTK_QUARTER:
4717 case DTK_MONTH:
4718 case DTK_DAY:
4719 case DTK_HOUR:
4720 case DTK_MINUTE:
4721 case DTK_SECOND:
4722 case DTK_MILLISEC:
4723 case DTK_MICROSEC:
4725 break;
4726 default:
4727 ereport(ERROR,
4729 errmsg("unit \"%s\" not supported for type %s",
4731 result = 0;
4732 }
4733 }
4734
4735 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4736 ereport(ERROR,
4738 errmsg("timestamp out of range")));
4739
4740 switch (val)
4741 {
4742 case DTK_WEEK:
4743 {
4744 int woy;
4745
4747
4748 /*
4749 * If it is week 52/53 and the month is January, then the
4750 * week must belong to the previous year. Also, some
4751 * December dates belong to the next year.
4752 */
4753 if (woy >= 52 && tm->tm_mon == 1)
4754 --tm->tm_year;
4756 ++tm->tm_year;
4757 isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
4758 tm->tm_hour = 0;
4759 tm->tm_min = 0;
4760 tm->tm_sec = 0;
4761 fsec = 0;
4762 break;
4763 }
4764 case DTK_MILLENNIUM:
4765 /* see comments in timestamptz_trunc */
4766 if (tm->tm_year > 0)
4767 tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
4768 else
4769 tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
4771 case DTK_CENTURY:
4772 /* see comments in timestamptz_trunc */
4773 if (tm->tm_year > 0)
4774 tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
4775 else
4776 tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
4778 case DTK_DECADE:
4779 /* see comments in timestamptz_trunc */
4780 if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
4781 {
4782 if (tm->tm_year > 0)
4783 tm->tm_year = (tm->tm_year / 10) * 10;
4784 else
4785 tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
4786 }
4788 case DTK_YEAR:
4789 tm->tm_mon = 1;
4791 case DTK_QUARTER:
4792 tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
4794 case DTK_MONTH:
4795 tm->tm_mday = 1;
4797 case DTK_DAY:
4798 tm->tm_hour = 0;
4800 case DTK_HOUR:
4801 tm->tm_min = 0;
4803 case DTK_MINUTE:
4804 tm->tm_sec = 0;
4806 case DTK_SECOND:
4807 fsec = 0;
4808 break;
4809
4810 case DTK_MILLISEC:
4811 fsec = (fsec / 1000) * 1000;
4812 break;
4813
4814 case DTK_MICROSEC:
4815 break;
4816
4817 default:
4818 ereport(ERROR,
4820 errmsg("unit \"%s\" not supported for type %s",
4822 result = 0;
4823 }
4824
4825 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
4826 ereport(ERROR,
4828 errmsg("timestamp out of range")));
4829 }
4830 else
4831 {
4832 ereport(ERROR,
4834 errmsg("unit \"%s\" not recognized for type %s",
4836 result = 0;
4837 }
4838
4840}
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition timestamp.c:5293

References date2isoweek(), 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, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), isoweek2date(), MONTHS_PER_YEAR, pg_fallthrough, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMP, result, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ timestamp_zone()

Datum timestamp_zone ( PG_FUNCTION_ARGS  )

Definition at line 6345 of file timestamp.c.

6346{
6350 int tz;
6351 char tzname[TZ_STRLEN_MAX + 1];
6352 int type,
6353 val;
6354 pg_tz *tzp;
6355 struct pg_tm tm;
6356 fsec_t fsec;
6357
6360
6361 /*
6362 * Look up the requested timezone.
6363 */
6365
6366 type = DecodeTimezoneName(tzname, &val, &tzp);
6367
6369 {
6370 /* fixed-offset abbreviation */
6371 tz = val;
6372 result = dt2local(timestamp, tz);
6373 }
6374 else if (type == TZNAME_DYNTZ)
6375 {
6376 /* dynamic-offset abbreviation, resolve using specified time */
6377 if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, tzp) != 0)
6378 ereport(ERROR,
6380 errmsg("timestamp out of range")));
6382 result = dt2local(timestamp, tz);
6383 }
6384 else
6385 {
6386 /* full zone name, rotate to that zone */
6387 if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, tzp) != 0)
6388 ereport(ERROR,
6390 errmsg("timestamp out of range")));
6391 tz = DetermineTimeZoneOffset(&tm, tzp);
6392 if (tm2timestamp(&tm, fsec, &tz, &result) != 0)
6393 ereport(ERROR,
6395 errmsg("timestamp out of range")));
6396 }
6397
6399 ereport(ERROR,
6401 errmsg("timestamp out of range")));
6402
6404}

References DecodeTimezoneName(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), dt2local(), ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMP, PG_RETURN_TIMESTAMPTZ, result, text_to_cstring_buffer(), timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), type, TZ_STRLEN_MAX, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, and val.

◆ TimestampDifference()

void TimestampDifference ( TimestampTz  start_time,
TimestampTz  stop_time,
long secs,
int microsecs 
)

◆ TimestampDifferenceExceeds()

◆ TimestampDifferenceExceedsSeconds()

bool TimestampDifferenceExceedsSeconds ( TimestampTz  start_time,
TimestampTz  stop_time,
int  threshold_sec 
)

Definition at line 1803 of file timestamp.c.

1806{
1807 long secs;
1808 int usecs;
1809
1810 /* Calculate the difference in seconds */
1812
1813 return (secs >= threshold_sec);
1814}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1729

References fb(), start_time, and TimestampDifference().

Referenced by DetermineSlotInvalidationCause().

◆ TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)

Definition at line 1765 of file timestamp.c.

1766{
1768
1769 /* Deal with zero or negative elapsed time quickly. */
1770 if (start_time >= stop_time)
1771 return 0;
1772 /* To not fail with timestamp infinities, we must detect overflow. */
1774 return (long) INT_MAX;
1775 if (diff >= (INT_MAX * INT64CONST(1000) - 999))
1776 return (long) INT_MAX;
1777 else
1778 return (long) ((diff + 999) / 1000);
1779}

References fb(), INT64CONST, pg_sub_s64_overflow(), and start_time.

Referenced by ApplyLauncherMain(), autoprewarm_main(), bbsink_copystream_archive_contents(), DetermineSleepTime(), do_analyze_rel(), GetReplicationApplyDelay(), GetReplicationTransferLatency(), IoWorkerMain(), libpqsrv_cancel(), LogCheckpointEnd(), pg_stash_advice_worker_main(), pgfdw_get_cleanup_result(), pgstat_report_analyze(), pgstat_report_vacuum(), recoveryApplyDelay(), wait_for_local_flush(), WaitForLSN(), WaitForWalSummarization(), WaitForWALToBecomeAvailable(), WalReceiverMain(), and WalSndComputeSleeptime().

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 6456 of file timestamp.c.

6457{
6458 long offset;
6459
6460 if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
6461 return false;
6462 return true;
6463}
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition localtime.c:1968

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

◆ timestamptypmodin()

Datum timestamptypmodin ( PG_FUNCTION_ARGS  )

Definition at line 297 of file timestamp.c.

298{
300
302}
static int32 anytimestamp_typmodin(bool istz, ArrayType *ta)
Definition timestamp.c:95

References anytimestamp_typmodin(), fb(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timestamptypmodout()

Datum timestamptypmodout ( PG_FUNCTION_ARGS  )

Definition at line 305 of file timestamp.c.

306{
307 int32 typmod = PG_GETARG_INT32(0);
308
310}
static char * anytimestamp_typmodout(bool istz, int32 typmod)
Definition timestamp.c:138

References anytimestamp_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ timestamptz2timestamp()

static Timestamp timestamptz2timestamp ( TimestampTz  timestamp)
static

Definition at line 6555 of file timestamp.c.

6556{
6558}
Timestamp timestamptz2timestamp_safe(TimestampTz timestamp, Node *escontext)
Definition timestamp.c:6571

References fb(), and timestamptz2timestamp_safe().

Referenced by GetSQLLocalTimestamp().

◆ timestamptz2timestamp_safe()

Timestamp timestamptz2timestamp_safe ( TimestampTz  timestamp,
Node escontext 
)

Definition at line 6571 of file timestamp.c.

6572{
6574 struct pg_tm tt,
6575 *tm = &tt;
6576 fsec_t fsec;
6577 int tz;
6578
6580 result = timestamp;
6581 else
6582 {
6583 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
6584 {
6585 if (timestamp < 0)
6587 else
6589
6590 ereturn(escontext, result,
6592 errmsg("timestamp out of range")));
6593 }
6594 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
6595 {
6596 if (timestamp < 0)
6598 else
6600
6601 ereturn(escontext, result,
6603 errmsg("timestamp out of range")));
6604 }
6605 }
6606 return result;
6607}

References ereturn, errcode(), errmsg, fb(), result, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, and tm2timestamp().

Referenced by cvt_timestamptz_timestamp(), timestamptz2timestamp(), and timestamptz_timestamp().

◆ timestamptz_age()

Datum timestamptz_age ( PG_FUNCTION_ARGS  )

Definition at line 4455 of file timestamp.c.

4456{
4460 fsec_t fsec1,
4461 fsec2;
4462 struct pg_itm tt,
4463 *tm = &tt;
4464 struct pg_tm tt1,
4465 *tm1 = &tt1;
4466 struct pg_tm tt2,
4467 *tm2 = &tt2;
4468 int tz1;
4469 int tz2;
4470
4472
4473 /*
4474 * Handle infinities.
4475 *
4476 * We treat anything that amounts to "infinity - infinity" as an error,
4477 * since the interval type has nothing equivalent to NaN.
4478 */
4480 {
4482 ereport(ERROR,
4484 errmsg("interval out of range")));
4485 else
4487 }
4488 else if (TIMESTAMP_IS_NOEND(dt1))
4489 {
4491 ereport(ERROR,
4493 errmsg("interval out of range")));
4494 else
4496 }
4497 else if (TIMESTAMP_IS_NOBEGIN(dt2))
4499 else if (TIMESTAMP_IS_NOEND(dt2))
4501 else if (timestamp2tm(dt1, &tz1, tm1, &fsec1, NULL, NULL) == 0 &&
4502 timestamp2tm(dt2, &tz2, tm2, &fsec2, NULL, NULL) == 0)
4503 {
4504 /* form the symbolic difference */
4505 tm->tm_usec = fsec1 - fsec2;
4506 tm->tm_sec = tm1->tm_sec - tm2->tm_sec;
4507 tm->tm_min = tm1->tm_min - tm2->tm_min;
4508 tm->tm_hour = tm1->tm_hour - tm2->tm_hour;
4509 tm->tm_mday = tm1->tm_mday - tm2->tm_mday;
4510 tm->tm_mon = tm1->tm_mon - tm2->tm_mon;
4511 tm->tm_year = tm1->tm_year - tm2->tm_year;
4512
4513 /* flip sign if necessary... */
4514 if (dt1 < dt2)
4515 {
4516 tm->tm_usec = -tm->tm_usec;
4517 tm->tm_sec = -tm->tm_sec;
4518 tm->tm_min = -tm->tm_min;
4519 tm->tm_hour = -tm->tm_hour;
4520 tm->tm_mday = -tm->tm_mday;
4521 tm->tm_mon = -tm->tm_mon;
4522 tm->tm_year = -tm->tm_year;
4523 }
4524
4525 /* propagate any negative fields into the next higher field */
4526 while (tm->tm_usec < 0)
4527 {
4528 tm->tm_usec += USECS_PER_SEC;
4529 tm->tm_sec--;
4530 }
4531
4532 while (tm->tm_sec < 0)
4533 {
4535 tm->tm_min--;
4536 }
4537
4538 while (tm->tm_min < 0)
4539 {
4541 tm->tm_hour--;
4542 }
4543
4544 while (tm->tm_hour < 0)
4545 {
4547 tm->tm_mday--;
4548 }
4549
4550 while (tm->tm_mday < 0)
4551 {
4552 if (dt1 < dt2)
4553 {
4554 tm->tm_mday += day_tab[isleap(tm1->tm_year)][tm1->tm_mon - 1];
4555 tm->tm_mon--;
4556 }
4557 else
4558 {
4559 tm->tm_mday += day_tab[isleap(tm2->tm_year)][tm2->tm_mon - 1];
4560 tm->tm_mon--;
4561 }
4562 }
4563
4564 while (tm->tm_mon < 0)
4565 {
4567 tm->tm_year--;
4568 }
4569
4570 /*
4571 * Note: we deliberately ignore any difference between tz1 and tz2.
4572 */
4573
4574 /* recover sign if necessary... */
4575 if (dt1 < dt2)
4576 {
4577 tm->tm_usec = -tm->tm_usec;
4578 tm->tm_sec = -tm->tm_sec;
4579 tm->tm_min = -tm->tm_min;
4580 tm->tm_hour = -tm->tm_hour;
4581 tm->tm_mday = -tm->tm_mday;
4582 tm->tm_mon = -tm->tm_mon;
4583 tm->tm_year = -tm->tm_year;
4584 }
4585
4586 if (itm2interval(tm, result) != 0)
4587 ereport(ERROR,
4589 errmsg("interval out of range")));
4590 }
4591 else
4592 ereport(ERROR,
4594 errmsg("timestamp out of range")));
4595
4597}

References day_tab, ereport, errcode(), errmsg, ERROR, fb(), HOURS_PER_DAY, INTERVAL_NOBEGIN, INTERVAL_NOEND, isleap, itm2interval(), MINS_PER_HOUR, MONTHS_PER_YEAR, palloc_object, PG_GETARG_TIMESTAMPTZ, PG_RETURN_INTERVAL_P, result, SECS_PER_MINUTE, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, 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, and USECS_PER_SEC.

◆ timestamptz_at_local()

Datum timestamptz_at_local ( PG_FUNCTION_ARGS  )

Definition at line 7000 of file timestamp.c.

7001{
7002 return timestamptz_timestamp(fcinfo);
7003}
Datum timestamptz_timestamp(PG_FUNCTION_ARGS)
Definition timestamp.c:6539

References timestamptz_timestamp().

◆ timestamptz_bin()

Datum timestamptz_bin ( PG_FUNCTION_ARGS  )

Definition at line 4847 of file timestamp.c.

4848{
4854 tm_diff,
4855 tm_modulo,
4856 tm_delta;
4857
4860
4861 if (TIMESTAMP_NOT_FINITE(origin))
4862 ereport(ERROR,
4864 errmsg("origin out of range")));
4865
4867 ereport(ERROR,
4869 errmsg("timestamps cannot be binned into infinite intervals")));
4870
4871 if (stride->month != 0)
4872 ereport(ERROR,
4874 errmsg("timestamps cannot be binned into intervals containing months or years")));
4875
4878 ereport(ERROR,
4880 errmsg("interval out of range")));
4881
4882 if (stride_usecs <= 0)
4883 ereport(ERROR,
4885 errmsg("stride must be greater than zero")));
4886
4888 ereport(ERROR,
4890 errmsg("interval out of range")));
4891
4892 /* These calculations cannot overflow */
4895 result = origin + tm_delta;
4896
4897 /*
4898 * We want to round towards -infinity, not 0, when tm_diff is negative and
4899 * not a multiple of stride_usecs. This adjustment *can* cause overflow,
4900 * since the result might now be out of the range origin .. timestamp.
4901 */
4902 if (tm_modulo < 0)
4903 {
4906 ereport(ERROR,
4908 errmsg("timestamp out of range")));
4909 }
4910
4912}

References ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, pg_mul_s64_overflow(), PG_RETURN_TIMESTAMPTZ, pg_sub_s64_overflow(), result, TIMESTAMP_NOT_FINITE, unlikely, and USECS_PER_DAY.

◆ timestamptz_cmp_timestamp()

◆ timestamptz_eq_timestamp()

◆ timestamptz_ge_timestamp()

◆ timestamptz_gt_timestamp()

◆ timestamptz_hash()

Datum timestamptz_hash ( PG_FUNCTION_ARGS  )

Definition at line 2359 of file timestamp.c.

2360{
2361 return hashint8(fcinfo);
2362}

References hashint8().

◆ timestamptz_hash_extended()

Datum timestamptz_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 2365 of file timestamp.c.

2366{
2367 return hashint8extended(fcinfo);
2368}

References hashint8extended().

◆ timestamptz_in()

Datum timestamptz_in ( PG_FUNCTION_ARGS  )

Definition at line 414 of file timestamp.c.

415{
416 char *str = PG_GETARG_CSTRING(0);
417#ifdef NOT_USED
418 Oid typelem = PG_GETARG_OID(1);
419#endif
420 int32 typmod = PG_GETARG_INT32(2);
421 Node *escontext = fcinfo->context;
423 fsec_t fsec;
424 struct pg_tm tt,
425 *tm = &tt;
426 int tz;
427 int dtype;
428 int nf;
429 int dterr;
430 char *field[MAXDATEFIELDS];
431 int ftype[MAXDATEFIELDS];
433 DateTimeErrorExtra extra;
434
436 field, ftype, MAXDATEFIELDS, &nf);
437 if (dterr == 0)
438 dterr = DecodeDateTime(field, ftype, nf,
439 &dtype, tm, &fsec, &tz, &extra);
440 if (dterr != 0)
441 {
442 DateTimeParseError(dterr, &extra, str, "timestamp with time zone",
443 escontext);
445 }
446
447 switch (dtype)
448 {
449 case DTK_DATE:
450 if (tm2timestamp(tm, fsec, &tz, &result) != 0)
451 ereturn(escontext, (Datum) 0,
453 errmsg("timestamp out of range: \"%s\"", str)));
454 break;
455
456 case DTK_EPOCH:
458 break;
459
460 case DTK_LATE:
462 break;
463
464 case DTK_EARLY:
466 break;
467
468 default:
469 elog(ERROR, "unexpected dtype %d while parsing timestamptz \"%s\"",
470 dtype, str);
472 }
473
474 AdjustTimestampForTypmod(&result, typmod, escontext);
475
477}

References AdjustTimestampForTypmod(), DateTimeParseError(), DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_LATE, elog, ereturn, errcode(), errmsg, ERROR, fb(), MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, result, SetEpochTimestamp(), str, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, and tm2timestamp().

Referenced by AlterRole(), CreateRole(), moddatetime(), and validateRecoveryParameters().

◆ timestamptz_izone()

Datum timestamptz_izone ( PG_FUNCTION_ARGS  )

Definition at line 6680 of file timestamp.c.

6681{
6685 int tz;
6686
6689
6691 ereport(ERROR,
6693 errmsg("interval time zone \"%s\" must be finite",
6695 PointerGetDatum(zone))))));
6696
6697 if (zone->month != 0 || zone->day != 0)
6698 ereport(ERROR,
6700 errmsg("interval time zone \"%s\" must not include months or days",
6702 PointerGetDatum(zone))))));
6703
6704 tz = -(zone->time / USECS_PER_SEC);
6705
6706 result = dt2local(timestamp, tz);
6707
6709 ereport(ERROR,
6711 errmsg("timestamp out of range")));
6712
6714}

References DatumGetCString(), DirectFunctionCall1, dt2local(), ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_NOT_FINITE, interval_out(), IS_VALID_TIMESTAMP, PG_GETARG_INTERVAL_P, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMP, PointerGetDatum, result, TIMESTAMP_NOT_FINITE, and USECS_PER_SEC.

◆ timestamptz_le_timestamp()

◆ timestamptz_lt_timestamp()

◆ timestamptz_mi_interval()

◆ timestamptz_mi_interval_at_zone()

◆ timestamptz_mi_interval_internal()

static TimestampTz timestamptz_mi_interval_internal ( TimestampTz  timestamp,
Interval span,
pg_tz attimezone 
)
static

◆ timestamptz_ne_timestamp()

◆ timestamptz_out()

Datum timestamptz_out ( PG_FUNCTION_ARGS  )

Definition at line 773 of file timestamp.c.

774{
776 char *result;
777 int tz;
778 struct pg_tm tt,
779 *tm = &tt;
780 fsec_t fsec;
781 const char *tzn;
782 char buf[MAXDATELEN + 1];
783
786 else if (timestamp2tm(dt, &tz, tm, &fsec, &tzn, NULL) == 0)
787 EncodeDateTime(tm, fsec, true, tz, tzn, DateStyle, buf);
788 else
791 errmsg("timestamp out of range")));
792
793 result = pstrdup(buf);
795}

References buf, DateStyle, EncodeDateTime(), EncodeSpecialTimestamp(), ereport, errcode(), errmsg, ERROR, fb(), MAXDATELEN, PG_GETARG_TIMESTAMPTZ, PG_RETURN_CSTRING, pstrdup(), result, timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

Referenced by ExecGetJsonValueItemString().

◆ timestamptz_part()

Datum timestamptz_part ( PG_FUNCTION_ARGS  )

Definition at line 6046 of file timestamp.c.

6047{
6048 return timestamptz_part_common(fcinfo, false);
6049}

References timestamptz_part_common().

◆ timestamptz_part_common()

static Datum timestamptz_part_common ( PG_FUNCTION_ARGS  ,
bool  retnumeric 
)
static

Definition at line 5789 of file timestamp.c.

5790{
5795 int tz;
5796 int type,
5797 val;
5798 char *lowunits;
5799 fsec_t fsec;
5800 struct pg_tm tt,
5801 *tm = &tt;
5802
5805 false);
5806
5807 type = DecodeUnits(0, lowunits, &val);
5808 if (type == UNKNOWN_FIELD)
5810
5812 {
5815 true);
5816
5817 if (r != 0.0)
5818 {
5819 if (retnumeric)
5820 {
5821 if (r < 0)
5823 CStringGetDatum("-Infinity"),
5825 Int32GetDatum(-1));
5826 else if (r > 0)
5828 CStringGetDatum("Infinity"),
5830 Int32GetDatum(-1));
5831 }
5832 else
5834 }
5835 else
5837 }
5838
5839 if (type == UNITS)
5840 {
5841 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
5842 ereport(ERROR,
5844 errmsg("timestamp out of range")));
5845
5846 switch (val)
5847 {
5848 case DTK_TZ:
5849 intresult = -tz;
5850 break;
5851
5852 case DTK_TZ_MINUTE:
5854 break;
5855
5856 case DTK_TZ_HOUR:
5857 intresult = -tz / SECS_PER_HOUR;
5858 break;
5859
5860 case DTK_MICROSEC:
5861 intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
5862 break;
5863
5864 case DTK_MILLISEC:
5865 if (retnumeric)
5866 /*---
5867 * tm->tm_sec * 1000 + fsec / 1000
5868 * = (tm->tm_sec * 1'000'000 + fsec) / 1000
5869 */
5871 else
5872 PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
5873 break;
5874
5875 case DTK_SECOND:
5876 if (retnumeric)
5877 /*---
5878 * tm->tm_sec + fsec / 1'000'000
5879 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
5880 */
5882 else
5883 PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
5884 break;
5885
5886 case DTK_MINUTE:
5887 intresult = tm->tm_min;
5888 break;
5889
5890 case DTK_HOUR:
5891 intresult = tm->tm_hour;
5892 break;
5893
5894 case DTK_DAY:
5895 intresult = tm->tm_mday;
5896 break;
5897
5898 case DTK_MONTH:
5899 intresult = tm->tm_mon;
5900 break;
5901
5902 case DTK_QUARTER:
5903 intresult = (tm->tm_mon - 1) / 3 + 1;
5904 break;
5905
5906 case DTK_WEEK:
5908 break;
5909
5910 case DTK_YEAR:
5911 if (tm->tm_year > 0)
5912 intresult = tm->tm_year;
5913 else
5914 /* there is no year 0, just 1 BC and 1 AD */
5915 intresult = tm->tm_year - 1;
5916 break;
5917
5918 case DTK_DECADE:
5919 /* see comments in timestamp_part */
5920 if (tm->tm_year > 0)
5921 intresult = tm->tm_year / 10;
5922 else
5923 intresult = -((8 - (tm->tm_year - 1)) / 10);
5924 break;
5925
5926 case DTK_CENTURY:
5927 /* see comments in timestamp_part */
5928 if (tm->tm_year > 0)
5929 intresult = (tm->tm_year + 99) / 100;
5930 else
5931 intresult = -((99 - (tm->tm_year - 1)) / 100);
5932 break;
5933
5934 case DTK_MILLENNIUM:
5935 /* see comments in timestamp_part */
5936 if (tm->tm_year > 0)
5937 intresult = (tm->tm_year + 999) / 1000;
5938 else
5939 intresult = -((999 - (tm->tm_year - 1)) / 1000);
5940 break;
5941
5942 case DTK_JULIAN:
5943 if (retnumeric)
5947 NULL),
5948 NULL));
5949 else
5952 tm->tm_sec + (fsec / 1000000.0)) / (double) SECS_PER_DAY);
5953 break;
5954
5955 case DTK_ISOYEAR:
5957 /* Adjust BC years */
5958 if (intresult <= 0)
5959 intresult -= 1;
5960 break;
5961
5962 case DTK_DOW:
5963 case DTK_ISODOW:
5965 if (val == DTK_ISODOW && intresult == 0)
5966 intresult = 7;
5967 break;
5968
5969 case DTK_DOY:
5971 - date2j(tm->tm_year, 1, 1) + 1);
5972 break;
5973
5974 default:
5975 ereport(ERROR,
5977 errmsg("unit \"%s\" not supported for type %s",
5979 intresult = 0;
5980 }
5981 }
5982 else if (type == RESERV)
5983 {
5984 switch (val)
5985 {
5986 case DTK_EPOCH:
5988 /* (timestamp - epoch) / 1000000 */
5989 if (retnumeric)
5990 {
5992
5993 if (timestamp < (PG_INT64_MAX + epoch))
5995 else
5996 {
5999 NULL),
6000 int64_to_numeric(1000000),
6001 NULL);
6004 Int32GetDatum(6)));
6005 }
6007 }
6008 else
6009 {
6010 float8 result;
6011
6012 /* try to avoid precision loss in subtraction */
6013 if (timestamp < (PG_INT64_MAX + epoch))
6014 result = (timestamp - epoch) / 1000000.0;
6015 else
6016 result = ((float8) timestamp - epoch) / 1000000.0;
6018 }
6019 break;
6020
6021 default:
6022 ereport(ERROR,
6024 errmsg("unit \"%s\" not supported for type %s",
6026 intresult = 0;
6027 }
6028 }
6029 else
6030 {
6031 ereport(ERROR,
6033 errmsg("unit \"%s\" not recognized for type %s",
6035
6036 intresult = 0;
6037 }
6038
6039 if (retnumeric)
6041 else
6043}
#define SECS_PER_HOUR
Definition timestamp.h:127

References CStringGetDatum(), date2isoweek(), date2isoyear(), date2j(), DatumGetNumeric(), DecodeSpecial(), DecodeUnits(), DirectFunctionCall2, DirectFunctionCall3, downcase_truncate_identifier(), 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, epoch, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), Int32GetDatum(), int64_div_fast_to_numeric(), int64_to_numeric(), INT64CONST, InvalidOid, j2day(), MINS_PER_HOUR, NonFiniteTimestampTzPart(), numeric_add_safe(), numeric_div_safe(), numeric_in(), numeric_round(), numeric_sub_safe(), NumericGetDatum(), ObjectIdGetDatum(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_INT64_MAX, PG_RETURN_FLOAT8, PG_RETURN_NULL, PG_RETURN_NUMERIC, RESERV, result, SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, SetEpochTimestamp(), timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_NOT_FINITE, 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, type, UNITS, UNKNOWN_FIELD, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by extract_timestamptz(), and timestamptz_part().

◆ timestamptz_pl_interval()

◆ timestamptz_pl_interval_at_zone()

◆ timestamptz_pl_interval_internal()

static TimestampTz timestamptz_pl_interval_internal ( TimestampTz  timestamp,
Interval span,
pg_tz attimezone 
)
static

Definition at line 3248 of file timestamp.c.

3251{
3253 int tz;
3254
3255 /*
3256 * Handle infinities.
3257 *
3258 * We treat anything that amounts to "infinity - infinity" as an error,
3259 * since the timestamptz type has nothing equivalent to NaN.
3260 */
3262 {
3264 ereport(ERROR,
3266 errmsg("timestamp out of range")));
3267 else
3269 }
3270 else if (INTERVAL_IS_NOEND(span))
3271 {
3273 ereport(ERROR,
3275 errmsg("timestamp out of range")));
3276 else
3278 }
3280 result = timestamp;
3281 else
3282 {
3283 /* Use session timezone if caller asks for default */
3284 if (attimezone == NULL)
3285 attimezone = session_timezone;
3286
3287 if (span->month != 0)
3288 {
3289 struct pg_tm tt,
3290 *tm = &tt;
3291 fsec_t fsec;
3292
3293 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, attimezone) != 0)
3294 ereport(ERROR,
3296 errmsg("timestamp out of range")));
3297
3298 if (pg_add_s32_overflow(tm->tm_mon, span->month, &tm->tm_mon))
3299 ereport(ERROR,
3301 errmsg("timestamp out of range")));
3302 if (tm->tm_mon > MONTHS_PER_YEAR)
3303 {
3304 tm->tm_year += (tm->tm_mon - 1) / MONTHS_PER_YEAR;
3305 tm->tm_mon = ((tm->tm_mon - 1) % MONTHS_PER_YEAR) + 1;
3306 }
3307 else if (tm->tm_mon < 1)
3308 {
3309 tm->tm_year += tm->tm_mon / MONTHS_PER_YEAR - 1;
3311 }
3312
3313 /* adjust for end of month boundary problems... */
3314 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
3315 tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
3316
3317 tz = DetermineTimeZoneOffset(tm, attimezone);
3318
3319 if (tm2timestamp(tm, fsec, &tz, &timestamp) != 0)
3320 ereport(ERROR,
3322 errmsg("timestamp out of range")));
3323 }
3324
3325 if (span->day != 0)
3326 {
3327 struct pg_tm tt,
3328 *tm = &tt;
3329 fsec_t fsec;
3330 int julian;
3331
3332 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, attimezone) != 0)
3333 ereport(ERROR,
3335 errmsg("timestamp out of range")));
3336
3337 /*
3338 * Add days by converting to and from Julian. We need an overflow
3339 * check here since j2date expects a non-negative integer input.
3340 * In practice though, it will give correct answers for small
3341 * negative Julian dates; we should allow -1 to avoid
3342 * timezone-dependent failures, as discussed in timestamp.h.
3343 */
3345 if (pg_add_s32_overflow(julian, span->day, &julian) ||
3346 julian < -1)
3347 ereport(ERROR,
3349 errmsg("timestamp out of range")));
3351
3352 tz = DetermineTimeZoneOffset(tm, attimezone);
3353
3354 if (tm2timestamp(tm, fsec, &tz, &timestamp) != 0)
3355 ereport(ERROR,
3357 errmsg("timestamp out of range")));
3358 }
3359
3361 ereport(ERROR,
3363 errmsg("timestamp out of range")));
3364
3366 ereport(ERROR,
3368 errmsg("timestamp out of range")));
3369
3370 result = timestamp;
3371 }
3372
3373 return result;
3374}

References date2j(), day_tab, DetermineTimeZoneOffset(), ereport, errcode(), errmsg, ERROR, fb(), INTERVAL_IS_NOBEGIN, INTERVAL_IS_NOEND, IS_VALID_TIMESTAMP, isleap, j2date(), MONTHS_PER_YEAR, pg_add_s32_overflow(), pg_add_s64_overflow(), result, session_timezone, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by generate_series_timestamptz_internal(), in_range_timestamptz_interval(), timestamptz_mi_interval_internal(), timestamptz_pl_interval(), and timestamptz_pl_interval_at_zone().

◆ timestamptz_recv()

Datum timestamptz_recv ( PG_FUNCTION_ARGS  )

Definition at line 801 of file timestamp.c.

802{
804
805#ifdef NOT_USED
806 Oid typelem = PG_GETARG_OID(1);
807#endif
808 int32 typmod = PG_GETARG_INT32(2);
810 int tz;
811 struct pg_tm tt,
812 *tm = &tt;
813 fsec_t fsec;
814
816
817 /* range check: see if timestamptz_out would like it */
819 /* ok */ ;
820 else if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0 ||
824 errmsg("timestamp out of range")));
825
827
829}

References AdjustTimestampForTypmod(), buf, ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_TIMESTAMPTZ, pq_getmsgint64(), timestamp2tm(), TIMESTAMP_NOT_FINITE, and tm.

◆ timestamptz_scale()

Datum timestamptz_scale ( PG_FUNCTION_ARGS  )

◆ timestamptz_send()

◆ timestamptz_timestamp()

◆ timestamptz_to_str()

const char * timestamptz_to_str ( TimestampTz  t)

◆ timestamptz_to_time_t()

pg_time_t timestamptz_to_time_t ( TimestampTz  t)

◆ timestamptz_trunc()

Datum timestamptz_trunc ( PG_FUNCTION_ARGS  )

Definition at line 5100 of file timestamp.c.

5101{
5105
5107
5109}
static TimestampTz timestamptz_trunc_internal(text *units, TimestampTz timestamp, pg_tz *tzp)
Definition timestamp.c:4921

References fb(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMPTZ, result, session_timezone, and timestamptz_trunc_internal().

◆ timestamptz_trunc_internal()

static TimestampTz timestamptz_trunc_internal ( text units,
TimestampTz  timestamp,
pg_tz tzp 
)
static

Definition at line 4921 of file timestamp.c.

4922{
4924 int tz;
4925 int type,
4926 val;
4927 bool redotz = false;
4928 char *lowunits;
4929 fsec_t fsec;
4930 struct pg_tm tt,
4931 *tm = &tt;
4932
4935 false);
4936
4937 type = DecodeUnits(0, lowunits, &val);
4938
4939 if (type == UNITS)
4940 {
4942 {
4943 /*
4944 * Errors thrown here for invalid units should exactly match those
4945 * below, else there will be unexpected discrepancies between
4946 * finite- and infinite-input cases.
4947 */
4948 switch (val)
4949 {
4950 case DTK_WEEK:
4951 case DTK_MILLENNIUM:
4952 case DTK_CENTURY:
4953 case DTK_DECADE:
4954 case DTK_YEAR:
4955 case DTK_QUARTER:
4956 case DTK_MONTH:
4957 case DTK_DAY:
4958 case DTK_HOUR:
4959 case DTK_MINUTE:
4960 case DTK_SECOND:
4961 case DTK_MILLISEC:
4962 case DTK_MICROSEC:
4963 return timestamp;
4964 break;
4965
4966 default:
4967 ereport(ERROR,
4969 errmsg("unit \"%s\" not supported for type %s",
4971 result = 0;
4972 }
4973 }
4974
4975 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, tzp) != 0)
4976 ereport(ERROR,
4978 errmsg("timestamp out of range")));
4979
4980 switch (val)
4981 {
4982 case DTK_WEEK:
4983 {
4984 int woy;
4985
4987
4988 /*
4989 * If it is week 52/53 and the month is January, then the
4990 * week must belong to the previous year. Also, some
4991 * December dates belong to the next year.
4992 */
4993 if (woy >= 52 && tm->tm_mon == 1)
4994 --tm->tm_year;
4996 ++tm->tm_year;
4997 isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
4998 tm->tm_hour = 0;
4999 tm->tm_min = 0;
5000 tm->tm_sec = 0;
5001 fsec = 0;
5002 redotz = true;
5003 break;
5004 }
5005 /* one may consider DTK_THOUSAND and DTK_HUNDRED... */
5006 case DTK_MILLENNIUM:
5007
5008 /*
5009 * truncating to the millennium? what is this supposed to
5010 * mean? let us put the first year of the millennium... i.e.
5011 * -1000, 1, 1001, 2001...
5012 */
5013 if (tm->tm_year > 0)
5014 tm->tm_year = ((tm->tm_year + 999) / 1000) * 1000 - 999;
5015 else
5016 tm->tm_year = -((999 - (tm->tm_year - 1)) / 1000) * 1000 + 1;
5018 case DTK_CENTURY:
5019 /* truncating to the century? as above: -100, 1, 101... */
5020 if (tm->tm_year > 0)
5021 tm->tm_year = ((tm->tm_year + 99) / 100) * 100 - 99;
5022 else
5023 tm->tm_year = -((99 - (tm->tm_year - 1)) / 100) * 100 + 1;
5025 case DTK_DECADE:
5026
5027 /*
5028 * truncating to the decade? first year of the decade. must
5029 * not be applied if year was truncated before!
5030 */
5031 if (val != DTK_MILLENNIUM && val != DTK_CENTURY)
5032 {
5033 if (tm->tm_year > 0)
5034 tm->tm_year = (tm->tm_year / 10) * 10;
5035 else
5036 tm->tm_year = -((8 - (tm->tm_year - 1)) / 10) * 10;
5037 }
5039 case DTK_YEAR:
5040 tm->tm_mon = 1;
5042 case DTK_QUARTER:
5043 tm->tm_mon = (3 * ((tm->tm_mon - 1) / 3)) + 1;
5045 case DTK_MONTH:
5046 tm->tm_mday = 1;
5048 case DTK_DAY:
5049 tm->tm_hour = 0;
5050 redotz = true; /* for all cases >= DAY */
5052 case DTK_HOUR:
5053 tm->tm_min = 0;
5055 case DTK_MINUTE:
5056 tm->tm_sec = 0;
5058 case DTK_SECOND:
5059 fsec = 0;
5060 break;
5061 case DTK_MILLISEC:
5062 fsec = (fsec / 1000) * 1000;
5063 break;
5064 case DTK_MICROSEC:
5065 break;
5066
5067 default:
5068 ereport(ERROR,
5070 errmsg("unit \"%s\" not supported for type %s",
5072 result = 0;
5073 }
5074
5075 if (redotz)
5076 tz = DetermineTimeZoneOffset(tm, tzp);
5077
5078 if (tm2timestamp(tm, fsec, &tz, &result) != 0)
5079 ereport(ERROR,
5081 errmsg("timestamp out of range")));
5082 }
5083 else
5084 {
5085 ereport(ERROR,
5087 errmsg("unit \"%s\" not recognized for type %s",
5089 result = 0;
5090 }
5091
5092 return result;
5093}

References date2isoweek(), DecodeUnits(), DetermineTimeZoneOffset(), 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, ereport, errcode(), errmsg, ERROR, fb(), format_type_be(), isoweek2date(), MONTHS_PER_YEAR, pg_fallthrough, result, timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, UNITS, val, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by timestamptz_trunc(), and timestamptz_trunc_zone().

◆ timestamptz_trunc_zone()

Datum timestamptz_trunc_zone ( PG_FUNCTION_ARGS  )

Definition at line 5116 of file timestamp.c.

5117{
5122 pg_tz *tzp;
5123
5124 /*
5125 * Look up the requested timezone.
5126 */
5127 tzp = lookup_timezone(zone);
5128
5130
5132}

References fb(), lookup_timezone(), PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMPTZ, result, and timestamptz_trunc_internal().

◆ timestamptz_zone()

Datum timestamptz_zone ( PG_FUNCTION_ARGS  )

Definition at line 6615 of file timestamp.c.

6616{
6620 int tz;
6621 char tzname[TZ_STRLEN_MAX + 1];
6622 int type,
6623 val;
6624 pg_tz *tzp;
6625
6628
6629 /*
6630 * Look up the requested timezone.
6631 */
6633
6634 type = DecodeTimezoneName(tzname, &val, &tzp);
6635
6637 {
6638 /* fixed-offset abbreviation */
6639 tz = -val;
6640 result = dt2local(timestamp, tz);
6641 }
6642 else if (type == TZNAME_DYNTZ)
6643 {
6644 /* dynamic-offset abbreviation, resolve using specified time */
6645 int isdst;
6646
6648 result = dt2local(timestamp, tz);
6649 }
6650 else
6651 {
6652 /* full zone name, rotate from that zone */
6653 struct pg_tm tm;
6654 fsec_t fsec;
6655
6656 if (timestamp2tm(timestamp, &tz, &tm, &fsec, NULL, tzp) != 0)
6657 ereport(ERROR,
6659 errmsg("timestamp out of range")));
6660 if (tm2timestamp(&tm, fsec, NULL, &result) != 0)
6661 ereport(ERROR,
6663 errmsg("timestamp out of range")));
6664 }
6665
6667 ereport(ERROR,
6669 errmsg("timestamp out of range")));
6670
6672}
int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr, pg_tz *tzp, int *isdst)
Definition datetime.c:1810

References DecodeTimezoneName(), DetermineTimeZoneAbbrevOffsetTS(), dt2local(), ereport, errcode(), errmsg, ERROR, fb(), IS_VALID_TIMESTAMP, PG_GETARG_TEXT_PP, PG_GETARG_TIMESTAMPTZ, PG_RETURN_TIMESTAMP, result, text_to_cstring_buffer(), timestamp2tm(), TIMESTAMP_NOT_FINITE, tm, tm2timestamp(), type, TZ_STRLEN_MAX, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, and val.

◆ timestamptztypmodin()

Datum timestamptztypmodin ( PG_FUNCTION_ARGS  )

Definition at line 846 of file timestamp.c.

References anytimestamp_typmodin(), fb(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

◆ timestamptztypmodout()

Datum timestamptztypmodout ( PG_FUNCTION_ARGS  )

Definition at line 854 of file timestamp.c.

855{
856 int32 typmod = PG_GETARG_INT32(0);
857
859}

References anytimestamp_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

◆ tm2timestamp()

int tm2timestamp ( struct pg_tm tm,
fsec_t  fsec,
int tzp,
Timestamp result 
)

Definition at line 2015 of file timestamp.c.

2016{
2018 TimeOffset time;
2019
2020 /* Prevent overflow in Julian-day routines */
2022 {
2023 *result = 0; /* keep compiler quiet */
2024 return -1;
2025 }
2026
2028 time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
2029
2032 {
2033 *result = 0; /* keep compiler quiet */
2034 return -1;
2035 }
2036 if (tzp != NULL)
2037 *result = dt2local(*result, -(*tzp));
2038
2039 /* final range check catches just-out-of-range timestamps */
2041 {
2042 *result = 0; /* keep compiler quiet */
2043 return -1;
2044 }
2045
2046 return 0;
2047}
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition timestamp.c:2140

References date2j(), dt2local(), fb(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), pg_mul_s64_overflow(), POSTGRES_EPOCH_JDATE, result, time2t(), 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, unlikely, and USECS_PER_DAY.

Referenced by check_recovery_target_time(), parse_datetime(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_current(), PGTYPEStimestamp_defmt_scan(), PGTYPEStimestamp_from_asc(), SetEpochTimestamp(), SetEpochTimestamp(), timestamp_in(), timestamp_pl_interval(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp_safe(), timestamptz_in(), timestamptz_pl_interval_internal(), timestamptz_trunc_internal(), timestamptz_zone(), and to_timestamp().

Variable Documentation

◆ PgReloadTime

TimestampTz PgReloadTime

Definition at line 48 of file timestamp.c.

Referenced by pg_conf_load_time(), and ProcessConfigFileInternal().

◆ PgStartTime

TimestampTz PgStartTime

Definition at line 45 of file timestamp.c.

Referenced by pg_postmaster_start_time(), PostgresSingleUserMain(), and PostmasterMain().