datetime.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. // o is a pointer to a time or a datetime object.
  99. #define _PyDateTime_HAS_TZINFO(o) (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
  100. #define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)(o))->data[0] << 8) | \
  101. ((PyDateTime_Date*)(o))->data[1])
  102. #define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)(o))->data[2])
  103. #define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)(o))->data[3])
  104. #define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)(o))->data[4])
  105. #define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)(o))->data[5])
  106. #define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)(o))->data[6])
  107. #define PyDateTime_DATE_GET_MICROSECOND(o) \
  108. ((((PyDateTime_DateTime*)(o))->data[7] << 16) | \
  109. (((PyDateTime_DateTime*)(o))->data[8] << 8) | \
  110. ((PyDateTime_DateTime*)(o))->data[9])
  111. #define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)(o))->fold)
  112. #define PyDateTime_DATE_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO((o)) ? \
  113. ((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
  114. /* Apply for time instances. */
  115. #define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)(o))->data[0])
  116. #define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)(o))->data[1])
  117. #define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)(o))->data[2])
  118. #define PyDateTime_TIME_GET_MICROSECOND(o) \
  119. ((((PyDateTime_Time*)(o))->data[3] << 16) | \
  120. (((PyDateTime_Time*)(o))->data[4] << 8) | \
  121. ((PyDateTime_Time*)(o))->data[5])
  122. #define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)(o))->fold)
  123. #define PyDateTime_TIME_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
  124. ((PyDateTime_Time *)(o))->tzinfo : Py_None)
  125. /* Apply for time delta instances */
  126. #define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)(o))->days)
  127. #define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)(o))->seconds)
  128. #define PyDateTime_DELTA_GET_MICROSECONDS(o) \
  129. (((PyDateTime_Delta*)(o))->microseconds)
  130. /* Define structure for C API. */
  131. typedef struct {
  132. /* type objects */
  133. PyTypeObject *DateType;
  134. PyTypeObject *DateTimeType;
  135. PyTypeObject *TimeType;
  136. PyTypeObject *DeltaType;
  137. PyTypeObject *TZInfoType;
  138. /* singletons */
  139. PyObject *TimeZone_UTC;
  140. /* constructors */
  141. PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
  142. PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
  143. PyObject*, PyTypeObject*);
  144. PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
  145. PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
  146. PyObject *(*TimeZone_FromTimeZone)(PyObject *offset, PyObject *name);
  147. /* constructors for the DB API */
  148. PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
  149. PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
  150. /* PEP 495 constructors */
  151. PyObject *(*DateTime_FromDateAndTimeAndFold)(int, int, int, int, int, int, int,
  152. PyObject*, int, PyTypeObject*);
  153. PyObject *(*Time_FromTimeAndFold)(int, int, int, int, PyObject*, int, PyTypeObject*);
  154. } PyDateTime_CAPI;
  155. #define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
  156. /* This block is only used as part of the public API and should not be
  157. * included in _datetimemodule.c, which does not use the C API capsule.
  158. * See bpo-35081 for more details.
  159. * */
  160. #ifndef _PY_DATETIME_IMPL
  161. /* Define global variable for the C API and a macro for setting it. */
  162. static PyDateTime_CAPI *PyDateTimeAPI = NULL;
  163. #define PyDateTime_IMPORT \
  164. PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
  165. /* Macro for access to the UTC singleton */
  166. #define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
  167. /* Macros for type checking when not building the Python core. */
  168. #define PyDate_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DateType)
  169. #define PyDate_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DateType)
  170. #define PyDateTime_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DateTimeType)
  171. #define PyDateTime_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DateTimeType)
  172. #define PyTime_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->TimeType)
  173. #define PyTime_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->TimeType)
  174. #define PyDelta_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DeltaType)
  175. #define PyDelta_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DeltaType)
  176. #define PyTZInfo_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->TZInfoType)
  177. #define PyTZInfo_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->TZInfoType)
  178. /* Macros for accessing constructors in a simplified fashion. */
  179. #define PyDate_FromDate(year, month, day) \
  180. PyDateTimeAPI->Date_FromDate((year), (month), (day), PyDateTimeAPI->DateType)
  181. #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
  182. PyDateTimeAPI->DateTime_FromDateAndTime((year), (month), (day), (hour), \
  183. (min), (sec), (usec), Py_None, PyDateTimeAPI->DateTimeType)
  184. #define PyDateTime_FromDateAndTimeAndFold(year, month, day, hour, min, sec, usec, fold) \
  185. PyDateTimeAPI->DateTime_FromDateAndTimeAndFold((year), (month), (day), (hour), \
  186. (min), (sec), (usec), Py_None, (fold), PyDateTimeAPI->DateTimeType)
  187. #define PyTime_FromTime(hour, minute, second, usecond) \
  188. PyDateTimeAPI->Time_FromTime((hour), (minute), (second), (usecond), \
  189. Py_None, PyDateTimeAPI->TimeType)
  190. #define PyTime_FromTimeAndFold(hour, minute, second, usecond, fold) \
  191. PyDateTimeAPI->Time_FromTimeAndFold((hour), (minute), (second), (usecond), \
  192. Py_None, (fold), PyDateTimeAPI->TimeType)
  193. #define PyDelta_FromDSU(days, seconds, useconds) \
  194. PyDateTimeAPI->Delta_FromDelta((days), (seconds), (useconds), 1, \
  195. PyDateTimeAPI->DeltaType)
  196. #define PyTimeZone_FromOffset(offset) \
  197. PyDateTimeAPI->TimeZone_FromTimeZone((offset), NULL)
  198. #define PyTimeZone_FromOffsetAndName(offset, name) \
  199. PyDateTimeAPI->TimeZone_FromTimeZone((offset), (name))
  200. /* Macros supporting the DB API. */
  201. #define PyDateTime_FromTimestamp(args) \
  202. PyDateTimeAPI->DateTime_FromTimestamp( \
  203. (PyObject*) (PyDateTimeAPI->DateTimeType), (args), NULL)
  204. #define PyDate_FromTimestamp(args) \
  205. PyDateTimeAPI->Date_FromTimestamp( \
  206. (PyObject*) (PyDateTimeAPI->DateType), (args))
  207. #endif /* !defined(_PY_DATETIME_IMPL) */
  208. #ifdef __cplusplus
  209. }
  210. #endif
  211. #endif
  212. #endif /* !Py_LIMITED_API */