Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

datetime.h 9.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  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. #ifdef Py_BUILD_CORE
  151. /* Macros for type checking when building the Python core. */
  152. #define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType)
  153. #define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType)
  154. #define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType)
  155. #define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType)
  156. #define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType)
  157. #define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType)
  158. #define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType)
  159. #define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType)
  160. #define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
  161. #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
  162. #else
  163. /* Define global variable for the C API and a macro for setting it. */
  164. static PyDateTime_CAPI *PyDateTimeAPI = NULL;
  165. #define PyDateTime_IMPORT \
  166. PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
  167. /* Macro for access to the UTC singleton */
  168. #define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
  169. /* Macros for type checking when not building the Python core. */
  170. #define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
  171. #define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
  172. #define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
  173. #define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
  174. #define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
  175. #define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
  176. #define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
  177. #define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
  178. #define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
  179. #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
  180. /* Macros for accessing constructors in a simplified fashion. */
  181. #define PyDate_FromDate(year, month, day) \
  182. PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
  183. #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
  184. PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
  185. min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
  186. #define PyDateTime_FromDateAndTimeAndFold(year, month, day, hour, min, sec, usec, fold) \
  187. PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(year, month, day, hour, \
  188. min, sec, usec, Py_None, fold, PyDateTimeAPI->DateTimeType)
  189. #define PyTime_FromTime(hour, minute, second, usecond) \
  190. PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
  191. Py_None, PyDateTimeAPI->TimeType)
  192. #define PyTime_FromTimeAndFold(hour, minute, second, usecond, fold) \
  193. PyDateTimeAPI->Time_FromTimeAndFold(hour, minute, second, usecond, \
  194. Py_None, fold, PyDateTimeAPI->TimeType)
  195. #define PyDelta_FromDSU(days, seconds, useconds) \
  196. PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
  197. PyDateTimeAPI->DeltaType)
  198. #define PyTimeZone_FromOffset(offset) \
  199. PyDateTimeAPI->TimeZone_FromTimeZone(offset, NULL)
  200. #define PyTimeZone_FromOffsetAndName(offset, name) \
  201. PyDateTimeAPI->TimeZone_FromTimeZone(offset, name)
  202. /* Macros supporting the DB API. */
  203. #define PyDateTime_FromTimestamp(args) \
  204. PyDateTimeAPI->DateTime_FromTimestamp( \
  205. (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
  206. #define PyDate_FromTimestamp(args) \
  207. PyDateTimeAPI->Date_FromTimestamp( \
  208. (PyObject*) (PyDateTimeAPI->DateType), args)
  209. #endif /* Py_BUILD_CORE */
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif
  214. #endif /* !Py_LIMITED_API */
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...