X-Git-Url: https://granicus.if.org/sourcecode?a=blobdiff_plain;f=src%2Finclude%2Futils%2Ftimestamp.h;h=94328b319368124a903fdce221c4978cf6d4b035;hb=4a14f13a0abfbf7e7d44a3d2689444d1806aa9dc;hp=5923c354936594db7cf878d3c68b71f81da1276b;hpb=c4320619635800a6116a02eee08b232c5abea266;p=postgresql diff --git a/src/include/utils/timestamp.h b/src/include/utils/timestamp.h index 5923c35493..94328b3193 100644 --- a/src/include/utils/timestamp.h +++ b/src/include/utils/timestamp.h @@ -1,93 +1,22 @@ /*------------------------------------------------------------------------- * * timestamp.h - * Definitions for the SQL92 "timestamp" and "interval" types. + * Definitions for the SQL "timestamp" and "interval" types. * - * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group + * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.69 2007/04/30 21:01:53 tgl Exp $ + * src/include/utils/timestamp.h * *------------------------------------------------------------------------- */ #ifndef TIMESTAMP_H #define TIMESTAMP_H -#include -#include -#include - +#include "datatype/timestamp.h" #include "fmgr.h" #include "pgtime.h" -#ifdef HAVE_INT64_TIMESTAMP -#include "utils/int8.h" -#endif - -/* - * Timestamp represents absolute time. - * Interval represents delta time. Keep track of months (and years), days, - * and time separately since the elapsed time spanned is unknown until - * instantiated relative to an absolute time. - * - * Note that Postgres uses "time interval" to mean a bounded interval, - * consisting of a beginning and ending time, not a time span - thomas 97/03/20 - */ - -#ifdef HAVE_INT64_TIMESTAMP -typedef int64 Timestamp; -typedef int64 TimestampTz; -#else -typedef double Timestamp; -typedef double TimestampTz; -#endif - -typedef struct -{ -#ifdef HAVE_INT64_TIMESTAMP - int64 time; /* all time units other than days, months and - * years */ -#else - double time; /* all time units other than days, months and - * years */ -#endif - int32 day; /* days, after time for alignment */ - int32 month; /* months and years, after time for alignment */ -} Interval; - - -#define MAX_TIMESTAMP_PRECISION 6 -#define MAX_INTERVAL_PRECISION 6 - -/* in both timestamp.h and ecpg/dt.h */ -#define DAYS_PER_YEAR 365.25 /* assumes leap year every four years */ -#define MONTHS_PER_YEAR 12 -/* - * DAYS_PER_MONTH is very imprecise. The more accurate value is - * 365.2425/12 = 30.436875, or '30 days 10:29:06'. Right now we only - * return an integral number of days, but someday perhaps we should - * also return a 'time' value to be used as well. ISO 8601 suggests - * 30 days. - */ -#define DAYS_PER_MONTH 30 /* assumes exactly 30 days per month */ -#define HOURS_PER_DAY 24 /* assume no daylight savings time changes */ - -/* - * This doesn't adjust for uneven daylight savings time intervals or leap - * seconds, and it crudely estimates leap years. A more accurate value - * for days per years is 365.2422. - */ -#define SECS_PER_YEAR (36525 * 864) /* avoid floating-point computation */ -#define SECS_PER_DAY 86400 -#define SECS_PER_HOUR 3600 -#define SECS_PER_MINUTE 60 -#define MINS_PER_HOUR 60 -#ifdef HAVE_INT64_TIMESTAMP -#define USECS_PER_DAY INT64CONST(86400000000) -#define USECS_PER_HOUR INT64CONST(3600000000) -#define USECS_PER_MINUTE INT64CONST(60000000) -#define USECS_PER_SEC INT64CONST(1000000) -#endif /* * Macros for fmgr-callable functions. @@ -106,17 +35,14 @@ typedef struct #define TimestampTzGetDatum(X) Int64GetDatum(X) #define IntervalPGetDatum(X) PointerGetDatum(X) -#define PG_GETARG_TIMESTAMP(n) PG_GETARG_INT64(n) -#define PG_GETARG_TIMESTAMPTZ(n) PG_GETARG_INT64(n) +#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) PG_RETURN_INT64(x) -#define PG_RETURN_TIMESTAMPTZ(x) PG_RETURN_INT64(x) +#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 DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1) -#define DT_NOEND (INT64CONST(0x7fffffffffffffff)) -#else +#else /* !HAVE_INT64_TIMESTAMP */ #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X)) #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X)) @@ -133,42 +59,9 @@ typedef struct #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) - -#ifdef HUGE_VAL -#define DT_NOBEGIN (-HUGE_VAL) -#define DT_NOEND (HUGE_VAL) -#else -#define DT_NOBEGIN (-DBL_MAX) -#define DT_NOEND (DBL_MAX) -#endif #endif /* HAVE_INT64_TIMESTAMP */ -#define TIMESTAMP_NOBEGIN(j) \ - do {(j) = DT_NOBEGIN;} while (0) -#define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN) - -#define TIMESTAMP_NOEND(j) \ - do {(j) = DT_NOEND;} while (0) -#define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND) - -#define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j)) - -#ifdef HAVE_INT64_TIMESTAMP - -typedef int32 fsec_t; -#else - -typedef double fsec_t; -#endif - -/* - * Round off to MAX_TIMESTAMP_PRECISION decimal places. - * Note: this is also used for rounding off intervals. - */ -#define TS_PREC_INV 1000000.0 -#define TSROUND(j) (rint(((double) (j)) * TS_PREC_INV) / TS_PREC_INV) - #define TIMESTAMP_MASK(b) (1 << (b)) #define INTERVAL_MASK(b) (1 << (b)) @@ -182,7 +75,7 @@ typedef double fsec_t; #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK) #ifdef HAVE_INT64_TIMESTAMP -#define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * 1000)) +#define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000)) #else #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) / 1000.0)) #endif @@ -191,6 +84,9 @@ typedef double fsec_t; /* Set at postmaster start */ extern TimestampTz PgStartTime; +/* Set at configuration reload */ +extern TimestampTz PgReloadTime; + /* * timestamp.c prototypes @@ -202,6 +98,7 @@ extern Datum timestamp_recv(PG_FUNCTION_ARGS); extern Datum timestamp_send(PG_FUNCTION_ARGS); extern Datum timestamptypmodin(PG_FUNCTION_ARGS); extern Datum timestamptypmodout(PG_FUNCTION_ARGS); +extern Datum timestamp_transform(PG_FUNCTION_ARGS); extern Datum timestamp_scale(PG_FUNCTION_ARGS); extern Datum timestamp_eq(PG_FUNCTION_ARGS); extern Datum timestamp_ne(PG_FUNCTION_ARGS); @@ -211,6 +108,8 @@ extern Datum timestamp_ge(PG_FUNCTION_ARGS); extern Datum timestamp_gt(PG_FUNCTION_ARGS); extern Datum timestamp_finite(PG_FUNCTION_ARGS); extern Datum timestamp_cmp(PG_FUNCTION_ARGS); +extern Datum timestamp_sortsupport(PG_FUNCTION_ARGS); +extern Datum timestamp_hash(PG_FUNCTION_ARGS); extern Datum timestamp_smaller(PG_FUNCTION_ARGS); extern Datum timestamp_larger(PG_FUNCTION_ARGS); @@ -222,6 +121,10 @@ extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS); extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS); extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS); +extern Datum make_timestamp(PG_FUNCTION_ARGS); +extern Datum make_timestamptz(PG_FUNCTION_ARGS); +extern Datum make_timestamptz_at_timezone(PG_FUNCTION_ARGS); + extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS); extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS); extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS); @@ -236,6 +139,7 @@ extern Datum interval_recv(PG_FUNCTION_ARGS); extern Datum interval_send(PG_FUNCTION_ARGS); extern Datum intervaltypmodin(PG_FUNCTION_ARGS); extern Datum intervaltypmodout(PG_FUNCTION_ARGS); +extern Datum interval_transform(PG_FUNCTION_ARGS); extern Datum interval_scale(PG_FUNCTION_ARGS); extern Datum interval_eq(PG_FUNCTION_ARGS); extern Datum interval_ne(PG_FUNCTION_ARGS); @@ -251,11 +155,8 @@ extern Datum interval_larger(PG_FUNCTION_ARGS); extern Datum interval_justify_interval(PG_FUNCTION_ARGS); extern Datum interval_justify_hours(PG_FUNCTION_ARGS); extern Datum interval_justify_days(PG_FUNCTION_ARGS); +extern Datum make_interval(PG_FUNCTION_ARGS); -extern Datum timestamp_text(PG_FUNCTION_ARGS); -extern Datum text_timestamp(PG_FUNCTION_ARGS); -extern Datum interval_text(PG_FUNCTION_ARGS); -extern Datum text_interval(PG_FUNCTION_ARGS); extern Datum timestamp_trunc(PG_FUNCTION_ARGS); extern Datum interval_trunc(PG_FUNCTION_ARGS); extern Datum timestamp_part(PG_FUNCTION_ARGS); @@ -283,6 +184,7 @@ extern Datum interval_mul(PG_FUNCTION_ARGS); extern Datum mul_d_interval(PG_FUNCTION_ARGS); extern Datum interval_div(PG_FUNCTION_ARGS); extern Datum interval_accum(PG_FUNCTION_ARGS); +extern Datum interval_accum_inv(PG_FUNCTION_ARGS); extern Datum interval_avg(PG_FUNCTION_ARGS); extern Datum timestamp_mi(PG_FUNCTION_ARGS); @@ -291,8 +193,6 @@ extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS); extern Datum timestamp_age(PG_FUNCTION_ARGS); extern Datum overlaps_timestamp(PG_FUNCTION_ARGS); -extern Datum timestamptz_text(PG_FUNCTION_ARGS); -extern Datum text_timestamptz(PG_FUNCTION_ARGS); extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS); extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS); extern Datum timestamptz_age(PG_FUNCTION_ARGS); @@ -303,26 +203,41 @@ extern Datum now(PG_FUNCTION_ARGS); extern Datum statement_timestamp(PG_FUNCTION_ARGS); extern Datum clock_timestamp(PG_FUNCTION_ARGS); -extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS); +extern Datum pg_postmaster_start_time(PG_FUNCTION_ARGS); +extern Datum pg_conf_load_time(PG_FUNCTION_ARGS); + +extern Datum generate_series_timestamp(PG_FUNCTION_ARGS); +extern Datum generate_series_timestamptz(PG_FUNCTION_ARGS); /* Internal routines (not fmgr-callable) */ extern TimestampTz GetCurrentTimestamp(void); - extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs); extern bool TimestampDifferenceExceeds(TimestampTz start_time, - TimestampTz stop_time, - int msec); + TimestampTz stop_time, + int msec); + +/* + * Prototypes for functions to deal with integer timestamps, when the native + * format is float timestamps. + */ +#ifndef HAVE_INT64_TIMESTAMP +extern int64 GetCurrentIntegerTimestamp(void); +extern TimestampTz IntegerTimestampToTimestampTz(int64 timestamp); +#else +#define GetCurrentIntegerTimestamp() GetCurrentTimestamp() +#define IntegerTimestampToTimestampTz(timestamp) (timestamp) +#endif -extern TimestampTz time_t_to_timestamptz(time_t tm); -extern time_t timestamptz_to_time_t(TimestampTz t); +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, char **tzn, pg_tz *attimezone); + 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); @@ -337,8 +252,8 @@ extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2); #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, 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 isowday, int *year, int *mon, int *mday); +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);