Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:49

0001 /*  datetime.h
0002  */
0003 #ifndef Py_LIMITED_API
0004 #ifndef DATETIME_H
0005 #define DATETIME_H
0006 #ifdef __cplusplus
0007 extern "C" {
0008 #endif
0009 
0010 /* Fields are packed into successive bytes, each viewed as unsigned and
0011  * big-endian, unless otherwise noted:
0012  *
0013  * byte offset
0014  *  0           year     2 bytes, 1-9999
0015  *  2           month    1 byte, 1-12
0016  *  3           day      1 byte, 1-31
0017  *  4           hour     1 byte, 0-23
0018  *  5           minute   1 byte, 0-59
0019  *  6           second   1 byte, 0-59
0020  *  7           usecond  3 bytes, 0-999999
0021  * 10
0022  */
0023 
0024 /* # of bytes for year, month, and day. */
0025 #define _PyDateTime_DATE_DATASIZE 4
0026 
0027 /* # of bytes for hour, minute, second, and usecond. */
0028 #define _PyDateTime_TIME_DATASIZE 6
0029 
0030 /* # of bytes for year, month, day, hour, minute, second, and usecond. */
0031 #define _PyDateTime_DATETIME_DATASIZE 10
0032 
0033 
0034 typedef struct
0035 {
0036     PyObject_HEAD
0037     Py_hash_t hashcode;         /* -1 when unknown */
0038     int days;                   /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */
0039     int seconds;                /* 0 <= seconds < 24*3600 is invariant */
0040     int microseconds;           /* 0 <= microseconds < 1000000 is invariant */
0041 } PyDateTime_Delta;
0042 
0043 typedef struct
0044 {
0045     PyObject_HEAD               /* a pure abstract base class */
0046 } PyDateTime_TZInfo;
0047 
0048 
0049 /* The datetime and time types have hashcodes, and an optional tzinfo member,
0050  * present if and only if hastzinfo is true.
0051  */
0052 #define _PyTZINFO_HEAD          \
0053     PyObject_HEAD               \
0054     Py_hash_t hashcode;         \
0055     char hastzinfo;             /* boolean flag */
0056 
0057 /* No _PyDateTime_BaseTZInfo is allocated; it's just to have something
0058  * convenient to cast to, when getting at the hastzinfo member of objects
0059  * starting with _PyTZINFO_HEAD.
0060  */
0061 typedef struct
0062 {
0063     _PyTZINFO_HEAD
0064 } _PyDateTime_BaseTZInfo;
0065 
0066 /* All time objects are of PyDateTime_TimeType, but that can be allocated
0067  * in two ways, with or without a tzinfo member.  Without is the same as
0068  * tzinfo == None, but consumes less memory.  _PyDateTime_BaseTime is an
0069  * internal struct used to allocate the right amount of space for the
0070  * "without" case.
0071  */
0072 #define _PyDateTime_TIMEHEAD    \
0073     _PyTZINFO_HEAD              \
0074     unsigned char data[_PyDateTime_TIME_DATASIZE];
0075 
0076 typedef struct
0077 {
0078     _PyDateTime_TIMEHEAD
0079 } _PyDateTime_BaseTime;         /* hastzinfo false */
0080 
0081 typedef struct
0082 {
0083     _PyDateTime_TIMEHEAD
0084     unsigned char fold;
0085     PyObject *tzinfo;
0086 } PyDateTime_Time;              /* hastzinfo true */
0087 
0088 
0089 /* All datetime objects are of PyDateTime_DateTimeType, but that can be
0090  * allocated in two ways too, just like for time objects above.  In addition,
0091  * the plain date type is a base class for datetime, so it must also have
0092  * a hastzinfo member (although it's unused there).
0093  */
0094 typedef struct
0095 {
0096     _PyTZINFO_HEAD
0097     unsigned char data[_PyDateTime_DATE_DATASIZE];
0098 } PyDateTime_Date;
0099 
0100 #define _PyDateTime_DATETIMEHEAD        \
0101     _PyTZINFO_HEAD                      \
0102     unsigned char data[_PyDateTime_DATETIME_DATASIZE];
0103 
0104 typedef struct
0105 {
0106     _PyDateTime_DATETIMEHEAD
0107 } _PyDateTime_BaseDateTime;     /* hastzinfo false */
0108 
0109 typedef struct
0110 {
0111     _PyDateTime_DATETIMEHEAD
0112     unsigned char fold;
0113     PyObject *tzinfo;
0114 } PyDateTime_DateTime;          /* hastzinfo true */
0115 
0116 
0117 /* Apply for date and datetime instances. */
0118 
0119 // o is a pointer to a time or a datetime object.
0120 #define _PyDateTime_HAS_TZINFO(o)  (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
0121 
0122 #define PyDateTime_GET_YEAR(o)     ((((PyDateTime_Date*)(o))->data[0] << 8) | \
0123                      ((PyDateTime_Date*)(o))->data[1])
0124 #define PyDateTime_GET_MONTH(o)    (((PyDateTime_Date*)(o))->data[2])
0125 #define PyDateTime_GET_DAY(o)      (((PyDateTime_Date*)(o))->data[3])
0126 
0127 #define PyDateTime_DATE_GET_HOUR(o)        (((PyDateTime_DateTime*)(o))->data[4])
0128 #define PyDateTime_DATE_GET_MINUTE(o)      (((PyDateTime_DateTime*)(o))->data[5])
0129 #define PyDateTime_DATE_GET_SECOND(o)      (((PyDateTime_DateTime*)(o))->data[6])
0130 #define PyDateTime_DATE_GET_MICROSECOND(o)              \
0131     ((((PyDateTime_DateTime*)(o))->data[7] << 16) |       \
0132      (((PyDateTime_DateTime*)(o))->data[8] << 8)  |       \
0133       ((PyDateTime_DateTime*)(o))->data[9])
0134 #define PyDateTime_DATE_GET_FOLD(o)        (((PyDateTime_DateTime*)(o))->fold)
0135 #define PyDateTime_DATE_GET_TZINFO(o)      (_PyDateTime_HAS_TZINFO((o)) ? \
0136     ((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
0137 
0138 /* Apply for time instances. */
0139 #define PyDateTime_TIME_GET_HOUR(o)        (((PyDateTime_Time*)(o))->data[0])
0140 #define PyDateTime_TIME_GET_MINUTE(o)      (((PyDateTime_Time*)(o))->data[1])
0141 #define PyDateTime_TIME_GET_SECOND(o)      (((PyDateTime_Time*)(o))->data[2])
0142 #define PyDateTime_TIME_GET_MICROSECOND(o)              \
0143     ((((PyDateTime_Time*)(o))->data[3] << 16) |           \
0144      (((PyDateTime_Time*)(o))->data[4] << 8)  |           \
0145       ((PyDateTime_Time*)(o))->data[5])
0146 #define PyDateTime_TIME_GET_FOLD(o)        (((PyDateTime_Time*)(o))->fold)
0147 #define PyDateTime_TIME_GET_TZINFO(o)      (_PyDateTime_HAS_TZINFO(o) ? \
0148     ((PyDateTime_Time *)(o))->tzinfo : Py_None)
0149 
0150 /* Apply for time delta instances */
0151 #define PyDateTime_DELTA_GET_DAYS(o)         (((PyDateTime_Delta*)(o))->days)
0152 #define PyDateTime_DELTA_GET_SECONDS(o)      (((PyDateTime_Delta*)(o))->seconds)
0153 #define PyDateTime_DELTA_GET_MICROSECONDS(o)            \
0154     (((PyDateTime_Delta*)(o))->microseconds)
0155 
0156 
0157 /* Define structure for C API. */
0158 typedef struct {
0159     /* type objects */
0160     PyTypeObject *DateType;
0161     PyTypeObject *DateTimeType;
0162     PyTypeObject *TimeType;
0163     PyTypeObject *DeltaType;
0164     PyTypeObject *TZInfoType;
0165 
0166     /* singletons */
0167     PyObject *TimeZone_UTC;
0168 
0169     /* constructors */
0170     PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
0171     PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
0172         PyObject*, PyTypeObject*);
0173     PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
0174     PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
0175     PyObject *(*TimeZone_FromTimeZone)(PyObject *offset, PyObject *name);
0176 
0177     /* constructors for the DB API */
0178     PyObject *(*DateTime_FromTimestamp)(PyObject*, PyObject*, PyObject*);
0179     PyObject *(*Date_FromTimestamp)(PyObject*, PyObject*);
0180 
0181     /* PEP 495 constructors */
0182     PyObject *(*DateTime_FromDateAndTimeAndFold)(int, int, int, int, int, int, int,
0183         PyObject*, int, PyTypeObject*);
0184     PyObject *(*Time_FromTimeAndFold)(int, int, int, int, PyObject*, int, PyTypeObject*);
0185 
0186 } PyDateTime_CAPI;
0187 
0188 #define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
0189 
0190 
0191 /* This block is only used as part of the public API and should not be
0192  * included in _datetimemodule.c, which does not use the C API capsule.
0193  * See bpo-35081 for more details.
0194  * */
0195 #ifndef _PY_DATETIME_IMPL
0196 /* Define global variable for the C API and a macro for setting it. */
0197 static PyDateTime_CAPI *PyDateTimeAPI = NULL;
0198 
0199 #define PyDateTime_IMPORT \
0200     PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
0201 
0202 /* Macro for access to the UTC singleton */
0203 #define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
0204 
0205 /* Macros for type checking when not building the Python core. */
0206 #define PyDate_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DateType)
0207 #define PyDate_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DateType)
0208 
0209 #define PyDateTime_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DateTimeType)
0210 #define PyDateTime_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DateTimeType)
0211 
0212 #define PyTime_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->TimeType)
0213 #define PyTime_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->TimeType)
0214 
0215 #define PyDelta_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DeltaType)
0216 #define PyDelta_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DeltaType)
0217 
0218 #define PyTZInfo_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->TZInfoType)
0219 #define PyTZInfo_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->TZInfoType)
0220 
0221 
0222 /* Macros for accessing constructors in a simplified fashion. */
0223 #define PyDate_FromDate(year, month, day) \
0224     PyDateTimeAPI->Date_FromDate((year), (month), (day), PyDateTimeAPI->DateType)
0225 
0226 #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
0227     PyDateTimeAPI->DateTime_FromDateAndTime((year), (month), (day), (hour), \
0228         (min), (sec), (usec), Py_None, PyDateTimeAPI->DateTimeType)
0229 
0230 #define PyDateTime_FromDateAndTimeAndFold(year, month, day, hour, min, sec, usec, fold) \
0231     PyDateTimeAPI->DateTime_FromDateAndTimeAndFold((year), (month), (day), (hour), \
0232         (min), (sec), (usec), Py_None, (fold), PyDateTimeAPI->DateTimeType)
0233 
0234 #define PyTime_FromTime(hour, minute, second, usecond) \
0235     PyDateTimeAPI->Time_FromTime((hour), (minute), (second), (usecond), \
0236         Py_None, PyDateTimeAPI->TimeType)
0237 
0238 #define PyTime_FromTimeAndFold(hour, minute, second, usecond, fold) \
0239     PyDateTimeAPI->Time_FromTimeAndFold((hour), (minute), (second), (usecond), \
0240         Py_None, (fold), PyDateTimeAPI->TimeType)
0241 
0242 #define PyDelta_FromDSU(days, seconds, useconds) \
0243     PyDateTimeAPI->Delta_FromDelta((days), (seconds), (useconds), 1, \
0244         PyDateTimeAPI->DeltaType)
0245 
0246 #define PyTimeZone_FromOffset(offset) \
0247     PyDateTimeAPI->TimeZone_FromTimeZone((offset), NULL)
0248 
0249 #define PyTimeZone_FromOffsetAndName(offset, name) \
0250     PyDateTimeAPI->TimeZone_FromTimeZone((offset), (name))
0251 
0252 /* Macros supporting the DB API. */
0253 #define PyDateTime_FromTimestamp(args) \
0254     PyDateTimeAPI->DateTime_FromTimestamp( \
0255         (PyObject*) (PyDateTimeAPI->DateTimeType), (args), NULL)
0256 
0257 #define PyDate_FromTimestamp(args) \
0258     PyDateTimeAPI->Date_FromTimestamp( \
0259         (PyObject*) (PyDateTimeAPI->DateType), (args))
0260 
0261 #endif   /* !defined(_PY_DATETIME_IMPL) */
0262 
0263 #ifdef __cplusplus
0264 }
0265 #endif
0266 #endif
0267 #endif /* !Py_LIMITED_API */