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