Eneboo - Documentación para desarrolladores
|
00001 /*------------------------------------------------------------------------- 00002 * 00003 * timestamp.h 00004 * Definitions for the SQL92 "timestamp" and "interval" types. 00005 * 00006 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group 00007 * Portions Copyright (c) 1994, Regents of the University of California 00008 * 00009 * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.57 2005/10/15 02:49:46 momjian Exp $ 00010 * 00011 *------------------------------------------------------------------------- 00012 */ 00013 #ifndef TIMESTAMP_H 00014 #define TIMESTAMP_H 00015 00016 #include <math.h> 00017 #include <limits.h> 00018 #include <float.h> 00019 00020 #include "fmgr.h" 00021 #include "pgtime.h" 00022 #ifdef HAVE_INT64_TIMESTAMP 00023 #include "utils/int8.h" 00024 #endif 00025 00026 /* 00027 * Timestamp represents absolute time. 00028 * Interval represents delta time. Keep track of months (and years), days, 00029 * and time separately since the elapsed time spanned is unknown until 00030 * instantiated relative to an absolute time. 00031 * 00032 * Note that Postgres uses "time interval" to mean a bounded interval, 00033 * consisting of a beginning and ending time, not a time span - thomas 97/03/20 00034 */ 00035 00036 #ifdef HAVE_INT64_TIMESTAMP 00037 typedef int64 Timestamp; 00038 typedef int64 TimestampTz; 00039 #else 00040 typedef double Timestamp; 00041 typedef double TimestampTz; 00042 #endif 00043 00044 typedef struct 00045 { 00046 #ifdef HAVE_INT64_TIMESTAMP 00047 int64 time; /* all time units other than days, months and 00048 * years */ 00049 #else 00050 double time; /* all time units other than days, months and 00051 * years */ 00052 #endif 00053 int32 day; /* days, after time for alignment */ 00054 int32 month; /* months and years, after time for alignment */ 00055 } Interval; 00056 00057 00058 #define MAX_TIMESTAMP_PRECISION 6 00059 #define MAX_INTERVAL_PRECISION 6 00060 00061 /* in both timestamp.h and ecpg/dt.h */ 00062 #define DAYS_PER_YEAR 365.25 /* assumes leap year every four years */ 00063 #define MONTHS_PER_YEAR 12 00064 /* 00065 * DAYS_PER_MONTH is very imprecise. The more accurate value is 00066 * 365.2425/12 = 30.436875, or '30 days 10:29:06'. Right now we only 00067 * return an integral number of days, but someday perhaps we should 00068 * also return a 'time' value to be used as well. ISO 8601 suggests 00069 * 30 days. 00070 */ 00071 #define DAYS_PER_MONTH 30 /* assumes exactly 30 days per month */ 00072 #define HOURS_PER_DAY 24 /* assume no daylight savings time changes */ 00073 00074 /* 00075 * This doesn't adjust for uneven daylight savings time intervals or leap 00076 * seconds, and it crudely estimates leap years. A more accurate value 00077 * for days per years is 365.2422. 00078 */ 00079 #define SECS_PER_YEAR (36525 * 864) /* avoid floating-point computation */ 00080 #define SECS_PER_DAY 86400 00081 #define SECS_PER_HOUR 3600 00082 #define SECS_PER_MINUTE 60 00083 #define MINS_PER_HOUR 60 00084 00085 #ifdef HAVE_INT64_TIMESTAMP 00086 #define USECS_PER_DAY INT64CONST(86400000000) 00087 #define USECS_PER_HOUR INT64CONST(3600000000) 00088 #define USECS_PER_MINUTE INT64CONST(60000000) 00089 #define USECS_PER_SEC INT64CONST(1000000) 00090 #endif 00091 00092 /* 00093 * Macros for fmgr-callable functions. 00094 * 00095 * For Timestamp, we make use of the same support routines as for int64 00096 * or float8. Therefore Timestamp is pass-by-reference if and only if 00097 * int64 or float8 is! 00098 */ 00099 #ifdef HAVE_INT64_TIMESTAMP 00100 00101 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X)) 00102 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X)) 00103 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X)) 00104 00105 #define TimestampGetDatum(X) Int64GetDatum(X) 00106 #define TimestampTzGetDatum(X) Int64GetDatum(X) 00107 #define IntervalPGetDatum(X) PointerGetDatum(X) 00108 00109 #define PG_GETARG_TIMESTAMP(n) PG_GETARG_INT64(n) 00110 #define PG_GETARG_TIMESTAMPTZ(n) PG_GETARG_INT64(n) 00111 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n)) 00112 00113 #define PG_RETURN_TIMESTAMP(x) PG_RETURN_INT64(x) 00114 #define PG_RETURN_TIMESTAMPTZ(x) PG_RETURN_INT64(x) 00115 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x) 00116 00117 #define DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1) 00118 #define DT_NOEND (INT64CONST(0x7fffffffffffffff)) 00119 #else 00120 00121 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X)) 00122 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X)) 00123 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X)) 00124 00125 #define TimestampGetDatum(X) Float8GetDatum(X) 00126 #define TimestampTzGetDatum(X) Float8GetDatum(X) 00127 #define IntervalPGetDatum(X) PointerGetDatum(X) 00128 00129 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n)) 00130 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n)) 00131 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n)) 00132 00133 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x) 00134 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x) 00135 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x) 00136 00137 #ifdef HUGE_VAL 00138 #define DT_NOBEGIN (-HUGE_VAL) 00139 #define DT_NOEND (HUGE_VAL) 00140 #else 00141 #define DT_NOBEGIN (-DBL_MAX) 00142 #define DT_NOEND (DBL_MAX) 00143 #endif 00144 #endif /* HAVE_INT64_TIMESTAMP */ 00145 00146 00147 #define TIMESTAMP_NOBEGIN(j) do {(j) = DT_NOBEGIN;} while (0) 00148 #define TIMESTAMP_IS_NOBEGIN(j) ((j) == DT_NOBEGIN) 00149 00150 #define TIMESTAMP_NOEND(j) do {(j) = DT_NOEND;} while (0) 00151 #define TIMESTAMP_IS_NOEND(j) ((j) == DT_NOEND) 00152 00153 #define TIMESTAMP_NOT_FINITE(j) (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j)) 00154 00155 #ifdef HAVE_INT64_TIMESTAMP 00156 00157 typedef int32 fsec_t; 00158 #else 00159 00160 typedef double fsec_t; 00161 00162 /* round off to MAX_TIMESTAMP_PRECISION decimal places */ 00163 /* note: this is also used for rounding off intervals */ 00164 #define TS_PREC_INV 1000000.0 00165 #define TSROUND(j) (rint(((double) (j)) * TS_PREC_INV) / TS_PREC_INV) 00166 #endif 00167 00168 #define TIMESTAMP_MASK(b) (1 << (b)) 00169 #define INTERVAL_MASK(b) (1 << (b)) 00170 00171 /* Macros to handle packing and unpacking the typmod field for intervals */ 00172 #define INTERVAL_FULL_RANGE (0x7FFF) 00173 #define INTERVAL_RANGE_MASK (0x7FFF) 00174 #define INTERVAL_FULL_PRECISION (0xFFFF) 00175 #define INTERVAL_PRECISION_MASK (0xFFFF) 00176 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK)) 00177 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK) 00178 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK) 00179 00180 00181 /* Set at postmaster start */ 00182 extern TimestampTz PgStartTime; 00183 00184 00185 /* 00186 * timestamp.c prototypes 00187 */ 00188 00189 extern Datum timestamp_in(PG_FUNCTION_ARGS); 00190 extern Datum timestamp_out(PG_FUNCTION_ARGS); 00191 extern Datum timestamp_recv(PG_FUNCTION_ARGS); 00192 extern Datum timestamp_send(PG_FUNCTION_ARGS); 00193 extern Datum timestamp_scale(PG_FUNCTION_ARGS); 00194 extern Datum timestamp_eq(PG_FUNCTION_ARGS); 00195 extern Datum timestamp_ne(PG_FUNCTION_ARGS); 00196 extern Datum timestamp_lt(PG_FUNCTION_ARGS); 00197 extern Datum timestamp_le(PG_FUNCTION_ARGS); 00198 extern Datum timestamp_ge(PG_FUNCTION_ARGS); 00199 extern Datum timestamp_gt(PG_FUNCTION_ARGS); 00200 extern Datum timestamp_finite(PG_FUNCTION_ARGS); 00201 extern Datum timestamp_cmp(PG_FUNCTION_ARGS); 00202 extern Datum timestamp_smaller(PG_FUNCTION_ARGS); 00203 extern Datum timestamp_larger(PG_FUNCTION_ARGS); 00204 00205 extern Datum timestamp_eq_timestamptz(PG_FUNCTION_ARGS); 00206 extern Datum timestamp_ne_timestamptz(PG_FUNCTION_ARGS); 00207 extern Datum timestamp_lt_timestamptz(PG_FUNCTION_ARGS); 00208 extern Datum timestamp_le_timestamptz(PG_FUNCTION_ARGS); 00209 extern Datum timestamp_gt_timestamptz(PG_FUNCTION_ARGS); 00210 extern Datum timestamp_ge_timestamptz(PG_FUNCTION_ARGS); 00211 extern Datum timestamp_cmp_timestamptz(PG_FUNCTION_ARGS); 00212 00213 extern Datum timestamptz_eq_timestamp(PG_FUNCTION_ARGS); 00214 extern Datum timestamptz_ne_timestamp(PG_FUNCTION_ARGS); 00215 extern Datum timestamptz_lt_timestamp(PG_FUNCTION_ARGS); 00216 extern Datum timestamptz_le_timestamp(PG_FUNCTION_ARGS); 00217 extern Datum timestamptz_gt_timestamp(PG_FUNCTION_ARGS); 00218 extern Datum timestamptz_ge_timestamp(PG_FUNCTION_ARGS); 00219 extern Datum timestamptz_cmp_timestamp(PG_FUNCTION_ARGS); 00220 00221 extern Datum interval_in(PG_FUNCTION_ARGS); 00222 extern Datum interval_out(PG_FUNCTION_ARGS); 00223 extern Datum interval_recv(PG_FUNCTION_ARGS); 00224 extern Datum interval_send(PG_FUNCTION_ARGS); 00225 extern Datum interval_scale(PG_FUNCTION_ARGS); 00226 extern Datum interval_eq(PG_FUNCTION_ARGS); 00227 extern Datum interval_ne(PG_FUNCTION_ARGS); 00228 extern Datum interval_lt(PG_FUNCTION_ARGS); 00229 extern Datum interval_le(PG_FUNCTION_ARGS); 00230 extern Datum interval_ge(PG_FUNCTION_ARGS); 00231 extern Datum interval_gt(PG_FUNCTION_ARGS); 00232 extern Datum interval_finite(PG_FUNCTION_ARGS); 00233 extern Datum interval_cmp(PG_FUNCTION_ARGS); 00234 extern Datum interval_hash(PG_FUNCTION_ARGS); 00235 extern Datum interval_smaller(PG_FUNCTION_ARGS); 00236 extern Datum interval_larger(PG_FUNCTION_ARGS); 00237 extern Datum interval_justify_hours(PG_FUNCTION_ARGS); 00238 extern Datum interval_justify_days(PG_FUNCTION_ARGS); 00239 00240 extern Datum timestamp_text(PG_FUNCTION_ARGS); 00241 extern Datum text_timestamp(PG_FUNCTION_ARGS); 00242 extern Datum interval_text(PG_FUNCTION_ARGS); 00243 extern Datum text_interval(PG_FUNCTION_ARGS); 00244 extern Datum timestamp_trunc(PG_FUNCTION_ARGS); 00245 extern Datum interval_trunc(PG_FUNCTION_ARGS); 00246 extern Datum timestamp_part(PG_FUNCTION_ARGS); 00247 extern Datum interval_part(PG_FUNCTION_ARGS); 00248 extern Datum timestamp_zone(PG_FUNCTION_ARGS); 00249 extern Datum timestamp_izone(PG_FUNCTION_ARGS); 00250 extern Datum timestamp_timestamptz(PG_FUNCTION_ARGS); 00251 00252 extern Datum timestamptz_in(PG_FUNCTION_ARGS); 00253 extern Datum timestamptz_out(PG_FUNCTION_ARGS); 00254 extern Datum timestamptz_recv(PG_FUNCTION_ARGS); 00255 extern Datum timestamptz_send(PG_FUNCTION_ARGS); 00256 extern Datum timestamptz_scale(PG_FUNCTION_ARGS); 00257 extern Datum timestamptz_timestamp(PG_FUNCTION_ARGS); 00258 extern Datum timestamptz_zone(PG_FUNCTION_ARGS); 00259 extern Datum timestamptz_izone(PG_FUNCTION_ARGS); 00260 extern Datum timestamptz_timestamptz(PG_FUNCTION_ARGS); 00261 00262 extern Datum interval_um(PG_FUNCTION_ARGS); 00263 extern Datum interval_pl(PG_FUNCTION_ARGS); 00264 extern Datum interval_mi(PG_FUNCTION_ARGS); 00265 extern Datum interval_mul(PG_FUNCTION_ARGS); 00266 extern Datum mul_d_interval(PG_FUNCTION_ARGS); 00267 extern Datum interval_div(PG_FUNCTION_ARGS); 00268 extern Datum interval_accum(PG_FUNCTION_ARGS); 00269 extern Datum interval_avg(PG_FUNCTION_ARGS); 00270 00271 extern Datum timestamp_mi(PG_FUNCTION_ARGS); 00272 extern Datum timestamp_pl_interval(PG_FUNCTION_ARGS); 00273 extern Datum timestamp_mi_interval(PG_FUNCTION_ARGS); 00274 extern Datum timestamp_age(PG_FUNCTION_ARGS); 00275 extern Datum overlaps_timestamp(PG_FUNCTION_ARGS); 00276 00277 extern Datum timestamptz_text(PG_FUNCTION_ARGS); 00278 extern Datum text_timestamptz(PG_FUNCTION_ARGS); 00279 extern Datum timestamptz_pl_interval(PG_FUNCTION_ARGS); 00280 extern Datum timestamptz_mi_interval(PG_FUNCTION_ARGS); 00281 extern Datum timestamptz_age(PG_FUNCTION_ARGS); 00282 extern Datum timestamptz_trunc(PG_FUNCTION_ARGS); 00283 extern Datum timestamptz_part(PG_FUNCTION_ARGS); 00284 00285 extern Datum now(PG_FUNCTION_ARGS); 00286 00287 extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS); 00288 00289 /* Internal routines (not fmgr-callable) */ 00290 00291 extern TimestampTz GetCurrentTimestamp(void); 00292 00293 extern TimestampTz time_t_to_timestamptz(time_t tm); 00294 00295 extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt); 00296 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, 00297 fsec_t *fsec, char **tzn, pg_tz *attimezone); 00298 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec); 00299 00300 extern int interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec); 00301 extern int tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span); 00302 00303 extern Timestamp SetEpochTimestamp(void); 00304 extern void GetEpochTime(struct pg_tm * tm); 00305 00306 extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2); 00307 00308 /* timestamp comparison works for timestamptz also */ 00309 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2) 00310 00311 extern void isoweek2date(int woy, int *year, int *mon, int *mday); 00312 extern int date2isoweek(int year, int mon, int mday); 00313 extern int date2isoyear(int year, int mon, int mday); 00314 00315 #endif /* TIMESTAMP_H */