]> 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 6b2efaf8d78b0e2c957360e1fda9bfd39c533b14..94328b319368124a903fdce221c4978cf6d4b035 100644 (file)
 /*-------------------------------------------------------------------------
  *
  * timestamp.h
- *       Definitions for the SQL92 "timestamp" and "interval" types.
+ *       Definitions for the SQL "timestamp" and "interval" types.
  *
- * Portions Copyright (c) 1996-2008, 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.78 2008/05/04 23:19:24 tgl Exp $
+ * src/include/utils/timestamp.h
  *
  *-------------------------------------------------------------------------
  */
 #ifndef TIMESTAMP_H
 #define TIMESTAMP_H
 
-#include <math.h>
-#include <limits.h>
-#include <float.h>
-
+#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 hours/minutes/seconds 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
- *
- * We have two implementations, one that uses int64 values with units of
- * microseconds, and one that uses double values with units of seconds.
- *
- * TimeOffset and fsec_t are convenience typedefs for temporary variables
- * that are of different types in the two cases.  Do not use fsec_t in values
- * stored on-disk, since it is not the same size in both implementations.
- */
-
-#ifdef HAVE_INT64_TIMESTAMP
-
-typedef int64 Timestamp;
-typedef int64 TimestampTz;
-typedef int64 TimeOffset;
-typedef int32 fsec_t;                  /* fractional seconds (in microseconds) */
-
-#else
-
-typedef double Timestamp;
-typedef double TimestampTz;
-typedef double TimeOffset;
-typedef double fsec_t;                 /* fractional seconds (in seconds) */
-
-#endif
-
-typedef struct
-{
-       TimeOffset      time;                   /* all time units other than days, months and
-                                                                * years */
-       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.
@@ -124,11 +42,7 @@ 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)
-
-#define DT_NOBEGIN             (-INT64CONST(0x7fffffffffffffff) - 1)
-#define DT_NOEND               (INT64CONST(0x7fffffffffffffff))
-
-#else  /* !HAVE_INT64_TIMESTAMP */
+#else                                                  /* !HAVE_INT64_TIMESTAMP */
 
 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetFloat8(X))
 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X))
@@ -145,35 +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))
-
-/*
- *     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))
 
@@ -195,6 +83,7 @@ typedef struct
 
 /* Set at postmaster start */
 extern TimestampTz PgStartTime;
+
 /* Set at configuration reload */
 extern TimestampTz PgReloadTime;
 
@@ -209,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);
@@ -218,6 +108,7 @@ 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);
@@ -230,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);
@@ -244,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);
@@ -259,6 +155,7 @@ 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_trunc(PG_FUNCTION_ARGS);
 extern Datum interval_trunc(PG_FUNCTION_ARGS);
@@ -287,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);
@@ -314,13 +212,24 @@ 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);
 
+/*
+ * 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);
 
@@ -328,7 +237,7 @@ 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);
@@ -344,7 +253,7 @@ extern int  timestamp_cmp_internal(Timestamp dt1, Timestamp 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 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);