| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1 | /*  C implementation for the date/time type documented at | 
 | 2 |  *  http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage | 
 | 3 |  */ | 
 | 4 |  | 
 | 5 | #include "Python.h" | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6 | #include "structmember.h" | 
 | 7 |  | 
 | 8 | #include <time.h> | 
 | 9 |  | 
| Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 10 | #ifdef MS_WINDOWS | 
 | 11 | #  include <winsock2.h>         /* struct timeval */ | 
 | 12 | #endif | 
 | 13 |  | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 14 | /* Differentiate between building the core module and building extension | 
 | 15 |  * modules. | 
 | 16 |  */ | 
| Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 17 | #ifndef Py_BUILD_CORE | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 18 | #define Py_BUILD_CORE | 
| Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 19 | #endif | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 20 | #include "datetime.h" | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 21 | #undef Py_BUILD_CORE | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 22 |  | 
| Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 23 | /*[clinic input] | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 24 | module datetime | 
| Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 25 | class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" | 
| Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 26 | [clinic start generated code]*/ | 
| Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 27 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78142cb64b9e98bc]*/ | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 28 |  | 
| Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 29 | #include "clinic/_datetimemodule.c.h" | 
 | 30 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 31 | /* We require that C int be at least 32 bits, and use int virtually | 
 | 32 |  * everywhere.  In just a few cases we use a temp long, where a Python | 
 | 33 |  * API returns a C long.  In such cases, we have to ensure that the | 
 | 34 |  * final result fits in a C int (this can be an issue on 64-bit boxes). | 
 | 35 |  */ | 
 | 36 | #if SIZEOF_INT < 4 | 
| Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 37 | #       error "_datetime.c requires that C int have at least 32 bits" | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 38 | #endif | 
 | 39 |  | 
 | 40 | #define MINYEAR 1 | 
 | 41 | #define MAXYEAR 9999 | 
| Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 42 | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 43 |  | 
 | 44 | /* Nine decimal digits is easy to communicate, and leaves enough room | 
 | 45 |  * so that two delta days can be added w/o fear of overflowing a signed | 
 | 46 |  * 32-bit int, and with plenty of room left over to absorb any possible | 
 | 47 |  * carries from adding seconds. | 
 | 48 |  */ | 
 | 49 | #define MAX_DELTA_DAYS 999999999 | 
 | 50 |  | 
 | 51 | /* Rename the long macros in datetime.h to more reasonable short names. */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | #define GET_YEAR                PyDateTime_GET_YEAR | 
 | 53 | #define GET_MONTH               PyDateTime_GET_MONTH | 
 | 54 | #define GET_DAY                 PyDateTime_GET_DAY | 
 | 55 | #define DATE_GET_HOUR           PyDateTime_DATE_GET_HOUR | 
 | 56 | #define DATE_GET_MINUTE         PyDateTime_DATE_GET_MINUTE | 
 | 57 | #define DATE_GET_SECOND         PyDateTime_DATE_GET_SECOND | 
 | 58 | #define DATE_GET_MICROSECOND    PyDateTime_DATE_GET_MICROSECOND | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 59 | #define DATE_GET_FOLD           PyDateTime_DATE_GET_FOLD | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 60 |  | 
 | 61 | /* Date accessors for date and datetime. */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | #define SET_YEAR(o, v)          (((o)->data[0] = ((v) & 0xff00) >> 8), \ | 
 | 63 |                  ((o)->data[1] = ((v) & 0x00ff))) | 
 | 64 | #define SET_MONTH(o, v)         (PyDateTime_GET_MONTH(o) = (v)) | 
 | 65 | #define SET_DAY(o, v)           (PyDateTime_GET_DAY(o) = (v)) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 66 |  | 
 | 67 | /* Date/Time accessors for datetime. */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | #define DATE_SET_HOUR(o, v)     (PyDateTime_DATE_GET_HOUR(o) = (v)) | 
 | 69 | #define DATE_SET_MINUTE(o, v)   (PyDateTime_DATE_GET_MINUTE(o) = (v)) | 
 | 70 | #define DATE_SET_SECOND(o, v)   (PyDateTime_DATE_GET_SECOND(o) = (v)) | 
 | 71 | #define DATE_SET_MICROSECOND(o, v)      \ | 
 | 72 |     (((o)->data[7] = ((v) & 0xff0000) >> 16), \ | 
 | 73 |      ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ | 
 | 74 |      ((o)->data[9] = ((v) & 0x0000ff))) | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 75 | #define DATE_SET_FOLD(o, v)   (PyDateTime_DATE_GET_FOLD(o) = (v)) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 76 |  | 
 | 77 | /* Time accessors for time. */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | #define TIME_GET_HOUR           PyDateTime_TIME_GET_HOUR | 
 | 79 | #define TIME_GET_MINUTE         PyDateTime_TIME_GET_MINUTE | 
 | 80 | #define TIME_GET_SECOND         PyDateTime_TIME_GET_SECOND | 
 | 81 | #define TIME_GET_MICROSECOND    PyDateTime_TIME_GET_MICROSECOND | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 82 | #define TIME_GET_FOLD           PyDateTime_TIME_GET_FOLD | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | #define TIME_SET_HOUR(o, v)     (PyDateTime_TIME_GET_HOUR(o) = (v)) | 
 | 84 | #define TIME_SET_MINUTE(o, v)   (PyDateTime_TIME_GET_MINUTE(o) = (v)) | 
 | 85 | #define TIME_SET_SECOND(o, v)   (PyDateTime_TIME_GET_SECOND(o) = (v)) | 
 | 86 | #define TIME_SET_MICROSECOND(o, v)      \ | 
 | 87 |     (((o)->data[3] = ((v) & 0xff0000) >> 16), \ | 
 | 88 |      ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ | 
 | 89 |      ((o)->data[5] = ((v) & 0x0000ff))) | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 90 | #define TIME_SET_FOLD(o, v)   (PyDateTime_TIME_GET_FOLD(o) = (v)) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 91 |  | 
 | 92 | /* Delta accessors for timedelta. */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | #define GET_TD_DAYS(o)          (((PyDateTime_Delta *)(o))->days) | 
 | 94 | #define GET_TD_SECONDS(o)       (((PyDateTime_Delta *)(o))->seconds) | 
 | 95 | #define GET_TD_MICROSECONDS(o)  (((PyDateTime_Delta *)(o))->microseconds) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 96 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | #define SET_TD_DAYS(o, v)       ((o)->days = (v)) | 
 | 98 | #define SET_TD_SECONDS(o, v)    ((o)->seconds = (v)) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 99 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) | 
 | 100 |  | 
| Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 101 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns | 
 | 102 |  * p->hastzinfo. | 
 | 103 |  */ | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 104 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) | 
 | 105 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ | 
 | 106 |                             ((PyDateTime_Time *)(p))->tzinfo : Py_None) | 
 | 107 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ | 
 | 108 |                           ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) | 
| Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 109 | /* M is a char or int claiming to be a valid month.  The macro is equivalent | 
 | 110 |  * to the two-sided Python test | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 |  *      1 <= M <= 12 | 
| Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 112 |  */ | 
 | 113 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) | 
 | 114 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 115 | /* Forward declarations. */ | 
 | 116 | static PyTypeObject PyDateTime_DateType; | 
 | 117 | static PyTypeObject PyDateTime_DateTimeType; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 118 | static PyTypeObject PyDateTime_DeltaType; | 
 | 119 | static PyTypeObject PyDateTime_TimeType; | 
 | 120 | static PyTypeObject PyDateTime_TZInfoType; | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 121 | static PyTypeObject PyDateTime_TimeZoneType; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 122 |  | 
| Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 123 | _Py_IDENTIFIER(as_integer_ratio); | 
 | 124 | _Py_IDENTIFIER(fromutc); | 
 | 125 | _Py_IDENTIFIER(isoformat); | 
 | 126 | _Py_IDENTIFIER(strftime); | 
 | 127 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 128 | /* --------------------------------------------------------------------------- | 
 | 129 |  * Math utilities. | 
 | 130 |  */ | 
 | 131 |  | 
 | 132 | /* k = i+j overflows iff k differs in sign from both inputs, | 
 | 133 |  * iff k^i has sign bit set and k^j has sign bit set, | 
 | 134 |  * iff (k^i)&(k^j) has sign bit set. | 
 | 135 |  */ | 
 | 136 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 |     ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 138 |  | 
 | 139 | /* Compute Python divmod(x, y), returning the quotient and storing the | 
 | 140 |  * remainder into *r.  The quotient is the floor of x/y, and that's | 
 | 141 |  * the real point of this.  C will probably truncate instead (C99 | 
 | 142 |  * requires truncation; C89 left it implementation-defined). | 
 | 143 |  * Simplification:  we *require* that y > 0 here.  That's appropriate | 
 | 144 |  * for all the uses made of it.  This simplifies the code and makes | 
 | 145 |  * the overflow case impossible (divmod(LONG_MIN, -1) is the only | 
 | 146 |  * overflow case). | 
 | 147 |  */ | 
 | 148 | static int | 
 | 149 | divmod(int x, int y, int *r) | 
 | 150 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 |     int quo; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 152 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 |     assert(y > 0); | 
 | 154 |     quo = x / y; | 
 | 155 |     *r = x - quo * y; | 
 | 156 |     if (*r < 0) { | 
 | 157 |         --quo; | 
 | 158 |         *r += y; | 
 | 159 |     } | 
 | 160 |     assert(0 <= *r && *r < y); | 
 | 161 |     return quo; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 162 | } | 
 | 163 |  | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 164 | /* Nearest integer to m / n for integers m and n. Half-integer results | 
 | 165 |  * are rounded to even. | 
 | 166 |  */ | 
 | 167 | static PyObject * | 
 | 168 | divide_nearest(PyObject *m, PyObject *n) | 
 | 169 | { | 
 | 170 |     PyObject *result; | 
 | 171 |     PyObject *temp; | 
 | 172 |  | 
| Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 173 |     temp = _PyLong_DivmodNear(m, n); | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 174 |     if (temp == NULL) | 
 | 175 |         return NULL; | 
 | 176 |     result = PyTuple_GET_ITEM(temp, 0); | 
 | 177 |     Py_INCREF(result); | 
 | 178 |     Py_DECREF(temp); | 
 | 179 |  | 
 | 180 |     return result; | 
 | 181 | } | 
 | 182 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 183 | /* --------------------------------------------------------------------------- | 
 | 184 |  * General calendrical helper functions | 
 | 185 |  */ | 
 | 186 |  | 
 | 187 | /* For each month ordinal in 1..12, the number of days in that month, | 
 | 188 |  * and the number of days before that month in the same year.  These | 
 | 189 |  * are correct for non-leap years only. | 
 | 190 |  */ | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 191 | static const int _days_in_month[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 |     0, /* unused; this vector uses 1-based indexing */ | 
 | 193 |     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 194 | }; | 
 | 195 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 196 | static const int _days_before_month[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 |     0, /* unused; this vector uses 1-based indexing */ | 
 | 198 |     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 199 | }; | 
 | 200 |  | 
 | 201 | /* year -> 1 if leap year, else 0. */ | 
 | 202 | static int | 
 | 203 | is_leap(int year) | 
 | 204 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 |     /* Cast year to unsigned.  The result is the same either way, but | 
 | 206 |      * C can generate faster code for unsigned mod than for signed | 
 | 207 |      * mod (especially for % 4 -- a good compiler should just grab | 
 | 208 |      * the last 2 bits when the LHS is unsigned). | 
 | 209 |      */ | 
 | 210 |     const unsigned int ayear = (unsigned int)year; | 
 | 211 |     return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 212 | } | 
 | 213 |  | 
 | 214 | /* year, month -> number of days in that month in that year */ | 
 | 215 | static int | 
 | 216 | days_in_month(int year, int month) | 
 | 217 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 |     assert(month >= 1); | 
 | 219 |     assert(month <= 12); | 
 | 220 |     if (month == 2 && is_leap(year)) | 
 | 221 |         return 29; | 
 | 222 |     else | 
 | 223 |         return _days_in_month[month]; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 224 | } | 
 | 225 |  | 
| Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 226 | /* year, month -> number of days in year preceding first day of month */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 227 | static int | 
 | 228 | days_before_month(int year, int month) | 
 | 229 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 |     int days; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 231 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 |     assert(month >= 1); | 
 | 233 |     assert(month <= 12); | 
 | 234 |     days = _days_before_month[month]; | 
 | 235 |     if (month > 2 && is_leap(year)) | 
 | 236 |         ++days; | 
 | 237 |     return days; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 238 | } | 
 | 239 |  | 
 | 240 | /* year -> number of days before January 1st of year.  Remember that we | 
 | 241 |  * start with year 1, so days_before_year(1) == 0. | 
 | 242 |  */ | 
 | 243 | static int | 
 | 244 | days_before_year(int year) | 
 | 245 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 |     int y = year - 1; | 
 | 247 |     /* This is incorrect if year <= 0; we really want the floor | 
 | 248 |      * here.  But so long as MINYEAR is 1, the smallest year this | 
| Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 249 |      * can see is 1. | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 |      */ | 
| Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 251 |     assert (year >= 1); | 
 | 252 |     return y*365 + y/4 - y/100 + y/400; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 253 | } | 
 | 254 |  | 
 | 255 | /* Number of days in 4, 100, and 400 year cycles.  That these have | 
 | 256 |  * the correct values is asserted in the module init function. | 
 | 257 |  */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | #define DI4Y    1461    /* days_before_year(5); days in 4 years */ | 
 | 259 | #define DI100Y  36524   /* days_before_year(101); days in 100 years */ | 
 | 260 | #define DI400Y  146097  /* days_before_year(401); days in 400 years  */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 261 |  | 
 | 262 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ | 
 | 263 | static void | 
 | 264 | ord_to_ymd(int ordinal, int *year, int *month, int *day) | 
 | 265 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 |     int n, n1, n4, n100, n400, leapyear, preceding; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 267 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 |     /* ordinal is a 1-based index, starting at 1-Jan-1.  The pattern of | 
 | 269 |      * leap years repeats exactly every 400 years.  The basic strategy is | 
 | 270 |      * to find the closest 400-year boundary at or before ordinal, then | 
 | 271 |      * work with the offset from that boundary to ordinal.  Life is much | 
 | 272 |      * clearer if we subtract 1 from ordinal first -- then the values | 
 | 273 |      * of ordinal at 400-year boundaries are exactly those divisible | 
 | 274 |      * by DI400Y: | 
 | 275 |      * | 
 | 276 |      *    D  M   Y            n              n-1 | 
 | 277 |      *    -- --- ----        ----------     ---------------- | 
 | 278 |      *    31 Dec -400        -DI400Y       -DI400Y -1 | 
 | 279 |      *     1 Jan -399         -DI400Y +1   -DI400Y      400-year boundary | 
 | 280 |      *    ... | 
 | 281 |      *    30 Dec  000        -1             -2 | 
 | 282 |      *    31 Dec  000         0             -1 | 
 | 283 |      *     1 Jan  001         1              0          400-year boundary | 
 | 284 |      *     2 Jan  001         2              1 | 
 | 285 |      *     3 Jan  001         3              2 | 
 | 286 |      *    ... | 
 | 287 |      *    31 Dec  400         DI400Y        DI400Y -1 | 
 | 288 |      *     1 Jan  401         DI400Y +1     DI400Y      400-year boundary | 
 | 289 |      */ | 
 | 290 |     assert(ordinal >= 1); | 
 | 291 |     --ordinal; | 
 | 292 |     n400 = ordinal / DI400Y; | 
 | 293 |     n = ordinal % DI400Y; | 
 | 294 |     *year = n400 * 400 + 1; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 295 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 |     /* Now n is the (non-negative) offset, in days, from January 1 of | 
 | 297 |      * year, to the desired date.  Now compute how many 100-year cycles | 
 | 298 |      * precede n. | 
 | 299 |      * Note that it's possible for n100 to equal 4!  In that case 4 full | 
 | 300 |      * 100-year cycles precede the desired day, which implies the | 
 | 301 |      * desired day is December 31 at the end of a 400-year cycle. | 
 | 302 |      */ | 
 | 303 |     n100 = n / DI100Y; | 
 | 304 |     n = n % DI100Y; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 305 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 |     /* Now compute how many 4-year cycles precede it. */ | 
 | 307 |     n4 = n / DI4Y; | 
 | 308 |     n = n % DI4Y; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 309 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 |     /* And now how many single years.  Again n1 can be 4, and again | 
 | 311 |      * meaning that the desired day is December 31 at the end of the | 
 | 312 |      * 4-year cycle. | 
 | 313 |      */ | 
 | 314 |     n1 = n / 365; | 
 | 315 |     n = n % 365; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 316 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 |     *year += n100 * 100 + n4 * 4 + n1; | 
 | 318 |     if (n1 == 4 || n100 == 4) { | 
 | 319 |         assert(n == 0); | 
 | 320 |         *year -= 1; | 
 | 321 |         *month = 12; | 
 | 322 |         *day = 31; | 
 | 323 |         return; | 
 | 324 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 325 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 |     /* Now the year is correct, and n is the offset from January 1.  We | 
 | 327 |      * find the month via an estimate that's either exact or one too | 
 | 328 |      * large. | 
 | 329 |      */ | 
 | 330 |     leapyear = n1 == 3 && (n4 != 24 || n100 == 3); | 
 | 331 |     assert(leapyear == is_leap(*year)); | 
 | 332 |     *month = (n + 50) >> 5; | 
 | 333 |     preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); | 
 | 334 |     if (preceding > n) { | 
 | 335 |         /* estimate is too large */ | 
 | 336 |         *month -= 1; | 
 | 337 |         preceding -= days_in_month(*year, *month); | 
 | 338 |     } | 
 | 339 |     n -= preceding; | 
 | 340 |     assert(0 <= n); | 
 | 341 |     assert(n < days_in_month(*year, *month)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 342 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 |     *day = n + 1; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 344 | } | 
 | 345 |  | 
 | 346 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ | 
 | 347 | static int | 
 | 348 | ymd_to_ord(int year, int month, int day) | 
 | 349 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 |     return days_before_year(year) + days_before_month(year, month) + day; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 351 | } | 
 | 352 |  | 
 | 353 | /* Day of week, where Monday==0, ..., Sunday==6.  1/1/1 was a Monday. */ | 
 | 354 | static int | 
 | 355 | weekday(int year, int month, int day) | 
 | 356 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 |     return (ymd_to_ord(year, month, day) + 6) % 7; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 358 | } | 
 | 359 |  | 
 | 360 | /* Ordinal of the Monday starting week 1 of the ISO year.  Week 1 is the | 
 | 361 |  * first calendar week containing a Thursday. | 
 | 362 |  */ | 
 | 363 | static int | 
 | 364 | iso_week1_monday(int year) | 
 | 365 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 |     int first_day = ymd_to_ord(year, 1, 1);     /* ord of 1/1 */ | 
 | 367 |     /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ | 
 | 368 |     int first_weekday = (first_day + 6) % 7; | 
 | 369 |     /* ordinal of closest Monday at or before 1/1 */ | 
 | 370 |     int week1_monday  = first_day - first_weekday; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 371 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 |     if (first_weekday > 3)      /* if 1/1 was Fri, Sat, Sun */ | 
 | 373 |         week1_monday += 7; | 
 | 374 |     return week1_monday; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 375 | } | 
 | 376 |  | 
 | 377 | /* --------------------------------------------------------------------------- | 
 | 378 |  * Range checkers. | 
 | 379 |  */ | 
 | 380 |  | 
 | 381 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS.  If so, return 0. | 
 | 382 |  * If not, raise OverflowError and return -1. | 
 | 383 |  */ | 
 | 384 | static int | 
 | 385 | check_delta_day_range(int days) | 
 | 386 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 |     if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) | 
 | 388 |         return 0; | 
 | 389 |     PyErr_Format(PyExc_OverflowError, | 
 | 390 |                  "days=%d; must have magnitude <= %d", | 
 | 391 |                  days, MAX_DELTA_DAYS); | 
 | 392 |     return -1; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 393 | } | 
 | 394 |  | 
 | 395 | /* Check that date arguments are in range.  Return 0 if they are.  If they | 
 | 396 |  * aren't, raise ValueError and return -1. | 
 | 397 |  */ | 
 | 398 | static int | 
 | 399 | check_date_args(int year, int month, int day) | 
 | 400 | { | 
 | 401 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 |     if (year < MINYEAR || year > MAXYEAR) { | 
 | 403 |         PyErr_SetString(PyExc_ValueError, | 
 | 404 |                         "year is out of range"); | 
 | 405 |         return -1; | 
 | 406 |     } | 
 | 407 |     if (month < 1 || month > 12) { | 
 | 408 |         PyErr_SetString(PyExc_ValueError, | 
 | 409 |                         "month must be in 1..12"); | 
 | 410 |         return -1; | 
 | 411 |     } | 
 | 412 |     if (day < 1 || day > days_in_month(year, month)) { | 
 | 413 |         PyErr_SetString(PyExc_ValueError, | 
 | 414 |                         "day is out of range for month"); | 
 | 415 |         return -1; | 
 | 416 |     } | 
 | 417 |     return 0; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 418 | } | 
 | 419 |  | 
 | 420 | /* Check that time arguments are in range.  Return 0 if they are.  If they | 
 | 421 |  * aren't, raise ValueError and return -1. | 
 | 422 |  */ | 
 | 423 | static int | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 424 | check_time_args(int h, int m, int s, int us, int fold) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 425 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 |     if (h < 0 || h > 23) { | 
 | 427 |         PyErr_SetString(PyExc_ValueError, | 
 | 428 |                         "hour must be in 0..23"); | 
 | 429 |         return -1; | 
 | 430 |     } | 
 | 431 |     if (m < 0 || m > 59) { | 
 | 432 |         PyErr_SetString(PyExc_ValueError, | 
 | 433 |                         "minute must be in 0..59"); | 
 | 434 |         return -1; | 
 | 435 |     } | 
 | 436 |     if (s < 0 || s > 59) { | 
 | 437 |         PyErr_SetString(PyExc_ValueError, | 
 | 438 |                         "second must be in 0..59"); | 
 | 439 |         return -1; | 
 | 440 |     } | 
 | 441 |     if (us < 0 || us > 999999) { | 
 | 442 |         PyErr_SetString(PyExc_ValueError, | 
 | 443 |                         "microsecond must be in 0..999999"); | 
 | 444 |         return -1; | 
 | 445 |     } | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 446 |     if (fold != 0 && fold != 1) { | 
 | 447 |         PyErr_SetString(PyExc_ValueError, | 
 | 448 |                         "fold must be either 0 or 1"); | 
 | 449 |         return -1; | 
 | 450 |     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 |     return 0; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 452 | } | 
 | 453 |  | 
 | 454 | /* --------------------------------------------------------------------------- | 
 | 455 |  * Normalization utilities. | 
 | 456 |  */ | 
 | 457 |  | 
 | 458 | /* One step of a mixed-radix conversion.  A "hi" unit is equivalent to | 
 | 459 |  * factor "lo" units.  factor must be > 0.  If *lo is less than 0, or | 
 | 460 |  * at least factor, enough of *lo is converted into "hi" units so that | 
 | 461 |  * 0 <= *lo < factor.  The input values must be such that int overflow | 
 | 462 |  * is impossible. | 
 | 463 |  */ | 
 | 464 | static void | 
 | 465 | normalize_pair(int *hi, int *lo, int factor) | 
 | 466 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 |     assert(factor > 0); | 
 | 468 |     assert(lo != hi); | 
 | 469 |     if (*lo < 0 || *lo >= factor) { | 
 | 470 |         const int num_hi = divmod(*lo, factor, lo); | 
 | 471 |         const int new_hi = *hi + num_hi; | 
 | 472 |         assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); | 
 | 473 |         *hi = new_hi; | 
 | 474 |     } | 
 | 475 |     assert(0 <= *lo && *lo < factor); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 476 | } | 
 | 477 |  | 
 | 478 | /* Fiddle days (d), seconds (s), and microseconds (us) so that | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 |  *      0 <= *s < 24*3600 | 
 | 480 |  *      0 <= *us < 1000000 | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 481 |  * The input values must be such that the internals don't overflow. | 
 | 482 |  * The way this routine is used, we don't get close. | 
 | 483 |  */ | 
 | 484 | static void | 
 | 485 | normalize_d_s_us(int *d, int *s, int *us) | 
 | 486 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 |     if (*us < 0 || *us >= 1000000) { | 
 | 488 |         normalize_pair(s, us, 1000000); | 
 | 489 |         /* |s| can't be bigger than about | 
 | 490 |          * |original s| + |original us|/1000000 now. | 
 | 491 |          */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 492 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 |     } | 
 | 494 |     if (*s < 0 || *s >= 24*3600) { | 
 | 495 |         normalize_pair(d, s, 24*3600); | 
 | 496 |         /* |d| can't be bigger than about | 
 | 497 |          * |original d| + | 
 | 498 |          * (|original s| + |original us|/1000000) / (24*3600) now. | 
 | 499 |          */ | 
 | 500 |     } | 
 | 501 |     assert(0 <= *s && *s < 24*3600); | 
 | 502 |     assert(0 <= *us && *us < 1000000); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 503 | } | 
 | 504 |  | 
 | 505 | /* Fiddle years (y), months (m), and days (d) so that | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 |  *      1 <= *m <= 12 | 
 | 507 |  *      1 <= *d <= days_in_month(*y, *m) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 508 |  * The input values must be such that the internals don't overflow. | 
 | 509 |  * The way this routine is used, we don't get close. | 
 | 510 |  */ | 
| Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 511 | static int | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 512 | normalize_y_m_d(int *y, int *m, int *d) | 
 | 513 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 |     int dim;            /* # of days in month */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 515 |  | 
| Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 516 |     /* In actual use, m is always the month component extracted from a | 
 | 517 |      * date/datetime object.  Therefore it is always in [1, 12] range. | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 |      */ | 
| Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 519 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 |     assert(1 <= *m && *m <= 12); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 521 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 |     /* Now only day can be out of bounds (year may also be out of bounds | 
 | 523 |      * for a datetime object, but we don't care about that here). | 
 | 524 |      * If day is out of bounds, what to do is arguable, but at least the | 
 | 525 |      * method here is principled and explainable. | 
 | 526 |      */ | 
 | 527 |     dim = days_in_month(*y, *m); | 
 | 528 |     if (*d < 1 || *d > dim) { | 
 | 529 |         /* Move day-1 days from the first of the month.  First try to | 
 | 530 |          * get off cheap if we're only one day out of range | 
 | 531 |          * (adjustments for timezone alone can't be worse than that). | 
 | 532 |          */ | 
 | 533 |         if (*d == 0) { | 
 | 534 |             --*m; | 
 | 535 |             if (*m > 0) | 
 | 536 |                 *d = days_in_month(*y, *m); | 
 | 537 |             else { | 
 | 538 |                 --*y; | 
 | 539 |                 *m = 12; | 
 | 540 |                 *d = 31; | 
 | 541 |             } | 
 | 542 |         } | 
 | 543 |         else if (*d == dim + 1) { | 
 | 544 |             /* move forward a day */ | 
 | 545 |             ++*m; | 
 | 546 |             *d = 1; | 
 | 547 |             if (*m > 12) { | 
 | 548 |                 *m = 1; | 
 | 549 |                 ++*y; | 
 | 550 |             } | 
 | 551 |         } | 
 | 552 |         else { | 
 | 553 |             int ordinal = ymd_to_ord(*y, *m, 1) + | 
 | 554 |                                       *d - 1; | 
| Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 555 |             if (ordinal < 1 || ordinal > MAXORDINAL) { | 
 | 556 |                 goto error; | 
 | 557 |             } else { | 
 | 558 |                 ord_to_ymd(ordinal, y, m, d); | 
 | 559 |                 return 0; | 
 | 560 |             } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 |         } | 
 | 562 |     } | 
 | 563 |     assert(*m > 0); | 
 | 564 |     assert(*d > 0); | 
| Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 565 |     if (MINYEAR <= *y && *y <= MAXYEAR) | 
 | 566 |         return 0; | 
 | 567 |  error: | 
 | 568 |     PyErr_SetString(PyExc_OverflowError, | 
 | 569 |             "date value out of range"); | 
 | 570 |     return -1; | 
 | 571 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 572 | } | 
 | 573 |  | 
 | 574 | /* Fiddle out-of-bounds months and days so that the result makes some kind | 
 | 575 |  * of sense.  The parameters are both inputs and outputs.  Returns < 0 on | 
 | 576 |  * failure, where failure means the adjusted year is out of bounds. | 
 | 577 |  */ | 
 | 578 | static int | 
 | 579 | normalize_date(int *year, int *month, int *day) | 
 | 580 | { | 
| Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 581 |     return normalize_y_m_d(year, month, day); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 582 | } | 
 | 583 |  | 
 | 584 | /* Force all the datetime fields into range.  The parameters are both | 
 | 585 |  * inputs and outputs.  Returns < 0 on error. | 
 | 586 |  */ | 
 | 587 | static int | 
 | 588 | normalize_datetime(int *year, int *month, int *day, | 
 | 589 |                    int *hour, int *minute, int *second, | 
 | 590 |                    int *microsecond) | 
 | 591 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 |     normalize_pair(second, microsecond, 1000000); | 
 | 593 |     normalize_pair(minute, second, 60); | 
 | 594 |     normalize_pair(hour, minute, 60); | 
 | 595 |     normalize_pair(day, hour, 24); | 
 | 596 |     return normalize_date(year, month, day); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 597 | } | 
 | 598 |  | 
 | 599 | /* --------------------------------------------------------------------------- | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 600 |  * Basic object allocation:  tp_alloc implementations.  These allocate | 
 | 601 |  * Python objects of the right size and type, and do the Python object- | 
 | 602 |  * initialization bit.  If there's not enough memory, they return NULL after | 
 | 603 |  * setting MemoryError.  All data members remain uninitialized trash. | 
 | 604 |  * | 
 | 605 |  * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo | 
| Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 606 |  * member is needed.  This is ugly, imprecise, and possibly insecure. | 
 | 607 |  * tp_basicsize for the time and datetime types is set to the size of the | 
 | 608 |  * struct that has room for the tzinfo member, so subclasses in Python will | 
 | 609 |  * allocate enough space for a tzinfo member whether or not one is actually | 
 | 610 |  * needed.  That's the "ugly and imprecise" parts.  The "possibly insecure" | 
 | 611 |  * part is that PyType_GenericAlloc() (which subclasses in Python end up | 
 | 612 |  * using) just happens today to effectively ignore the nitems argument | 
 | 613 |  * when tp_itemsize is 0, which it is for these type objects.  If that | 
 | 614 |  * changes, perhaps the callers of tp_alloc slots in this file should | 
 | 615 |  * be changed to force a 0 nitems argument unless the type being allocated | 
 | 616 |  * is a base type implemented in this file (so that tp_alloc is time_alloc | 
 | 617 |  * or datetime_alloc below, which know about the nitems abuse). | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 618 |  */ | 
 | 619 |  | 
 | 620 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 621 | time_alloc(PyTypeObject *type, Py_ssize_t aware) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 622 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 |     PyObject *self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 624 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 |     self = (PyObject *) | 
 | 626 |         PyObject_MALLOC(aware ? | 
 | 627 |                         sizeof(PyDateTime_Time) : | 
 | 628 |                 sizeof(_PyDateTime_BaseTime)); | 
 | 629 |     if (self == NULL) | 
 | 630 |         return (PyObject *)PyErr_NoMemory(); | 
| Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 631 |     (void)PyObject_INIT(self, type); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 |     return self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 633 | } | 
 | 634 |  | 
 | 635 | static PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 636 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 637 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 |     PyObject *self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 639 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 |     self = (PyObject *) | 
 | 641 |         PyObject_MALLOC(aware ? | 
 | 642 |                         sizeof(PyDateTime_DateTime) : | 
 | 643 |                 sizeof(_PyDateTime_BaseDateTime)); | 
 | 644 |     if (self == NULL) | 
 | 645 |         return (PyObject *)PyErr_NoMemory(); | 
| Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 646 |     (void)PyObject_INIT(self, type); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 |     return self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 648 | } | 
 | 649 |  | 
 | 650 | /* --------------------------------------------------------------------------- | 
 | 651 |  * Helpers for setting object fields.  These work on pointers to the | 
 | 652 |  * appropriate base class. | 
 | 653 |  */ | 
 | 654 |  | 
 | 655 | /* For date and datetime. */ | 
 | 656 | static void | 
 | 657 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) | 
 | 658 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 |     self->hashcode = -1; | 
 | 660 |     SET_YEAR(self, y); | 
 | 661 |     SET_MONTH(self, m); | 
 | 662 |     SET_DAY(self, d); | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 663 | } | 
 | 664 |  | 
 | 665 | /* --------------------------------------------------------------------------- | 
 | 666 |  * Create various objects, mostly without range checking. | 
 | 667 |  */ | 
 | 668 |  | 
 | 669 | /* Create a date instance with no range checking. */ | 
 | 670 | static PyObject * | 
 | 671 | new_date_ex(int year, int month, int day, PyTypeObject *type) | 
 | 672 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 |     PyDateTime_Date *self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 674 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 |     self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); | 
 | 676 |     if (self != NULL) | 
 | 677 |         set_date_fields(self, year, month, day); | 
 | 678 |     return (PyObject *) self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 679 | } | 
 | 680 |  | 
 | 681 | #define new_date(year, month, day) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 |     new_date_ex(year, month, day, &PyDateTime_DateType) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 683 |  | 
 | 684 | /* Create a datetime instance with no range checking. */ | 
 | 685 | static PyObject * | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 686 | new_datetime_ex2(int year, int month, int day, int hour, int minute, | 
 | 687 |                  int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 688 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 |     PyDateTime_DateTime *self; | 
 | 690 |     char aware = tzinfo != Py_None; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 691 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 |     self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); | 
 | 693 |     if (self != NULL) { | 
 | 694 |         self->hastzinfo = aware; | 
 | 695 |         set_date_fields((PyDateTime_Date *)self, year, month, day); | 
 | 696 |         DATE_SET_HOUR(self, hour); | 
 | 697 |         DATE_SET_MINUTE(self, minute); | 
 | 698 |         DATE_SET_SECOND(self, second); | 
 | 699 |         DATE_SET_MICROSECOND(self, usecond); | 
 | 700 |         if (aware) { | 
 | 701 |             Py_INCREF(tzinfo); | 
 | 702 |             self->tzinfo = tzinfo; | 
 | 703 |         } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 704 |         DATE_SET_FOLD(self, fold); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 |     } | 
 | 706 |     return (PyObject *)self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 707 | } | 
 | 708 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 709 | static PyObject * | 
 | 710 | new_datetime_ex(int year, int month, int day, int hour, int minute, | 
 | 711 |                 int second, int usecond, PyObject *tzinfo, PyTypeObject *type) | 
 | 712 | { | 
 | 713 |     return new_datetime_ex2(year, month, day, hour, minute, second, usecond, | 
 | 714 |                             tzinfo, 0, type); | 
 | 715 | } | 
 | 716 |  | 
 | 717 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ | 
 | 718 |     new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 |                     &PyDateTime_DateTimeType) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 720 |  | 
 | 721 | /* Create a time instance with no range checking. */ | 
 | 722 | static PyObject * | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 723 | new_time_ex2(int hour, int minute, int second, int usecond, | 
 | 724 |              PyObject *tzinfo, int fold, PyTypeObject *type) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 725 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 |     PyDateTime_Time *self; | 
 | 727 |     char aware = tzinfo != Py_None; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 728 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 |     self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); | 
 | 730 |     if (self != NULL) { | 
 | 731 |         self->hastzinfo = aware; | 
 | 732 |         self->hashcode = -1; | 
 | 733 |         TIME_SET_HOUR(self, hour); | 
 | 734 |         TIME_SET_MINUTE(self, minute); | 
 | 735 |         TIME_SET_SECOND(self, second); | 
 | 736 |         TIME_SET_MICROSECOND(self, usecond); | 
 | 737 |         if (aware) { | 
 | 738 |             Py_INCREF(tzinfo); | 
 | 739 |             self->tzinfo = tzinfo; | 
 | 740 |         } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 741 |         TIME_SET_FOLD(self, fold); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 |     } | 
 | 743 |     return (PyObject *)self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 744 | } | 
 | 745 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 746 | static PyObject * | 
 | 747 | new_time_ex(int hour, int minute, int second, int usecond, | 
 | 748 |             PyObject *tzinfo, PyTypeObject *type) | 
 | 749 | { | 
 | 750 |     return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); | 
 | 751 | } | 
 | 752 |  | 
 | 753 | #define new_time(hh, mm, ss, us, tzinfo, fold)                       \ | 
 | 754 |     new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 755 |  | 
 | 756 | /* Create a timedelta instance.  Normalize the members iff normalize is | 
 | 757 |  * true.  Passing false is a speed optimization, if you know for sure | 
 | 758 |  * that seconds and microseconds are already in their proper ranges.  In any | 
 | 759 |  * case, raises OverflowError and returns NULL if the normalized days is out | 
 | 760 |  * of range). | 
 | 761 |  */ | 
 | 762 | static PyObject * | 
 | 763 | new_delta_ex(int days, int seconds, int microseconds, int normalize, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 |              PyTypeObject *type) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 765 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 |     PyDateTime_Delta *self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 767 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 |     if (normalize) | 
 | 769 |         normalize_d_s_us(&days, &seconds, µseconds); | 
 | 770 |     assert(0 <= seconds && seconds < 24*3600); | 
 | 771 |     assert(0 <= microseconds && microseconds < 1000000); | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 772 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 |     if (check_delta_day_range(days) < 0) | 
 | 774 |         return NULL; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 775 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 |     self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); | 
 | 777 |     if (self != NULL) { | 
 | 778 |         self->hashcode = -1; | 
 | 779 |         SET_TD_DAYS(self, days); | 
 | 780 |         SET_TD_SECONDS(self, seconds); | 
 | 781 |         SET_TD_MICROSECONDS(self, microseconds); | 
 | 782 |     } | 
 | 783 |     return (PyObject *) self; | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 784 | } | 
 | 785 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | #define new_delta(d, s, us, normalize)  \ | 
 | 787 |     new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 788 |  | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 789 |  | 
 | 790 | typedef struct | 
 | 791 | { | 
 | 792 |     PyObject_HEAD | 
 | 793 |     PyObject *offset; | 
 | 794 |     PyObject *name; | 
 | 795 | } PyDateTime_TimeZone; | 
 | 796 |  | 
| Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 797 | /* The interned UTC timezone instance */ | 
| Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 798 | static PyObject *PyDateTime_TimeZone_UTC; | 
| Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 799 | /* The interned Epoch datetime instance */ | 
 | 800 | static PyObject *PyDateTime_Epoch; | 
| Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 801 |  | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 802 | /* Create new timezone instance checking offset range.  This | 
 | 803 |    function does not check the name argument.  Caller must assure | 
 | 804 |    that offset is a timedelta instance and name is either NULL | 
 | 805 |    or a unicode object. */ | 
 | 806 | static PyObject * | 
| Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 807 | create_timezone(PyObject *offset, PyObject *name) | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 808 | { | 
 | 809 |     PyDateTime_TimeZone *self; | 
 | 810 |     PyTypeObject *type = &PyDateTime_TimeZoneType; | 
 | 811 |  | 
 | 812 |     assert(offset != NULL); | 
 | 813 |     assert(PyDelta_Check(offset)); | 
 | 814 |     assert(name == NULL || PyUnicode_Check(name)); | 
 | 815 |  | 
| Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 816 |     self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); | 
 | 817 |     if (self == NULL) { | 
 | 818 |         return NULL; | 
 | 819 |     } | 
 | 820 |     Py_INCREF(offset); | 
 | 821 |     self->offset = offset; | 
 | 822 |     Py_XINCREF(name); | 
 | 823 |     self->name = name; | 
 | 824 |     return (PyObject *)self; | 
 | 825 | } | 
 | 826 |  | 
 | 827 | static int delta_bool(PyDateTime_Delta *self); | 
 | 828 |  | 
 | 829 | static PyObject * | 
 | 830 | new_timezone(PyObject *offset, PyObject *name) | 
 | 831 | { | 
 | 832 |     assert(offset != NULL); | 
 | 833 |     assert(PyDelta_Check(offset)); | 
 | 834 |     assert(name == NULL || PyUnicode_Check(name)); | 
 | 835 |  | 
 | 836 |     if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { | 
 | 837 |         Py_INCREF(PyDateTime_TimeZone_UTC); | 
 | 838 |         return PyDateTime_TimeZone_UTC; | 
 | 839 |     } | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 840 |     if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { | 
 | 841 |         PyErr_Format(PyExc_ValueError, "offset must be a timedelta" | 
| Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 842 |                      " representing a whole number of minutes," | 
 | 843 |                      " not %R.", offset); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 844 |         return NULL; | 
 | 845 |     } | 
 | 846 |     if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || | 
 | 847 |         GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { | 
 | 848 |         PyErr_Format(PyExc_ValueError, "offset must be a timedelta" | 
 | 849 |                      " strictly between -timedelta(hours=24) and" | 
| Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 850 |                      " timedelta(hours=24)," | 
 | 851 |                      " not %R.", offset); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 852 |         return NULL; | 
 | 853 |     } | 
 | 854 |  | 
| Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 855 |     return create_timezone(offset, name); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 856 | } | 
 | 857 |  | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 858 | /* --------------------------------------------------------------------------- | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 859 |  * tzinfo helpers. | 
 | 860 |  */ | 
 | 861 |  | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 862 | /* Ensure that p is None or of a tzinfo subclass.  Return 0 if OK; if not | 
 | 863 |  * raise TypeError and return -1. | 
 | 864 |  */ | 
 | 865 | static int | 
 | 866 | check_tzinfo_subclass(PyObject *p) | 
 | 867 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 |     if (p == Py_None || PyTZInfo_Check(p)) | 
 | 869 |         return 0; | 
 | 870 |     PyErr_Format(PyExc_TypeError, | 
 | 871 |                  "tzinfo argument must be None or of a tzinfo subclass, " | 
 | 872 |                  "not type '%s'", | 
 | 873 |                  Py_TYPE(p)->tp_name); | 
 | 874 |     return -1; | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 875 | } | 
 | 876 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 877 | /* If self has a tzinfo member, return a BORROWED reference to it.  Else | 
 | 878 |  * return NULL, which is NOT AN ERROR.  There are no error returns here, | 
 | 879 |  * and the caller must not decref the result. | 
 | 880 |  */ | 
 | 881 | static PyObject * | 
 | 882 | get_tzinfo_member(PyObject *self) | 
 | 883 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 |     PyObject *tzinfo = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 885 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 |     if (PyDateTime_Check(self) && HASTZINFO(self)) | 
 | 887 |         tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; | 
 | 888 |     else if (PyTime_Check(self) && HASTZINFO(self)) | 
 | 889 |         tzinfo = ((PyDateTime_Time *)self)->tzinfo; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 890 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 |     return tzinfo; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 892 | } | 
 | 893 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 894 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result.  tzinfo must | 
 | 895 |  * be an instance of the tzinfo class.  If the method returns None, this | 
 | 896 |  * returns None.  If the method doesn't return None or timedelta, TypeError is | 
 | 897 |  * raised and this returns NULL.  If it returns a timedelta and the value is | 
 | 898 |  * out of range or isn't a whole number of minutes, ValueError is raised and | 
 | 899 |  * this returns NULL.  Else result is returned. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 900 |  */ | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 901 | static PyObject * | 
| Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 902 | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 903 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 904 |     PyObject *offset; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 905 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 |     assert(tzinfo != NULL); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 907 |     assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 |     assert(tzinfoarg != NULL); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 909 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 910 |     if (tzinfo == Py_None) | 
 | 911 |         Py_RETURN_NONE; | 
 | 912 |     offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); | 
 | 913 |     if (offset == Py_None || offset == NULL) | 
 | 914 |         return offset; | 
 | 915 |     if (PyDelta_Check(offset)) { | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 916 |         if (GET_TD_MICROSECONDS(offset) != 0) { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 917 |             Py_DECREF(offset); | 
 | 918 |             PyErr_Format(PyExc_ValueError, "offset must be a timedelta" | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 919 |                          " representing a whole number of seconds"); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 920 |             return NULL; | 
 | 921 |         } | 
 | 922 |         if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || | 
 | 923 |             GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { | 
 | 924 |             Py_DECREF(offset); | 
 | 925 |             PyErr_Format(PyExc_ValueError, "offset must be a timedelta" | 
 | 926 |                          " strictly between -timedelta(hours=24) and" | 
 | 927 |                          " timedelta(hours=24)."); | 
 | 928 |             return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 |         } | 
 | 930 |     } | 
 | 931 |     else { | 
 | 932 |         PyErr_Format(PyExc_TypeError, | 
 | 933 |                      "tzinfo.%s() must return None or " | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 934 |                      "timedelta, not '%.200s'", | 
 | 935 |                      name, Py_TYPE(offset)->tp_name); | 
| Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 936 |         Py_DECREF(offset); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 937 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 939 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 940 |     return offset; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 941 | } | 
 | 942 |  | 
 | 943 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the | 
 | 944 |  * result.  tzinfo must be an instance of the tzinfo class.  If utcoffset() | 
 | 945 |  * returns None, call_utcoffset returns 0 and sets *none to 1.  If uctoffset() | 
| Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 946 |  * doesn't return None or timedelta, TypeError is raised and this returns -1. | 
 | 947 |  * If utcoffset() returns an invalid timedelta (out of range, or not a whole | 
 | 948 |  * # of minutes), ValueError is raised and this returns -1.  Else *none is | 
 | 949 |  * set to 0 and the offset is returned (as int # of minutes east of UTC). | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 950 |  */ | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 951 | static PyObject * | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 952 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) | 
 | 953 | { | 
 | 954 |     return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 955 | } | 
 | 956 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 957 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the | 
 | 958 |  * result.  tzinfo must be an instance of the tzinfo class.  If dst() | 
 | 959 |  * returns None, call_dst returns 0 and sets *none to 1.  If dst() | 
| Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 960 |  & doesn't return None or timedelta, TypeError is raised and this | 
| Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 961 |  * returns -1.  If dst() returns an invalid timedelta for a UTC offset, | 
| Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 962 |  * ValueError is raised and this returns -1.  Else *none is set to 0 and | 
 | 963 |  * the offset is returned (as an int # of minutes east of UTC). | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 964 |  */ | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 965 | static PyObject * | 
 | 966 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 967 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 968 |     return call_tzinfo_method(tzinfo, "dst", tzinfoarg); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 969 | } | 
 | 970 |  | 
| Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 971 | /* Call tzinfo.tzname(tzinfoarg), and return the result.  tzinfo must be | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 972 |  * an instance of the tzinfo class or None.  If tzinfo isn't None, and | 
| Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 973 |  * tzname() doesn't return None or a string, TypeError is raised and this | 
| Guido van Rossum | e3d1d41 | 2007-05-23 21:24:35 +0000 | [diff] [blame] | 974 |  * returns NULL.  If the result is a string, we ensure it is a Unicode | 
 | 975 |  * string. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 976 |  */ | 
 | 977 | static PyObject * | 
| Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 978 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 979 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 |     PyObject *result; | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 981 |     _Py_IDENTIFIER(tzname); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 982 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 |     assert(tzinfo != NULL); | 
 | 984 |     assert(check_tzinfo_subclass(tzinfo) >= 0); | 
 | 985 |     assert(tzinfoarg != NULL); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 986 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 |     if (tzinfo == Py_None) | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 988 |         Py_RETURN_NONE; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 989 |  | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 990 |     result = _PyObject_CallMethodId(tzinfo, &PyId_tzname, "O", tzinfoarg); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 991 |  | 
 | 992 |     if (result == NULL || result == Py_None) | 
 | 993 |         return result; | 
 | 994 |  | 
 | 995 |     if (!PyUnicode_Check(result)) { | 
 | 996 |         PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " | 
 | 997 |                      "return None or a string, not '%s'", | 
 | 998 |                      Py_TYPE(result)->tp_name); | 
 | 999 |         Py_DECREF(result); | 
 | 1000 |         result = NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1002 |  | 
 | 1003 |     return result; | 
| Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 1004 | } | 
 | 1005 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1006 | /* repr is like "someclass(arg1, arg2)".  If tzinfo isn't None, | 
 | 1007 |  * stuff | 
 | 1008 |  *     ", tzinfo=" + repr(tzinfo) | 
 | 1009 |  * before the closing ")". | 
 | 1010 |  */ | 
 | 1011 | static PyObject * | 
 | 1012 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) | 
 | 1013 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 |     PyObject *temp; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1015 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 |     assert(PyUnicode_Check(repr)); | 
 | 1017 |     assert(tzinfo); | 
 | 1018 |     if (tzinfo == Py_None) | 
 | 1019 |         return repr; | 
 | 1020 |     /* Get rid of the trailing ')'. */ | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1021 |     assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); | 
 | 1022 |     temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 |     Py_DECREF(repr); | 
 | 1024 |     if (temp == NULL) | 
 | 1025 |         return NULL; | 
 | 1026 |     repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); | 
 | 1027 |     Py_DECREF(temp); | 
 | 1028 |     return repr; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1029 | } | 
 | 1030 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1031 | /* repr is like "someclass(arg1, arg2)".  If fold isn't 0, | 
 | 1032 |  * stuff | 
 | 1033 |  *     ", fold=" + repr(tzinfo) | 
 | 1034 |  * before the closing ")". | 
 | 1035 |  */ | 
 | 1036 | static PyObject * | 
 | 1037 | append_keyword_fold(PyObject *repr, int fold) | 
 | 1038 | { | 
 | 1039 |     PyObject *temp; | 
 | 1040 |  | 
 | 1041 |     assert(PyUnicode_Check(repr)); | 
 | 1042 |     if (fold == 0) | 
 | 1043 |         return repr; | 
 | 1044 |     /* Get rid of the trailing ')'. */ | 
 | 1045 |     assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); | 
 | 1046 |     temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); | 
 | 1047 |     Py_DECREF(repr); | 
 | 1048 |     if (temp == NULL) | 
 | 1049 |         return NULL; | 
 | 1050 |     repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); | 
 | 1051 |     Py_DECREF(temp); | 
 | 1052 |     return repr; | 
 | 1053 | } | 
 | 1054 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1055 | /* --------------------------------------------------------------------------- | 
 | 1056 |  * String format helpers. | 
 | 1057 |  */ | 
 | 1058 |  | 
 | 1059 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1060 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1061 | { | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1062 |     static const char * const DayNames[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 |         "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" | 
 | 1064 |     }; | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1065 |     static const char * const MonthNames[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 |         "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 
 | 1067 |         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | 
 | 1068 |     }; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1069 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 |     int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1071 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 |     return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", | 
 | 1073 |                                 DayNames[wday], MonthNames[GET_MONTH(date)-1], | 
 | 1074 |                                 GET_DAY(date), hours, minutes, seconds, | 
 | 1075 |                                 GET_YEAR(date)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1076 | } | 
 | 1077 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1078 | static PyObject *delta_negative(PyDateTime_Delta *self); | 
 | 1079 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1080 | /* Add an hours & minutes UTC offset string to buf.  buf has no more than | 
 | 1081 |  * buflen bytes remaining.  The UTC offset is gotten by calling | 
 | 1082 |  * tzinfo.uctoffset(tzinfoarg).  If that returns None, \0 is stored into | 
 | 1083 |  * *buf, and that's all.  Else the returned value is checked for sanity (an | 
 | 1084 |  * integer in range), and if that's OK it's converted to an hours & minutes | 
 | 1085 |  * string of the form | 
 | 1086 |  *   sign HH sep MM | 
 | 1087 |  * Returns 0 if everything is OK.  If the return value from utcoffset() is | 
 | 1088 |  * bogus, an appropriate exception is set and -1 is returned. | 
 | 1089 |  */ | 
 | 1090 | static int | 
| Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1091 | format_utcoffset(char *buf, size_t buflen, const char *sep, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 |                 PyObject *tzinfo, PyObject *tzinfoarg) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1093 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1094 |     PyObject *offset; | 
 | 1095 |     int hours, minutes, seconds; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 |     char sign; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1097 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 |     assert(buflen >= 1); | 
| Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1099 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1100 |     offset = call_utcoffset(tzinfo, tzinfoarg); | 
 | 1101 |     if (offset == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 |         return -1; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1103 |     if (offset == Py_None) { | 
 | 1104 |         Py_DECREF(offset); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 |         *buf = '\0'; | 
 | 1106 |         return 0; | 
 | 1107 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1108 |     /* Offset is normalized, so it is negative if days < 0 */ | 
 | 1109 |     if (GET_TD_DAYS(offset) < 0) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 |         sign = '-'; | 
| Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 1111 |         Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1112 |         if (offset == NULL) | 
 | 1113 |             return -1; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1115 |     else { | 
 | 1116 |         sign = '+'; | 
 | 1117 |     } | 
 | 1118 |     /* Offset is not negative here. */ | 
 | 1119 |     seconds = GET_TD_SECONDS(offset); | 
 | 1120 |     Py_DECREF(offset); | 
 | 1121 |     minutes = divmod(seconds, 60, &seconds); | 
 | 1122 |     hours = divmod(minutes, 60, &minutes); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1123 |     if (seconds == 0) | 
 | 1124 |         PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); | 
 | 1125 |     else | 
 | 1126 |         PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, | 
 | 1127 |                       sep, minutes, sep, seconds); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 |     return 0; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1129 | } | 
 | 1130 |  | 
| Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1131 | static PyObject * | 
 | 1132 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) | 
 | 1133 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 |     PyObject *temp; | 
 | 1135 |     PyObject *tzinfo = get_tzinfo_member(object); | 
 | 1136 |     PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1137 |     _Py_IDENTIFIER(replace); | 
| Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1138 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 |     if (Zreplacement == NULL) | 
 | 1140 |         return NULL; | 
 | 1141 |     if (tzinfo == Py_None || tzinfo == NULL) | 
 | 1142 |         return Zreplacement; | 
| Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1143 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 |     assert(tzinfoarg != NULL); | 
 | 1145 |     temp = call_tzname(tzinfo, tzinfoarg); | 
 | 1146 |     if (temp == NULL) | 
 | 1147 |         goto Error; | 
 | 1148 |     if (temp == Py_None) { | 
 | 1149 |         Py_DECREF(temp); | 
 | 1150 |         return Zreplacement; | 
 | 1151 |     } | 
| Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1152 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 |     assert(PyUnicode_Check(temp)); | 
 | 1154 |     /* Since the tzname is getting stuffed into the | 
 | 1155 |      * format, we have to double any % signs so that | 
 | 1156 |      * strftime doesn't treat them as format codes. | 
 | 1157 |      */ | 
 | 1158 |     Py_DECREF(Zreplacement); | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1159 |     Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 |     Py_DECREF(temp); | 
 | 1161 |     if (Zreplacement == NULL) | 
 | 1162 |         return NULL; | 
 | 1163 |     if (!PyUnicode_Check(Zreplacement)) { | 
 | 1164 |         PyErr_SetString(PyExc_TypeError, | 
 | 1165 |                         "tzname.replace() did not return a string"); | 
 | 1166 |         goto Error; | 
 | 1167 |     } | 
 | 1168 |     return Zreplacement; | 
| Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1169 |  | 
 | 1170 |   Error: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 |     Py_DECREF(Zreplacement); | 
 | 1172 |     return NULL; | 
| Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1173 | } | 
 | 1174 |  | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1175 | static PyObject * | 
 | 1176 | make_freplacement(PyObject *object) | 
 | 1177 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 |     char freplacement[64]; | 
 | 1179 |     if (PyTime_Check(object)) | 
 | 1180 |         sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); | 
 | 1181 |     else if (PyDateTime_Check(object)) | 
 | 1182 |         sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); | 
 | 1183 |     else | 
 | 1184 |         sprintf(freplacement, "%06d", 0); | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1185 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 |     return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1187 | } | 
 | 1188 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1189 | /* I sure don't want to reproduce the strftime code from the time module, | 
 | 1190 |  * so this imports the module and calls it.  All the hair is due to | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1191 |  * giving special meanings to the %z, %Z and %f format codes via a | 
 | 1192 |  * preprocessing step on the format string. | 
| Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1193 |  * tzinfoarg is the argument to pass to the object's tzinfo method, if | 
 | 1194 |  * needed. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1195 |  */ | 
 | 1196 | static PyObject * | 
| Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1197 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 |               PyObject *tzinfoarg) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1199 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 |     PyObject *result = NULL;            /* guilty until proved innocent */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1201 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 |     PyObject *zreplacement = NULL;      /* py string, replacement for %z */ | 
 | 1203 |     PyObject *Zreplacement = NULL;      /* py string, replacement for %Z */ | 
 | 1204 |     PyObject *freplacement = NULL;      /* py string, replacement for %f */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1205 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 |     const char *pin;            /* pointer to next char in input format */ | 
 | 1207 |     Py_ssize_t flen;            /* length of input format */ | 
 | 1208 |     char ch;                    /* next char in input format */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1209 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 |     PyObject *newfmt = NULL;            /* py string, the output format */ | 
 | 1211 |     char *pnew;         /* pointer to available byte in output format */ | 
 | 1212 |     size_t totalnew;            /* number bytes total in output format buffer, | 
 | 1213 |                                exclusive of trailing \0 */ | 
 | 1214 |     size_t usednew;     /* number bytes used so far in output format buffer */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1215 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 |     const char *ptoappend;      /* ptr to string to append to output buffer */ | 
 | 1217 |     Py_ssize_t ntoappend;       /* # of bytes to append to output buffer */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1218 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 |     assert(object && format && timetuple); | 
 | 1220 |     assert(PyUnicode_Check(format)); | 
 | 1221 |     /* Convert the input format to a C string and size */ | 
| Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1222 |     pin = PyUnicode_AsUTF8AndSize(format, &flen); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 |     if (!pin) | 
 | 1224 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1225 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 |     /* Scan the input format, looking for %z/%Z/%f escapes, building | 
 | 1227 |      * a new format.  Since computing the replacements for those codes | 
 | 1228 |      * is expensive, don't unless they're actually used. | 
 | 1229 |      */ | 
 | 1230 |     if (flen > INT_MAX - 1) { | 
 | 1231 |         PyErr_NoMemory(); | 
 | 1232 |         goto Done; | 
 | 1233 |     } | 
| Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1234 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 |     totalnew = flen + 1;        /* realistic if no %z/%Z */ | 
 | 1236 |     newfmt = PyBytes_FromStringAndSize(NULL, totalnew); | 
 | 1237 |     if (newfmt == NULL) goto Done; | 
 | 1238 |     pnew = PyBytes_AsString(newfmt); | 
 | 1239 |     usednew = 0; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1240 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 |     while ((ch = *pin++) != '\0') { | 
 | 1242 |         if (ch != '%') { | 
 | 1243 |             ptoappend = pin - 1; | 
 | 1244 |             ntoappend = 1; | 
 | 1245 |         } | 
 | 1246 |         else if ((ch = *pin++) == '\0') { | 
 | 1247 |             /* There's a lone trailing %; doesn't make sense. */ | 
 | 1248 |             PyErr_SetString(PyExc_ValueError, "strftime format " | 
 | 1249 |                             "ends with raw %"); | 
 | 1250 |             goto Done; | 
 | 1251 |         } | 
 | 1252 |         /* A % has been seen and ch is the character after it. */ | 
 | 1253 |         else if (ch == 'z') { | 
 | 1254 |             if (zreplacement == NULL) { | 
 | 1255 |                 /* format utcoffset */ | 
 | 1256 |                 char buf[100]; | 
 | 1257 |                 PyObject *tzinfo = get_tzinfo_member(object); | 
 | 1258 |                 zreplacement = PyBytes_FromStringAndSize("", 0); | 
 | 1259 |                 if (zreplacement == NULL) goto Done; | 
 | 1260 |                 if (tzinfo != Py_None && tzinfo != NULL) { | 
 | 1261 |                     assert(tzinfoarg != NULL); | 
 | 1262 |                     if (format_utcoffset(buf, | 
 | 1263 |                                          sizeof(buf), | 
 | 1264 |                                          "", | 
 | 1265 |                                          tzinfo, | 
 | 1266 |                                          tzinfoarg) < 0) | 
 | 1267 |                         goto Done; | 
 | 1268 |                     Py_DECREF(zreplacement); | 
 | 1269 |                     zreplacement = | 
 | 1270 |                       PyBytes_FromStringAndSize(buf, | 
 | 1271 |                                                strlen(buf)); | 
 | 1272 |                     if (zreplacement == NULL) | 
 | 1273 |                         goto Done; | 
 | 1274 |                 } | 
 | 1275 |             } | 
 | 1276 |             assert(zreplacement != NULL); | 
 | 1277 |             ptoappend = PyBytes_AS_STRING(zreplacement); | 
 | 1278 |             ntoappend = PyBytes_GET_SIZE(zreplacement); | 
 | 1279 |         } | 
 | 1280 |         else if (ch == 'Z') { | 
 | 1281 |             /* format tzname */ | 
 | 1282 |             if (Zreplacement == NULL) { | 
 | 1283 |                 Zreplacement = make_Zreplacement(object, | 
 | 1284 |                                                  tzinfoarg); | 
 | 1285 |                 if (Zreplacement == NULL) | 
 | 1286 |                     goto Done; | 
 | 1287 |             } | 
 | 1288 |             assert(Zreplacement != NULL); | 
 | 1289 |             assert(PyUnicode_Check(Zreplacement)); | 
| Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1290 |             ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 |                                                   &ntoappend); | 
| Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1292 |             if (ptoappend == NULL) | 
 | 1293 |                 goto Done; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 |         } | 
 | 1295 |         else if (ch == 'f') { | 
 | 1296 |             /* format microseconds */ | 
 | 1297 |             if (freplacement == NULL) { | 
 | 1298 |                 freplacement = make_freplacement(object); | 
 | 1299 |                 if (freplacement == NULL) | 
 | 1300 |                     goto Done; | 
 | 1301 |             } | 
 | 1302 |             assert(freplacement != NULL); | 
 | 1303 |             assert(PyBytes_Check(freplacement)); | 
 | 1304 |             ptoappend = PyBytes_AS_STRING(freplacement); | 
 | 1305 |             ntoappend = PyBytes_GET_SIZE(freplacement); | 
 | 1306 |         } | 
 | 1307 |         else { | 
 | 1308 |             /* percent followed by neither z nor Z */ | 
 | 1309 |             ptoappend = pin - 2; | 
 | 1310 |             ntoappend = 2; | 
 | 1311 |         } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1312 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 |         /* Append the ntoappend chars starting at ptoappend to | 
 | 1314 |          * the new format. | 
 | 1315 |          */ | 
 | 1316 |         if (ntoappend == 0) | 
 | 1317 |             continue; | 
 | 1318 |         assert(ptoappend != NULL); | 
 | 1319 |         assert(ntoappend > 0); | 
 | 1320 |         while (usednew + ntoappend > totalnew) { | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1321 |             if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 |                 PyErr_NoMemory(); | 
 | 1323 |                 goto Done; | 
 | 1324 |             } | 
| Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1325 |             totalnew <<= 1; | 
 | 1326 |             if (_PyBytes_Resize(&newfmt, totalnew) < 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 |                 goto Done; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 |             pnew = PyBytes_AsString(newfmt) + usednew; | 
 | 1329 |         } | 
 | 1330 |         memcpy(pnew, ptoappend, ntoappend); | 
 | 1331 |         pnew += ntoappend; | 
 | 1332 |         usednew += ntoappend; | 
 | 1333 |         assert(usednew <= totalnew); | 
 | 1334 |     }  /* end while() */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1335 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 |     if (_PyBytes_Resize(&newfmt, usednew) < 0) | 
 | 1337 |         goto Done; | 
 | 1338 |     { | 
 | 1339 |         PyObject *format; | 
 | 1340 |         PyObject *time = PyImport_ImportModuleNoBlock("time"); | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1341 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 |         if (time == NULL) | 
 | 1343 |             goto Done; | 
 | 1344 |         format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); | 
 | 1345 |         if (format != NULL) { | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1346 |             result = _PyObject_CallMethodId(time, &PyId_strftime, "OO", | 
 | 1347 |                                             format, timetuple, NULL); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 |             Py_DECREF(format); | 
 | 1349 |         } | 
 | 1350 |         Py_DECREF(time); | 
 | 1351 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1352 |  Done: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 |     Py_XDECREF(freplacement); | 
 | 1354 |     Py_XDECREF(zreplacement); | 
 | 1355 |     Py_XDECREF(Zreplacement); | 
 | 1356 |     Py_XDECREF(newfmt); | 
 | 1357 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1358 | } | 
 | 1359 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1360 | /* --------------------------------------------------------------------------- | 
 | 1361 |  * Wrap functions from the time module.  These aren't directly available | 
 | 1362 |  * from C.  Perhaps they should be. | 
 | 1363 |  */ | 
 | 1364 |  | 
 | 1365 | /* Call time.time() and return its result (a Python float). */ | 
 | 1366 | static PyObject * | 
| Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1367 | time_time(void) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1368 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 |     PyObject *result = NULL; | 
 | 1370 |     PyObject *time = PyImport_ImportModuleNoBlock("time"); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1371 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 |     if (time != NULL) { | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1373 |         _Py_IDENTIFIER(time); | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1374 |  | 
| Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 1375 |         result = _PyObject_CallMethodId(time, &PyId_time, NULL); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 |         Py_DECREF(time); | 
 | 1377 |     } | 
 | 1378 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1379 | } | 
 | 1380 |  | 
 | 1381 | /* Build a time.struct_time.  The weekday and day number are automatically | 
 | 1382 |  * computed from the y,m,d args. | 
 | 1383 |  */ | 
 | 1384 | static PyObject * | 
 | 1385 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) | 
 | 1386 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 |     PyObject *time; | 
 | 1388 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1389 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 |     time = PyImport_ImportModuleNoBlock("time"); | 
 | 1391 |     if (time != NULL) { | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1392 |         _Py_IDENTIFIER(struct_time); | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1393 |  | 
 | 1394 |         result = _PyObject_CallMethodId(time, &PyId_struct_time, | 
 | 1395 |                                         "((iiiiiiiii))", | 
 | 1396 |                                         y, m, d, | 
 | 1397 |                                         hh, mm, ss, | 
 | 1398 |                                         weekday(y, m, d), | 
 | 1399 |                                         days_before_month(y, m) + d, | 
 | 1400 |                                         dstflag); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 |         Py_DECREF(time); | 
 | 1402 |     } | 
 | 1403 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1404 | } | 
 | 1405 |  | 
 | 1406 | /* --------------------------------------------------------------------------- | 
 | 1407 |  * Miscellaneous helpers. | 
 | 1408 |  */ | 
 | 1409 |  | 
| Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1410 | /* For various reasons, we need to use tp_richcompare instead of tp_reserved. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1411 |  * The comparisons here all most naturally compute a cmp()-like result. | 
 | 1412 |  * This little helper turns that into a bool result for rich comparisons. | 
 | 1413 |  */ | 
 | 1414 | static PyObject * | 
 | 1415 | diff_to_bool(int diff, int op) | 
 | 1416 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 |     PyObject *result; | 
 | 1418 |     int istrue; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1419 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 |     switch (op) { | 
 | 1421 |         case Py_EQ: istrue = diff == 0; break; | 
 | 1422 |         case Py_NE: istrue = diff != 0; break; | 
 | 1423 |         case Py_LE: istrue = diff <= 0; break; | 
 | 1424 |         case Py_GE: istrue = diff >= 0; break; | 
 | 1425 |         case Py_LT: istrue = diff < 0; break; | 
 | 1426 |         case Py_GT: istrue = diff > 0; break; | 
 | 1427 |         default: | 
 | 1428 |             assert(! "op unknown"); | 
 | 1429 |             istrue = 0; /* To shut up compiler */ | 
 | 1430 |     } | 
 | 1431 |     result = istrue ? Py_True : Py_False; | 
 | 1432 |     Py_INCREF(result); | 
 | 1433 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1434 | } | 
 | 1435 |  | 
| Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1436 | /* Raises a "can't compare" TypeError and returns NULL. */ | 
 | 1437 | static PyObject * | 
 | 1438 | cmperror(PyObject *a, PyObject *b) | 
 | 1439 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 |     PyErr_Format(PyExc_TypeError, | 
 | 1441 |                  "can't compare %s to %s", | 
 | 1442 |                  Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); | 
 | 1443 |     return NULL; | 
| Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1444 | } | 
 | 1445 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1446 | /* --------------------------------------------------------------------------- | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1447 |  * Cached Python objects; these are set by the module init function. | 
 | 1448 |  */ | 
 | 1449 |  | 
 | 1450 | /* Conversion factors. */ | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 1451 | static PyObject *one = NULL;      /* 1 */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | static PyObject *us_per_ms = NULL;      /* 1000 */ | 
 | 1453 | static PyObject *us_per_second = NULL;  /* 1000000 */ | 
 | 1454 | static PyObject *us_per_minute = NULL;  /* 1e6 * 60 as Python int */ | 
| Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1455 | static PyObject *us_per_hour = NULL;    /* 1e6 * 3600 as Python int */ | 
 | 1456 | static PyObject *us_per_day = NULL;     /* 1e6 * 3600 * 24 as Python int */ | 
 | 1457 | static PyObject *us_per_week = NULL;    /* 1e6*3600*24*7 as Python int */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1458 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ | 
 | 1459 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1460 | /* --------------------------------------------------------------------------- | 
 | 1461 |  * Class implementations. | 
 | 1462 |  */ | 
 | 1463 |  | 
 | 1464 | /* | 
 | 1465 |  * PyDateTime_Delta implementation. | 
 | 1466 |  */ | 
 | 1467 |  | 
 | 1468 | /* Convert a timedelta to a number of us, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 |  *      (24*3600*self.days + self.seconds)*1000000 + self.microseconds | 
| Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1470 |  * as a Python int. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1471 |  * Doing mixed-radix arithmetic by hand instead is excruciating in C, | 
 | 1472 |  * due to ubiquitous overflow possibilities. | 
 | 1473 |  */ | 
 | 1474 | static PyObject * | 
 | 1475 | delta_to_microseconds(PyDateTime_Delta *self) | 
 | 1476 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1477 |     PyObject *x1 = NULL; | 
 | 1478 |     PyObject *x2 = NULL; | 
 | 1479 |     PyObject *x3 = NULL; | 
 | 1480 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1481 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 |     x1 = PyLong_FromLong(GET_TD_DAYS(self)); | 
 | 1483 |     if (x1 == NULL) | 
 | 1484 |         goto Done; | 
 | 1485 |     x2 = PyNumber_Multiply(x1, seconds_per_day);        /* days in seconds */ | 
 | 1486 |     if (x2 == NULL) | 
 | 1487 |         goto Done; | 
 | 1488 |     Py_DECREF(x1); | 
 | 1489 |     x1 = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1490 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 |     /* x2 has days in seconds */ | 
 | 1492 |     x1 = PyLong_FromLong(GET_TD_SECONDS(self));         /* seconds */ | 
 | 1493 |     if (x1 == NULL) | 
 | 1494 |         goto Done; | 
 | 1495 |     x3 = PyNumber_Add(x1, x2);          /* days and seconds in seconds */ | 
 | 1496 |     if (x3 == NULL) | 
 | 1497 |         goto Done; | 
 | 1498 |     Py_DECREF(x1); | 
 | 1499 |     Py_DECREF(x2); | 
| Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1500 |     /* x1 = */ x2 = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1501 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 |     /* x3 has days+seconds in seconds */ | 
 | 1503 |     x1 = PyNumber_Multiply(x3, us_per_second);          /* us */ | 
 | 1504 |     if (x1 == NULL) | 
 | 1505 |         goto Done; | 
 | 1506 |     Py_DECREF(x3); | 
 | 1507 |     x3 = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1508 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 |     /* x1 has days+seconds in us */ | 
 | 1510 |     x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); | 
 | 1511 |     if (x2 == NULL) | 
 | 1512 |         goto Done; | 
 | 1513 |     result = PyNumber_Add(x1, x2); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1514 |  | 
 | 1515 | Done: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 |     Py_XDECREF(x1); | 
 | 1517 |     Py_XDECREF(x2); | 
 | 1518 |     Py_XDECREF(x3); | 
 | 1519 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1520 | } | 
 | 1521 |  | 
| Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1522 | /* Convert a number of us (as a Python int) to a timedelta. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1523 |  */ | 
 | 1524 | static PyObject * | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1525 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1526 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 |     int us; | 
 | 1528 |     int s; | 
 | 1529 |     int d; | 
 | 1530 |     long temp; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1531 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 |     PyObject *tuple = NULL; | 
 | 1533 |     PyObject *num = NULL; | 
 | 1534 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1535 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 |     tuple = PyNumber_Divmod(pyus, us_per_second); | 
 | 1537 |     if (tuple == NULL) | 
 | 1538 |         goto Done; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1539 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 |     num = PyTuple_GetItem(tuple, 1);            /* us */ | 
 | 1541 |     if (num == NULL) | 
 | 1542 |         goto Done; | 
 | 1543 |     temp = PyLong_AsLong(num); | 
 | 1544 |     num = NULL; | 
 | 1545 |     if (temp == -1 && PyErr_Occurred()) | 
 | 1546 |         goto Done; | 
 | 1547 |     assert(0 <= temp && temp < 1000000); | 
 | 1548 |     us = (int)temp; | 
 | 1549 |     if (us < 0) { | 
 | 1550 |         /* The divisor was positive, so this must be an error. */ | 
 | 1551 |         assert(PyErr_Occurred()); | 
 | 1552 |         goto Done; | 
 | 1553 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1554 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1555 |     num = PyTuple_GetItem(tuple, 0);            /* leftover seconds */ | 
 | 1556 |     if (num == NULL) | 
 | 1557 |         goto Done; | 
 | 1558 |     Py_INCREF(num); | 
 | 1559 |     Py_DECREF(tuple); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1560 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 |     tuple = PyNumber_Divmod(num, seconds_per_day); | 
 | 1562 |     if (tuple == NULL) | 
 | 1563 |         goto Done; | 
 | 1564 |     Py_DECREF(num); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1565 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 |     num = PyTuple_GetItem(tuple, 1);            /* seconds */ | 
 | 1567 |     if (num == NULL) | 
 | 1568 |         goto Done; | 
 | 1569 |     temp = PyLong_AsLong(num); | 
 | 1570 |     num = NULL; | 
 | 1571 |     if (temp == -1 && PyErr_Occurred()) | 
 | 1572 |         goto Done; | 
 | 1573 |     assert(0 <= temp && temp < 24*3600); | 
 | 1574 |     s = (int)temp; | 
| Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1575 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 |     if (s < 0) { | 
 | 1577 |         /* The divisor was positive, so this must be an error. */ | 
 | 1578 |         assert(PyErr_Occurred()); | 
 | 1579 |         goto Done; | 
 | 1580 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1581 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 |     num = PyTuple_GetItem(tuple, 0);            /* leftover days */ | 
 | 1583 |     if (num == NULL) | 
 | 1584 |         goto Done; | 
 | 1585 |     Py_INCREF(num); | 
 | 1586 |     temp = PyLong_AsLong(num); | 
 | 1587 |     if (temp == -1 && PyErr_Occurred()) | 
 | 1588 |         goto Done; | 
 | 1589 |     d = (int)temp; | 
 | 1590 |     if ((long)d != temp) { | 
 | 1591 |         PyErr_SetString(PyExc_OverflowError, "normalized days too " | 
 | 1592 |                         "large to fit in a C int"); | 
 | 1593 |         goto Done; | 
 | 1594 |     } | 
 | 1595 |     result = new_delta_ex(d, s, us, 0, type); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1596 |  | 
 | 1597 | Done: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 |     Py_XDECREF(tuple); | 
 | 1599 |     Py_XDECREF(num); | 
 | 1600 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1601 | } | 
 | 1602 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | #define microseconds_to_delta(pymicros) \ | 
 | 1604 |     microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) | 
| Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1605 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1606 | static PyObject * | 
 | 1607 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) | 
 | 1608 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 |     PyObject *pyus_in; | 
 | 1610 |     PyObject *pyus_out; | 
 | 1611 |     PyObject *result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1612 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 |     pyus_in = delta_to_microseconds(delta); | 
 | 1614 |     if (pyus_in == NULL) | 
 | 1615 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1616 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 |     pyus_out = PyNumber_Multiply(pyus_in, intobj); | 
 | 1618 |     Py_DECREF(pyus_in); | 
 | 1619 |     if (pyus_out == NULL) | 
 | 1620 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1621 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 |     result = microseconds_to_delta(pyus_out); | 
 | 1623 |     Py_DECREF(pyus_out); | 
 | 1624 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1625 | } | 
 | 1626 |  | 
 | 1627 | static PyObject * | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1628 | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) | 
 | 1629 | { | 
 | 1630 |     PyObject *result = NULL; | 
 | 1631 |     PyObject *pyus_in = NULL, *temp, *pyus_out; | 
 | 1632 |     PyObject *ratio = NULL; | 
 | 1633 |  | 
 | 1634 |     pyus_in = delta_to_microseconds(delta); | 
 | 1635 |     if (pyus_in == NULL) | 
 | 1636 |         return NULL; | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1637 |     ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1638 |     if (ratio == NULL) | 
 | 1639 |         goto error; | 
 | 1640 |     temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); | 
 | 1641 |     Py_DECREF(pyus_in); | 
 | 1642 |     pyus_in = NULL; | 
 | 1643 |     if (temp == NULL) | 
 | 1644 |         goto error; | 
 | 1645 |     pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); | 
 | 1646 |     Py_DECREF(temp); | 
 | 1647 |     if (pyus_out == NULL) | 
 | 1648 |         goto error; | 
 | 1649 |     result = microseconds_to_delta(pyus_out); | 
 | 1650 |     Py_DECREF(pyus_out); | 
 | 1651 |  error: | 
 | 1652 |     Py_XDECREF(pyus_in); | 
 | 1653 |     Py_XDECREF(ratio); | 
 | 1654 |  | 
 | 1655 |     return result; | 
 | 1656 | } | 
 | 1657 |  | 
 | 1658 | static PyObject * | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1659 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) | 
 | 1660 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 |     PyObject *pyus_in; | 
 | 1662 |     PyObject *pyus_out; | 
 | 1663 |     PyObject *result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1664 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1665 |     pyus_in = delta_to_microseconds(delta); | 
 | 1666 |     if (pyus_in == NULL) | 
 | 1667 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1668 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 |     pyus_out = PyNumber_FloorDivide(pyus_in, intobj); | 
 | 1670 |     Py_DECREF(pyus_in); | 
 | 1671 |     if (pyus_out == NULL) | 
 | 1672 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1673 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 |     result = microseconds_to_delta(pyus_out); | 
 | 1675 |     Py_DECREF(pyus_out); | 
 | 1676 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1677 | } | 
 | 1678 |  | 
 | 1679 | static PyObject * | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1680 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) | 
 | 1681 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 |     PyObject *pyus_left; | 
 | 1683 |     PyObject *pyus_right; | 
 | 1684 |     PyObject *result; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1685 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 |     pyus_left = delta_to_microseconds(left); | 
 | 1687 |     if (pyus_left == NULL) | 
 | 1688 |         return NULL; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1689 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 |     pyus_right = delta_to_microseconds(right); | 
 | 1691 |     if (pyus_right == NULL)     { | 
 | 1692 |         Py_DECREF(pyus_left); | 
 | 1693 |         return NULL; | 
 | 1694 |     } | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1695 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 |     result = PyNumber_FloorDivide(pyus_left, pyus_right); | 
 | 1697 |     Py_DECREF(pyus_left); | 
 | 1698 |     Py_DECREF(pyus_right); | 
 | 1699 |     return result; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1700 | } | 
 | 1701 |  | 
 | 1702 | static PyObject * | 
 | 1703 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) | 
 | 1704 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 |     PyObject *pyus_left; | 
 | 1706 |     PyObject *pyus_right; | 
 | 1707 |     PyObject *result; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1708 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 |     pyus_left = delta_to_microseconds(left); | 
 | 1710 |     if (pyus_left == NULL) | 
 | 1711 |         return NULL; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1712 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 |     pyus_right = delta_to_microseconds(right); | 
 | 1714 |     if (pyus_right == NULL)     { | 
 | 1715 |         Py_DECREF(pyus_left); | 
 | 1716 |         return NULL; | 
 | 1717 |     } | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1718 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 |     result = PyNumber_TrueDivide(pyus_left, pyus_right); | 
 | 1720 |     Py_DECREF(pyus_left); | 
 | 1721 |     Py_DECREF(pyus_right); | 
 | 1722 |     return result; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1723 | } | 
 | 1724 |  | 
 | 1725 | static PyObject * | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1726 | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) | 
 | 1727 | { | 
 | 1728 |     PyObject *result = NULL; | 
 | 1729 |     PyObject *pyus_in = NULL, *temp, *pyus_out; | 
 | 1730 |     PyObject *ratio = NULL; | 
 | 1731 |  | 
 | 1732 |     pyus_in = delta_to_microseconds(delta); | 
 | 1733 |     if (pyus_in == NULL) | 
 | 1734 |         return NULL; | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1735 |     ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1736 |     if (ratio == NULL) | 
 | 1737 |         goto error; | 
 | 1738 |     temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1)); | 
 | 1739 |     Py_DECREF(pyus_in); | 
 | 1740 |     pyus_in = NULL; | 
 | 1741 |     if (temp == NULL) | 
 | 1742 |         goto error; | 
 | 1743 |     pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0)); | 
 | 1744 |     Py_DECREF(temp); | 
 | 1745 |     if (pyus_out == NULL) | 
 | 1746 |         goto error; | 
 | 1747 |     result = microseconds_to_delta(pyus_out); | 
 | 1748 |     Py_DECREF(pyus_out); | 
 | 1749 |  error: | 
 | 1750 |     Py_XDECREF(pyus_in); | 
 | 1751 |     Py_XDECREF(ratio); | 
 | 1752 |  | 
 | 1753 |     return result; | 
 | 1754 | } | 
 | 1755 |  | 
 | 1756 | static PyObject * | 
 | 1757 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) | 
 | 1758 | { | 
 | 1759 |     PyObject *result; | 
 | 1760 |     PyObject *pyus_in, *pyus_out; | 
 | 1761 |     pyus_in = delta_to_microseconds(delta); | 
 | 1762 |     if (pyus_in == NULL) | 
 | 1763 |         return NULL; | 
 | 1764 |     pyus_out = divide_nearest(pyus_in, i); | 
 | 1765 |     Py_DECREF(pyus_in); | 
 | 1766 |     if (pyus_out == NULL) | 
 | 1767 |         return NULL; | 
 | 1768 |     result = microseconds_to_delta(pyus_out); | 
 | 1769 |     Py_DECREF(pyus_out); | 
 | 1770 |  | 
 | 1771 |     return result; | 
 | 1772 | } | 
 | 1773 |  | 
 | 1774 | static PyObject * | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1775 | delta_add(PyObject *left, PyObject *right) | 
 | 1776 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 |     PyObject *result = Py_NotImplemented; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1778 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 |     if (PyDelta_Check(left) && PyDelta_Check(right)) { | 
 | 1780 |         /* delta + delta */ | 
 | 1781 |         /* The C-level additions can't overflow because of the | 
 | 1782 |          * invariant bounds. | 
 | 1783 |          */ | 
 | 1784 |         int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); | 
 | 1785 |         int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); | 
 | 1786 |         int microseconds = GET_TD_MICROSECONDS(left) + | 
 | 1787 |                            GET_TD_MICROSECONDS(right); | 
 | 1788 |         result = new_delta(days, seconds, microseconds, 1); | 
 | 1789 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1790 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 |     if (result == Py_NotImplemented) | 
 | 1792 |         Py_INCREF(result); | 
 | 1793 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1794 | } | 
 | 1795 |  | 
 | 1796 | static PyObject * | 
 | 1797 | delta_negative(PyDateTime_Delta *self) | 
 | 1798 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 |     return new_delta(-GET_TD_DAYS(self), | 
 | 1800 |                      -GET_TD_SECONDS(self), | 
 | 1801 |                      -GET_TD_MICROSECONDS(self), | 
 | 1802 |                      1); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1803 | } | 
 | 1804 |  | 
 | 1805 | static PyObject * | 
 | 1806 | delta_positive(PyDateTime_Delta *self) | 
 | 1807 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 |     /* Could optimize this (by returning self) if this isn't a | 
 | 1809 |      * subclass -- but who uses unary + ?  Approximately nobody. | 
 | 1810 |      */ | 
 | 1811 |     return new_delta(GET_TD_DAYS(self), | 
 | 1812 |                      GET_TD_SECONDS(self), | 
 | 1813 |                      GET_TD_MICROSECONDS(self), | 
 | 1814 |                      0); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1815 | } | 
 | 1816 |  | 
 | 1817 | static PyObject * | 
 | 1818 | delta_abs(PyDateTime_Delta *self) | 
 | 1819 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 |     PyObject *result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1821 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 |     assert(GET_TD_MICROSECONDS(self) >= 0); | 
 | 1823 |     assert(GET_TD_SECONDS(self) >= 0); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1824 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 |     if (GET_TD_DAYS(self) < 0) | 
 | 1826 |         result = delta_negative(self); | 
 | 1827 |     else | 
 | 1828 |         result = delta_positive(self); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1829 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1831 | } | 
 | 1832 |  | 
 | 1833 | static PyObject * | 
 | 1834 | delta_subtract(PyObject *left, PyObject *right) | 
 | 1835 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 |     PyObject *result = Py_NotImplemented; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1837 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 |     if (PyDelta_Check(left) && PyDelta_Check(right)) { | 
 | 1839 |         /* delta - delta */ | 
| Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 1840 |         /* The C-level additions can't overflow because of the | 
 | 1841 |          * invariant bounds. | 
 | 1842 |          */ | 
 | 1843 |         int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); | 
 | 1844 |         int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); | 
 | 1845 |         int microseconds = GET_TD_MICROSECONDS(left) - | 
 | 1846 |                            GET_TD_MICROSECONDS(right); | 
 | 1847 |         result = new_delta(days, seconds, microseconds, 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1849 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 |     if (result == Py_NotImplemented) | 
 | 1851 |         Py_INCREF(result); | 
 | 1852 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1853 | } | 
 | 1854 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1855 | static int | 
 | 1856 | delta_cmp(PyObject *self, PyObject *other) | 
 | 1857 | { | 
 | 1858 |     int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); | 
 | 1859 |     if (diff == 0) { | 
 | 1860 |         diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); | 
 | 1861 |         if (diff == 0) | 
 | 1862 |             diff = GET_TD_MICROSECONDS(self) - | 
 | 1863 |                 GET_TD_MICROSECONDS(other); | 
 | 1864 |     } | 
 | 1865 |     return diff; | 
 | 1866 | } | 
 | 1867 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1868 | static PyObject * | 
| Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1869 | delta_richcompare(PyObject *self, PyObject *other, int op) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1870 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 |     if (PyDelta_Check(other)) { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1872 |         int diff = delta_cmp(self, other); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 |         return diff_to_bool(diff, op); | 
 | 1874 |     } | 
 | 1875 |     else { | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1876 |         Py_RETURN_NOTIMPLEMENTED; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1878 | } | 
 | 1879 |  | 
 | 1880 | static PyObject *delta_getstate(PyDateTime_Delta *self); | 
 | 1881 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1882 | static Py_hash_t | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1883 | delta_hash(PyDateTime_Delta *self) | 
 | 1884 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 |     if (self->hashcode == -1) { | 
 | 1886 |         PyObject *temp = delta_getstate(self); | 
 | 1887 |         if (temp != NULL) { | 
 | 1888 |             self->hashcode = PyObject_Hash(temp); | 
 | 1889 |             Py_DECREF(temp); | 
 | 1890 |         } | 
 | 1891 |     } | 
 | 1892 |     return self->hashcode; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1893 | } | 
 | 1894 |  | 
 | 1895 | static PyObject * | 
 | 1896 | delta_multiply(PyObject *left, PyObject *right) | 
 | 1897 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 |     PyObject *result = Py_NotImplemented; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1899 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 |     if (PyDelta_Check(left)) { | 
 | 1901 |         /* delta * ??? */ | 
 | 1902 |         if (PyLong_Check(right)) | 
 | 1903 |             result = multiply_int_timedelta(right, | 
 | 1904 |                             (PyDateTime_Delta *) left); | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1905 |         else if (PyFloat_Check(right)) | 
 | 1906 |             result = multiply_float_timedelta(right, | 
 | 1907 |                             (PyDateTime_Delta *) left); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 |     } | 
 | 1909 |     else if (PyLong_Check(left)) | 
 | 1910 |         result = multiply_int_timedelta(left, | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1911 |                         (PyDateTime_Delta *) right); | 
 | 1912 |     else if (PyFloat_Check(left)) | 
 | 1913 |         result = multiply_float_timedelta(left, | 
 | 1914 |                         (PyDateTime_Delta *) right); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1915 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 |     if (result == Py_NotImplemented) | 
 | 1917 |         Py_INCREF(result); | 
 | 1918 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1919 | } | 
 | 1920 |  | 
 | 1921 | static PyObject * | 
 | 1922 | delta_divide(PyObject *left, PyObject *right) | 
 | 1923 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 |     PyObject *result = Py_NotImplemented; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1925 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 |     if (PyDelta_Check(left)) { | 
 | 1927 |         /* delta * ??? */ | 
 | 1928 |         if (PyLong_Check(right)) | 
 | 1929 |             result = divide_timedelta_int( | 
 | 1930 |                             (PyDateTime_Delta *)left, | 
 | 1931 |                             right); | 
 | 1932 |         else if (PyDelta_Check(right)) | 
 | 1933 |             result = divide_timedelta_timedelta( | 
 | 1934 |                             (PyDateTime_Delta *)left, | 
 | 1935 |                             (PyDateTime_Delta *)right); | 
 | 1936 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1937 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 |     if (result == Py_NotImplemented) | 
 | 1939 |         Py_INCREF(result); | 
 | 1940 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1941 | } | 
 | 1942 |  | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1943 | static PyObject * | 
 | 1944 | delta_truedivide(PyObject *left, PyObject *right) | 
 | 1945 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1946 |     PyObject *result = Py_NotImplemented; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1947 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 |     if (PyDelta_Check(left)) { | 
 | 1949 |         if (PyDelta_Check(right)) | 
 | 1950 |             result = truedivide_timedelta_timedelta( | 
 | 1951 |                             (PyDateTime_Delta *)left, | 
 | 1952 |                             (PyDateTime_Delta *)right); | 
| Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1953 |         else if (PyFloat_Check(right)) | 
 | 1954 |             result = truedivide_timedelta_float( | 
 | 1955 |                             (PyDateTime_Delta *)left, right); | 
 | 1956 |         else if (PyLong_Check(right)) | 
 | 1957 |             result = truedivide_timedelta_int( | 
 | 1958 |                             (PyDateTime_Delta *)left, right); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 |     } | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1960 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 |     if (result == Py_NotImplemented) | 
 | 1962 |         Py_INCREF(result); | 
 | 1963 |     return result; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1964 | } | 
 | 1965 |  | 
 | 1966 | static PyObject * | 
 | 1967 | delta_remainder(PyObject *left, PyObject *right) | 
 | 1968 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 |     PyObject *pyus_left; | 
 | 1970 |     PyObject *pyus_right; | 
 | 1971 |     PyObject *pyus_remainder; | 
 | 1972 |     PyObject *remainder; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1973 |  | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1974 |     if (!PyDelta_Check(left) || !PyDelta_Check(right)) | 
 | 1975 |         Py_RETURN_NOTIMPLEMENTED; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1976 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1977 |     pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); | 
 | 1978 |     if (pyus_left == NULL) | 
 | 1979 |         return NULL; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1980 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 |     pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); | 
 | 1982 |     if (pyus_right == NULL) { | 
 | 1983 |         Py_DECREF(pyus_left); | 
 | 1984 |         return NULL; | 
 | 1985 |     } | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1986 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 |     pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); | 
 | 1988 |     Py_DECREF(pyus_left); | 
 | 1989 |     Py_DECREF(pyus_right); | 
 | 1990 |     if (pyus_remainder == NULL) | 
 | 1991 |         return NULL; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1992 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 |     remainder = microseconds_to_delta(pyus_remainder); | 
 | 1994 |     Py_DECREF(pyus_remainder); | 
 | 1995 |     if (remainder == NULL) | 
 | 1996 |         return NULL; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1997 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 |     return remainder; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1999 | } | 
 | 2000 |  | 
 | 2001 | static PyObject * | 
 | 2002 | delta_divmod(PyObject *left, PyObject *right) | 
 | 2003 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 |     PyObject *pyus_left; | 
 | 2005 |     PyObject *pyus_right; | 
 | 2006 |     PyObject *divmod; | 
 | 2007 |     PyObject *delta; | 
 | 2008 |     PyObject *result; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2009 |  | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2010 |     if (!PyDelta_Check(left) || !PyDelta_Check(right)) | 
 | 2011 |         Py_RETURN_NOTIMPLEMENTED; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2012 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 |     pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); | 
 | 2014 |     if (pyus_left == NULL) | 
 | 2015 |         return NULL; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2016 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2017 |     pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); | 
 | 2018 |     if (pyus_right == NULL) { | 
 | 2019 |         Py_DECREF(pyus_left); | 
 | 2020 |         return NULL; | 
 | 2021 |     } | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2022 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 |     divmod = PyNumber_Divmod(pyus_left, pyus_right); | 
 | 2024 |     Py_DECREF(pyus_left); | 
 | 2025 |     Py_DECREF(pyus_right); | 
 | 2026 |     if (divmod == NULL) | 
 | 2027 |         return NULL; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2028 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 |     assert(PyTuple_Size(divmod) == 2); | 
 | 2030 |     delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); | 
 | 2031 |     if (delta == NULL) { | 
 | 2032 |         Py_DECREF(divmod); | 
 | 2033 |         return NULL; | 
 | 2034 |     } | 
 | 2035 |     result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); | 
 | 2036 |     Py_DECREF(delta); | 
 | 2037 |     Py_DECREF(divmod); | 
 | 2038 |     return result; | 
| Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2039 | } | 
 | 2040 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2041 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a | 
 | 2042 |  * timedelta constructor.  sofar is the # of microseconds accounted for | 
 | 2043 |  * so far, and there are factor microseconds per current unit, the number | 
 | 2044 |  * of which is given by num.  num * factor is added to sofar in a | 
 | 2045 |  * numerically careful way, and that's the result.  Any fractional | 
 | 2046 |  * microseconds left over (this can happen if num is a float type) are | 
 | 2047 |  * added into *leftover. | 
 | 2048 |  * Note that there are many ways this can give an error (NULL) return. | 
 | 2049 |  */ | 
 | 2050 | static PyObject * | 
 | 2051 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, | 
 | 2052 |       double *leftover) | 
 | 2053 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 |     PyObject *prod; | 
 | 2055 |     PyObject *sum; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2056 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 |     assert(num != NULL); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2058 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 |     if (PyLong_Check(num)) { | 
 | 2060 |         prod = PyNumber_Multiply(num, factor); | 
 | 2061 |         if (prod == NULL) | 
 | 2062 |             return NULL; | 
 | 2063 |         sum = PyNumber_Add(sofar, prod); | 
 | 2064 |         Py_DECREF(prod); | 
 | 2065 |         return sum; | 
 | 2066 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2067 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 |     if (PyFloat_Check(num)) { | 
 | 2069 |         double dnum; | 
 | 2070 |         double fracpart; | 
 | 2071 |         double intpart; | 
 | 2072 |         PyObject *x; | 
 | 2073 |         PyObject *y; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2074 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 |         /* The Plan:  decompose num into an integer part and a | 
 | 2076 |          * fractional part, num = intpart + fracpart. | 
 | 2077 |          * Then num * factor == | 
 | 2078 |          *      intpart * factor + fracpart * factor | 
 | 2079 |          * and the LHS can be computed exactly in long arithmetic. | 
 | 2080 |          * The RHS is again broken into an int part and frac part. | 
 | 2081 |          * and the frac part is added into *leftover. | 
 | 2082 |          */ | 
 | 2083 |         dnum = PyFloat_AsDouble(num); | 
 | 2084 |         if (dnum == -1.0 && PyErr_Occurred()) | 
 | 2085 |             return NULL; | 
 | 2086 |         fracpart = modf(dnum, &intpart); | 
 | 2087 |         x = PyLong_FromDouble(intpart); | 
 | 2088 |         if (x == NULL) | 
 | 2089 |             return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2090 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 |         prod = PyNumber_Multiply(x, factor); | 
 | 2092 |         Py_DECREF(x); | 
 | 2093 |         if (prod == NULL) | 
 | 2094 |             return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2095 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 |         sum = PyNumber_Add(sofar, prod); | 
 | 2097 |         Py_DECREF(prod); | 
 | 2098 |         if (sum == NULL) | 
 | 2099 |             return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2100 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 |         if (fracpart == 0.0) | 
 | 2102 |             return sum; | 
 | 2103 |         /* So far we've lost no information.  Dealing with the | 
 | 2104 |          * fractional part requires float arithmetic, and may | 
 | 2105 |          * lose a little info. | 
 | 2106 |          */ | 
 | 2107 |         assert(PyLong_Check(factor)); | 
 | 2108 |         dnum = PyLong_AsDouble(factor); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2109 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 |         dnum *= fracpart; | 
 | 2111 |         fracpart = modf(dnum, &intpart); | 
 | 2112 |         x = PyLong_FromDouble(intpart); | 
 | 2113 |         if (x == NULL) { | 
 | 2114 |             Py_DECREF(sum); | 
 | 2115 |             return NULL; | 
 | 2116 |         } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2117 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 |         y = PyNumber_Add(sum, x); | 
 | 2119 |         Py_DECREF(sum); | 
 | 2120 |         Py_DECREF(x); | 
 | 2121 |         *leftover += fracpart; | 
 | 2122 |         return y; | 
 | 2123 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2124 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 |     PyErr_Format(PyExc_TypeError, | 
 | 2126 |                  "unsupported type for timedelta %s component: %s", | 
 | 2127 |                  tag, Py_TYPE(num)->tp_name); | 
 | 2128 |     return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2129 | } | 
 | 2130 |  | 
 | 2131 | static PyObject * | 
 | 2132 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) | 
 | 2133 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 |     PyObject *self = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2135 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 |     /* Argument objects. */ | 
 | 2137 |     PyObject *day = NULL; | 
 | 2138 |     PyObject *second = NULL; | 
 | 2139 |     PyObject *us = NULL; | 
 | 2140 |     PyObject *ms = NULL; | 
 | 2141 |     PyObject *minute = NULL; | 
 | 2142 |     PyObject *hour = NULL; | 
 | 2143 |     PyObject *week = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2144 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 |     PyObject *x = NULL;         /* running sum of microseconds */ | 
 | 2146 |     PyObject *y = NULL;         /* temp sum of microseconds */ | 
 | 2147 |     double leftover_us = 0.0; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2148 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 |     static char *keywords[] = { | 
 | 2150 |         "days", "seconds", "microseconds", "milliseconds", | 
 | 2151 |         "minutes", "hours", "weeks", NULL | 
 | 2152 |     }; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2153 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2154 |     if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", | 
 | 2155 |                                     keywords, | 
 | 2156 |                                     &day, &second, &us, | 
 | 2157 |                                     &ms, &minute, &hour, &week) == 0) | 
 | 2158 |         goto Done; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2159 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 |     x = PyLong_FromLong(0); | 
 | 2161 |     if (x == NULL) | 
 | 2162 |         goto Done; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2163 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | #define CLEANUP         \ | 
 | 2165 |     Py_DECREF(x);       \ | 
 | 2166 |     x = y;              \ | 
 | 2167 |     if (x == NULL)      \ | 
 | 2168 |         goto Done | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2169 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 |     if (us) { | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2171 |         y = accum("microseconds", x, us, one, &leftover_us); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 |         CLEANUP; | 
 | 2173 |     } | 
 | 2174 |     if (ms) { | 
 | 2175 |         y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); | 
 | 2176 |         CLEANUP; | 
 | 2177 |     } | 
 | 2178 |     if (second) { | 
 | 2179 |         y = accum("seconds", x, second, us_per_second, &leftover_us); | 
 | 2180 |         CLEANUP; | 
 | 2181 |     } | 
 | 2182 |     if (minute) { | 
 | 2183 |         y = accum("minutes", x, minute, us_per_minute, &leftover_us); | 
 | 2184 |         CLEANUP; | 
 | 2185 |     } | 
 | 2186 |     if (hour) { | 
 | 2187 |         y = accum("hours", x, hour, us_per_hour, &leftover_us); | 
 | 2188 |         CLEANUP; | 
 | 2189 |     } | 
 | 2190 |     if (day) { | 
 | 2191 |         y = accum("days", x, day, us_per_day, &leftover_us); | 
 | 2192 |         CLEANUP; | 
 | 2193 |     } | 
 | 2194 |     if (week) { | 
 | 2195 |         y = accum("weeks", x, week, us_per_week, &leftover_us); | 
 | 2196 |         CLEANUP; | 
 | 2197 |     } | 
 | 2198 |     if (leftover_us) { | 
 | 2199 |         /* Round to nearest whole # of us, and add into x. */ | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2200 |         double whole_us = round(leftover_us); | 
| Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2201 |         int x_is_odd; | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2202 |         PyObject *temp; | 
 | 2203 |  | 
| Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2204 |         whole_us = round(leftover_us); | 
 | 2205 |         if (fabs(whole_us - leftover_us) == 0.5) { | 
 | 2206 |             /* We're exactly halfway between two integers.  In order | 
 | 2207 |              * to do round-half-to-even, we must determine whether x | 
 | 2208 |              * is odd. Note that x is odd when it's last bit is 1. The | 
 | 2209 |              * code below uses bitwise and operation to check the last | 
 | 2210 |              * bit. */ | 
 | 2211 |             temp = PyNumber_And(x, one);  /* temp <- x & 1 */ | 
 | 2212 |             if (temp == NULL) { | 
 | 2213 |                 Py_DECREF(x); | 
 | 2214 |                 goto Done; | 
 | 2215 |             } | 
 | 2216 |             x_is_odd = PyObject_IsTrue(temp); | 
 | 2217 |             Py_DECREF(temp); | 
 | 2218 |             if (x_is_odd == -1) { | 
 | 2219 |                 Py_DECREF(x); | 
 | 2220 |                 goto Done; | 
 | 2221 |             } | 
 | 2222 |             whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; | 
 | 2223 |         } | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2224 |  | 
| Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2225 |         temp = PyLong_FromLong((long)whole_us); | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2226 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 |         if (temp == NULL) { | 
 | 2228 |             Py_DECREF(x); | 
 | 2229 |             goto Done; | 
 | 2230 |         } | 
 | 2231 |         y = PyNumber_Add(x, temp); | 
 | 2232 |         Py_DECREF(temp); | 
 | 2233 |         CLEANUP; | 
 | 2234 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2235 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 |     self = microseconds_to_delta_ex(x, type); | 
 | 2237 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2238 | Done: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 |     return self; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2240 |  | 
 | 2241 | #undef CLEANUP | 
 | 2242 | } | 
 | 2243 |  | 
 | 2244 | static int | 
| Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2245 | delta_bool(PyDateTime_Delta *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2246 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 |     return (GET_TD_DAYS(self) != 0 | 
 | 2248 |         || GET_TD_SECONDS(self) != 0 | 
 | 2249 |         || GET_TD_MICROSECONDS(self) != 0); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2250 | } | 
 | 2251 |  | 
 | 2252 | static PyObject * | 
 | 2253 | delta_repr(PyDateTime_Delta *self) | 
 | 2254 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 |     if (GET_TD_MICROSECONDS(self) != 0) | 
 | 2256 |         return PyUnicode_FromFormat("%s(%d, %d, %d)", | 
 | 2257 |                                     Py_TYPE(self)->tp_name, | 
 | 2258 |                                     GET_TD_DAYS(self), | 
 | 2259 |                                     GET_TD_SECONDS(self), | 
 | 2260 |                                     GET_TD_MICROSECONDS(self)); | 
 | 2261 |     if (GET_TD_SECONDS(self) != 0) | 
 | 2262 |         return PyUnicode_FromFormat("%s(%d, %d)", | 
 | 2263 |                                     Py_TYPE(self)->tp_name, | 
 | 2264 |                                     GET_TD_DAYS(self), | 
 | 2265 |                                     GET_TD_SECONDS(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2266 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 |     return PyUnicode_FromFormat("%s(%d)", | 
 | 2268 |                                 Py_TYPE(self)->tp_name, | 
 | 2269 |                                 GET_TD_DAYS(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2270 | } | 
 | 2271 |  | 
 | 2272 | static PyObject * | 
 | 2273 | delta_str(PyDateTime_Delta *self) | 
 | 2274 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 |     int us = GET_TD_MICROSECONDS(self); | 
 | 2276 |     int seconds = GET_TD_SECONDS(self); | 
 | 2277 |     int minutes = divmod(seconds, 60, &seconds); | 
 | 2278 |     int hours = divmod(minutes, 60, &minutes); | 
 | 2279 |     int days = GET_TD_DAYS(self); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2280 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 |     if (days) { | 
 | 2282 |         if (us) | 
 | 2283 |             return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", | 
 | 2284 |                                         days, (days == 1 || days == -1) ? "" : "s", | 
 | 2285 |                                         hours, minutes, seconds, us); | 
 | 2286 |         else | 
 | 2287 |             return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", | 
 | 2288 |                                         days, (days == 1 || days == -1) ? "" : "s", | 
 | 2289 |                                         hours, minutes, seconds); | 
 | 2290 |     } else { | 
 | 2291 |         if (us) | 
 | 2292 |             return PyUnicode_FromFormat("%d:%02d:%02d.%06d", | 
 | 2293 |                                         hours, minutes, seconds, us); | 
 | 2294 |         else | 
 | 2295 |             return PyUnicode_FromFormat("%d:%02d:%02d", | 
 | 2296 |                                         hours, minutes, seconds); | 
 | 2297 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2298 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2299 | } | 
 | 2300 |  | 
| Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2301 | /* Pickle support, a simple use of __reduce__. */ | 
 | 2302 |  | 
| Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2303 | /* __getstate__ isn't exposed */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2304 | static PyObject * | 
 | 2305 | delta_getstate(PyDateTime_Delta *self) | 
 | 2306 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 |     return Py_BuildValue("iii", GET_TD_DAYS(self), | 
 | 2308 |                                 GET_TD_SECONDS(self), | 
 | 2309 |                                 GET_TD_MICROSECONDS(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2310 | } | 
 | 2311 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2312 | static PyObject * | 
| Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2313 | delta_total_seconds(PyObject *self) | 
 | 2314 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 |     PyObject *total_seconds; | 
 | 2316 |     PyObject *total_microseconds; | 
| Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2317 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 |     total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); | 
 | 2319 |     if (total_microseconds == NULL) | 
 | 2320 |         return NULL; | 
| Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2321 |  | 
| Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2322 |     total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); | 
| Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2323 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 |     Py_DECREF(total_microseconds); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 |     return total_seconds; | 
| Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2326 | } | 
 | 2327 |  | 
 | 2328 | static PyObject * | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2329 | delta_reduce(PyDateTime_Delta* self) | 
 | 2330 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 |     return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2332 | } | 
 | 2333 |  | 
 | 2334 | #define OFFSET(field)  offsetof(PyDateTime_Delta, field) | 
 | 2335 |  | 
 | 2336 | static PyMemberDef delta_members[] = { | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2337 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 |     {"days",         T_INT, OFFSET(days),         READONLY, | 
 | 2339 |      PyDoc_STR("Number of days.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2340 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 |     {"seconds",      T_INT, OFFSET(seconds),      READONLY, | 
 | 2342 |      PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2343 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 |     {"microseconds", T_INT, OFFSET(microseconds), READONLY, | 
 | 2345 |      PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, | 
 | 2346 |     {NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2347 | }; | 
 | 2348 |  | 
 | 2349 | static PyMethodDef delta_methods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 |     {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, | 
 | 2351 |      PyDoc_STR("Total seconds in the duration.")}, | 
| Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2352 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2353 |     {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, | 
 | 2354 |      PyDoc_STR("__reduce__() -> (cls, state)")}, | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2355 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 |     {NULL,      NULL}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2357 | }; | 
 | 2358 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2359 | static const char delta_doc[] = | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2360 | PyDoc_STR("Difference between two datetime values."); | 
 | 2361 |  | 
 | 2362 | static PyNumberMethods delta_as_number = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 |     delta_add,                                  /* nb_add */ | 
 | 2364 |     delta_subtract,                             /* nb_subtract */ | 
 | 2365 |     delta_multiply,                             /* nb_multiply */ | 
 | 2366 |     delta_remainder,                            /* nb_remainder */ | 
 | 2367 |     delta_divmod,                               /* nb_divmod */ | 
 | 2368 |     0,                                          /* nb_power */ | 
 | 2369 |     (unaryfunc)delta_negative,                  /* nb_negative */ | 
 | 2370 |     (unaryfunc)delta_positive,                  /* nb_positive */ | 
 | 2371 |     (unaryfunc)delta_abs,                       /* nb_absolute */ | 
 | 2372 |     (inquiry)delta_bool,                        /* nb_bool */ | 
 | 2373 |     0,                                          /*nb_invert*/ | 
 | 2374 |     0,                                          /*nb_lshift*/ | 
 | 2375 |     0,                                          /*nb_rshift*/ | 
 | 2376 |     0,                                          /*nb_and*/ | 
 | 2377 |     0,                                          /*nb_xor*/ | 
 | 2378 |     0,                                          /*nb_or*/ | 
 | 2379 |     0,                                          /*nb_int*/ | 
 | 2380 |     0,                                          /*nb_reserved*/ | 
 | 2381 |     0,                                          /*nb_float*/ | 
 | 2382 |     0,                                          /*nb_inplace_add*/ | 
 | 2383 |     0,                                          /*nb_inplace_subtract*/ | 
 | 2384 |     0,                                          /*nb_inplace_multiply*/ | 
 | 2385 |     0,                                          /*nb_inplace_remainder*/ | 
 | 2386 |     0,                                          /*nb_inplace_power*/ | 
 | 2387 |     0,                                          /*nb_inplace_lshift*/ | 
 | 2388 |     0,                                          /*nb_inplace_rshift*/ | 
 | 2389 |     0,                                          /*nb_inplace_and*/ | 
 | 2390 |     0,                                          /*nb_inplace_xor*/ | 
 | 2391 |     0,                                          /*nb_inplace_or*/ | 
 | 2392 |     delta_divide,                               /* nb_floor_divide */ | 
 | 2393 |     delta_truedivide,                           /* nb_true_divide */ | 
 | 2394 |     0,                                          /* nb_inplace_floor_divide */ | 
 | 2395 |     0,                                          /* nb_inplace_true_divide */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2396 | }; | 
 | 2397 |  | 
 | 2398 | static PyTypeObject PyDateTime_DeltaType = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 2400 |     "datetime.timedelta",                               /* tp_name */ | 
 | 2401 |     sizeof(PyDateTime_Delta),                           /* tp_basicsize */ | 
 | 2402 |     0,                                                  /* tp_itemsize */ | 
 | 2403 |     0,                                                  /* tp_dealloc */ | 
 | 2404 |     0,                                                  /* tp_print */ | 
 | 2405 |     0,                                                  /* tp_getattr */ | 
 | 2406 |     0,                                                  /* tp_setattr */ | 
 | 2407 |     0,                                                  /* tp_reserved */ | 
 | 2408 |     (reprfunc)delta_repr,                               /* tp_repr */ | 
 | 2409 |     &delta_as_number,                                   /* tp_as_number */ | 
 | 2410 |     0,                                                  /* tp_as_sequence */ | 
 | 2411 |     0,                                                  /* tp_as_mapping */ | 
 | 2412 |     (hashfunc)delta_hash,                               /* tp_hash */ | 
 | 2413 |     0,                                                  /* tp_call */ | 
 | 2414 |     (reprfunc)delta_str,                                /* tp_str */ | 
 | 2415 |     PyObject_GenericGetAttr,                            /* tp_getattro */ | 
 | 2416 |     0,                                                  /* tp_setattro */ | 
 | 2417 |     0,                                                  /* tp_as_buffer */ | 
 | 2418 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,           /* tp_flags */ | 
 | 2419 |     delta_doc,                                          /* tp_doc */ | 
 | 2420 |     0,                                                  /* tp_traverse */ | 
 | 2421 |     0,                                                  /* tp_clear */ | 
 | 2422 |     delta_richcompare,                                  /* tp_richcompare */ | 
 | 2423 |     0,                                                  /* tp_weaklistoffset */ | 
 | 2424 |     0,                                                  /* tp_iter */ | 
 | 2425 |     0,                                                  /* tp_iternext */ | 
 | 2426 |     delta_methods,                                      /* tp_methods */ | 
 | 2427 |     delta_members,                                      /* tp_members */ | 
 | 2428 |     0,                                                  /* tp_getset */ | 
 | 2429 |     0,                                                  /* tp_base */ | 
 | 2430 |     0,                                                  /* tp_dict */ | 
 | 2431 |     0,                                                  /* tp_descr_get */ | 
 | 2432 |     0,                                                  /* tp_descr_set */ | 
 | 2433 |     0,                                                  /* tp_dictoffset */ | 
 | 2434 |     0,                                                  /* tp_init */ | 
 | 2435 |     0,                                                  /* tp_alloc */ | 
 | 2436 |     delta_new,                                          /* tp_new */ | 
 | 2437 |     0,                                                  /* tp_free */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2438 | }; | 
 | 2439 |  | 
 | 2440 | /* | 
 | 2441 |  * PyDateTime_Date implementation. | 
 | 2442 |  */ | 
 | 2443 |  | 
 | 2444 | /* Accessor properties. */ | 
 | 2445 |  | 
 | 2446 | static PyObject * | 
 | 2447 | date_year(PyDateTime_Date *self, void *unused) | 
 | 2448 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 |     return PyLong_FromLong(GET_YEAR(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2450 | } | 
 | 2451 |  | 
 | 2452 | static PyObject * | 
 | 2453 | date_month(PyDateTime_Date *self, void *unused) | 
 | 2454 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 |     return PyLong_FromLong(GET_MONTH(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2456 | } | 
 | 2457 |  | 
 | 2458 | static PyObject * | 
 | 2459 | date_day(PyDateTime_Date *self, void *unused) | 
 | 2460 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 |     return PyLong_FromLong(GET_DAY(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2462 | } | 
 | 2463 |  | 
 | 2464 | static PyGetSetDef date_getset[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 |     {"year",        (getter)date_year}, | 
 | 2466 |     {"month",       (getter)date_month}, | 
 | 2467 |     {"day",         (getter)date_day}, | 
 | 2468 |     {NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2469 | }; | 
 | 2470 |  | 
 | 2471 | /* Constructors. */ | 
 | 2472 |  | 
| Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2473 | static char *date_kws[] = {"year", "month", "day", NULL}; | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2474 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2475 | static PyObject * | 
 | 2476 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) | 
 | 2477 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 |     PyObject *self = NULL; | 
 | 2479 |     PyObject *state; | 
 | 2480 |     int year; | 
 | 2481 |     int month; | 
 | 2482 |     int day; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2483 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 |     /* Check for invocation from pickle with __getstate__ state */ | 
 | 2485 |     if (PyTuple_GET_SIZE(args) == 1 && | 
 | 2486 |         PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && | 
 | 2487 |         PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && | 
 | 2488 |         MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) | 
 | 2489 |     { | 
 | 2490 |         PyDateTime_Date *me; | 
| Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2491 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2492 |         me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); | 
 | 2493 |         if (me != NULL) { | 
 | 2494 |             char *pdata = PyBytes_AS_STRING(state); | 
 | 2495 |             memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); | 
 | 2496 |             me->hashcode = -1; | 
 | 2497 |         } | 
 | 2498 |         return (PyObject *)me; | 
 | 2499 |     } | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2500 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2501 |     if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, | 
 | 2502 |                                     &year, &month, &day)) { | 
 | 2503 |         if (check_date_args(year, month, day) < 0) | 
 | 2504 |             return NULL; | 
 | 2505 |         self = new_date_ex(year, month, day, type); | 
 | 2506 |     } | 
 | 2507 |     return self; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2508 | } | 
 | 2509 |  | 
 | 2510 | /* Return new date from localtime(t). */ | 
 | 2511 | static PyObject * | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2512 | date_local_from_object(PyObject *cls, PyObject *obj) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2513 | { | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2514 |     struct tm tm; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 |     time_t t; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2516 |  | 
| Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2517 |     if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2518 |         return NULL; | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2519 |  | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 2520 |     if (_PyTime_localtime(t, &tm) != 0) | 
| Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2521 |         return NULL; | 
| Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2522 |  | 
 | 2523 |     return PyObject_CallFunction(cls, "iii", | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2524 |                                  tm.tm_year + 1900, | 
 | 2525 |                                  tm.tm_mon + 1, | 
 | 2526 |                                  tm.tm_mday); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2527 | } | 
 | 2528 |  | 
 | 2529 | /* Return new date from current time. | 
 | 2530 |  * We say this is equivalent to fromtimestamp(time.time()), and the | 
 | 2531 |  * only way to be sure of that is to *call* time.time().  That's not | 
 | 2532 |  * generally the same as calling C's time. | 
 | 2533 |  */ | 
 | 2534 | static PyObject * | 
 | 2535 | date_today(PyObject *cls, PyObject *dummy) | 
 | 2536 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2537 |     PyObject *time; | 
 | 2538 |     PyObject *result; | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2539 |     _Py_IDENTIFIER(fromtimestamp); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2540 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 |     time = time_time(); | 
 | 2542 |     if (time == NULL) | 
 | 2543 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2544 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 |     /* Note well:  today() is a class method, so this may not call | 
 | 2546 |      * date.fromtimestamp.  For example, it may call | 
 | 2547 |      * datetime.fromtimestamp.  That's why we need all the accuracy | 
 | 2548 |      * time.time() delivers; if someone were gonzo about optimization, | 
 | 2549 |      * date.today() could get away with plain C time(). | 
 | 2550 |      */ | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2551 |     result = _PyObject_CallMethodId(cls, &PyId_fromtimestamp, "O", time); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2552 |     Py_DECREF(time); | 
 | 2553 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2554 | } | 
 | 2555 |  | 
 | 2556 | /* Return new date from given timestamp (Python timestamp -- a double). */ | 
 | 2557 | static PyObject * | 
 | 2558 | date_fromtimestamp(PyObject *cls, PyObject *args) | 
 | 2559 | { | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2560 |     PyObject *timestamp; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2562 |  | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2563 |     if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) | 
 | 2564 |         result = date_local_from_object(cls, timestamp); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2565 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2566 | } | 
 | 2567 |  | 
 | 2568 | /* Return new date from proleptic Gregorian ordinal.  Raises ValueError if | 
 | 2569 |  * the ordinal is out of range. | 
 | 2570 |  */ | 
 | 2571 | static PyObject * | 
 | 2572 | date_fromordinal(PyObject *cls, PyObject *args) | 
 | 2573 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 |     PyObject *result = NULL; | 
 | 2575 |     int ordinal; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2576 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2577 |     if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { | 
 | 2578 |         int year; | 
 | 2579 |         int month; | 
 | 2580 |         int day; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2581 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 |         if (ordinal < 1) | 
 | 2583 |             PyErr_SetString(PyExc_ValueError, "ordinal must be " | 
 | 2584 |                                               ">= 1"); | 
 | 2585 |         else { | 
 | 2586 |             ord_to_ymd(ordinal, &year, &month, &day); | 
 | 2587 |             result = PyObject_CallFunction(cls, "iii", | 
 | 2588 |                                            year, month, day); | 
 | 2589 |         } | 
 | 2590 |     } | 
 | 2591 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2592 | } | 
 | 2593 |  | 
 | 2594 | /* | 
 | 2595 |  * Date arithmetic. | 
 | 2596 |  */ | 
 | 2597 |  | 
 | 2598 | /* date + timedelta -> date.  If arg negate is true, subtract the timedelta | 
 | 2599 |  * instead. | 
 | 2600 |  */ | 
 | 2601 | static PyObject * | 
 | 2602 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) | 
 | 2603 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 |     PyObject *result = NULL; | 
 | 2605 |     int year = GET_YEAR(date); | 
 | 2606 |     int month = GET_MONTH(date); | 
 | 2607 |     int deltadays = GET_TD_DAYS(delta); | 
 | 2608 |     /* C-level overflow is impossible because |deltadays| < 1e9. */ | 
 | 2609 |     int day = GET_DAY(date) + (negate ? -deltadays : deltadays); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2610 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2611 |     if (normalize_date(&year, &month, &day) >= 0) | 
 | 2612 |         result = new_date(year, month, day); | 
 | 2613 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2614 | } | 
 | 2615 |  | 
 | 2616 | static PyObject * | 
 | 2617 | date_add(PyObject *left, PyObject *right) | 
 | 2618 | { | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2619 |     if (PyDateTime_Check(left) || PyDateTime_Check(right)) | 
 | 2620 |         Py_RETURN_NOTIMPLEMENTED; | 
 | 2621 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 |     if (PyDate_Check(left)) { | 
 | 2623 |         /* date + ??? */ | 
 | 2624 |         if (PyDelta_Check(right)) | 
 | 2625 |             /* date + delta */ | 
 | 2626 |             return add_date_timedelta((PyDateTime_Date *) left, | 
 | 2627 |                                       (PyDateTime_Delta *) right, | 
 | 2628 |                                       0); | 
 | 2629 |     } | 
 | 2630 |     else { | 
 | 2631 |         /* ??? + date | 
 | 2632 |          * 'right' must be one of us, or we wouldn't have been called | 
 | 2633 |          */ | 
 | 2634 |         if (PyDelta_Check(left)) | 
 | 2635 |             /* delta + date */ | 
 | 2636 |             return add_date_timedelta((PyDateTime_Date *) right, | 
 | 2637 |                                       (PyDateTime_Delta *) left, | 
 | 2638 |                                       0); | 
 | 2639 |     } | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2640 |     Py_RETURN_NOTIMPLEMENTED; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2641 | } | 
 | 2642 |  | 
 | 2643 | static PyObject * | 
 | 2644 | date_subtract(PyObject *left, PyObject *right) | 
 | 2645 | { | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2646 |     if (PyDateTime_Check(left) || PyDateTime_Check(right)) | 
 | 2647 |         Py_RETURN_NOTIMPLEMENTED; | 
 | 2648 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 |     if (PyDate_Check(left)) { | 
 | 2650 |         if (PyDate_Check(right)) { | 
 | 2651 |             /* date - date */ | 
 | 2652 |             int left_ord = ymd_to_ord(GET_YEAR(left), | 
 | 2653 |                                       GET_MONTH(left), | 
 | 2654 |                                       GET_DAY(left)); | 
 | 2655 |             int right_ord = ymd_to_ord(GET_YEAR(right), | 
 | 2656 |                                        GET_MONTH(right), | 
 | 2657 |                                        GET_DAY(right)); | 
 | 2658 |             return new_delta(left_ord - right_ord, 0, 0, 0); | 
 | 2659 |         } | 
 | 2660 |         if (PyDelta_Check(right)) { | 
 | 2661 |             /* date - delta */ | 
 | 2662 |             return add_date_timedelta((PyDateTime_Date *) left, | 
 | 2663 |                                       (PyDateTime_Delta *) right, | 
 | 2664 |                                       1); | 
 | 2665 |         } | 
 | 2666 |     } | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2667 |     Py_RETURN_NOTIMPLEMENTED; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2668 | } | 
 | 2669 |  | 
 | 2670 |  | 
 | 2671 | /* Various ways to turn a date into a string. */ | 
 | 2672 |  | 
 | 2673 | static PyObject * | 
 | 2674 | date_repr(PyDateTime_Date *self) | 
 | 2675 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 |     return PyUnicode_FromFormat("%s(%d, %d, %d)", | 
 | 2677 |                                 Py_TYPE(self)->tp_name, | 
 | 2678 |                                 GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2679 | } | 
 | 2680 |  | 
 | 2681 | static PyObject * | 
 | 2682 | date_isoformat(PyDateTime_Date *self) | 
 | 2683 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 |     return PyUnicode_FromFormat("%04d-%02d-%02d", | 
 | 2685 |                                 GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2686 | } | 
 | 2687 |  | 
| Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2688 | /* str() calls the appropriate isoformat() method. */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2689 | static PyObject * | 
 | 2690 | date_str(PyDateTime_Date *self) | 
 | 2691 | { | 
| Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 2692 |     return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2693 | } | 
 | 2694 |  | 
 | 2695 |  | 
 | 2696 | static PyObject * | 
 | 2697 | date_ctime(PyDateTime_Date *self) | 
 | 2698 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 |     return format_ctime(self, 0, 0, 0); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2700 | } | 
 | 2701 |  | 
 | 2702 | static PyObject * | 
 | 2703 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) | 
 | 2704 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 |     /* This method can be inherited, and needs to call the | 
 | 2706 |      * timetuple() method appropriate to self's class. | 
 | 2707 |      */ | 
 | 2708 |     PyObject *result; | 
 | 2709 |     PyObject *tuple; | 
 | 2710 |     PyObject *format; | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2711 |     _Py_IDENTIFIER(timetuple); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2712 |     static char *keywords[] = {"format", NULL}; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2713 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2714 |     if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, | 
 | 2715 |                                       &format)) | 
 | 2716 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2717 |  | 
| Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 2718 |     tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, NULL); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 |     if (tuple == NULL) | 
 | 2720 |         return NULL; | 
 | 2721 |     result = wrap_strftime((PyObject *)self, format, tuple, | 
 | 2722 |                            (PyObject *)self); | 
 | 2723 |     Py_DECREF(tuple); | 
 | 2724 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2725 | } | 
 | 2726 |  | 
| Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2727 | static PyObject * | 
 | 2728 | date_format(PyDateTime_Date *self, PyObject *args) | 
 | 2729 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 |     PyObject *format; | 
| Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2731 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 |     if (!PyArg_ParseTuple(args, "U:__format__", &format)) | 
 | 2733 |         return NULL; | 
| Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2734 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 |     /* if the format is zero length, return str(self) */ | 
| Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2736 |     if (PyUnicode_GetLength(format) == 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 |         return PyObject_Str((PyObject *)self); | 
| Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2738 |  | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2739 |     return _PyObject_CallMethodId((PyObject *)self, &PyId_strftime, "O", format); | 
| Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2740 | } | 
 | 2741 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2742 | /* ISO methods. */ | 
 | 2743 |  | 
 | 2744 | static PyObject * | 
 | 2745 | date_isoweekday(PyDateTime_Date *self) | 
 | 2746 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2747 |     int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2748 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2749 |     return PyLong_FromLong(dow + 1); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2750 | } | 
 | 2751 |  | 
 | 2752 | static PyObject * | 
 | 2753 | date_isocalendar(PyDateTime_Date *self) | 
 | 2754 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 |     int  year         = GET_YEAR(self); | 
 | 2756 |     int  week1_monday = iso_week1_monday(year); | 
 | 2757 |     int today         = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); | 
 | 2758 |     int  week; | 
 | 2759 |     int  day; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2760 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 |     week = divmod(today - week1_monday, 7, &day); | 
 | 2762 |     if (week < 0) { | 
 | 2763 |         --year; | 
 | 2764 |         week1_monday = iso_week1_monday(year); | 
 | 2765 |         week = divmod(today - week1_monday, 7, &day); | 
 | 2766 |     } | 
 | 2767 |     else if (week >= 52 && today >= iso_week1_monday(year + 1)) { | 
 | 2768 |         ++year; | 
 | 2769 |         week = 0; | 
 | 2770 |     } | 
 | 2771 |     return Py_BuildValue("iii", year, week + 1, day + 1); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2772 | } | 
 | 2773 |  | 
 | 2774 | /* Miscellaneous methods. */ | 
 | 2775 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2776 | static PyObject * | 
| Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2777 | date_richcompare(PyObject *self, PyObject *other, int op) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2778 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 |     if (PyDate_Check(other)) { | 
 | 2780 |         int diff = memcmp(((PyDateTime_Date *)self)->data, | 
 | 2781 |                           ((PyDateTime_Date *)other)->data, | 
 | 2782 |                           _PyDateTime_DATE_DATASIZE); | 
 | 2783 |         return diff_to_bool(diff, op); | 
 | 2784 |     } | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2785 |     else | 
 | 2786 |         Py_RETURN_NOTIMPLEMENTED; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2787 | } | 
 | 2788 |  | 
 | 2789 | static PyObject * | 
 | 2790 | date_timetuple(PyDateTime_Date *self) | 
 | 2791 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 |     return build_struct_time(GET_YEAR(self), | 
 | 2793 |                              GET_MONTH(self), | 
 | 2794 |                              GET_DAY(self), | 
 | 2795 |                              0, 0, 0, -1); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2796 | } | 
 | 2797 |  | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2798 | static PyObject * | 
 | 2799 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) | 
 | 2800 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2801 |     PyObject *clone; | 
 | 2802 |     PyObject *tuple; | 
 | 2803 |     int year = GET_YEAR(self); | 
 | 2804 |     int month = GET_MONTH(self); | 
 | 2805 |     int day = GET_DAY(self); | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2806 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 |     if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, | 
 | 2808 |                                       &year, &month, &day)) | 
 | 2809 |         return NULL; | 
 | 2810 |     tuple = Py_BuildValue("iii", year, month, day); | 
 | 2811 |     if (tuple == NULL) | 
 | 2812 |         return NULL; | 
 | 2813 |     clone = date_new(Py_TYPE(self), tuple, NULL); | 
 | 2814 |     Py_DECREF(tuple); | 
 | 2815 |     return clone; | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2816 | } | 
 | 2817 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2818 | static Py_hash_t | 
| Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2819 | generic_hash(unsigned char *data, int len) | 
 | 2820 | { | 
| Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 2821 |     return _Py_HashBytes(data, len); | 
| Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2822 | } | 
 | 2823 |  | 
 | 2824 |  | 
 | 2825 | static PyObject *date_getstate(PyDateTime_Date *self); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2826 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2827 | static Py_hash_t | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2828 | date_hash(PyDateTime_Date *self) | 
 | 2829 | { | 
| Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 2830 |     if (self->hashcode == -1) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 |         self->hashcode = generic_hash( | 
 | 2832 |             (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); | 
| Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 2833 |     } | 
| Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2834 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 |     return self->hashcode; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2836 | } | 
 | 2837 |  | 
 | 2838 | static PyObject * | 
 | 2839 | date_toordinal(PyDateTime_Date *self) | 
 | 2840 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 |     return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), | 
 | 2842 |                                      GET_DAY(self))); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2843 | } | 
 | 2844 |  | 
 | 2845 | static PyObject * | 
 | 2846 | date_weekday(PyDateTime_Date *self) | 
 | 2847 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 |     int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2849 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 |     return PyLong_FromLong(dow); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2851 | } | 
 | 2852 |  | 
| Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2853 | /* Pickle support, a simple use of __reduce__. */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2854 |  | 
| Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2855 | /* __getstate__ isn't exposed */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2856 | static PyObject * | 
| Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2857 | date_getstate(PyDateTime_Date *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2858 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 |     PyObject* field; | 
 | 2860 |     field = PyBytes_FromStringAndSize((char*)self->data, | 
 | 2861 |                                        _PyDateTime_DATE_DATASIZE); | 
 | 2862 |     return Py_BuildValue("(N)", field); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2863 | } | 
 | 2864 |  | 
 | 2865 | static PyObject * | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2866 | date_reduce(PyDateTime_Date *self, PyObject *arg) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2867 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 |     return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2869 | } | 
 | 2870 |  | 
 | 2871 | static PyMethodDef date_methods[] = { | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2872 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 |     /* Class methods: */ | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2874 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 |     {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | | 
 | 2876 |                                                        METH_CLASS, | 
 | 2877 |      PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " | 
 | 2878 |                "time.time()).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2879 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 |     {"fromordinal", (PyCFunction)date_fromordinal,      METH_VARARGS | | 
 | 2881 |                                                     METH_CLASS, | 
 | 2882 |      PyDoc_STR("int -> date corresponding to a proleptic Gregorian " | 
 | 2883 |                "ordinal.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2884 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 |     {"today",         (PyCFunction)date_today,   METH_NOARGS | METH_CLASS, | 
 | 2886 |      PyDoc_STR("Current date or datetime:  same as " | 
 | 2887 |                "self.__class__.fromtimestamp(time.time()).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2888 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 |     /* Instance methods: */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2890 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 |     {"ctime",       (PyCFunction)date_ctime,        METH_NOARGS, | 
 | 2892 |      PyDoc_STR("Return ctime() style string.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2893 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 |     {"strftime",        (PyCFunction)date_strftime,     METH_VARARGS | METH_KEYWORDS, | 
 | 2895 |      PyDoc_STR("format -> strftime() style string.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2896 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 |     {"__format__",      (PyCFunction)date_format,       METH_VARARGS, | 
 | 2898 |      PyDoc_STR("Formats self with strftime.")}, | 
| Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2899 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 |     {"timetuple",   (PyCFunction)date_timetuple,    METH_NOARGS, | 
 | 2901 |      PyDoc_STR("Return time tuple, compatible with time.localtime().")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2902 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 |     {"isocalendar", (PyCFunction)date_isocalendar,  METH_NOARGS, | 
 | 2904 |      PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " | 
 | 2905 |                "weekday.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2906 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 |     {"isoformat",   (PyCFunction)date_isoformat,        METH_NOARGS, | 
 | 2908 |      PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2909 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2910 |     {"isoweekday",  (PyCFunction)date_isoweekday,   METH_NOARGS, | 
 | 2911 |      PyDoc_STR("Return the day of the week represented by the date.\n" | 
 | 2912 |                "Monday == 1 ... Sunday == 7")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2913 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 |     {"toordinal",   (PyCFunction)date_toordinal,    METH_NOARGS, | 
 | 2915 |      PyDoc_STR("Return proleptic Gregorian ordinal.  January 1 of year " | 
 | 2916 |                "1 is day 1.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2917 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 |     {"weekday",     (PyCFunction)date_weekday,      METH_NOARGS, | 
 | 2919 |      PyDoc_STR("Return the day of the week represented by the date.\n" | 
 | 2920 |                "Monday == 0 ... Sunday == 6")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2921 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 |     {"replace",     (PyCFunction)date_replace,      METH_VARARGS | METH_KEYWORDS, | 
 | 2923 |      PyDoc_STR("Return date with new specified fields.")}, | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2924 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 |     {"__reduce__", (PyCFunction)date_reduce,        METH_NOARGS, | 
 | 2926 |      PyDoc_STR("__reduce__() -> (cls, state)")}, | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2927 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 |     {NULL,      NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2929 | }; | 
 | 2930 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2931 | static const char date_doc[] = | 
| Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2932 | PyDoc_STR("date(year, month, day) --> date object"); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2933 |  | 
 | 2934 | static PyNumberMethods date_as_number = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 |     date_add,                                           /* nb_add */ | 
 | 2936 |     date_subtract,                                      /* nb_subtract */ | 
 | 2937 |     0,                                                  /* nb_multiply */ | 
 | 2938 |     0,                                                  /* nb_remainder */ | 
 | 2939 |     0,                                                  /* nb_divmod */ | 
 | 2940 |     0,                                                  /* nb_power */ | 
 | 2941 |     0,                                                  /* nb_negative */ | 
 | 2942 |     0,                                                  /* nb_positive */ | 
 | 2943 |     0,                                                  /* nb_absolute */ | 
 | 2944 |     0,                                                  /* nb_bool */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2945 | }; | 
 | 2946 |  | 
 | 2947 | static PyTypeObject PyDateTime_DateType = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2948 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 2949 |     "datetime.date",                                    /* tp_name */ | 
 | 2950 |     sizeof(PyDateTime_Date),                            /* tp_basicsize */ | 
 | 2951 |     0,                                                  /* tp_itemsize */ | 
 | 2952 |     0,                                                  /* tp_dealloc */ | 
 | 2953 |     0,                                                  /* tp_print */ | 
 | 2954 |     0,                                                  /* tp_getattr */ | 
 | 2955 |     0,                                                  /* tp_setattr */ | 
 | 2956 |     0,                                                  /* tp_reserved */ | 
 | 2957 |     (reprfunc)date_repr,                                /* tp_repr */ | 
 | 2958 |     &date_as_number,                                    /* tp_as_number */ | 
 | 2959 |     0,                                                  /* tp_as_sequence */ | 
 | 2960 |     0,                                                  /* tp_as_mapping */ | 
 | 2961 |     (hashfunc)date_hash,                                /* tp_hash */ | 
 | 2962 |     0,                                                  /* tp_call */ | 
 | 2963 |     (reprfunc)date_str,                                 /* tp_str */ | 
 | 2964 |     PyObject_GenericGetAttr,                            /* tp_getattro */ | 
 | 2965 |     0,                                                  /* tp_setattro */ | 
 | 2966 |     0,                                                  /* tp_as_buffer */ | 
 | 2967 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,           /* tp_flags */ | 
 | 2968 |     date_doc,                                           /* tp_doc */ | 
 | 2969 |     0,                                                  /* tp_traverse */ | 
 | 2970 |     0,                                                  /* tp_clear */ | 
 | 2971 |     date_richcompare,                                   /* tp_richcompare */ | 
 | 2972 |     0,                                                  /* tp_weaklistoffset */ | 
 | 2973 |     0,                                                  /* tp_iter */ | 
 | 2974 |     0,                                                  /* tp_iternext */ | 
 | 2975 |     date_methods,                                       /* tp_methods */ | 
 | 2976 |     0,                                                  /* tp_members */ | 
 | 2977 |     date_getset,                                        /* tp_getset */ | 
 | 2978 |     0,                                                  /* tp_base */ | 
 | 2979 |     0,                                                  /* tp_dict */ | 
 | 2980 |     0,                                                  /* tp_descr_get */ | 
 | 2981 |     0,                                                  /* tp_descr_set */ | 
 | 2982 |     0,                                                  /* tp_dictoffset */ | 
 | 2983 |     0,                                                  /* tp_init */ | 
 | 2984 |     0,                                                  /* tp_alloc */ | 
 | 2985 |     date_new,                                           /* tp_new */ | 
 | 2986 |     0,                                                  /* tp_free */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2987 | }; | 
 | 2988 |  | 
 | 2989 | /* | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2990 |  * PyDateTime_TZInfo implementation. | 
 | 2991 |  */ | 
 | 2992 |  | 
 | 2993 | /* This is a pure abstract base class, so doesn't do anything beyond | 
 | 2994 |  * raising NotImplemented exceptions.  Real tzinfo classes need | 
 | 2995 |  * to derive from this.  This is mostly for clarity, and for efficiency in | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 2996 |  * datetime and time constructors (their tzinfo arguments need to | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2997 |  * be subclasses of this tzinfo class, which is easy and quick to check). | 
 | 2998 |  * | 
 | 2999 |  * Note:  For reasons having to do with pickling of subclasses, we have | 
 | 3000 |  * to allow tzinfo objects to be instantiated.  This wasn't an issue | 
 | 3001 |  * in the Python implementation (__init__() could raise NotImplementedError | 
 | 3002 |  * there without ill effect), but doing so in the C implementation hit a | 
 | 3003 |  * brick wall. | 
 | 3004 |  */ | 
 | 3005 |  | 
 | 3006 | static PyObject * | 
 | 3007 | tzinfo_nogo(const char* methodname) | 
 | 3008 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3009 |     PyErr_Format(PyExc_NotImplementedError, | 
 | 3010 |                  "a tzinfo subclass must implement %s()", | 
 | 3011 |                  methodname); | 
 | 3012 |     return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3013 | } | 
 | 3014 |  | 
 | 3015 | /* Methods.  A subclass must implement these. */ | 
 | 3016 |  | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3017 | static PyObject * | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3018 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) | 
 | 3019 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3020 |     return tzinfo_nogo("tzname"); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3021 | } | 
 | 3022 |  | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3023 | static PyObject * | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3024 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) | 
 | 3025 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 |     return tzinfo_nogo("utcoffset"); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3027 | } | 
 | 3028 |  | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3029 | static PyObject * | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3030 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) | 
 | 3031 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 |     return tzinfo_nogo("dst"); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3033 | } | 
 | 3034 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3035 |  | 
 | 3036 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, | 
 | 3037 |                                         PyDateTime_Delta *delta, | 
 | 3038 |                                         int factor); | 
 | 3039 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); | 
 | 3040 | static PyObject *datetime_dst(PyObject *self, PyObject *); | 
 | 3041 |  | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3042 | static PyObject * | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3043 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3044 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3045 |     PyObject *result = NULL; | 
 | 3046 |     PyObject *off = NULL, *dst = NULL; | 
 | 3047 |     PyDateTime_Delta *delta = NULL; | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3048 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3049 |     if (!PyDateTime_Check(dt)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 |         PyErr_SetString(PyExc_TypeError, | 
 | 3051 |                         "fromutc: argument must be a datetime"); | 
 | 3052 |         return NULL; | 
 | 3053 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3054 |     if (GET_DT_TZINFO(dt) != (PyObject *)self) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3055 |         PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " | 
 | 3056 |                         "is not self"); | 
 | 3057 |         return NULL; | 
 | 3058 |     } | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3059 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3060 |     off = datetime_utcoffset(dt, NULL); | 
 | 3061 |     if (off == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 |         return NULL; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3063 |     if (off == Py_None) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 |         PyErr_SetString(PyExc_ValueError, "fromutc: non-None " | 
 | 3065 |                         "utcoffset() result required"); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3066 |         goto Fail; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3067 |     } | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3068 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3069 |     dst = datetime_dst(dt, NULL); | 
 | 3070 |     if (dst == NULL) | 
 | 3071 |         goto Fail; | 
 | 3072 |     if (dst == Py_None) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 |         PyErr_SetString(PyExc_ValueError, "fromutc: non-None " | 
 | 3074 |                         "dst() result required"); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3075 |         goto Fail; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 |     } | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3077 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3078 |     delta = (PyDateTime_Delta *)delta_subtract(off, dst); | 
 | 3079 |     if (delta == NULL) | 
 | 3080 |         goto Fail; | 
 | 3081 |     result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 |     if (result == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 |         goto Fail; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3084 |  | 
 | 3085 |     Py_DECREF(dst); | 
 | 3086 |     dst = call_dst(GET_DT_TZINFO(dt), result); | 
 | 3087 |     if (dst == NULL) | 
 | 3088 |         goto Fail; | 
 | 3089 |     if (dst == Py_None) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 |         goto Inconsistent; | 
| Alexander Belopolsky | c79447b | 2015-09-27 21:41:55 -0400 | [diff] [blame] | 3091 |     if (delta_bool((PyDateTime_Delta *)dst) != 0) { | 
| Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3092 |         Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, | 
| Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 3093 |                                                  (PyDateTime_Delta *)dst, 1)); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3094 |         if (result == NULL) | 
 | 3095 |             goto Fail; | 
 | 3096 |     } | 
 | 3097 |     Py_DECREF(delta); | 
 | 3098 |     Py_DECREF(dst); | 
 | 3099 |     Py_DECREF(off); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 |     return result; | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3101 |  | 
 | 3102 | Inconsistent: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 |     PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" | 
 | 3104 |                     "inconsistent results; cannot convert"); | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3105 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3106 |     /* fall thru to failure */ | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3107 | Fail: | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3108 |     Py_XDECREF(off); | 
 | 3109 |     Py_XDECREF(dst); | 
 | 3110 |     Py_XDECREF(delta); | 
 | 3111 |     Py_XDECREF(result); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 |     return NULL; | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3113 | } | 
 | 3114 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3115 | /* | 
 | 3116 |  * Pickle support.  This is solely so that tzinfo subclasses can use | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3117 |  * pickling -- tzinfo itself is supposed to be uninstantiable. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3118 |  */ | 
 | 3119 |  | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3120 | static PyObject * | 
 | 3121 | tzinfo_reduce(PyObject *self) | 
 | 3122 | { | 
| Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3123 |     PyObject *args, *state; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 |     PyObject *getinitargs, *getstate; | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3125 |     _Py_IDENTIFIER(__getinitargs__); | 
 | 3126 |     _Py_IDENTIFIER(__getstate__); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3127 |  | 
| Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3128 |     getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 |     if (getinitargs != NULL) { | 
| Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3130 |         args = _PyObject_CallNoArg(getinitargs); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 |         Py_DECREF(getinitargs); | 
 | 3132 |         if (args == NULL) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 |             return NULL; | 
 | 3134 |         } | 
 | 3135 |     } | 
 | 3136 |     else { | 
 | 3137 |         PyErr_Clear(); | 
| Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3138 |  | 
 | 3139 |         args = PyTuple_New(0); | 
 | 3140 |         if (args == NULL) { | 
 | 3141 |             return NULL; | 
 | 3142 |         } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 |     } | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3144 |  | 
| Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3145 |     getstate = _PyObject_GetAttrId(self, &PyId___getstate__); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 |     if (getstate != NULL) { | 
| Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3147 |         state = _PyObject_CallNoArg(getstate); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 |         Py_DECREF(getstate); | 
 | 3149 |         if (state == NULL) { | 
 | 3150 |             Py_DECREF(args); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 |             return NULL; | 
 | 3152 |         } | 
 | 3153 |     } | 
 | 3154 |     else { | 
 | 3155 |         PyObject **dictptr; | 
 | 3156 |         PyErr_Clear(); | 
 | 3157 |         state = Py_None; | 
 | 3158 |         dictptr = _PyObject_GetDictPtr(self); | 
| Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3159 |         if (dictptr && *dictptr && PyDict_Size(*dictptr)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 |             state = *dictptr; | 
| Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3161 |         } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 |         Py_INCREF(state); | 
 | 3163 |     } | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3164 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 |     if (state == Py_None) { | 
 | 3166 |         Py_DECREF(state); | 
 | 3167 |         return Py_BuildValue("(ON)", Py_TYPE(self), args); | 
 | 3168 |     } | 
 | 3169 |     else | 
 | 3170 |         return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3171 | } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3172 |  | 
 | 3173 | static PyMethodDef tzinfo_methods[] = { | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3174 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 |     {"tzname",          (PyCFunction)tzinfo_tzname,             METH_O, | 
 | 3176 |      PyDoc_STR("datetime -> string name of time zone.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3177 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 |     {"utcoffset",       (PyCFunction)tzinfo_utcoffset,          METH_O, | 
| Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3179 |      PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " | 
 | 3180 |            "values indicating West of UTC")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3181 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 |     {"dst",             (PyCFunction)tzinfo_dst,                METH_O, | 
 | 3183 |      PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3184 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 |     {"fromutc",         (PyCFunction)tzinfo_fromutc,            METH_O, | 
| Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3186 |      PyDoc_STR("datetime in UTC -> datetime in local time.")}, | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3187 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 |     {"__reduce__",  (PyCFunction)tzinfo_reduce,             METH_NOARGS, | 
 | 3189 |      PyDoc_STR("-> (cls, state)")}, | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3190 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3191 |     {NULL, NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3192 | }; | 
 | 3193 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3194 | static const char tzinfo_doc[] = | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3195 | PyDoc_STR("Abstract base class for time zone info objects."); | 
 | 3196 |  | 
| Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3197 | static PyTypeObject PyDateTime_TZInfoType = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 3199 |     "datetime.tzinfo",                          /* tp_name */ | 
 | 3200 |     sizeof(PyDateTime_TZInfo),                  /* tp_basicsize */ | 
 | 3201 |     0,                                          /* tp_itemsize */ | 
 | 3202 |     0,                                          /* tp_dealloc */ | 
 | 3203 |     0,                                          /* tp_print */ | 
 | 3204 |     0,                                          /* tp_getattr */ | 
 | 3205 |     0,                                          /* tp_setattr */ | 
 | 3206 |     0,                                          /* tp_reserved */ | 
 | 3207 |     0,                                          /* tp_repr */ | 
 | 3208 |     0,                                          /* tp_as_number */ | 
 | 3209 |     0,                                          /* tp_as_sequence */ | 
 | 3210 |     0,                                          /* tp_as_mapping */ | 
 | 3211 |     0,                                          /* tp_hash */ | 
 | 3212 |     0,                                          /* tp_call */ | 
 | 3213 |     0,                                          /* tp_str */ | 
 | 3214 |     PyObject_GenericGetAttr,                    /* tp_getattro */ | 
 | 3215 |     0,                                          /* tp_setattro */ | 
 | 3216 |     0,                                          /* tp_as_buffer */ | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3217 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 |     tzinfo_doc,                                 /* tp_doc */ | 
 | 3219 |     0,                                          /* tp_traverse */ | 
 | 3220 |     0,                                          /* tp_clear */ | 
 | 3221 |     0,                                          /* tp_richcompare */ | 
 | 3222 |     0,                                          /* tp_weaklistoffset */ | 
 | 3223 |     0,                                          /* tp_iter */ | 
 | 3224 |     0,                                          /* tp_iternext */ | 
 | 3225 |     tzinfo_methods,                             /* tp_methods */ | 
 | 3226 |     0,                                          /* tp_members */ | 
 | 3227 |     0,                                          /* tp_getset */ | 
 | 3228 |     0,                                          /* tp_base */ | 
 | 3229 |     0,                                          /* tp_dict */ | 
 | 3230 |     0,                                          /* tp_descr_get */ | 
 | 3231 |     0,                                          /* tp_descr_set */ | 
 | 3232 |     0,                                          /* tp_dictoffset */ | 
 | 3233 |     0,                                          /* tp_init */ | 
 | 3234 |     0,                                          /* tp_alloc */ | 
 | 3235 |     PyType_GenericNew,                          /* tp_new */ | 
 | 3236 |     0,                                          /* tp_free */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3237 | }; | 
 | 3238 |  | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3239 | static char *timezone_kws[] = {"offset", "name", NULL}; | 
 | 3240 |  | 
 | 3241 | static PyObject * | 
 | 3242 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) | 
 | 3243 | { | 
 | 3244 |     PyObject *offset; | 
 | 3245 |     PyObject *name = NULL; | 
 | 3246 |     if (PyArg_ParseTupleAndKeywords(args, kw, "O!|O!:timezone", timezone_kws, | 
 | 3247 |                                     &PyDateTime_DeltaType, &offset, | 
 | 3248 |                                     &PyUnicode_Type, &name)) | 
 | 3249 |         return new_timezone(offset, name); | 
 | 3250 |  | 
 | 3251 |     return NULL; | 
 | 3252 | } | 
 | 3253 |  | 
 | 3254 | static void | 
 | 3255 | timezone_dealloc(PyDateTime_TimeZone *self) | 
 | 3256 | { | 
 | 3257 |     Py_CLEAR(self->offset); | 
 | 3258 |     Py_CLEAR(self->name); | 
 | 3259 |     Py_TYPE(self)->tp_free((PyObject *)self); | 
 | 3260 | } | 
 | 3261 |  | 
 | 3262 | static PyObject * | 
 | 3263 | timezone_richcompare(PyDateTime_TimeZone *self, | 
 | 3264 |                      PyDateTime_TimeZone *other, int op) | 
 | 3265 | { | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3266 |     if (op != Py_EQ && op != Py_NE) | 
 | 3267 |         Py_RETURN_NOTIMPLEMENTED; | 
| Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3268 |     if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { | 
| Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 3269 |         if (op == Py_EQ) | 
 | 3270 |             Py_RETURN_FALSE; | 
 | 3271 |         else | 
 | 3272 |             Py_RETURN_TRUE; | 
| Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3273 |     } | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3274 |     return delta_richcompare(self->offset, other->offset, op); | 
 | 3275 | } | 
 | 3276 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3277 | static Py_hash_t | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3278 | timezone_hash(PyDateTime_TimeZone *self) | 
 | 3279 | { | 
 | 3280 |     return delta_hash((PyDateTime_Delta *)self->offset); | 
 | 3281 | } | 
 | 3282 |  | 
 | 3283 | /* Check argument type passed to tzname, utcoffset, or dst methods. | 
 | 3284 |    Returns 0 for good argument.  Returns -1 and sets exception info | 
 | 3285 |    otherwise. | 
 | 3286 |  */ | 
 | 3287 | static int | 
 | 3288 | _timezone_check_argument(PyObject *dt, const char *meth) | 
 | 3289 | { | 
 | 3290 |     if (dt == Py_None || PyDateTime_Check(dt)) | 
 | 3291 |         return 0; | 
 | 3292 |     PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" | 
 | 3293 |                  " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); | 
 | 3294 |     return -1; | 
 | 3295 | } | 
 | 3296 |  | 
 | 3297 | static PyObject * | 
| Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3298 | timezone_repr(PyDateTime_TimeZone *self) | 
 | 3299 | { | 
 | 3300 |     /* Note that although timezone is not subclassable, it is convenient | 
 | 3301 |        to use Py_TYPE(self)->tp_name here. */ | 
 | 3302 |     const char *type_name = Py_TYPE(self)->tp_name; | 
 | 3303 |  | 
 | 3304 |     if (((PyObject *)self) == PyDateTime_TimeZone_UTC) | 
 | 3305 |         return PyUnicode_FromFormat("%s.utc", type_name); | 
 | 3306 |  | 
 | 3307 |     if (self->name == NULL) | 
 | 3308 |         return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); | 
 | 3309 |  | 
 | 3310 |     return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, | 
 | 3311 |                                 self->name); | 
 | 3312 | } | 
 | 3313 |  | 
 | 3314 |  | 
 | 3315 | static PyObject * | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3316 | timezone_str(PyDateTime_TimeZone *self) | 
 | 3317 | { | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3318 |     int hours, minutes, seconds; | 
 | 3319 |     PyObject *offset; | 
 | 3320 |     char sign; | 
 | 3321 |  | 
 | 3322 |     if (self->name != NULL) { | 
 | 3323 |         Py_INCREF(self->name); | 
 | 3324 |         return self->name; | 
 | 3325 |     } | 
| Victor Stinner | 90fd895 | 2015-09-08 00:12:49 +0200 | [diff] [blame] | 3326 |     if ((PyObject *)self == PyDateTime_TimeZone_UTC || | 
| Alexander Belopolsky | 7827a5b | 2015-09-06 13:07:21 -0400 | [diff] [blame] | 3327 |            (GET_TD_DAYS(self->offset) == 0 && | 
 | 3328 |             GET_TD_SECONDS(self->offset) == 0 && | 
 | 3329 |             GET_TD_MICROSECONDS(self->offset) == 0)) | 
 | 3330 |         return PyUnicode_FromString("UTC"); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3331 |     /* Offset is normalized, so it is negative if days < 0 */ | 
 | 3332 |     if (GET_TD_DAYS(self->offset) < 0) { | 
 | 3333 |         sign = '-'; | 
 | 3334 |         offset = delta_negative((PyDateTime_Delta *)self->offset); | 
 | 3335 |         if (offset == NULL) | 
 | 3336 |             return NULL; | 
 | 3337 |     } | 
 | 3338 |     else { | 
 | 3339 |         sign = '+'; | 
 | 3340 |         offset = self->offset; | 
 | 3341 |         Py_INCREF(offset); | 
 | 3342 |     } | 
 | 3343 |     /* Offset is not negative here. */ | 
 | 3344 |     seconds = GET_TD_SECONDS(offset); | 
 | 3345 |     Py_DECREF(offset); | 
 | 3346 |     minutes = divmod(seconds, 60, &seconds); | 
 | 3347 |     hours = divmod(minutes, 60, &minutes); | 
| Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 3348 |     /* XXX ignore sub-minute data, currently not allowed. */ | 
| Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3349 |     assert(seconds == 0); | 
 | 3350 |     return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3351 | } | 
 | 3352 |  | 
 | 3353 | static PyObject * | 
 | 3354 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) | 
 | 3355 | { | 
 | 3356 |     if (_timezone_check_argument(dt, "tzname") == -1) | 
 | 3357 |         return NULL; | 
 | 3358 |  | 
 | 3359 |     return timezone_str(self); | 
 | 3360 | } | 
 | 3361 |  | 
 | 3362 | static PyObject * | 
 | 3363 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) | 
 | 3364 | { | 
 | 3365 |     if (_timezone_check_argument(dt, "utcoffset") == -1) | 
 | 3366 |         return NULL; | 
 | 3367 |  | 
 | 3368 |     Py_INCREF(self->offset); | 
 | 3369 |     return self->offset; | 
 | 3370 | } | 
 | 3371 |  | 
 | 3372 | static PyObject * | 
 | 3373 | timezone_dst(PyObject *self, PyObject *dt) | 
 | 3374 | { | 
 | 3375 |     if (_timezone_check_argument(dt, "dst") == -1) | 
 | 3376 |         return NULL; | 
 | 3377 |  | 
 | 3378 |     Py_RETURN_NONE; | 
 | 3379 | } | 
 | 3380 |  | 
 | 3381 | static PyObject * | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3382 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) | 
 | 3383 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3384 |     if (!PyDateTime_Check(dt)) { | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3385 |         PyErr_SetString(PyExc_TypeError, | 
 | 3386 |                         "fromutc: argument must be a datetime"); | 
 | 3387 |         return NULL; | 
 | 3388 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3389 |     if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3390 |         PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " | 
 | 3391 |                         "is not self"); | 
 | 3392 |         return NULL; | 
 | 3393 |     } | 
 | 3394 |  | 
 | 3395 |     return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); | 
 | 3396 | } | 
 | 3397 |  | 
| Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3398 | static PyObject * | 
 | 3399 | timezone_getinitargs(PyDateTime_TimeZone *self) | 
 | 3400 | { | 
 | 3401 |     if (self->name == NULL) | 
 | 3402 |         return Py_BuildValue("(O)", self->offset); | 
 | 3403 |     return Py_BuildValue("(OO)", self->offset, self->name); | 
 | 3404 | } | 
 | 3405 |  | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3406 | static PyMethodDef timezone_methods[] = { | 
 | 3407 |     {"tzname", (PyCFunction)timezone_tzname, METH_O, | 
 | 3408 |      PyDoc_STR("If name is specified when timezone is created, returns the name." | 
| Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3409 |                "  Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3410 |  | 
 | 3411 |     {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, | 
| Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3412 |      PyDoc_STR("Return fixed offset.")}, | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3413 |  | 
 | 3414 |     {"dst", (PyCFunction)timezone_dst, METH_O, | 
| Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3415 |      PyDoc_STR("Return None.")}, | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3416 |  | 
 | 3417 |     {"fromutc", (PyCFunction)timezone_fromutc, METH_O, | 
 | 3418 |      PyDoc_STR("datetime in UTC -> datetime in local time.")}, | 
 | 3419 |  | 
| Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3420 |     {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, | 
 | 3421 |      PyDoc_STR("pickle support")}, | 
 | 3422 |  | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3423 |     {NULL, NULL} | 
 | 3424 | }; | 
 | 3425 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3426 | static const char timezone_doc[] = | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3427 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); | 
 | 3428 |  | 
 | 3429 | static PyTypeObject PyDateTime_TimeZoneType = { | 
 | 3430 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 3431 |     "datetime.timezone",              /* tp_name */ | 
 | 3432 |     sizeof(PyDateTime_TimeZone),      /* tp_basicsize */ | 
 | 3433 |     0,                                /* tp_itemsize */ | 
 | 3434 |     (destructor)timezone_dealloc,     /* tp_dealloc */ | 
 | 3435 |     0,                                /* tp_print */ | 
 | 3436 |     0,                                /* tp_getattr */ | 
 | 3437 |     0,                                /* tp_setattr */ | 
 | 3438 |     0,                                /* tp_reserved */ | 
| Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3439 |     (reprfunc)timezone_repr,          /* tp_repr */ | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3440 |     0,                                /* tp_as_number */ | 
 | 3441 |     0,                                /* tp_as_sequence */ | 
 | 3442 |     0,                                /* tp_as_mapping */ | 
 | 3443 |     (hashfunc)timezone_hash,          /* tp_hash */ | 
 | 3444 |     0,                                /* tp_call */ | 
 | 3445 |     (reprfunc)timezone_str,           /* tp_str */ | 
 | 3446 |     0,                                /* tp_getattro */ | 
 | 3447 |     0,                                /* tp_setattro */ | 
 | 3448 |     0,                                /* tp_as_buffer */ | 
 | 3449 |     Py_TPFLAGS_DEFAULT,               /* tp_flags */ | 
 | 3450 |     timezone_doc,                     /* tp_doc */ | 
 | 3451 |     0,                                /* tp_traverse */ | 
 | 3452 |     0,                                /* tp_clear */ | 
 | 3453 |     (richcmpfunc)timezone_richcompare,/* tp_richcompare */ | 
 | 3454 |     0,                                /* tp_weaklistoffset */ | 
 | 3455 |     0,                                /* tp_iter */ | 
 | 3456 |     0,                                /* tp_iternext */ | 
 | 3457 |     timezone_methods,                 /* tp_methods */ | 
 | 3458 |     0,                                /* tp_members */ | 
 | 3459 |     0,                                /* tp_getset */ | 
 | 3460 |     &PyDateTime_TZInfoType,           /* tp_base */ | 
 | 3461 |     0,                                /* tp_dict */ | 
 | 3462 |     0,                                /* tp_descr_get */ | 
 | 3463 |     0,                                /* tp_descr_set */ | 
 | 3464 |     0,                                /* tp_dictoffset */ | 
 | 3465 |     0,                                /* tp_init */ | 
 | 3466 |     0,                                /* tp_alloc */ | 
 | 3467 |     timezone_new,                     /* tp_new */ | 
 | 3468 | }; | 
 | 3469 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3470 | /* | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3471 |  * PyDateTime_Time implementation. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3472 |  */ | 
 | 3473 |  | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3474 | /* Accessor properties. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3475 |  */ | 
 | 3476 |  | 
 | 3477 | static PyObject * | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3478 | time_hour(PyDateTime_Time *self, void *unused) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3479 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 |     return PyLong_FromLong(TIME_GET_HOUR(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3481 | } | 
 | 3482 |  | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3483 | static PyObject * | 
 | 3484 | time_minute(PyDateTime_Time *self, void *unused) | 
 | 3485 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 |     return PyLong_FromLong(TIME_GET_MINUTE(self)); | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3487 | } | 
 | 3488 |  | 
 | 3489 | /* The name time_second conflicted with some platform header file. */ | 
 | 3490 | static PyObject * | 
 | 3491 | py_time_second(PyDateTime_Time *self, void *unused) | 
 | 3492 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3493 |     return PyLong_FromLong(TIME_GET_SECOND(self)); | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3494 | } | 
 | 3495 |  | 
 | 3496 | static PyObject * | 
 | 3497 | time_microsecond(PyDateTime_Time *self, void *unused) | 
 | 3498 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3499 |     return PyLong_FromLong(TIME_GET_MICROSECOND(self)); | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3500 | } | 
 | 3501 |  | 
 | 3502 | static PyObject * | 
 | 3503 | time_tzinfo(PyDateTime_Time *self, void *unused) | 
 | 3504 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 |     PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; | 
 | 3506 |     Py_INCREF(result); | 
 | 3507 |     return result; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3508 | } | 
 | 3509 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3510 | static PyObject * | 
 | 3511 | time_fold(PyDateTime_Time *self, void *unused) | 
 | 3512 | { | 
 | 3513 |     return PyLong_FromLong(TIME_GET_FOLD(self)); | 
 | 3514 | } | 
 | 3515 |  | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3516 | static PyGetSetDef time_getset[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 |     {"hour",        (getter)time_hour}, | 
 | 3518 |     {"minute",      (getter)time_minute}, | 
 | 3519 |     {"second",      (getter)py_time_second}, | 
 | 3520 |     {"microsecond", (getter)time_microsecond}, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3521 |     {"tzinfo",      (getter)time_tzinfo}, | 
 | 3522 |     {"fold",        (getter)time_fold}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 |     {NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3524 | }; | 
 | 3525 |  | 
 | 3526 | /* | 
 | 3527 |  * Constructors. | 
 | 3528 |  */ | 
 | 3529 |  | 
| Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3530 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3531 |                            "tzinfo", "fold", NULL}; | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3532 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3533 | static PyObject * | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3534 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3535 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 |     PyObject *self = NULL; | 
 | 3537 |     PyObject *state; | 
 | 3538 |     int hour = 0; | 
 | 3539 |     int minute = 0; | 
 | 3540 |     int second = 0; | 
 | 3541 |     int usecond = 0; | 
 | 3542 |     PyObject *tzinfo = Py_None; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3543 |     int fold = 0; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3544 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3545 |     /* Check for invocation from pickle with __getstate__ state */ | 
 | 3546 |     if (PyTuple_GET_SIZE(args) >= 1 && | 
 | 3547 |         PyTuple_GET_SIZE(args) <= 2 && | 
 | 3548 |         PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && | 
 | 3549 |         PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3550 |         (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3551 |     { | 
 | 3552 |         PyDateTime_Time *me; | 
 | 3553 |         char aware; | 
| Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3554 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3555 |         if (PyTuple_GET_SIZE(args) == 2) { | 
 | 3556 |             tzinfo = PyTuple_GET_ITEM(args, 1); | 
 | 3557 |             if (check_tzinfo_subclass(tzinfo) < 0) { | 
 | 3558 |                 PyErr_SetString(PyExc_TypeError, "bad " | 
 | 3559 |                     "tzinfo state arg"); | 
 | 3560 |                 return NULL; | 
 | 3561 |             } | 
 | 3562 |         } | 
 | 3563 |         aware = (char)(tzinfo != Py_None); | 
 | 3564 |         me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); | 
 | 3565 |         if (me != NULL) { | 
 | 3566 |             char *pdata = PyBytes_AS_STRING(state); | 
| Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3567 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3568 |             memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); | 
 | 3569 |             me->hashcode = -1; | 
 | 3570 |             me->hastzinfo = aware; | 
 | 3571 |             if (aware) { | 
 | 3572 |                 Py_INCREF(tzinfo); | 
 | 3573 |                 me->tzinfo = tzinfo; | 
 | 3574 |             } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3575 |             if (pdata[0] & (1 << 7)) { | 
 | 3576 |                 me->data[0] -= 128; | 
 | 3577 |                 me->fold = 1; | 
 | 3578 |             } | 
 | 3579 |             else { | 
 | 3580 |                 me->fold = 0; | 
 | 3581 |             } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 |         } | 
 | 3583 |         return (PyObject *)me; | 
 | 3584 |     } | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3585 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3586 |     if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3587 |                                     &hour, &minute, &second, &usecond, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3588 |                                     &tzinfo, &fold)) { | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3589 |         if (check_time_args(hour, minute, second, usecond, fold) < 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3590 |             return NULL; | 
 | 3591 |         if (check_tzinfo_subclass(tzinfo) < 0) | 
 | 3592 |             return NULL; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3593 |         self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, | 
 | 3594 |                             type); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3595 |     } | 
 | 3596 |     return self; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3597 | } | 
 | 3598 |  | 
 | 3599 | /* | 
 | 3600 |  * Destructor. | 
 | 3601 |  */ | 
 | 3602 |  | 
 | 3603 | static void | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3604 | time_dealloc(PyDateTime_Time *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3605 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 |     if (HASTZINFO(self)) { | 
 | 3607 |         Py_XDECREF(self->tzinfo); | 
 | 3608 |     } | 
 | 3609 |     Py_TYPE(self)->tp_free((PyObject *)self); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3610 | } | 
 | 3611 |  | 
 | 3612 | /* | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3613 |  * Indirect access to tzinfo methods. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3614 |  */ | 
 | 3615 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3616 | /* These are all METH_NOARGS, so don't need to check the arglist. */ | 
 | 3617 | static PyObject * | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3618 | time_utcoffset(PyObject *self, PyObject *unused) { | 
 | 3619 |     return call_utcoffset(GET_TIME_TZINFO(self), Py_None); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3620 | } | 
 | 3621 |  | 
 | 3622 | static PyObject * | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3623 | time_dst(PyObject *self, PyObject *unused) { | 
 | 3624 |     return call_dst(GET_TIME_TZINFO(self), Py_None); | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3625 | } | 
 | 3626 |  | 
 | 3627 | static PyObject * | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3628 | time_tzname(PyDateTime_Time *self, PyObject *unused) { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3629 |     return call_tzname(GET_TIME_TZINFO(self), Py_None); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3630 | } | 
 | 3631 |  | 
 | 3632 | /* | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3633 |  * Various ways to turn a time into a string. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3634 |  */ | 
 | 3635 |  | 
 | 3636 | static PyObject * | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3637 | time_repr(PyDateTime_Time *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3638 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3639 |     const char *type_name = Py_TYPE(self)->tp_name; | 
 | 3640 |     int h = TIME_GET_HOUR(self); | 
 | 3641 |     int m = TIME_GET_MINUTE(self); | 
 | 3642 |     int s = TIME_GET_SECOND(self); | 
 | 3643 |     int us = TIME_GET_MICROSECOND(self); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3644 |     int fold = TIME_GET_FOLD(self); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3646 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3647 |     if (us) | 
 | 3648 |         result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", | 
 | 3649 |                                       type_name, h, m, s, us); | 
 | 3650 |     else if (s) | 
 | 3651 |         result = PyUnicode_FromFormat("%s(%d, %d, %d)", | 
 | 3652 |                                       type_name, h, m, s); | 
 | 3653 |     else | 
 | 3654 |         result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); | 
 | 3655 |     if (result != NULL && HASTZINFO(self)) | 
 | 3656 |         result = append_keyword_tzinfo(result, self->tzinfo); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3657 |     if (result != NULL && fold) | 
 | 3658 |         result = append_keyword_fold(result, fold); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3659 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3660 | } | 
 | 3661 |  | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3662 | static PyObject * | 
 | 3663 | time_str(PyDateTime_Time *self) | 
 | 3664 | { | 
| Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3665 |     return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3666 | } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3667 |  | 
 | 3668 | static PyObject * | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3669 | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3670 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 |     char buf[100]; | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3672 |     char *timespec = NULL; | 
 | 3673 |     static char *keywords[] = {"timespec", NULL}; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 |     PyObject *result; | 
| Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3675 |     int us = TIME_GET_MICROSECOND(self); | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3676 |     static char *specs[][2] = { | 
 | 3677 |         {"hours", "%02d"}, | 
 | 3678 |         {"minutes", "%02d:%02d"}, | 
 | 3679 |         {"seconds", "%02d:%02d:%02d"}, | 
 | 3680 |         {"milliseconds", "%02d:%02d:%02d.%03d"}, | 
 | 3681 |         {"microseconds", "%02d:%02d:%02d.%06d"}, | 
 | 3682 |     }; | 
 | 3683 |     size_t given_spec; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3684 |  | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3685 |     if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) | 
 | 3686 |         return NULL; | 
 | 3687 |  | 
 | 3688 |     if (timespec == NULL || strcmp(timespec, "auto") == 0) { | 
 | 3689 |         if (us == 0) { | 
 | 3690 |             /* seconds */ | 
 | 3691 |             given_spec = 2; | 
 | 3692 |         } | 
 | 3693 |         else { | 
 | 3694 |             /* microseconds */ | 
 | 3695 |             given_spec = 4; | 
 | 3696 |         } | 
 | 3697 |     } | 
 | 3698 |     else { | 
 | 3699 |         for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { | 
 | 3700 |             if (strcmp(timespec, specs[given_spec][0]) == 0) { | 
 | 3701 |                 if (given_spec == 3) { | 
 | 3702 |                     /* milliseconds */ | 
 | 3703 |                     us = us / 1000; | 
 | 3704 |                 } | 
 | 3705 |                 break; | 
 | 3706 |             } | 
 | 3707 |         } | 
 | 3708 |     } | 
 | 3709 |  | 
 | 3710 |     if (given_spec == Py_ARRAY_LENGTH(specs)) { | 
 | 3711 |         PyErr_Format(PyExc_ValueError, "Unknown timespec value"); | 
 | 3712 |         return NULL; | 
 | 3713 |     } | 
 | 3714 |     else { | 
 | 3715 |         result = PyUnicode_FromFormat(specs[given_spec][1], | 
 | 3716 |                                       TIME_GET_HOUR(self), TIME_GET_MINUTE(self), | 
 | 3717 |                                       TIME_GET_SECOND(self), us); | 
 | 3718 |     } | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3719 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3720 |     if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3721 |         return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3722 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 |     /* We need to append the UTC offset. */ | 
 | 3724 |     if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, | 
 | 3725 |                          Py_None) < 0) { | 
 | 3726 |         Py_DECREF(result); | 
 | 3727 |         return NULL; | 
 | 3728 |     } | 
 | 3729 |     PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); | 
 | 3730 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3731 | } | 
 | 3732 |  | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3733 | static PyObject * | 
 | 3734 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) | 
 | 3735 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3736 |     PyObject *result; | 
 | 3737 |     PyObject *tuple; | 
 | 3738 |     PyObject *format; | 
 | 3739 |     static char *keywords[] = {"format", NULL}; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3740 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 |     if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, | 
 | 3742 |                                       &format)) | 
 | 3743 |         return NULL; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3744 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 |     /* Python's strftime does insane things with the year part of the | 
 | 3746 |      * timetuple.  The year is forced to (the otherwise nonsensical) | 
| Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 3747 |      * 1900 to work around that. | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3748 |      */ | 
 | 3749 |     tuple = Py_BuildValue("iiiiiiiii", | 
 | 3750 |                           1900, 1, 1, /* year, month, day */ | 
 | 3751 |                   TIME_GET_HOUR(self), | 
 | 3752 |                   TIME_GET_MINUTE(self), | 
 | 3753 |                   TIME_GET_SECOND(self), | 
 | 3754 |                   0, 1, -1); /* weekday, daynum, dst */ | 
 | 3755 |     if (tuple == NULL) | 
 | 3756 |         return NULL; | 
 | 3757 |     assert(PyTuple_Size(tuple) == 9); | 
 | 3758 |     result = wrap_strftime((PyObject *)self, format, tuple, | 
 | 3759 |                            Py_None); | 
 | 3760 |     Py_DECREF(tuple); | 
 | 3761 |     return result; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3762 | } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3763 |  | 
 | 3764 | /* | 
 | 3765 |  * Miscellaneous methods. | 
 | 3766 |  */ | 
 | 3767 |  | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3768 | static PyObject * | 
| Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3769 | time_richcompare(PyObject *self, PyObject *other, int op) | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3770 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3771 |     PyObject *result = NULL; | 
 | 3772 |     PyObject *offset1, *offset2; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3773 |     int diff; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3774 |  | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3775 |     if (! PyTime_Check(other)) | 
 | 3776 |         Py_RETURN_NOTIMPLEMENTED; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3777 |  | 
 | 3778 |     if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3779 |         diff = memcmp(((PyDateTime_Time *)self)->data, | 
 | 3780 |                       ((PyDateTime_Time *)other)->data, | 
 | 3781 |                       _PyDateTime_TIME_DATASIZE); | 
 | 3782 |         return diff_to_bool(diff, op); | 
 | 3783 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3784 |     offset1 = time_utcoffset(self, NULL); | 
 | 3785 |     if (offset1 == NULL) | 
 | 3786 |         return NULL; | 
 | 3787 |     offset2 = time_utcoffset(other, NULL); | 
 | 3788 |     if (offset2 == NULL) | 
 | 3789 |         goto done; | 
 | 3790 |     /* If they're both naive, or both aware and have the same offsets, | 
 | 3791 |      * we get off cheap.  Note that if they're both naive, offset1 == | 
 | 3792 |      * offset2 == Py_None at this point. | 
 | 3793 |      */ | 
 | 3794 |     if ((offset1 == offset2) || | 
 | 3795 |         (PyDelta_Check(offset1) && PyDelta_Check(offset2) && | 
 | 3796 |          delta_cmp(offset1, offset2) == 0)) { | 
 | 3797 |         diff = memcmp(((PyDateTime_Time *)self)->data, | 
 | 3798 |                       ((PyDateTime_Time *)other)->data, | 
 | 3799 |                       _PyDateTime_TIME_DATASIZE); | 
 | 3800 |         result = diff_to_bool(diff, op); | 
 | 3801 |     } | 
 | 3802 |     /* The hard case: both aware with different UTC offsets */ | 
 | 3803 |     else if (offset1 != Py_None && offset2 != Py_None) { | 
 | 3804 |         int offsecs1, offsecs2; | 
 | 3805 |         assert(offset1 != offset2); /* else last "if" handled it */ | 
 | 3806 |         offsecs1 = TIME_GET_HOUR(self) * 3600 + | 
 | 3807 |                    TIME_GET_MINUTE(self) * 60 + | 
 | 3808 |                    TIME_GET_SECOND(self) - | 
 | 3809 |                    GET_TD_DAYS(offset1) * 86400 - | 
 | 3810 |                    GET_TD_SECONDS(offset1); | 
 | 3811 |         offsecs2 = TIME_GET_HOUR(other) * 3600 + | 
 | 3812 |                    TIME_GET_MINUTE(other) * 60 + | 
 | 3813 |                    TIME_GET_SECOND(other) - | 
 | 3814 |                    GET_TD_DAYS(offset2) * 86400 - | 
 | 3815 |                    GET_TD_SECONDS(offset2); | 
 | 3816 |         diff = offsecs1 - offsecs2; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3817 |         if (diff == 0) | 
 | 3818 |             diff = TIME_GET_MICROSECOND(self) - | 
 | 3819 |                    TIME_GET_MICROSECOND(other); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3820 |         result = diff_to_bool(diff, op); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3821 |     } | 
| Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 3822 |     else if (op == Py_EQ) { | 
 | 3823 |         result = Py_False; | 
 | 3824 |         Py_INCREF(result); | 
 | 3825 |     } | 
 | 3826 |     else if (op == Py_NE) { | 
 | 3827 |         result = Py_True; | 
 | 3828 |         Py_INCREF(result); | 
 | 3829 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3830 |     else { | 
 | 3831 |         PyErr_SetString(PyExc_TypeError, | 
 | 3832 |                         "can't compare offset-naive and " | 
 | 3833 |                         "offset-aware times"); | 
 | 3834 |     } | 
 | 3835 |  done: | 
 | 3836 |     Py_DECREF(offset1); | 
 | 3837 |     Py_XDECREF(offset2); | 
 | 3838 |     return result; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3839 | } | 
 | 3840 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3841 | static Py_hash_t | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3842 | time_hash(PyDateTime_Time *self) | 
 | 3843 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3844 |     if (self->hashcode == -1) { | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3845 |         PyObject *offset, *self0; | 
 | 3846 |         if (DATE_GET_FOLD(self)) { | 
 | 3847 |             self0 = new_time_ex2(DATE_GET_HOUR(self), | 
 | 3848 |                                  DATE_GET_MINUTE(self), | 
 | 3849 |                                  DATE_GET_SECOND(self), | 
 | 3850 |                                  DATE_GET_MICROSECOND(self), | 
 | 3851 |                                  HASTZINFO(self) ? self->tzinfo : Py_None, | 
 | 3852 |                                  0, Py_TYPE(self)); | 
 | 3853 |             if (self0 == NULL) | 
 | 3854 |                 return -1; | 
 | 3855 |         } | 
 | 3856 |         else { | 
 | 3857 |             self0 = (PyObject *)self; | 
 | 3858 |             Py_INCREF(self0); | 
 | 3859 |         } | 
 | 3860 |         offset = time_utcoffset(self0, NULL); | 
 | 3861 |         Py_DECREF(self0); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3862 |  | 
 | 3863 |         if (offset == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3864 |             return -1; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3865 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 |         /* Reduce this to a hash of another object. */ | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3867 |         if (offset == Py_None) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 |             self->hashcode = generic_hash( | 
 | 3869 |                 (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3870 |         else { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3871 |             PyObject *temp1, *temp2; | 
 | 3872 |             int seconds, microseconds; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 |             assert(HASTZINFO(self)); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3874 |             seconds = TIME_GET_HOUR(self) * 3600 + | 
 | 3875 |                       TIME_GET_MINUTE(self) * 60 + | 
 | 3876 |                       TIME_GET_SECOND(self); | 
 | 3877 |             microseconds = TIME_GET_MICROSECOND(self); | 
 | 3878 |             temp1 = new_delta(0, seconds, microseconds, 1); | 
 | 3879 |             if (temp1 == NULL) { | 
 | 3880 |                 Py_DECREF(offset); | 
 | 3881 |                 return -1; | 
 | 3882 |             } | 
 | 3883 |             temp2 = delta_subtract(temp1, offset); | 
 | 3884 |             Py_DECREF(temp1); | 
 | 3885 |             if (temp2 == NULL) { | 
 | 3886 |                 Py_DECREF(offset); | 
 | 3887 |                 return -1; | 
 | 3888 |             } | 
 | 3889 |             self->hashcode = PyObject_Hash(temp2); | 
 | 3890 |             Py_DECREF(temp2); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 |         } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3892 |         Py_DECREF(offset); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 |     } | 
 | 3894 |     return self->hashcode; | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3895 | } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3896 |  | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3897 | static PyObject * | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3898 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3899 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 |     PyObject *clone; | 
 | 3901 |     PyObject *tuple; | 
 | 3902 |     int hh = TIME_GET_HOUR(self); | 
 | 3903 |     int mm = TIME_GET_MINUTE(self); | 
 | 3904 |     int ss = TIME_GET_SECOND(self); | 
 | 3905 |     int us = TIME_GET_MICROSECOND(self); | 
 | 3906 |     PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3907 |     int fold = TIME_GET_FOLD(self); | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3908 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3909 |     if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3910 |                                       time_kws, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3911 |                                       &hh, &mm, &ss, &us, &tzinfo, &fold)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 |         return NULL; | 
 | 3913 |     tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); | 
 | 3914 |     if (tuple == NULL) | 
 | 3915 |         return NULL; | 
 | 3916 |     clone = time_new(Py_TYPE(self), tuple, NULL); | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3917 |     if (clone != NULL) { | 
 | 3918 |         if (fold != 0 && fold != 1) { | 
 | 3919 |             PyErr_SetString(PyExc_ValueError, | 
 | 3920 |                             "fold must be either 0 or 1"); | 
 | 3921 |             return NULL; | 
 | 3922 |         } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3923 |         TIME_SET_FOLD(clone, fold); | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3924 |     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 |     Py_DECREF(tuple); | 
 | 3926 |     return clone; | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3927 | } | 
 | 3928 |  | 
| Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3929 | /* Pickle support, a simple use of __reduce__. */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3930 |  | 
| Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3931 | /* Let basestate be the non-tzinfo data string. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3932 |  * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). | 
 | 3933 |  * So it's a tuple in any (non-error) case. | 
| Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3934 |  * __getstate__ isn't exposed. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3935 |  */ | 
 | 3936 | static PyObject * | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3937 | time_getstate(PyDateTime_Time *self, int proto) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3938 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3939 |     PyObject *basestate; | 
 | 3940 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3941 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3942 |     basestate =  PyBytes_FromStringAndSize((char *)self->data, | 
 | 3943 |                                             _PyDateTime_TIME_DATASIZE); | 
 | 3944 |     if (basestate != NULL) { | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3945 |         if (proto > 3 && TIME_GET_FOLD(self)) | 
 | 3946 |             /* Set the first bit of the first byte */ | 
 | 3947 |             PyBytes_AS_STRING(basestate)[0] |= (1 << 7); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3948 |         if (! HASTZINFO(self) || self->tzinfo == Py_None) | 
 | 3949 |             result = PyTuple_Pack(1, basestate); | 
 | 3950 |         else | 
 | 3951 |             result = PyTuple_Pack(2, basestate, self->tzinfo); | 
 | 3952 |         Py_DECREF(basestate); | 
 | 3953 |     } | 
 | 3954 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3955 | } | 
 | 3956 |  | 
 | 3957 | static PyObject * | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 3958 | time_reduce_ex(PyDateTime_Time *self, PyObject *args) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3959 | { | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 3960 |     int proto; | 
 | 3961 |     if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3962 |         return NULL; | 
 | 3963 |  | 
 | 3964 |     return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3965 | } | 
 | 3966 |  | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 3967 | static PyObject * | 
 | 3968 | time_reduce(PyDateTime_Time *self, PyObject *arg) | 
 | 3969 | { | 
 | 3970 |     return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); | 
 | 3971 | } | 
 | 3972 |  | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3973 | static PyMethodDef time_methods[] = { | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3974 |  | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3975 |     {"isoformat",   (PyCFunction)time_isoformat,        METH_VARARGS | METH_KEYWORDS, | 
 | 3976 |      PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" | 
 | 3977 |                "[+HH:MM].\n\n" | 
 | 3978 |                "timespec specifies what components of the time to include.\n")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3979 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3980 |     {"strftime",        (PyCFunction)time_strftime,     METH_VARARGS | METH_KEYWORDS, | 
 | 3981 |      PyDoc_STR("format -> strftime() style string.")}, | 
| Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3982 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 |     {"__format__",      (PyCFunction)date_format,       METH_VARARGS, | 
 | 3984 |      PyDoc_STR("Formats self with strftime.")}, | 
| Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3985 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3986 |     {"utcoffset",       (PyCFunction)time_utcoffset,    METH_NOARGS, | 
 | 3987 |      PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3988 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3989 |     {"tzname",          (PyCFunction)time_tzname,       METH_NOARGS, | 
 | 3990 |      PyDoc_STR("Return self.tzinfo.tzname(self).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3991 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3992 |     {"dst",             (PyCFunction)time_dst,          METH_NOARGS, | 
 | 3993 |      PyDoc_STR("Return self.tzinfo.dst(self).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3994 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 |     {"replace",     (PyCFunction)time_replace,          METH_VARARGS | METH_KEYWORDS, | 
 | 3996 |      PyDoc_STR("Return time with new specified fields.")}, | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3997 |  | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 3998 |     {"__reduce_ex__", (PyCFunction)time_reduce_ex,        METH_VARARGS, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3999 |      PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4000 |  | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 4001 |     {"__reduce__", (PyCFunction)time_reduce,        METH_NOARGS, | 
 | 4002 |      PyDoc_STR("__reduce__() -> (cls, state)")}, | 
 | 4003 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 |     {NULL,      NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4005 | }; | 
 | 4006 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4007 | static const char time_doc[] = | 
| Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4008 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ | 
 | 4009 | \n\ | 
 | 4010 | All arguments are optional. tzinfo may be None, or an instance of\n\ | 
| Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4011 | a tzinfo subclass. The remaining arguments may be ints.\n"); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4012 |  | 
| Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4013 | static PyTypeObject PyDateTime_TimeType = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4014 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 4015 |     "datetime.time",                            /* tp_name */ | 
 | 4016 |     sizeof(PyDateTime_Time),                    /* tp_basicsize */ | 
 | 4017 |     0,                                          /* tp_itemsize */ | 
 | 4018 |     (destructor)time_dealloc,                   /* tp_dealloc */ | 
 | 4019 |     0,                                          /* tp_print */ | 
 | 4020 |     0,                                          /* tp_getattr */ | 
 | 4021 |     0,                                          /* tp_setattr */ | 
 | 4022 |     0,                                          /* tp_reserved */ | 
 | 4023 |     (reprfunc)time_repr,                        /* tp_repr */ | 
| Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 4024 |     0,                                          /* tp_as_number */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4025 |     0,                                          /* tp_as_sequence */ | 
 | 4026 |     0,                                          /* tp_as_mapping */ | 
 | 4027 |     (hashfunc)time_hash,                        /* tp_hash */ | 
 | 4028 |     0,                                          /* tp_call */ | 
 | 4029 |     (reprfunc)time_str,                         /* tp_str */ | 
 | 4030 |     PyObject_GenericGetAttr,                    /* tp_getattro */ | 
 | 4031 |     0,                                          /* tp_setattro */ | 
 | 4032 |     0,                                          /* tp_as_buffer */ | 
 | 4033 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | 
 | 4034 |     time_doc,                                   /* tp_doc */ | 
 | 4035 |     0,                                          /* tp_traverse */ | 
 | 4036 |     0,                                          /* tp_clear */ | 
 | 4037 |     time_richcompare,                           /* tp_richcompare */ | 
 | 4038 |     0,                                          /* tp_weaklistoffset */ | 
 | 4039 |     0,                                          /* tp_iter */ | 
 | 4040 |     0,                                          /* tp_iternext */ | 
 | 4041 |     time_methods,                               /* tp_methods */ | 
 | 4042 |     0,                                          /* tp_members */ | 
 | 4043 |     time_getset,                                /* tp_getset */ | 
 | 4044 |     0,                                          /* tp_base */ | 
 | 4045 |     0,                                          /* tp_dict */ | 
 | 4046 |     0,                                          /* tp_descr_get */ | 
 | 4047 |     0,                                          /* tp_descr_set */ | 
 | 4048 |     0,                                          /* tp_dictoffset */ | 
 | 4049 |     0,                                          /* tp_init */ | 
 | 4050 |     time_alloc,                                 /* tp_alloc */ | 
 | 4051 |     time_new,                                   /* tp_new */ | 
 | 4052 |     0,                                          /* tp_free */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4053 | }; | 
 | 4054 |  | 
 | 4055 | /* | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4056 |  * PyDateTime_DateTime implementation. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4057 |  */ | 
 | 4058 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4059 | /* Accessor properties.  Properties for day, month, and year are inherited | 
 | 4060 |  * from date. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4061 |  */ | 
 | 4062 |  | 
 | 4063 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4064 | datetime_hour(PyDateTime_DateTime *self, void *unused) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4065 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4066 |     return PyLong_FromLong(DATE_GET_HOUR(self)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4067 | } | 
 | 4068 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4069 | static PyObject * | 
 | 4070 | datetime_minute(PyDateTime_DateTime *self, void *unused) | 
 | 4071 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4072 |     return PyLong_FromLong(DATE_GET_MINUTE(self)); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4073 | } | 
 | 4074 |  | 
 | 4075 | static PyObject * | 
 | 4076 | datetime_second(PyDateTime_DateTime *self, void *unused) | 
 | 4077 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4078 |     return PyLong_FromLong(DATE_GET_SECOND(self)); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4079 | } | 
 | 4080 |  | 
 | 4081 | static PyObject * | 
 | 4082 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) | 
 | 4083 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4084 |     return PyLong_FromLong(DATE_GET_MICROSECOND(self)); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4085 | } | 
 | 4086 |  | 
 | 4087 | static PyObject * | 
 | 4088 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) | 
 | 4089 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4090 |     PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; | 
 | 4091 |     Py_INCREF(result); | 
 | 4092 |     return result; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4093 | } | 
 | 4094 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4095 | static PyObject * | 
 | 4096 | datetime_fold(PyDateTime_DateTime *self, void *unused) | 
 | 4097 | { | 
 | 4098 |     return PyLong_FromLong(DATE_GET_FOLD(self)); | 
 | 4099 | } | 
 | 4100 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4101 | static PyGetSetDef datetime_getset[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4102 |     {"hour",        (getter)datetime_hour}, | 
 | 4103 |     {"minute",      (getter)datetime_minute}, | 
 | 4104 |     {"second",      (getter)datetime_second}, | 
 | 4105 |     {"microsecond", (getter)datetime_microsecond}, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4106 |     {"tzinfo",      (getter)datetime_tzinfo}, | 
 | 4107 |     {"fold",        (getter)datetime_fold}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4108 |     {NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4109 | }; | 
 | 4110 |  | 
 | 4111 | /* | 
 | 4112 |  * Constructors. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4113 |  */ | 
 | 4114 |  | 
| Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4115 | static char *datetime_kws[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 |     "year", "month", "day", "hour", "minute", "second", | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4117 |     "microsecond", "tzinfo", "fold", NULL | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4118 | }; | 
 | 4119 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4120 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4121 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4122 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4123 |     PyObject *self = NULL; | 
 | 4124 |     PyObject *state; | 
 | 4125 |     int year; | 
 | 4126 |     int month; | 
 | 4127 |     int day; | 
 | 4128 |     int hour = 0; | 
 | 4129 |     int minute = 0; | 
 | 4130 |     int second = 0; | 
 | 4131 |     int usecond = 0; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4132 |     int fold = 0; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4133 |     PyObject *tzinfo = Py_None; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4134 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4135 |     /* Check for invocation from pickle with __getstate__ state */ | 
 | 4136 |     if (PyTuple_GET_SIZE(args) >= 1 && | 
 | 4137 |         PyTuple_GET_SIZE(args) <= 2 && | 
 | 4138 |         PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && | 
 | 4139 |         PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4140 |         MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 |     { | 
 | 4142 |         PyDateTime_DateTime *me; | 
 | 4143 |         char aware; | 
| Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4144 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4145 |         if (PyTuple_GET_SIZE(args) == 2) { | 
 | 4146 |             tzinfo = PyTuple_GET_ITEM(args, 1); | 
 | 4147 |             if (check_tzinfo_subclass(tzinfo) < 0) { | 
 | 4148 |                 PyErr_SetString(PyExc_TypeError, "bad " | 
 | 4149 |                     "tzinfo state arg"); | 
 | 4150 |                 return NULL; | 
 | 4151 |             } | 
 | 4152 |         } | 
 | 4153 |         aware = (char)(tzinfo != Py_None); | 
 | 4154 |         me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); | 
 | 4155 |         if (me != NULL) { | 
 | 4156 |             char *pdata = PyBytes_AS_STRING(state); | 
| Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4157 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 |             memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); | 
 | 4159 |             me->hashcode = -1; | 
 | 4160 |             me->hastzinfo = aware; | 
 | 4161 |             if (aware) { | 
 | 4162 |                 Py_INCREF(tzinfo); | 
 | 4163 |                 me->tzinfo = tzinfo; | 
 | 4164 |             } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4165 |             if (pdata[2] & (1 << 7)) { | 
 | 4166 |                 me->data[2] -= 128; | 
 | 4167 |                 me->fold = 1; | 
 | 4168 |             } | 
 | 4169 |             else { | 
 | 4170 |                 me->fold = 0; | 
 | 4171 |             } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 |         } | 
 | 4173 |         return (PyObject *)me; | 
 | 4174 |     } | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4175 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4176 |     if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4177 |                                     &year, &month, &day, &hour, &minute, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4178 |                                     &second, &usecond, &tzinfo, &fold)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4179 |         if (check_date_args(year, month, day) < 0) | 
 | 4180 |             return NULL; | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4181 |         if (check_time_args(hour, minute, second, usecond, fold) < 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4182 |             return NULL; | 
 | 4183 |         if (check_tzinfo_subclass(tzinfo) < 0) | 
 | 4184 |             return NULL; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4185 |         self = new_datetime_ex2(year, month, day, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 |                                 hour, minute, second, usecond, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4187 |                                 tzinfo, fold, type); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4188 |     } | 
 | 4189 |     return self; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4190 | } | 
 | 4191 |  | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4192 | /* TM_FUNC is the shared type of _PyTime_localtime() and | 
 | 4193 |  * _PyTime_gmtime(). */ | 
 | 4194 | typedef int (*TM_FUNC)(time_t timer, struct tm*); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4195 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4196 | /* As of version 2015f max fold in IANA database is | 
 | 4197 |  * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4198 | static long long max_fold_seconds = 24 * 3600; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4199 | /* NB: date(1970,1,1).toordinal() == 719163 */ | 
| Benjamin Peterson | ac965ca | 2016-09-18 18:12:21 -0700 | [diff] [blame] | 4200 | static long long epoch = 719163LL * 24 * 60 * 60; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4201 |  | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4202 | static long long | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4203 | utc_to_seconds(int year, int month, int day, | 
 | 4204 |                int hour, int minute, int second) | 
 | 4205 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4206 |     long long ordinal = ymd_to_ord(year, month, day); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4207 |     return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; | 
 | 4208 | } | 
 | 4209 |  | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4210 | static long long | 
 | 4211 | local(long long u) | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4212 | { | 
 | 4213 |     struct tm local_time; | 
| Alexander Belopolsky | 8e1d3a2 | 2016-07-25 13:54:51 -0400 | [diff] [blame] | 4214 |     time_t t; | 
 | 4215 |     u -= epoch; | 
 | 4216 |     t = u; | 
 | 4217 |     if (t != u) { | 
 | 4218 |         PyErr_SetString(PyExc_OverflowError, | 
 | 4219 |         "timestamp out of range for platform time_t"); | 
 | 4220 |         return -1; | 
 | 4221 |     } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4222 |     /* XXX: add bounds checking */ | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4223 |     if (_PyTime_localtime(t, &local_time) != 0) | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4224 |         return -1; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4225 |     return utc_to_seconds(local_time.tm_year + 1900, | 
 | 4226 |                           local_time.tm_mon + 1, | 
 | 4227 |                           local_time.tm_mday, | 
 | 4228 |                           local_time.tm_hour, | 
 | 4229 |                           local_time.tm_min, | 
 | 4230 |                           local_time.tm_sec); | 
 | 4231 | } | 
 | 4232 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4233 | /* Internal helper. | 
 | 4234 |  * Build datetime from a time_t and a distinct count of microseconds. | 
 | 4235 |  * Pass localtime or gmtime for f, to control the interpretation of timet. | 
 | 4236 |  */ | 
 | 4237 | static PyObject * | 
 | 4238 | datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4239 |                            PyObject *tzinfo) | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4240 | { | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4241 |     struct tm tm; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4242 |     int year, month, day, hour, minute, second, fold = 0; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4243 |  | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4244 |     if (f(timet, &tm) != 0) | 
 | 4245 |         return NULL; | 
| Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4246 |  | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4247 |     year = tm.tm_year + 1900; | 
 | 4248 |     month = tm.tm_mon + 1; | 
 | 4249 |     day = tm.tm_mday; | 
 | 4250 |     hour = tm.tm_hour; | 
 | 4251 |     minute = tm.tm_min; | 
| Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4252 |     /* The platform localtime/gmtime may insert leap seconds, | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4253 |      * indicated by tm.tm_sec > 59.  We don't care about them, | 
| Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4254 |      * except to the extent that passing them on to the datetime | 
 | 4255 |      * constructor would raise ValueError for a reason that | 
 | 4256 |      * made no sense to the user. | 
 | 4257 |      */ | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4258 |     second = Py_MIN(59, tm.tm_sec); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4259 |  | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4260 |     if (tzinfo == Py_None && f == _PyTime_localtime) { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4261 |         long long probe_seconds, result_seconds, transition; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4262 |  | 
 | 4263 |         result_seconds = utc_to_seconds(year, month, day, | 
 | 4264 |                                         hour, minute, second); | 
 | 4265 |         /* Probe max_fold_seconds to detect a fold. */ | 
 | 4266 |         probe_seconds = local(epoch + timet - max_fold_seconds); | 
 | 4267 |         if (probe_seconds == -1) | 
 | 4268 |             return NULL; | 
 | 4269 |         transition = result_seconds - probe_seconds - max_fold_seconds; | 
 | 4270 |         if (transition < 0) { | 
 | 4271 |             probe_seconds = local(epoch + timet + transition); | 
 | 4272 |             if (probe_seconds == -1) | 
 | 4273 |                 return NULL; | 
 | 4274 |             if (probe_seconds == result_seconds) | 
 | 4275 |                 fold = 1; | 
 | 4276 |         } | 
 | 4277 |     } | 
 | 4278 |     return new_datetime_ex2(year, month, day, hour, | 
 | 4279 |                             minute, second, us, tzinfo, fold, | 
 | 4280 |                             (PyTypeObject *)cls); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4281 | } | 
 | 4282 |  | 
 | 4283 | /* Internal helper. | 
 | 4284 |  * Build datetime from a Python timestamp.  Pass localtime or gmtime for f, | 
 | 4285 |  * to control the interpretation of the timestamp.  Since a double doesn't | 
 | 4286 |  * have enough bits to cover a datetime's full range of precision, it's | 
 | 4287 |  * better to call datetime_from_timet_and_us provided you have a way | 
 | 4288 |  * to get that much precision (e.g., C time() isn't good enough). | 
 | 4289 |  */ | 
 | 4290 | static PyObject * | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4291 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4292 |                         PyObject *tzinfo) | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4293 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4294 |     time_t timet; | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4295 |     long us; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4296 |  | 
| Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 4297 |     if (_PyTime_ObjectToTimeval(timestamp, | 
| Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 4298 |                                 &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 |         return NULL; | 
| Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4300 |  | 
| Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4301 |     return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4302 | } | 
 | 4303 |  | 
 | 4304 | /* Internal helper. | 
 | 4305 |  * Build most accurate possible datetime for current time.  Pass localtime or | 
 | 4306 |  * gmtime for f as appropriate. | 
 | 4307 |  */ | 
 | 4308 | static PyObject * | 
 | 4309 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) | 
 | 4310 | { | 
| Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4311 |     _PyTime_t ts = _PyTime_GetSystemClock(); | 
| Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4312 |     time_t secs; | 
 | 4313 |     int us; | 
| Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4314 |  | 
| Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4315 |     if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) | 
| Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4316 |         return NULL; | 
| Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4317 |     assert(0 <= us && us <= 999999); | 
| Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4318 |  | 
| Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4319 |     return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4320 | } | 
 | 4321 |  | 
| Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4322 | /*[clinic input] | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4323 |  | 
 | 4324 | @classmethod | 
| Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4325 | datetime.datetime.now | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4326 |  | 
 | 4327 |     tz: object = None | 
 | 4328 |         Timezone object. | 
 | 4329 |  | 
 | 4330 | Returns new datetime object representing current time local to tz. | 
 | 4331 |  | 
 | 4332 | If no tz is specified, uses local timezone. | 
| Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4333 | [clinic start generated code]*/ | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4334 |  | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4335 | static PyObject * | 
| Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4336 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) | 
| Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4337 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4338 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4339 |     PyObject *self; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4340 |  | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4341 |     /* Return best possible local time -- this isn't constrained by the | 
 | 4342 |      * precision of a timestamp. | 
 | 4343 |      */ | 
 | 4344 |     if (check_tzinfo_subclass(tz) < 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 |         return NULL; | 
| Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4346 |  | 
| Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4347 |     self = datetime_best_possible((PyObject *)type, | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4348 |                                   tz == Py_None ? _PyTime_localtime : | 
 | 4349 |                                   _PyTime_gmtime, | 
| Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4350 |                                   tz); | 
 | 4351 |     if (self != NULL && tz != Py_None) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 |         /* Convert UTC to tzinfo's zone. */ | 
| Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4353 |         self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 |     } | 
 | 4355 |     return self; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4356 | } | 
 | 4357 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4358 | /* Return best possible UTC time -- this isn't constrained by the | 
 | 4359 |  * precision of a timestamp. | 
 | 4360 |  */ | 
 | 4361 | static PyObject * | 
 | 4362 | datetime_utcnow(PyObject *cls, PyObject *dummy) | 
 | 4363 | { | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4364 |     return datetime_best_possible(cls, _PyTime_gmtime, Py_None); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4365 | } | 
 | 4366 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4367 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ | 
 | 4368 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4369 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4370 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4371 |     PyObject *self; | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4372 |     PyObject *timestamp; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4373 |     PyObject *tzinfo = Py_None; | 
 | 4374 |     static char *keywords[] = {"timestamp", "tz", NULL}; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4375 |  | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4376 |     if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 |                                       keywords, ×tamp, &tzinfo)) | 
 | 4378 |         return NULL; | 
 | 4379 |     if (check_tzinfo_subclass(tzinfo) < 0) | 
 | 4380 |         return NULL; | 
| Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4381 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 |     self = datetime_from_timestamp(cls, | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4383 |                                    tzinfo == Py_None ? _PyTime_localtime : | 
 | 4384 |                                    _PyTime_gmtime, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 |                                    timestamp, | 
 | 4386 |                                    tzinfo); | 
 | 4387 |     if (self != NULL && tzinfo != Py_None) { | 
 | 4388 |         /* Convert UTC to tzinfo's zone. */ | 
| Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4389 |         self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4390 |     } | 
 | 4391 |     return self; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4392 | } | 
 | 4393 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4394 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ | 
 | 4395 | static PyObject * | 
 | 4396 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) | 
 | 4397 | { | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4398 |     PyObject *timestamp; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4399 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4400 |  | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4401 |     if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4402 |         result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 |                                          Py_None); | 
 | 4404 |     return result; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4405 | } | 
 | 4406 |  | 
| Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4407 | /* Return new datetime from _strptime.strptime_datetime(). */ | 
| Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4408 | static PyObject * | 
 | 4409 | datetime_strptime(PyObject *cls, PyObject *args) | 
 | 4410 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 |     static PyObject *module = NULL; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4412 |     PyObject *string, *format; | 
| Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4413 |     _Py_IDENTIFIER(_strptime_datetime); | 
| Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4414 |  | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4415 |     if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 |         return NULL; | 
| Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4417 |  | 
| Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4418 |     if (module == NULL) { | 
 | 4419 |         module = PyImport_ImportModuleNoBlock("_strptime"); | 
| Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4420 |         if (module == NULL) | 
| Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4421 |             return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 |     } | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4423 |     return _PyObject_CallMethodId(module, &PyId__strptime_datetime, "OOO", | 
 | 4424 |                                  cls, string, format); | 
| Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4425 | } | 
 | 4426 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4427 | /* Return new datetime from date/datetime and time arguments. */ | 
 | 4428 | static PyObject * | 
 | 4429 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) | 
 | 4430 | { | 
| Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4431 |     static char *keywords[] = {"date", "time", "tzinfo", NULL}; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 |     PyObject *date; | 
 | 4433 |     PyObject *time; | 
| Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4434 |     PyObject *tzinfo = NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 |     PyObject *result = NULL; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4436 |  | 
| Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4437 |     if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 |                                     &PyDateTime_DateType, &date, | 
| Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4439 |                                     &PyDateTime_TimeType, &time, &tzinfo)) { | 
 | 4440 |         if (tzinfo == NULL) { | 
 | 4441 |             if (HASTZINFO(time)) | 
 | 4442 |                 tzinfo = ((PyDateTime_Time *)time)->tzinfo; | 
 | 4443 |             else | 
 | 4444 |                 tzinfo = Py_None; | 
 | 4445 |         } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4446 |         result = PyObject_CallFunction(cls, "iiiiiiiO", | 
| Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4447 |                                        GET_YEAR(date), | 
 | 4448 |                                        GET_MONTH(date), | 
 | 4449 |                                        GET_DAY(date), | 
 | 4450 |                                        TIME_GET_HOUR(time), | 
 | 4451 |                                        TIME_GET_MINUTE(time), | 
 | 4452 |                                        TIME_GET_SECOND(time), | 
 | 4453 |                                        TIME_GET_MICROSECOND(time), | 
 | 4454 |                                        tzinfo); | 
 | 4455 |         if (result) | 
 | 4456 |             DATE_SET_FOLD(result, TIME_GET_FOLD(time)); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 |     } | 
 | 4458 |     return result; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4459 | } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4460 |  | 
 | 4461 | /* | 
 | 4462 |  * Destructor. | 
 | 4463 |  */ | 
 | 4464 |  | 
 | 4465 | static void | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4466 | datetime_dealloc(PyDateTime_DateTime *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4467 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 |     if (HASTZINFO(self)) { | 
 | 4469 |         Py_XDECREF(self->tzinfo); | 
 | 4470 |     } | 
 | 4471 |     Py_TYPE(self)->tp_free((PyObject *)self); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4472 | } | 
 | 4473 |  | 
 | 4474 | /* | 
 | 4475 |  * Indirect access to tzinfo methods. | 
 | 4476 |  */ | 
 | 4477 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4478 | /* These are all METH_NOARGS, so don't need to check the arglist. */ | 
 | 4479 | static PyObject * | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4480 | datetime_utcoffset(PyObject *self, PyObject *unused) { | 
 | 4481 |     return call_utcoffset(GET_DT_TZINFO(self), self); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4482 | } | 
 | 4483 |  | 
 | 4484 | static PyObject * | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4485 | datetime_dst(PyObject *self, PyObject *unused) { | 
 | 4486 |     return call_dst(GET_DT_TZINFO(self), self); | 
| Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4487 | } | 
 | 4488 |  | 
 | 4489 | static PyObject * | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4490 | datetime_tzname(PyObject *self, PyObject *unused) { | 
 | 4491 |     return call_tzname(GET_DT_TZINFO(self), self); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4492 | } | 
 | 4493 |  | 
 | 4494 | /* | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4495 |  * datetime arithmetic. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4496 |  */ | 
 | 4497 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4498 | /* factor must be 1 (to add) or -1 (to subtract).  The result inherits | 
 | 4499 |  * the tzinfo state of date. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4500 |  */ | 
 | 4501 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4502 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 |                        int factor) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4504 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 |     /* Note that the C-level additions can't overflow, because of | 
 | 4506 |      * invariant bounds on the member values. | 
 | 4507 |      */ | 
 | 4508 |     int year = GET_YEAR(date); | 
 | 4509 |     int month = GET_MONTH(date); | 
 | 4510 |     int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; | 
 | 4511 |     int hour = DATE_GET_HOUR(date); | 
 | 4512 |     int minute = DATE_GET_MINUTE(date); | 
 | 4513 |     int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; | 
 | 4514 |     int microsecond = DATE_GET_MICROSECOND(date) + | 
 | 4515 |                       GET_TD_MICROSECONDS(delta) * factor; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4516 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 |     assert(factor == 1 || factor == -1); | 
 | 4518 |     if (normalize_datetime(&year, &month, &day, | 
 | 4519 |                            &hour, &minute, &second, µsecond) < 0) | 
 | 4520 |         return NULL; | 
 | 4521 |     else | 
 | 4522 |         return new_datetime(year, month, day, | 
 | 4523 |                             hour, minute, second, microsecond, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4524 |                             HASTZINFO(date) ? date->tzinfo : Py_None, 0); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4525 | } | 
 | 4526 |  | 
 | 4527 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4528 | datetime_add(PyObject *left, PyObject *right) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4529 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 |     if (PyDateTime_Check(left)) { | 
 | 4531 |         /* datetime + ??? */ | 
 | 4532 |         if (PyDelta_Check(right)) | 
 | 4533 |             /* datetime + delta */ | 
 | 4534 |             return add_datetime_timedelta( | 
 | 4535 |                             (PyDateTime_DateTime *)left, | 
 | 4536 |                             (PyDateTime_Delta *)right, | 
 | 4537 |                             1); | 
 | 4538 |     } | 
 | 4539 |     else if (PyDelta_Check(left)) { | 
 | 4540 |         /* delta + datetime */ | 
 | 4541 |         return add_datetime_timedelta((PyDateTime_DateTime *) right, | 
 | 4542 |                                       (PyDateTime_Delta *) left, | 
 | 4543 |                                       1); | 
 | 4544 |     } | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4545 |     Py_RETURN_NOTIMPLEMENTED; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4546 | } | 
 | 4547 |  | 
 | 4548 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4549 | datetime_subtract(PyObject *left, PyObject *right) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4550 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 |     PyObject *result = Py_NotImplemented; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4552 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 |     if (PyDateTime_Check(left)) { | 
 | 4554 |         /* datetime - ??? */ | 
 | 4555 |         if (PyDateTime_Check(right)) { | 
 | 4556 |             /* datetime - datetime */ | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4557 |             PyObject *offset1, *offset2, *offdiff = NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4558 |             int delta_d, delta_s, delta_us; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4559 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4560 |             if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { | 
 | 4561 |                 offset2 = offset1 = Py_None; | 
 | 4562 |                 Py_INCREF(offset1); | 
 | 4563 |                 Py_INCREF(offset2); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 |             } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4565 |             else { | 
 | 4566 |                 offset1 = datetime_utcoffset(left, NULL); | 
 | 4567 |                 if (offset1 == NULL) | 
 | 4568 |                     return NULL; | 
 | 4569 |                 offset2 = datetime_utcoffset(right, NULL); | 
 | 4570 |                 if (offset2 == NULL) { | 
 | 4571 |                     Py_DECREF(offset1); | 
 | 4572 |                     return NULL; | 
 | 4573 |                 } | 
 | 4574 |                 if ((offset1 != Py_None) != (offset2 != Py_None)) { | 
 | 4575 |                     PyErr_SetString(PyExc_TypeError, | 
 | 4576 |                                     "can't subtract offset-naive and " | 
 | 4577 |                                     "offset-aware datetimes"); | 
 | 4578 |                     Py_DECREF(offset1); | 
 | 4579 |                     Py_DECREF(offset2); | 
 | 4580 |                     return NULL; | 
 | 4581 |                 } | 
 | 4582 |             } | 
 | 4583 |             if ((offset1 != offset2) && | 
 | 4584 |                 delta_cmp(offset1, offset2) != 0) { | 
 | 4585 |                 offdiff = delta_subtract(offset1, offset2); | 
 | 4586 |                 if (offdiff == NULL) { | 
 | 4587 |                     Py_DECREF(offset1); | 
 | 4588 |                     Py_DECREF(offset2); | 
 | 4589 |                     return NULL; | 
 | 4590 |                 } | 
 | 4591 |             } | 
 | 4592 |             Py_DECREF(offset1); | 
 | 4593 |             Py_DECREF(offset2); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4594 |             delta_d = ymd_to_ord(GET_YEAR(left), | 
 | 4595 |                                  GET_MONTH(left), | 
 | 4596 |                                  GET_DAY(left)) - | 
 | 4597 |                       ymd_to_ord(GET_YEAR(right), | 
 | 4598 |                                  GET_MONTH(right), | 
 | 4599 |                                  GET_DAY(right)); | 
 | 4600 |             /* These can't overflow, since the values are | 
 | 4601 |              * normalized.  At most this gives the number of | 
 | 4602 |              * seconds in one day. | 
 | 4603 |              */ | 
 | 4604 |             delta_s = (DATE_GET_HOUR(left) - | 
 | 4605 |                        DATE_GET_HOUR(right)) * 3600 + | 
 | 4606 |                       (DATE_GET_MINUTE(left) - | 
 | 4607 |                        DATE_GET_MINUTE(right)) * 60 + | 
 | 4608 |                       (DATE_GET_SECOND(left) - | 
 | 4609 |                        DATE_GET_SECOND(right)); | 
 | 4610 |             delta_us = DATE_GET_MICROSECOND(left) - | 
 | 4611 |                        DATE_GET_MICROSECOND(right); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4612 |             result = new_delta(delta_d, delta_s, delta_us, 1); | 
| Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 4613 |             if (result == NULL) | 
 | 4614 |                 return NULL; | 
 | 4615 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4616 |             if (offdiff != NULL) { | 
| Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 4617 |                 Py_SETREF(result, delta_subtract(result, offdiff)); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4618 |                 Py_DECREF(offdiff); | 
 | 4619 |             } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4620 |         } | 
 | 4621 |         else if (PyDelta_Check(right)) { | 
 | 4622 |             /* datetime - delta */ | 
 | 4623 |             result = add_datetime_timedelta( | 
 | 4624 |                             (PyDateTime_DateTime *)left, | 
 | 4625 |                             (PyDateTime_Delta *)right, | 
 | 4626 |                             -1); | 
 | 4627 |         } | 
 | 4628 |     } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4629 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4630 |     if (result == Py_NotImplemented) | 
 | 4631 |         Py_INCREF(result); | 
 | 4632 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4633 | } | 
 | 4634 |  | 
 | 4635 | /* Various ways to turn a datetime into a string. */ | 
 | 4636 |  | 
 | 4637 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4638 | datetime_repr(PyDateTime_DateTime *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4639 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 |     const char *type_name = Py_TYPE(self)->tp_name; | 
 | 4641 |     PyObject *baserepr; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4642 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4643 |     if (DATE_GET_MICROSECOND(self)) { | 
 | 4644 |         baserepr = PyUnicode_FromFormat( | 
 | 4645 |                       "%s(%d, %d, %d, %d, %d, %d, %d)", | 
 | 4646 |                       type_name, | 
 | 4647 |                       GET_YEAR(self), GET_MONTH(self), GET_DAY(self), | 
 | 4648 |                       DATE_GET_HOUR(self), DATE_GET_MINUTE(self), | 
 | 4649 |                       DATE_GET_SECOND(self), | 
 | 4650 |                       DATE_GET_MICROSECOND(self)); | 
 | 4651 |     } | 
 | 4652 |     else if (DATE_GET_SECOND(self)) { | 
 | 4653 |         baserepr = PyUnicode_FromFormat( | 
 | 4654 |                       "%s(%d, %d, %d, %d, %d, %d)", | 
 | 4655 |                       type_name, | 
 | 4656 |                       GET_YEAR(self), GET_MONTH(self), GET_DAY(self), | 
 | 4657 |                       DATE_GET_HOUR(self), DATE_GET_MINUTE(self), | 
 | 4658 |                       DATE_GET_SECOND(self)); | 
 | 4659 |     } | 
 | 4660 |     else { | 
 | 4661 |         baserepr = PyUnicode_FromFormat( | 
 | 4662 |                       "%s(%d, %d, %d, %d, %d)", | 
 | 4663 |                       type_name, | 
 | 4664 |                       GET_YEAR(self), GET_MONTH(self), GET_DAY(self), | 
 | 4665 |                       DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); | 
 | 4666 |     } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4667 |     if (baserepr != NULL && DATE_GET_FOLD(self) != 0) | 
 | 4668 |         baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 |     if (baserepr == NULL || ! HASTZINFO(self)) | 
 | 4670 |         return baserepr; | 
 | 4671 |     return append_keyword_tzinfo(baserepr, self->tzinfo); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4672 | } | 
 | 4673 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4674 | static PyObject * | 
 | 4675 | datetime_str(PyDateTime_DateTime *self) | 
 | 4676 | { | 
| Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4677 |     return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "(s)", " "); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4678 | } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4679 |  | 
 | 4680 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4681 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4682 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 |     int sep = 'T'; | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4684 |     char *timespec = NULL; | 
 | 4685 |     static char *keywords[] = {"sep", "timespec", NULL}; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4686 |     char buffer[100]; | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4687 |     PyObject *result = NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 |     int us = DATE_GET_MICROSECOND(self); | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4689 |     static char *specs[][2] = { | 
 | 4690 |         {"hours", "%04d-%02d-%02d%c%02d"}, | 
 | 4691 |         {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, | 
 | 4692 |         {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, | 
 | 4693 |         {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, | 
 | 4694 |         {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, | 
 | 4695 |     }; | 
 | 4696 |     size_t given_spec; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4697 |  | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4698 |     if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 |         return NULL; | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4700 |  | 
 | 4701 |     if (timespec == NULL || strcmp(timespec, "auto") == 0) { | 
 | 4702 |         if (us == 0) { | 
 | 4703 |             /* seconds */ | 
 | 4704 |             given_spec = 2; | 
 | 4705 |         } | 
 | 4706 |         else { | 
 | 4707 |             /* microseconds */ | 
 | 4708 |             given_spec = 4; | 
 | 4709 |         } | 
 | 4710 |     } | 
 | 4711 |     else { | 
 | 4712 |         for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { | 
 | 4713 |             if (strcmp(timespec, specs[given_spec][0]) == 0) { | 
 | 4714 |                 if (given_spec == 3) { | 
 | 4715 |                     us = us / 1000; | 
 | 4716 |                 } | 
 | 4717 |                 break; | 
 | 4718 |             } | 
 | 4719 |         } | 
 | 4720 |     } | 
 | 4721 |  | 
 | 4722 |     if (given_spec == Py_ARRAY_LENGTH(specs)) { | 
 | 4723 |         PyErr_Format(PyExc_ValueError, "Unknown timespec value"); | 
 | 4724 |         return NULL; | 
 | 4725 |     } | 
 | 4726 |     else { | 
 | 4727 |         result = PyUnicode_FromFormat(specs[given_spec][1], | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 |                                       GET_YEAR(self), GET_MONTH(self), | 
 | 4729 |                                       GET_DAY(self), (int)sep, | 
 | 4730 |                                       DATE_GET_HOUR(self), DATE_GET_MINUTE(self), | 
 | 4731 |                                       DATE_GET_SECOND(self), us); | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4732 |     } | 
| Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4733 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4734 |     if (!result || !HASTZINFO(self)) | 
 | 4735 |         return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4736 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4737 |     /* We need to append the UTC offset. */ | 
 | 4738 |     if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, | 
 | 4739 |                          (PyObject *)self) < 0) { | 
 | 4740 |         Py_DECREF(result); | 
 | 4741 |         return NULL; | 
 | 4742 |     } | 
 | 4743 |     PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); | 
 | 4744 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4745 | } | 
 | 4746 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4747 | static PyObject * | 
 | 4748 | datetime_ctime(PyDateTime_DateTime *self) | 
 | 4749 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4750 |     return format_ctime((PyDateTime_Date *)self, | 
 | 4751 |                         DATE_GET_HOUR(self), | 
 | 4752 |                         DATE_GET_MINUTE(self), | 
 | 4753 |                         DATE_GET_SECOND(self)); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4754 | } | 
 | 4755 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4756 | /* Miscellaneous methods. */ | 
 | 4757 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4758 | static PyObject * | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4759 | flip_fold(PyObject *dt) | 
 | 4760 | { | 
 | 4761 |     return new_datetime_ex2(GET_YEAR(dt), | 
 | 4762 |                             GET_MONTH(dt), | 
 | 4763 |                             GET_DAY(dt), | 
 | 4764 |                             DATE_GET_HOUR(dt), | 
 | 4765 |                             DATE_GET_MINUTE(dt), | 
 | 4766 |                             DATE_GET_SECOND(dt), | 
 | 4767 |                             DATE_GET_MICROSECOND(dt), | 
 | 4768 |                             HASTZINFO(dt) ? | 
 | 4769 |                              ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, | 
 | 4770 |                             !DATE_GET_FOLD(dt), | 
 | 4771 |                             Py_TYPE(dt)); | 
 | 4772 | } | 
 | 4773 |  | 
 | 4774 | static PyObject * | 
 | 4775 | get_flip_fold_offset(PyObject *dt) | 
 | 4776 | { | 
 | 4777 |     PyObject *result, *flip_dt; | 
 | 4778 |  | 
 | 4779 |     flip_dt = flip_fold(dt); | 
 | 4780 |     if (flip_dt == NULL) | 
 | 4781 |         return NULL; | 
 | 4782 |     result = datetime_utcoffset(flip_dt, NULL); | 
 | 4783 |     Py_DECREF(flip_dt); | 
 | 4784 |     return result; | 
 | 4785 | } | 
 | 4786 |  | 
 | 4787 | /* PEP 495 exception: Whenever one or both of the operands in | 
 | 4788 |  * inter-zone comparison is such that its utcoffset() depends | 
 | 4789 |  * on the value of its fold fold attribute, the result is False. | 
 | 4790 |  * | 
 | 4791 |  * Return 1 if exception applies, 0 if not,  and -1 on error. | 
 | 4792 |  */ | 
 | 4793 | static int | 
 | 4794 | pep495_eq_exception(PyObject *self, PyObject *other, | 
 | 4795 |                     PyObject *offset_self, PyObject *offset_other) | 
 | 4796 | { | 
 | 4797 |     int result = 0; | 
 | 4798 |     PyObject *flip_offset; | 
 | 4799 |  | 
 | 4800 |     flip_offset = get_flip_fold_offset(self); | 
 | 4801 |     if (flip_offset == NULL) | 
 | 4802 |         return -1; | 
 | 4803 |     if (flip_offset != offset_self && | 
 | 4804 |         delta_cmp(flip_offset, offset_self)) | 
 | 4805 |     { | 
 | 4806 |         result = 1; | 
 | 4807 |         goto done; | 
 | 4808 |     } | 
 | 4809 |     Py_DECREF(flip_offset); | 
 | 4810 |  | 
 | 4811 |     flip_offset = get_flip_fold_offset(other); | 
 | 4812 |     if (flip_offset == NULL) | 
 | 4813 |         return -1; | 
 | 4814 |     if (flip_offset != offset_other && | 
 | 4815 |         delta_cmp(flip_offset, offset_other)) | 
 | 4816 |         result = 1; | 
 | 4817 |  done: | 
 | 4818 |     Py_DECREF(flip_offset); | 
 | 4819 |     return result; | 
 | 4820 | } | 
 | 4821 |  | 
 | 4822 | static PyObject * | 
| Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4823 | datetime_richcompare(PyObject *self, PyObject *other, int op) | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4824 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4825 |     PyObject *result = NULL; | 
 | 4826 |     PyObject *offset1, *offset2; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4827 |     int diff; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4828 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4829 |     if (! PyDateTime_Check(other)) { | 
 | 4830 |         if (PyDate_Check(other)) { | 
 | 4831 |             /* Prevent invocation of date_richcompare.  We want to | 
 | 4832 |                return NotImplemented here to give the other object | 
 | 4833 |                a chance.  But since DateTime is a subclass of | 
 | 4834 |                Date, if the other object is a Date, it would | 
 | 4835 |                compute an ordering based on the date part alone, | 
 | 4836 |                and we don't want that.  So force unequal or | 
 | 4837 |                uncomparable here in that case. */ | 
 | 4838 |             if (op == Py_EQ) | 
 | 4839 |                 Py_RETURN_FALSE; | 
 | 4840 |             if (op == Py_NE) | 
 | 4841 |                 Py_RETURN_TRUE; | 
 | 4842 |             return cmperror(self, other); | 
 | 4843 |         } | 
| Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4844 |         Py_RETURN_NOTIMPLEMENTED; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4845 |     } | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4846 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4847 |     if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4848 |         diff = memcmp(((PyDateTime_DateTime *)self)->data, | 
 | 4849 |                       ((PyDateTime_DateTime *)other)->data, | 
 | 4850 |                       _PyDateTime_DATETIME_DATASIZE); | 
 | 4851 |         return diff_to_bool(diff, op); | 
 | 4852 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4853 |     offset1 = datetime_utcoffset(self, NULL); | 
 | 4854 |     if (offset1 == NULL) | 
 | 4855 |         return NULL; | 
 | 4856 |     offset2 = datetime_utcoffset(other, NULL); | 
 | 4857 |     if (offset2 == NULL) | 
 | 4858 |         goto done; | 
 | 4859 |     /* If they're both naive, or both aware and have the same offsets, | 
 | 4860 |      * we get off cheap.  Note that if they're both naive, offset1 == | 
 | 4861 |      * offset2 == Py_None at this point. | 
 | 4862 |      */ | 
 | 4863 |     if ((offset1 == offset2) || | 
 | 4864 |         (PyDelta_Check(offset1) && PyDelta_Check(offset2) && | 
 | 4865 |          delta_cmp(offset1, offset2) == 0)) { | 
 | 4866 |         diff = memcmp(((PyDateTime_DateTime *)self)->data, | 
 | 4867 |                       ((PyDateTime_DateTime *)other)->data, | 
 | 4868 |                       _PyDateTime_DATETIME_DATASIZE); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4869 |         if ((op == Py_EQ || op == Py_NE) && diff == 0) { | 
 | 4870 |             int ex = pep495_eq_exception(self, other, offset1, offset2); | 
 | 4871 |             if (ex == -1) | 
 | 4872 |                 goto done; | 
 | 4873 |             if (ex) | 
 | 4874 |                 diff = 1; | 
 | 4875 |         } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4876 |         result = diff_to_bool(diff, op); | 
 | 4877 |     } | 
 | 4878 |     else if (offset1 != Py_None && offset2 != Py_None) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4879 |         PyDateTime_Delta *delta; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4880 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4881 |         assert(offset1 != offset2); /* else last "if" handled it */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4882 |         delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, | 
 | 4883 |                                                        other); | 
 | 4884 |         if (delta == NULL) | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4885 |             goto done; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4886 |         diff = GET_TD_DAYS(delta); | 
 | 4887 |         if (diff == 0) | 
 | 4888 |             diff = GET_TD_SECONDS(delta) | | 
 | 4889 |                    GET_TD_MICROSECONDS(delta); | 
 | 4890 |         Py_DECREF(delta); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4891 |         if ((op == Py_EQ || op == Py_NE) && diff == 0) { | 
 | 4892 |             int ex = pep495_eq_exception(self, other, offset1, offset2); | 
 | 4893 |             if (ex == -1) | 
 | 4894 |                 goto done; | 
 | 4895 |             if (ex) | 
 | 4896 |                 diff = 1; | 
 | 4897 |         } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4898 |         result = diff_to_bool(diff, op); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4899 |     } | 
| Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4900 |     else if (op == Py_EQ) { | 
 | 4901 |         result = Py_False; | 
 | 4902 |         Py_INCREF(result); | 
 | 4903 |     } | 
 | 4904 |     else if (op == Py_NE) { | 
 | 4905 |         result = Py_True; | 
 | 4906 |         Py_INCREF(result); | 
 | 4907 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4908 |     else { | 
 | 4909 |         PyErr_SetString(PyExc_TypeError, | 
 | 4910 |                         "can't compare offset-naive and " | 
 | 4911 |                         "offset-aware datetimes"); | 
 | 4912 |     } | 
 | 4913 |  done: | 
 | 4914 |     Py_DECREF(offset1); | 
 | 4915 |     Py_XDECREF(offset2); | 
 | 4916 |     return result; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4917 | } | 
 | 4918 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4919 | static Py_hash_t | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4920 | datetime_hash(PyDateTime_DateTime *self) | 
 | 4921 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4922 |     if (self->hashcode == -1) { | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4923 |         PyObject *offset, *self0; | 
 | 4924 |         if (DATE_GET_FOLD(self)) { | 
 | 4925 |             self0 = new_datetime_ex2(GET_YEAR(self), | 
 | 4926 |                                      GET_MONTH(self), | 
 | 4927 |                                      GET_DAY(self), | 
 | 4928 |                                      DATE_GET_HOUR(self), | 
 | 4929 |                                      DATE_GET_MINUTE(self), | 
 | 4930 |                                      DATE_GET_SECOND(self), | 
 | 4931 |                                      DATE_GET_MICROSECOND(self), | 
 | 4932 |                                      HASTZINFO(self) ? self->tzinfo : Py_None, | 
 | 4933 |                                      0, Py_TYPE(self)); | 
 | 4934 |             if (self0 == NULL) | 
 | 4935 |                 return -1; | 
 | 4936 |         } | 
 | 4937 |         else { | 
 | 4938 |             self0 = (PyObject *)self; | 
 | 4939 |             Py_INCREF(self0); | 
 | 4940 |         } | 
 | 4941 |         offset = datetime_utcoffset(self0, NULL); | 
 | 4942 |         Py_DECREF(self0); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4943 |  | 
 | 4944 |         if (offset == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4945 |             return -1; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4946 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 |         /* Reduce this to a hash of another object. */ | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4948 |         if (offset == Py_None) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4949 |             self->hashcode = generic_hash( | 
 | 4950 |                 (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4951 |         else { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4952 |             PyObject *temp1, *temp2; | 
 | 4953 |             int days, seconds; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4954 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4955 |             assert(HASTZINFO(self)); | 
 | 4956 |             days = ymd_to_ord(GET_YEAR(self), | 
 | 4957 |                               GET_MONTH(self), | 
 | 4958 |                               GET_DAY(self)); | 
 | 4959 |             seconds = DATE_GET_HOUR(self) * 3600 + | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4960 |                       DATE_GET_MINUTE(self) * 60 + | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 |                       DATE_GET_SECOND(self); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4962 |             temp1 = new_delta(days, seconds, | 
 | 4963 |                               DATE_GET_MICROSECOND(self), | 
 | 4964 |                               1); | 
 | 4965 |             if (temp1 == NULL) { | 
 | 4966 |                 Py_DECREF(offset); | 
 | 4967 |                 return -1; | 
 | 4968 |             } | 
 | 4969 |             temp2 = delta_subtract(temp1, offset); | 
 | 4970 |             Py_DECREF(temp1); | 
 | 4971 |             if (temp2 == NULL) { | 
 | 4972 |                 Py_DECREF(offset); | 
 | 4973 |                 return -1; | 
 | 4974 |             } | 
 | 4975 |             self->hashcode = PyObject_Hash(temp2); | 
 | 4976 |             Py_DECREF(temp2); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 |         } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4978 |         Py_DECREF(offset); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 |     } | 
 | 4980 |     return self->hashcode; | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4981 | } | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4982 |  | 
 | 4983 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4984 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4985 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4986 |     PyObject *clone; | 
 | 4987 |     PyObject *tuple; | 
 | 4988 |     int y = GET_YEAR(self); | 
 | 4989 |     int m = GET_MONTH(self); | 
 | 4990 |     int d = GET_DAY(self); | 
 | 4991 |     int hh = DATE_GET_HOUR(self); | 
 | 4992 |     int mm = DATE_GET_MINUTE(self); | 
 | 4993 |     int ss = DATE_GET_SECOND(self); | 
 | 4994 |     int us = DATE_GET_MICROSECOND(self); | 
 | 4995 |     PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4996 |     int fold = DATE_GET_FOLD(self); | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4997 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4998 |     if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4999 |                                       datetime_kws, | 
 | 5000 |                                       &y, &m, &d, &hh, &mm, &ss, &us, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5001 |                                       &tzinfo, &fold)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 |         return NULL; | 
 | 5003 |     tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); | 
 | 5004 |     if (tuple == NULL) | 
 | 5005 |         return NULL; | 
 | 5006 |     clone = datetime_new(Py_TYPE(self), tuple, NULL); | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5007 |  | 
 | 5008 |     if (clone != NULL) { | 
 | 5009 |         if (fold != 0 && fold != 1) { | 
 | 5010 |             PyErr_SetString(PyExc_ValueError, | 
 | 5011 |                             "fold must be either 0 or 1"); | 
 | 5012 |             return NULL; | 
 | 5013 |         } | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5014 |         DATE_SET_FOLD(clone, fold); | 
| Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5015 |     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 |     Py_DECREF(tuple); | 
 | 5017 |     return clone; | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5018 | } | 
 | 5019 |  | 
 | 5020 | static PyObject * | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5021 | local_timezone_from_timestamp(time_t timestamp) | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5022 | { | 
 | 5023 |     PyObject *result = NULL; | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5024 |     PyObject *delta; | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5025 |     struct tm local_time_tm; | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5026 |     PyObject *nameo = NULL; | 
 | 5027 |     const char *zone = NULL; | 
 | 5028 |  | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5029 |     if (_PyTime_localtime(timestamp, &local_time_tm) != 0) | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5030 |         return NULL; | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5031 | #ifdef HAVE_STRUCT_TM_TM_ZONE | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5032 |     zone = local_time_tm.tm_zone; | 
 | 5033 |     delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5034 | #else /* HAVE_STRUCT_TM_TM_ZONE */ | 
 | 5035 |     { | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5036 |         PyObject *local_time, *utc_time; | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5037 |         struct tm utc_time_tm; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5038 |         char buf[100]; | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5039 |         strftime(buf, sizeof(buf), "%Z", &local_time_tm); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5040 |         zone = buf; | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5041 |         local_time = new_datetime(local_time_tm.tm_year + 1900, | 
 | 5042 |                                   local_time_tm.tm_mon + 1, | 
 | 5043 |                                   local_time_tm.tm_mday, | 
 | 5044 |                                   local_time_tm.tm_hour, | 
 | 5045 |                                   local_time_tm.tm_min, | 
 | 5046 |                                   local_time_tm.tm_sec, 0, Py_None, 0); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5047 |         if (local_time == NULL) { | 
 | 5048 |             return NULL; | 
 | 5049 |         } | 
| Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5050 |         if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5051 |             return NULL; | 
| Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5052 |         utc_time = new_datetime(utc_time_tm.tm_year + 1900, | 
 | 5053 |                                 utc_time_tm.tm_mon + 1, | 
 | 5054 |                                 utc_time_tm.tm_mday, | 
 | 5055 |                                 utc_time_tm.tm_hour, | 
 | 5056 |                                 utc_time_tm.tm_min, | 
 | 5057 |                                 utc_time_tm.tm_sec, 0, Py_None, 0); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5058 |         if (utc_time == NULL) { | 
 | 5059 |             Py_DECREF(local_time); | 
 | 5060 |             return NULL; | 
 | 5061 |         } | 
 | 5062 |         delta = datetime_subtract(local_time, utc_time); | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5063 |         Py_DECREF(local_time); | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5064 |         Py_DECREF(utc_time); | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5065 |     } | 
 | 5066 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5067 |     if (delta == NULL) { | 
 | 5068 |             return NULL; | 
 | 5069 |     } | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5070 |     if (zone != NULL) { | 
 | 5071 |         nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); | 
 | 5072 |         if (nameo == NULL) | 
 | 5073 |             goto error; | 
 | 5074 |     } | 
 | 5075 |     result = new_timezone(delta, nameo); | 
| Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 5076 |     Py_XDECREF(nameo); | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5077 |   error: | 
 | 5078 |     Py_DECREF(delta); | 
 | 5079 |     return result; | 
 | 5080 | } | 
 | 5081 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5082 | static PyObject * | 
 | 5083 | local_timezone(PyDateTime_DateTime *utc_time) | 
 | 5084 | { | 
 | 5085 |     time_t timestamp; | 
 | 5086 |     PyObject *delta; | 
 | 5087 |     PyObject *one_second; | 
 | 5088 |     PyObject *seconds; | 
 | 5089 |  | 
 | 5090 |     delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); | 
 | 5091 |     if (delta == NULL) | 
 | 5092 |         return NULL; | 
 | 5093 |     one_second = new_delta(0, 1, 0, 0); | 
 | 5094 |     if (one_second == NULL) { | 
 | 5095 |         Py_DECREF(delta); | 
 | 5096 |         return NULL; | 
 | 5097 |     } | 
 | 5098 |     seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, | 
 | 5099 |                                          (PyDateTime_Delta *)one_second); | 
 | 5100 |     Py_DECREF(one_second); | 
 | 5101 |     Py_DECREF(delta); | 
 | 5102 |     if (seconds == NULL) | 
 | 5103 |         return NULL; | 
 | 5104 |     timestamp = _PyLong_AsTime_t(seconds); | 
 | 5105 |     Py_DECREF(seconds); | 
 | 5106 |     if (timestamp == -1 && PyErr_Occurred()) | 
 | 5107 |         return NULL; | 
 | 5108 |     return local_timezone_from_timestamp(timestamp); | 
 | 5109 | } | 
 | 5110 |  | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5111 | static long long | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5112 | local_to_seconds(int year, int month, int day, | 
 | 5113 |                  int hour, int minute, int second, int fold); | 
 | 5114 |  | 
 | 5115 | static PyObject * | 
 | 5116 | local_timezone_from_local(PyDateTime_DateTime *local_dt) | 
 | 5117 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5118 |     long long seconds; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5119 |     time_t timestamp; | 
 | 5120 |     seconds = local_to_seconds(GET_YEAR(local_dt), | 
 | 5121 |                                GET_MONTH(local_dt), | 
 | 5122 |                                GET_DAY(local_dt), | 
 | 5123 |                                DATE_GET_HOUR(local_dt), | 
 | 5124 |                                DATE_GET_MINUTE(local_dt), | 
 | 5125 |                                DATE_GET_SECOND(local_dt), | 
 | 5126 |                                DATE_GET_FOLD(local_dt)); | 
 | 5127 |     if (seconds == -1) | 
 | 5128 |         return NULL; | 
 | 5129 |     /* XXX: add bounds check */ | 
 | 5130 |     timestamp = seconds - epoch; | 
 | 5131 |     return local_timezone_from_timestamp(timestamp); | 
 | 5132 | } | 
 | 5133 |  | 
| Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5134 | static PyDateTime_DateTime * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5135 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) | 
| Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5136 | { | 
| Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5137 |     PyDateTime_DateTime *result; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5138 |     PyObject *offset; | 
 | 5139 |     PyObject *temp; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5140 |     PyObject *self_tzinfo; | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5141 |     PyObject *tzinfo = Py_None; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5142 |     static char *keywords[] = {"tz", NULL}; | 
| Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5143 |  | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5144 |     if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, | 
| Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 5145 |                                       &tzinfo)) | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5146 |         return NULL; | 
 | 5147 |  | 
 | 5148 |     if (check_tzinfo_subclass(tzinfo) == -1) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5149 |         return NULL; | 
| Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5150 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5151 |     if (!HASTZINFO(self) || self->tzinfo == Py_None) { | 
 | 5152 |         self_tzinfo = local_timezone_from_local(self); | 
 | 5153 |         if (self_tzinfo == NULL) | 
 | 5154 |             return NULL; | 
 | 5155 |     } else { | 
 | 5156 |         self_tzinfo = self->tzinfo; | 
 | 5157 |         Py_INCREF(self_tzinfo); | 
 | 5158 |     } | 
| Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5159 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5160 |     /* Conversion to self's own time zone is a NOP. */ | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5161 |     if (self_tzinfo == tzinfo) { | 
 | 5162 |         Py_DECREF(self_tzinfo); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5163 |         Py_INCREF(self); | 
| Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5164 |         return self; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5165 |     } | 
| Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5166 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5167 |     /* Convert self to UTC. */ | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5168 |     offset = call_utcoffset(self_tzinfo, (PyObject *)self); | 
 | 5169 |     Py_DECREF(self_tzinfo); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5170 |     if (offset == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 |         return NULL; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5172 |     /* result = self - offset */ | 
| Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5173 |     result = (PyDateTime_DateTime *)add_datetime_timedelta(self, | 
 | 5174 |                                        (PyDateTime_Delta *)offset, -1); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5175 |     Py_DECREF(offset); | 
 | 5176 |     if (result == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5177 |         return NULL; | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5178 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5179 |     /* Make sure result is aware and UTC. */ | 
 | 5180 |     if (!HASTZINFO(result)) { | 
 | 5181 |         temp = (PyObject *)result; | 
 | 5182 |         result = (PyDateTime_DateTime *) | 
 | 5183 |                    new_datetime_ex2(GET_YEAR(result), | 
 | 5184 |                                     GET_MONTH(result), | 
 | 5185 |                                     GET_DAY(result), | 
 | 5186 |                                     DATE_GET_HOUR(result), | 
 | 5187 |                                     DATE_GET_MINUTE(result), | 
 | 5188 |                                     DATE_GET_SECOND(result), | 
 | 5189 |                                     DATE_GET_MICROSECOND(result), | 
 | 5190 |                                     PyDateTime_TimeZone_UTC, | 
 | 5191 |                                     DATE_GET_FOLD(result), | 
 | 5192 |                                     Py_TYPE(result)); | 
 | 5193 |         Py_DECREF(temp); | 
 | 5194 |         if (result == NULL) | 
 | 5195 |             return NULL; | 
 | 5196 |     } | 
 | 5197 |     else { | 
 | 5198 |         /* Result is already aware - just replace tzinfo. */ | 
 | 5199 |         temp = result->tzinfo; | 
 | 5200 |         result->tzinfo = PyDateTime_TimeZone_UTC; | 
 | 5201 |         Py_INCREF(result->tzinfo); | 
 | 5202 |         Py_DECREF(temp); | 
 | 5203 |     } | 
 | 5204 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5205 |     /* Attach new tzinfo and let fromutc() do the rest. */ | 
| Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5206 |     temp = result->tzinfo; | 
| Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5207 |     if (tzinfo == Py_None) { | 
 | 5208 |         tzinfo = local_timezone(result); | 
 | 5209 |         if (tzinfo == NULL) { | 
 | 5210 |             Py_DECREF(result); | 
 | 5211 |             return NULL; | 
 | 5212 |         } | 
 | 5213 |     } | 
 | 5214 |     else | 
 | 5215 |       Py_INCREF(tzinfo); | 
| Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5216 |     result->tzinfo = tzinfo; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5217 |     Py_DECREF(temp); | 
| Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5218 |  | 
| Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5219 |     temp = (PyObject *)result; | 
| Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5220 |     result = (PyDateTime_DateTime *) | 
 | 5221 |         _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", temp); | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5222 |     Py_DECREF(temp); | 
 | 5223 |  | 
| Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5224 |     return result; | 
| Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5225 | } | 
 | 5226 |  | 
 | 5227 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5228 | datetime_timetuple(PyDateTime_DateTime *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5229 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 |     int dstflag = -1; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5231 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 |     if (HASTZINFO(self) && self->tzinfo != Py_None) { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5233 |         PyObject * dst; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5234 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5235 |         dst = call_dst(self->tzinfo, (PyObject *)self); | 
 | 5236 |         if (dst == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5237 |             return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5238 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5239 |         if (dst != Py_None) | 
 | 5240 |             dstflag = delta_bool((PyDateTime_Delta *)dst); | 
 | 5241 |         Py_DECREF(dst); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5242 |     } | 
 | 5243 |     return build_struct_time(GET_YEAR(self), | 
 | 5244 |                              GET_MONTH(self), | 
 | 5245 |                              GET_DAY(self), | 
 | 5246 |                              DATE_GET_HOUR(self), | 
 | 5247 |                              DATE_GET_MINUTE(self), | 
 | 5248 |                              DATE_GET_SECOND(self), | 
 | 5249 |                              dstflag); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5250 | } | 
 | 5251 |  | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5252 | static long long | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5253 | local_to_seconds(int year, int month, int day, | 
 | 5254 |                  int hour, int minute, int second, int fold) | 
 | 5255 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5256 |     long long t, a, b, u1, u2, t1, t2, lt; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5257 |     t = utc_to_seconds(year, month, day, hour, minute, second); | 
 | 5258 |     /* Our goal is to solve t = local(u) for u. */ | 
 | 5259 |     lt = local(t); | 
 | 5260 |     if (lt == -1) | 
 | 5261 |         return -1; | 
 | 5262 |     a = lt - t; | 
 | 5263 |     u1 = t - a; | 
 | 5264 |     t1 = local(u1); | 
 | 5265 |     if (t1 == -1) | 
 | 5266 |         return -1; | 
 | 5267 |     if (t1 == t) { | 
 | 5268 |         /* We found one solution, but it may not be the one we need. | 
 | 5269 |          * Look for an earlier solution (if `fold` is 0), or a | 
 | 5270 |          * later one (if `fold` is 1). */ | 
 | 5271 |         if (fold) | 
 | 5272 |             u2 = u1 + max_fold_seconds; | 
 | 5273 |         else | 
 | 5274 |             u2 = u1 - max_fold_seconds; | 
 | 5275 |         lt = local(u2); | 
 | 5276 |         if (lt == -1) | 
 | 5277 |             return -1; | 
 | 5278 |         b = lt - u2; | 
 | 5279 |         if (a == b) | 
 | 5280 |             return u1; | 
 | 5281 |     } | 
 | 5282 |     else { | 
 | 5283 |         b = t1 - u1; | 
 | 5284 |         assert(a != b); | 
 | 5285 |     } | 
 | 5286 |     u2 = t - b; | 
 | 5287 |     t2 = local(u2); | 
 | 5288 |     if (t2 == -1) | 
 | 5289 |         return -1; | 
 | 5290 |     if (t2 == t) | 
 | 5291 |         return u2; | 
 | 5292 |     if (t1 == t) | 
 | 5293 |         return u1; | 
 | 5294 |     /* We have found both offsets a and b, but neither t - a nor t - b is | 
 | 5295 |      * a solution.  This means t is in the gap. */ | 
 | 5296 |     return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); | 
 | 5297 | } | 
 | 5298 |  | 
 | 5299 | /* date(1970,1,1).toordinal() == 719163 */ | 
 | 5300 | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) | 
 | 5301 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5302 | static PyObject * | 
| Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5303 | datetime_timestamp(PyDateTime_DateTime *self) | 
 | 5304 | { | 
 | 5305 |     PyObject *result; | 
 | 5306 |  | 
 | 5307 |     if (HASTZINFO(self) && self->tzinfo != Py_None) { | 
 | 5308 |         PyObject *delta; | 
 | 5309 |         delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); | 
 | 5310 |         if (delta == NULL) | 
 | 5311 |             return NULL; | 
 | 5312 |         result = delta_total_seconds(delta); | 
 | 5313 |         Py_DECREF(delta); | 
 | 5314 |     } | 
 | 5315 |     else { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5316 |         long long seconds; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5317 |         seconds = local_to_seconds(GET_YEAR(self), | 
 | 5318 |                                    GET_MONTH(self), | 
 | 5319 |                                    GET_DAY(self), | 
 | 5320 |                                    DATE_GET_HOUR(self), | 
 | 5321 |                                    DATE_GET_MINUTE(self), | 
 | 5322 |                                    DATE_GET_SECOND(self), | 
 | 5323 |                                    DATE_GET_FOLD(self)); | 
 | 5324 |         if (seconds == -1) | 
| Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5325 |             return NULL; | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5326 |         result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + | 
 | 5327 |                                     DATE_GET_MICROSECOND(self) / 1e6); | 
| Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5328 |     } | 
 | 5329 |     return result; | 
 | 5330 | } | 
 | 5331 |  | 
 | 5332 | static PyObject * | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5333 | datetime_getdate(PyDateTime_DateTime *self) | 
 | 5334 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 |     return new_date(GET_YEAR(self), | 
 | 5336 |                     GET_MONTH(self), | 
 | 5337 |                     GET_DAY(self)); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5338 | } | 
 | 5339 |  | 
 | 5340 | static PyObject * | 
 | 5341 | datetime_gettime(PyDateTime_DateTime *self) | 
 | 5342 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5343 |     return new_time(DATE_GET_HOUR(self), | 
 | 5344 |                     DATE_GET_MINUTE(self), | 
 | 5345 |                     DATE_GET_SECOND(self), | 
 | 5346 |                     DATE_GET_MICROSECOND(self), | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5347 |                     Py_None, | 
 | 5348 |                     DATE_GET_FOLD(self)); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5349 | } | 
 | 5350 |  | 
 | 5351 | static PyObject * | 
 | 5352 | datetime_gettimetz(PyDateTime_DateTime *self) | 
 | 5353 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5354 |     return new_time(DATE_GET_HOUR(self), | 
 | 5355 |                     DATE_GET_MINUTE(self), | 
 | 5356 |                     DATE_GET_SECOND(self), | 
 | 5357 |                     DATE_GET_MICROSECOND(self), | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5358 |                     GET_DT_TZINFO(self), | 
 | 5359 |                     DATE_GET_FOLD(self)); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5360 | } | 
 | 5361 |  | 
 | 5362 | static PyObject * | 
 | 5363 | datetime_utctimetuple(PyDateTime_DateTime *self) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5364 | { | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5365 |     int y, m, d, hh, mm, ss; | 
 | 5366 |     PyObject *tzinfo; | 
 | 5367 |     PyDateTime_DateTime *utcself; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5368 |  | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5369 |     tzinfo = GET_DT_TZINFO(self); | 
 | 5370 |     if (tzinfo == Py_None) { | 
 | 5371 |         utcself = self; | 
 | 5372 |         Py_INCREF(utcself); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5373 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5374 |     else { | 
 | 5375 |         PyObject *offset; | 
 | 5376 |         offset = call_utcoffset(tzinfo, (PyObject *)self); | 
 | 5377 |         if (offset == NULL) | 
| Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 5378 |             return NULL; | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5379 |         if (offset == Py_None) { | 
 | 5380 |             Py_DECREF(offset); | 
 | 5381 |             utcself = self; | 
 | 5382 |             Py_INCREF(utcself); | 
 | 5383 |         } | 
 | 5384 |         else { | 
 | 5385 |             utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, | 
 | 5386 |                                                 (PyDateTime_Delta *)offset, -1); | 
 | 5387 |             Py_DECREF(offset); | 
 | 5388 |             if (utcself == NULL) | 
 | 5389 |                 return NULL; | 
 | 5390 |         } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5391 |     } | 
| Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5392 |     y = GET_YEAR(utcself); | 
 | 5393 |     m = GET_MONTH(utcself); | 
 | 5394 |     d = GET_DAY(utcself); | 
 | 5395 |     hh = DATE_GET_HOUR(utcself); | 
 | 5396 |     mm = DATE_GET_MINUTE(utcself); | 
 | 5397 |     ss = DATE_GET_SECOND(utcself); | 
 | 5398 |  | 
 | 5399 |     Py_DECREF(utcself); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5400 |     return build_struct_time(y, m, d, hh, mm, ss, 0); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5401 | } | 
 | 5402 |  | 
| Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 5403 | /* Pickle support, a simple use of __reduce__. */ | 
| Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 5404 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5405 | /* Let basestate be the non-tzinfo data string. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5406 |  * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). | 
 | 5407 |  * So it's a tuple in any (non-error) case. | 
| Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 5408 |  * __getstate__ isn't exposed. | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5409 |  */ | 
 | 5410 | static PyObject * | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5411 | datetime_getstate(PyDateTime_DateTime *self, int proto) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5412 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 |     PyObject *basestate; | 
 | 5414 |     PyObject *result = NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5415 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5416 |     basestate = PyBytes_FromStringAndSize((char *)self->data, | 
 | 5417 |                                            _PyDateTime_DATETIME_DATASIZE); | 
 | 5418 |     if (basestate != NULL) { | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5419 |         if (proto > 3 && DATE_GET_FOLD(self)) | 
 | 5420 |             /* Set the first bit of the third byte */ | 
 | 5421 |             PyBytes_AS_STRING(basestate)[2] |= (1 << 7); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5422 |         if (! HASTZINFO(self) || self->tzinfo == Py_None) | 
 | 5423 |             result = PyTuple_Pack(1, basestate); | 
 | 5424 |         else | 
 | 5425 |             result = PyTuple_Pack(2, basestate, self->tzinfo); | 
 | 5426 |         Py_DECREF(basestate); | 
 | 5427 |     } | 
 | 5428 |     return result; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5429 | } | 
 | 5430 |  | 
 | 5431 | static PyObject * | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 5432 | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5433 | { | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 5434 |     int proto; | 
 | 5435 |     if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5436 |         return NULL; | 
 | 5437 |  | 
 | 5438 |     return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5439 | } | 
 | 5440 |  | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 5441 | static PyObject * | 
 | 5442 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) | 
 | 5443 | { | 
 | 5444 |     return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); | 
 | 5445 | } | 
 | 5446 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5447 | static PyMethodDef datetime_methods[] = { | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5448 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5449 |     /* Class methods: */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5450 |  | 
| Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 5451 |     DATETIME_DATETIME_NOW_METHODDEF | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5452 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5453 |     {"utcnow",         (PyCFunction)datetime_utcnow, | 
 | 5454 |      METH_NOARGS | METH_CLASS, | 
 | 5455 |      PyDoc_STR("Return a new datetime representing UTC day and time.")}, | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5456 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5457 |     {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, | 
 | 5458 |      METH_VARARGS | METH_KEYWORDS | METH_CLASS, | 
 | 5459 |      PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5460 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5461 |     {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, | 
 | 5462 |      METH_VARARGS | METH_CLASS, | 
| Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 5463 |      PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5464 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 |     {"strptime", (PyCFunction)datetime_strptime, | 
 | 5466 |      METH_VARARGS | METH_CLASS, | 
 | 5467 |      PyDoc_STR("string, format -> new datetime parsed from a string " | 
 | 5468 |                "(like time.strptime()).")}, | 
| Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5469 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5470 |     {"combine", (PyCFunction)datetime_combine, | 
 | 5471 |      METH_VARARGS | METH_KEYWORDS | METH_CLASS, | 
 | 5472 |      PyDoc_STR("date, time -> datetime with same date and time fields")}, | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5473 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5474 |     /* Instance methods: */ | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5475 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5476 |     {"date",   (PyCFunction)datetime_getdate, METH_NOARGS, | 
 | 5477 |      PyDoc_STR("Return date object with same year, month and day.")}, | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5478 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 |     {"time",   (PyCFunction)datetime_gettime, METH_NOARGS, | 
 | 5480 |      PyDoc_STR("Return time object with same time but with tzinfo=None.")}, | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5481 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5482 |     {"timetz",   (PyCFunction)datetime_gettimetz, METH_NOARGS, | 
 | 5483 |      PyDoc_STR("Return time object with same time and tzinfo.")}, | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5484 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5485 |     {"ctime",       (PyCFunction)datetime_ctime,        METH_NOARGS, | 
 | 5486 |      PyDoc_STR("Return ctime() style string.")}, | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5487 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5488 |     {"timetuple",   (PyCFunction)datetime_timetuple, METH_NOARGS, | 
 | 5489 |      PyDoc_STR("Return time tuple, compatible with time.localtime().")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5490 |  | 
| Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5491 |     {"timestamp",   (PyCFunction)datetime_timestamp, METH_NOARGS, | 
 | 5492 |      PyDoc_STR("Return POSIX timestamp as float.")}, | 
 | 5493 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 |     {"utctimetuple",   (PyCFunction)datetime_utctimetuple, METH_NOARGS, | 
 | 5495 |      PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5496 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5497 |     {"isoformat",   (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, | 
 | 5498 |      PyDoc_STR("[sep] -> string in ISO 8601 format, " | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5499 |                "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5500 |                "sep is used to separate the year from the time, and " | 
| Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5501 |                "defaults to 'T'.\n" | 
 | 5502 |                "timespec specifies what components of the time to include" | 
 | 5503 |                " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," | 
 | 5504 |                " 'milliseconds', and 'microseconds').\n")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5505 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5506 |     {"utcoffset",       (PyCFunction)datetime_utcoffset, METH_NOARGS, | 
 | 5507 |      PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5508 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5509 |     {"tzname",          (PyCFunction)datetime_tzname,   METH_NOARGS, | 
 | 5510 |      PyDoc_STR("Return self.tzinfo.tzname(self).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5511 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 |     {"dst",             (PyCFunction)datetime_dst, METH_NOARGS, | 
 | 5513 |      PyDoc_STR("Return self.tzinfo.dst(self).")}, | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5514 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5515 |     {"replace",     (PyCFunction)datetime_replace,      METH_VARARGS | METH_KEYWORDS, | 
 | 5516 |      PyDoc_STR("Return datetime with new specified fields.")}, | 
| Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5517 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5518 |     {"astimezone",  (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, | 
 | 5519 |      PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, | 
| Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5520 |  | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 5521 |     {"__reduce_ex__", (PyCFunction)datetime_reduce_ex,     METH_VARARGS, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5522 |      PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, | 
| Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5523 |  | 
| Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame^] | 5524 |     {"__reduce__", (PyCFunction)datetime_reduce,     METH_NOARGS, | 
 | 5525 |      PyDoc_STR("__reduce__() -> (cls, state)")}, | 
 | 5526 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5527 |     {NULL,      NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5528 | }; | 
 | 5529 |  | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 5530 | static const char datetime_doc[] = | 
| Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5531 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ | 
 | 5532 | \n\ | 
 | 5533 | The year, month and day arguments are required. tzinfo may be None, or an\n\ | 
| Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5534 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5535 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5536 | static PyNumberMethods datetime_as_number = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5537 |     datetime_add,                               /* nb_add */ | 
 | 5538 |     datetime_subtract,                          /* nb_subtract */ | 
 | 5539 |     0,                                          /* nb_multiply */ | 
 | 5540 |     0,                                          /* nb_remainder */ | 
 | 5541 |     0,                                          /* nb_divmod */ | 
 | 5542 |     0,                                          /* nb_power */ | 
 | 5543 |     0,                                          /* nb_negative */ | 
 | 5544 |     0,                                          /* nb_positive */ | 
 | 5545 |     0,                                          /* nb_absolute */ | 
 | 5546 |     0,                                          /* nb_bool */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5547 | }; | 
 | 5548 |  | 
| Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5549 | static PyTypeObject PyDateTime_DateTimeType = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 5551 |     "datetime.datetime",                        /* tp_name */ | 
 | 5552 |     sizeof(PyDateTime_DateTime),                /* tp_basicsize */ | 
 | 5553 |     0,                                          /* tp_itemsize */ | 
 | 5554 |     (destructor)datetime_dealloc,               /* tp_dealloc */ | 
 | 5555 |     0,                                          /* tp_print */ | 
 | 5556 |     0,                                          /* tp_getattr */ | 
 | 5557 |     0,                                          /* tp_setattr */ | 
 | 5558 |     0,                                          /* tp_reserved */ | 
 | 5559 |     (reprfunc)datetime_repr,                    /* tp_repr */ | 
 | 5560 |     &datetime_as_number,                        /* tp_as_number */ | 
 | 5561 |     0,                                          /* tp_as_sequence */ | 
 | 5562 |     0,                                          /* tp_as_mapping */ | 
 | 5563 |     (hashfunc)datetime_hash,                    /* tp_hash */ | 
 | 5564 |     0,                                          /* tp_call */ | 
 | 5565 |     (reprfunc)datetime_str,                     /* tp_str */ | 
 | 5566 |     PyObject_GenericGetAttr,                    /* tp_getattro */ | 
 | 5567 |     0,                                          /* tp_setattro */ | 
 | 5568 |     0,                                          /* tp_as_buffer */ | 
 | 5569 |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | 
 | 5570 |     datetime_doc,                               /* tp_doc */ | 
 | 5571 |     0,                                          /* tp_traverse */ | 
 | 5572 |     0,                                          /* tp_clear */ | 
 | 5573 |     datetime_richcompare,                       /* tp_richcompare */ | 
 | 5574 |     0,                                          /* tp_weaklistoffset */ | 
 | 5575 |     0,                                          /* tp_iter */ | 
 | 5576 |     0,                                          /* tp_iternext */ | 
 | 5577 |     datetime_methods,                           /* tp_methods */ | 
 | 5578 |     0,                                          /* tp_members */ | 
 | 5579 |     datetime_getset,                            /* tp_getset */ | 
 | 5580 |     &PyDateTime_DateType,                       /* tp_base */ | 
 | 5581 |     0,                                          /* tp_dict */ | 
 | 5582 |     0,                                          /* tp_descr_get */ | 
 | 5583 |     0,                                          /* tp_descr_set */ | 
 | 5584 |     0,                                          /* tp_dictoffset */ | 
 | 5585 |     0,                                          /* tp_init */ | 
 | 5586 |     datetime_alloc,                             /* tp_alloc */ | 
 | 5587 |     datetime_new,                               /* tp_new */ | 
 | 5588 |     0,                                          /* tp_free */ | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5589 | }; | 
 | 5590 |  | 
 | 5591 | /* --------------------------------------------------------------------------- | 
 | 5592 |  * Module methods and initialization. | 
 | 5593 |  */ | 
 | 5594 |  | 
 | 5595 | static PyMethodDef module_methods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5596 |     {NULL, NULL} | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5597 | }; | 
 | 5598 |  | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5599 | /* C API.  Clients get at this via PyDateTime_IMPORT, defined in | 
 | 5600 |  * datetime.h. | 
 | 5601 |  */ | 
 | 5602 | static PyDateTime_CAPI CAPI = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5603 |     &PyDateTime_DateType, | 
 | 5604 |     &PyDateTime_DateTimeType, | 
 | 5605 |     &PyDateTime_TimeType, | 
 | 5606 |     &PyDateTime_DeltaType, | 
 | 5607 |     &PyDateTime_TZInfoType, | 
 | 5608 |     new_date_ex, | 
 | 5609 |     new_datetime_ex, | 
 | 5610 |     new_time_ex, | 
 | 5611 |     new_delta_ex, | 
 | 5612 |     datetime_fromtimestamp, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5613 |     date_fromtimestamp, | 
 | 5614 |     new_datetime_ex2, | 
 | 5615 |     new_time_ex2 | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5616 | }; | 
 | 5617 |  | 
 | 5618 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5619 |  | 
 | 5620 | static struct PyModuleDef datetimemodule = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5621 |     PyModuleDef_HEAD_INIT, | 
| Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5622 |     "_datetime", | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5623 |     "Fast implementation of the datetime type.", | 
 | 5624 |     -1, | 
 | 5625 |     module_methods, | 
 | 5626 |     NULL, | 
 | 5627 |     NULL, | 
 | 5628 |     NULL, | 
 | 5629 |     NULL | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5630 | }; | 
 | 5631 |  | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5632 | PyMODINIT_FUNC | 
| Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5633 | PyInit__datetime(void) | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5634 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5635 |     PyObject *m;        /* a module object */ | 
 | 5636 |     PyObject *d;        /* its dict */ | 
 | 5637 |     PyObject *x; | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5638 |     PyObject *delta; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5639 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5640 |     m = PyModule_Create(&datetimemodule); | 
 | 5641 |     if (m == NULL) | 
 | 5642 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5643 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5644 |     if (PyType_Ready(&PyDateTime_DateType) < 0) | 
 | 5645 |         return NULL; | 
 | 5646 |     if (PyType_Ready(&PyDateTime_DateTimeType) < 0) | 
 | 5647 |         return NULL; | 
 | 5648 |     if (PyType_Ready(&PyDateTime_DeltaType) < 0) | 
 | 5649 |         return NULL; | 
 | 5650 |     if (PyType_Ready(&PyDateTime_TimeType) < 0) | 
 | 5651 |         return NULL; | 
 | 5652 |     if (PyType_Ready(&PyDateTime_TZInfoType) < 0) | 
 | 5653 |         return NULL; | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5654 |     if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) | 
 | 5655 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5656 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5657 |     /* timedelta values */ | 
 | 5658 |     d = PyDateTime_DeltaType.tp_dict; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5659 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5660 |     x = new_delta(0, 0, 1, 0); | 
 | 5661 |     if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) | 
 | 5662 |         return NULL; | 
 | 5663 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5664 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5665 |     x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); | 
 | 5666 |     if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) | 
 | 5667 |         return NULL; | 
 | 5668 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5669 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5670 |     x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); | 
 | 5671 |     if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) | 
 | 5672 |         return NULL; | 
 | 5673 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5674 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5675 |     /* date values */ | 
 | 5676 |     d = PyDateTime_DateType.tp_dict; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5677 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 |     x = new_date(1, 1, 1); | 
 | 5679 |     if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) | 
 | 5680 |         return NULL; | 
 | 5681 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5682 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 |     x = new_date(MAXYEAR, 12, 31); | 
 | 5684 |     if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) | 
 | 5685 |         return NULL; | 
 | 5686 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5687 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5688 |     x = new_delta(1, 0, 0, 0); | 
 | 5689 |     if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) | 
 | 5690 |         return NULL; | 
 | 5691 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5692 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5693 |     /* time values */ | 
 | 5694 |     d = PyDateTime_TimeType.tp_dict; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5695 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5696 |     x = new_time(0, 0, 0, 0, Py_None, 0); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5697 |     if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) | 
 | 5698 |         return NULL; | 
 | 5699 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5700 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5701 |     x = new_time(23, 59, 59, 999999, Py_None, 0); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5702 |     if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) | 
 | 5703 |         return NULL; | 
 | 5704 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5705 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 |     x = new_delta(0, 0, 1, 0); | 
 | 5707 |     if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) | 
 | 5708 |         return NULL; | 
 | 5709 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5710 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5711 |     /* datetime values */ | 
 | 5712 |     d = PyDateTime_DateTimeType.tp_dict; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5713 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5714 |     x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None, 0); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 |     if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) | 
 | 5716 |         return NULL; | 
 | 5717 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5718 |  | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5719 |     x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None, 0); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5720 |     if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) | 
 | 5721 |         return NULL; | 
 | 5722 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5723 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5724 |     x = new_delta(0, 0, 1, 0); | 
 | 5725 |     if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) | 
 | 5726 |         return NULL; | 
 | 5727 |     Py_DECREF(x); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5728 |  | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5729 |     /* timezone values */ | 
 | 5730 |     d = PyDateTime_TimeZoneType.tp_dict; | 
 | 5731 |  | 
 | 5732 |     delta = new_delta(0, 0, 0, 0); | 
 | 5733 |     if (delta == NULL) | 
 | 5734 |         return NULL; | 
| Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5735 |     x = create_timezone(delta, NULL); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5736 |     Py_DECREF(delta); | 
 | 5737 |     if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) | 
 | 5738 |         return NULL; | 
| Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 5739 |     PyDateTime_TimeZone_UTC = x; | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5740 |  | 
 | 5741 |     delta = new_delta(-1, 60, 0, 1); /* -23:59 */ | 
 | 5742 |     if (delta == NULL) | 
 | 5743 |         return NULL; | 
| Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5744 |     x = create_timezone(delta, NULL); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5745 |     Py_DECREF(delta); | 
 | 5746 |     if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) | 
 | 5747 |         return NULL; | 
 | 5748 |     Py_DECREF(x); | 
 | 5749 |  | 
 | 5750 |     delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ | 
 | 5751 |     if (delta == NULL) | 
 | 5752 |         return NULL; | 
| Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5753 |     x = create_timezone(delta, NULL); | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5754 |     Py_DECREF(delta); | 
 | 5755 |     if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) | 
 | 5756 |         return NULL; | 
 | 5757 |     Py_DECREF(x); | 
 | 5758 |  | 
| Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5759 |     /* Epoch */ | 
 | 5760 |     PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, | 
| Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5761 |                                     PyDateTime_TimeZone_UTC, 0); | 
| Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5762 |     if (PyDateTime_Epoch == NULL) | 
 | 5763 |       return NULL; | 
 | 5764 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5765 |     /* module initialization */ | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5766 |     PyModule_AddIntMacro(m, MINYEAR); | 
 | 5767 |     PyModule_AddIntMacro(m, MAXYEAR); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5768 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5769 |     Py_INCREF(&PyDateTime_DateType); | 
 | 5770 |     PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5771 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5772 |     Py_INCREF(&PyDateTime_DateTimeType); | 
 | 5773 |     PyModule_AddObject(m, "datetime", | 
 | 5774 |                        (PyObject *)&PyDateTime_DateTimeType); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5775 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5776 |     Py_INCREF(&PyDateTime_TimeType); | 
 | 5777 |     PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5778 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5779 |     Py_INCREF(&PyDateTime_DeltaType); | 
 | 5780 |     PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5781 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5782 |     Py_INCREF(&PyDateTime_TZInfoType); | 
 | 5783 |     PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5784 |  | 
| Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5785 |     Py_INCREF(&PyDateTime_TimeZoneType); | 
 | 5786 |     PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); | 
 | 5787 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5788 |     x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); | 
 | 5789 |     if (x == NULL) | 
 | 5790 |         return NULL; | 
 | 5791 |     PyModule_AddObject(m, "datetime_CAPI", x); | 
| Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5792 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5793 |     /* A 4-year cycle has an extra leap day over what we'd get from | 
 | 5794 |      * pasting together 4 single years. | 
 | 5795 |      */ | 
| Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5796 |     Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 |     assert(DI4Y == days_before_year(4+1)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5798 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5799 |     /* Similarly, a 400-year cycle has an extra leap day over what we'd | 
 | 5800 |      * get from pasting together 4 100-year cycles. | 
 | 5801 |      */ | 
| Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5802 |     Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5803 |     assert(DI400Y == days_before_year(400+1)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5804 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5805 |     /* OTOH, a 100-year cycle has one fewer leap day than we'd get from | 
 | 5806 |      * pasting together 25 4-year cycles. | 
 | 5807 |      */ | 
| Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5808 |     Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5809 |     assert(DI100Y == days_before_year(100+1)); | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5810 |  | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5811 |     one = PyLong_FromLong(1); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 |     us_per_ms = PyLong_FromLong(1000); | 
 | 5813 |     us_per_second = PyLong_FromLong(1000000); | 
 | 5814 |     us_per_minute = PyLong_FromLong(60000000); | 
 | 5815 |     seconds_per_day = PyLong_FromLong(24 * 3600); | 
| Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5816 |     if (one == NULL || us_per_ms == NULL || us_per_second == NULL || | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5817 |         us_per_minute == NULL || seconds_per_day == NULL) | 
 | 5818 |         return NULL; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5819 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5820 |     /* The rest are too big for 32-bit ints, but even | 
 | 5821 |      * us_per_week fits in 40 bits, so doubles should be exact. | 
 | 5822 |      */ | 
 | 5823 |     us_per_hour = PyLong_FromDouble(3600000000.0); | 
 | 5824 |     us_per_day = PyLong_FromDouble(86400000000.0); | 
 | 5825 |     us_per_week = PyLong_FromDouble(604800000000.0); | 
 | 5826 |     if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) | 
 | 5827 |         return NULL; | 
 | 5828 |     return m; | 
| Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5829 | } | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5830 |  | 
 | 5831 | /* --------------------------------------------------------------------------- | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5832 | Some time zone algebra.  For a datetime x, let | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5833 |     x.n = x stripped of its timezone -- its naive time. | 
 | 5834 |     x.o = x.utcoffset(), and assuming that doesn't raise an exception or | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5835 |       return None | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5836 |     x.d = x.dst(), and assuming that doesn't raise an exception or | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5837 |       return None | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5838 |     x.s = x's standard offset, x.o - x.d | 
 | 5839 |  | 
 | 5840 | Now some derived rules, where k is a duration (timedelta). | 
 | 5841 |  | 
 | 5842 | 1. x.o = x.s + x.d | 
 | 5843 |    This follows from the definition of x.s. | 
 | 5844 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5845 | 2. If x and y have the same tzinfo member, x.s = y.s. | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5846 |    This is actually a requirement, an assumption we need to make about | 
 | 5847 |    sane tzinfo classes. | 
 | 5848 |  | 
 | 5849 | 3. The naive UTC time corresponding to x is x.n - x.o. | 
 | 5850 |    This is again a requirement for a sane tzinfo class. | 
 | 5851 |  | 
 | 5852 | 4. (x+k).s = x.s | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5853 |    This follows from #2, and that datimetimetz+timedelta preserves tzinfo. | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5854 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5855 | 5. (x+k).n = x.n + k | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5856 |    Again follows from how arithmetic is defined. | 
 | 5857 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5858 | Now we can explain tz.fromutc(x).  Let's assume it's an interesting case | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5859 | (meaning that the various tzinfo methods exist, and don't blow up or return | 
 | 5860 | None when called). | 
 | 5861 |  | 
| Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5862 | The function wants to return a datetime y with timezone tz, equivalent to x. | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5863 | x is already in UTC. | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5864 |  | 
 | 5865 | By #3, we want | 
 | 5866 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5867 |     y.n - y.o = x.n                             [1] | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5868 |  | 
 | 5869 | The algorithm starts by attaching tz to x.n, and calling that y.  So | 
 | 5870 | x.n = y.n at the start.  Then it wants to add a duration k to y, so that [1] | 
 | 5871 | becomes true; in effect, we want to solve [2] for k: | 
 | 5872 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5873 |    (y+k).n - (y+k).o = x.n                      [2] | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5874 |  | 
 | 5875 | By #1, this is the same as | 
 | 5876 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5877 |    (y+k).n - ((y+k).s + (y+k).d) = x.n          [3] | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5878 |  | 
 | 5879 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. | 
 | 5880 | Substituting that into [3], | 
 | 5881 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5882 |    x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving | 
 | 5883 |    k - (y+k).s - (y+k).d = 0; rearranging, | 
 | 5884 |    k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so | 
 | 5885 |    k = y.s - (y+k).d | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5886 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5887 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we | 
 | 5888 | approximate k by ignoring the (y+k).d term at first.  Note that k can't be | 
 | 5889 | very large, since all offset-returning methods return a duration of magnitude | 
 | 5890 | less than 24 hours.  For that reason, if y is firmly in std time, (y+k).d must | 
 | 5891 | be 0, so ignoring it has no consequence then. | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5892 |  | 
 | 5893 | In any case, the new value is | 
 | 5894 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5895 |     z = y + y.s                                 [4] | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5896 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5897 | It's helpful to step back at look at [4] from a higher level:  it's simply | 
 | 5898 | mapping from UTC to tz's standard time. | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5899 |  | 
 | 5900 | At this point, if | 
 | 5901 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5902 |     z.n - z.o = x.n                             [5] | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5903 |  | 
 | 5904 | we have an equivalent time, and are almost done.  The insecurity here is | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5905 | at the start of daylight time.  Picture US Eastern for concreteness.  The wall | 
 | 5906 | time jumps from 1:59 to 3:00, and wall hours of the form 2:MM don't make good | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5907 | sense then.  The docs ask that an Eastern tzinfo class consider such a time to | 
 | 5908 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST | 
 | 5909 | on the day DST starts.  We want to return the 1:MM EST spelling because that's | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5910 | the only spelling that makes sense on the local wall clock. | 
 | 5911 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5912 | In fact, if [5] holds at this point, we do have the standard-time spelling, | 
 | 5913 | but that takes a bit of proof.  We first prove a stronger result.  What's the | 
 | 5914 | difference between the LHS and RHS of [5]?  Let | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5915 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5916 |     diff = x.n - (z.n - z.o)                    [6] | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5917 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5918 | Now | 
 | 5919 |     z.n =                       by [4] | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5920 |     (y + y.s).n =               by #5 | 
 | 5921 |     y.n + y.s =                 since y.n = x.n | 
 | 5922 |     x.n + y.s =                 since z and y are have the same tzinfo member, | 
 | 5923 |                                     y.s = z.s by #2 | 
 | 5924 |     x.n + z.s | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5925 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5926 | Plugging that back into [6] gives | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5927 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5928 |     diff = | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5929 |     x.n - ((x.n + z.s) - z.o) =     expanding | 
 | 5930 |     x.n - x.n - z.s + z.o =         cancelling | 
 | 5931 |     - z.s + z.o =                   by #2 | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5932 |     z.d | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5933 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5934 | So diff = z.d. | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5935 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5936 | If [5] is true now, diff = 0, so z.d = 0 too, and we have the standard-time | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5937 | spelling we wanted in the endcase described above.  We're done.  Contrarily, | 
 | 5938 | if z.d = 0, then we have a UTC equivalent, and are also done. | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5939 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5940 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to | 
 | 5941 | add to z (in effect, z is in tz's standard time, and we need to shift the | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5942 | local clock into tz's daylight time). | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5943 |  | 
| Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5944 | Let | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5945 |  | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5946 |     z' = z + z.d = z + diff                     [7] | 
| Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5947 |  | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5948 | and we can again ask whether | 
| Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5949 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5950 |     z'.n - z'.o = x.n                           [8] | 
| Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5951 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5952 | If so, we're done.  If not, the tzinfo class is insane, according to the | 
 | 5953 | assumptions we've made.  This also requires a bit of proof.  As before, let's | 
 | 5954 | compute the difference between the LHS and RHS of [8] (and skipping some of | 
 | 5955 | the justifications for the kinds of substitutions we've done several times | 
 | 5956 | already): | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5957 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5958 |     diff' = x.n - (z'.n - z'.o) =           replacing z'.n via [7] | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5959 |         x.n  - (z.n + diff - z'.o) =    replacing diff via [6] | 
 | 5960 |         x.n - (z.n + x.n - (z.n - z.o) - z'.o) = | 
 | 5961 |         x.n - z.n - x.n + z.n - z.o + z'.o =    cancel x.n | 
 | 5962 |         - z.n + z.n - z.o + z'.o =              cancel z.n | 
 | 5963 |         - z.o + z'.o =                      #1 twice | 
 | 5964 |         -z.s - z.d + z'.s + z'.d =          z and z' have same tzinfo | 
 | 5965 |         z'.d - z.d | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5966 |  | 
 | 5967 | So z' is UTC-equivalent to x iff z'.d = z.d at this point.  If they are equal, | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5968 | we've found the UTC-equivalent so are done.  In fact, we stop with [7] and | 
 | 5969 | return z', not bothering to compute z'.d. | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5970 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5971 | How could z.d and z'd differ?  z' = z + z.d [7], so merely moving z' by | 
 | 5972 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), | 
 | 5973 | would have to change the result dst() returns:  we start in DST, and moving | 
 | 5974 | a little further into it takes us out of DST. | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5975 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5976 | There isn't a sane case where this can happen.  The closest it gets is at | 
 | 5977 | the end of DST, where there's an hour in UTC with no spelling in a hybrid | 
 | 5978 | tzinfo class.  In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT.  During | 
 | 5979 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM | 
 | 5980 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight | 
 | 5981 | time (4:MM UTC).  There is no local time mapping to 5:MM UTC.  The local | 
 | 5982 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in | 
 | 5983 | standard time.  Since that's what the local clock *does*, we want to map both | 
 | 5984 | UTC hours 5:MM and 6:MM to 1:MM Eastern.  The result is ambiguous | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5985 | in local time, but so it goes -- it's the way the local clock works. | 
 | 5986 |  | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5987 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, | 
 | 5988 | so z=0:MM.  z.d=60 (minutes) then, so [5] doesn't hold and we keep going. | 
 | 5989 | z' = z + z.d = 1:MM then, and z'.d=0, and z'.d - z.d = -60 != 0 so [8] | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5990 | (correctly) concludes that z' is not UTC-equivalent to x. | 
 | 5991 |  | 
 | 5992 | Because we know z.d said z was in daylight time (else [5] would have held and | 
 | 5993 | we would have stopped then), and we know z.d != z'.d (else [8] would have held | 
| Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 5994 | and we would have stopped then), and there are only 2 possible values dst() can | 
| Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5995 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, | 
 | 5996 | but the reasoning doesn't depend on the example -- it depends on there being | 
 | 5997 | two possible dst() outcomes, one zero and the other non-zero).  Therefore | 
| Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5998 | z' must be in standard time, and is the spelling we want in this case. | 
 | 5999 |  | 
 | 6000 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is | 
 | 6001 | concerned (because it takes z' as being in standard time rather than the | 
 | 6002 | daylight time we intend here), but returning it gives the real-life "local | 
 | 6003 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into | 
 | 6004 | tz. | 
 | 6005 |  | 
 | 6006 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with | 
 | 6007 | the 1:MM standard time spelling we want. | 
 | 6008 |  | 
 | 6009 | So how can this break?  One of the assumptions must be violated.  Two | 
 | 6010 | possibilities: | 
 | 6011 |  | 
 | 6012 | 1) [2] effectively says that y.s is invariant across all y belong to a given | 
 | 6013 |    time zone.  This isn't true if, for political reasons or continental drift, | 
 | 6014 |    a region decides to change its base offset from UTC. | 
 | 6015 |  | 
 | 6016 | 2) There may be versions of "double daylight" time where the tail end of | 
 | 6017 |    the analysis gives up a step too early.  I haven't thought about that | 
 | 6018 |    enough to say. | 
 | 6019 |  | 
 | 6020 | In any case, it's clear that the default fromutc() is strong enough to handle | 
 | 6021 | "almost all" time zones:  so long as the standard offset is invariant, it | 
 | 6022 | doesn't matter if daylight time transition points change from year to year, or | 
 | 6023 | if daylight time is skipped in some years; it doesn't matter how large or | 
 | 6024 | small dst() may get within its bounds; and it doesn't even matter if some | 
 | 6025 | perverse time zone returns a negative dst()).  So a breaking case must be | 
 | 6026 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. | 
| Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6027 | --------------------------------------------------------------------------- */ |