mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-14 18:37:03 +08:00
The date-vs-timestamp, date-vs-timestamptz, and timestamp-vs-timestamptz comparators all worked by promoting the first type to the second and then doing a simple same-type comparison. This works fine, except when the conversion result is out of range, in which case we throw an entirely avoidable error. The sources of such failures are (a) type date can represent dates much farther in the future than the timestamp types can; (b) timezone rotation might cause a just-in-range timestamp value to become a just-out-of-range timestamptz value. Up to now we just ignored these corner-case issues, but now we have an actual user complaint (bug #16657 from Huss EL-Sheikh), so let's do something about it. It turns out that commit 52ad1e659 already built all the necessary infrastructure to support error-free comparisons, but neglected to actually use it in the main-line code paths. Fix that, do a little bit of code style review, and remove the now-duplicate logic in jsonpath_exec.c. Back-patch to v13 where 52ad1e659 came in. We could take this back further by back-patching said infrastructure, but given the small number of complaints so far, I don't feel a great need to. Discussion: https://postgr.es/m/16657-cde2f876d8cc7971@postgresql.org
115 lines
4.1 KiB
C
115 lines
4.1 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* timestamp.h
|
|
* Definitions for the SQL "timestamp" and "interval" types.
|
|
*
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/utils/timestamp.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef TIMESTAMP_H
|
|
#define TIMESTAMP_H
|
|
|
|
#include "datatype/timestamp.h"
|
|
#include "fmgr.h"
|
|
#include "pgtime.h"
|
|
|
|
|
|
/*
|
|
* Macros for fmgr-callable functions.
|
|
*
|
|
* For Timestamp, we make use of the same support routines as for int64.
|
|
* Therefore Timestamp is pass-by-reference if and only if int64 is!
|
|
*/
|
|
#define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
|
|
#define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
|
|
#define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
|
|
|
|
#define TimestampGetDatum(X) Int64GetDatum(X)
|
|
#define TimestampTzGetDatum(X) Int64GetDatum(X)
|
|
#define IntervalPGetDatum(X) PointerGetDatum(X)
|
|
|
|
#define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
|
|
#define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
|
|
#define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
|
|
|
|
#define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
|
|
#define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
|
|
#define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
|
|
|
|
|
|
#define TIMESTAMP_MASK(b) (1 << (b))
|
|
#define INTERVAL_MASK(b) (1 << (b))
|
|
|
|
/* Macros to handle packing and unpacking the typmod field for intervals */
|
|
#define INTERVAL_FULL_RANGE (0x7FFF)
|
|
#define INTERVAL_RANGE_MASK (0x7FFF)
|
|
#define INTERVAL_FULL_PRECISION (0xFFFF)
|
|
#define INTERVAL_PRECISION_MASK (0xFFFF)
|
|
#define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
|
|
#define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
|
|
#define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
|
|
|
|
#define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
|
|
|
|
|
|
/* Set at postmaster start */
|
|
extern TimestampTz PgStartTime;
|
|
|
|
/* Set at configuration reload */
|
|
extern TimestampTz PgReloadTime;
|
|
|
|
|
|
/* Internal routines (not fmgr-callable) */
|
|
|
|
extern int32 anytimestamp_typmod_check(bool istz, int32 typmod);
|
|
|
|
extern TimestampTz GetCurrentTimestamp(void);
|
|
extern TimestampTz GetSQLCurrentTimestamp(int32 typmod);
|
|
extern Timestamp GetSQLLocalTimestamp(int32 typmod);
|
|
extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
|
|
long *secs, int *microsecs);
|
|
extern bool TimestampDifferenceExceeds(TimestampTz start_time,
|
|
TimestampTz stop_time,
|
|
int msec);
|
|
|
|
extern TimestampTz time_t_to_timestamptz(pg_time_t tm);
|
|
extern pg_time_t timestamptz_to_time_t(TimestampTz t);
|
|
|
|
extern const char *timestamptz_to_str(TimestampTz t);
|
|
|
|
extern int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt);
|
|
extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
|
|
fsec_t *fsec, const char **tzn, pg_tz *attimezone);
|
|
extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
|
|
|
|
extern int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec);
|
|
extern int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span);
|
|
|
|
extern Timestamp SetEpochTimestamp(void);
|
|
extern void GetEpochTime(struct pg_tm *tm);
|
|
|
|
extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
|
|
|
|
/* timestamp comparison works for timestamptz also */
|
|
#define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
|
|
|
|
extern TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp,
|
|
int *overflow);
|
|
extern int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal,
|
|
TimestampTz dt2);
|
|
|
|
extern int isoweek2j(int year, int week);
|
|
extern void isoweek2date(int woy, int *year, int *mon, int *mday);
|
|
extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
|
|
extern int date2isoweek(int year, int mon, int mday);
|
|
extern int date2isoyear(int year, int mon, int mday);
|
|
extern int date2isoyearday(int year, int mon, int mday);
|
|
|
|
extern bool TimestampTimestampTzRequiresRewrite(void);
|
|
|
|
#endif /* TIMESTAMP_H */
|