date.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*-------------------------------------------------------------------------
  2. *
  3. * date.h
  4. * Definitions for the SQL "date" and "time" types.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/utils/date.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef DATE_H
  15. #define DATE_H
  16. #include <math.h>
  17. #include "fmgr.h"
  18. typedef int32 DateADT;
  19. #ifdef HAVE_INT64_TIMESTAMP
  20. typedef int64 TimeADT;
  21. #else
  22. typedef float8 TimeADT;
  23. #endif
  24. typedef struct
  25. {
  26. TimeADT time; /* all time units other than months and years */
  27. int32 zone; /* numeric time zone, in seconds */
  28. } TimeTzADT;
  29. /*
  30. * Infinity and minus infinity must be the max and min values of DateADT.
  31. */
  32. #define DATEVAL_NOBEGIN ((DateADT) PG_INT32_MIN)
  33. #define DATEVAL_NOEND ((DateADT) PG_INT32_MAX)
  34. #define DATE_NOBEGIN(j) ((j) = DATEVAL_NOBEGIN)
  35. #define DATE_IS_NOBEGIN(j) ((j) == DATEVAL_NOBEGIN)
  36. #define DATE_NOEND(j) ((j) = DATEVAL_NOEND)
  37. #define DATE_IS_NOEND(j) ((j) == DATEVAL_NOEND)
  38. #define DATE_NOT_FINITE(j) (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))
  39. /*
  40. * Macros for fmgr-callable functions.
  41. *
  42. * For TimeADT, we make use of the same support routines as for float8 or int64.
  43. * Therefore TimeADT is pass-by-reference if and only if float8 or int64 is!
  44. */
  45. #ifdef HAVE_INT64_TIMESTAMP
  46. #define MAX_TIME_PRECISION 6
  47. #define DatumGetDateADT(X) ((DateADT) DatumGetInt32(X))
  48. #define DatumGetTimeADT(X) ((TimeADT) DatumGetInt64(X))
  49. #define DatumGetTimeTzADTP(X) ((TimeTzADT *) DatumGetPointer(X))
  50. #define DateADTGetDatum(X) Int32GetDatum(X)
  51. #define TimeADTGetDatum(X) Int64GetDatum(X)
  52. #define TimeTzADTPGetDatum(X) PointerGetDatum(X)
  53. #else /* !HAVE_INT64_TIMESTAMP */
  54. #define MAX_TIME_PRECISION 10
  55. /* round off to MAX_TIME_PRECISION decimal places */
  56. #define TIME_PREC_INV 10000000000.0
  57. #define TIMEROUND(j) (rint(((double) (j)) * TIME_PREC_INV) / TIME_PREC_INV)
  58. #define DatumGetDateADT(X) ((DateADT) DatumGetInt32(X))
  59. #define DatumGetTimeADT(X) ((TimeADT) DatumGetFloat8(X))
  60. #define DatumGetTimeTzADTP(X) ((TimeTzADT *) DatumGetPointer(X))
  61. #define DateADTGetDatum(X) Int32GetDatum(X)
  62. #define TimeADTGetDatum(X) Float8GetDatum(X)
  63. #define TimeTzADTPGetDatum(X) PointerGetDatum(X)
  64. #endif /* HAVE_INT64_TIMESTAMP */
  65. #define PG_GETARG_DATEADT(n) DatumGetDateADT(PG_GETARG_DATUM(n))
  66. #define PG_GETARG_TIMEADT(n) DatumGetTimeADT(PG_GETARG_DATUM(n))
  67. #define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n))
  68. #define PG_RETURN_DATEADT(x) return DateADTGetDatum(x)
  69. #define PG_RETURN_TIMEADT(x) return TimeADTGetDatum(x)
  70. #define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x)
  71. /* date.c */
  72. extern double date2timestamp_no_overflow(DateADT dateVal);
  73. extern void EncodeSpecialDate(DateADT dt, char *str);
  74. extern Datum date_in(PG_FUNCTION_ARGS);
  75. extern Datum date_out(PG_FUNCTION_ARGS);
  76. extern Datum date_recv(PG_FUNCTION_ARGS);
  77. extern Datum date_send(PG_FUNCTION_ARGS);
  78. extern Datum make_date(PG_FUNCTION_ARGS);
  79. extern Datum date_eq(PG_FUNCTION_ARGS);
  80. extern Datum date_ne(PG_FUNCTION_ARGS);
  81. extern Datum date_lt(PG_FUNCTION_ARGS);
  82. extern Datum date_le(PG_FUNCTION_ARGS);
  83. extern Datum date_gt(PG_FUNCTION_ARGS);
  84. extern Datum date_ge(PG_FUNCTION_ARGS);
  85. extern Datum date_cmp(PG_FUNCTION_ARGS);
  86. extern Datum date_sortsupport(PG_FUNCTION_ARGS);
  87. extern Datum date_finite(PG_FUNCTION_ARGS);
  88. extern Datum date_larger(PG_FUNCTION_ARGS);
  89. extern Datum date_smaller(PG_FUNCTION_ARGS);
  90. extern Datum date_mi(PG_FUNCTION_ARGS);
  91. extern Datum date_pli(PG_FUNCTION_ARGS);
  92. extern Datum date_mii(PG_FUNCTION_ARGS);
  93. extern Datum date_eq_timestamp(PG_FUNCTION_ARGS);
  94. extern Datum date_ne_timestamp(PG_FUNCTION_ARGS);
  95. extern Datum date_lt_timestamp(PG_FUNCTION_ARGS);
  96. extern Datum date_le_timestamp(PG_FUNCTION_ARGS);
  97. extern Datum date_gt_timestamp(PG_FUNCTION_ARGS);
  98. extern Datum date_ge_timestamp(PG_FUNCTION_ARGS);
  99. extern Datum date_cmp_timestamp(PG_FUNCTION_ARGS);
  100. extern Datum date_eq_timestamptz(PG_FUNCTION_ARGS);
  101. extern Datum date_ne_timestamptz(PG_FUNCTION_ARGS);
  102. extern Datum date_lt_timestamptz(PG_FUNCTION_ARGS);
  103. extern Datum date_le_timestamptz(PG_FUNCTION_ARGS);
  104. extern Datum date_gt_timestamptz(PG_FUNCTION_ARGS);
  105. extern Datum date_ge_timestamptz(PG_FUNCTION_ARGS);
  106. extern Datum date_cmp_timestamptz(PG_FUNCTION_ARGS);
  107. extern Datum timestamp_eq_date(PG_FUNCTION_ARGS);
  108. extern Datum timestamp_ne_date(PG_FUNCTION_ARGS);
  109. extern Datum timestamp_lt_date(PG_FUNCTION_ARGS);
  110. extern Datum timestamp_le_date(PG_FUNCTION_ARGS);
  111. extern Datum timestamp_gt_date(PG_FUNCTION_ARGS);
  112. extern Datum timestamp_ge_date(PG_FUNCTION_ARGS);
  113. extern Datum timestamp_cmp_date(PG_FUNCTION_ARGS);
  114. extern Datum timestamptz_eq_date(PG_FUNCTION_ARGS);
  115. extern Datum timestamptz_ne_date(PG_FUNCTION_ARGS);
  116. extern Datum timestamptz_lt_date(PG_FUNCTION_ARGS);
  117. extern Datum timestamptz_le_date(PG_FUNCTION_ARGS);
  118. extern Datum timestamptz_gt_date(PG_FUNCTION_ARGS);
  119. extern Datum timestamptz_ge_date(PG_FUNCTION_ARGS);
  120. extern Datum timestamptz_cmp_date(PG_FUNCTION_ARGS);
  121. extern Datum date_pl_interval(PG_FUNCTION_ARGS);
  122. extern Datum date_mi_interval(PG_FUNCTION_ARGS);
  123. extern Datum date_timestamp(PG_FUNCTION_ARGS);
  124. extern Datum timestamp_date(PG_FUNCTION_ARGS);
  125. extern Datum date_timestamptz(PG_FUNCTION_ARGS);
  126. extern Datum timestamptz_date(PG_FUNCTION_ARGS);
  127. extern Datum datetime_timestamp(PG_FUNCTION_ARGS);
  128. extern Datum abstime_date(PG_FUNCTION_ARGS);
  129. extern Datum time_in(PG_FUNCTION_ARGS);
  130. extern Datum time_out(PG_FUNCTION_ARGS);
  131. extern Datum time_recv(PG_FUNCTION_ARGS);
  132. extern Datum time_send(PG_FUNCTION_ARGS);
  133. extern Datum timetypmodin(PG_FUNCTION_ARGS);
  134. extern Datum timetypmodout(PG_FUNCTION_ARGS);
  135. extern Datum make_time(PG_FUNCTION_ARGS);
  136. extern Datum time_transform(PG_FUNCTION_ARGS);
  137. extern Datum time_scale(PG_FUNCTION_ARGS);
  138. extern Datum time_eq(PG_FUNCTION_ARGS);
  139. extern Datum time_ne(PG_FUNCTION_ARGS);
  140. extern Datum time_lt(PG_FUNCTION_ARGS);
  141. extern Datum time_le(PG_FUNCTION_ARGS);
  142. extern Datum time_gt(PG_FUNCTION_ARGS);
  143. extern Datum time_ge(PG_FUNCTION_ARGS);
  144. extern Datum time_cmp(PG_FUNCTION_ARGS);
  145. extern Datum time_hash(PG_FUNCTION_ARGS);
  146. extern Datum overlaps_time(PG_FUNCTION_ARGS);
  147. extern Datum time_larger(PG_FUNCTION_ARGS);
  148. extern Datum time_smaller(PG_FUNCTION_ARGS);
  149. extern Datum time_mi_time(PG_FUNCTION_ARGS);
  150. extern Datum timestamp_time(PG_FUNCTION_ARGS);
  151. extern Datum timestamptz_time(PG_FUNCTION_ARGS);
  152. extern Datum time_interval(PG_FUNCTION_ARGS);
  153. extern Datum interval_time(PG_FUNCTION_ARGS);
  154. extern Datum time_pl_interval(PG_FUNCTION_ARGS);
  155. extern Datum time_mi_interval(PG_FUNCTION_ARGS);
  156. extern Datum time_part(PG_FUNCTION_ARGS);
  157. extern Datum timetz_in(PG_FUNCTION_ARGS);
  158. extern Datum timetz_out(PG_FUNCTION_ARGS);
  159. extern Datum timetz_recv(PG_FUNCTION_ARGS);
  160. extern Datum timetz_send(PG_FUNCTION_ARGS);
  161. extern Datum timetztypmodin(PG_FUNCTION_ARGS);
  162. extern Datum timetztypmodout(PG_FUNCTION_ARGS);
  163. extern Datum timetz_scale(PG_FUNCTION_ARGS);
  164. extern Datum timetz_eq(PG_FUNCTION_ARGS);
  165. extern Datum timetz_ne(PG_FUNCTION_ARGS);
  166. extern Datum timetz_lt(PG_FUNCTION_ARGS);
  167. extern Datum timetz_le(PG_FUNCTION_ARGS);
  168. extern Datum timetz_gt(PG_FUNCTION_ARGS);
  169. extern Datum timetz_ge(PG_FUNCTION_ARGS);
  170. extern Datum timetz_cmp(PG_FUNCTION_ARGS);
  171. extern Datum timetz_hash(PG_FUNCTION_ARGS);
  172. extern Datum overlaps_timetz(PG_FUNCTION_ARGS);
  173. extern Datum timetz_larger(PG_FUNCTION_ARGS);
  174. extern Datum timetz_smaller(PG_FUNCTION_ARGS);
  175. extern Datum timetz_time(PG_FUNCTION_ARGS);
  176. extern Datum time_timetz(PG_FUNCTION_ARGS);
  177. extern Datum timestamptz_timetz(PG_FUNCTION_ARGS);
  178. extern Datum datetimetz_timestamptz(PG_FUNCTION_ARGS);
  179. extern Datum timetz_part(PG_FUNCTION_ARGS);
  180. extern Datum timetz_zone(PG_FUNCTION_ARGS);
  181. extern Datum timetz_izone(PG_FUNCTION_ARGS);
  182. extern Datum timetz_pl_interval(PG_FUNCTION_ARGS);
  183. extern Datum timetz_mi_interval(PG_FUNCTION_ARGS);
  184. #endif /* DATE_H */