| 1 | /*
|
|---|
| 2 | PostgreSQL Data Base Management System (formerly known as Postgres, then
|
|---|
| 3 | as Postgres95).
|
|---|
| 4 |
|
|---|
| 5 | Copyright (c) 1994-7 Regents of the University of California
|
|---|
| 6 |
|
|---|
| 7 | Permission to use, copy, modify, and distribute this software and its
|
|---|
| 8 | documentation for any purpose, without fee, and without a written agreement
|
|---|
| 9 | is hereby granted, provided that the above copyright notice and this
|
|---|
| 10 | paragraph and the following two paragraphs appear in all copies.
|
|---|
| 11 |
|
|---|
| 12 | IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
|
|---|
| 13 | DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
|
|---|
| 14 | LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
|
|---|
| 15 | DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
|
|---|
| 16 | POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 17 |
|
|---|
| 18 | THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
|
|---|
| 19 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|---|
| 20 | AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|---|
| 21 | ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
|
|---|
| 22 | PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|---|
| 23 | */
|
|---|
| 24 | /*-------------------------------------------------------------------------
|
|---|
| 25 | *
|
|---|
| 26 | * dt.h--
|
|---|
| 27 | * Definitions for the date/time and other date/time support code.
|
|---|
| 28 | * The support code is shared with other date data types,
|
|---|
| 29 | * including abstime, reltime, date, and time.
|
|---|
| 30 | *
|
|---|
| 31 | *
|
|---|
| 32 | * Copyright (c) 1994, Regents of the University of California
|
|---|
| 33 | *
|
|---|
| 34 | * $Id: parsedt.h,v 1.3 2000-12-24 14:39:55 sandervl Exp $
|
|---|
| 35 | *
|
|---|
| 36 | *-------------------------------------------------------------------------
|
|---|
| 37 | */
|
|---|
| 38 | #ifndef DT_H
|
|---|
| 39 | #define DT_H
|
|---|
| 40 |
|
|---|
| 41 | #include <time.h>
|
|---|
| 42 | #include <math.h>
|
|---|
| 43 |
|
|---|
| 44 | /* We have to include stdlib.h here because it defines many of these macros
|
|---|
| 45 | on some platforms, and we only want our definitions used if stdlib.h doesn't
|
|---|
| 46 | have its own.
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | #include <stdlib.h>
|
|---|
| 50 |
|
|---|
| 51 | /* ----------------------------------------------------------------
|
|---|
| 52 | * Section 1: bool, true, false, TRUE, FALSE
|
|---|
| 53 | * ----------------------------------------------------------------
|
|---|
| 54 | */
|
|---|
| 55 | /*
|
|---|
| 56 | * bool --
|
|---|
| 57 | * Boolean value, either true or false.
|
|---|
| 58 | *
|
|---|
| 59 | */
|
|---|
| 60 | #define false ((char) 0)
|
|---|
| 61 | #define true ((char) 1)
|
|---|
| 62 | #ifndef __cplusplus
|
|---|
| 63 | #ifndef bool
|
|---|
| 64 | typedef char bool;
|
|---|
| 65 | #endif /* ndef bool */
|
|---|
| 66 | #endif /* not C++ */
|
|---|
| 67 | typedef bool *BoolPtr;
|
|---|
| 68 |
|
|---|
| 69 | #ifndef TRUE
|
|---|
| 70 | #define TRUE 1
|
|---|
| 71 | #endif /* TRUE */
|
|---|
| 72 |
|
|---|
| 73 | #ifndef FALSE
|
|---|
| 74 | #define FALSE 0
|
|---|
| 75 | #endif /* FALSE */
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | /* ----------------------------------------------------------------
|
|---|
| 80 | * Section 3: standard system types
|
|---|
| 81 | * ----------------------------------------------------------------
|
|---|
| 82 | */
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | * intN --
|
|---|
| 86 | * Signed integer, EXACTLY N BITS IN SIZE,
|
|---|
| 87 | * used for numerical computations and the
|
|---|
| 88 | * frontend/backend protocol.
|
|---|
| 89 | */
|
|---|
| 90 | typedef signed char int8; /* == 8 bits */
|
|---|
| 91 | typedef signed short int16; /* == 16 bits */
|
|---|
| 92 | typedef signed int int32; /* == 32 bits */
|
|---|
| 93 |
|
|---|
| 94 | /*
|
|---|
| 95 | * uintN --
|
|---|
| 96 | * Unsigned integer, EXACTLY N BITS IN SIZE,
|
|---|
| 97 | * used for numerical computations and the
|
|---|
| 98 | * frontend/backend protocol.
|
|---|
| 99 | */
|
|---|
| 100 | typedef unsigned char uint8; /* == 8 bits */
|
|---|
| 101 | typedef unsigned short uint16; /* == 16 bits */
|
|---|
| 102 | typedef unsigned int uint32; /* == 32 bits */
|
|---|
| 103 |
|
|---|
| 104 | /*
|
|---|
| 105 | * floatN --
|
|---|
| 106 | * Floating point number, AT LEAST N BITS IN SIZE,
|
|---|
| 107 | * used for numerical computations.
|
|---|
| 108 | *
|
|---|
| 109 | * Since sizeof(floatN) may be > sizeof(char *), always pass
|
|---|
| 110 | * floatN by reference.
|
|---|
| 111 | */
|
|---|
| 112 | typedef float float32data;
|
|---|
| 113 | typedef double float64data;
|
|---|
| 114 | typedef float *float32;
|
|---|
| 115 | typedef double *float64;
|
|---|
| 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * boolN --
|
|---|
| 119 | * Boolean value, AT LEAST N BITS IN SIZE.
|
|---|
| 120 | */
|
|---|
| 121 | typedef uint8 bool8; /* >= 8 bits */
|
|---|
| 122 | typedef uint16 bool16; /* >= 16 bits */
|
|---|
| 123 | typedef uint32 bool32; /* >= 32 bits */
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | /* Date/Time Configuration
|
|---|
| 127 | *
|
|---|
| 128 | * Constants to pass info from runtime environment:
|
|---|
| 129 | * USE_POSTGRES_DATES specifies traditional postgres format for output.
|
|---|
| 130 | * USE_ISO_DATES specifies ISO-compliant format for output.
|
|---|
| 131 | * USE_SQL_DATES specified Oracle/Ingres-compliant format for output.
|
|---|
| 132 | * USE_GERMAN_DATES specifies German-style dd.mm/yyyy date format.
|
|---|
| 133 | *
|
|---|
| 134 | * DateStyle specifies preference for date formatting for output.
|
|---|
| 135 | * EuroDates if client prefers dates interpreted and written w/European conventions.
|
|---|
| 136 | *
|
|---|
| 137 | * HasCTZSet if client timezone is specified by client.
|
|---|
| 138 | * CDayLight is the apparent daylight savings time status.
|
|---|
| 139 | * CTimeZone is the timezone offset in seconds.
|
|---|
| 140 | * CTZName is the timezone label.
|
|---|
| 141 | */
|
|---|
| 142 |
|
|---|
| 143 | #define USE_POSTGRES_DATES 0
|
|---|
| 144 | #define USE_ISO_DATES 1
|
|---|
| 145 | #define USE_SQL_DATES 2
|
|---|
| 146 | #define USE_GERMAN_DATES 3
|
|---|
| 147 |
|
|---|
| 148 | extern int DateStyle;
|
|---|
| 149 | extern bool EuroDates;
|
|---|
| 150 | extern int CTimeZone;
|
|---|
| 151 |
|
|---|
| 152 | typedef double float8;
|
|---|
| 153 |
|
|---|
| 154 | struct varlena
|
|---|
| 155 | {
|
|---|
| 156 | int vl_len;
|
|---|
| 157 | char vl_dat[1];
|
|---|
| 158 | };
|
|---|
| 159 |
|
|---|
| 160 | typedef struct varlena text;
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | typedef int AbsoluteTime;
|
|---|
| 165 | typedef int RelativeTime;
|
|---|
| 166 |
|
|---|
| 167 | /*
|
|---|
| 168 | * Note a leap year is one that is a multiple of 4
|
|---|
| 169 | * but not of a 100. Except if it is a multiple of
|
|---|
| 170 | * 400 then it is a leap year.
|
|---|
| 171 | */
|
|---|
| 172 | #define isleap(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
|
|---|
| 173 |
|
|---|
| 174 | /*
|
|---|
| 175 | * DateTime represents absolute time.
|
|---|
| 176 | * TimeSpan represents delta time. Keep track of months (and years)
|
|---|
| 177 | * separately since the elapsed time spanned is unknown until instantiated
|
|---|
| 178 | * relative to an absolute time.
|
|---|
| 179 | *
|
|---|
| 180 | * Note that Postgres uses "time interval" to mean a bounded interval,
|
|---|
| 181 | * consisting of a beginning and ending time, not a time span - thomas 97/03/20
|
|---|
| 182 | */
|
|---|
| 183 |
|
|---|
| 184 | typedef double DateTime;
|
|---|
| 185 |
|
|---|
| 186 | typedef struct
|
|---|
| 187 | {
|
|---|
| 188 | double time; /* all time units other than months and
|
|---|
| 189 | * years */
|
|---|
| 190 | int month; /* months and years, after time for
|
|---|
| 191 | * alignment */
|
|---|
| 192 | } TimeSpan;
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 | /* ----------------------------------------------------------------
|
|---|
| 196 | * time types + support macros
|
|---|
| 197 | *
|
|---|
| 198 | * String definitions for standard time quantities.
|
|---|
| 199 | *
|
|---|
| 200 | * These strings are the defaults used to form output time strings.
|
|---|
| 201 | * Other alternate forms are hardcoded into token tables in dt.c.
|
|---|
| 202 | * ----------------------------------------------------------------
|
|---|
| 203 | */
|
|---|
| 204 |
|
|---|
| 205 | #define DAGO "ago"
|
|---|
| 206 | #define DCURRENT "current"
|
|---|
| 207 | #define EPOCH "epoch"
|
|---|
| 208 | #define INVALID "invalid"
|
|---|
| 209 | #define EARLY "-infinity"
|
|---|
| 210 | #define LATE "infinity"
|
|---|
| 211 | #define NOW "now"
|
|---|
| 212 | #define TODAY "today"
|
|---|
| 213 | #define TOMORROW "tomorrow"
|
|---|
| 214 | #define YESTERDAY "yesterday"
|
|---|
| 215 | #define ZULU "zulu"
|
|---|
| 216 |
|
|---|
| 217 | #define DMICROSEC "usecond"
|
|---|
| 218 | #define DMILLISEC "msecond"
|
|---|
| 219 | #define DSECOND "second"
|
|---|
| 220 | #define DMINUTE "minute"
|
|---|
| 221 | #define DHOUR "hour"
|
|---|
| 222 | #define DDAY "day"
|
|---|
| 223 | #define DWEEK "week"
|
|---|
| 224 | #define DMONTH "month"
|
|---|
| 225 | #define DQUARTER "quarter"
|
|---|
| 226 | #define DYEAR "year"
|
|---|
| 227 | #define DDECADE "decade"
|
|---|
| 228 | #define DCENTURY "century"
|
|---|
| 229 | #define DMILLENIUM "millenium"
|
|---|
| 230 | #define DA_D "ad"
|
|---|
| 231 | #define DB_C "bc"
|
|---|
| 232 | #define DTIMEZONE "timezone"
|
|---|
| 233 |
|
|---|
| 234 | /*
|
|---|
| 235 | * Fundamental time field definitions for parsing.
|
|---|
| 236 | *
|
|---|
| 237 | * Meridian: am, pm, or 24-hour style.
|
|---|
| 238 | * Millenium: ad, bc
|
|---|
| 239 | */
|
|---|
| 240 |
|
|---|
| 241 | #define AM 0
|
|---|
| 242 | #define PM 1
|
|---|
| 243 | #define HR24 2
|
|---|
| 244 |
|
|---|
| 245 | #define AD 0
|
|---|
| 246 | #define BC 1
|
|---|
| 247 |
|
|---|
| 248 | /*
|
|---|
| 249 | * Fields for time decoding.
|
|---|
| 250 | * Can't have more of these than there are bits in an unsigned int
|
|---|
| 251 | * since these are turned into bit masks during parsing and decoding.
|
|---|
| 252 | */
|
|---|
| 253 |
|
|---|
| 254 | #define RESERV 0
|
|---|
| 255 | #define MONTH 1
|
|---|
| 256 | #define YEAR 2
|
|---|
| 257 | #define DAY 3
|
|---|
| 258 | #define TIMES 4 /* not used - thomas 1997-07-14 */
|
|---|
| 259 | #define TZ 5
|
|---|
| 260 | #define DTZ 6
|
|---|
| 261 | #define DTZMOD 7
|
|---|
| 262 | #define IGNOREFIELD 8
|
|---|
| 263 | #define AMPM 9
|
|---|
| 264 | #define HOUR 10
|
|---|
| 265 | #define MINUTE 11
|
|---|
| 266 | #define SECOND 12
|
|---|
| 267 | #define DOY 13
|
|---|
| 268 | #define DOW 14
|
|---|
| 269 | #define UNITS 15
|
|---|
| 270 | #define ADBC 16
|
|---|
| 271 | /* these are only for relative dates */
|
|---|
| 272 | #define AGO 17
|
|---|
| 273 | #define ABS_BEFORE 18
|
|---|
| 274 | #define ABS_AFTER 19
|
|---|
| 275 |
|
|---|
| 276 | /*
|
|---|
| 277 | * Token field definitions for time parsing and decoding.
|
|---|
| 278 | * These need to fit into the datetkn table type.
|
|---|
| 279 | * At the moment, that means keep them within [-127,127].
|
|---|
| 280 | * These are also used for bit masks in DecodeDateDelta()
|
|---|
| 281 | * so actually restrict them to within [0,31] for now.
|
|---|
| 282 | * - thomas 97/06/19
|
|---|
| 283 | * Not all of these fields are used for masks in DecodeDateDelta
|
|---|
| 284 | * so allow some larger than 31. - thomas 1997-11-17
|
|---|
| 285 | */
|
|---|
| 286 |
|
|---|
| 287 | #define DTK_NUMBER 0
|
|---|
| 288 | #define DTK_STRING 1
|
|---|
| 289 |
|
|---|
| 290 | #define DTK_DATE 2
|
|---|
| 291 | #define DTK_TIME 3
|
|---|
| 292 | #define DTK_TZ 4
|
|---|
| 293 | #define DTK_AGO 5
|
|---|
| 294 |
|
|---|
| 295 | #define DTK_SPECIAL 6
|
|---|
| 296 | #define DTK_INVALID 7
|
|---|
| 297 | #define DTK_CURRENT 8
|
|---|
| 298 | #define DTK_EARLY 9
|
|---|
| 299 | #define DTK_LATE 10
|
|---|
| 300 | #define DTK_EPOCH 11
|
|---|
| 301 | #define DTK_NOW 12
|
|---|
| 302 | #define DTK_YESTERDAY 13
|
|---|
| 303 | #define DTK_TODAY 14
|
|---|
| 304 | #define DTK_TOMORROW 15
|
|---|
| 305 | #define DTK_ZULU 16
|
|---|
| 306 |
|
|---|
| 307 | #define DTK_DELTA 17
|
|---|
| 308 | #define DTK_SECOND 18
|
|---|
| 309 | #define DTK_MINUTE 19
|
|---|
| 310 | #define DTK_HOUR 20
|
|---|
| 311 | #define DTK_DAY 21
|
|---|
| 312 | #define DTK_WEEK 22
|
|---|
| 313 | #define DTK_MONTH 23
|
|---|
| 314 | #define DTK_QUARTER 24
|
|---|
| 315 | #define DTK_YEAR 25
|
|---|
| 316 | #define DTK_DECADE 26
|
|---|
| 317 | #define DTK_CENTURY 27
|
|---|
| 318 | #define DTK_MILLENIUM 28
|
|---|
| 319 | #define DTK_MILLISEC 29
|
|---|
| 320 | #define DTK_MICROSEC 30
|
|---|
| 321 |
|
|---|
| 322 | #define DTK_DOW 32
|
|---|
| 323 | #define DTK_DOY 33
|
|---|
| 324 | #define DTK_TZ_HOUR 34
|
|---|
| 325 | #define DTK_TZ_MINUTE 35
|
|---|
| 326 |
|
|---|
| 327 | /*
|
|---|
| 328 | * Bit mask definitions for time parsing.
|
|---|
| 329 | */
|
|---|
| 330 |
|
|---|
| 331 | #define DTK_M(t) (0x01 << (t))
|
|---|
| 332 |
|
|---|
| 333 | #define DTK_DATE_M (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
|
|---|
| 334 | #define DTK_TIME_M (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_M(SECOND))
|
|---|
| 335 |
|
|---|
| 336 | #define MAXDATELEN 47 /* maximum possible length of an input
|
|---|
| 337 | * date string */
|
|---|
| 338 | #define MAXDATEFIELDS 25 /* maximum possible number of fields in a
|
|---|
| 339 | * date string */
|
|---|
| 340 | #define TOKMAXLEN 10 /* only this many chars are stored in
|
|---|
| 341 | * datetktbl */
|
|---|
| 342 |
|
|---|
| 343 | /* keep this struct small; it gets used a lot */
|
|---|
| 344 | typedef struct
|
|---|
| 345 | {
|
|---|
| 346 | #if defined(_AIX)
|
|---|
| 347 | char *token;
|
|---|
| 348 | #else
|
|---|
| 349 | char token[TOKMAXLEN];
|
|---|
| 350 | #endif /* _AIX */
|
|---|
| 351 | char type;
|
|---|
| 352 | char value; /* this may be unsigned, alas */
|
|---|
| 353 | } datetkn;
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 | /*
|
|---|
| 358 | * dt.c prototypes
|
|---|
| 359 | */
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 | void j2date(int jd, int *year, int *month, int *day);
|
|---|
| 363 | int date2j(int year, int month, int day);
|
|---|
| 364 |
|
|---|
| 365 | int ParseDateTime(char *timestr, char *lowstr,
|
|---|
| 366 | char **field, int *ftype, int maxfields, int *numfields);
|
|---|
| 367 | int DecodeDateTime(char **field, int *ftype,
|
|---|
| 368 | int nf, int *dtype, struct tm * tm, double *fsec, int *tzp);
|
|---|
| 369 |
|
|---|
| 370 | int DecodeTimeOnly(char **field, int *ftype, int nf,
|
|---|
| 371 | int *dtype, struct tm * tm, double *fsec);
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 | #endif /* DT_H */
|
|---|