2
0

datetime.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*-------------------------------------------------------------------------
  2. *
  3. * datetime.h
  4. * Definitions for date/time support code.
  5. * The support code is shared with other date data types,
  6. * including date, and time.
  7. *
  8. *
  9. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  10. * Portions Copyright (c) 1994, Regents of the University of California
  11. *
  12. * src/include/utils/datetime.h
  13. *
  14. *-------------------------------------------------------------------------
  15. */
  16. #ifndef DATETIME_H
  17. #define DATETIME_H
  18. #include "nodes/nodes.h"
  19. #include "utils/timestamp.h"
  20. /* this struct is declared in utils/tzparser.h: */
  21. struct tzEntry;
  22. /* ----------------------------------------------------------------
  23. * time types + support macros
  24. *
  25. * String definitions for standard time quantities.
  26. *
  27. * These strings are the defaults used to form output time strings.
  28. * Other alternative forms are hardcoded into token tables in datetime.c.
  29. * ----------------------------------------------------------------
  30. */
  31. #define DAGO "ago"
  32. #define DCURRENT "current"
  33. #define EPOCH "epoch"
  34. #define INVALID "invalid"
  35. #define EARLY "-infinity"
  36. #define LATE "infinity"
  37. #define NOW "now"
  38. #define TODAY "today"
  39. #define TOMORROW "tomorrow"
  40. #define YESTERDAY "yesterday"
  41. #define ZULU "zulu"
  42. #define DMICROSEC "usecond"
  43. #define DMILLISEC "msecond"
  44. #define DSECOND "second"
  45. #define DMINUTE "minute"
  46. #define DHOUR "hour"
  47. #define DDAY "day"
  48. #define DWEEK "week"
  49. #define DMONTH "month"
  50. #define DQUARTER "quarter"
  51. #define DYEAR "year"
  52. #define DDECADE "decade"
  53. #define DCENTURY "century"
  54. #define DMILLENNIUM "millennium"
  55. #define DA_D "ad"
  56. #define DB_C "bc"
  57. #define DTIMEZONE "timezone"
  58. /*
  59. * Fundamental time field definitions for parsing.
  60. *
  61. * Meridian: am, pm, or 24-hour style.
  62. * Millennium: ad, bc
  63. */
  64. #define AM 0
  65. #define PM 1
  66. #define HR24 2
  67. #define AD 0
  68. #define BC 1
  69. /*
  70. * Field types for time decoding.
  71. *
  72. * Can't have more of these than there are bits in an unsigned int
  73. * since these are turned into bit masks during parsing and decoding.
  74. *
  75. * Furthermore, the values for YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
  76. * must be in the range 0..14 so that the associated bitmasks can fit
  77. * into the left half of an INTERVAL's typmod value. Since those bits
  78. * are stored in typmods, you can't change them without initdb!
  79. */
  80. #define RESERV 0
  81. #define MONTH 1
  82. #define YEAR 2
  83. #define DAY 3
  84. #define JULIAN 4
  85. #define TZ 5 /* fixed-offset timezone abbreviation */
  86. #define DTZ 6 /* fixed-offset timezone abbrev, DST */
  87. #define DYNTZ 7 /* dynamic timezone abbreviation */
  88. #define IGNORE_DTF 8
  89. #define AMPM 9
  90. #define HOUR 10
  91. #define MINUTE 11
  92. #define SECOND 12
  93. #define MILLISECOND 13
  94. #define MICROSECOND 14
  95. #define DOY 15
  96. #define DOW 16
  97. #define UNITS 17
  98. #define ADBC 18
  99. /* these are only for relative dates */
  100. #define AGO 19
  101. #define ABS_BEFORE 20
  102. #define ABS_AFTER 21
  103. /* generic fields to help with parsing */
  104. #define ISODATE 22
  105. #define ISOTIME 23
  106. /* these are only for parsing intervals */
  107. #define WEEK 24
  108. #define DECADE 25
  109. #define CENTURY 26
  110. #define MILLENNIUM 27
  111. /* hack for parsing two-word timezone specs "MET DST" etc */
  112. #define DTZMOD 28 /* "DST" as a separate word */
  113. /* reserved for unrecognized string values */
  114. #define UNKNOWN_FIELD 31
  115. /*
  116. * Token field definitions for time parsing and decoding.
  117. *
  118. * Some field type codes (see above) use these as the "value" in datetktbl[].
  119. * These are also used for bit masks in DecodeDateTime and friends
  120. * so actually restrict them to within [0,31] for now.
  121. * - thomas 97/06/19
  122. * Not all of these fields are used for masks in DecodeDateTime
  123. * so allow some larger than 31. - thomas 1997-11-17
  124. *
  125. * Caution: there are undocumented assumptions in the code that most of these
  126. * values are not equal to IGNORE_DTF nor RESERV. Be very careful when
  127. * renumbering values in either of these apparently-independent lists :-(
  128. */
  129. #define DTK_NUMBER 0
  130. #define DTK_STRING 1
  131. #define DTK_DATE 2
  132. #define DTK_TIME 3
  133. #define DTK_TZ 4
  134. #define DTK_AGO 5
  135. #define DTK_SPECIAL 6
  136. #define DTK_EARLY 9
  137. #define DTK_LATE 10
  138. #define DTK_EPOCH 11
  139. #define DTK_NOW 12
  140. #define DTK_YESTERDAY 13
  141. #define DTK_TODAY 14
  142. #define DTK_TOMORROW 15
  143. #define DTK_ZULU 16
  144. #define DTK_DELTA 17
  145. #define DTK_SECOND 18
  146. #define DTK_MINUTE 19
  147. #define DTK_HOUR 20
  148. #define DTK_DAY 21
  149. #define DTK_WEEK 22
  150. #define DTK_MONTH 23
  151. #define DTK_QUARTER 24
  152. #define DTK_YEAR 25
  153. #define DTK_DECADE 26
  154. #define DTK_CENTURY 27
  155. #define DTK_MILLENNIUM 28
  156. #define DTK_MILLISEC 29
  157. #define DTK_MICROSEC 30
  158. #define DTK_JULIAN 31
  159. #define DTK_DOW 32
  160. #define DTK_DOY 33
  161. #define DTK_TZ_HOUR 34
  162. #define DTK_TZ_MINUTE 35
  163. #define DTK_ISOYEAR 36
  164. #define DTK_ISODOW 37
  165. /*
  166. * Bit mask definitions for time parsing.
  167. */
  168. #define DTK_M(t) (0x01 << (t))
  169. /* Convenience: a second, plus any fractional component */
  170. #define DTK_ALL_SECS_M (DTK_M(SECOND) | DTK_M(MILLISECOND) | DTK_M(MICROSECOND))
  171. #define DTK_DATE_M (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
  172. #define DTK_TIME_M (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_ALL_SECS_M)
  173. /*
  174. * Working buffer size for input and output of interval, timestamp, etc.
  175. * Inputs that need more working space will be rejected early. Longer outputs
  176. * will overrun buffers, so this must suffice for all possible output. As of
  177. * this writing, interval_out() needs the most space at ~90 bytes.
  178. */
  179. #define MAXDATELEN 128
  180. /* maximum possible number of fields in a date string */
  181. #define MAXDATEFIELDS 25
  182. /* only this many chars are stored in datetktbl */
  183. #define TOKMAXLEN 10
  184. /* keep this struct small; it gets used a lot */
  185. typedef struct
  186. {
  187. char token[TOKMAXLEN + 1]; /* always NUL-terminated */
  188. char type; /* see field type codes above */
  189. int32 value; /* meaning depends on type */
  190. } datetkn;
  191. /* one of its uses is in tables of time zone abbreviations */
  192. typedef struct TimeZoneAbbrevTable
  193. {
  194. Size tblsize; /* size in bytes of TimeZoneAbbrevTable */
  195. int numabbrevs; /* number of entries in abbrevs[] array */
  196. datetkn abbrevs[FLEXIBLE_ARRAY_MEMBER];
  197. /* DynamicZoneAbbrev(s) may follow the abbrevs[] array */
  198. } TimeZoneAbbrevTable;
  199. /* auxiliary data for a dynamic time zone abbreviation (non-fixed-offset) */
  200. typedef struct DynamicZoneAbbrev
  201. {
  202. pg_tz *tz; /* NULL if not yet looked up */
  203. char zone[FLEXIBLE_ARRAY_MEMBER]; /* NUL-terminated zone name */
  204. } DynamicZoneAbbrev;
  205. /* FMODULO()
  206. * Macro to replace modf(), which is broken on some platforms.
  207. * t = input and remainder
  208. * q = integer part
  209. * u = divisor
  210. */
  211. #define FMODULO(t,q,u) \
  212. do { \
  213. (q) = (((t) < 0) ? ceil((t) / (u)) : floor((t) / (u))); \
  214. if ((q) != 0) (t) -= rint((q) * (u)); \
  215. } while(0)
  216. /* TMODULO()
  217. * Like FMODULO(), but work on the timestamp datatype (now always int64).
  218. * We assume that int64 follows the C99 semantics for division (negative
  219. * quotients truncate towards zero).
  220. */
  221. #define TMODULO(t,q,u) \
  222. do { \
  223. (q) = ((t) / (u)); \
  224. if ((q) != 0) (t) -= ((q) * (u)); \
  225. } while(0)
  226. /*
  227. * Date/time validation
  228. * Include check for leap year.
  229. */
  230. extern PGDLLIMPORT const char *const months[]; /* months (3-char
  231. * abbreviations) */
  232. extern PGDLLIMPORT const char *const days[]; /* days (full names) */
  233. extern PGDLLIMPORT const int day_tab[2][13];
  234. /*
  235. * These are the rules for the Gregorian calendar, which was adopted in 1582.
  236. * However, we use this calculation for all prior years as well because the
  237. * SQL standard specifies use of the Gregorian calendar. This prevents the
  238. * date 1500-02-29 from being stored, even though it is valid in the Julian
  239. * calendar.
  240. */
  241. #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
  242. /*
  243. * Datetime input parsing routines (ParseDateTime, DecodeDateTime, etc)
  244. * return zero or a positive value on success. On failure, they return
  245. * one of these negative code values. DateTimeParseError may be used to
  246. * produce a correct ereport.
  247. */
  248. #define DTERR_BAD_FORMAT (-1)
  249. #define DTERR_FIELD_OVERFLOW (-2)
  250. #define DTERR_MD_FIELD_OVERFLOW (-3) /* triggers hint about DateStyle */
  251. #define DTERR_INTERVAL_OVERFLOW (-4)
  252. #define DTERR_TZDISP_OVERFLOW (-5)
  253. extern void GetCurrentDateTime(struct pg_tm *tm);
  254. extern void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp);
  255. extern void j2date(int jd, int *year, int *month, int *day);
  256. extern int date2j(int year, int month, int day);
  257. extern int ParseDateTime(const char *timestr, char *workbuf, size_t buflen,
  258. char **field, int *ftype,
  259. int maxfields, int *numfields);
  260. extern int DecodeDateTime(char **field, int *ftype,
  261. int nf, int *dtype,
  262. struct pg_tm *tm, fsec_t *fsec, int *tzp);
  263. extern int DecodeTimezone(char *str, int *tzp);
  264. extern int DecodeTimeOnly(char **field, int *ftype,
  265. int nf, int *dtype,
  266. struct pg_tm *tm, fsec_t *fsec, int *tzp);
  267. extern int DecodeInterval(char **field, int *ftype, int nf, int range,
  268. int *dtype, struct pg_itm_in *itm_in);
  269. extern int DecodeISO8601Interval(char *str,
  270. int *dtype, struct pg_itm_in *itm_in);
  271. extern void DateTimeParseError(int dterr, const char *str,
  272. const char *datatype) pg_attribute_noreturn();
  273. extern int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp);
  274. extern int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp);
  275. extern int DetermineTimeZoneAbbrevOffsetTS(TimestampTz ts, const char *abbr,
  276. pg_tz *tzp, int *isdst);
  277. extern void EncodeDateOnly(struct pg_tm *tm, int style, char *str);
  278. extern void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str);
  279. extern void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str);
  280. extern void EncodeInterval(struct pg_itm *itm, int style, char *str);
  281. extern void EncodeSpecialTimestamp(Timestamp dt, char *str);
  282. extern int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc,
  283. struct pg_tm *tm);
  284. extern int DecodeTimezoneAbbrev(int field, char *lowtoken,
  285. int *offset, pg_tz **tz);
  286. extern int DecodeSpecial(int field, char *lowtoken, int *val);
  287. extern int DecodeUnits(int field, char *lowtoken, int *val);
  288. extern int j2day(int jd);
  289. extern Node *TemporalSimplify(int32 max_precis, Node *node);
  290. extern bool CheckDateTokenTables(void);
  291. extern TimeZoneAbbrevTable *ConvertTimeZoneAbbrevs(struct tzEntry *abbrevs,
  292. int n);
  293. extern void InstallTimeZoneAbbrevs(TimeZoneAbbrevTable *tbl);
  294. extern void AdjustTimestampForTypmod(Timestamp *time, int32 typmod);
  295. extern bool AdjustTimestampForTypmodError(Timestamp *time, int32 typmod,
  296. bool *error);
  297. #endif /* DATETIME_H */