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 | |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 5 | /* bpo-35081: Defining this prevents including the C API capsule; |
| 6 | * internal versions of the Py*_Check macros which do not require |
| 7 | * the capsule are defined below */ |
| 8 | #define _PY_DATETIME_IMPL |
| 9 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 11 | #include "datetime.h" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 12 | #include "structmember.h" // PyMemberDef |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 13 | |
| 14 | #include <time.h> |
| 15 | |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 16 | #ifdef MS_WINDOWS |
| 17 | # include <winsock2.h> /* struct timeval */ |
| 18 | #endif |
| 19 | |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 20 | #define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType) |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 21 | #define PyDate_CheckExact(op) Py_IS_TYPE(op, &PyDateTime_DateType) |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 22 | |
| 23 | #define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType) |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 24 | #define PyDateTime_CheckExact(op) Py_IS_TYPE(op, &PyDateTime_DateTimeType) |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 25 | |
| 26 | #define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType) |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 27 | #define PyTime_CheckExact(op) Py_IS_TYPE(op, &PyDateTime_TimeType) |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 28 | |
| 29 | #define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType) |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 30 | #define PyDelta_CheckExact(op) Py_IS_TYPE(op, &PyDateTime_DeltaType) |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 31 | |
| 32 | #define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType) |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 33 | #define PyTZInfo_CheckExact(op) Py_IS_TYPE(op, &PyDateTime_TZInfoType) |
Paul Ganssle | 0d12672 | 2018-11-13 03:02:25 -0500 | [diff] [blame] | 34 | |
Pablo Galindo | 4be11c0 | 2019-08-22 20:24:25 +0100 | [diff] [blame] | 35 | #define PyTimezone_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeZoneType) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 36 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 37 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 38 | module datetime |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 39 | class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 40 | class datetime.date "PyDateTime_Date *" "&PyDateTime_DateType" |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 41 | class datetime.IsoCalendarDate "PyDateTime_IsoCalendarDate *" "&PyDateTime_IsoCalendarDateType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 42 | [clinic start generated code]*/ |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 43 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=81bec0fa19837f63]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 44 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 45 | #include "clinic/_datetimemodule.c.h" |
| 46 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 47 | /* We require that C int be at least 32 bits, and use int virtually |
| 48 | * everywhere. In just a few cases we use a temp long, where a Python |
| 49 | * API returns a C long. In such cases, we have to ensure that the |
| 50 | * final result fits in a C int (this can be an issue on 64-bit boxes). |
| 51 | */ |
| 52 | #if SIZEOF_INT < 4 |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 53 | # error "_datetime.c requires that C int have at least 32 bits" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | #define MINYEAR 1 |
| 57 | #define MAXYEAR 9999 |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 58 | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 59 | |
| 60 | /* Nine decimal digits is easy to communicate, and leaves enough room |
| 61 | * so that two delta days can be added w/o fear of overflowing a signed |
| 62 | * 32-bit int, and with plenty of room left over to absorb any possible |
| 63 | * carries from adding seconds. |
| 64 | */ |
| 65 | #define MAX_DELTA_DAYS 999999999 |
| 66 | |
| 67 | /* Rename the long macros in datetime.h to more reasonable short names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | #define GET_YEAR PyDateTime_GET_YEAR |
| 69 | #define GET_MONTH PyDateTime_GET_MONTH |
| 70 | #define GET_DAY PyDateTime_GET_DAY |
| 71 | #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
| 72 | #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
| 73 | #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
| 74 | #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 75 | #define DATE_GET_FOLD PyDateTime_DATE_GET_FOLD |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 76 | |
| 77 | /* Date accessors for date and datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
| 79 | ((o)->data[1] = ((v) & 0x00ff))) |
| 80 | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
| 81 | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 82 | |
| 83 | /* Date/Time accessors for datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
| 85 | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
| 86 | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
| 87 | #define DATE_SET_MICROSECOND(o, v) \ |
| 88 | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
| 89 | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
| 90 | ((o)->data[9] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 91 | #define DATE_SET_FOLD(o, v) (PyDateTime_DATE_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 92 | |
| 93 | /* Time accessors for time. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
| 95 | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
| 96 | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
| 97 | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 98 | #define TIME_GET_FOLD PyDateTime_TIME_GET_FOLD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
| 100 | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
| 101 | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
| 102 | #define TIME_SET_MICROSECOND(o, v) \ |
| 103 | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
| 104 | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
| 105 | ((o)->data[5] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 106 | #define TIME_SET_FOLD(o, v) (PyDateTime_TIME_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 107 | |
| 108 | /* Delta accessors for timedelta. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
| 110 | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
| 111 | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
| 114 | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 115 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
| 116 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 117 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
| 118 | * p->hastzinfo. |
| 119 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 120 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
| 121 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
| 122 | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
| 123 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
| 124 | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 125 | /* M is a char or int claiming to be a valid month. The macro is equivalent |
| 126 | * to the two-sided Python test |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | * 1 <= M <= 12 |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 128 | */ |
| 129 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
| 130 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 131 | /* Forward declarations. */ |
| 132 | static PyTypeObject PyDateTime_DateType; |
| 133 | static PyTypeObject PyDateTime_DateTimeType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 134 | static PyTypeObject PyDateTime_DeltaType; |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 135 | static PyTypeObject PyDateTime_IsoCalendarDateType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 136 | static PyTypeObject PyDateTime_TimeType; |
| 137 | static PyTypeObject PyDateTime_TZInfoType; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 138 | static PyTypeObject PyDateTime_TimeZoneType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 139 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 140 | static int check_tzinfo_subclass(PyObject *p); |
| 141 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 142 | _Py_IDENTIFIER(as_integer_ratio); |
| 143 | _Py_IDENTIFIER(fromutc); |
| 144 | _Py_IDENTIFIER(isoformat); |
| 145 | _Py_IDENTIFIER(strftime); |
| 146 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 147 | /* --------------------------------------------------------------------------- |
| 148 | * Math utilities. |
| 149 | */ |
| 150 | |
| 151 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 152 | * iff k^i has sign bit set and k^j has sign bit set, |
| 153 | * iff (k^i)&(k^j) has sign bit set. |
| 154 | */ |
| 155 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 157 | |
| 158 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 159 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 160 | * the real point of this. C will probably truncate instead (C99 |
| 161 | * requires truncation; C89 left it implementation-defined). |
| 162 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 163 | * for all the uses made of it. This simplifies the code and makes |
| 164 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 165 | * overflow case). |
| 166 | */ |
| 167 | static int |
| 168 | divmod(int x, int y, int *r) |
| 169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | assert(y > 0); |
| 173 | quo = x / y; |
| 174 | *r = x - quo * y; |
| 175 | if (*r < 0) { |
| 176 | --quo; |
| 177 | *r += y; |
| 178 | } |
| 179 | assert(0 <= *r && *r < y); |
| 180 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 183 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 184 | * are rounded to even. |
| 185 | */ |
| 186 | static PyObject * |
| 187 | divide_nearest(PyObject *m, PyObject *n) |
| 188 | { |
| 189 | PyObject *result; |
| 190 | PyObject *temp; |
| 191 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 192 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 193 | if (temp == NULL) |
| 194 | return NULL; |
| 195 | result = PyTuple_GET_ITEM(temp, 0); |
| 196 | Py_INCREF(result); |
| 197 | Py_DECREF(temp); |
| 198 | |
| 199 | return result; |
| 200 | } |
| 201 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 202 | /* --------------------------------------------------------------------------- |
| 203 | * General calendrical helper functions |
| 204 | */ |
| 205 | |
| 206 | /* For each month ordinal in 1..12, the number of days in that month, |
| 207 | * and the number of days before that month in the same year. These |
| 208 | * are correct for non-leap years only. |
| 209 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 210 | static const int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | 0, /* unused; this vector uses 1-based indexing */ |
| 212 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 215 | static const int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | 0, /* unused; this vector uses 1-based indexing */ |
| 217 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | /* year -> 1 if leap year, else 0. */ |
| 221 | static int |
| 222 | is_leap(int year) |
| 223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | /* Cast year to unsigned. The result is the same either way, but |
| 225 | * C can generate faster code for unsigned mod than for signed |
| 226 | * mod (especially for % 4 -- a good compiler should just grab |
| 227 | * the last 2 bits when the LHS is unsigned). |
| 228 | */ |
| 229 | const unsigned int ayear = (unsigned int)year; |
| 230 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* year, month -> number of days in that month in that year */ |
| 234 | static int |
| 235 | days_in_month(int year, int month) |
| 236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | assert(month >= 1); |
| 238 | assert(month <= 12); |
| 239 | if (month == 2 && is_leap(year)) |
| 240 | return 29; |
| 241 | else |
| 242 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 245 | /* year, month -> number of days in year preceding first day of month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 246 | static int |
| 247 | days_before_month(int year, int month) |
| 248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | assert(month >= 1); |
| 252 | assert(month <= 12); |
| 253 | days = _days_before_month[month]; |
| 254 | if (month > 2 && is_leap(year)) |
| 255 | ++days; |
| 256 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* year -> number of days before January 1st of year. Remember that we |
| 260 | * start with year 1, so days_before_year(1) == 0. |
| 261 | */ |
| 262 | static int |
| 263 | days_before_year(int year) |
| 264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | int y = year - 1; |
| 266 | /* This is incorrect if year <= 0; we really want the floor |
| 267 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 268 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 270 | assert (year >= 1); |
| 271 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 275 | * the correct values is asserted in the module init function. |
| 276 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 278 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 279 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 280 | |
| 281 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 282 | static void |
| 283 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 288 | * leap years repeats exactly every 400 years. The basic strategy is |
| 289 | * to find the closest 400-year boundary at or before ordinal, then |
| 290 | * work with the offset from that boundary to ordinal. Life is much |
| 291 | * clearer if we subtract 1 from ordinal first -- then the values |
| 292 | * of ordinal at 400-year boundaries are exactly those divisible |
| 293 | * by DI400Y: |
| 294 | * |
| 295 | * D M Y n n-1 |
| 296 | * -- --- ---- ---------- ---------------- |
| 297 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 298 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 299 | * ... |
| 300 | * 30 Dec 000 -1 -2 |
| 301 | * 31 Dec 000 0 -1 |
| 302 | * 1 Jan 001 1 0 400-year boundary |
| 303 | * 2 Jan 001 2 1 |
| 304 | * 3 Jan 001 3 2 |
| 305 | * ... |
| 306 | * 31 Dec 400 DI400Y DI400Y -1 |
| 307 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 308 | */ |
| 309 | assert(ordinal >= 1); |
| 310 | --ordinal; |
| 311 | n400 = ordinal / DI400Y; |
| 312 | n = ordinal % DI400Y; |
| 313 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 316 | * year, to the desired date. Now compute how many 100-year cycles |
| 317 | * precede n. |
| 318 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 319 | * 100-year cycles precede the desired day, which implies the |
| 320 | * desired day is December 31 at the end of a 400-year cycle. |
| 321 | */ |
| 322 | n100 = n / DI100Y; |
| 323 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | /* Now compute how many 4-year cycles precede it. */ |
| 326 | n4 = n / DI4Y; |
| 327 | n = n % DI4Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | /* And now how many single years. Again n1 can be 4, and again |
| 330 | * meaning that the desired day is December 31 at the end of the |
| 331 | * 4-year cycle. |
| 332 | */ |
| 333 | n1 = n / 365; |
| 334 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | *year += n100 * 100 + n4 * 4 + n1; |
| 337 | if (n1 == 4 || n100 == 4) { |
| 338 | assert(n == 0); |
| 339 | *year -= 1; |
| 340 | *month = 12; |
| 341 | *day = 31; |
| 342 | return; |
| 343 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | /* Now the year is correct, and n is the offset from January 1. We |
| 346 | * find the month via an estimate that's either exact or one too |
| 347 | * large. |
| 348 | */ |
| 349 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 350 | assert(leapyear == is_leap(*year)); |
| 351 | *month = (n + 50) >> 5; |
| 352 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 353 | if (preceding > n) { |
| 354 | /* estimate is too large */ |
| 355 | *month -= 1; |
| 356 | preceding -= days_in_month(*year, *month); |
| 357 | } |
| 358 | n -= preceding; |
| 359 | assert(0 <= n); |
| 360 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 366 | static int |
| 367 | ymd_to_ord(int year, int month, int day) |
| 368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 373 | static int |
| 374 | weekday(int year, int month, int day) |
| 375 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 380 | * first calendar week containing a Thursday. |
| 381 | */ |
| 382 | static int |
| 383 | iso_week1_monday(int year) |
| 384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 386 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 387 | int first_weekday = (first_day + 6) % 7; |
| 388 | /* ordinal of closest Monday at or before 1/1 */ |
| 389 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 392 | week1_monday += 7; |
| 393 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* --------------------------------------------------------------------------- |
| 397 | * Range checkers. |
| 398 | */ |
| 399 | |
| 400 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 401 | * If not, raise OverflowError and return -1. |
| 402 | */ |
| 403 | static int |
| 404 | check_delta_day_range(int days) |
| 405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 407 | return 0; |
| 408 | PyErr_Format(PyExc_OverflowError, |
| 409 | "days=%d; must have magnitude <= %d", |
| 410 | days, MAX_DELTA_DAYS); |
| 411 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 415 | * aren't, raise ValueError and return -1. |
| 416 | */ |
| 417 | static int |
| 418 | check_date_args(int year, int month, int day) |
| 419 | { |
| 420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | if (year < MINYEAR || year > MAXYEAR) { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 422 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | return -1; |
| 424 | } |
| 425 | if (month < 1 || month > 12) { |
| 426 | PyErr_SetString(PyExc_ValueError, |
| 427 | "month must be in 1..12"); |
| 428 | return -1; |
| 429 | } |
| 430 | if (day < 1 || day > days_in_month(year, month)) { |
| 431 | PyErr_SetString(PyExc_ValueError, |
| 432 | "day is out of range for month"); |
| 433 | return -1; |
| 434 | } |
| 435 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 439 | * aren't, raise ValueError and return -1. |
| 440 | */ |
| 441 | static int |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 442 | 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] | 443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | if (h < 0 || h > 23) { |
| 445 | PyErr_SetString(PyExc_ValueError, |
| 446 | "hour must be in 0..23"); |
| 447 | return -1; |
| 448 | } |
| 449 | if (m < 0 || m > 59) { |
| 450 | PyErr_SetString(PyExc_ValueError, |
| 451 | "minute must be in 0..59"); |
| 452 | return -1; |
| 453 | } |
| 454 | if (s < 0 || s > 59) { |
| 455 | PyErr_SetString(PyExc_ValueError, |
| 456 | "second must be in 0..59"); |
| 457 | return -1; |
| 458 | } |
| 459 | if (us < 0 || us > 999999) { |
| 460 | PyErr_SetString(PyExc_ValueError, |
| 461 | "microsecond must be in 0..999999"); |
| 462 | return -1; |
| 463 | } |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 464 | if (fold != 0 && fold != 1) { |
| 465 | PyErr_SetString(PyExc_ValueError, |
| 466 | "fold must be either 0 or 1"); |
| 467 | return -1; |
| 468 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* --------------------------------------------------------------------------- |
| 473 | * Normalization utilities. |
| 474 | */ |
| 475 | |
| 476 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 477 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 478 | * at least factor, enough of *lo is converted into "hi" units so that |
| 479 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 480 | * is impossible. |
| 481 | */ |
| 482 | static void |
| 483 | normalize_pair(int *hi, int *lo, int factor) |
| 484 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | assert(factor > 0); |
| 486 | assert(lo != hi); |
| 487 | if (*lo < 0 || *lo >= factor) { |
| 488 | const int num_hi = divmod(*lo, factor, lo); |
| 489 | const int new_hi = *hi + num_hi; |
| 490 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 491 | *hi = new_hi; |
| 492 | } |
| 493 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | * 0 <= *s < 24*3600 |
| 498 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 499 | * The input values must be such that the internals don't overflow. |
| 500 | * The way this routine is used, we don't get close. |
| 501 | */ |
| 502 | static void |
| 503 | normalize_d_s_us(int *d, int *s, int *us) |
| 504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | if (*us < 0 || *us >= 1000000) { |
| 506 | normalize_pair(s, us, 1000000); |
| 507 | /* |s| can't be bigger than about |
| 508 | * |original s| + |original us|/1000000 now. |
| 509 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | } |
| 512 | if (*s < 0 || *s >= 24*3600) { |
| 513 | normalize_pair(d, s, 24*3600); |
| 514 | /* |d| can't be bigger than about |
| 515 | * |original d| + |
| 516 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 517 | */ |
| 518 | } |
| 519 | assert(0 <= *s && *s < 24*3600); |
| 520 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | * 1 <= *m <= 12 |
| 525 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 526 | * The input values must be such that the internals don't overflow. |
| 527 | * The way this routine is used, we don't get close. |
| 528 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 529 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 530 | normalize_y_m_d(int *y, int *m, int *d) |
| 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 533 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 534 | /* In actual use, m is always the month component extracted from a |
| 535 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | /* Now only day can be out of bounds (year may also be out of bounds |
| 541 | * for a datetime object, but we don't care about that here). |
| 542 | * If day is out of bounds, what to do is arguable, but at least the |
| 543 | * method here is principled and explainable. |
| 544 | */ |
| 545 | dim = days_in_month(*y, *m); |
| 546 | if (*d < 1 || *d > dim) { |
| 547 | /* Move day-1 days from the first of the month. First try to |
| 548 | * get off cheap if we're only one day out of range |
| 549 | * (adjustments for timezone alone can't be worse than that). |
| 550 | */ |
| 551 | if (*d == 0) { |
| 552 | --*m; |
| 553 | if (*m > 0) |
| 554 | *d = days_in_month(*y, *m); |
| 555 | else { |
| 556 | --*y; |
| 557 | *m = 12; |
| 558 | *d = 31; |
| 559 | } |
| 560 | } |
| 561 | else if (*d == dim + 1) { |
| 562 | /* move forward a day */ |
| 563 | ++*m; |
| 564 | *d = 1; |
| 565 | if (*m > 12) { |
| 566 | *m = 1; |
| 567 | ++*y; |
| 568 | } |
| 569 | } |
| 570 | else { |
| 571 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 572 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 573 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 574 | goto error; |
| 575 | } else { |
| 576 | ord_to_ymd(ordinal, y, m, d); |
| 577 | return 0; |
| 578 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | assert(*m > 0); |
| 582 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 583 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 584 | return 0; |
| 585 | error: |
| 586 | PyErr_SetString(PyExc_OverflowError, |
| 587 | "date value out of range"); |
| 588 | return -1; |
| 589 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 593 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 594 | * failure, where failure means the adjusted year is out of bounds. |
| 595 | */ |
| 596 | static int |
| 597 | normalize_date(int *year, int *month, int *day) |
| 598 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 599 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* Force all the datetime fields into range. The parameters are both |
| 603 | * inputs and outputs. Returns < 0 on error. |
| 604 | */ |
| 605 | static int |
| 606 | normalize_datetime(int *year, int *month, int *day, |
| 607 | int *hour, int *minute, int *second, |
| 608 | int *microsecond) |
| 609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | normalize_pair(second, microsecond, 1000000); |
| 611 | normalize_pair(minute, second, 60); |
| 612 | normalize_pair(hour, minute, 60); |
| 613 | normalize_pair(day, hour, 24); |
| 614 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 618 | * Basic object allocation: tp_alloc implementations. These allocate |
| 619 | * Python objects of the right size and type, and do the Python object- |
| 620 | * initialization bit. If there's not enough memory, they return NULL after |
| 621 | * setting MemoryError. All data members remain uninitialized trash. |
| 622 | * |
| 623 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 624 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 625 | * tp_basicsize for the time and datetime types is set to the size of the |
| 626 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 627 | * allocate enough space for a tzinfo member whether or not one is actually |
| 628 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 629 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 630 | * using) just happens today to effectively ignore the nitems argument |
| 631 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 632 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 633 | * be changed to force a 0 nitems argument unless the type being allocated |
| 634 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 635 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 636 | */ |
| 637 | |
| 638 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 639 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | self = (PyObject *) |
| 644 | PyObject_MALLOC(aware ? |
| 645 | sizeof(PyDateTime_Time) : |
| 646 | sizeof(_PyDateTime_BaseTime)); |
| 647 | if (self == NULL) |
| 648 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 649 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 654 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | self = (PyObject *) |
| 659 | PyObject_MALLOC(aware ? |
| 660 | sizeof(PyDateTime_DateTime) : |
| 661 | sizeof(_PyDateTime_BaseDateTime)); |
| 662 | if (self == NULL) |
| 663 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 664 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* --------------------------------------------------------------------------- |
| 669 | * Helpers for setting object fields. These work on pointers to the |
| 670 | * appropriate base class. |
| 671 | */ |
| 672 | |
| 673 | /* For date and datetime. */ |
| 674 | static void |
| 675 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | self->hashcode = -1; |
| 678 | SET_YEAR(self, y); |
| 679 | SET_MONTH(self, m); |
| 680 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* --------------------------------------------------------------------------- |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 684 | * String parsing utilities and helper functions |
| 685 | */ |
| 686 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 687 | static const char * |
| 688 | parse_digits(const char *ptr, int *var, size_t num_digits) |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 689 | { |
| 690 | for (size_t i = 0; i < num_digits; ++i) { |
| 691 | unsigned int tmp = (unsigned int)(*(ptr++) - '0'); |
| 692 | if (tmp > 9) { |
| 693 | return NULL; |
| 694 | } |
| 695 | *var *= 10; |
| 696 | *var += (signed int)tmp; |
| 697 | } |
| 698 | |
| 699 | return ptr; |
| 700 | } |
| 701 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 702 | static int |
| 703 | parse_isoformat_date(const char *dtstr, int *year, int *month, int *day) |
| 704 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 705 | /* Parse the date components of the result of date.isoformat() |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 706 | * |
| 707 | * Return codes: |
| 708 | * 0: Success |
| 709 | * -1: Failed to parse date component |
| 710 | * -2: Failed to parse dateseparator |
| 711 | */ |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 712 | const char *p = dtstr; |
| 713 | p = parse_digits(p, year, 4); |
| 714 | if (NULL == p) { |
| 715 | return -1; |
| 716 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 717 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 718 | if (*(p++) != '-') { |
| 719 | return -2; |
| 720 | } |
| 721 | |
| 722 | p = parse_digits(p, month, 2); |
| 723 | if (NULL == p) { |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | if (*(p++) != '-') { |
| 728 | return -2; |
| 729 | } |
| 730 | |
| 731 | p = parse_digits(p, day, 2); |
| 732 | if (p == NULL) { |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static int |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 740 | parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour, |
| 741 | int *minute, int *second, int *microsecond) |
| 742 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 743 | const char *p = tstr; |
| 744 | const char *p_end = tstr_end; |
| 745 | int *vals[3] = {hour, minute, second}; |
| 746 | |
| 747 | // Parse [HH[:MM[:SS]]] |
| 748 | for (size_t i = 0; i < 3; ++i) { |
| 749 | p = parse_digits(p, vals[i], 2); |
| 750 | if (NULL == p) { |
| 751 | return -3; |
| 752 | } |
| 753 | |
| 754 | char c = *(p++); |
| 755 | if (p >= p_end) { |
| 756 | return c != '\0'; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 757 | } |
| 758 | else if (c == ':') { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 759 | continue; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 760 | } |
| 761 | else if (c == '.') { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 762 | break; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 763 | } |
| 764 | else { |
| 765 | return -4; // Malformed time separator |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | // Parse .fff[fff] |
| 770 | size_t len_remains = p_end - p; |
| 771 | if (!(len_remains == 6 || len_remains == 3)) { |
| 772 | return -3; |
| 773 | } |
| 774 | |
| 775 | p = parse_digits(p, microsecond, len_remains); |
| 776 | if (NULL == p) { |
| 777 | return -3; |
| 778 | } |
| 779 | |
| 780 | if (len_remains == 3) { |
| 781 | *microsecond *= 1000; |
| 782 | } |
| 783 | |
| 784 | // Return 1 if it's not the end of the string |
| 785 | return *p != '\0'; |
| 786 | } |
| 787 | |
| 788 | static int |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 789 | parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute, |
| 790 | int *second, int *microsecond, int *tzoffset, |
| 791 | int *tzmicrosecond) |
| 792 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 793 | // Parse the time portion of a datetime.isoformat() string |
| 794 | // |
| 795 | // Return codes: |
| 796 | // 0: Success (no tzoffset) |
| 797 | // 1: Success (with tzoffset) |
| 798 | // -3: Failed to parse time component |
| 799 | // -4: Failed to parse time separator |
| 800 | // -5: Malformed timezone string |
| 801 | |
| 802 | const char *p = dtstr; |
| 803 | const char *p_end = dtstr + dtlen; |
| 804 | |
| 805 | const char *tzinfo_pos = p; |
| 806 | do { |
| 807 | if (*tzinfo_pos == '+' || *tzinfo_pos == '-') { |
| 808 | break; |
| 809 | } |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 810 | } while (++tzinfo_pos < p_end); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 811 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 812 | int rv = parse_hh_mm_ss_ff(dtstr, tzinfo_pos, hour, minute, second, |
| 813 | microsecond); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 814 | |
| 815 | if (rv < 0) { |
| 816 | return rv; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 817 | } |
| 818 | else if (tzinfo_pos == p_end) { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 819 | // We know that there's no time zone, so if there's stuff at the |
| 820 | // end of the string it's an error. |
| 821 | if (rv == 1) { |
| 822 | return -5; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 823 | } |
| 824 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // Parse time zone component |
| 830 | // Valid formats are: |
| 831 | // - +HH:MM (len 6) |
| 832 | // - +HH:MM:SS (len 9) |
| 833 | // - +HH:MM:SS.ffffff (len 16) |
| 834 | size_t tzlen = p_end - tzinfo_pos; |
| 835 | if (!(tzlen == 6 || tzlen == 9 || tzlen == 16)) { |
| 836 | return -5; |
| 837 | } |
| 838 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 839 | int tzsign = (*tzinfo_pos == '-') ? -1 : 1; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 840 | tzinfo_pos++; |
| 841 | int tzhour = 0, tzminute = 0, tzsecond = 0; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 842 | rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, &tzhour, &tzminute, &tzsecond, |
| 843 | tzmicrosecond); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 844 | |
| 845 | *tzoffset = tzsign * ((tzhour * 3600) + (tzminute * 60) + tzsecond); |
| 846 | *tzmicrosecond *= tzsign; |
| 847 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 848 | return rv ? -5 : 1; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 849 | } |
| 850 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 851 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 852 | * Create various objects, mostly without range checking. |
| 853 | */ |
| 854 | |
| 855 | /* Create a date instance with no range checking. */ |
| 856 | static PyObject * |
| 857 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 860 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 861 | if (check_date_args(year, month, day) < 0) { |
| 862 | return NULL; |
| 863 | } |
| 864 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 865 | self = (PyDateTime_Date *)(type->tp_alloc(type, 0)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | if (self != NULL) |
| 867 | set_date_fields(self, year, month, day); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 868 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 873 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 874 | // Forward declaration |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 875 | static PyObject * |
| 876 | new_datetime_ex(int, int, int, int, int, int, int, PyObject *, PyTypeObject *); |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 877 | |
| 878 | /* Create date instance with no range checking, or call subclass constructor */ |
| 879 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 880 | new_date_subclass_ex(int year, int month, int day, PyObject *cls) |
| 881 | { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 882 | PyObject *result; |
| 883 | // We have "fast path" constructors for two subclasses: date and datetime |
| 884 | if ((PyTypeObject *)cls == &PyDateTime_DateType) { |
| 885 | result = new_date_ex(year, month, day, (PyTypeObject *)cls); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 886 | } |
| 887 | else if ((PyTypeObject *)cls == &PyDateTime_DateTimeType) { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 888 | result = new_datetime_ex(year, month, day, 0, 0, 0, 0, Py_None, |
| 889 | (PyTypeObject *)cls); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 890 | } |
| 891 | else { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 892 | result = PyObject_CallFunction(cls, "iii", year, month, day); |
| 893 | } |
| 894 | |
| 895 | return result; |
| 896 | } |
| 897 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 898 | /* Create a datetime instance with no range checking. */ |
| 899 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 900 | new_datetime_ex2(int year, int month, int day, int hour, int minute, |
| 901 | int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | PyDateTime_DateTime *self; |
| 904 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 905 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 906 | if (check_date_args(year, month, day) < 0) { |
| 907 | return NULL; |
| 908 | } |
| 909 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 910 | return NULL; |
| 911 | } |
| 912 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 913 | return NULL; |
| 914 | } |
| 915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 917 | if (self != NULL) { |
| 918 | self->hastzinfo = aware; |
| 919 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 920 | DATE_SET_HOUR(self, hour); |
| 921 | DATE_SET_MINUTE(self, minute); |
| 922 | DATE_SET_SECOND(self, second); |
| 923 | DATE_SET_MICROSECOND(self, usecond); |
| 924 | if (aware) { |
| 925 | Py_INCREF(tzinfo); |
| 926 | self->tzinfo = tzinfo; |
| 927 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 928 | DATE_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | } |
| 930 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 933 | static PyObject * |
| 934 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
| 935 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
| 936 | { |
| 937 | return new_datetime_ex2(year, month, day, hour, minute, second, usecond, |
| 938 | tzinfo, 0, type); |
| 939 | } |
| 940 | |
| 941 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ |
| 942 | new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 944 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 945 | static PyObject * |
| 946 | new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute, |
| 947 | int second, int usecond, PyObject *tzinfo, |
| 948 | int fold, PyObject *cls) { |
| 949 | PyObject* dt; |
| 950 | if ((PyTypeObject*)cls == &PyDateTime_DateTimeType) { |
| 951 | // Use the fast path constructor |
| 952 | dt = new_datetime(year, month, day, hour, minute, second, usecond, |
| 953 | tzinfo, fold); |
| 954 | } else { |
| 955 | // Subclass |
| 956 | dt = PyObject_CallFunction(cls, "iiiiiiiO", |
| 957 | year, |
| 958 | month, |
| 959 | day, |
| 960 | hour, |
| 961 | minute, |
| 962 | second, |
| 963 | usecond, |
| 964 | tzinfo); |
| 965 | } |
| 966 | |
| 967 | return dt; |
| 968 | } |
| 969 | |
| 970 | static PyObject * |
| 971 | new_datetime_subclass_ex(int year, int month, int day, int hour, int minute, |
| 972 | int second, int usecond, PyObject *tzinfo, |
| 973 | PyObject *cls) { |
| 974 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 975 | second, usecond, tzinfo, 0, |
| 976 | cls); |
| 977 | } |
| 978 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 979 | /* Create a time instance with no range checking. */ |
| 980 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 981 | new_time_ex2(int hour, int minute, int second, int usecond, |
| 982 | PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | PyDateTime_Time *self; |
| 985 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 986 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 987 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 988 | return NULL; |
| 989 | } |
| 990 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 991 | return NULL; |
| 992 | } |
| 993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 995 | if (self != NULL) { |
| 996 | self->hastzinfo = aware; |
| 997 | self->hashcode = -1; |
| 998 | TIME_SET_HOUR(self, hour); |
| 999 | TIME_SET_MINUTE(self, minute); |
| 1000 | TIME_SET_SECOND(self, second); |
| 1001 | TIME_SET_MICROSECOND(self, usecond); |
| 1002 | if (aware) { |
| 1003 | Py_INCREF(tzinfo); |
| 1004 | self->tzinfo = tzinfo; |
| 1005 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1006 | TIME_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | } |
| 1008 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1011 | static PyObject * |
| 1012 | new_time_ex(int hour, int minute, int second, int usecond, |
| 1013 | PyObject *tzinfo, PyTypeObject *type) |
| 1014 | { |
| 1015 | return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); |
| 1016 | } |
| 1017 | |
| 1018 | #define new_time(hh, mm, ss, us, tzinfo, fold) \ |
| 1019 | new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1020 | |
| 1021 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 1022 | * true. Passing false is a speed optimization, if you know for sure |
| 1023 | * that seconds and microseconds are already in their proper ranges. In any |
| 1024 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 1025 | * of range). |
| 1026 | */ |
| 1027 | static PyObject * |
| 1028 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1030 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | if (normalize) |
| 1034 | normalize_d_s_us(&days, &seconds, µseconds); |
| 1035 | assert(0 <= seconds && seconds < 24*3600); |
| 1036 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | if (check_delta_day_range(days) < 0) |
| 1039 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 1042 | if (self != NULL) { |
| 1043 | self->hashcode = -1; |
| 1044 | SET_TD_DAYS(self, days); |
| 1045 | SET_TD_SECONDS(self, seconds); |
| 1046 | SET_TD_MICROSECONDS(self, microseconds); |
| 1047 | } |
| 1048 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | #define new_delta(d, s, us, normalize) \ |
| 1052 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1053 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1054 | |
| 1055 | typedef struct |
| 1056 | { |
| 1057 | PyObject_HEAD |
| 1058 | PyObject *offset; |
| 1059 | PyObject *name; |
| 1060 | } PyDateTime_TimeZone; |
| 1061 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1062 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1063 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 1064 | /* The interned Epoch datetime instance */ |
| 1065 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 1066 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1067 | /* Create new timezone instance checking offset range. This |
| 1068 | function does not check the name argument. Caller must assure |
| 1069 | that offset is a timedelta instance and name is either NULL |
| 1070 | or a unicode object. */ |
| 1071 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1072 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1073 | { |
| 1074 | PyDateTime_TimeZone *self; |
| 1075 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 1076 | |
| 1077 | assert(offset != NULL); |
| 1078 | assert(PyDelta_Check(offset)); |
| 1079 | assert(name == NULL || PyUnicode_Check(name)); |
| 1080 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1081 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 1082 | if (self == NULL) { |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | Py_INCREF(offset); |
| 1086 | self->offset = offset; |
| 1087 | Py_XINCREF(name); |
| 1088 | self->name = name; |
| 1089 | return (PyObject *)self; |
| 1090 | } |
| 1091 | |
| 1092 | static int delta_bool(PyDateTime_Delta *self); |
| 1093 | |
| 1094 | static PyObject * |
| 1095 | new_timezone(PyObject *offset, PyObject *name) |
| 1096 | { |
| 1097 | assert(offset != NULL); |
| 1098 | assert(PyDelta_Check(offset)); |
| 1099 | assert(name == NULL || PyUnicode_Check(name)); |
| 1100 | |
| 1101 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 1102 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1103 | return PyDateTime_TimeZone_UTC; |
| 1104 | } |
Ngalim Siregar | 92c7e30 | 2019-08-09 21:22:16 +0700 | [diff] [blame] | 1105 | if ((GET_TD_DAYS(offset) == -1 && |
| 1106 | GET_TD_SECONDS(offset) == 0 && |
| 1107 | GET_TD_MICROSECONDS(offset) < 1) || |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1108 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1109 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1110 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 1111 | " timedelta(hours=24)," |
| 1112 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1113 | return NULL; |
| 1114 | } |
| 1115 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1116 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1119 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1120 | * tzinfo helpers. |
| 1121 | */ |
| 1122 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1123 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 1124 | * raise TypeError and return -1. |
| 1125 | */ |
| 1126 | static int |
| 1127 | check_tzinfo_subclass(PyObject *p) |
| 1128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | if (p == Py_None || PyTZInfo_Check(p)) |
| 1130 | return 0; |
| 1131 | PyErr_Format(PyExc_TypeError, |
| 1132 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 1133 | "not type '%s'", |
| 1134 | Py_TYPE(p)->tp_name); |
| 1135 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1138 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 1139 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 1140 | * and the caller must not decref the result. |
| 1141 | */ |
| 1142 | static PyObject * |
| 1143 | get_tzinfo_member(PyObject *self) |
| 1144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 1148 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 1149 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 1150 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1155 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 1156 | * be an instance of the tzinfo class. If the method returns None, this |
| 1157 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 1158 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 1159 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 1160 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1161 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1162 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1163 | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1164 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1165 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1168 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1170 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1171 | if (tzinfo == Py_None) |
| 1172 | Py_RETURN_NONE; |
| 1173 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 1174 | if (offset == Py_None || offset == NULL) |
| 1175 | return offset; |
| 1176 | if (PyDelta_Check(offset)) { |
Ngalim Siregar | 92c7e30 | 2019-08-09 21:22:16 +0700 | [diff] [blame] | 1177 | if ((GET_TD_DAYS(offset) == -1 && |
| 1178 | GET_TD_SECONDS(offset) == 0 && |
| 1179 | GET_TD_MICROSECONDS(offset) < 1) || |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1180 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1181 | Py_DECREF(offset); |
| 1182 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1183 | " strictly between -timedelta(hours=24) and" |
| 1184 | " timedelta(hours=24)."); |
| 1185 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | else { |
| 1189 | PyErr_Format(PyExc_TypeError, |
| 1190 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1191 | "timedelta, not '%.200s'", |
| 1192 | name, Py_TYPE(offset)->tp_name); |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 1193 | Py_DECREF(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1194 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1196 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1197 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 1201 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 1202 | * 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] | 1203 | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1204 | * If utcoffset() returns an out of range timedelta, |
| 1205 | * ValueError is raised and this returns -1. Else *none is |
| 1206 | * set to 0 and the offset is returned (as timedelta, positive east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1207 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1208 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1209 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 1210 | { |
| 1211 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1214 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 1215 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 1216 | * returns None, call_dst returns 0 and sets *none to 1. If dst() |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1217 | * doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 1218 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 1219 | * ValueError is raised and this returns -1. Else *none is set to 0 and |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1220 | * the offset is returned (as timedelta, positive east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1221 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1222 | static PyObject * |
| 1223 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1224 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1225 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1228 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1229 | * 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] | 1230 | * 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] | 1231 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 1232 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1233 | */ |
| 1234 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1235 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1238 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | assert(tzinfo != NULL); |
| 1241 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 1242 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1245 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1246 | |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 1247 | result = _PyObject_CallMethodIdOneArg(tzinfo, &PyId_tzname, tzinfoarg); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (result == NULL || result == Py_None) |
| 1250 | return result; |
| 1251 | |
| 1252 | if (!PyUnicode_Check(result)) { |
| 1253 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 1254 | "return None or a string, not '%s'", |
| 1255 | Py_TYPE(result)->tp_name); |
| 1256 | Py_DECREF(result); |
| 1257 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1259 | |
| 1260 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1263 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 1264 | * stuff |
| 1265 | * ", tzinfo=" + repr(tzinfo) |
| 1266 | * before the closing ")". |
| 1267 | */ |
| 1268 | static PyObject * |
| 1269 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 1270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | assert(PyUnicode_Check(repr)); |
| 1274 | assert(tzinfo); |
| 1275 | if (tzinfo == Py_None) |
| 1276 | return repr; |
| 1277 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1278 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1279 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | Py_DECREF(repr); |
| 1281 | if (temp == NULL) |
| 1282 | return NULL; |
| 1283 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 1284 | Py_DECREF(temp); |
| 1285 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1288 | /* repr is like "someclass(arg1, arg2)". If fold isn't 0, |
| 1289 | * stuff |
| 1290 | * ", fold=" + repr(tzinfo) |
| 1291 | * before the closing ")". |
| 1292 | */ |
| 1293 | static PyObject * |
| 1294 | append_keyword_fold(PyObject *repr, int fold) |
| 1295 | { |
| 1296 | PyObject *temp; |
| 1297 | |
| 1298 | assert(PyUnicode_Check(repr)); |
| 1299 | if (fold == 0) |
| 1300 | return repr; |
| 1301 | /* Get rid of the trailing ')'. */ |
| 1302 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1303 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
| 1304 | Py_DECREF(repr); |
| 1305 | if (temp == NULL) |
| 1306 | return NULL; |
| 1307 | repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); |
| 1308 | Py_DECREF(temp); |
| 1309 | return repr; |
| 1310 | } |
| 1311 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1312 | static inline PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 1313 | tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds) |
| 1314 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1315 | PyObject *tzinfo; |
| 1316 | if (rv == 1) { |
| 1317 | // Create a timezone from offset in seconds (0 returns UTC) |
| 1318 | if (tzoffset == 0) { |
| 1319 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1320 | return PyDateTime_TimeZone_UTC; |
| 1321 | } |
| 1322 | |
| 1323 | PyObject *delta = new_delta(0, tzoffset, tz_useconds, 1); |
Alexey Izbyshev | 4988453 | 2018-08-24 18:53:16 +0300 | [diff] [blame] | 1324 | if (delta == NULL) { |
| 1325 | return NULL; |
| 1326 | } |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1327 | tzinfo = new_timezone(delta, NULL); |
Alexey Izbyshev | 4988453 | 2018-08-24 18:53:16 +0300 | [diff] [blame] | 1328 | Py_DECREF(delta); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 1329 | } |
| 1330 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1331 | tzinfo = Py_None; |
| 1332 | Py_INCREF(Py_None); |
| 1333 | } |
| 1334 | |
| 1335 | return tzinfo; |
| 1336 | } |
| 1337 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1338 | /* --------------------------------------------------------------------------- |
| 1339 | * String format helpers. |
| 1340 | */ |
| 1341 | |
| 1342 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1343 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1344 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1345 | static const char * const DayNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1347 | }; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1348 | static const char * const MonthNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1350 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1351 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1356 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1357 | GET_DAY(date), hours, minutes, seconds, |
| 1358 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1361 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1362 | |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1363 | /* Add formatted UTC offset string to buf. buf has no more than |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1364 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1365 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1366 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1367 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1368 | * string of the form |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1369 | * sign HH sep MM [sep SS [. UUUUUU]] |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1370 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1371 | * bogus, an appropriate exception is set and -1 is returned. |
| 1372 | */ |
| 1373 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1374 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1376 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1377 | PyObject *offset; |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1378 | int hours, minutes, seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1382 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1383 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1384 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1386 | if (offset == Py_None) { |
| 1387 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | *buf = '\0'; |
| 1389 | return 0; |
| 1390 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1391 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1392 | if (GET_TD_DAYS(offset) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | sign = '-'; |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 1394 | Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1395 | if (offset == NULL) |
| 1396 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1398 | else { |
| 1399 | sign = '+'; |
| 1400 | } |
| 1401 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1402 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1403 | seconds = GET_TD_SECONDS(offset); |
| 1404 | Py_DECREF(offset); |
| 1405 | minutes = divmod(seconds, 60, &seconds); |
| 1406 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1407 | if (microseconds) { |
| 1408 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d.%06d", sign, |
| 1409 | hours, sep, minutes, sep, seconds, microseconds); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | if (seconds) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1413 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, |
| 1414 | sep, minutes, sep, seconds); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1415 | return 0; |
| 1416 | } |
| 1417 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1421 | static PyObject * |
| 1422 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | PyObject *temp; |
| 1425 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1426 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1427 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | if (Zreplacement == NULL) |
| 1430 | return NULL; |
| 1431 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1432 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | assert(tzinfoarg != NULL); |
| 1435 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1436 | if (temp == NULL) |
| 1437 | goto Error; |
| 1438 | if (temp == Py_None) { |
| 1439 | Py_DECREF(temp); |
| 1440 | return Zreplacement; |
| 1441 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | assert(PyUnicode_Check(temp)); |
| 1444 | /* Since the tzname is getting stuffed into the |
| 1445 | * format, we have to double any % signs so that |
| 1446 | * strftime doesn't treat them as format codes. |
| 1447 | */ |
| 1448 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1449 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | Py_DECREF(temp); |
| 1451 | if (Zreplacement == NULL) |
| 1452 | return NULL; |
| 1453 | if (!PyUnicode_Check(Zreplacement)) { |
| 1454 | PyErr_SetString(PyExc_TypeError, |
| 1455 | "tzname.replace() did not return a string"); |
| 1456 | goto Error; |
| 1457 | } |
| 1458 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1459 | |
| 1460 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | Py_DECREF(Zreplacement); |
| 1462 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1465 | static PyObject * |
| 1466 | make_freplacement(PyObject *object) |
| 1467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | char freplacement[64]; |
| 1469 | if (PyTime_Check(object)) |
| 1470 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1471 | else if (PyDateTime_Check(object)) |
| 1472 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1473 | else |
| 1474 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1479 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1480 | * 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] | 1481 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1482 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1483 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1484 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1485 | */ |
| 1486 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1487 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1493 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1494 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | const char *pin; /* pointer to next char in input format */ |
| 1497 | Py_ssize_t flen; /* length of input format */ |
| 1498 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1501 | char *pnew; /* pointer to available byte in output format */ |
| 1502 | size_t totalnew; /* number bytes total in output format buffer, |
| 1503 | exclusive of trailing \0 */ |
| 1504 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1507 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
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 | assert(object && format && timetuple); |
| 1510 | assert(PyUnicode_Check(format)); |
| 1511 | /* Convert the input format to a C string and size */ |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1512 | pin = PyUnicode_AsUTF8AndSize(format, &flen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | if (!pin) |
| 1514 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1517 | * a new format. Since computing the replacements for those codes |
| 1518 | * is expensive, don't unless they're actually used. |
| 1519 | */ |
| 1520 | if (flen > INT_MAX - 1) { |
| 1521 | PyErr_NoMemory(); |
| 1522 | goto Done; |
| 1523 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1526 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1527 | if (newfmt == NULL) goto Done; |
| 1528 | pnew = PyBytes_AsString(newfmt); |
| 1529 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | while ((ch = *pin++) != '\0') { |
| 1532 | if (ch != '%') { |
| 1533 | ptoappend = pin - 1; |
| 1534 | ntoappend = 1; |
| 1535 | } |
| 1536 | else if ((ch = *pin++) == '\0') { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 1537 | /* Null byte follows %, copy only '%'. |
| 1538 | * |
MichaelSaah | 454b3d4 | 2019-01-14 05:23:39 -0500 | [diff] [blame] | 1539 | * Back the pin up one char so that we catch the null check |
| 1540 | * the next time through the loop.*/ |
| 1541 | pin--; |
| 1542 | ptoappend = pin - 1; |
| 1543 | ntoappend = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | } |
| 1545 | /* A % has been seen and ch is the character after it. */ |
| 1546 | else if (ch == 'z') { |
| 1547 | if (zreplacement == NULL) { |
| 1548 | /* format utcoffset */ |
| 1549 | char buf[100]; |
| 1550 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1551 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1552 | if (zreplacement == NULL) goto Done; |
| 1553 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1554 | assert(tzinfoarg != NULL); |
| 1555 | if (format_utcoffset(buf, |
| 1556 | sizeof(buf), |
| 1557 | "", |
| 1558 | tzinfo, |
| 1559 | tzinfoarg) < 0) |
| 1560 | goto Done; |
| 1561 | Py_DECREF(zreplacement); |
| 1562 | zreplacement = |
| 1563 | PyBytes_FromStringAndSize(buf, |
| 1564 | strlen(buf)); |
| 1565 | if (zreplacement == NULL) |
| 1566 | goto Done; |
| 1567 | } |
| 1568 | } |
| 1569 | assert(zreplacement != NULL); |
| 1570 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1571 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1572 | } |
| 1573 | else if (ch == 'Z') { |
| 1574 | /* format tzname */ |
| 1575 | if (Zreplacement == NULL) { |
| 1576 | Zreplacement = make_Zreplacement(object, |
| 1577 | tzinfoarg); |
| 1578 | if (Zreplacement == NULL) |
| 1579 | goto Done; |
| 1580 | } |
| 1581 | assert(Zreplacement != NULL); |
| 1582 | assert(PyUnicode_Check(Zreplacement)); |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1583 | ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1585 | if (ptoappend == NULL) |
| 1586 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | } |
| 1588 | else if (ch == 'f') { |
| 1589 | /* format microseconds */ |
| 1590 | if (freplacement == NULL) { |
| 1591 | freplacement = make_freplacement(object); |
| 1592 | if (freplacement == NULL) |
| 1593 | goto Done; |
| 1594 | } |
| 1595 | assert(freplacement != NULL); |
| 1596 | assert(PyBytes_Check(freplacement)); |
| 1597 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1598 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1599 | } |
| 1600 | else { |
| 1601 | /* percent followed by neither z nor Z */ |
| 1602 | ptoappend = pin - 2; |
| 1603 | ntoappend = 2; |
| 1604 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | /* Append the ntoappend chars starting at ptoappend to |
| 1607 | * the new format. |
| 1608 | */ |
| 1609 | if (ntoappend == 0) |
| 1610 | continue; |
| 1611 | assert(ptoappend != NULL); |
| 1612 | assert(ntoappend > 0); |
| 1613 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1614 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1615 | PyErr_NoMemory(); |
| 1616 | goto Done; |
| 1617 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1618 | totalnew <<= 1; |
| 1619 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1622 | } |
| 1623 | memcpy(pnew, ptoappend, ntoappend); |
| 1624 | pnew += ntoappend; |
| 1625 | usednew += ntoappend; |
| 1626 | assert(usednew <= totalnew); |
| 1627 | } /* end while() */ |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 1628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1630 | goto Done; |
| 1631 | { |
| 1632 | PyObject *format; |
| 1633 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | if (time == NULL) |
| 1636 | goto Done; |
| 1637 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1638 | if (format != NULL) { |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1639 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime, |
| 1640 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | Py_DECREF(format); |
| 1642 | } |
| 1643 | Py_DECREF(time); |
| 1644 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1645 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1646 | Py_XDECREF(freplacement); |
| 1647 | Py_XDECREF(zreplacement); |
| 1648 | Py_XDECREF(Zreplacement); |
| 1649 | Py_XDECREF(newfmt); |
| 1650 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1653 | /* --------------------------------------------------------------------------- |
| 1654 | * Wrap functions from the time module. These aren't directly available |
| 1655 | * from C. Perhaps they should be. |
| 1656 | */ |
| 1657 | |
| 1658 | /* Call time.time() and return its result (a Python float). */ |
| 1659 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1660 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1661 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | PyObject *result = NULL; |
| 1663 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
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 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1666 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1667 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1668 | result = _PyObject_CallMethodIdNoArgs(time, &PyId_time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | Py_DECREF(time); |
| 1670 | } |
| 1671 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1675 | * computed from the y,m,d args. |
| 1676 | */ |
| 1677 | static PyObject * |
| 1678 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1680 | PyObject *time; |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1681 | PyObject *result; |
| 1682 | _Py_IDENTIFIER(struct_time); |
| 1683 | PyObject *args; |
| 1684 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | time = PyImport_ImportModuleNoBlock("time"); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1687 | if (time == NULL) { |
| 1688 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | } |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1690 | |
| 1691 | args = Py_BuildValue("iiiiiiiii", |
| 1692 | y, m, d, |
| 1693 | hh, mm, ss, |
| 1694 | weekday(y, m, d), |
| 1695 | days_before_month(y, m) + d, |
| 1696 | dstflag); |
| 1697 | if (args == NULL) { |
| 1698 | Py_DECREF(time); |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 1702 | result = _PyObject_CallMethodIdOneArg(time, &PyId_struct_time, args); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1703 | Py_DECREF(time); |
Victor Stinner | ddc120f | 2016-12-09 15:35:40 +0100 | [diff] [blame] | 1704 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | /* --------------------------------------------------------------------------- |
| 1709 | * Miscellaneous helpers. |
| 1710 | */ |
| 1711 | |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1712 | /* The comparisons here all most naturally compute a cmp()-like result. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1713 | * This little helper turns that into a bool result for rich comparisons. |
| 1714 | */ |
| 1715 | static PyObject * |
| 1716 | diff_to_bool(int diff, int op) |
| 1717 | { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1718 | Py_RETURN_RICHCOMPARE(diff, 0, op); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1721 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1722 | static PyObject * |
| 1723 | cmperror(PyObject *a, PyObject *b) |
| 1724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | PyErr_Format(PyExc_TypeError, |
| 1726 | "can't compare %s to %s", |
| 1727 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1728 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1731 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1732 | * Cached Python objects; these are set by the module init function. |
| 1733 | */ |
| 1734 | |
| 1735 | /* Conversion factors. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1737 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1738 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1739 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1740 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1741 | 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] | 1742 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1743 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1744 | /* --------------------------------------------------------------------------- |
| 1745 | * Class implementations. |
| 1746 | */ |
| 1747 | |
| 1748 | /* |
| 1749 | * PyDateTime_Delta implementation. |
| 1750 | */ |
| 1751 | |
| 1752 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1753 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1754 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1755 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1756 | * due to ubiquitous overflow possibilities. |
| 1757 | */ |
| 1758 | static PyObject * |
| 1759 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | PyObject *x1 = NULL; |
| 1762 | PyObject *x2 = NULL; |
| 1763 | PyObject *x3 = NULL; |
| 1764 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1767 | if (x1 == NULL) |
| 1768 | goto Done; |
| 1769 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1770 | if (x2 == NULL) |
| 1771 | goto Done; |
| 1772 | Py_DECREF(x1); |
| 1773 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | /* x2 has days in seconds */ |
| 1776 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1777 | if (x1 == NULL) |
| 1778 | goto Done; |
| 1779 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1780 | if (x3 == NULL) |
| 1781 | goto Done; |
| 1782 | Py_DECREF(x1); |
| 1783 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1784 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | /* x3 has days+seconds in seconds */ |
| 1787 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1788 | if (x1 == NULL) |
| 1789 | goto Done; |
| 1790 | Py_DECREF(x3); |
| 1791 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | /* x1 has days+seconds in us */ |
| 1794 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1795 | if (x2 == NULL) |
| 1796 | goto Done; |
| 1797 | result = PyNumber_Add(x1, x2); |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 1798 | assert(result == NULL || PyLong_CheckExact(result)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1799 | |
| 1800 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | Py_XDECREF(x1); |
| 1802 | Py_XDECREF(x2); |
| 1803 | Py_XDECREF(x3); |
| 1804 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1807 | static PyObject * |
| 1808 | checked_divmod(PyObject *a, PyObject *b) |
| 1809 | { |
| 1810 | PyObject *result = PyNumber_Divmod(a, b); |
| 1811 | if (result != NULL) { |
| 1812 | if (!PyTuple_Check(result)) { |
| 1813 | PyErr_Format(PyExc_TypeError, |
| 1814 | "divmod() returned non-tuple (type %.200s)", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 1815 | Py_TYPE(result)->tp_name); |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1816 | Py_DECREF(result); |
| 1817 | return NULL; |
| 1818 | } |
| 1819 | if (PyTuple_GET_SIZE(result) != 2) { |
| 1820 | PyErr_Format(PyExc_TypeError, |
| 1821 | "divmod() returned a tuple of size %zd", |
| 1822 | PyTuple_GET_SIZE(result)); |
| 1823 | Py_DECREF(result); |
| 1824 | return NULL; |
| 1825 | } |
| 1826 | } |
| 1827 | return result; |
| 1828 | } |
| 1829 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1830 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1831 | */ |
| 1832 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1833 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1834 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | int us; |
| 1836 | int s; |
| 1837 | int d; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | PyObject *tuple = NULL; |
| 1840 | PyObject *num = NULL; |
| 1841 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1842 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1843 | tuple = checked_divmod(pyus, us_per_second); |
| 1844 | if (tuple == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | goto Done; |
| 1846 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1847 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1848 | num = PyTuple_GET_ITEM(tuple, 1); /* us */ |
| 1849 | us = _PyLong_AsInt(num); |
| 1850 | num = NULL; |
| 1851 | if (us == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | goto Done; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1853 | } |
| 1854 | if (!(0 <= us && us < 1000000)) { |
| 1855 | goto BadDivmod; |
| 1856 | } |
| 1857 | |
| 1858 | num = PyTuple_GET_ITEM(tuple, 0); /* leftover seconds */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | Py_INCREF(num); |
| 1860 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1861 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1862 | tuple = checked_divmod(num, seconds_per_day); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | if (tuple == NULL) |
| 1864 | goto Done; |
| 1865 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1866 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1867 | num = PyTuple_GET_ITEM(tuple, 1); /* seconds */ |
| 1868 | s = _PyLong_AsInt(num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | num = NULL; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1870 | if (s == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | goto Done; |
| 1872 | } |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1873 | if (!(0 <= s && s < 24*3600)) { |
| 1874 | goto BadDivmod; |
| 1875 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1876 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1877 | num = PyTuple_GET_ITEM(tuple, 0); /* leftover days */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | Py_INCREF(num); |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1879 | d = _PyLong_AsInt(num); |
| 1880 | if (d == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1881 | goto Done; |
| 1882 | } |
| 1883 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1884 | |
| 1885 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | Py_XDECREF(tuple); |
| 1887 | Py_XDECREF(num); |
| 1888 | return result; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1889 | |
| 1890 | BadDivmod: |
| 1891 | PyErr_SetString(PyExc_TypeError, |
| 1892 | "divmod() returned a value out of range"); |
| 1893 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | #define microseconds_to_delta(pymicros) \ |
| 1897 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1898 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1899 | static PyObject * |
| 1900 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | PyObject *pyus_in; |
| 1903 | PyObject *pyus_out; |
| 1904 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | pyus_in = delta_to_microseconds(delta); |
| 1907 | if (pyus_in == NULL) |
| 1908 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1909 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1910 | pyus_out = PyNumber_Multiply(intobj, pyus_in); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | Py_DECREF(pyus_in); |
| 1912 | if (pyus_out == NULL) |
| 1913 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | result = microseconds_to_delta(pyus_out); |
| 1916 | Py_DECREF(pyus_out); |
| 1917 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | static PyObject * |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1921 | get_float_as_integer_ratio(PyObject *floatobj) |
| 1922 | { |
| 1923 | PyObject *ratio; |
| 1924 | |
| 1925 | assert(floatobj && PyFloat_Check(floatobj)); |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1926 | ratio = _PyObject_CallMethodIdNoArgs(floatobj, &PyId_as_integer_ratio); |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1927 | if (ratio == NULL) { |
| 1928 | return NULL; |
| 1929 | } |
| 1930 | if (!PyTuple_Check(ratio)) { |
| 1931 | PyErr_Format(PyExc_TypeError, |
| 1932 | "unexpected return type from as_integer_ratio(): " |
| 1933 | "expected tuple, got '%.200s'", |
| 1934 | Py_TYPE(ratio)->tp_name); |
| 1935 | Py_DECREF(ratio); |
| 1936 | return NULL; |
| 1937 | } |
| 1938 | if (PyTuple_Size(ratio) != 2) { |
| 1939 | PyErr_SetString(PyExc_ValueError, |
| 1940 | "as_integer_ratio() must return a 2-tuple"); |
| 1941 | Py_DECREF(ratio); |
| 1942 | return NULL; |
| 1943 | } |
| 1944 | return ratio; |
| 1945 | } |
| 1946 | |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1947 | /* op is 0 for multiplication, 1 for division */ |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1948 | static PyObject * |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1949 | multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj, int op) |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1950 | { |
| 1951 | PyObject *result = NULL; |
| 1952 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1953 | PyObject *ratio = NULL; |
| 1954 | |
| 1955 | pyus_in = delta_to_microseconds(delta); |
| 1956 | if (pyus_in == NULL) |
| 1957 | return NULL; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1958 | ratio = get_float_as_integer_ratio(floatobj); |
| 1959 | if (ratio == NULL) { |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1960 | goto error; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1961 | } |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1962 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1963 | Py_DECREF(pyus_in); |
| 1964 | pyus_in = NULL; |
| 1965 | if (temp == NULL) |
| 1966 | goto error; |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1967 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1968 | Py_DECREF(temp); |
| 1969 | if (pyus_out == NULL) |
| 1970 | goto error; |
| 1971 | result = microseconds_to_delta(pyus_out); |
| 1972 | Py_DECREF(pyus_out); |
| 1973 | error: |
| 1974 | Py_XDECREF(pyus_in); |
| 1975 | Py_XDECREF(ratio); |
| 1976 | |
| 1977 | return result; |
| 1978 | } |
| 1979 | |
| 1980 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1981 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | PyObject *pyus_in; |
| 1984 | PyObject *pyus_out; |
| 1985 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | pyus_in = delta_to_microseconds(delta); |
| 1988 | if (pyus_in == NULL) |
| 1989 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1992 | Py_DECREF(pyus_in); |
| 1993 | if (pyus_out == NULL) |
| 1994 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | result = microseconds_to_delta(pyus_out); |
| 1997 | Py_DECREF(pyus_out); |
| 1998 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2002 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 2003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | PyObject *pyus_left; |
| 2005 | PyObject *pyus_right; |
| 2006 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | pyus_left = delta_to_microseconds(left); |
| 2009 | if (pyus_left == NULL) |
| 2010 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | pyus_right = delta_to_microseconds(right); |
| 2013 | if (pyus_right == NULL) { |
| 2014 | Py_DECREF(pyus_left); |
| 2015 | return NULL; |
| 2016 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 2019 | Py_DECREF(pyus_left); |
| 2020 | Py_DECREF(pyus_right); |
| 2021 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | static PyObject * |
| 2025 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 2026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | PyObject *pyus_left; |
| 2028 | PyObject *pyus_right; |
| 2029 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | pyus_left = delta_to_microseconds(left); |
| 2032 | if (pyus_left == NULL) |
| 2033 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | pyus_right = delta_to_microseconds(right); |
| 2036 | if (pyus_right == NULL) { |
| 2037 | Py_DECREF(pyus_left); |
| 2038 | return NULL; |
| 2039 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 2042 | Py_DECREF(pyus_left); |
| 2043 | Py_DECREF(pyus_right); |
| 2044 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2048 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 2049 | { |
| 2050 | PyObject *result; |
| 2051 | PyObject *pyus_in, *pyus_out; |
| 2052 | pyus_in = delta_to_microseconds(delta); |
| 2053 | if (pyus_in == NULL) |
| 2054 | return NULL; |
| 2055 | pyus_out = divide_nearest(pyus_in, i); |
| 2056 | Py_DECREF(pyus_in); |
| 2057 | if (pyus_out == NULL) |
| 2058 | return NULL; |
| 2059 | result = microseconds_to_delta(pyus_out); |
| 2060 | Py_DECREF(pyus_out); |
| 2061 | |
| 2062 | return result; |
| 2063 | } |
| 2064 | |
| 2065 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2066 | delta_add(PyObject *left, PyObject *right) |
| 2067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2071 | /* delta + delta */ |
| 2072 | /* The C-level additions can't overflow because of the |
| 2073 | * invariant bounds. |
| 2074 | */ |
| 2075 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 2076 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 2077 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 2078 | GET_TD_MICROSECONDS(right); |
| 2079 | result = new_delta(days, seconds, microseconds, 1); |
| 2080 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | if (result == Py_NotImplemented) |
| 2083 | Py_INCREF(result); |
| 2084 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | static PyObject * |
| 2088 | delta_negative(PyDateTime_Delta *self) |
| 2089 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | return new_delta(-GET_TD_DAYS(self), |
| 2091 | -GET_TD_SECONDS(self), |
| 2092 | -GET_TD_MICROSECONDS(self), |
| 2093 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | static PyObject * |
| 2097 | delta_positive(PyDateTime_Delta *self) |
| 2098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | /* Could optimize this (by returning self) if this isn't a |
| 2100 | * subclass -- but who uses unary + ? Approximately nobody. |
| 2101 | */ |
| 2102 | return new_delta(GET_TD_DAYS(self), |
| 2103 | GET_TD_SECONDS(self), |
| 2104 | GET_TD_MICROSECONDS(self), |
| 2105 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | static PyObject * |
| 2109 | delta_abs(PyDateTime_Delta *self) |
| 2110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 2114 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | if (GET_TD_DAYS(self) < 0) |
| 2117 | result = delta_negative(self); |
| 2118 | else |
| 2119 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | static PyObject * |
| 2125 | delta_subtract(PyObject *left, PyObject *right) |
| 2126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2130 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 2131 | /* The C-level additions can't overflow because of the |
| 2132 | * invariant bounds. |
| 2133 | */ |
| 2134 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 2135 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 2136 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 2137 | GET_TD_MICROSECONDS(right); |
| 2138 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | if (result == Py_NotImplemented) |
| 2142 | Py_INCREF(result); |
| 2143 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2146 | static int |
| 2147 | delta_cmp(PyObject *self, PyObject *other) |
| 2148 | { |
| 2149 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 2150 | if (diff == 0) { |
| 2151 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 2152 | if (diff == 0) |
| 2153 | diff = GET_TD_MICROSECONDS(self) - |
| 2154 | GET_TD_MICROSECONDS(other); |
| 2155 | } |
| 2156 | return diff; |
| 2157 | } |
| 2158 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2159 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2160 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2163 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | return diff_to_bool(diff, op); |
| 2165 | } |
| 2166 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2167 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 2172 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2173 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2174 | delta_hash(PyDateTime_Delta *self) |
| 2175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | if (self->hashcode == -1) { |
| 2177 | PyObject *temp = delta_getstate(self); |
| 2178 | if (temp != NULL) { |
| 2179 | self->hashcode = PyObject_Hash(temp); |
| 2180 | Py_DECREF(temp); |
| 2181 | } |
| 2182 | } |
| 2183 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | static PyObject * |
| 2187 | delta_multiply(PyObject *left, PyObject *right) |
| 2188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 | if (PyDelta_Check(left)) { |
| 2192 | /* delta * ??? */ |
| 2193 | if (PyLong_Check(right)) |
| 2194 | result = multiply_int_timedelta(right, |
| 2195 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2196 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2197 | result = multiply_truedivide_timedelta_float( |
| 2198 | (PyDateTime_Delta *) left, right, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | } |
| 2200 | else if (PyLong_Check(left)) |
| 2201 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2202 | (PyDateTime_Delta *) right); |
| 2203 | else if (PyFloat_Check(left)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2204 | result = multiply_truedivide_timedelta_float( |
| 2205 | (PyDateTime_Delta *) right, left, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | if (result == Py_NotImplemented) |
| 2208 | Py_INCREF(result); |
| 2209 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | static PyObject * |
| 2213 | delta_divide(PyObject *left, PyObject *right) |
| 2214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2215 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | if (PyDelta_Check(left)) { |
| 2218 | /* delta * ??? */ |
| 2219 | if (PyLong_Check(right)) |
| 2220 | result = divide_timedelta_int( |
| 2221 | (PyDateTime_Delta *)left, |
| 2222 | right); |
| 2223 | else if (PyDelta_Check(right)) |
| 2224 | result = divide_timedelta_timedelta( |
| 2225 | (PyDateTime_Delta *)left, |
| 2226 | (PyDateTime_Delta *)right); |
| 2227 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | if (result == Py_NotImplemented) |
| 2230 | Py_INCREF(result); |
| 2231 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2234 | static PyObject * |
| 2235 | delta_truedivide(PyObject *left, PyObject *right) |
| 2236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | if (PyDelta_Check(left)) { |
| 2240 | if (PyDelta_Check(right)) |
| 2241 | result = truedivide_timedelta_timedelta( |
| 2242 | (PyDateTime_Delta *)left, |
| 2243 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2244 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2245 | result = multiply_truedivide_timedelta_float( |
| 2246 | (PyDateTime_Delta *)left, right, 1); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2247 | else if (PyLong_Check(right)) |
| 2248 | result = truedivide_timedelta_int( |
| 2249 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | if (result == Py_NotImplemented) |
| 2253 | Py_INCREF(result); |
| 2254 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | static PyObject * |
| 2258 | delta_remainder(PyObject *left, PyObject *right) |
| 2259 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 | PyObject *pyus_left; |
| 2261 | PyObject *pyus_right; |
| 2262 | PyObject *pyus_remainder; |
| 2263 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2264 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2265 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2266 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2269 | if (pyus_left == NULL) |
| 2270 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2273 | if (pyus_right == NULL) { |
| 2274 | Py_DECREF(pyus_left); |
| 2275 | return NULL; |
| 2276 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 2279 | Py_DECREF(pyus_left); |
| 2280 | Py_DECREF(pyus_right); |
| 2281 | if (pyus_remainder == NULL) |
| 2282 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | remainder = microseconds_to_delta(pyus_remainder); |
| 2285 | Py_DECREF(pyus_remainder); |
| 2286 | if (remainder == NULL) |
| 2287 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | static PyObject * |
| 2293 | delta_divmod(PyObject *left, PyObject *right) |
| 2294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | PyObject *pyus_left; |
| 2296 | PyObject *pyus_right; |
| 2297 | PyObject *divmod; |
| 2298 | PyObject *delta; |
| 2299 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2300 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2301 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2302 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2305 | if (pyus_left == NULL) |
| 2306 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2309 | if (pyus_right == NULL) { |
| 2310 | Py_DECREF(pyus_left); |
| 2311 | return NULL; |
| 2312 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2313 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 2314 | divmod = checked_divmod(pyus_left, pyus_right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | Py_DECREF(pyus_left); |
| 2316 | Py_DECREF(pyus_right); |
| 2317 | if (divmod == NULL) |
| 2318 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 2321 | if (delta == NULL) { |
| 2322 | Py_DECREF(divmod); |
| 2323 | return NULL; |
| 2324 | } |
| 2325 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 2326 | Py_DECREF(delta); |
| 2327 | Py_DECREF(divmod); |
| 2328 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2331 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 2332 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 2333 | * so far, and there are factor microseconds per current unit, the number |
| 2334 | * of which is given by num. num * factor is added to sofar in a |
| 2335 | * numerically careful way, and that's the result. Any fractional |
| 2336 | * microseconds left over (this can happen if num is a float type) are |
| 2337 | * added into *leftover. |
| 2338 | * Note that there are many ways this can give an error (NULL) return. |
| 2339 | */ |
| 2340 | static PyObject * |
| 2341 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2342 | double *leftover) |
| 2343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | PyObject *prod; |
| 2345 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | if (PyLong_Check(num)) { |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 2350 | prod = PyNumber_Multiply(num, factor); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | if (prod == NULL) |
| 2352 | return NULL; |
| 2353 | sum = PyNumber_Add(sofar, prod); |
| 2354 | Py_DECREF(prod); |
| 2355 | return sum; |
| 2356 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | if (PyFloat_Check(num)) { |
| 2359 | double dnum; |
| 2360 | double fracpart; |
| 2361 | double intpart; |
| 2362 | PyObject *x; |
| 2363 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | /* The Plan: decompose num into an integer part and a |
| 2366 | * fractional part, num = intpart + fracpart. |
| 2367 | * Then num * factor == |
| 2368 | * intpart * factor + fracpart * factor |
| 2369 | * and the LHS can be computed exactly in long arithmetic. |
| 2370 | * The RHS is again broken into an int part and frac part. |
| 2371 | * and the frac part is added into *leftover. |
| 2372 | */ |
| 2373 | dnum = PyFloat_AsDouble(num); |
| 2374 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2375 | return NULL; |
| 2376 | fracpart = modf(dnum, &intpart); |
| 2377 | x = PyLong_FromDouble(intpart); |
| 2378 | if (x == NULL) |
| 2379 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | prod = PyNumber_Multiply(x, factor); |
| 2382 | Py_DECREF(x); |
| 2383 | if (prod == NULL) |
| 2384 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | sum = PyNumber_Add(sofar, prod); |
| 2387 | Py_DECREF(prod); |
| 2388 | if (sum == NULL) |
| 2389 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | if (fracpart == 0.0) |
| 2392 | return sum; |
| 2393 | /* So far we've lost no information. Dealing with the |
| 2394 | * fractional part requires float arithmetic, and may |
| 2395 | * lose a little info. |
| 2396 | */ |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2397 | assert(PyLong_CheckExact(factor)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | dnum *= fracpart; |
| 2401 | fracpart = modf(dnum, &intpart); |
| 2402 | x = PyLong_FromDouble(intpart); |
| 2403 | if (x == NULL) { |
| 2404 | Py_DECREF(sum); |
| 2405 | return NULL; |
| 2406 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | y = PyNumber_Add(sum, x); |
| 2409 | Py_DECREF(sum); |
| 2410 | Py_DECREF(x); |
| 2411 | *leftover += fracpart; |
| 2412 | return y; |
| 2413 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | PyErr_Format(PyExc_TypeError, |
| 2416 | "unsupported type for timedelta %s component: %s", |
| 2417 | tag, Py_TYPE(num)->tp_name); |
| 2418 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | static PyObject * |
| 2422 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2426 | /* Argument objects. */ |
| 2427 | PyObject *day = NULL; |
| 2428 | PyObject *second = NULL; |
| 2429 | PyObject *us = NULL; |
| 2430 | PyObject *ms = NULL; |
| 2431 | PyObject *minute = NULL; |
| 2432 | PyObject *hour = NULL; |
| 2433 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2436 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2437 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | static char *keywords[] = { |
| 2440 | "days", "seconds", "microseconds", "milliseconds", |
| 2441 | "minutes", "hours", "weeks", NULL |
| 2442 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2445 | keywords, |
| 2446 | &day, &second, &us, |
| 2447 | &ms, &minute, &hour, &week) == 0) |
| 2448 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2450 | x = PyLong_FromLong(0); |
| 2451 | if (x == NULL) |
| 2452 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2454 | #define CLEANUP \ |
| 2455 | Py_DECREF(x); \ |
| 2456 | x = y; \ |
| 2457 | if (x == NULL) \ |
| 2458 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | if (us) { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2461 | y = accum("microseconds", x, us, _PyLong_One, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | CLEANUP; |
| 2463 | } |
| 2464 | if (ms) { |
| 2465 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2466 | CLEANUP; |
| 2467 | } |
| 2468 | if (second) { |
| 2469 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2470 | CLEANUP; |
| 2471 | } |
| 2472 | if (minute) { |
| 2473 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2474 | CLEANUP; |
| 2475 | } |
| 2476 | if (hour) { |
| 2477 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2478 | CLEANUP; |
| 2479 | } |
| 2480 | if (day) { |
| 2481 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2482 | CLEANUP; |
| 2483 | } |
| 2484 | if (week) { |
| 2485 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2486 | CLEANUP; |
| 2487 | } |
| 2488 | if (leftover_us) { |
| 2489 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2490 | double whole_us = round(leftover_us); |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2491 | int x_is_odd; |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2492 | PyObject *temp; |
| 2493 | |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2494 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2495 | /* We're exactly halfway between two integers. In order |
| 2496 | * to do round-half-to-even, we must determine whether x |
| 2497 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2498 | * code below uses bitwise and operation to check the last |
| 2499 | * bit. */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2500 | temp = PyNumber_And(x, _PyLong_One); /* temp <- x & 1 */ |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2501 | if (temp == NULL) { |
| 2502 | Py_DECREF(x); |
| 2503 | goto Done; |
| 2504 | } |
| 2505 | x_is_odd = PyObject_IsTrue(temp); |
| 2506 | Py_DECREF(temp); |
| 2507 | if (x_is_odd == -1) { |
| 2508 | Py_DECREF(x); |
| 2509 | goto Done; |
| 2510 | } |
| 2511 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2512 | } |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2513 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2514 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | if (temp == NULL) { |
| 2517 | Py_DECREF(x); |
| 2518 | goto Done; |
| 2519 | } |
| 2520 | y = PyNumber_Add(x, temp); |
| 2521 | Py_DECREF(temp); |
| 2522 | CLEANUP; |
| 2523 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2525 | self = microseconds_to_delta_ex(x, type); |
| 2526 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2527 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2529 | |
| 2530 | #undef CLEANUP |
| 2531 | } |
| 2532 | |
| 2533 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2534 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2535 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2536 | return (GET_TD_DAYS(self) != 0 |
| 2537 | || GET_TD_SECONDS(self) != 0 |
| 2538 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | static PyObject * |
| 2542 | delta_repr(PyDateTime_Delta *self) |
| 2543 | { |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2544 | PyObject *args = PyUnicode_FromString(""); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2545 | |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2546 | if (args == NULL) { |
| 2547 | return NULL; |
| 2548 | } |
| 2549 | |
| 2550 | const char *sep = ""; |
| 2551 | |
| 2552 | if (GET_TD_DAYS(self) != 0) { |
| 2553 | Py_SETREF(args, PyUnicode_FromFormat("days=%d", GET_TD_DAYS(self))); |
| 2554 | if (args == NULL) { |
| 2555 | return NULL; |
| 2556 | } |
| 2557 | sep = ", "; |
| 2558 | } |
| 2559 | |
| 2560 | if (GET_TD_SECONDS(self) != 0) { |
| 2561 | Py_SETREF(args, PyUnicode_FromFormat("%U%sseconds=%d", args, sep, |
| 2562 | GET_TD_SECONDS(self))); |
| 2563 | if (args == NULL) { |
| 2564 | return NULL; |
| 2565 | } |
| 2566 | sep = ", "; |
| 2567 | } |
| 2568 | |
| 2569 | if (GET_TD_MICROSECONDS(self) != 0) { |
| 2570 | Py_SETREF(args, PyUnicode_FromFormat("%U%smicroseconds=%d", args, sep, |
| 2571 | GET_TD_MICROSECONDS(self))); |
| 2572 | if (args == NULL) { |
| 2573 | return NULL; |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | if (PyUnicode_GET_LENGTH(args) == 0) { |
| 2578 | Py_SETREF(args, PyUnicode_FromString("0")); |
| 2579 | if (args == NULL) { |
| 2580 | return NULL; |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | PyObject *repr = PyUnicode_FromFormat("%s(%S)", Py_TYPE(self)->tp_name, |
| 2585 | args); |
| 2586 | Py_DECREF(args); |
| 2587 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | static PyObject * |
| 2591 | delta_str(PyDateTime_Delta *self) |
| 2592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | int us = GET_TD_MICROSECONDS(self); |
| 2594 | int seconds = GET_TD_SECONDS(self); |
| 2595 | int minutes = divmod(seconds, 60, &seconds); |
| 2596 | int hours = divmod(minutes, 60, &minutes); |
| 2597 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2599 | if (days) { |
| 2600 | if (us) |
| 2601 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2602 | days, (days == 1 || days == -1) ? "" : "s", |
| 2603 | hours, minutes, seconds, us); |
| 2604 | else |
| 2605 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2606 | days, (days == 1 || days == -1) ? "" : "s", |
| 2607 | hours, minutes, seconds); |
| 2608 | } else { |
| 2609 | if (us) |
| 2610 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2611 | hours, minutes, seconds, us); |
| 2612 | else |
| 2613 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2614 | hours, minutes, seconds); |
| 2615 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2616 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2619 | /* Pickle support, a simple use of __reduce__. */ |
| 2620 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2621 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2622 | static PyObject * |
| 2623 | delta_getstate(PyDateTime_Delta *self) |
| 2624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2626 | GET_TD_SECONDS(self), |
| 2627 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2630 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2631 | delta_total_seconds(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2633 | PyObject *total_seconds; |
| 2634 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2637 | if (total_microseconds == NULL) |
| 2638 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2639 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2640 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2647 | delta_reduce(PyDateTime_Delta* self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2653 | |
| 2654 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2656 | {"days", T_INT, OFFSET(days), READONLY, |
| 2657 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2660 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2663 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2664 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2665 | }; |
| 2666 | |
| 2667 | static PyMethodDef delta_methods[] = { |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2668 | {"total_seconds", delta_total_seconds, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2671 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2672 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2675 | }; |
| 2676 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2677 | static const char delta_doc[] = |
Chris Barker | d6a61f2 | 2018-10-19 15:43:24 -0700 | [diff] [blame] | 2678 | PyDoc_STR("Difference between two datetime values.\n\n" |
| 2679 | "timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, " |
| 2680 | "minutes=0, hours=0, weeks=0)\n\n" |
| 2681 | "All arguments are optional and default to 0.\n" |
| 2682 | "Arguments may be integers or floats, and may be positive or negative."); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2683 | |
| 2684 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2685 | delta_add, /* nb_add */ |
| 2686 | delta_subtract, /* nb_subtract */ |
| 2687 | delta_multiply, /* nb_multiply */ |
| 2688 | delta_remainder, /* nb_remainder */ |
| 2689 | delta_divmod, /* nb_divmod */ |
| 2690 | 0, /* nb_power */ |
| 2691 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2692 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2693 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2694 | (inquiry)delta_bool, /* nb_bool */ |
| 2695 | 0, /*nb_invert*/ |
| 2696 | 0, /*nb_lshift*/ |
| 2697 | 0, /*nb_rshift*/ |
| 2698 | 0, /*nb_and*/ |
| 2699 | 0, /*nb_xor*/ |
| 2700 | 0, /*nb_or*/ |
| 2701 | 0, /*nb_int*/ |
| 2702 | 0, /*nb_reserved*/ |
| 2703 | 0, /*nb_float*/ |
| 2704 | 0, /*nb_inplace_add*/ |
| 2705 | 0, /*nb_inplace_subtract*/ |
| 2706 | 0, /*nb_inplace_multiply*/ |
| 2707 | 0, /*nb_inplace_remainder*/ |
| 2708 | 0, /*nb_inplace_power*/ |
| 2709 | 0, /*nb_inplace_lshift*/ |
| 2710 | 0, /*nb_inplace_rshift*/ |
| 2711 | 0, /*nb_inplace_and*/ |
| 2712 | 0, /*nb_inplace_xor*/ |
| 2713 | 0, /*nb_inplace_or*/ |
| 2714 | delta_divide, /* nb_floor_divide */ |
| 2715 | delta_truedivide, /* nb_true_divide */ |
| 2716 | 0, /* nb_inplace_floor_divide */ |
| 2717 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2718 | }; |
| 2719 | |
| 2720 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2722 | "datetime.timedelta", /* tp_name */ |
| 2723 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2724 | 0, /* tp_itemsize */ |
| 2725 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2726 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | 0, /* tp_getattr */ |
| 2728 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2729 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | (reprfunc)delta_repr, /* tp_repr */ |
| 2731 | &delta_as_number, /* tp_as_number */ |
| 2732 | 0, /* tp_as_sequence */ |
| 2733 | 0, /* tp_as_mapping */ |
| 2734 | (hashfunc)delta_hash, /* tp_hash */ |
| 2735 | 0, /* tp_call */ |
| 2736 | (reprfunc)delta_str, /* tp_str */ |
| 2737 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2738 | 0, /* tp_setattro */ |
| 2739 | 0, /* tp_as_buffer */ |
| 2740 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2741 | delta_doc, /* tp_doc */ |
| 2742 | 0, /* tp_traverse */ |
| 2743 | 0, /* tp_clear */ |
| 2744 | delta_richcompare, /* tp_richcompare */ |
| 2745 | 0, /* tp_weaklistoffset */ |
| 2746 | 0, /* tp_iter */ |
| 2747 | 0, /* tp_iternext */ |
| 2748 | delta_methods, /* tp_methods */ |
| 2749 | delta_members, /* tp_members */ |
| 2750 | 0, /* tp_getset */ |
| 2751 | 0, /* tp_base */ |
| 2752 | 0, /* tp_dict */ |
| 2753 | 0, /* tp_descr_get */ |
| 2754 | 0, /* tp_descr_set */ |
| 2755 | 0, /* tp_dictoffset */ |
| 2756 | 0, /* tp_init */ |
| 2757 | 0, /* tp_alloc */ |
| 2758 | delta_new, /* tp_new */ |
| 2759 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2760 | }; |
| 2761 | |
| 2762 | /* |
| 2763 | * PyDateTime_Date implementation. |
| 2764 | */ |
| 2765 | |
| 2766 | /* Accessor properties. */ |
| 2767 | |
| 2768 | static PyObject * |
| 2769 | date_year(PyDateTime_Date *self, void *unused) |
| 2770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | static PyObject * |
| 2775 | date_month(PyDateTime_Date *self, void *unused) |
| 2776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | static PyObject * |
| 2781 | date_day(PyDateTime_Date *self, void *unused) |
| 2782 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | {"year", (getter)date_year}, |
| 2788 | {"month", (getter)date_month}, |
| 2789 | {"day", (getter)date_day}, |
| 2790 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2791 | }; |
| 2792 | |
| 2793 | /* Constructors. */ |
| 2794 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2795 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2796 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2797 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2798 | date_from_pickle(PyTypeObject *type, PyObject *state) |
| 2799 | { |
| 2800 | PyDateTime_Date *me; |
| 2801 | |
| 2802 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2803 | if (me != NULL) { |
| 2804 | const char *pdata = PyBytes_AS_STRING(state); |
| 2805 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2806 | me->hashcode = -1; |
| 2807 | } |
| 2808 | return (PyObject *)me; |
| 2809 | } |
| 2810 | |
| 2811 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2812 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | int year; |
| 2816 | int month; |
| 2817 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 1133a8c | 2018-12-07 16:48:21 +0200 | [diff] [blame] | 2820 | if (PyTuple_GET_SIZE(args) == 1) { |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2821 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 2822 | if (PyBytes_Check(state)) { |
| 2823 | if (PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2824 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2825 | { |
| 2826 | return date_from_pickle(type, state); |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 2827 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2828 | } |
| 2829 | else if (PyUnicode_Check(state)) { |
| 2830 | if (PyUnicode_READY(state)) { |
| 2831 | return NULL; |
| 2832 | } |
| 2833 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATE_DATASIZE && |
| 2834 | MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2))) |
| 2835 | { |
| 2836 | state = PyUnicode_AsLatin1String(state); |
| 2837 | if (state == NULL) { |
| 2838 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 2839 | /* More informative error message. */ |
| 2840 | PyErr_SetString(PyExc_ValueError, |
| 2841 | "Failed to encode latin1 string when unpickling " |
| 2842 | "a date object. " |
| 2843 | "pickle.load(data, encoding='latin1') is assumed."); |
| 2844 | } |
| 2845 | return NULL; |
| 2846 | } |
| 2847 | self = date_from_pickle(type, state); |
| 2848 | Py_DECREF(state); |
| 2849 | return self; |
| 2850 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2855 | &year, &month, &day)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | self = new_date_ex(year, month, day, type); |
| 2857 | } |
| 2858 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2861 | static PyObject * |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2862 | date_fromtimestamp(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2863 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2864 | struct tm tm; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2866 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2867 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2869 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 2870 | if (_PyTime_localtime(t, &tm) != 0) |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2871 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2872 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2873 | return new_date_subclass_ex(tm.tm_year + 1900, |
| 2874 | tm.tm_mon + 1, |
| 2875 | tm.tm_mday, |
| 2876 | cls); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /* Return new date from current time. |
| 2880 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2881 | * only way to be sure of that is to *call* time.time(). That's not |
| 2882 | * generally the same as calling C's time. |
| 2883 | */ |
| 2884 | static PyObject * |
| 2885 | date_today(PyObject *cls, PyObject *dummy) |
| 2886 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | PyObject *time; |
| 2888 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2889 | _Py_IDENTIFIER(fromtimestamp); |
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 | time = time_time(); |
| 2892 | if (time == NULL) |
| 2893 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | /* Note well: today() is a class method, so this may not call |
| 2896 | * date.fromtimestamp. For example, it may call |
| 2897 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2898 | * time.time() delivers; if someone were gonzo about optimization, |
| 2899 | * date.today() could get away with plain C time(). |
| 2900 | */ |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 2901 | result = _PyObject_CallMethodIdOneArg(cls, &PyId_fromtimestamp, time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | Py_DECREF(time); |
| 2903 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2906 | /*[clinic input] |
| 2907 | @classmethod |
| 2908 | datetime.date.fromtimestamp |
| 2909 | |
| 2910 | timestamp: object |
| 2911 | / |
| 2912 | |
| 2913 | Create a date from a POSIX timestamp. |
| 2914 | |
| 2915 | The timestamp is a number, e.g. created via time.time(), that is interpreted |
| 2916 | as local time. |
| 2917 | [clinic start generated code]*/ |
| 2918 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2919 | static PyObject * |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2920 | datetime_date_fromtimestamp(PyTypeObject *type, PyObject *timestamp) |
| 2921 | /*[clinic end generated code: output=fd045fda58168869 input=eabb3fe7f40491fe]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2922 | { |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2923 | return date_fromtimestamp((PyObject *) type, timestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2926 | /* bpo-36025: This is a wrapper for API compatibility with the public C API, |
| 2927 | * which expects a function that takes an *args tuple, whereas the argument |
| 2928 | * clinic generates code that takes METH_O. |
| 2929 | */ |
| 2930 | static PyObject * |
| 2931 | datetime_date_fromtimestamp_capi(PyObject *cls, PyObject *args) |
| 2932 | { |
| 2933 | PyObject *timestamp; |
| 2934 | PyObject *result = NULL; |
| 2935 | |
| 2936 | if (PyArg_UnpackTuple(args, "fromtimestamp", 1, 1, ×tamp)) { |
| 2937 | result = date_fromtimestamp(cls, timestamp); |
| 2938 | } |
| 2939 | |
| 2940 | return result; |
| 2941 | } |
| 2942 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2943 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2944 | * the ordinal is out of range. |
| 2945 | */ |
| 2946 | static PyObject * |
| 2947 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | PyObject *result = NULL; |
| 2950 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2953 | int year; |
| 2954 | int month; |
| 2955 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | if (ordinal < 1) |
| 2958 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2959 | ">= 1"); |
| 2960 | else { |
| 2961 | ord_to_ymd(ordinal, &year, &month, &day); |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2962 | result = new_date_subclass_ex(year, month, day, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2968 | /* Return the new date from a string as generated by date.isoformat() */ |
| 2969 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2970 | date_fromisoformat(PyObject *cls, PyObject *dtstr) |
| 2971 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2972 | assert(dtstr != NULL); |
| 2973 | |
| 2974 | if (!PyUnicode_Check(dtstr)) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2975 | PyErr_SetString(PyExc_TypeError, |
| 2976 | "fromisoformat: argument must be str"); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2977 | return NULL; |
| 2978 | } |
| 2979 | |
| 2980 | Py_ssize_t len; |
| 2981 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2982 | const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 2983 | if (dt_ptr == NULL) { |
| 2984 | goto invalid_string_error; |
| 2985 | } |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2986 | |
| 2987 | int year = 0, month = 0, day = 0; |
| 2988 | |
| 2989 | int rv; |
| 2990 | if (len == 10) { |
| 2991 | rv = parse_isoformat_date(dt_ptr, &year, &month, &day); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2992 | } |
| 2993 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2994 | rv = -1; |
| 2995 | } |
| 2996 | |
| 2997 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 2998 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2999 | } |
| 3000 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 3001 | return new_date_subclass_ex(year, month, day, cls); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 3002 | |
| 3003 | invalid_string_error: |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 3004 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 3005 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 3006 | } |
| 3007 | |
Paul Ganssle | 88c0937 | 2019-04-29 09:22:03 -0400 | [diff] [blame] | 3008 | |
| 3009 | static PyObject * |
| 3010 | date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) |
| 3011 | { |
| 3012 | static char *keywords[] = { |
| 3013 | "year", "week", "day", NULL |
| 3014 | }; |
| 3015 | |
| 3016 | int year, week, day; |
| 3017 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii:fromisocalendar", |
| 3018 | keywords, |
| 3019 | &year, &week, &day) == 0) { |
| 3020 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 3021 | PyErr_Format(PyExc_ValueError, |
| 3022 | "ISO calendar component out of range"); |
| 3023 | |
| 3024 | } |
| 3025 | return NULL; |
| 3026 | } |
| 3027 | |
| 3028 | // Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5) |
| 3029 | if (year < MINYEAR || year > MAXYEAR) { |
| 3030 | PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year); |
| 3031 | return NULL; |
| 3032 | } |
| 3033 | |
| 3034 | if (week <= 0 || week >= 53) { |
| 3035 | int out_of_range = 1; |
| 3036 | if (week == 53) { |
| 3037 | // ISO years have 53 weeks in it on years starting with a Thursday |
| 3038 | // and on leap years starting on Wednesday |
| 3039 | int first_weekday = weekday(year, 1, 1); |
| 3040 | if (first_weekday == 3 || (first_weekday == 2 && is_leap(year))) { |
| 3041 | out_of_range = 0; |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | if (out_of_range) { |
| 3046 | PyErr_Format(PyExc_ValueError, "Invalid week: %d", week); |
| 3047 | return NULL; |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | if (day <= 0 || day >= 8) { |
| 3052 | PyErr_Format(PyExc_ValueError, "Invalid day: %d (range is [1, 7])", |
| 3053 | day); |
| 3054 | return NULL; |
| 3055 | } |
| 3056 | |
| 3057 | // Convert (Y, W, D) to (Y, M, D) in-place |
| 3058 | int day_1 = iso_week1_monday(year); |
| 3059 | |
| 3060 | int month = week; |
| 3061 | int day_offset = (month - 1)*7 + day - 1; |
| 3062 | |
| 3063 | ord_to_ymd(day_1 + day_offset, &year, &month, &day); |
| 3064 | |
| 3065 | return new_date_subclass_ex(year, month, day, cls); |
| 3066 | } |
| 3067 | |
| 3068 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3069 | /* |
| 3070 | * Date arithmetic. |
| 3071 | */ |
| 3072 | |
| 3073 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 3074 | * instead. |
| 3075 | */ |
| 3076 | static PyObject * |
| 3077 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 3078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | PyObject *result = NULL; |
| 3080 | int year = GET_YEAR(date); |
| 3081 | int month = GET_MONTH(date); |
| 3082 | int deltadays = GET_TD_DAYS(delta); |
| 3083 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 3084 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | if (normalize_date(&year, &month, &day) >= 0) |
Paul Ganssle | 89427cd | 2019-02-04 14:42:04 -0500 | [diff] [blame] | 3087 | result = new_date_subclass_ex(year, month, day, |
| 3088 | (PyObject* )Py_TYPE(date)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | static PyObject * |
| 3093 | date_add(PyObject *left, PyObject *right) |
| 3094 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3095 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 3096 | Py_RETURN_NOTIMPLEMENTED; |
| 3097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | if (PyDate_Check(left)) { |
| 3099 | /* date + ??? */ |
| 3100 | if (PyDelta_Check(right)) |
| 3101 | /* date + delta */ |
| 3102 | return add_date_timedelta((PyDateTime_Date *) left, |
| 3103 | (PyDateTime_Delta *) right, |
| 3104 | 0); |
| 3105 | } |
| 3106 | else { |
| 3107 | /* ??? + date |
| 3108 | * 'right' must be one of us, or we wouldn't have been called |
| 3109 | */ |
| 3110 | if (PyDelta_Check(left)) |
| 3111 | /* delta + date */ |
| 3112 | return add_date_timedelta((PyDateTime_Date *) right, |
| 3113 | (PyDateTime_Delta *) left, |
| 3114 | 0); |
| 3115 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3116 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | static PyObject * |
| 3120 | date_subtract(PyObject *left, PyObject *right) |
| 3121 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3122 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 3123 | Py_RETURN_NOTIMPLEMENTED; |
| 3124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | if (PyDate_Check(left)) { |
| 3126 | if (PyDate_Check(right)) { |
| 3127 | /* date - date */ |
| 3128 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 3129 | GET_MONTH(left), |
| 3130 | GET_DAY(left)); |
| 3131 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 3132 | GET_MONTH(right), |
| 3133 | GET_DAY(right)); |
| 3134 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 3135 | } |
| 3136 | if (PyDelta_Check(right)) { |
| 3137 | /* date - delta */ |
| 3138 | return add_date_timedelta((PyDateTime_Date *) left, |
| 3139 | (PyDateTime_Delta *) right, |
| 3140 | 1); |
| 3141 | } |
| 3142 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3143 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | |
| 3147 | /* Various ways to turn a date into a string. */ |
| 3148 | |
| 3149 | static PyObject * |
| 3150 | date_repr(PyDateTime_Date *self) |
| 3151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3153 | Py_TYPE(self)->tp_name, |
| 3154 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3158 | date_isoformat(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 3161 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 3164 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3165 | static PyObject * |
| 3166 | date_str(PyDateTime_Date *self) |
| 3167 | { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 3168 | return _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_isoformat); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | |
| 3172 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3173 | date_ctime(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | static PyObject * |
| 3179 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | /* This method can be inherited, and needs to call the |
| 3182 | * timetuple() method appropriate to self's class. |
| 3183 | */ |
| 3184 | PyObject *result; |
| 3185 | PyObject *tuple; |
| 3186 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3187 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3191 | &format)) |
| 3192 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3193 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 3194 | tuple = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | if (tuple == NULL) |
| 3196 | return NULL; |
| 3197 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3198 | (PyObject *)self); |
| 3199 | Py_DECREF(tuple); |
| 3200 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3203 | static PyObject * |
| 3204 | date_format(PyDateTime_Date *self, PyObject *args) |
| 3205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 3209 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 3212 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3214 | |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 3215 | return _PyObject_CallMethodIdOneArg((PyObject *)self, &PyId_strftime, |
| 3216 | format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3219 | /* ISO methods. */ |
| 3220 | |
| 3221 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3222 | date_isoweekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 3229 | PyDoc_STRVAR(iso_calendar_date__doc__, |
| 3230 | "The result of date.isocalendar() or datetime.isocalendar()\n\n\ |
| 3231 | This object may be accessed either as a tuple of\n\ |
| 3232 | ((year, week, weekday)\n\ |
| 3233 | or via the object attributes as named in the above tuple."); |
| 3234 | |
| 3235 | typedef struct { |
| 3236 | PyTupleObject tuple; |
| 3237 | } PyDateTime_IsoCalendarDate; |
| 3238 | |
| 3239 | static PyObject * |
| 3240 | iso_calendar_date_repr(PyDateTime_IsoCalendarDate *self) |
| 3241 | { |
| 3242 | PyObject* year = PyTuple_GetItem((PyObject *)self, 0); |
| 3243 | if (year == NULL) { |
| 3244 | return NULL; |
| 3245 | } |
| 3246 | PyObject* week = PyTuple_GetItem((PyObject *)self, 1); |
| 3247 | if (week == NULL) { |
| 3248 | return NULL; |
| 3249 | } |
| 3250 | PyObject* weekday = PyTuple_GetItem((PyObject *)self, 2); |
| 3251 | if (weekday == NULL) { |
| 3252 | return NULL; |
| 3253 | } |
| 3254 | |
| 3255 | return PyUnicode_FromFormat("%.200s(year=%S, week=%S, weekday=%S)", |
| 3256 | Py_TYPE(self)->tp_name, year, week, weekday); |
| 3257 | } |
| 3258 | |
| 3259 | static PyObject * |
| 3260 | iso_calendar_date_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 3261 | { |
| 3262 | // Construct the tuple that this reduces to |
| 3263 | PyObject * reduce_tuple = Py_BuildValue( |
| 3264 | "O((OOO))", &PyTuple_Type, |
| 3265 | PyTuple_GET_ITEM(self, 0), |
| 3266 | PyTuple_GET_ITEM(self, 1), |
| 3267 | PyTuple_GET_ITEM(self, 2) |
| 3268 | ); |
| 3269 | |
| 3270 | return reduce_tuple; |
| 3271 | } |
| 3272 | |
| 3273 | static PyObject * |
| 3274 | iso_calendar_date_year(PyDateTime_IsoCalendarDate *self, void *unused) |
| 3275 | { |
| 3276 | PyObject *year = PyTuple_GetItem((PyObject *)self, 0); |
| 3277 | if (year == NULL) { |
| 3278 | return NULL; |
| 3279 | } |
| 3280 | Py_INCREF(year); |
| 3281 | return year; |
| 3282 | } |
| 3283 | |
| 3284 | static PyObject * |
| 3285 | iso_calendar_date_week(PyDateTime_IsoCalendarDate *self, void *unused) |
| 3286 | { |
| 3287 | PyObject *week = PyTuple_GetItem((PyObject *)self, 1); |
| 3288 | if (week == NULL) { |
| 3289 | return NULL; |
| 3290 | } |
| 3291 | Py_INCREF(week); |
| 3292 | return week; |
| 3293 | } |
| 3294 | |
| 3295 | static PyObject * |
| 3296 | iso_calendar_date_weekday(PyDateTime_IsoCalendarDate *self, void *unused) |
| 3297 | { |
| 3298 | PyObject *weekday = PyTuple_GetItem((PyObject *)self, 2); |
| 3299 | if (weekday == NULL) { |
| 3300 | return NULL; |
| 3301 | } |
| 3302 | Py_INCREF(weekday); |
| 3303 | return weekday; |
| 3304 | } |
| 3305 | |
| 3306 | static PyGetSetDef iso_calendar_date_getset[] = { |
| 3307 | {"year", (getter)iso_calendar_date_year}, |
| 3308 | {"week", (getter)iso_calendar_date_week}, |
| 3309 | {"weekday", (getter)iso_calendar_date_weekday}, |
| 3310 | {NULL} |
| 3311 | }; |
| 3312 | |
| 3313 | static PyMethodDef iso_calendar_date_methods[] = { |
| 3314 | {"__reduce__", (PyCFunction)iso_calendar_date_reduce, METH_NOARGS, |
| 3315 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 3316 | {NULL, NULL}, |
| 3317 | }; |
| 3318 | |
| 3319 | static PyTypeObject PyDateTime_IsoCalendarDateType = { |
| 3320 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3321 | .tp_name = "datetime.IsoCalendarDate", |
| 3322 | .tp_basicsize = sizeof(PyDateTime_IsoCalendarDate), |
| 3323 | .tp_repr = (reprfunc) iso_calendar_date_repr, |
| 3324 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 3325 | .tp_doc = iso_calendar_date__doc__, |
| 3326 | .tp_methods = iso_calendar_date_methods, |
| 3327 | .tp_getset = iso_calendar_date_getset, |
Miss Islington (bot) | eceee54 | 2020-05-28 09:41:41 -0700 | [diff] [blame^] | 3328 | // .tp_base = &PyTuple_Type, // filled in PyInit__datetime |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 3329 | .tp_new = iso_calendar_date_new, |
| 3330 | }; |
| 3331 | |
| 3332 | /*[clinic input] |
| 3333 | @classmethod |
| 3334 | datetime.IsoCalendarDate.__new__ as iso_calendar_date_new |
| 3335 | year: int |
| 3336 | week: int |
| 3337 | weekday: int |
| 3338 | [clinic start generated code]*/ |
| 3339 | |
| 3340 | static PyObject * |
| 3341 | iso_calendar_date_new_impl(PyTypeObject *type, int year, int week, |
| 3342 | int weekday) |
| 3343 | /*[clinic end generated code: output=383d33d8dc7183a2 input=4f2c663c9d19c4ee]*/ |
| 3344 | |
| 3345 | { |
| 3346 | PyDateTime_IsoCalendarDate *self; |
| 3347 | self = (PyDateTime_IsoCalendarDate *) type->tp_alloc(type, 3); |
| 3348 | if (self == NULL) { |
| 3349 | return NULL; |
| 3350 | } |
| 3351 | |
| 3352 | PyTuple_SET_ITEM(self, 0, PyLong_FromLong(year)); |
| 3353 | PyTuple_SET_ITEM(self, 1, PyLong_FromLong(week)); |
| 3354 | PyTuple_SET_ITEM(self, 2, PyLong_FromLong(weekday)); |
| 3355 | |
| 3356 | return (PyObject *)self; |
| 3357 | } |
| 3358 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3359 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3360 | date_isocalendar(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3362 | int year = GET_YEAR(self); |
| 3363 | int week1_monday = iso_week1_monday(year); |
| 3364 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 3365 | int week; |
| 3366 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | week = divmod(today - week1_monday, 7, &day); |
| 3369 | if (week < 0) { |
| 3370 | --year; |
| 3371 | week1_monday = iso_week1_monday(year); |
| 3372 | week = divmod(today - week1_monday, 7, &day); |
| 3373 | } |
| 3374 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 3375 | ++year; |
| 3376 | week = 0; |
| 3377 | } |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 3378 | |
| 3379 | PyObject* v = iso_calendar_date_new_impl(&PyDateTime_IsoCalendarDateType, |
| 3380 | year, week + 1, day + 1); |
| 3381 | if (v == NULL) { |
| 3382 | return NULL; |
| 3383 | } |
| 3384 | return v; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | /* Miscellaneous methods. */ |
| 3388 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3389 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3390 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | if (PyDate_Check(other)) { |
| 3393 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 3394 | ((PyDateTime_Date *)other)->data, |
| 3395 | _PyDateTime_DATE_DATASIZE); |
| 3396 | return diff_to_bool(diff, op); |
| 3397 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3398 | else |
| 3399 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3403 | date_timetuple(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3404 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | return build_struct_time(GET_YEAR(self), |
| 3406 | GET_MONTH(self), |
| 3407 | GET_DAY(self), |
| 3408 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3411 | static PyObject * |
| 3412 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3414 | PyObject *clone; |
| 3415 | PyObject *tuple; |
| 3416 | int year = GET_YEAR(self); |
| 3417 | int month = GET_MONTH(self); |
| 3418 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 3421 | &year, &month, &day)) |
| 3422 | return NULL; |
| 3423 | tuple = Py_BuildValue("iii", year, month, day); |
| 3424 | if (tuple == NULL) |
| 3425 | return NULL; |
| 3426 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 3427 | Py_DECREF(tuple); |
| 3428 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3431 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3432 | generic_hash(unsigned char *data, int len) |
| 3433 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 3434 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | |
| 3438 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3439 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3440 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3441 | date_hash(PyDateTime_Date *self) |
| 3442 | { |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3443 | if (self->hashcode == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | self->hashcode = generic_hash( |
| 3445 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3446 | } |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3452 | date_toordinal(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 3455 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3459 | date_weekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3466 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3467 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3468 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3469 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3470 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | PyObject* field; |
| 3473 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 3474 | _PyDateTime_DATE_DATASIZE); |
| 3475 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3479 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | /* Class methods: */ |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 3487 | DATETIME_DATE_FROMTIMESTAMP_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 3490 | METH_CLASS, |
| 3491 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 3492 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3493 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 3494 | {"fromisoformat", (PyCFunction)date_fromisoformat, METH_O | |
| 3495 | METH_CLASS, |
| 3496 | PyDoc_STR("str -> Construct a date from the output of date.isoformat()")}, |
| 3497 | |
Paul Ganssle | 88c0937 | 2019-04-29 09:22:03 -0400 | [diff] [blame] | 3498 | {"fromisocalendar", (PyCFunction)(void(*)(void))date_fromisocalendar, |
| 3499 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 3500 | PyDoc_STR("int, int, int -> Construct a date from the ISO year, week " |
| 3501 | "number and weekday.\n\n" |
| 3502 | "This is the inverse of the date.isocalendar() function")}, |
| 3503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3504 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 3505 | PyDoc_STR("Current date or datetime: same as " |
| 3506 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 3511 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3512 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 3513 | {"strftime", (PyCFunction)(void(*)(void))date_strftime, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3517 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 3520 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 3523 | PyDoc_STR("Return a named tuple containing ISO year, week number, and " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3526 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 3527 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3529 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 3530 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3531 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 3534 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 3535 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 3538 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3539 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3540 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 3541 | {"replace", (PyCFunction)(void(*)(void))date_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3542 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 3545 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3548 | }; |
| 3549 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3550 | static const char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3551 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3552 | |
| 3553 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | date_add, /* nb_add */ |
| 3555 | date_subtract, /* nb_subtract */ |
| 3556 | 0, /* nb_multiply */ |
| 3557 | 0, /* nb_remainder */ |
| 3558 | 0, /* nb_divmod */ |
| 3559 | 0, /* nb_power */ |
| 3560 | 0, /* nb_negative */ |
| 3561 | 0, /* nb_positive */ |
| 3562 | 0, /* nb_absolute */ |
| 3563 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3564 | }; |
| 3565 | |
| 3566 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3568 | "datetime.date", /* tp_name */ |
| 3569 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 3570 | 0, /* tp_itemsize */ |
| 3571 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3572 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | 0, /* tp_getattr */ |
| 3574 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3575 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | (reprfunc)date_repr, /* tp_repr */ |
| 3577 | &date_as_number, /* tp_as_number */ |
| 3578 | 0, /* tp_as_sequence */ |
| 3579 | 0, /* tp_as_mapping */ |
| 3580 | (hashfunc)date_hash, /* tp_hash */ |
| 3581 | 0, /* tp_call */ |
| 3582 | (reprfunc)date_str, /* tp_str */ |
| 3583 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3584 | 0, /* tp_setattro */ |
| 3585 | 0, /* tp_as_buffer */ |
| 3586 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3587 | date_doc, /* tp_doc */ |
| 3588 | 0, /* tp_traverse */ |
| 3589 | 0, /* tp_clear */ |
| 3590 | date_richcompare, /* tp_richcompare */ |
| 3591 | 0, /* tp_weaklistoffset */ |
| 3592 | 0, /* tp_iter */ |
| 3593 | 0, /* tp_iternext */ |
| 3594 | date_methods, /* tp_methods */ |
| 3595 | 0, /* tp_members */ |
| 3596 | date_getset, /* tp_getset */ |
| 3597 | 0, /* tp_base */ |
| 3598 | 0, /* tp_dict */ |
| 3599 | 0, /* tp_descr_get */ |
| 3600 | 0, /* tp_descr_set */ |
| 3601 | 0, /* tp_dictoffset */ |
| 3602 | 0, /* tp_init */ |
| 3603 | 0, /* tp_alloc */ |
| 3604 | date_new, /* tp_new */ |
| 3605 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3606 | }; |
| 3607 | |
| 3608 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3609 | * PyDateTime_TZInfo implementation. |
| 3610 | */ |
| 3611 | |
| 3612 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 3613 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 3614 | * 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] | 3615 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3616 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 3617 | * |
| 3618 | * Note: For reasons having to do with pickling of subclasses, we have |
| 3619 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 3620 | * in the Python implementation (__init__() could raise NotImplementedError |
| 3621 | * there without ill effect), but doing so in the C implementation hit a |
| 3622 | * brick wall. |
| 3623 | */ |
| 3624 | |
| 3625 | static PyObject * |
| 3626 | tzinfo_nogo(const char* methodname) |
| 3627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | PyErr_Format(PyExc_NotImplementedError, |
| 3629 | "a tzinfo subclass must implement %s()", |
| 3630 | methodname); |
| 3631 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | /* Methods. A subclass must implement these. */ |
| 3635 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3636 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3637 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 3638 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3639 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3642 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3643 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 3644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3648 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3649 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 3650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3654 | |
| 3655 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 3656 | PyDateTime_Delta *delta, |
| 3657 | int factor); |
| 3658 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 3659 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 3660 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3661 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3662 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3663 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3664 | PyObject *result = NULL; |
| 3665 | PyObject *off = NULL, *dst = NULL; |
| 3666 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3667 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3668 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | PyErr_SetString(PyExc_TypeError, |
| 3670 | "fromutc: argument must be a datetime"); |
| 3671 | return NULL; |
| 3672 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3673 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3675 | "is not self"); |
| 3676 | return NULL; |
| 3677 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3678 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3679 | off = datetime_utcoffset(dt, NULL); |
| 3680 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3682 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3683 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3684 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3685 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3686 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3687 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3688 | dst = datetime_dst(dt, NULL); |
| 3689 | if (dst == NULL) |
| 3690 | goto Fail; |
| 3691 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3692 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3693 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3694 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3695 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3696 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3697 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3698 | if (delta == NULL) |
| 3699 | goto Fail; |
| 3700 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3701 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3702 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3703 | |
| 3704 | Py_DECREF(dst); |
| 3705 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3706 | if (dst == NULL) |
| 3707 | goto Fail; |
| 3708 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3709 | goto Inconsistent; |
Alexander Belopolsky | c79447b | 2015-09-27 21:41:55 -0400 | [diff] [blame] | 3710 | if (delta_bool((PyDateTime_Delta *)dst) != 0) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3711 | Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 3712 | (PyDateTime_Delta *)dst, 1)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3713 | if (result == NULL) |
| 3714 | goto Fail; |
| 3715 | } |
| 3716 | Py_DECREF(delta); |
| 3717 | Py_DECREF(dst); |
| 3718 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3719 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3720 | |
| 3721 | Inconsistent: |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 3722 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3724 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 3725 | /* fall through to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3726 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3727 | Py_XDECREF(off); |
| 3728 | Py_XDECREF(dst); |
| 3729 | Py_XDECREF(delta); |
| 3730 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3731 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3734 | /* |
| 3735 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3736 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3737 | */ |
| 3738 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3739 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3740 | tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3741 | { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3742 | PyObject *args, *state; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3744 | _Py_IDENTIFIER(__getinitargs__); |
| 3745 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3746 | |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 3747 | if (_PyObject_LookupAttrId(self, &PyId___getinitargs__, &getinitargs) < 0) { |
| 3748 | return NULL; |
| 3749 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | if (getinitargs != NULL) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 3751 | args = PyObject_CallNoArgs(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | Py_DECREF(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3753 | } |
| 3754 | else { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3755 | args = PyTuple_New(0); |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 3756 | } |
| 3757 | if (args == NULL) { |
| 3758 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3760 | |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 3761 | if (_PyObject_LookupAttrId(self, &PyId___getstate__, &getstate) < 0) { |
| 3762 | Py_DECREF(args); |
| 3763 | return NULL; |
| 3764 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3765 | if (getstate != NULL) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 3766 | state = PyObject_CallNoArgs(getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | Py_DECREF(getstate); |
| 3768 | if (state == NULL) { |
| 3769 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3770 | return NULL; |
| 3771 | } |
| 3772 | } |
| 3773 | else { |
| 3774 | PyObject **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3775 | state = Py_None; |
| 3776 | dictptr = _PyObject_GetDictPtr(self); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3777 | if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3778 | state = *dictptr; |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3779 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3780 | Py_INCREF(state); |
| 3781 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3783 | if (state == Py_None) { |
| 3784 | Py_DECREF(state); |
| 3785 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3786 | } |
| 3787 | else |
| 3788 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3789 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3790 | |
| 3791 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3793 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3794 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3797 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3798 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3800 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3801 | PyDoc_STR("datetime -> DST offset as timedelta positive east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3804 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3805 | |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3806 | {"__reduce__", tzinfo_reduce, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3807 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3809 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3810 | }; |
| 3811 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3812 | static const char tzinfo_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3813 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3814 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3815 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3816 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3817 | "datetime.tzinfo", /* tp_name */ |
| 3818 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3819 | 0, /* tp_itemsize */ |
| 3820 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3821 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3822 | 0, /* tp_getattr */ |
| 3823 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3824 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | 0, /* tp_repr */ |
| 3826 | 0, /* tp_as_number */ |
| 3827 | 0, /* tp_as_sequence */ |
| 3828 | 0, /* tp_as_mapping */ |
| 3829 | 0, /* tp_hash */ |
| 3830 | 0, /* tp_call */ |
| 3831 | 0, /* tp_str */ |
| 3832 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3833 | 0, /* tp_setattro */ |
| 3834 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3835 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3836 | tzinfo_doc, /* tp_doc */ |
| 3837 | 0, /* tp_traverse */ |
| 3838 | 0, /* tp_clear */ |
| 3839 | 0, /* tp_richcompare */ |
| 3840 | 0, /* tp_weaklistoffset */ |
| 3841 | 0, /* tp_iter */ |
| 3842 | 0, /* tp_iternext */ |
| 3843 | tzinfo_methods, /* tp_methods */ |
| 3844 | 0, /* tp_members */ |
| 3845 | 0, /* tp_getset */ |
| 3846 | 0, /* tp_base */ |
| 3847 | 0, /* tp_dict */ |
| 3848 | 0, /* tp_descr_get */ |
| 3849 | 0, /* tp_descr_set */ |
| 3850 | 0, /* tp_dictoffset */ |
| 3851 | 0, /* tp_init */ |
| 3852 | 0, /* tp_alloc */ |
| 3853 | PyType_GenericNew, /* tp_new */ |
| 3854 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3855 | }; |
| 3856 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3857 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3858 | |
| 3859 | static PyObject * |
| 3860 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3861 | { |
| 3862 | PyObject *offset; |
| 3863 | PyObject *name = NULL; |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 3864 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|U:timezone", timezone_kws, |
| 3865 | &PyDateTime_DeltaType, &offset, &name)) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3866 | return new_timezone(offset, name); |
| 3867 | |
| 3868 | return NULL; |
| 3869 | } |
| 3870 | |
| 3871 | static void |
| 3872 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3873 | { |
| 3874 | Py_CLEAR(self->offset); |
| 3875 | Py_CLEAR(self->name); |
| 3876 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3877 | } |
| 3878 | |
| 3879 | static PyObject * |
| 3880 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3881 | PyDateTime_TimeZone *other, int op) |
| 3882 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3883 | if (op != Py_EQ && op != Py_NE) |
| 3884 | Py_RETURN_NOTIMPLEMENTED; |
Pablo Galindo | 4be11c0 | 2019-08-22 20:24:25 +0100 | [diff] [blame] | 3885 | if (!PyTimezone_Check(other)) { |
Serhiy Storchaka | 17e5264 | 2019-08-04 12:38:46 +0300 | [diff] [blame] | 3886 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3887 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3888 | return delta_richcompare(self->offset, other->offset, op); |
| 3889 | } |
| 3890 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3891 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3892 | timezone_hash(PyDateTime_TimeZone *self) |
| 3893 | { |
| 3894 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3895 | } |
| 3896 | |
| 3897 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3898 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3899 | otherwise. |
| 3900 | */ |
| 3901 | static int |
| 3902 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3903 | { |
| 3904 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3905 | return 0; |
| 3906 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3907 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3908 | return -1; |
| 3909 | } |
| 3910 | |
| 3911 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3912 | timezone_repr(PyDateTime_TimeZone *self) |
| 3913 | { |
| 3914 | /* Note that although timezone is not subclassable, it is convenient |
| 3915 | to use Py_TYPE(self)->tp_name here. */ |
| 3916 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3917 | |
| 3918 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3919 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3920 | |
| 3921 | if (self->name == NULL) |
| 3922 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3923 | |
| 3924 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3925 | self->name); |
| 3926 | } |
| 3927 | |
| 3928 | |
| 3929 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3930 | timezone_str(PyDateTime_TimeZone *self) |
| 3931 | { |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3932 | int hours, minutes, seconds, microseconds; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3933 | PyObject *offset; |
| 3934 | char sign; |
| 3935 | |
| 3936 | if (self->name != NULL) { |
| 3937 | Py_INCREF(self->name); |
| 3938 | return self->name; |
| 3939 | } |
Victor Stinner | 90fd895 | 2015-09-08 00:12:49 +0200 | [diff] [blame] | 3940 | if ((PyObject *)self == PyDateTime_TimeZone_UTC || |
Alexander Belopolsky | 7827a5b | 2015-09-06 13:07:21 -0400 | [diff] [blame] | 3941 | (GET_TD_DAYS(self->offset) == 0 && |
| 3942 | GET_TD_SECONDS(self->offset) == 0 && |
| 3943 | GET_TD_MICROSECONDS(self->offset) == 0)) |
| 3944 | return PyUnicode_FromString("UTC"); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3945 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3946 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3947 | sign = '-'; |
| 3948 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3949 | if (offset == NULL) |
| 3950 | return NULL; |
| 3951 | } |
| 3952 | else { |
| 3953 | sign = '+'; |
| 3954 | offset = self->offset; |
| 3955 | Py_INCREF(offset); |
| 3956 | } |
| 3957 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3958 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3959 | seconds = GET_TD_SECONDS(offset); |
| 3960 | Py_DECREF(offset); |
| 3961 | minutes = divmod(seconds, 60, &seconds); |
| 3962 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3963 | if (microseconds != 0) { |
| 3964 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d.%06d", |
| 3965 | sign, hours, minutes, |
| 3966 | seconds, microseconds); |
| 3967 | } |
| 3968 | if (seconds != 0) { |
| 3969 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d", |
| 3970 | sign, hours, minutes, seconds); |
| 3971 | } |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3972 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | static PyObject * |
| 3976 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3977 | { |
| 3978 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3979 | return NULL; |
| 3980 | |
| 3981 | return timezone_str(self); |
| 3982 | } |
| 3983 | |
| 3984 | static PyObject * |
| 3985 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3986 | { |
| 3987 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3988 | return NULL; |
| 3989 | |
| 3990 | Py_INCREF(self->offset); |
| 3991 | return self->offset; |
| 3992 | } |
| 3993 | |
| 3994 | static PyObject * |
| 3995 | timezone_dst(PyObject *self, PyObject *dt) |
| 3996 | { |
| 3997 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3998 | return NULL; |
| 3999 | |
| 4000 | Py_RETURN_NONE; |
| 4001 | } |
| 4002 | |
| 4003 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4004 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 4005 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4006 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4007 | PyErr_SetString(PyExc_TypeError, |
| 4008 | "fromutc: argument must be a datetime"); |
| 4009 | return NULL; |
| 4010 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4011 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4012 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 4013 | "is not self"); |
| 4014 | return NULL; |
| 4015 | } |
| 4016 | |
| 4017 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 4018 | } |
| 4019 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 4020 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4021 | timezone_getinitargs(PyDateTime_TimeZone *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 4022 | { |
| 4023 | if (self->name == NULL) |
| 4024 | return Py_BuildValue("(O)", self->offset); |
| 4025 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 4026 | } |
| 4027 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4028 | static PyMethodDef timezone_methods[] = { |
| 4029 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 4030 | 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] | 4031 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4032 | |
| 4033 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 4034 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4035 | |
| 4036 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 4037 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4038 | |
| 4039 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 4040 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 4041 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 4042 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 4043 | PyDoc_STR("pickle support")}, |
| 4044 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4045 | {NULL, NULL} |
| 4046 | }; |
| 4047 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4048 | static const char timezone_doc[] = |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4049 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 4050 | |
| 4051 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 4052 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4053 | "datetime.timezone", /* tp_name */ |
| 4054 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 4055 | 0, /* tp_itemsize */ |
| 4056 | (destructor)timezone_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4057 | 0, /* tp_vectorcall_offset */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4058 | 0, /* tp_getattr */ |
| 4059 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4060 | 0, /* tp_as_async */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 4061 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4062 | 0, /* tp_as_number */ |
| 4063 | 0, /* tp_as_sequence */ |
| 4064 | 0, /* tp_as_mapping */ |
| 4065 | (hashfunc)timezone_hash, /* tp_hash */ |
| 4066 | 0, /* tp_call */ |
| 4067 | (reprfunc)timezone_str, /* tp_str */ |
| 4068 | 0, /* tp_getattro */ |
| 4069 | 0, /* tp_setattro */ |
| 4070 | 0, /* tp_as_buffer */ |
| 4071 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 4072 | timezone_doc, /* tp_doc */ |
| 4073 | 0, /* tp_traverse */ |
| 4074 | 0, /* tp_clear */ |
| 4075 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 4076 | 0, /* tp_weaklistoffset */ |
| 4077 | 0, /* tp_iter */ |
| 4078 | 0, /* tp_iternext */ |
| 4079 | timezone_methods, /* tp_methods */ |
| 4080 | 0, /* tp_members */ |
| 4081 | 0, /* tp_getset */ |
Miss Islington (bot) | eceee54 | 2020-05-28 09:41:41 -0700 | [diff] [blame^] | 4082 | 0, /* tp_base; filled in PyInit__datetime */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 4083 | 0, /* tp_dict */ |
| 4084 | 0, /* tp_descr_get */ |
| 4085 | 0, /* tp_descr_set */ |
| 4086 | 0, /* tp_dictoffset */ |
| 4087 | 0, /* tp_init */ |
| 4088 | 0, /* tp_alloc */ |
| 4089 | timezone_new, /* tp_new */ |
| 4090 | }; |
| 4091 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4092 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4093 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4094 | */ |
| 4095 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4096 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4097 | */ |
| 4098 | |
| 4099 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4100 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4102 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4105 | static PyObject * |
| 4106 | time_minute(PyDateTime_Time *self, void *unused) |
| 4107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4108 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | /* The name time_second conflicted with some platform header file. */ |
| 4112 | static PyObject * |
| 4113 | py_time_second(PyDateTime_Time *self, void *unused) |
| 4114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4115 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | static PyObject * |
| 4119 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 4120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4121 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | static PyObject * |
| 4125 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 4126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4127 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 4128 | Py_INCREF(result); |
| 4129 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4132 | static PyObject * |
| 4133 | time_fold(PyDateTime_Time *self, void *unused) |
| 4134 | { |
| 4135 | return PyLong_FromLong(TIME_GET_FOLD(self)); |
| 4136 | } |
| 4137 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4138 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4139 | {"hour", (getter)time_hour}, |
| 4140 | {"minute", (getter)time_minute}, |
| 4141 | {"second", (getter)py_time_second}, |
| 4142 | {"microsecond", (getter)time_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4143 | {"tzinfo", (getter)time_tzinfo}, |
| 4144 | {"fold", (getter)time_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4145 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4146 | }; |
| 4147 | |
| 4148 | /* |
| 4149 | * Constructors. |
| 4150 | */ |
| 4151 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4152 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4153 | "tzinfo", "fold", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4154 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4155 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4156 | time_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo) |
| 4157 | { |
| 4158 | PyDateTime_Time *me; |
| 4159 | char aware = (char)(tzinfo != Py_None); |
| 4160 | |
| 4161 | if (aware && check_tzinfo_subclass(tzinfo) < 0) { |
| 4162 | PyErr_SetString(PyExc_TypeError, "bad tzinfo state arg"); |
| 4163 | return NULL; |
| 4164 | } |
| 4165 | |
| 4166 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 4167 | if (me != NULL) { |
| 4168 | const char *pdata = PyBytes_AS_STRING(state); |
| 4169 | |
| 4170 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 4171 | me->hashcode = -1; |
| 4172 | me->hastzinfo = aware; |
| 4173 | if (aware) { |
| 4174 | Py_INCREF(tzinfo); |
| 4175 | me->tzinfo = tzinfo; |
| 4176 | } |
| 4177 | if (pdata[0] & (1 << 7)) { |
| 4178 | me->data[0] -= 128; |
| 4179 | me->fold = 1; |
| 4180 | } |
| 4181 | else { |
| 4182 | me->fold = 0; |
| 4183 | } |
| 4184 | } |
| 4185 | return (PyObject *)me; |
| 4186 | } |
| 4187 | |
| 4188 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4189 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4191 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4192 | int hour = 0; |
| 4193 | int minute = 0; |
| 4194 | int second = 0; |
| 4195 | int usecond = 0; |
| 4196 | PyObject *tzinfo = Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4197 | int fold = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4200 | if (PyTuple_GET_SIZE(args) >= 1 && PyTuple_GET_SIZE(args) <= 2) { |
| 4201 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 4202 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4203 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4204 | } |
| 4205 | if (PyBytes_Check(state)) { |
| 4206 | if (PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 4207 | (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) |
| 4208 | { |
| 4209 | return time_from_pickle(type, state, tzinfo); |
| 4210 | } |
| 4211 | } |
| 4212 | else if (PyUnicode_Check(state)) { |
| 4213 | if (PyUnicode_READY(state)) { |
| 4214 | return NULL; |
| 4215 | } |
| 4216 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_TIME_DATASIZE && |
Justin Blanchard | 122376d | 2019-08-29 03:36:15 -0400 | [diff] [blame] | 4217 | (0x7F & PyUnicode_READ_CHAR(state, 0)) < 24) |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4218 | { |
| 4219 | state = PyUnicode_AsLatin1String(state); |
| 4220 | if (state == NULL) { |
| 4221 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4222 | /* More informative error message. */ |
| 4223 | PyErr_SetString(PyExc_ValueError, |
| 4224 | "Failed to encode latin1 string when unpickling " |
| 4225 | "a time object. " |
| 4226 | "pickle.load(data, encoding='latin1') is assumed."); |
| 4227 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 4228 | return NULL; |
| 4229 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4230 | self = time_from_pickle(type, state, tzinfo); |
| 4231 | Py_DECREF(state); |
| 4232 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4234 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4235 | tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4236 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4237 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4238 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4239 | &hour, &minute, &second, &usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4240 | &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4241 | self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, |
| 4242 | type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | } |
| 4244 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | /* |
| 4248 | * Destructor. |
| 4249 | */ |
| 4250 | |
| 4251 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4252 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4254 | if (HASTZINFO(self)) { |
| 4255 | Py_XDECREF(self->tzinfo); |
| 4256 | } |
| 4257 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4261 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4262 | */ |
| 4263 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4264 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4265 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4266 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 4267 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4271 | time_dst(PyObject *self, PyObject *unused) { |
| 4272 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4276 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4277 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4281 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4282 | */ |
| 4283 | |
| 4284 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4285 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4287 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4288 | int h = TIME_GET_HOUR(self); |
| 4289 | int m = TIME_GET_MINUTE(self); |
| 4290 | int s = TIME_GET_SECOND(self); |
| 4291 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4292 | int fold = TIME_GET_FOLD(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4295 | if (us) |
| 4296 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 4297 | type_name, h, m, s, us); |
| 4298 | else if (s) |
| 4299 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 4300 | type_name, h, m, s); |
| 4301 | else |
| 4302 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 4303 | if (result != NULL && HASTZINFO(self)) |
| 4304 | result = append_keyword_tzinfo(result, self->tzinfo); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4305 | if (result != NULL && fold) |
| 4306 | result = append_keyword_fold(result, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4310 | static PyObject * |
| 4311 | time_str(PyDateTime_Time *self) |
| 4312 | { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 4313 | return _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_isoformat); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4314 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4315 | |
| 4316 | static PyObject * |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4317 | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | char buf[100]; |
Andy Lester | c3fa634 | 2020-02-24 00:40:43 -0600 | [diff] [blame] | 4320 | const char *timespec = NULL; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4321 | static char *keywords[] = {"timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4322 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 4323 | int us = TIME_GET_MICROSECOND(self); |
Andy Lester | c3fa634 | 2020-02-24 00:40:43 -0600 | [diff] [blame] | 4324 | static const char *specs[][2] = { |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4325 | {"hours", "%02d"}, |
| 4326 | {"minutes", "%02d:%02d"}, |
| 4327 | {"seconds", "%02d:%02d:%02d"}, |
| 4328 | {"milliseconds", "%02d:%02d:%02d.%03d"}, |
| 4329 | {"microseconds", "%02d:%02d:%02d.%06d"}, |
| 4330 | }; |
| 4331 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4332 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4333 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) |
| 4334 | return NULL; |
| 4335 | |
| 4336 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 4337 | if (us == 0) { |
| 4338 | /* seconds */ |
| 4339 | given_spec = 2; |
| 4340 | } |
| 4341 | else { |
| 4342 | /* microseconds */ |
| 4343 | given_spec = 4; |
| 4344 | } |
| 4345 | } |
| 4346 | else { |
| 4347 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 4348 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 4349 | if (given_spec == 3) { |
| 4350 | /* milliseconds */ |
| 4351 | us = us / 1000; |
| 4352 | } |
| 4353 | break; |
| 4354 | } |
| 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 4359 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 4360 | return NULL; |
| 4361 | } |
| 4362 | else { |
| 4363 | result = PyUnicode_FromFormat(specs[given_spec][1], |
| 4364 | TIME_GET_HOUR(self), TIME_GET_MINUTE(self), |
| 4365 | TIME_GET_SECOND(self), us); |
| 4366 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4367 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4368 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | return result; |
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 | /* We need to append the UTC offset. */ |
| 4372 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 4373 | Py_None) < 0) { |
| 4374 | Py_DECREF(result); |
| 4375 | return NULL; |
| 4376 | } |
| 4377 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 4378 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4381 | static PyObject * |
| 4382 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 4383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4384 | PyObject *result; |
| 4385 | PyObject *tuple; |
| 4386 | PyObject *format; |
| 4387 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 4390 | &format)) |
| 4391 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | /* Python's strftime does insane things with the year part of the |
| 4394 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 4395 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | */ |
| 4397 | tuple = Py_BuildValue("iiiiiiiii", |
| 4398 | 1900, 1, 1, /* year, month, day */ |
| 4399 | TIME_GET_HOUR(self), |
| 4400 | TIME_GET_MINUTE(self), |
| 4401 | TIME_GET_SECOND(self), |
| 4402 | 0, 1, -1); /* weekday, daynum, dst */ |
| 4403 | if (tuple == NULL) |
| 4404 | return NULL; |
| 4405 | assert(PyTuple_Size(tuple) == 9); |
| 4406 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 4407 | Py_None); |
| 4408 | Py_DECREF(tuple); |
| 4409 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4410 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4411 | |
| 4412 | /* |
| 4413 | * Miscellaneous methods. |
| 4414 | */ |
| 4415 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4416 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4417 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4418 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4419 | PyObject *result = NULL; |
| 4420 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4421 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4422 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4423 | if (! PyTime_Check(other)) |
| 4424 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4425 | |
| 4426 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4428 | ((PyDateTime_Time *)other)->data, |
| 4429 | _PyDateTime_TIME_DATASIZE); |
| 4430 | return diff_to_bool(diff, op); |
| 4431 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4432 | offset1 = time_utcoffset(self, NULL); |
| 4433 | if (offset1 == NULL) |
| 4434 | return NULL; |
| 4435 | offset2 = time_utcoffset(other, NULL); |
| 4436 | if (offset2 == NULL) |
| 4437 | goto done; |
| 4438 | /* If they're both naive, or both aware and have the same offsets, |
| 4439 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4440 | * offset2 == Py_None at this point. |
| 4441 | */ |
| 4442 | if ((offset1 == offset2) || |
| 4443 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4444 | delta_cmp(offset1, offset2) == 0)) { |
| 4445 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4446 | ((PyDateTime_Time *)other)->data, |
| 4447 | _PyDateTime_TIME_DATASIZE); |
| 4448 | result = diff_to_bool(diff, op); |
| 4449 | } |
| 4450 | /* The hard case: both aware with different UTC offsets */ |
| 4451 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 4452 | int offsecs1, offsecs2; |
| 4453 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 4454 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 4455 | TIME_GET_MINUTE(self) * 60 + |
| 4456 | TIME_GET_SECOND(self) - |
| 4457 | GET_TD_DAYS(offset1) * 86400 - |
| 4458 | GET_TD_SECONDS(offset1); |
| 4459 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 4460 | TIME_GET_MINUTE(other) * 60 + |
| 4461 | TIME_GET_SECOND(other) - |
| 4462 | GET_TD_DAYS(offset2) * 86400 - |
| 4463 | GET_TD_SECONDS(offset2); |
| 4464 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | if (diff == 0) |
| 4466 | diff = TIME_GET_MICROSECOND(self) - |
| 4467 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4468 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4469 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4470 | else if (op == Py_EQ) { |
| 4471 | result = Py_False; |
| 4472 | Py_INCREF(result); |
| 4473 | } |
| 4474 | else if (op == Py_NE) { |
| 4475 | result = Py_True; |
| 4476 | Py_INCREF(result); |
| 4477 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4478 | else { |
| 4479 | PyErr_SetString(PyExc_TypeError, |
| 4480 | "can't compare offset-naive and " |
| 4481 | "offset-aware times"); |
| 4482 | } |
| 4483 | done: |
| 4484 | Py_DECREF(offset1); |
| 4485 | Py_XDECREF(offset2); |
| 4486 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4489 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4490 | time_hash(PyDateTime_Time *self) |
| 4491 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4492 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4493 | PyObject *offset, *self0; |
Victor Stinner | 423c16b | 2017-01-03 23:47:12 +0100 | [diff] [blame] | 4494 | if (TIME_GET_FOLD(self)) { |
| 4495 | self0 = new_time_ex2(TIME_GET_HOUR(self), |
| 4496 | TIME_GET_MINUTE(self), |
| 4497 | TIME_GET_SECOND(self), |
| 4498 | TIME_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4499 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4500 | 0, Py_TYPE(self)); |
| 4501 | if (self0 == NULL) |
| 4502 | return -1; |
| 4503 | } |
| 4504 | else { |
| 4505 | self0 = (PyObject *)self; |
| 4506 | Py_INCREF(self0); |
| 4507 | } |
| 4508 | offset = time_utcoffset(self0, NULL); |
| 4509 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4510 | |
| 4511 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4515 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | self->hashcode = generic_hash( |
| 4517 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4519 | PyObject *temp1, *temp2; |
| 4520 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4521 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4522 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 4523 | TIME_GET_MINUTE(self) * 60 + |
| 4524 | TIME_GET_SECOND(self); |
| 4525 | microseconds = TIME_GET_MICROSECOND(self); |
| 4526 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 4527 | if (temp1 == NULL) { |
| 4528 | Py_DECREF(offset); |
| 4529 | return -1; |
| 4530 | } |
| 4531 | temp2 = delta_subtract(temp1, offset); |
| 4532 | Py_DECREF(temp1); |
| 4533 | if (temp2 == NULL) { |
| 4534 | Py_DECREF(offset); |
| 4535 | return -1; |
| 4536 | } |
| 4537 | self->hashcode = PyObject_Hash(temp2); |
| 4538 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4539 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4540 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | } |
| 4542 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4543 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4544 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4545 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4546 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4548 | PyObject *clone; |
| 4549 | PyObject *tuple; |
| 4550 | int hh = TIME_GET_HOUR(self); |
| 4551 | int mm = TIME_GET_MINUTE(self); |
| 4552 | int ss = TIME_GET_SECOND(self); |
| 4553 | int us = TIME_GET_MICROSECOND(self); |
| 4554 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4555 | int fold = TIME_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4556 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4557 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4558 | time_kws, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4559 | &hh, &mm, &ss, &us, &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4560 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 4561 | if (fold != 0 && fold != 1) { |
| 4562 | PyErr_SetString(PyExc_ValueError, |
| 4563 | "fold must be either 0 or 1"); |
| 4564 | return NULL; |
| 4565 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 4567 | if (tuple == NULL) |
| 4568 | return NULL; |
| 4569 | clone = time_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4570 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4571 | TIME_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4572 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4573 | Py_DECREF(tuple); |
| 4574 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4577 | static PyObject * |
| 4578 | time_fromisoformat(PyObject *cls, PyObject *tstr) { |
| 4579 | assert(tstr != NULL); |
| 4580 | |
| 4581 | if (!PyUnicode_Check(tstr)) { |
| 4582 | PyErr_SetString(PyExc_TypeError, "fromisoformat: argument must be str"); |
| 4583 | return NULL; |
| 4584 | } |
| 4585 | |
| 4586 | Py_ssize_t len; |
| 4587 | const char *p = PyUnicode_AsUTF8AndSize(tstr, &len); |
| 4588 | |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4589 | if (p == NULL) { |
| 4590 | goto invalid_string_error; |
| 4591 | } |
| 4592 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4593 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 4594 | int tzoffset, tzimicrosecond = 0; |
| 4595 | int rv = parse_isoformat_time(p, len, |
| 4596 | &hour, &minute, &second, µsecond, |
| 4597 | &tzoffset, &tzimicrosecond); |
| 4598 | |
| 4599 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4600 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, |
| 4604 | tzimicrosecond); |
| 4605 | |
| 4606 | if (tzinfo == NULL) { |
| 4607 | return NULL; |
| 4608 | } |
| 4609 | |
| 4610 | PyObject *t; |
| 4611 | if ( (PyTypeObject *)cls == &PyDateTime_TimeType ) { |
| 4612 | t = new_time(hour, minute, second, microsecond, tzinfo, 0); |
| 4613 | } else { |
| 4614 | t = PyObject_CallFunction(cls, "iiiiO", |
| 4615 | hour, minute, second, microsecond, tzinfo); |
| 4616 | } |
| 4617 | |
| 4618 | Py_DECREF(tzinfo); |
| 4619 | return t; |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4620 | |
| 4621 | invalid_string_error: |
| 4622 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", tstr); |
| 4623 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4627 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4628 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4629 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4630 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4631 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4632 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4633 | */ |
| 4634 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4635 | time_getstate(PyDateTime_Time *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4636 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | PyObject *basestate; |
| 4638 | PyObject *result = NULL; |
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 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 4641 | _PyDateTime_TIME_DATASIZE); |
| 4642 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4643 | if (proto > 3 && TIME_GET_FOLD(self)) |
| 4644 | /* Set the first bit of the first byte */ |
| 4645 | PyBytes_AS_STRING(basestate)[0] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 4647 | result = PyTuple_Pack(1, basestate); |
| 4648 | else |
| 4649 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 4650 | Py_DECREF(basestate); |
| 4651 | } |
| 4652 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4656 | time_reduce_ex(PyDateTime_Time *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4657 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4658 | int proto; |
| 4659 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4660 | return NULL; |
| 4661 | |
| 4662 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4665 | static PyObject * |
| 4666 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
| 4667 | { |
| 4668 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); |
| 4669 | } |
| 4670 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4671 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4672 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4673 | {"isoformat", (PyCFunction)(void(*)(void))time_isoformat, METH_VARARGS | METH_KEYWORDS, |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4674 | PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" |
| 4675 | "[+HH:MM].\n\n" |
| 4676 | "timespec specifies what components of the time to include.\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4677 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4678 | {"strftime", (PyCFunction)(void(*)(void))time_strftime, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 4682 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 4683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4684 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 4685 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4687 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 4688 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 4691 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4692 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4693 | {"replace", (PyCFunction)(void(*)(void))time_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4694 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4695 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4696 | {"fromisoformat", (PyCFunction)time_fromisoformat, METH_O | METH_CLASS, |
| 4697 | PyDoc_STR("string -> time from time.isoformat() output")}, |
| 4698 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4699 | {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4700 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4701 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4702 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 4703 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 4704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4705 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4706 | }; |
| 4707 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4708 | static const char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4709 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 4710 | \n\ |
| 4711 | 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] | 4712 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4713 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4714 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4715 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4716 | "datetime.time", /* tp_name */ |
| 4717 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 4718 | 0, /* tp_itemsize */ |
| 4719 | (destructor)time_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4720 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | 0, /* tp_getattr */ |
| 4722 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4723 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4724 | (reprfunc)time_repr, /* tp_repr */ |
Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 4725 | 0, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4726 | 0, /* tp_as_sequence */ |
| 4727 | 0, /* tp_as_mapping */ |
| 4728 | (hashfunc)time_hash, /* tp_hash */ |
| 4729 | 0, /* tp_call */ |
| 4730 | (reprfunc)time_str, /* tp_str */ |
| 4731 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4732 | 0, /* tp_setattro */ |
| 4733 | 0, /* tp_as_buffer */ |
| 4734 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4735 | time_doc, /* tp_doc */ |
| 4736 | 0, /* tp_traverse */ |
| 4737 | 0, /* tp_clear */ |
| 4738 | time_richcompare, /* tp_richcompare */ |
| 4739 | 0, /* tp_weaklistoffset */ |
| 4740 | 0, /* tp_iter */ |
| 4741 | 0, /* tp_iternext */ |
| 4742 | time_methods, /* tp_methods */ |
| 4743 | 0, /* tp_members */ |
| 4744 | time_getset, /* tp_getset */ |
| 4745 | 0, /* tp_base */ |
| 4746 | 0, /* tp_dict */ |
| 4747 | 0, /* tp_descr_get */ |
| 4748 | 0, /* tp_descr_set */ |
| 4749 | 0, /* tp_dictoffset */ |
| 4750 | 0, /* tp_init */ |
| 4751 | time_alloc, /* tp_alloc */ |
| 4752 | time_new, /* tp_new */ |
| 4753 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4754 | }; |
| 4755 | |
| 4756 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4757 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4758 | */ |
| 4759 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4760 | /* Accessor properties. Properties for day, month, and year are inherited |
| 4761 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4762 | */ |
| 4763 | |
| 4764 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4765 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4770 | static PyObject * |
| 4771 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 4772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4773 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | static PyObject * |
| 4777 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 4778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | static PyObject * |
| 4783 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 4784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | static PyObject * |
| 4789 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 4790 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4791 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 4792 | Py_INCREF(result); |
| 4793 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4796 | static PyObject * |
| 4797 | datetime_fold(PyDateTime_DateTime *self, void *unused) |
| 4798 | { |
| 4799 | return PyLong_FromLong(DATE_GET_FOLD(self)); |
| 4800 | } |
| 4801 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4802 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4803 | {"hour", (getter)datetime_hour}, |
| 4804 | {"minute", (getter)datetime_minute}, |
| 4805 | {"second", (getter)datetime_second}, |
| 4806 | {"microsecond", (getter)datetime_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4807 | {"tzinfo", (getter)datetime_tzinfo}, |
| 4808 | {"fold", (getter)datetime_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4810 | }; |
| 4811 | |
| 4812 | /* |
| 4813 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4814 | */ |
| 4815 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4816 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4817 | "year", "month", "day", "hour", "minute", "second", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4818 | "microsecond", "tzinfo", "fold", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4819 | }; |
| 4820 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4821 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4822 | datetime_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo) |
| 4823 | { |
| 4824 | PyDateTime_DateTime *me; |
| 4825 | char aware = (char)(tzinfo != Py_None); |
| 4826 | |
| 4827 | if (aware && check_tzinfo_subclass(tzinfo) < 0) { |
| 4828 | PyErr_SetString(PyExc_TypeError, "bad tzinfo state arg"); |
| 4829 | return NULL; |
| 4830 | } |
| 4831 | |
| 4832 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4833 | if (me != NULL) { |
| 4834 | const char *pdata = PyBytes_AS_STRING(state); |
| 4835 | |
| 4836 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4837 | me->hashcode = -1; |
| 4838 | me->hastzinfo = aware; |
| 4839 | if (aware) { |
| 4840 | Py_INCREF(tzinfo); |
| 4841 | me->tzinfo = tzinfo; |
| 4842 | } |
| 4843 | if (pdata[2] & (1 << 7)) { |
| 4844 | me->data[2] -= 128; |
| 4845 | me->fold = 1; |
| 4846 | } |
| 4847 | else { |
| 4848 | me->fold = 0; |
| 4849 | } |
| 4850 | } |
| 4851 | return (PyObject *)me; |
| 4852 | } |
| 4853 | |
| 4854 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4855 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4858 | int year; |
| 4859 | int month; |
| 4860 | int day; |
| 4861 | int hour = 0; |
| 4862 | int minute = 0; |
| 4863 | int second = 0; |
| 4864 | int usecond = 0; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4865 | int fold = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4866 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4869 | if (PyTuple_GET_SIZE(args) >= 1 && PyTuple_GET_SIZE(args) <= 2) { |
| 4870 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 4871 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4872 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4873 | } |
| 4874 | if (PyBytes_Check(state)) { |
| 4875 | if (PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4876 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) |
| 4877 | { |
| 4878 | return datetime_from_pickle(type, state, tzinfo); |
| 4879 | } |
| 4880 | } |
| 4881 | else if (PyUnicode_Check(state)) { |
| 4882 | if (PyUnicode_READY(state)) { |
| 4883 | return NULL; |
| 4884 | } |
| 4885 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4886 | MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2) & 0x7F)) |
| 4887 | { |
| 4888 | state = PyUnicode_AsLatin1String(state); |
| 4889 | if (state == NULL) { |
| 4890 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4891 | /* More informative error message. */ |
| 4892 | PyErr_SetString(PyExc_ValueError, |
| 4893 | "Failed to encode latin1 string when unpickling " |
| 4894 | "a datetime object. " |
| 4895 | "pickle.load(data, encoding='latin1') is assumed."); |
| 4896 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 4897 | return NULL; |
| 4898 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4899 | self = datetime_from_pickle(type, state, tzinfo); |
| 4900 | Py_DECREF(state); |
| 4901 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4902 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4903 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4904 | tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4905 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4906 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4907 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | &year, &month, &day, &hour, &minute, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4909 | &second, &usecond, &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4910 | self = new_datetime_ex2(year, month, day, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4911 | hour, minute, second, usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4912 | tzinfo, fold, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | } |
| 4914 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4917 | /* TM_FUNC is the shared type of _PyTime_localtime() and |
| 4918 | * _PyTime_gmtime(). */ |
| 4919 | typedef int (*TM_FUNC)(time_t timer, struct tm*); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4920 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4921 | /* As of version 2015f max fold in IANA database is |
| 4922 | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4923 | static long long max_fold_seconds = 24 * 3600; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4924 | /* NB: date(1970,1,1).toordinal() == 719163 */ |
Benjamin Peterson | ac965ca | 2016-09-18 18:12:21 -0700 | [diff] [blame] | 4925 | static long long epoch = 719163LL * 24 * 60 * 60; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4926 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4927 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4928 | utc_to_seconds(int year, int month, int day, |
| 4929 | int hour, int minute, int second) |
| 4930 | { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4931 | long long ordinal; |
| 4932 | |
| 4933 | /* ymd_to_ord() doesn't support year <= 0 */ |
| 4934 | if (year < MINYEAR || year > MAXYEAR) { |
| 4935 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
| 4936 | return -1; |
| 4937 | } |
| 4938 | |
| 4939 | ordinal = ymd_to_ord(year, month, day); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4940 | return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; |
| 4941 | } |
| 4942 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4943 | static long long |
| 4944 | local(long long u) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4945 | { |
| 4946 | struct tm local_time; |
Alexander Belopolsky | 8e1d3a2 | 2016-07-25 13:54:51 -0400 | [diff] [blame] | 4947 | time_t t; |
| 4948 | u -= epoch; |
| 4949 | t = u; |
| 4950 | if (t != u) { |
| 4951 | PyErr_SetString(PyExc_OverflowError, |
| 4952 | "timestamp out of range for platform time_t"); |
| 4953 | return -1; |
| 4954 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4955 | if (_PyTime_localtime(t, &local_time) != 0) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4956 | return -1; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4957 | return utc_to_seconds(local_time.tm_year + 1900, |
| 4958 | local_time.tm_mon + 1, |
| 4959 | local_time.tm_mday, |
| 4960 | local_time.tm_hour, |
| 4961 | local_time.tm_min, |
| 4962 | local_time.tm_sec); |
| 4963 | } |
| 4964 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4965 | /* Internal helper. |
| 4966 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4967 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4968 | */ |
| 4969 | static PyObject * |
| 4970 | 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] | 4971 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4972 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4973 | struct tm tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4974 | int year, month, day, hour, minute, second, fold = 0; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4975 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4976 | if (f(timet, &tm) != 0) |
| 4977 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4978 | |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4979 | year = tm.tm_year + 1900; |
| 4980 | month = tm.tm_mon + 1; |
| 4981 | day = tm.tm_mday; |
| 4982 | hour = tm.tm_hour; |
| 4983 | minute = tm.tm_min; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4984 | /* The platform localtime/gmtime may insert leap seconds, |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4985 | * indicated by tm.tm_sec > 59. We don't care about them, |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4986 | * except to the extent that passing them on to the datetime |
| 4987 | * constructor would raise ValueError for a reason that |
| 4988 | * made no sense to the user. |
| 4989 | */ |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4990 | second = Py_MIN(59, tm.tm_sec); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4991 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4992 | /* local timezone requires to compute fold */ |
Ammar Askar | 96d1e69 | 2018-07-25 09:54:58 -0700 | [diff] [blame] | 4993 | if (tzinfo == Py_None && f == _PyTime_localtime |
| 4994 | /* On Windows, passing a negative value to local results |
| 4995 | * in an OSError because localtime_s on Windows does |
| 4996 | * not support negative timestamps. Unfortunately this |
| 4997 | * means that fold detection for time values between |
| 4998 | * 0 and max_fold_seconds will result in an identical |
| 4999 | * error since we subtract max_fold_seconds to detect a |
| 5000 | * fold. However, since we know there haven't been any |
| 5001 | * folds in the interval [0, max_fold_seconds) in any |
| 5002 | * timezone, we can hackily just forego fold detection |
| 5003 | * for this time range. |
| 5004 | */ |
| 5005 | #ifdef MS_WINDOWS |
| 5006 | && (timet - max_fold_seconds > 0) |
| 5007 | #endif |
| 5008 | ) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5009 | long long probe_seconds, result_seconds, transition; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5010 | |
| 5011 | result_seconds = utc_to_seconds(year, month, day, |
| 5012 | hour, minute, second); |
| 5013 | /* Probe max_fold_seconds to detect a fold. */ |
| 5014 | probe_seconds = local(epoch + timet - max_fold_seconds); |
| 5015 | if (probe_seconds == -1) |
| 5016 | return NULL; |
| 5017 | transition = result_seconds - probe_seconds - max_fold_seconds; |
| 5018 | if (transition < 0) { |
| 5019 | probe_seconds = local(epoch + timet + transition); |
| 5020 | if (probe_seconds == -1) |
| 5021 | return NULL; |
| 5022 | if (probe_seconds == result_seconds) |
| 5023 | fold = 1; |
| 5024 | } |
| 5025 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 5026 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 5027 | second, us, tzinfo, fold, cls); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | /* Internal helper. |
| 5031 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 5032 | * to control the interpretation of the timestamp. Since a double doesn't |
| 5033 | * have enough bits to cover a datetime's full range of precision, it's |
| 5034 | * better to call datetime_from_timet_and_us provided you have a way |
| 5035 | * to get that much precision (e.g., C time() isn't good enough). |
| 5036 | */ |
| 5037 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5038 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5040 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5042 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5043 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 5044 | if (_PyTime_ObjectToTimeval(timestamp, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 5045 | &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5046 | return NULL; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 5047 | |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 5048 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | /* Internal helper. |
| 5052 | * Build most accurate possible datetime for current time. Pass localtime or |
| 5053 | * gmtime for f as appropriate. |
| 5054 | */ |
| 5055 | static PyObject * |
| 5056 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 5057 | { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 5058 | _PyTime_t ts = _PyTime_GetSystemClock(); |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 5059 | time_t secs; |
| 5060 | int us; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 5061 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 5062 | if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 5063 | return NULL; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 5064 | assert(0 <= us && us <= 999999); |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 5065 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 5066 | return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5069 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5070 | |
| 5071 | @classmethod |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 5072 | datetime.datetime.now |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5073 | |
| 5074 | tz: object = None |
| 5075 | Timezone object. |
| 5076 | |
| 5077 | Returns new datetime object representing current time local to tz. |
| 5078 | |
| 5079 | If no tz is specified, uses local timezone. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5080 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5081 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5082 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 5083 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 5084 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5085 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5086 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5087 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5088 | /* Return best possible local time -- this isn't constrained by the |
| 5089 | * precision of a timestamp. |
| 5090 | */ |
| 5091 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 5093 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 5094 | self = datetime_best_possible((PyObject *)type, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5095 | tz == Py_None ? _PyTime_localtime : |
| 5096 | _PyTime_gmtime, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5097 | tz); |
| 5098 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5099 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 5100 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | } |
| 5102 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5105 | /* Return best possible UTC time -- this isn't constrained by the |
| 5106 | * precision of a timestamp. |
| 5107 | */ |
| 5108 | static PyObject * |
| 5109 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 5110 | { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5111 | return datetime_best_possible(cls, _PyTime_gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5114 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 5115 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5116 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5118 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5119 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5120 | PyObject *tzinfo = Py_None; |
| 5121 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5122 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5123 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5124 | keywords, ×tamp, &tzinfo)) |
| 5125 | return NULL; |
| 5126 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 5127 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 5128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5129 | self = datetime_from_timestamp(cls, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5130 | tzinfo == Py_None ? _PyTime_localtime : |
| 5131 | _PyTime_gmtime, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5132 | timestamp, |
| 5133 | tzinfo); |
| 5134 | if (self != NULL && tzinfo != Py_None) { |
| 5135 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 5136 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | } |
| 5138 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5141 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 5142 | static PyObject * |
| 5143 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 5144 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5145 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5146 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5147 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5148 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5149 | result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5150 | Py_None); |
| 5151 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5154 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5155 | static PyObject * |
| 5156 | datetime_strptime(PyObject *cls, PyObject *args) |
| 5157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5158 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5159 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5160 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5162 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5163 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5164 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5165 | if (module == NULL) { |
| 5166 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 5167 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5168 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5169 | } |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 5170 | return _PyObject_CallMethodIdObjArgs(module, &PyId__strptime_datetime, |
| 5171 | cls, string, format, NULL); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5174 | /* Return new datetime from date/datetime and time arguments. */ |
| 5175 | static PyObject * |
| 5176 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 5177 | { |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5178 | static char *keywords[] = {"date", "time", "tzinfo", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | PyObject *date; |
| 5180 | PyObject *time; |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5181 | PyObject *tzinfo = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5182 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5183 | |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5184 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5185 | &PyDateTime_DateType, &date, |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5186 | &PyDateTime_TimeType, &time, &tzinfo)) { |
| 5187 | if (tzinfo == NULL) { |
| 5188 | if (HASTZINFO(time)) |
| 5189 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 5190 | else |
| 5191 | tzinfo = Py_None; |
| 5192 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 5193 | result = new_datetime_subclass_fold_ex(GET_YEAR(date), |
| 5194 | GET_MONTH(date), |
| 5195 | GET_DAY(date), |
| 5196 | TIME_GET_HOUR(time), |
| 5197 | TIME_GET_MINUTE(time), |
| 5198 | TIME_GET_SECOND(time), |
| 5199 | TIME_GET_MICROSECOND(time), |
| 5200 | tzinfo, |
| 5201 | TIME_GET_FOLD(time), |
| 5202 | cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | } |
| 5204 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5205 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5206 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5207 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5208 | _sanitize_isoformat_str(PyObject *dtstr) |
| 5209 | { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5210 | // `fromisoformat` allows surrogate characters in exactly one position, |
| 5211 | // the separator; to allow datetime_fromisoformat to make the simplifying |
| 5212 | // assumption that all valid strings can be encoded in UTF-8, this function |
| 5213 | // replaces any surrogate character separators with `T`. |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5214 | // |
| 5215 | // The result of this, if not NULL, returns a new reference |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5216 | Py_ssize_t len = PyUnicode_GetLength(dtstr); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5217 | if (len < 0) { |
| 5218 | return NULL; |
| 5219 | } |
| 5220 | |
| 5221 | if (len <= 10 || |
| 5222 | !Py_UNICODE_IS_SURROGATE(PyUnicode_READ_CHAR(dtstr, 10))) { |
| 5223 | Py_INCREF(dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5224 | return dtstr; |
| 5225 | } |
| 5226 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5227 | PyObject *str_out = _PyUnicode_Copy(dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5228 | if (str_out == NULL) { |
| 5229 | return NULL; |
| 5230 | } |
| 5231 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5232 | if (PyUnicode_WriteChar(str_out, 10, (Py_UCS4)'T')) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5233 | Py_DECREF(str_out); |
| 5234 | return NULL; |
| 5235 | } |
| 5236 | |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5237 | return str_out; |
| 5238 | } |
| 5239 | |
| 5240 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5241 | datetime_fromisoformat(PyObject *cls, PyObject *dtstr) |
| 5242 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5243 | assert(dtstr != NULL); |
| 5244 | |
| 5245 | if (!PyUnicode_Check(dtstr)) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5246 | PyErr_SetString(PyExc_TypeError, |
| 5247 | "fromisoformat: argument must be str"); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5248 | return NULL; |
| 5249 | } |
| 5250 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5251 | PyObject *dtstr_clean = _sanitize_isoformat_str(dtstr); |
| 5252 | if (dtstr_clean == NULL) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5253 | goto error; |
| 5254 | } |
| 5255 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5256 | Py_ssize_t len; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5257 | const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr_clean, &len); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5258 | |
| 5259 | if (dt_ptr == NULL) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5260 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 5261 | // Encoding errors are invalid string errors at this point |
| 5262 | goto invalid_string_error; |
| 5263 | } |
| 5264 | else { |
| 5265 | goto error; |
| 5266 | } |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5267 | } |
| 5268 | |
| 5269 | const char *p = dt_ptr; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5270 | |
| 5271 | int year = 0, month = 0, day = 0; |
| 5272 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 5273 | int tzoffset = 0, tzusec = 0; |
| 5274 | |
| 5275 | // date has a fixed length of 10 |
| 5276 | int rv = parse_isoformat_date(p, &year, &month, &day); |
| 5277 | |
| 5278 | if (!rv && len > 10) { |
| 5279 | // In UTF-8, the length of multi-byte characters is encoded in the MSB |
| 5280 | if ((p[10] & 0x80) == 0) { |
| 5281 | p += 11; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5282 | } |
| 5283 | else { |
| 5284 | switch (p[10] & 0xf0) { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5285 | case 0xe0: |
| 5286 | p += 13; |
| 5287 | break; |
| 5288 | case 0xf0: |
| 5289 | p += 14; |
| 5290 | break; |
| 5291 | default: |
| 5292 | p += 12; |
| 5293 | break; |
| 5294 | } |
| 5295 | } |
| 5296 | |
| 5297 | len -= (p - dt_ptr); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5298 | rv = parse_isoformat_time(p, len, &hour, &minute, &second, |
| 5299 | µsecond, &tzoffset, &tzusec); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5300 | } |
| 5301 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5302 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5303 | } |
| 5304 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5305 | PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, tzusec); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5306 | if (tzinfo == NULL) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5307 | goto error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5308 | } |
| 5309 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 5310 | PyObject *dt = new_datetime_subclass_ex(year, month, day, hour, minute, |
| 5311 | second, microsecond, tzinfo, cls); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5312 | |
| 5313 | Py_DECREF(tzinfo); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5314 | Py_DECREF(dtstr_clean); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5315 | return dt; |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5316 | |
| 5317 | invalid_string_error: |
| 5318 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", dtstr); |
| 5319 | |
| 5320 | error: |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5321 | Py_XDECREF(dtstr_clean); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5322 | |
| 5323 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5324 | } |
| 5325 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5326 | /* |
| 5327 | * Destructor. |
| 5328 | */ |
| 5329 | |
| 5330 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5331 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5333 | if (HASTZINFO(self)) { |
| 5334 | Py_XDECREF(self->tzinfo); |
| 5335 | } |
| 5336 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | /* |
| 5340 | * Indirect access to tzinfo methods. |
| 5341 | */ |
| 5342 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5343 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 5344 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5345 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 5346 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5350 | datetime_dst(PyObject *self, PyObject *unused) { |
| 5351 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5355 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 5356 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5360 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5361 | */ |
| 5362 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5363 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 5364 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5365 | */ |
| 5366 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5367 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5368 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5370 | /* Note that the C-level additions can't overflow, because of |
| 5371 | * invariant bounds on the member values. |
| 5372 | */ |
| 5373 | int year = GET_YEAR(date); |
| 5374 | int month = GET_MONTH(date); |
| 5375 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 5376 | int hour = DATE_GET_HOUR(date); |
| 5377 | int minute = DATE_GET_MINUTE(date); |
| 5378 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 5379 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 5380 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5382 | assert(factor == 1 || factor == -1); |
| 5383 | if (normalize_datetime(&year, &month, &day, |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 5384 | &hour, &minute, &second, µsecond) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5385 | return NULL; |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 5386 | } |
| 5387 | |
Paul Ganssle | 89427cd | 2019-02-04 14:42:04 -0500 | [diff] [blame] | 5388 | return new_datetime_subclass_ex(year, month, day, |
| 5389 | hour, minute, second, microsecond, |
| 5390 | HASTZINFO(date) ? date->tzinfo : Py_None, |
| 5391 | (PyObject *)Py_TYPE(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5395 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5396 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5397 | if (PyDateTime_Check(left)) { |
| 5398 | /* datetime + ??? */ |
| 5399 | if (PyDelta_Check(right)) |
| 5400 | /* datetime + delta */ |
| 5401 | return add_datetime_timedelta( |
| 5402 | (PyDateTime_DateTime *)left, |
| 5403 | (PyDateTime_Delta *)right, |
| 5404 | 1); |
| 5405 | } |
| 5406 | else if (PyDelta_Check(left)) { |
| 5407 | /* delta + datetime */ |
| 5408 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 5409 | (PyDateTime_Delta *) left, |
| 5410 | 1); |
| 5411 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5412 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5413 | } |
| 5414 | |
| 5415 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5416 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5418 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | if (PyDateTime_Check(left)) { |
| 5421 | /* datetime - ??? */ |
| 5422 | if (PyDateTime_Check(right)) { |
| 5423 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5424 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5426 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5427 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 5428 | offset2 = offset1 = Py_None; |
| 5429 | Py_INCREF(offset1); |
| 5430 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5431 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5432 | else { |
| 5433 | offset1 = datetime_utcoffset(left, NULL); |
| 5434 | if (offset1 == NULL) |
| 5435 | return NULL; |
| 5436 | offset2 = datetime_utcoffset(right, NULL); |
| 5437 | if (offset2 == NULL) { |
| 5438 | Py_DECREF(offset1); |
| 5439 | return NULL; |
| 5440 | } |
| 5441 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 5442 | PyErr_SetString(PyExc_TypeError, |
| 5443 | "can't subtract offset-naive and " |
| 5444 | "offset-aware datetimes"); |
| 5445 | Py_DECREF(offset1); |
| 5446 | Py_DECREF(offset2); |
| 5447 | return NULL; |
| 5448 | } |
| 5449 | } |
| 5450 | if ((offset1 != offset2) && |
| 5451 | delta_cmp(offset1, offset2) != 0) { |
| 5452 | offdiff = delta_subtract(offset1, offset2); |
| 5453 | if (offdiff == NULL) { |
| 5454 | Py_DECREF(offset1); |
| 5455 | Py_DECREF(offset2); |
| 5456 | return NULL; |
| 5457 | } |
| 5458 | } |
| 5459 | Py_DECREF(offset1); |
| 5460 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5461 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 5462 | GET_MONTH(left), |
| 5463 | GET_DAY(left)) - |
| 5464 | ymd_to_ord(GET_YEAR(right), |
| 5465 | GET_MONTH(right), |
| 5466 | GET_DAY(right)); |
| 5467 | /* These can't overflow, since the values are |
| 5468 | * normalized. At most this gives the number of |
| 5469 | * seconds in one day. |
| 5470 | */ |
| 5471 | delta_s = (DATE_GET_HOUR(left) - |
| 5472 | DATE_GET_HOUR(right)) * 3600 + |
| 5473 | (DATE_GET_MINUTE(left) - |
| 5474 | DATE_GET_MINUTE(right)) * 60 + |
| 5475 | (DATE_GET_SECOND(left) - |
| 5476 | DATE_GET_SECOND(right)); |
| 5477 | delta_us = DATE_GET_MICROSECOND(left) - |
| 5478 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 5480 | if (result == NULL) |
| 5481 | return NULL; |
| 5482 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5483 | if (offdiff != NULL) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 5484 | Py_SETREF(result, delta_subtract(result, offdiff)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5485 | Py_DECREF(offdiff); |
| 5486 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | } |
| 5488 | else if (PyDelta_Check(right)) { |
| 5489 | /* datetime - delta */ |
| 5490 | result = add_datetime_timedelta( |
| 5491 | (PyDateTime_DateTime *)left, |
| 5492 | (PyDateTime_Delta *)right, |
| 5493 | -1); |
| 5494 | } |
| 5495 | } |
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 | if (result == Py_NotImplemented) |
| 5498 | Py_INCREF(result); |
| 5499 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | /* Various ways to turn a datetime into a string. */ |
| 5503 | |
| 5504 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5505 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5507 | const char *type_name = Py_TYPE(self)->tp_name; |
| 5508 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5510 | if (DATE_GET_MICROSECOND(self)) { |
| 5511 | baserepr = PyUnicode_FromFormat( |
| 5512 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 5513 | type_name, |
| 5514 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5515 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5516 | DATE_GET_SECOND(self), |
| 5517 | DATE_GET_MICROSECOND(self)); |
| 5518 | } |
| 5519 | else if (DATE_GET_SECOND(self)) { |
| 5520 | baserepr = PyUnicode_FromFormat( |
| 5521 | "%s(%d, %d, %d, %d, %d, %d)", |
| 5522 | type_name, |
| 5523 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5524 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5525 | DATE_GET_SECOND(self)); |
| 5526 | } |
| 5527 | else { |
| 5528 | baserepr = PyUnicode_FromFormat( |
| 5529 | "%s(%d, %d, %d, %d, %d)", |
| 5530 | type_name, |
| 5531 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5532 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 5533 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5534 | if (baserepr != NULL && DATE_GET_FOLD(self) != 0) |
| 5535 | baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 5537 | return baserepr; |
| 5538 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5541 | static PyObject * |
| 5542 | datetime_str(PyDateTime_DateTime *self) |
| 5543 | { |
Victor Stinner | 4c38154 | 2016-12-09 00:33:39 +0100 | [diff] [blame] | 5544 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5545 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5546 | |
| 5547 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5548 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | int sep = 'T'; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5551 | char *timespec = NULL; |
| 5552 | static char *keywords[] = {"sep", "timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5553 | char buffer[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5554 | PyObject *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | int us = DATE_GET_MICROSECOND(self); |
Andy Lester | c3fa634 | 2020-02-24 00:40:43 -0600 | [diff] [blame] | 5556 | static const char *specs[][2] = { |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5557 | {"hours", "%04d-%02d-%02d%c%02d"}, |
| 5558 | {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, |
| 5559 | {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, |
| 5560 | {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, |
| 5561 | {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, |
| 5562 | }; |
| 5563 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5564 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5565 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | return NULL; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5567 | |
| 5568 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 5569 | if (us == 0) { |
| 5570 | /* seconds */ |
| 5571 | given_spec = 2; |
| 5572 | } |
| 5573 | else { |
| 5574 | /* microseconds */ |
| 5575 | given_spec = 4; |
| 5576 | } |
| 5577 | } |
| 5578 | else { |
| 5579 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 5580 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 5581 | if (given_spec == 3) { |
| 5582 | us = us / 1000; |
| 5583 | } |
| 5584 | break; |
| 5585 | } |
| 5586 | } |
| 5587 | } |
| 5588 | |
| 5589 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 5590 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 5591 | return NULL; |
| 5592 | } |
| 5593 | else { |
| 5594 | result = PyUnicode_FromFormat(specs[given_spec][1], |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5595 | GET_YEAR(self), GET_MONTH(self), |
| 5596 | GET_DAY(self), (int)sep, |
| 5597 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5598 | DATE_GET_SECOND(self), us); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5599 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 5600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5601 | if (!result || !HASTZINFO(self)) |
| 5602 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5604 | /* We need to append the UTC offset. */ |
| 5605 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 5606 | (PyObject *)self) < 0) { |
| 5607 | Py_DECREF(result); |
| 5608 | return NULL; |
| 5609 | } |
| 5610 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 5611 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5614 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5615 | datetime_ctime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5617 | return format_ctime((PyDateTime_Date *)self, |
| 5618 | DATE_GET_HOUR(self), |
| 5619 | DATE_GET_MINUTE(self), |
| 5620 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5623 | /* Miscellaneous methods. */ |
| 5624 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5625 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5626 | flip_fold(PyObject *dt) |
| 5627 | { |
| 5628 | return new_datetime_ex2(GET_YEAR(dt), |
| 5629 | GET_MONTH(dt), |
| 5630 | GET_DAY(dt), |
| 5631 | DATE_GET_HOUR(dt), |
| 5632 | DATE_GET_MINUTE(dt), |
| 5633 | DATE_GET_SECOND(dt), |
| 5634 | DATE_GET_MICROSECOND(dt), |
| 5635 | HASTZINFO(dt) ? |
| 5636 | ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, |
| 5637 | !DATE_GET_FOLD(dt), |
| 5638 | Py_TYPE(dt)); |
| 5639 | } |
| 5640 | |
| 5641 | static PyObject * |
| 5642 | get_flip_fold_offset(PyObject *dt) |
| 5643 | { |
| 5644 | PyObject *result, *flip_dt; |
| 5645 | |
| 5646 | flip_dt = flip_fold(dt); |
| 5647 | if (flip_dt == NULL) |
| 5648 | return NULL; |
| 5649 | result = datetime_utcoffset(flip_dt, NULL); |
| 5650 | Py_DECREF(flip_dt); |
| 5651 | return result; |
| 5652 | } |
| 5653 | |
| 5654 | /* PEP 495 exception: Whenever one or both of the operands in |
| 5655 | * inter-zone comparison is such that its utcoffset() depends |
Serhiy Storchaka | bac2d5b | 2018-03-28 22:14:26 +0300 | [diff] [blame] | 5656 | * on the value of its fold attribute, the result is False. |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5657 | * |
| 5658 | * Return 1 if exception applies, 0 if not, and -1 on error. |
| 5659 | */ |
| 5660 | static int |
| 5661 | pep495_eq_exception(PyObject *self, PyObject *other, |
| 5662 | PyObject *offset_self, PyObject *offset_other) |
| 5663 | { |
| 5664 | int result = 0; |
| 5665 | PyObject *flip_offset; |
| 5666 | |
| 5667 | flip_offset = get_flip_fold_offset(self); |
| 5668 | if (flip_offset == NULL) |
| 5669 | return -1; |
| 5670 | if (flip_offset != offset_self && |
| 5671 | delta_cmp(flip_offset, offset_self)) |
| 5672 | { |
| 5673 | result = 1; |
| 5674 | goto done; |
| 5675 | } |
| 5676 | Py_DECREF(flip_offset); |
| 5677 | |
| 5678 | flip_offset = get_flip_fold_offset(other); |
| 5679 | if (flip_offset == NULL) |
| 5680 | return -1; |
| 5681 | if (flip_offset != offset_other && |
| 5682 | delta_cmp(flip_offset, offset_other)) |
| 5683 | result = 1; |
| 5684 | done: |
| 5685 | Py_DECREF(flip_offset); |
| 5686 | return result; |
| 5687 | } |
| 5688 | |
| 5689 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 5690 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5691 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5692 | PyObject *result = NULL; |
| 5693 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | if (! PyDateTime_Check(other)) { |
| 5697 | if (PyDate_Check(other)) { |
| 5698 | /* Prevent invocation of date_richcompare. We want to |
| 5699 | return NotImplemented here to give the other object |
| 5700 | a chance. But since DateTime is a subclass of |
| 5701 | Date, if the other object is a Date, it would |
| 5702 | compute an ordering based on the date part alone, |
| 5703 | and we don't want that. So force unequal or |
| 5704 | uncomparable here in that case. */ |
| 5705 | if (op == Py_EQ) |
| 5706 | Py_RETURN_FALSE; |
| 5707 | if (op == Py_NE) |
| 5708 | Py_RETURN_TRUE; |
| 5709 | return cmperror(self, other); |
| 5710 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5711 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5713 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5714 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5716 | ((PyDateTime_DateTime *)other)->data, |
| 5717 | _PyDateTime_DATETIME_DATASIZE); |
| 5718 | return diff_to_bool(diff, op); |
| 5719 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5720 | offset1 = datetime_utcoffset(self, NULL); |
| 5721 | if (offset1 == NULL) |
| 5722 | return NULL; |
| 5723 | offset2 = datetime_utcoffset(other, NULL); |
| 5724 | if (offset2 == NULL) |
| 5725 | goto done; |
| 5726 | /* If they're both naive, or both aware and have the same offsets, |
| 5727 | * we get off cheap. Note that if they're both naive, offset1 == |
| 5728 | * offset2 == Py_None at this point. |
| 5729 | */ |
| 5730 | if ((offset1 == offset2) || |
| 5731 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 5732 | delta_cmp(offset1, offset2) == 0)) { |
| 5733 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5734 | ((PyDateTime_DateTime *)other)->data, |
| 5735 | _PyDateTime_DATETIME_DATASIZE); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5736 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5737 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5738 | if (ex == -1) |
| 5739 | goto done; |
| 5740 | if (ex) |
| 5741 | diff = 1; |
| 5742 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5743 | result = diff_to_bool(diff, op); |
| 5744 | } |
| 5745 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5746 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5747 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5748 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5749 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 5750 | other); |
| 5751 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5752 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | diff = GET_TD_DAYS(delta); |
| 5754 | if (diff == 0) |
| 5755 | diff = GET_TD_SECONDS(delta) | |
| 5756 | GET_TD_MICROSECONDS(delta); |
| 5757 | Py_DECREF(delta); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5758 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5759 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5760 | if (ex == -1) |
| 5761 | goto done; |
| 5762 | if (ex) |
| 5763 | diff = 1; |
| 5764 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5765 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5766 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 5767 | else if (op == Py_EQ) { |
| 5768 | result = Py_False; |
| 5769 | Py_INCREF(result); |
| 5770 | } |
| 5771 | else if (op == Py_NE) { |
| 5772 | result = Py_True; |
| 5773 | Py_INCREF(result); |
| 5774 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5775 | else { |
| 5776 | PyErr_SetString(PyExc_TypeError, |
| 5777 | "can't compare offset-naive and " |
| 5778 | "offset-aware datetimes"); |
| 5779 | } |
| 5780 | done: |
| 5781 | Py_DECREF(offset1); |
| 5782 | Py_XDECREF(offset2); |
| 5783 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5786 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5787 | datetime_hash(PyDateTime_DateTime *self) |
| 5788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5789 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5790 | PyObject *offset, *self0; |
| 5791 | if (DATE_GET_FOLD(self)) { |
| 5792 | self0 = new_datetime_ex2(GET_YEAR(self), |
| 5793 | GET_MONTH(self), |
| 5794 | GET_DAY(self), |
| 5795 | DATE_GET_HOUR(self), |
| 5796 | DATE_GET_MINUTE(self), |
| 5797 | DATE_GET_SECOND(self), |
| 5798 | DATE_GET_MICROSECOND(self), |
| 5799 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 5800 | 0, Py_TYPE(self)); |
| 5801 | if (self0 == NULL) |
| 5802 | return -1; |
| 5803 | } |
| 5804 | else { |
| 5805 | self0 = (PyObject *)self; |
| 5806 | Py_INCREF(self0); |
| 5807 | } |
| 5808 | offset = datetime_utcoffset(self0, NULL); |
| 5809 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5810 | |
| 5811 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5814 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5815 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5816 | self->hashcode = generic_hash( |
| 5817 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5818 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5819 | PyObject *temp1, *temp2; |
| 5820 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | assert(HASTZINFO(self)); |
| 5823 | days = ymd_to_ord(GET_YEAR(self), |
| 5824 | GET_MONTH(self), |
| 5825 | GET_DAY(self)); |
| 5826 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5827 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5828 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5829 | temp1 = new_delta(days, seconds, |
| 5830 | DATE_GET_MICROSECOND(self), |
| 5831 | 1); |
| 5832 | if (temp1 == NULL) { |
| 5833 | Py_DECREF(offset); |
| 5834 | return -1; |
| 5835 | } |
| 5836 | temp2 = delta_subtract(temp1, offset); |
| 5837 | Py_DECREF(temp1); |
| 5838 | if (temp2 == NULL) { |
| 5839 | Py_DECREF(offset); |
| 5840 | return -1; |
| 5841 | } |
| 5842 | self->hashcode = PyObject_Hash(temp2); |
| 5843 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5845 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5846 | } |
| 5847 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5848 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5849 | |
| 5850 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5851 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5853 | PyObject *clone; |
| 5854 | PyObject *tuple; |
| 5855 | int y = GET_YEAR(self); |
| 5856 | int m = GET_MONTH(self); |
| 5857 | int d = GET_DAY(self); |
| 5858 | int hh = DATE_GET_HOUR(self); |
| 5859 | int mm = DATE_GET_MINUTE(self); |
| 5860 | int ss = DATE_GET_SECOND(self); |
| 5861 | int us = DATE_GET_MICROSECOND(self); |
| 5862 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5863 | int fold = DATE_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5864 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5865 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5866 | datetime_kws, |
| 5867 | &y, &m, &d, &hh, &mm, &ss, &us, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5868 | &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5869 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 5870 | if (fold != 0 && fold != 1) { |
| 5871 | PyErr_SetString(PyExc_ValueError, |
| 5872 | "fold must be either 0 or 1"); |
| 5873 | return NULL; |
| 5874 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5875 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 5876 | if (tuple == NULL) |
| 5877 | return NULL; |
| 5878 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5879 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5880 | DATE_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5881 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | Py_DECREF(tuple); |
| 5883 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5887 | local_timezone_from_timestamp(time_t timestamp) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5888 | { |
| 5889 | PyObject *result = NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5890 | PyObject *delta; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5891 | struct tm local_time_tm; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5892 | PyObject *nameo = NULL; |
| 5893 | const char *zone = NULL; |
| 5894 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5895 | if (_PyTime_localtime(timestamp, &local_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5896 | return NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5897 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5898 | zone = local_time_tm.tm_zone; |
| 5899 | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5900 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 5901 | { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5902 | PyObject *local_time, *utc_time; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5903 | struct tm utc_time_tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5904 | char buf[100]; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5905 | strftime(buf, sizeof(buf), "%Z", &local_time_tm); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5906 | zone = buf; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5907 | local_time = new_datetime(local_time_tm.tm_year + 1900, |
| 5908 | local_time_tm.tm_mon + 1, |
| 5909 | local_time_tm.tm_mday, |
| 5910 | local_time_tm.tm_hour, |
| 5911 | local_time_tm.tm_min, |
| 5912 | local_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5913 | if (local_time == NULL) { |
| 5914 | return NULL; |
| 5915 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5916 | if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5917 | return NULL; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5918 | utc_time = new_datetime(utc_time_tm.tm_year + 1900, |
| 5919 | utc_time_tm.tm_mon + 1, |
| 5920 | utc_time_tm.tm_mday, |
| 5921 | utc_time_tm.tm_hour, |
| 5922 | utc_time_tm.tm_min, |
| 5923 | utc_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5924 | if (utc_time == NULL) { |
| 5925 | Py_DECREF(local_time); |
| 5926 | return NULL; |
| 5927 | } |
| 5928 | delta = datetime_subtract(local_time, utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5929 | Py_DECREF(local_time); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5930 | Py_DECREF(utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5931 | } |
| 5932 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5933 | if (delta == NULL) { |
| 5934 | return NULL; |
| 5935 | } |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5936 | if (zone != NULL) { |
| 5937 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 5938 | if (nameo == NULL) |
| 5939 | goto error; |
| 5940 | } |
| 5941 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 5942 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5943 | error: |
| 5944 | Py_DECREF(delta); |
| 5945 | return result; |
| 5946 | } |
| 5947 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5948 | static PyObject * |
| 5949 | local_timezone(PyDateTime_DateTime *utc_time) |
| 5950 | { |
| 5951 | time_t timestamp; |
| 5952 | PyObject *delta; |
| 5953 | PyObject *one_second; |
| 5954 | PyObject *seconds; |
| 5955 | |
| 5956 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 5957 | if (delta == NULL) |
| 5958 | return NULL; |
| 5959 | one_second = new_delta(0, 1, 0, 0); |
| 5960 | if (one_second == NULL) { |
| 5961 | Py_DECREF(delta); |
| 5962 | return NULL; |
| 5963 | } |
| 5964 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 5965 | (PyDateTime_Delta *)one_second); |
| 5966 | Py_DECREF(one_second); |
| 5967 | Py_DECREF(delta); |
| 5968 | if (seconds == NULL) |
| 5969 | return NULL; |
| 5970 | timestamp = _PyLong_AsTime_t(seconds); |
| 5971 | Py_DECREF(seconds); |
| 5972 | if (timestamp == -1 && PyErr_Occurred()) |
| 5973 | return NULL; |
| 5974 | return local_timezone_from_timestamp(timestamp); |
| 5975 | } |
| 5976 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5977 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5978 | local_to_seconds(int year, int month, int day, |
| 5979 | int hour, int minute, int second, int fold); |
| 5980 | |
| 5981 | static PyObject * |
| 5982 | local_timezone_from_local(PyDateTime_DateTime *local_dt) |
| 5983 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5984 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5985 | time_t timestamp; |
| 5986 | seconds = local_to_seconds(GET_YEAR(local_dt), |
| 5987 | GET_MONTH(local_dt), |
| 5988 | GET_DAY(local_dt), |
| 5989 | DATE_GET_HOUR(local_dt), |
| 5990 | DATE_GET_MINUTE(local_dt), |
| 5991 | DATE_GET_SECOND(local_dt), |
| 5992 | DATE_GET_FOLD(local_dt)); |
| 5993 | if (seconds == -1) |
| 5994 | return NULL; |
| 5995 | /* XXX: add bounds check */ |
| 5996 | timestamp = seconds - epoch; |
| 5997 | return local_timezone_from_timestamp(timestamp); |
| 5998 | } |
| 5999 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 6000 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6001 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 6002 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 6003 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6004 | PyObject *offset; |
| 6005 | PyObject *temp; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6006 | PyObject *self_tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 6007 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6008 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 6009 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 6010 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 6011 | &tzinfo)) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 6012 | return NULL; |
| 6013 | |
| 6014 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6015 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 6016 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6017 | if (!HASTZINFO(self) || self->tzinfo == Py_None) { |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 6018 | naive: |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6019 | self_tzinfo = local_timezone_from_local(self); |
| 6020 | if (self_tzinfo == NULL) |
| 6021 | return NULL; |
| 6022 | } else { |
| 6023 | self_tzinfo = self->tzinfo; |
| 6024 | Py_INCREF(self_tzinfo); |
| 6025 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 6026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6027 | /* Conversion to self's own time zone is a NOP. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6028 | if (self_tzinfo == tzinfo) { |
| 6029 | Py_DECREF(self_tzinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6030 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 6031 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6032 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 6033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6034 | /* Convert self to UTC. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6035 | offset = call_utcoffset(self_tzinfo, (PyObject *)self); |
| 6036 | Py_DECREF(self_tzinfo); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6037 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6038 | return NULL; |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 6039 | else if(offset == Py_None) { |
| 6040 | Py_DECREF(offset); |
| 6041 | goto naive; |
| 6042 | } |
| 6043 | else if (!PyDelta_Check(offset)) { |
| 6044 | Py_DECREF(offset); |
| 6045 | PyErr_Format(PyExc_TypeError, "utcoffset() returned %.200s," |
| 6046 | " expected timedelta or None", Py_TYPE(offset)->tp_name); |
| 6047 | return NULL; |
| 6048 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6049 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 6050 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 6051 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6052 | Py_DECREF(offset); |
| 6053 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6054 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 6055 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6056 | /* Make sure result is aware and UTC. */ |
| 6057 | if (!HASTZINFO(result)) { |
| 6058 | temp = (PyObject *)result; |
| 6059 | result = (PyDateTime_DateTime *) |
| 6060 | new_datetime_ex2(GET_YEAR(result), |
| 6061 | GET_MONTH(result), |
| 6062 | GET_DAY(result), |
| 6063 | DATE_GET_HOUR(result), |
| 6064 | DATE_GET_MINUTE(result), |
| 6065 | DATE_GET_SECOND(result), |
| 6066 | DATE_GET_MICROSECOND(result), |
| 6067 | PyDateTime_TimeZone_UTC, |
| 6068 | DATE_GET_FOLD(result), |
| 6069 | Py_TYPE(result)); |
| 6070 | Py_DECREF(temp); |
| 6071 | if (result == NULL) |
| 6072 | return NULL; |
| 6073 | } |
| 6074 | else { |
| 6075 | /* Result is already aware - just replace tzinfo. */ |
| 6076 | temp = result->tzinfo; |
| 6077 | result->tzinfo = PyDateTime_TimeZone_UTC; |
| 6078 | Py_INCREF(result->tzinfo); |
| 6079 | Py_DECREF(temp); |
| 6080 | } |
| 6081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6082 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 6083 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 6084 | if (tzinfo == Py_None) { |
| 6085 | tzinfo = local_timezone(result); |
| 6086 | if (tzinfo == NULL) { |
| 6087 | Py_DECREF(result); |
| 6088 | return NULL; |
| 6089 | } |
| 6090 | } |
| 6091 | else |
| 6092 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 6093 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6094 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 6095 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 6096 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 6097 | result = (PyDateTime_DateTime *) |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 6098 | _PyObject_CallMethodIdOneArg(tzinfo, &PyId_fromutc, temp); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6099 | Py_DECREF(temp); |
| 6100 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 6101 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
| 6104 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6105 | datetime_timetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6106 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6107 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6109 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6110 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6111 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6112 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 6113 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6114 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6115 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6116 | if (dst != Py_None) |
| 6117 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 6118 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6119 | } |
| 6120 | return build_struct_time(GET_YEAR(self), |
| 6121 | GET_MONTH(self), |
| 6122 | GET_DAY(self), |
| 6123 | DATE_GET_HOUR(self), |
| 6124 | DATE_GET_MINUTE(self), |
| 6125 | DATE_GET_SECOND(self), |
| 6126 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 6129 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6130 | local_to_seconds(int year, int month, int day, |
| 6131 | int hour, int minute, int second, int fold) |
| 6132 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 6133 | long long t, a, b, u1, u2, t1, t2, lt; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6134 | t = utc_to_seconds(year, month, day, hour, minute, second); |
| 6135 | /* Our goal is to solve t = local(u) for u. */ |
| 6136 | lt = local(t); |
| 6137 | if (lt == -1) |
| 6138 | return -1; |
| 6139 | a = lt - t; |
| 6140 | u1 = t - a; |
| 6141 | t1 = local(u1); |
| 6142 | if (t1 == -1) |
| 6143 | return -1; |
| 6144 | if (t1 == t) { |
| 6145 | /* We found one solution, but it may not be the one we need. |
| 6146 | * Look for an earlier solution (if `fold` is 0), or a |
| 6147 | * later one (if `fold` is 1). */ |
| 6148 | if (fold) |
| 6149 | u2 = u1 + max_fold_seconds; |
| 6150 | else |
| 6151 | u2 = u1 - max_fold_seconds; |
| 6152 | lt = local(u2); |
| 6153 | if (lt == -1) |
| 6154 | return -1; |
| 6155 | b = lt - u2; |
| 6156 | if (a == b) |
| 6157 | return u1; |
| 6158 | } |
| 6159 | else { |
| 6160 | b = t1 - u1; |
| 6161 | assert(a != b); |
| 6162 | } |
| 6163 | u2 = t - b; |
| 6164 | t2 = local(u2); |
| 6165 | if (t2 == -1) |
| 6166 | return -1; |
| 6167 | if (t2 == t) |
| 6168 | return u2; |
| 6169 | if (t1 == t) |
| 6170 | return u1; |
| 6171 | /* We have found both offsets a and b, but neither t - a nor t - b is |
| 6172 | * a solution. This means t is in the gap. */ |
| 6173 | return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); |
| 6174 | } |
| 6175 | |
| 6176 | /* date(1970,1,1).toordinal() == 719163 */ |
| 6177 | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) |
| 6178 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6179 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6180 | datetime_timestamp(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6181 | { |
| 6182 | PyObject *result; |
| 6183 | |
| 6184 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 6185 | PyObject *delta; |
| 6186 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 6187 | if (delta == NULL) |
| 6188 | return NULL; |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6189 | result = delta_total_seconds(delta, NULL); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6190 | Py_DECREF(delta); |
| 6191 | } |
| 6192 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 6193 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6194 | seconds = local_to_seconds(GET_YEAR(self), |
| 6195 | GET_MONTH(self), |
| 6196 | GET_DAY(self), |
| 6197 | DATE_GET_HOUR(self), |
| 6198 | DATE_GET_MINUTE(self), |
| 6199 | DATE_GET_SECOND(self), |
| 6200 | DATE_GET_FOLD(self)); |
| 6201 | if (seconds == -1) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6202 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6203 | result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + |
| 6204 | DATE_GET_MICROSECOND(self) / 1e6); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6205 | } |
| 6206 | return result; |
| 6207 | } |
| 6208 | |
| 6209 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6210 | datetime_getdate(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6211 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6212 | return new_date(GET_YEAR(self), |
| 6213 | GET_MONTH(self), |
| 6214 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6215 | } |
| 6216 | |
| 6217 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6218 | datetime_gettime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6220 | return new_time(DATE_GET_HOUR(self), |
| 6221 | DATE_GET_MINUTE(self), |
| 6222 | DATE_GET_SECOND(self), |
| 6223 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6224 | Py_None, |
| 6225 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6229 | datetime_gettimetz(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6231 | return new_time(DATE_GET_HOUR(self), |
| 6232 | DATE_GET_MINUTE(self), |
| 6233 | DATE_GET_SECOND(self), |
| 6234 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6235 | GET_DT_TZINFO(self), |
| 6236 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6240 | datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6241 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6242 | int y, m, d, hh, mm, ss; |
| 6243 | PyObject *tzinfo; |
| 6244 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6245 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6246 | tzinfo = GET_DT_TZINFO(self); |
| 6247 | if (tzinfo == Py_None) { |
| 6248 | utcself = self; |
| 6249 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6250 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6251 | else { |
| 6252 | PyObject *offset; |
| 6253 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 6254 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 6255 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6256 | if (offset == Py_None) { |
| 6257 | Py_DECREF(offset); |
| 6258 | utcself = self; |
| 6259 | Py_INCREF(utcself); |
| 6260 | } |
| 6261 | else { |
| 6262 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 6263 | (PyDateTime_Delta *)offset, -1); |
| 6264 | Py_DECREF(offset); |
| 6265 | if (utcself == NULL) |
| 6266 | return NULL; |
| 6267 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6268 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6269 | y = GET_YEAR(utcself); |
| 6270 | m = GET_MONTH(utcself); |
| 6271 | d = GET_DAY(utcself); |
| 6272 | hh = DATE_GET_HOUR(utcself); |
| 6273 | mm = DATE_GET_MINUTE(utcself); |
| 6274 | ss = DATE_GET_SECOND(utcself); |
| 6275 | |
| 6276 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6277 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 6280 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 6281 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6282 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6283 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 6284 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 6285 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6286 | */ |
| 6287 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6288 | datetime_getstate(PyDateTime_DateTime *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6290 | PyObject *basestate; |
| 6291 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6293 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 6294 | _PyDateTime_DATETIME_DATASIZE); |
| 6295 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6296 | if (proto > 3 && DATE_GET_FOLD(self)) |
| 6297 | /* Set the first bit of the third byte */ |
| 6298 | PyBytes_AS_STRING(basestate)[2] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6299 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 6300 | result = PyTuple_Pack(1, basestate); |
| 6301 | else |
| 6302 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 6303 | Py_DECREF(basestate); |
| 6304 | } |
| 6305 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
| 6308 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6309 | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6310 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6311 | int proto; |
| 6312 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6313 | return NULL; |
| 6314 | |
| 6315 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6318 | static PyObject * |
| 6319 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
| 6320 | { |
| 6321 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); |
| 6322 | } |
| 6323 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6324 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6326 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6327 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 6328 | DATETIME_DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6330 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 6331 | METH_NOARGS | METH_CLASS, |
| 6332 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6333 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6334 | {"fromtimestamp", (PyCFunction)(void(*)(void))datetime_fromtimestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6335 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 6336 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6338 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 6339 | METH_VARARGS | METH_CLASS, |
Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 6340 | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6342 | {"strptime", (PyCFunction)datetime_strptime, |
| 6343 | METH_VARARGS | METH_CLASS, |
| 6344 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 6345 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 6346 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6347 | {"combine", (PyCFunction)(void(*)(void))datetime_combine, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6348 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 6349 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6350 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 6351 | {"fromisoformat", (PyCFunction)datetime_fromisoformat, |
| 6352 | METH_O | METH_CLASS, |
| 6353 | PyDoc_STR("string -> datetime from datetime.isoformat() output")}, |
| 6354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6355 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6357 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 6358 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6360 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 6361 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6363 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 6364 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6366 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 6367 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 6370 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6371 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6372 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 6373 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 6374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6375 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 6376 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6377 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6378 | {"isoformat", (PyCFunction)(void(*)(void))datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6379 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 6380 | "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6381 | "sep is used to separate the year from the time, and " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 6382 | "defaults to 'T'.\n" |
| 6383 | "timespec specifies what components of the time to include" |
| 6384 | " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," |
| 6385 | " 'milliseconds', and 'microseconds').\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6387 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 6388 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6390 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 6391 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6393 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 6394 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6395 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6396 | {"replace", (PyCFunction)(void(*)(void))datetime_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6397 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 6398 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6399 | {"astimezone", (PyCFunction)(void(*)(void))datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 6401 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6402 | {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6403 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6404 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6405 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 6406 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 6407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6408 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6409 | }; |
| 6410 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 6411 | static const char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 6412 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 6413 | \n\ |
| 6414 | 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] | 6415 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6416 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6417 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6418 | datetime_add, /* nb_add */ |
| 6419 | datetime_subtract, /* nb_subtract */ |
| 6420 | 0, /* nb_multiply */ |
| 6421 | 0, /* nb_remainder */ |
| 6422 | 0, /* nb_divmod */ |
| 6423 | 0, /* nb_power */ |
| 6424 | 0, /* nb_negative */ |
| 6425 | 0, /* nb_positive */ |
| 6426 | 0, /* nb_absolute */ |
| 6427 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6428 | }; |
| 6429 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 6430 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6431 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6432 | "datetime.datetime", /* tp_name */ |
| 6433 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 6434 | 0, /* tp_itemsize */ |
| 6435 | (destructor)datetime_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6436 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6437 | 0, /* tp_getattr */ |
| 6438 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6439 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6440 | (reprfunc)datetime_repr, /* tp_repr */ |
| 6441 | &datetime_as_number, /* tp_as_number */ |
| 6442 | 0, /* tp_as_sequence */ |
| 6443 | 0, /* tp_as_mapping */ |
| 6444 | (hashfunc)datetime_hash, /* tp_hash */ |
| 6445 | 0, /* tp_call */ |
| 6446 | (reprfunc)datetime_str, /* tp_str */ |
| 6447 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6448 | 0, /* tp_setattro */ |
| 6449 | 0, /* tp_as_buffer */ |
| 6450 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6451 | datetime_doc, /* tp_doc */ |
| 6452 | 0, /* tp_traverse */ |
| 6453 | 0, /* tp_clear */ |
| 6454 | datetime_richcompare, /* tp_richcompare */ |
| 6455 | 0, /* tp_weaklistoffset */ |
| 6456 | 0, /* tp_iter */ |
| 6457 | 0, /* tp_iternext */ |
| 6458 | datetime_methods, /* tp_methods */ |
| 6459 | 0, /* tp_members */ |
| 6460 | datetime_getset, /* tp_getset */ |
Miss Islington (bot) | eceee54 | 2020-05-28 09:41:41 -0700 | [diff] [blame^] | 6461 | 0, /* tp_base; filled in |
| 6462 | PyInit__datetime */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6463 | 0, /* tp_dict */ |
| 6464 | 0, /* tp_descr_get */ |
| 6465 | 0, /* tp_descr_set */ |
| 6466 | 0, /* tp_dictoffset */ |
| 6467 | 0, /* tp_init */ |
| 6468 | datetime_alloc, /* tp_alloc */ |
| 6469 | datetime_new, /* tp_new */ |
| 6470 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6471 | }; |
| 6472 | |
| 6473 | /* --------------------------------------------------------------------------- |
| 6474 | * Module methods and initialization. |
| 6475 | */ |
| 6476 | |
| 6477 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6478 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6479 | }; |
| 6480 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6481 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 6482 | * datetime.h. |
| 6483 | */ |
| 6484 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6485 | &PyDateTime_DateType, |
| 6486 | &PyDateTime_DateTimeType, |
| 6487 | &PyDateTime_TimeType, |
| 6488 | &PyDateTime_DeltaType, |
| 6489 | &PyDateTime_TZInfoType, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6490 | NULL, // PyDatetime_TimeZone_UTC not initialized yet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6491 | new_date_ex, |
| 6492 | new_datetime_ex, |
| 6493 | new_time_ex, |
| 6494 | new_delta_ex, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6495 | new_timezone, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6496 | datetime_fromtimestamp, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 6497 | datetime_date_fromtimestamp_capi, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6498 | new_datetime_ex2, |
| 6499 | new_time_ex2 |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6500 | }; |
| 6501 | |
| 6502 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6503 | |
| 6504 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6505 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6506 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6507 | "Fast implementation of the datetime type.", |
| 6508 | -1, |
| 6509 | module_methods, |
| 6510 | NULL, |
| 6511 | NULL, |
| 6512 | NULL, |
| 6513 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6514 | }; |
| 6515 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6516 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6517 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6519 | PyObject *m; /* a module object */ |
| 6520 | PyObject *d; /* its dict */ |
| 6521 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6522 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6524 | m = PyModule_Create(&datetimemodule); |
| 6525 | if (m == NULL) |
| 6526 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6527 | |
Miss Islington (bot) | eceee54 | 2020-05-28 09:41:41 -0700 | [diff] [blame^] | 6528 | // `&...` is not a constant expression according to a strict reading |
| 6529 | // of C standards. Fill tp_base at run-time rather than statically. |
| 6530 | // See https://bugs.python.org/issue40777 |
| 6531 | PyDateTime_IsoCalendarDateType.tp_base = &PyTuple_Type; |
| 6532 | PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType; |
| 6533 | PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType; |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 6534 | |
Dong-hee Na | 37fcbb6 | 2020-03-25 07:08:51 +0900 | [diff] [blame] | 6535 | PyTypeObject *types[] = { |
| 6536 | &PyDateTime_DateType, |
| 6537 | &PyDateTime_DateTimeType, |
| 6538 | &PyDateTime_TimeType, |
| 6539 | &PyDateTime_DeltaType, |
| 6540 | &PyDateTime_TZInfoType, |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 6541 | &PyDateTime_TimeZoneType, |
Dong-hee Na | 37fcbb6 | 2020-03-25 07:08:51 +0900 | [diff] [blame] | 6542 | }; |
| 6543 | |
| 6544 | for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) { |
| 6545 | if (PyModule_AddType(m, types[i]) < 0) { |
| 6546 | return NULL; |
| 6547 | } |
| 6548 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6549 | |
Paul Ganssle | 1b97b9b | 2020-05-16 10:02:59 -0400 | [diff] [blame] | 6550 | if (PyType_Ready(&PyDateTime_IsoCalendarDateType) < 0) { |
| 6551 | return NULL; |
| 6552 | } |
| 6553 | Py_INCREF(&PyDateTime_IsoCalendarDateType); |
| 6554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6555 | /* timedelta values */ |
| 6556 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6558 | x = new_delta(0, 0, 1, 0); |
| 6559 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6560 | return NULL; |
| 6561 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6563 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 6564 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6565 | return NULL; |
| 6566 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6568 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 6569 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6570 | return NULL; |
| 6571 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6573 | /* date values */ |
| 6574 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6576 | x = new_date(1, 1, 1); |
| 6577 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6578 | return NULL; |
| 6579 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6581 | x = new_date(MAXYEAR, 12, 31); |
| 6582 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6583 | return NULL; |
| 6584 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6586 | x = new_delta(1, 0, 0, 0); |
| 6587 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6588 | return NULL; |
| 6589 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6591 | /* time values */ |
| 6592 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6593 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6594 | x = new_time(0, 0, 0, 0, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6596 | return NULL; |
| 6597 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6598 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6599 | x = new_time(23, 59, 59, 999999, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6600 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6601 | return NULL; |
| 6602 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6604 | x = new_delta(0, 0, 1, 0); |
| 6605 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6606 | return NULL; |
| 6607 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6609 | /* datetime values */ |
| 6610 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6611 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6612 | 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] | 6613 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6614 | return NULL; |
| 6615 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6616 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6617 | 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] | 6618 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6619 | return NULL; |
| 6620 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6622 | x = new_delta(0, 0, 1, 0); |
| 6623 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6624 | return NULL; |
| 6625 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6626 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6627 | /* timezone values */ |
| 6628 | d = PyDateTime_TimeZoneType.tp_dict; |
| 6629 | |
| 6630 | delta = new_delta(0, 0, 0, 0); |
| 6631 | if (delta == NULL) |
| 6632 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6633 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6634 | Py_DECREF(delta); |
| 6635 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 6636 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 6637 | PyDateTime_TimeZone_UTC = x; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6638 | CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6639 | |
Ngalim Siregar | 92c7e30 | 2019-08-09 21:22:16 +0700 | [diff] [blame] | 6640 | /* bpo-37642: These attributes are rounded to the nearest minute for backwards |
| 6641 | * compatibility, even though the constructor will accept a wider range of |
| 6642 | * values. This may change in the future.*/ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6643 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 6644 | if (delta == NULL) |
| 6645 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6646 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6647 | Py_DECREF(delta); |
| 6648 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6649 | return NULL; |
| 6650 | Py_DECREF(x); |
| 6651 | |
| 6652 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 6653 | if (delta == NULL) |
| 6654 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6655 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6656 | Py_DECREF(delta); |
| 6657 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6658 | return NULL; |
| 6659 | Py_DECREF(x); |
| 6660 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6661 | /* Epoch */ |
| 6662 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6663 | PyDateTime_TimeZone_UTC, 0); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6664 | if (PyDateTime_Epoch == NULL) |
| 6665 | return NULL; |
| 6666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6667 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6668 | PyModule_AddIntMacro(m, MINYEAR); |
| 6669 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6671 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 6672 | if (x == NULL) |
| 6673 | return NULL; |
| 6674 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6676 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 6677 | * pasting together 4 single years. |
| 6678 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6679 | Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6680 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6682 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 6683 | * get from pasting together 4 100-year cycles. |
| 6684 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6685 | Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6686 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6688 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 6689 | * pasting together 25 4-year cycles. |
| 6690 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6691 | Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6692 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6694 | us_per_ms = PyLong_FromLong(1000); |
| 6695 | us_per_second = PyLong_FromLong(1000000); |
| 6696 | us_per_minute = PyLong_FromLong(60000000); |
| 6697 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 6698 | if (us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6699 | us_per_minute == NULL || seconds_per_day == NULL) |
| 6700 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6702 | /* The rest are too big for 32-bit ints, but even |
| 6703 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 6704 | */ |
| 6705 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 6706 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 6707 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 6708 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 6709 | return NULL; |
| 6710 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6711 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6712 | |
| 6713 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6714 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6715 | x.n = x stripped of its timezone -- its naive time. |
| 6716 | 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] | 6717 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6718 | 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] | 6719 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6720 | x.s = x's standard offset, x.o - x.d |
| 6721 | |
| 6722 | Now some derived rules, where k is a duration (timedelta). |
| 6723 | |
| 6724 | 1. x.o = x.s + x.d |
| 6725 | This follows from the definition of x.s. |
| 6726 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6727 | 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] | 6728 | This is actually a requirement, an assumption we need to make about |
| 6729 | sane tzinfo classes. |
| 6730 | |
| 6731 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 6732 | This is again a requirement for a sane tzinfo class. |
| 6733 | |
| 6734 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6735 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6736 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6737 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6738 | Again follows from how arithmetic is defined. |
| 6739 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6740 | 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] | 6741 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 6742 | None when called). |
| 6743 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6744 | 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] | 6745 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6746 | |
| 6747 | By #3, we want |
| 6748 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6749 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6750 | |
| 6751 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 6752 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 6753 | becomes true; in effect, we want to solve [2] for k: |
| 6754 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6755 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6756 | |
| 6757 | By #1, this is the same as |
| 6758 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6759 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6760 | |
| 6761 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 6762 | Substituting that into [3], |
| 6763 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6764 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 6765 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 6766 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 6767 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6768 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6769 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 6770 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 6771 | very large, since all offset-returning methods return a duration of magnitude |
| 6772 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 6773 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6774 | |
| 6775 | In any case, the new value is |
| 6776 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6777 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6778 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6779 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 6780 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6781 | |
| 6782 | At this point, if |
| 6783 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6784 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6785 | |
| 6786 | 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] | 6787 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 6788 | 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] | 6789 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 6790 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 6791 | 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] | 6792 | the only spelling that makes sense on the local wall clock. |
| 6793 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6794 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 6795 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 6796 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6797 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6798 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6799 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6800 | Now |
| 6801 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6802 | (y + y.s).n = by #5 |
| 6803 | y.n + y.s = since y.n = x.n |
| 6804 | x.n + y.s = since z and y are have the same tzinfo member, |
| 6805 | y.s = z.s by #2 |
| 6806 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6807 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6808 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6809 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6810 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6811 | x.n - ((x.n + z.s) - z.o) = expanding |
| 6812 | x.n - x.n - z.s + z.o = cancelling |
| 6813 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6814 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6815 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6816 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6817 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6818 | 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] | 6819 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 6820 | 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] | 6821 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6822 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 6823 | 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] | 6824 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6825 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6826 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6827 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6828 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6829 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6830 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6831 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6832 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6833 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6834 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 6835 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 6836 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 6837 | the justifications for the kinds of substitutions we've done several times |
| 6838 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6839 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6840 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6841 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 6842 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 6843 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 6844 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 6845 | - z.o + z'.o = #1 twice |
| 6846 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 6847 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6848 | |
| 6849 | 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] | 6850 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 6851 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6852 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6853 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 6854 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 6855 | would have to change the result dst() returns: we start in DST, and moving |
| 6856 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6857 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6858 | There isn't a sane case where this can happen. The closest it gets is at |
| 6859 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 6860 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 6861 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 6862 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 6863 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 6864 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 6865 | standard time. Since that's what the local clock *does*, we want to map both |
| 6866 | 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] | 6867 | in local time, but so it goes -- it's the way the local clock works. |
| 6868 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6869 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 6870 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 6871 | 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] | 6872 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 6873 | |
| 6874 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 6875 | 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] | 6876 | 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] | 6877 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 6878 | but the reasoning doesn't depend on the example -- it depends on there being |
| 6879 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6880 | z' must be in standard time, and is the spelling we want in this case. |
| 6881 | |
| 6882 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 6883 | concerned (because it takes z' as being in standard time rather than the |
| 6884 | daylight time we intend here), but returning it gives the real-life "local |
| 6885 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 6886 | tz. |
| 6887 | |
| 6888 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 6889 | the 1:MM standard time spelling we want. |
| 6890 | |
| 6891 | So how can this break? One of the assumptions must be violated. Two |
| 6892 | possibilities: |
| 6893 | |
| 6894 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 6895 | time zone. This isn't true if, for political reasons or continental drift, |
| 6896 | a region decides to change its base offset from UTC. |
| 6897 | |
| 6898 | 2) There may be versions of "double daylight" time where the tail end of |
| 6899 | the analysis gives up a step too early. I haven't thought about that |
| 6900 | enough to say. |
| 6901 | |
| 6902 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 6903 | "almost all" time zones: so long as the standard offset is invariant, it |
| 6904 | doesn't matter if daylight time transition points change from year to year, or |
| 6905 | if daylight time is skipped in some years; it doesn't matter how large or |
| 6906 | small dst() may get within its bounds; and it doesn't even matter if some |
| 6907 | perverse time zone returns a negative dst()). So a breaking case must be |
| 6908 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6909 | --------------------------------------------------------------------------- */ |