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