Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1 | /* datetime.h |
| 2 | */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3 | #ifndef Py_LIMITED_API |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4 | #ifndef DATETIME_H |
| 5 | #define DATETIME_H |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 9 | |
| 10 | /* Fields are packed into successive bytes, each viewed as unsigned and |
| 11 | * big-endian, unless otherwise noted: |
| 12 | * |
| 13 | * byte offset |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | * 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 Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 21 | * 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 Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 33 | |
| 34 | typedef struct |
| 35 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | PyObject_HEAD |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 37 | Py_hash_t hashcode; /* -1 when unknown */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 41 | } PyDateTime_Delta; |
| 42 | |
| 43 | typedef struct |
| 44 | { |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame^] | 45 | PyObject_HEAD /* a pure abstract base class */ |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 46 | } PyDateTime_TZInfo; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 47 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 48 | |
| 49 | /* The datetime and time types have hashcodes, and an optional tzinfo member, |
| 50 | * present if and only if hastzinfo is true. |
| 51 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | #define _PyTZINFO_HEAD \ |
| 53 | PyObject_HEAD \ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 54 | Py_hash_t hashcode; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | char hastzinfo; /* boolean flag */ |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 56 | |
| 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 | */ |
| 61 | typedef struct |
| 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | _PyTZINFO_HEAD |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 64 | } _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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | #define _PyDateTime_TIMEHEAD \ |
| 73 | _PyTZINFO_HEAD \ |
| 74 | unsigned char data[_PyDateTime_TIME_DATASIZE]; |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 75 | |
| 76 | typedef struct |
| 77 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | _PyDateTime_TIMEHEAD |
| 79 | } _PyDateTime_BaseTime; /* hastzinfo false */ |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 80 | |
| 81 | typedef struct |
| 82 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | _PyDateTime_TIMEHEAD |
| 84 | PyObject *tzinfo; |
| 85 | } PyDateTime_Time; /* hastzinfo true */ |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 86 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 87 | |
| 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 | */ |
| 93 | typedef struct |
| 94 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | _PyTZINFO_HEAD |
| 96 | unsigned char data[_PyDateTime_DATE_DATASIZE]; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 97 | } PyDateTime_Date; |
| 98 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | #define _PyDateTime_DATETIMEHEAD \ |
| 100 | _PyTZINFO_HEAD \ |
| 101 | unsigned char data[_PyDateTime_DATETIME_DATASIZE]; |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 102 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 103 | typedef struct |
| 104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | _PyDateTime_DATETIMEHEAD |
| 106 | } _PyDateTime_BaseDateTime; /* hastzinfo false */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 107 | |
| 108 | typedef struct |
| 109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | _PyDateTime_DATETIMEHEAD |
| 111 | PyObject *tzinfo; |
| 112 | } PyDateTime_DateTime; /* hastzinfo true */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 113 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 114 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 115 | /* Apply for date and datetime instances. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 116 | #define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | ((PyDateTime_Date*)o)->data[1]) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 118 | #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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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]) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 128 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 129 | /* Apply for time instances. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 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]) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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]) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 137 | |
Amaury Forgeot d'Arc | 5e8260b | 2012-01-17 21:31:50 +0100 | [diff] [blame] | 138 | /* Apply for time delta instances */ |
| 139 | #define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days) |
| 140 | #define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds) |
| 141 | #define PyDateTime_DELTA_GET_MICROSECONDS(o) \ |
| 142 | (((PyDateTime_Delta*)o)->microseconds) |
| 143 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 144 | |
| 145 | /* Define structure for C API. */ |
| 146 | typedef struct { |
| 147 | /* type objects */ |
| 148 | PyTypeObject *DateType; |
| 149 | PyTypeObject *DateTimeType; |
| 150 | PyTypeObject *TimeType; |
| 151 | PyTypeObject *DeltaType; |
| 152 | PyTypeObject *TZInfoType; |
| 153 | |
| 154 | /* constructors */ |
| 155 | PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*); |
| 156 | PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | PyObject*, PyTypeObject*); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 158 | PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*); |
| 159 | PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*); |
| 160 | |
| 161 | /* constructors for the DB API */ |
| 162 | PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*); |
| 163 | PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*); |
| 164 | |
| 165 | } PyDateTime_CAPI; |
| 166 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 167 | #define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI" |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 168 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 169 | |
| 170 | #ifdef Py_BUILD_CORE |
| 171 | |
| 172 | /* Macros for type checking when building the Python core. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 173 | #define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 174 | #define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 175 | |
| 176 | #define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 177 | #define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 178 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 179 | #define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 180 | #define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 181 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 182 | #define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 183 | #define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 184 | |
| 185 | #define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 186 | #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 187 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 188 | #else |
| 189 | |
| 190 | /* Define global variable for the C API and a macro for setting it. */ |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 191 | static PyDateTime_CAPI *PyDateTimeAPI = NULL; |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 192 | |
| 193 | #define PyDateTime_IMPORT \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 195 | |
| 196 | /* Macros for type checking when not building the Python core. */ |
| 197 | #define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 198 | #define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 199 | |
| 200 | #define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 201 | #define PyDateTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateTimeType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 202 | |
| 203 | #define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 204 | #define PyTime_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TimeType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 205 | |
| 206 | #define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 207 | #define PyDelta_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DeltaType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 208 | |
| 209 | #define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 210 | #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 211 | |
| 212 | /* Macros for accessing constructors in a simplified fashion. */ |
| 213 | #define PyDate_FromDate(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 215 | |
| 216 | #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \ |
| 218 | min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 219 | |
| 220 | #define PyTime_FromTime(hour, minute, second, usecond) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \ |
| 222 | Py_None, PyDateTimeAPI->TimeType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 223 | |
| 224 | #define PyDelta_FromDSU(days, seconds, useconds) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \ |
| 226 | PyDateTimeAPI->DeltaType) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 227 | |
| 228 | /* Macros supporting the DB API. */ |
| 229 | #define PyDateTime_FromTimestamp(args) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | PyDateTimeAPI->DateTime_FromTimestamp( \ |
| 231 | (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 232 | |
| 233 | #define PyDate_FromTimestamp(args) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | PyDateTimeAPI->Date_FromTimestamp( \ |
| 235 | (PyObject*) (PyDateTimeAPI->DateType), args) |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | #endif /* Py_BUILD_CORE */ |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 238 | |
| 239 | #ifdef __cplusplus |
| 240 | } |
| 241 | #endif |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 242 | #endif |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 243 | #endif /* !Py_LIMITED_API */ |