| 1 | n/a | /* C implementation for the date/time type documented at |
|---|
| 2 | n/a | * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage |
|---|
| 3 | n/a | */ |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | #include "Python.h" |
|---|
| 6 | n/a | #include "structmember.h" |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | #include <time.h> |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | #ifdef MS_WINDOWS |
|---|
| 11 | n/a | # include <winsock2.h> /* struct timeval */ |
|---|
| 12 | n/a | #endif |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | /* Differentiate between building the core module and building extension |
|---|
| 15 | n/a | * modules. |
|---|
| 16 | n/a | */ |
|---|
| 17 | n/a | #ifndef Py_BUILD_CORE |
|---|
| 18 | n/a | #define Py_BUILD_CORE |
|---|
| 19 | n/a | #endif |
|---|
| 20 | n/a | #include "datetime.h" |
|---|
| 21 | n/a | #undef Py_BUILD_CORE |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | /*[clinic input] |
|---|
| 24 | n/a | module datetime |
|---|
| 25 | n/a | class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" |
|---|
| 26 | n/a | [clinic start generated code]*/ |
|---|
| 27 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78142cb64b9e98bc]*/ |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | #include "clinic/_datetimemodule.c.h" |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | /* We require that C int be at least 32 bits, and use int virtually |
|---|
| 32 | n/a | * everywhere. In just a few cases we use a temp long, where a Python |
|---|
| 33 | n/a | * API returns a C long. In such cases, we have to ensure that the |
|---|
| 34 | n/a | * final result fits in a C int (this can be an issue on 64-bit boxes). |
|---|
| 35 | n/a | */ |
|---|
| 36 | n/a | #if SIZEOF_INT < 4 |
|---|
| 37 | n/a | # error "_datetime.c requires that C int have at least 32 bits" |
|---|
| 38 | n/a | #endif |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | #define MINYEAR 1 |
|---|
| 41 | n/a | #define MAXYEAR 9999 |
|---|
| 42 | n/a | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | /* Nine decimal digits is easy to communicate, and leaves enough room |
|---|
| 45 | n/a | * so that two delta days can be added w/o fear of overflowing a signed |
|---|
| 46 | n/a | * 32-bit int, and with plenty of room left over to absorb any possible |
|---|
| 47 | n/a | * carries from adding seconds. |
|---|
| 48 | n/a | */ |
|---|
| 49 | n/a | #define MAX_DELTA_DAYS 999999999 |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | /* Rename the long macros in datetime.h to more reasonable short names. */ |
|---|
| 52 | n/a | #define GET_YEAR PyDateTime_GET_YEAR |
|---|
| 53 | n/a | #define GET_MONTH PyDateTime_GET_MONTH |
|---|
| 54 | n/a | #define GET_DAY PyDateTime_GET_DAY |
|---|
| 55 | n/a | #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
|---|
| 56 | n/a | #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
|---|
| 57 | n/a | #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
|---|
| 58 | n/a | #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
|---|
| 59 | n/a | #define DATE_GET_FOLD PyDateTime_DATE_GET_FOLD |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | /* Date accessors for date and datetime. */ |
|---|
| 62 | n/a | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
|---|
| 63 | n/a | ((o)->data[1] = ((v) & 0x00ff))) |
|---|
| 64 | n/a | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
|---|
| 65 | n/a | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | /* Date/Time accessors for datetime. */ |
|---|
| 68 | n/a | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
|---|
| 69 | n/a | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
|---|
| 70 | n/a | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
|---|
| 71 | n/a | #define DATE_SET_MICROSECOND(o, v) \ |
|---|
| 72 | n/a | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
|---|
| 73 | n/a | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
|---|
| 74 | n/a | ((o)->data[9] = ((v) & 0x0000ff))) |
|---|
| 75 | n/a | #define DATE_SET_FOLD(o, v) (PyDateTime_DATE_GET_FOLD(o) = (v)) |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | /* Time accessors for time. */ |
|---|
| 78 | n/a | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
|---|
| 79 | n/a | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
|---|
| 80 | n/a | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
|---|
| 81 | n/a | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
|---|
| 82 | n/a | #define TIME_GET_FOLD PyDateTime_TIME_GET_FOLD |
|---|
| 83 | n/a | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
|---|
| 84 | n/a | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
|---|
| 85 | n/a | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
|---|
| 86 | n/a | #define TIME_SET_MICROSECOND(o, v) \ |
|---|
| 87 | n/a | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
|---|
| 88 | n/a | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
|---|
| 89 | n/a | ((o)->data[5] = ((v) & 0x0000ff))) |
|---|
| 90 | n/a | #define TIME_SET_FOLD(o, v) (PyDateTime_TIME_GET_FOLD(o) = (v)) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | /* Delta accessors for timedelta. */ |
|---|
| 93 | n/a | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
|---|
| 94 | n/a | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
|---|
| 95 | n/a | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
|---|
| 98 | n/a | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
|---|
| 99 | n/a | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
|---|
| 102 | n/a | * p->hastzinfo. |
|---|
| 103 | n/a | */ |
|---|
| 104 | n/a | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
|---|
| 105 | n/a | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
|---|
| 106 | n/a | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
|---|
| 107 | n/a | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
|---|
| 108 | n/a | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
|---|
| 109 | n/a | /* M is a char or int claiming to be a valid month. The macro is equivalent |
|---|
| 110 | n/a | * to the two-sided Python test |
|---|
| 111 | n/a | * 1 <= M <= 12 |
|---|
| 112 | n/a | */ |
|---|
| 113 | n/a | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | /* Forward declarations. */ |
|---|
| 116 | n/a | static PyTypeObject PyDateTime_DateType; |
|---|
| 117 | n/a | static PyTypeObject PyDateTime_DateTimeType; |
|---|
| 118 | n/a | static PyTypeObject PyDateTime_DeltaType; |
|---|
| 119 | n/a | static PyTypeObject PyDateTime_TimeType; |
|---|
| 120 | n/a | static PyTypeObject PyDateTime_TZInfoType; |
|---|
| 121 | n/a | static PyTypeObject PyDateTime_TimeZoneType; |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | static int check_tzinfo_subclass(PyObject *p); |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | _Py_IDENTIFIER(as_integer_ratio); |
|---|
| 126 | n/a | _Py_IDENTIFIER(fromutc); |
|---|
| 127 | n/a | _Py_IDENTIFIER(isoformat); |
|---|
| 128 | n/a | _Py_IDENTIFIER(strftime); |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 131 | n/a | * Math utilities. |
|---|
| 132 | n/a | */ |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | /* k = i+j overflows iff k differs in sign from both inputs, |
|---|
| 135 | n/a | * iff k^i has sign bit set and k^j has sign bit set, |
|---|
| 136 | n/a | * iff (k^i)&(k^j) has sign bit set. |
|---|
| 137 | n/a | */ |
|---|
| 138 | n/a | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
|---|
| 139 | n/a | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | /* Compute Python divmod(x, y), returning the quotient and storing the |
|---|
| 142 | n/a | * remainder into *r. The quotient is the floor of x/y, and that's |
|---|
| 143 | n/a | * the real point of this. C will probably truncate instead (C99 |
|---|
| 144 | n/a | * requires truncation; C89 left it implementation-defined). |
|---|
| 145 | n/a | * Simplification: we *require* that y > 0 here. That's appropriate |
|---|
| 146 | n/a | * for all the uses made of it. This simplifies the code and makes |
|---|
| 147 | n/a | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
|---|
| 148 | n/a | * overflow case). |
|---|
| 149 | n/a | */ |
|---|
| 150 | n/a | static int |
|---|
| 151 | n/a | divmod(int x, int y, int *r) |
|---|
| 152 | n/a | { |
|---|
| 153 | n/a | int quo; |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | assert(y > 0); |
|---|
| 156 | n/a | quo = x / y; |
|---|
| 157 | n/a | *r = x - quo * y; |
|---|
| 158 | n/a | if (*r < 0) { |
|---|
| 159 | n/a | --quo; |
|---|
| 160 | n/a | *r += y; |
|---|
| 161 | n/a | } |
|---|
| 162 | n/a | assert(0 <= *r && *r < y); |
|---|
| 163 | n/a | return quo; |
|---|
| 164 | n/a | } |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | /* Nearest integer to m / n for integers m and n. Half-integer results |
|---|
| 167 | n/a | * are rounded to even. |
|---|
| 168 | n/a | */ |
|---|
| 169 | n/a | static PyObject * |
|---|
| 170 | n/a | divide_nearest(PyObject *m, PyObject *n) |
|---|
| 171 | n/a | { |
|---|
| 172 | n/a | PyObject *result; |
|---|
| 173 | n/a | PyObject *temp; |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | temp = _PyLong_DivmodNear(m, n); |
|---|
| 176 | n/a | if (temp == NULL) |
|---|
| 177 | n/a | return NULL; |
|---|
| 178 | n/a | result = PyTuple_GET_ITEM(temp, 0); |
|---|
| 179 | n/a | Py_INCREF(result); |
|---|
| 180 | n/a | Py_DECREF(temp); |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | return result; |
|---|
| 183 | n/a | } |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 186 | n/a | * General calendrical helper functions |
|---|
| 187 | n/a | */ |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | /* For each month ordinal in 1..12, the number of days in that month, |
|---|
| 190 | n/a | * and the number of days before that month in the same year. These |
|---|
| 191 | n/a | * are correct for non-leap years only. |
|---|
| 192 | n/a | */ |
|---|
| 193 | n/a | static const int _days_in_month[] = { |
|---|
| 194 | n/a | 0, /* unused; this vector uses 1-based indexing */ |
|---|
| 195 | n/a | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
|---|
| 196 | n/a | }; |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | static const int _days_before_month[] = { |
|---|
| 199 | n/a | 0, /* unused; this vector uses 1-based indexing */ |
|---|
| 200 | n/a | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
|---|
| 201 | n/a | }; |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | /* year -> 1 if leap year, else 0. */ |
|---|
| 204 | n/a | static int |
|---|
| 205 | n/a | is_leap(int year) |
|---|
| 206 | n/a | { |
|---|
| 207 | n/a | /* Cast year to unsigned. The result is the same either way, but |
|---|
| 208 | n/a | * C can generate faster code for unsigned mod than for signed |
|---|
| 209 | n/a | * mod (especially for % 4 -- a good compiler should just grab |
|---|
| 210 | n/a | * the last 2 bits when the LHS is unsigned). |
|---|
| 211 | n/a | */ |
|---|
| 212 | n/a | const unsigned int ayear = (unsigned int)year; |
|---|
| 213 | n/a | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
|---|
| 214 | n/a | } |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | /* year, month -> number of days in that month in that year */ |
|---|
| 217 | n/a | static int |
|---|
| 218 | n/a | days_in_month(int year, int month) |
|---|
| 219 | n/a | { |
|---|
| 220 | n/a | assert(month >= 1); |
|---|
| 221 | n/a | assert(month <= 12); |
|---|
| 222 | n/a | if (month == 2 && is_leap(year)) |
|---|
| 223 | n/a | return 29; |
|---|
| 224 | n/a | else |
|---|
| 225 | n/a | return _days_in_month[month]; |
|---|
| 226 | n/a | } |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | /* year, month -> number of days in year preceding first day of month */ |
|---|
| 229 | n/a | static int |
|---|
| 230 | n/a | days_before_month(int year, int month) |
|---|
| 231 | n/a | { |
|---|
| 232 | n/a | int days; |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | assert(month >= 1); |
|---|
| 235 | n/a | assert(month <= 12); |
|---|
| 236 | n/a | days = _days_before_month[month]; |
|---|
| 237 | n/a | if (month > 2 && is_leap(year)) |
|---|
| 238 | n/a | ++days; |
|---|
| 239 | n/a | return days; |
|---|
| 240 | n/a | } |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | /* year -> number of days before January 1st of year. Remember that we |
|---|
| 243 | n/a | * start with year 1, so days_before_year(1) == 0. |
|---|
| 244 | n/a | */ |
|---|
| 245 | n/a | static int |
|---|
| 246 | n/a | days_before_year(int year) |
|---|
| 247 | n/a | { |
|---|
| 248 | n/a | int y = year - 1; |
|---|
| 249 | n/a | /* This is incorrect if year <= 0; we really want the floor |
|---|
| 250 | n/a | * here. But so long as MINYEAR is 1, the smallest year this |
|---|
| 251 | n/a | * can see is 1. |
|---|
| 252 | n/a | */ |
|---|
| 253 | n/a | assert (year >= 1); |
|---|
| 254 | n/a | return y*365 + y/4 - y/100 + y/400; |
|---|
| 255 | n/a | } |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | /* Number of days in 4, 100, and 400 year cycles. That these have |
|---|
| 258 | n/a | * the correct values is asserted in the module init function. |
|---|
| 259 | n/a | */ |
|---|
| 260 | n/a | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
|---|
| 261 | n/a | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
|---|
| 262 | n/a | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
|---|
| 265 | n/a | static void |
|---|
| 266 | n/a | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
|---|
| 267 | n/a | { |
|---|
| 268 | n/a | int n, n1, n4, n100, n400, leapyear, preceding; |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
|---|
| 271 | n/a | * leap years repeats exactly every 400 years. The basic strategy is |
|---|
| 272 | n/a | * to find the closest 400-year boundary at or before ordinal, then |
|---|
| 273 | n/a | * work with the offset from that boundary to ordinal. Life is much |
|---|
| 274 | n/a | * clearer if we subtract 1 from ordinal first -- then the values |
|---|
| 275 | n/a | * of ordinal at 400-year boundaries are exactly those divisible |
|---|
| 276 | n/a | * by DI400Y: |
|---|
| 277 | n/a | * |
|---|
| 278 | n/a | * D M Y n n-1 |
|---|
| 279 | n/a | * -- --- ---- ---------- ---------------- |
|---|
| 280 | n/a | * 31 Dec -400 -DI400Y -DI400Y -1 |
|---|
| 281 | n/a | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
|---|
| 282 | n/a | * ... |
|---|
| 283 | n/a | * 30 Dec 000 -1 -2 |
|---|
| 284 | n/a | * 31 Dec 000 0 -1 |
|---|
| 285 | n/a | * 1 Jan 001 1 0 400-year boundary |
|---|
| 286 | n/a | * 2 Jan 001 2 1 |
|---|
| 287 | n/a | * 3 Jan 001 3 2 |
|---|
| 288 | n/a | * ... |
|---|
| 289 | n/a | * 31 Dec 400 DI400Y DI400Y -1 |
|---|
| 290 | n/a | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
|---|
| 291 | n/a | */ |
|---|
| 292 | n/a | assert(ordinal >= 1); |
|---|
| 293 | n/a | --ordinal; |
|---|
| 294 | n/a | n400 = ordinal / DI400Y; |
|---|
| 295 | n/a | n = ordinal % DI400Y; |
|---|
| 296 | n/a | *year = n400 * 400 + 1; |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | /* Now n is the (non-negative) offset, in days, from January 1 of |
|---|
| 299 | n/a | * year, to the desired date. Now compute how many 100-year cycles |
|---|
| 300 | n/a | * precede n. |
|---|
| 301 | n/a | * Note that it's possible for n100 to equal 4! In that case 4 full |
|---|
| 302 | n/a | * 100-year cycles precede the desired day, which implies the |
|---|
| 303 | n/a | * desired day is December 31 at the end of a 400-year cycle. |
|---|
| 304 | n/a | */ |
|---|
| 305 | n/a | n100 = n / DI100Y; |
|---|
| 306 | n/a | n = n % DI100Y; |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | /* Now compute how many 4-year cycles precede it. */ |
|---|
| 309 | n/a | n4 = n / DI4Y; |
|---|
| 310 | n/a | n = n % DI4Y; |
|---|
| 311 | n/a | |
|---|
| 312 | n/a | /* And now how many single years. Again n1 can be 4, and again |
|---|
| 313 | n/a | * meaning that the desired day is December 31 at the end of the |
|---|
| 314 | n/a | * 4-year cycle. |
|---|
| 315 | n/a | */ |
|---|
| 316 | n/a | n1 = n / 365; |
|---|
| 317 | n/a | n = n % 365; |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | *year += n100 * 100 + n4 * 4 + n1; |
|---|
| 320 | n/a | if (n1 == 4 || n100 == 4) { |
|---|
| 321 | n/a | assert(n == 0); |
|---|
| 322 | n/a | *year -= 1; |
|---|
| 323 | n/a | *month = 12; |
|---|
| 324 | n/a | *day = 31; |
|---|
| 325 | n/a | return; |
|---|
| 326 | n/a | } |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | /* Now the year is correct, and n is the offset from January 1. We |
|---|
| 329 | n/a | * find the month via an estimate that's either exact or one too |
|---|
| 330 | n/a | * large. |
|---|
| 331 | n/a | */ |
|---|
| 332 | n/a | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
|---|
| 333 | n/a | assert(leapyear == is_leap(*year)); |
|---|
| 334 | n/a | *month = (n + 50) >> 5; |
|---|
| 335 | n/a | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
|---|
| 336 | n/a | if (preceding > n) { |
|---|
| 337 | n/a | /* estimate is too large */ |
|---|
| 338 | n/a | *month -= 1; |
|---|
| 339 | n/a | preceding -= days_in_month(*year, *month); |
|---|
| 340 | n/a | } |
|---|
| 341 | n/a | n -= preceding; |
|---|
| 342 | n/a | assert(0 <= n); |
|---|
| 343 | n/a | assert(n < days_in_month(*year, *month)); |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | *day = n + 1; |
|---|
| 346 | n/a | } |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
|---|
| 349 | n/a | static int |
|---|
| 350 | n/a | ymd_to_ord(int year, int month, int day) |
|---|
| 351 | n/a | { |
|---|
| 352 | n/a | return days_before_year(year) + days_before_month(year, month) + day; |
|---|
| 353 | n/a | } |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
|---|
| 356 | n/a | static int |
|---|
| 357 | n/a | weekday(int year, int month, int day) |
|---|
| 358 | n/a | { |
|---|
| 359 | n/a | return (ymd_to_ord(year, month, day) + 6) % 7; |
|---|
| 360 | n/a | } |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
|---|
| 363 | n/a | * first calendar week containing a Thursday. |
|---|
| 364 | n/a | */ |
|---|
| 365 | n/a | static int |
|---|
| 366 | n/a | iso_week1_monday(int year) |
|---|
| 367 | n/a | { |
|---|
| 368 | n/a | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
|---|
| 369 | n/a | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
|---|
| 370 | n/a | int first_weekday = (first_day + 6) % 7; |
|---|
| 371 | n/a | /* ordinal of closest Monday at or before 1/1 */ |
|---|
| 372 | n/a | int week1_monday = first_day - first_weekday; |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
|---|
| 375 | n/a | week1_monday += 7; |
|---|
| 376 | n/a | return week1_monday; |
|---|
| 377 | n/a | } |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 380 | n/a | * Range checkers. |
|---|
| 381 | n/a | */ |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
|---|
| 384 | n/a | * If not, raise OverflowError and return -1. |
|---|
| 385 | n/a | */ |
|---|
| 386 | n/a | static int |
|---|
| 387 | n/a | check_delta_day_range(int days) |
|---|
| 388 | n/a | { |
|---|
| 389 | n/a | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
|---|
| 390 | n/a | return 0; |
|---|
| 391 | n/a | PyErr_Format(PyExc_OverflowError, |
|---|
| 392 | n/a | "days=%d; must have magnitude <= %d", |
|---|
| 393 | n/a | days, MAX_DELTA_DAYS); |
|---|
| 394 | n/a | return -1; |
|---|
| 395 | n/a | } |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | /* Check that date arguments are in range. Return 0 if they are. If they |
|---|
| 398 | n/a | * aren't, raise ValueError and return -1. |
|---|
| 399 | n/a | */ |
|---|
| 400 | n/a | static int |
|---|
| 401 | n/a | check_date_args(int year, int month, int day) |
|---|
| 402 | n/a | { |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | if (year < MINYEAR || year > MAXYEAR) { |
|---|
| 405 | n/a | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
|---|
| 406 | n/a | return -1; |
|---|
| 407 | n/a | } |
|---|
| 408 | n/a | if (month < 1 || month > 12) { |
|---|
| 409 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 410 | n/a | "month must be in 1..12"); |
|---|
| 411 | n/a | return -1; |
|---|
| 412 | n/a | } |
|---|
| 413 | n/a | if (day < 1 || day > days_in_month(year, month)) { |
|---|
| 414 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 415 | n/a | "day is out of range for month"); |
|---|
| 416 | n/a | return -1; |
|---|
| 417 | n/a | } |
|---|
| 418 | n/a | return 0; |
|---|
| 419 | n/a | } |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | /* Check that time arguments are in range. Return 0 if they are. If they |
|---|
| 422 | n/a | * aren't, raise ValueError and return -1. |
|---|
| 423 | n/a | */ |
|---|
| 424 | n/a | static int |
|---|
| 425 | n/a | check_time_args(int h, int m, int s, int us, int fold) |
|---|
| 426 | n/a | { |
|---|
| 427 | n/a | if (h < 0 || h > 23) { |
|---|
| 428 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 429 | n/a | "hour must be in 0..23"); |
|---|
| 430 | n/a | return -1; |
|---|
| 431 | n/a | } |
|---|
| 432 | n/a | if (m < 0 || m > 59) { |
|---|
| 433 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 434 | n/a | "minute must be in 0..59"); |
|---|
| 435 | n/a | return -1; |
|---|
| 436 | n/a | } |
|---|
| 437 | n/a | if (s < 0 || s > 59) { |
|---|
| 438 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 439 | n/a | "second must be in 0..59"); |
|---|
| 440 | n/a | return -1; |
|---|
| 441 | n/a | } |
|---|
| 442 | n/a | if (us < 0 || us > 999999) { |
|---|
| 443 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 444 | n/a | "microsecond must be in 0..999999"); |
|---|
| 445 | n/a | return -1; |
|---|
| 446 | n/a | } |
|---|
| 447 | n/a | if (fold != 0 && fold != 1) { |
|---|
| 448 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 449 | n/a | "fold must be either 0 or 1"); |
|---|
| 450 | n/a | return -1; |
|---|
| 451 | n/a | } |
|---|
| 452 | n/a | return 0; |
|---|
| 453 | n/a | } |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 456 | n/a | * Normalization utilities. |
|---|
| 457 | n/a | */ |
|---|
| 458 | n/a | |
|---|
| 459 | n/a | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
|---|
| 460 | n/a | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
|---|
| 461 | n/a | * at least factor, enough of *lo is converted into "hi" units so that |
|---|
| 462 | n/a | * 0 <= *lo < factor. The input values must be such that int overflow |
|---|
| 463 | n/a | * is impossible. |
|---|
| 464 | n/a | */ |
|---|
| 465 | n/a | static void |
|---|
| 466 | n/a | normalize_pair(int *hi, int *lo, int factor) |
|---|
| 467 | n/a | { |
|---|
| 468 | n/a | assert(factor > 0); |
|---|
| 469 | n/a | assert(lo != hi); |
|---|
| 470 | n/a | if (*lo < 0 || *lo >= factor) { |
|---|
| 471 | n/a | const int num_hi = divmod(*lo, factor, lo); |
|---|
| 472 | n/a | const int new_hi = *hi + num_hi; |
|---|
| 473 | n/a | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
|---|
| 474 | n/a | *hi = new_hi; |
|---|
| 475 | n/a | } |
|---|
| 476 | n/a | assert(0 <= *lo && *lo < factor); |
|---|
| 477 | n/a | } |
|---|
| 478 | n/a | |
|---|
| 479 | n/a | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
|---|
| 480 | n/a | * 0 <= *s < 24*3600 |
|---|
| 481 | n/a | * 0 <= *us < 1000000 |
|---|
| 482 | n/a | * The input values must be such that the internals don't overflow. |
|---|
| 483 | n/a | * The way this routine is used, we don't get close. |
|---|
| 484 | n/a | */ |
|---|
| 485 | n/a | static void |
|---|
| 486 | n/a | normalize_d_s_us(int *d, int *s, int *us) |
|---|
| 487 | n/a | { |
|---|
| 488 | n/a | if (*us < 0 || *us >= 1000000) { |
|---|
| 489 | n/a | normalize_pair(s, us, 1000000); |
|---|
| 490 | n/a | /* |s| can't be bigger than about |
|---|
| 491 | n/a | * |original s| + |original us|/1000000 now. |
|---|
| 492 | n/a | */ |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | } |
|---|
| 495 | n/a | if (*s < 0 || *s >= 24*3600) { |
|---|
| 496 | n/a | normalize_pair(d, s, 24*3600); |
|---|
| 497 | n/a | /* |d| can't be bigger than about |
|---|
| 498 | n/a | * |original d| + |
|---|
| 499 | n/a | * (|original s| + |original us|/1000000) / (24*3600) now. |
|---|
| 500 | n/a | */ |
|---|
| 501 | n/a | } |
|---|
| 502 | n/a | assert(0 <= *s && *s < 24*3600); |
|---|
| 503 | n/a | assert(0 <= *us && *us < 1000000); |
|---|
| 504 | n/a | } |
|---|
| 505 | n/a | |
|---|
| 506 | n/a | /* Fiddle years (y), months (m), and days (d) so that |
|---|
| 507 | n/a | * 1 <= *m <= 12 |
|---|
| 508 | n/a | * 1 <= *d <= days_in_month(*y, *m) |
|---|
| 509 | n/a | * The input values must be such that the internals don't overflow. |
|---|
| 510 | n/a | * The way this routine is used, we don't get close. |
|---|
| 511 | n/a | */ |
|---|
| 512 | n/a | static int |
|---|
| 513 | n/a | normalize_y_m_d(int *y, int *m, int *d) |
|---|
| 514 | n/a | { |
|---|
| 515 | n/a | int dim; /* # of days in month */ |
|---|
| 516 | n/a | |
|---|
| 517 | n/a | /* In actual use, m is always the month component extracted from a |
|---|
| 518 | n/a | * date/datetime object. Therefore it is always in [1, 12] range. |
|---|
| 519 | n/a | */ |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | assert(1 <= *m && *m <= 12); |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | /* Now only day can be out of bounds (year may also be out of bounds |
|---|
| 524 | n/a | * for a datetime object, but we don't care about that here). |
|---|
| 525 | n/a | * If day is out of bounds, what to do is arguable, but at least the |
|---|
| 526 | n/a | * method here is principled and explainable. |
|---|
| 527 | n/a | */ |
|---|
| 528 | n/a | dim = days_in_month(*y, *m); |
|---|
| 529 | n/a | if (*d < 1 || *d > dim) { |
|---|
| 530 | n/a | /* Move day-1 days from the first of the month. First try to |
|---|
| 531 | n/a | * get off cheap if we're only one day out of range |
|---|
| 532 | n/a | * (adjustments for timezone alone can't be worse than that). |
|---|
| 533 | n/a | */ |
|---|
| 534 | n/a | if (*d == 0) { |
|---|
| 535 | n/a | --*m; |
|---|
| 536 | n/a | if (*m > 0) |
|---|
| 537 | n/a | *d = days_in_month(*y, *m); |
|---|
| 538 | n/a | else { |
|---|
| 539 | n/a | --*y; |
|---|
| 540 | n/a | *m = 12; |
|---|
| 541 | n/a | *d = 31; |
|---|
| 542 | n/a | } |
|---|
| 543 | n/a | } |
|---|
| 544 | n/a | else if (*d == dim + 1) { |
|---|
| 545 | n/a | /* move forward a day */ |
|---|
| 546 | n/a | ++*m; |
|---|
| 547 | n/a | *d = 1; |
|---|
| 548 | n/a | if (*m > 12) { |
|---|
| 549 | n/a | *m = 1; |
|---|
| 550 | n/a | ++*y; |
|---|
| 551 | n/a | } |
|---|
| 552 | n/a | } |
|---|
| 553 | n/a | else { |
|---|
| 554 | n/a | int ordinal = ymd_to_ord(*y, *m, 1) + |
|---|
| 555 | n/a | *d - 1; |
|---|
| 556 | n/a | if (ordinal < 1 || ordinal > MAXORDINAL) { |
|---|
| 557 | n/a | goto error; |
|---|
| 558 | n/a | } else { |
|---|
| 559 | n/a | ord_to_ymd(ordinal, y, m, d); |
|---|
| 560 | n/a | return 0; |
|---|
| 561 | n/a | } |
|---|
| 562 | n/a | } |
|---|
| 563 | n/a | } |
|---|
| 564 | n/a | assert(*m > 0); |
|---|
| 565 | n/a | assert(*d > 0); |
|---|
| 566 | n/a | if (MINYEAR <= *y && *y <= MAXYEAR) |
|---|
| 567 | n/a | return 0; |
|---|
| 568 | n/a | error: |
|---|
| 569 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 570 | n/a | "date value out of range"); |
|---|
| 571 | n/a | return -1; |
|---|
| 572 | n/a | |
|---|
| 573 | n/a | } |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | /* Fiddle out-of-bounds months and days so that the result makes some kind |
|---|
| 576 | n/a | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
|---|
| 577 | n/a | * failure, where failure means the adjusted year is out of bounds. |
|---|
| 578 | n/a | */ |
|---|
| 579 | n/a | static int |
|---|
| 580 | n/a | normalize_date(int *year, int *month, int *day) |
|---|
| 581 | n/a | { |
|---|
| 582 | n/a | return normalize_y_m_d(year, month, day); |
|---|
| 583 | n/a | } |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | /* Force all the datetime fields into range. The parameters are both |
|---|
| 586 | n/a | * inputs and outputs. Returns < 0 on error. |
|---|
| 587 | n/a | */ |
|---|
| 588 | n/a | static int |
|---|
| 589 | n/a | normalize_datetime(int *year, int *month, int *day, |
|---|
| 590 | n/a | int *hour, int *minute, int *second, |
|---|
| 591 | n/a | int *microsecond) |
|---|
| 592 | n/a | { |
|---|
| 593 | n/a | normalize_pair(second, microsecond, 1000000); |
|---|
| 594 | n/a | normalize_pair(minute, second, 60); |
|---|
| 595 | n/a | normalize_pair(hour, minute, 60); |
|---|
| 596 | n/a | normalize_pair(day, hour, 24); |
|---|
| 597 | n/a | return normalize_date(year, month, day); |
|---|
| 598 | n/a | } |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 601 | n/a | * Basic object allocation: tp_alloc implementations. These allocate |
|---|
| 602 | n/a | * Python objects of the right size and type, and do the Python object- |
|---|
| 603 | n/a | * initialization bit. If there's not enough memory, they return NULL after |
|---|
| 604 | n/a | * setting MemoryError. All data members remain uninitialized trash. |
|---|
| 605 | n/a | * |
|---|
| 606 | n/a | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
|---|
| 607 | n/a | * member is needed. This is ugly, imprecise, and possibly insecure. |
|---|
| 608 | n/a | * tp_basicsize for the time and datetime types is set to the size of the |
|---|
| 609 | n/a | * struct that has room for the tzinfo member, so subclasses in Python will |
|---|
| 610 | n/a | * allocate enough space for a tzinfo member whether or not one is actually |
|---|
| 611 | n/a | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
|---|
| 612 | n/a | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
|---|
| 613 | n/a | * using) just happens today to effectively ignore the nitems argument |
|---|
| 614 | n/a | * when tp_itemsize is 0, which it is for these type objects. If that |
|---|
| 615 | n/a | * changes, perhaps the callers of tp_alloc slots in this file should |
|---|
| 616 | n/a | * be changed to force a 0 nitems argument unless the type being allocated |
|---|
| 617 | n/a | * is a base type implemented in this file (so that tp_alloc is time_alloc |
|---|
| 618 | n/a | * or datetime_alloc below, which know about the nitems abuse). |
|---|
| 619 | n/a | */ |
|---|
| 620 | n/a | |
|---|
| 621 | n/a | static PyObject * |
|---|
| 622 | n/a | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
|---|
| 623 | n/a | { |
|---|
| 624 | n/a | PyObject *self; |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | self = (PyObject *) |
|---|
| 627 | n/a | PyObject_MALLOC(aware ? |
|---|
| 628 | n/a | sizeof(PyDateTime_Time) : |
|---|
| 629 | n/a | sizeof(_PyDateTime_BaseTime)); |
|---|
| 630 | n/a | if (self == NULL) |
|---|
| 631 | n/a | return (PyObject *)PyErr_NoMemory(); |
|---|
| 632 | n/a | (void)PyObject_INIT(self, type); |
|---|
| 633 | n/a | return self; |
|---|
| 634 | n/a | } |
|---|
| 635 | n/a | |
|---|
| 636 | n/a | static PyObject * |
|---|
| 637 | n/a | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
|---|
| 638 | n/a | { |
|---|
| 639 | n/a | PyObject *self; |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | self = (PyObject *) |
|---|
| 642 | n/a | PyObject_MALLOC(aware ? |
|---|
| 643 | n/a | sizeof(PyDateTime_DateTime) : |
|---|
| 644 | n/a | sizeof(_PyDateTime_BaseDateTime)); |
|---|
| 645 | n/a | if (self == NULL) |
|---|
| 646 | n/a | return (PyObject *)PyErr_NoMemory(); |
|---|
| 647 | n/a | (void)PyObject_INIT(self, type); |
|---|
| 648 | n/a | return self; |
|---|
| 649 | n/a | } |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 652 | n/a | * Helpers for setting object fields. These work on pointers to the |
|---|
| 653 | n/a | * appropriate base class. |
|---|
| 654 | n/a | */ |
|---|
| 655 | n/a | |
|---|
| 656 | n/a | /* For date and datetime. */ |
|---|
| 657 | n/a | static void |
|---|
| 658 | n/a | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
|---|
| 659 | n/a | { |
|---|
| 660 | n/a | self->hashcode = -1; |
|---|
| 661 | n/a | SET_YEAR(self, y); |
|---|
| 662 | n/a | SET_MONTH(self, m); |
|---|
| 663 | n/a | SET_DAY(self, d); |
|---|
| 664 | n/a | } |
|---|
| 665 | n/a | |
|---|
| 666 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 667 | n/a | * Create various objects, mostly without range checking. |
|---|
| 668 | n/a | */ |
|---|
| 669 | n/a | |
|---|
| 670 | n/a | /* Create a date instance with no range checking. */ |
|---|
| 671 | n/a | static PyObject * |
|---|
| 672 | n/a | new_date_ex(int year, int month, int day, PyTypeObject *type) |
|---|
| 673 | n/a | { |
|---|
| 674 | n/a | PyDateTime_Date *self; |
|---|
| 675 | n/a | |
|---|
| 676 | n/a | if (check_date_args(year, month, day) < 0) { |
|---|
| 677 | n/a | return NULL; |
|---|
| 678 | n/a | } |
|---|
| 679 | n/a | |
|---|
| 680 | n/a | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
|---|
| 681 | n/a | if (self != NULL) |
|---|
| 682 | n/a | set_date_fields(self, year, month, day); |
|---|
| 683 | n/a | return (PyObject *) self; |
|---|
| 684 | n/a | } |
|---|
| 685 | n/a | |
|---|
| 686 | n/a | #define new_date(year, month, day) \ |
|---|
| 687 | n/a | new_date_ex(year, month, day, &PyDateTime_DateType) |
|---|
| 688 | n/a | |
|---|
| 689 | n/a | /* Create a datetime instance with no range checking. */ |
|---|
| 690 | n/a | static PyObject * |
|---|
| 691 | n/a | new_datetime_ex2(int year, int month, int day, int hour, int minute, |
|---|
| 692 | n/a | int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) |
|---|
| 693 | n/a | { |
|---|
| 694 | n/a | PyDateTime_DateTime *self; |
|---|
| 695 | n/a | char aware = tzinfo != Py_None; |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | if (check_date_args(year, month, day) < 0) { |
|---|
| 698 | n/a | return NULL; |
|---|
| 699 | n/a | } |
|---|
| 700 | n/a | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
|---|
| 701 | n/a | return NULL; |
|---|
| 702 | n/a | } |
|---|
| 703 | n/a | if (check_tzinfo_subclass(tzinfo) < 0) { |
|---|
| 704 | n/a | return NULL; |
|---|
| 705 | n/a | } |
|---|
| 706 | n/a | |
|---|
| 707 | n/a | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
|---|
| 708 | n/a | if (self != NULL) { |
|---|
| 709 | n/a | self->hastzinfo = aware; |
|---|
| 710 | n/a | set_date_fields((PyDateTime_Date *)self, year, month, day); |
|---|
| 711 | n/a | DATE_SET_HOUR(self, hour); |
|---|
| 712 | n/a | DATE_SET_MINUTE(self, minute); |
|---|
| 713 | n/a | DATE_SET_SECOND(self, second); |
|---|
| 714 | n/a | DATE_SET_MICROSECOND(self, usecond); |
|---|
| 715 | n/a | if (aware) { |
|---|
| 716 | n/a | Py_INCREF(tzinfo); |
|---|
| 717 | n/a | self->tzinfo = tzinfo; |
|---|
| 718 | n/a | } |
|---|
| 719 | n/a | DATE_SET_FOLD(self, fold); |
|---|
| 720 | n/a | } |
|---|
| 721 | n/a | return (PyObject *)self; |
|---|
| 722 | n/a | } |
|---|
| 723 | n/a | |
|---|
| 724 | n/a | static PyObject * |
|---|
| 725 | n/a | new_datetime_ex(int year, int month, int day, int hour, int minute, |
|---|
| 726 | n/a | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
|---|
| 727 | n/a | { |
|---|
| 728 | n/a | return new_datetime_ex2(year, month, day, hour, minute, second, usecond, |
|---|
| 729 | n/a | tzinfo, 0, type); |
|---|
| 730 | n/a | } |
|---|
| 731 | n/a | |
|---|
| 732 | n/a | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ |
|---|
| 733 | n/a | new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ |
|---|
| 734 | n/a | &PyDateTime_DateTimeType) |
|---|
| 735 | n/a | |
|---|
| 736 | n/a | /* Create a time instance with no range checking. */ |
|---|
| 737 | n/a | static PyObject * |
|---|
| 738 | n/a | new_time_ex2(int hour, int minute, int second, int usecond, |
|---|
| 739 | n/a | PyObject *tzinfo, int fold, PyTypeObject *type) |
|---|
| 740 | n/a | { |
|---|
| 741 | n/a | PyDateTime_Time *self; |
|---|
| 742 | n/a | char aware = tzinfo != Py_None; |
|---|
| 743 | n/a | |
|---|
| 744 | n/a | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
|---|
| 745 | n/a | return NULL; |
|---|
| 746 | n/a | } |
|---|
| 747 | n/a | if (check_tzinfo_subclass(tzinfo) < 0) { |
|---|
| 748 | n/a | return NULL; |
|---|
| 749 | n/a | } |
|---|
| 750 | n/a | |
|---|
| 751 | n/a | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
|---|
| 752 | n/a | if (self != NULL) { |
|---|
| 753 | n/a | self->hastzinfo = aware; |
|---|
| 754 | n/a | self->hashcode = -1; |
|---|
| 755 | n/a | TIME_SET_HOUR(self, hour); |
|---|
| 756 | n/a | TIME_SET_MINUTE(self, minute); |
|---|
| 757 | n/a | TIME_SET_SECOND(self, second); |
|---|
| 758 | n/a | TIME_SET_MICROSECOND(self, usecond); |
|---|
| 759 | n/a | if (aware) { |
|---|
| 760 | n/a | Py_INCREF(tzinfo); |
|---|
| 761 | n/a | self->tzinfo = tzinfo; |
|---|
| 762 | n/a | } |
|---|
| 763 | n/a | TIME_SET_FOLD(self, fold); |
|---|
| 764 | n/a | } |
|---|
| 765 | n/a | return (PyObject *)self; |
|---|
| 766 | n/a | } |
|---|
| 767 | n/a | |
|---|
| 768 | n/a | static PyObject * |
|---|
| 769 | n/a | new_time_ex(int hour, int minute, int second, int usecond, |
|---|
| 770 | n/a | PyObject *tzinfo, PyTypeObject *type) |
|---|
| 771 | n/a | { |
|---|
| 772 | n/a | return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); |
|---|
| 773 | n/a | } |
|---|
| 774 | n/a | |
|---|
| 775 | n/a | #define new_time(hh, mm, ss, us, tzinfo, fold) \ |
|---|
| 776 | n/a | new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) |
|---|
| 777 | n/a | |
|---|
| 778 | n/a | /* Create a timedelta instance. Normalize the members iff normalize is |
|---|
| 779 | n/a | * true. Passing false is a speed optimization, if you know for sure |
|---|
| 780 | n/a | * that seconds and microseconds are already in their proper ranges. In any |
|---|
| 781 | n/a | * case, raises OverflowError and returns NULL if the normalized days is out |
|---|
| 782 | n/a | * of range). |
|---|
| 783 | n/a | */ |
|---|
| 784 | n/a | static PyObject * |
|---|
| 785 | n/a | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
|---|
| 786 | n/a | PyTypeObject *type) |
|---|
| 787 | n/a | { |
|---|
| 788 | n/a | PyDateTime_Delta *self; |
|---|
| 789 | n/a | |
|---|
| 790 | n/a | if (normalize) |
|---|
| 791 | n/a | normalize_d_s_us(&days, &seconds, µseconds); |
|---|
| 792 | n/a | assert(0 <= seconds && seconds < 24*3600); |
|---|
| 793 | n/a | assert(0 <= microseconds && microseconds < 1000000); |
|---|
| 794 | n/a | |
|---|
| 795 | n/a | if (check_delta_day_range(days) < 0) |
|---|
| 796 | n/a | return NULL; |
|---|
| 797 | n/a | |
|---|
| 798 | n/a | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
|---|
| 799 | n/a | if (self != NULL) { |
|---|
| 800 | n/a | self->hashcode = -1; |
|---|
| 801 | n/a | SET_TD_DAYS(self, days); |
|---|
| 802 | n/a | SET_TD_SECONDS(self, seconds); |
|---|
| 803 | n/a | SET_TD_MICROSECONDS(self, microseconds); |
|---|
| 804 | n/a | } |
|---|
| 805 | n/a | return (PyObject *) self; |
|---|
| 806 | n/a | } |
|---|
| 807 | n/a | |
|---|
| 808 | n/a | #define new_delta(d, s, us, normalize) \ |
|---|
| 809 | n/a | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
|---|
| 810 | n/a | |
|---|
| 811 | n/a | |
|---|
| 812 | n/a | typedef struct |
|---|
| 813 | n/a | { |
|---|
| 814 | n/a | PyObject_HEAD |
|---|
| 815 | n/a | PyObject *offset; |
|---|
| 816 | n/a | PyObject *name; |
|---|
| 817 | n/a | } PyDateTime_TimeZone; |
|---|
| 818 | n/a | |
|---|
| 819 | n/a | /* The interned UTC timezone instance */ |
|---|
| 820 | n/a | static PyObject *PyDateTime_TimeZone_UTC; |
|---|
| 821 | n/a | /* The interned Epoch datetime instance */ |
|---|
| 822 | n/a | static PyObject *PyDateTime_Epoch; |
|---|
| 823 | n/a | |
|---|
| 824 | n/a | /* Create new timezone instance checking offset range. This |
|---|
| 825 | n/a | function does not check the name argument. Caller must assure |
|---|
| 826 | n/a | that offset is a timedelta instance and name is either NULL |
|---|
| 827 | n/a | or a unicode object. */ |
|---|
| 828 | n/a | static PyObject * |
|---|
| 829 | n/a | create_timezone(PyObject *offset, PyObject *name) |
|---|
| 830 | n/a | { |
|---|
| 831 | n/a | PyDateTime_TimeZone *self; |
|---|
| 832 | n/a | PyTypeObject *type = &PyDateTime_TimeZoneType; |
|---|
| 833 | n/a | |
|---|
| 834 | n/a | assert(offset != NULL); |
|---|
| 835 | n/a | assert(PyDelta_Check(offset)); |
|---|
| 836 | n/a | assert(name == NULL || PyUnicode_Check(name)); |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
|---|
| 839 | n/a | if (self == NULL) { |
|---|
| 840 | n/a | return NULL; |
|---|
| 841 | n/a | } |
|---|
| 842 | n/a | Py_INCREF(offset); |
|---|
| 843 | n/a | self->offset = offset; |
|---|
| 844 | n/a | Py_XINCREF(name); |
|---|
| 845 | n/a | self->name = name; |
|---|
| 846 | n/a | return (PyObject *)self; |
|---|
| 847 | n/a | } |
|---|
| 848 | n/a | |
|---|
| 849 | n/a | static int delta_bool(PyDateTime_Delta *self); |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | static PyObject * |
|---|
| 852 | n/a | new_timezone(PyObject *offset, PyObject *name) |
|---|
| 853 | n/a | { |
|---|
| 854 | n/a | assert(offset != NULL); |
|---|
| 855 | n/a | assert(PyDelta_Check(offset)); |
|---|
| 856 | n/a | assert(name == NULL || PyUnicode_Check(name)); |
|---|
| 857 | n/a | |
|---|
| 858 | n/a | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
|---|
| 859 | n/a | Py_INCREF(PyDateTime_TimeZone_UTC); |
|---|
| 860 | n/a | return PyDateTime_TimeZone_UTC; |
|---|
| 861 | n/a | } |
|---|
| 862 | n/a | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
|---|
| 863 | n/a | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
|---|
| 864 | n/a | " representing a whole number of minutes," |
|---|
| 865 | n/a | " not %R.", offset); |
|---|
| 866 | n/a | return NULL; |
|---|
| 867 | n/a | } |
|---|
| 868 | n/a | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
|---|
| 869 | n/a | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
|---|
| 870 | n/a | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
|---|
| 871 | n/a | " strictly between -timedelta(hours=24) and" |
|---|
| 872 | n/a | " timedelta(hours=24)," |
|---|
| 873 | n/a | " not %R.", offset); |
|---|
| 874 | n/a | return NULL; |
|---|
| 875 | n/a | } |
|---|
| 876 | n/a | |
|---|
| 877 | n/a | return create_timezone(offset, name); |
|---|
| 878 | n/a | } |
|---|
| 879 | n/a | |
|---|
| 880 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 881 | n/a | * tzinfo helpers. |
|---|
| 882 | n/a | */ |
|---|
| 883 | n/a | |
|---|
| 884 | n/a | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
|---|
| 885 | n/a | * raise TypeError and return -1. |
|---|
| 886 | n/a | */ |
|---|
| 887 | n/a | static int |
|---|
| 888 | n/a | check_tzinfo_subclass(PyObject *p) |
|---|
| 889 | n/a | { |
|---|
| 890 | n/a | if (p == Py_None || PyTZInfo_Check(p)) |
|---|
| 891 | n/a | return 0; |
|---|
| 892 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 893 | n/a | "tzinfo argument must be None or of a tzinfo subclass, " |
|---|
| 894 | n/a | "not type '%s'", |
|---|
| 895 | n/a | Py_TYPE(p)->tp_name); |
|---|
| 896 | n/a | return -1; |
|---|
| 897 | n/a | } |
|---|
| 898 | n/a | |
|---|
| 899 | n/a | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
|---|
| 900 | n/a | * return NULL, which is NOT AN ERROR. There are no error returns here, |
|---|
| 901 | n/a | * and the caller must not decref the result. |
|---|
| 902 | n/a | */ |
|---|
| 903 | n/a | static PyObject * |
|---|
| 904 | n/a | get_tzinfo_member(PyObject *self) |
|---|
| 905 | n/a | { |
|---|
| 906 | n/a | PyObject *tzinfo = NULL; |
|---|
| 907 | n/a | |
|---|
| 908 | n/a | if (PyDateTime_Check(self) && HASTZINFO(self)) |
|---|
| 909 | n/a | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
|---|
| 910 | n/a | else if (PyTime_Check(self) && HASTZINFO(self)) |
|---|
| 911 | n/a | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
|---|
| 912 | n/a | |
|---|
| 913 | n/a | return tzinfo; |
|---|
| 914 | n/a | } |
|---|
| 915 | n/a | |
|---|
| 916 | n/a | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
|---|
| 917 | n/a | * be an instance of the tzinfo class. If the method returns None, this |
|---|
| 918 | n/a | * returns None. If the method doesn't return None or timedelta, TypeError is |
|---|
| 919 | n/a | * raised and this returns NULL. If it returns a timedelta and the value is |
|---|
| 920 | n/a | * out of range or isn't a whole number of minutes, ValueError is raised and |
|---|
| 921 | n/a | * this returns NULL. Else result is returned. |
|---|
| 922 | n/a | */ |
|---|
| 923 | n/a | static PyObject * |
|---|
| 924 | n/a | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) |
|---|
| 925 | n/a | { |
|---|
| 926 | n/a | PyObject *offset; |
|---|
| 927 | n/a | |
|---|
| 928 | n/a | assert(tzinfo != NULL); |
|---|
| 929 | n/a | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
|---|
| 930 | n/a | assert(tzinfoarg != NULL); |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | if (tzinfo == Py_None) |
|---|
| 933 | n/a | Py_RETURN_NONE; |
|---|
| 934 | n/a | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
|---|
| 935 | n/a | if (offset == Py_None || offset == NULL) |
|---|
| 936 | n/a | return offset; |
|---|
| 937 | n/a | if (PyDelta_Check(offset)) { |
|---|
| 938 | n/a | if (GET_TD_MICROSECONDS(offset) != 0) { |
|---|
| 939 | n/a | Py_DECREF(offset); |
|---|
| 940 | n/a | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
|---|
| 941 | n/a | " representing a whole number of seconds"); |
|---|
| 942 | n/a | return NULL; |
|---|
| 943 | n/a | } |
|---|
| 944 | n/a | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
|---|
| 945 | n/a | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
|---|
| 946 | n/a | Py_DECREF(offset); |
|---|
| 947 | n/a | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
|---|
| 948 | n/a | " strictly between -timedelta(hours=24) and" |
|---|
| 949 | n/a | " timedelta(hours=24)."); |
|---|
| 950 | n/a | return NULL; |
|---|
| 951 | n/a | } |
|---|
| 952 | n/a | } |
|---|
| 953 | n/a | else { |
|---|
| 954 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 955 | n/a | "tzinfo.%s() must return None or " |
|---|
| 956 | n/a | "timedelta, not '%.200s'", |
|---|
| 957 | n/a | name, Py_TYPE(offset)->tp_name); |
|---|
| 958 | n/a | Py_DECREF(offset); |
|---|
| 959 | n/a | return NULL; |
|---|
| 960 | n/a | } |
|---|
| 961 | n/a | |
|---|
| 962 | n/a | return offset; |
|---|
| 963 | n/a | } |
|---|
| 964 | n/a | |
|---|
| 965 | n/a | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
|---|
| 966 | n/a | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
|---|
| 967 | n/a | * returns None, call_utcoffset returns 0 and sets *none to 1. If uctoffset() |
|---|
| 968 | n/a | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
|---|
| 969 | n/a | * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
|---|
| 970 | n/a | * # of minutes), ValueError is raised and this returns -1. Else *none is |
|---|
| 971 | n/a | * set to 0 and the offset is returned (as int # of minutes east of UTC). |
|---|
| 972 | n/a | */ |
|---|
| 973 | n/a | static PyObject * |
|---|
| 974 | n/a | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
|---|
| 975 | n/a | { |
|---|
| 976 | n/a | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
|---|
| 977 | n/a | } |
|---|
| 978 | n/a | |
|---|
| 979 | n/a | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
|---|
| 980 | n/a | * result. tzinfo must be an instance of the tzinfo class. If dst() |
|---|
| 981 | n/a | * returns None, call_dst returns 0 and sets *none to 1. If dst() |
|---|
| 982 | n/a | & doesn't return None or timedelta, TypeError is raised and this |
|---|
| 983 | n/a | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
|---|
| 984 | n/a | * ValueError is raised and this returns -1. Else *none is set to 0 and |
|---|
| 985 | n/a | * the offset is returned (as an int # of minutes east of UTC). |
|---|
| 986 | n/a | */ |
|---|
| 987 | n/a | static PyObject * |
|---|
| 988 | n/a | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
|---|
| 989 | n/a | { |
|---|
| 990 | n/a | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
|---|
| 991 | n/a | } |
|---|
| 992 | n/a | |
|---|
| 993 | n/a | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
|---|
| 994 | n/a | * an instance of the tzinfo class or None. If tzinfo isn't None, and |
|---|
| 995 | n/a | * tzname() doesn't return None or a string, TypeError is raised and this |
|---|
| 996 | n/a | * returns NULL. If the result is a string, we ensure it is a Unicode |
|---|
| 997 | n/a | * string. |
|---|
| 998 | n/a | */ |
|---|
| 999 | n/a | static PyObject * |
|---|
| 1000 | n/a | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
|---|
| 1001 | n/a | { |
|---|
| 1002 | n/a | PyObject *result; |
|---|
| 1003 | n/a | _Py_IDENTIFIER(tzname); |
|---|
| 1004 | n/a | |
|---|
| 1005 | n/a | assert(tzinfo != NULL); |
|---|
| 1006 | n/a | assert(check_tzinfo_subclass(tzinfo) >= 0); |
|---|
| 1007 | n/a | assert(tzinfoarg != NULL); |
|---|
| 1008 | n/a | |
|---|
| 1009 | n/a | if (tzinfo == Py_None) |
|---|
| 1010 | n/a | Py_RETURN_NONE; |
|---|
| 1011 | n/a | |
|---|
| 1012 | n/a | result = _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_tzname, |
|---|
| 1013 | n/a | tzinfoarg, NULL); |
|---|
| 1014 | n/a | |
|---|
| 1015 | n/a | if (result == NULL || result == Py_None) |
|---|
| 1016 | n/a | return result; |
|---|
| 1017 | n/a | |
|---|
| 1018 | n/a | if (!PyUnicode_Check(result)) { |
|---|
| 1019 | n/a | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
|---|
| 1020 | n/a | "return None or a string, not '%s'", |
|---|
| 1021 | n/a | Py_TYPE(result)->tp_name); |
|---|
| 1022 | n/a | Py_DECREF(result); |
|---|
| 1023 | n/a | result = NULL; |
|---|
| 1024 | n/a | } |
|---|
| 1025 | n/a | |
|---|
| 1026 | n/a | return result; |
|---|
| 1027 | n/a | } |
|---|
| 1028 | n/a | |
|---|
| 1029 | n/a | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
|---|
| 1030 | n/a | * stuff |
|---|
| 1031 | n/a | * ", tzinfo=" + repr(tzinfo) |
|---|
| 1032 | n/a | * before the closing ")". |
|---|
| 1033 | n/a | */ |
|---|
| 1034 | n/a | static PyObject * |
|---|
| 1035 | n/a | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
|---|
| 1036 | n/a | { |
|---|
| 1037 | n/a | PyObject *temp; |
|---|
| 1038 | n/a | |
|---|
| 1039 | n/a | assert(PyUnicode_Check(repr)); |
|---|
| 1040 | n/a | assert(tzinfo); |
|---|
| 1041 | n/a | if (tzinfo == Py_None) |
|---|
| 1042 | n/a | return repr; |
|---|
| 1043 | n/a | /* Get rid of the trailing ')'. */ |
|---|
| 1044 | n/a | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
|---|
| 1045 | n/a | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
|---|
| 1046 | n/a | Py_DECREF(repr); |
|---|
| 1047 | n/a | if (temp == NULL) |
|---|
| 1048 | n/a | return NULL; |
|---|
| 1049 | n/a | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
|---|
| 1050 | n/a | Py_DECREF(temp); |
|---|
| 1051 | n/a | return repr; |
|---|
| 1052 | n/a | } |
|---|
| 1053 | n/a | |
|---|
| 1054 | n/a | /* repr is like "someclass(arg1, arg2)". If fold isn't 0, |
|---|
| 1055 | n/a | * stuff |
|---|
| 1056 | n/a | * ", fold=" + repr(tzinfo) |
|---|
| 1057 | n/a | * before the closing ")". |
|---|
| 1058 | n/a | */ |
|---|
| 1059 | n/a | static PyObject * |
|---|
| 1060 | n/a | append_keyword_fold(PyObject *repr, int fold) |
|---|
| 1061 | n/a | { |
|---|
| 1062 | n/a | PyObject *temp; |
|---|
| 1063 | n/a | |
|---|
| 1064 | n/a | assert(PyUnicode_Check(repr)); |
|---|
| 1065 | n/a | if (fold == 0) |
|---|
| 1066 | n/a | return repr; |
|---|
| 1067 | n/a | /* Get rid of the trailing ')'. */ |
|---|
| 1068 | n/a | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
|---|
| 1069 | n/a | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
|---|
| 1070 | n/a | Py_DECREF(repr); |
|---|
| 1071 | n/a | if (temp == NULL) |
|---|
| 1072 | n/a | return NULL; |
|---|
| 1073 | n/a | repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); |
|---|
| 1074 | n/a | Py_DECREF(temp); |
|---|
| 1075 | n/a | return repr; |
|---|
| 1076 | n/a | } |
|---|
| 1077 | n/a | |
|---|
| 1078 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 1079 | n/a | * String format helpers. |
|---|
| 1080 | n/a | */ |
|---|
| 1081 | n/a | |
|---|
| 1082 | n/a | static PyObject * |
|---|
| 1083 | n/a | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
|---|
| 1084 | n/a | { |
|---|
| 1085 | n/a | static const char * const DayNames[] = { |
|---|
| 1086 | n/a | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
|---|
| 1087 | n/a | }; |
|---|
| 1088 | n/a | static const char * const MonthNames[] = { |
|---|
| 1089 | n/a | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
|---|
| 1090 | n/a | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
|---|
| 1091 | n/a | }; |
|---|
| 1092 | n/a | |
|---|
| 1093 | n/a | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
|---|
| 1094 | n/a | |
|---|
| 1095 | n/a | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
|---|
| 1096 | n/a | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
|---|
| 1097 | n/a | GET_DAY(date), hours, minutes, seconds, |
|---|
| 1098 | n/a | GET_YEAR(date)); |
|---|
| 1099 | n/a | } |
|---|
| 1100 | n/a | |
|---|
| 1101 | n/a | static PyObject *delta_negative(PyDateTime_Delta *self); |
|---|
| 1102 | n/a | |
|---|
| 1103 | n/a | /* Add an hours & minutes UTC offset string to buf. buf has no more than |
|---|
| 1104 | n/a | * buflen bytes remaining. The UTC offset is gotten by calling |
|---|
| 1105 | n/a | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
|---|
| 1106 | n/a | * *buf, and that's all. Else the returned value is checked for sanity (an |
|---|
| 1107 | n/a | * integer in range), and if that's OK it's converted to an hours & minutes |
|---|
| 1108 | n/a | * string of the form |
|---|
| 1109 | n/a | * sign HH sep MM |
|---|
| 1110 | n/a | * Returns 0 if everything is OK. If the return value from utcoffset() is |
|---|
| 1111 | n/a | * bogus, an appropriate exception is set and -1 is returned. |
|---|
| 1112 | n/a | */ |
|---|
| 1113 | n/a | static int |
|---|
| 1114 | n/a | format_utcoffset(char *buf, size_t buflen, const char *sep, |
|---|
| 1115 | n/a | PyObject *tzinfo, PyObject *tzinfoarg) |
|---|
| 1116 | n/a | { |
|---|
| 1117 | n/a | PyObject *offset; |
|---|
| 1118 | n/a | int hours, minutes, seconds; |
|---|
| 1119 | n/a | char sign; |
|---|
| 1120 | n/a | |
|---|
| 1121 | n/a | assert(buflen >= 1); |
|---|
| 1122 | n/a | |
|---|
| 1123 | n/a | offset = call_utcoffset(tzinfo, tzinfoarg); |
|---|
| 1124 | n/a | if (offset == NULL) |
|---|
| 1125 | n/a | return -1; |
|---|
| 1126 | n/a | if (offset == Py_None) { |
|---|
| 1127 | n/a | Py_DECREF(offset); |
|---|
| 1128 | n/a | *buf = '\0'; |
|---|
| 1129 | n/a | return 0; |
|---|
| 1130 | n/a | } |
|---|
| 1131 | n/a | /* Offset is normalized, so it is negative if days < 0 */ |
|---|
| 1132 | n/a | if (GET_TD_DAYS(offset) < 0) { |
|---|
| 1133 | n/a | sign = '-'; |
|---|
| 1134 | n/a | Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); |
|---|
| 1135 | n/a | if (offset == NULL) |
|---|
| 1136 | n/a | return -1; |
|---|
| 1137 | n/a | } |
|---|
| 1138 | n/a | else { |
|---|
| 1139 | n/a | sign = '+'; |
|---|
| 1140 | n/a | } |
|---|
| 1141 | n/a | /* Offset is not negative here. */ |
|---|
| 1142 | n/a | seconds = GET_TD_SECONDS(offset); |
|---|
| 1143 | n/a | Py_DECREF(offset); |
|---|
| 1144 | n/a | minutes = divmod(seconds, 60, &seconds); |
|---|
| 1145 | n/a | hours = divmod(minutes, 60, &minutes); |
|---|
| 1146 | n/a | if (seconds == 0) |
|---|
| 1147 | n/a | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
|---|
| 1148 | n/a | else |
|---|
| 1149 | n/a | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, |
|---|
| 1150 | n/a | sep, minutes, sep, seconds); |
|---|
| 1151 | n/a | return 0; |
|---|
| 1152 | n/a | } |
|---|
| 1153 | n/a | |
|---|
| 1154 | n/a | static PyObject * |
|---|
| 1155 | n/a | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
|---|
| 1156 | n/a | { |
|---|
| 1157 | n/a | PyObject *temp; |
|---|
| 1158 | n/a | PyObject *tzinfo = get_tzinfo_member(object); |
|---|
| 1159 | n/a | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
|---|
| 1160 | n/a | _Py_IDENTIFIER(replace); |
|---|
| 1161 | n/a | |
|---|
| 1162 | n/a | if (Zreplacement == NULL) |
|---|
| 1163 | n/a | return NULL; |
|---|
| 1164 | n/a | if (tzinfo == Py_None || tzinfo == NULL) |
|---|
| 1165 | n/a | return Zreplacement; |
|---|
| 1166 | n/a | |
|---|
| 1167 | n/a | assert(tzinfoarg != NULL); |
|---|
| 1168 | n/a | temp = call_tzname(tzinfo, tzinfoarg); |
|---|
| 1169 | n/a | if (temp == NULL) |
|---|
| 1170 | n/a | goto Error; |
|---|
| 1171 | n/a | if (temp == Py_None) { |
|---|
| 1172 | n/a | Py_DECREF(temp); |
|---|
| 1173 | n/a | return Zreplacement; |
|---|
| 1174 | n/a | } |
|---|
| 1175 | n/a | |
|---|
| 1176 | n/a | assert(PyUnicode_Check(temp)); |
|---|
| 1177 | n/a | /* Since the tzname is getting stuffed into the |
|---|
| 1178 | n/a | * format, we have to double any % signs so that |
|---|
| 1179 | n/a | * strftime doesn't treat them as format codes. |
|---|
| 1180 | n/a | */ |
|---|
| 1181 | n/a | Py_DECREF(Zreplacement); |
|---|
| 1182 | n/a | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
|---|
| 1183 | n/a | Py_DECREF(temp); |
|---|
| 1184 | n/a | if (Zreplacement == NULL) |
|---|
| 1185 | n/a | return NULL; |
|---|
| 1186 | n/a | if (!PyUnicode_Check(Zreplacement)) { |
|---|
| 1187 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1188 | n/a | "tzname.replace() did not return a string"); |
|---|
| 1189 | n/a | goto Error; |
|---|
| 1190 | n/a | } |
|---|
| 1191 | n/a | return Zreplacement; |
|---|
| 1192 | n/a | |
|---|
| 1193 | n/a | Error: |
|---|
| 1194 | n/a | Py_DECREF(Zreplacement); |
|---|
| 1195 | n/a | return NULL; |
|---|
| 1196 | n/a | } |
|---|
| 1197 | n/a | |
|---|
| 1198 | n/a | static PyObject * |
|---|
| 1199 | n/a | make_freplacement(PyObject *object) |
|---|
| 1200 | n/a | { |
|---|
| 1201 | n/a | char freplacement[64]; |
|---|
| 1202 | n/a | if (PyTime_Check(object)) |
|---|
| 1203 | n/a | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
|---|
| 1204 | n/a | else if (PyDateTime_Check(object)) |
|---|
| 1205 | n/a | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
|---|
| 1206 | n/a | else |
|---|
| 1207 | n/a | sprintf(freplacement, "%06d", 0); |
|---|
| 1208 | n/a | |
|---|
| 1209 | n/a | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
|---|
| 1210 | n/a | } |
|---|
| 1211 | n/a | |
|---|
| 1212 | n/a | /* I sure don't want to reproduce the strftime code from the time module, |
|---|
| 1213 | n/a | * so this imports the module and calls it. All the hair is due to |
|---|
| 1214 | n/a | * giving special meanings to the %z, %Z and %f format codes via a |
|---|
| 1215 | n/a | * preprocessing step on the format string. |
|---|
| 1216 | n/a | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
|---|
| 1217 | n/a | * needed. |
|---|
| 1218 | n/a | */ |
|---|
| 1219 | n/a | static PyObject * |
|---|
| 1220 | n/a | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
|---|
| 1221 | n/a | PyObject *tzinfoarg) |
|---|
| 1222 | n/a | { |
|---|
| 1223 | n/a | PyObject *result = NULL; /* guilty until proved innocent */ |
|---|
| 1224 | n/a | |
|---|
| 1225 | n/a | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
|---|
| 1226 | n/a | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
|---|
| 1227 | n/a | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
|---|
| 1228 | n/a | |
|---|
| 1229 | n/a | const char *pin; /* pointer to next char in input format */ |
|---|
| 1230 | n/a | Py_ssize_t flen; /* length of input format */ |
|---|
| 1231 | n/a | char ch; /* next char in input format */ |
|---|
| 1232 | n/a | |
|---|
| 1233 | n/a | PyObject *newfmt = NULL; /* py string, the output format */ |
|---|
| 1234 | n/a | char *pnew; /* pointer to available byte in output format */ |
|---|
| 1235 | n/a | size_t totalnew; /* number bytes total in output format buffer, |
|---|
| 1236 | n/a | exclusive of trailing \0 */ |
|---|
| 1237 | n/a | size_t usednew; /* number bytes used so far in output format buffer */ |
|---|
| 1238 | n/a | |
|---|
| 1239 | n/a | const char *ptoappend; /* ptr to string to append to output buffer */ |
|---|
| 1240 | n/a | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
|---|
| 1241 | n/a | |
|---|
| 1242 | n/a | assert(object && format && timetuple); |
|---|
| 1243 | n/a | assert(PyUnicode_Check(format)); |
|---|
| 1244 | n/a | /* Convert the input format to a C string and size */ |
|---|
| 1245 | n/a | pin = PyUnicode_AsUTF8AndSize(format, &flen); |
|---|
| 1246 | n/a | if (!pin) |
|---|
| 1247 | n/a | return NULL; |
|---|
| 1248 | n/a | |
|---|
| 1249 | n/a | /* Scan the input format, looking for %z/%Z/%f escapes, building |
|---|
| 1250 | n/a | * a new format. Since computing the replacements for those codes |
|---|
| 1251 | n/a | * is expensive, don't unless they're actually used. |
|---|
| 1252 | n/a | */ |
|---|
| 1253 | n/a | if (flen > INT_MAX - 1) { |
|---|
| 1254 | n/a | PyErr_NoMemory(); |
|---|
| 1255 | n/a | goto Done; |
|---|
| 1256 | n/a | } |
|---|
| 1257 | n/a | |
|---|
| 1258 | n/a | totalnew = flen + 1; /* realistic if no %z/%Z */ |
|---|
| 1259 | n/a | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
|---|
| 1260 | n/a | if (newfmt == NULL) goto Done; |
|---|
| 1261 | n/a | pnew = PyBytes_AsString(newfmt); |
|---|
| 1262 | n/a | usednew = 0; |
|---|
| 1263 | n/a | |
|---|
| 1264 | n/a | while ((ch = *pin++) != '\0') { |
|---|
| 1265 | n/a | if (ch != '%') { |
|---|
| 1266 | n/a | ptoappend = pin - 1; |
|---|
| 1267 | n/a | ntoappend = 1; |
|---|
| 1268 | n/a | } |
|---|
| 1269 | n/a | else if ((ch = *pin++) == '\0') { |
|---|
| 1270 | n/a | /* There's a lone trailing %; doesn't make sense. */ |
|---|
| 1271 | n/a | PyErr_SetString(PyExc_ValueError, "strftime format " |
|---|
| 1272 | n/a | "ends with raw %"); |
|---|
| 1273 | n/a | goto Done; |
|---|
| 1274 | n/a | } |
|---|
| 1275 | n/a | /* A % has been seen and ch is the character after it. */ |
|---|
| 1276 | n/a | else if (ch == 'z') { |
|---|
| 1277 | n/a | if (zreplacement == NULL) { |
|---|
| 1278 | n/a | /* format utcoffset */ |
|---|
| 1279 | n/a | char buf[100]; |
|---|
| 1280 | n/a | PyObject *tzinfo = get_tzinfo_member(object); |
|---|
| 1281 | n/a | zreplacement = PyBytes_FromStringAndSize("", 0); |
|---|
| 1282 | n/a | if (zreplacement == NULL) goto Done; |
|---|
| 1283 | n/a | if (tzinfo != Py_None && tzinfo != NULL) { |
|---|
| 1284 | n/a | assert(tzinfoarg != NULL); |
|---|
| 1285 | n/a | if (format_utcoffset(buf, |
|---|
| 1286 | n/a | sizeof(buf), |
|---|
| 1287 | n/a | "", |
|---|
| 1288 | n/a | tzinfo, |
|---|
| 1289 | n/a | tzinfoarg) < 0) |
|---|
| 1290 | n/a | goto Done; |
|---|
| 1291 | n/a | Py_DECREF(zreplacement); |
|---|
| 1292 | n/a | zreplacement = |
|---|
| 1293 | n/a | PyBytes_FromStringAndSize(buf, |
|---|
| 1294 | n/a | strlen(buf)); |
|---|
| 1295 | n/a | if (zreplacement == NULL) |
|---|
| 1296 | n/a | goto Done; |
|---|
| 1297 | n/a | } |
|---|
| 1298 | n/a | } |
|---|
| 1299 | n/a | assert(zreplacement != NULL); |
|---|
| 1300 | n/a | ptoappend = PyBytes_AS_STRING(zreplacement); |
|---|
| 1301 | n/a | ntoappend = PyBytes_GET_SIZE(zreplacement); |
|---|
| 1302 | n/a | } |
|---|
| 1303 | n/a | else if (ch == 'Z') { |
|---|
| 1304 | n/a | /* format tzname */ |
|---|
| 1305 | n/a | if (Zreplacement == NULL) { |
|---|
| 1306 | n/a | Zreplacement = make_Zreplacement(object, |
|---|
| 1307 | n/a | tzinfoarg); |
|---|
| 1308 | n/a | if (Zreplacement == NULL) |
|---|
| 1309 | n/a | goto Done; |
|---|
| 1310 | n/a | } |
|---|
| 1311 | n/a | assert(Zreplacement != NULL); |
|---|
| 1312 | n/a | assert(PyUnicode_Check(Zreplacement)); |
|---|
| 1313 | n/a | ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, |
|---|
| 1314 | n/a | &ntoappend); |
|---|
| 1315 | n/a | if (ptoappend == NULL) |
|---|
| 1316 | n/a | goto Done; |
|---|
| 1317 | n/a | } |
|---|
| 1318 | n/a | else if (ch == 'f') { |
|---|
| 1319 | n/a | /* format microseconds */ |
|---|
| 1320 | n/a | if (freplacement == NULL) { |
|---|
| 1321 | n/a | freplacement = make_freplacement(object); |
|---|
| 1322 | n/a | if (freplacement == NULL) |
|---|
| 1323 | n/a | goto Done; |
|---|
| 1324 | n/a | } |
|---|
| 1325 | n/a | assert(freplacement != NULL); |
|---|
| 1326 | n/a | assert(PyBytes_Check(freplacement)); |
|---|
| 1327 | n/a | ptoappend = PyBytes_AS_STRING(freplacement); |
|---|
| 1328 | n/a | ntoappend = PyBytes_GET_SIZE(freplacement); |
|---|
| 1329 | n/a | } |
|---|
| 1330 | n/a | else { |
|---|
| 1331 | n/a | /* percent followed by neither z nor Z */ |
|---|
| 1332 | n/a | ptoappend = pin - 2; |
|---|
| 1333 | n/a | ntoappend = 2; |
|---|
| 1334 | n/a | } |
|---|
| 1335 | n/a | |
|---|
| 1336 | n/a | /* Append the ntoappend chars starting at ptoappend to |
|---|
| 1337 | n/a | * the new format. |
|---|
| 1338 | n/a | */ |
|---|
| 1339 | n/a | if (ntoappend == 0) |
|---|
| 1340 | n/a | continue; |
|---|
| 1341 | n/a | assert(ptoappend != NULL); |
|---|
| 1342 | n/a | assert(ntoappend > 0); |
|---|
| 1343 | n/a | while (usednew + ntoappend > totalnew) { |
|---|
| 1344 | n/a | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
|---|
| 1345 | n/a | PyErr_NoMemory(); |
|---|
| 1346 | n/a | goto Done; |
|---|
| 1347 | n/a | } |
|---|
| 1348 | n/a | totalnew <<= 1; |
|---|
| 1349 | n/a | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
|---|
| 1350 | n/a | goto Done; |
|---|
| 1351 | n/a | pnew = PyBytes_AsString(newfmt) + usednew; |
|---|
| 1352 | n/a | } |
|---|
| 1353 | n/a | memcpy(pnew, ptoappend, ntoappend); |
|---|
| 1354 | n/a | pnew += ntoappend; |
|---|
| 1355 | n/a | usednew += ntoappend; |
|---|
| 1356 | n/a | assert(usednew <= totalnew); |
|---|
| 1357 | n/a | } /* end while() */ |
|---|
| 1358 | n/a | |
|---|
| 1359 | n/a | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
|---|
| 1360 | n/a | goto Done; |
|---|
| 1361 | n/a | { |
|---|
| 1362 | n/a | PyObject *format; |
|---|
| 1363 | n/a | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
|---|
| 1364 | n/a | |
|---|
| 1365 | n/a | if (time == NULL) |
|---|
| 1366 | n/a | goto Done; |
|---|
| 1367 | n/a | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
|---|
| 1368 | n/a | if (format != NULL) { |
|---|
| 1369 | n/a | result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime, |
|---|
| 1370 | n/a | format, timetuple, NULL); |
|---|
| 1371 | n/a | Py_DECREF(format); |
|---|
| 1372 | n/a | } |
|---|
| 1373 | n/a | Py_DECREF(time); |
|---|
| 1374 | n/a | } |
|---|
| 1375 | n/a | Done: |
|---|
| 1376 | n/a | Py_XDECREF(freplacement); |
|---|
| 1377 | n/a | Py_XDECREF(zreplacement); |
|---|
| 1378 | n/a | Py_XDECREF(Zreplacement); |
|---|
| 1379 | n/a | Py_XDECREF(newfmt); |
|---|
| 1380 | n/a | return result; |
|---|
| 1381 | n/a | } |
|---|
| 1382 | n/a | |
|---|
| 1383 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 1384 | n/a | * Wrap functions from the time module. These aren't directly available |
|---|
| 1385 | n/a | * from C. Perhaps they should be. |
|---|
| 1386 | n/a | */ |
|---|
| 1387 | n/a | |
|---|
| 1388 | n/a | /* Call time.time() and return its result (a Python float). */ |
|---|
| 1389 | n/a | static PyObject * |
|---|
| 1390 | n/a | time_time(void) |
|---|
| 1391 | n/a | { |
|---|
| 1392 | n/a | PyObject *result = NULL; |
|---|
| 1393 | n/a | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
|---|
| 1394 | n/a | |
|---|
| 1395 | n/a | if (time != NULL) { |
|---|
| 1396 | n/a | _Py_IDENTIFIER(time); |
|---|
| 1397 | n/a | |
|---|
| 1398 | n/a | result = _PyObject_CallMethodId(time, &PyId_time, NULL); |
|---|
| 1399 | n/a | Py_DECREF(time); |
|---|
| 1400 | n/a | } |
|---|
| 1401 | n/a | return result; |
|---|
| 1402 | n/a | } |
|---|
| 1403 | n/a | |
|---|
| 1404 | n/a | /* Build a time.struct_time. The weekday and day number are automatically |
|---|
| 1405 | n/a | * computed from the y,m,d args. |
|---|
| 1406 | n/a | */ |
|---|
| 1407 | n/a | static PyObject * |
|---|
| 1408 | n/a | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
|---|
| 1409 | n/a | { |
|---|
| 1410 | n/a | PyObject *time; |
|---|
| 1411 | n/a | PyObject *result; |
|---|
| 1412 | n/a | _Py_IDENTIFIER(struct_time); |
|---|
| 1413 | n/a | PyObject *args; |
|---|
| 1414 | n/a | |
|---|
| 1415 | n/a | |
|---|
| 1416 | n/a | time = PyImport_ImportModuleNoBlock("time"); |
|---|
| 1417 | n/a | if (time == NULL) { |
|---|
| 1418 | n/a | return NULL; |
|---|
| 1419 | n/a | } |
|---|
| 1420 | n/a | |
|---|
| 1421 | n/a | args = Py_BuildValue("iiiiiiiii", |
|---|
| 1422 | n/a | y, m, d, |
|---|
| 1423 | n/a | hh, mm, ss, |
|---|
| 1424 | n/a | weekday(y, m, d), |
|---|
| 1425 | n/a | days_before_month(y, m) + d, |
|---|
| 1426 | n/a | dstflag); |
|---|
| 1427 | n/a | if (args == NULL) { |
|---|
| 1428 | n/a | Py_DECREF(time); |
|---|
| 1429 | n/a | return NULL; |
|---|
| 1430 | n/a | } |
|---|
| 1431 | n/a | |
|---|
| 1432 | n/a | result = _PyObject_CallMethodIdObjArgs(time, &PyId_struct_time, |
|---|
| 1433 | n/a | args, NULL); |
|---|
| 1434 | n/a | Py_DECREF(time); |
|---|
| 1435 | n/a | Py_DECREF(args); |
|---|
| 1436 | n/a | return result; |
|---|
| 1437 | n/a | } |
|---|
| 1438 | n/a | |
|---|
| 1439 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 1440 | n/a | * Miscellaneous helpers. |
|---|
| 1441 | n/a | */ |
|---|
| 1442 | n/a | |
|---|
| 1443 | n/a | /* For various reasons, we need to use tp_richcompare instead of tp_reserved. |
|---|
| 1444 | n/a | * The comparisons here all most naturally compute a cmp()-like result. |
|---|
| 1445 | n/a | * This little helper turns that into a bool result for rich comparisons. |
|---|
| 1446 | n/a | */ |
|---|
| 1447 | n/a | static PyObject * |
|---|
| 1448 | n/a | diff_to_bool(int diff, int op) |
|---|
| 1449 | n/a | { |
|---|
| 1450 | n/a | PyObject *result; |
|---|
| 1451 | n/a | int istrue; |
|---|
| 1452 | n/a | |
|---|
| 1453 | n/a | switch (op) { |
|---|
| 1454 | n/a | case Py_EQ: istrue = diff == 0; break; |
|---|
| 1455 | n/a | case Py_NE: istrue = diff != 0; break; |
|---|
| 1456 | n/a | case Py_LE: istrue = diff <= 0; break; |
|---|
| 1457 | n/a | case Py_GE: istrue = diff >= 0; break; |
|---|
| 1458 | n/a | case Py_LT: istrue = diff < 0; break; |
|---|
| 1459 | n/a | case Py_GT: istrue = diff > 0; break; |
|---|
| 1460 | n/a | default: |
|---|
| 1461 | n/a | assert(! "op unknown"); |
|---|
| 1462 | n/a | istrue = 0; /* To shut up compiler */ |
|---|
| 1463 | n/a | } |
|---|
| 1464 | n/a | result = istrue ? Py_True : Py_False; |
|---|
| 1465 | n/a | Py_INCREF(result); |
|---|
| 1466 | n/a | return result; |
|---|
| 1467 | n/a | } |
|---|
| 1468 | n/a | |
|---|
| 1469 | n/a | /* Raises a "can't compare" TypeError and returns NULL. */ |
|---|
| 1470 | n/a | static PyObject * |
|---|
| 1471 | n/a | cmperror(PyObject *a, PyObject *b) |
|---|
| 1472 | n/a | { |
|---|
| 1473 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1474 | n/a | "can't compare %s to %s", |
|---|
| 1475 | n/a | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
|---|
| 1476 | n/a | return NULL; |
|---|
| 1477 | n/a | } |
|---|
| 1478 | n/a | |
|---|
| 1479 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 1480 | n/a | * Cached Python objects; these are set by the module init function. |
|---|
| 1481 | n/a | */ |
|---|
| 1482 | n/a | |
|---|
| 1483 | n/a | /* Conversion factors. */ |
|---|
| 1484 | n/a | static PyObject *one = NULL; /* 1 */ |
|---|
| 1485 | n/a | static PyObject *us_per_ms = NULL; /* 1000 */ |
|---|
| 1486 | n/a | static PyObject *us_per_second = NULL; /* 1000000 */ |
|---|
| 1487 | n/a | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
|---|
| 1488 | n/a | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
|---|
| 1489 | n/a | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
|---|
| 1490 | n/a | static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python int */ |
|---|
| 1491 | n/a | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
|---|
| 1492 | n/a | |
|---|
| 1493 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 1494 | n/a | * Class implementations. |
|---|
| 1495 | n/a | */ |
|---|
| 1496 | n/a | |
|---|
| 1497 | n/a | /* |
|---|
| 1498 | n/a | * PyDateTime_Delta implementation. |
|---|
| 1499 | n/a | */ |
|---|
| 1500 | n/a | |
|---|
| 1501 | n/a | /* Convert a timedelta to a number of us, |
|---|
| 1502 | n/a | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
|---|
| 1503 | n/a | * as a Python int. |
|---|
| 1504 | n/a | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
|---|
| 1505 | n/a | * due to ubiquitous overflow possibilities. |
|---|
| 1506 | n/a | */ |
|---|
| 1507 | n/a | static PyObject * |
|---|
| 1508 | n/a | delta_to_microseconds(PyDateTime_Delta *self) |
|---|
| 1509 | n/a | { |
|---|
| 1510 | n/a | PyObject *x1 = NULL; |
|---|
| 1511 | n/a | PyObject *x2 = NULL; |
|---|
| 1512 | n/a | PyObject *x3 = NULL; |
|---|
| 1513 | n/a | PyObject *result = NULL; |
|---|
| 1514 | n/a | |
|---|
| 1515 | n/a | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
|---|
| 1516 | n/a | if (x1 == NULL) |
|---|
| 1517 | n/a | goto Done; |
|---|
| 1518 | n/a | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
|---|
| 1519 | n/a | if (x2 == NULL) |
|---|
| 1520 | n/a | goto Done; |
|---|
| 1521 | n/a | Py_DECREF(x1); |
|---|
| 1522 | n/a | x1 = NULL; |
|---|
| 1523 | n/a | |
|---|
| 1524 | n/a | /* x2 has days in seconds */ |
|---|
| 1525 | n/a | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
|---|
| 1526 | n/a | if (x1 == NULL) |
|---|
| 1527 | n/a | goto Done; |
|---|
| 1528 | n/a | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
|---|
| 1529 | n/a | if (x3 == NULL) |
|---|
| 1530 | n/a | goto Done; |
|---|
| 1531 | n/a | Py_DECREF(x1); |
|---|
| 1532 | n/a | Py_DECREF(x2); |
|---|
| 1533 | n/a | /* x1 = */ x2 = NULL; |
|---|
| 1534 | n/a | |
|---|
| 1535 | n/a | /* x3 has days+seconds in seconds */ |
|---|
| 1536 | n/a | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
|---|
| 1537 | n/a | if (x1 == NULL) |
|---|
| 1538 | n/a | goto Done; |
|---|
| 1539 | n/a | Py_DECREF(x3); |
|---|
| 1540 | n/a | x3 = NULL; |
|---|
| 1541 | n/a | |
|---|
| 1542 | n/a | /* x1 has days+seconds in us */ |
|---|
| 1543 | n/a | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
|---|
| 1544 | n/a | if (x2 == NULL) |
|---|
| 1545 | n/a | goto Done; |
|---|
| 1546 | n/a | result = PyNumber_Add(x1, x2); |
|---|
| 1547 | n/a | |
|---|
| 1548 | n/a | Done: |
|---|
| 1549 | n/a | Py_XDECREF(x1); |
|---|
| 1550 | n/a | Py_XDECREF(x2); |
|---|
| 1551 | n/a | Py_XDECREF(x3); |
|---|
| 1552 | n/a | return result; |
|---|
| 1553 | n/a | } |
|---|
| 1554 | n/a | |
|---|
| 1555 | n/a | /* Convert a number of us (as a Python int) to a timedelta. |
|---|
| 1556 | n/a | */ |
|---|
| 1557 | n/a | static PyObject * |
|---|
| 1558 | n/a | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
|---|
| 1559 | n/a | { |
|---|
| 1560 | n/a | int us; |
|---|
| 1561 | n/a | int s; |
|---|
| 1562 | n/a | int d; |
|---|
| 1563 | n/a | long temp; |
|---|
| 1564 | n/a | |
|---|
| 1565 | n/a | PyObject *tuple = NULL; |
|---|
| 1566 | n/a | PyObject *num = NULL; |
|---|
| 1567 | n/a | PyObject *result = NULL; |
|---|
| 1568 | n/a | |
|---|
| 1569 | n/a | tuple = PyNumber_Divmod(pyus, us_per_second); |
|---|
| 1570 | n/a | if (tuple == NULL) |
|---|
| 1571 | n/a | goto Done; |
|---|
| 1572 | n/a | |
|---|
| 1573 | n/a | num = PyTuple_GetItem(tuple, 1); /* us */ |
|---|
| 1574 | n/a | if (num == NULL) |
|---|
| 1575 | n/a | goto Done; |
|---|
| 1576 | n/a | temp = PyLong_AsLong(num); |
|---|
| 1577 | n/a | num = NULL; |
|---|
| 1578 | n/a | if (temp == -1 && PyErr_Occurred()) |
|---|
| 1579 | n/a | goto Done; |
|---|
| 1580 | n/a | assert(0 <= temp && temp < 1000000); |
|---|
| 1581 | n/a | us = (int)temp; |
|---|
| 1582 | n/a | if (us < 0) { |
|---|
| 1583 | n/a | /* The divisor was positive, so this must be an error. */ |
|---|
| 1584 | n/a | assert(PyErr_Occurred()); |
|---|
| 1585 | n/a | goto Done; |
|---|
| 1586 | n/a | } |
|---|
| 1587 | n/a | |
|---|
| 1588 | n/a | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
|---|
| 1589 | n/a | if (num == NULL) |
|---|
| 1590 | n/a | goto Done; |
|---|
| 1591 | n/a | Py_INCREF(num); |
|---|
| 1592 | n/a | Py_DECREF(tuple); |
|---|
| 1593 | n/a | |
|---|
| 1594 | n/a | tuple = PyNumber_Divmod(num, seconds_per_day); |
|---|
| 1595 | n/a | if (tuple == NULL) |
|---|
| 1596 | n/a | goto Done; |
|---|
| 1597 | n/a | Py_DECREF(num); |
|---|
| 1598 | n/a | |
|---|
| 1599 | n/a | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
|---|
| 1600 | n/a | if (num == NULL) |
|---|
| 1601 | n/a | goto Done; |
|---|
| 1602 | n/a | temp = PyLong_AsLong(num); |
|---|
| 1603 | n/a | num = NULL; |
|---|
| 1604 | n/a | if (temp == -1 && PyErr_Occurred()) |
|---|
| 1605 | n/a | goto Done; |
|---|
| 1606 | n/a | assert(0 <= temp && temp < 24*3600); |
|---|
| 1607 | n/a | s = (int)temp; |
|---|
| 1608 | n/a | |
|---|
| 1609 | n/a | if (s < 0) { |
|---|
| 1610 | n/a | /* The divisor was positive, so this must be an error. */ |
|---|
| 1611 | n/a | assert(PyErr_Occurred()); |
|---|
| 1612 | n/a | goto Done; |
|---|
| 1613 | n/a | } |
|---|
| 1614 | n/a | |
|---|
| 1615 | n/a | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
|---|
| 1616 | n/a | if (num == NULL) |
|---|
| 1617 | n/a | goto Done; |
|---|
| 1618 | n/a | Py_INCREF(num); |
|---|
| 1619 | n/a | temp = PyLong_AsLong(num); |
|---|
| 1620 | n/a | if (temp == -1 && PyErr_Occurred()) |
|---|
| 1621 | n/a | goto Done; |
|---|
| 1622 | n/a | d = (int)temp; |
|---|
| 1623 | n/a | if ((long)d != temp) { |
|---|
| 1624 | n/a | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
|---|
| 1625 | n/a | "large to fit in a C int"); |
|---|
| 1626 | n/a | goto Done; |
|---|
| 1627 | n/a | } |
|---|
| 1628 | n/a | result = new_delta_ex(d, s, us, 0, type); |
|---|
| 1629 | n/a | |
|---|
| 1630 | n/a | Done: |
|---|
| 1631 | n/a | Py_XDECREF(tuple); |
|---|
| 1632 | n/a | Py_XDECREF(num); |
|---|
| 1633 | n/a | return result; |
|---|
| 1634 | n/a | } |
|---|
| 1635 | n/a | |
|---|
| 1636 | n/a | #define microseconds_to_delta(pymicros) \ |
|---|
| 1637 | n/a | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
|---|
| 1638 | n/a | |
|---|
| 1639 | n/a | static PyObject * |
|---|
| 1640 | n/a | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
|---|
| 1641 | n/a | { |
|---|
| 1642 | n/a | PyObject *pyus_in; |
|---|
| 1643 | n/a | PyObject *pyus_out; |
|---|
| 1644 | n/a | PyObject *result; |
|---|
| 1645 | n/a | |
|---|
| 1646 | n/a | pyus_in = delta_to_microseconds(delta); |
|---|
| 1647 | n/a | if (pyus_in == NULL) |
|---|
| 1648 | n/a | return NULL; |
|---|
| 1649 | n/a | |
|---|
| 1650 | n/a | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
|---|
| 1651 | n/a | Py_DECREF(pyus_in); |
|---|
| 1652 | n/a | if (pyus_out == NULL) |
|---|
| 1653 | n/a | return NULL; |
|---|
| 1654 | n/a | |
|---|
| 1655 | n/a | result = microseconds_to_delta(pyus_out); |
|---|
| 1656 | n/a | Py_DECREF(pyus_out); |
|---|
| 1657 | n/a | return result; |
|---|
| 1658 | n/a | } |
|---|
| 1659 | n/a | |
|---|
| 1660 | n/a | static PyObject * |
|---|
| 1661 | n/a | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) |
|---|
| 1662 | n/a | { |
|---|
| 1663 | n/a | PyObject *result = NULL; |
|---|
| 1664 | n/a | PyObject *pyus_in = NULL, *temp, *pyus_out; |
|---|
| 1665 | n/a | PyObject *ratio = NULL; |
|---|
| 1666 | n/a | |
|---|
| 1667 | n/a | pyus_in = delta_to_microseconds(delta); |
|---|
| 1668 | n/a | if (pyus_in == NULL) |
|---|
| 1669 | n/a | return NULL; |
|---|
| 1670 | n/a | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
|---|
| 1671 | n/a | if (ratio == NULL) |
|---|
| 1672 | n/a | goto error; |
|---|
| 1673 | n/a | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); |
|---|
| 1674 | n/a | Py_DECREF(pyus_in); |
|---|
| 1675 | n/a | pyus_in = NULL; |
|---|
| 1676 | n/a | if (temp == NULL) |
|---|
| 1677 | n/a | goto error; |
|---|
| 1678 | n/a | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); |
|---|
| 1679 | n/a | Py_DECREF(temp); |
|---|
| 1680 | n/a | if (pyus_out == NULL) |
|---|
| 1681 | n/a | goto error; |
|---|
| 1682 | n/a | result = microseconds_to_delta(pyus_out); |
|---|
| 1683 | n/a | Py_DECREF(pyus_out); |
|---|
| 1684 | n/a | error: |
|---|
| 1685 | n/a | Py_XDECREF(pyus_in); |
|---|
| 1686 | n/a | Py_XDECREF(ratio); |
|---|
| 1687 | n/a | |
|---|
| 1688 | n/a | return result; |
|---|
| 1689 | n/a | } |
|---|
| 1690 | n/a | |
|---|
| 1691 | n/a | static PyObject * |
|---|
| 1692 | n/a | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
|---|
| 1693 | n/a | { |
|---|
| 1694 | n/a | PyObject *pyus_in; |
|---|
| 1695 | n/a | PyObject *pyus_out; |
|---|
| 1696 | n/a | PyObject *result; |
|---|
| 1697 | n/a | |
|---|
| 1698 | n/a | pyus_in = delta_to_microseconds(delta); |
|---|
| 1699 | n/a | if (pyus_in == NULL) |
|---|
| 1700 | n/a | return NULL; |
|---|
| 1701 | n/a | |
|---|
| 1702 | n/a | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
|---|
| 1703 | n/a | Py_DECREF(pyus_in); |
|---|
| 1704 | n/a | if (pyus_out == NULL) |
|---|
| 1705 | n/a | return NULL; |
|---|
| 1706 | n/a | |
|---|
| 1707 | n/a | result = microseconds_to_delta(pyus_out); |
|---|
| 1708 | n/a | Py_DECREF(pyus_out); |
|---|
| 1709 | n/a | return result; |
|---|
| 1710 | n/a | } |
|---|
| 1711 | n/a | |
|---|
| 1712 | n/a | static PyObject * |
|---|
| 1713 | n/a | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
|---|
| 1714 | n/a | { |
|---|
| 1715 | n/a | PyObject *pyus_left; |
|---|
| 1716 | n/a | PyObject *pyus_right; |
|---|
| 1717 | n/a | PyObject *result; |
|---|
| 1718 | n/a | |
|---|
| 1719 | n/a | pyus_left = delta_to_microseconds(left); |
|---|
| 1720 | n/a | if (pyus_left == NULL) |
|---|
| 1721 | n/a | return NULL; |
|---|
| 1722 | n/a | |
|---|
| 1723 | n/a | pyus_right = delta_to_microseconds(right); |
|---|
| 1724 | n/a | if (pyus_right == NULL) { |
|---|
| 1725 | n/a | Py_DECREF(pyus_left); |
|---|
| 1726 | n/a | return NULL; |
|---|
| 1727 | n/a | } |
|---|
| 1728 | n/a | |
|---|
| 1729 | n/a | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
|---|
| 1730 | n/a | Py_DECREF(pyus_left); |
|---|
| 1731 | n/a | Py_DECREF(pyus_right); |
|---|
| 1732 | n/a | return result; |
|---|
| 1733 | n/a | } |
|---|
| 1734 | n/a | |
|---|
| 1735 | n/a | static PyObject * |
|---|
| 1736 | n/a | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
|---|
| 1737 | n/a | { |
|---|
| 1738 | n/a | PyObject *pyus_left; |
|---|
| 1739 | n/a | PyObject *pyus_right; |
|---|
| 1740 | n/a | PyObject *result; |
|---|
| 1741 | n/a | |
|---|
| 1742 | n/a | pyus_left = delta_to_microseconds(left); |
|---|
| 1743 | n/a | if (pyus_left == NULL) |
|---|
| 1744 | n/a | return NULL; |
|---|
| 1745 | n/a | |
|---|
| 1746 | n/a | pyus_right = delta_to_microseconds(right); |
|---|
| 1747 | n/a | if (pyus_right == NULL) { |
|---|
| 1748 | n/a | Py_DECREF(pyus_left); |
|---|
| 1749 | n/a | return NULL; |
|---|
| 1750 | n/a | } |
|---|
| 1751 | n/a | |
|---|
| 1752 | n/a | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
|---|
| 1753 | n/a | Py_DECREF(pyus_left); |
|---|
| 1754 | n/a | Py_DECREF(pyus_right); |
|---|
| 1755 | n/a | return result; |
|---|
| 1756 | n/a | } |
|---|
| 1757 | n/a | |
|---|
| 1758 | n/a | static PyObject * |
|---|
| 1759 | n/a | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) |
|---|
| 1760 | n/a | { |
|---|
| 1761 | n/a | PyObject *result = NULL; |
|---|
| 1762 | n/a | PyObject *pyus_in = NULL, *temp, *pyus_out; |
|---|
| 1763 | n/a | PyObject *ratio = NULL; |
|---|
| 1764 | n/a | |
|---|
| 1765 | n/a | pyus_in = delta_to_microseconds(delta); |
|---|
| 1766 | n/a | if (pyus_in == NULL) |
|---|
| 1767 | n/a | return NULL; |
|---|
| 1768 | n/a | ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); |
|---|
| 1769 | n/a | if (ratio == NULL) |
|---|
| 1770 | n/a | goto error; |
|---|
| 1771 | n/a | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1)); |
|---|
| 1772 | n/a | Py_DECREF(pyus_in); |
|---|
| 1773 | n/a | pyus_in = NULL; |
|---|
| 1774 | n/a | if (temp == NULL) |
|---|
| 1775 | n/a | goto error; |
|---|
| 1776 | n/a | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0)); |
|---|
| 1777 | n/a | Py_DECREF(temp); |
|---|
| 1778 | n/a | if (pyus_out == NULL) |
|---|
| 1779 | n/a | goto error; |
|---|
| 1780 | n/a | result = microseconds_to_delta(pyus_out); |
|---|
| 1781 | n/a | Py_DECREF(pyus_out); |
|---|
| 1782 | n/a | error: |
|---|
| 1783 | n/a | Py_XDECREF(pyus_in); |
|---|
| 1784 | n/a | Py_XDECREF(ratio); |
|---|
| 1785 | n/a | |
|---|
| 1786 | n/a | return result; |
|---|
| 1787 | n/a | } |
|---|
| 1788 | n/a | |
|---|
| 1789 | n/a | static PyObject * |
|---|
| 1790 | n/a | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
|---|
| 1791 | n/a | { |
|---|
| 1792 | n/a | PyObject *result; |
|---|
| 1793 | n/a | PyObject *pyus_in, *pyus_out; |
|---|
| 1794 | n/a | pyus_in = delta_to_microseconds(delta); |
|---|
| 1795 | n/a | if (pyus_in == NULL) |
|---|
| 1796 | n/a | return NULL; |
|---|
| 1797 | n/a | pyus_out = divide_nearest(pyus_in, i); |
|---|
| 1798 | n/a | Py_DECREF(pyus_in); |
|---|
| 1799 | n/a | if (pyus_out == NULL) |
|---|
| 1800 | n/a | return NULL; |
|---|
| 1801 | n/a | result = microseconds_to_delta(pyus_out); |
|---|
| 1802 | n/a | Py_DECREF(pyus_out); |
|---|
| 1803 | n/a | |
|---|
| 1804 | n/a | return result; |
|---|
| 1805 | n/a | } |
|---|
| 1806 | n/a | |
|---|
| 1807 | n/a | static PyObject * |
|---|
| 1808 | n/a | delta_add(PyObject *left, PyObject *right) |
|---|
| 1809 | n/a | { |
|---|
| 1810 | n/a | PyObject *result = Py_NotImplemented; |
|---|
| 1811 | n/a | |
|---|
| 1812 | n/a | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
|---|
| 1813 | n/a | /* delta + delta */ |
|---|
| 1814 | n/a | /* The C-level additions can't overflow because of the |
|---|
| 1815 | n/a | * invariant bounds. |
|---|
| 1816 | n/a | */ |
|---|
| 1817 | n/a | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
|---|
| 1818 | n/a | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
|---|
| 1819 | n/a | int microseconds = GET_TD_MICROSECONDS(left) + |
|---|
| 1820 | n/a | GET_TD_MICROSECONDS(right); |
|---|
| 1821 | n/a | result = new_delta(days, seconds, microseconds, 1); |
|---|
| 1822 | n/a | } |
|---|
| 1823 | n/a | |
|---|
| 1824 | n/a | if (result == Py_NotImplemented) |
|---|
| 1825 | n/a | Py_INCREF(result); |
|---|
| 1826 | n/a | return result; |
|---|
| 1827 | n/a | } |
|---|
| 1828 | n/a | |
|---|
| 1829 | n/a | static PyObject * |
|---|
| 1830 | n/a | delta_negative(PyDateTime_Delta *self) |
|---|
| 1831 | n/a | { |
|---|
| 1832 | n/a | return new_delta(-GET_TD_DAYS(self), |
|---|
| 1833 | n/a | -GET_TD_SECONDS(self), |
|---|
| 1834 | n/a | -GET_TD_MICROSECONDS(self), |
|---|
| 1835 | n/a | 1); |
|---|
| 1836 | n/a | } |
|---|
| 1837 | n/a | |
|---|
| 1838 | n/a | static PyObject * |
|---|
| 1839 | n/a | delta_positive(PyDateTime_Delta *self) |
|---|
| 1840 | n/a | { |
|---|
| 1841 | n/a | /* Could optimize this (by returning self) if this isn't a |
|---|
| 1842 | n/a | * subclass -- but who uses unary + ? Approximately nobody. |
|---|
| 1843 | n/a | */ |
|---|
| 1844 | n/a | return new_delta(GET_TD_DAYS(self), |
|---|
| 1845 | n/a | GET_TD_SECONDS(self), |
|---|
| 1846 | n/a | GET_TD_MICROSECONDS(self), |
|---|
| 1847 | n/a | 0); |
|---|
| 1848 | n/a | } |
|---|
| 1849 | n/a | |
|---|
| 1850 | n/a | static PyObject * |
|---|
| 1851 | n/a | delta_abs(PyDateTime_Delta *self) |
|---|
| 1852 | n/a | { |
|---|
| 1853 | n/a | PyObject *result; |
|---|
| 1854 | n/a | |
|---|
| 1855 | n/a | assert(GET_TD_MICROSECONDS(self) >= 0); |
|---|
| 1856 | n/a | assert(GET_TD_SECONDS(self) >= 0); |
|---|
| 1857 | n/a | |
|---|
| 1858 | n/a | if (GET_TD_DAYS(self) < 0) |
|---|
| 1859 | n/a | result = delta_negative(self); |
|---|
| 1860 | n/a | else |
|---|
| 1861 | n/a | result = delta_positive(self); |
|---|
| 1862 | n/a | |
|---|
| 1863 | n/a | return result; |
|---|
| 1864 | n/a | } |
|---|
| 1865 | n/a | |
|---|
| 1866 | n/a | static PyObject * |
|---|
| 1867 | n/a | delta_subtract(PyObject *left, PyObject *right) |
|---|
| 1868 | n/a | { |
|---|
| 1869 | n/a | PyObject *result = Py_NotImplemented; |
|---|
| 1870 | n/a | |
|---|
| 1871 | n/a | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
|---|
| 1872 | n/a | /* delta - delta */ |
|---|
| 1873 | n/a | /* The C-level additions can't overflow because of the |
|---|
| 1874 | n/a | * invariant bounds. |
|---|
| 1875 | n/a | */ |
|---|
| 1876 | n/a | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
|---|
| 1877 | n/a | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
|---|
| 1878 | n/a | int microseconds = GET_TD_MICROSECONDS(left) - |
|---|
| 1879 | n/a | GET_TD_MICROSECONDS(right); |
|---|
| 1880 | n/a | result = new_delta(days, seconds, microseconds, 1); |
|---|
| 1881 | n/a | } |
|---|
| 1882 | n/a | |
|---|
| 1883 | n/a | if (result == Py_NotImplemented) |
|---|
| 1884 | n/a | Py_INCREF(result); |
|---|
| 1885 | n/a | return result; |
|---|
| 1886 | n/a | } |
|---|
| 1887 | n/a | |
|---|
| 1888 | n/a | static int |
|---|
| 1889 | n/a | delta_cmp(PyObject *self, PyObject *other) |
|---|
| 1890 | n/a | { |
|---|
| 1891 | n/a | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
|---|
| 1892 | n/a | if (diff == 0) { |
|---|
| 1893 | n/a | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
|---|
| 1894 | n/a | if (diff == 0) |
|---|
| 1895 | n/a | diff = GET_TD_MICROSECONDS(self) - |
|---|
| 1896 | n/a | GET_TD_MICROSECONDS(other); |
|---|
| 1897 | n/a | } |
|---|
| 1898 | n/a | return diff; |
|---|
| 1899 | n/a | } |
|---|
| 1900 | n/a | |
|---|
| 1901 | n/a | static PyObject * |
|---|
| 1902 | n/a | delta_richcompare(PyObject *self, PyObject *other, int op) |
|---|
| 1903 | n/a | { |
|---|
| 1904 | n/a | if (PyDelta_Check(other)) { |
|---|
| 1905 | n/a | int diff = delta_cmp(self, other); |
|---|
| 1906 | n/a | return diff_to_bool(diff, op); |
|---|
| 1907 | n/a | } |
|---|
| 1908 | n/a | else { |
|---|
| 1909 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 1910 | n/a | } |
|---|
| 1911 | n/a | } |
|---|
| 1912 | n/a | |
|---|
| 1913 | n/a | static PyObject *delta_getstate(PyDateTime_Delta *self); |
|---|
| 1914 | n/a | |
|---|
| 1915 | n/a | static Py_hash_t |
|---|
| 1916 | n/a | delta_hash(PyDateTime_Delta *self) |
|---|
| 1917 | n/a | { |
|---|
| 1918 | n/a | if (self->hashcode == -1) { |
|---|
| 1919 | n/a | PyObject *temp = delta_getstate(self); |
|---|
| 1920 | n/a | if (temp != NULL) { |
|---|
| 1921 | n/a | self->hashcode = PyObject_Hash(temp); |
|---|
| 1922 | n/a | Py_DECREF(temp); |
|---|
| 1923 | n/a | } |
|---|
| 1924 | n/a | } |
|---|
| 1925 | n/a | return self->hashcode; |
|---|
| 1926 | n/a | } |
|---|
| 1927 | n/a | |
|---|
| 1928 | n/a | static PyObject * |
|---|
| 1929 | n/a | delta_multiply(PyObject *left, PyObject *right) |
|---|
| 1930 | n/a | { |
|---|
| 1931 | n/a | PyObject *result = Py_NotImplemented; |
|---|
| 1932 | n/a | |
|---|
| 1933 | n/a | if (PyDelta_Check(left)) { |
|---|
| 1934 | n/a | /* delta * ??? */ |
|---|
| 1935 | n/a | if (PyLong_Check(right)) |
|---|
| 1936 | n/a | result = multiply_int_timedelta(right, |
|---|
| 1937 | n/a | (PyDateTime_Delta *) left); |
|---|
| 1938 | n/a | else if (PyFloat_Check(right)) |
|---|
| 1939 | n/a | result = multiply_float_timedelta(right, |
|---|
| 1940 | n/a | (PyDateTime_Delta *) left); |
|---|
| 1941 | n/a | } |
|---|
| 1942 | n/a | else if (PyLong_Check(left)) |
|---|
| 1943 | n/a | result = multiply_int_timedelta(left, |
|---|
| 1944 | n/a | (PyDateTime_Delta *) right); |
|---|
| 1945 | n/a | else if (PyFloat_Check(left)) |
|---|
| 1946 | n/a | result = multiply_float_timedelta(left, |
|---|
| 1947 | n/a | (PyDateTime_Delta *) right); |
|---|
| 1948 | n/a | |
|---|
| 1949 | n/a | if (result == Py_NotImplemented) |
|---|
| 1950 | n/a | Py_INCREF(result); |
|---|
| 1951 | n/a | return result; |
|---|
| 1952 | n/a | } |
|---|
| 1953 | n/a | |
|---|
| 1954 | n/a | static PyObject * |
|---|
| 1955 | n/a | delta_divide(PyObject *left, PyObject *right) |
|---|
| 1956 | n/a | { |
|---|
| 1957 | n/a | PyObject *result = Py_NotImplemented; |
|---|
| 1958 | n/a | |
|---|
| 1959 | n/a | if (PyDelta_Check(left)) { |
|---|
| 1960 | n/a | /* delta * ??? */ |
|---|
| 1961 | n/a | if (PyLong_Check(right)) |
|---|
| 1962 | n/a | result = divide_timedelta_int( |
|---|
| 1963 | n/a | (PyDateTime_Delta *)left, |
|---|
| 1964 | n/a | right); |
|---|
| 1965 | n/a | else if (PyDelta_Check(right)) |
|---|
| 1966 | n/a | result = divide_timedelta_timedelta( |
|---|
| 1967 | n/a | (PyDateTime_Delta *)left, |
|---|
| 1968 | n/a | (PyDateTime_Delta *)right); |
|---|
| 1969 | n/a | } |
|---|
| 1970 | n/a | |
|---|
| 1971 | n/a | if (result == Py_NotImplemented) |
|---|
| 1972 | n/a | Py_INCREF(result); |
|---|
| 1973 | n/a | return result; |
|---|
| 1974 | n/a | } |
|---|
| 1975 | n/a | |
|---|
| 1976 | n/a | static PyObject * |
|---|
| 1977 | n/a | delta_truedivide(PyObject *left, PyObject *right) |
|---|
| 1978 | n/a | { |
|---|
| 1979 | n/a | PyObject *result = Py_NotImplemented; |
|---|
| 1980 | n/a | |
|---|
| 1981 | n/a | if (PyDelta_Check(left)) { |
|---|
| 1982 | n/a | if (PyDelta_Check(right)) |
|---|
| 1983 | n/a | result = truedivide_timedelta_timedelta( |
|---|
| 1984 | n/a | (PyDateTime_Delta *)left, |
|---|
| 1985 | n/a | (PyDateTime_Delta *)right); |
|---|
| 1986 | n/a | else if (PyFloat_Check(right)) |
|---|
| 1987 | n/a | result = truedivide_timedelta_float( |
|---|
| 1988 | n/a | (PyDateTime_Delta *)left, right); |
|---|
| 1989 | n/a | else if (PyLong_Check(right)) |
|---|
| 1990 | n/a | result = truedivide_timedelta_int( |
|---|
| 1991 | n/a | (PyDateTime_Delta *)left, right); |
|---|
| 1992 | n/a | } |
|---|
| 1993 | n/a | |
|---|
| 1994 | n/a | if (result == Py_NotImplemented) |
|---|
| 1995 | n/a | Py_INCREF(result); |
|---|
| 1996 | n/a | return result; |
|---|
| 1997 | n/a | } |
|---|
| 1998 | n/a | |
|---|
| 1999 | n/a | static PyObject * |
|---|
| 2000 | n/a | delta_remainder(PyObject *left, PyObject *right) |
|---|
| 2001 | n/a | { |
|---|
| 2002 | n/a | PyObject *pyus_left; |
|---|
| 2003 | n/a | PyObject *pyus_right; |
|---|
| 2004 | n/a | PyObject *pyus_remainder; |
|---|
| 2005 | n/a | PyObject *remainder; |
|---|
| 2006 | n/a | |
|---|
| 2007 | n/a | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
|---|
| 2008 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 2009 | n/a | |
|---|
| 2010 | n/a | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
|---|
| 2011 | n/a | if (pyus_left == NULL) |
|---|
| 2012 | n/a | return NULL; |
|---|
| 2013 | n/a | |
|---|
| 2014 | n/a | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
|---|
| 2015 | n/a | if (pyus_right == NULL) { |
|---|
| 2016 | n/a | Py_DECREF(pyus_left); |
|---|
| 2017 | n/a | return NULL; |
|---|
| 2018 | n/a | } |
|---|
| 2019 | n/a | |
|---|
| 2020 | n/a | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
|---|
| 2021 | n/a | Py_DECREF(pyus_left); |
|---|
| 2022 | n/a | Py_DECREF(pyus_right); |
|---|
| 2023 | n/a | if (pyus_remainder == NULL) |
|---|
| 2024 | n/a | return NULL; |
|---|
| 2025 | n/a | |
|---|
| 2026 | n/a | remainder = microseconds_to_delta(pyus_remainder); |
|---|
| 2027 | n/a | Py_DECREF(pyus_remainder); |
|---|
| 2028 | n/a | if (remainder == NULL) |
|---|
| 2029 | n/a | return NULL; |
|---|
| 2030 | n/a | |
|---|
| 2031 | n/a | return remainder; |
|---|
| 2032 | n/a | } |
|---|
| 2033 | n/a | |
|---|
| 2034 | n/a | static PyObject * |
|---|
| 2035 | n/a | delta_divmod(PyObject *left, PyObject *right) |
|---|
| 2036 | n/a | { |
|---|
| 2037 | n/a | PyObject *pyus_left; |
|---|
| 2038 | n/a | PyObject *pyus_right; |
|---|
| 2039 | n/a | PyObject *divmod; |
|---|
| 2040 | n/a | PyObject *delta; |
|---|
| 2041 | n/a | PyObject *result; |
|---|
| 2042 | n/a | |
|---|
| 2043 | n/a | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
|---|
| 2044 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 2045 | n/a | |
|---|
| 2046 | n/a | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
|---|
| 2047 | n/a | if (pyus_left == NULL) |
|---|
| 2048 | n/a | return NULL; |
|---|
| 2049 | n/a | |
|---|
| 2050 | n/a | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
|---|
| 2051 | n/a | if (pyus_right == NULL) { |
|---|
| 2052 | n/a | Py_DECREF(pyus_left); |
|---|
| 2053 | n/a | return NULL; |
|---|
| 2054 | n/a | } |
|---|
| 2055 | n/a | |
|---|
| 2056 | n/a | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
|---|
| 2057 | n/a | Py_DECREF(pyus_left); |
|---|
| 2058 | n/a | Py_DECREF(pyus_right); |
|---|
| 2059 | n/a | if (divmod == NULL) |
|---|
| 2060 | n/a | return NULL; |
|---|
| 2061 | n/a | |
|---|
| 2062 | n/a | assert(PyTuple_Size(divmod) == 2); |
|---|
| 2063 | n/a | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
|---|
| 2064 | n/a | if (delta == NULL) { |
|---|
| 2065 | n/a | Py_DECREF(divmod); |
|---|
| 2066 | n/a | return NULL; |
|---|
| 2067 | n/a | } |
|---|
| 2068 | n/a | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
|---|
| 2069 | n/a | Py_DECREF(delta); |
|---|
| 2070 | n/a | Py_DECREF(divmod); |
|---|
| 2071 | n/a | return result; |
|---|
| 2072 | n/a | } |
|---|
| 2073 | n/a | |
|---|
| 2074 | n/a | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
|---|
| 2075 | n/a | * timedelta constructor. sofar is the # of microseconds accounted for |
|---|
| 2076 | n/a | * so far, and there are factor microseconds per current unit, the number |
|---|
| 2077 | n/a | * of which is given by num. num * factor is added to sofar in a |
|---|
| 2078 | n/a | * numerically careful way, and that's the result. Any fractional |
|---|
| 2079 | n/a | * microseconds left over (this can happen if num is a float type) are |
|---|
| 2080 | n/a | * added into *leftover. |
|---|
| 2081 | n/a | * Note that there are many ways this can give an error (NULL) return. |
|---|
| 2082 | n/a | */ |
|---|
| 2083 | n/a | static PyObject * |
|---|
| 2084 | n/a | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
|---|
| 2085 | n/a | double *leftover) |
|---|
| 2086 | n/a | { |
|---|
| 2087 | n/a | PyObject *prod; |
|---|
| 2088 | n/a | PyObject *sum; |
|---|
| 2089 | n/a | |
|---|
| 2090 | n/a | assert(num != NULL); |
|---|
| 2091 | n/a | |
|---|
| 2092 | n/a | if (PyLong_Check(num)) { |
|---|
| 2093 | n/a | prod = PyNumber_Multiply(num, factor); |
|---|
| 2094 | n/a | if (prod == NULL) |
|---|
| 2095 | n/a | return NULL; |
|---|
| 2096 | n/a | sum = PyNumber_Add(sofar, prod); |
|---|
| 2097 | n/a | Py_DECREF(prod); |
|---|
| 2098 | n/a | return sum; |
|---|
| 2099 | n/a | } |
|---|
| 2100 | n/a | |
|---|
| 2101 | n/a | if (PyFloat_Check(num)) { |
|---|
| 2102 | n/a | double dnum; |
|---|
| 2103 | n/a | double fracpart; |
|---|
| 2104 | n/a | double intpart; |
|---|
| 2105 | n/a | PyObject *x; |
|---|
| 2106 | n/a | PyObject *y; |
|---|
| 2107 | n/a | |
|---|
| 2108 | n/a | /* The Plan: decompose num into an integer part and a |
|---|
| 2109 | n/a | * fractional part, num = intpart + fracpart. |
|---|
| 2110 | n/a | * Then num * factor == |
|---|
| 2111 | n/a | * intpart * factor + fracpart * factor |
|---|
| 2112 | n/a | * and the LHS can be computed exactly in long arithmetic. |
|---|
| 2113 | n/a | * The RHS is again broken into an int part and frac part. |
|---|
| 2114 | n/a | * and the frac part is added into *leftover. |
|---|
| 2115 | n/a | */ |
|---|
| 2116 | n/a | dnum = PyFloat_AsDouble(num); |
|---|
| 2117 | n/a | if (dnum == -1.0 && PyErr_Occurred()) |
|---|
| 2118 | n/a | return NULL; |
|---|
| 2119 | n/a | fracpart = modf(dnum, &intpart); |
|---|
| 2120 | n/a | x = PyLong_FromDouble(intpart); |
|---|
| 2121 | n/a | if (x == NULL) |
|---|
| 2122 | n/a | return NULL; |
|---|
| 2123 | n/a | |
|---|
| 2124 | n/a | prod = PyNumber_Multiply(x, factor); |
|---|
| 2125 | n/a | Py_DECREF(x); |
|---|
| 2126 | n/a | if (prod == NULL) |
|---|
| 2127 | n/a | return NULL; |
|---|
| 2128 | n/a | |
|---|
| 2129 | n/a | sum = PyNumber_Add(sofar, prod); |
|---|
| 2130 | n/a | Py_DECREF(prod); |
|---|
| 2131 | n/a | if (sum == NULL) |
|---|
| 2132 | n/a | return NULL; |
|---|
| 2133 | n/a | |
|---|
| 2134 | n/a | if (fracpart == 0.0) |
|---|
| 2135 | n/a | return sum; |
|---|
| 2136 | n/a | /* So far we've lost no information. Dealing with the |
|---|
| 2137 | n/a | * fractional part requires float arithmetic, and may |
|---|
| 2138 | n/a | * lose a little info. |
|---|
| 2139 | n/a | */ |
|---|
| 2140 | n/a | assert(PyLong_Check(factor)); |
|---|
| 2141 | n/a | dnum = PyLong_AsDouble(factor); |
|---|
| 2142 | n/a | |
|---|
| 2143 | n/a | dnum *= fracpart; |
|---|
| 2144 | n/a | fracpart = modf(dnum, &intpart); |
|---|
| 2145 | n/a | x = PyLong_FromDouble(intpart); |
|---|
| 2146 | n/a | if (x == NULL) { |
|---|
| 2147 | n/a | Py_DECREF(sum); |
|---|
| 2148 | n/a | return NULL; |
|---|
| 2149 | n/a | } |
|---|
| 2150 | n/a | |
|---|
| 2151 | n/a | y = PyNumber_Add(sum, x); |
|---|
| 2152 | n/a | Py_DECREF(sum); |
|---|
| 2153 | n/a | Py_DECREF(x); |
|---|
| 2154 | n/a | *leftover += fracpart; |
|---|
| 2155 | n/a | return y; |
|---|
| 2156 | n/a | } |
|---|
| 2157 | n/a | |
|---|
| 2158 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2159 | n/a | "unsupported type for timedelta %s component: %s", |
|---|
| 2160 | n/a | tag, Py_TYPE(num)->tp_name); |
|---|
| 2161 | n/a | return NULL; |
|---|
| 2162 | n/a | } |
|---|
| 2163 | n/a | |
|---|
| 2164 | n/a | static PyObject * |
|---|
| 2165 | n/a | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|---|
| 2166 | n/a | { |
|---|
| 2167 | n/a | PyObject *self = NULL; |
|---|
| 2168 | n/a | |
|---|
| 2169 | n/a | /* Argument objects. */ |
|---|
| 2170 | n/a | PyObject *day = NULL; |
|---|
| 2171 | n/a | PyObject *second = NULL; |
|---|
| 2172 | n/a | PyObject *us = NULL; |
|---|
| 2173 | n/a | PyObject *ms = NULL; |
|---|
| 2174 | n/a | PyObject *minute = NULL; |
|---|
| 2175 | n/a | PyObject *hour = NULL; |
|---|
| 2176 | n/a | PyObject *week = NULL; |
|---|
| 2177 | n/a | |
|---|
| 2178 | n/a | PyObject *x = NULL; /* running sum of microseconds */ |
|---|
| 2179 | n/a | PyObject *y = NULL; /* temp sum of microseconds */ |
|---|
| 2180 | n/a | double leftover_us = 0.0; |
|---|
| 2181 | n/a | |
|---|
| 2182 | n/a | static char *keywords[] = { |
|---|
| 2183 | n/a | "days", "seconds", "microseconds", "milliseconds", |
|---|
| 2184 | n/a | "minutes", "hours", "weeks", NULL |
|---|
| 2185 | n/a | }; |
|---|
| 2186 | n/a | |
|---|
| 2187 | n/a | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
|---|
| 2188 | n/a | keywords, |
|---|
| 2189 | n/a | &day, &second, &us, |
|---|
| 2190 | n/a | &ms, &minute, &hour, &week) == 0) |
|---|
| 2191 | n/a | goto Done; |
|---|
| 2192 | n/a | |
|---|
| 2193 | n/a | x = PyLong_FromLong(0); |
|---|
| 2194 | n/a | if (x == NULL) |
|---|
| 2195 | n/a | goto Done; |
|---|
| 2196 | n/a | |
|---|
| 2197 | n/a | #define CLEANUP \ |
|---|
| 2198 | n/a | Py_DECREF(x); \ |
|---|
| 2199 | n/a | x = y; \ |
|---|
| 2200 | n/a | if (x == NULL) \ |
|---|
| 2201 | n/a | goto Done |
|---|
| 2202 | n/a | |
|---|
| 2203 | n/a | if (us) { |
|---|
| 2204 | n/a | y = accum("microseconds", x, us, one, &leftover_us); |
|---|
| 2205 | n/a | CLEANUP; |
|---|
| 2206 | n/a | } |
|---|
| 2207 | n/a | if (ms) { |
|---|
| 2208 | n/a | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
|---|
| 2209 | n/a | CLEANUP; |
|---|
| 2210 | n/a | } |
|---|
| 2211 | n/a | if (second) { |
|---|
| 2212 | n/a | y = accum("seconds", x, second, us_per_second, &leftover_us); |
|---|
| 2213 | n/a | CLEANUP; |
|---|
| 2214 | n/a | } |
|---|
| 2215 | n/a | if (minute) { |
|---|
| 2216 | n/a | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
|---|
| 2217 | n/a | CLEANUP; |
|---|
| 2218 | n/a | } |
|---|
| 2219 | n/a | if (hour) { |
|---|
| 2220 | n/a | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
|---|
| 2221 | n/a | CLEANUP; |
|---|
| 2222 | n/a | } |
|---|
| 2223 | n/a | if (day) { |
|---|
| 2224 | n/a | y = accum("days", x, day, us_per_day, &leftover_us); |
|---|
| 2225 | n/a | CLEANUP; |
|---|
| 2226 | n/a | } |
|---|
| 2227 | n/a | if (week) { |
|---|
| 2228 | n/a | y = accum("weeks", x, week, us_per_week, &leftover_us); |
|---|
| 2229 | n/a | CLEANUP; |
|---|
| 2230 | n/a | } |
|---|
| 2231 | n/a | if (leftover_us) { |
|---|
| 2232 | n/a | /* Round to nearest whole # of us, and add into x. */ |
|---|
| 2233 | n/a | double whole_us = round(leftover_us); |
|---|
| 2234 | n/a | int x_is_odd; |
|---|
| 2235 | n/a | PyObject *temp; |
|---|
| 2236 | n/a | |
|---|
| 2237 | n/a | whole_us = round(leftover_us); |
|---|
| 2238 | n/a | if (fabs(whole_us - leftover_us) == 0.5) { |
|---|
| 2239 | n/a | /* We're exactly halfway between two integers. In order |
|---|
| 2240 | n/a | * to do round-half-to-even, we must determine whether x |
|---|
| 2241 | n/a | * is odd. Note that x is odd when it's last bit is 1. The |
|---|
| 2242 | n/a | * code below uses bitwise and operation to check the last |
|---|
| 2243 | n/a | * bit. */ |
|---|
| 2244 | n/a | temp = PyNumber_And(x, one); /* temp <- x & 1 */ |
|---|
| 2245 | n/a | if (temp == NULL) { |
|---|
| 2246 | n/a | Py_DECREF(x); |
|---|
| 2247 | n/a | goto Done; |
|---|
| 2248 | n/a | } |
|---|
| 2249 | n/a | x_is_odd = PyObject_IsTrue(temp); |
|---|
| 2250 | n/a | Py_DECREF(temp); |
|---|
| 2251 | n/a | if (x_is_odd == -1) { |
|---|
| 2252 | n/a | Py_DECREF(x); |
|---|
| 2253 | n/a | goto Done; |
|---|
| 2254 | n/a | } |
|---|
| 2255 | n/a | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
|---|
| 2256 | n/a | } |
|---|
| 2257 | n/a | |
|---|
| 2258 | n/a | temp = PyLong_FromLong((long)whole_us); |
|---|
| 2259 | n/a | |
|---|
| 2260 | n/a | if (temp == NULL) { |
|---|
| 2261 | n/a | Py_DECREF(x); |
|---|
| 2262 | n/a | goto Done; |
|---|
| 2263 | n/a | } |
|---|
| 2264 | n/a | y = PyNumber_Add(x, temp); |
|---|
| 2265 | n/a | Py_DECREF(temp); |
|---|
| 2266 | n/a | CLEANUP; |
|---|
| 2267 | n/a | } |
|---|
| 2268 | n/a | |
|---|
| 2269 | n/a | self = microseconds_to_delta_ex(x, type); |
|---|
| 2270 | n/a | Py_DECREF(x); |
|---|
| 2271 | n/a | Done: |
|---|
| 2272 | n/a | return self; |
|---|
| 2273 | n/a | |
|---|
| 2274 | n/a | #undef CLEANUP |
|---|
| 2275 | n/a | } |
|---|
| 2276 | n/a | |
|---|
| 2277 | n/a | static int |
|---|
| 2278 | n/a | delta_bool(PyDateTime_Delta *self) |
|---|
| 2279 | n/a | { |
|---|
| 2280 | n/a | return (GET_TD_DAYS(self) != 0 |
|---|
| 2281 | n/a | || GET_TD_SECONDS(self) != 0 |
|---|
| 2282 | n/a | || GET_TD_MICROSECONDS(self) != 0); |
|---|
| 2283 | n/a | } |
|---|
| 2284 | n/a | |
|---|
| 2285 | n/a | static PyObject * |
|---|
| 2286 | n/a | delta_repr(PyDateTime_Delta *self) |
|---|
| 2287 | n/a | { |
|---|
| 2288 | n/a | if (GET_TD_MICROSECONDS(self) != 0) |
|---|
| 2289 | n/a | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
|---|
| 2290 | n/a | Py_TYPE(self)->tp_name, |
|---|
| 2291 | n/a | GET_TD_DAYS(self), |
|---|
| 2292 | n/a | GET_TD_SECONDS(self), |
|---|
| 2293 | n/a | GET_TD_MICROSECONDS(self)); |
|---|
| 2294 | n/a | if (GET_TD_SECONDS(self) != 0) |
|---|
| 2295 | n/a | return PyUnicode_FromFormat("%s(%d, %d)", |
|---|
| 2296 | n/a | Py_TYPE(self)->tp_name, |
|---|
| 2297 | n/a | GET_TD_DAYS(self), |
|---|
| 2298 | n/a | GET_TD_SECONDS(self)); |
|---|
| 2299 | n/a | |
|---|
| 2300 | n/a | return PyUnicode_FromFormat("%s(%d)", |
|---|
| 2301 | n/a | Py_TYPE(self)->tp_name, |
|---|
| 2302 | n/a | GET_TD_DAYS(self)); |
|---|
| 2303 | n/a | } |
|---|
| 2304 | n/a | |
|---|
| 2305 | n/a | static PyObject * |
|---|
| 2306 | n/a | delta_str(PyDateTime_Delta *self) |
|---|
| 2307 | n/a | { |
|---|
| 2308 | n/a | int us = GET_TD_MICROSECONDS(self); |
|---|
| 2309 | n/a | int seconds = GET_TD_SECONDS(self); |
|---|
| 2310 | n/a | int minutes = divmod(seconds, 60, &seconds); |
|---|
| 2311 | n/a | int hours = divmod(minutes, 60, &minutes); |
|---|
| 2312 | n/a | int days = GET_TD_DAYS(self); |
|---|
| 2313 | n/a | |
|---|
| 2314 | n/a | if (days) { |
|---|
| 2315 | n/a | if (us) |
|---|
| 2316 | n/a | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
|---|
| 2317 | n/a | days, (days == 1 || days == -1) ? "" : "s", |
|---|
| 2318 | n/a | hours, minutes, seconds, us); |
|---|
| 2319 | n/a | else |
|---|
| 2320 | n/a | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
|---|
| 2321 | n/a | days, (days == 1 || days == -1) ? "" : "s", |
|---|
| 2322 | n/a | hours, minutes, seconds); |
|---|
| 2323 | n/a | } else { |
|---|
| 2324 | n/a | if (us) |
|---|
| 2325 | n/a | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
|---|
| 2326 | n/a | hours, minutes, seconds, us); |
|---|
| 2327 | n/a | else |
|---|
| 2328 | n/a | return PyUnicode_FromFormat("%d:%02d:%02d", |
|---|
| 2329 | n/a | hours, minutes, seconds); |
|---|
| 2330 | n/a | } |
|---|
| 2331 | n/a | |
|---|
| 2332 | n/a | } |
|---|
| 2333 | n/a | |
|---|
| 2334 | n/a | /* Pickle support, a simple use of __reduce__. */ |
|---|
| 2335 | n/a | |
|---|
| 2336 | n/a | /* __getstate__ isn't exposed */ |
|---|
| 2337 | n/a | static PyObject * |
|---|
| 2338 | n/a | delta_getstate(PyDateTime_Delta *self) |
|---|
| 2339 | n/a | { |
|---|
| 2340 | n/a | return Py_BuildValue("iii", GET_TD_DAYS(self), |
|---|
| 2341 | n/a | GET_TD_SECONDS(self), |
|---|
| 2342 | n/a | GET_TD_MICROSECONDS(self)); |
|---|
| 2343 | n/a | } |
|---|
| 2344 | n/a | |
|---|
| 2345 | n/a | static PyObject * |
|---|
| 2346 | n/a | delta_total_seconds(PyObject *self) |
|---|
| 2347 | n/a | { |
|---|
| 2348 | n/a | PyObject *total_seconds; |
|---|
| 2349 | n/a | PyObject *total_microseconds; |
|---|
| 2350 | n/a | |
|---|
| 2351 | n/a | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
|---|
| 2352 | n/a | if (total_microseconds == NULL) |
|---|
| 2353 | n/a | return NULL; |
|---|
| 2354 | n/a | |
|---|
| 2355 | n/a | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
|---|
| 2356 | n/a | |
|---|
| 2357 | n/a | Py_DECREF(total_microseconds); |
|---|
| 2358 | n/a | return total_seconds; |
|---|
| 2359 | n/a | } |
|---|
| 2360 | n/a | |
|---|
| 2361 | n/a | static PyObject * |
|---|
| 2362 | n/a | delta_reduce(PyDateTime_Delta* self) |
|---|
| 2363 | n/a | { |
|---|
| 2364 | n/a | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
|---|
| 2365 | n/a | } |
|---|
| 2366 | n/a | |
|---|
| 2367 | n/a | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
|---|
| 2368 | n/a | |
|---|
| 2369 | n/a | static PyMemberDef delta_members[] = { |
|---|
| 2370 | n/a | |
|---|
| 2371 | n/a | {"days", T_INT, OFFSET(days), READONLY, |
|---|
| 2372 | n/a | PyDoc_STR("Number of days.")}, |
|---|
| 2373 | n/a | |
|---|
| 2374 | n/a | {"seconds", T_INT, OFFSET(seconds), READONLY, |
|---|
| 2375 | n/a | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
|---|
| 2376 | n/a | |
|---|
| 2377 | n/a | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
|---|
| 2378 | n/a | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
|---|
| 2379 | n/a | {NULL} |
|---|
| 2380 | n/a | }; |
|---|
| 2381 | n/a | |
|---|
| 2382 | n/a | static PyMethodDef delta_methods[] = { |
|---|
| 2383 | n/a | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
|---|
| 2384 | n/a | PyDoc_STR("Total seconds in the duration.")}, |
|---|
| 2385 | n/a | |
|---|
| 2386 | n/a | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
|---|
| 2387 | n/a | PyDoc_STR("__reduce__() -> (cls, state)")}, |
|---|
| 2388 | n/a | |
|---|
| 2389 | n/a | {NULL, NULL}, |
|---|
| 2390 | n/a | }; |
|---|
| 2391 | n/a | |
|---|
| 2392 | n/a | static const char delta_doc[] = |
|---|
| 2393 | n/a | PyDoc_STR("Difference between two datetime values."); |
|---|
| 2394 | n/a | |
|---|
| 2395 | n/a | static PyNumberMethods delta_as_number = { |
|---|
| 2396 | n/a | delta_add, /* nb_add */ |
|---|
| 2397 | n/a | delta_subtract, /* nb_subtract */ |
|---|
| 2398 | n/a | delta_multiply, /* nb_multiply */ |
|---|
| 2399 | n/a | delta_remainder, /* nb_remainder */ |
|---|
| 2400 | n/a | delta_divmod, /* nb_divmod */ |
|---|
| 2401 | n/a | 0, /* nb_power */ |
|---|
| 2402 | n/a | (unaryfunc)delta_negative, /* nb_negative */ |
|---|
| 2403 | n/a | (unaryfunc)delta_positive, /* nb_positive */ |
|---|
| 2404 | n/a | (unaryfunc)delta_abs, /* nb_absolute */ |
|---|
| 2405 | n/a | (inquiry)delta_bool, /* nb_bool */ |
|---|
| 2406 | n/a | 0, /*nb_invert*/ |
|---|
| 2407 | n/a | 0, /*nb_lshift*/ |
|---|
| 2408 | n/a | 0, /*nb_rshift*/ |
|---|
| 2409 | n/a | 0, /*nb_and*/ |
|---|
| 2410 | n/a | 0, /*nb_xor*/ |
|---|
| 2411 | n/a | 0, /*nb_or*/ |
|---|
| 2412 | n/a | 0, /*nb_int*/ |
|---|
| 2413 | n/a | 0, /*nb_reserved*/ |
|---|
| 2414 | n/a | 0, /*nb_float*/ |
|---|
| 2415 | n/a | 0, /*nb_inplace_add*/ |
|---|
| 2416 | n/a | 0, /*nb_inplace_subtract*/ |
|---|
| 2417 | n/a | 0, /*nb_inplace_multiply*/ |
|---|
| 2418 | n/a | 0, /*nb_inplace_remainder*/ |
|---|
| 2419 | n/a | 0, /*nb_inplace_power*/ |
|---|
| 2420 | n/a | 0, /*nb_inplace_lshift*/ |
|---|
| 2421 | n/a | 0, /*nb_inplace_rshift*/ |
|---|
| 2422 | n/a | 0, /*nb_inplace_and*/ |
|---|
| 2423 | n/a | 0, /*nb_inplace_xor*/ |
|---|
| 2424 | n/a | 0, /*nb_inplace_or*/ |
|---|
| 2425 | n/a | delta_divide, /* nb_floor_divide */ |
|---|
| 2426 | n/a | delta_truedivide, /* nb_true_divide */ |
|---|
| 2427 | n/a | 0, /* nb_inplace_floor_divide */ |
|---|
| 2428 | n/a | 0, /* nb_inplace_true_divide */ |
|---|
| 2429 | n/a | }; |
|---|
| 2430 | n/a | |
|---|
| 2431 | n/a | static PyTypeObject PyDateTime_DeltaType = { |
|---|
| 2432 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 2433 | n/a | "datetime.timedelta", /* tp_name */ |
|---|
| 2434 | n/a | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
|---|
| 2435 | n/a | 0, /* tp_itemsize */ |
|---|
| 2436 | n/a | 0, /* tp_dealloc */ |
|---|
| 2437 | n/a | 0, /* tp_print */ |
|---|
| 2438 | n/a | 0, /* tp_getattr */ |
|---|
| 2439 | n/a | 0, /* tp_setattr */ |
|---|
| 2440 | n/a | 0, /* tp_reserved */ |
|---|
| 2441 | n/a | (reprfunc)delta_repr, /* tp_repr */ |
|---|
| 2442 | n/a | &delta_as_number, /* tp_as_number */ |
|---|
| 2443 | n/a | 0, /* tp_as_sequence */ |
|---|
| 2444 | n/a | 0, /* tp_as_mapping */ |
|---|
| 2445 | n/a | (hashfunc)delta_hash, /* tp_hash */ |
|---|
| 2446 | n/a | 0, /* tp_call */ |
|---|
| 2447 | n/a | (reprfunc)delta_str, /* tp_str */ |
|---|
| 2448 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 2449 | n/a | 0, /* tp_setattro */ |
|---|
| 2450 | n/a | 0, /* tp_as_buffer */ |
|---|
| 2451 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 2452 | n/a | delta_doc, /* tp_doc */ |
|---|
| 2453 | n/a | 0, /* tp_traverse */ |
|---|
| 2454 | n/a | 0, /* tp_clear */ |
|---|
| 2455 | n/a | delta_richcompare, /* tp_richcompare */ |
|---|
| 2456 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 2457 | n/a | 0, /* tp_iter */ |
|---|
| 2458 | n/a | 0, /* tp_iternext */ |
|---|
| 2459 | n/a | delta_methods, /* tp_methods */ |
|---|
| 2460 | n/a | delta_members, /* tp_members */ |
|---|
| 2461 | n/a | 0, /* tp_getset */ |
|---|
| 2462 | n/a | 0, /* tp_base */ |
|---|
| 2463 | n/a | 0, /* tp_dict */ |
|---|
| 2464 | n/a | 0, /* tp_descr_get */ |
|---|
| 2465 | n/a | 0, /* tp_descr_set */ |
|---|
| 2466 | n/a | 0, /* tp_dictoffset */ |
|---|
| 2467 | n/a | 0, /* tp_init */ |
|---|
| 2468 | n/a | 0, /* tp_alloc */ |
|---|
| 2469 | n/a | delta_new, /* tp_new */ |
|---|
| 2470 | n/a | 0, /* tp_free */ |
|---|
| 2471 | n/a | }; |
|---|
| 2472 | n/a | |
|---|
| 2473 | n/a | /* |
|---|
| 2474 | n/a | * PyDateTime_Date implementation. |
|---|
| 2475 | n/a | */ |
|---|
| 2476 | n/a | |
|---|
| 2477 | n/a | /* Accessor properties. */ |
|---|
| 2478 | n/a | |
|---|
| 2479 | n/a | static PyObject * |
|---|
| 2480 | n/a | date_year(PyDateTime_Date *self, void *unused) |
|---|
| 2481 | n/a | { |
|---|
| 2482 | n/a | return PyLong_FromLong(GET_YEAR(self)); |
|---|
| 2483 | n/a | } |
|---|
| 2484 | n/a | |
|---|
| 2485 | n/a | static PyObject * |
|---|
| 2486 | n/a | date_month(PyDateTime_Date *self, void *unused) |
|---|
| 2487 | n/a | { |
|---|
| 2488 | n/a | return PyLong_FromLong(GET_MONTH(self)); |
|---|
| 2489 | n/a | } |
|---|
| 2490 | n/a | |
|---|
| 2491 | n/a | static PyObject * |
|---|
| 2492 | n/a | date_day(PyDateTime_Date *self, void *unused) |
|---|
| 2493 | n/a | { |
|---|
| 2494 | n/a | return PyLong_FromLong(GET_DAY(self)); |
|---|
| 2495 | n/a | } |
|---|
| 2496 | n/a | |
|---|
| 2497 | n/a | static PyGetSetDef date_getset[] = { |
|---|
| 2498 | n/a | {"year", (getter)date_year}, |
|---|
| 2499 | n/a | {"month", (getter)date_month}, |
|---|
| 2500 | n/a | {"day", (getter)date_day}, |
|---|
| 2501 | n/a | {NULL} |
|---|
| 2502 | n/a | }; |
|---|
| 2503 | n/a | |
|---|
| 2504 | n/a | /* Constructors. */ |
|---|
| 2505 | n/a | |
|---|
| 2506 | n/a | static char *date_kws[] = {"year", "month", "day", NULL}; |
|---|
| 2507 | n/a | |
|---|
| 2508 | n/a | static PyObject * |
|---|
| 2509 | n/a | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|---|
| 2510 | n/a | { |
|---|
| 2511 | n/a | PyObject *self = NULL; |
|---|
| 2512 | n/a | PyObject *state; |
|---|
| 2513 | n/a | int year; |
|---|
| 2514 | n/a | int month; |
|---|
| 2515 | n/a | int day; |
|---|
| 2516 | n/a | |
|---|
| 2517 | n/a | /* Check for invocation from pickle with __getstate__ state */ |
|---|
| 2518 | n/a | if (PyTuple_GET_SIZE(args) == 1 && |
|---|
| 2519 | n/a | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
|---|
| 2520 | n/a | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
|---|
| 2521 | n/a | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
|---|
| 2522 | n/a | { |
|---|
| 2523 | n/a | PyDateTime_Date *me; |
|---|
| 2524 | n/a | |
|---|
| 2525 | n/a | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
|---|
| 2526 | n/a | if (me != NULL) { |
|---|
| 2527 | n/a | char *pdata = PyBytes_AS_STRING(state); |
|---|
| 2528 | n/a | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
|---|
| 2529 | n/a | me->hashcode = -1; |
|---|
| 2530 | n/a | } |
|---|
| 2531 | n/a | return (PyObject *)me; |
|---|
| 2532 | n/a | } |
|---|
| 2533 | n/a | |
|---|
| 2534 | n/a | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
|---|
| 2535 | n/a | &year, &month, &day)) { |
|---|
| 2536 | n/a | self = new_date_ex(year, month, day, type); |
|---|
| 2537 | n/a | } |
|---|
| 2538 | n/a | return self; |
|---|
| 2539 | n/a | } |
|---|
| 2540 | n/a | |
|---|
| 2541 | n/a | /* Return new date from localtime(t). */ |
|---|
| 2542 | n/a | static PyObject * |
|---|
| 2543 | n/a | date_local_from_object(PyObject *cls, PyObject *obj) |
|---|
| 2544 | n/a | { |
|---|
| 2545 | n/a | struct tm tm; |
|---|
| 2546 | n/a | time_t t; |
|---|
| 2547 | n/a | |
|---|
| 2548 | n/a | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
|---|
| 2549 | n/a | return NULL; |
|---|
| 2550 | n/a | |
|---|
| 2551 | n/a | if (_PyTime_localtime(t, &tm) != 0) |
|---|
| 2552 | n/a | return NULL; |
|---|
| 2553 | n/a | |
|---|
| 2554 | n/a | return PyObject_CallFunction(cls, "iii", |
|---|
| 2555 | n/a | tm.tm_year + 1900, |
|---|
| 2556 | n/a | tm.tm_mon + 1, |
|---|
| 2557 | n/a | tm.tm_mday); |
|---|
| 2558 | n/a | } |
|---|
| 2559 | n/a | |
|---|
| 2560 | n/a | /* Return new date from current time. |
|---|
| 2561 | n/a | * We say this is equivalent to fromtimestamp(time.time()), and the |
|---|
| 2562 | n/a | * only way to be sure of that is to *call* time.time(). That's not |
|---|
| 2563 | n/a | * generally the same as calling C's time. |
|---|
| 2564 | n/a | */ |
|---|
| 2565 | n/a | static PyObject * |
|---|
| 2566 | n/a | date_today(PyObject *cls, PyObject *dummy) |
|---|
| 2567 | n/a | { |
|---|
| 2568 | n/a | PyObject *time; |
|---|
| 2569 | n/a | PyObject *result; |
|---|
| 2570 | n/a | _Py_IDENTIFIER(fromtimestamp); |
|---|
| 2571 | n/a | |
|---|
| 2572 | n/a | time = time_time(); |
|---|
| 2573 | n/a | if (time == NULL) |
|---|
| 2574 | n/a | return NULL; |
|---|
| 2575 | n/a | |
|---|
| 2576 | n/a | /* Note well: today() is a class method, so this may not call |
|---|
| 2577 | n/a | * date.fromtimestamp. For example, it may call |
|---|
| 2578 | n/a | * datetime.fromtimestamp. That's why we need all the accuracy |
|---|
| 2579 | n/a | * time.time() delivers; if someone were gonzo about optimization, |
|---|
| 2580 | n/a | * date.today() could get away with plain C time(). |
|---|
| 2581 | n/a | */ |
|---|
| 2582 | n/a | result = _PyObject_CallMethodIdObjArgs(cls, &PyId_fromtimestamp, |
|---|
| 2583 | n/a | time, NULL); |
|---|
| 2584 | n/a | Py_DECREF(time); |
|---|
| 2585 | n/a | return result; |
|---|
| 2586 | n/a | } |
|---|
| 2587 | n/a | |
|---|
| 2588 | n/a | /* Return new date from given timestamp (Python timestamp -- a double). */ |
|---|
| 2589 | n/a | static PyObject * |
|---|
| 2590 | n/a | date_fromtimestamp(PyObject *cls, PyObject *args) |
|---|
| 2591 | n/a | { |
|---|
| 2592 | n/a | PyObject *timestamp; |
|---|
| 2593 | n/a | PyObject *result = NULL; |
|---|
| 2594 | n/a | |
|---|
| 2595 | n/a | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
|---|
| 2596 | n/a | result = date_local_from_object(cls, timestamp); |
|---|
| 2597 | n/a | return result; |
|---|
| 2598 | n/a | } |
|---|
| 2599 | n/a | |
|---|
| 2600 | n/a | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
|---|
| 2601 | n/a | * the ordinal is out of range. |
|---|
| 2602 | n/a | */ |
|---|
| 2603 | n/a | static PyObject * |
|---|
| 2604 | n/a | date_fromordinal(PyObject *cls, PyObject *args) |
|---|
| 2605 | n/a | { |
|---|
| 2606 | n/a | PyObject *result = NULL; |
|---|
| 2607 | n/a | int ordinal; |
|---|
| 2608 | n/a | |
|---|
| 2609 | n/a | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
|---|
| 2610 | n/a | int year; |
|---|
| 2611 | n/a | int month; |
|---|
| 2612 | n/a | int day; |
|---|
| 2613 | n/a | |
|---|
| 2614 | n/a | if (ordinal < 1) |
|---|
| 2615 | n/a | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
|---|
| 2616 | n/a | ">= 1"); |
|---|
| 2617 | n/a | else { |
|---|
| 2618 | n/a | ord_to_ymd(ordinal, &year, &month, &day); |
|---|
| 2619 | n/a | result = PyObject_CallFunction(cls, "iii", |
|---|
| 2620 | n/a | year, month, day); |
|---|
| 2621 | n/a | } |
|---|
| 2622 | n/a | } |
|---|
| 2623 | n/a | return result; |
|---|
| 2624 | n/a | } |
|---|
| 2625 | n/a | |
|---|
| 2626 | n/a | /* |
|---|
| 2627 | n/a | * Date arithmetic. |
|---|
| 2628 | n/a | */ |
|---|
| 2629 | n/a | |
|---|
| 2630 | n/a | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
|---|
| 2631 | n/a | * instead. |
|---|
| 2632 | n/a | */ |
|---|
| 2633 | n/a | static PyObject * |
|---|
| 2634 | n/a | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
|---|
| 2635 | n/a | { |
|---|
| 2636 | n/a | PyObject *result = NULL; |
|---|
| 2637 | n/a | int year = GET_YEAR(date); |
|---|
| 2638 | n/a | int month = GET_MONTH(date); |
|---|
| 2639 | n/a | int deltadays = GET_TD_DAYS(delta); |
|---|
| 2640 | n/a | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
|---|
| 2641 | n/a | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
|---|
| 2642 | n/a | |
|---|
| 2643 | n/a | if (normalize_date(&year, &month, &day) >= 0) |
|---|
| 2644 | n/a | result = new_date(year, month, day); |
|---|
| 2645 | n/a | return result; |
|---|
| 2646 | n/a | } |
|---|
| 2647 | n/a | |
|---|
| 2648 | n/a | static PyObject * |
|---|
| 2649 | n/a | date_add(PyObject *left, PyObject *right) |
|---|
| 2650 | n/a | { |
|---|
| 2651 | n/a | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
|---|
| 2652 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 2653 | n/a | |
|---|
| 2654 | n/a | if (PyDate_Check(left)) { |
|---|
| 2655 | n/a | /* date + ??? */ |
|---|
| 2656 | n/a | if (PyDelta_Check(right)) |
|---|
| 2657 | n/a | /* date + delta */ |
|---|
| 2658 | n/a | return add_date_timedelta((PyDateTime_Date *) left, |
|---|
| 2659 | n/a | (PyDateTime_Delta *) right, |
|---|
| 2660 | n/a | 0); |
|---|
| 2661 | n/a | } |
|---|
| 2662 | n/a | else { |
|---|
| 2663 | n/a | /* ??? + date |
|---|
| 2664 | n/a | * 'right' must be one of us, or we wouldn't have been called |
|---|
| 2665 | n/a | */ |
|---|
| 2666 | n/a | if (PyDelta_Check(left)) |
|---|
| 2667 | n/a | /* delta + date */ |
|---|
| 2668 | n/a | return add_date_timedelta((PyDateTime_Date *) right, |
|---|
| 2669 | n/a | (PyDateTime_Delta *) left, |
|---|
| 2670 | n/a | 0); |
|---|
| 2671 | n/a | } |
|---|
| 2672 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 2673 | n/a | } |
|---|
| 2674 | n/a | |
|---|
| 2675 | n/a | static PyObject * |
|---|
| 2676 | n/a | date_subtract(PyObject *left, PyObject *right) |
|---|
| 2677 | n/a | { |
|---|
| 2678 | n/a | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
|---|
| 2679 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 2680 | n/a | |
|---|
| 2681 | n/a | if (PyDate_Check(left)) { |
|---|
| 2682 | n/a | if (PyDate_Check(right)) { |
|---|
| 2683 | n/a | /* date - date */ |
|---|
| 2684 | n/a | int left_ord = ymd_to_ord(GET_YEAR(left), |
|---|
| 2685 | n/a | GET_MONTH(left), |
|---|
| 2686 | n/a | GET_DAY(left)); |
|---|
| 2687 | n/a | int right_ord = ymd_to_ord(GET_YEAR(right), |
|---|
| 2688 | n/a | GET_MONTH(right), |
|---|
| 2689 | n/a | GET_DAY(right)); |
|---|
| 2690 | n/a | return new_delta(left_ord - right_ord, 0, 0, 0); |
|---|
| 2691 | n/a | } |
|---|
| 2692 | n/a | if (PyDelta_Check(right)) { |
|---|
| 2693 | n/a | /* date - delta */ |
|---|
| 2694 | n/a | return add_date_timedelta((PyDateTime_Date *) left, |
|---|
| 2695 | n/a | (PyDateTime_Delta *) right, |
|---|
| 2696 | n/a | 1); |
|---|
| 2697 | n/a | } |
|---|
| 2698 | n/a | } |
|---|
| 2699 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 2700 | n/a | } |
|---|
| 2701 | n/a | |
|---|
| 2702 | n/a | |
|---|
| 2703 | n/a | /* Various ways to turn a date into a string. */ |
|---|
| 2704 | n/a | |
|---|
| 2705 | n/a | static PyObject * |
|---|
| 2706 | n/a | date_repr(PyDateTime_Date *self) |
|---|
| 2707 | n/a | { |
|---|
| 2708 | n/a | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
|---|
| 2709 | n/a | Py_TYPE(self)->tp_name, |
|---|
| 2710 | n/a | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
|---|
| 2711 | n/a | } |
|---|
| 2712 | n/a | |
|---|
| 2713 | n/a | static PyObject * |
|---|
| 2714 | n/a | date_isoformat(PyDateTime_Date *self) |
|---|
| 2715 | n/a | { |
|---|
| 2716 | n/a | return PyUnicode_FromFormat("%04d-%02d-%02d", |
|---|
| 2717 | n/a | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
|---|
| 2718 | n/a | } |
|---|
| 2719 | n/a | |
|---|
| 2720 | n/a | /* str() calls the appropriate isoformat() method. */ |
|---|
| 2721 | n/a | static PyObject * |
|---|
| 2722 | n/a | date_str(PyDateTime_Date *self) |
|---|
| 2723 | n/a | { |
|---|
| 2724 | n/a | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
|---|
| 2725 | n/a | } |
|---|
| 2726 | n/a | |
|---|
| 2727 | n/a | |
|---|
| 2728 | n/a | static PyObject * |
|---|
| 2729 | n/a | date_ctime(PyDateTime_Date *self) |
|---|
| 2730 | n/a | { |
|---|
| 2731 | n/a | return format_ctime(self, 0, 0, 0); |
|---|
| 2732 | n/a | } |
|---|
| 2733 | n/a | |
|---|
| 2734 | n/a | static PyObject * |
|---|
| 2735 | n/a | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
|---|
| 2736 | n/a | { |
|---|
| 2737 | n/a | /* This method can be inherited, and needs to call the |
|---|
| 2738 | n/a | * timetuple() method appropriate to self's class. |
|---|
| 2739 | n/a | */ |
|---|
| 2740 | n/a | PyObject *result; |
|---|
| 2741 | n/a | PyObject *tuple; |
|---|
| 2742 | n/a | PyObject *format; |
|---|
| 2743 | n/a | _Py_IDENTIFIER(timetuple); |
|---|
| 2744 | n/a | static char *keywords[] = {"format", NULL}; |
|---|
| 2745 | n/a | |
|---|
| 2746 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
|---|
| 2747 | n/a | &format)) |
|---|
| 2748 | n/a | return NULL; |
|---|
| 2749 | n/a | |
|---|
| 2750 | n/a | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, NULL); |
|---|
| 2751 | n/a | if (tuple == NULL) |
|---|
| 2752 | n/a | return NULL; |
|---|
| 2753 | n/a | result = wrap_strftime((PyObject *)self, format, tuple, |
|---|
| 2754 | n/a | (PyObject *)self); |
|---|
| 2755 | n/a | Py_DECREF(tuple); |
|---|
| 2756 | n/a | return result; |
|---|
| 2757 | n/a | } |
|---|
| 2758 | n/a | |
|---|
| 2759 | n/a | static PyObject * |
|---|
| 2760 | n/a | date_format(PyDateTime_Date *self, PyObject *args) |
|---|
| 2761 | n/a | { |
|---|
| 2762 | n/a | PyObject *format; |
|---|
| 2763 | n/a | |
|---|
| 2764 | n/a | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
|---|
| 2765 | n/a | return NULL; |
|---|
| 2766 | n/a | |
|---|
| 2767 | n/a | /* if the format is zero length, return str(self) */ |
|---|
| 2768 | n/a | if (PyUnicode_GetLength(format) == 0) |
|---|
| 2769 | n/a | return PyObject_Str((PyObject *)self); |
|---|
| 2770 | n/a | |
|---|
| 2771 | n/a | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_strftime, |
|---|
| 2772 | n/a | format, NULL); |
|---|
| 2773 | n/a | } |
|---|
| 2774 | n/a | |
|---|
| 2775 | n/a | /* ISO methods. */ |
|---|
| 2776 | n/a | |
|---|
| 2777 | n/a | static PyObject * |
|---|
| 2778 | n/a | date_isoweekday(PyDateTime_Date *self) |
|---|
| 2779 | n/a | { |
|---|
| 2780 | n/a | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
|---|
| 2781 | n/a | |
|---|
| 2782 | n/a | return PyLong_FromLong(dow + 1); |
|---|
| 2783 | n/a | } |
|---|
| 2784 | n/a | |
|---|
| 2785 | n/a | static PyObject * |
|---|
| 2786 | n/a | date_isocalendar(PyDateTime_Date *self) |
|---|
| 2787 | n/a | { |
|---|
| 2788 | n/a | int year = GET_YEAR(self); |
|---|
| 2789 | n/a | int week1_monday = iso_week1_monday(year); |
|---|
| 2790 | n/a | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
|---|
| 2791 | n/a | int week; |
|---|
| 2792 | n/a | int day; |
|---|
| 2793 | n/a | |
|---|
| 2794 | n/a | week = divmod(today - week1_monday, 7, &day); |
|---|
| 2795 | n/a | if (week < 0) { |
|---|
| 2796 | n/a | --year; |
|---|
| 2797 | n/a | week1_monday = iso_week1_monday(year); |
|---|
| 2798 | n/a | week = divmod(today - week1_monday, 7, &day); |
|---|
| 2799 | n/a | } |
|---|
| 2800 | n/a | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
|---|
| 2801 | n/a | ++year; |
|---|
| 2802 | n/a | week = 0; |
|---|
| 2803 | n/a | } |
|---|
| 2804 | n/a | return Py_BuildValue("iii", year, week + 1, day + 1); |
|---|
| 2805 | n/a | } |
|---|
| 2806 | n/a | |
|---|
| 2807 | n/a | /* Miscellaneous methods. */ |
|---|
| 2808 | n/a | |
|---|
| 2809 | n/a | static PyObject * |
|---|
| 2810 | n/a | date_richcompare(PyObject *self, PyObject *other, int op) |
|---|
| 2811 | n/a | { |
|---|
| 2812 | n/a | if (PyDate_Check(other)) { |
|---|
| 2813 | n/a | int diff = memcmp(((PyDateTime_Date *)self)->data, |
|---|
| 2814 | n/a | ((PyDateTime_Date *)other)->data, |
|---|
| 2815 | n/a | _PyDateTime_DATE_DATASIZE); |
|---|
| 2816 | n/a | return diff_to_bool(diff, op); |
|---|
| 2817 | n/a | } |
|---|
| 2818 | n/a | else |
|---|
| 2819 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 2820 | n/a | } |
|---|
| 2821 | n/a | |
|---|
| 2822 | n/a | static PyObject * |
|---|
| 2823 | n/a | date_timetuple(PyDateTime_Date *self) |
|---|
| 2824 | n/a | { |
|---|
| 2825 | n/a | return build_struct_time(GET_YEAR(self), |
|---|
| 2826 | n/a | GET_MONTH(self), |
|---|
| 2827 | n/a | GET_DAY(self), |
|---|
| 2828 | n/a | 0, 0, 0, -1); |
|---|
| 2829 | n/a | } |
|---|
| 2830 | n/a | |
|---|
| 2831 | n/a | static PyObject * |
|---|
| 2832 | n/a | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
|---|
| 2833 | n/a | { |
|---|
| 2834 | n/a | PyObject *clone; |
|---|
| 2835 | n/a | PyObject *tuple; |
|---|
| 2836 | n/a | int year = GET_YEAR(self); |
|---|
| 2837 | n/a | int month = GET_MONTH(self); |
|---|
| 2838 | n/a | int day = GET_DAY(self); |
|---|
| 2839 | n/a | |
|---|
| 2840 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
|---|
| 2841 | n/a | &year, &month, &day)) |
|---|
| 2842 | n/a | return NULL; |
|---|
| 2843 | n/a | tuple = Py_BuildValue("iii", year, month, day); |
|---|
| 2844 | n/a | if (tuple == NULL) |
|---|
| 2845 | n/a | return NULL; |
|---|
| 2846 | n/a | clone = date_new(Py_TYPE(self), tuple, NULL); |
|---|
| 2847 | n/a | Py_DECREF(tuple); |
|---|
| 2848 | n/a | return clone; |
|---|
| 2849 | n/a | } |
|---|
| 2850 | n/a | |
|---|
| 2851 | n/a | static Py_hash_t |
|---|
| 2852 | n/a | generic_hash(unsigned char *data, int len) |
|---|
| 2853 | n/a | { |
|---|
| 2854 | n/a | return _Py_HashBytes(data, len); |
|---|
| 2855 | n/a | } |
|---|
| 2856 | n/a | |
|---|
| 2857 | n/a | |
|---|
| 2858 | n/a | static PyObject *date_getstate(PyDateTime_Date *self); |
|---|
| 2859 | n/a | |
|---|
| 2860 | n/a | static Py_hash_t |
|---|
| 2861 | n/a | date_hash(PyDateTime_Date *self) |
|---|
| 2862 | n/a | { |
|---|
| 2863 | n/a | if (self->hashcode == -1) { |
|---|
| 2864 | n/a | self->hashcode = generic_hash( |
|---|
| 2865 | n/a | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
|---|
| 2866 | n/a | } |
|---|
| 2867 | n/a | |
|---|
| 2868 | n/a | return self->hashcode; |
|---|
| 2869 | n/a | } |
|---|
| 2870 | n/a | |
|---|
| 2871 | n/a | static PyObject * |
|---|
| 2872 | n/a | date_toordinal(PyDateTime_Date *self) |
|---|
| 2873 | n/a | { |
|---|
| 2874 | n/a | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
|---|
| 2875 | n/a | GET_DAY(self))); |
|---|
| 2876 | n/a | } |
|---|
| 2877 | n/a | |
|---|
| 2878 | n/a | static PyObject * |
|---|
| 2879 | n/a | date_weekday(PyDateTime_Date *self) |
|---|
| 2880 | n/a | { |
|---|
| 2881 | n/a | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
|---|
| 2882 | n/a | |
|---|
| 2883 | n/a | return PyLong_FromLong(dow); |
|---|
| 2884 | n/a | } |
|---|
| 2885 | n/a | |
|---|
| 2886 | n/a | /* Pickle support, a simple use of __reduce__. */ |
|---|
| 2887 | n/a | |
|---|
| 2888 | n/a | /* __getstate__ isn't exposed */ |
|---|
| 2889 | n/a | static PyObject * |
|---|
| 2890 | n/a | date_getstate(PyDateTime_Date *self) |
|---|
| 2891 | n/a | { |
|---|
| 2892 | n/a | PyObject* field; |
|---|
| 2893 | n/a | field = PyBytes_FromStringAndSize((char*)self->data, |
|---|
| 2894 | n/a | _PyDateTime_DATE_DATASIZE); |
|---|
| 2895 | n/a | return Py_BuildValue("(N)", field); |
|---|
| 2896 | n/a | } |
|---|
| 2897 | n/a | |
|---|
| 2898 | n/a | static PyObject * |
|---|
| 2899 | n/a | date_reduce(PyDateTime_Date *self, PyObject *arg) |
|---|
| 2900 | n/a | { |
|---|
| 2901 | n/a | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
|---|
| 2902 | n/a | } |
|---|
| 2903 | n/a | |
|---|
| 2904 | n/a | static PyMethodDef date_methods[] = { |
|---|
| 2905 | n/a | |
|---|
| 2906 | n/a | /* Class methods: */ |
|---|
| 2907 | n/a | |
|---|
| 2908 | n/a | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
|---|
| 2909 | n/a | METH_CLASS, |
|---|
| 2910 | n/a | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
|---|
| 2911 | n/a | "time.time()).")}, |
|---|
| 2912 | n/a | |
|---|
| 2913 | n/a | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
|---|
| 2914 | n/a | METH_CLASS, |
|---|
| 2915 | n/a | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
|---|
| 2916 | n/a | "ordinal.")}, |
|---|
| 2917 | n/a | |
|---|
| 2918 | n/a | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
|---|
| 2919 | n/a | PyDoc_STR("Current date or datetime: same as " |
|---|
| 2920 | n/a | "self.__class__.fromtimestamp(time.time()).")}, |
|---|
| 2921 | n/a | |
|---|
| 2922 | n/a | /* Instance methods: */ |
|---|
| 2923 | n/a | |
|---|
| 2924 | n/a | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
|---|
| 2925 | n/a | PyDoc_STR("Return ctime() style string.")}, |
|---|
| 2926 | n/a | |
|---|
| 2927 | n/a | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
|---|
| 2928 | n/a | PyDoc_STR("format -> strftime() style string.")}, |
|---|
| 2929 | n/a | |
|---|
| 2930 | n/a | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
|---|
| 2931 | n/a | PyDoc_STR("Formats self with strftime.")}, |
|---|
| 2932 | n/a | |
|---|
| 2933 | n/a | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
|---|
| 2934 | n/a | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
|---|
| 2935 | n/a | |
|---|
| 2936 | n/a | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
|---|
| 2937 | n/a | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
|---|
| 2938 | n/a | "weekday.")}, |
|---|
| 2939 | n/a | |
|---|
| 2940 | n/a | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
|---|
| 2941 | n/a | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
|---|
| 2942 | n/a | |
|---|
| 2943 | n/a | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
|---|
| 2944 | n/a | PyDoc_STR("Return the day of the week represented by the date.\n" |
|---|
| 2945 | n/a | "Monday == 1 ... Sunday == 7")}, |
|---|
| 2946 | n/a | |
|---|
| 2947 | n/a | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
|---|
| 2948 | n/a | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
|---|
| 2949 | n/a | "1 is day 1.")}, |
|---|
| 2950 | n/a | |
|---|
| 2951 | n/a | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
|---|
| 2952 | n/a | PyDoc_STR("Return the day of the week represented by the date.\n" |
|---|
| 2953 | n/a | "Monday == 0 ... Sunday == 6")}, |
|---|
| 2954 | n/a | |
|---|
| 2955 | n/a | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
|---|
| 2956 | n/a | PyDoc_STR("Return date with new specified fields.")}, |
|---|
| 2957 | n/a | |
|---|
| 2958 | n/a | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
|---|
| 2959 | n/a | PyDoc_STR("__reduce__() -> (cls, state)")}, |
|---|
| 2960 | n/a | |
|---|
| 2961 | n/a | {NULL, NULL} |
|---|
| 2962 | n/a | }; |
|---|
| 2963 | n/a | |
|---|
| 2964 | n/a | static const char date_doc[] = |
|---|
| 2965 | n/a | PyDoc_STR("date(year, month, day) --> date object"); |
|---|
| 2966 | n/a | |
|---|
| 2967 | n/a | static PyNumberMethods date_as_number = { |
|---|
| 2968 | n/a | date_add, /* nb_add */ |
|---|
| 2969 | n/a | date_subtract, /* nb_subtract */ |
|---|
| 2970 | n/a | 0, /* nb_multiply */ |
|---|
| 2971 | n/a | 0, /* nb_remainder */ |
|---|
| 2972 | n/a | 0, /* nb_divmod */ |
|---|
| 2973 | n/a | 0, /* nb_power */ |
|---|
| 2974 | n/a | 0, /* nb_negative */ |
|---|
| 2975 | n/a | 0, /* nb_positive */ |
|---|
| 2976 | n/a | 0, /* nb_absolute */ |
|---|
| 2977 | n/a | 0, /* nb_bool */ |
|---|
| 2978 | n/a | }; |
|---|
| 2979 | n/a | |
|---|
| 2980 | n/a | static PyTypeObject PyDateTime_DateType = { |
|---|
| 2981 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 2982 | n/a | "datetime.date", /* tp_name */ |
|---|
| 2983 | n/a | sizeof(PyDateTime_Date), /* tp_basicsize */ |
|---|
| 2984 | n/a | 0, /* tp_itemsize */ |
|---|
| 2985 | n/a | 0, /* tp_dealloc */ |
|---|
| 2986 | n/a | 0, /* tp_print */ |
|---|
| 2987 | n/a | 0, /* tp_getattr */ |
|---|
| 2988 | n/a | 0, /* tp_setattr */ |
|---|
| 2989 | n/a | 0, /* tp_reserved */ |
|---|
| 2990 | n/a | (reprfunc)date_repr, /* tp_repr */ |
|---|
| 2991 | n/a | &date_as_number, /* tp_as_number */ |
|---|
| 2992 | n/a | 0, /* tp_as_sequence */ |
|---|
| 2993 | n/a | 0, /* tp_as_mapping */ |
|---|
| 2994 | n/a | (hashfunc)date_hash, /* tp_hash */ |
|---|
| 2995 | n/a | 0, /* tp_call */ |
|---|
| 2996 | n/a | (reprfunc)date_str, /* tp_str */ |
|---|
| 2997 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 2998 | n/a | 0, /* tp_setattro */ |
|---|
| 2999 | n/a | 0, /* tp_as_buffer */ |
|---|
| 3000 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 3001 | n/a | date_doc, /* tp_doc */ |
|---|
| 3002 | n/a | 0, /* tp_traverse */ |
|---|
| 3003 | n/a | 0, /* tp_clear */ |
|---|
| 3004 | n/a | date_richcompare, /* tp_richcompare */ |
|---|
| 3005 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 3006 | n/a | 0, /* tp_iter */ |
|---|
| 3007 | n/a | 0, /* tp_iternext */ |
|---|
| 3008 | n/a | date_methods, /* tp_methods */ |
|---|
| 3009 | n/a | 0, /* tp_members */ |
|---|
| 3010 | n/a | date_getset, /* tp_getset */ |
|---|
| 3011 | n/a | 0, /* tp_base */ |
|---|
| 3012 | n/a | 0, /* tp_dict */ |
|---|
| 3013 | n/a | 0, /* tp_descr_get */ |
|---|
| 3014 | n/a | 0, /* tp_descr_set */ |
|---|
| 3015 | n/a | 0, /* tp_dictoffset */ |
|---|
| 3016 | n/a | 0, /* tp_init */ |
|---|
| 3017 | n/a | 0, /* tp_alloc */ |
|---|
| 3018 | n/a | date_new, /* tp_new */ |
|---|
| 3019 | n/a | 0, /* tp_free */ |
|---|
| 3020 | n/a | }; |
|---|
| 3021 | n/a | |
|---|
| 3022 | n/a | /* |
|---|
| 3023 | n/a | * PyDateTime_TZInfo implementation. |
|---|
| 3024 | n/a | */ |
|---|
| 3025 | n/a | |
|---|
| 3026 | n/a | /* This is a pure abstract base class, so doesn't do anything beyond |
|---|
| 3027 | n/a | * raising NotImplemented exceptions. Real tzinfo classes need |
|---|
| 3028 | n/a | * to derive from this. This is mostly for clarity, and for efficiency in |
|---|
| 3029 | n/a | * datetime and time constructors (their tzinfo arguments need to |
|---|
| 3030 | n/a | * be subclasses of this tzinfo class, which is easy and quick to check). |
|---|
| 3031 | n/a | * |
|---|
| 3032 | n/a | * Note: For reasons having to do with pickling of subclasses, we have |
|---|
| 3033 | n/a | * to allow tzinfo objects to be instantiated. This wasn't an issue |
|---|
| 3034 | n/a | * in the Python implementation (__init__() could raise NotImplementedError |
|---|
| 3035 | n/a | * there without ill effect), but doing so in the C implementation hit a |
|---|
| 3036 | n/a | * brick wall. |
|---|
| 3037 | n/a | */ |
|---|
| 3038 | n/a | |
|---|
| 3039 | n/a | static PyObject * |
|---|
| 3040 | n/a | tzinfo_nogo(const char* methodname) |
|---|
| 3041 | n/a | { |
|---|
| 3042 | n/a | PyErr_Format(PyExc_NotImplementedError, |
|---|
| 3043 | n/a | "a tzinfo subclass must implement %s()", |
|---|
| 3044 | n/a | methodname); |
|---|
| 3045 | n/a | return NULL; |
|---|
| 3046 | n/a | } |
|---|
| 3047 | n/a | |
|---|
| 3048 | n/a | /* Methods. A subclass must implement these. */ |
|---|
| 3049 | n/a | |
|---|
| 3050 | n/a | static PyObject * |
|---|
| 3051 | n/a | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
|---|
| 3052 | n/a | { |
|---|
| 3053 | n/a | return tzinfo_nogo("tzname"); |
|---|
| 3054 | n/a | } |
|---|
| 3055 | n/a | |
|---|
| 3056 | n/a | static PyObject * |
|---|
| 3057 | n/a | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
|---|
| 3058 | n/a | { |
|---|
| 3059 | n/a | return tzinfo_nogo("utcoffset"); |
|---|
| 3060 | n/a | } |
|---|
| 3061 | n/a | |
|---|
| 3062 | n/a | static PyObject * |
|---|
| 3063 | n/a | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
|---|
| 3064 | n/a | { |
|---|
| 3065 | n/a | return tzinfo_nogo("dst"); |
|---|
| 3066 | n/a | } |
|---|
| 3067 | n/a | |
|---|
| 3068 | n/a | |
|---|
| 3069 | n/a | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
|---|
| 3070 | n/a | PyDateTime_Delta *delta, |
|---|
| 3071 | n/a | int factor); |
|---|
| 3072 | n/a | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
|---|
| 3073 | n/a | static PyObject *datetime_dst(PyObject *self, PyObject *); |
|---|
| 3074 | n/a | |
|---|
| 3075 | n/a | static PyObject * |
|---|
| 3076 | n/a | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
|---|
| 3077 | n/a | { |
|---|
| 3078 | n/a | PyObject *result = NULL; |
|---|
| 3079 | n/a | PyObject *off = NULL, *dst = NULL; |
|---|
| 3080 | n/a | PyDateTime_Delta *delta = NULL; |
|---|
| 3081 | n/a | |
|---|
| 3082 | n/a | if (!PyDateTime_Check(dt)) { |
|---|
| 3083 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3084 | n/a | "fromutc: argument must be a datetime"); |
|---|
| 3085 | n/a | return NULL; |
|---|
| 3086 | n/a | } |
|---|
| 3087 | n/a | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
|---|
| 3088 | n/a | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
|---|
| 3089 | n/a | "is not self"); |
|---|
| 3090 | n/a | return NULL; |
|---|
| 3091 | n/a | } |
|---|
| 3092 | n/a | |
|---|
| 3093 | n/a | off = datetime_utcoffset(dt, NULL); |
|---|
| 3094 | n/a | if (off == NULL) |
|---|
| 3095 | n/a | return NULL; |
|---|
| 3096 | n/a | if (off == Py_None) { |
|---|
| 3097 | n/a | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
|---|
| 3098 | n/a | "utcoffset() result required"); |
|---|
| 3099 | n/a | goto Fail; |
|---|
| 3100 | n/a | } |
|---|
| 3101 | n/a | |
|---|
| 3102 | n/a | dst = datetime_dst(dt, NULL); |
|---|
| 3103 | n/a | if (dst == NULL) |
|---|
| 3104 | n/a | goto Fail; |
|---|
| 3105 | n/a | if (dst == Py_None) { |
|---|
| 3106 | n/a | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
|---|
| 3107 | n/a | "dst() result required"); |
|---|
| 3108 | n/a | goto Fail; |
|---|
| 3109 | n/a | } |
|---|
| 3110 | n/a | |
|---|
| 3111 | n/a | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
|---|
| 3112 | n/a | if (delta == NULL) |
|---|
| 3113 | n/a | goto Fail; |
|---|
| 3114 | n/a | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
|---|
| 3115 | n/a | if (result == NULL) |
|---|
| 3116 | n/a | goto Fail; |
|---|
| 3117 | n/a | |
|---|
| 3118 | n/a | Py_DECREF(dst); |
|---|
| 3119 | n/a | dst = call_dst(GET_DT_TZINFO(dt), result); |
|---|
| 3120 | n/a | if (dst == NULL) |
|---|
| 3121 | n/a | goto Fail; |
|---|
| 3122 | n/a | if (dst == Py_None) |
|---|
| 3123 | n/a | goto Inconsistent; |
|---|
| 3124 | n/a | if (delta_bool((PyDateTime_Delta *)dst) != 0) { |
|---|
| 3125 | n/a | Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, |
|---|
| 3126 | n/a | (PyDateTime_Delta *)dst, 1)); |
|---|
| 3127 | n/a | if (result == NULL) |
|---|
| 3128 | n/a | goto Fail; |
|---|
| 3129 | n/a | } |
|---|
| 3130 | n/a | Py_DECREF(delta); |
|---|
| 3131 | n/a | Py_DECREF(dst); |
|---|
| 3132 | n/a | Py_DECREF(off); |
|---|
| 3133 | n/a | return result; |
|---|
| 3134 | n/a | |
|---|
| 3135 | n/a | Inconsistent: |
|---|
| 3136 | n/a | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
|---|
| 3137 | n/a | "inconsistent results; cannot convert"); |
|---|
| 3138 | n/a | |
|---|
| 3139 | n/a | /* fall thru to failure */ |
|---|
| 3140 | n/a | Fail: |
|---|
| 3141 | n/a | Py_XDECREF(off); |
|---|
| 3142 | n/a | Py_XDECREF(dst); |
|---|
| 3143 | n/a | Py_XDECREF(delta); |
|---|
| 3144 | n/a | Py_XDECREF(result); |
|---|
| 3145 | n/a | return NULL; |
|---|
| 3146 | n/a | } |
|---|
| 3147 | n/a | |
|---|
| 3148 | n/a | /* |
|---|
| 3149 | n/a | * Pickle support. This is solely so that tzinfo subclasses can use |
|---|
| 3150 | n/a | * pickling -- tzinfo itself is supposed to be uninstantiable. |
|---|
| 3151 | n/a | */ |
|---|
| 3152 | n/a | |
|---|
| 3153 | n/a | static PyObject * |
|---|
| 3154 | n/a | tzinfo_reduce(PyObject *self) |
|---|
| 3155 | n/a | { |
|---|
| 3156 | n/a | PyObject *args, *state; |
|---|
| 3157 | n/a | PyObject *getinitargs, *getstate; |
|---|
| 3158 | n/a | _Py_IDENTIFIER(__getinitargs__); |
|---|
| 3159 | n/a | _Py_IDENTIFIER(__getstate__); |
|---|
| 3160 | n/a | |
|---|
| 3161 | n/a | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
|---|
| 3162 | n/a | if (getinitargs != NULL) { |
|---|
| 3163 | n/a | args = _PyObject_CallNoArg(getinitargs); |
|---|
| 3164 | n/a | Py_DECREF(getinitargs); |
|---|
| 3165 | n/a | if (args == NULL) { |
|---|
| 3166 | n/a | return NULL; |
|---|
| 3167 | n/a | } |
|---|
| 3168 | n/a | } |
|---|
| 3169 | n/a | else { |
|---|
| 3170 | n/a | PyErr_Clear(); |
|---|
| 3171 | n/a | |
|---|
| 3172 | n/a | args = PyTuple_New(0); |
|---|
| 3173 | n/a | if (args == NULL) { |
|---|
| 3174 | n/a | return NULL; |
|---|
| 3175 | n/a | } |
|---|
| 3176 | n/a | } |
|---|
| 3177 | n/a | |
|---|
| 3178 | n/a | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
|---|
| 3179 | n/a | if (getstate != NULL) { |
|---|
| 3180 | n/a | state = _PyObject_CallNoArg(getstate); |
|---|
| 3181 | n/a | Py_DECREF(getstate); |
|---|
| 3182 | n/a | if (state == NULL) { |
|---|
| 3183 | n/a | Py_DECREF(args); |
|---|
| 3184 | n/a | return NULL; |
|---|
| 3185 | n/a | } |
|---|
| 3186 | n/a | } |
|---|
| 3187 | n/a | else { |
|---|
| 3188 | n/a | PyObject **dictptr; |
|---|
| 3189 | n/a | PyErr_Clear(); |
|---|
| 3190 | n/a | state = Py_None; |
|---|
| 3191 | n/a | dictptr = _PyObject_GetDictPtr(self); |
|---|
| 3192 | n/a | if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) { |
|---|
| 3193 | n/a | state = *dictptr; |
|---|
| 3194 | n/a | } |
|---|
| 3195 | n/a | Py_INCREF(state); |
|---|
| 3196 | n/a | } |
|---|
| 3197 | n/a | |
|---|
| 3198 | n/a | if (state == Py_None) { |
|---|
| 3199 | n/a | Py_DECREF(state); |
|---|
| 3200 | n/a | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
|---|
| 3201 | n/a | } |
|---|
| 3202 | n/a | else |
|---|
| 3203 | n/a | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
|---|
| 3204 | n/a | } |
|---|
| 3205 | n/a | |
|---|
| 3206 | n/a | static PyMethodDef tzinfo_methods[] = { |
|---|
| 3207 | n/a | |
|---|
| 3208 | n/a | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
|---|
| 3209 | n/a | PyDoc_STR("datetime -> string name of time zone.")}, |
|---|
| 3210 | n/a | |
|---|
| 3211 | n/a | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
|---|
| 3212 | n/a | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
|---|
| 3213 | n/a | "values indicating West of UTC")}, |
|---|
| 3214 | n/a | |
|---|
| 3215 | n/a | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
|---|
| 3216 | n/a | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
|---|
| 3217 | n/a | |
|---|
| 3218 | n/a | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
|---|
| 3219 | n/a | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
|---|
| 3220 | n/a | |
|---|
| 3221 | n/a | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
|---|
| 3222 | n/a | PyDoc_STR("-> (cls, state)")}, |
|---|
| 3223 | n/a | |
|---|
| 3224 | n/a | {NULL, NULL} |
|---|
| 3225 | n/a | }; |
|---|
| 3226 | n/a | |
|---|
| 3227 | n/a | static const char tzinfo_doc[] = |
|---|
| 3228 | n/a | PyDoc_STR("Abstract base class for time zone info objects."); |
|---|
| 3229 | n/a | |
|---|
| 3230 | n/a | static PyTypeObject PyDateTime_TZInfoType = { |
|---|
| 3231 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 3232 | n/a | "datetime.tzinfo", /* tp_name */ |
|---|
| 3233 | n/a | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
|---|
| 3234 | n/a | 0, /* tp_itemsize */ |
|---|
| 3235 | n/a | 0, /* tp_dealloc */ |
|---|
| 3236 | n/a | 0, /* tp_print */ |
|---|
| 3237 | n/a | 0, /* tp_getattr */ |
|---|
| 3238 | n/a | 0, /* tp_setattr */ |
|---|
| 3239 | n/a | 0, /* tp_reserved */ |
|---|
| 3240 | n/a | 0, /* tp_repr */ |
|---|
| 3241 | n/a | 0, /* tp_as_number */ |
|---|
| 3242 | n/a | 0, /* tp_as_sequence */ |
|---|
| 3243 | n/a | 0, /* tp_as_mapping */ |
|---|
| 3244 | n/a | 0, /* tp_hash */ |
|---|
| 3245 | n/a | 0, /* tp_call */ |
|---|
| 3246 | n/a | 0, /* tp_str */ |
|---|
| 3247 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 3248 | n/a | 0, /* tp_setattro */ |
|---|
| 3249 | n/a | 0, /* tp_as_buffer */ |
|---|
| 3250 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 3251 | n/a | tzinfo_doc, /* tp_doc */ |
|---|
| 3252 | n/a | 0, /* tp_traverse */ |
|---|
| 3253 | n/a | 0, /* tp_clear */ |
|---|
| 3254 | n/a | 0, /* tp_richcompare */ |
|---|
| 3255 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 3256 | n/a | 0, /* tp_iter */ |
|---|
| 3257 | n/a | 0, /* tp_iternext */ |
|---|
| 3258 | n/a | tzinfo_methods, /* tp_methods */ |
|---|
| 3259 | n/a | 0, /* tp_members */ |
|---|
| 3260 | n/a | 0, /* tp_getset */ |
|---|
| 3261 | n/a | 0, /* tp_base */ |
|---|
| 3262 | n/a | 0, /* tp_dict */ |
|---|
| 3263 | n/a | 0, /* tp_descr_get */ |
|---|
| 3264 | n/a | 0, /* tp_descr_set */ |
|---|
| 3265 | n/a | 0, /* tp_dictoffset */ |
|---|
| 3266 | n/a | 0, /* tp_init */ |
|---|
| 3267 | n/a | 0, /* tp_alloc */ |
|---|
| 3268 | n/a | PyType_GenericNew, /* tp_new */ |
|---|
| 3269 | n/a | 0, /* tp_free */ |
|---|
| 3270 | n/a | }; |
|---|
| 3271 | n/a | |
|---|
| 3272 | n/a | static char *timezone_kws[] = {"offset", "name", NULL}; |
|---|
| 3273 | n/a | |
|---|
| 3274 | n/a | static PyObject * |
|---|
| 3275 | n/a | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|---|
| 3276 | n/a | { |
|---|
| 3277 | n/a | PyObject *offset; |
|---|
| 3278 | n/a | PyObject *name = NULL; |
|---|
| 3279 | n/a | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|U:timezone", timezone_kws, |
|---|
| 3280 | n/a | &PyDateTime_DeltaType, &offset, &name)) |
|---|
| 3281 | n/a | return new_timezone(offset, name); |
|---|
| 3282 | n/a | |
|---|
| 3283 | n/a | return NULL; |
|---|
| 3284 | n/a | } |
|---|
| 3285 | n/a | |
|---|
| 3286 | n/a | static void |
|---|
| 3287 | n/a | timezone_dealloc(PyDateTime_TimeZone *self) |
|---|
| 3288 | n/a | { |
|---|
| 3289 | n/a | Py_CLEAR(self->offset); |
|---|
| 3290 | n/a | Py_CLEAR(self->name); |
|---|
| 3291 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
|---|
| 3292 | n/a | } |
|---|
| 3293 | n/a | |
|---|
| 3294 | n/a | static PyObject * |
|---|
| 3295 | n/a | timezone_richcompare(PyDateTime_TimeZone *self, |
|---|
| 3296 | n/a | PyDateTime_TimeZone *other, int op) |
|---|
| 3297 | n/a | { |
|---|
| 3298 | n/a | if (op != Py_EQ && op != Py_NE) |
|---|
| 3299 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 3300 | n/a | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
|---|
| 3301 | n/a | if (op == Py_EQ) |
|---|
| 3302 | n/a | Py_RETURN_FALSE; |
|---|
| 3303 | n/a | else |
|---|
| 3304 | n/a | Py_RETURN_TRUE; |
|---|
| 3305 | n/a | } |
|---|
| 3306 | n/a | return delta_richcompare(self->offset, other->offset, op); |
|---|
| 3307 | n/a | } |
|---|
| 3308 | n/a | |
|---|
| 3309 | n/a | static Py_hash_t |
|---|
| 3310 | n/a | timezone_hash(PyDateTime_TimeZone *self) |
|---|
| 3311 | n/a | { |
|---|
| 3312 | n/a | return delta_hash((PyDateTime_Delta *)self->offset); |
|---|
| 3313 | n/a | } |
|---|
| 3314 | n/a | |
|---|
| 3315 | n/a | /* Check argument type passed to tzname, utcoffset, or dst methods. |
|---|
| 3316 | n/a | Returns 0 for good argument. Returns -1 and sets exception info |
|---|
| 3317 | n/a | otherwise. |
|---|
| 3318 | n/a | */ |
|---|
| 3319 | n/a | static int |
|---|
| 3320 | n/a | _timezone_check_argument(PyObject *dt, const char *meth) |
|---|
| 3321 | n/a | { |
|---|
| 3322 | n/a | if (dt == Py_None || PyDateTime_Check(dt)) |
|---|
| 3323 | n/a | return 0; |
|---|
| 3324 | n/a | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
|---|
| 3325 | n/a | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
|---|
| 3326 | n/a | return -1; |
|---|
| 3327 | n/a | } |
|---|
| 3328 | n/a | |
|---|
| 3329 | n/a | static PyObject * |
|---|
| 3330 | n/a | timezone_repr(PyDateTime_TimeZone *self) |
|---|
| 3331 | n/a | { |
|---|
| 3332 | n/a | /* Note that although timezone is not subclassable, it is convenient |
|---|
| 3333 | n/a | to use Py_TYPE(self)->tp_name here. */ |
|---|
| 3334 | n/a | const char *type_name = Py_TYPE(self)->tp_name; |
|---|
| 3335 | n/a | |
|---|
| 3336 | n/a | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
|---|
| 3337 | n/a | return PyUnicode_FromFormat("%s.utc", type_name); |
|---|
| 3338 | n/a | |
|---|
| 3339 | n/a | if (self->name == NULL) |
|---|
| 3340 | n/a | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
|---|
| 3341 | n/a | |
|---|
| 3342 | n/a | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
|---|
| 3343 | n/a | self->name); |
|---|
| 3344 | n/a | } |
|---|
| 3345 | n/a | |
|---|
| 3346 | n/a | |
|---|
| 3347 | n/a | static PyObject * |
|---|
| 3348 | n/a | timezone_str(PyDateTime_TimeZone *self) |
|---|
| 3349 | n/a | { |
|---|
| 3350 | n/a | int hours, minutes, seconds; |
|---|
| 3351 | n/a | PyObject *offset; |
|---|
| 3352 | n/a | char sign; |
|---|
| 3353 | n/a | |
|---|
| 3354 | n/a | if (self->name != NULL) { |
|---|
| 3355 | n/a | Py_INCREF(self->name); |
|---|
| 3356 | n/a | return self->name; |
|---|
| 3357 | n/a | } |
|---|
| 3358 | n/a | if ((PyObject *)self == PyDateTime_TimeZone_UTC || |
|---|
| 3359 | n/a | (GET_TD_DAYS(self->offset) == 0 && |
|---|
| 3360 | n/a | GET_TD_SECONDS(self->offset) == 0 && |
|---|
| 3361 | n/a | GET_TD_MICROSECONDS(self->offset) == 0)) |
|---|
| 3362 | n/a | return PyUnicode_FromString("UTC"); |
|---|
| 3363 | n/a | /* Offset is normalized, so it is negative if days < 0 */ |
|---|
| 3364 | n/a | if (GET_TD_DAYS(self->offset) < 0) { |
|---|
| 3365 | n/a | sign = '-'; |
|---|
| 3366 | n/a | offset = delta_negative((PyDateTime_Delta *)self->offset); |
|---|
| 3367 | n/a | if (offset == NULL) |
|---|
| 3368 | n/a | return NULL; |
|---|
| 3369 | n/a | } |
|---|
| 3370 | n/a | else { |
|---|
| 3371 | n/a | sign = '+'; |
|---|
| 3372 | n/a | offset = self->offset; |
|---|
| 3373 | n/a | Py_INCREF(offset); |
|---|
| 3374 | n/a | } |
|---|
| 3375 | n/a | /* Offset is not negative here. */ |
|---|
| 3376 | n/a | seconds = GET_TD_SECONDS(offset); |
|---|
| 3377 | n/a | Py_DECREF(offset); |
|---|
| 3378 | n/a | minutes = divmod(seconds, 60, &seconds); |
|---|
| 3379 | n/a | hours = divmod(minutes, 60, &minutes); |
|---|
| 3380 | n/a | /* XXX ignore sub-minute data, currently not allowed. */ |
|---|
| 3381 | n/a | assert(seconds == 0); |
|---|
| 3382 | n/a | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
|---|
| 3383 | n/a | } |
|---|
| 3384 | n/a | |
|---|
| 3385 | n/a | static PyObject * |
|---|
| 3386 | n/a | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
|---|
| 3387 | n/a | { |
|---|
| 3388 | n/a | if (_timezone_check_argument(dt, "tzname") == -1) |
|---|
| 3389 | n/a | return NULL; |
|---|
| 3390 | n/a | |
|---|
| 3391 | n/a | return timezone_str(self); |
|---|
| 3392 | n/a | } |
|---|
| 3393 | n/a | |
|---|
| 3394 | n/a | static PyObject * |
|---|
| 3395 | n/a | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
|---|
| 3396 | n/a | { |
|---|
| 3397 | n/a | if (_timezone_check_argument(dt, "utcoffset") == -1) |
|---|
| 3398 | n/a | return NULL; |
|---|
| 3399 | n/a | |
|---|
| 3400 | n/a | Py_INCREF(self->offset); |
|---|
| 3401 | n/a | return self->offset; |
|---|
| 3402 | n/a | } |
|---|
| 3403 | n/a | |
|---|
| 3404 | n/a | static PyObject * |
|---|
| 3405 | n/a | timezone_dst(PyObject *self, PyObject *dt) |
|---|
| 3406 | n/a | { |
|---|
| 3407 | n/a | if (_timezone_check_argument(dt, "dst") == -1) |
|---|
| 3408 | n/a | return NULL; |
|---|
| 3409 | n/a | |
|---|
| 3410 | n/a | Py_RETURN_NONE; |
|---|
| 3411 | n/a | } |
|---|
| 3412 | n/a | |
|---|
| 3413 | n/a | static PyObject * |
|---|
| 3414 | n/a | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
|---|
| 3415 | n/a | { |
|---|
| 3416 | n/a | if (!PyDateTime_Check(dt)) { |
|---|
| 3417 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3418 | n/a | "fromutc: argument must be a datetime"); |
|---|
| 3419 | n/a | return NULL; |
|---|
| 3420 | n/a | } |
|---|
| 3421 | n/a | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
|---|
| 3422 | n/a | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
|---|
| 3423 | n/a | "is not self"); |
|---|
| 3424 | n/a | return NULL; |
|---|
| 3425 | n/a | } |
|---|
| 3426 | n/a | |
|---|
| 3427 | n/a | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
|---|
| 3428 | n/a | } |
|---|
| 3429 | n/a | |
|---|
| 3430 | n/a | static PyObject * |
|---|
| 3431 | n/a | timezone_getinitargs(PyDateTime_TimeZone *self) |
|---|
| 3432 | n/a | { |
|---|
| 3433 | n/a | if (self->name == NULL) |
|---|
| 3434 | n/a | return Py_BuildValue("(O)", self->offset); |
|---|
| 3435 | n/a | return Py_BuildValue("(OO)", self->offset, self->name); |
|---|
| 3436 | n/a | } |
|---|
| 3437 | n/a | |
|---|
| 3438 | n/a | static PyMethodDef timezone_methods[] = { |
|---|
| 3439 | n/a | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
|---|
| 3440 | n/a | PyDoc_STR("If name is specified when timezone is created, returns the name." |
|---|
| 3441 | n/a | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
|---|
| 3442 | n/a | |
|---|
| 3443 | n/a | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
|---|
| 3444 | n/a | PyDoc_STR("Return fixed offset.")}, |
|---|
| 3445 | n/a | |
|---|
| 3446 | n/a | {"dst", (PyCFunction)timezone_dst, METH_O, |
|---|
| 3447 | n/a | PyDoc_STR("Return None.")}, |
|---|
| 3448 | n/a | |
|---|
| 3449 | n/a | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
|---|
| 3450 | n/a | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
|---|
| 3451 | n/a | |
|---|
| 3452 | n/a | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
|---|
| 3453 | n/a | PyDoc_STR("pickle support")}, |
|---|
| 3454 | n/a | |
|---|
| 3455 | n/a | {NULL, NULL} |
|---|
| 3456 | n/a | }; |
|---|
| 3457 | n/a | |
|---|
| 3458 | n/a | static const char timezone_doc[] = |
|---|
| 3459 | n/a | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
|---|
| 3460 | n/a | |
|---|
| 3461 | n/a | static PyTypeObject PyDateTime_TimeZoneType = { |
|---|
| 3462 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 3463 | n/a | "datetime.timezone", /* tp_name */ |
|---|
| 3464 | n/a | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
|---|
| 3465 | n/a | 0, /* tp_itemsize */ |
|---|
| 3466 | n/a | (destructor)timezone_dealloc, /* tp_dealloc */ |
|---|
| 3467 | n/a | 0, /* tp_print */ |
|---|
| 3468 | n/a | 0, /* tp_getattr */ |
|---|
| 3469 | n/a | 0, /* tp_setattr */ |
|---|
| 3470 | n/a | 0, /* tp_reserved */ |
|---|
| 3471 | n/a | (reprfunc)timezone_repr, /* tp_repr */ |
|---|
| 3472 | n/a | 0, /* tp_as_number */ |
|---|
| 3473 | n/a | 0, /* tp_as_sequence */ |
|---|
| 3474 | n/a | 0, /* tp_as_mapping */ |
|---|
| 3475 | n/a | (hashfunc)timezone_hash, /* tp_hash */ |
|---|
| 3476 | n/a | 0, /* tp_call */ |
|---|
| 3477 | n/a | (reprfunc)timezone_str, /* tp_str */ |
|---|
| 3478 | n/a | 0, /* tp_getattro */ |
|---|
| 3479 | n/a | 0, /* tp_setattro */ |
|---|
| 3480 | n/a | 0, /* tp_as_buffer */ |
|---|
| 3481 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|---|
| 3482 | n/a | timezone_doc, /* tp_doc */ |
|---|
| 3483 | n/a | 0, /* tp_traverse */ |
|---|
| 3484 | n/a | 0, /* tp_clear */ |
|---|
| 3485 | n/a | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
|---|
| 3486 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 3487 | n/a | 0, /* tp_iter */ |
|---|
| 3488 | n/a | 0, /* tp_iternext */ |
|---|
| 3489 | n/a | timezone_methods, /* tp_methods */ |
|---|
| 3490 | n/a | 0, /* tp_members */ |
|---|
| 3491 | n/a | 0, /* tp_getset */ |
|---|
| 3492 | n/a | &PyDateTime_TZInfoType, /* tp_base */ |
|---|
| 3493 | n/a | 0, /* tp_dict */ |
|---|
| 3494 | n/a | 0, /* tp_descr_get */ |
|---|
| 3495 | n/a | 0, /* tp_descr_set */ |
|---|
| 3496 | n/a | 0, /* tp_dictoffset */ |
|---|
| 3497 | n/a | 0, /* tp_init */ |
|---|
| 3498 | n/a | 0, /* tp_alloc */ |
|---|
| 3499 | n/a | timezone_new, /* tp_new */ |
|---|
| 3500 | n/a | }; |
|---|
| 3501 | n/a | |
|---|
| 3502 | n/a | /* |
|---|
| 3503 | n/a | * PyDateTime_Time implementation. |
|---|
| 3504 | n/a | */ |
|---|
| 3505 | n/a | |
|---|
| 3506 | n/a | /* Accessor properties. |
|---|
| 3507 | n/a | */ |
|---|
| 3508 | n/a | |
|---|
| 3509 | n/a | static PyObject * |
|---|
| 3510 | n/a | time_hour(PyDateTime_Time *self, void *unused) |
|---|
| 3511 | n/a | { |
|---|
| 3512 | n/a | return PyLong_FromLong(TIME_GET_HOUR(self)); |
|---|
| 3513 | n/a | } |
|---|
| 3514 | n/a | |
|---|
| 3515 | n/a | static PyObject * |
|---|
| 3516 | n/a | time_minute(PyDateTime_Time *self, void *unused) |
|---|
| 3517 | n/a | { |
|---|
| 3518 | n/a | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
|---|
| 3519 | n/a | } |
|---|
| 3520 | n/a | |
|---|
| 3521 | n/a | /* The name time_second conflicted with some platform header file. */ |
|---|
| 3522 | n/a | static PyObject * |
|---|
| 3523 | n/a | py_time_second(PyDateTime_Time *self, void *unused) |
|---|
| 3524 | n/a | { |
|---|
| 3525 | n/a | return PyLong_FromLong(TIME_GET_SECOND(self)); |
|---|
| 3526 | n/a | } |
|---|
| 3527 | n/a | |
|---|
| 3528 | n/a | static PyObject * |
|---|
| 3529 | n/a | time_microsecond(PyDateTime_Time *self, void *unused) |
|---|
| 3530 | n/a | { |
|---|
| 3531 | n/a | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
|---|
| 3532 | n/a | } |
|---|
| 3533 | n/a | |
|---|
| 3534 | n/a | static PyObject * |
|---|
| 3535 | n/a | time_tzinfo(PyDateTime_Time *self, void *unused) |
|---|
| 3536 | n/a | { |
|---|
| 3537 | n/a | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
|---|
| 3538 | n/a | Py_INCREF(result); |
|---|
| 3539 | n/a | return result; |
|---|
| 3540 | n/a | } |
|---|
| 3541 | n/a | |
|---|
| 3542 | n/a | static PyObject * |
|---|
| 3543 | n/a | time_fold(PyDateTime_Time *self, void *unused) |
|---|
| 3544 | n/a | { |
|---|
| 3545 | n/a | return PyLong_FromLong(TIME_GET_FOLD(self)); |
|---|
| 3546 | n/a | } |
|---|
| 3547 | n/a | |
|---|
| 3548 | n/a | static PyGetSetDef time_getset[] = { |
|---|
| 3549 | n/a | {"hour", (getter)time_hour}, |
|---|
| 3550 | n/a | {"minute", (getter)time_minute}, |
|---|
| 3551 | n/a | {"second", (getter)py_time_second}, |
|---|
| 3552 | n/a | {"microsecond", (getter)time_microsecond}, |
|---|
| 3553 | n/a | {"tzinfo", (getter)time_tzinfo}, |
|---|
| 3554 | n/a | {"fold", (getter)time_fold}, |
|---|
| 3555 | n/a | {NULL} |
|---|
| 3556 | n/a | }; |
|---|
| 3557 | n/a | |
|---|
| 3558 | n/a | /* |
|---|
| 3559 | n/a | * Constructors. |
|---|
| 3560 | n/a | */ |
|---|
| 3561 | n/a | |
|---|
| 3562 | n/a | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
|---|
| 3563 | n/a | "tzinfo", "fold", NULL}; |
|---|
| 3564 | n/a | |
|---|
| 3565 | n/a | static PyObject * |
|---|
| 3566 | n/a | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|---|
| 3567 | n/a | { |
|---|
| 3568 | n/a | PyObject *self = NULL; |
|---|
| 3569 | n/a | PyObject *state; |
|---|
| 3570 | n/a | int hour = 0; |
|---|
| 3571 | n/a | int minute = 0; |
|---|
| 3572 | n/a | int second = 0; |
|---|
| 3573 | n/a | int usecond = 0; |
|---|
| 3574 | n/a | PyObject *tzinfo = Py_None; |
|---|
| 3575 | n/a | int fold = 0; |
|---|
| 3576 | n/a | |
|---|
| 3577 | n/a | /* Check for invocation from pickle with __getstate__ state */ |
|---|
| 3578 | n/a | if (PyTuple_GET_SIZE(args) >= 1 && |
|---|
| 3579 | n/a | PyTuple_GET_SIZE(args) <= 2 && |
|---|
| 3580 | n/a | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
|---|
| 3581 | n/a | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
|---|
| 3582 | n/a | (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) |
|---|
| 3583 | n/a | { |
|---|
| 3584 | n/a | PyDateTime_Time *me; |
|---|
| 3585 | n/a | char aware; |
|---|
| 3586 | n/a | |
|---|
| 3587 | n/a | if (PyTuple_GET_SIZE(args) == 2) { |
|---|
| 3588 | n/a | tzinfo = PyTuple_GET_ITEM(args, 1); |
|---|
| 3589 | n/a | if (check_tzinfo_subclass(tzinfo) < 0) { |
|---|
| 3590 | n/a | PyErr_SetString(PyExc_TypeError, "bad " |
|---|
| 3591 | n/a | "tzinfo state arg"); |
|---|
| 3592 | n/a | return NULL; |
|---|
| 3593 | n/a | } |
|---|
| 3594 | n/a | } |
|---|
| 3595 | n/a | aware = (char)(tzinfo != Py_None); |
|---|
| 3596 | n/a | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
|---|
| 3597 | n/a | if (me != NULL) { |
|---|
| 3598 | n/a | char *pdata = PyBytes_AS_STRING(state); |
|---|
| 3599 | n/a | |
|---|
| 3600 | n/a | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
|---|
| 3601 | n/a | me->hashcode = -1; |
|---|
| 3602 | n/a | me->hastzinfo = aware; |
|---|
| 3603 | n/a | if (aware) { |
|---|
| 3604 | n/a | Py_INCREF(tzinfo); |
|---|
| 3605 | n/a | me->tzinfo = tzinfo; |
|---|
| 3606 | n/a | } |
|---|
| 3607 | n/a | if (pdata[0] & (1 << 7)) { |
|---|
| 3608 | n/a | me->data[0] -= 128; |
|---|
| 3609 | n/a | me->fold = 1; |
|---|
| 3610 | n/a | } |
|---|
| 3611 | n/a | else { |
|---|
| 3612 | n/a | me->fold = 0; |
|---|
| 3613 | n/a | } |
|---|
| 3614 | n/a | } |
|---|
| 3615 | n/a | return (PyObject *)me; |
|---|
| 3616 | n/a | } |
|---|
| 3617 | n/a | |
|---|
| 3618 | n/a | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, |
|---|
| 3619 | n/a | &hour, &minute, &second, &usecond, |
|---|
| 3620 | n/a | &tzinfo, &fold)) { |
|---|
| 3621 | n/a | self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, |
|---|
| 3622 | n/a | type); |
|---|
| 3623 | n/a | } |
|---|
| 3624 | n/a | return self; |
|---|
| 3625 | n/a | } |
|---|
| 3626 | n/a | |
|---|
| 3627 | n/a | /* |
|---|
| 3628 | n/a | * Destructor. |
|---|
| 3629 | n/a | */ |
|---|
| 3630 | n/a | |
|---|
| 3631 | n/a | static void |
|---|
| 3632 | n/a | time_dealloc(PyDateTime_Time *self) |
|---|
| 3633 | n/a | { |
|---|
| 3634 | n/a | if (HASTZINFO(self)) { |
|---|
| 3635 | n/a | Py_XDECREF(self->tzinfo); |
|---|
| 3636 | n/a | } |
|---|
| 3637 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
|---|
| 3638 | n/a | } |
|---|
| 3639 | n/a | |
|---|
| 3640 | n/a | /* |
|---|
| 3641 | n/a | * Indirect access to tzinfo methods. |
|---|
| 3642 | n/a | */ |
|---|
| 3643 | n/a | |
|---|
| 3644 | n/a | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
|---|
| 3645 | n/a | static PyObject * |
|---|
| 3646 | n/a | time_utcoffset(PyObject *self, PyObject *unused) { |
|---|
| 3647 | n/a | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
|---|
| 3648 | n/a | } |
|---|
| 3649 | n/a | |
|---|
| 3650 | n/a | static PyObject * |
|---|
| 3651 | n/a | time_dst(PyObject *self, PyObject *unused) { |
|---|
| 3652 | n/a | return call_dst(GET_TIME_TZINFO(self), Py_None); |
|---|
| 3653 | n/a | } |
|---|
| 3654 | n/a | |
|---|
| 3655 | n/a | static PyObject * |
|---|
| 3656 | n/a | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
|---|
| 3657 | n/a | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
|---|
| 3658 | n/a | } |
|---|
| 3659 | n/a | |
|---|
| 3660 | n/a | /* |
|---|
| 3661 | n/a | * Various ways to turn a time into a string. |
|---|
| 3662 | n/a | */ |
|---|
| 3663 | n/a | |
|---|
| 3664 | n/a | static PyObject * |
|---|
| 3665 | n/a | time_repr(PyDateTime_Time *self) |
|---|
| 3666 | n/a | { |
|---|
| 3667 | n/a | const char *type_name = Py_TYPE(self)->tp_name; |
|---|
| 3668 | n/a | int h = TIME_GET_HOUR(self); |
|---|
| 3669 | n/a | int m = TIME_GET_MINUTE(self); |
|---|
| 3670 | n/a | int s = TIME_GET_SECOND(self); |
|---|
| 3671 | n/a | int us = TIME_GET_MICROSECOND(self); |
|---|
| 3672 | n/a | int fold = TIME_GET_FOLD(self); |
|---|
| 3673 | n/a | PyObject *result = NULL; |
|---|
| 3674 | n/a | |
|---|
| 3675 | n/a | if (us) |
|---|
| 3676 | n/a | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
|---|
| 3677 | n/a | type_name, h, m, s, us); |
|---|
| 3678 | n/a | else if (s) |
|---|
| 3679 | n/a | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
|---|
| 3680 | n/a | type_name, h, m, s); |
|---|
| 3681 | n/a | else |
|---|
| 3682 | n/a | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
|---|
| 3683 | n/a | if (result != NULL && HASTZINFO(self)) |
|---|
| 3684 | n/a | result = append_keyword_tzinfo(result, self->tzinfo); |
|---|
| 3685 | n/a | if (result != NULL && fold) |
|---|
| 3686 | n/a | result = append_keyword_fold(result, fold); |
|---|
| 3687 | n/a | return result; |
|---|
| 3688 | n/a | } |
|---|
| 3689 | n/a | |
|---|
| 3690 | n/a | static PyObject * |
|---|
| 3691 | n/a | time_str(PyDateTime_Time *self) |
|---|
| 3692 | n/a | { |
|---|
| 3693 | n/a | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
|---|
| 3694 | n/a | } |
|---|
| 3695 | n/a | |
|---|
| 3696 | n/a | static PyObject * |
|---|
| 3697 | n/a | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
|---|
| 3698 | n/a | { |
|---|
| 3699 | n/a | char buf[100]; |
|---|
| 3700 | n/a | char *timespec = NULL; |
|---|
| 3701 | n/a | static char *keywords[] = {"timespec", NULL}; |
|---|
| 3702 | n/a | PyObject *result; |
|---|
| 3703 | n/a | int us = TIME_GET_MICROSECOND(self); |
|---|
| 3704 | n/a | static char *specs[][2] = { |
|---|
| 3705 | n/a | {"hours", "%02d"}, |
|---|
| 3706 | n/a | {"minutes", "%02d:%02d"}, |
|---|
| 3707 | n/a | {"seconds", "%02d:%02d:%02d"}, |
|---|
| 3708 | n/a | {"milliseconds", "%02d:%02d:%02d.%03d"}, |
|---|
| 3709 | n/a | {"microseconds", "%02d:%02d:%02d.%06d"}, |
|---|
| 3710 | n/a | }; |
|---|
| 3711 | n/a | size_t given_spec; |
|---|
| 3712 | n/a | |
|---|
| 3713 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) |
|---|
| 3714 | n/a | return NULL; |
|---|
| 3715 | n/a | |
|---|
| 3716 | n/a | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
|---|
| 3717 | n/a | if (us == 0) { |
|---|
| 3718 | n/a | /* seconds */ |
|---|
| 3719 | n/a | given_spec = 2; |
|---|
| 3720 | n/a | } |
|---|
| 3721 | n/a | else { |
|---|
| 3722 | n/a | /* microseconds */ |
|---|
| 3723 | n/a | given_spec = 4; |
|---|
| 3724 | n/a | } |
|---|
| 3725 | n/a | } |
|---|
| 3726 | n/a | else { |
|---|
| 3727 | n/a | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
|---|
| 3728 | n/a | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
|---|
| 3729 | n/a | if (given_spec == 3) { |
|---|
| 3730 | n/a | /* milliseconds */ |
|---|
| 3731 | n/a | us = us / 1000; |
|---|
| 3732 | n/a | } |
|---|
| 3733 | n/a | break; |
|---|
| 3734 | n/a | } |
|---|
| 3735 | n/a | } |
|---|
| 3736 | n/a | } |
|---|
| 3737 | n/a | |
|---|
| 3738 | n/a | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
|---|
| 3739 | n/a | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
|---|
| 3740 | n/a | return NULL; |
|---|
| 3741 | n/a | } |
|---|
| 3742 | n/a | else { |
|---|
| 3743 | n/a | result = PyUnicode_FromFormat(specs[given_spec][1], |
|---|
| 3744 | n/a | TIME_GET_HOUR(self), TIME_GET_MINUTE(self), |
|---|
| 3745 | n/a | TIME_GET_SECOND(self), us); |
|---|
| 3746 | n/a | } |
|---|
| 3747 | n/a | |
|---|
| 3748 | n/a | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
|---|
| 3749 | n/a | return result; |
|---|
| 3750 | n/a | |
|---|
| 3751 | n/a | /* We need to append the UTC offset. */ |
|---|
| 3752 | n/a | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
|---|
| 3753 | n/a | Py_None) < 0) { |
|---|
| 3754 | n/a | Py_DECREF(result); |
|---|
| 3755 | n/a | return NULL; |
|---|
| 3756 | n/a | } |
|---|
| 3757 | n/a | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
|---|
| 3758 | n/a | return result; |
|---|
| 3759 | n/a | } |
|---|
| 3760 | n/a | |
|---|
| 3761 | n/a | static PyObject * |
|---|
| 3762 | n/a | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
|---|
| 3763 | n/a | { |
|---|
| 3764 | n/a | PyObject *result; |
|---|
| 3765 | n/a | PyObject *tuple; |
|---|
| 3766 | n/a | PyObject *format; |
|---|
| 3767 | n/a | static char *keywords[] = {"format", NULL}; |
|---|
| 3768 | n/a | |
|---|
| 3769 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
|---|
| 3770 | n/a | &format)) |
|---|
| 3771 | n/a | return NULL; |
|---|
| 3772 | n/a | |
|---|
| 3773 | n/a | /* Python's strftime does insane things with the year part of the |
|---|
| 3774 | n/a | * timetuple. The year is forced to (the otherwise nonsensical) |
|---|
| 3775 | n/a | * 1900 to work around that. |
|---|
| 3776 | n/a | */ |
|---|
| 3777 | n/a | tuple = Py_BuildValue("iiiiiiiii", |
|---|
| 3778 | n/a | 1900, 1, 1, /* year, month, day */ |
|---|
| 3779 | n/a | TIME_GET_HOUR(self), |
|---|
| 3780 | n/a | TIME_GET_MINUTE(self), |
|---|
| 3781 | n/a | TIME_GET_SECOND(self), |
|---|
| 3782 | n/a | 0, 1, -1); /* weekday, daynum, dst */ |
|---|
| 3783 | n/a | if (tuple == NULL) |
|---|
| 3784 | n/a | return NULL; |
|---|
| 3785 | n/a | assert(PyTuple_Size(tuple) == 9); |
|---|
| 3786 | n/a | result = wrap_strftime((PyObject *)self, format, tuple, |
|---|
| 3787 | n/a | Py_None); |
|---|
| 3788 | n/a | Py_DECREF(tuple); |
|---|
| 3789 | n/a | return result; |
|---|
| 3790 | n/a | } |
|---|
| 3791 | n/a | |
|---|
| 3792 | n/a | /* |
|---|
| 3793 | n/a | * Miscellaneous methods. |
|---|
| 3794 | n/a | */ |
|---|
| 3795 | n/a | |
|---|
| 3796 | n/a | static PyObject * |
|---|
| 3797 | n/a | time_richcompare(PyObject *self, PyObject *other, int op) |
|---|
| 3798 | n/a | { |
|---|
| 3799 | n/a | PyObject *result = NULL; |
|---|
| 3800 | n/a | PyObject *offset1, *offset2; |
|---|
| 3801 | n/a | int diff; |
|---|
| 3802 | n/a | |
|---|
| 3803 | n/a | if (! PyTime_Check(other)) |
|---|
| 3804 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 3805 | n/a | |
|---|
| 3806 | n/a | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
|---|
| 3807 | n/a | diff = memcmp(((PyDateTime_Time *)self)->data, |
|---|
| 3808 | n/a | ((PyDateTime_Time *)other)->data, |
|---|
| 3809 | n/a | _PyDateTime_TIME_DATASIZE); |
|---|
| 3810 | n/a | return diff_to_bool(diff, op); |
|---|
| 3811 | n/a | } |
|---|
| 3812 | n/a | offset1 = time_utcoffset(self, NULL); |
|---|
| 3813 | n/a | if (offset1 == NULL) |
|---|
| 3814 | n/a | return NULL; |
|---|
| 3815 | n/a | offset2 = time_utcoffset(other, NULL); |
|---|
| 3816 | n/a | if (offset2 == NULL) |
|---|
| 3817 | n/a | goto done; |
|---|
| 3818 | n/a | /* If they're both naive, or both aware and have the same offsets, |
|---|
| 3819 | n/a | * we get off cheap. Note that if they're both naive, offset1 == |
|---|
| 3820 | n/a | * offset2 == Py_None at this point. |
|---|
| 3821 | n/a | */ |
|---|
| 3822 | n/a | if ((offset1 == offset2) || |
|---|
| 3823 | n/a | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
|---|
| 3824 | n/a | delta_cmp(offset1, offset2) == 0)) { |
|---|
| 3825 | n/a | diff = memcmp(((PyDateTime_Time *)self)->data, |
|---|
| 3826 | n/a | ((PyDateTime_Time *)other)->data, |
|---|
| 3827 | n/a | _PyDateTime_TIME_DATASIZE); |
|---|
| 3828 | n/a | result = diff_to_bool(diff, op); |
|---|
| 3829 | n/a | } |
|---|
| 3830 | n/a | /* The hard case: both aware with different UTC offsets */ |
|---|
| 3831 | n/a | else if (offset1 != Py_None && offset2 != Py_None) { |
|---|
| 3832 | n/a | int offsecs1, offsecs2; |
|---|
| 3833 | n/a | assert(offset1 != offset2); /* else last "if" handled it */ |
|---|
| 3834 | n/a | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
|---|
| 3835 | n/a | TIME_GET_MINUTE(self) * 60 + |
|---|
| 3836 | n/a | TIME_GET_SECOND(self) - |
|---|
| 3837 | n/a | GET_TD_DAYS(offset1) * 86400 - |
|---|
| 3838 | n/a | GET_TD_SECONDS(offset1); |
|---|
| 3839 | n/a | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
|---|
| 3840 | n/a | TIME_GET_MINUTE(other) * 60 + |
|---|
| 3841 | n/a | TIME_GET_SECOND(other) - |
|---|
| 3842 | n/a | GET_TD_DAYS(offset2) * 86400 - |
|---|
| 3843 | n/a | GET_TD_SECONDS(offset2); |
|---|
| 3844 | n/a | diff = offsecs1 - offsecs2; |
|---|
| 3845 | n/a | if (diff == 0) |
|---|
| 3846 | n/a | diff = TIME_GET_MICROSECOND(self) - |
|---|
| 3847 | n/a | TIME_GET_MICROSECOND(other); |
|---|
| 3848 | n/a | result = diff_to_bool(diff, op); |
|---|
| 3849 | n/a | } |
|---|
| 3850 | n/a | else if (op == Py_EQ) { |
|---|
| 3851 | n/a | result = Py_False; |
|---|
| 3852 | n/a | Py_INCREF(result); |
|---|
| 3853 | n/a | } |
|---|
| 3854 | n/a | else if (op == Py_NE) { |
|---|
| 3855 | n/a | result = Py_True; |
|---|
| 3856 | n/a | Py_INCREF(result); |
|---|
| 3857 | n/a | } |
|---|
| 3858 | n/a | else { |
|---|
| 3859 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3860 | n/a | "can't compare offset-naive and " |
|---|
| 3861 | n/a | "offset-aware times"); |
|---|
| 3862 | n/a | } |
|---|
| 3863 | n/a | done: |
|---|
| 3864 | n/a | Py_DECREF(offset1); |
|---|
| 3865 | n/a | Py_XDECREF(offset2); |
|---|
| 3866 | n/a | return result; |
|---|
| 3867 | n/a | } |
|---|
| 3868 | n/a | |
|---|
| 3869 | n/a | static Py_hash_t |
|---|
| 3870 | n/a | time_hash(PyDateTime_Time *self) |
|---|
| 3871 | n/a | { |
|---|
| 3872 | n/a | if (self->hashcode == -1) { |
|---|
| 3873 | n/a | PyObject *offset, *self0; |
|---|
| 3874 | n/a | if (TIME_GET_FOLD(self)) { |
|---|
| 3875 | n/a | self0 = new_time_ex2(TIME_GET_HOUR(self), |
|---|
| 3876 | n/a | TIME_GET_MINUTE(self), |
|---|
| 3877 | n/a | TIME_GET_SECOND(self), |
|---|
| 3878 | n/a | TIME_GET_MICROSECOND(self), |
|---|
| 3879 | n/a | HASTZINFO(self) ? self->tzinfo : Py_None, |
|---|
| 3880 | n/a | 0, Py_TYPE(self)); |
|---|
| 3881 | n/a | if (self0 == NULL) |
|---|
| 3882 | n/a | return -1; |
|---|
| 3883 | n/a | } |
|---|
| 3884 | n/a | else { |
|---|
| 3885 | n/a | self0 = (PyObject *)self; |
|---|
| 3886 | n/a | Py_INCREF(self0); |
|---|
| 3887 | n/a | } |
|---|
| 3888 | n/a | offset = time_utcoffset(self0, NULL); |
|---|
| 3889 | n/a | Py_DECREF(self0); |
|---|
| 3890 | n/a | |
|---|
| 3891 | n/a | if (offset == NULL) |
|---|
| 3892 | n/a | return -1; |
|---|
| 3893 | n/a | |
|---|
| 3894 | n/a | /* Reduce this to a hash of another object. */ |
|---|
| 3895 | n/a | if (offset == Py_None) |
|---|
| 3896 | n/a | self->hashcode = generic_hash( |
|---|
| 3897 | n/a | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
|---|
| 3898 | n/a | else { |
|---|
| 3899 | n/a | PyObject *temp1, *temp2; |
|---|
| 3900 | n/a | int seconds, microseconds; |
|---|
| 3901 | n/a | assert(HASTZINFO(self)); |
|---|
| 3902 | n/a | seconds = TIME_GET_HOUR(self) * 3600 + |
|---|
| 3903 | n/a | TIME_GET_MINUTE(self) * 60 + |
|---|
| 3904 | n/a | TIME_GET_SECOND(self); |
|---|
| 3905 | n/a | microseconds = TIME_GET_MICROSECOND(self); |
|---|
| 3906 | n/a | temp1 = new_delta(0, seconds, microseconds, 1); |
|---|
| 3907 | n/a | if (temp1 == NULL) { |
|---|
| 3908 | n/a | Py_DECREF(offset); |
|---|
| 3909 | n/a | return -1; |
|---|
| 3910 | n/a | } |
|---|
| 3911 | n/a | temp2 = delta_subtract(temp1, offset); |
|---|
| 3912 | n/a | Py_DECREF(temp1); |
|---|
| 3913 | n/a | if (temp2 == NULL) { |
|---|
| 3914 | n/a | Py_DECREF(offset); |
|---|
| 3915 | n/a | return -1; |
|---|
| 3916 | n/a | } |
|---|
| 3917 | n/a | self->hashcode = PyObject_Hash(temp2); |
|---|
| 3918 | n/a | Py_DECREF(temp2); |
|---|
| 3919 | n/a | } |
|---|
| 3920 | n/a | Py_DECREF(offset); |
|---|
| 3921 | n/a | } |
|---|
| 3922 | n/a | return self->hashcode; |
|---|
| 3923 | n/a | } |
|---|
| 3924 | n/a | |
|---|
| 3925 | n/a | static PyObject * |
|---|
| 3926 | n/a | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
|---|
| 3927 | n/a | { |
|---|
| 3928 | n/a | PyObject *clone; |
|---|
| 3929 | n/a | PyObject *tuple; |
|---|
| 3930 | n/a | int hh = TIME_GET_HOUR(self); |
|---|
| 3931 | n/a | int mm = TIME_GET_MINUTE(self); |
|---|
| 3932 | n/a | int ss = TIME_GET_SECOND(self); |
|---|
| 3933 | n/a | int us = TIME_GET_MICROSECOND(self); |
|---|
| 3934 | n/a | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
|---|
| 3935 | n/a | int fold = TIME_GET_FOLD(self); |
|---|
| 3936 | n/a | |
|---|
| 3937 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", |
|---|
| 3938 | n/a | time_kws, |
|---|
| 3939 | n/a | &hh, &mm, &ss, &us, &tzinfo, &fold)) |
|---|
| 3940 | n/a | return NULL; |
|---|
| 3941 | n/a | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
|---|
| 3942 | n/a | if (tuple == NULL) |
|---|
| 3943 | n/a | return NULL; |
|---|
| 3944 | n/a | clone = time_new(Py_TYPE(self), tuple, NULL); |
|---|
| 3945 | n/a | if (clone != NULL) { |
|---|
| 3946 | n/a | if (fold != 0 && fold != 1) { |
|---|
| 3947 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 3948 | n/a | "fold must be either 0 or 1"); |
|---|
| 3949 | n/a | return NULL; |
|---|
| 3950 | n/a | } |
|---|
| 3951 | n/a | TIME_SET_FOLD(clone, fold); |
|---|
| 3952 | n/a | } |
|---|
| 3953 | n/a | Py_DECREF(tuple); |
|---|
| 3954 | n/a | return clone; |
|---|
| 3955 | n/a | } |
|---|
| 3956 | n/a | |
|---|
| 3957 | n/a | /* Pickle support, a simple use of __reduce__. */ |
|---|
| 3958 | n/a | |
|---|
| 3959 | n/a | /* Let basestate be the non-tzinfo data string. |
|---|
| 3960 | n/a | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
|---|
| 3961 | n/a | * So it's a tuple in any (non-error) case. |
|---|
| 3962 | n/a | * __getstate__ isn't exposed. |
|---|
| 3963 | n/a | */ |
|---|
| 3964 | n/a | static PyObject * |
|---|
| 3965 | n/a | time_getstate(PyDateTime_Time *self, int proto) |
|---|
| 3966 | n/a | { |
|---|
| 3967 | n/a | PyObject *basestate; |
|---|
| 3968 | n/a | PyObject *result = NULL; |
|---|
| 3969 | n/a | |
|---|
| 3970 | n/a | basestate = PyBytes_FromStringAndSize((char *)self->data, |
|---|
| 3971 | n/a | _PyDateTime_TIME_DATASIZE); |
|---|
| 3972 | n/a | if (basestate != NULL) { |
|---|
| 3973 | n/a | if (proto > 3 && TIME_GET_FOLD(self)) |
|---|
| 3974 | n/a | /* Set the first bit of the first byte */ |
|---|
| 3975 | n/a | PyBytes_AS_STRING(basestate)[0] |= (1 << 7); |
|---|
| 3976 | n/a | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
|---|
| 3977 | n/a | result = PyTuple_Pack(1, basestate); |
|---|
| 3978 | n/a | else |
|---|
| 3979 | n/a | result = PyTuple_Pack(2, basestate, self->tzinfo); |
|---|
| 3980 | n/a | Py_DECREF(basestate); |
|---|
| 3981 | n/a | } |
|---|
| 3982 | n/a | return result; |
|---|
| 3983 | n/a | } |
|---|
| 3984 | n/a | |
|---|
| 3985 | n/a | static PyObject * |
|---|
| 3986 | n/a | time_reduce_ex(PyDateTime_Time *self, PyObject *args) |
|---|
| 3987 | n/a | { |
|---|
| 3988 | n/a | int proto; |
|---|
| 3989 | n/a | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
|---|
| 3990 | n/a | return NULL; |
|---|
| 3991 | n/a | |
|---|
| 3992 | n/a | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); |
|---|
| 3993 | n/a | } |
|---|
| 3994 | n/a | |
|---|
| 3995 | n/a | static PyObject * |
|---|
| 3996 | n/a | time_reduce(PyDateTime_Time *self, PyObject *arg) |
|---|
| 3997 | n/a | { |
|---|
| 3998 | n/a | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); |
|---|
| 3999 | n/a | } |
|---|
| 4000 | n/a | |
|---|
| 4001 | n/a | static PyMethodDef time_methods[] = { |
|---|
| 4002 | n/a | |
|---|
| 4003 | n/a | {"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS, |
|---|
| 4004 | n/a | PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" |
|---|
| 4005 | n/a | "[+HH:MM].\n\n" |
|---|
| 4006 | n/a | "timespec specifies what components of the time to include.\n")}, |
|---|
| 4007 | n/a | |
|---|
| 4008 | n/a | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
|---|
| 4009 | n/a | PyDoc_STR("format -> strftime() style string.")}, |
|---|
| 4010 | n/a | |
|---|
| 4011 | n/a | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
|---|
| 4012 | n/a | PyDoc_STR("Formats self with strftime.")}, |
|---|
| 4013 | n/a | |
|---|
| 4014 | n/a | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
|---|
| 4015 | n/a | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
|---|
| 4016 | n/a | |
|---|
| 4017 | n/a | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
|---|
| 4018 | n/a | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
|---|
| 4019 | n/a | |
|---|
| 4020 | n/a | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
|---|
| 4021 | n/a | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
|---|
| 4022 | n/a | |
|---|
| 4023 | n/a | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
|---|
| 4024 | n/a | PyDoc_STR("Return time with new specified fields.")}, |
|---|
| 4025 | n/a | |
|---|
| 4026 | n/a | {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS, |
|---|
| 4027 | n/a | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
|---|
| 4028 | n/a | |
|---|
| 4029 | n/a | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
|---|
| 4030 | n/a | PyDoc_STR("__reduce__() -> (cls, state)")}, |
|---|
| 4031 | n/a | |
|---|
| 4032 | n/a | {NULL, NULL} |
|---|
| 4033 | n/a | }; |
|---|
| 4034 | n/a | |
|---|
| 4035 | n/a | static const char time_doc[] = |
|---|
| 4036 | n/a | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
|---|
| 4037 | n/a | \n\ |
|---|
| 4038 | n/a | All arguments are optional. tzinfo may be None, or an instance of\n\ |
|---|
| 4039 | n/a | a tzinfo subclass. The remaining arguments may be ints.\n"); |
|---|
| 4040 | n/a | |
|---|
| 4041 | n/a | static PyTypeObject PyDateTime_TimeType = { |
|---|
| 4042 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 4043 | n/a | "datetime.time", /* tp_name */ |
|---|
| 4044 | n/a | sizeof(PyDateTime_Time), /* tp_basicsize */ |
|---|
| 4045 | n/a | 0, /* tp_itemsize */ |
|---|
| 4046 | n/a | (destructor)time_dealloc, /* tp_dealloc */ |
|---|
| 4047 | n/a | 0, /* tp_print */ |
|---|
| 4048 | n/a | 0, /* tp_getattr */ |
|---|
| 4049 | n/a | 0, /* tp_setattr */ |
|---|
| 4050 | n/a | 0, /* tp_reserved */ |
|---|
| 4051 | n/a | (reprfunc)time_repr, /* tp_repr */ |
|---|
| 4052 | n/a | 0, /* tp_as_number */ |
|---|
| 4053 | n/a | 0, /* tp_as_sequence */ |
|---|
| 4054 | n/a | 0, /* tp_as_mapping */ |
|---|
| 4055 | n/a | (hashfunc)time_hash, /* tp_hash */ |
|---|
| 4056 | n/a | 0, /* tp_call */ |
|---|
| 4057 | n/a | (reprfunc)time_str, /* tp_str */ |
|---|
| 4058 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 4059 | n/a | 0, /* tp_setattro */ |
|---|
| 4060 | n/a | 0, /* tp_as_buffer */ |
|---|
| 4061 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 4062 | n/a | time_doc, /* tp_doc */ |
|---|
| 4063 | n/a | 0, /* tp_traverse */ |
|---|
| 4064 | n/a | 0, /* tp_clear */ |
|---|
| 4065 | n/a | time_richcompare, /* tp_richcompare */ |
|---|
| 4066 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 4067 | n/a | 0, /* tp_iter */ |
|---|
| 4068 | n/a | 0, /* tp_iternext */ |
|---|
| 4069 | n/a | time_methods, /* tp_methods */ |
|---|
| 4070 | n/a | 0, /* tp_members */ |
|---|
| 4071 | n/a | time_getset, /* tp_getset */ |
|---|
| 4072 | n/a | 0, /* tp_base */ |
|---|
| 4073 | n/a | 0, /* tp_dict */ |
|---|
| 4074 | n/a | 0, /* tp_descr_get */ |
|---|
| 4075 | n/a | 0, /* tp_descr_set */ |
|---|
| 4076 | n/a | 0, /* tp_dictoffset */ |
|---|
| 4077 | n/a | 0, /* tp_init */ |
|---|
| 4078 | n/a | time_alloc, /* tp_alloc */ |
|---|
| 4079 | n/a | time_new, /* tp_new */ |
|---|
| 4080 | n/a | 0, /* tp_free */ |
|---|
| 4081 | n/a | }; |
|---|
| 4082 | n/a | |
|---|
| 4083 | n/a | /* |
|---|
| 4084 | n/a | * PyDateTime_DateTime implementation. |
|---|
| 4085 | n/a | */ |
|---|
| 4086 | n/a | |
|---|
| 4087 | n/a | /* Accessor properties. Properties for day, month, and year are inherited |
|---|
| 4088 | n/a | * from date. |
|---|
| 4089 | n/a | */ |
|---|
| 4090 | n/a | |
|---|
| 4091 | n/a | static PyObject * |
|---|
| 4092 | n/a | datetime_hour(PyDateTime_DateTime *self, void *unused) |
|---|
| 4093 | n/a | { |
|---|
| 4094 | n/a | return PyLong_FromLong(DATE_GET_HOUR(self)); |
|---|
| 4095 | n/a | } |
|---|
| 4096 | n/a | |
|---|
| 4097 | n/a | static PyObject * |
|---|
| 4098 | n/a | datetime_minute(PyDateTime_DateTime *self, void *unused) |
|---|
| 4099 | n/a | { |
|---|
| 4100 | n/a | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
|---|
| 4101 | n/a | } |
|---|
| 4102 | n/a | |
|---|
| 4103 | n/a | static PyObject * |
|---|
| 4104 | n/a | datetime_second(PyDateTime_DateTime *self, void *unused) |
|---|
| 4105 | n/a | { |
|---|
| 4106 | n/a | return PyLong_FromLong(DATE_GET_SECOND(self)); |
|---|
| 4107 | n/a | } |
|---|
| 4108 | n/a | |
|---|
| 4109 | n/a | static PyObject * |
|---|
| 4110 | n/a | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
|---|
| 4111 | n/a | { |
|---|
| 4112 | n/a | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
|---|
| 4113 | n/a | } |
|---|
| 4114 | n/a | |
|---|
| 4115 | n/a | static PyObject * |
|---|
| 4116 | n/a | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
|---|
| 4117 | n/a | { |
|---|
| 4118 | n/a | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
|---|
| 4119 | n/a | Py_INCREF(result); |
|---|
| 4120 | n/a | return result; |
|---|
| 4121 | n/a | } |
|---|
| 4122 | n/a | |
|---|
| 4123 | n/a | static PyObject * |
|---|
| 4124 | n/a | datetime_fold(PyDateTime_DateTime *self, void *unused) |
|---|
| 4125 | n/a | { |
|---|
| 4126 | n/a | return PyLong_FromLong(DATE_GET_FOLD(self)); |
|---|
| 4127 | n/a | } |
|---|
| 4128 | n/a | |
|---|
| 4129 | n/a | static PyGetSetDef datetime_getset[] = { |
|---|
| 4130 | n/a | {"hour", (getter)datetime_hour}, |
|---|
| 4131 | n/a | {"minute", (getter)datetime_minute}, |
|---|
| 4132 | n/a | {"second", (getter)datetime_second}, |
|---|
| 4133 | n/a | {"microsecond", (getter)datetime_microsecond}, |
|---|
| 4134 | n/a | {"tzinfo", (getter)datetime_tzinfo}, |
|---|
| 4135 | n/a | {"fold", (getter)datetime_fold}, |
|---|
| 4136 | n/a | {NULL} |
|---|
| 4137 | n/a | }; |
|---|
| 4138 | n/a | |
|---|
| 4139 | n/a | /* |
|---|
| 4140 | n/a | * Constructors. |
|---|
| 4141 | n/a | */ |
|---|
| 4142 | n/a | |
|---|
| 4143 | n/a | static char *datetime_kws[] = { |
|---|
| 4144 | n/a | "year", "month", "day", "hour", "minute", "second", |
|---|
| 4145 | n/a | "microsecond", "tzinfo", "fold", NULL |
|---|
| 4146 | n/a | }; |
|---|
| 4147 | n/a | |
|---|
| 4148 | n/a | static PyObject * |
|---|
| 4149 | n/a | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|---|
| 4150 | n/a | { |
|---|
| 4151 | n/a | PyObject *self = NULL; |
|---|
| 4152 | n/a | PyObject *state; |
|---|
| 4153 | n/a | int year; |
|---|
| 4154 | n/a | int month; |
|---|
| 4155 | n/a | int day; |
|---|
| 4156 | n/a | int hour = 0; |
|---|
| 4157 | n/a | int minute = 0; |
|---|
| 4158 | n/a | int second = 0; |
|---|
| 4159 | n/a | int usecond = 0; |
|---|
| 4160 | n/a | int fold = 0; |
|---|
| 4161 | n/a | PyObject *tzinfo = Py_None; |
|---|
| 4162 | n/a | |
|---|
| 4163 | n/a | /* Check for invocation from pickle with __getstate__ state */ |
|---|
| 4164 | n/a | if (PyTuple_GET_SIZE(args) >= 1 && |
|---|
| 4165 | n/a | PyTuple_GET_SIZE(args) <= 2 && |
|---|
| 4166 | n/a | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
|---|
| 4167 | n/a | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
|---|
| 4168 | n/a | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) |
|---|
| 4169 | n/a | { |
|---|
| 4170 | n/a | PyDateTime_DateTime *me; |
|---|
| 4171 | n/a | char aware; |
|---|
| 4172 | n/a | |
|---|
| 4173 | n/a | if (PyTuple_GET_SIZE(args) == 2) { |
|---|
| 4174 | n/a | tzinfo = PyTuple_GET_ITEM(args, 1); |
|---|
| 4175 | n/a | if (check_tzinfo_subclass(tzinfo) < 0) { |
|---|
| 4176 | n/a | PyErr_SetString(PyExc_TypeError, "bad " |
|---|
| 4177 | n/a | "tzinfo state arg"); |
|---|
| 4178 | n/a | return NULL; |
|---|
| 4179 | n/a | } |
|---|
| 4180 | n/a | } |
|---|
| 4181 | n/a | aware = (char)(tzinfo != Py_None); |
|---|
| 4182 | n/a | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
|---|
| 4183 | n/a | if (me != NULL) { |
|---|
| 4184 | n/a | char *pdata = PyBytes_AS_STRING(state); |
|---|
| 4185 | n/a | |
|---|
| 4186 | n/a | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
|---|
| 4187 | n/a | me->hashcode = -1; |
|---|
| 4188 | n/a | me->hastzinfo = aware; |
|---|
| 4189 | n/a | if (aware) { |
|---|
| 4190 | n/a | Py_INCREF(tzinfo); |
|---|
| 4191 | n/a | me->tzinfo = tzinfo; |
|---|
| 4192 | n/a | } |
|---|
| 4193 | n/a | if (pdata[2] & (1 << 7)) { |
|---|
| 4194 | n/a | me->data[2] -= 128; |
|---|
| 4195 | n/a | me->fold = 1; |
|---|
| 4196 | n/a | } |
|---|
| 4197 | n/a | else { |
|---|
| 4198 | n/a | me->fold = 0; |
|---|
| 4199 | n/a | } |
|---|
| 4200 | n/a | } |
|---|
| 4201 | n/a | return (PyObject *)me; |
|---|
| 4202 | n/a | } |
|---|
| 4203 | n/a | |
|---|
| 4204 | n/a | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, |
|---|
| 4205 | n/a | &year, &month, &day, &hour, &minute, |
|---|
| 4206 | n/a | &second, &usecond, &tzinfo, &fold)) { |
|---|
| 4207 | n/a | self = new_datetime_ex2(year, month, day, |
|---|
| 4208 | n/a | hour, minute, second, usecond, |
|---|
| 4209 | n/a | tzinfo, fold, type); |
|---|
| 4210 | n/a | } |
|---|
| 4211 | n/a | return self; |
|---|
| 4212 | n/a | } |
|---|
| 4213 | n/a | |
|---|
| 4214 | n/a | /* TM_FUNC is the shared type of _PyTime_localtime() and |
|---|
| 4215 | n/a | * _PyTime_gmtime(). */ |
|---|
| 4216 | n/a | typedef int (*TM_FUNC)(time_t timer, struct tm*); |
|---|
| 4217 | n/a | |
|---|
| 4218 | n/a | /* As of version 2015f max fold in IANA database is |
|---|
| 4219 | n/a | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ |
|---|
| 4220 | n/a | static long long max_fold_seconds = 24 * 3600; |
|---|
| 4221 | n/a | /* NB: date(1970,1,1).toordinal() == 719163 */ |
|---|
| 4222 | n/a | static long long epoch = 719163LL * 24 * 60 * 60; |
|---|
| 4223 | n/a | |
|---|
| 4224 | n/a | static long long |
|---|
| 4225 | n/a | utc_to_seconds(int year, int month, int day, |
|---|
| 4226 | n/a | int hour, int minute, int second) |
|---|
| 4227 | n/a | { |
|---|
| 4228 | n/a | long long ordinal; |
|---|
| 4229 | n/a | |
|---|
| 4230 | n/a | /* ymd_to_ord() doesn't support year <= 0 */ |
|---|
| 4231 | n/a | if (year < MINYEAR || year > MAXYEAR) { |
|---|
| 4232 | n/a | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
|---|
| 4233 | n/a | return -1; |
|---|
| 4234 | n/a | } |
|---|
| 4235 | n/a | |
|---|
| 4236 | n/a | ordinal = ymd_to_ord(year, month, day); |
|---|
| 4237 | n/a | return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; |
|---|
| 4238 | n/a | } |
|---|
| 4239 | n/a | |
|---|
| 4240 | n/a | static long long |
|---|
| 4241 | n/a | local(long long u) |
|---|
| 4242 | n/a | { |
|---|
| 4243 | n/a | struct tm local_time; |
|---|
| 4244 | n/a | time_t t; |
|---|
| 4245 | n/a | u -= epoch; |
|---|
| 4246 | n/a | t = u; |
|---|
| 4247 | n/a | if (t != u) { |
|---|
| 4248 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 4249 | n/a | "timestamp out of range for platform time_t"); |
|---|
| 4250 | n/a | return -1; |
|---|
| 4251 | n/a | } |
|---|
| 4252 | n/a | if (_PyTime_localtime(t, &local_time) != 0) |
|---|
| 4253 | n/a | return -1; |
|---|
| 4254 | n/a | return utc_to_seconds(local_time.tm_year + 1900, |
|---|
| 4255 | n/a | local_time.tm_mon + 1, |
|---|
| 4256 | n/a | local_time.tm_mday, |
|---|
| 4257 | n/a | local_time.tm_hour, |
|---|
| 4258 | n/a | local_time.tm_min, |
|---|
| 4259 | n/a | local_time.tm_sec); |
|---|
| 4260 | n/a | } |
|---|
| 4261 | n/a | |
|---|
| 4262 | n/a | /* Internal helper. |
|---|
| 4263 | n/a | * Build datetime from a time_t and a distinct count of microseconds. |
|---|
| 4264 | n/a | * Pass localtime or gmtime for f, to control the interpretation of timet. |
|---|
| 4265 | n/a | */ |
|---|
| 4266 | n/a | static PyObject * |
|---|
| 4267 | n/a | datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, |
|---|
| 4268 | n/a | PyObject *tzinfo) |
|---|
| 4269 | n/a | { |
|---|
| 4270 | n/a | struct tm tm; |
|---|
| 4271 | n/a | int year, month, day, hour, minute, second, fold = 0; |
|---|
| 4272 | n/a | |
|---|
| 4273 | n/a | if (f(timet, &tm) != 0) |
|---|
| 4274 | n/a | return NULL; |
|---|
| 4275 | n/a | |
|---|
| 4276 | n/a | year = tm.tm_year + 1900; |
|---|
| 4277 | n/a | month = tm.tm_mon + 1; |
|---|
| 4278 | n/a | day = tm.tm_mday; |
|---|
| 4279 | n/a | hour = tm.tm_hour; |
|---|
| 4280 | n/a | minute = tm.tm_min; |
|---|
| 4281 | n/a | /* The platform localtime/gmtime may insert leap seconds, |
|---|
| 4282 | n/a | * indicated by tm.tm_sec > 59. We don't care about them, |
|---|
| 4283 | n/a | * except to the extent that passing them on to the datetime |
|---|
| 4284 | n/a | * constructor would raise ValueError for a reason that |
|---|
| 4285 | n/a | * made no sense to the user. |
|---|
| 4286 | n/a | */ |
|---|
| 4287 | n/a | second = Py_MIN(59, tm.tm_sec); |
|---|
| 4288 | n/a | |
|---|
| 4289 | n/a | /* local timezone requires to compute fold */ |
|---|
| 4290 | n/a | if (tzinfo == Py_None && f == _PyTime_localtime) { |
|---|
| 4291 | n/a | long long probe_seconds, result_seconds, transition; |
|---|
| 4292 | n/a | |
|---|
| 4293 | n/a | result_seconds = utc_to_seconds(year, month, day, |
|---|
| 4294 | n/a | hour, minute, second); |
|---|
| 4295 | n/a | /* Probe max_fold_seconds to detect a fold. */ |
|---|
| 4296 | n/a | probe_seconds = local(epoch + timet - max_fold_seconds); |
|---|
| 4297 | n/a | if (probe_seconds == -1) |
|---|
| 4298 | n/a | return NULL; |
|---|
| 4299 | n/a | transition = result_seconds - probe_seconds - max_fold_seconds; |
|---|
| 4300 | n/a | if (transition < 0) { |
|---|
| 4301 | n/a | probe_seconds = local(epoch + timet + transition); |
|---|
| 4302 | n/a | if (probe_seconds == -1) |
|---|
| 4303 | n/a | return NULL; |
|---|
| 4304 | n/a | if (probe_seconds == result_seconds) |
|---|
| 4305 | n/a | fold = 1; |
|---|
| 4306 | n/a | } |
|---|
| 4307 | n/a | } |
|---|
| 4308 | n/a | return new_datetime_ex2(year, month, day, hour, |
|---|
| 4309 | n/a | minute, second, us, tzinfo, fold, |
|---|
| 4310 | n/a | (PyTypeObject *)cls); |
|---|
| 4311 | n/a | } |
|---|
| 4312 | n/a | |
|---|
| 4313 | n/a | /* Internal helper. |
|---|
| 4314 | n/a | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
|---|
| 4315 | n/a | * to control the interpretation of the timestamp. Since a double doesn't |
|---|
| 4316 | n/a | * have enough bits to cover a datetime's full range of precision, it's |
|---|
| 4317 | n/a | * better to call datetime_from_timet_and_us provided you have a way |
|---|
| 4318 | n/a | * to get that much precision (e.g., C time() isn't good enough). |
|---|
| 4319 | n/a | */ |
|---|
| 4320 | n/a | static PyObject * |
|---|
| 4321 | n/a | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
|---|
| 4322 | n/a | PyObject *tzinfo) |
|---|
| 4323 | n/a | { |
|---|
| 4324 | n/a | time_t timet; |
|---|
| 4325 | n/a | long us; |
|---|
| 4326 | n/a | |
|---|
| 4327 | n/a | if (_PyTime_ObjectToTimeval(timestamp, |
|---|
| 4328 | n/a | &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) |
|---|
| 4329 | n/a | return NULL; |
|---|
| 4330 | n/a | |
|---|
| 4331 | n/a | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
|---|
| 4332 | n/a | } |
|---|
| 4333 | n/a | |
|---|
| 4334 | n/a | /* Internal helper. |
|---|
| 4335 | n/a | * Build most accurate possible datetime for current time. Pass localtime or |
|---|
| 4336 | n/a | * gmtime for f as appropriate. |
|---|
| 4337 | n/a | */ |
|---|
| 4338 | n/a | static PyObject * |
|---|
| 4339 | n/a | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
|---|
| 4340 | n/a | { |
|---|
| 4341 | n/a | _PyTime_t ts = _PyTime_GetSystemClock(); |
|---|
| 4342 | n/a | time_t secs; |
|---|
| 4343 | n/a | int us; |
|---|
| 4344 | n/a | |
|---|
| 4345 | n/a | if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) |
|---|
| 4346 | n/a | return NULL; |
|---|
| 4347 | n/a | assert(0 <= us && us <= 999999); |
|---|
| 4348 | n/a | |
|---|
| 4349 | n/a | return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); |
|---|
| 4350 | n/a | } |
|---|
| 4351 | n/a | |
|---|
| 4352 | n/a | /*[clinic input] |
|---|
| 4353 | n/a | |
|---|
| 4354 | n/a | @classmethod |
|---|
| 4355 | n/a | datetime.datetime.now |
|---|
| 4356 | n/a | |
|---|
| 4357 | n/a | tz: object = None |
|---|
| 4358 | n/a | Timezone object. |
|---|
| 4359 | n/a | |
|---|
| 4360 | n/a | Returns new datetime object representing current time local to tz. |
|---|
| 4361 | n/a | |
|---|
| 4362 | n/a | If no tz is specified, uses local timezone. |
|---|
| 4363 | n/a | [clinic start generated code]*/ |
|---|
| 4364 | n/a | |
|---|
| 4365 | n/a | static PyObject * |
|---|
| 4366 | n/a | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
|---|
| 4367 | n/a | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
|---|
| 4368 | n/a | { |
|---|
| 4369 | n/a | PyObject *self; |
|---|
| 4370 | n/a | |
|---|
| 4371 | n/a | /* Return best possible local time -- this isn't constrained by the |
|---|
| 4372 | n/a | * precision of a timestamp. |
|---|
| 4373 | n/a | */ |
|---|
| 4374 | n/a | if (check_tzinfo_subclass(tz) < 0) |
|---|
| 4375 | n/a | return NULL; |
|---|
| 4376 | n/a | |
|---|
| 4377 | n/a | self = datetime_best_possible((PyObject *)type, |
|---|
| 4378 | n/a | tz == Py_None ? _PyTime_localtime : |
|---|
| 4379 | n/a | _PyTime_gmtime, |
|---|
| 4380 | n/a | tz); |
|---|
| 4381 | n/a | if (self != NULL && tz != Py_None) { |
|---|
| 4382 | n/a | /* Convert UTC to tzinfo's zone. */ |
|---|
| 4383 | n/a | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); |
|---|
| 4384 | n/a | } |
|---|
| 4385 | n/a | return self; |
|---|
| 4386 | n/a | } |
|---|
| 4387 | n/a | |
|---|
| 4388 | n/a | /* Return best possible UTC time -- this isn't constrained by the |
|---|
| 4389 | n/a | * precision of a timestamp. |
|---|
| 4390 | n/a | */ |
|---|
| 4391 | n/a | static PyObject * |
|---|
| 4392 | n/a | datetime_utcnow(PyObject *cls, PyObject *dummy) |
|---|
| 4393 | n/a | { |
|---|
| 4394 | n/a | return datetime_best_possible(cls, _PyTime_gmtime, Py_None); |
|---|
| 4395 | n/a | } |
|---|
| 4396 | n/a | |
|---|
| 4397 | n/a | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
|---|
| 4398 | n/a | static PyObject * |
|---|
| 4399 | n/a | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
|---|
| 4400 | n/a | { |
|---|
| 4401 | n/a | PyObject *self; |
|---|
| 4402 | n/a | PyObject *timestamp; |
|---|
| 4403 | n/a | PyObject *tzinfo = Py_None; |
|---|
| 4404 | n/a | static char *keywords[] = {"timestamp", "tz", NULL}; |
|---|
| 4405 | n/a | |
|---|
| 4406 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
|---|
| 4407 | n/a | keywords, ×tamp, &tzinfo)) |
|---|
| 4408 | n/a | return NULL; |
|---|
| 4409 | n/a | if (check_tzinfo_subclass(tzinfo) < 0) |
|---|
| 4410 | n/a | return NULL; |
|---|
| 4411 | n/a | |
|---|
| 4412 | n/a | self = datetime_from_timestamp(cls, |
|---|
| 4413 | n/a | tzinfo == Py_None ? _PyTime_localtime : |
|---|
| 4414 | n/a | _PyTime_gmtime, |
|---|
| 4415 | n/a | timestamp, |
|---|
| 4416 | n/a | tzinfo); |
|---|
| 4417 | n/a | if (self != NULL && tzinfo != Py_None) { |
|---|
| 4418 | n/a | /* Convert UTC to tzinfo's zone. */ |
|---|
| 4419 | n/a | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); |
|---|
| 4420 | n/a | } |
|---|
| 4421 | n/a | return self; |
|---|
| 4422 | n/a | } |
|---|
| 4423 | n/a | |
|---|
| 4424 | n/a | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
|---|
| 4425 | n/a | static PyObject * |
|---|
| 4426 | n/a | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
|---|
| 4427 | n/a | { |
|---|
| 4428 | n/a | PyObject *timestamp; |
|---|
| 4429 | n/a | PyObject *result = NULL; |
|---|
| 4430 | n/a | |
|---|
| 4431 | n/a | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
|---|
| 4432 | n/a | result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, |
|---|
| 4433 | n/a | Py_None); |
|---|
| 4434 | n/a | return result; |
|---|
| 4435 | n/a | } |
|---|
| 4436 | n/a | |
|---|
| 4437 | n/a | /* Return new datetime from _strptime.strptime_datetime(). */ |
|---|
| 4438 | n/a | static PyObject * |
|---|
| 4439 | n/a | datetime_strptime(PyObject *cls, PyObject *args) |
|---|
| 4440 | n/a | { |
|---|
| 4441 | n/a | static PyObject *module = NULL; |
|---|
| 4442 | n/a | PyObject *string, *format; |
|---|
| 4443 | n/a | _Py_IDENTIFIER(_strptime_datetime); |
|---|
| 4444 | n/a | |
|---|
| 4445 | n/a | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
|---|
| 4446 | n/a | return NULL; |
|---|
| 4447 | n/a | |
|---|
| 4448 | n/a | if (module == NULL) { |
|---|
| 4449 | n/a | module = PyImport_ImportModuleNoBlock("_strptime"); |
|---|
| 4450 | n/a | if (module == NULL) |
|---|
| 4451 | n/a | return NULL; |
|---|
| 4452 | n/a | } |
|---|
| 4453 | n/a | return _PyObject_CallMethodIdObjArgs(module, &PyId__strptime_datetime, |
|---|
| 4454 | n/a | cls, string, format, NULL); |
|---|
| 4455 | n/a | } |
|---|
| 4456 | n/a | |
|---|
| 4457 | n/a | /* Return new datetime from date/datetime and time arguments. */ |
|---|
| 4458 | n/a | static PyObject * |
|---|
| 4459 | n/a | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
|---|
| 4460 | n/a | { |
|---|
| 4461 | n/a | static char *keywords[] = {"date", "time", "tzinfo", NULL}; |
|---|
| 4462 | n/a | PyObject *date; |
|---|
| 4463 | n/a | PyObject *time; |
|---|
| 4464 | n/a | PyObject *tzinfo = NULL; |
|---|
| 4465 | n/a | PyObject *result = NULL; |
|---|
| 4466 | n/a | |
|---|
| 4467 | n/a | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, |
|---|
| 4468 | n/a | &PyDateTime_DateType, &date, |
|---|
| 4469 | n/a | &PyDateTime_TimeType, &time, &tzinfo)) { |
|---|
| 4470 | n/a | if (tzinfo == NULL) { |
|---|
| 4471 | n/a | if (HASTZINFO(time)) |
|---|
| 4472 | n/a | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
|---|
| 4473 | n/a | else |
|---|
| 4474 | n/a | tzinfo = Py_None; |
|---|
| 4475 | n/a | } |
|---|
| 4476 | n/a | result = PyObject_CallFunction(cls, "iiiiiiiO", |
|---|
| 4477 | n/a | GET_YEAR(date), |
|---|
| 4478 | n/a | GET_MONTH(date), |
|---|
| 4479 | n/a | GET_DAY(date), |
|---|
| 4480 | n/a | TIME_GET_HOUR(time), |
|---|
| 4481 | n/a | TIME_GET_MINUTE(time), |
|---|
| 4482 | n/a | TIME_GET_SECOND(time), |
|---|
| 4483 | n/a | TIME_GET_MICROSECOND(time), |
|---|
| 4484 | n/a | tzinfo); |
|---|
| 4485 | n/a | if (result) |
|---|
| 4486 | n/a | DATE_SET_FOLD(result, TIME_GET_FOLD(time)); |
|---|
| 4487 | n/a | } |
|---|
| 4488 | n/a | return result; |
|---|
| 4489 | n/a | } |
|---|
| 4490 | n/a | |
|---|
| 4491 | n/a | /* |
|---|
| 4492 | n/a | * Destructor. |
|---|
| 4493 | n/a | */ |
|---|
| 4494 | n/a | |
|---|
| 4495 | n/a | static void |
|---|
| 4496 | n/a | datetime_dealloc(PyDateTime_DateTime *self) |
|---|
| 4497 | n/a | { |
|---|
| 4498 | n/a | if (HASTZINFO(self)) { |
|---|
| 4499 | n/a | Py_XDECREF(self->tzinfo); |
|---|
| 4500 | n/a | } |
|---|
| 4501 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
|---|
| 4502 | n/a | } |
|---|
| 4503 | n/a | |
|---|
| 4504 | n/a | /* |
|---|
| 4505 | n/a | * Indirect access to tzinfo methods. |
|---|
| 4506 | n/a | */ |
|---|
| 4507 | n/a | |
|---|
| 4508 | n/a | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
|---|
| 4509 | n/a | static PyObject * |
|---|
| 4510 | n/a | datetime_utcoffset(PyObject *self, PyObject *unused) { |
|---|
| 4511 | n/a | return call_utcoffset(GET_DT_TZINFO(self), self); |
|---|
| 4512 | n/a | } |
|---|
| 4513 | n/a | |
|---|
| 4514 | n/a | static PyObject * |
|---|
| 4515 | n/a | datetime_dst(PyObject *self, PyObject *unused) { |
|---|
| 4516 | n/a | return call_dst(GET_DT_TZINFO(self), self); |
|---|
| 4517 | n/a | } |
|---|
| 4518 | n/a | |
|---|
| 4519 | n/a | static PyObject * |
|---|
| 4520 | n/a | datetime_tzname(PyObject *self, PyObject *unused) { |
|---|
| 4521 | n/a | return call_tzname(GET_DT_TZINFO(self), self); |
|---|
| 4522 | n/a | } |
|---|
| 4523 | n/a | |
|---|
| 4524 | n/a | /* |
|---|
| 4525 | n/a | * datetime arithmetic. |
|---|
| 4526 | n/a | */ |
|---|
| 4527 | n/a | |
|---|
| 4528 | n/a | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
|---|
| 4529 | n/a | * the tzinfo state of date. |
|---|
| 4530 | n/a | */ |
|---|
| 4531 | n/a | static PyObject * |
|---|
| 4532 | n/a | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
|---|
| 4533 | n/a | int factor) |
|---|
| 4534 | n/a | { |
|---|
| 4535 | n/a | /* Note that the C-level additions can't overflow, because of |
|---|
| 4536 | n/a | * invariant bounds on the member values. |
|---|
| 4537 | n/a | */ |
|---|
| 4538 | n/a | int year = GET_YEAR(date); |
|---|
| 4539 | n/a | int month = GET_MONTH(date); |
|---|
| 4540 | n/a | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
|---|
| 4541 | n/a | int hour = DATE_GET_HOUR(date); |
|---|
| 4542 | n/a | int minute = DATE_GET_MINUTE(date); |
|---|
| 4543 | n/a | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
|---|
| 4544 | n/a | int microsecond = DATE_GET_MICROSECOND(date) + |
|---|
| 4545 | n/a | GET_TD_MICROSECONDS(delta) * factor; |
|---|
| 4546 | n/a | |
|---|
| 4547 | n/a | assert(factor == 1 || factor == -1); |
|---|
| 4548 | n/a | if (normalize_datetime(&year, &month, &day, |
|---|
| 4549 | n/a | &hour, &minute, &second, µsecond) < 0) { |
|---|
| 4550 | n/a | return NULL; |
|---|
| 4551 | n/a | } |
|---|
| 4552 | n/a | |
|---|
| 4553 | n/a | return new_datetime(year, month, day, |
|---|
| 4554 | n/a | hour, minute, second, microsecond, |
|---|
| 4555 | n/a | HASTZINFO(date) ? date->tzinfo : Py_None, 0); |
|---|
| 4556 | n/a | } |
|---|
| 4557 | n/a | |
|---|
| 4558 | n/a | static PyObject * |
|---|
| 4559 | n/a | datetime_add(PyObject *left, PyObject *right) |
|---|
| 4560 | n/a | { |
|---|
| 4561 | n/a | if (PyDateTime_Check(left)) { |
|---|
| 4562 | n/a | /* datetime + ??? */ |
|---|
| 4563 | n/a | if (PyDelta_Check(right)) |
|---|
| 4564 | n/a | /* datetime + delta */ |
|---|
| 4565 | n/a | return add_datetime_timedelta( |
|---|
| 4566 | n/a | (PyDateTime_DateTime *)left, |
|---|
| 4567 | n/a | (PyDateTime_Delta *)right, |
|---|
| 4568 | n/a | 1); |
|---|
| 4569 | n/a | } |
|---|
| 4570 | n/a | else if (PyDelta_Check(left)) { |
|---|
| 4571 | n/a | /* delta + datetime */ |
|---|
| 4572 | n/a | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
|---|
| 4573 | n/a | (PyDateTime_Delta *) left, |
|---|
| 4574 | n/a | 1); |
|---|
| 4575 | n/a | } |
|---|
| 4576 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 4577 | n/a | } |
|---|
| 4578 | n/a | |
|---|
| 4579 | n/a | static PyObject * |
|---|
| 4580 | n/a | datetime_subtract(PyObject *left, PyObject *right) |
|---|
| 4581 | n/a | { |
|---|
| 4582 | n/a | PyObject *result = Py_NotImplemented; |
|---|
| 4583 | n/a | |
|---|
| 4584 | n/a | if (PyDateTime_Check(left)) { |
|---|
| 4585 | n/a | /* datetime - ??? */ |
|---|
| 4586 | n/a | if (PyDateTime_Check(right)) { |
|---|
| 4587 | n/a | /* datetime - datetime */ |
|---|
| 4588 | n/a | PyObject *offset1, *offset2, *offdiff = NULL; |
|---|
| 4589 | n/a | int delta_d, delta_s, delta_us; |
|---|
| 4590 | n/a | |
|---|
| 4591 | n/a | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
|---|
| 4592 | n/a | offset2 = offset1 = Py_None; |
|---|
| 4593 | n/a | Py_INCREF(offset1); |
|---|
| 4594 | n/a | Py_INCREF(offset2); |
|---|
| 4595 | n/a | } |
|---|
| 4596 | n/a | else { |
|---|
| 4597 | n/a | offset1 = datetime_utcoffset(left, NULL); |
|---|
| 4598 | n/a | if (offset1 == NULL) |
|---|
| 4599 | n/a | return NULL; |
|---|
| 4600 | n/a | offset2 = datetime_utcoffset(right, NULL); |
|---|
| 4601 | n/a | if (offset2 == NULL) { |
|---|
| 4602 | n/a | Py_DECREF(offset1); |
|---|
| 4603 | n/a | return NULL; |
|---|
| 4604 | n/a | } |
|---|
| 4605 | n/a | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
|---|
| 4606 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4607 | n/a | "can't subtract offset-naive and " |
|---|
| 4608 | n/a | "offset-aware datetimes"); |
|---|
| 4609 | n/a | Py_DECREF(offset1); |
|---|
| 4610 | n/a | Py_DECREF(offset2); |
|---|
| 4611 | n/a | return NULL; |
|---|
| 4612 | n/a | } |
|---|
| 4613 | n/a | } |
|---|
| 4614 | n/a | if ((offset1 != offset2) && |
|---|
| 4615 | n/a | delta_cmp(offset1, offset2) != 0) { |
|---|
| 4616 | n/a | offdiff = delta_subtract(offset1, offset2); |
|---|
| 4617 | n/a | if (offdiff == NULL) { |
|---|
| 4618 | n/a | Py_DECREF(offset1); |
|---|
| 4619 | n/a | Py_DECREF(offset2); |
|---|
| 4620 | n/a | return NULL; |
|---|
| 4621 | n/a | } |
|---|
| 4622 | n/a | } |
|---|
| 4623 | n/a | Py_DECREF(offset1); |
|---|
| 4624 | n/a | Py_DECREF(offset2); |
|---|
| 4625 | n/a | delta_d = ymd_to_ord(GET_YEAR(left), |
|---|
| 4626 | n/a | GET_MONTH(left), |
|---|
| 4627 | n/a | GET_DAY(left)) - |
|---|
| 4628 | n/a | ymd_to_ord(GET_YEAR(right), |
|---|
| 4629 | n/a | GET_MONTH(right), |
|---|
| 4630 | n/a | GET_DAY(right)); |
|---|
| 4631 | n/a | /* These can't overflow, since the values are |
|---|
| 4632 | n/a | * normalized. At most this gives the number of |
|---|
| 4633 | n/a | * seconds in one day. |
|---|
| 4634 | n/a | */ |
|---|
| 4635 | n/a | delta_s = (DATE_GET_HOUR(left) - |
|---|
| 4636 | n/a | DATE_GET_HOUR(right)) * 3600 + |
|---|
| 4637 | n/a | (DATE_GET_MINUTE(left) - |
|---|
| 4638 | n/a | DATE_GET_MINUTE(right)) * 60 + |
|---|
| 4639 | n/a | (DATE_GET_SECOND(left) - |
|---|
| 4640 | n/a | DATE_GET_SECOND(right)); |
|---|
| 4641 | n/a | delta_us = DATE_GET_MICROSECOND(left) - |
|---|
| 4642 | n/a | DATE_GET_MICROSECOND(right); |
|---|
| 4643 | n/a | result = new_delta(delta_d, delta_s, delta_us, 1); |
|---|
| 4644 | n/a | if (result == NULL) |
|---|
| 4645 | n/a | return NULL; |
|---|
| 4646 | n/a | |
|---|
| 4647 | n/a | if (offdiff != NULL) { |
|---|
| 4648 | n/a | Py_SETREF(result, delta_subtract(result, offdiff)); |
|---|
| 4649 | n/a | Py_DECREF(offdiff); |
|---|
| 4650 | n/a | } |
|---|
| 4651 | n/a | } |
|---|
| 4652 | n/a | else if (PyDelta_Check(right)) { |
|---|
| 4653 | n/a | /* datetime - delta */ |
|---|
| 4654 | n/a | result = add_datetime_timedelta( |
|---|
| 4655 | n/a | (PyDateTime_DateTime *)left, |
|---|
| 4656 | n/a | (PyDateTime_Delta *)right, |
|---|
| 4657 | n/a | -1); |
|---|
| 4658 | n/a | } |
|---|
| 4659 | n/a | } |
|---|
| 4660 | n/a | |
|---|
| 4661 | n/a | if (result == Py_NotImplemented) |
|---|
| 4662 | n/a | Py_INCREF(result); |
|---|
| 4663 | n/a | return result; |
|---|
| 4664 | n/a | } |
|---|
| 4665 | n/a | |
|---|
| 4666 | n/a | /* Various ways to turn a datetime into a string. */ |
|---|
| 4667 | n/a | |
|---|
| 4668 | n/a | static PyObject * |
|---|
| 4669 | n/a | datetime_repr(PyDateTime_DateTime *self) |
|---|
| 4670 | n/a | { |
|---|
| 4671 | n/a | const char *type_name = Py_TYPE(self)->tp_name; |
|---|
| 4672 | n/a | PyObject *baserepr; |
|---|
| 4673 | n/a | |
|---|
| 4674 | n/a | if (DATE_GET_MICROSECOND(self)) { |
|---|
| 4675 | n/a | baserepr = PyUnicode_FromFormat( |
|---|
| 4676 | n/a | "%s(%d, %d, %d, %d, %d, %d, %d)", |
|---|
| 4677 | n/a | type_name, |
|---|
| 4678 | n/a | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
|---|
| 4679 | n/a | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
|---|
| 4680 | n/a | DATE_GET_SECOND(self), |
|---|
| 4681 | n/a | DATE_GET_MICROSECOND(self)); |
|---|
| 4682 | n/a | } |
|---|
| 4683 | n/a | else if (DATE_GET_SECOND(self)) { |
|---|
| 4684 | n/a | baserepr = PyUnicode_FromFormat( |
|---|
| 4685 | n/a | "%s(%d, %d, %d, %d, %d, %d)", |
|---|
| 4686 | n/a | type_name, |
|---|
| 4687 | n/a | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
|---|
| 4688 | n/a | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
|---|
| 4689 | n/a | DATE_GET_SECOND(self)); |
|---|
| 4690 | n/a | } |
|---|
| 4691 | n/a | else { |
|---|
| 4692 | n/a | baserepr = PyUnicode_FromFormat( |
|---|
| 4693 | n/a | "%s(%d, %d, %d, %d, %d)", |
|---|
| 4694 | n/a | type_name, |
|---|
| 4695 | n/a | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
|---|
| 4696 | n/a | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
|---|
| 4697 | n/a | } |
|---|
| 4698 | n/a | if (baserepr != NULL && DATE_GET_FOLD(self) != 0) |
|---|
| 4699 | n/a | baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); |
|---|
| 4700 | n/a | if (baserepr == NULL || ! HASTZINFO(self)) |
|---|
| 4701 | n/a | return baserepr; |
|---|
| 4702 | n/a | return append_keyword_tzinfo(baserepr, self->tzinfo); |
|---|
| 4703 | n/a | } |
|---|
| 4704 | n/a | |
|---|
| 4705 | n/a | static PyObject * |
|---|
| 4706 | n/a | datetime_str(PyDateTime_DateTime *self) |
|---|
| 4707 | n/a | { |
|---|
| 4708 | n/a | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " "); |
|---|
| 4709 | n/a | } |
|---|
| 4710 | n/a | |
|---|
| 4711 | n/a | static PyObject * |
|---|
| 4712 | n/a | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
|---|
| 4713 | n/a | { |
|---|
| 4714 | n/a | int sep = 'T'; |
|---|
| 4715 | n/a | char *timespec = NULL; |
|---|
| 4716 | n/a | static char *keywords[] = {"sep", "timespec", NULL}; |
|---|
| 4717 | n/a | char buffer[100]; |
|---|
| 4718 | n/a | PyObject *result = NULL; |
|---|
| 4719 | n/a | int us = DATE_GET_MICROSECOND(self); |
|---|
| 4720 | n/a | static char *specs[][2] = { |
|---|
| 4721 | n/a | {"hours", "%04d-%02d-%02d%c%02d"}, |
|---|
| 4722 | n/a | {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, |
|---|
| 4723 | n/a | {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, |
|---|
| 4724 | n/a | {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, |
|---|
| 4725 | n/a | {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, |
|---|
| 4726 | n/a | }; |
|---|
| 4727 | n/a | size_t given_spec; |
|---|
| 4728 | n/a | |
|---|
| 4729 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) |
|---|
| 4730 | n/a | return NULL; |
|---|
| 4731 | n/a | |
|---|
| 4732 | n/a | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
|---|
| 4733 | n/a | if (us == 0) { |
|---|
| 4734 | n/a | /* seconds */ |
|---|
| 4735 | n/a | given_spec = 2; |
|---|
| 4736 | n/a | } |
|---|
| 4737 | n/a | else { |
|---|
| 4738 | n/a | /* microseconds */ |
|---|
| 4739 | n/a | given_spec = 4; |
|---|
| 4740 | n/a | } |
|---|
| 4741 | n/a | } |
|---|
| 4742 | n/a | else { |
|---|
| 4743 | n/a | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
|---|
| 4744 | n/a | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
|---|
| 4745 | n/a | if (given_spec == 3) { |
|---|
| 4746 | n/a | us = us / 1000; |
|---|
| 4747 | n/a | } |
|---|
| 4748 | n/a | break; |
|---|
| 4749 | n/a | } |
|---|
| 4750 | n/a | } |
|---|
| 4751 | n/a | } |
|---|
| 4752 | n/a | |
|---|
| 4753 | n/a | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
|---|
| 4754 | n/a | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
|---|
| 4755 | n/a | return NULL; |
|---|
| 4756 | n/a | } |
|---|
| 4757 | n/a | else { |
|---|
| 4758 | n/a | result = PyUnicode_FromFormat(specs[given_spec][1], |
|---|
| 4759 | n/a | GET_YEAR(self), GET_MONTH(self), |
|---|
| 4760 | n/a | GET_DAY(self), (int)sep, |
|---|
| 4761 | n/a | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
|---|
| 4762 | n/a | DATE_GET_SECOND(self), us); |
|---|
| 4763 | n/a | } |
|---|
| 4764 | n/a | |
|---|
| 4765 | n/a | if (!result || !HASTZINFO(self)) |
|---|
| 4766 | n/a | return result; |
|---|
| 4767 | n/a | |
|---|
| 4768 | n/a | /* We need to append the UTC offset. */ |
|---|
| 4769 | n/a | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
|---|
| 4770 | n/a | (PyObject *)self) < 0) { |
|---|
| 4771 | n/a | Py_DECREF(result); |
|---|
| 4772 | n/a | return NULL; |
|---|
| 4773 | n/a | } |
|---|
| 4774 | n/a | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
|---|
| 4775 | n/a | return result; |
|---|
| 4776 | n/a | } |
|---|
| 4777 | n/a | |
|---|
| 4778 | n/a | static PyObject * |
|---|
| 4779 | n/a | datetime_ctime(PyDateTime_DateTime *self) |
|---|
| 4780 | n/a | { |
|---|
| 4781 | n/a | return format_ctime((PyDateTime_Date *)self, |
|---|
| 4782 | n/a | DATE_GET_HOUR(self), |
|---|
| 4783 | n/a | DATE_GET_MINUTE(self), |
|---|
| 4784 | n/a | DATE_GET_SECOND(self)); |
|---|
| 4785 | n/a | } |
|---|
| 4786 | n/a | |
|---|
| 4787 | n/a | /* Miscellaneous methods. */ |
|---|
| 4788 | n/a | |
|---|
| 4789 | n/a | static PyObject * |
|---|
| 4790 | n/a | flip_fold(PyObject *dt) |
|---|
| 4791 | n/a | { |
|---|
| 4792 | n/a | return new_datetime_ex2(GET_YEAR(dt), |
|---|
| 4793 | n/a | GET_MONTH(dt), |
|---|
| 4794 | n/a | GET_DAY(dt), |
|---|
| 4795 | n/a | DATE_GET_HOUR(dt), |
|---|
| 4796 | n/a | DATE_GET_MINUTE(dt), |
|---|
| 4797 | n/a | DATE_GET_SECOND(dt), |
|---|
| 4798 | n/a | DATE_GET_MICROSECOND(dt), |
|---|
| 4799 | n/a | HASTZINFO(dt) ? |
|---|
| 4800 | n/a | ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, |
|---|
| 4801 | n/a | !DATE_GET_FOLD(dt), |
|---|
| 4802 | n/a | Py_TYPE(dt)); |
|---|
| 4803 | n/a | } |
|---|
| 4804 | n/a | |
|---|
| 4805 | n/a | static PyObject * |
|---|
| 4806 | n/a | get_flip_fold_offset(PyObject *dt) |
|---|
| 4807 | n/a | { |
|---|
| 4808 | n/a | PyObject *result, *flip_dt; |
|---|
| 4809 | n/a | |
|---|
| 4810 | n/a | flip_dt = flip_fold(dt); |
|---|
| 4811 | n/a | if (flip_dt == NULL) |
|---|
| 4812 | n/a | return NULL; |
|---|
| 4813 | n/a | result = datetime_utcoffset(flip_dt, NULL); |
|---|
| 4814 | n/a | Py_DECREF(flip_dt); |
|---|
| 4815 | n/a | return result; |
|---|
| 4816 | n/a | } |
|---|
| 4817 | n/a | |
|---|
| 4818 | n/a | /* PEP 495 exception: Whenever one or both of the operands in |
|---|
| 4819 | n/a | * inter-zone comparison is such that its utcoffset() depends |
|---|
| 4820 | n/a | * on the value of its fold fold attribute, the result is False. |
|---|
| 4821 | n/a | * |
|---|
| 4822 | n/a | * Return 1 if exception applies, 0 if not, and -1 on error. |
|---|
| 4823 | n/a | */ |
|---|
| 4824 | n/a | static int |
|---|
| 4825 | n/a | pep495_eq_exception(PyObject *self, PyObject *other, |
|---|
| 4826 | n/a | PyObject *offset_self, PyObject *offset_other) |
|---|
| 4827 | n/a | { |
|---|
| 4828 | n/a | int result = 0; |
|---|
| 4829 | n/a | PyObject *flip_offset; |
|---|
| 4830 | n/a | |
|---|
| 4831 | n/a | flip_offset = get_flip_fold_offset(self); |
|---|
| 4832 | n/a | if (flip_offset == NULL) |
|---|
| 4833 | n/a | return -1; |
|---|
| 4834 | n/a | if (flip_offset != offset_self && |
|---|
| 4835 | n/a | delta_cmp(flip_offset, offset_self)) |
|---|
| 4836 | n/a | { |
|---|
| 4837 | n/a | result = 1; |
|---|
| 4838 | n/a | goto done; |
|---|
| 4839 | n/a | } |
|---|
| 4840 | n/a | Py_DECREF(flip_offset); |
|---|
| 4841 | n/a | |
|---|
| 4842 | n/a | flip_offset = get_flip_fold_offset(other); |
|---|
| 4843 | n/a | if (flip_offset == NULL) |
|---|
| 4844 | n/a | return -1; |
|---|
| 4845 | n/a | if (flip_offset != offset_other && |
|---|
| 4846 | n/a | delta_cmp(flip_offset, offset_other)) |
|---|
| 4847 | n/a | result = 1; |
|---|
| 4848 | n/a | done: |
|---|
| 4849 | n/a | Py_DECREF(flip_offset); |
|---|
| 4850 | n/a | return result; |
|---|
| 4851 | n/a | } |
|---|
| 4852 | n/a | |
|---|
| 4853 | n/a | static PyObject * |
|---|
| 4854 | n/a | datetime_richcompare(PyObject *self, PyObject *other, int op) |
|---|
| 4855 | n/a | { |
|---|
| 4856 | n/a | PyObject *result = NULL; |
|---|
| 4857 | n/a | PyObject *offset1, *offset2; |
|---|
| 4858 | n/a | int diff; |
|---|
| 4859 | n/a | |
|---|
| 4860 | n/a | if (! PyDateTime_Check(other)) { |
|---|
| 4861 | n/a | if (PyDate_Check(other)) { |
|---|
| 4862 | n/a | /* Prevent invocation of date_richcompare. We want to |
|---|
| 4863 | n/a | return NotImplemented here to give the other object |
|---|
| 4864 | n/a | a chance. But since DateTime is a subclass of |
|---|
| 4865 | n/a | Date, if the other object is a Date, it would |
|---|
| 4866 | n/a | compute an ordering based on the date part alone, |
|---|
| 4867 | n/a | and we don't want that. So force unequal or |
|---|
| 4868 | n/a | uncomparable here in that case. */ |
|---|
| 4869 | n/a | if (op == Py_EQ) |
|---|
| 4870 | n/a | Py_RETURN_FALSE; |
|---|
| 4871 | n/a | if (op == Py_NE) |
|---|
| 4872 | n/a | Py_RETURN_TRUE; |
|---|
| 4873 | n/a | return cmperror(self, other); |
|---|
| 4874 | n/a | } |
|---|
| 4875 | n/a | Py_RETURN_NOTIMPLEMENTED; |
|---|
| 4876 | n/a | } |
|---|
| 4877 | n/a | |
|---|
| 4878 | n/a | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
|---|
| 4879 | n/a | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
|---|
| 4880 | n/a | ((PyDateTime_DateTime *)other)->data, |
|---|
| 4881 | n/a | _PyDateTime_DATETIME_DATASIZE); |
|---|
| 4882 | n/a | return diff_to_bool(diff, op); |
|---|
| 4883 | n/a | } |
|---|
| 4884 | n/a | offset1 = datetime_utcoffset(self, NULL); |
|---|
| 4885 | n/a | if (offset1 == NULL) |
|---|
| 4886 | n/a | return NULL; |
|---|
| 4887 | n/a | offset2 = datetime_utcoffset(other, NULL); |
|---|
| 4888 | n/a | if (offset2 == NULL) |
|---|
| 4889 | n/a | goto done; |
|---|
| 4890 | n/a | /* If they're both naive, or both aware and have the same offsets, |
|---|
| 4891 | n/a | * we get off cheap. Note that if they're both naive, offset1 == |
|---|
| 4892 | n/a | * offset2 == Py_None at this point. |
|---|
| 4893 | n/a | */ |
|---|
| 4894 | n/a | if ((offset1 == offset2) || |
|---|
| 4895 | n/a | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
|---|
| 4896 | n/a | delta_cmp(offset1, offset2) == 0)) { |
|---|
| 4897 | n/a | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
|---|
| 4898 | n/a | ((PyDateTime_DateTime *)other)->data, |
|---|
| 4899 | n/a | _PyDateTime_DATETIME_DATASIZE); |
|---|
| 4900 | n/a | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
|---|
| 4901 | n/a | int ex = pep495_eq_exception(self, other, offset1, offset2); |
|---|
| 4902 | n/a | if (ex == -1) |
|---|
| 4903 | n/a | goto done; |
|---|
| 4904 | n/a | if (ex) |
|---|
| 4905 | n/a | diff = 1; |
|---|
| 4906 | n/a | } |
|---|
| 4907 | n/a | result = diff_to_bool(diff, op); |
|---|
| 4908 | n/a | } |
|---|
| 4909 | n/a | else if (offset1 != Py_None && offset2 != Py_None) { |
|---|
| 4910 | n/a | PyDateTime_Delta *delta; |
|---|
| 4911 | n/a | |
|---|
| 4912 | n/a | assert(offset1 != offset2); /* else last "if" handled it */ |
|---|
| 4913 | n/a | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
|---|
| 4914 | n/a | other); |
|---|
| 4915 | n/a | if (delta == NULL) |
|---|
| 4916 | n/a | goto done; |
|---|
| 4917 | n/a | diff = GET_TD_DAYS(delta); |
|---|
| 4918 | n/a | if (diff == 0) |
|---|
| 4919 | n/a | diff = GET_TD_SECONDS(delta) | |
|---|
| 4920 | n/a | GET_TD_MICROSECONDS(delta); |
|---|
| 4921 | n/a | Py_DECREF(delta); |
|---|
| 4922 | n/a | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
|---|
| 4923 | n/a | int ex = pep495_eq_exception(self, other, offset1, offset2); |
|---|
| 4924 | n/a | if (ex == -1) |
|---|
| 4925 | n/a | goto done; |
|---|
| 4926 | n/a | if (ex) |
|---|
| 4927 | n/a | diff = 1; |
|---|
| 4928 | n/a | } |
|---|
| 4929 | n/a | result = diff_to_bool(diff, op); |
|---|
| 4930 | n/a | } |
|---|
| 4931 | n/a | else if (op == Py_EQ) { |
|---|
| 4932 | n/a | result = Py_False; |
|---|
| 4933 | n/a | Py_INCREF(result); |
|---|
| 4934 | n/a | } |
|---|
| 4935 | n/a | else if (op == Py_NE) { |
|---|
| 4936 | n/a | result = Py_True; |
|---|
| 4937 | n/a | Py_INCREF(result); |
|---|
| 4938 | n/a | } |
|---|
| 4939 | n/a | else { |
|---|
| 4940 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4941 | n/a | "can't compare offset-naive and " |
|---|
| 4942 | n/a | "offset-aware datetimes"); |
|---|
| 4943 | n/a | } |
|---|
| 4944 | n/a | done: |
|---|
| 4945 | n/a | Py_DECREF(offset1); |
|---|
| 4946 | n/a | Py_XDECREF(offset2); |
|---|
| 4947 | n/a | return result; |
|---|
| 4948 | n/a | } |
|---|
| 4949 | n/a | |
|---|
| 4950 | n/a | static Py_hash_t |
|---|
| 4951 | n/a | datetime_hash(PyDateTime_DateTime *self) |
|---|
| 4952 | n/a | { |
|---|
| 4953 | n/a | if (self->hashcode == -1) { |
|---|
| 4954 | n/a | PyObject *offset, *self0; |
|---|
| 4955 | n/a | if (DATE_GET_FOLD(self)) { |
|---|
| 4956 | n/a | self0 = new_datetime_ex2(GET_YEAR(self), |
|---|
| 4957 | n/a | GET_MONTH(self), |
|---|
| 4958 | n/a | GET_DAY(self), |
|---|
| 4959 | n/a | DATE_GET_HOUR(self), |
|---|
| 4960 | n/a | DATE_GET_MINUTE(self), |
|---|
| 4961 | n/a | DATE_GET_SECOND(self), |
|---|
| 4962 | n/a | DATE_GET_MICROSECOND(self), |
|---|
| 4963 | n/a | HASTZINFO(self) ? self->tzinfo : Py_None, |
|---|
| 4964 | n/a | 0, Py_TYPE(self)); |
|---|
| 4965 | n/a | if (self0 == NULL) |
|---|
| 4966 | n/a | return -1; |
|---|
| 4967 | n/a | } |
|---|
| 4968 | n/a | else { |
|---|
| 4969 | n/a | self0 = (PyObject *)self; |
|---|
| 4970 | n/a | Py_INCREF(self0); |
|---|
| 4971 | n/a | } |
|---|
| 4972 | n/a | offset = datetime_utcoffset(self0, NULL); |
|---|
| 4973 | n/a | Py_DECREF(self0); |
|---|
| 4974 | n/a | |
|---|
| 4975 | n/a | if (offset == NULL) |
|---|
| 4976 | n/a | return -1; |
|---|
| 4977 | n/a | |
|---|
| 4978 | n/a | /* Reduce this to a hash of another object. */ |
|---|
| 4979 | n/a | if (offset == Py_None) |
|---|
| 4980 | n/a | self->hashcode = generic_hash( |
|---|
| 4981 | n/a | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
|---|
| 4982 | n/a | else { |
|---|
| 4983 | n/a | PyObject *temp1, *temp2; |
|---|
| 4984 | n/a | int days, seconds; |
|---|
| 4985 | n/a | |
|---|
| 4986 | n/a | assert(HASTZINFO(self)); |
|---|
| 4987 | n/a | days = ymd_to_ord(GET_YEAR(self), |
|---|
| 4988 | n/a | GET_MONTH(self), |
|---|
| 4989 | n/a | GET_DAY(self)); |
|---|
| 4990 | n/a | seconds = DATE_GET_HOUR(self) * 3600 + |
|---|
| 4991 | n/a | DATE_GET_MINUTE(self) * 60 + |
|---|
| 4992 | n/a | DATE_GET_SECOND(self); |
|---|
| 4993 | n/a | temp1 = new_delta(days, seconds, |
|---|
| 4994 | n/a | DATE_GET_MICROSECOND(self), |
|---|
| 4995 | n/a | 1); |
|---|
| 4996 | n/a | if (temp1 == NULL) { |
|---|
| 4997 | n/a | Py_DECREF(offset); |
|---|
| 4998 | n/a | return -1; |
|---|
| 4999 | n/a | } |
|---|
| 5000 | n/a | temp2 = delta_subtract(temp1, offset); |
|---|
| 5001 | n/a | Py_DECREF(temp1); |
|---|
| 5002 | n/a | if (temp2 == NULL) { |
|---|
| 5003 | n/a | Py_DECREF(offset); |
|---|
| 5004 | n/a | return -1; |
|---|
| 5005 | n/a | } |
|---|
| 5006 | n/a | self->hashcode = PyObject_Hash(temp2); |
|---|
| 5007 | n/a | Py_DECREF(temp2); |
|---|
| 5008 | n/a | } |
|---|
| 5009 | n/a | Py_DECREF(offset); |
|---|
| 5010 | n/a | } |
|---|
| 5011 | n/a | return self->hashcode; |
|---|
| 5012 | n/a | } |
|---|
| 5013 | n/a | |
|---|
| 5014 | n/a | static PyObject * |
|---|
| 5015 | n/a | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
|---|
| 5016 | n/a | { |
|---|
| 5017 | n/a | PyObject *clone; |
|---|
| 5018 | n/a | PyObject *tuple; |
|---|
| 5019 | n/a | int y = GET_YEAR(self); |
|---|
| 5020 | n/a | int m = GET_MONTH(self); |
|---|
| 5021 | n/a | int d = GET_DAY(self); |
|---|
| 5022 | n/a | int hh = DATE_GET_HOUR(self); |
|---|
| 5023 | n/a | int mm = DATE_GET_MINUTE(self); |
|---|
| 5024 | n/a | int ss = DATE_GET_SECOND(self); |
|---|
| 5025 | n/a | int us = DATE_GET_MICROSECOND(self); |
|---|
| 5026 | n/a | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
|---|
| 5027 | n/a | int fold = DATE_GET_FOLD(self); |
|---|
| 5028 | n/a | |
|---|
| 5029 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", |
|---|
| 5030 | n/a | datetime_kws, |
|---|
| 5031 | n/a | &y, &m, &d, &hh, &mm, &ss, &us, |
|---|
| 5032 | n/a | &tzinfo, &fold)) |
|---|
| 5033 | n/a | return NULL; |
|---|
| 5034 | n/a | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
|---|
| 5035 | n/a | if (tuple == NULL) |
|---|
| 5036 | n/a | return NULL; |
|---|
| 5037 | n/a | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
|---|
| 5038 | n/a | |
|---|
| 5039 | n/a | if (clone != NULL) { |
|---|
| 5040 | n/a | if (fold != 0 && fold != 1) { |
|---|
| 5041 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 5042 | n/a | "fold must be either 0 or 1"); |
|---|
| 5043 | n/a | return NULL; |
|---|
| 5044 | n/a | } |
|---|
| 5045 | n/a | DATE_SET_FOLD(clone, fold); |
|---|
| 5046 | n/a | } |
|---|
| 5047 | n/a | Py_DECREF(tuple); |
|---|
| 5048 | n/a | return clone; |
|---|
| 5049 | n/a | } |
|---|
| 5050 | n/a | |
|---|
| 5051 | n/a | static PyObject * |
|---|
| 5052 | n/a | local_timezone_from_timestamp(time_t timestamp) |
|---|
| 5053 | n/a | { |
|---|
| 5054 | n/a | PyObject *result = NULL; |
|---|
| 5055 | n/a | PyObject *delta; |
|---|
| 5056 | n/a | struct tm local_time_tm; |
|---|
| 5057 | n/a | PyObject *nameo = NULL; |
|---|
| 5058 | n/a | const char *zone = NULL; |
|---|
| 5059 | n/a | |
|---|
| 5060 | n/a | if (_PyTime_localtime(timestamp, &local_time_tm) != 0) |
|---|
| 5061 | n/a | return NULL; |
|---|
| 5062 | n/a | #ifdef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 5063 | n/a | zone = local_time_tm.tm_zone; |
|---|
| 5064 | n/a | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); |
|---|
| 5065 | n/a | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
|---|
| 5066 | n/a | { |
|---|
| 5067 | n/a | PyObject *local_time, *utc_time; |
|---|
| 5068 | n/a | struct tm utc_time_tm; |
|---|
| 5069 | n/a | char buf[100]; |
|---|
| 5070 | n/a | strftime(buf, sizeof(buf), "%Z", &local_time_tm); |
|---|
| 5071 | n/a | zone = buf; |
|---|
| 5072 | n/a | local_time = new_datetime(local_time_tm.tm_year + 1900, |
|---|
| 5073 | n/a | local_time_tm.tm_mon + 1, |
|---|
| 5074 | n/a | local_time_tm.tm_mday, |
|---|
| 5075 | n/a | local_time_tm.tm_hour, |
|---|
| 5076 | n/a | local_time_tm.tm_min, |
|---|
| 5077 | n/a | local_time_tm.tm_sec, 0, Py_None, 0); |
|---|
| 5078 | n/a | if (local_time == NULL) { |
|---|
| 5079 | n/a | return NULL; |
|---|
| 5080 | n/a | } |
|---|
| 5081 | n/a | if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) |
|---|
| 5082 | n/a | return NULL; |
|---|
| 5083 | n/a | utc_time = new_datetime(utc_time_tm.tm_year + 1900, |
|---|
| 5084 | n/a | utc_time_tm.tm_mon + 1, |
|---|
| 5085 | n/a | utc_time_tm.tm_mday, |
|---|
| 5086 | n/a | utc_time_tm.tm_hour, |
|---|
| 5087 | n/a | utc_time_tm.tm_min, |
|---|
| 5088 | n/a | utc_time_tm.tm_sec, 0, Py_None, 0); |
|---|
| 5089 | n/a | if (utc_time == NULL) { |
|---|
| 5090 | n/a | Py_DECREF(local_time); |
|---|
| 5091 | n/a | return NULL; |
|---|
| 5092 | n/a | } |
|---|
| 5093 | n/a | delta = datetime_subtract(local_time, utc_time); |
|---|
| 5094 | n/a | Py_DECREF(local_time); |
|---|
| 5095 | n/a | Py_DECREF(utc_time); |
|---|
| 5096 | n/a | } |
|---|
| 5097 | n/a | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
|---|
| 5098 | n/a | if (delta == NULL) { |
|---|
| 5099 | n/a | return NULL; |
|---|
| 5100 | n/a | } |
|---|
| 5101 | n/a | if (zone != NULL) { |
|---|
| 5102 | n/a | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
|---|
| 5103 | n/a | if (nameo == NULL) |
|---|
| 5104 | n/a | goto error; |
|---|
| 5105 | n/a | } |
|---|
| 5106 | n/a | result = new_timezone(delta, nameo); |
|---|
| 5107 | n/a | Py_XDECREF(nameo); |
|---|
| 5108 | n/a | error: |
|---|
| 5109 | n/a | Py_DECREF(delta); |
|---|
| 5110 | n/a | return result; |
|---|
| 5111 | n/a | } |
|---|
| 5112 | n/a | |
|---|
| 5113 | n/a | static PyObject * |
|---|
| 5114 | n/a | local_timezone(PyDateTime_DateTime *utc_time) |
|---|
| 5115 | n/a | { |
|---|
| 5116 | n/a | time_t timestamp; |
|---|
| 5117 | n/a | PyObject *delta; |
|---|
| 5118 | n/a | PyObject *one_second; |
|---|
| 5119 | n/a | PyObject *seconds; |
|---|
| 5120 | n/a | |
|---|
| 5121 | n/a | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
|---|
| 5122 | n/a | if (delta == NULL) |
|---|
| 5123 | n/a | return NULL; |
|---|
| 5124 | n/a | one_second = new_delta(0, 1, 0, 0); |
|---|
| 5125 | n/a | if (one_second == NULL) { |
|---|
| 5126 | n/a | Py_DECREF(delta); |
|---|
| 5127 | n/a | return NULL; |
|---|
| 5128 | n/a | } |
|---|
| 5129 | n/a | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
|---|
| 5130 | n/a | (PyDateTime_Delta *)one_second); |
|---|
| 5131 | n/a | Py_DECREF(one_second); |
|---|
| 5132 | n/a | Py_DECREF(delta); |
|---|
| 5133 | n/a | if (seconds == NULL) |
|---|
| 5134 | n/a | return NULL; |
|---|
| 5135 | n/a | timestamp = _PyLong_AsTime_t(seconds); |
|---|
| 5136 | n/a | Py_DECREF(seconds); |
|---|
| 5137 | n/a | if (timestamp == -1 && PyErr_Occurred()) |
|---|
| 5138 | n/a | return NULL; |
|---|
| 5139 | n/a | return local_timezone_from_timestamp(timestamp); |
|---|
| 5140 | n/a | } |
|---|
| 5141 | n/a | |
|---|
| 5142 | n/a | static long long |
|---|
| 5143 | n/a | local_to_seconds(int year, int month, int day, |
|---|
| 5144 | n/a | int hour, int minute, int second, int fold); |
|---|
| 5145 | n/a | |
|---|
| 5146 | n/a | static PyObject * |
|---|
| 5147 | n/a | local_timezone_from_local(PyDateTime_DateTime *local_dt) |
|---|
| 5148 | n/a | { |
|---|
| 5149 | n/a | long long seconds; |
|---|
| 5150 | n/a | time_t timestamp; |
|---|
| 5151 | n/a | seconds = local_to_seconds(GET_YEAR(local_dt), |
|---|
| 5152 | n/a | GET_MONTH(local_dt), |
|---|
| 5153 | n/a | GET_DAY(local_dt), |
|---|
| 5154 | n/a | DATE_GET_HOUR(local_dt), |
|---|
| 5155 | n/a | DATE_GET_MINUTE(local_dt), |
|---|
| 5156 | n/a | DATE_GET_SECOND(local_dt), |
|---|
| 5157 | n/a | DATE_GET_FOLD(local_dt)); |
|---|
| 5158 | n/a | if (seconds == -1) |
|---|
| 5159 | n/a | return NULL; |
|---|
| 5160 | n/a | /* XXX: add bounds check */ |
|---|
| 5161 | n/a | timestamp = seconds - epoch; |
|---|
| 5162 | n/a | return local_timezone_from_timestamp(timestamp); |
|---|
| 5163 | n/a | } |
|---|
| 5164 | n/a | |
|---|
| 5165 | n/a | static PyDateTime_DateTime * |
|---|
| 5166 | n/a | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
|---|
| 5167 | n/a | { |
|---|
| 5168 | n/a | PyDateTime_DateTime *result; |
|---|
| 5169 | n/a | PyObject *offset; |
|---|
| 5170 | n/a | PyObject *temp; |
|---|
| 5171 | n/a | PyObject *self_tzinfo; |
|---|
| 5172 | n/a | PyObject *tzinfo = Py_None; |
|---|
| 5173 | n/a | static char *keywords[] = {"tz", NULL}; |
|---|
| 5174 | n/a | |
|---|
| 5175 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
|---|
| 5176 | n/a | &tzinfo)) |
|---|
| 5177 | n/a | return NULL; |
|---|
| 5178 | n/a | |
|---|
| 5179 | n/a | if (check_tzinfo_subclass(tzinfo) == -1) |
|---|
| 5180 | n/a | return NULL; |
|---|
| 5181 | n/a | |
|---|
| 5182 | n/a | if (!HASTZINFO(self) || self->tzinfo == Py_None) { |
|---|
| 5183 | n/a | self_tzinfo = local_timezone_from_local(self); |
|---|
| 5184 | n/a | if (self_tzinfo == NULL) |
|---|
| 5185 | n/a | return NULL; |
|---|
| 5186 | n/a | } else { |
|---|
| 5187 | n/a | self_tzinfo = self->tzinfo; |
|---|
| 5188 | n/a | Py_INCREF(self_tzinfo); |
|---|
| 5189 | n/a | } |
|---|
| 5190 | n/a | |
|---|
| 5191 | n/a | /* Conversion to self's own time zone is a NOP. */ |
|---|
| 5192 | n/a | if (self_tzinfo == tzinfo) { |
|---|
| 5193 | n/a | Py_DECREF(self_tzinfo); |
|---|
| 5194 | n/a | Py_INCREF(self); |
|---|
| 5195 | n/a | return self; |
|---|
| 5196 | n/a | } |
|---|
| 5197 | n/a | |
|---|
| 5198 | n/a | /* Convert self to UTC. */ |
|---|
| 5199 | n/a | offset = call_utcoffset(self_tzinfo, (PyObject *)self); |
|---|
| 5200 | n/a | Py_DECREF(self_tzinfo); |
|---|
| 5201 | n/a | if (offset == NULL) |
|---|
| 5202 | n/a | return NULL; |
|---|
| 5203 | n/a | /* result = self - offset */ |
|---|
| 5204 | n/a | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
|---|
| 5205 | n/a | (PyDateTime_Delta *)offset, -1); |
|---|
| 5206 | n/a | Py_DECREF(offset); |
|---|
| 5207 | n/a | if (result == NULL) |
|---|
| 5208 | n/a | return NULL; |
|---|
| 5209 | n/a | |
|---|
| 5210 | n/a | /* Make sure result is aware and UTC. */ |
|---|
| 5211 | n/a | if (!HASTZINFO(result)) { |
|---|
| 5212 | n/a | temp = (PyObject *)result; |
|---|
| 5213 | n/a | result = (PyDateTime_DateTime *) |
|---|
| 5214 | n/a | new_datetime_ex2(GET_YEAR(result), |
|---|
| 5215 | n/a | GET_MONTH(result), |
|---|
| 5216 | n/a | GET_DAY(result), |
|---|
| 5217 | n/a | DATE_GET_HOUR(result), |
|---|
| 5218 | n/a | DATE_GET_MINUTE(result), |
|---|
| 5219 | n/a | DATE_GET_SECOND(result), |
|---|
| 5220 | n/a | DATE_GET_MICROSECOND(result), |
|---|
| 5221 | n/a | PyDateTime_TimeZone_UTC, |
|---|
| 5222 | n/a | DATE_GET_FOLD(result), |
|---|
| 5223 | n/a | Py_TYPE(result)); |
|---|
| 5224 | n/a | Py_DECREF(temp); |
|---|
| 5225 | n/a | if (result == NULL) |
|---|
| 5226 | n/a | return NULL; |
|---|
| 5227 | n/a | } |
|---|
| 5228 | n/a | else { |
|---|
| 5229 | n/a | /* Result is already aware - just replace tzinfo. */ |
|---|
| 5230 | n/a | temp = result->tzinfo; |
|---|
| 5231 | n/a | result->tzinfo = PyDateTime_TimeZone_UTC; |
|---|
| 5232 | n/a | Py_INCREF(result->tzinfo); |
|---|
| 5233 | n/a | Py_DECREF(temp); |
|---|
| 5234 | n/a | } |
|---|
| 5235 | n/a | |
|---|
| 5236 | n/a | /* Attach new tzinfo and let fromutc() do the rest. */ |
|---|
| 5237 | n/a | temp = result->tzinfo; |
|---|
| 5238 | n/a | if (tzinfo == Py_None) { |
|---|
| 5239 | n/a | tzinfo = local_timezone(result); |
|---|
| 5240 | n/a | if (tzinfo == NULL) { |
|---|
| 5241 | n/a | Py_DECREF(result); |
|---|
| 5242 | n/a | return NULL; |
|---|
| 5243 | n/a | } |
|---|
| 5244 | n/a | } |
|---|
| 5245 | n/a | else |
|---|
| 5246 | n/a | Py_INCREF(tzinfo); |
|---|
| 5247 | n/a | result->tzinfo = tzinfo; |
|---|
| 5248 | n/a | Py_DECREF(temp); |
|---|
| 5249 | n/a | |
|---|
| 5250 | n/a | temp = (PyObject *)result; |
|---|
| 5251 | n/a | result = (PyDateTime_DateTime *) |
|---|
| 5252 | n/a | _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_fromutc, temp, NULL); |
|---|
| 5253 | n/a | Py_DECREF(temp); |
|---|
| 5254 | n/a | |
|---|
| 5255 | n/a | return result; |
|---|
| 5256 | n/a | } |
|---|
| 5257 | n/a | |
|---|
| 5258 | n/a | static PyObject * |
|---|
| 5259 | n/a | datetime_timetuple(PyDateTime_DateTime *self) |
|---|
| 5260 | n/a | { |
|---|
| 5261 | n/a | int dstflag = -1; |
|---|
| 5262 | n/a | |
|---|
| 5263 | n/a | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
|---|
| 5264 | n/a | PyObject * dst; |
|---|
| 5265 | n/a | |
|---|
| 5266 | n/a | dst = call_dst(self->tzinfo, (PyObject *)self); |
|---|
| 5267 | n/a | if (dst == NULL) |
|---|
| 5268 | n/a | return NULL; |
|---|
| 5269 | n/a | |
|---|
| 5270 | n/a | if (dst != Py_None) |
|---|
| 5271 | n/a | dstflag = delta_bool((PyDateTime_Delta *)dst); |
|---|
| 5272 | n/a | Py_DECREF(dst); |
|---|
| 5273 | n/a | } |
|---|
| 5274 | n/a | return build_struct_time(GET_YEAR(self), |
|---|
| 5275 | n/a | GET_MONTH(self), |
|---|
| 5276 | n/a | GET_DAY(self), |
|---|
| 5277 | n/a | DATE_GET_HOUR(self), |
|---|
| 5278 | n/a | DATE_GET_MINUTE(self), |
|---|
| 5279 | n/a | DATE_GET_SECOND(self), |
|---|
| 5280 | n/a | dstflag); |
|---|
| 5281 | n/a | } |
|---|
| 5282 | n/a | |
|---|
| 5283 | n/a | static long long |
|---|
| 5284 | n/a | local_to_seconds(int year, int month, int day, |
|---|
| 5285 | n/a | int hour, int minute, int second, int fold) |
|---|
| 5286 | n/a | { |
|---|
| 5287 | n/a | long long t, a, b, u1, u2, t1, t2, lt; |
|---|
| 5288 | n/a | t = utc_to_seconds(year, month, day, hour, minute, second); |
|---|
| 5289 | n/a | /* Our goal is to solve t = local(u) for u. */ |
|---|
| 5290 | n/a | lt = local(t); |
|---|
| 5291 | n/a | if (lt == -1) |
|---|
| 5292 | n/a | return -1; |
|---|
| 5293 | n/a | a = lt - t; |
|---|
| 5294 | n/a | u1 = t - a; |
|---|
| 5295 | n/a | t1 = local(u1); |
|---|
| 5296 | n/a | if (t1 == -1) |
|---|
| 5297 | n/a | return -1; |
|---|
| 5298 | n/a | if (t1 == t) { |
|---|
| 5299 | n/a | /* We found one solution, but it may not be the one we need. |
|---|
| 5300 | n/a | * Look for an earlier solution (if `fold` is 0), or a |
|---|
| 5301 | n/a | * later one (if `fold` is 1). */ |
|---|
| 5302 | n/a | if (fold) |
|---|
| 5303 | n/a | u2 = u1 + max_fold_seconds; |
|---|
| 5304 | n/a | else |
|---|
| 5305 | n/a | u2 = u1 - max_fold_seconds; |
|---|
| 5306 | n/a | lt = local(u2); |
|---|
| 5307 | n/a | if (lt == -1) |
|---|
| 5308 | n/a | return -1; |
|---|
| 5309 | n/a | b = lt - u2; |
|---|
| 5310 | n/a | if (a == b) |
|---|
| 5311 | n/a | return u1; |
|---|
| 5312 | n/a | } |
|---|
| 5313 | n/a | else { |
|---|
| 5314 | n/a | b = t1 - u1; |
|---|
| 5315 | n/a | assert(a != b); |
|---|
| 5316 | n/a | } |
|---|
| 5317 | n/a | u2 = t - b; |
|---|
| 5318 | n/a | t2 = local(u2); |
|---|
| 5319 | n/a | if (t2 == -1) |
|---|
| 5320 | n/a | return -1; |
|---|
| 5321 | n/a | if (t2 == t) |
|---|
| 5322 | n/a | return u2; |
|---|
| 5323 | n/a | if (t1 == t) |
|---|
| 5324 | n/a | return u1; |
|---|
| 5325 | n/a | /* We have found both offsets a and b, but neither t - a nor t - b is |
|---|
| 5326 | n/a | * a solution. This means t is in the gap. */ |
|---|
| 5327 | n/a | return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); |
|---|
| 5328 | n/a | } |
|---|
| 5329 | n/a | |
|---|
| 5330 | n/a | /* date(1970,1,1).toordinal() == 719163 */ |
|---|
| 5331 | n/a | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) |
|---|
| 5332 | n/a | |
|---|
| 5333 | n/a | static PyObject * |
|---|
| 5334 | n/a | datetime_timestamp(PyDateTime_DateTime *self) |
|---|
| 5335 | n/a | { |
|---|
| 5336 | n/a | PyObject *result; |
|---|
| 5337 | n/a | |
|---|
| 5338 | n/a | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
|---|
| 5339 | n/a | PyObject *delta; |
|---|
| 5340 | n/a | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
|---|
| 5341 | n/a | if (delta == NULL) |
|---|
| 5342 | n/a | return NULL; |
|---|
| 5343 | n/a | result = delta_total_seconds(delta); |
|---|
| 5344 | n/a | Py_DECREF(delta); |
|---|
| 5345 | n/a | } |
|---|
| 5346 | n/a | else { |
|---|
| 5347 | n/a | long long seconds; |
|---|
| 5348 | n/a | seconds = local_to_seconds(GET_YEAR(self), |
|---|
| 5349 | n/a | GET_MONTH(self), |
|---|
| 5350 | n/a | GET_DAY(self), |
|---|
| 5351 | n/a | DATE_GET_HOUR(self), |
|---|
| 5352 | n/a | DATE_GET_MINUTE(self), |
|---|
| 5353 | n/a | DATE_GET_SECOND(self), |
|---|
| 5354 | n/a | DATE_GET_FOLD(self)); |
|---|
| 5355 | n/a | if (seconds == -1) |
|---|
| 5356 | n/a | return NULL; |
|---|
| 5357 | n/a | result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + |
|---|
| 5358 | n/a | DATE_GET_MICROSECOND(self) / 1e6); |
|---|
| 5359 | n/a | } |
|---|
| 5360 | n/a | return result; |
|---|
| 5361 | n/a | } |
|---|
| 5362 | n/a | |
|---|
| 5363 | n/a | static PyObject * |
|---|
| 5364 | n/a | datetime_getdate(PyDateTime_DateTime *self) |
|---|
| 5365 | n/a | { |
|---|
| 5366 | n/a | return new_date(GET_YEAR(self), |
|---|
| 5367 | n/a | GET_MONTH(self), |
|---|
| 5368 | n/a | GET_DAY(self)); |
|---|
| 5369 | n/a | } |
|---|
| 5370 | n/a | |
|---|
| 5371 | n/a | static PyObject * |
|---|
| 5372 | n/a | datetime_gettime(PyDateTime_DateTime *self) |
|---|
| 5373 | n/a | { |
|---|
| 5374 | n/a | return new_time(DATE_GET_HOUR(self), |
|---|
| 5375 | n/a | DATE_GET_MINUTE(self), |
|---|
| 5376 | n/a | DATE_GET_SECOND(self), |
|---|
| 5377 | n/a | DATE_GET_MICROSECOND(self), |
|---|
| 5378 | n/a | Py_None, |
|---|
| 5379 | n/a | DATE_GET_FOLD(self)); |
|---|
| 5380 | n/a | } |
|---|
| 5381 | n/a | |
|---|
| 5382 | n/a | static PyObject * |
|---|
| 5383 | n/a | datetime_gettimetz(PyDateTime_DateTime *self) |
|---|
| 5384 | n/a | { |
|---|
| 5385 | n/a | return new_time(DATE_GET_HOUR(self), |
|---|
| 5386 | n/a | DATE_GET_MINUTE(self), |
|---|
| 5387 | n/a | DATE_GET_SECOND(self), |
|---|
| 5388 | n/a | DATE_GET_MICROSECOND(self), |
|---|
| 5389 | n/a | GET_DT_TZINFO(self), |
|---|
| 5390 | n/a | DATE_GET_FOLD(self)); |
|---|
| 5391 | n/a | } |
|---|
| 5392 | n/a | |
|---|
| 5393 | n/a | static PyObject * |
|---|
| 5394 | n/a | datetime_utctimetuple(PyDateTime_DateTime *self) |
|---|
| 5395 | n/a | { |
|---|
| 5396 | n/a | int y, m, d, hh, mm, ss; |
|---|
| 5397 | n/a | PyObject *tzinfo; |
|---|
| 5398 | n/a | PyDateTime_DateTime *utcself; |
|---|
| 5399 | n/a | |
|---|
| 5400 | n/a | tzinfo = GET_DT_TZINFO(self); |
|---|
| 5401 | n/a | if (tzinfo == Py_None) { |
|---|
| 5402 | n/a | utcself = self; |
|---|
| 5403 | n/a | Py_INCREF(utcself); |
|---|
| 5404 | n/a | } |
|---|
| 5405 | n/a | else { |
|---|
| 5406 | n/a | PyObject *offset; |
|---|
| 5407 | n/a | offset = call_utcoffset(tzinfo, (PyObject *)self); |
|---|
| 5408 | n/a | if (offset == NULL) |
|---|
| 5409 | n/a | return NULL; |
|---|
| 5410 | n/a | if (offset == Py_None) { |
|---|
| 5411 | n/a | Py_DECREF(offset); |
|---|
| 5412 | n/a | utcself = self; |
|---|
| 5413 | n/a | Py_INCREF(utcself); |
|---|
| 5414 | n/a | } |
|---|
| 5415 | n/a | else { |
|---|
| 5416 | n/a | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
|---|
| 5417 | n/a | (PyDateTime_Delta *)offset, -1); |
|---|
| 5418 | n/a | Py_DECREF(offset); |
|---|
| 5419 | n/a | if (utcself == NULL) |
|---|
| 5420 | n/a | return NULL; |
|---|
| 5421 | n/a | } |
|---|
| 5422 | n/a | } |
|---|
| 5423 | n/a | y = GET_YEAR(utcself); |
|---|
| 5424 | n/a | m = GET_MONTH(utcself); |
|---|
| 5425 | n/a | d = GET_DAY(utcself); |
|---|
| 5426 | n/a | hh = DATE_GET_HOUR(utcself); |
|---|
| 5427 | n/a | mm = DATE_GET_MINUTE(utcself); |
|---|
| 5428 | n/a | ss = DATE_GET_SECOND(utcself); |
|---|
| 5429 | n/a | |
|---|
| 5430 | n/a | Py_DECREF(utcself); |
|---|
| 5431 | n/a | return build_struct_time(y, m, d, hh, mm, ss, 0); |
|---|
| 5432 | n/a | } |
|---|
| 5433 | n/a | |
|---|
| 5434 | n/a | /* Pickle support, a simple use of __reduce__. */ |
|---|
| 5435 | n/a | |
|---|
| 5436 | n/a | /* Let basestate be the non-tzinfo data string. |
|---|
| 5437 | n/a | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
|---|
| 5438 | n/a | * So it's a tuple in any (non-error) case. |
|---|
| 5439 | n/a | * __getstate__ isn't exposed. |
|---|
| 5440 | n/a | */ |
|---|
| 5441 | n/a | static PyObject * |
|---|
| 5442 | n/a | datetime_getstate(PyDateTime_DateTime *self, int proto) |
|---|
| 5443 | n/a | { |
|---|
| 5444 | n/a | PyObject *basestate; |
|---|
| 5445 | n/a | PyObject *result = NULL; |
|---|
| 5446 | n/a | |
|---|
| 5447 | n/a | basestate = PyBytes_FromStringAndSize((char *)self->data, |
|---|
| 5448 | n/a | _PyDateTime_DATETIME_DATASIZE); |
|---|
| 5449 | n/a | if (basestate != NULL) { |
|---|
| 5450 | n/a | if (proto > 3 && DATE_GET_FOLD(self)) |
|---|
| 5451 | n/a | /* Set the first bit of the third byte */ |
|---|
| 5452 | n/a | PyBytes_AS_STRING(basestate)[2] |= (1 << 7); |
|---|
| 5453 | n/a | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
|---|
| 5454 | n/a | result = PyTuple_Pack(1, basestate); |
|---|
| 5455 | n/a | else |
|---|
| 5456 | n/a | result = PyTuple_Pack(2, basestate, self->tzinfo); |
|---|
| 5457 | n/a | Py_DECREF(basestate); |
|---|
| 5458 | n/a | } |
|---|
| 5459 | n/a | return result; |
|---|
| 5460 | n/a | } |
|---|
| 5461 | n/a | |
|---|
| 5462 | n/a | static PyObject * |
|---|
| 5463 | n/a | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) |
|---|
| 5464 | n/a | { |
|---|
| 5465 | n/a | int proto; |
|---|
| 5466 | n/a | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
|---|
| 5467 | n/a | return NULL; |
|---|
| 5468 | n/a | |
|---|
| 5469 | n/a | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); |
|---|
| 5470 | n/a | } |
|---|
| 5471 | n/a | |
|---|
| 5472 | n/a | static PyObject * |
|---|
| 5473 | n/a | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
|---|
| 5474 | n/a | { |
|---|
| 5475 | n/a | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); |
|---|
| 5476 | n/a | } |
|---|
| 5477 | n/a | |
|---|
| 5478 | n/a | static PyMethodDef datetime_methods[] = { |
|---|
| 5479 | n/a | |
|---|
| 5480 | n/a | /* Class methods: */ |
|---|
| 5481 | n/a | |
|---|
| 5482 | n/a | DATETIME_DATETIME_NOW_METHODDEF |
|---|
| 5483 | n/a | |
|---|
| 5484 | n/a | {"utcnow", (PyCFunction)datetime_utcnow, |
|---|
| 5485 | n/a | METH_NOARGS | METH_CLASS, |
|---|
| 5486 | n/a | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
|---|
| 5487 | n/a | |
|---|
| 5488 | n/a | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
|---|
| 5489 | n/a | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
|---|
| 5490 | n/a | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
|---|
| 5491 | n/a | |
|---|
| 5492 | n/a | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
|---|
| 5493 | n/a | METH_VARARGS | METH_CLASS, |
|---|
| 5494 | n/a | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
|---|
| 5495 | n/a | |
|---|
| 5496 | n/a | {"strptime", (PyCFunction)datetime_strptime, |
|---|
| 5497 | n/a | METH_VARARGS | METH_CLASS, |
|---|
| 5498 | n/a | PyDoc_STR("string, format -> new datetime parsed from a string " |
|---|
| 5499 | n/a | "(like time.strptime()).")}, |
|---|
| 5500 | n/a | |
|---|
| 5501 | n/a | {"combine", (PyCFunction)datetime_combine, |
|---|
| 5502 | n/a | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
|---|
| 5503 | n/a | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
|---|
| 5504 | n/a | |
|---|
| 5505 | n/a | /* Instance methods: */ |
|---|
| 5506 | n/a | |
|---|
| 5507 | n/a | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
|---|
| 5508 | n/a | PyDoc_STR("Return date object with same year, month and day.")}, |
|---|
| 5509 | n/a | |
|---|
| 5510 | n/a | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
|---|
| 5511 | n/a | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
|---|
| 5512 | n/a | |
|---|
| 5513 | n/a | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
|---|
| 5514 | n/a | PyDoc_STR("Return time object with same time and tzinfo.")}, |
|---|
| 5515 | n/a | |
|---|
| 5516 | n/a | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
|---|
| 5517 | n/a | PyDoc_STR("Return ctime() style string.")}, |
|---|
| 5518 | n/a | |
|---|
| 5519 | n/a | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
|---|
| 5520 | n/a | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
|---|
| 5521 | n/a | |
|---|
| 5522 | n/a | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
|---|
| 5523 | n/a | PyDoc_STR("Return POSIX timestamp as float.")}, |
|---|
| 5524 | n/a | |
|---|
| 5525 | n/a | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
|---|
| 5526 | n/a | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
|---|
| 5527 | n/a | |
|---|
| 5528 | n/a | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
|---|
| 5529 | n/a | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
|---|
| 5530 | n/a | "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" |
|---|
| 5531 | n/a | "sep is used to separate the year from the time, and " |
|---|
| 5532 | n/a | "defaults to 'T'.\n" |
|---|
| 5533 | n/a | "timespec specifies what components of the time to include" |
|---|
| 5534 | n/a | " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," |
|---|
| 5535 | n/a | " 'milliseconds', and 'microseconds').\n")}, |
|---|
| 5536 | n/a | |
|---|
| 5537 | n/a | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
|---|
| 5538 | n/a | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
|---|
| 5539 | n/a | |
|---|
| 5540 | n/a | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
|---|
| 5541 | n/a | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
|---|
| 5542 | n/a | |
|---|
| 5543 | n/a | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
|---|
| 5544 | n/a | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
|---|
| 5545 | n/a | |
|---|
| 5546 | n/a | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
|---|
| 5547 | n/a | PyDoc_STR("Return datetime with new specified fields.")}, |
|---|
| 5548 | n/a | |
|---|
| 5549 | n/a | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
|---|
| 5550 | n/a | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
|---|
| 5551 | n/a | |
|---|
| 5552 | n/a | {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS, |
|---|
| 5553 | n/a | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
|---|
| 5554 | n/a | |
|---|
| 5555 | n/a | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
|---|
| 5556 | n/a | PyDoc_STR("__reduce__() -> (cls, state)")}, |
|---|
| 5557 | n/a | |
|---|
| 5558 | n/a | {NULL, NULL} |
|---|
| 5559 | n/a | }; |
|---|
| 5560 | n/a | |
|---|
| 5561 | n/a | static const char datetime_doc[] = |
|---|
| 5562 | n/a | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
|---|
| 5563 | n/a | \n\ |
|---|
| 5564 | n/a | The year, month and day arguments are required. tzinfo may be None, or an\n\ |
|---|
| 5565 | n/a | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
|---|
| 5566 | n/a | |
|---|
| 5567 | n/a | static PyNumberMethods datetime_as_number = { |
|---|
| 5568 | n/a | datetime_add, /* nb_add */ |
|---|
| 5569 | n/a | datetime_subtract, /* nb_subtract */ |
|---|
| 5570 | n/a | 0, /* nb_multiply */ |
|---|
| 5571 | n/a | 0, /* nb_remainder */ |
|---|
| 5572 | n/a | 0, /* nb_divmod */ |
|---|
| 5573 | n/a | 0, /* nb_power */ |
|---|
| 5574 | n/a | 0, /* nb_negative */ |
|---|
| 5575 | n/a | 0, /* nb_positive */ |
|---|
| 5576 | n/a | 0, /* nb_absolute */ |
|---|
| 5577 | n/a | 0, /* nb_bool */ |
|---|
| 5578 | n/a | }; |
|---|
| 5579 | n/a | |
|---|
| 5580 | n/a | static PyTypeObject PyDateTime_DateTimeType = { |
|---|
| 5581 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 5582 | n/a | "datetime.datetime", /* tp_name */ |
|---|
| 5583 | n/a | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
|---|
| 5584 | n/a | 0, /* tp_itemsize */ |
|---|
| 5585 | n/a | (destructor)datetime_dealloc, /* tp_dealloc */ |
|---|
| 5586 | n/a | 0, /* tp_print */ |
|---|
| 5587 | n/a | 0, /* tp_getattr */ |
|---|
| 5588 | n/a | 0, /* tp_setattr */ |
|---|
| 5589 | n/a | 0, /* tp_reserved */ |
|---|
| 5590 | n/a | (reprfunc)datetime_repr, /* tp_repr */ |
|---|
| 5591 | n/a | &datetime_as_number, /* tp_as_number */ |
|---|
| 5592 | n/a | 0, /* tp_as_sequence */ |
|---|
| 5593 | n/a | 0, /* tp_as_mapping */ |
|---|
| 5594 | n/a | (hashfunc)datetime_hash, /* tp_hash */ |
|---|
| 5595 | n/a | 0, /* tp_call */ |
|---|
| 5596 | n/a | (reprfunc)datetime_str, /* tp_str */ |
|---|
| 5597 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 5598 | n/a | 0, /* tp_setattro */ |
|---|
| 5599 | n/a | 0, /* tp_as_buffer */ |
|---|
| 5600 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 5601 | n/a | datetime_doc, /* tp_doc */ |
|---|
| 5602 | n/a | 0, /* tp_traverse */ |
|---|
| 5603 | n/a | 0, /* tp_clear */ |
|---|
| 5604 | n/a | datetime_richcompare, /* tp_richcompare */ |
|---|
| 5605 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 5606 | n/a | 0, /* tp_iter */ |
|---|
| 5607 | n/a | 0, /* tp_iternext */ |
|---|
| 5608 | n/a | datetime_methods, /* tp_methods */ |
|---|
| 5609 | n/a | 0, /* tp_members */ |
|---|
| 5610 | n/a | datetime_getset, /* tp_getset */ |
|---|
| 5611 | n/a | &PyDateTime_DateType, /* tp_base */ |
|---|
| 5612 | n/a | 0, /* tp_dict */ |
|---|
| 5613 | n/a | 0, /* tp_descr_get */ |
|---|
| 5614 | n/a | 0, /* tp_descr_set */ |
|---|
| 5615 | n/a | 0, /* tp_dictoffset */ |
|---|
| 5616 | n/a | 0, /* tp_init */ |
|---|
| 5617 | n/a | datetime_alloc, /* tp_alloc */ |
|---|
| 5618 | n/a | datetime_new, /* tp_new */ |
|---|
| 5619 | n/a | 0, /* tp_free */ |
|---|
| 5620 | n/a | }; |
|---|
| 5621 | n/a | |
|---|
| 5622 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 5623 | n/a | * Module methods and initialization. |
|---|
| 5624 | n/a | */ |
|---|
| 5625 | n/a | |
|---|
| 5626 | n/a | static PyMethodDef module_methods[] = { |
|---|
| 5627 | n/a | {NULL, NULL} |
|---|
| 5628 | n/a | }; |
|---|
| 5629 | n/a | |
|---|
| 5630 | n/a | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
|---|
| 5631 | n/a | * datetime.h. |
|---|
| 5632 | n/a | */ |
|---|
| 5633 | n/a | static PyDateTime_CAPI CAPI = { |
|---|
| 5634 | n/a | &PyDateTime_DateType, |
|---|
| 5635 | n/a | &PyDateTime_DateTimeType, |
|---|
| 5636 | n/a | &PyDateTime_TimeType, |
|---|
| 5637 | n/a | &PyDateTime_DeltaType, |
|---|
| 5638 | n/a | &PyDateTime_TZInfoType, |
|---|
| 5639 | n/a | new_date_ex, |
|---|
| 5640 | n/a | new_datetime_ex, |
|---|
| 5641 | n/a | new_time_ex, |
|---|
| 5642 | n/a | new_delta_ex, |
|---|
| 5643 | n/a | datetime_fromtimestamp, |
|---|
| 5644 | n/a | date_fromtimestamp, |
|---|
| 5645 | n/a | new_datetime_ex2, |
|---|
| 5646 | n/a | new_time_ex2 |
|---|
| 5647 | n/a | }; |
|---|
| 5648 | n/a | |
|---|
| 5649 | n/a | |
|---|
| 5650 | n/a | |
|---|
| 5651 | n/a | static struct PyModuleDef datetimemodule = { |
|---|
| 5652 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 5653 | n/a | "_datetime", |
|---|
| 5654 | n/a | "Fast implementation of the datetime type.", |
|---|
| 5655 | n/a | -1, |
|---|
| 5656 | n/a | module_methods, |
|---|
| 5657 | n/a | NULL, |
|---|
| 5658 | n/a | NULL, |
|---|
| 5659 | n/a | NULL, |
|---|
| 5660 | n/a | NULL |
|---|
| 5661 | n/a | }; |
|---|
| 5662 | n/a | |
|---|
| 5663 | n/a | PyMODINIT_FUNC |
|---|
| 5664 | n/a | PyInit__datetime(void) |
|---|
| 5665 | n/a | { |
|---|
| 5666 | n/a | PyObject *m; /* a module object */ |
|---|
| 5667 | n/a | PyObject *d; /* its dict */ |
|---|
| 5668 | n/a | PyObject *x; |
|---|
| 5669 | n/a | PyObject *delta; |
|---|
| 5670 | n/a | |
|---|
| 5671 | n/a | m = PyModule_Create(&datetimemodule); |
|---|
| 5672 | n/a | if (m == NULL) |
|---|
| 5673 | n/a | return NULL; |
|---|
| 5674 | n/a | |
|---|
| 5675 | n/a | if (PyType_Ready(&PyDateTime_DateType) < 0) |
|---|
| 5676 | n/a | return NULL; |
|---|
| 5677 | n/a | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
|---|
| 5678 | n/a | return NULL; |
|---|
| 5679 | n/a | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
|---|
| 5680 | n/a | return NULL; |
|---|
| 5681 | n/a | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
|---|
| 5682 | n/a | return NULL; |
|---|
| 5683 | n/a | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
|---|
| 5684 | n/a | return NULL; |
|---|
| 5685 | n/a | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
|---|
| 5686 | n/a | return NULL; |
|---|
| 5687 | n/a | |
|---|
| 5688 | n/a | /* timedelta values */ |
|---|
| 5689 | n/a | d = PyDateTime_DeltaType.tp_dict; |
|---|
| 5690 | n/a | |
|---|
| 5691 | n/a | x = new_delta(0, 0, 1, 0); |
|---|
| 5692 | n/a | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|---|
| 5693 | n/a | return NULL; |
|---|
| 5694 | n/a | Py_DECREF(x); |
|---|
| 5695 | n/a | |
|---|
| 5696 | n/a | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
|---|
| 5697 | n/a | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|---|
| 5698 | n/a | return NULL; |
|---|
| 5699 | n/a | Py_DECREF(x); |
|---|
| 5700 | n/a | |
|---|
| 5701 | n/a | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
|---|
| 5702 | n/a | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|---|
| 5703 | n/a | return NULL; |
|---|
| 5704 | n/a | Py_DECREF(x); |
|---|
| 5705 | n/a | |
|---|
| 5706 | n/a | /* date values */ |
|---|
| 5707 | n/a | d = PyDateTime_DateType.tp_dict; |
|---|
| 5708 | n/a | |
|---|
| 5709 | n/a | x = new_date(1, 1, 1); |
|---|
| 5710 | n/a | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|---|
| 5711 | n/a | return NULL; |
|---|
| 5712 | n/a | Py_DECREF(x); |
|---|
| 5713 | n/a | |
|---|
| 5714 | n/a | x = new_date(MAXYEAR, 12, 31); |
|---|
| 5715 | n/a | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|---|
| 5716 | n/a | return NULL; |
|---|
| 5717 | n/a | Py_DECREF(x); |
|---|
| 5718 | n/a | |
|---|
| 5719 | n/a | x = new_delta(1, 0, 0, 0); |
|---|
| 5720 | n/a | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|---|
| 5721 | n/a | return NULL; |
|---|
| 5722 | n/a | Py_DECREF(x); |
|---|
| 5723 | n/a | |
|---|
| 5724 | n/a | /* time values */ |
|---|
| 5725 | n/a | d = PyDateTime_TimeType.tp_dict; |
|---|
| 5726 | n/a | |
|---|
| 5727 | n/a | x = new_time(0, 0, 0, 0, Py_None, 0); |
|---|
| 5728 | n/a | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|---|
| 5729 | n/a | return NULL; |
|---|
| 5730 | n/a | Py_DECREF(x); |
|---|
| 5731 | n/a | |
|---|
| 5732 | n/a | x = new_time(23, 59, 59, 999999, Py_None, 0); |
|---|
| 5733 | n/a | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|---|
| 5734 | n/a | return NULL; |
|---|
| 5735 | n/a | Py_DECREF(x); |
|---|
| 5736 | n/a | |
|---|
| 5737 | n/a | x = new_delta(0, 0, 1, 0); |
|---|
| 5738 | n/a | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|---|
| 5739 | n/a | return NULL; |
|---|
| 5740 | n/a | Py_DECREF(x); |
|---|
| 5741 | n/a | |
|---|
| 5742 | n/a | /* datetime values */ |
|---|
| 5743 | n/a | d = PyDateTime_DateTimeType.tp_dict; |
|---|
| 5744 | n/a | |
|---|
| 5745 | n/a | x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None, 0); |
|---|
| 5746 | n/a | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|---|
| 5747 | n/a | return NULL; |
|---|
| 5748 | n/a | Py_DECREF(x); |
|---|
| 5749 | n/a | |
|---|
| 5750 | n/a | x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None, 0); |
|---|
| 5751 | n/a | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|---|
| 5752 | n/a | return NULL; |
|---|
| 5753 | n/a | Py_DECREF(x); |
|---|
| 5754 | n/a | |
|---|
| 5755 | n/a | x = new_delta(0, 0, 1, 0); |
|---|
| 5756 | n/a | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
|---|
| 5757 | n/a | return NULL; |
|---|
| 5758 | n/a | Py_DECREF(x); |
|---|
| 5759 | n/a | |
|---|
| 5760 | n/a | /* timezone values */ |
|---|
| 5761 | n/a | d = PyDateTime_TimeZoneType.tp_dict; |
|---|
| 5762 | n/a | |
|---|
| 5763 | n/a | delta = new_delta(0, 0, 0, 0); |
|---|
| 5764 | n/a | if (delta == NULL) |
|---|
| 5765 | n/a | return NULL; |
|---|
| 5766 | n/a | x = create_timezone(delta, NULL); |
|---|
| 5767 | n/a | Py_DECREF(delta); |
|---|
| 5768 | n/a | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
|---|
| 5769 | n/a | return NULL; |
|---|
| 5770 | n/a | PyDateTime_TimeZone_UTC = x; |
|---|
| 5771 | n/a | |
|---|
| 5772 | n/a | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
|---|
| 5773 | n/a | if (delta == NULL) |
|---|
| 5774 | n/a | return NULL; |
|---|
| 5775 | n/a | x = create_timezone(delta, NULL); |
|---|
| 5776 | n/a | Py_DECREF(delta); |
|---|
| 5777 | n/a | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
|---|
| 5778 | n/a | return NULL; |
|---|
| 5779 | n/a | Py_DECREF(x); |
|---|
| 5780 | n/a | |
|---|
| 5781 | n/a | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
|---|
| 5782 | n/a | if (delta == NULL) |
|---|
| 5783 | n/a | return NULL; |
|---|
| 5784 | n/a | x = create_timezone(delta, NULL); |
|---|
| 5785 | n/a | Py_DECREF(delta); |
|---|
| 5786 | n/a | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
|---|
| 5787 | n/a | return NULL; |
|---|
| 5788 | n/a | Py_DECREF(x); |
|---|
| 5789 | n/a | |
|---|
| 5790 | n/a | /* Epoch */ |
|---|
| 5791 | n/a | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
|---|
| 5792 | n/a | PyDateTime_TimeZone_UTC, 0); |
|---|
| 5793 | n/a | if (PyDateTime_Epoch == NULL) |
|---|
| 5794 | n/a | return NULL; |
|---|
| 5795 | n/a | |
|---|
| 5796 | n/a | /* module initialization */ |
|---|
| 5797 | n/a | PyModule_AddIntMacro(m, MINYEAR); |
|---|
| 5798 | n/a | PyModule_AddIntMacro(m, MAXYEAR); |
|---|
| 5799 | n/a | |
|---|
| 5800 | n/a | Py_INCREF(&PyDateTime_DateType); |
|---|
| 5801 | n/a | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
|---|
| 5802 | n/a | |
|---|
| 5803 | n/a | Py_INCREF(&PyDateTime_DateTimeType); |
|---|
| 5804 | n/a | PyModule_AddObject(m, "datetime", |
|---|
| 5805 | n/a | (PyObject *)&PyDateTime_DateTimeType); |
|---|
| 5806 | n/a | |
|---|
| 5807 | n/a | Py_INCREF(&PyDateTime_TimeType); |
|---|
| 5808 | n/a | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
|---|
| 5809 | n/a | |
|---|
| 5810 | n/a | Py_INCREF(&PyDateTime_DeltaType); |
|---|
| 5811 | n/a | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
|---|
| 5812 | n/a | |
|---|
| 5813 | n/a | Py_INCREF(&PyDateTime_TZInfoType); |
|---|
| 5814 | n/a | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
|---|
| 5815 | n/a | |
|---|
| 5816 | n/a | Py_INCREF(&PyDateTime_TimeZoneType); |
|---|
| 5817 | n/a | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
|---|
| 5818 | n/a | |
|---|
| 5819 | n/a | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
|---|
| 5820 | n/a | if (x == NULL) |
|---|
| 5821 | n/a | return NULL; |
|---|
| 5822 | n/a | PyModule_AddObject(m, "datetime_CAPI", x); |
|---|
| 5823 | n/a | |
|---|
| 5824 | n/a | /* A 4-year cycle has an extra leap day over what we'd get from |
|---|
| 5825 | n/a | * pasting together 4 single years. |
|---|
| 5826 | n/a | */ |
|---|
| 5827 | n/a | Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1); |
|---|
| 5828 | n/a | assert(DI4Y == days_before_year(4+1)); |
|---|
| 5829 | n/a | |
|---|
| 5830 | n/a | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
|---|
| 5831 | n/a | * get from pasting together 4 100-year cycles. |
|---|
| 5832 | n/a | */ |
|---|
| 5833 | n/a | Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1); |
|---|
| 5834 | n/a | assert(DI400Y == days_before_year(400+1)); |
|---|
| 5835 | n/a | |
|---|
| 5836 | n/a | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
|---|
| 5837 | n/a | * pasting together 25 4-year cycles. |
|---|
| 5838 | n/a | */ |
|---|
| 5839 | n/a | Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1); |
|---|
| 5840 | n/a | assert(DI100Y == days_before_year(100+1)); |
|---|
| 5841 | n/a | |
|---|
| 5842 | n/a | one = PyLong_FromLong(1); |
|---|
| 5843 | n/a | us_per_ms = PyLong_FromLong(1000); |
|---|
| 5844 | n/a | us_per_second = PyLong_FromLong(1000000); |
|---|
| 5845 | n/a | us_per_minute = PyLong_FromLong(60000000); |
|---|
| 5846 | n/a | seconds_per_day = PyLong_FromLong(24 * 3600); |
|---|
| 5847 | n/a | if (one == NULL || us_per_ms == NULL || us_per_second == NULL || |
|---|
| 5848 | n/a | us_per_minute == NULL || seconds_per_day == NULL) |
|---|
| 5849 | n/a | return NULL; |
|---|
| 5850 | n/a | |
|---|
| 5851 | n/a | /* The rest are too big for 32-bit ints, but even |
|---|
| 5852 | n/a | * us_per_week fits in 40 bits, so doubles should be exact. |
|---|
| 5853 | n/a | */ |
|---|
| 5854 | n/a | us_per_hour = PyLong_FromDouble(3600000000.0); |
|---|
| 5855 | n/a | us_per_day = PyLong_FromDouble(86400000000.0); |
|---|
| 5856 | n/a | us_per_week = PyLong_FromDouble(604800000000.0); |
|---|
| 5857 | n/a | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
|---|
| 5858 | n/a | return NULL; |
|---|
| 5859 | n/a | return m; |
|---|
| 5860 | n/a | } |
|---|
| 5861 | n/a | |
|---|
| 5862 | n/a | /* --------------------------------------------------------------------------- |
|---|
| 5863 | n/a | Some time zone algebra. For a datetime x, let |
|---|
| 5864 | n/a | x.n = x stripped of its timezone -- its naive time. |
|---|
| 5865 | n/a | x.o = x.utcoffset(), and assuming that doesn't raise an exception or |
|---|
| 5866 | n/a | return None |
|---|
| 5867 | n/a | x.d = x.dst(), and assuming that doesn't raise an exception or |
|---|
| 5868 | n/a | return None |
|---|
| 5869 | n/a | x.s = x's standard offset, x.o - x.d |
|---|
| 5870 | n/a | |
|---|
| 5871 | n/a | Now some derived rules, where k is a duration (timedelta). |
|---|
| 5872 | n/a | |
|---|
| 5873 | n/a | 1. x.o = x.s + x.d |
|---|
| 5874 | n/a | This follows from the definition of x.s. |
|---|
| 5875 | n/a | |
|---|
| 5876 | n/a | 2. If x and y have the same tzinfo member, x.s = y.s. |
|---|
| 5877 | n/a | This is actually a requirement, an assumption we need to make about |
|---|
| 5878 | n/a | sane tzinfo classes. |
|---|
| 5879 | n/a | |
|---|
| 5880 | n/a | 3. The naive UTC time corresponding to x is x.n - x.o. |
|---|
| 5881 | n/a | This is again a requirement for a sane tzinfo class. |
|---|
| 5882 | n/a | |
|---|
| 5883 | n/a | 4. (x+k).s = x.s |
|---|
| 5884 | n/a | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
|---|
| 5885 | n/a | |
|---|
| 5886 | n/a | 5. (x+k).n = x.n + k |
|---|
| 5887 | n/a | Again follows from how arithmetic is defined. |
|---|
| 5888 | n/a | |
|---|
| 5889 | n/a | Now we can explain tz.fromutc(x). Let's assume it's an interesting case |
|---|
| 5890 | n/a | (meaning that the various tzinfo methods exist, and don't blow up or return |
|---|
| 5891 | n/a | None when called). |
|---|
| 5892 | n/a | |
|---|
| 5893 | n/a | The function wants to return a datetime y with timezone tz, equivalent to x. |
|---|
| 5894 | n/a | x is already in UTC. |
|---|
| 5895 | n/a | |
|---|
| 5896 | n/a | By #3, we want |
|---|
| 5897 | n/a | |
|---|
| 5898 | n/a | y.n - y.o = x.n [1] |
|---|
| 5899 | n/a | |
|---|
| 5900 | n/a | The algorithm starts by attaching tz to x.n, and calling that y. So |
|---|
| 5901 | n/a | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
|---|
| 5902 | n/a | becomes true; in effect, we want to solve [2] for k: |
|---|
| 5903 | n/a | |
|---|
| 5904 | n/a | (y+k).n - (y+k).o = x.n [2] |
|---|
| 5905 | n/a | |
|---|
| 5906 | n/a | By #1, this is the same as |
|---|
| 5907 | n/a | |
|---|
| 5908 | n/a | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
|---|
| 5909 | n/a | |
|---|
| 5910 | n/a | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
|---|
| 5911 | n/a | Substituting that into [3], |
|---|
| 5912 | n/a | |
|---|
| 5913 | n/a | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
|---|
| 5914 | n/a | k - (y+k).s - (y+k).d = 0; rearranging, |
|---|
| 5915 | n/a | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
|---|
| 5916 | n/a | k = y.s - (y+k).d |
|---|
| 5917 | n/a | |
|---|
| 5918 | n/a | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
|---|
| 5919 | n/a | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
|---|
| 5920 | n/a | very large, since all offset-returning methods return a duration of magnitude |
|---|
| 5921 | n/a | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
|---|
| 5922 | n/a | be 0, so ignoring it has no consequence then. |
|---|
| 5923 | n/a | |
|---|
| 5924 | n/a | In any case, the new value is |
|---|
| 5925 | n/a | |
|---|
| 5926 | n/a | z = y + y.s [4] |
|---|
| 5927 | n/a | |
|---|
| 5928 | n/a | It's helpful to step back at look at [4] from a higher level: it's simply |
|---|
| 5929 | n/a | mapping from UTC to tz's standard time. |
|---|
| 5930 | n/a | |
|---|
| 5931 | n/a | At this point, if |
|---|
| 5932 | n/a | |
|---|
| 5933 | n/a | z.n - z.o = x.n [5] |
|---|
| 5934 | n/a | |
|---|
| 5935 | n/a | we have an equivalent time, and are almost done. The insecurity here is |
|---|
| 5936 | n/a | at the start of daylight time. Picture US Eastern for concreteness. The wall |
|---|
| 5937 | n/a | time jumps from 1:59 to 3:00, and wall hours of the form 2:MM don't make good |
|---|
| 5938 | n/a | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
|---|
| 5939 | n/a | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
|---|
| 5940 | n/a | on the day DST starts. We want to return the 1:MM EST spelling because that's |
|---|
| 5941 | n/a | the only spelling that makes sense on the local wall clock. |
|---|
| 5942 | n/a | |
|---|
| 5943 | n/a | In fact, if [5] holds at this point, we do have the standard-time spelling, |
|---|
| 5944 | n/a | but that takes a bit of proof. We first prove a stronger result. What's the |
|---|
| 5945 | n/a | difference between the LHS and RHS of [5]? Let |
|---|
| 5946 | n/a | |
|---|
| 5947 | n/a | diff = x.n - (z.n - z.o) [6] |
|---|
| 5948 | n/a | |
|---|
| 5949 | n/a | Now |
|---|
| 5950 | n/a | z.n = by [4] |
|---|
| 5951 | n/a | (y + y.s).n = by #5 |
|---|
| 5952 | n/a | y.n + y.s = since y.n = x.n |
|---|
| 5953 | n/a | x.n + y.s = since z and y are have the same tzinfo member, |
|---|
| 5954 | n/a | y.s = z.s by #2 |
|---|
| 5955 | n/a | x.n + z.s |
|---|
| 5956 | n/a | |
|---|
| 5957 | n/a | Plugging that back into [6] gives |
|---|
| 5958 | n/a | |
|---|
| 5959 | n/a | diff = |
|---|
| 5960 | n/a | x.n - ((x.n + z.s) - z.o) = expanding |
|---|
| 5961 | n/a | x.n - x.n - z.s + z.o = cancelling |
|---|
| 5962 | n/a | - z.s + z.o = by #2 |
|---|
| 5963 | n/a | z.d |
|---|
| 5964 | n/a | |
|---|
| 5965 | n/a | So diff = z.d. |
|---|
| 5966 | n/a | |
|---|
| 5967 | n/a | If [5] is true now, diff = 0, so z.d = 0 too, and we have the standard-time |
|---|
| 5968 | n/a | spelling we wanted in the endcase described above. We're done. Contrarily, |
|---|
| 5969 | n/a | if z.d = 0, then we have a UTC equivalent, and are also done. |
|---|
| 5970 | n/a | |
|---|
| 5971 | n/a | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
|---|
| 5972 | n/a | add to z (in effect, z is in tz's standard time, and we need to shift the |
|---|
| 5973 | n/a | local clock into tz's daylight time). |
|---|
| 5974 | n/a | |
|---|
| 5975 | n/a | Let |
|---|
| 5976 | n/a | |
|---|
| 5977 | n/a | z' = z + z.d = z + diff [7] |
|---|
| 5978 | n/a | |
|---|
| 5979 | n/a | and we can again ask whether |
|---|
| 5980 | n/a | |
|---|
| 5981 | n/a | z'.n - z'.o = x.n [8] |
|---|
| 5982 | n/a | |
|---|
| 5983 | n/a | If so, we're done. If not, the tzinfo class is insane, according to the |
|---|
| 5984 | n/a | assumptions we've made. This also requires a bit of proof. As before, let's |
|---|
| 5985 | n/a | compute the difference between the LHS and RHS of [8] (and skipping some of |
|---|
| 5986 | n/a | the justifications for the kinds of substitutions we've done several times |
|---|
| 5987 | n/a | already): |
|---|
| 5988 | n/a | |
|---|
| 5989 | n/a | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
|---|
| 5990 | n/a | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
|---|
| 5991 | n/a | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
|---|
| 5992 | n/a | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
|---|
| 5993 | n/a | - z.n + z.n - z.o + z'.o = cancel z.n |
|---|
| 5994 | n/a | - z.o + z'.o = #1 twice |
|---|
| 5995 | n/a | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
|---|
| 5996 | n/a | z'.d - z.d |
|---|
| 5997 | n/a | |
|---|
| 5998 | n/a | So z' is UTC-equivalent to x iff z'.d = z.d at this point. If they are equal, |
|---|
| 5999 | n/a | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
|---|
| 6000 | n/a | return z', not bothering to compute z'.d. |
|---|
| 6001 | n/a | |
|---|
| 6002 | n/a | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
|---|
| 6003 | n/a | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
|---|
| 6004 | n/a | would have to change the result dst() returns: we start in DST, and moving |
|---|
| 6005 | n/a | a little further into it takes us out of DST. |
|---|
| 6006 | n/a | |
|---|
| 6007 | n/a | There isn't a sane case where this can happen. The closest it gets is at |
|---|
| 6008 | n/a | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
|---|
| 6009 | n/a | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
|---|
| 6010 | n/a | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
|---|
| 6011 | n/a | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
|---|
| 6012 | n/a | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
|---|
| 6013 | n/a | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
|---|
| 6014 | n/a | standard time. Since that's what the local clock *does*, we want to map both |
|---|
| 6015 | n/a | UTC hours 5:MM and 6:MM to 1:MM Eastern. The result is ambiguous |
|---|
| 6016 | n/a | in local time, but so it goes -- it's the way the local clock works. |
|---|
| 6017 | n/a | |
|---|
| 6018 | n/a | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
|---|
| 6019 | n/a | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
|---|
| 6020 | n/a | z' = z + z.d = 1:MM then, and z'.d=0, and z'.d - z.d = -60 != 0 so [8] |
|---|
| 6021 | n/a | (correctly) concludes that z' is not UTC-equivalent to x. |
|---|
| 6022 | n/a | |
|---|
| 6023 | n/a | Because we know z.d said z was in daylight time (else [5] would have held and |
|---|
| 6024 | n/a | we would have stopped then), and we know z.d != z'.d (else [8] would have held |
|---|
| 6025 | n/a | and we would have stopped then), and there are only 2 possible values dst() can |
|---|
| 6026 | n/a | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
|---|
| 6027 | n/a | but the reasoning doesn't depend on the example -- it depends on there being |
|---|
| 6028 | n/a | two possible dst() outcomes, one zero and the other non-zero). Therefore |
|---|
| 6029 | n/a | z' must be in standard time, and is the spelling we want in this case. |
|---|
| 6030 | n/a | |
|---|
| 6031 | n/a | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
|---|
| 6032 | n/a | concerned (because it takes z' as being in standard time rather than the |
|---|
| 6033 | n/a | daylight time we intend here), but returning it gives the real-life "local |
|---|
| 6034 | n/a | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
|---|
| 6035 | n/a | tz. |
|---|
| 6036 | n/a | |
|---|
| 6037 | n/a | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
|---|
| 6038 | n/a | the 1:MM standard time spelling we want. |
|---|
| 6039 | n/a | |
|---|
| 6040 | n/a | So how can this break? One of the assumptions must be violated. Two |
|---|
| 6041 | n/a | possibilities: |
|---|
| 6042 | n/a | |
|---|
| 6043 | n/a | 1) [2] effectively says that y.s is invariant across all y belong to a given |
|---|
| 6044 | n/a | time zone. This isn't true if, for political reasons or continental drift, |
|---|
| 6045 | n/a | a region decides to change its base offset from UTC. |
|---|
| 6046 | n/a | |
|---|
| 6047 | n/a | 2) There may be versions of "double daylight" time where the tail end of |
|---|
| 6048 | n/a | the analysis gives up a step too early. I haven't thought about that |
|---|
| 6049 | n/a | enough to say. |
|---|
| 6050 | n/a | |
|---|
| 6051 | n/a | In any case, it's clear that the default fromutc() is strong enough to handle |
|---|
| 6052 | n/a | "almost all" time zones: so long as the standard offset is invariant, it |
|---|
| 6053 | n/a | doesn't matter if daylight time transition points change from year to year, or |
|---|
| 6054 | n/a | if daylight time is skipped in some years; it doesn't matter how large or |
|---|
| 6055 | n/a | small dst() may get within its bounds; and it doesn't even matter if some |
|---|
| 6056 | n/a | perverse time zone returns a negative dst()). So a breaking case must be |
|---|
| 6057 | n/a | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
|---|
| 6058 | n/a | --------------------------------------------------------------------------- */ |
|---|