blob: bb565201a164d7ed66c66697ae3ac5d6951b54b2 [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{
Ezio Melotti85a86292013-08-17 16:57:41 +030045 PyObject_HEAD /* a pure abstract base class */
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
Alexander Belopolsky5d0c5982016-07-22 18:47:04 -040084 unsigned char fold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 PyObject *tzinfo;
86} PyDateTime_Time; /* hastzinfo true */
Tim Petersa032d2e2003-01-11 00:15:54 +000087
Tim Petersa9bc1682003-01-11 03:39:11 +000088
89/* All datetime objects are of PyDateTime_DateTimeType, but that can be
90 * allocated in two ways too, just like for time objects above. In addition,
91 * the plain date type is a base class for datetime, so it must also have
92 * a hastzinfo member (although it's unused there).
93 */
94typedef struct
95{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 _PyTZINFO_HEAD
97 unsigned char data[_PyDateTime_DATE_DATASIZE];
Tim Petersa9bc1682003-01-11 03:39:11 +000098} PyDateTime_Date;
99
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define _PyDateTime_DATETIMEHEAD \
101 _PyTZINFO_HEAD \
102 unsigned char data[_PyDateTime_DATETIME_DATASIZE];
Tim Petersa032d2e2003-01-11 00:15:54 +0000103
Tim Peters2a799bf2002-12-16 20:18:38 +0000104typedef struct
105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 _PyDateTime_DATETIMEHEAD
107} _PyDateTime_BaseDateTime; /* hastzinfo false */
Tim Peters2a799bf2002-12-16 20:18:38 +0000108
109typedef struct
110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 _PyDateTime_DATETIMEHEAD
Alexander Belopolsky5d0c5982016-07-22 18:47:04 -0400112 unsigned char fold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 PyObject *tzinfo;
114} PyDateTime_DateTime; /* hastzinfo true */
Tim Peters2a799bf2002-12-16 20:18:38 +0000115
Tim Peters37f39822003-01-10 03:49:02 +0000116
Tim Petersa9bc1682003-01-11 03:39:11 +0000117/* Apply for date and datetime instances. */
Zackery Spytz2e4dd332020-09-23 12:43:45 -0600118
119// o is a pointer to a time or a datetime object.
120#define _PyDateTime_HAS_TZINFO(o) (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
121
Tim Peters2a799bf2002-12-16 20:18:38 +0000122#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 ((PyDateTime_Date*)o)->data[1])
Tim Peters2a799bf2002-12-16 20:18:38 +0000124#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
125#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
126
127#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
128#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
129#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130#define PyDateTime_DATE_GET_MICROSECOND(o) \
131 ((((PyDateTime_DateTime*)o)->data[7] << 16) | \
132 (((PyDateTime_DateTime*)o)->data[8] << 8) | \
133 ((PyDateTime_DateTime*)o)->data[9])
Alexander Belopolsky5d0c5982016-07-22 18:47:04 -0400134#define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
Zackery Spytz2e4dd332020-09-23 12:43:45 -0600135#define PyDateTime_DATE_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
136 ((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
Tim Peters2a799bf2002-12-16 20:18:38 +0000137
Tim Peters37f39822003-01-10 03:49:02 +0000138/* Apply for time instances. */
Tim Peters2a799bf2002-12-16 20:18:38 +0000139#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
140#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
141#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142#define PyDateTime_TIME_GET_MICROSECOND(o) \
143 ((((PyDateTime_Time*)o)->data[3] << 16) | \
144 (((PyDateTime_Time*)o)->data[4] << 8) | \
145 ((PyDateTime_Time*)o)->data[5])
Alexander Belopolsky5d0c5982016-07-22 18:47:04 -0400146#define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
Zackery Spytz2e4dd332020-09-23 12:43:45 -0600147#define PyDateTime_TIME_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
148 ((PyDateTime_Time *)(o))->tzinfo : Py_None)
Tim Peters2a799bf2002-12-16 20:18:38 +0000149
Amaury Forgeot d'Arc5e8260b2012-01-17 21:31:50 +0100150/* Apply for time delta instances */
151#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
152#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
153#define PyDateTime_DELTA_GET_MICROSECONDS(o) \
154 (((PyDateTime_Delta*)o)->microseconds)
155
Tim Peters9ddf40b2004-06-20 22:41:32 +0000156
157/* Define structure for C API. */
158typedef struct {
159 /* type objects */
160 PyTypeObject *DateType;
161 PyTypeObject *DateTimeType;
162 PyTypeObject *TimeType;
163 PyTypeObject *DeltaType;
164 PyTypeObject *TZInfoType;
165
Paul Ganssle04af5b12018-01-24 17:29:30 -0500166 /* singletons */
167 PyObject *TimeZone_UTC;
168
Tim Peters9ddf40b2004-06-20 22:41:32 +0000169 /* constructors */
170 PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
171 PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyObject*, PyTypeObject*);
Tim Peters9ddf40b2004-06-20 22:41:32 +0000173 PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
174 PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
Paul Ganssle04af5b12018-01-24 17:29:30 -0500175 PyObject *(*TimeZone_FromTimeZone)(PyObject *offset, PyObject *name);
Tim Peters9ddf40b2004-06-20 22:41:32 +0000176
177 /* constructors for the DB API */
178 PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
179 PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
180
Alexander Belopolsky5d0c5982016-07-22 18:47:04 -0400181 /* PEP 495 constructors */
182 PyObject *(*DateTime_FromDateAndTimeAndFold)(int, int, int, int, int, int, int,
183 PyObject*, int, PyTypeObject*);
184 PyObject *(*Time_FromTimeAndFold)(int, int, int, int, PyObject*, int, PyTypeObject*);
185
Tim Peters9ddf40b2004-06-20 22:41:32 +0000186} PyDateTime_CAPI;
187
Benjamin Petersonb173f782009-05-05 22:31:58 +0000188#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
Tim Peters9ddf40b2004-06-20 22:41:32 +0000189
Tim Peters9ddf40b2004-06-20 22:41:32 +0000190
Paul Ganssle0d126722018-11-13 03:02:25 -0500191/* This block is only used as part of the public API and should not be
192 * included in _datetimemodule.c, which does not use the C API capsule.
193 * See bpo-35081 for more details.
194 * */
195#ifndef _PY_DATETIME_IMPL
Tim Peters9ddf40b2004-06-20 22:41:32 +0000196/* Define global variable for the C API and a macro for setting it. */
Benjamin Peterson91d58bd2009-12-13 21:30:54 +0000197static PyDateTime_CAPI *PyDateTimeAPI = NULL;
Tim Peters9ddf40b2004-06-20 22:41:32 +0000198
199#define PyDateTime_IMPORT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000201
Paul Ganssle04af5b12018-01-24 17:29:30 -0500202/* Macro for access to the UTC singleton */
203#define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
204
Tim Peters9ddf40b2004-06-20 22:41:32 +0000205/* Macros for type checking when not building the Python core. */
206#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
Dong-hee Nad905df72020-02-14 02:37:17 +0900207#define PyDate_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000208
209#define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
Dong-hee Nad905df72020-02-14 02:37:17 +0900210#define PyDateTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateTimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000211
212#define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
Dong-hee Nad905df72020-02-14 02:37:17 +0900213#define PyTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000214
215#define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
Dong-hee Nad905df72020-02-14 02:37:17 +0900216#define PyDelta_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DeltaType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000217
218#define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
Dong-hee Nad905df72020-02-14 02:37:17 +0900219#define PyTZInfo_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TZInfoType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000220
Paul Ganssle0d126722018-11-13 03:02:25 -0500221
Tim Peters9ddf40b2004-06-20 22:41:32 +0000222/* Macros for accessing constructors in a simplified fashion. */
223#define PyDate_FromDate(year, month, day) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000225
226#define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
228 min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000229
Alexander Belopolsky5d0c5982016-07-22 18:47:04 -0400230#define PyDateTime_FromDateAndTimeAndFold(year, month, day, hour, min, sec, usec, fold) \
231 PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(year, month, day, hour, \
232 min, sec, usec, Py_None, fold, PyDateTimeAPI->DateTimeType)
233
Tim Peters9ddf40b2004-06-20 22:41:32 +0000234#define PyTime_FromTime(hour, minute, second, usecond) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
236 Py_None, PyDateTimeAPI->TimeType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000237
Alexander Belopolsky5d0c5982016-07-22 18:47:04 -0400238#define PyTime_FromTimeAndFold(hour, minute, second, usecond, fold) \
239 PyDateTimeAPI->Time_FromTimeAndFold(hour, minute, second, usecond, \
240 Py_None, fold, PyDateTimeAPI->TimeType)
241
Tim Peters9ddf40b2004-06-20 22:41:32 +0000242#define PyDelta_FromDSU(days, seconds, useconds) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
244 PyDateTimeAPI->DeltaType)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000245
Paul Ganssle04af5b12018-01-24 17:29:30 -0500246#define PyTimeZone_FromOffset(offset) \
247 PyDateTimeAPI->TimeZone_FromTimeZone(offset, NULL)
248
249#define PyTimeZone_FromOffsetAndName(offset, name) \
250 PyDateTimeAPI->TimeZone_FromTimeZone(offset, name)
251
Tim Peters9ddf40b2004-06-20 22:41:32 +0000252/* Macros supporting the DB API. */
253#define PyDateTime_FromTimestamp(args) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyDateTimeAPI->DateTime_FromTimestamp( \
255 (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000256
257#define PyDate_FromTimestamp(args) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 PyDateTimeAPI->Date_FromTimestamp( \
259 (PyObject*) (PyDateTimeAPI->DateType), args)
Tim Peters9ddf40b2004-06-20 22:41:32 +0000260
Paul Ganssle0d126722018-11-13 03:02:25 -0500261#endif /* !defined(_PY_DATETIME_IMPL) */
Tim Peters9ddf40b2004-06-20 22:41:32 +0000262
263#ifdef __cplusplus
264}
265#endif
Tim Peters2a799bf2002-12-16 20:18:38 +0000266#endif
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000267#endif /* !Py_LIMITED_API */