Eneboo - Documentación para desarrolladores
src/libpq/include/utils/datetime.h
Ir a la documentación de este archivo.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * datetime.h
00004  *        Definitions for the date/time and other date/time support code.
00005  *        The support code is shared with other date data types,
00006  *         including abstime, reltime, date, and time.
00007  *
00008  *
00009  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
00010  * Portions Copyright (c) 1994, Regents of the University of California
00011  *
00012  * $PostgreSQL: pgsql/src/include/utils/datetime.h,v 1.57 2005/10/15 02:49:46 momjian Exp $
00013  *
00014  *-------------------------------------------------------------------------
00015  */
00016 #ifndef DATETIME_H
00017 #define DATETIME_H
00018 
00019 #include <limits.h>
00020 #include <math.h>
00021 
00022 #include "utils/timestamp.h"
00023 
00024 
00025 /* ----------------------------------------------------------------
00026  *                              time types + support macros
00027  *
00028  * String definitions for standard time quantities.
00029  *
00030  * These strings are the defaults used to form output time strings.
00031  * Other alternate forms are hardcoded into token tables in datetime.c.
00032  * ----------------------------------------------------------------
00033  */
00034 
00035 #define DAGO                    "ago"
00036 #define DCURRENT                "current"
00037 #define EPOCH                   "epoch"
00038 #define INVALID                 "invalid"
00039 #define EARLY                   "-infinity"
00040 #define LATE                    "infinity"
00041 #define NOW                             "now"
00042 #define TODAY                   "today"
00043 #define TOMORROW                "tomorrow"
00044 #define YESTERDAY               "yesterday"
00045 #define ZULU                    "zulu"
00046 
00047 #define DMICROSEC               "usecond"
00048 #define DMILLISEC               "msecond"
00049 #define DSECOND                 "second"
00050 #define DMINUTE                 "minute"
00051 #define DHOUR                   "hour"
00052 #define DDAY                    "day"
00053 #define DWEEK                   "week"
00054 #define DMONTH                  "month"
00055 #define DQUARTER                "quarter"
00056 #define DYEAR                   "year"
00057 #define DDECADE                 "decade"
00058 #define DCENTURY                "century"
00059 #define DMILLENNIUM             "millennium"
00060 #define DA_D                    "ad"
00061 #define DB_C                    "bc"
00062 #define DTIMEZONE               "timezone"
00063 
00064 /*
00065  * Fundamental time field definitions for parsing.
00066  *
00067  *      Meridian:  am, pm, or 24-hour style.
00068  *      Millennium: ad, bc
00069  */
00070 
00071 #define AM              0
00072 #define PM              1
00073 #define HR24    2
00074 
00075 #define AD              0
00076 #define BC              1
00077 
00078 /*
00079  * Fields for time decoding.
00080  *
00081  * Can't have more of these than there are bits in an unsigned int
00082  * since these are turned into bit masks during parsing and decoding.
00083  *
00084  * Furthermore, the values for YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
00085  * must be in the range 0..14 so that the associated bitmasks can fit
00086  * into the left half of an INTERVAL's typmod value.
00087  */
00088 
00089 #define RESERV  0
00090 #define MONTH   1
00091 #define YEAR    2
00092 #define DAY             3
00093 #define JULIAN  4
00094 #define TZ              5
00095 #define DTZ             6
00096 #define DTZMOD  7
00097 #define IGNORE_DTF      8
00098 #define AMPM    9
00099 #define HOUR    10
00100 #define MINUTE  11
00101 #define SECOND  12
00102 #define DOY             13
00103 #define DOW             14
00104 #define UNITS   15
00105 #define ADBC    16
00106 /* these are only for relative dates */
00107 #define AGO             17
00108 #define ABS_BEFORE              18
00109 #define ABS_AFTER               19
00110 /* generic fields to help with parsing */
00111 #define ISODATE 20
00112 #define ISOTIME 21
00113 /* reserved for unrecognized string values */
00114 #define UNKNOWN_FIELD   31
00115 
00116 /*
00117  * Token field definitions for time parsing and decoding.
00118  * These need to fit into the datetkn table type.
00119  * At the moment, that means keep them within [-127,127].
00120  * These are also used for bit masks in DecodeDateDelta()
00121  *      so actually restrict them to within [0,31] for now.
00122  * - thomas 97/06/19
00123  * Not all of these fields are used for masks in DecodeDateDelta
00124  *      so allow some larger than 31. - thomas 1997-11-17
00125  */
00126 
00127 #define DTK_NUMBER              0
00128 #define DTK_STRING              1
00129 
00130 #define DTK_DATE                2
00131 #define DTK_TIME                3
00132 #define DTK_TZ                  4
00133 #define DTK_AGO                 5
00134 
00135 #define DTK_SPECIAL             6
00136 #define DTK_INVALID             7
00137 #define DTK_CURRENT             8
00138 #define DTK_EARLY               9
00139 #define DTK_LATE                10
00140 #define DTK_EPOCH               11
00141 #define DTK_NOW                 12
00142 #define DTK_YESTERDAY   13
00143 #define DTK_TODAY               14
00144 #define DTK_TOMORROW    15
00145 #define DTK_ZULU                16
00146 
00147 #define DTK_DELTA               17
00148 #define DTK_SECOND              18
00149 #define DTK_MINUTE              19
00150 #define DTK_HOUR                20
00151 #define DTK_DAY                 21
00152 #define DTK_WEEK                22
00153 #define DTK_MONTH               23
00154 #define DTK_QUARTER             24
00155 #define DTK_YEAR                25
00156 #define DTK_DECADE              26
00157 #define DTK_CENTURY             27
00158 #define DTK_MILLENNIUM  28
00159 #define DTK_MILLISEC    29
00160 #define DTK_MICROSEC    30
00161 #define DTK_JULIAN              31
00162 
00163 #define DTK_DOW                 32
00164 #define DTK_DOY                 33
00165 #define DTK_TZ_HOUR             34
00166 #define DTK_TZ_MINUTE   35
00167 
00168 
00169 /*
00170  * Bit mask definitions for time parsing.
00171  */
00172 
00173 #define DTK_M(t)                (0x01 << (t))
00174 
00175 #define DTK_DATE_M              (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
00176 #define DTK_TIME_M              (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_M(SECOND))
00177 
00178 #define MAXDATELEN              51              /* maximum possible length of an input date
00179                                                                  * string (not counting tr. null) */
00180 #define MAXDATEFIELDS   25              /* maximum possible number of fields in a date
00181                                                                  * string */
00182 #define TOKMAXLEN               10              /* only this many chars are stored in
00183                                                                  * datetktbl */
00184 
00185 /* keep this struct small; it gets used a lot */
00186 typedef struct
00187 {
00188 #if defined(_AIX)
00189         char       *token;
00190 #else
00191         char            token[TOKMAXLEN];
00192 #endif   /* _AIX */
00193         char            type;
00194         char            value;                  /* this may be unsigned, alas */
00195 } datetkn;
00196 
00197 
00198 /* FMODULO()
00199  * Macro to replace modf(), which is broken on some platforms.
00200  * t = input and remainder
00201  * q = integer part
00202  * u = divisor
00203  */
00204 #define FMODULO(t,q,u) \
00205 do { \
00206         (q) = (((t) < 0) ? ceil((t) / (u)) : floor((t) / (u))); \
00207         if ((q) != 0) (t) -= rint((q) * (u)); \
00208 } while(0)
00209 
00210 /* TMODULO()
00211  * Like FMODULO(), but work on the timestamp datatype (either int64 or float8).
00212  * We assume that int64 follows the C99 semantics for division (negative
00213  * quotients truncate towards zero).
00214  */
00215 #ifdef HAVE_INT64_TIMESTAMP
00216 #define TMODULO(t,q,u) \
00217 do { \
00218         (q) = ((t) / (u)); \
00219         if ((q) != 0) (t) -= ((q) * (u)); \
00220 } while(0)
00221 #else
00222 #define TMODULO(t,q,u) \
00223 do { \
00224         (q) = (((t) < 0) ? ceil((t) / (u)) : floor((t) / (u))); \
00225         if ((q) != 0) (t) -= rint((q) * (u)); \
00226 } while(0)
00227 #endif
00228 
00229 /*
00230  * Date/time validation
00231  * Include check for leap year.
00232  */
00233 
00234 extern const int day_tab[2][13];
00235 
00236 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
00237 
00238 
00239 /* Julian date support for date2j() and j2date()
00240  *
00241  * IS_VALID_JULIAN checks the minimum date exactly, but is a bit sloppy
00242  * about the maximum, since it's far enough out to not be especially
00243  * interesting.
00244  */
00245 
00246 #define JULIAN_MINYEAR (-4713)
00247 #define JULIAN_MINMONTH (11)
00248 #define JULIAN_MINDAY (24)
00249 #define JULIAN_MAXYEAR (5874898)
00250 
00251 #define IS_VALID_JULIAN(y,m,d) ((((y) > JULIAN_MINYEAR) \
00252   || (((y) == JULIAN_MINYEAR) && (((m) > JULIAN_MINMONTH) \
00253   || (((m) == JULIAN_MINMONTH) && ((d) >= JULIAN_MINDAY))))) \
00254  && ((y) < JULIAN_MAXYEAR))
00255 
00256 /* Julian-date equivalents of Day 0 in Unix and Postgres reckoning */
00257 #define UNIX_EPOCH_JDATE                2440588 /* == date2j(1970, 1, 1) */
00258 #define POSTGRES_EPOCH_JDATE    2451545 /* == date2j(2000, 1, 1) */
00259 
00260 
00261 /*
00262  * Datetime input parsing routines (ParseDateTime, DecodeDateTime, etc)
00263  * return zero or a positive value on success.  On failure, they return
00264  * one of these negative code values.  DateTimeParseError may be used to
00265  * produce a correct ereport.
00266  */
00267 #define DTERR_BAD_FORMAT                (-1)
00268 #define DTERR_FIELD_OVERFLOW    (-2)
00269 #define DTERR_MD_FIELD_OVERFLOW (-3)    /* triggers hint about DateStyle */
00270 #define DTERR_INTERVAL_OVERFLOW (-4)
00271 #define DTERR_TZDISP_OVERFLOW   (-5)
00272 
00273 
00274 extern void GetCurrentDateTime(struct pg_tm * tm);
00275 extern void GetCurrentTimeUsec(struct pg_tm * tm, fsec_t *fsec, int *tzp);
00276 extern void j2date(int jd, int *year, int *month, int *day);
00277 extern int      date2j(int year, int month, int day);
00278 
00279 extern int ParseDateTime(const char *timestr, char *workbuf, size_t buflen,
00280                           char **field, int *ftype,
00281                           int maxfields, int *numfields);
00282 extern int DecodeDateTime(char **field, int *ftype,
00283                            int nf, int *dtype,
00284                            struct pg_tm * tm, fsec_t *fsec, int *tzp);
00285 extern int DecodeTimeOnly(char **field, int *ftype,
00286                            int nf, int *dtype,
00287                            struct pg_tm * tm, fsec_t *fsec, int *tzp);
00288 extern int DecodeInterval(char **field, int *ftype,
00289                            int nf, int *dtype,
00290                            struct pg_tm * tm, fsec_t *fsec);
00291 extern void DateTimeParseError(int dterr, const char *str,
00292                                    const char *datatype);
00293 
00294 extern int      DetermineTimeZoneOffset(struct pg_tm * tm, pg_tz *tzp);
00295 
00296 extern int      EncodeDateOnly(struct pg_tm * tm, int style, char *str);
00297 extern int      EncodeTimeOnly(struct pg_tm * tm, fsec_t fsec, int *tzp, int style, char *str);
00298 extern int      EncodeDateTime(struct pg_tm * tm, fsec_t fsec, int *tzp, char **tzn, int style, char *str);
00299 extern int      EncodeInterval(struct pg_tm * tm, fsec_t fsec, int style, char *str);
00300 
00301 extern int      DecodeSpecial(int field, char *lowtoken, int *val);
00302 extern int      DecodeUnits(int field, char *lowtoken, int *val);
00303 
00304 extern int      j2day(int jd);
00305 
00306 extern bool CheckDateTokenTables(void);
00307 
00308 #endif   /* DATETIME_H */
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'