datetime.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. PyObject *tzinfo;
  72. } PyDateTime_Time; /* hastzinfo true */
  73. /* All datetime objects are of PyDateTime_DateTimeType, but that can be
  74. * allocated in two ways too, just like for time objects above. In addition,
  75. * the plain date type is a base class for datetime, so it must also have
  76. * a hastzinfo member (although it's unused there).
  77. */
  78. typedef struct
  79. {
  80. _PyTZINFO_HEAD
  81. unsigned char data[_PyDateTime_DATE_DATASIZE];
  82. } PyDateTime_Date;
  83. #define _PyDateTime_DATETIMEHEAD \
  84. _PyTZINFO_HEAD \
  85. unsigned char data[_PyDateTime_DATETIME_DATASIZE];
  86. typedef struct
  87. {
  88. _PyDateTime_DATETIMEHEAD
  89. } _PyDateTime_BaseDateTime; /* hastzinfo false */
  90. typedef struct
  91. {
  92. _PyDateTime_DATETIMEHEAD
  93. PyObject *tzinfo;
  94. } PyDateTime_DateTime; /* hastzinfo true */
  95. /* Apply for date and datetime instances. */
  96. #define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
  97. ((PyDateTime_Date*)o)->data[1])
  98. #define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
  99. #define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
  100. #define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
  101. #define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
  102. #define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
  103. #define PyDateTime_DATE_GET_MICROSECOND(o) \
  104. ((((PyDateTime_DateTime*)o)->data[7] << 16) | \
  105. (((PyDateTime_DateTime*)o)->data[8] << 8) | \
  106. ((PyDateTime_DateTime*)o)->data[9])
  107. /* Apply for time instances. */
  108. #define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
  109. #define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
  110. #define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
  111. #define PyDateTime_TIME_GET_MICROSECOND(o) \
  112. ((((PyDateTime_Time*)o)->data[3] << 16) | \
  113. (((PyDateTime_Time*)o)->data[4] << 8) | \
  114. ((PyDateTime_Time*)o)->data[5])
  115. /* Apply for time delta instances */
  116. #define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
  117. #define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
  118. #define PyDateTime_DELTA_GET_MICROSECONDS(o) \
  119. (((PyDateTime_Delta*)o)->microseconds)
  120. /* Define structure for C API. */
  121. typedef struct {
  122. /* type objects */
  123. PyTypeObject *DateType;
  124. PyTypeObject *DateTimeType;
  125. PyTypeObject *TimeType;
  126. PyTypeObject *DeltaType;
  127. PyTypeObject *TZInfoType;
  128. /* constructors */
  129. PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
  130. PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
  131. PyObject*, PyTypeObject*);
  132. PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
  133. PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
  134. /* constructors for the DB API */
  135. PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
  136. PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
  137. } PyDateTime_CAPI;
  138. #define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
  139. #ifdef Py_BUILD_CORE
  140. /* Macros for type checking when building the Python core. */
  141. #define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType)
  142. #define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType)
  143. #define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType)
  144. #define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType)
  145. #define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType)
  146. #define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType)
  147. #define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType)
  148. #define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType)
  149. #define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
  150. #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
  151. #else
  152. /* Define global variable for the C API and a macro for setting it. */
  153. static PyDateTime_CAPI *PyDateTimeAPI = NULL;
  154. #define PyDateTime_IMPORT \
  155. PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
  156. /* Macros for type checking when not building the Python core. */
  157. #define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
  158. #define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
  159. #define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
  160. #define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
  161. #define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
  162. #define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
  163. #define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
  164. #define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
  165. #define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
  166. #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
  167. /* Macros for accessing constructors in a simplified fashion. */
  168. #define PyDate_FromDate(year, month, day) \
  169. PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
  170. #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
  171. PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
  172. min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
  173. #define PyTime_FromTime(hour, minute, second, usecond) \
  174. PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
  175. Py_None, PyDateTimeAPI->TimeType)
  176. #define PyDelta_FromDSU(days, seconds, useconds) \
  177. PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
  178. PyDateTimeAPI->DeltaType)
  179. /* Macros supporting the DB API. */
  180. #define PyDateTime_FromTimestamp(args) \
  181. PyDateTimeAPI->DateTime_FromTimestamp( \
  182. (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
  183. #define PyDate_FromTimestamp(args) \
  184. PyDateTimeAPI->Date_FromTimestamp( \
  185. (PyObject*) (PyDateTimeAPI->DateType), args)
  186. #endif /* Py_BUILD_CORE */
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif
  191. #endif /* !Py_LIMITED_API */