datetime.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* datetime.h
  2. */
  3. #ifndef Py_LIMITED_API
  4. #ifndef DATETIME_H
  5. #define DATETIME_H
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* Fields are packed into successive bytes, each viewed as unsigned and
  10. * big-endian, unless otherwise noted:
  11. *
  12. * byte offset
  13. * 0 year 2 bytes, 1-9999
  14. * 2 month 1 byte, 1-12
  15. * 3 day 1 byte, 1-31
  16. * 4 hour 1 byte, 0-23
  17. * 5 minute 1 byte, 0-59
  18. * 6 second 1 byte, 0-59
  19. * 7 usecond 3 bytes, 0-999999
  20. * 10
  21. */
  22. /* # of bytes for year, month, and day. */
  23. #define _PyDateTime_DATE_DATASIZE 4
  24. /* # of bytes for hour, minute, second, and usecond. */
  25. #define _PyDateTime_TIME_DATASIZE 6
  26. /* # of bytes for year, month, day, hour, minute, second, and usecond. */
  27. #define _PyDateTime_DATETIME_DATASIZE 10
  28. typedef struct
  29. {
  30. PyObject_HEAD
  31. Py_hash_t hashcode; /* -1 when unknown */
  32. int days; /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */
  33. int seconds; /* 0 <= seconds < 24*3600 is invariant */
  34. int microseconds; /* 0 <= microseconds < 1000000 is invariant */
  35. } PyDateTime_Delta;
  36. typedef struct
  37. {
  38. PyObject_HEAD /* a pure abstract base class */
  39. } PyDateTime_TZInfo;
  40. /* The datetime and time types have hashcodes, and an optional tzinfo member,
  41. * present if and only if hastzinfo is true.
  42. */
  43. #define _PyTZINFO_HEAD \
  44. PyObject_HEAD \
  45. Py_hash_t hashcode; \
  46. char hastzinfo; /* boolean flag */
  47. /* No _PyDateTime_BaseTZInfo is allocated; it's just to have something
  48. * convenient to cast to, when getting at the hastzinfo member of objects
  49. * starting with _PyTZINFO_HEAD.
  50. */
  51. typedef struct
  52. {
  53. _PyTZINFO_HEAD
  54. } _PyDateTime_BaseTZInfo;
  55. /* All time objects are of PyDateTime_TimeType, but that can be allocated
  56. * in two ways, with or without a tzinfo member. Without is the same as
  57. * tzinfo == None, but consumes less memory. _PyDateTime_BaseTime is an
  58. * internal struct used to allocate the right amount of space for the
  59. * "without" case.
  60. */
  61. #define _PyDateTime_TIMEHEAD \
  62. _PyTZINFO_HEAD \
  63. unsigned char data[_PyDateTime_TIME_DATASIZE];
  64. typedef struct
  65. {
  66. _PyDateTime_TIMEHEAD
  67. } _PyDateTime_BaseTime; /* hastzinfo false */
  68. typedef struct
  69. {
  70. _PyDateTime_TIMEHEAD
  71. unsigned char fold;
  72. PyObject *tzinfo;
  73. } PyDateTime_Time; /* hastzinfo true */
  74. /* All datetime objects are of PyDateTime_DateTimeType, but that can be
  75. * allocated in two ways too, just like for time objects above. In addition,
  76. * the plain date type is a base class for datetime, so it must also have
  77. * a hastzinfo member (although it's unused there).
  78. */
  79. typedef struct
  80. {
  81. _PyTZINFO_HEAD
  82. unsigned char data[_PyDateTime_DATE_DATASIZE];
  83. } PyDateTime_Date;
  84. #define _PyDateTime_DATETIMEHEAD \
  85. _PyTZINFO_HEAD \
  86. unsigned char data[_PyDateTime_DATETIME_DATASIZE];
  87. typedef struct
  88. {
  89. _PyDateTime_DATETIMEHEAD
  90. } _PyDateTime_BaseDateTime; /* hastzinfo false */
  91. typedef struct
  92. {
  93. _PyDateTime_DATETIMEHEAD
  94. unsigned char fold;
  95. PyObject *tzinfo;
  96. } PyDateTime_DateTime; /* hastzinfo true */
  97. /* Apply for date and datetime instances. */
  98. #define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
  99. ((PyDateTime_Date*)o)->data[1])
  100. #define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
  101. #define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
  102. #define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
  103. #define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
  104. #define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
  105. #define PyDateTime_DATE_GET_MICROSECOND(o) \
  106. ((((PyDateTime_DateTime*)o)->data[7] << 16) | \
  107. (((PyDateTime_DateTime*)o)->data[8] << 8) | \
  108. ((PyDateTime_DateTime*)o)->data[9])
  109. #define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
  110. /* Apply for time instances. */
  111. #define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
  112. #define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
  113. #define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
  114. #define PyDateTime_TIME_GET_MICROSECOND(o) \
  115. ((((PyDateTime_Time*)o)->data[3] << 16) | \
  116. (((PyDateTime_Time*)o)->data[4] << 8) | \
  117. ((PyDateTime_Time*)o)->data[5])
  118. #define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
  119. /* Apply for time delta instances */
  120. #define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
  121. #define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
  122. #define PyDateTime_DELTA_GET_MICROSECONDS(o) \
  123. (((PyDateTime_Delta*)o)->microseconds)
  124. /* Define structure for C API. */
  125. typedef struct {
  126. /* type objects */
  127. PyTypeObject *DateType;
  128. PyTypeObject *DateTimeType;
  129. PyTypeObject *TimeType;
  130. PyTypeObject *DeltaType;
  131. PyTypeObject *TZInfoType;
  132. /* singletons */
  133. PyObject *TimeZone_UTC;
  134. /* constructors */
  135. PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
  136. PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
  137. PyObject*, PyTypeObject*);
  138. PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
  139. PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
  140. PyObject *(*TimeZone_FromTimeZone)(PyObject *offset, PyObject *name);
  141. /* constructors for the DB API */
  142. PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
  143. PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
  144. /* PEP 495 constructors */
  145. PyObject *(*DateTime_FromDateAndTimeAndFold)(int, int, int, int, int, int, int,
  146. PyObject*, int, PyTypeObject*);
  147. PyObject *(*Time_FromTimeAndFold)(int, int, int, int, PyObject*, int, PyTypeObject*);
  148. } PyDateTime_CAPI;
  149. #define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
  150. /* This block is only used as part of the public API and should not be
  151. * included in _datetimemodule.c, which does not use the C API capsule.
  152. * See bpo-35081 for more details.
  153. * */
  154. #ifndef _PY_DATETIME_IMPL
  155. /* Define global variable for the C API and a macro for setting it. */
  156. static PyDateTime_CAPI *PyDateTimeAPI = NULL;
  157. #define PyDateTime_IMPORT \
  158. PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
  159. /* Macro for access to the UTC singleton */
  160. #define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
  161. /* Macros for type checking when not building the Python core. */
  162. #define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
  163. #define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
  164. #define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
  165. #define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
  166. #define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
  167. #define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
  168. #define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
  169. #define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
  170. #define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
  171. #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
  172. /* Macros for accessing constructors in a simplified fashion. */
  173. #define PyDate_FromDate(year, month, day) \
  174. PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
  175. #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
  176. PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
  177. min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
  178. #define PyDateTime_FromDateAndTimeAndFold(year, month, day, hour, min, sec, usec, fold) \
  179. PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(year, month, day, hour, \
  180. min, sec, usec, Py_None, fold, PyDateTimeAPI->DateTimeType)
  181. #define PyTime_FromTime(hour, minute, second, usecond) \
  182. PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
  183. Py_None, PyDateTimeAPI->TimeType)
  184. #define PyTime_FromTimeAndFold(hour, minute, second, usecond, fold) \
  185. PyDateTimeAPI->Time_FromTimeAndFold(hour, minute, second, usecond, \
  186. Py_None, fold, PyDateTimeAPI->TimeType)
  187. #define PyDelta_FromDSU(days, seconds, useconds) \
  188. PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
  189. PyDateTimeAPI->DeltaType)
  190. #define PyTimeZone_FromOffset(offset) \
  191. PyDateTimeAPI->TimeZone_FromTimeZone(offset, NULL)
  192. #define PyTimeZone_FromOffsetAndName(offset, name) \
  193. PyDateTimeAPI->TimeZone_FromTimeZone(offset, name)
  194. /* Macros supporting the DB API. */
  195. #define PyDateTime_FromTimestamp(args) \
  196. PyDateTimeAPI->DateTime_FromTimestamp( \
  197. (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
  198. #define PyDate_FromTimestamp(args) \
  199. PyDateTimeAPI->Date_FromTimestamp( \
  200. (PyObject*) (PyDateTimeAPI->DateType), args)
  201. #endif /* !defined(_PY_DATETIME_IMPL) */
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif
  206. #endif /* !Py_LIMITED_API */