Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
8.1 KiB

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