blob: db57a18e66b34803f360e3956b9de9f4bc737e64 [file] [log] [blame]
Tim Peters2a799bf2002-12-16 20:18:38 +00001/* datetime.h
2 */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003#ifndef Py_LIMITED_API
Tim Peters2a799bf2002-12-16 20:18:38 +00004#ifndef DATETIME_H
5#define DATETIME_H
Tim Peters9ddf40b2004-06-20 22:41:32 +00006#ifdef __cplusplus
7extern "C" {
8#endif
Tim Peters2a799bf2002-12-16 20:18:38 +00009
10/* Fields are packed into successive bytes, each viewed as unsigned and
11 * big-endian, unless otherwise noted:
12 *
13 * byte offset
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 * 0 year 2 bytes, 1-9999
15 * 2 month 1 byte, 1-12
16 * 3 day 1 byte, 1-31
17 * 4 hour 1 byte, 0-23
18 * 5 minute 1 byte, 0-59
19 * 6 second 1 byte, 0-59
20 * 7 usecond 3 bytes, 0-999999
Tim Peters2a799bf2002-12-16 20:18:38 +000021 * 10
22 */
23
24/* # of bytes for year, month, and day. */
25#define _PyDateTime_DATE_DATASIZE 4
26
27/* # of bytes for hour, minute, second, and usecond. */
28#define _PyDateTime_TIME_DATASIZE 6
29
30/* # of bytes for year, month, day, hour, minute, second, and usecond. */
31#define _PyDateTime_DATETIME_DATASIZE 10
32
Tim Petersa032d2e2003-01-11 00:15:54 +000033
34typedef struct
35{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 PyObject_HEAD
Benjamin Peterson8f67d082010-10-17 20:54:53 +000037 Py_hash_t hashcode; /* -1 when unknown */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 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 */
Tim Petersa032d2e2003-01-11 00:15:54 +000041} PyDateTime_Delta;
42
43typedef struct
44{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 PyObject_HEAD /* a pure abstract base clase */
Tim Petersa032d2e2003-01-11 00:15:54 +000046} PyDateTime_TZInfo;
Tim Peters37f39822003-01-10 03:49:02 +000047
Tim Petersa032d2e2003-01-11 00:15:54 +000048
49/* The datetime and time types have hashcodes, and an optional tzinfo member,
50 * present if and only if hastzinfo is true.
51 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052#define _PyTZINFO_HEAD \
53 PyObject_HEAD \
Benjamin Peterson8f67d082010-10-17 20:54:53 +000054 Py_hash_t hashcode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 char hastzinfo; /* boolean flag */
Tim Petersa032d2e2003-01-11 00:15:54 +000056
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.
60 */
61typedef struct
62{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 _PyTZINFO_HEAD
Tim Petersa032d2e2003-01-11 00:15:54 +000064} _PyDateTime_BaseTZInfo;
65
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
70 * "without" case.
71 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072#define _PyDateTime_TIMEHEAD \
73 _PyTZINFO_HEAD \
74 unsigned char data[_PyDateTime_TIME_DATASIZE];
Tim Petersa032d2e2003-01-11 00:15:54 +000075
76typedef struct
77{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 _PyDateTime_TIMEHEAD
79} _PyDateTime_BaseTime; /* hastzinfo false */
Tim Petersa032d2e2003-01-11 00:15:54 +000080
81typedef struct
82{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 _PyDateTime_TIMEHEAD
84 PyObject *tzinfo;
85} PyDateTime_Time; /* hastzinfo true */
Tim Petersa032d2e2003-01-11 00:15:54 +000086
Tim Petersa9bc1682003-01-11 03:39:11 +000087
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).
92 */
93typedef struct
94{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 _PyTZINFO_HEAD
96 unsigned char data[_PyDateTime_DATE_DATASIZE];
Tim Petersa9bc1682003-01-11 03:39:11 +000097} PyDateTime_Date;
98
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099#define _PyDateTime_DATETIMEHEAD \
100 _PyTZINFO_HEAD \
101 unsigned char data[_PyDateTime_DATETIME_DATASIZE];
Tim Petersa032d2e2003-01-11 00:15:54 +0000102
Tim Peters2a799bf2002-12-16 20:18:38 +0000103typedef struct
104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 _PyDateTime_DATETIMEHEAD
106} _PyDateTime_BaseDateTime; /* hastzinfo false */
Tim Peters2a799bf2002-12-16 20:18:38 +0000107
108typedef struct
109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 _PyDateTime_DATETIMEHEAD
111 PyObject *tzinfo;
112} PyDateTime_DateTime; /* hastzinfo true */
Tim Peters2a799bf2002-12-16 20:18:38 +0000113
Tim Peters37f39822003-01-10 03:49:02 +0000114
Tim Petersa9bc1682003-01-11 03:39:11 +0000115/* Apply for date and datetime instances. */
Tim Peters2a799bf2002-12-16 20:18:38 +0000116#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 ((PyDateTime_Date*)o)->data[1])
Tim Peters2a799bf2002-12-16 20:18:38 +0000118#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
119#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
120
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])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124#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])
Tim Peters2a799bf2002-12-16 20:18:38 +0000128
Tim Peters37f39822003-01-10 03:49:02 +0000129/* Apply for time instances. */
Tim Peters2a799bf2002-12-16 20:18:38 +0000130#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])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133#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])
Tim Peters2a799bf2002-12-16 20:18:38 +0000137
Tim Peters9ddf40b2004-06-20 22:41:32 +0000138
139/* Define structure for C API. */
140typedef struct {
141 /* type objects */
142 PyTypeObject *DateType;
143 PyTypeObject *DateTimeType;
144 PyTypeObject *TimeType;
145 PyTypeObject *DeltaType;
146 PyTypeObject *TZInfoType;
147
148 /* constructors */
149 PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
150 PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyObject*, PyTypeObject*);
Tim Peters9ddf40b2004-06-20 22:41:32 +0000152 PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
153 PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
154
155 /* constructors for the DB API */
156 PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
157 PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
158
159} PyDateTime_CAPI;
160
Benjamin Petersonb173f782009-05-05 22:31:58 +0000161#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
Tim Peters9ddf40b2004-06-20 22:41:32 +0000162
Tim Peters9ddf40b2004-06-20 22:41:32 +0000163
164#ifdef Py_BUILD_CORE
165
166/* Macros for type checking when building the Python core. */
Tim Peters2a799bf2002-12-16 20:18:38 +0000167#define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000168#define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType)
Tim Peters2a799bf2002-12-16 20:18:38 +0000169
170#define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000171#define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType)
Tim Peters2a799bf2002-12-16 20:18:38 +0000172
Tim Peters2a799bf2002-12-16 20:18:38 +0000173#define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000174#define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType)
Tim Peters2a799bf2002-12-16 20:18:38 +0000175
Tim Peters2a799bf2002-12-16 20:18:38 +0000176#define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000177#define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType)
Tim Peters2a799bf2002-12-16 20:18:38 +0000178
179#define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000180#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
Tim Peters2a799bf2002-12-16 20:18:38 +0000181
Tim Peters9ddf40b2004-06-20 22:41:32 +0000182#else
183
184/* Define global variable for the C API and a macro for setting it. */
Benjamin Peterson91d58bd2009-12-13 21:30:54 +0000185static PyDateTime_CAPI *PyDateTimeAPI = NULL;
Tim Peters9ddf40b2004-06-20 22:41:32 +0000186
187#define PyDateTime_IMPORT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000189
190/* Macros for type checking when not building the Python core. */
191#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000192#define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000193
194#define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000195#define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000196
197#define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000198#define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000199
200#define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000201#define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000202
203#define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
Christian Heimes90aa7642007-12-19 02:45:37 +0000204#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000205
206/* Macros for accessing constructors in a simplified fashion. */
207#define PyDate_FromDate(year, month, day) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000209
210#define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
212 min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000213
214#define PyTime_FromTime(hour, minute, second, usecond) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
216 Py_None, PyDateTimeAPI->TimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000217
218#define PyDelta_FromDSU(days, seconds, useconds) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
220 PyDateTimeAPI->DeltaType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000221
222/* Macros supporting the DB API. */
223#define PyDateTime_FromTimestamp(args) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyDateTimeAPI->DateTime_FromTimestamp( \
225 (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000226
227#define PyDate_FromTimestamp(args) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 PyDateTimeAPI->Date_FromTimestamp( \
229 (PyObject*) (PyDateTimeAPI->DateType), args)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231#endif /* Py_BUILD_CORE */
Tim Peters9ddf40b2004-06-20 22:41:32 +0000232
233#ifdef __cplusplus
234}
235#endif
Tim Peters2a799bf2002-12-16 20:18:38 +0000236#endif
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000237#endif /* !Py_LIMITED_API */