10
/* Fields are packed into successive bytes, each viewed as unsigned and
11
* big-endian, unless otherwise noted:
14
* 0 year 2 bytes, 1-9999
15
* 2 month 1 byte, 1-12
18
* 5 minute 1 byte, 0-59
19
* 6 second 1 byte, 0-59
20
* 7 usecond 3 bytes, 0-999999
24
/* # of bytes for year, month, and day. */
25
#define _PyDateTime_DATE_DATASIZE 4
27
/* # of bytes for hour, minute, second, and usecond. */
28
#define _PyDateTime_TIME_DATASIZE 6
30
/* # of bytes for year, month, day, hour, minute, second, and usecond. */
31
#define _PyDateTime_DATETIME_DATASIZE 10
37
long hashcode; /* -1 when unknown */
38
int days; /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */
39
int seconds; /* 0 <= seconds < 24*3600 is invariant */
40
int microseconds; /* 0 <= microseconds < 1000000 is invariant */
45
PyObject_HEAD /* a pure abstract base class */
49
/* The datetime and time types have hashcodes, and an optional tzinfo member,
50
* present if and only if hastzinfo is true.
52
#define _PyTZINFO_HEAD \
55
char hastzinfo; /* boolean flag */
57
/* No _PyDateTime_BaseTZInfo is allocated; it's just to have something
58
* convenient to cast to, when getting at the hastzinfo member of objects
59
* starting with _PyTZINFO_HEAD.
64
} _PyDateTime_BaseTZInfo;
66
/* All time objects are of PyDateTime_TimeType, but that can be allocated
67
* in two ways, with or without a tzinfo member. Without is the same as
68
* tzinfo == None, but consumes less memory. _PyDateTime_BaseTime is an
69
* internal struct used to allocate the right amount of space for the
72
#define _PyDateTime_TIMEHEAD \
74
unsigned char data[_PyDateTime_TIME_DATASIZE];
79
} _PyDateTime_BaseTime; /* hastzinfo false */
85
} PyDateTime_Time; /* hastzinfo true */
88
/* All datetime objects are of PyDateTime_DateTimeType, but that can be
89
* allocated in two ways too, just like for time objects above. In addition,
90
* the plain date type is a base class for datetime, so it must also have
91
* a hastzinfo member (although it's unused there).
96
unsigned char data[_PyDateTime_DATE_DATASIZE];
99
#define _PyDateTime_DATETIMEHEAD \
101
unsigned char data[_PyDateTime_DATETIME_DATASIZE];
105
_PyDateTime_DATETIMEHEAD
106
} _PyDateTime_BaseDateTime; /* hastzinfo false */
110
_PyDateTime_DATETIMEHEAD
112
} PyDateTime_DateTime; /* hastzinfo true */
115
/* Apply for date and datetime instances. */
116
#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
117
((PyDateTime_Date*)o)->data[1])
118
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
119
#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
121
#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
122
#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
123
#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
124
#define PyDateTime_DATE_GET_MICROSECOND(o) \
125
((((PyDateTime_DateTime*)o)->data[7] << 16) | \
126
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
127
((PyDateTime_DateTime*)o)->data[9])
129
/* Apply for time instances. */
130
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
131
#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
132
#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
133
#define PyDateTime_TIME_GET_MICROSECOND(o) \
134
((((PyDateTime_Time*)o)->data[3] << 16) | \
135
(((PyDateTime_Time*)o)->data[4] << 8) | \
136
((PyDateTime_Time*)o)->data[5])
139
/* Define structure for C API. */
142
PyTypeObject *DateType;
143
PyTypeObject *DateTimeType;
144
PyTypeObject *TimeType;
145
PyTypeObject *DeltaType;
146
PyTypeObject *TZInfoType;
149
PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
150
PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
151
PyObject*, PyTypeObject*);
152
PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
153
PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
155
/* constructors for the DB API */
156
PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
157
PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
161
#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
164
/* "magic" constant used to partially protect against developer mistakes. */
165
#define DATETIME_API_MAGIC 0x414548d5
169
/* Macros for type checking when building the Python core. */
170
#define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType)
171
#define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType)
173
#define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType)
174
#define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType)
176
#define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType)
177
#define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType)
179
#define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType)
180
#define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType)
182
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
183
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
187
/* Define global variable for the C API and a macro for setting it. */
188
static PyDateTime_CAPI *PyDateTimeAPI = NULL;
190
#define PyDateTime_IMPORT \
191
PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
193
/* Macros for type checking when not building the Python core. */
194
#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
195
#define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
197
#define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
198
#define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
200
#define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
201
#define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
203
#define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
204
#define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
206
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
207
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
209
/* Macros for accessing constructors in a simplified fashion. */
210
#define PyDate_FromDate(year, month, day) \
211
PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
213
#define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
214
PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
215
min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
217
#define PyTime_FromTime(hour, minute, second, usecond) \
218
PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
219
Py_None, PyDateTimeAPI->TimeType)
221
#define PyDelta_FromDSU(days, seconds, useconds) \
222
PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
223
PyDateTimeAPI->DeltaType)
225
/* Macros supporting the DB API. */
226
#define PyDateTime_FromTimestamp(args) \
227
PyDateTimeAPI->DateTime_FromTimestamp( \
228
(PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
230
#define PyDate_FromTimestamp(args) \
231
PyDateTimeAPI->Date_FromTimestamp( \
232
(PyObject*) (PyDateTimeAPI->DateType), args)
234
#endif /* Py_BUILD_CORE */
10
/* Fields are packed into successive bytes, each viewed as unsigned and
11
* big-endian, unless otherwise noted:
14
* 0 year 2 bytes, 1-9999
15
* 2 month 1 byte, 1-12
18
* 5 minute 1 byte, 0-59
19
* 6 second 1 byte, 0-59
20
* 7 usecond 3 bytes, 0-999999
24
/* # of bytes for year, month, and day. */
25
#define _PyDateTime_DATE_DATASIZE 4
27
/* # of bytes for hour, minute, second, and usecond. */
28
#define _PyDateTime_TIME_DATASIZE 6
30
/* # of bytes for year, month, day, hour, minute, second, and usecond. */
31
#define _PyDateTime_DATETIME_DATASIZE 10
37
long hashcode; /* -1 when unknown */
38
int days; /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */
39
int seconds; /* 0 <= seconds < 24*3600 is invariant */
40
int microseconds; /* 0 <= microseconds < 1000000 is invariant */
45
PyObject_HEAD /* a pure abstract base class */
49
/* The datetime and time types have hashcodes, and an optional tzinfo member,
50
* present if and only if hastzinfo is true.
52
#define _PyTZINFO_HEAD \
55
char hastzinfo; /* boolean flag */
57
/* No _PyDateTime_BaseTZInfo is allocated; it's just to have something
58
* convenient to cast to, when getting at the hastzinfo member of objects
59
* starting with _PyTZINFO_HEAD.
64
} _PyDateTime_BaseTZInfo;
66
/* All time objects are of PyDateTime_TimeType, but that can be allocated
67
* in two ways, with or without a tzinfo member. Without is the same as
68
* tzinfo == None, but consumes less memory. _PyDateTime_BaseTime is an
69
* internal struct used to allocate the right amount of space for the
72
#define _PyDateTime_TIMEHEAD \
74
unsigned char data[_PyDateTime_TIME_DATASIZE];
79
} _PyDateTime_BaseTime; /* hastzinfo false */
85
} PyDateTime_Time; /* hastzinfo true */
88
/* All datetime objects are of PyDateTime_DateTimeType, but that can be
89
* allocated in two ways too, just like for time objects above. In addition,
90
* the plain date type is a base class for datetime, so it must also have
91
* a hastzinfo member (although it's unused there).
96
unsigned char data[_PyDateTime_DATE_DATASIZE];
99
#define _PyDateTime_DATETIMEHEAD \
101
unsigned char data[_PyDateTime_DATETIME_DATASIZE];
105
_PyDateTime_DATETIMEHEAD
106
} _PyDateTime_BaseDateTime; /* hastzinfo false */
110
_PyDateTime_DATETIMEHEAD
112
} PyDateTime_DateTime; /* hastzinfo true */
115
/* Apply for date and datetime instances. */
116
#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
117
((PyDateTime_Date*)o)->data[1])
118
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
119
#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
121
#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
122
#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
123
#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
124
#define PyDateTime_DATE_GET_MICROSECOND(o) \
125
((((PyDateTime_DateTime*)o)->data[7] << 16) | \
126
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
127
((PyDateTime_DateTime*)o)->data[9])
129
/* Apply for time instances. */
130
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
131
#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
132
#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
133
#define PyDateTime_TIME_GET_MICROSECOND(o) \
134
((((PyDateTime_Time*)o)->data[3] << 16) | \
135
(((PyDateTime_Time*)o)->data[4] << 8) | \
136
((PyDateTime_Time*)o)->data[5])
139
/* Define structure for C API. */
142
PyTypeObject *DateType;
143
PyTypeObject *DateTimeType;
144
PyTypeObject *TimeType;
145
PyTypeObject *DeltaType;
146
PyTypeObject *TZInfoType;
149
PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
150
PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
151
PyObject*, PyTypeObject*);
152
PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
153
PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
155
/* constructors for the DB API */
156
PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
157
PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
161
#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
164
/* "magic" constant used to partially protect against developer mistakes. */
165
#define DATETIME_API_MAGIC 0x414548d5
169
/* Macros for type checking when building the Python core. */
170
#define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType)
171
#define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType)
173
#define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType)
174
#define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType)
176
#define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType)
177
#define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType)
179
#define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType)
180
#define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType)
182
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
183
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
187
/* Define global variable for the C API and a macro for setting it. */
188
static PyDateTime_CAPI *PyDateTimeAPI = NULL;
190
#define PyDateTime_IMPORT \
191
PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
193
/* Macros for type checking when not building the Python core. */
194
#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
195
#define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
197
#define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
198
#define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
200
#define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
201
#define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
203
#define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
204
#define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
206
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
207
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
209
/* Macros for accessing constructors in a simplified fashion. */
210
#define PyDate_FromDate(year, month, day) \
211
PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
213
#define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
214
PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
215
min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
217
#define PyTime_FromTime(hour, minute, second, usecond) \
218
PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
219
Py_None, PyDateTimeAPI->TimeType)
221
#define PyDelta_FromDSU(days, seconds, useconds) \
222
PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
223
PyDateTimeAPI->DeltaType)
225
/* Macros supporting the DB API. */
226
#define PyDateTime_FromTimestamp(args) \
227
PyDateTimeAPI->DateTime_FromTimestamp( \
228
(PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
230
#define PyDate_FromTimestamp(args) \
231
PyDateTimeAPI->Date_FromTimestamp( \
232
(PyObject*) (PyDateTimeAPI->DateType), args)
234
#endif /* Py_BUILD_CORE */