2
0

date.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*-------------------------------------------------------------------------
  2. *
  3. * date.h
  4. * Definitions for the SQL "date" and "time" types.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, 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 "datatype/timestamp.h"
  18. #include "fmgr.h"
  19. #include "pgtime.h"
  20. typedef int32 DateADT;
  21. typedef int64 TimeADT;
  22. typedef struct
  23. {
  24. TimeADT time; /* all time units other than months and years */
  25. int32 zone; /* numeric time zone, in seconds */
  26. } TimeTzADT;
  27. /*
  28. * Infinity and minus infinity must be the max and min values of DateADT.
  29. */
  30. #define DATEVAL_NOBEGIN ((DateADT) PG_INT32_MIN)
  31. #define DATEVAL_NOEND ((DateADT) PG_INT32_MAX)
  32. #define DATE_NOBEGIN(j) ((j) = DATEVAL_NOBEGIN)
  33. #define DATE_IS_NOBEGIN(j) ((j) == DATEVAL_NOBEGIN)
  34. #define DATE_NOEND(j) ((j) = DATEVAL_NOEND)
  35. #define DATE_IS_NOEND(j) ((j) == DATEVAL_NOEND)
  36. #define DATE_NOT_FINITE(j) (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))
  37. /*
  38. * Macros for fmgr-callable functions.
  39. *
  40. * For TimeADT, we make use of the same support routines as for int64.
  41. * Therefore TimeADT is pass-by-reference if and only if int64 is!
  42. */
  43. #define MAX_TIME_PRECISION 6
  44. #define DatumGetDateADT(X) ((DateADT) DatumGetInt32(X))
  45. #define DatumGetTimeADT(X) ((TimeADT) DatumGetInt64(X))
  46. #define DatumGetTimeTzADTP(X) ((TimeTzADT *) DatumGetPointer(X))
  47. #define DateADTGetDatum(X) Int32GetDatum(X)
  48. #define TimeADTGetDatum(X) Int64GetDatum(X)
  49. #define TimeTzADTPGetDatum(X) PointerGetDatum(X)
  50. #define PG_GETARG_DATEADT(n) DatumGetDateADT(PG_GETARG_DATUM(n))
  51. #define PG_GETARG_TIMEADT(n) DatumGetTimeADT(PG_GETARG_DATUM(n))
  52. #define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n))
  53. #define PG_RETURN_DATEADT(x) return DateADTGetDatum(x)
  54. #define PG_RETURN_TIMEADT(x) return TimeADTGetDatum(x)
  55. #define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x)
  56. /* date.c */
  57. extern int32 anytime_typmod_check(bool istz, int32 typmod);
  58. extern double date2timestamp_no_overflow(DateADT dateVal);
  59. extern Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow);
  60. extern TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow);
  61. extern int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2);
  62. extern int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2);
  63. extern void EncodeSpecialDate(DateADT dt, char *str);
  64. extern DateADT GetSQLCurrentDate(void);
  65. extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
  66. extern TimeADT GetSQLLocalTime(int32 typmod);
  67. extern int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec);
  68. extern int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp);
  69. extern int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result);
  70. extern int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result);
  71. extern bool time_overflows(int hour, int min, int sec, fsec_t fsec);
  72. extern bool float_time_overflows(int hour, int min, double sec);
  73. extern void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
  74. #endif /* DATE_H */