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" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 12 | #include "structmember.h" |
| 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) |
| 21 | #define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType) |
| 22 | |
| 23 | #define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType) |
| 24 | #define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType) |
| 25 | |
| 26 | #define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType) |
| 27 | #define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType) |
| 28 | |
| 29 | #define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType) |
| 30 | #define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType) |
| 31 | |
| 32 | #define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType) |
| 33 | #define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType) |
| 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" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 41 | [clinic start generated code]*/ |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 42 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=25138ad6a696b785]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 43 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 44 | #include "clinic/_datetimemodule.c.h" |
| 45 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 46 | /* We require that C int be at least 32 bits, and use int virtually |
| 47 | * everywhere. In just a few cases we use a temp long, where a Python |
| 48 | * API returns a C long. In such cases, we have to ensure that the |
| 49 | * final result fits in a C int (this can be an issue on 64-bit boxes). |
| 50 | */ |
| 51 | #if SIZEOF_INT < 4 |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 52 | # error "_datetime.c requires that C int have at least 32 bits" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | #define MINYEAR 1 |
| 56 | #define MAXYEAR 9999 |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 57 | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 58 | |
| 59 | /* Nine decimal digits is easy to communicate, and leaves enough room |
| 60 | * so that two delta days can be added w/o fear of overflowing a signed |
| 61 | * 32-bit int, and with plenty of room left over to absorb any possible |
| 62 | * carries from adding seconds. |
| 63 | */ |
| 64 | #define MAX_DELTA_DAYS 999999999 |
| 65 | |
| 66 | /* Rename the long macros in datetime.h to more reasonable short names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | #define GET_YEAR PyDateTime_GET_YEAR |
| 68 | #define GET_MONTH PyDateTime_GET_MONTH |
| 69 | #define GET_DAY PyDateTime_GET_DAY |
| 70 | #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
| 71 | #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
| 72 | #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
| 73 | #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 74 | #define DATE_GET_FOLD PyDateTime_DATE_GET_FOLD |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 75 | |
| 76 | /* Date accessors for date and datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
| 78 | ((o)->data[1] = ((v) & 0x00ff))) |
| 79 | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
| 80 | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 81 | |
| 82 | /* Date/Time accessors for datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
| 84 | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
| 85 | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
| 86 | #define DATE_SET_MICROSECOND(o, v) \ |
| 87 | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
| 88 | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
| 89 | ((o)->data[9] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 90 | #define DATE_SET_FOLD(o, v) (PyDateTime_DATE_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 91 | |
| 92 | /* Time accessors for time. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
| 94 | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
| 95 | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
| 96 | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 97 | #define TIME_GET_FOLD PyDateTime_TIME_GET_FOLD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
| 99 | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
| 100 | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
| 101 | #define TIME_SET_MICROSECOND(o, v) \ |
| 102 | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
| 103 | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
| 104 | ((o)->data[5] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 105 | #define TIME_SET_FOLD(o, v) (PyDateTime_TIME_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 106 | |
| 107 | /* Delta accessors for timedelta. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
| 109 | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
| 110 | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
| 113 | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 114 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
| 115 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 116 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
| 117 | * p->hastzinfo. |
| 118 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 119 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
| 120 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
| 121 | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
| 122 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
| 123 | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 124 | /* M is a char or int claiming to be a valid month. The macro is equivalent |
| 125 | * to the two-sided Python test |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | * 1 <= M <= 12 |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 127 | */ |
| 128 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
| 129 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 130 | /* Forward declarations. */ |
| 131 | static PyTypeObject PyDateTime_DateType; |
| 132 | static PyTypeObject PyDateTime_DateTimeType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 133 | static PyTypeObject PyDateTime_DeltaType; |
| 134 | static PyTypeObject PyDateTime_TimeType; |
| 135 | static PyTypeObject PyDateTime_TZInfoType; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 136 | static PyTypeObject PyDateTime_TimeZoneType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 137 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 138 | static int check_tzinfo_subclass(PyObject *p); |
| 139 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 140 | _Py_IDENTIFIER(as_integer_ratio); |
| 141 | _Py_IDENTIFIER(fromutc); |
| 142 | _Py_IDENTIFIER(isoformat); |
| 143 | _Py_IDENTIFIER(strftime); |
| 144 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 145 | /* --------------------------------------------------------------------------- |
| 146 | * Math utilities. |
| 147 | */ |
| 148 | |
| 149 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 150 | * iff k^i has sign bit set and k^j has sign bit set, |
| 151 | * iff (k^i)&(k^j) has sign bit set. |
| 152 | */ |
| 153 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 155 | |
| 156 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 157 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 158 | * the real point of this. C will probably truncate instead (C99 |
| 159 | * requires truncation; C89 left it implementation-defined). |
| 160 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 161 | * for all the uses made of it. This simplifies the code and makes |
| 162 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 163 | * overflow case). |
| 164 | */ |
| 165 | static int |
| 166 | divmod(int x, int y, int *r) |
| 167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | assert(y > 0); |
| 171 | quo = x / y; |
| 172 | *r = x - quo * y; |
| 173 | if (*r < 0) { |
| 174 | --quo; |
| 175 | *r += y; |
| 176 | } |
| 177 | assert(0 <= *r && *r < y); |
| 178 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 181 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 182 | * are rounded to even. |
| 183 | */ |
| 184 | static PyObject * |
| 185 | divide_nearest(PyObject *m, PyObject *n) |
| 186 | { |
| 187 | PyObject *result; |
| 188 | PyObject *temp; |
| 189 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 190 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 191 | if (temp == NULL) |
| 192 | return NULL; |
| 193 | result = PyTuple_GET_ITEM(temp, 0); |
| 194 | Py_INCREF(result); |
| 195 | Py_DECREF(temp); |
| 196 | |
| 197 | return result; |
| 198 | } |
| 199 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 200 | /* --------------------------------------------------------------------------- |
| 201 | * General calendrical helper functions |
| 202 | */ |
| 203 | |
| 204 | /* For each month ordinal in 1..12, the number of days in that month, |
| 205 | * and the number of days before that month in the same year. These |
| 206 | * are correct for non-leap years only. |
| 207 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 208 | static const int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | 0, /* unused; this vector uses 1-based indexing */ |
| 210 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 213 | static const int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | 0, /* unused; this vector uses 1-based indexing */ |
| 215 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | /* year -> 1 if leap year, else 0. */ |
| 219 | static int |
| 220 | is_leap(int year) |
| 221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | /* Cast year to unsigned. The result is the same either way, but |
| 223 | * C can generate faster code for unsigned mod than for signed |
| 224 | * mod (especially for % 4 -- a good compiler should just grab |
| 225 | * the last 2 bits when the LHS is unsigned). |
| 226 | */ |
| 227 | const unsigned int ayear = (unsigned int)year; |
| 228 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* year, month -> number of days in that month in that year */ |
| 232 | static int |
| 233 | days_in_month(int year, int month) |
| 234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | assert(month >= 1); |
| 236 | assert(month <= 12); |
| 237 | if (month == 2 && is_leap(year)) |
| 238 | return 29; |
| 239 | else |
| 240 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 243 | /* year, month -> number of days in year preceding first day of month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 244 | static int |
| 245 | days_before_month(int year, int month) |
| 246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | assert(month >= 1); |
| 250 | assert(month <= 12); |
| 251 | days = _days_before_month[month]; |
| 252 | if (month > 2 && is_leap(year)) |
| 253 | ++days; |
| 254 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* year -> number of days before January 1st of year. Remember that we |
| 258 | * start with year 1, so days_before_year(1) == 0. |
| 259 | */ |
| 260 | static int |
| 261 | days_before_year(int year) |
| 262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | int y = year - 1; |
| 264 | /* This is incorrect if year <= 0; we really want the floor |
| 265 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 266 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 268 | assert (year >= 1); |
| 269 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 273 | * the correct values is asserted in the module init function. |
| 274 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 276 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 277 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 278 | |
| 279 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 280 | static void |
| 281 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 286 | * leap years repeats exactly every 400 years. The basic strategy is |
| 287 | * to find the closest 400-year boundary at or before ordinal, then |
| 288 | * work with the offset from that boundary to ordinal. Life is much |
| 289 | * clearer if we subtract 1 from ordinal first -- then the values |
| 290 | * of ordinal at 400-year boundaries are exactly those divisible |
| 291 | * by DI400Y: |
| 292 | * |
| 293 | * D M Y n n-1 |
| 294 | * -- --- ---- ---------- ---------------- |
| 295 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 296 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 297 | * ... |
| 298 | * 30 Dec 000 -1 -2 |
| 299 | * 31 Dec 000 0 -1 |
| 300 | * 1 Jan 001 1 0 400-year boundary |
| 301 | * 2 Jan 001 2 1 |
| 302 | * 3 Jan 001 3 2 |
| 303 | * ... |
| 304 | * 31 Dec 400 DI400Y DI400Y -1 |
| 305 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 306 | */ |
| 307 | assert(ordinal >= 1); |
| 308 | --ordinal; |
| 309 | n400 = ordinal / DI400Y; |
| 310 | n = ordinal % DI400Y; |
| 311 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 314 | * year, to the desired date. Now compute how many 100-year cycles |
| 315 | * precede n. |
| 316 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 317 | * 100-year cycles precede the desired day, which implies the |
| 318 | * desired day is December 31 at the end of a 400-year cycle. |
| 319 | */ |
| 320 | n100 = n / DI100Y; |
| 321 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | /* Now compute how many 4-year cycles precede it. */ |
| 324 | n4 = n / DI4Y; |
| 325 | n = n % DI4Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | /* And now how many single years. Again n1 can be 4, and again |
| 328 | * meaning that the desired day is December 31 at the end of the |
| 329 | * 4-year cycle. |
| 330 | */ |
| 331 | n1 = n / 365; |
| 332 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | *year += n100 * 100 + n4 * 4 + n1; |
| 335 | if (n1 == 4 || n100 == 4) { |
| 336 | assert(n == 0); |
| 337 | *year -= 1; |
| 338 | *month = 12; |
| 339 | *day = 31; |
| 340 | return; |
| 341 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | /* Now the year is correct, and n is the offset from January 1. We |
| 344 | * find the month via an estimate that's either exact or one too |
| 345 | * large. |
| 346 | */ |
| 347 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 348 | assert(leapyear == is_leap(*year)); |
| 349 | *month = (n + 50) >> 5; |
| 350 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 351 | if (preceding > n) { |
| 352 | /* estimate is too large */ |
| 353 | *month -= 1; |
| 354 | preceding -= days_in_month(*year, *month); |
| 355 | } |
| 356 | n -= preceding; |
| 357 | assert(0 <= n); |
| 358 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 364 | static int |
| 365 | ymd_to_ord(int year, int month, int day) |
| 366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 371 | static int |
| 372 | weekday(int year, int month, int day) |
| 373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 378 | * first calendar week containing a Thursday. |
| 379 | */ |
| 380 | static int |
| 381 | iso_week1_monday(int year) |
| 382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 384 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 385 | int first_weekday = (first_day + 6) % 7; |
| 386 | /* ordinal of closest Monday at or before 1/1 */ |
| 387 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 390 | week1_monday += 7; |
| 391 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* --------------------------------------------------------------------------- |
| 395 | * Range checkers. |
| 396 | */ |
| 397 | |
| 398 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 399 | * If not, raise OverflowError and return -1. |
| 400 | */ |
| 401 | static int |
| 402 | check_delta_day_range(int days) |
| 403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 405 | return 0; |
| 406 | PyErr_Format(PyExc_OverflowError, |
| 407 | "days=%d; must have magnitude <= %d", |
| 408 | days, MAX_DELTA_DAYS); |
| 409 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 413 | * aren't, raise ValueError and return -1. |
| 414 | */ |
| 415 | static int |
| 416 | check_date_args(int year, int month, int day) |
| 417 | { |
| 418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | if (year < MINYEAR || year > MAXYEAR) { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 420 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | return -1; |
| 422 | } |
| 423 | if (month < 1 || month > 12) { |
| 424 | PyErr_SetString(PyExc_ValueError, |
| 425 | "month must be in 1..12"); |
| 426 | return -1; |
| 427 | } |
| 428 | if (day < 1 || day > days_in_month(year, month)) { |
| 429 | PyErr_SetString(PyExc_ValueError, |
| 430 | "day is out of range for month"); |
| 431 | return -1; |
| 432 | } |
| 433 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 437 | * aren't, raise ValueError and return -1. |
| 438 | */ |
| 439 | static int |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 440 | 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] | 441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | if (h < 0 || h > 23) { |
| 443 | PyErr_SetString(PyExc_ValueError, |
| 444 | "hour must be in 0..23"); |
| 445 | return -1; |
| 446 | } |
| 447 | if (m < 0 || m > 59) { |
| 448 | PyErr_SetString(PyExc_ValueError, |
| 449 | "minute must be in 0..59"); |
| 450 | return -1; |
| 451 | } |
| 452 | if (s < 0 || s > 59) { |
| 453 | PyErr_SetString(PyExc_ValueError, |
| 454 | "second must be in 0..59"); |
| 455 | return -1; |
| 456 | } |
| 457 | if (us < 0 || us > 999999) { |
| 458 | PyErr_SetString(PyExc_ValueError, |
| 459 | "microsecond must be in 0..999999"); |
| 460 | return -1; |
| 461 | } |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 462 | if (fold != 0 && fold != 1) { |
| 463 | PyErr_SetString(PyExc_ValueError, |
| 464 | "fold must be either 0 or 1"); |
| 465 | return -1; |
| 466 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* --------------------------------------------------------------------------- |
| 471 | * Normalization utilities. |
| 472 | */ |
| 473 | |
| 474 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 475 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 476 | * at least factor, enough of *lo is converted into "hi" units so that |
| 477 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 478 | * is impossible. |
| 479 | */ |
| 480 | static void |
| 481 | normalize_pair(int *hi, int *lo, int factor) |
| 482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | assert(factor > 0); |
| 484 | assert(lo != hi); |
| 485 | if (*lo < 0 || *lo >= factor) { |
| 486 | const int num_hi = divmod(*lo, factor, lo); |
| 487 | const int new_hi = *hi + num_hi; |
| 488 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 489 | *hi = new_hi; |
| 490 | } |
| 491 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | * 0 <= *s < 24*3600 |
| 496 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 497 | * The input values must be such that the internals don't overflow. |
| 498 | * The way this routine is used, we don't get close. |
| 499 | */ |
| 500 | static void |
| 501 | normalize_d_s_us(int *d, int *s, int *us) |
| 502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | if (*us < 0 || *us >= 1000000) { |
| 504 | normalize_pair(s, us, 1000000); |
| 505 | /* |s| can't be bigger than about |
| 506 | * |original s| + |original us|/1000000 now. |
| 507 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | } |
| 510 | if (*s < 0 || *s >= 24*3600) { |
| 511 | normalize_pair(d, s, 24*3600); |
| 512 | /* |d| can't be bigger than about |
| 513 | * |original d| + |
| 514 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 515 | */ |
| 516 | } |
| 517 | assert(0 <= *s && *s < 24*3600); |
| 518 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | * 1 <= *m <= 12 |
| 523 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 524 | * The input values must be such that the internals don't overflow. |
| 525 | * The way this routine is used, we don't get close. |
| 526 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 527 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 528 | normalize_y_m_d(int *y, int *m, int *d) |
| 529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 531 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 532 | /* In actual use, m is always the month component extracted from a |
| 533 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | /* Now only day can be out of bounds (year may also be out of bounds |
| 539 | * for a datetime object, but we don't care about that here). |
| 540 | * If day is out of bounds, what to do is arguable, but at least the |
| 541 | * method here is principled and explainable. |
| 542 | */ |
| 543 | dim = days_in_month(*y, *m); |
| 544 | if (*d < 1 || *d > dim) { |
| 545 | /* Move day-1 days from the first of the month. First try to |
| 546 | * get off cheap if we're only one day out of range |
| 547 | * (adjustments for timezone alone can't be worse than that). |
| 548 | */ |
| 549 | if (*d == 0) { |
| 550 | --*m; |
| 551 | if (*m > 0) |
| 552 | *d = days_in_month(*y, *m); |
| 553 | else { |
| 554 | --*y; |
| 555 | *m = 12; |
| 556 | *d = 31; |
| 557 | } |
| 558 | } |
| 559 | else if (*d == dim + 1) { |
| 560 | /* move forward a day */ |
| 561 | ++*m; |
| 562 | *d = 1; |
| 563 | if (*m > 12) { |
| 564 | *m = 1; |
| 565 | ++*y; |
| 566 | } |
| 567 | } |
| 568 | else { |
| 569 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 570 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 571 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 572 | goto error; |
| 573 | } else { |
| 574 | ord_to_ymd(ordinal, y, m, d); |
| 575 | return 0; |
| 576 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | assert(*m > 0); |
| 580 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 581 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 582 | return 0; |
| 583 | error: |
| 584 | PyErr_SetString(PyExc_OverflowError, |
| 585 | "date value out of range"); |
| 586 | return -1; |
| 587 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 591 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 592 | * failure, where failure means the adjusted year is out of bounds. |
| 593 | */ |
| 594 | static int |
| 595 | normalize_date(int *year, int *month, int *day) |
| 596 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 597 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* Force all the datetime fields into range. The parameters are both |
| 601 | * inputs and outputs. Returns < 0 on error. |
| 602 | */ |
| 603 | static int |
| 604 | normalize_datetime(int *year, int *month, int *day, |
| 605 | int *hour, int *minute, int *second, |
| 606 | int *microsecond) |
| 607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | normalize_pair(second, microsecond, 1000000); |
| 609 | normalize_pair(minute, second, 60); |
| 610 | normalize_pair(hour, minute, 60); |
| 611 | normalize_pair(day, hour, 24); |
| 612 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 616 | * Basic object allocation: tp_alloc implementations. These allocate |
| 617 | * Python objects of the right size and type, and do the Python object- |
| 618 | * initialization bit. If there's not enough memory, they return NULL after |
| 619 | * setting MemoryError. All data members remain uninitialized trash. |
| 620 | * |
| 621 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 622 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 623 | * tp_basicsize for the time and datetime types is set to the size of the |
| 624 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 625 | * allocate enough space for a tzinfo member whether or not one is actually |
| 626 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 627 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 628 | * using) just happens today to effectively ignore the nitems argument |
| 629 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 630 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 631 | * be changed to force a 0 nitems argument unless the type being allocated |
| 632 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 633 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 634 | */ |
| 635 | |
| 636 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 637 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 638 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | PyObject *self; |
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 | self = (PyObject *) |
| 642 | PyObject_MALLOC(aware ? |
| 643 | sizeof(PyDateTime_Time) : |
| 644 | sizeof(_PyDateTime_BaseTime)); |
| 645 | if (self == NULL) |
| 646 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 647 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 652 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | PyObject *self; |
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 | self = (PyObject *) |
| 657 | PyObject_MALLOC(aware ? |
| 658 | sizeof(PyDateTime_DateTime) : |
| 659 | sizeof(_PyDateTime_BaseDateTime)); |
| 660 | if (self == NULL) |
| 661 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 662 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* --------------------------------------------------------------------------- |
| 667 | * Helpers for setting object fields. These work on pointers to the |
| 668 | * appropriate base class. |
| 669 | */ |
| 670 | |
| 671 | /* For date and datetime. */ |
| 672 | static void |
| 673 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | self->hashcode = -1; |
| 676 | SET_YEAR(self, y); |
| 677 | SET_MONTH(self, m); |
| 678 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* --------------------------------------------------------------------------- |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 682 | * String parsing utilities and helper functions |
| 683 | */ |
| 684 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 685 | static const char * |
| 686 | parse_digits(const char *ptr, int *var, size_t num_digits) |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 687 | { |
| 688 | for (size_t i = 0; i < num_digits; ++i) { |
| 689 | unsigned int tmp = (unsigned int)(*(ptr++) - '0'); |
| 690 | if (tmp > 9) { |
| 691 | return NULL; |
| 692 | } |
| 693 | *var *= 10; |
| 694 | *var += (signed int)tmp; |
| 695 | } |
| 696 | |
| 697 | return ptr; |
| 698 | } |
| 699 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 700 | static int |
| 701 | parse_isoformat_date(const char *dtstr, int *year, int *month, int *day) |
| 702 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 703 | /* Parse the date components of the result of date.isoformat() |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 704 | * |
| 705 | * Return codes: |
| 706 | * 0: Success |
| 707 | * -1: Failed to parse date component |
| 708 | * -2: Failed to parse dateseparator |
| 709 | */ |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 710 | const char *p = dtstr; |
| 711 | p = parse_digits(p, year, 4); |
| 712 | if (NULL == p) { |
| 713 | return -1; |
| 714 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 715 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 716 | if (*(p++) != '-') { |
| 717 | return -2; |
| 718 | } |
| 719 | |
| 720 | p = parse_digits(p, month, 2); |
| 721 | if (NULL == p) { |
| 722 | return -1; |
| 723 | } |
| 724 | |
| 725 | if (*(p++) != '-') { |
| 726 | return -2; |
| 727 | } |
| 728 | |
| 729 | p = parse_digits(p, day, 2); |
| 730 | if (p == NULL) { |
| 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static int |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 738 | parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour, |
| 739 | int *minute, int *second, int *microsecond) |
| 740 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 741 | const char *p = tstr; |
| 742 | const char *p_end = tstr_end; |
| 743 | int *vals[3] = {hour, minute, second}; |
| 744 | |
| 745 | // Parse [HH[:MM[:SS]]] |
| 746 | for (size_t i = 0; i < 3; ++i) { |
| 747 | p = parse_digits(p, vals[i], 2); |
| 748 | if (NULL == p) { |
| 749 | return -3; |
| 750 | } |
| 751 | |
| 752 | char c = *(p++); |
| 753 | if (p >= p_end) { |
| 754 | return c != '\0'; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 755 | } |
| 756 | else if (c == ':') { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 757 | continue; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 758 | } |
| 759 | else if (c == '.') { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 760 | break; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 761 | } |
| 762 | else { |
| 763 | return -4; // Malformed time separator |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| 767 | // Parse .fff[fff] |
| 768 | size_t len_remains = p_end - p; |
| 769 | if (!(len_remains == 6 || len_remains == 3)) { |
| 770 | return -3; |
| 771 | } |
| 772 | |
| 773 | p = parse_digits(p, microsecond, len_remains); |
| 774 | if (NULL == p) { |
| 775 | return -3; |
| 776 | } |
| 777 | |
| 778 | if (len_remains == 3) { |
| 779 | *microsecond *= 1000; |
| 780 | } |
| 781 | |
| 782 | // Return 1 if it's not the end of the string |
| 783 | return *p != '\0'; |
| 784 | } |
| 785 | |
| 786 | static int |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 787 | parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute, |
| 788 | int *second, int *microsecond, int *tzoffset, |
| 789 | int *tzmicrosecond) |
| 790 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 791 | // Parse the time portion of a datetime.isoformat() string |
| 792 | // |
| 793 | // Return codes: |
| 794 | // 0: Success (no tzoffset) |
| 795 | // 1: Success (with tzoffset) |
| 796 | // -3: Failed to parse time component |
| 797 | // -4: Failed to parse time separator |
| 798 | // -5: Malformed timezone string |
| 799 | |
| 800 | const char *p = dtstr; |
| 801 | const char *p_end = dtstr + dtlen; |
| 802 | |
| 803 | const char *tzinfo_pos = p; |
| 804 | do { |
| 805 | if (*tzinfo_pos == '+' || *tzinfo_pos == '-') { |
| 806 | break; |
| 807 | } |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 808 | } while (++tzinfo_pos < p_end); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 809 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 810 | int rv = parse_hh_mm_ss_ff(dtstr, tzinfo_pos, hour, minute, second, |
| 811 | microsecond); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 812 | |
| 813 | if (rv < 0) { |
| 814 | return rv; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 815 | } |
| 816 | else if (tzinfo_pos == p_end) { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 817 | // We know that there's no time zone, so if there's stuff at the |
| 818 | // end of the string it's an error. |
| 819 | if (rv == 1) { |
| 820 | return -5; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 821 | } |
| 822 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | // Parse time zone component |
| 828 | // Valid formats are: |
| 829 | // - +HH:MM (len 6) |
| 830 | // - +HH:MM:SS (len 9) |
| 831 | // - +HH:MM:SS.ffffff (len 16) |
| 832 | size_t tzlen = p_end - tzinfo_pos; |
| 833 | if (!(tzlen == 6 || tzlen == 9 || tzlen == 16)) { |
| 834 | return -5; |
| 835 | } |
| 836 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 837 | int tzsign = (*tzinfo_pos == '-') ? -1 : 1; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 838 | tzinfo_pos++; |
| 839 | int tzhour = 0, tzminute = 0, tzsecond = 0; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 840 | rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, &tzhour, &tzminute, &tzsecond, |
| 841 | tzmicrosecond); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 842 | |
| 843 | *tzoffset = tzsign * ((tzhour * 3600) + (tzminute * 60) + tzsecond); |
| 844 | *tzmicrosecond *= tzsign; |
| 845 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 846 | return rv ? -5 : 1; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 847 | } |
| 848 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 849 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 850 | * Create various objects, mostly without range checking. |
| 851 | */ |
| 852 | |
| 853 | /* Create a date instance with no range checking. */ |
| 854 | static PyObject * |
| 855 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 858 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 859 | if (check_date_args(year, month, day) < 0) { |
| 860 | return NULL; |
| 861 | } |
| 862 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 863 | self = (PyDateTime_Date *)(type->tp_alloc(type, 0)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | if (self != NULL) |
| 865 | set_date_fields(self, year, month, day); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 866 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 871 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 872 | // Forward declaration |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 873 | static PyObject * |
| 874 | new_datetime_ex(int, int, int, int, int, int, int, PyObject *, PyTypeObject *); |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 875 | |
| 876 | /* Create date instance with no range checking, or call subclass constructor */ |
| 877 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 878 | new_date_subclass_ex(int year, int month, int day, PyObject *cls) |
| 879 | { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 880 | PyObject *result; |
| 881 | // We have "fast path" constructors for two subclasses: date and datetime |
| 882 | if ((PyTypeObject *)cls == &PyDateTime_DateType) { |
| 883 | result = new_date_ex(year, month, day, (PyTypeObject *)cls); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 884 | } |
| 885 | else if ((PyTypeObject *)cls == &PyDateTime_DateTimeType) { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 886 | result = new_datetime_ex(year, month, day, 0, 0, 0, 0, Py_None, |
| 887 | (PyTypeObject *)cls); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 888 | } |
| 889 | else { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 890 | result = PyObject_CallFunction(cls, "iii", year, month, day); |
| 891 | } |
| 892 | |
| 893 | return result; |
| 894 | } |
| 895 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 896 | /* Create a datetime instance with no range checking. */ |
| 897 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 898 | new_datetime_ex2(int year, int month, int day, int hour, int minute, |
| 899 | int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | PyDateTime_DateTime *self; |
| 902 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 903 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 904 | if (check_date_args(year, month, day) < 0) { |
| 905 | return NULL; |
| 906 | } |
| 907 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 908 | return NULL; |
| 909 | } |
| 910 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 911 | return NULL; |
| 912 | } |
| 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 915 | if (self != NULL) { |
| 916 | self->hastzinfo = aware; |
| 917 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 918 | DATE_SET_HOUR(self, hour); |
| 919 | DATE_SET_MINUTE(self, minute); |
| 920 | DATE_SET_SECOND(self, second); |
| 921 | DATE_SET_MICROSECOND(self, usecond); |
| 922 | if (aware) { |
| 923 | Py_INCREF(tzinfo); |
| 924 | self->tzinfo = tzinfo; |
| 925 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 926 | DATE_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | } |
| 928 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 931 | static PyObject * |
| 932 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
| 933 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
| 934 | { |
| 935 | return new_datetime_ex2(year, month, day, hour, minute, second, usecond, |
| 936 | tzinfo, 0, type); |
| 937 | } |
| 938 | |
| 939 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ |
| 940 | new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 942 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 943 | static PyObject * |
| 944 | new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute, |
| 945 | int second, int usecond, PyObject *tzinfo, |
| 946 | int fold, PyObject *cls) { |
| 947 | PyObject* dt; |
| 948 | if ((PyTypeObject*)cls == &PyDateTime_DateTimeType) { |
| 949 | // Use the fast path constructor |
| 950 | dt = new_datetime(year, month, day, hour, minute, second, usecond, |
| 951 | tzinfo, fold); |
| 952 | } else { |
| 953 | // Subclass |
| 954 | dt = PyObject_CallFunction(cls, "iiiiiiiO", |
| 955 | year, |
| 956 | month, |
| 957 | day, |
| 958 | hour, |
| 959 | minute, |
| 960 | second, |
| 961 | usecond, |
| 962 | tzinfo); |
| 963 | } |
| 964 | |
| 965 | return dt; |
| 966 | } |
| 967 | |
| 968 | static PyObject * |
| 969 | new_datetime_subclass_ex(int year, int month, int day, int hour, int minute, |
| 970 | int second, int usecond, PyObject *tzinfo, |
| 971 | PyObject *cls) { |
| 972 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 973 | second, usecond, tzinfo, 0, |
| 974 | cls); |
| 975 | } |
| 976 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 977 | /* Create a time instance with no range checking. */ |
| 978 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 979 | new_time_ex2(int hour, int minute, int second, int usecond, |
| 980 | PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | PyDateTime_Time *self; |
| 983 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 984 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 985 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 986 | return NULL; |
| 987 | } |
| 988 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 989 | return NULL; |
| 990 | } |
| 991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 993 | if (self != NULL) { |
| 994 | self->hastzinfo = aware; |
| 995 | self->hashcode = -1; |
| 996 | TIME_SET_HOUR(self, hour); |
| 997 | TIME_SET_MINUTE(self, minute); |
| 998 | TIME_SET_SECOND(self, second); |
| 999 | TIME_SET_MICROSECOND(self, usecond); |
| 1000 | if (aware) { |
| 1001 | Py_INCREF(tzinfo); |
| 1002 | self->tzinfo = tzinfo; |
| 1003 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1004 | TIME_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | } |
| 1006 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1009 | static PyObject * |
| 1010 | new_time_ex(int hour, int minute, int second, int usecond, |
| 1011 | PyObject *tzinfo, PyTypeObject *type) |
| 1012 | { |
| 1013 | return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); |
| 1014 | } |
| 1015 | |
| 1016 | #define new_time(hh, mm, ss, us, tzinfo, fold) \ |
| 1017 | new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1018 | |
| 1019 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 1020 | * true. Passing false is a speed optimization, if you know for sure |
| 1021 | * that seconds and microseconds are already in their proper ranges. In any |
| 1022 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 1023 | * of range). |
| 1024 | */ |
| 1025 | static PyObject * |
| 1026 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1028 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | PyDateTime_Delta *self; |
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 | if (normalize) |
| 1032 | normalize_d_s_us(&days, &seconds, µseconds); |
| 1033 | assert(0 <= seconds && seconds < 24*3600); |
| 1034 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | if (check_delta_day_range(days) < 0) |
| 1037 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 1040 | if (self != NULL) { |
| 1041 | self->hashcode = -1; |
| 1042 | SET_TD_DAYS(self, days); |
| 1043 | SET_TD_SECONDS(self, seconds); |
| 1044 | SET_TD_MICROSECONDS(self, microseconds); |
| 1045 | } |
| 1046 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | #define new_delta(d, s, us, normalize) \ |
| 1050 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1051 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1052 | |
| 1053 | typedef struct |
| 1054 | { |
| 1055 | PyObject_HEAD |
| 1056 | PyObject *offset; |
| 1057 | PyObject *name; |
| 1058 | } PyDateTime_TimeZone; |
| 1059 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1060 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1061 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 1062 | /* The interned Epoch datetime instance */ |
| 1063 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 1064 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1065 | /* Create new timezone instance checking offset range. This |
| 1066 | function does not check the name argument. Caller must assure |
| 1067 | that offset is a timedelta instance and name is either NULL |
| 1068 | or a unicode object. */ |
| 1069 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1070 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1071 | { |
| 1072 | PyDateTime_TimeZone *self; |
| 1073 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 1074 | |
| 1075 | assert(offset != NULL); |
| 1076 | assert(PyDelta_Check(offset)); |
| 1077 | assert(name == NULL || PyUnicode_Check(name)); |
| 1078 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1079 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 1080 | if (self == NULL) { |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | Py_INCREF(offset); |
| 1084 | self->offset = offset; |
| 1085 | Py_XINCREF(name); |
| 1086 | self->name = name; |
| 1087 | return (PyObject *)self; |
| 1088 | } |
| 1089 | |
| 1090 | static int delta_bool(PyDateTime_Delta *self); |
| 1091 | |
| 1092 | static PyObject * |
| 1093 | new_timezone(PyObject *offset, PyObject *name) |
| 1094 | { |
| 1095 | assert(offset != NULL); |
| 1096 | assert(PyDelta_Check(offset)); |
| 1097 | assert(name == NULL || PyUnicode_Check(name)); |
| 1098 | |
| 1099 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 1100 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1101 | return PyDateTime_TimeZone_UTC; |
| 1102 | } |
Ngalim Siregar | 92c7e30 | 2019-08-09 21:22:16 +0700 | [diff] [blame] | 1103 | if ((GET_TD_DAYS(offset) == -1 && |
| 1104 | GET_TD_SECONDS(offset) == 0 && |
| 1105 | GET_TD_MICROSECONDS(offset) < 1) || |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1106 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1107 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1108 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 1109 | " timedelta(hours=24)," |
| 1110 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1111 | return NULL; |
| 1112 | } |
| 1113 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1114 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1117 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1118 | * tzinfo helpers. |
| 1119 | */ |
| 1120 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1121 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 1122 | * raise TypeError and return -1. |
| 1123 | */ |
| 1124 | static int |
| 1125 | check_tzinfo_subclass(PyObject *p) |
| 1126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | if (p == Py_None || PyTZInfo_Check(p)) |
| 1128 | return 0; |
| 1129 | PyErr_Format(PyExc_TypeError, |
| 1130 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 1131 | "not type '%s'", |
| 1132 | Py_TYPE(p)->tp_name); |
| 1133 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1136 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 1137 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 1138 | * and the caller must not decref the result. |
| 1139 | */ |
| 1140 | static PyObject * |
| 1141 | get_tzinfo_member(PyObject *self) |
| 1142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 1146 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 1147 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 1148 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1153 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 1154 | * be an instance of the tzinfo class. If the method returns None, this |
| 1155 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 1156 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 1157 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 1158 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1159 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1160 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1161 | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1162 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1163 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1166 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1168 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1169 | if (tzinfo == Py_None) |
| 1170 | Py_RETURN_NONE; |
| 1171 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 1172 | if (offset == Py_None || offset == NULL) |
| 1173 | return offset; |
| 1174 | if (PyDelta_Check(offset)) { |
Ngalim Siregar | 92c7e30 | 2019-08-09 21:22:16 +0700 | [diff] [blame] | 1175 | if ((GET_TD_DAYS(offset) == -1 && |
| 1176 | GET_TD_SECONDS(offset) == 0 && |
| 1177 | GET_TD_MICROSECONDS(offset) < 1) || |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1178 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1179 | Py_DECREF(offset); |
| 1180 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1181 | " strictly between -timedelta(hours=24) and" |
| 1182 | " timedelta(hours=24)."); |
| 1183 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | else { |
| 1187 | PyErr_Format(PyExc_TypeError, |
| 1188 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1189 | "timedelta, not '%.200s'", |
| 1190 | name, Py_TYPE(offset)->tp_name); |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 1191 | Py_DECREF(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1192 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1194 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1195 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 1199 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 1200 | * 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] | 1201 | * 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] | 1202 | * If utcoffset() returns an out of range timedelta, |
| 1203 | * ValueError is raised and this returns -1. Else *none is |
| 1204 | * 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] | 1205 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1206 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1207 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 1208 | { |
| 1209 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1212 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 1213 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 1214 | * 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] | 1215 | * doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 1216 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 1217 | * 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] | 1218 | * the offset is returned (as timedelta, positive east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1219 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1220 | static PyObject * |
| 1221 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1222 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1223 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1226 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1227 | * 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] | 1228 | * 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] | 1229 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 1230 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1231 | */ |
| 1232 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1233 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1236 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | assert(tzinfo != NULL); |
| 1239 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 1240 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1243 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1244 | |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 1245 | result = _PyObject_CallMethodIdOneArg(tzinfo, &PyId_tzname, tzinfoarg); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (result == NULL || result == Py_None) |
| 1248 | return result; |
| 1249 | |
| 1250 | if (!PyUnicode_Check(result)) { |
| 1251 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 1252 | "return None or a string, not '%s'", |
| 1253 | Py_TYPE(result)->tp_name); |
| 1254 | Py_DECREF(result); |
| 1255 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1257 | |
| 1258 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1261 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 1262 | * stuff |
| 1263 | * ", tzinfo=" + repr(tzinfo) |
| 1264 | * before the closing ")". |
| 1265 | */ |
| 1266 | static PyObject * |
| 1267 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 1268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | assert(PyUnicode_Check(repr)); |
| 1272 | assert(tzinfo); |
| 1273 | if (tzinfo == Py_None) |
| 1274 | return repr; |
| 1275 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1276 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1277 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | Py_DECREF(repr); |
| 1279 | if (temp == NULL) |
| 1280 | return NULL; |
| 1281 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 1282 | Py_DECREF(temp); |
| 1283 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1286 | /* repr is like "someclass(arg1, arg2)". If fold isn't 0, |
| 1287 | * stuff |
| 1288 | * ", fold=" + repr(tzinfo) |
| 1289 | * before the closing ")". |
| 1290 | */ |
| 1291 | static PyObject * |
| 1292 | append_keyword_fold(PyObject *repr, int fold) |
| 1293 | { |
| 1294 | PyObject *temp; |
| 1295 | |
| 1296 | assert(PyUnicode_Check(repr)); |
| 1297 | if (fold == 0) |
| 1298 | return repr; |
| 1299 | /* Get rid of the trailing ')'. */ |
| 1300 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1301 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
| 1302 | Py_DECREF(repr); |
| 1303 | if (temp == NULL) |
| 1304 | return NULL; |
| 1305 | repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); |
| 1306 | Py_DECREF(temp); |
| 1307 | return repr; |
| 1308 | } |
| 1309 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1310 | static inline PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 1311 | tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds) |
| 1312 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1313 | PyObject *tzinfo; |
| 1314 | if (rv == 1) { |
| 1315 | // Create a timezone from offset in seconds (0 returns UTC) |
| 1316 | if (tzoffset == 0) { |
| 1317 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1318 | return PyDateTime_TimeZone_UTC; |
| 1319 | } |
| 1320 | |
| 1321 | PyObject *delta = new_delta(0, tzoffset, tz_useconds, 1); |
Alexey Izbyshev | 4988453 | 2018-08-24 18:53:16 +0300 | [diff] [blame] | 1322 | if (delta == NULL) { |
| 1323 | return NULL; |
| 1324 | } |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1325 | tzinfo = new_timezone(delta, NULL); |
Alexey Izbyshev | 4988453 | 2018-08-24 18:53:16 +0300 | [diff] [blame] | 1326 | Py_DECREF(delta); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 1327 | } |
| 1328 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1329 | tzinfo = Py_None; |
| 1330 | Py_INCREF(Py_None); |
| 1331 | } |
| 1332 | |
| 1333 | return tzinfo; |
| 1334 | } |
| 1335 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1336 | /* --------------------------------------------------------------------------- |
| 1337 | * String format helpers. |
| 1338 | */ |
| 1339 | |
| 1340 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1341 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1342 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1343 | static const char * const DayNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1345 | }; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1346 | static const char * const MonthNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1348 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1349 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
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 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1354 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1355 | GET_DAY(date), hours, minutes, seconds, |
| 1356 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1359 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1360 | |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1361 | /* Add formatted UTC offset string to buf. buf has no more than |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1362 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1363 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1364 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1365 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1366 | * string of the form |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1367 | * sign HH sep MM [sep SS [. UUUUUU]] |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1368 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1369 | * bogus, an appropriate exception is set and -1 is returned. |
| 1370 | */ |
| 1371 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1372 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1374 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1375 | PyObject *offset; |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1376 | int hours, minutes, seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1380 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1381 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1382 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1384 | if (offset == Py_None) { |
| 1385 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | *buf = '\0'; |
| 1387 | return 0; |
| 1388 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1389 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1390 | if (GET_TD_DAYS(offset) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | sign = '-'; |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 1392 | Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1393 | if (offset == NULL) |
| 1394 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1396 | else { |
| 1397 | sign = '+'; |
| 1398 | } |
| 1399 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1400 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1401 | seconds = GET_TD_SECONDS(offset); |
| 1402 | Py_DECREF(offset); |
| 1403 | minutes = divmod(seconds, 60, &seconds); |
| 1404 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1405 | if (microseconds) { |
| 1406 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d.%06d", sign, |
| 1407 | hours, sep, minutes, sep, seconds, microseconds); |
| 1408 | return 0; |
| 1409 | } |
| 1410 | if (seconds) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1411 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, |
| 1412 | sep, minutes, sep, seconds); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1413 | return 0; |
| 1414 | } |
| 1415 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1419 | static PyObject * |
| 1420 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1422 | PyObject *temp; |
| 1423 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1424 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1425 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | if (Zreplacement == NULL) |
| 1428 | return NULL; |
| 1429 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1430 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | assert(tzinfoarg != NULL); |
| 1433 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1434 | if (temp == NULL) |
| 1435 | goto Error; |
| 1436 | if (temp == Py_None) { |
| 1437 | Py_DECREF(temp); |
| 1438 | return Zreplacement; |
| 1439 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | assert(PyUnicode_Check(temp)); |
| 1442 | /* Since the tzname is getting stuffed into the |
| 1443 | * format, we have to double any % signs so that |
| 1444 | * strftime doesn't treat them as format codes. |
| 1445 | */ |
| 1446 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1447 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | Py_DECREF(temp); |
| 1449 | if (Zreplacement == NULL) |
| 1450 | return NULL; |
| 1451 | if (!PyUnicode_Check(Zreplacement)) { |
| 1452 | PyErr_SetString(PyExc_TypeError, |
| 1453 | "tzname.replace() did not return a string"); |
| 1454 | goto Error; |
| 1455 | } |
| 1456 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1457 | |
| 1458 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | Py_DECREF(Zreplacement); |
| 1460 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1463 | static PyObject * |
| 1464 | make_freplacement(PyObject *object) |
| 1465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | char freplacement[64]; |
| 1467 | if (PyTime_Check(object)) |
| 1468 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1469 | else if (PyDateTime_Check(object)) |
| 1470 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1471 | else |
| 1472 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1477 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1478 | * 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] | 1479 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1480 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1481 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1482 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1483 | */ |
| 1484 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1485 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | PyObject *result = NULL; /* guilty until proved innocent */ |
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 *zreplacement = NULL; /* py string, replacement for %z */ |
| 1491 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1492 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | const char *pin; /* pointer to next char in input format */ |
| 1495 | Py_ssize_t flen; /* length of input format */ |
| 1496 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1499 | char *pnew; /* pointer to available byte in output format */ |
| 1500 | size_t totalnew; /* number bytes total in output format buffer, |
| 1501 | exclusive of trailing \0 */ |
| 1502 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1505 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | assert(object && format && timetuple); |
| 1508 | assert(PyUnicode_Check(format)); |
| 1509 | /* Convert the input format to a C string and size */ |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1510 | pin = PyUnicode_AsUTF8AndSize(format, &flen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | if (!pin) |
| 1512 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1515 | * a new format. Since computing the replacements for those codes |
| 1516 | * is expensive, don't unless they're actually used. |
| 1517 | */ |
| 1518 | if (flen > INT_MAX - 1) { |
| 1519 | PyErr_NoMemory(); |
| 1520 | goto Done; |
| 1521 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1524 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1525 | if (newfmt == NULL) goto Done; |
| 1526 | pnew = PyBytes_AsString(newfmt); |
| 1527 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | while ((ch = *pin++) != '\0') { |
| 1530 | if (ch != '%') { |
| 1531 | ptoappend = pin - 1; |
| 1532 | ntoappend = 1; |
| 1533 | } |
| 1534 | else if ((ch = *pin++) == '\0') { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 1535 | /* Null byte follows %, copy only '%'. |
| 1536 | * |
MichaelSaah | 454b3d4 | 2019-01-14 05:23:39 -0500 | [diff] [blame] | 1537 | * Back the pin up one char so that we catch the null check |
| 1538 | * the next time through the loop.*/ |
| 1539 | pin--; |
| 1540 | ptoappend = pin - 1; |
| 1541 | ntoappend = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | } |
| 1543 | /* A % has been seen and ch is the character after it. */ |
| 1544 | else if (ch == 'z') { |
| 1545 | if (zreplacement == NULL) { |
| 1546 | /* format utcoffset */ |
| 1547 | char buf[100]; |
| 1548 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1549 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1550 | if (zreplacement == NULL) goto Done; |
| 1551 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1552 | assert(tzinfoarg != NULL); |
| 1553 | if (format_utcoffset(buf, |
| 1554 | sizeof(buf), |
| 1555 | "", |
| 1556 | tzinfo, |
| 1557 | tzinfoarg) < 0) |
| 1558 | goto Done; |
| 1559 | Py_DECREF(zreplacement); |
| 1560 | zreplacement = |
| 1561 | PyBytes_FromStringAndSize(buf, |
| 1562 | strlen(buf)); |
| 1563 | if (zreplacement == NULL) |
| 1564 | goto Done; |
| 1565 | } |
| 1566 | } |
| 1567 | assert(zreplacement != NULL); |
| 1568 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1569 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1570 | } |
| 1571 | else if (ch == 'Z') { |
| 1572 | /* format tzname */ |
| 1573 | if (Zreplacement == NULL) { |
| 1574 | Zreplacement = make_Zreplacement(object, |
| 1575 | tzinfoarg); |
| 1576 | if (Zreplacement == NULL) |
| 1577 | goto Done; |
| 1578 | } |
| 1579 | assert(Zreplacement != NULL); |
| 1580 | assert(PyUnicode_Check(Zreplacement)); |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1581 | ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1583 | if (ptoappend == NULL) |
| 1584 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | } |
| 1586 | else if (ch == 'f') { |
| 1587 | /* format microseconds */ |
| 1588 | if (freplacement == NULL) { |
| 1589 | freplacement = make_freplacement(object); |
| 1590 | if (freplacement == NULL) |
| 1591 | goto Done; |
| 1592 | } |
| 1593 | assert(freplacement != NULL); |
| 1594 | assert(PyBytes_Check(freplacement)); |
| 1595 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1596 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1597 | } |
| 1598 | else { |
| 1599 | /* percent followed by neither z nor Z */ |
| 1600 | ptoappend = pin - 2; |
| 1601 | ntoappend = 2; |
| 1602 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | /* Append the ntoappend chars starting at ptoappend to |
| 1605 | * the new format. |
| 1606 | */ |
| 1607 | if (ntoappend == 0) |
| 1608 | continue; |
| 1609 | assert(ptoappend != NULL); |
| 1610 | assert(ntoappend > 0); |
| 1611 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1612 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | PyErr_NoMemory(); |
| 1614 | goto Done; |
| 1615 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1616 | totalnew <<= 1; |
| 1617 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1620 | } |
| 1621 | memcpy(pnew, ptoappend, ntoappend); |
| 1622 | pnew += ntoappend; |
| 1623 | usednew += ntoappend; |
| 1624 | assert(usednew <= totalnew); |
| 1625 | } /* end while() */ |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1628 | goto Done; |
| 1629 | { |
| 1630 | PyObject *format; |
| 1631 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | if (time == NULL) |
| 1634 | goto Done; |
| 1635 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1636 | if (format != NULL) { |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1637 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime, |
| 1638 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | Py_DECREF(format); |
| 1640 | } |
| 1641 | Py_DECREF(time); |
| 1642 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1643 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | Py_XDECREF(freplacement); |
| 1645 | Py_XDECREF(zreplacement); |
| 1646 | Py_XDECREF(Zreplacement); |
| 1647 | Py_XDECREF(newfmt); |
| 1648 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1651 | /* --------------------------------------------------------------------------- |
| 1652 | * Wrap functions from the time module. These aren't directly available |
| 1653 | * from C. Perhaps they should be. |
| 1654 | */ |
| 1655 | |
| 1656 | /* Call time.time() and return its result (a Python float). */ |
| 1657 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1658 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1659 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | PyObject *result = NULL; |
| 1661 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1664 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1665 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1666 | result = _PyObject_CallMethodIdNoArgs(time, &PyId_time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | Py_DECREF(time); |
| 1668 | } |
| 1669 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1673 | * computed from the y,m,d args. |
| 1674 | */ |
| 1675 | static PyObject * |
| 1676 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1677 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | PyObject *time; |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1679 | PyObject *result; |
| 1680 | _Py_IDENTIFIER(struct_time); |
| 1681 | PyObject *args; |
| 1682 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | time = PyImport_ImportModuleNoBlock("time"); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1685 | if (time == NULL) { |
| 1686 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | } |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1688 | |
| 1689 | args = Py_BuildValue("iiiiiiiii", |
| 1690 | y, m, d, |
| 1691 | hh, mm, ss, |
| 1692 | weekday(y, m, d), |
| 1693 | days_before_month(y, m) + d, |
| 1694 | dstflag); |
| 1695 | if (args == NULL) { |
| 1696 | Py_DECREF(time); |
| 1697 | return NULL; |
| 1698 | } |
| 1699 | |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 1700 | result = _PyObject_CallMethodIdOneArg(time, &PyId_struct_time, args); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1701 | Py_DECREF(time); |
Victor Stinner | ddc120f | 2016-12-09 15:35:40 +0100 | [diff] [blame] | 1702 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* --------------------------------------------------------------------------- |
| 1707 | * Miscellaneous helpers. |
| 1708 | */ |
| 1709 | |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1710 | /* The comparisons here all most naturally compute a cmp()-like result. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1711 | * This little helper turns that into a bool result for rich comparisons. |
| 1712 | */ |
| 1713 | static PyObject * |
| 1714 | diff_to_bool(int diff, int op) |
| 1715 | { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1716 | Py_RETURN_RICHCOMPARE(diff, 0, op); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1719 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1720 | static PyObject * |
| 1721 | cmperror(PyObject *a, PyObject *b) |
| 1722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | PyErr_Format(PyExc_TypeError, |
| 1724 | "can't compare %s to %s", |
| 1725 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1726 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1729 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1730 | * Cached Python objects; these are set by the module init function. |
| 1731 | */ |
| 1732 | |
| 1733 | /* Conversion factors. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1735 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1736 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1737 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1738 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1739 | 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] | 1740 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1741 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1742 | /* --------------------------------------------------------------------------- |
| 1743 | * Class implementations. |
| 1744 | */ |
| 1745 | |
| 1746 | /* |
| 1747 | * PyDateTime_Delta implementation. |
| 1748 | */ |
| 1749 | |
| 1750 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1752 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1753 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1754 | * due to ubiquitous overflow possibilities. |
| 1755 | */ |
| 1756 | static PyObject * |
| 1757 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | PyObject *x1 = NULL; |
| 1760 | PyObject *x2 = NULL; |
| 1761 | PyObject *x3 = NULL; |
| 1762 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1765 | if (x1 == NULL) |
| 1766 | goto Done; |
| 1767 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1768 | if (x2 == NULL) |
| 1769 | goto Done; |
| 1770 | Py_DECREF(x1); |
| 1771 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | /* x2 has days in seconds */ |
| 1774 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1775 | if (x1 == NULL) |
| 1776 | goto Done; |
| 1777 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1778 | if (x3 == NULL) |
| 1779 | goto Done; |
| 1780 | Py_DECREF(x1); |
| 1781 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1782 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | /* x3 has days+seconds in seconds */ |
| 1785 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1786 | if (x1 == NULL) |
| 1787 | goto Done; |
| 1788 | Py_DECREF(x3); |
| 1789 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | /* x1 has days+seconds in us */ |
| 1792 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1793 | if (x2 == NULL) |
| 1794 | goto Done; |
| 1795 | result = PyNumber_Add(x1, x2); |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 1796 | assert(result == NULL || PyLong_CheckExact(result)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1797 | |
| 1798 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | Py_XDECREF(x1); |
| 1800 | Py_XDECREF(x2); |
| 1801 | Py_XDECREF(x3); |
| 1802 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1805 | static PyObject * |
| 1806 | checked_divmod(PyObject *a, PyObject *b) |
| 1807 | { |
| 1808 | PyObject *result = PyNumber_Divmod(a, b); |
| 1809 | if (result != NULL) { |
| 1810 | if (!PyTuple_Check(result)) { |
| 1811 | PyErr_Format(PyExc_TypeError, |
| 1812 | "divmod() returned non-tuple (type %.200s)", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 1813 | Py_TYPE(result)->tp_name); |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1814 | Py_DECREF(result); |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | if (PyTuple_GET_SIZE(result) != 2) { |
| 1818 | PyErr_Format(PyExc_TypeError, |
| 1819 | "divmod() returned a tuple of size %zd", |
| 1820 | PyTuple_GET_SIZE(result)); |
| 1821 | Py_DECREF(result); |
| 1822 | return NULL; |
| 1823 | } |
| 1824 | } |
| 1825 | return result; |
| 1826 | } |
| 1827 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1828 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1829 | */ |
| 1830 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1831 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | int us; |
| 1834 | int s; |
| 1835 | int d; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | PyObject *tuple = NULL; |
| 1838 | PyObject *num = NULL; |
| 1839 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1840 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1841 | tuple = checked_divmod(pyus, us_per_second); |
| 1842 | if (tuple == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | goto Done; |
| 1844 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1845 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1846 | num = PyTuple_GET_ITEM(tuple, 1); /* us */ |
| 1847 | us = _PyLong_AsInt(num); |
| 1848 | num = NULL; |
| 1849 | if (us == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | goto Done; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1851 | } |
| 1852 | if (!(0 <= us && us < 1000000)) { |
| 1853 | goto BadDivmod; |
| 1854 | } |
| 1855 | |
| 1856 | num = PyTuple_GET_ITEM(tuple, 0); /* leftover seconds */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | Py_INCREF(num); |
| 1858 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1859 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1860 | tuple = checked_divmod(num, seconds_per_day); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | if (tuple == NULL) |
| 1862 | goto Done; |
| 1863 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1864 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1865 | num = PyTuple_GET_ITEM(tuple, 1); /* seconds */ |
| 1866 | s = _PyLong_AsInt(num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | num = NULL; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1868 | if (s == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | goto Done; |
| 1870 | } |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1871 | if (!(0 <= s && s < 24*3600)) { |
| 1872 | goto BadDivmod; |
| 1873 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1874 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1875 | num = PyTuple_GET_ITEM(tuple, 0); /* leftover days */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | Py_INCREF(num); |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1877 | d = _PyLong_AsInt(num); |
| 1878 | if (d == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | goto Done; |
| 1880 | } |
| 1881 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1882 | |
| 1883 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1884 | Py_XDECREF(tuple); |
| 1885 | Py_XDECREF(num); |
| 1886 | return result; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1887 | |
| 1888 | BadDivmod: |
| 1889 | PyErr_SetString(PyExc_TypeError, |
| 1890 | "divmod() returned a value out of range"); |
| 1891 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | #define microseconds_to_delta(pymicros) \ |
| 1895 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1896 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1897 | static PyObject * |
| 1898 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | PyObject *pyus_in; |
| 1901 | PyObject *pyus_out; |
| 1902 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1904 | pyus_in = delta_to_microseconds(delta); |
| 1905 | if (pyus_in == NULL) |
| 1906 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1907 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1908 | pyus_out = PyNumber_Multiply(intobj, pyus_in); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | Py_DECREF(pyus_in); |
| 1910 | if (pyus_out == NULL) |
| 1911 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | result = microseconds_to_delta(pyus_out); |
| 1914 | Py_DECREF(pyus_out); |
| 1915 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | static PyObject * |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1919 | get_float_as_integer_ratio(PyObject *floatobj) |
| 1920 | { |
| 1921 | PyObject *ratio; |
| 1922 | |
| 1923 | assert(floatobj && PyFloat_Check(floatobj)); |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 1924 | ratio = _PyObject_CallMethodIdNoArgs(floatobj, &PyId_as_integer_ratio); |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1925 | if (ratio == NULL) { |
| 1926 | return NULL; |
| 1927 | } |
| 1928 | if (!PyTuple_Check(ratio)) { |
| 1929 | PyErr_Format(PyExc_TypeError, |
| 1930 | "unexpected return type from as_integer_ratio(): " |
| 1931 | "expected tuple, got '%.200s'", |
| 1932 | Py_TYPE(ratio)->tp_name); |
| 1933 | Py_DECREF(ratio); |
| 1934 | return NULL; |
| 1935 | } |
| 1936 | if (PyTuple_Size(ratio) != 2) { |
| 1937 | PyErr_SetString(PyExc_ValueError, |
| 1938 | "as_integer_ratio() must return a 2-tuple"); |
| 1939 | Py_DECREF(ratio); |
| 1940 | return NULL; |
| 1941 | } |
| 1942 | return ratio; |
| 1943 | } |
| 1944 | |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1945 | /* op is 0 for multiplication, 1 for division */ |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1946 | static PyObject * |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1947 | multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj, int op) |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1948 | { |
| 1949 | PyObject *result = NULL; |
| 1950 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1951 | PyObject *ratio = NULL; |
| 1952 | |
| 1953 | pyus_in = delta_to_microseconds(delta); |
| 1954 | if (pyus_in == NULL) |
| 1955 | return NULL; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1956 | ratio = get_float_as_integer_ratio(floatobj); |
| 1957 | if (ratio == NULL) { |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1958 | goto error; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1959 | } |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1960 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1961 | Py_DECREF(pyus_in); |
| 1962 | pyus_in = NULL; |
| 1963 | if (temp == NULL) |
| 1964 | goto error; |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1965 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1966 | Py_DECREF(temp); |
| 1967 | if (pyus_out == NULL) |
| 1968 | goto error; |
| 1969 | result = microseconds_to_delta(pyus_out); |
| 1970 | Py_DECREF(pyus_out); |
| 1971 | error: |
| 1972 | Py_XDECREF(pyus_in); |
| 1973 | Py_XDECREF(ratio); |
| 1974 | |
| 1975 | return result; |
| 1976 | } |
| 1977 | |
| 1978 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1979 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | PyObject *pyus_in; |
| 1982 | PyObject *pyus_out; |
| 1983 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1985 | pyus_in = delta_to_microseconds(delta); |
| 1986 | if (pyus_in == NULL) |
| 1987 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1990 | Py_DECREF(pyus_in); |
| 1991 | if (pyus_out == NULL) |
| 1992 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | result = microseconds_to_delta(pyus_out); |
| 1995 | Py_DECREF(pyus_out); |
| 1996 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2000 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 2001 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | PyObject *pyus_left; |
| 2003 | PyObject *pyus_right; |
| 2004 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | pyus_left = delta_to_microseconds(left); |
| 2007 | if (pyus_left == NULL) |
| 2008 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | pyus_right = delta_to_microseconds(right); |
| 2011 | if (pyus_right == NULL) { |
| 2012 | Py_DECREF(pyus_left); |
| 2013 | return NULL; |
| 2014 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 2017 | Py_DECREF(pyus_left); |
| 2018 | Py_DECREF(pyus_right); |
| 2019 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | static PyObject * |
| 2023 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 2024 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | PyObject *pyus_left; |
| 2026 | PyObject *pyus_right; |
| 2027 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | pyus_left = delta_to_microseconds(left); |
| 2030 | if (pyus_left == NULL) |
| 2031 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | pyus_right = delta_to_microseconds(right); |
| 2034 | if (pyus_right == NULL) { |
| 2035 | Py_DECREF(pyus_left); |
| 2036 | return NULL; |
| 2037 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 2040 | Py_DECREF(pyus_left); |
| 2041 | Py_DECREF(pyus_right); |
| 2042 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2046 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 2047 | { |
| 2048 | PyObject *result; |
| 2049 | PyObject *pyus_in, *pyus_out; |
| 2050 | pyus_in = delta_to_microseconds(delta); |
| 2051 | if (pyus_in == NULL) |
| 2052 | return NULL; |
| 2053 | pyus_out = divide_nearest(pyus_in, i); |
| 2054 | Py_DECREF(pyus_in); |
| 2055 | if (pyus_out == NULL) |
| 2056 | return NULL; |
| 2057 | result = microseconds_to_delta(pyus_out); |
| 2058 | Py_DECREF(pyus_out); |
| 2059 | |
| 2060 | return result; |
| 2061 | } |
| 2062 | |
| 2063 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2064 | delta_add(PyObject *left, PyObject *right) |
| 2065 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2069 | /* delta + delta */ |
| 2070 | /* The C-level additions can't overflow because of the |
| 2071 | * invariant bounds. |
| 2072 | */ |
| 2073 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 2074 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 2075 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 2076 | GET_TD_MICROSECONDS(right); |
| 2077 | result = new_delta(days, seconds, microseconds, 1); |
| 2078 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | if (result == Py_NotImplemented) |
| 2081 | Py_INCREF(result); |
| 2082 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | static PyObject * |
| 2086 | delta_negative(PyDateTime_Delta *self) |
| 2087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | return new_delta(-GET_TD_DAYS(self), |
| 2089 | -GET_TD_SECONDS(self), |
| 2090 | -GET_TD_MICROSECONDS(self), |
| 2091 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static PyObject * |
| 2095 | delta_positive(PyDateTime_Delta *self) |
| 2096 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | /* Could optimize this (by returning self) if this isn't a |
| 2098 | * subclass -- but who uses unary + ? Approximately nobody. |
| 2099 | */ |
| 2100 | return new_delta(GET_TD_DAYS(self), |
| 2101 | GET_TD_SECONDS(self), |
| 2102 | GET_TD_MICROSECONDS(self), |
| 2103 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | static PyObject * |
| 2107 | delta_abs(PyDateTime_Delta *self) |
| 2108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 2112 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | if (GET_TD_DAYS(self) < 0) |
| 2115 | result = delta_negative(self); |
| 2116 | else |
| 2117 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | static PyObject * |
| 2123 | delta_subtract(PyObject *left, PyObject *right) |
| 2124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2128 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 2129 | /* The C-level additions can't overflow because of the |
| 2130 | * invariant bounds. |
| 2131 | */ |
| 2132 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 2133 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 2134 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 2135 | GET_TD_MICROSECONDS(right); |
| 2136 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | if (result == Py_NotImplemented) |
| 2140 | Py_INCREF(result); |
| 2141 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2144 | static int |
| 2145 | delta_cmp(PyObject *self, PyObject *other) |
| 2146 | { |
| 2147 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 2148 | if (diff == 0) { |
| 2149 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 2150 | if (diff == 0) |
| 2151 | diff = GET_TD_MICROSECONDS(self) - |
| 2152 | GET_TD_MICROSECONDS(other); |
| 2153 | } |
| 2154 | return diff; |
| 2155 | } |
| 2156 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2157 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2158 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2161 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | return diff_to_bool(diff, op); |
| 2163 | } |
| 2164 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2165 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 2170 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2171 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2172 | delta_hash(PyDateTime_Delta *self) |
| 2173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | if (self->hashcode == -1) { |
| 2175 | PyObject *temp = delta_getstate(self); |
| 2176 | if (temp != NULL) { |
| 2177 | self->hashcode = PyObject_Hash(temp); |
| 2178 | Py_DECREF(temp); |
| 2179 | } |
| 2180 | } |
| 2181 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | static PyObject * |
| 2185 | delta_multiply(PyObject *left, PyObject *right) |
| 2186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | if (PyDelta_Check(left)) { |
| 2190 | /* delta * ??? */ |
| 2191 | if (PyLong_Check(right)) |
| 2192 | result = multiply_int_timedelta(right, |
| 2193 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2194 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2195 | result = multiply_truedivide_timedelta_float( |
| 2196 | (PyDateTime_Delta *) left, right, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | } |
| 2198 | else if (PyLong_Check(left)) |
| 2199 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2200 | (PyDateTime_Delta *) right); |
| 2201 | else if (PyFloat_Check(left)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2202 | result = multiply_truedivide_timedelta_float( |
| 2203 | (PyDateTime_Delta *) right, left, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | if (result == Py_NotImplemented) |
| 2206 | Py_INCREF(result); |
| 2207 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | static PyObject * |
| 2211 | delta_divide(PyObject *left, PyObject *right) |
| 2212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2215 | if (PyDelta_Check(left)) { |
| 2216 | /* delta * ??? */ |
| 2217 | if (PyLong_Check(right)) |
| 2218 | result = divide_timedelta_int( |
| 2219 | (PyDateTime_Delta *)left, |
| 2220 | right); |
| 2221 | else if (PyDelta_Check(right)) |
| 2222 | result = divide_timedelta_timedelta( |
| 2223 | (PyDateTime_Delta *)left, |
| 2224 | (PyDateTime_Delta *)right); |
| 2225 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | if (result == Py_NotImplemented) |
| 2228 | Py_INCREF(result); |
| 2229 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2232 | static PyObject * |
| 2233 | delta_truedivide(PyObject *left, PyObject *right) |
| 2234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | if (PyDelta_Check(left)) { |
| 2238 | if (PyDelta_Check(right)) |
| 2239 | result = truedivide_timedelta_timedelta( |
| 2240 | (PyDateTime_Delta *)left, |
| 2241 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2242 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2243 | result = multiply_truedivide_timedelta_float( |
| 2244 | (PyDateTime_Delta *)left, right, 1); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2245 | else if (PyLong_Check(right)) |
| 2246 | result = truedivide_timedelta_int( |
| 2247 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | if (result == Py_NotImplemented) |
| 2251 | Py_INCREF(result); |
| 2252 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | static PyObject * |
| 2256 | delta_remainder(PyObject *left, PyObject *right) |
| 2257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | PyObject *pyus_left; |
| 2259 | PyObject *pyus_right; |
| 2260 | PyObject *pyus_remainder; |
| 2261 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2262 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2263 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2264 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2267 | if (pyus_left == NULL) |
| 2268 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2271 | if (pyus_right == NULL) { |
| 2272 | Py_DECREF(pyus_left); |
| 2273 | return NULL; |
| 2274 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 2277 | Py_DECREF(pyus_left); |
| 2278 | Py_DECREF(pyus_right); |
| 2279 | if (pyus_remainder == NULL) |
| 2280 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | remainder = microseconds_to_delta(pyus_remainder); |
| 2283 | Py_DECREF(pyus_remainder); |
| 2284 | if (remainder == NULL) |
| 2285 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | static PyObject * |
| 2291 | delta_divmod(PyObject *left, PyObject *right) |
| 2292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | PyObject *pyus_left; |
| 2294 | PyObject *pyus_right; |
| 2295 | PyObject *divmod; |
| 2296 | PyObject *delta; |
| 2297 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2298 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2299 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2300 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2303 | if (pyus_left == NULL) |
| 2304 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2307 | if (pyus_right == NULL) { |
| 2308 | Py_DECREF(pyus_left); |
| 2309 | return NULL; |
| 2310 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2311 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 2312 | divmod = checked_divmod(pyus_left, pyus_right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | Py_DECREF(pyus_left); |
| 2314 | Py_DECREF(pyus_right); |
| 2315 | if (divmod == NULL) |
| 2316 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 2319 | if (delta == NULL) { |
| 2320 | Py_DECREF(divmod); |
| 2321 | return NULL; |
| 2322 | } |
| 2323 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 2324 | Py_DECREF(delta); |
| 2325 | Py_DECREF(divmod); |
| 2326 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2329 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 2330 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 2331 | * so far, and there are factor microseconds per current unit, the number |
| 2332 | * of which is given by num. num * factor is added to sofar in a |
| 2333 | * numerically careful way, and that's the result. Any fractional |
| 2334 | * microseconds left over (this can happen if num is a float type) are |
| 2335 | * added into *leftover. |
| 2336 | * Note that there are many ways this can give an error (NULL) return. |
| 2337 | */ |
| 2338 | static PyObject * |
| 2339 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2340 | double *leftover) |
| 2341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | PyObject *prod; |
| 2343 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | assert(num != NULL); |
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 | if (PyLong_Check(num)) { |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 2348 | prod = PyNumber_Multiply(num, factor); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | if (prod == NULL) |
| 2350 | return NULL; |
| 2351 | sum = PyNumber_Add(sofar, prod); |
| 2352 | Py_DECREF(prod); |
| 2353 | return sum; |
| 2354 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | if (PyFloat_Check(num)) { |
| 2357 | double dnum; |
| 2358 | double fracpart; |
| 2359 | double intpart; |
| 2360 | PyObject *x; |
| 2361 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | /* The Plan: decompose num into an integer part and a |
| 2364 | * fractional part, num = intpart + fracpart. |
| 2365 | * Then num * factor == |
| 2366 | * intpart * factor + fracpart * factor |
| 2367 | * and the LHS can be computed exactly in long arithmetic. |
| 2368 | * The RHS is again broken into an int part and frac part. |
| 2369 | * and the frac part is added into *leftover. |
| 2370 | */ |
| 2371 | dnum = PyFloat_AsDouble(num); |
| 2372 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2373 | return NULL; |
| 2374 | fracpart = modf(dnum, &intpart); |
| 2375 | x = PyLong_FromDouble(intpart); |
| 2376 | if (x == NULL) |
| 2377 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | prod = PyNumber_Multiply(x, factor); |
| 2380 | Py_DECREF(x); |
| 2381 | if (prod == NULL) |
| 2382 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2384 | sum = PyNumber_Add(sofar, prod); |
| 2385 | Py_DECREF(prod); |
| 2386 | if (sum == NULL) |
| 2387 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2389 | if (fracpart == 0.0) |
| 2390 | return sum; |
| 2391 | /* So far we've lost no information. Dealing with the |
| 2392 | * fractional part requires float arithmetic, and may |
| 2393 | * lose a little info. |
| 2394 | */ |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2395 | assert(PyLong_CheckExact(factor)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | dnum *= fracpart; |
| 2399 | fracpart = modf(dnum, &intpart); |
| 2400 | x = PyLong_FromDouble(intpart); |
| 2401 | if (x == NULL) { |
| 2402 | Py_DECREF(sum); |
| 2403 | return NULL; |
| 2404 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | y = PyNumber_Add(sum, x); |
| 2407 | Py_DECREF(sum); |
| 2408 | Py_DECREF(x); |
| 2409 | *leftover += fracpart; |
| 2410 | return y; |
| 2411 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | PyErr_Format(PyExc_TypeError, |
| 2414 | "unsupported type for timedelta %s component: %s", |
| 2415 | tag, Py_TYPE(num)->tp_name); |
| 2416 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | static PyObject * |
| 2420 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | /* Argument objects. */ |
| 2425 | PyObject *day = NULL; |
| 2426 | PyObject *second = NULL; |
| 2427 | PyObject *us = NULL; |
| 2428 | PyObject *ms = NULL; |
| 2429 | PyObject *minute = NULL; |
| 2430 | PyObject *hour = NULL; |
| 2431 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2433 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2434 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2435 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | static char *keywords[] = { |
| 2438 | "days", "seconds", "microseconds", "milliseconds", |
| 2439 | "minutes", "hours", "weeks", NULL |
| 2440 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2442 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2443 | keywords, |
| 2444 | &day, &second, &us, |
| 2445 | &ms, &minute, &hour, &week) == 0) |
| 2446 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2448 | x = PyLong_FromLong(0); |
| 2449 | if (x == NULL) |
| 2450 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | #define CLEANUP \ |
| 2453 | Py_DECREF(x); \ |
| 2454 | x = y; \ |
| 2455 | if (x == NULL) \ |
| 2456 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | if (us) { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2459 | y = accum("microseconds", x, us, _PyLong_One, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | CLEANUP; |
| 2461 | } |
| 2462 | if (ms) { |
| 2463 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2464 | CLEANUP; |
| 2465 | } |
| 2466 | if (second) { |
| 2467 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2468 | CLEANUP; |
| 2469 | } |
| 2470 | if (minute) { |
| 2471 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2472 | CLEANUP; |
| 2473 | } |
| 2474 | if (hour) { |
| 2475 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2476 | CLEANUP; |
| 2477 | } |
| 2478 | if (day) { |
| 2479 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2480 | CLEANUP; |
| 2481 | } |
| 2482 | if (week) { |
| 2483 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2484 | CLEANUP; |
| 2485 | } |
| 2486 | if (leftover_us) { |
| 2487 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2488 | double whole_us = round(leftover_us); |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2489 | int x_is_odd; |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2490 | PyObject *temp; |
| 2491 | |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2492 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2493 | /* We're exactly halfway between two integers. In order |
| 2494 | * to do round-half-to-even, we must determine whether x |
| 2495 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2496 | * code below uses bitwise and operation to check the last |
| 2497 | * bit. */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2498 | temp = PyNumber_And(x, _PyLong_One); /* temp <- x & 1 */ |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2499 | if (temp == NULL) { |
| 2500 | Py_DECREF(x); |
| 2501 | goto Done; |
| 2502 | } |
| 2503 | x_is_odd = PyObject_IsTrue(temp); |
| 2504 | Py_DECREF(temp); |
| 2505 | if (x_is_odd == -1) { |
| 2506 | Py_DECREF(x); |
| 2507 | goto Done; |
| 2508 | } |
| 2509 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2510 | } |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2511 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2512 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2514 | if (temp == NULL) { |
| 2515 | Py_DECREF(x); |
| 2516 | goto Done; |
| 2517 | } |
| 2518 | y = PyNumber_Add(x, temp); |
| 2519 | Py_DECREF(temp); |
| 2520 | CLEANUP; |
| 2521 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | self = microseconds_to_delta_ex(x, type); |
| 2524 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2525 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2527 | |
| 2528 | #undef CLEANUP |
| 2529 | } |
| 2530 | |
| 2531 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2532 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2534 | return (GET_TD_DAYS(self) != 0 |
| 2535 | || GET_TD_SECONDS(self) != 0 |
| 2536 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | static PyObject * |
| 2540 | delta_repr(PyDateTime_Delta *self) |
| 2541 | { |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2542 | PyObject *args = PyUnicode_FromString(""); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2543 | |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2544 | if (args == NULL) { |
| 2545 | return NULL; |
| 2546 | } |
| 2547 | |
| 2548 | const char *sep = ""; |
| 2549 | |
| 2550 | if (GET_TD_DAYS(self) != 0) { |
| 2551 | Py_SETREF(args, PyUnicode_FromFormat("days=%d", GET_TD_DAYS(self))); |
| 2552 | if (args == NULL) { |
| 2553 | return NULL; |
| 2554 | } |
| 2555 | sep = ", "; |
| 2556 | } |
| 2557 | |
| 2558 | if (GET_TD_SECONDS(self) != 0) { |
| 2559 | Py_SETREF(args, PyUnicode_FromFormat("%U%sseconds=%d", args, sep, |
| 2560 | GET_TD_SECONDS(self))); |
| 2561 | if (args == NULL) { |
| 2562 | return NULL; |
| 2563 | } |
| 2564 | sep = ", "; |
| 2565 | } |
| 2566 | |
| 2567 | if (GET_TD_MICROSECONDS(self) != 0) { |
| 2568 | Py_SETREF(args, PyUnicode_FromFormat("%U%smicroseconds=%d", args, sep, |
| 2569 | GET_TD_MICROSECONDS(self))); |
| 2570 | if (args == NULL) { |
| 2571 | return NULL; |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | if (PyUnicode_GET_LENGTH(args) == 0) { |
| 2576 | Py_SETREF(args, PyUnicode_FromString("0")); |
| 2577 | if (args == NULL) { |
| 2578 | return NULL; |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | PyObject *repr = PyUnicode_FromFormat("%s(%S)", Py_TYPE(self)->tp_name, |
| 2583 | args); |
| 2584 | Py_DECREF(args); |
| 2585 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | static PyObject * |
| 2589 | delta_str(PyDateTime_Delta *self) |
| 2590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | int us = GET_TD_MICROSECONDS(self); |
| 2592 | int seconds = GET_TD_SECONDS(self); |
| 2593 | int minutes = divmod(seconds, 60, &seconds); |
| 2594 | int hours = divmod(minutes, 60, &minutes); |
| 2595 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | if (days) { |
| 2598 | if (us) |
| 2599 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2600 | days, (days == 1 || days == -1) ? "" : "s", |
| 2601 | hours, minutes, seconds, us); |
| 2602 | else |
| 2603 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2604 | days, (days == 1 || days == -1) ? "" : "s", |
| 2605 | hours, minutes, seconds); |
| 2606 | } else { |
| 2607 | if (us) |
| 2608 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2609 | hours, minutes, seconds, us); |
| 2610 | else |
| 2611 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2612 | hours, minutes, seconds); |
| 2613 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2614 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2617 | /* Pickle support, a simple use of __reduce__. */ |
| 2618 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2619 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2620 | static PyObject * |
| 2621 | delta_getstate(PyDateTime_Delta *self) |
| 2622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2623 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2624 | GET_TD_SECONDS(self), |
| 2625 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2628 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2629 | delta_total_seconds(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | PyObject *total_seconds; |
| 2632 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2635 | if (total_microseconds == NULL) |
| 2636 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2637 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2638 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2645 | delta_reduce(PyDateTime_Delta* self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2651 | |
| 2652 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | {"days", T_INT, OFFSET(days), READONLY, |
| 2655 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2658 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2661 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2662 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2663 | }; |
| 2664 | |
| 2665 | static PyMethodDef delta_methods[] = { |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2666 | {"total_seconds", delta_total_seconds, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2670 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2673 | }; |
| 2674 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2675 | static const char delta_doc[] = |
Chris Barker | d6a61f2 | 2018-10-19 15:43:24 -0700 | [diff] [blame] | 2676 | PyDoc_STR("Difference between two datetime values.\n\n" |
| 2677 | "timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, " |
| 2678 | "minutes=0, hours=0, weeks=0)\n\n" |
| 2679 | "All arguments are optional and default to 0.\n" |
| 2680 | "Arguments may be integers or floats, and may be positive or negative."); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2681 | |
| 2682 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | delta_add, /* nb_add */ |
| 2684 | delta_subtract, /* nb_subtract */ |
| 2685 | delta_multiply, /* nb_multiply */ |
| 2686 | delta_remainder, /* nb_remainder */ |
| 2687 | delta_divmod, /* nb_divmod */ |
| 2688 | 0, /* nb_power */ |
| 2689 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2690 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2691 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2692 | (inquiry)delta_bool, /* nb_bool */ |
| 2693 | 0, /*nb_invert*/ |
| 2694 | 0, /*nb_lshift*/ |
| 2695 | 0, /*nb_rshift*/ |
| 2696 | 0, /*nb_and*/ |
| 2697 | 0, /*nb_xor*/ |
| 2698 | 0, /*nb_or*/ |
| 2699 | 0, /*nb_int*/ |
| 2700 | 0, /*nb_reserved*/ |
| 2701 | 0, /*nb_float*/ |
| 2702 | 0, /*nb_inplace_add*/ |
| 2703 | 0, /*nb_inplace_subtract*/ |
| 2704 | 0, /*nb_inplace_multiply*/ |
| 2705 | 0, /*nb_inplace_remainder*/ |
| 2706 | 0, /*nb_inplace_power*/ |
| 2707 | 0, /*nb_inplace_lshift*/ |
| 2708 | 0, /*nb_inplace_rshift*/ |
| 2709 | 0, /*nb_inplace_and*/ |
| 2710 | 0, /*nb_inplace_xor*/ |
| 2711 | 0, /*nb_inplace_or*/ |
| 2712 | delta_divide, /* nb_floor_divide */ |
| 2713 | delta_truedivide, /* nb_true_divide */ |
| 2714 | 0, /* nb_inplace_floor_divide */ |
| 2715 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2716 | }; |
| 2717 | |
| 2718 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2720 | "datetime.timedelta", /* tp_name */ |
| 2721 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2722 | 0, /* tp_itemsize */ |
| 2723 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2724 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | 0, /* tp_getattr */ |
| 2726 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2727 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | (reprfunc)delta_repr, /* tp_repr */ |
| 2729 | &delta_as_number, /* tp_as_number */ |
| 2730 | 0, /* tp_as_sequence */ |
| 2731 | 0, /* tp_as_mapping */ |
| 2732 | (hashfunc)delta_hash, /* tp_hash */ |
| 2733 | 0, /* tp_call */ |
| 2734 | (reprfunc)delta_str, /* tp_str */ |
| 2735 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2736 | 0, /* tp_setattro */ |
| 2737 | 0, /* tp_as_buffer */ |
| 2738 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2739 | delta_doc, /* tp_doc */ |
| 2740 | 0, /* tp_traverse */ |
| 2741 | 0, /* tp_clear */ |
| 2742 | delta_richcompare, /* tp_richcompare */ |
| 2743 | 0, /* tp_weaklistoffset */ |
| 2744 | 0, /* tp_iter */ |
| 2745 | 0, /* tp_iternext */ |
| 2746 | delta_methods, /* tp_methods */ |
| 2747 | delta_members, /* tp_members */ |
| 2748 | 0, /* tp_getset */ |
| 2749 | 0, /* tp_base */ |
| 2750 | 0, /* tp_dict */ |
| 2751 | 0, /* tp_descr_get */ |
| 2752 | 0, /* tp_descr_set */ |
| 2753 | 0, /* tp_dictoffset */ |
| 2754 | 0, /* tp_init */ |
| 2755 | 0, /* tp_alloc */ |
| 2756 | delta_new, /* tp_new */ |
| 2757 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2758 | }; |
| 2759 | |
| 2760 | /* |
| 2761 | * PyDateTime_Date implementation. |
| 2762 | */ |
| 2763 | |
| 2764 | /* Accessor properties. */ |
| 2765 | |
| 2766 | static PyObject * |
| 2767 | date_year(PyDateTime_Date *self, void *unused) |
| 2768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | static PyObject * |
| 2773 | date_month(PyDateTime_Date *self, void *unused) |
| 2774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | static PyObject * |
| 2779 | date_day(PyDateTime_Date *self, void *unused) |
| 2780 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | {"year", (getter)date_year}, |
| 2786 | {"month", (getter)date_month}, |
| 2787 | {"day", (getter)date_day}, |
| 2788 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2789 | }; |
| 2790 | |
| 2791 | /* Constructors. */ |
| 2792 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2793 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2794 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2795 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2796 | date_from_pickle(PyTypeObject *type, PyObject *state) |
| 2797 | { |
| 2798 | PyDateTime_Date *me; |
| 2799 | |
| 2800 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2801 | if (me != NULL) { |
| 2802 | const char *pdata = PyBytes_AS_STRING(state); |
| 2803 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2804 | me->hashcode = -1; |
| 2805 | } |
| 2806 | return (PyObject *)me; |
| 2807 | } |
| 2808 | |
| 2809 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2810 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2811 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2812 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | int year; |
| 2814 | int month; |
| 2815 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2817 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 1133a8c | 2018-12-07 16:48:21 +0200 | [diff] [blame] | 2818 | if (PyTuple_GET_SIZE(args) == 1) { |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2819 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 2820 | if (PyBytes_Check(state)) { |
| 2821 | if (PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2822 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2823 | { |
| 2824 | return date_from_pickle(type, state); |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 2825 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2826 | } |
| 2827 | else if (PyUnicode_Check(state)) { |
| 2828 | if (PyUnicode_READY(state)) { |
| 2829 | return NULL; |
| 2830 | } |
| 2831 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATE_DATASIZE && |
| 2832 | MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2))) |
| 2833 | { |
| 2834 | state = PyUnicode_AsLatin1String(state); |
| 2835 | if (state == NULL) { |
| 2836 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 2837 | /* More informative error message. */ |
| 2838 | PyErr_SetString(PyExc_ValueError, |
| 2839 | "Failed to encode latin1 string when unpickling " |
| 2840 | "a date object. " |
| 2841 | "pickle.load(data, encoding='latin1') is assumed."); |
| 2842 | } |
| 2843 | return NULL; |
| 2844 | } |
| 2845 | self = date_from_pickle(type, state); |
| 2846 | Py_DECREF(state); |
| 2847 | return self; |
| 2848 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2853 | &year, &month, &day)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | self = new_date_ex(year, month, day, type); |
| 2855 | } |
| 2856 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2859 | static PyObject * |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2860 | date_fromtimestamp(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2861 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2862 | struct tm tm; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2864 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2865 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2867 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 2868 | if (_PyTime_localtime(t, &tm) != 0) |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2869 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2870 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2871 | return new_date_subclass_ex(tm.tm_year + 1900, |
| 2872 | tm.tm_mon + 1, |
| 2873 | tm.tm_mday, |
| 2874 | cls); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | /* Return new date from current time. |
| 2878 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2879 | * only way to be sure of that is to *call* time.time(). That's not |
| 2880 | * generally the same as calling C's time. |
| 2881 | */ |
| 2882 | static PyObject * |
| 2883 | date_today(PyObject *cls, PyObject *dummy) |
| 2884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | PyObject *time; |
| 2886 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2887 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | time = time_time(); |
| 2890 | if (time == NULL) |
| 2891 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | /* Note well: today() is a class method, so this may not call |
| 2894 | * date.fromtimestamp. For example, it may call |
| 2895 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2896 | * time.time() delivers; if someone were gonzo about optimization, |
| 2897 | * date.today() could get away with plain C time(). |
| 2898 | */ |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 2899 | result = _PyObject_CallMethodIdOneArg(cls, &PyId_fromtimestamp, time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | Py_DECREF(time); |
| 2901 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2904 | /*[clinic input] |
| 2905 | @classmethod |
| 2906 | datetime.date.fromtimestamp |
| 2907 | |
| 2908 | timestamp: object |
| 2909 | / |
| 2910 | |
| 2911 | Create a date from a POSIX timestamp. |
| 2912 | |
| 2913 | The timestamp is a number, e.g. created via time.time(), that is interpreted |
| 2914 | as local time. |
| 2915 | [clinic start generated code]*/ |
| 2916 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2917 | static PyObject * |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2918 | datetime_date_fromtimestamp(PyTypeObject *type, PyObject *timestamp) |
| 2919 | /*[clinic end generated code: output=fd045fda58168869 input=eabb3fe7f40491fe]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2920 | { |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2921 | return date_fromtimestamp((PyObject *) type, timestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2924 | /* bpo-36025: This is a wrapper for API compatibility with the public C API, |
| 2925 | * which expects a function that takes an *args tuple, whereas the argument |
| 2926 | * clinic generates code that takes METH_O. |
| 2927 | */ |
| 2928 | static PyObject * |
| 2929 | datetime_date_fromtimestamp_capi(PyObject *cls, PyObject *args) |
| 2930 | { |
| 2931 | PyObject *timestamp; |
| 2932 | PyObject *result = NULL; |
| 2933 | |
| 2934 | if (PyArg_UnpackTuple(args, "fromtimestamp", 1, 1, ×tamp)) { |
| 2935 | result = date_fromtimestamp(cls, timestamp); |
| 2936 | } |
| 2937 | |
| 2938 | return result; |
| 2939 | } |
| 2940 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2941 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2942 | * the ordinal is out of range. |
| 2943 | */ |
| 2944 | static PyObject * |
| 2945 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | PyObject *result = NULL; |
| 2948 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2950 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2951 | int year; |
| 2952 | int month; |
| 2953 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | if (ordinal < 1) |
| 2956 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2957 | ">= 1"); |
| 2958 | else { |
| 2959 | ord_to_ymd(ordinal, &year, &month, &day); |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2960 | result = new_date_subclass_ex(year, month, day, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | } |
| 2962 | } |
| 2963 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2966 | /* Return the new date from a string as generated by date.isoformat() */ |
| 2967 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2968 | date_fromisoformat(PyObject *cls, PyObject *dtstr) |
| 2969 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2970 | assert(dtstr != NULL); |
| 2971 | |
| 2972 | if (!PyUnicode_Check(dtstr)) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2973 | PyErr_SetString(PyExc_TypeError, |
| 2974 | "fromisoformat: argument must be str"); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2975 | return NULL; |
| 2976 | } |
| 2977 | |
| 2978 | Py_ssize_t len; |
| 2979 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2980 | const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 2981 | if (dt_ptr == NULL) { |
| 2982 | goto invalid_string_error; |
| 2983 | } |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2984 | |
| 2985 | int year = 0, month = 0, day = 0; |
| 2986 | |
| 2987 | int rv; |
| 2988 | if (len == 10) { |
| 2989 | rv = parse_isoformat_date(dt_ptr, &year, &month, &day); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2990 | } |
| 2991 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2992 | rv = -1; |
| 2993 | } |
| 2994 | |
| 2995 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 2996 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2997 | } |
| 2998 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2999 | return new_date_subclass_ex(year, month, day, cls); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 3000 | |
| 3001 | invalid_string_error: |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 3002 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 3003 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 3004 | } |
| 3005 | |
Paul Ganssle | 88c0937 | 2019-04-29 09:22:03 -0400 | [diff] [blame] | 3006 | |
| 3007 | static PyObject * |
| 3008 | date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) |
| 3009 | { |
| 3010 | static char *keywords[] = { |
| 3011 | "year", "week", "day", NULL |
| 3012 | }; |
| 3013 | |
| 3014 | int year, week, day; |
| 3015 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii:fromisocalendar", |
| 3016 | keywords, |
| 3017 | &year, &week, &day) == 0) { |
| 3018 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 3019 | PyErr_Format(PyExc_ValueError, |
| 3020 | "ISO calendar component out of range"); |
| 3021 | |
| 3022 | } |
| 3023 | return NULL; |
| 3024 | } |
| 3025 | |
| 3026 | // Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5) |
| 3027 | if (year < MINYEAR || year > MAXYEAR) { |
| 3028 | PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year); |
| 3029 | return NULL; |
| 3030 | } |
| 3031 | |
| 3032 | if (week <= 0 || week >= 53) { |
| 3033 | int out_of_range = 1; |
| 3034 | if (week == 53) { |
| 3035 | // ISO years have 53 weeks in it on years starting with a Thursday |
| 3036 | // and on leap years starting on Wednesday |
| 3037 | int first_weekday = weekday(year, 1, 1); |
| 3038 | if (first_weekday == 3 || (first_weekday == 2 && is_leap(year))) { |
| 3039 | out_of_range = 0; |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | if (out_of_range) { |
| 3044 | PyErr_Format(PyExc_ValueError, "Invalid week: %d", week); |
| 3045 | return NULL; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | if (day <= 0 || day >= 8) { |
| 3050 | PyErr_Format(PyExc_ValueError, "Invalid day: %d (range is [1, 7])", |
| 3051 | day); |
| 3052 | return NULL; |
| 3053 | } |
| 3054 | |
| 3055 | // Convert (Y, W, D) to (Y, M, D) in-place |
| 3056 | int day_1 = iso_week1_monday(year); |
| 3057 | |
| 3058 | int month = week; |
| 3059 | int day_offset = (month - 1)*7 + day - 1; |
| 3060 | |
| 3061 | ord_to_ymd(day_1 + day_offset, &year, &month, &day); |
| 3062 | |
| 3063 | return new_date_subclass_ex(year, month, day, cls); |
| 3064 | } |
| 3065 | |
| 3066 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3067 | /* |
| 3068 | * Date arithmetic. |
| 3069 | */ |
| 3070 | |
| 3071 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 3072 | * instead. |
| 3073 | */ |
| 3074 | static PyObject * |
| 3075 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 3076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | PyObject *result = NULL; |
| 3078 | int year = GET_YEAR(date); |
| 3079 | int month = GET_MONTH(date); |
| 3080 | int deltadays = GET_TD_DAYS(delta); |
| 3081 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 3082 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | if (normalize_date(&year, &month, &day) >= 0) |
Paul Ganssle | 89427cd | 2019-02-04 14:42:04 -0500 | [diff] [blame] | 3085 | result = new_date_subclass_ex(year, month, day, |
| 3086 | (PyObject* )Py_TYPE(date)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | static PyObject * |
| 3091 | date_add(PyObject *left, PyObject *right) |
| 3092 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3093 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 3094 | Py_RETURN_NOTIMPLEMENTED; |
| 3095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | if (PyDate_Check(left)) { |
| 3097 | /* date + ??? */ |
| 3098 | if (PyDelta_Check(right)) |
| 3099 | /* date + delta */ |
| 3100 | return add_date_timedelta((PyDateTime_Date *) left, |
| 3101 | (PyDateTime_Delta *) right, |
| 3102 | 0); |
| 3103 | } |
| 3104 | else { |
| 3105 | /* ??? + date |
| 3106 | * 'right' must be one of us, or we wouldn't have been called |
| 3107 | */ |
| 3108 | if (PyDelta_Check(left)) |
| 3109 | /* delta + date */ |
| 3110 | return add_date_timedelta((PyDateTime_Date *) right, |
| 3111 | (PyDateTime_Delta *) left, |
| 3112 | 0); |
| 3113 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3114 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | static PyObject * |
| 3118 | date_subtract(PyObject *left, PyObject *right) |
| 3119 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3120 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 3121 | Py_RETURN_NOTIMPLEMENTED; |
| 3122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3123 | if (PyDate_Check(left)) { |
| 3124 | if (PyDate_Check(right)) { |
| 3125 | /* date - date */ |
| 3126 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 3127 | GET_MONTH(left), |
| 3128 | GET_DAY(left)); |
| 3129 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 3130 | GET_MONTH(right), |
| 3131 | GET_DAY(right)); |
| 3132 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 3133 | } |
| 3134 | if (PyDelta_Check(right)) { |
| 3135 | /* date - delta */ |
| 3136 | return add_date_timedelta((PyDateTime_Date *) left, |
| 3137 | (PyDateTime_Delta *) right, |
| 3138 | 1); |
| 3139 | } |
| 3140 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3141 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | |
| 3145 | /* Various ways to turn a date into a string. */ |
| 3146 | |
| 3147 | static PyObject * |
| 3148 | date_repr(PyDateTime_Date *self) |
| 3149 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3151 | Py_TYPE(self)->tp_name, |
| 3152 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3156 | date_isoformat(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 3159 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 3162 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3163 | static PyObject * |
| 3164 | date_str(PyDateTime_Date *self) |
| 3165 | { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 3166 | return _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_isoformat); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | |
| 3170 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3171 | date_ctime(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | static PyObject * |
| 3177 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3179 | /* This method can be inherited, and needs to call the |
| 3180 | * timetuple() method appropriate to self's class. |
| 3181 | */ |
| 3182 | PyObject *result; |
| 3183 | PyObject *tuple; |
| 3184 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3185 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3189 | &format)) |
| 3190 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3191 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 3192 | tuple = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | if (tuple == NULL) |
| 3194 | return NULL; |
| 3195 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3196 | (PyObject *)self); |
| 3197 | Py_DECREF(tuple); |
| 3198 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3201 | static PyObject * |
| 3202 | date_format(PyDateTime_Date *self, PyObject *args) |
| 3203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 3207 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 3210 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3212 | |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 3213 | return _PyObject_CallMethodIdOneArg((PyObject *)self, &PyId_strftime, |
| 3214 | format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3217 | /* ISO methods. */ |
| 3218 | |
| 3219 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3220 | date_isoweekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
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 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3228 | date_isocalendar(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | int year = GET_YEAR(self); |
| 3231 | int week1_monday = iso_week1_monday(year); |
| 3232 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 3233 | int week; |
| 3234 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | week = divmod(today - week1_monday, 7, &day); |
| 3237 | if (week < 0) { |
| 3238 | --year; |
| 3239 | week1_monday = iso_week1_monday(year); |
| 3240 | week = divmod(today - week1_monday, 7, &day); |
| 3241 | } |
| 3242 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 3243 | ++year; |
| 3244 | week = 0; |
| 3245 | } |
| 3246 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | /* Miscellaneous methods. */ |
| 3250 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3251 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3252 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | if (PyDate_Check(other)) { |
| 3255 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 3256 | ((PyDateTime_Date *)other)->data, |
| 3257 | _PyDateTime_DATE_DATASIZE); |
| 3258 | return diff_to_bool(diff, op); |
| 3259 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3260 | else |
| 3261 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3265 | date_timetuple(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3266 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | return build_struct_time(GET_YEAR(self), |
| 3268 | GET_MONTH(self), |
| 3269 | GET_DAY(self), |
| 3270 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3273 | static PyObject * |
| 3274 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3276 | PyObject *clone; |
| 3277 | PyObject *tuple; |
| 3278 | int year = GET_YEAR(self); |
| 3279 | int month = GET_MONTH(self); |
| 3280 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 3283 | &year, &month, &day)) |
| 3284 | return NULL; |
| 3285 | tuple = Py_BuildValue("iii", year, month, day); |
| 3286 | if (tuple == NULL) |
| 3287 | return NULL; |
| 3288 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 3289 | Py_DECREF(tuple); |
| 3290 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3293 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3294 | generic_hash(unsigned char *data, int len) |
| 3295 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 3296 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | |
| 3300 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3301 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3302 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3303 | date_hash(PyDateTime_Date *self) |
| 3304 | { |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3305 | if (self->hashcode == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | self->hashcode = generic_hash( |
| 3307 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3308 | } |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3314 | date_toordinal(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 3317 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3321 | date_weekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3328 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3329 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3330 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3331 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3332 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | PyObject* field; |
| 3335 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 3336 | _PyDateTime_DATE_DATASIZE); |
| 3337 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3341 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | /* Class methods: */ |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 3349 | DATETIME_DATE_FROMTIMESTAMP_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 3352 | METH_CLASS, |
| 3353 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 3354 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3355 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 3356 | {"fromisoformat", (PyCFunction)date_fromisoformat, METH_O | |
| 3357 | METH_CLASS, |
| 3358 | PyDoc_STR("str -> Construct a date from the output of date.isoformat()")}, |
| 3359 | |
Paul Ganssle | 88c0937 | 2019-04-29 09:22:03 -0400 | [diff] [blame] | 3360 | {"fromisocalendar", (PyCFunction)(void(*)(void))date_fromisocalendar, |
| 3361 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 3362 | PyDoc_STR("int, int, int -> Construct a date from the ISO year, week " |
| 3363 | "number and weekday.\n\n" |
| 3364 | "This is the inverse of the date.isocalendar() function")}, |
| 3365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3366 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 3367 | PyDoc_STR("Current date or datetime: same as " |
| 3368 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3370 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 3373 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3374 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 3375 | {"strftime", (PyCFunction)(void(*)(void))date_strftime, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3376 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3379 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 3382 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3384 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 3385 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 3386 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 3389 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3391 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 3392 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3393 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 3396 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 3397 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3399 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 3400 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3401 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3402 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 3403 | {"replace", (PyCFunction)(void(*)(void))date_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 3407 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3409 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3410 | }; |
| 3411 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3412 | static const char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3413 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3414 | |
| 3415 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3416 | date_add, /* nb_add */ |
| 3417 | date_subtract, /* nb_subtract */ |
| 3418 | 0, /* nb_multiply */ |
| 3419 | 0, /* nb_remainder */ |
| 3420 | 0, /* nb_divmod */ |
| 3421 | 0, /* nb_power */ |
| 3422 | 0, /* nb_negative */ |
| 3423 | 0, /* nb_positive */ |
| 3424 | 0, /* nb_absolute */ |
| 3425 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3426 | }; |
| 3427 | |
| 3428 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3430 | "datetime.date", /* tp_name */ |
| 3431 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 3432 | 0, /* tp_itemsize */ |
| 3433 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3434 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | 0, /* tp_getattr */ |
| 3436 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3437 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3438 | (reprfunc)date_repr, /* tp_repr */ |
| 3439 | &date_as_number, /* tp_as_number */ |
| 3440 | 0, /* tp_as_sequence */ |
| 3441 | 0, /* tp_as_mapping */ |
| 3442 | (hashfunc)date_hash, /* tp_hash */ |
| 3443 | 0, /* tp_call */ |
| 3444 | (reprfunc)date_str, /* tp_str */ |
| 3445 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3446 | 0, /* tp_setattro */ |
| 3447 | 0, /* tp_as_buffer */ |
| 3448 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3449 | date_doc, /* tp_doc */ |
| 3450 | 0, /* tp_traverse */ |
| 3451 | 0, /* tp_clear */ |
| 3452 | date_richcompare, /* tp_richcompare */ |
| 3453 | 0, /* tp_weaklistoffset */ |
| 3454 | 0, /* tp_iter */ |
| 3455 | 0, /* tp_iternext */ |
| 3456 | date_methods, /* tp_methods */ |
| 3457 | 0, /* tp_members */ |
| 3458 | date_getset, /* tp_getset */ |
| 3459 | 0, /* tp_base */ |
| 3460 | 0, /* tp_dict */ |
| 3461 | 0, /* tp_descr_get */ |
| 3462 | 0, /* tp_descr_set */ |
| 3463 | 0, /* tp_dictoffset */ |
| 3464 | 0, /* tp_init */ |
| 3465 | 0, /* tp_alloc */ |
| 3466 | date_new, /* tp_new */ |
| 3467 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3468 | }; |
| 3469 | |
| 3470 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3471 | * PyDateTime_TZInfo implementation. |
| 3472 | */ |
| 3473 | |
| 3474 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 3475 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 3476 | * 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] | 3477 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3478 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 3479 | * |
| 3480 | * Note: For reasons having to do with pickling of subclasses, we have |
| 3481 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 3482 | * in the Python implementation (__init__() could raise NotImplementedError |
| 3483 | * there without ill effect), but doing so in the C implementation hit a |
| 3484 | * brick wall. |
| 3485 | */ |
| 3486 | |
| 3487 | static PyObject * |
| 3488 | tzinfo_nogo(const char* methodname) |
| 3489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3490 | PyErr_Format(PyExc_NotImplementedError, |
| 3491 | "a tzinfo subclass must implement %s()", |
| 3492 | methodname); |
| 3493 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | /* Methods. A subclass must implement these. */ |
| 3497 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3498 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3499 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 3500 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3504 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3505 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 3506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3510 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3511 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 3512 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3516 | |
| 3517 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 3518 | PyDateTime_Delta *delta, |
| 3519 | int factor); |
| 3520 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 3521 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 3522 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3523 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3524 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3525 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3526 | PyObject *result = NULL; |
| 3527 | PyObject *off = NULL, *dst = NULL; |
| 3528 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3529 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3530 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3531 | PyErr_SetString(PyExc_TypeError, |
| 3532 | "fromutc: argument must be a datetime"); |
| 3533 | return NULL; |
| 3534 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3535 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3537 | "is not self"); |
| 3538 | return NULL; |
| 3539 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3540 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3541 | off = datetime_utcoffset(dt, NULL); |
| 3542 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3543 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3544 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3545 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3546 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3547 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3549 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3550 | dst = datetime_dst(dt, NULL); |
| 3551 | if (dst == NULL) |
| 3552 | goto Fail; |
| 3553 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3555 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3556 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3557 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3558 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3559 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3560 | if (delta == NULL) |
| 3561 | goto Fail; |
| 3562 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3565 | |
| 3566 | Py_DECREF(dst); |
| 3567 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3568 | if (dst == NULL) |
| 3569 | goto Fail; |
| 3570 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | goto Inconsistent; |
Alexander Belopolsky | c79447b | 2015-09-27 21:41:55 -0400 | [diff] [blame] | 3572 | if (delta_bool((PyDateTime_Delta *)dst) != 0) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3573 | Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 3574 | (PyDateTime_Delta *)dst, 1)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3575 | if (result == NULL) |
| 3576 | goto Fail; |
| 3577 | } |
| 3578 | Py_DECREF(delta); |
| 3579 | Py_DECREF(dst); |
| 3580 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3582 | |
| 3583 | Inconsistent: |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 3584 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3586 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 3587 | /* fall through to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3588 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3589 | Py_XDECREF(off); |
| 3590 | Py_XDECREF(dst); |
| 3591 | Py_XDECREF(delta); |
| 3592 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3593 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3596 | /* |
| 3597 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3598 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3599 | */ |
| 3600 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3601 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3602 | tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3603 | { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3604 | PyObject *args, *state; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3605 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3606 | _Py_IDENTIFIER(__getinitargs__); |
| 3607 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3608 | |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 3609 | if (_PyObject_LookupAttrId(self, &PyId___getinitargs__, &getinitargs) < 0) { |
| 3610 | return NULL; |
| 3611 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3612 | if (getinitargs != NULL) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 3613 | args = PyObject_CallNoArgs(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3614 | Py_DECREF(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3615 | } |
| 3616 | else { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3617 | args = PyTuple_New(0); |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 3618 | } |
| 3619 | if (args == NULL) { |
| 3620 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3622 | |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 3623 | if (_PyObject_LookupAttrId(self, &PyId___getstate__, &getstate) < 0) { |
| 3624 | Py_DECREF(args); |
| 3625 | return NULL; |
| 3626 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3627 | if (getstate != NULL) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 3628 | state = PyObject_CallNoArgs(getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | Py_DECREF(getstate); |
| 3630 | if (state == NULL) { |
| 3631 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | return NULL; |
| 3633 | } |
| 3634 | } |
| 3635 | else { |
| 3636 | PyObject **dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | state = Py_None; |
| 3638 | dictptr = _PyObject_GetDictPtr(self); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3639 | if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | state = *dictptr; |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3641 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3642 | Py_INCREF(state); |
| 3643 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | if (state == Py_None) { |
| 3646 | Py_DECREF(state); |
| 3647 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3648 | } |
| 3649 | else |
| 3650 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3651 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3652 | |
| 3653 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3655 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3656 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3659 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3660 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3662 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3663 | PyDoc_STR("datetime -> DST offset as timedelta positive east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3665 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3666 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3667 | |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3668 | {"__reduce__", tzinfo_reduce, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3672 | }; |
| 3673 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3674 | static const char tzinfo_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3675 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3676 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3677 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3679 | "datetime.tzinfo", /* tp_name */ |
| 3680 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3681 | 0, /* tp_itemsize */ |
| 3682 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3683 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3684 | 0, /* tp_getattr */ |
| 3685 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3686 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3687 | 0, /* tp_repr */ |
| 3688 | 0, /* tp_as_number */ |
| 3689 | 0, /* tp_as_sequence */ |
| 3690 | 0, /* tp_as_mapping */ |
| 3691 | 0, /* tp_hash */ |
| 3692 | 0, /* tp_call */ |
| 3693 | 0, /* tp_str */ |
| 3694 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3695 | 0, /* tp_setattro */ |
| 3696 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3697 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | tzinfo_doc, /* tp_doc */ |
| 3699 | 0, /* tp_traverse */ |
| 3700 | 0, /* tp_clear */ |
| 3701 | 0, /* tp_richcompare */ |
| 3702 | 0, /* tp_weaklistoffset */ |
| 3703 | 0, /* tp_iter */ |
| 3704 | 0, /* tp_iternext */ |
| 3705 | tzinfo_methods, /* tp_methods */ |
| 3706 | 0, /* tp_members */ |
| 3707 | 0, /* tp_getset */ |
| 3708 | 0, /* tp_base */ |
| 3709 | 0, /* tp_dict */ |
| 3710 | 0, /* tp_descr_get */ |
| 3711 | 0, /* tp_descr_set */ |
| 3712 | 0, /* tp_dictoffset */ |
| 3713 | 0, /* tp_init */ |
| 3714 | 0, /* tp_alloc */ |
| 3715 | PyType_GenericNew, /* tp_new */ |
| 3716 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3717 | }; |
| 3718 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3719 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3720 | |
| 3721 | static PyObject * |
| 3722 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3723 | { |
| 3724 | PyObject *offset; |
| 3725 | PyObject *name = NULL; |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 3726 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|U:timezone", timezone_kws, |
| 3727 | &PyDateTime_DeltaType, &offset, &name)) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3728 | return new_timezone(offset, name); |
| 3729 | |
| 3730 | return NULL; |
| 3731 | } |
| 3732 | |
| 3733 | static void |
| 3734 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3735 | { |
| 3736 | Py_CLEAR(self->offset); |
| 3737 | Py_CLEAR(self->name); |
| 3738 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3739 | } |
| 3740 | |
| 3741 | static PyObject * |
| 3742 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3743 | PyDateTime_TimeZone *other, int op) |
| 3744 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3745 | if (op != Py_EQ && op != Py_NE) |
| 3746 | Py_RETURN_NOTIMPLEMENTED; |
Pablo Galindo | 4be11c0 | 2019-08-22 20:24:25 +0100 | [diff] [blame] | 3747 | if (!PyTimezone_Check(other)) { |
Serhiy Storchaka | 17e5264 | 2019-08-04 12:38:46 +0300 | [diff] [blame] | 3748 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3749 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3750 | return delta_richcompare(self->offset, other->offset, op); |
| 3751 | } |
| 3752 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3753 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3754 | timezone_hash(PyDateTime_TimeZone *self) |
| 3755 | { |
| 3756 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3757 | } |
| 3758 | |
| 3759 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3760 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3761 | otherwise. |
| 3762 | */ |
| 3763 | static int |
| 3764 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3765 | { |
| 3766 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3767 | return 0; |
| 3768 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3769 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3770 | return -1; |
| 3771 | } |
| 3772 | |
| 3773 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3774 | timezone_repr(PyDateTime_TimeZone *self) |
| 3775 | { |
| 3776 | /* Note that although timezone is not subclassable, it is convenient |
| 3777 | to use Py_TYPE(self)->tp_name here. */ |
| 3778 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3779 | |
| 3780 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3781 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3782 | |
| 3783 | if (self->name == NULL) |
| 3784 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3785 | |
| 3786 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3787 | self->name); |
| 3788 | } |
| 3789 | |
| 3790 | |
| 3791 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3792 | timezone_str(PyDateTime_TimeZone *self) |
| 3793 | { |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3794 | int hours, minutes, seconds, microseconds; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3795 | PyObject *offset; |
| 3796 | char sign; |
| 3797 | |
| 3798 | if (self->name != NULL) { |
| 3799 | Py_INCREF(self->name); |
| 3800 | return self->name; |
| 3801 | } |
Victor Stinner | 90fd895 | 2015-09-08 00:12:49 +0200 | [diff] [blame] | 3802 | if ((PyObject *)self == PyDateTime_TimeZone_UTC || |
Alexander Belopolsky | 7827a5b | 2015-09-06 13:07:21 -0400 | [diff] [blame] | 3803 | (GET_TD_DAYS(self->offset) == 0 && |
| 3804 | GET_TD_SECONDS(self->offset) == 0 && |
| 3805 | GET_TD_MICROSECONDS(self->offset) == 0)) |
| 3806 | return PyUnicode_FromString("UTC"); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3807 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3808 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3809 | sign = '-'; |
| 3810 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3811 | if (offset == NULL) |
| 3812 | return NULL; |
| 3813 | } |
| 3814 | else { |
| 3815 | sign = '+'; |
| 3816 | offset = self->offset; |
| 3817 | Py_INCREF(offset); |
| 3818 | } |
| 3819 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3820 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3821 | seconds = GET_TD_SECONDS(offset); |
| 3822 | Py_DECREF(offset); |
| 3823 | minutes = divmod(seconds, 60, &seconds); |
| 3824 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3825 | if (microseconds != 0) { |
| 3826 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d.%06d", |
| 3827 | sign, hours, minutes, |
| 3828 | seconds, microseconds); |
| 3829 | } |
| 3830 | if (seconds != 0) { |
| 3831 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d", |
| 3832 | sign, hours, minutes, seconds); |
| 3833 | } |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3834 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | static PyObject * |
| 3838 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3839 | { |
| 3840 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3841 | return NULL; |
| 3842 | |
| 3843 | return timezone_str(self); |
| 3844 | } |
| 3845 | |
| 3846 | static PyObject * |
| 3847 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3848 | { |
| 3849 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3850 | return NULL; |
| 3851 | |
| 3852 | Py_INCREF(self->offset); |
| 3853 | return self->offset; |
| 3854 | } |
| 3855 | |
| 3856 | static PyObject * |
| 3857 | timezone_dst(PyObject *self, PyObject *dt) |
| 3858 | { |
| 3859 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3860 | return NULL; |
| 3861 | |
| 3862 | Py_RETURN_NONE; |
| 3863 | } |
| 3864 | |
| 3865 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3866 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3867 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3868 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3869 | PyErr_SetString(PyExc_TypeError, |
| 3870 | "fromutc: argument must be a datetime"); |
| 3871 | return NULL; |
| 3872 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3873 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3874 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3875 | "is not self"); |
| 3876 | return NULL; |
| 3877 | } |
| 3878 | |
| 3879 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3880 | } |
| 3881 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3882 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3883 | timezone_getinitargs(PyDateTime_TimeZone *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3884 | { |
| 3885 | if (self->name == NULL) |
| 3886 | return Py_BuildValue("(O)", self->offset); |
| 3887 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3888 | } |
| 3889 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3890 | static PyMethodDef timezone_methods[] = { |
| 3891 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3892 | 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] | 3893 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3894 | |
| 3895 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3896 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3897 | |
| 3898 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3899 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3900 | |
| 3901 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3902 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3903 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3904 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3905 | PyDoc_STR("pickle support")}, |
| 3906 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3907 | {NULL, NULL} |
| 3908 | }; |
| 3909 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3910 | static const char timezone_doc[] = |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3911 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3912 | |
| 3913 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3914 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3915 | "datetime.timezone", /* tp_name */ |
| 3916 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3917 | 0, /* tp_itemsize */ |
| 3918 | (destructor)timezone_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3919 | 0, /* tp_vectorcall_offset */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3920 | 0, /* tp_getattr */ |
| 3921 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3922 | 0, /* tp_as_async */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3923 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3924 | 0, /* tp_as_number */ |
| 3925 | 0, /* tp_as_sequence */ |
| 3926 | 0, /* tp_as_mapping */ |
| 3927 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3928 | 0, /* tp_call */ |
| 3929 | (reprfunc)timezone_str, /* tp_str */ |
| 3930 | 0, /* tp_getattro */ |
| 3931 | 0, /* tp_setattro */ |
| 3932 | 0, /* tp_as_buffer */ |
| 3933 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3934 | timezone_doc, /* tp_doc */ |
| 3935 | 0, /* tp_traverse */ |
| 3936 | 0, /* tp_clear */ |
| 3937 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3938 | 0, /* tp_weaklistoffset */ |
| 3939 | 0, /* tp_iter */ |
| 3940 | 0, /* tp_iternext */ |
| 3941 | timezone_methods, /* tp_methods */ |
| 3942 | 0, /* tp_members */ |
| 3943 | 0, /* tp_getset */ |
| 3944 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3945 | 0, /* tp_dict */ |
| 3946 | 0, /* tp_descr_get */ |
| 3947 | 0, /* tp_descr_set */ |
| 3948 | 0, /* tp_dictoffset */ |
| 3949 | 0, /* tp_init */ |
| 3950 | 0, /* tp_alloc */ |
| 3951 | timezone_new, /* tp_new */ |
| 3952 | }; |
| 3953 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3954 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3955 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3956 | */ |
| 3957 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3958 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3959 | */ |
| 3960 | |
| 3961 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3962 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3967 | static PyObject * |
| 3968 | time_minute(PyDateTime_Time *self, void *unused) |
| 3969 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3970 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | /* The name time_second conflicted with some platform header file. */ |
| 3974 | static PyObject * |
| 3975 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3976 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3977 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | static PyObject * |
| 3981 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | static PyObject * |
| 3987 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3989 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3990 | Py_INCREF(result); |
| 3991 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3994 | static PyObject * |
| 3995 | time_fold(PyDateTime_Time *self, void *unused) |
| 3996 | { |
| 3997 | return PyLong_FromLong(TIME_GET_FOLD(self)); |
| 3998 | } |
| 3999 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4000 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4001 | {"hour", (getter)time_hour}, |
| 4002 | {"minute", (getter)time_minute}, |
| 4003 | {"second", (getter)py_time_second}, |
| 4004 | {"microsecond", (getter)time_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4005 | {"tzinfo", (getter)time_tzinfo}, |
| 4006 | {"fold", (getter)time_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4007 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4008 | }; |
| 4009 | |
| 4010 | /* |
| 4011 | * Constructors. |
| 4012 | */ |
| 4013 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4014 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4015 | "tzinfo", "fold", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4016 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4017 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4018 | time_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo) |
| 4019 | { |
| 4020 | PyDateTime_Time *me; |
| 4021 | char aware = (char)(tzinfo != Py_None); |
| 4022 | |
| 4023 | if (aware && check_tzinfo_subclass(tzinfo) < 0) { |
| 4024 | PyErr_SetString(PyExc_TypeError, "bad tzinfo state arg"); |
| 4025 | return NULL; |
| 4026 | } |
| 4027 | |
| 4028 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 4029 | if (me != NULL) { |
| 4030 | const char *pdata = PyBytes_AS_STRING(state); |
| 4031 | |
| 4032 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 4033 | me->hashcode = -1; |
| 4034 | me->hastzinfo = aware; |
| 4035 | if (aware) { |
| 4036 | Py_INCREF(tzinfo); |
| 4037 | me->tzinfo = tzinfo; |
| 4038 | } |
| 4039 | if (pdata[0] & (1 << 7)) { |
| 4040 | me->data[0] -= 128; |
| 4041 | me->fold = 1; |
| 4042 | } |
| 4043 | else { |
| 4044 | me->fold = 0; |
| 4045 | } |
| 4046 | } |
| 4047 | return (PyObject *)me; |
| 4048 | } |
| 4049 | |
| 4050 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4051 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4052 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | int hour = 0; |
| 4055 | int minute = 0; |
| 4056 | int second = 0; |
| 4057 | int usecond = 0; |
| 4058 | PyObject *tzinfo = Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4059 | int fold = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4061 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4062 | if (PyTuple_GET_SIZE(args) >= 1 && PyTuple_GET_SIZE(args) <= 2) { |
| 4063 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 4064 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4065 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4066 | } |
| 4067 | if (PyBytes_Check(state)) { |
| 4068 | if (PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 4069 | (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) |
| 4070 | { |
| 4071 | return time_from_pickle(type, state, tzinfo); |
| 4072 | } |
| 4073 | } |
| 4074 | else if (PyUnicode_Check(state)) { |
| 4075 | if (PyUnicode_READY(state)) { |
| 4076 | return NULL; |
| 4077 | } |
| 4078 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_TIME_DATASIZE && |
Justin Blanchard | 122376d | 2019-08-29 03:36:15 -0400 | [diff] [blame] | 4079 | (0x7F & PyUnicode_READ_CHAR(state, 0)) < 24) |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4080 | { |
| 4081 | state = PyUnicode_AsLatin1String(state); |
| 4082 | if (state == NULL) { |
| 4083 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4084 | /* More informative error message. */ |
| 4085 | PyErr_SetString(PyExc_ValueError, |
| 4086 | "Failed to encode latin1 string when unpickling " |
| 4087 | "a time object. " |
| 4088 | "pickle.load(data, encoding='latin1') is assumed."); |
| 4089 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 4090 | return NULL; |
| 4091 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4092 | self = time_from_pickle(type, state, tzinfo); |
| 4093 | Py_DECREF(state); |
| 4094 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4096 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4097 | tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4098 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4099 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4100 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | &hour, &minute, &second, &usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4102 | &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4103 | self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, |
| 4104 | type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | } |
| 4106 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | /* |
| 4110 | * Destructor. |
| 4111 | */ |
| 4112 | |
| 4113 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4114 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4115 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 | if (HASTZINFO(self)) { |
| 4117 | Py_XDECREF(self->tzinfo); |
| 4118 | } |
| 4119 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4123 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4124 | */ |
| 4125 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4126 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4127 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4128 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 4129 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4133 | time_dst(PyObject *self, PyObject *unused) { |
| 4134 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4138 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4139 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4143 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4144 | */ |
| 4145 | |
| 4146 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4147 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4149 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4150 | int h = TIME_GET_HOUR(self); |
| 4151 | int m = TIME_GET_MINUTE(self); |
| 4152 | int s = TIME_GET_SECOND(self); |
| 4153 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4154 | int fold = TIME_GET_FOLD(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4157 | if (us) |
| 4158 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 4159 | type_name, h, m, s, us); |
| 4160 | else if (s) |
| 4161 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 4162 | type_name, h, m, s); |
| 4163 | else |
| 4164 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 4165 | if (result != NULL && HASTZINFO(self)) |
| 4166 | result = append_keyword_tzinfo(result, self->tzinfo); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4167 | if (result != NULL && fold) |
| 4168 | result = append_keyword_fold(result, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4169 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4172 | static PyObject * |
| 4173 | time_str(PyDateTime_Time *self) |
| 4174 | { |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 4175 | return _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_isoformat); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4176 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4177 | |
| 4178 | static PyObject * |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4179 | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4181 | char buf[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4182 | char *timespec = NULL; |
| 4183 | static char *keywords[] = {"timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 4185 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4186 | static char *specs[][2] = { |
| 4187 | {"hours", "%02d"}, |
| 4188 | {"minutes", "%02d:%02d"}, |
| 4189 | {"seconds", "%02d:%02d:%02d"}, |
| 4190 | {"milliseconds", "%02d:%02d:%02d.%03d"}, |
| 4191 | {"microseconds", "%02d:%02d:%02d.%06d"}, |
| 4192 | }; |
| 4193 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4194 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4195 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) |
| 4196 | return NULL; |
| 4197 | |
| 4198 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 4199 | if (us == 0) { |
| 4200 | /* seconds */ |
| 4201 | given_spec = 2; |
| 4202 | } |
| 4203 | else { |
| 4204 | /* microseconds */ |
| 4205 | given_spec = 4; |
| 4206 | } |
| 4207 | } |
| 4208 | else { |
| 4209 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 4210 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 4211 | if (given_spec == 3) { |
| 4212 | /* milliseconds */ |
| 4213 | us = us / 1000; |
| 4214 | } |
| 4215 | break; |
| 4216 | } |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 4221 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 4222 | return NULL; |
| 4223 | } |
| 4224 | else { |
| 4225 | result = PyUnicode_FromFormat(specs[given_spec][1], |
| 4226 | TIME_GET_HOUR(self), TIME_GET_MINUTE(self), |
| 4227 | TIME_GET_SECOND(self), us); |
| 4228 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4229 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4230 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4231 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | /* We need to append the UTC offset. */ |
| 4234 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 4235 | Py_None) < 0) { |
| 4236 | Py_DECREF(result); |
| 4237 | return NULL; |
| 4238 | } |
| 4239 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 4240 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4243 | static PyObject * |
| 4244 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 4245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4246 | PyObject *result; |
| 4247 | PyObject *tuple; |
| 4248 | PyObject *format; |
| 4249 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 4252 | &format)) |
| 4253 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | /* Python's strftime does insane things with the year part of the |
| 4256 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 4257 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | */ |
| 4259 | tuple = Py_BuildValue("iiiiiiiii", |
| 4260 | 1900, 1, 1, /* year, month, day */ |
| 4261 | TIME_GET_HOUR(self), |
| 4262 | TIME_GET_MINUTE(self), |
| 4263 | TIME_GET_SECOND(self), |
| 4264 | 0, 1, -1); /* weekday, daynum, dst */ |
| 4265 | if (tuple == NULL) |
| 4266 | return NULL; |
| 4267 | assert(PyTuple_Size(tuple) == 9); |
| 4268 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 4269 | Py_None); |
| 4270 | Py_DECREF(tuple); |
| 4271 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4272 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4273 | |
| 4274 | /* |
| 4275 | * Miscellaneous methods. |
| 4276 | */ |
| 4277 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4278 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4279 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4280 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4281 | PyObject *result = NULL; |
| 4282 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4283 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4284 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4285 | if (! PyTime_Check(other)) |
| 4286 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4287 | |
| 4288 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4289 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4290 | ((PyDateTime_Time *)other)->data, |
| 4291 | _PyDateTime_TIME_DATASIZE); |
| 4292 | return diff_to_bool(diff, op); |
| 4293 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4294 | offset1 = time_utcoffset(self, NULL); |
| 4295 | if (offset1 == NULL) |
| 4296 | return NULL; |
| 4297 | offset2 = time_utcoffset(other, NULL); |
| 4298 | if (offset2 == NULL) |
| 4299 | goto done; |
| 4300 | /* If they're both naive, or both aware and have the same offsets, |
| 4301 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4302 | * offset2 == Py_None at this point. |
| 4303 | */ |
| 4304 | if ((offset1 == offset2) || |
| 4305 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4306 | delta_cmp(offset1, offset2) == 0)) { |
| 4307 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4308 | ((PyDateTime_Time *)other)->data, |
| 4309 | _PyDateTime_TIME_DATASIZE); |
| 4310 | result = diff_to_bool(diff, op); |
| 4311 | } |
| 4312 | /* The hard case: both aware with different UTC offsets */ |
| 4313 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 4314 | int offsecs1, offsecs2; |
| 4315 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 4316 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 4317 | TIME_GET_MINUTE(self) * 60 + |
| 4318 | TIME_GET_SECOND(self) - |
| 4319 | GET_TD_DAYS(offset1) * 86400 - |
| 4320 | GET_TD_SECONDS(offset1); |
| 4321 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 4322 | TIME_GET_MINUTE(other) * 60 + |
| 4323 | TIME_GET_SECOND(other) - |
| 4324 | GET_TD_DAYS(offset2) * 86400 - |
| 4325 | GET_TD_SECONDS(offset2); |
| 4326 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | if (diff == 0) |
| 4328 | diff = TIME_GET_MICROSECOND(self) - |
| 4329 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4330 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4332 | else if (op == Py_EQ) { |
| 4333 | result = Py_False; |
| 4334 | Py_INCREF(result); |
| 4335 | } |
| 4336 | else if (op == Py_NE) { |
| 4337 | result = Py_True; |
| 4338 | Py_INCREF(result); |
| 4339 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4340 | else { |
| 4341 | PyErr_SetString(PyExc_TypeError, |
| 4342 | "can't compare offset-naive and " |
| 4343 | "offset-aware times"); |
| 4344 | } |
| 4345 | done: |
| 4346 | Py_DECREF(offset1); |
| 4347 | Py_XDECREF(offset2); |
| 4348 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4351 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4352 | time_hash(PyDateTime_Time *self) |
| 4353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4355 | PyObject *offset, *self0; |
Victor Stinner | 423c16b | 2017-01-03 23:47:12 +0100 | [diff] [blame] | 4356 | if (TIME_GET_FOLD(self)) { |
| 4357 | self0 = new_time_ex2(TIME_GET_HOUR(self), |
| 4358 | TIME_GET_MINUTE(self), |
| 4359 | TIME_GET_SECOND(self), |
| 4360 | TIME_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4361 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4362 | 0, Py_TYPE(self)); |
| 4363 | if (self0 == NULL) |
| 4364 | return -1; |
| 4365 | } |
| 4366 | else { |
| 4367 | self0 = (PyObject *)self; |
| 4368 | Py_INCREF(self0); |
| 4369 | } |
| 4370 | offset = time_utcoffset(self0, NULL); |
| 4371 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4372 | |
| 4373 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4377 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | self->hashcode = generic_hash( |
| 4379 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4381 | PyObject *temp1, *temp2; |
| 4382 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4384 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 4385 | TIME_GET_MINUTE(self) * 60 + |
| 4386 | TIME_GET_SECOND(self); |
| 4387 | microseconds = TIME_GET_MICROSECOND(self); |
| 4388 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 4389 | if (temp1 == NULL) { |
| 4390 | Py_DECREF(offset); |
| 4391 | return -1; |
| 4392 | } |
| 4393 | temp2 = delta_subtract(temp1, offset); |
| 4394 | Py_DECREF(temp1); |
| 4395 | if (temp2 == NULL) { |
| 4396 | Py_DECREF(offset); |
| 4397 | return -1; |
| 4398 | } |
| 4399 | self->hashcode = PyObject_Hash(temp2); |
| 4400 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4402 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | } |
| 4404 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4405 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4406 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4407 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4408 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | PyObject *clone; |
| 4411 | PyObject *tuple; |
| 4412 | int hh = TIME_GET_HOUR(self); |
| 4413 | int mm = TIME_GET_MINUTE(self); |
| 4414 | int ss = TIME_GET_SECOND(self); |
| 4415 | int us = TIME_GET_MICROSECOND(self); |
| 4416 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4417 | int fold = TIME_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4418 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4419 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | time_kws, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4421 | &hh, &mm, &ss, &us, &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 4423 | if (fold != 0 && fold != 1) { |
| 4424 | PyErr_SetString(PyExc_ValueError, |
| 4425 | "fold must be either 0 or 1"); |
| 4426 | return NULL; |
| 4427 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4428 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 4429 | if (tuple == NULL) |
| 4430 | return NULL; |
| 4431 | clone = time_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4432 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4433 | TIME_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4434 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | Py_DECREF(tuple); |
| 4436 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4439 | static PyObject * |
| 4440 | time_fromisoformat(PyObject *cls, PyObject *tstr) { |
| 4441 | assert(tstr != NULL); |
| 4442 | |
| 4443 | if (!PyUnicode_Check(tstr)) { |
| 4444 | PyErr_SetString(PyExc_TypeError, "fromisoformat: argument must be str"); |
| 4445 | return NULL; |
| 4446 | } |
| 4447 | |
| 4448 | Py_ssize_t len; |
| 4449 | const char *p = PyUnicode_AsUTF8AndSize(tstr, &len); |
| 4450 | |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4451 | if (p == NULL) { |
| 4452 | goto invalid_string_error; |
| 4453 | } |
| 4454 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4455 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 4456 | int tzoffset, tzimicrosecond = 0; |
| 4457 | int rv = parse_isoformat_time(p, len, |
| 4458 | &hour, &minute, &second, µsecond, |
| 4459 | &tzoffset, &tzimicrosecond); |
| 4460 | |
| 4461 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4462 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, |
| 4466 | tzimicrosecond); |
| 4467 | |
| 4468 | if (tzinfo == NULL) { |
| 4469 | return NULL; |
| 4470 | } |
| 4471 | |
| 4472 | PyObject *t; |
| 4473 | if ( (PyTypeObject *)cls == &PyDateTime_TimeType ) { |
| 4474 | t = new_time(hour, minute, second, microsecond, tzinfo, 0); |
| 4475 | } else { |
| 4476 | t = PyObject_CallFunction(cls, "iiiiO", |
| 4477 | hour, minute, second, microsecond, tzinfo); |
| 4478 | } |
| 4479 | |
| 4480 | Py_DECREF(tzinfo); |
| 4481 | return t; |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4482 | |
| 4483 | invalid_string_error: |
| 4484 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", tstr); |
| 4485 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4489 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4490 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4491 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4492 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4493 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4494 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4495 | */ |
| 4496 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4497 | time_getstate(PyDateTime_Time *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | PyObject *basestate; |
| 4500 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4502 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 4503 | _PyDateTime_TIME_DATASIZE); |
| 4504 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4505 | if (proto > 3 && TIME_GET_FOLD(self)) |
| 4506 | /* Set the first bit of the first byte */ |
| 4507 | PyBytes_AS_STRING(basestate)[0] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 4509 | result = PyTuple_Pack(1, basestate); |
| 4510 | else |
| 4511 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 4512 | Py_DECREF(basestate); |
| 4513 | } |
| 4514 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4518 | time_reduce_ex(PyDateTime_Time *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4519 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4520 | int proto; |
| 4521 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4522 | return NULL; |
| 4523 | |
| 4524 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4527 | static PyObject * |
| 4528 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
| 4529 | { |
| 4530 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); |
| 4531 | } |
| 4532 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4533 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4534 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4535 | {"isoformat", (PyCFunction)(void(*)(void))time_isoformat, METH_VARARGS | METH_KEYWORDS, |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4536 | PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" |
| 4537 | "[+HH:MM].\n\n" |
| 4538 | "timespec specifies what components of the time to include.\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4539 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4540 | {"strftime", (PyCFunction)(void(*)(void))time_strftime, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 4544 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 4547 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 4550 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4552 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 4553 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4554 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4555 | {"replace", (PyCFunction)(void(*)(void))time_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4556 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4557 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4558 | {"fromisoformat", (PyCFunction)time_fromisoformat, METH_O | METH_CLASS, |
| 4559 | PyDoc_STR("string -> time from time.isoformat() output")}, |
| 4560 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4561 | {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4562 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4563 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4564 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 4565 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 4566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4568 | }; |
| 4569 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4570 | static const char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4571 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 4572 | \n\ |
| 4573 | 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] | 4574 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4575 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4576 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4577 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4578 | "datetime.time", /* tp_name */ |
| 4579 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 4580 | 0, /* tp_itemsize */ |
| 4581 | (destructor)time_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4582 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4583 | 0, /* tp_getattr */ |
| 4584 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4585 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | (reprfunc)time_repr, /* tp_repr */ |
Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 4587 | 0, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | 0, /* tp_as_sequence */ |
| 4589 | 0, /* tp_as_mapping */ |
| 4590 | (hashfunc)time_hash, /* tp_hash */ |
| 4591 | 0, /* tp_call */ |
| 4592 | (reprfunc)time_str, /* tp_str */ |
| 4593 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4594 | 0, /* tp_setattro */ |
| 4595 | 0, /* tp_as_buffer */ |
| 4596 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4597 | time_doc, /* tp_doc */ |
| 4598 | 0, /* tp_traverse */ |
| 4599 | 0, /* tp_clear */ |
| 4600 | time_richcompare, /* tp_richcompare */ |
| 4601 | 0, /* tp_weaklistoffset */ |
| 4602 | 0, /* tp_iter */ |
| 4603 | 0, /* tp_iternext */ |
| 4604 | time_methods, /* tp_methods */ |
| 4605 | 0, /* tp_members */ |
| 4606 | time_getset, /* tp_getset */ |
| 4607 | 0, /* tp_base */ |
| 4608 | 0, /* tp_dict */ |
| 4609 | 0, /* tp_descr_get */ |
| 4610 | 0, /* tp_descr_set */ |
| 4611 | 0, /* tp_dictoffset */ |
| 4612 | 0, /* tp_init */ |
| 4613 | time_alloc, /* tp_alloc */ |
| 4614 | time_new, /* tp_new */ |
| 4615 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4616 | }; |
| 4617 | |
| 4618 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4619 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4620 | */ |
| 4621 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4622 | /* Accessor properties. Properties for day, month, and year are inherited |
| 4623 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4624 | */ |
| 4625 | |
| 4626 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4627 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4632 | static PyObject * |
| 4633 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 4634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | static PyObject * |
| 4639 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 4640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | static PyObject * |
| 4645 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 4646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4647 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | static PyObject * |
| 4651 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 4652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4653 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 4654 | Py_INCREF(result); |
| 4655 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4658 | static PyObject * |
| 4659 | datetime_fold(PyDateTime_DateTime *self, void *unused) |
| 4660 | { |
| 4661 | return PyLong_FromLong(DATE_GET_FOLD(self)); |
| 4662 | } |
| 4663 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4664 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4665 | {"hour", (getter)datetime_hour}, |
| 4666 | {"minute", (getter)datetime_minute}, |
| 4667 | {"second", (getter)datetime_second}, |
| 4668 | {"microsecond", (getter)datetime_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4669 | {"tzinfo", (getter)datetime_tzinfo}, |
| 4670 | {"fold", (getter)datetime_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4671 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4672 | }; |
| 4673 | |
| 4674 | /* |
| 4675 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4676 | */ |
| 4677 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4678 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | "year", "month", "day", "hour", "minute", "second", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4680 | "microsecond", "tzinfo", "fold", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4681 | }; |
| 4682 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4683 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4684 | datetime_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo) |
| 4685 | { |
| 4686 | PyDateTime_DateTime *me; |
| 4687 | char aware = (char)(tzinfo != Py_None); |
| 4688 | |
| 4689 | if (aware && check_tzinfo_subclass(tzinfo) < 0) { |
| 4690 | PyErr_SetString(PyExc_TypeError, "bad tzinfo state arg"); |
| 4691 | return NULL; |
| 4692 | } |
| 4693 | |
| 4694 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4695 | if (me != NULL) { |
| 4696 | const char *pdata = PyBytes_AS_STRING(state); |
| 4697 | |
| 4698 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4699 | me->hashcode = -1; |
| 4700 | me->hastzinfo = aware; |
| 4701 | if (aware) { |
| 4702 | Py_INCREF(tzinfo); |
| 4703 | me->tzinfo = tzinfo; |
| 4704 | } |
| 4705 | if (pdata[2] & (1 << 7)) { |
| 4706 | me->data[2] -= 128; |
| 4707 | me->fold = 1; |
| 4708 | } |
| 4709 | else { |
| 4710 | me->fold = 0; |
| 4711 | } |
| 4712 | } |
| 4713 | return (PyObject *)me; |
| 4714 | } |
| 4715 | |
| 4716 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4717 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4719 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4720 | int year; |
| 4721 | int month; |
| 4722 | int day; |
| 4723 | int hour = 0; |
| 4724 | int minute = 0; |
| 4725 | int second = 0; |
| 4726 | int usecond = 0; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4727 | int fold = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4730 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4731 | if (PyTuple_GET_SIZE(args) >= 1 && PyTuple_GET_SIZE(args) <= 2) { |
| 4732 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 4733 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4734 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4735 | } |
| 4736 | if (PyBytes_Check(state)) { |
| 4737 | if (PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4738 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) |
| 4739 | { |
| 4740 | return datetime_from_pickle(type, state, tzinfo); |
| 4741 | } |
| 4742 | } |
| 4743 | else if (PyUnicode_Check(state)) { |
| 4744 | if (PyUnicode_READY(state)) { |
| 4745 | return NULL; |
| 4746 | } |
| 4747 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4748 | MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2) & 0x7F)) |
| 4749 | { |
| 4750 | state = PyUnicode_AsLatin1String(state); |
| 4751 | if (state == NULL) { |
| 4752 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4753 | /* More informative error message. */ |
| 4754 | PyErr_SetString(PyExc_ValueError, |
| 4755 | "Failed to encode latin1 string when unpickling " |
| 4756 | "a datetime object. " |
| 4757 | "pickle.load(data, encoding='latin1') is assumed."); |
| 4758 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 4759 | return NULL; |
| 4760 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4761 | self = datetime_from_pickle(type, state, tzinfo); |
| 4762 | Py_DECREF(state); |
| 4763 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4765 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4766 | tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4768 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4769 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4770 | &year, &month, &day, &hour, &minute, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4771 | &second, &usecond, &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4772 | self = new_datetime_ex2(year, month, day, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4773 | hour, minute, second, usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4774 | tzinfo, fold, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | } |
| 4776 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4779 | /* TM_FUNC is the shared type of _PyTime_localtime() and |
| 4780 | * _PyTime_gmtime(). */ |
| 4781 | typedef int (*TM_FUNC)(time_t timer, struct tm*); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4782 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4783 | /* As of version 2015f max fold in IANA database is |
| 4784 | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4785 | static long long max_fold_seconds = 24 * 3600; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4786 | /* NB: date(1970,1,1).toordinal() == 719163 */ |
Benjamin Peterson | ac965ca | 2016-09-18 18:12:21 -0700 | [diff] [blame] | 4787 | static long long epoch = 719163LL * 24 * 60 * 60; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4788 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4789 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4790 | utc_to_seconds(int year, int month, int day, |
| 4791 | int hour, int minute, int second) |
| 4792 | { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4793 | long long ordinal; |
| 4794 | |
| 4795 | /* ymd_to_ord() doesn't support year <= 0 */ |
| 4796 | if (year < MINYEAR || year > MAXYEAR) { |
| 4797 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
| 4798 | return -1; |
| 4799 | } |
| 4800 | |
| 4801 | ordinal = ymd_to_ord(year, month, day); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4802 | return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; |
| 4803 | } |
| 4804 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4805 | static long long |
| 4806 | local(long long u) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4807 | { |
| 4808 | struct tm local_time; |
Alexander Belopolsky | 8e1d3a2 | 2016-07-25 13:54:51 -0400 | [diff] [blame] | 4809 | time_t t; |
| 4810 | u -= epoch; |
| 4811 | t = u; |
| 4812 | if (t != u) { |
| 4813 | PyErr_SetString(PyExc_OverflowError, |
| 4814 | "timestamp out of range for platform time_t"); |
| 4815 | return -1; |
| 4816 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4817 | if (_PyTime_localtime(t, &local_time) != 0) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4818 | return -1; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4819 | return utc_to_seconds(local_time.tm_year + 1900, |
| 4820 | local_time.tm_mon + 1, |
| 4821 | local_time.tm_mday, |
| 4822 | local_time.tm_hour, |
| 4823 | local_time.tm_min, |
| 4824 | local_time.tm_sec); |
| 4825 | } |
| 4826 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4827 | /* Internal helper. |
| 4828 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4829 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4830 | */ |
| 4831 | static PyObject * |
| 4832 | 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] | 4833 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4834 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4835 | struct tm tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4836 | int year, month, day, hour, minute, second, fold = 0; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4837 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4838 | if (f(timet, &tm) != 0) |
| 4839 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4840 | |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4841 | year = tm.tm_year + 1900; |
| 4842 | month = tm.tm_mon + 1; |
| 4843 | day = tm.tm_mday; |
| 4844 | hour = tm.tm_hour; |
| 4845 | minute = tm.tm_min; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4846 | /* The platform localtime/gmtime may insert leap seconds, |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4847 | * indicated by tm.tm_sec > 59. We don't care about them, |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4848 | * except to the extent that passing them on to the datetime |
| 4849 | * constructor would raise ValueError for a reason that |
| 4850 | * made no sense to the user. |
| 4851 | */ |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4852 | second = Py_MIN(59, tm.tm_sec); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4853 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4854 | /* local timezone requires to compute fold */ |
Ammar Askar | 96d1e69 | 2018-07-25 09:54:58 -0700 | [diff] [blame] | 4855 | if (tzinfo == Py_None && f == _PyTime_localtime |
| 4856 | /* On Windows, passing a negative value to local results |
| 4857 | * in an OSError because localtime_s on Windows does |
| 4858 | * not support negative timestamps. Unfortunately this |
| 4859 | * means that fold detection for time values between |
| 4860 | * 0 and max_fold_seconds will result in an identical |
| 4861 | * error since we subtract max_fold_seconds to detect a |
| 4862 | * fold. However, since we know there haven't been any |
| 4863 | * folds in the interval [0, max_fold_seconds) in any |
| 4864 | * timezone, we can hackily just forego fold detection |
| 4865 | * for this time range. |
| 4866 | */ |
| 4867 | #ifdef MS_WINDOWS |
| 4868 | && (timet - max_fold_seconds > 0) |
| 4869 | #endif |
| 4870 | ) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4871 | long long probe_seconds, result_seconds, transition; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4872 | |
| 4873 | result_seconds = utc_to_seconds(year, month, day, |
| 4874 | hour, minute, second); |
| 4875 | /* Probe max_fold_seconds to detect a fold. */ |
| 4876 | probe_seconds = local(epoch + timet - max_fold_seconds); |
| 4877 | if (probe_seconds == -1) |
| 4878 | return NULL; |
| 4879 | transition = result_seconds - probe_seconds - max_fold_seconds; |
| 4880 | if (transition < 0) { |
| 4881 | probe_seconds = local(epoch + timet + transition); |
| 4882 | if (probe_seconds == -1) |
| 4883 | return NULL; |
| 4884 | if (probe_seconds == result_seconds) |
| 4885 | fold = 1; |
| 4886 | } |
| 4887 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 4888 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 4889 | second, us, tzinfo, fold, cls); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | /* Internal helper. |
| 4893 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4894 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4895 | * have enough bits to cover a datetime's full range of precision, it's |
| 4896 | * better to call datetime_from_timet_and_us provided you have a way |
| 4897 | * to get that much precision (e.g., C time() isn't good enough). |
| 4898 | */ |
| 4899 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4900 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4901 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4903 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4904 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4905 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 4906 | if (_PyTime_ObjectToTimeval(timestamp, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 4907 | &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | return NULL; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4909 | |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4910 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | /* Internal helper. |
| 4914 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4915 | * gmtime for f as appropriate. |
| 4916 | */ |
| 4917 | static PyObject * |
| 4918 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4919 | { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4920 | _PyTime_t ts = _PyTime_GetSystemClock(); |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4921 | time_t secs; |
| 4922 | int us; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4923 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4924 | if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4925 | return NULL; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4926 | assert(0 <= us && us <= 999999); |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4927 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4928 | return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4931 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4932 | |
| 4933 | @classmethod |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4934 | datetime.datetime.now |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4935 | |
| 4936 | tz: object = None |
| 4937 | Timezone object. |
| 4938 | |
| 4939 | Returns new datetime object representing current time local to tz. |
| 4940 | |
| 4941 | If no tz is specified, uses local timezone. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4942 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4943 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4944 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4945 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4946 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4947 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4948 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4949 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4950 | /* Return best possible local time -- this isn't constrained by the |
| 4951 | * precision of a timestamp. |
| 4952 | */ |
| 4953 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4955 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4956 | self = datetime_best_possible((PyObject *)type, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4957 | tz == Py_None ? _PyTime_localtime : |
| 4958 | _PyTime_gmtime, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4959 | tz); |
| 4960 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4962 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4963 | } |
| 4964 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4967 | /* Return best possible UTC time -- this isn't constrained by the |
| 4968 | * precision of a timestamp. |
| 4969 | */ |
| 4970 | static PyObject * |
| 4971 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4972 | { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4973 | return datetime_best_possible(cls, _PyTime_gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4976 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4977 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4978 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4980 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4981 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4982 | PyObject *tzinfo = Py_None; |
| 4983 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4984 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4985 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4986 | keywords, ×tamp, &tzinfo)) |
| 4987 | return NULL; |
| 4988 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4989 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4991 | self = datetime_from_timestamp(cls, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4992 | tzinfo == Py_None ? _PyTime_localtime : |
| 4993 | _PyTime_gmtime, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4994 | timestamp, |
| 4995 | tzinfo); |
| 4996 | if (self != NULL && tzinfo != Py_None) { |
| 4997 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4998 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4999 | } |
| 5000 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5003 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 5004 | static PyObject * |
| 5005 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 5006 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5007 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5008 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5009 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5010 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5011 | result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | Py_None); |
| 5013 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5016 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5017 | static PyObject * |
| 5018 | datetime_strptime(PyObject *cls, PyObject *args) |
| 5019 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5020 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5021 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5022 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5024 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5025 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5026 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5027 | if (module == NULL) { |
| 5028 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 5029 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5030 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5031 | } |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 5032 | return _PyObject_CallMethodIdObjArgs(module, &PyId__strptime_datetime, |
| 5033 | cls, string, format, NULL); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5036 | /* Return new datetime from date/datetime and time arguments. */ |
| 5037 | static PyObject * |
| 5038 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 5039 | { |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5040 | static char *keywords[] = {"date", "time", "tzinfo", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | PyObject *date; |
| 5042 | PyObject *time; |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5043 | PyObject *tzinfo = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5045 | |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5046 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | &PyDateTime_DateType, &date, |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5048 | &PyDateTime_TimeType, &time, &tzinfo)) { |
| 5049 | if (tzinfo == NULL) { |
| 5050 | if (HASTZINFO(time)) |
| 5051 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 5052 | else |
| 5053 | tzinfo = Py_None; |
| 5054 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 5055 | result = new_datetime_subclass_fold_ex(GET_YEAR(date), |
| 5056 | GET_MONTH(date), |
| 5057 | GET_DAY(date), |
| 5058 | TIME_GET_HOUR(time), |
| 5059 | TIME_GET_MINUTE(time), |
| 5060 | TIME_GET_SECOND(time), |
| 5061 | TIME_GET_MICROSECOND(time), |
| 5062 | tzinfo, |
| 5063 | TIME_GET_FOLD(time), |
| 5064 | cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | } |
| 5066 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5067 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5068 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5069 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5070 | _sanitize_isoformat_str(PyObject *dtstr) |
| 5071 | { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5072 | // `fromisoformat` allows surrogate characters in exactly one position, |
| 5073 | // the separator; to allow datetime_fromisoformat to make the simplifying |
| 5074 | // assumption that all valid strings can be encoded in UTF-8, this function |
| 5075 | // replaces any surrogate character separators with `T`. |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5076 | // |
| 5077 | // The result of this, if not NULL, returns a new reference |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5078 | Py_ssize_t len = PyUnicode_GetLength(dtstr); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5079 | if (len < 0) { |
| 5080 | return NULL; |
| 5081 | } |
| 5082 | |
| 5083 | if (len <= 10 || |
| 5084 | !Py_UNICODE_IS_SURROGATE(PyUnicode_READ_CHAR(dtstr, 10))) { |
| 5085 | Py_INCREF(dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5086 | return dtstr; |
| 5087 | } |
| 5088 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5089 | PyObject *str_out = _PyUnicode_Copy(dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5090 | if (str_out == NULL) { |
| 5091 | return NULL; |
| 5092 | } |
| 5093 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5094 | if (PyUnicode_WriteChar(str_out, 10, (Py_UCS4)'T')) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5095 | Py_DECREF(str_out); |
| 5096 | return NULL; |
| 5097 | } |
| 5098 | |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5099 | return str_out; |
| 5100 | } |
| 5101 | |
| 5102 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5103 | datetime_fromisoformat(PyObject *cls, PyObject *dtstr) |
| 5104 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5105 | assert(dtstr != NULL); |
| 5106 | |
| 5107 | if (!PyUnicode_Check(dtstr)) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5108 | PyErr_SetString(PyExc_TypeError, |
| 5109 | "fromisoformat: argument must be str"); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5110 | return NULL; |
| 5111 | } |
| 5112 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5113 | PyObject *dtstr_clean = _sanitize_isoformat_str(dtstr); |
| 5114 | if (dtstr_clean == NULL) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5115 | goto error; |
| 5116 | } |
| 5117 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5118 | Py_ssize_t len; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5119 | const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr_clean, &len); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5120 | |
| 5121 | if (dt_ptr == NULL) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5122 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 5123 | // Encoding errors are invalid string errors at this point |
| 5124 | goto invalid_string_error; |
| 5125 | } |
| 5126 | else { |
| 5127 | goto error; |
| 5128 | } |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | const char *p = dt_ptr; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5132 | |
| 5133 | int year = 0, month = 0, day = 0; |
| 5134 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 5135 | int tzoffset = 0, tzusec = 0; |
| 5136 | |
| 5137 | // date has a fixed length of 10 |
| 5138 | int rv = parse_isoformat_date(p, &year, &month, &day); |
| 5139 | |
| 5140 | if (!rv && len > 10) { |
| 5141 | // In UTF-8, the length of multi-byte characters is encoded in the MSB |
| 5142 | if ((p[10] & 0x80) == 0) { |
| 5143 | p += 11; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5144 | } |
| 5145 | else { |
| 5146 | switch (p[10] & 0xf0) { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5147 | case 0xe0: |
| 5148 | p += 13; |
| 5149 | break; |
| 5150 | case 0xf0: |
| 5151 | p += 14; |
| 5152 | break; |
| 5153 | default: |
| 5154 | p += 12; |
| 5155 | break; |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | len -= (p - dt_ptr); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5160 | rv = parse_isoformat_time(p, len, &hour, &minute, &second, |
| 5161 | µsecond, &tzoffset, &tzusec); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5162 | } |
| 5163 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5164 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5165 | } |
| 5166 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5167 | PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, tzusec); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5168 | if (tzinfo == NULL) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5169 | goto error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5170 | } |
| 5171 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 5172 | PyObject *dt = new_datetime_subclass_ex(year, month, day, hour, minute, |
| 5173 | second, microsecond, tzinfo, cls); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5174 | |
| 5175 | Py_DECREF(tzinfo); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5176 | Py_DECREF(dtstr_clean); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5177 | return dt; |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5178 | |
| 5179 | invalid_string_error: |
| 5180 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", dtstr); |
| 5181 | |
| 5182 | error: |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5183 | Py_XDECREF(dtstr_clean); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5184 | |
| 5185 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5186 | } |
| 5187 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5188 | /* |
| 5189 | * Destructor. |
| 5190 | */ |
| 5191 | |
| 5192 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5193 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5195 | if (HASTZINFO(self)) { |
| 5196 | Py_XDECREF(self->tzinfo); |
| 5197 | } |
| 5198 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | /* |
| 5202 | * Indirect access to tzinfo methods. |
| 5203 | */ |
| 5204 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5205 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 5206 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5207 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 5208 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5212 | datetime_dst(PyObject *self, PyObject *unused) { |
| 5213 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5217 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 5218 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5222 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5223 | */ |
| 5224 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5225 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 5226 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5227 | */ |
| 5228 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5229 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | /* Note that the C-level additions can't overflow, because of |
| 5233 | * invariant bounds on the member values. |
| 5234 | */ |
| 5235 | int year = GET_YEAR(date); |
| 5236 | int month = GET_MONTH(date); |
| 5237 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 5238 | int hour = DATE_GET_HOUR(date); |
| 5239 | int minute = DATE_GET_MINUTE(date); |
| 5240 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 5241 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 5242 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5244 | assert(factor == 1 || factor == -1); |
| 5245 | if (normalize_datetime(&year, &month, &day, |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 5246 | &hour, &minute, &second, µsecond) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | return NULL; |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 5248 | } |
| 5249 | |
Paul Ganssle | 89427cd | 2019-02-04 14:42:04 -0500 | [diff] [blame] | 5250 | return new_datetime_subclass_ex(year, month, day, |
| 5251 | hour, minute, second, microsecond, |
| 5252 | HASTZINFO(date) ? date->tzinfo : Py_None, |
| 5253 | (PyObject *)Py_TYPE(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5257 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | if (PyDateTime_Check(left)) { |
| 5260 | /* datetime + ??? */ |
| 5261 | if (PyDelta_Check(right)) |
| 5262 | /* datetime + delta */ |
| 5263 | return add_datetime_timedelta( |
| 5264 | (PyDateTime_DateTime *)left, |
| 5265 | (PyDateTime_Delta *)right, |
| 5266 | 1); |
| 5267 | } |
| 5268 | else if (PyDelta_Check(left)) { |
| 5269 | /* delta + datetime */ |
| 5270 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 5271 | (PyDateTime_Delta *) left, |
| 5272 | 1); |
| 5273 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5274 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5278 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5279 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5280 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5282 | if (PyDateTime_Check(left)) { |
| 5283 | /* datetime - ??? */ |
| 5284 | if (PyDateTime_Check(right)) { |
| 5285 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5286 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5288 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5289 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 5290 | offset2 = offset1 = Py_None; |
| 5291 | Py_INCREF(offset1); |
| 5292 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5293 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5294 | else { |
| 5295 | offset1 = datetime_utcoffset(left, NULL); |
| 5296 | if (offset1 == NULL) |
| 5297 | return NULL; |
| 5298 | offset2 = datetime_utcoffset(right, NULL); |
| 5299 | if (offset2 == NULL) { |
| 5300 | Py_DECREF(offset1); |
| 5301 | return NULL; |
| 5302 | } |
| 5303 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 5304 | PyErr_SetString(PyExc_TypeError, |
| 5305 | "can't subtract offset-naive and " |
| 5306 | "offset-aware datetimes"); |
| 5307 | Py_DECREF(offset1); |
| 5308 | Py_DECREF(offset2); |
| 5309 | return NULL; |
| 5310 | } |
| 5311 | } |
| 5312 | if ((offset1 != offset2) && |
| 5313 | delta_cmp(offset1, offset2) != 0) { |
| 5314 | offdiff = delta_subtract(offset1, offset2); |
| 5315 | if (offdiff == NULL) { |
| 5316 | Py_DECREF(offset1); |
| 5317 | Py_DECREF(offset2); |
| 5318 | return NULL; |
| 5319 | } |
| 5320 | } |
| 5321 | Py_DECREF(offset1); |
| 5322 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5323 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 5324 | GET_MONTH(left), |
| 5325 | GET_DAY(left)) - |
| 5326 | ymd_to_ord(GET_YEAR(right), |
| 5327 | GET_MONTH(right), |
| 5328 | GET_DAY(right)); |
| 5329 | /* These can't overflow, since the values are |
| 5330 | * normalized. At most this gives the number of |
| 5331 | * seconds in one day. |
| 5332 | */ |
| 5333 | delta_s = (DATE_GET_HOUR(left) - |
| 5334 | DATE_GET_HOUR(right)) * 3600 + |
| 5335 | (DATE_GET_MINUTE(left) - |
| 5336 | DATE_GET_MINUTE(right)) * 60 + |
| 5337 | (DATE_GET_SECOND(left) - |
| 5338 | DATE_GET_SECOND(right)); |
| 5339 | delta_us = DATE_GET_MICROSECOND(left) - |
| 5340 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5341 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 5342 | if (result == NULL) |
| 5343 | return NULL; |
| 5344 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5345 | if (offdiff != NULL) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 5346 | Py_SETREF(result, delta_subtract(result, offdiff)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5347 | Py_DECREF(offdiff); |
| 5348 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5349 | } |
| 5350 | else if (PyDelta_Check(right)) { |
| 5351 | /* datetime - delta */ |
| 5352 | result = add_datetime_timedelta( |
| 5353 | (PyDateTime_DateTime *)left, |
| 5354 | (PyDateTime_Delta *)right, |
| 5355 | -1); |
| 5356 | } |
| 5357 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5359 | if (result == Py_NotImplemented) |
| 5360 | Py_INCREF(result); |
| 5361 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | /* Various ways to turn a datetime into a string. */ |
| 5365 | |
| 5366 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5367 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5369 | const char *type_name = Py_TYPE(self)->tp_name; |
| 5370 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5372 | if (DATE_GET_MICROSECOND(self)) { |
| 5373 | baserepr = PyUnicode_FromFormat( |
| 5374 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 5375 | type_name, |
| 5376 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5377 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5378 | DATE_GET_SECOND(self), |
| 5379 | DATE_GET_MICROSECOND(self)); |
| 5380 | } |
| 5381 | else if (DATE_GET_SECOND(self)) { |
| 5382 | baserepr = PyUnicode_FromFormat( |
| 5383 | "%s(%d, %d, %d, %d, %d, %d)", |
| 5384 | type_name, |
| 5385 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5386 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5387 | DATE_GET_SECOND(self)); |
| 5388 | } |
| 5389 | else { |
| 5390 | baserepr = PyUnicode_FromFormat( |
| 5391 | "%s(%d, %d, %d, %d, %d)", |
| 5392 | type_name, |
| 5393 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5394 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 5395 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5396 | if (baserepr != NULL && DATE_GET_FOLD(self) != 0) |
| 5397 | baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 5399 | return baserepr; |
| 5400 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5403 | static PyObject * |
| 5404 | datetime_str(PyDateTime_DateTime *self) |
| 5405 | { |
Victor Stinner | 4c38154 | 2016-12-09 00:33:39 +0100 | [diff] [blame] | 5406 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5407 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5408 | |
| 5409 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5410 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5412 | int sep = 'T'; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5413 | char *timespec = NULL; |
| 5414 | static char *keywords[] = {"sep", "timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5415 | char buffer[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5416 | PyObject *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | int us = DATE_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5418 | static char *specs[][2] = { |
| 5419 | {"hours", "%04d-%02d-%02d%c%02d"}, |
| 5420 | {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, |
| 5421 | {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, |
| 5422 | {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, |
| 5423 | {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, |
| 5424 | }; |
| 5425 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5426 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5427 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5428 | return NULL; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5429 | |
| 5430 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 5431 | if (us == 0) { |
| 5432 | /* seconds */ |
| 5433 | given_spec = 2; |
| 5434 | } |
| 5435 | else { |
| 5436 | /* microseconds */ |
| 5437 | given_spec = 4; |
| 5438 | } |
| 5439 | } |
| 5440 | else { |
| 5441 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 5442 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 5443 | if (given_spec == 3) { |
| 5444 | us = us / 1000; |
| 5445 | } |
| 5446 | break; |
| 5447 | } |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 5452 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 5453 | return NULL; |
| 5454 | } |
| 5455 | else { |
| 5456 | result = PyUnicode_FromFormat(specs[given_spec][1], |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5457 | GET_YEAR(self), GET_MONTH(self), |
| 5458 | GET_DAY(self), (int)sep, |
| 5459 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5460 | DATE_GET_SECOND(self), us); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5461 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 5462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5463 | if (!result || !HASTZINFO(self)) |
| 5464 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | /* We need to append the UTC offset. */ |
| 5467 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 5468 | (PyObject *)self) < 0) { |
| 5469 | Py_DECREF(result); |
| 5470 | return NULL; |
| 5471 | } |
| 5472 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 5473 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5476 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5477 | datetime_ctime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 | return format_ctime((PyDateTime_Date *)self, |
| 5480 | DATE_GET_HOUR(self), |
| 5481 | DATE_GET_MINUTE(self), |
| 5482 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5485 | /* Miscellaneous methods. */ |
| 5486 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5487 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5488 | flip_fold(PyObject *dt) |
| 5489 | { |
| 5490 | return new_datetime_ex2(GET_YEAR(dt), |
| 5491 | GET_MONTH(dt), |
| 5492 | GET_DAY(dt), |
| 5493 | DATE_GET_HOUR(dt), |
| 5494 | DATE_GET_MINUTE(dt), |
| 5495 | DATE_GET_SECOND(dt), |
| 5496 | DATE_GET_MICROSECOND(dt), |
| 5497 | HASTZINFO(dt) ? |
| 5498 | ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, |
| 5499 | !DATE_GET_FOLD(dt), |
| 5500 | Py_TYPE(dt)); |
| 5501 | } |
| 5502 | |
| 5503 | static PyObject * |
| 5504 | get_flip_fold_offset(PyObject *dt) |
| 5505 | { |
| 5506 | PyObject *result, *flip_dt; |
| 5507 | |
| 5508 | flip_dt = flip_fold(dt); |
| 5509 | if (flip_dt == NULL) |
| 5510 | return NULL; |
| 5511 | result = datetime_utcoffset(flip_dt, NULL); |
| 5512 | Py_DECREF(flip_dt); |
| 5513 | return result; |
| 5514 | } |
| 5515 | |
| 5516 | /* PEP 495 exception: Whenever one or both of the operands in |
| 5517 | * inter-zone comparison is such that its utcoffset() depends |
Serhiy Storchaka | bac2d5b | 2018-03-28 22:14:26 +0300 | [diff] [blame] | 5518 | * on the value of its fold attribute, the result is False. |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5519 | * |
| 5520 | * Return 1 if exception applies, 0 if not, and -1 on error. |
| 5521 | */ |
| 5522 | static int |
| 5523 | pep495_eq_exception(PyObject *self, PyObject *other, |
| 5524 | PyObject *offset_self, PyObject *offset_other) |
| 5525 | { |
| 5526 | int result = 0; |
| 5527 | PyObject *flip_offset; |
| 5528 | |
| 5529 | flip_offset = get_flip_fold_offset(self); |
| 5530 | if (flip_offset == NULL) |
| 5531 | return -1; |
| 5532 | if (flip_offset != offset_self && |
| 5533 | delta_cmp(flip_offset, offset_self)) |
| 5534 | { |
| 5535 | result = 1; |
| 5536 | goto done; |
| 5537 | } |
| 5538 | Py_DECREF(flip_offset); |
| 5539 | |
| 5540 | flip_offset = get_flip_fold_offset(other); |
| 5541 | if (flip_offset == NULL) |
| 5542 | return -1; |
| 5543 | if (flip_offset != offset_other && |
| 5544 | delta_cmp(flip_offset, offset_other)) |
| 5545 | result = 1; |
| 5546 | done: |
| 5547 | Py_DECREF(flip_offset); |
| 5548 | return result; |
| 5549 | } |
| 5550 | |
| 5551 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 5552 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5553 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5554 | PyObject *result = NULL; |
| 5555 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5558 | if (! PyDateTime_Check(other)) { |
| 5559 | if (PyDate_Check(other)) { |
| 5560 | /* Prevent invocation of date_richcompare. We want to |
| 5561 | return NotImplemented here to give the other object |
| 5562 | a chance. But since DateTime is a subclass of |
| 5563 | Date, if the other object is a Date, it would |
| 5564 | compute an ordering based on the date part alone, |
| 5565 | and we don't want that. So force unequal or |
| 5566 | uncomparable here in that case. */ |
| 5567 | if (op == Py_EQ) |
| 5568 | Py_RETURN_FALSE; |
| 5569 | if (op == Py_NE) |
| 5570 | Py_RETURN_TRUE; |
| 5571 | return cmperror(self, other); |
| 5572 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5573 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5574 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5575 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5576 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5577 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5578 | ((PyDateTime_DateTime *)other)->data, |
| 5579 | _PyDateTime_DATETIME_DATASIZE); |
| 5580 | return diff_to_bool(diff, op); |
| 5581 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5582 | offset1 = datetime_utcoffset(self, NULL); |
| 5583 | if (offset1 == NULL) |
| 5584 | return NULL; |
| 5585 | offset2 = datetime_utcoffset(other, NULL); |
| 5586 | if (offset2 == NULL) |
| 5587 | goto done; |
| 5588 | /* If they're both naive, or both aware and have the same offsets, |
| 5589 | * we get off cheap. Note that if they're both naive, offset1 == |
| 5590 | * offset2 == Py_None at this point. |
| 5591 | */ |
| 5592 | if ((offset1 == offset2) || |
| 5593 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 5594 | delta_cmp(offset1, offset2) == 0)) { |
| 5595 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5596 | ((PyDateTime_DateTime *)other)->data, |
| 5597 | _PyDateTime_DATETIME_DATASIZE); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5598 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5599 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5600 | if (ex == -1) |
| 5601 | goto done; |
| 5602 | if (ex) |
| 5603 | diff = 1; |
| 5604 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5605 | result = diff_to_bool(diff, op); |
| 5606 | } |
| 5607 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5608 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5609 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5610 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5611 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 5612 | other); |
| 5613 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5614 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5615 | diff = GET_TD_DAYS(delta); |
| 5616 | if (diff == 0) |
| 5617 | diff = GET_TD_SECONDS(delta) | |
| 5618 | GET_TD_MICROSECONDS(delta); |
| 5619 | Py_DECREF(delta); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5620 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5621 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5622 | if (ex == -1) |
| 5623 | goto done; |
| 5624 | if (ex) |
| 5625 | diff = 1; |
| 5626 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5627 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5628 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 5629 | else if (op == Py_EQ) { |
| 5630 | result = Py_False; |
| 5631 | Py_INCREF(result); |
| 5632 | } |
| 5633 | else if (op == Py_NE) { |
| 5634 | result = Py_True; |
| 5635 | Py_INCREF(result); |
| 5636 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5637 | else { |
| 5638 | PyErr_SetString(PyExc_TypeError, |
| 5639 | "can't compare offset-naive and " |
| 5640 | "offset-aware datetimes"); |
| 5641 | } |
| 5642 | done: |
| 5643 | Py_DECREF(offset1); |
| 5644 | Py_XDECREF(offset2); |
| 5645 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5648 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5649 | datetime_hash(PyDateTime_DateTime *self) |
| 5650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5652 | PyObject *offset, *self0; |
| 5653 | if (DATE_GET_FOLD(self)) { |
| 5654 | self0 = new_datetime_ex2(GET_YEAR(self), |
| 5655 | GET_MONTH(self), |
| 5656 | GET_DAY(self), |
| 5657 | DATE_GET_HOUR(self), |
| 5658 | DATE_GET_MINUTE(self), |
| 5659 | DATE_GET_SECOND(self), |
| 5660 | DATE_GET_MICROSECOND(self), |
| 5661 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 5662 | 0, Py_TYPE(self)); |
| 5663 | if (self0 == NULL) |
| 5664 | return -1; |
| 5665 | } |
| 5666 | else { |
| 5667 | self0 = (PyObject *)self; |
| 5668 | Py_INCREF(self0); |
| 5669 | } |
| 5670 | offset = datetime_utcoffset(self0, NULL); |
| 5671 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5672 | |
| 5673 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5676 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5677 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | self->hashcode = generic_hash( |
| 5679 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5680 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5681 | PyObject *temp1, *temp2; |
| 5682 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 | assert(HASTZINFO(self)); |
| 5685 | days = ymd_to_ord(GET_YEAR(self), |
| 5686 | GET_MONTH(self), |
| 5687 | GET_DAY(self)); |
| 5688 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5689 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5690 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5691 | temp1 = new_delta(days, seconds, |
| 5692 | DATE_GET_MICROSECOND(self), |
| 5693 | 1); |
| 5694 | if (temp1 == NULL) { |
| 5695 | Py_DECREF(offset); |
| 5696 | return -1; |
| 5697 | } |
| 5698 | temp2 = delta_subtract(temp1, offset); |
| 5699 | Py_DECREF(temp1); |
| 5700 | if (temp2 == NULL) { |
| 5701 | Py_DECREF(offset); |
| 5702 | return -1; |
| 5703 | } |
| 5704 | self->hashcode = PyObject_Hash(temp2); |
| 5705 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5707 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5708 | } |
| 5709 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5710 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5711 | |
| 5712 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5713 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | PyObject *clone; |
| 5716 | PyObject *tuple; |
| 5717 | int y = GET_YEAR(self); |
| 5718 | int m = GET_MONTH(self); |
| 5719 | int d = GET_DAY(self); |
| 5720 | int hh = DATE_GET_HOUR(self); |
| 5721 | int mm = DATE_GET_MINUTE(self); |
| 5722 | int ss = DATE_GET_SECOND(self); |
| 5723 | int us = DATE_GET_MICROSECOND(self); |
| 5724 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5725 | int fold = DATE_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5726 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5727 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5728 | datetime_kws, |
| 5729 | &y, &m, &d, &hh, &mm, &ss, &us, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5730 | &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5731 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 5732 | if (fold != 0 && fold != 1) { |
| 5733 | PyErr_SetString(PyExc_ValueError, |
| 5734 | "fold must be either 0 or 1"); |
| 5735 | return NULL; |
| 5736 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5737 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 5738 | if (tuple == NULL) |
| 5739 | return NULL; |
| 5740 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5741 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5742 | DATE_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5743 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5744 | Py_DECREF(tuple); |
| 5745 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5749 | local_timezone_from_timestamp(time_t timestamp) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5750 | { |
| 5751 | PyObject *result = NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5752 | PyObject *delta; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5753 | struct tm local_time_tm; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5754 | PyObject *nameo = NULL; |
| 5755 | const char *zone = NULL; |
| 5756 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5757 | if (_PyTime_localtime(timestamp, &local_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5758 | return NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5759 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5760 | zone = local_time_tm.tm_zone; |
| 5761 | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5762 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 5763 | { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5764 | PyObject *local_time, *utc_time; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5765 | struct tm utc_time_tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5766 | char buf[100]; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5767 | strftime(buf, sizeof(buf), "%Z", &local_time_tm); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5768 | zone = buf; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5769 | local_time = new_datetime(local_time_tm.tm_year + 1900, |
| 5770 | local_time_tm.tm_mon + 1, |
| 5771 | local_time_tm.tm_mday, |
| 5772 | local_time_tm.tm_hour, |
| 5773 | local_time_tm.tm_min, |
| 5774 | local_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5775 | if (local_time == NULL) { |
| 5776 | return NULL; |
| 5777 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5778 | if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5779 | return NULL; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5780 | utc_time = new_datetime(utc_time_tm.tm_year + 1900, |
| 5781 | utc_time_tm.tm_mon + 1, |
| 5782 | utc_time_tm.tm_mday, |
| 5783 | utc_time_tm.tm_hour, |
| 5784 | utc_time_tm.tm_min, |
| 5785 | utc_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5786 | if (utc_time == NULL) { |
| 5787 | Py_DECREF(local_time); |
| 5788 | return NULL; |
| 5789 | } |
| 5790 | delta = datetime_subtract(local_time, utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5791 | Py_DECREF(local_time); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5792 | Py_DECREF(utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5793 | } |
| 5794 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5795 | if (delta == NULL) { |
| 5796 | return NULL; |
| 5797 | } |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5798 | if (zone != NULL) { |
| 5799 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 5800 | if (nameo == NULL) |
| 5801 | goto error; |
| 5802 | } |
| 5803 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 5804 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5805 | error: |
| 5806 | Py_DECREF(delta); |
| 5807 | return result; |
| 5808 | } |
| 5809 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5810 | static PyObject * |
| 5811 | local_timezone(PyDateTime_DateTime *utc_time) |
| 5812 | { |
| 5813 | time_t timestamp; |
| 5814 | PyObject *delta; |
| 5815 | PyObject *one_second; |
| 5816 | PyObject *seconds; |
| 5817 | |
| 5818 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 5819 | if (delta == NULL) |
| 5820 | return NULL; |
| 5821 | one_second = new_delta(0, 1, 0, 0); |
| 5822 | if (one_second == NULL) { |
| 5823 | Py_DECREF(delta); |
| 5824 | return NULL; |
| 5825 | } |
| 5826 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 5827 | (PyDateTime_Delta *)one_second); |
| 5828 | Py_DECREF(one_second); |
| 5829 | Py_DECREF(delta); |
| 5830 | if (seconds == NULL) |
| 5831 | return NULL; |
| 5832 | timestamp = _PyLong_AsTime_t(seconds); |
| 5833 | Py_DECREF(seconds); |
| 5834 | if (timestamp == -1 && PyErr_Occurred()) |
| 5835 | return NULL; |
| 5836 | return local_timezone_from_timestamp(timestamp); |
| 5837 | } |
| 5838 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5839 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5840 | local_to_seconds(int year, int month, int day, |
| 5841 | int hour, int minute, int second, int fold); |
| 5842 | |
| 5843 | static PyObject * |
| 5844 | local_timezone_from_local(PyDateTime_DateTime *local_dt) |
| 5845 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5846 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5847 | time_t timestamp; |
| 5848 | seconds = local_to_seconds(GET_YEAR(local_dt), |
| 5849 | GET_MONTH(local_dt), |
| 5850 | GET_DAY(local_dt), |
| 5851 | DATE_GET_HOUR(local_dt), |
| 5852 | DATE_GET_MINUTE(local_dt), |
| 5853 | DATE_GET_SECOND(local_dt), |
| 5854 | DATE_GET_FOLD(local_dt)); |
| 5855 | if (seconds == -1) |
| 5856 | return NULL; |
| 5857 | /* XXX: add bounds check */ |
| 5858 | timestamp = seconds - epoch; |
| 5859 | return local_timezone_from_timestamp(timestamp); |
| 5860 | } |
| 5861 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5862 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5863 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5864 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5865 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5866 | PyObject *offset; |
| 5867 | PyObject *temp; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5868 | PyObject *self_tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5869 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5870 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5871 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5872 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 5873 | &tzinfo)) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5874 | return NULL; |
| 5875 | |
| 5876 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5877 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5878 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5879 | if (!HASTZINFO(self) || self->tzinfo == Py_None) { |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 5880 | naive: |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5881 | self_tzinfo = local_timezone_from_local(self); |
| 5882 | if (self_tzinfo == NULL) |
| 5883 | return NULL; |
| 5884 | } else { |
| 5885 | self_tzinfo = self->tzinfo; |
| 5886 | Py_INCREF(self_tzinfo); |
| 5887 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5889 | /* Conversion to self's own time zone is a NOP. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5890 | if (self_tzinfo == tzinfo) { |
| 5891 | Py_DECREF(self_tzinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5892 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5893 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5894 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5896 | /* Convert self to UTC. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5897 | offset = call_utcoffset(self_tzinfo, (PyObject *)self); |
| 5898 | Py_DECREF(self_tzinfo); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5899 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5900 | return NULL; |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 5901 | else if(offset == Py_None) { |
| 5902 | Py_DECREF(offset); |
| 5903 | goto naive; |
| 5904 | } |
| 5905 | else if (!PyDelta_Check(offset)) { |
| 5906 | Py_DECREF(offset); |
| 5907 | PyErr_Format(PyExc_TypeError, "utcoffset() returned %.200s," |
| 5908 | " expected timedelta or None", Py_TYPE(offset)->tp_name); |
| 5909 | return NULL; |
| 5910 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5911 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5912 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5913 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5914 | Py_DECREF(offset); |
| 5915 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5916 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5917 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5918 | /* Make sure result is aware and UTC. */ |
| 5919 | if (!HASTZINFO(result)) { |
| 5920 | temp = (PyObject *)result; |
| 5921 | result = (PyDateTime_DateTime *) |
| 5922 | new_datetime_ex2(GET_YEAR(result), |
| 5923 | GET_MONTH(result), |
| 5924 | GET_DAY(result), |
| 5925 | DATE_GET_HOUR(result), |
| 5926 | DATE_GET_MINUTE(result), |
| 5927 | DATE_GET_SECOND(result), |
| 5928 | DATE_GET_MICROSECOND(result), |
| 5929 | PyDateTime_TimeZone_UTC, |
| 5930 | DATE_GET_FOLD(result), |
| 5931 | Py_TYPE(result)); |
| 5932 | Py_DECREF(temp); |
| 5933 | if (result == NULL) |
| 5934 | return NULL; |
| 5935 | } |
| 5936 | else { |
| 5937 | /* Result is already aware - just replace tzinfo. */ |
| 5938 | temp = result->tzinfo; |
| 5939 | result->tzinfo = PyDateTime_TimeZone_UTC; |
| 5940 | Py_INCREF(result->tzinfo); |
| 5941 | Py_DECREF(temp); |
| 5942 | } |
| 5943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5944 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5945 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5946 | if (tzinfo == Py_None) { |
| 5947 | tzinfo = local_timezone(result); |
| 5948 | if (tzinfo == NULL) { |
| 5949 | Py_DECREF(result); |
| 5950 | return NULL; |
| 5951 | } |
| 5952 | } |
| 5953 | else |
| 5954 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5955 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5956 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5957 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5958 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5959 | result = (PyDateTime_DateTime *) |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 5960 | _PyObject_CallMethodIdOneArg(tzinfo, &PyId_fromutc, temp); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5961 | Py_DECREF(temp); |
| 5962 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5963 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5967 | datetime_timetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5971 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5972 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5973 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5974 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 5975 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5976 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5977 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5978 | if (dst != Py_None) |
| 5979 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 5980 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5981 | } |
| 5982 | return build_struct_time(GET_YEAR(self), |
| 5983 | GET_MONTH(self), |
| 5984 | GET_DAY(self), |
| 5985 | DATE_GET_HOUR(self), |
| 5986 | DATE_GET_MINUTE(self), |
| 5987 | DATE_GET_SECOND(self), |
| 5988 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5991 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5992 | local_to_seconds(int year, int month, int day, |
| 5993 | int hour, int minute, int second, int fold) |
| 5994 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5995 | long long t, a, b, u1, u2, t1, t2, lt; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5996 | t = utc_to_seconds(year, month, day, hour, minute, second); |
| 5997 | /* Our goal is to solve t = local(u) for u. */ |
| 5998 | lt = local(t); |
| 5999 | if (lt == -1) |
| 6000 | return -1; |
| 6001 | a = lt - t; |
| 6002 | u1 = t - a; |
| 6003 | t1 = local(u1); |
| 6004 | if (t1 == -1) |
| 6005 | return -1; |
| 6006 | if (t1 == t) { |
| 6007 | /* We found one solution, but it may not be the one we need. |
| 6008 | * Look for an earlier solution (if `fold` is 0), or a |
| 6009 | * later one (if `fold` is 1). */ |
| 6010 | if (fold) |
| 6011 | u2 = u1 + max_fold_seconds; |
| 6012 | else |
| 6013 | u2 = u1 - max_fold_seconds; |
| 6014 | lt = local(u2); |
| 6015 | if (lt == -1) |
| 6016 | return -1; |
| 6017 | b = lt - u2; |
| 6018 | if (a == b) |
| 6019 | return u1; |
| 6020 | } |
| 6021 | else { |
| 6022 | b = t1 - u1; |
| 6023 | assert(a != b); |
| 6024 | } |
| 6025 | u2 = t - b; |
| 6026 | t2 = local(u2); |
| 6027 | if (t2 == -1) |
| 6028 | return -1; |
| 6029 | if (t2 == t) |
| 6030 | return u2; |
| 6031 | if (t1 == t) |
| 6032 | return u1; |
| 6033 | /* We have found both offsets a and b, but neither t - a nor t - b is |
| 6034 | * a solution. This means t is in the gap. */ |
| 6035 | return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); |
| 6036 | } |
| 6037 | |
| 6038 | /* date(1970,1,1).toordinal() == 719163 */ |
| 6039 | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) |
| 6040 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6041 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6042 | datetime_timestamp(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6043 | { |
| 6044 | PyObject *result; |
| 6045 | |
| 6046 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 6047 | PyObject *delta; |
| 6048 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 6049 | if (delta == NULL) |
| 6050 | return NULL; |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6051 | result = delta_total_seconds(delta, NULL); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6052 | Py_DECREF(delta); |
| 6053 | } |
| 6054 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 6055 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6056 | seconds = local_to_seconds(GET_YEAR(self), |
| 6057 | GET_MONTH(self), |
| 6058 | GET_DAY(self), |
| 6059 | DATE_GET_HOUR(self), |
| 6060 | DATE_GET_MINUTE(self), |
| 6061 | DATE_GET_SECOND(self), |
| 6062 | DATE_GET_FOLD(self)); |
| 6063 | if (seconds == -1) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6064 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6065 | result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + |
| 6066 | DATE_GET_MICROSECOND(self) / 1e6); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6067 | } |
| 6068 | return result; |
| 6069 | } |
| 6070 | |
| 6071 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6072 | datetime_getdate(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6073 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6074 | return new_date(GET_YEAR(self), |
| 6075 | GET_MONTH(self), |
| 6076 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6077 | } |
| 6078 | |
| 6079 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6080 | datetime_gettime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6081 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6082 | return new_time(DATE_GET_HOUR(self), |
| 6083 | DATE_GET_MINUTE(self), |
| 6084 | DATE_GET_SECOND(self), |
| 6085 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6086 | Py_None, |
| 6087 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6091 | datetime_gettimetz(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6092 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6093 | return new_time(DATE_GET_HOUR(self), |
| 6094 | DATE_GET_MINUTE(self), |
| 6095 | DATE_GET_SECOND(self), |
| 6096 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6097 | GET_DT_TZINFO(self), |
| 6098 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6102 | datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6103 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6104 | int y, m, d, hh, mm, ss; |
| 6105 | PyObject *tzinfo; |
| 6106 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6107 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6108 | tzinfo = GET_DT_TZINFO(self); |
| 6109 | if (tzinfo == Py_None) { |
| 6110 | utcself = self; |
| 6111 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6112 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6113 | else { |
| 6114 | PyObject *offset; |
| 6115 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 6116 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 6117 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6118 | if (offset == Py_None) { |
| 6119 | Py_DECREF(offset); |
| 6120 | utcself = self; |
| 6121 | Py_INCREF(utcself); |
| 6122 | } |
| 6123 | else { |
| 6124 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 6125 | (PyDateTime_Delta *)offset, -1); |
| 6126 | Py_DECREF(offset); |
| 6127 | if (utcself == NULL) |
| 6128 | return NULL; |
| 6129 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6130 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6131 | y = GET_YEAR(utcself); |
| 6132 | m = GET_MONTH(utcself); |
| 6133 | d = GET_DAY(utcself); |
| 6134 | hh = DATE_GET_HOUR(utcself); |
| 6135 | mm = DATE_GET_MINUTE(utcself); |
| 6136 | ss = DATE_GET_SECOND(utcself); |
| 6137 | |
| 6138 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6139 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 6142 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 6143 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6144 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6145 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 6146 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 6147 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6148 | */ |
| 6149 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6150 | datetime_getstate(PyDateTime_DateTime *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6152 | PyObject *basestate; |
| 6153 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6155 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 6156 | _PyDateTime_DATETIME_DATASIZE); |
| 6157 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6158 | if (proto > 3 && DATE_GET_FOLD(self)) |
| 6159 | /* Set the first bit of the third byte */ |
| 6160 | PyBytes_AS_STRING(basestate)[2] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6161 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 6162 | result = PyTuple_Pack(1, basestate); |
| 6163 | else |
| 6164 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 6165 | Py_DECREF(basestate); |
| 6166 | } |
| 6167 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
| 6170 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6171 | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6172 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6173 | int proto; |
| 6174 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6175 | return NULL; |
| 6176 | |
| 6177 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6180 | static PyObject * |
| 6181 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
| 6182 | { |
| 6183 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); |
| 6184 | } |
| 6185 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6186 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6188 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6189 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 6190 | DATETIME_DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6192 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 6193 | METH_NOARGS | METH_CLASS, |
| 6194 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6195 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6196 | {"fromtimestamp", (PyCFunction)(void(*)(void))datetime_fromtimestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6197 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 6198 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6200 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 6201 | METH_VARARGS | METH_CLASS, |
Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 6202 | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6204 | {"strptime", (PyCFunction)datetime_strptime, |
| 6205 | METH_VARARGS | METH_CLASS, |
| 6206 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 6207 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 6208 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6209 | {"combine", (PyCFunction)(void(*)(void))datetime_combine, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6210 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 6211 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6212 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 6213 | {"fromisoformat", (PyCFunction)datetime_fromisoformat, |
| 6214 | METH_O | METH_CLASS, |
| 6215 | PyDoc_STR("string -> datetime from datetime.isoformat() output")}, |
| 6216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6217 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6219 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 6220 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6222 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 6223 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6225 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 6226 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6228 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 6229 | PyDoc_STR("Return ctime() style string.")}, |
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 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 6232 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6233 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6234 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 6235 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 6236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6237 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 6238 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6239 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6240 | {"isoformat", (PyCFunction)(void(*)(void))datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6241 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 6242 | "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6243 | "sep is used to separate the year from the time, and " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 6244 | "defaults to 'T'.\n" |
| 6245 | "timespec specifies what components of the time to include" |
| 6246 | " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," |
| 6247 | " 'milliseconds', and 'microseconds').\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6249 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 6250 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6252 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 6253 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6255 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 6256 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6257 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6258 | {"replace", (PyCFunction)(void(*)(void))datetime_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6259 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 6260 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6261 | {"astimezone", (PyCFunction)(void(*)(void))datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6262 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 6263 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6264 | {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6265 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6266 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6267 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 6268 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 6269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6270 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6271 | }; |
| 6272 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 6273 | static const char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 6274 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 6275 | \n\ |
| 6276 | 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] | 6277 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6278 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6279 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6280 | datetime_add, /* nb_add */ |
| 6281 | datetime_subtract, /* nb_subtract */ |
| 6282 | 0, /* nb_multiply */ |
| 6283 | 0, /* nb_remainder */ |
| 6284 | 0, /* nb_divmod */ |
| 6285 | 0, /* nb_power */ |
| 6286 | 0, /* nb_negative */ |
| 6287 | 0, /* nb_positive */ |
| 6288 | 0, /* nb_absolute */ |
| 6289 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6290 | }; |
| 6291 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 6292 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6293 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6294 | "datetime.datetime", /* tp_name */ |
| 6295 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 6296 | 0, /* tp_itemsize */ |
| 6297 | (destructor)datetime_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6298 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6299 | 0, /* tp_getattr */ |
| 6300 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6301 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6302 | (reprfunc)datetime_repr, /* tp_repr */ |
| 6303 | &datetime_as_number, /* tp_as_number */ |
| 6304 | 0, /* tp_as_sequence */ |
| 6305 | 0, /* tp_as_mapping */ |
| 6306 | (hashfunc)datetime_hash, /* tp_hash */ |
| 6307 | 0, /* tp_call */ |
| 6308 | (reprfunc)datetime_str, /* tp_str */ |
| 6309 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6310 | 0, /* tp_setattro */ |
| 6311 | 0, /* tp_as_buffer */ |
| 6312 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6313 | datetime_doc, /* tp_doc */ |
| 6314 | 0, /* tp_traverse */ |
| 6315 | 0, /* tp_clear */ |
| 6316 | datetime_richcompare, /* tp_richcompare */ |
| 6317 | 0, /* tp_weaklistoffset */ |
| 6318 | 0, /* tp_iter */ |
| 6319 | 0, /* tp_iternext */ |
| 6320 | datetime_methods, /* tp_methods */ |
| 6321 | 0, /* tp_members */ |
| 6322 | datetime_getset, /* tp_getset */ |
| 6323 | &PyDateTime_DateType, /* tp_base */ |
| 6324 | 0, /* tp_dict */ |
| 6325 | 0, /* tp_descr_get */ |
| 6326 | 0, /* tp_descr_set */ |
| 6327 | 0, /* tp_dictoffset */ |
| 6328 | 0, /* tp_init */ |
| 6329 | datetime_alloc, /* tp_alloc */ |
| 6330 | datetime_new, /* tp_new */ |
| 6331 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6332 | }; |
| 6333 | |
| 6334 | /* --------------------------------------------------------------------------- |
| 6335 | * Module methods and initialization. |
| 6336 | */ |
| 6337 | |
| 6338 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6339 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6340 | }; |
| 6341 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6342 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 6343 | * datetime.h. |
| 6344 | */ |
| 6345 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6346 | &PyDateTime_DateType, |
| 6347 | &PyDateTime_DateTimeType, |
| 6348 | &PyDateTime_TimeType, |
| 6349 | &PyDateTime_DeltaType, |
| 6350 | &PyDateTime_TZInfoType, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6351 | NULL, // PyDatetime_TimeZone_UTC not initialized yet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6352 | new_date_ex, |
| 6353 | new_datetime_ex, |
| 6354 | new_time_ex, |
| 6355 | new_delta_ex, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6356 | new_timezone, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6357 | datetime_fromtimestamp, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 6358 | datetime_date_fromtimestamp_capi, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6359 | new_datetime_ex2, |
| 6360 | new_time_ex2 |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6361 | }; |
| 6362 | |
| 6363 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6364 | |
| 6365 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6366 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6367 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6368 | "Fast implementation of the datetime type.", |
| 6369 | -1, |
| 6370 | module_methods, |
| 6371 | NULL, |
| 6372 | NULL, |
| 6373 | NULL, |
| 6374 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6375 | }; |
| 6376 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6377 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6378 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6380 | PyObject *m; /* a module object */ |
| 6381 | PyObject *d; /* its dict */ |
| 6382 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6383 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6385 | m = PyModule_Create(&datetimemodule); |
| 6386 | if (m == NULL) |
| 6387 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6389 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 6390 | return NULL; |
| 6391 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 6392 | return NULL; |
| 6393 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 6394 | return NULL; |
| 6395 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 6396 | return NULL; |
| 6397 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 6398 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6399 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 6400 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6402 | /* timedelta values */ |
| 6403 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6405 | x = new_delta(0, 0, 1, 0); |
| 6406 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6407 | return NULL; |
| 6408 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6410 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 6411 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6412 | return NULL; |
| 6413 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6415 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 6416 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6417 | return NULL; |
| 6418 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6420 | /* date values */ |
| 6421 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6423 | x = new_date(1, 1, 1); |
| 6424 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6425 | return NULL; |
| 6426 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6428 | x = new_date(MAXYEAR, 12, 31); |
| 6429 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6430 | return NULL; |
| 6431 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6433 | x = new_delta(1, 0, 0, 0); |
| 6434 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6435 | return NULL; |
| 6436 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6438 | /* time values */ |
| 6439 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6440 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6441 | x = new_time(0, 0, 0, 0, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6442 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6443 | return NULL; |
| 6444 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6445 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6446 | x = new_time(23, 59, 59, 999999, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6447 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6448 | return NULL; |
| 6449 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6451 | x = new_delta(0, 0, 1, 0); |
| 6452 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6453 | return NULL; |
| 6454 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6456 | /* datetime values */ |
| 6457 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6458 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6459 | 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] | 6460 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6461 | return NULL; |
| 6462 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6463 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6464 | 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] | 6465 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6466 | return NULL; |
| 6467 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6469 | x = new_delta(0, 0, 1, 0); |
| 6470 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6471 | return NULL; |
| 6472 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6473 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6474 | /* timezone values */ |
| 6475 | d = PyDateTime_TimeZoneType.tp_dict; |
| 6476 | |
| 6477 | delta = new_delta(0, 0, 0, 0); |
| 6478 | if (delta == NULL) |
| 6479 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6480 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6481 | Py_DECREF(delta); |
| 6482 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 6483 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 6484 | PyDateTime_TimeZone_UTC = x; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6485 | CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6486 | |
Ngalim Siregar | 92c7e30 | 2019-08-09 21:22:16 +0700 | [diff] [blame] | 6487 | /* bpo-37642: These attributes are rounded to the nearest minute for backwards |
| 6488 | * compatibility, even though the constructor will accept a wider range of |
| 6489 | * values. This may change in the future.*/ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6490 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 6491 | if (delta == NULL) |
| 6492 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6493 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6494 | Py_DECREF(delta); |
| 6495 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6496 | return NULL; |
| 6497 | Py_DECREF(x); |
| 6498 | |
| 6499 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 6500 | if (delta == NULL) |
| 6501 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6502 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6503 | Py_DECREF(delta); |
| 6504 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6505 | return NULL; |
| 6506 | Py_DECREF(x); |
| 6507 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6508 | /* Epoch */ |
| 6509 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6510 | PyDateTime_TimeZone_UTC, 0); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6511 | if (PyDateTime_Epoch == NULL) |
| 6512 | return NULL; |
| 6513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6514 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6515 | PyModule_AddIntMacro(m, MINYEAR); |
| 6516 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6518 | Py_INCREF(&PyDateTime_DateType); |
| 6519 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6521 | Py_INCREF(&PyDateTime_DateTimeType); |
| 6522 | PyModule_AddObject(m, "datetime", |
| 6523 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6525 | Py_INCREF(&PyDateTime_TimeType); |
| 6526 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6528 | Py_INCREF(&PyDateTime_DeltaType); |
| 6529 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6531 | Py_INCREF(&PyDateTime_TZInfoType); |
| 6532 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6533 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6534 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 6535 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 6536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6537 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 6538 | if (x == NULL) |
| 6539 | return NULL; |
| 6540 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6542 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 6543 | * pasting together 4 single years. |
| 6544 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6545 | Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6546 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6548 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 6549 | * get from pasting together 4 100-year cycles. |
| 6550 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6551 | Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6552 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6554 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 6555 | * pasting together 25 4-year cycles. |
| 6556 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6557 | Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6558 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6560 | us_per_ms = PyLong_FromLong(1000); |
| 6561 | us_per_second = PyLong_FromLong(1000000); |
| 6562 | us_per_minute = PyLong_FromLong(60000000); |
| 6563 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 6564 | if (us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6565 | us_per_minute == NULL || seconds_per_day == NULL) |
| 6566 | return NULL; |
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 | /* The rest are too big for 32-bit ints, but even |
| 6569 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 6570 | */ |
| 6571 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 6572 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 6573 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 6574 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 6575 | return NULL; |
| 6576 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6577 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6578 | |
| 6579 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6580 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6581 | x.n = x stripped of its timezone -- its naive time. |
| 6582 | 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] | 6583 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6584 | 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] | 6585 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6586 | x.s = x's standard offset, x.o - x.d |
| 6587 | |
| 6588 | Now some derived rules, where k is a duration (timedelta). |
| 6589 | |
| 6590 | 1. x.o = x.s + x.d |
| 6591 | This follows from the definition of x.s. |
| 6592 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6593 | 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] | 6594 | This is actually a requirement, an assumption we need to make about |
| 6595 | sane tzinfo classes. |
| 6596 | |
| 6597 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 6598 | This is again a requirement for a sane tzinfo class. |
| 6599 | |
| 6600 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6601 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6602 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6603 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6604 | Again follows from how arithmetic is defined. |
| 6605 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6606 | 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] | 6607 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 6608 | None when called). |
| 6609 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6610 | 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] | 6611 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6612 | |
| 6613 | By #3, we want |
| 6614 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6615 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6616 | |
| 6617 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 6618 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 6619 | becomes true; in effect, we want to solve [2] for k: |
| 6620 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6621 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6622 | |
| 6623 | By #1, this is the same as |
| 6624 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6625 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6626 | |
| 6627 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 6628 | Substituting that into [3], |
| 6629 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6630 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 6631 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 6632 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 6633 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6634 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6635 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 6636 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 6637 | very large, since all offset-returning methods return a duration of magnitude |
| 6638 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 6639 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6640 | |
| 6641 | In any case, the new value is |
| 6642 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6643 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6644 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6645 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 6646 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6647 | |
| 6648 | At this point, if |
| 6649 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6650 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6651 | |
| 6652 | 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] | 6653 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 6654 | 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] | 6655 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 6656 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 6657 | 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] | 6658 | the only spelling that makes sense on the local wall clock. |
| 6659 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6660 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 6661 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 6662 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6663 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6664 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6665 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6666 | Now |
| 6667 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6668 | (y + y.s).n = by #5 |
| 6669 | y.n + y.s = since y.n = x.n |
| 6670 | x.n + y.s = since z and y are have the same tzinfo member, |
| 6671 | y.s = z.s by #2 |
| 6672 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6673 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6674 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6675 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6676 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6677 | x.n - ((x.n + z.s) - z.o) = expanding |
| 6678 | x.n - x.n - z.s + z.o = cancelling |
| 6679 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6680 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6681 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6682 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6683 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6684 | 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] | 6685 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 6686 | 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] | 6687 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6688 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 6689 | 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] | 6690 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6691 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6692 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6693 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6694 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6695 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6696 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6697 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6698 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6699 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6700 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 6701 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 6702 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 6703 | the justifications for the kinds of substitutions we've done several times |
| 6704 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6705 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6706 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6707 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 6708 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 6709 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 6710 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 6711 | - z.o + z'.o = #1 twice |
| 6712 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 6713 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6714 | |
| 6715 | 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] | 6716 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 6717 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6718 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6719 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 6720 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 6721 | would have to change the result dst() returns: we start in DST, and moving |
| 6722 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6723 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6724 | There isn't a sane case where this can happen. The closest it gets is at |
| 6725 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 6726 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 6727 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 6728 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 6729 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 6730 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 6731 | standard time. Since that's what the local clock *does*, we want to map both |
| 6732 | 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] | 6733 | in local time, but so it goes -- it's the way the local clock works. |
| 6734 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6735 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 6736 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 6737 | 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] | 6738 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 6739 | |
| 6740 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 6741 | 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] | 6742 | 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] | 6743 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 6744 | but the reasoning doesn't depend on the example -- it depends on there being |
| 6745 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6746 | z' must be in standard time, and is the spelling we want in this case. |
| 6747 | |
| 6748 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 6749 | concerned (because it takes z' as being in standard time rather than the |
| 6750 | daylight time we intend here), but returning it gives the real-life "local |
| 6751 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 6752 | tz. |
| 6753 | |
| 6754 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 6755 | the 1:MM standard time spelling we want. |
| 6756 | |
| 6757 | So how can this break? One of the assumptions must be violated. Two |
| 6758 | possibilities: |
| 6759 | |
| 6760 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 6761 | time zone. This isn't true if, for political reasons or continental drift, |
| 6762 | a region decides to change its base offset from UTC. |
| 6763 | |
| 6764 | 2) There may be versions of "double daylight" time where the tail end of |
| 6765 | the analysis gives up a step too early. I haven't thought about that |
| 6766 | enough to say. |
| 6767 | |
| 6768 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 6769 | "almost all" time zones: so long as the standard offset is invariant, it |
| 6770 | doesn't matter if daylight time transition points change from year to year, or |
| 6771 | if daylight time is skipped in some years; it doesn't matter how large or |
| 6772 | small dst() may get within its bounds; and it doesn't even matter if some |
| 6773 | perverse time zone returns a negative dst()). So a breaking case must be |
| 6774 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6775 | --------------------------------------------------------------------------- */ |