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