Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1 | /* datetime.h |
| 2 | */ |
| 3 | |
| 4 | #ifndef DATETIME_H |
| 5 | #define DATETIME_H |
| 6 | |
| 7 | /* Fields are packed into successive bytes, each viewed as unsigned and |
| 8 | * big-endian, unless otherwise noted: |
| 9 | * |
| 10 | * byte offset |
| 11 | * 0 year 2 bytes, 1-9999 |
| 12 | * 2 month 1 byte, 1-12 |
| 13 | * 3 day 1 byte, 1-31 |
| 14 | * 4 hour 1 byte, 0-23 |
| 15 | * 5 minute 1 byte, 0-59 |
| 16 | * 6 second 1 byte, 0-59 |
| 17 | * 7 usecond 3 bytes, 0-999999 |
| 18 | * 10 |
| 19 | */ |
| 20 | |
| 21 | /* # of bytes for year, month, and day. */ |
| 22 | #define _PyDateTime_DATE_DATASIZE 4 |
| 23 | |
| 24 | /* # of bytes for hour, minute, second, and usecond. */ |
| 25 | #define _PyDateTime_TIME_DATASIZE 6 |
| 26 | |
| 27 | /* # of bytes for year, month, day, hour, minute, second, and usecond. */ |
| 28 | #define _PyDateTime_DATETIME_DATASIZE 10 |
| 29 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 30 | |
| 31 | typedef struct |
| 32 | { |
| 33 | PyObject_HEAD |
| 34 | long hashcode; /* -1 when unknown */ |
| 35 | int days; /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */ |
| 36 | int seconds; /* 0 <= seconds < 24*3600 is invariant */ |
| 37 | int microseconds; /* 0 <= microseconds < 1000000 is invariant */ |
| 38 | } PyDateTime_Delta; |
| 39 | |
| 40 | typedef struct |
| 41 | { |
| 42 | PyObject_HEAD /* a pure abstract base clase */ |
| 43 | } PyDateTime_TZInfo; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 44 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 45 | |
| 46 | /* The datetime and time types have hashcodes, and an optional tzinfo member, |
| 47 | * present if and only if hastzinfo is true. |
| 48 | */ |
| 49 | #define _PyTZINFO_HEAD \ |
| 50 | PyObject_HEAD \ |
| 51 | long hashcode; \ |
| 52 | char hastzinfo; /* boolean flag */ |
| 53 | |
| 54 | /* No _PyDateTime_BaseTZInfo is allocated; it's just to have something |
| 55 | * convenient to cast to, when getting at the hastzinfo member of objects |
| 56 | * starting with _PyTZINFO_HEAD. |
| 57 | */ |
| 58 | typedef struct |
| 59 | { |
| 60 | _PyTZINFO_HEAD |
| 61 | } _PyDateTime_BaseTZInfo; |
| 62 | |
| 63 | /* All time objects are of PyDateTime_TimeType, but that can be allocated |
| 64 | * in two ways, with or without a tzinfo member. Without is the same as |
| 65 | * tzinfo == None, but consumes less memory. _PyDateTime_BaseTime is an |
| 66 | * internal struct used to allocate the right amount of space for the |
| 67 | * "without" case. |
| 68 | */ |
| 69 | #define _PyDateTime_TIMEHEAD \ |
| 70 | _PyTZINFO_HEAD \ |
| 71 | unsigned char data[_PyDateTime_TIME_DATASIZE]; |
| 72 | |
| 73 | typedef struct |
| 74 | { |
| 75 | _PyDateTime_TIMEHEAD |
| 76 | } _PyDateTime_BaseTime; /* hastzinfo false */ |
| 77 | |
| 78 | typedef struct |
| 79 | { |
| 80 | _PyDateTime_TIMEHEAD |
| 81 | PyObject *tzinfo; |
| 82 | } PyDateTime_Time; /* hastzinfo true */ |
| 83 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 84 | |
| 85 | /* All datetime objects are of PyDateTime_DateTimeType, but that can be |
| 86 | * allocated in two ways too, just like for time objects above. In addition, |
| 87 | * the plain date type is a base class for datetime, so it must also have |
| 88 | * a hastzinfo member (although it's unused there). |
| 89 | */ |
| 90 | typedef struct |
| 91 | { |
| 92 | _PyTZINFO_HEAD |
| 93 | unsigned char data[_PyDateTime_DATE_DATASIZE]; |
| 94 | } PyDateTime_Date; |
| 95 | |
| 96 | #define _PyDateTime_DATETIMEHEAD \ |
| 97 | _PyTZINFO_HEAD \ |
| 98 | unsigned char data[_PyDateTime_DATETIME_DATASIZE]; |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 99 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 100 | typedef struct |
| 101 | { |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 102 | _PyDateTime_DATETIMEHEAD |
| 103 | } _PyDateTime_BaseDateTime; /* hastzinfo false */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 104 | |
| 105 | typedef struct |
| 106 | { |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 107 | _PyDateTime_DATETIMEHEAD |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 108 | PyObject *tzinfo; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 109 | } PyDateTime_DateTime; /* hastzinfo true */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 110 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 111 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 112 | /* Apply for date and datetime instances. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 113 | #define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \ |
| 114 | ((PyDateTime_Date*)o)->data[1]) |
| 115 | #define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2]) |
| 116 | #define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3]) |
| 117 | |
| 118 | #define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4]) |
| 119 | #define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5]) |
| 120 | #define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6]) |
| 121 | #define PyDateTime_DATE_GET_MICROSECOND(o) \ |
| 122 | ((((PyDateTime_DateTime*)o)->data[7] << 16) | \ |
| 123 | (((PyDateTime_DateTime*)o)->data[8] << 8) | \ |
| 124 | ((PyDateTime_DateTime*)o)->data[9]) |
| 125 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 126 | /* Apply for time instances. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 127 | #define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0]) |
| 128 | #define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1]) |
| 129 | #define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2]) |
| 130 | #define PyDateTime_TIME_GET_MICROSECOND(o) \ |
| 131 | ((((PyDateTime_Time*)o)->data[3] << 16) | \ |
| 132 | (((PyDateTime_Time*)o)->data[4] << 8) | \ |
| 133 | ((PyDateTime_Time*)o)->data[5]) |
| 134 | |
| 135 | #define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType) |
| 136 | #define PyDate_CheckExact(op) ((op)->ob_type == &PyDateTime_DateType) |
| 137 | |
| 138 | #define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType) |
| 139 | #define PyDateTime_CheckExact(op) ((op)->ob_type == &PyDateTime_DateTimeType) |
| 140 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 141 | #define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType) |
| 142 | #define PyTime_CheckExact(op) ((op)->ob_type == &PyDateTime_TimeType) |
| 143 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 144 | #define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType) |
| 145 | #define PyDelta_CheckExact(op) ((op)->ob_type == &PyDateTime_DeltaType) |
| 146 | |
| 147 | #define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType) |
| 148 | #define PyTZInfo_CheckExact(op) ((op)->ob_type == &PyDateTime_TZInfoType) |
| 149 | |
| 150 | #endif |