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