Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* timestamp.h
|
2013-04-20 11:04:41 -04:00
|
|
|
* Definitions for the SQL "timestamp" and "interval" types.
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
*
|
2020-01-01 12:21:45 -05:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
|
*
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/include/utils/timestamp.h
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
#ifndef TIMESTAMP_H
|
|
|
|
|
#define TIMESTAMP_H
|
|
|
|
|
|
2011-09-09 13:23:41 -04:00
|
|
|
#include "datatype/timestamp.h"
|
2000-06-08 21:11:16 -04:00
|
|
|
#include "fmgr.h"
|
2004-05-21 01:08:06 -04:00
|
|
|
#include "pgtime.h"
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
|
2002-08-04 02:42:18 -04:00
|
|
|
|
2000-06-08 21:11:16 -04:00
|
|
|
/*
|
|
|
|
|
* Macros for fmgr-callable functions.
|
|
|
|
|
*
|
2017-02-23 14:04:43 -05:00
|
|
|
* 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!
|
2000-06-08 21:11:16 -04:00
|
|
|
*/
|
2002-04-21 15:52:18 -04:00
|
|
|
#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)
|
|
|
|
|
|
2008-03-20 21:31:43 -04:00
|
|
|
#define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
|
|
|
|
|
#define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
|
2002-04-21 15:52:18 -04:00
|
|
|
#define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
|
|
|
|
|
|
2008-03-20 21:31:43 -04:00
|
|
|
#define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
|
|
|
|
|
#define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
|
2002-04-21 15:52:18 -04:00
|
|
|
#define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
|
2002-04-23 11:45:30 -04:00
|
|
|
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
|
2002-08-04 02:42:18 -04:00
|
|
|
#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)
|
|
|
|
|
|
2008-01-23 16:26:13 -05:00
|
|
|
#define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
|
2006-06-20 18:52:00 -04:00
|
|
|
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
|
2005-06-29 18:51:57 -04:00
|
|
|
/* Set at postmaster start */
|
|
|
|
|
extern TimestampTz PgStartTime;
|
2009-06-11 10:49:15 -04:00
|
|
|
|
2008-05-04 17:13:36 -04:00
|
|
|
/* Set at configuration reload */
|
|
|
|
|
extern TimestampTz PgReloadTime;
|
2005-06-29 18:51:57 -04:00
|
|
|
|
|
|
|
|
|
2000-06-08 21:11:16 -04:00
|
|
|
/* Internal routines (not fmgr-callable) */
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
|
2016-08-16 20:33:01 -04:00
|
|
|
extern int32 anytimestamp_typmod_check(bool istz, int32 typmod);
|
|
|
|
|
|
2005-06-29 18:51:57 -04:00
|
|
|
extern TimestampTz GetCurrentTimestamp(void);
|
2016-08-16 20:33:01 -04:00
|
|
|
extern TimestampTz GetSQLCurrentTimestamp(int32 typmod);
|
|
|
|
|
extern Timestamp GetSQLLocalTimestamp(int32 typmod);
|
2006-06-20 18:52:00 -04:00
|
|
|
extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
|
2019-05-22 13:04:48 -04:00
|
|
|
long *secs, int *microsecs);
|
Fix and simplify some usages of TimestampDifference().
Introduce TimestampDifferenceMilliseconds() to simplify callers
that would rather have the difference in milliseconds, instead of
the select()-oriented seconds-and-microseconds format. This gets
rid of at least one integer division per call, and it eliminates
some apparently-easy-to-mess-up arithmetic.
Two of these call sites were in fact wrong:
* pg_prewarm's autoprewarm_main() forgot to multiply the seconds
by 1000, thus ending up with a delay 1000X shorter than intended.
That doesn't quite make it a busy-wait, but close.
* postgres_fdw's pgfdw_get_cleanup_result() thought it needed to compute
microseconds not milliseconds, thus ending up with a delay 1000X longer
than intended. Somebody along the way had noticed this problem but
misdiagnosed the cause, and imposed an ad-hoc 60-second limit rather
than fixing the units. This was relatively harmless in context, because
we don't care that much about exactly how long this delay is; still,
it's wrong.
There are a few more callers of TimestampDifference() that don't
have a direct need for seconds-and-microseconds, but can't use
TimestampDifferenceMilliseconds() either because they do need
microsecond precision or because they might possibly deal with
intervals long enough to overflow 32-bit milliseconds. It might be
worth inventing another API to improve that, but that seems outside
the scope of this patch; so those callers are untouched here.
Given the fact that we are fixing some bugs, and the likelihood
that future patches might want to back-patch code that uses this
new API, back-patch to all supported branches.
Alexey Kondratov and Tom Lane
Discussion: https://postgr.es/m/3b1c053a21c07c1ed5e00be3b2b855ef@postgrespro.ru
2020-11-10 22:51:19 -05:00
|
|
|
extern long TimestampDifferenceMilliseconds(TimestampTz start_time,
|
|
|
|
|
TimestampTz stop_time);
|
2007-04-29 23:23:49 -04:00
|
|
|
extern bool TimestampDifferenceExceeds(TimestampTz start_time,
|
2019-05-22 13:04:48 -04:00
|
|
|
TimestampTz stop_time,
|
|
|
|
|
int msec);
|
2006-06-20 18:52:00 -04:00
|
|
|
|
2008-02-16 21:09:32 -05:00
|
|
|
extern TimestampTz time_t_to_timestamptz(pg_time_t tm);
|
|
|
|
|
extern pg_time_t timestamptz_to_time_t(TimestampTz t);
|
2005-08-12 14:23:56 -04:00
|
|
|
|
2007-04-30 17:01:53 -04:00
|
|
|
extern const char *timestamptz_to_str(TimestampTz t);
|
|
|
|
|
|
2017-06-21 14:39:04 -04:00
|
|
|
extern int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt);
|
2019-05-22 13:04:48 -04:00
|
|
|
extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
|
|
|
|
|
fsec_t *fsec, const char **tzn, pg_tz *attimezone);
|
2002-04-21 15:52:18 -04:00
|
|
|
extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
|
2017-06-21 14:39:04 -04:00
|
|
|
extern int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec);
|
|
|
|
|
extern int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span);
|
2001-09-05 23:22:42 -04:00
|
|
|
|
2001-09-28 04:09:14 -04:00
|
|
|
extern Timestamp SetEpochTimestamp(void);
|
2017-06-21 14:39:04 -04:00
|
|
|
extern void GetEpochTime(struct pg_tm *tm);
|
Implement "date/time grand unification".
Transform datetime and timespan into timestamp and interval.
Deprecate datetime and timespan, though translate to new types in gram.y.
Transform all datetime and timespan catalog entries into new types.
Make "INTERVAL" reserved word allowed as a column identifier in gram.y.
Remove dt.h, dt.c files, and retarget datetime.h, datetime.c as utility
routines for all date/time types.
date.{h,c} now deals with date, time types.
timestamp.{h,c} now deals with timestamp, interval types.
nabstime.{h,c} now deals with abstime, reltime, tinterval types.
Make NUMERIC a known native type for purposes of type coersion. Not tested.
2000-02-16 12:26:26 -05:00
|
|
|
|
2004-02-14 15:16:18 -05:00
|
|
|
extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
|
2004-08-29 01:07:03 -04:00
|
|
|
|
2004-02-14 15:16:18 -05:00
|
|
|
/* timestamp comparison works for timestamptz also */
|
|
|
|
|
#define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
|
|
|
|
|
|
2019-10-21 16:04:14 -04:00
|
|
|
extern TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp,
|
|
|
|
|
int *overflow);
|
Prevent internal overflows in date-vs-timestamp and related comparisons.
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
2020-10-07 17:10:26 -04:00
|
|
|
extern int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal,
|
|
|
|
|
TimestampTz dt2);
|
2019-09-25 14:51:47 -04:00
|
|
|
|
2007-02-15 22:39:46 -05:00
|
|
|
extern int isoweek2j(int year, int week);
|
2007-11-15 16:14:46 -05:00
|
|
|
extern void isoweek2date(int woy, int *year, int *mon, int *mday);
|
2012-09-03 22:52:34 -04:00
|
|
|
extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
|
2000-08-29 00:41:48 -04:00
|
|
|
extern int date2isoweek(int year, int mon, int mday);
|
2003-12-24 22:36:24 -05:00
|
|
|
extern int date2isoyear(int year, int mon, int mday);
|
2007-02-15 22:39:46 -05:00
|
|
|
extern int date2isoyearday(int year, int mon, int mday);
|
2001-10-28 01:26:15 -05:00
|
|
|
|
2019-03-08 23:16:27 -05:00
|
|
|
extern bool TimestampTimestampTzRequiresRewrite(void);
|
|
|
|
|
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 15:18:54 -04:00
|
|
|
#endif /* TIMESTAMP_H */
|