]> granicus.if.org Git - postgresql/blobdiff - src/include/utils/timestamp.h
Improve hash_create's API for selecting simple-binary-key hash functions.
[postgresql] / src / include / utils / timestamp.h
index ca3e761a9128a4604e3006481e42c646a0aeb9c7..94328b319368124a903fdce221c4978cf6d4b035 100644 (file)
@@ -1,55 +1,21 @@
 /*-------------------------------------------------------------------------
  *
  * timestamp.h
- *       Definitions for the SQL92 "timestamp" and "interval" types.
+ *       Definitions for the SQL "timestamp" and "interval" types.
  *
- * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: timestamp.h,v 1.26 2002/04/23 15:45:30 tgl Exp $
+ * src/include/utils/timestamp.h
  *
  *-------------------------------------------------------------------------
  */
 #ifndef TIMESTAMP_H
 #define TIMESTAMP_H
 
-#include <time.h>
-#include <math.h>
-#include <limits.h>
-#include <float.h>
-
+#include "datatype/timestamp.h"
 #include "fmgr.h"
-#ifdef HAVE_INT64_TIMESTAMP
-#include "utils/int8.h"
-#endif
-
-/*
- * Timestamp represents absolute time.
- * Interval represents delta time. Keep track of months (and years)
- *     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 months and years */
-#else
-       double          time;   /* all time units other than months and years */
-#endif
-       int32           month;  /* months and years, after time for alignment */
-} Interval;
+#include "pgtime.h"
 
 
 /*
@@ -69,18 +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))
@@ -97,42 +59,33 @@ 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)
+#endif   /* HAVE_INT64_TIMESTAMP */
 
-#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))
 
+#define TIMESTAMP_MASK(b) (1 << (b))
+#define INTERVAL_MASK(b) (1 << (b))
 
-#define MAX_TIMESTAMP_PRECISION 6
-#define MAX_INTERVAL_PRECISION 6
+/* 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)
 
 #ifdef HAVE_INT64_TIMESTAMP
-
-typedef int32 fsec_t;
-
+#define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
 #else
+#define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) / 1000.0))
+#endif
 
-typedef double fsec_t;
 
-#define TIME_PREC_INV 1000000.0
-#define JROUND(j) (rint(((double) (j))*TIME_PREC_INV)/TIME_PREC_INV)
+/* Set at postmaster start */
+extern TimestampTz PgStartTime;
 
-#endif
+/* Set at configuration reload */
+extern TimestampTz PgReloadTime;
 
 
 /*
@@ -141,6 +94,11 @@ typedef double fsec_t;
 
 extern Datum timestamp_in(PG_FUNCTION_ARGS);
 extern Datum timestamp_out(PG_FUNCTION_ARGS);
+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);
@@ -150,11 +108,38 @@ 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);
 
+extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
+extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
+extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
+extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
+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);
+extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
+extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
+extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
+extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);
+
 extern Datum interval_in(PG_FUNCTION_ARGS);
 extern Datum interval_out(PG_FUNCTION_ARGS);
+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);
@@ -167,11 +152,11 @@ extern Datum interval_cmp(PG_FUNCTION_ARGS);
 extern Datum interval_hash(PG_FUNCTION_ARGS);
 extern Datum interval_smaller(PG_FUNCTION_ARGS);
 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);
@@ -182,6 +167,10 @@ extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
 
 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
+extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
+extern Datum timestamptz_send(PG_FUNCTION_ARGS);
+extern Datum timestamptztypmodin(PG_FUNCTION_ARGS);
+extern Datum timestamptztypmodout(PG_FUNCTION_ARGS);
 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
@@ -195,38 +184,78 @@ 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);
-extern Datum timestamp_pl_span(PG_FUNCTION_ARGS);
-extern Datum timestamp_mi_span(PG_FUNCTION_ARGS);
+extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
+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_span(PG_FUNCTION_ARGS);
-extern Datum timestamptz_mi_span(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);
 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
 
 extern Datum now(PG_FUNCTION_ARGS);
+extern Datum statement_timestamp(PG_FUNCTION_ARGS);
+extern Datum clock_timestamp(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 int     tm2timestamp(struct tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
-extern int timestamp2tm(Timestamp dt, int *tzp, struct tm * tm,
-                        fsec_t *fsec, char **tzn);
+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);
+
+/*
+ * 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(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 tm * tm, fsec_t *fsec);
-extern int     tm2interval(struct tm * tm, fsec_t fsec, Interval *span);
+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 tm * tm);
+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 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);
 
 #endif   /* TIMESTAMP_H */