Header And Logo

PostgreSQL
| The world's most advanced open source database.

timestamp.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * timestamp.h
00004  *    Definitions for the SQL "timestamp" and "interval" types.
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  * src/include/utils/timestamp.h
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #ifndef TIMESTAMP_H
00014 #define TIMESTAMP_H
00015 
00016 #include "datatype/timestamp.h"
00017 #include "fmgr.h"
00018 #include "pgtime.h"
00019 
00020 
00021 /*
00022  * Macros for fmgr-callable functions.
00023  *
00024  * For Timestamp, we make use of the same support routines as for int64
00025  * or float8.  Therefore Timestamp is pass-by-reference if and only if
00026  * int64 or float8 is!
00027  */
00028 #ifdef HAVE_INT64_TIMESTAMP
00029 
00030 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetInt64(X))
00031 #define DatumGetTimestampTz(X)  ((TimestampTz) DatumGetInt64(X))
00032 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
00033 
00034 #define TimestampGetDatum(X) Int64GetDatum(X)
00035 #define TimestampTzGetDatum(X) Int64GetDatum(X)
00036 #define IntervalPGetDatum(X) PointerGetDatum(X)
00037 
00038 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
00039 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
00040 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
00041 
00042 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
00043 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
00044 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
00045 #else                           /* !HAVE_INT64_TIMESTAMP */
00046 
00047 #define DatumGetTimestamp(X)  ((Timestamp) DatumGetFloat8(X))
00048 #define DatumGetTimestampTz(X)  ((TimestampTz) DatumGetFloat8(X))
00049 #define DatumGetIntervalP(X)  ((Interval *) DatumGetPointer(X))
00050 
00051 #define TimestampGetDatum(X) Float8GetDatum(X)
00052 #define TimestampTzGetDatum(X) Float8GetDatum(X)
00053 #define IntervalPGetDatum(X) PointerGetDatum(X)
00054 
00055 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
00056 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
00057 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
00058 
00059 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
00060 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
00061 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
00062 #endif   /* HAVE_INT64_TIMESTAMP */
00063 
00064 
00065 #define TIMESTAMP_MASK(b) (1 << (b))
00066 #define INTERVAL_MASK(b) (1 << (b))
00067 
00068 /* Macros to handle packing and unpacking the typmod field for intervals */
00069 #define INTERVAL_FULL_RANGE (0x7FFF)
00070 #define INTERVAL_RANGE_MASK (0x7FFF)
00071 #define INTERVAL_FULL_PRECISION (0xFFFF)
00072 #define INTERVAL_PRECISION_MASK (0xFFFF)
00073 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
00074 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
00075 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
00076 
00077 #ifdef HAVE_INT64_TIMESTAMP
00078 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
00079 #else
00080 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) / 1000.0))
00081 #endif
00082 
00083 
00084 /* Set at postmaster start */
00085 extern TimestampTz PgStartTime;
00086 
00087 /* Set at configuration reload */
00088 extern TimestampTz PgReloadTime;
00089 
00090 
00091 /*
00092  * timestamp.c prototypes
00093  */
00094 
00095 extern Datum timestamp_in(PG_FUNCTION_ARGS);
00096 extern Datum timestamp_out(PG_FUNCTION_ARGS);
00097 extern Datum timestamp_recv(PG_FUNCTION_ARGS);
00098 extern Datum timestamp_send(PG_FUNCTION_ARGS);
00099 extern Datum timestamptypmodin(PG_FUNCTION_ARGS);
00100 extern Datum timestamptypmodout(PG_FUNCTION_ARGS);
00101 extern Datum timestamp_transform(PG_FUNCTION_ARGS);
00102 extern Datum timestamp_scale(PG_FUNCTION_ARGS);
00103 extern Datum timestamp_eq(PG_FUNCTION_ARGS);
00104 extern Datum timestamp_ne(PG_FUNCTION_ARGS);
00105 extern Datum timestamp_lt(PG_FUNCTION_ARGS);
00106 extern Datum timestamp_le(PG_FUNCTION_ARGS);
00107 extern Datum timestamp_ge(PG_FUNCTION_ARGS);
00108 extern Datum timestamp_gt(PG_FUNCTION_ARGS);
00109 extern Datum timestamp_finite(PG_FUNCTION_ARGS);
00110 extern Datum timestamp_cmp(PG_FUNCTION_ARGS);
00111 extern Datum timestamp_sortsupport(PG_FUNCTION_ARGS);
00112 extern Datum timestamp_hash(PG_FUNCTION_ARGS);
00113 extern Datum timestamp_smaller(PG_FUNCTION_ARGS);
00114 extern Datum timestamp_larger(PG_FUNCTION_ARGS);
00115 
00116 extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS);
00117 extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS);
00118 extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS);
00119 extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS);
00120 extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS);
00121 extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS);
00122 extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS);
00123 
00124 extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS);
00125 extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS);
00126 extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS);
00127 extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS);
00128 extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS);
00129 extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS);
00130 extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS);
00131 
00132 extern Datum interval_in(PG_FUNCTION_ARGS);
00133 extern Datum interval_out(PG_FUNCTION_ARGS);
00134 extern Datum interval_recv(PG_FUNCTION_ARGS);
00135 extern Datum interval_send(PG_FUNCTION_ARGS);
00136 extern Datum intervaltypmodin(PG_FUNCTION_ARGS);
00137 extern Datum intervaltypmodout(PG_FUNCTION_ARGS);
00138 extern Datum interval_transform(PG_FUNCTION_ARGS);
00139 extern Datum interval_scale(PG_FUNCTION_ARGS);
00140 extern Datum interval_eq(PG_FUNCTION_ARGS);
00141 extern Datum interval_ne(PG_FUNCTION_ARGS);
00142 extern Datum interval_lt(PG_FUNCTION_ARGS);
00143 extern Datum interval_le(PG_FUNCTION_ARGS);
00144 extern Datum interval_ge(PG_FUNCTION_ARGS);
00145 extern Datum interval_gt(PG_FUNCTION_ARGS);
00146 extern Datum interval_finite(PG_FUNCTION_ARGS);
00147 extern Datum interval_cmp(PG_FUNCTION_ARGS);
00148 extern Datum interval_hash(PG_FUNCTION_ARGS);
00149 extern Datum interval_smaller(PG_FUNCTION_ARGS);
00150 extern Datum interval_larger(PG_FUNCTION_ARGS);
00151 extern Datum interval_justify_interval(PG_FUNCTION_ARGS);
00152 extern Datum interval_justify_hours(PG_FUNCTION_ARGS);
00153 extern Datum interval_justify_days(PG_FUNCTION_ARGS);
00154 
00155 extern Datum timestamp_trunc(PG_FUNCTION_ARGS);
00156 extern Datum interval_trunc(PG_FUNCTION_ARGS);
00157 extern Datum timestamp_part(PG_FUNCTION_ARGS);
00158 extern Datum interval_part(PG_FUNCTION_ARGS);
00159 extern Datum timestamp_zone(PG_FUNCTION_ARGS);
00160 extern Datum timestamp_izone(PG_FUNCTION_ARGS);
00161 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS);
00162 
00163 extern Datum timestamptz_in(PG_FUNCTION_ARGS);
00164 extern Datum timestamptz_out(PG_FUNCTION_ARGS);
00165 extern Datum timestamptz_recv(PG_FUNCTION_ARGS);
00166 extern Datum timestamptz_send(PG_FUNCTION_ARGS);
00167 extern Datum timestamptztypmodin(PG_FUNCTION_ARGS);
00168 extern Datum timestamptztypmodout(PG_FUNCTION_ARGS);
00169 extern Datum timestamptz_scale(PG_FUNCTION_ARGS);
00170 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS);
00171 extern Datum timestamptz_zone(PG_FUNCTION_ARGS);
00172 extern Datum timestamptz_izone(PG_FUNCTION_ARGS);
00173 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS);
00174 
00175 extern Datum interval_um(PG_FUNCTION_ARGS);
00176 extern Datum interval_pl(PG_FUNCTION_ARGS);
00177 extern Datum interval_mi(PG_FUNCTION_ARGS);
00178 extern Datum interval_mul(PG_FUNCTION_ARGS);
00179 extern Datum mul_d_interval(PG_FUNCTION_ARGS);
00180 extern Datum interval_div(PG_FUNCTION_ARGS);
00181 extern Datum interval_accum(PG_FUNCTION_ARGS);
00182 extern Datum interval_avg(PG_FUNCTION_ARGS);
00183 
00184 extern Datum timestamp_mi(PG_FUNCTION_ARGS);
00185 extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS);
00186 extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS);
00187 extern Datum timestamp_age(PG_FUNCTION_ARGS);
00188 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS);
00189 
00190 extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS);
00191 extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS);
00192 extern Datum timestamptz_age(PG_FUNCTION_ARGS);
00193 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS);
00194 extern Datum timestamptz_part(PG_FUNCTION_ARGS);
00195 
00196 extern Datum now(PG_FUNCTION_ARGS);
00197 extern Datum statement_timestamp(PG_FUNCTION_ARGS);
00198 extern Datum clock_timestamp(PG_FUNCTION_ARGS);
00199 
00200 extern Datum pg_postmaster_start_time(PG_FUNCTION_ARGS);
00201 extern Datum pg_conf_load_time(PG_FUNCTION_ARGS);
00202 
00203 extern Datum generate_series_timestamp(PG_FUNCTION_ARGS);
00204 extern Datum generate_series_timestamptz(PG_FUNCTION_ARGS);
00205 
00206 /* Internal routines (not fmgr-callable) */
00207 
00208 extern TimestampTz GetCurrentTimestamp(void);
00209 extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
00210                     long *secs, int *microsecs);
00211 extern bool TimestampDifferenceExceeds(TimestampTz start_time,
00212                            TimestampTz stop_time,
00213                            int msec);
00214 
00215 /*
00216  * Prototypes for functions to deal with integer timestamps, when the native
00217  * format is float timestamps.
00218  */
00219 #ifndef HAVE_INT64_TIMESTAMP
00220 extern int64 GetCurrentIntegerTimestamp(void);
00221 extern TimestampTz IntegerTimestampToTimestampTz(int64 timestamp);
00222 #else
00223 #define GetCurrentIntegerTimestamp()    GetCurrentTimestamp()
00224 #define IntegerTimestampToTimestampTz(timestamp) (timestamp)
00225 #endif
00226 
00227 extern TimestampTz time_t_to_timestamptz(pg_time_t tm);
00228 extern pg_time_t timestamptz_to_time_t(TimestampTz t);
00229 
00230 extern const char *timestamptz_to_str(TimestampTz t);
00231 
00232 extern int  tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
00233 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm,
00234              fsec_t *fsec, const char **tzn, pg_tz *attimezone);
00235 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
00236 
00237 extern int  interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec);
00238 extern int  tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span);
00239 
00240 extern Timestamp SetEpochTimestamp(void);
00241 extern void GetEpochTime(struct pg_tm * tm);
00242 
00243 extern int  timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
00244 
00245 /* timestamp comparison works for timestamptz also */
00246 #define timestamptz_cmp_internal(dt1,dt2)   timestamp_cmp_internal(dt1, dt2)
00247 
00248 extern int  isoweek2j(int year, int week);
00249 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
00250 extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
00251 extern int  date2isoweek(int year, int mon, int mday);
00252 extern int  date2isoyear(int year, int mon, int mday);
00253 extern int  date2isoyearday(int year, int mon, int mday);
00254 
00255 #endif   /* TIMESTAMP_H */