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 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 35 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 36 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 37 | module datetime |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 38 | class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 39 | class datetime.date "PyDateTime_Date *" "&PyDateTime_DateType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 40 | [clinic start generated code]*/ |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 41 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=25138ad6a696b785]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 42 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 43 | #include "clinic/_datetimemodule.c.h" |
| 44 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 45 | /* We require that C int be at least 32 bits, and use int virtually |
| 46 | * everywhere. In just a few cases we use a temp long, where a Python |
| 47 | * API returns a C long. In such cases, we have to ensure that the |
| 48 | * final result fits in a C int (this can be an issue on 64-bit boxes). |
| 49 | */ |
| 50 | #if SIZEOF_INT < 4 |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 51 | # error "_datetime.c requires that C int have at least 32 bits" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | #define MINYEAR 1 |
| 55 | #define MAXYEAR 9999 |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 56 | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 57 | |
| 58 | /* Nine decimal digits is easy to communicate, and leaves enough room |
| 59 | * so that two delta days can be added w/o fear of overflowing a signed |
| 60 | * 32-bit int, and with plenty of room left over to absorb any possible |
| 61 | * carries from adding seconds. |
| 62 | */ |
| 63 | #define MAX_DELTA_DAYS 999999999 |
| 64 | |
| 65 | /* Rename the long macros in datetime.h to more reasonable short names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | #define GET_YEAR PyDateTime_GET_YEAR |
| 67 | #define GET_MONTH PyDateTime_GET_MONTH |
| 68 | #define GET_DAY PyDateTime_GET_DAY |
| 69 | #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
| 70 | #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
| 71 | #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
| 72 | #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 73 | #define DATE_GET_FOLD PyDateTime_DATE_GET_FOLD |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 74 | |
| 75 | /* Date accessors for date and datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
| 77 | ((o)->data[1] = ((v) & 0x00ff))) |
| 78 | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
| 79 | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 80 | |
| 81 | /* Date/Time accessors for datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
| 83 | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
| 84 | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
| 85 | #define DATE_SET_MICROSECOND(o, v) \ |
| 86 | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
| 87 | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
| 88 | ((o)->data[9] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 89 | #define DATE_SET_FOLD(o, v) (PyDateTime_DATE_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 90 | |
| 91 | /* Time accessors for time. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
| 93 | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
| 94 | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
| 95 | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 96 | #define TIME_GET_FOLD PyDateTime_TIME_GET_FOLD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
| 98 | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
| 99 | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
| 100 | #define TIME_SET_MICROSECOND(o, v) \ |
| 101 | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
| 102 | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
| 103 | ((o)->data[5] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 104 | #define TIME_SET_FOLD(o, v) (PyDateTime_TIME_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 105 | |
| 106 | /* Delta accessors for timedelta. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
| 108 | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
| 109 | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
| 112 | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 113 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
| 114 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 115 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
| 116 | * p->hastzinfo. |
| 117 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 118 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
| 119 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
| 120 | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
| 121 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
| 122 | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 123 | /* M is a char or int claiming to be a valid month. The macro is equivalent |
| 124 | * to the two-sided Python test |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | * 1 <= M <= 12 |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 126 | */ |
| 127 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
| 128 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 129 | /* Forward declarations. */ |
| 130 | static PyTypeObject PyDateTime_DateType; |
| 131 | static PyTypeObject PyDateTime_DateTimeType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 132 | static PyTypeObject PyDateTime_DeltaType; |
| 133 | static PyTypeObject PyDateTime_TimeType; |
| 134 | static PyTypeObject PyDateTime_TZInfoType; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 135 | static PyTypeObject PyDateTime_TimeZoneType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 136 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 137 | static int check_tzinfo_subclass(PyObject *p); |
| 138 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 139 | _Py_IDENTIFIER(as_integer_ratio); |
| 140 | _Py_IDENTIFIER(fromutc); |
| 141 | _Py_IDENTIFIER(isoformat); |
| 142 | _Py_IDENTIFIER(strftime); |
| 143 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 144 | /* --------------------------------------------------------------------------- |
| 145 | * Math utilities. |
| 146 | */ |
| 147 | |
| 148 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 149 | * iff k^i has sign bit set and k^j has sign bit set, |
| 150 | * iff (k^i)&(k^j) has sign bit set. |
| 151 | */ |
| 152 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 154 | |
| 155 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 156 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 157 | * the real point of this. C will probably truncate instead (C99 |
| 158 | * requires truncation; C89 left it implementation-defined). |
| 159 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 160 | * for all the uses made of it. This simplifies the code and makes |
| 161 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 162 | * overflow case). |
| 163 | */ |
| 164 | static int |
| 165 | divmod(int x, int y, int *r) |
| 166 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | assert(y > 0); |
| 170 | quo = x / y; |
| 171 | *r = x - quo * y; |
| 172 | if (*r < 0) { |
| 173 | --quo; |
| 174 | *r += y; |
| 175 | } |
| 176 | assert(0 <= *r && *r < y); |
| 177 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 180 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 181 | * are rounded to even. |
| 182 | */ |
| 183 | static PyObject * |
| 184 | divide_nearest(PyObject *m, PyObject *n) |
| 185 | { |
| 186 | PyObject *result; |
| 187 | PyObject *temp; |
| 188 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 189 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 190 | if (temp == NULL) |
| 191 | return NULL; |
| 192 | result = PyTuple_GET_ITEM(temp, 0); |
| 193 | Py_INCREF(result); |
| 194 | Py_DECREF(temp); |
| 195 | |
| 196 | return result; |
| 197 | } |
| 198 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 199 | /* --------------------------------------------------------------------------- |
| 200 | * General calendrical helper functions |
| 201 | */ |
| 202 | |
| 203 | /* For each month ordinal in 1..12, the number of days in that month, |
| 204 | * and the number of days before that month in the same year. These |
| 205 | * are correct for non-leap years only. |
| 206 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 207 | static const int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | 0, /* unused; this vector uses 1-based indexing */ |
| 209 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 212 | static const int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | 0, /* unused; this vector uses 1-based indexing */ |
| 214 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /* year -> 1 if leap year, else 0. */ |
| 218 | static int |
| 219 | is_leap(int year) |
| 220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | /* Cast year to unsigned. The result is the same either way, but |
| 222 | * C can generate faster code for unsigned mod than for signed |
| 223 | * mod (especially for % 4 -- a good compiler should just grab |
| 224 | * the last 2 bits when the LHS is unsigned). |
| 225 | */ |
| 226 | const unsigned int ayear = (unsigned int)year; |
| 227 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* year, month -> number of days in that month in that year */ |
| 231 | static int |
| 232 | days_in_month(int year, int month) |
| 233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | assert(month >= 1); |
| 235 | assert(month <= 12); |
| 236 | if (month == 2 && is_leap(year)) |
| 237 | return 29; |
| 238 | else |
| 239 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 242 | /* year, month -> number of days in year preceding first day of month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 243 | static int |
| 244 | days_before_month(int year, int month) |
| 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | assert(month >= 1); |
| 249 | assert(month <= 12); |
| 250 | days = _days_before_month[month]; |
| 251 | if (month > 2 && is_leap(year)) |
| 252 | ++days; |
| 253 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* year -> number of days before January 1st of year. Remember that we |
| 257 | * start with year 1, so days_before_year(1) == 0. |
| 258 | */ |
| 259 | static int |
| 260 | days_before_year(int year) |
| 261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | int y = year - 1; |
| 263 | /* This is incorrect if year <= 0; we really want the floor |
| 264 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 265 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 267 | assert (year >= 1); |
| 268 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 272 | * the correct values is asserted in the module init function. |
| 273 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 275 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 276 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 277 | |
| 278 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 279 | static void |
| 280 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 285 | * leap years repeats exactly every 400 years. The basic strategy is |
| 286 | * to find the closest 400-year boundary at or before ordinal, then |
| 287 | * work with the offset from that boundary to ordinal. Life is much |
| 288 | * clearer if we subtract 1 from ordinal first -- then the values |
| 289 | * of ordinal at 400-year boundaries are exactly those divisible |
| 290 | * by DI400Y: |
| 291 | * |
| 292 | * D M Y n n-1 |
| 293 | * -- --- ---- ---------- ---------------- |
| 294 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 295 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 296 | * ... |
| 297 | * 30 Dec 000 -1 -2 |
| 298 | * 31 Dec 000 0 -1 |
| 299 | * 1 Jan 001 1 0 400-year boundary |
| 300 | * 2 Jan 001 2 1 |
| 301 | * 3 Jan 001 3 2 |
| 302 | * ... |
| 303 | * 31 Dec 400 DI400Y DI400Y -1 |
| 304 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 305 | */ |
| 306 | assert(ordinal >= 1); |
| 307 | --ordinal; |
| 308 | n400 = ordinal / DI400Y; |
| 309 | n = ordinal % DI400Y; |
| 310 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 313 | * year, to the desired date. Now compute how many 100-year cycles |
| 314 | * precede n. |
| 315 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 316 | * 100-year cycles precede the desired day, which implies the |
| 317 | * desired day is December 31 at the end of a 400-year cycle. |
| 318 | */ |
| 319 | n100 = n / DI100Y; |
| 320 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | /* Now compute how many 4-year cycles precede it. */ |
| 323 | n4 = n / DI4Y; |
| 324 | n = n % DI4Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | /* And now how many single years. Again n1 can be 4, and again |
| 327 | * meaning that the desired day is December 31 at the end of the |
| 328 | * 4-year cycle. |
| 329 | */ |
| 330 | n1 = n / 365; |
| 331 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | *year += n100 * 100 + n4 * 4 + n1; |
| 334 | if (n1 == 4 || n100 == 4) { |
| 335 | assert(n == 0); |
| 336 | *year -= 1; |
| 337 | *month = 12; |
| 338 | *day = 31; |
| 339 | return; |
| 340 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | /* Now the year is correct, and n is the offset from January 1. We |
| 343 | * find the month via an estimate that's either exact or one too |
| 344 | * large. |
| 345 | */ |
| 346 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 347 | assert(leapyear == is_leap(*year)); |
| 348 | *month = (n + 50) >> 5; |
| 349 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 350 | if (preceding > n) { |
| 351 | /* estimate is too large */ |
| 352 | *month -= 1; |
| 353 | preceding -= days_in_month(*year, *month); |
| 354 | } |
| 355 | n -= preceding; |
| 356 | assert(0 <= n); |
| 357 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 363 | static int |
| 364 | ymd_to_ord(int year, int month, int day) |
| 365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 370 | static int |
| 371 | weekday(int year, int month, int day) |
| 372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 377 | * first calendar week containing a Thursday. |
| 378 | */ |
| 379 | static int |
| 380 | iso_week1_monday(int year) |
| 381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 383 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 384 | int first_weekday = (first_day + 6) % 7; |
| 385 | /* ordinal of closest Monday at or before 1/1 */ |
| 386 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 389 | week1_monday += 7; |
| 390 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* --------------------------------------------------------------------------- |
| 394 | * Range checkers. |
| 395 | */ |
| 396 | |
| 397 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 398 | * If not, raise OverflowError and return -1. |
| 399 | */ |
| 400 | static int |
| 401 | check_delta_day_range(int days) |
| 402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 404 | return 0; |
| 405 | PyErr_Format(PyExc_OverflowError, |
| 406 | "days=%d; must have magnitude <= %d", |
| 407 | days, MAX_DELTA_DAYS); |
| 408 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 412 | * aren't, raise ValueError and return -1. |
| 413 | */ |
| 414 | static int |
| 415 | check_date_args(int year, int month, int day) |
| 416 | { |
| 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | if (year < MINYEAR || year > MAXYEAR) { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 419 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | if (month < 1 || month > 12) { |
| 423 | PyErr_SetString(PyExc_ValueError, |
| 424 | "month must be in 1..12"); |
| 425 | return -1; |
| 426 | } |
| 427 | if (day < 1 || day > days_in_month(year, month)) { |
| 428 | PyErr_SetString(PyExc_ValueError, |
| 429 | "day is out of range for month"); |
| 430 | return -1; |
| 431 | } |
| 432 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 436 | * aren't, raise ValueError and return -1. |
| 437 | */ |
| 438 | static int |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 439 | 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] | 440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | if (h < 0 || h > 23) { |
| 442 | PyErr_SetString(PyExc_ValueError, |
| 443 | "hour must be in 0..23"); |
| 444 | return -1; |
| 445 | } |
| 446 | if (m < 0 || m > 59) { |
| 447 | PyErr_SetString(PyExc_ValueError, |
| 448 | "minute must be in 0..59"); |
| 449 | return -1; |
| 450 | } |
| 451 | if (s < 0 || s > 59) { |
| 452 | PyErr_SetString(PyExc_ValueError, |
| 453 | "second must be in 0..59"); |
| 454 | return -1; |
| 455 | } |
| 456 | if (us < 0 || us > 999999) { |
| 457 | PyErr_SetString(PyExc_ValueError, |
| 458 | "microsecond must be in 0..999999"); |
| 459 | return -1; |
| 460 | } |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 461 | if (fold != 0 && fold != 1) { |
| 462 | PyErr_SetString(PyExc_ValueError, |
| 463 | "fold must be either 0 or 1"); |
| 464 | return -1; |
| 465 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* --------------------------------------------------------------------------- |
| 470 | * Normalization utilities. |
| 471 | */ |
| 472 | |
| 473 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 474 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 475 | * at least factor, enough of *lo is converted into "hi" units so that |
| 476 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 477 | * is impossible. |
| 478 | */ |
| 479 | static void |
| 480 | normalize_pair(int *hi, int *lo, int factor) |
| 481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | assert(factor > 0); |
| 483 | assert(lo != hi); |
| 484 | if (*lo < 0 || *lo >= factor) { |
| 485 | const int num_hi = divmod(*lo, factor, lo); |
| 486 | const int new_hi = *hi + num_hi; |
| 487 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 488 | *hi = new_hi; |
| 489 | } |
| 490 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | * 0 <= *s < 24*3600 |
| 495 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 496 | * The input values must be such that the internals don't overflow. |
| 497 | * The way this routine is used, we don't get close. |
| 498 | */ |
| 499 | static void |
| 500 | normalize_d_s_us(int *d, int *s, int *us) |
| 501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | if (*us < 0 || *us >= 1000000) { |
| 503 | normalize_pair(s, us, 1000000); |
| 504 | /* |s| can't be bigger than about |
| 505 | * |original s| + |original us|/1000000 now. |
| 506 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | } |
| 509 | if (*s < 0 || *s >= 24*3600) { |
| 510 | normalize_pair(d, s, 24*3600); |
| 511 | /* |d| can't be bigger than about |
| 512 | * |original d| + |
| 513 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 514 | */ |
| 515 | } |
| 516 | assert(0 <= *s && *s < 24*3600); |
| 517 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | * 1 <= *m <= 12 |
| 522 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 523 | * The input values must be such that the internals don't overflow. |
| 524 | * The way this routine is used, we don't get close. |
| 525 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 526 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 527 | normalize_y_m_d(int *y, int *m, int *d) |
| 528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 530 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 531 | /* In actual use, m is always the month component extracted from a |
| 532 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | /* Now only day can be out of bounds (year may also be out of bounds |
| 538 | * for a datetime object, but we don't care about that here). |
| 539 | * If day is out of bounds, what to do is arguable, but at least the |
| 540 | * method here is principled and explainable. |
| 541 | */ |
| 542 | dim = days_in_month(*y, *m); |
| 543 | if (*d < 1 || *d > dim) { |
| 544 | /* Move day-1 days from the first of the month. First try to |
| 545 | * get off cheap if we're only one day out of range |
| 546 | * (adjustments for timezone alone can't be worse than that). |
| 547 | */ |
| 548 | if (*d == 0) { |
| 549 | --*m; |
| 550 | if (*m > 0) |
| 551 | *d = days_in_month(*y, *m); |
| 552 | else { |
| 553 | --*y; |
| 554 | *m = 12; |
| 555 | *d = 31; |
| 556 | } |
| 557 | } |
| 558 | else if (*d == dim + 1) { |
| 559 | /* move forward a day */ |
| 560 | ++*m; |
| 561 | *d = 1; |
| 562 | if (*m > 12) { |
| 563 | *m = 1; |
| 564 | ++*y; |
| 565 | } |
| 566 | } |
| 567 | else { |
| 568 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 569 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 570 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 571 | goto error; |
| 572 | } else { |
| 573 | ord_to_ymd(ordinal, y, m, d); |
| 574 | return 0; |
| 575 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | assert(*m > 0); |
| 579 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 580 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 581 | return 0; |
| 582 | error: |
| 583 | PyErr_SetString(PyExc_OverflowError, |
| 584 | "date value out of range"); |
| 585 | return -1; |
| 586 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 590 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 591 | * failure, where failure means the adjusted year is out of bounds. |
| 592 | */ |
| 593 | static int |
| 594 | normalize_date(int *year, int *month, int *day) |
| 595 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 596 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* Force all the datetime fields into range. The parameters are both |
| 600 | * inputs and outputs. Returns < 0 on error. |
| 601 | */ |
| 602 | static int |
| 603 | normalize_datetime(int *year, int *month, int *day, |
| 604 | int *hour, int *minute, int *second, |
| 605 | int *microsecond) |
| 606 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | normalize_pair(second, microsecond, 1000000); |
| 608 | normalize_pair(minute, second, 60); |
| 609 | normalize_pair(hour, minute, 60); |
| 610 | normalize_pair(day, hour, 24); |
| 611 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 615 | * Basic object allocation: tp_alloc implementations. These allocate |
| 616 | * Python objects of the right size and type, and do the Python object- |
| 617 | * initialization bit. If there's not enough memory, they return NULL after |
| 618 | * setting MemoryError. All data members remain uninitialized trash. |
| 619 | * |
| 620 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 621 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 622 | * tp_basicsize for the time and datetime types is set to the size of the |
| 623 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 624 | * allocate enough space for a tzinfo member whether or not one is actually |
| 625 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 626 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 627 | * using) just happens today to effectively ignore the nitems argument |
| 628 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 629 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 630 | * be changed to force a 0 nitems argument unless the type being allocated |
| 631 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 632 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 633 | */ |
| 634 | |
| 635 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 636 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | self = (PyObject *) |
| 641 | PyObject_MALLOC(aware ? |
| 642 | sizeof(PyDateTime_Time) : |
| 643 | sizeof(_PyDateTime_BaseTime)); |
| 644 | if (self == NULL) |
| 645 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 646 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 651 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | self = (PyObject *) |
| 656 | PyObject_MALLOC(aware ? |
| 657 | sizeof(PyDateTime_DateTime) : |
| 658 | sizeof(_PyDateTime_BaseDateTime)); |
| 659 | if (self == NULL) |
| 660 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 661 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* --------------------------------------------------------------------------- |
| 666 | * Helpers for setting object fields. These work on pointers to the |
| 667 | * appropriate base class. |
| 668 | */ |
| 669 | |
| 670 | /* For date and datetime. */ |
| 671 | static void |
| 672 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | self->hashcode = -1; |
| 675 | SET_YEAR(self, y); |
| 676 | SET_MONTH(self, m); |
| 677 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* --------------------------------------------------------------------------- |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 681 | * String parsing utilities and helper functions |
| 682 | */ |
| 683 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 684 | static const char * |
| 685 | parse_digits(const char *ptr, int *var, size_t num_digits) |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 686 | { |
| 687 | for (size_t i = 0; i < num_digits; ++i) { |
| 688 | unsigned int tmp = (unsigned int)(*(ptr++) - '0'); |
| 689 | if (tmp > 9) { |
| 690 | return NULL; |
| 691 | } |
| 692 | *var *= 10; |
| 693 | *var += (signed int)tmp; |
| 694 | } |
| 695 | |
| 696 | return ptr; |
| 697 | } |
| 698 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 699 | static int |
| 700 | parse_isoformat_date(const char *dtstr, int *year, int *month, int *day) |
| 701 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 702 | /* Parse the date components of the result of date.isoformat() |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 703 | * |
| 704 | * Return codes: |
| 705 | * 0: Success |
| 706 | * -1: Failed to parse date component |
| 707 | * -2: Failed to parse dateseparator |
| 708 | */ |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 709 | const char *p = dtstr; |
| 710 | p = parse_digits(p, year, 4); |
| 711 | if (NULL == p) { |
| 712 | return -1; |
| 713 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 714 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 715 | if (*(p++) != '-') { |
| 716 | return -2; |
| 717 | } |
| 718 | |
| 719 | p = parse_digits(p, month, 2); |
| 720 | if (NULL == p) { |
| 721 | return -1; |
| 722 | } |
| 723 | |
| 724 | if (*(p++) != '-') { |
| 725 | return -2; |
| 726 | } |
| 727 | |
| 728 | p = parse_digits(p, day, 2); |
| 729 | if (p == NULL) { |
| 730 | return -1; |
| 731 | } |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static int |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 737 | parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour, |
| 738 | int *minute, int *second, int *microsecond) |
| 739 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 740 | const char *p = tstr; |
| 741 | const char *p_end = tstr_end; |
| 742 | int *vals[3] = {hour, minute, second}; |
| 743 | |
| 744 | // Parse [HH[:MM[:SS]]] |
| 745 | for (size_t i = 0; i < 3; ++i) { |
| 746 | p = parse_digits(p, vals[i], 2); |
| 747 | if (NULL == p) { |
| 748 | return -3; |
| 749 | } |
| 750 | |
| 751 | char c = *(p++); |
| 752 | if (p >= p_end) { |
| 753 | return c != '\0'; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 754 | } |
| 755 | else if (c == ':') { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 756 | continue; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 757 | } |
| 758 | else if (c == '.') { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 759 | break; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 760 | } |
| 761 | else { |
| 762 | return -4; // Malformed time separator |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | // Parse .fff[fff] |
| 767 | size_t len_remains = p_end - p; |
| 768 | if (!(len_remains == 6 || len_remains == 3)) { |
| 769 | return -3; |
| 770 | } |
| 771 | |
| 772 | p = parse_digits(p, microsecond, len_remains); |
| 773 | if (NULL == p) { |
| 774 | return -3; |
| 775 | } |
| 776 | |
| 777 | if (len_remains == 3) { |
| 778 | *microsecond *= 1000; |
| 779 | } |
| 780 | |
| 781 | // Return 1 if it's not the end of the string |
| 782 | return *p != '\0'; |
| 783 | } |
| 784 | |
| 785 | static int |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 786 | parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute, |
| 787 | int *second, int *microsecond, int *tzoffset, |
| 788 | int *tzmicrosecond) |
| 789 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 790 | // Parse the time portion of a datetime.isoformat() string |
| 791 | // |
| 792 | // Return codes: |
| 793 | // 0: Success (no tzoffset) |
| 794 | // 1: Success (with tzoffset) |
| 795 | // -3: Failed to parse time component |
| 796 | // -4: Failed to parse time separator |
| 797 | // -5: Malformed timezone string |
| 798 | |
| 799 | const char *p = dtstr; |
| 800 | const char *p_end = dtstr + dtlen; |
| 801 | |
| 802 | const char *tzinfo_pos = p; |
| 803 | do { |
| 804 | if (*tzinfo_pos == '+' || *tzinfo_pos == '-') { |
| 805 | break; |
| 806 | } |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 807 | } while (++tzinfo_pos < p_end); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 808 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 809 | int rv = parse_hh_mm_ss_ff(dtstr, tzinfo_pos, hour, minute, second, |
| 810 | microsecond); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 811 | |
| 812 | if (rv < 0) { |
| 813 | return rv; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 814 | } |
| 815 | else if (tzinfo_pos == p_end) { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 816 | // We know that there's no time zone, so if there's stuff at the |
| 817 | // end of the string it's an error. |
| 818 | if (rv == 1) { |
| 819 | return -5; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 820 | } |
| 821 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 822 | return 0; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | // Parse time zone component |
| 827 | // Valid formats are: |
| 828 | // - +HH:MM (len 6) |
| 829 | // - +HH:MM:SS (len 9) |
| 830 | // - +HH:MM:SS.ffffff (len 16) |
| 831 | size_t tzlen = p_end - tzinfo_pos; |
| 832 | if (!(tzlen == 6 || tzlen == 9 || tzlen == 16)) { |
| 833 | return -5; |
| 834 | } |
| 835 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 836 | int tzsign = (*tzinfo_pos == '-') ? -1 : 1; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 837 | tzinfo_pos++; |
| 838 | int tzhour = 0, tzminute = 0, tzsecond = 0; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 839 | rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, &tzhour, &tzminute, &tzsecond, |
| 840 | tzmicrosecond); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 841 | |
| 842 | *tzoffset = tzsign * ((tzhour * 3600) + (tzminute * 60) + tzsecond); |
| 843 | *tzmicrosecond *= tzsign; |
| 844 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 845 | return rv ? -5 : 1; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 848 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 849 | * Create various objects, mostly without range checking. |
| 850 | */ |
| 851 | |
| 852 | /* Create a date instance with no range checking. */ |
| 853 | static PyObject * |
| 854 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 855 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 857 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 858 | if (check_date_args(year, month, day) < 0) { |
| 859 | return NULL; |
| 860 | } |
| 861 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 862 | self = (PyDateTime_Date *)(type->tp_alloc(type, 0)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | if (self != NULL) |
| 864 | set_date_fields(self, year, month, day); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 865 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 870 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 871 | // Forward declaration |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 872 | static PyObject * |
| 873 | new_datetime_ex(int, int, int, int, int, int, int, PyObject *, PyTypeObject *); |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 874 | |
| 875 | /* Create date instance with no range checking, or call subclass constructor */ |
| 876 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 877 | new_date_subclass_ex(int year, int month, int day, PyObject *cls) |
| 878 | { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 879 | PyObject *result; |
| 880 | // We have "fast path" constructors for two subclasses: date and datetime |
| 881 | if ((PyTypeObject *)cls == &PyDateTime_DateType) { |
| 882 | result = new_date_ex(year, month, day, (PyTypeObject *)cls); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 883 | } |
| 884 | else if ((PyTypeObject *)cls == &PyDateTime_DateTimeType) { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 885 | result = new_datetime_ex(year, month, day, 0, 0, 0, 0, Py_None, |
| 886 | (PyTypeObject *)cls); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 887 | } |
| 888 | else { |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 889 | result = PyObject_CallFunction(cls, "iii", year, month, day); |
| 890 | } |
| 891 | |
| 892 | return result; |
| 893 | } |
| 894 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 895 | /* Create a datetime instance with no range checking. */ |
| 896 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 897 | new_datetime_ex2(int year, int month, int day, int hour, int minute, |
| 898 | int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | PyDateTime_DateTime *self; |
| 901 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 902 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 903 | if (check_date_args(year, month, day) < 0) { |
| 904 | return NULL; |
| 905 | } |
| 906 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 907 | return NULL; |
| 908 | } |
| 909 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 910 | return NULL; |
| 911 | } |
| 912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 914 | if (self != NULL) { |
| 915 | self->hastzinfo = aware; |
| 916 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 917 | DATE_SET_HOUR(self, hour); |
| 918 | DATE_SET_MINUTE(self, minute); |
| 919 | DATE_SET_SECOND(self, second); |
| 920 | DATE_SET_MICROSECOND(self, usecond); |
| 921 | if (aware) { |
| 922 | Py_INCREF(tzinfo); |
| 923 | self->tzinfo = tzinfo; |
| 924 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 925 | DATE_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | } |
| 927 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 930 | static PyObject * |
| 931 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
| 932 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
| 933 | { |
| 934 | return new_datetime_ex2(year, month, day, hour, minute, second, usecond, |
| 935 | tzinfo, 0, type); |
| 936 | } |
| 937 | |
| 938 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ |
| 939 | new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 941 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 942 | static PyObject * |
| 943 | new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute, |
| 944 | int second, int usecond, PyObject *tzinfo, |
| 945 | int fold, PyObject *cls) { |
| 946 | PyObject* dt; |
| 947 | if ((PyTypeObject*)cls == &PyDateTime_DateTimeType) { |
| 948 | // Use the fast path constructor |
| 949 | dt = new_datetime(year, month, day, hour, minute, second, usecond, |
| 950 | tzinfo, fold); |
| 951 | } else { |
| 952 | // Subclass |
| 953 | dt = PyObject_CallFunction(cls, "iiiiiiiO", |
| 954 | year, |
| 955 | month, |
| 956 | day, |
| 957 | hour, |
| 958 | minute, |
| 959 | second, |
| 960 | usecond, |
| 961 | tzinfo); |
| 962 | } |
| 963 | |
| 964 | return dt; |
| 965 | } |
| 966 | |
| 967 | static PyObject * |
| 968 | new_datetime_subclass_ex(int year, int month, int day, int hour, int minute, |
| 969 | int second, int usecond, PyObject *tzinfo, |
| 970 | PyObject *cls) { |
| 971 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 972 | second, usecond, tzinfo, 0, |
| 973 | cls); |
| 974 | } |
| 975 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 976 | /* Create a time instance with no range checking. */ |
| 977 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 978 | new_time_ex2(int hour, int minute, int second, int usecond, |
| 979 | PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | PyDateTime_Time *self; |
| 982 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 983 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 984 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 985 | return NULL; |
| 986 | } |
| 987 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 988 | return NULL; |
| 989 | } |
| 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 992 | if (self != NULL) { |
| 993 | self->hastzinfo = aware; |
| 994 | self->hashcode = -1; |
| 995 | TIME_SET_HOUR(self, hour); |
| 996 | TIME_SET_MINUTE(self, minute); |
| 997 | TIME_SET_SECOND(self, second); |
| 998 | TIME_SET_MICROSECOND(self, usecond); |
| 999 | if (aware) { |
| 1000 | Py_INCREF(tzinfo); |
| 1001 | self->tzinfo = tzinfo; |
| 1002 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1003 | TIME_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | } |
| 1005 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1008 | static PyObject * |
| 1009 | new_time_ex(int hour, int minute, int second, int usecond, |
| 1010 | PyObject *tzinfo, PyTypeObject *type) |
| 1011 | { |
| 1012 | return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); |
| 1013 | } |
| 1014 | |
| 1015 | #define new_time(hh, mm, ss, us, tzinfo, fold) \ |
| 1016 | new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1017 | |
| 1018 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 1019 | * true. Passing false is a speed optimization, if you know for sure |
| 1020 | * that seconds and microseconds are already in their proper ranges. In any |
| 1021 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 1022 | * of range). |
| 1023 | */ |
| 1024 | static PyObject * |
| 1025 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | if (normalize) |
| 1031 | normalize_d_s_us(&days, &seconds, µseconds); |
| 1032 | assert(0 <= seconds && seconds < 24*3600); |
| 1033 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | if (check_delta_day_range(days) < 0) |
| 1036 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 1039 | if (self != NULL) { |
| 1040 | self->hashcode = -1; |
| 1041 | SET_TD_DAYS(self, days); |
| 1042 | SET_TD_SECONDS(self, seconds); |
| 1043 | SET_TD_MICROSECONDS(self, microseconds); |
| 1044 | } |
| 1045 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | #define new_delta(d, s, us, normalize) \ |
| 1049 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1050 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1051 | |
| 1052 | typedef struct |
| 1053 | { |
| 1054 | PyObject_HEAD |
| 1055 | PyObject *offset; |
| 1056 | PyObject *name; |
| 1057 | } PyDateTime_TimeZone; |
| 1058 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1059 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1060 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 1061 | /* The interned Epoch datetime instance */ |
| 1062 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 1063 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1064 | /* Create new timezone instance checking offset range. This |
| 1065 | function does not check the name argument. Caller must assure |
| 1066 | that offset is a timedelta instance and name is either NULL |
| 1067 | or a unicode object. */ |
| 1068 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1069 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1070 | { |
| 1071 | PyDateTime_TimeZone *self; |
| 1072 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 1073 | |
| 1074 | assert(offset != NULL); |
| 1075 | assert(PyDelta_Check(offset)); |
| 1076 | assert(name == NULL || PyUnicode_Check(name)); |
| 1077 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1078 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 1079 | if (self == NULL) { |
| 1080 | return NULL; |
| 1081 | } |
| 1082 | Py_INCREF(offset); |
| 1083 | self->offset = offset; |
| 1084 | Py_XINCREF(name); |
| 1085 | self->name = name; |
| 1086 | return (PyObject *)self; |
| 1087 | } |
| 1088 | |
| 1089 | static int delta_bool(PyDateTime_Delta *self); |
| 1090 | |
| 1091 | static PyObject * |
| 1092 | new_timezone(PyObject *offset, PyObject *name) |
| 1093 | { |
| 1094 | assert(offset != NULL); |
| 1095 | assert(PyDelta_Check(offset)); |
| 1096 | assert(name == NULL || PyUnicode_Check(name)); |
| 1097 | |
| 1098 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 1099 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1100 | return PyDateTime_TimeZone_UTC; |
| 1101 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1102 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 1103 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1104 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1105 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 1106 | " timedelta(hours=24)," |
| 1107 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1108 | return NULL; |
| 1109 | } |
| 1110 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1111 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1114 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1115 | * tzinfo helpers. |
| 1116 | */ |
| 1117 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1118 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 1119 | * raise TypeError and return -1. |
| 1120 | */ |
| 1121 | static int |
| 1122 | check_tzinfo_subclass(PyObject *p) |
| 1123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | if (p == Py_None || PyTZInfo_Check(p)) |
| 1125 | return 0; |
| 1126 | PyErr_Format(PyExc_TypeError, |
| 1127 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 1128 | "not type '%s'", |
| 1129 | Py_TYPE(p)->tp_name); |
| 1130 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1133 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 1134 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 1135 | * and the caller must not decref the result. |
| 1136 | */ |
| 1137 | static PyObject * |
| 1138 | get_tzinfo_member(PyObject *self) |
| 1139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 1143 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 1144 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 1145 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1150 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 1151 | * be an instance of the tzinfo class. If the method returns None, this |
| 1152 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 1153 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 1154 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 1155 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1156 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1157 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1158 | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) |
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 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1163 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1165 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1166 | if (tzinfo == Py_None) |
| 1167 | Py_RETURN_NONE; |
| 1168 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 1169 | if (offset == Py_None || offset == NULL) |
| 1170 | return offset; |
| 1171 | if (PyDelta_Check(offset)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1172 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 1173 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1174 | Py_DECREF(offset); |
| 1175 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1176 | " strictly between -timedelta(hours=24) and" |
| 1177 | " timedelta(hours=24)."); |
| 1178 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | else { |
| 1182 | PyErr_Format(PyExc_TypeError, |
| 1183 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1184 | "timedelta, not '%.200s'", |
| 1185 | name, Py_TYPE(offset)->tp_name); |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 1186 | Py_DECREF(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1187 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1189 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1190 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 1194 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 1195 | * 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] | 1196 | * 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] | 1197 | * If utcoffset() returns an out of range timedelta, |
| 1198 | * ValueError is raised and this returns -1. Else *none is |
| 1199 | * 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] | 1200 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1201 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1202 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 1203 | { |
| 1204 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1207 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 1208 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 1209 | * 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] | 1210 | * doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 1211 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 1212 | * 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] | 1213 | * the offset is returned (as timedelta, positive east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1214 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1215 | static PyObject * |
| 1216 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1217 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1218 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1221 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1222 | * 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] | 1223 | * 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] | 1224 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 1225 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1226 | */ |
| 1227 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1228 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1231 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | assert(tzinfo != NULL); |
| 1234 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 1235 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1238 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1239 | |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1240 | result = _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_tzname, |
| 1241 | tzinfoarg, NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1242 | |
| 1243 | if (result == NULL || result == Py_None) |
| 1244 | return result; |
| 1245 | |
| 1246 | if (!PyUnicode_Check(result)) { |
| 1247 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 1248 | "return None or a string, not '%s'", |
| 1249 | Py_TYPE(result)->tp_name); |
| 1250 | Py_DECREF(result); |
| 1251 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1253 | |
| 1254 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1257 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 1258 | * stuff |
| 1259 | * ", tzinfo=" + repr(tzinfo) |
| 1260 | * before the closing ")". |
| 1261 | */ |
| 1262 | static PyObject * |
| 1263 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 1264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | assert(PyUnicode_Check(repr)); |
| 1268 | assert(tzinfo); |
| 1269 | if (tzinfo == Py_None) |
| 1270 | return repr; |
| 1271 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1272 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1273 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | Py_DECREF(repr); |
| 1275 | if (temp == NULL) |
| 1276 | return NULL; |
| 1277 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 1278 | Py_DECREF(temp); |
| 1279 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1282 | /* repr is like "someclass(arg1, arg2)". If fold isn't 0, |
| 1283 | * stuff |
| 1284 | * ", fold=" + repr(tzinfo) |
| 1285 | * before the closing ")". |
| 1286 | */ |
| 1287 | static PyObject * |
| 1288 | append_keyword_fold(PyObject *repr, int fold) |
| 1289 | { |
| 1290 | PyObject *temp; |
| 1291 | |
| 1292 | assert(PyUnicode_Check(repr)); |
| 1293 | if (fold == 0) |
| 1294 | return repr; |
| 1295 | /* Get rid of the trailing ')'. */ |
| 1296 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1297 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
| 1298 | Py_DECREF(repr); |
| 1299 | if (temp == NULL) |
| 1300 | return NULL; |
| 1301 | repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); |
| 1302 | Py_DECREF(temp); |
| 1303 | return repr; |
| 1304 | } |
| 1305 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1306 | static inline PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 1307 | tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds) |
| 1308 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1309 | PyObject *tzinfo; |
| 1310 | if (rv == 1) { |
| 1311 | // Create a timezone from offset in seconds (0 returns UTC) |
| 1312 | if (tzoffset == 0) { |
| 1313 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1314 | return PyDateTime_TimeZone_UTC; |
| 1315 | } |
| 1316 | |
| 1317 | PyObject *delta = new_delta(0, tzoffset, tz_useconds, 1); |
Alexey Izbyshev | 4988453 | 2018-08-24 18:53:16 +0300 | [diff] [blame] | 1318 | if (delta == NULL) { |
| 1319 | return NULL; |
| 1320 | } |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1321 | tzinfo = new_timezone(delta, NULL); |
Alexey Izbyshev | 4988453 | 2018-08-24 18:53:16 +0300 | [diff] [blame] | 1322 | Py_DECREF(delta); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 1323 | } |
| 1324 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1325 | tzinfo = Py_None; |
| 1326 | Py_INCREF(Py_None); |
| 1327 | } |
| 1328 | |
| 1329 | return tzinfo; |
| 1330 | } |
| 1331 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1332 | /* --------------------------------------------------------------------------- |
| 1333 | * String format helpers. |
| 1334 | */ |
| 1335 | |
| 1336 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1337 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1338 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1339 | static const char * const DayNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1341 | }; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1342 | static const char * const MonthNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1344 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1345 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1350 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1351 | GET_DAY(date), hours, minutes, seconds, |
| 1352 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1355 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1356 | |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1357 | /* Add formatted UTC offset string to buf. buf has no more than |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1358 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1359 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1360 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1361 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1362 | * string of the form |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1363 | * sign HH sep MM [sep SS [. UUUUUU]] |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1364 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1365 | * bogus, an appropriate exception is set and -1 is returned. |
| 1366 | */ |
| 1367 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1368 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1370 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1371 | PyObject *offset; |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1372 | int hours, minutes, seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1376 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1377 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1378 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1380 | if (offset == Py_None) { |
| 1381 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | *buf = '\0'; |
| 1383 | return 0; |
| 1384 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1385 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1386 | if (GET_TD_DAYS(offset) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | sign = '-'; |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 1388 | Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1389 | if (offset == NULL) |
| 1390 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1392 | else { |
| 1393 | sign = '+'; |
| 1394 | } |
| 1395 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1396 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1397 | seconds = GET_TD_SECONDS(offset); |
| 1398 | Py_DECREF(offset); |
| 1399 | minutes = divmod(seconds, 60, &seconds); |
| 1400 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1401 | if (microseconds) { |
| 1402 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d.%06d", sign, |
| 1403 | hours, sep, minutes, sep, seconds, microseconds); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | if (seconds) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1407 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, |
| 1408 | sep, minutes, sep, seconds); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1409 | return 0; |
| 1410 | } |
| 1411 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1415 | static PyObject * |
| 1416 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | PyObject *temp; |
| 1419 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1420 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1421 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | if (Zreplacement == NULL) |
| 1424 | return NULL; |
| 1425 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1426 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | assert(tzinfoarg != NULL); |
| 1429 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1430 | if (temp == NULL) |
| 1431 | goto Error; |
| 1432 | if (temp == Py_None) { |
| 1433 | Py_DECREF(temp); |
| 1434 | return Zreplacement; |
| 1435 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | assert(PyUnicode_Check(temp)); |
| 1438 | /* Since the tzname is getting stuffed into the |
| 1439 | * format, we have to double any % signs so that |
| 1440 | * strftime doesn't treat them as format codes. |
| 1441 | */ |
| 1442 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1443 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | Py_DECREF(temp); |
| 1445 | if (Zreplacement == NULL) |
| 1446 | return NULL; |
| 1447 | if (!PyUnicode_Check(Zreplacement)) { |
| 1448 | PyErr_SetString(PyExc_TypeError, |
| 1449 | "tzname.replace() did not return a string"); |
| 1450 | goto Error; |
| 1451 | } |
| 1452 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1453 | |
| 1454 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | Py_DECREF(Zreplacement); |
| 1456 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1459 | static PyObject * |
| 1460 | make_freplacement(PyObject *object) |
| 1461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | char freplacement[64]; |
| 1463 | if (PyTime_Check(object)) |
| 1464 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1465 | else if (PyDateTime_Check(object)) |
| 1466 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1467 | else |
| 1468 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1473 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1474 | * 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] | 1475 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1476 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1477 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1478 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1479 | */ |
| 1480 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1481 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1487 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1488 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
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 | const char *pin; /* pointer to next char in input format */ |
| 1491 | Py_ssize_t flen; /* length of input format */ |
| 1492 | char ch; /* next char in input format */ |
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 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1495 | char *pnew; /* pointer to available byte in output format */ |
| 1496 | size_t totalnew; /* number bytes total in output format buffer, |
| 1497 | exclusive of trailing \0 */ |
| 1498 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1501 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | assert(object && format && timetuple); |
| 1504 | assert(PyUnicode_Check(format)); |
| 1505 | /* Convert the input format to a C string and size */ |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1506 | pin = PyUnicode_AsUTF8AndSize(format, &flen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | if (!pin) |
| 1508 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1511 | * a new format. Since computing the replacements for those codes |
| 1512 | * is expensive, don't unless they're actually used. |
| 1513 | */ |
| 1514 | if (flen > INT_MAX - 1) { |
| 1515 | PyErr_NoMemory(); |
| 1516 | goto Done; |
| 1517 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1520 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1521 | if (newfmt == NULL) goto Done; |
| 1522 | pnew = PyBytes_AsString(newfmt); |
| 1523 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | while ((ch = *pin++) != '\0') { |
| 1526 | if (ch != '%') { |
| 1527 | ptoappend = pin - 1; |
| 1528 | ntoappend = 1; |
| 1529 | } |
| 1530 | else if ((ch = *pin++) == '\0') { |
MichaelSaah | 454b3d4 | 2019-01-14 05:23:39 -0500 | [diff] [blame] | 1531 | /* Null byte follows %, copy only '%'. |
| 1532 | * |
| 1533 | * Back the pin up one char so that we catch the null check |
| 1534 | * the next time through the loop.*/ |
| 1535 | pin--; |
| 1536 | ptoappend = pin - 1; |
| 1537 | ntoappend = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | } |
| 1539 | /* A % has been seen and ch is the character after it. */ |
| 1540 | else if (ch == 'z') { |
| 1541 | if (zreplacement == NULL) { |
| 1542 | /* format utcoffset */ |
| 1543 | char buf[100]; |
| 1544 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1545 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1546 | if (zreplacement == NULL) goto Done; |
| 1547 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1548 | assert(tzinfoarg != NULL); |
| 1549 | if (format_utcoffset(buf, |
| 1550 | sizeof(buf), |
| 1551 | "", |
| 1552 | tzinfo, |
| 1553 | tzinfoarg) < 0) |
| 1554 | goto Done; |
| 1555 | Py_DECREF(zreplacement); |
| 1556 | zreplacement = |
| 1557 | PyBytes_FromStringAndSize(buf, |
| 1558 | strlen(buf)); |
| 1559 | if (zreplacement == NULL) |
| 1560 | goto Done; |
| 1561 | } |
| 1562 | } |
| 1563 | assert(zreplacement != NULL); |
| 1564 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1565 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1566 | } |
| 1567 | else if (ch == 'Z') { |
| 1568 | /* format tzname */ |
| 1569 | if (Zreplacement == NULL) { |
| 1570 | Zreplacement = make_Zreplacement(object, |
| 1571 | tzinfoarg); |
| 1572 | if (Zreplacement == NULL) |
| 1573 | goto Done; |
| 1574 | } |
| 1575 | assert(Zreplacement != NULL); |
| 1576 | assert(PyUnicode_Check(Zreplacement)); |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1577 | ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1579 | if (ptoappend == NULL) |
| 1580 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | } |
| 1582 | else if (ch == 'f') { |
| 1583 | /* format microseconds */ |
| 1584 | if (freplacement == NULL) { |
| 1585 | freplacement = make_freplacement(object); |
| 1586 | if (freplacement == NULL) |
| 1587 | goto Done; |
| 1588 | } |
| 1589 | assert(freplacement != NULL); |
| 1590 | assert(PyBytes_Check(freplacement)); |
| 1591 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1592 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1593 | } |
| 1594 | else { |
| 1595 | /* percent followed by neither z nor Z */ |
| 1596 | ptoappend = pin - 2; |
| 1597 | ntoappend = 2; |
| 1598 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | /* Append the ntoappend chars starting at ptoappend to |
| 1601 | * the new format. |
| 1602 | */ |
| 1603 | if (ntoappend == 0) |
| 1604 | continue; |
| 1605 | assert(ptoappend != NULL); |
| 1606 | assert(ntoappend > 0); |
| 1607 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1608 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | PyErr_NoMemory(); |
| 1610 | goto Done; |
| 1611 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1612 | totalnew <<= 1; |
| 1613 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1615 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1616 | } |
| 1617 | memcpy(pnew, ptoappend, ntoappend); |
| 1618 | pnew += ntoappend; |
| 1619 | usednew += ntoappend; |
| 1620 | assert(usednew <= totalnew); |
| 1621 | } /* end while() */ |
MichaelSaah | 454b3d4 | 2019-01-14 05:23:39 -0500 | [diff] [blame] | 1622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1624 | goto Done; |
| 1625 | { |
| 1626 | PyObject *format; |
| 1627 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | if (time == NULL) |
| 1630 | goto Done; |
| 1631 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1632 | if (format != NULL) { |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1633 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime, |
| 1634 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | Py_DECREF(format); |
| 1636 | } |
| 1637 | Py_DECREF(time); |
| 1638 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1639 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | Py_XDECREF(freplacement); |
| 1641 | Py_XDECREF(zreplacement); |
| 1642 | Py_XDECREF(Zreplacement); |
| 1643 | Py_XDECREF(newfmt); |
| 1644 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1647 | /* --------------------------------------------------------------------------- |
| 1648 | * Wrap functions from the time module. These aren't directly available |
| 1649 | * from C. Perhaps they should be. |
| 1650 | */ |
| 1651 | |
| 1652 | /* Call time.time() and return its result (a Python float). */ |
| 1653 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1654 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | PyObject *result = NULL; |
| 1657 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1660 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1661 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 1662 | result = _PyObject_CallMethodId(time, &PyId_time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | Py_DECREF(time); |
| 1664 | } |
| 1665 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1669 | * computed from the y,m,d args. |
| 1670 | */ |
| 1671 | static PyObject * |
| 1672 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | PyObject *time; |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1675 | PyObject *result; |
| 1676 | _Py_IDENTIFIER(struct_time); |
| 1677 | PyObject *args; |
| 1678 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1680 | time = PyImport_ImportModuleNoBlock("time"); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1681 | if (time == NULL) { |
| 1682 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | } |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1684 | |
| 1685 | args = Py_BuildValue("iiiiiiiii", |
| 1686 | y, m, d, |
| 1687 | hh, mm, ss, |
| 1688 | weekday(y, m, d), |
| 1689 | days_before_month(y, m) + d, |
| 1690 | dstflag); |
| 1691 | if (args == NULL) { |
| 1692 | Py_DECREF(time); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | |
| 1696 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_struct_time, |
| 1697 | args, NULL); |
| 1698 | Py_DECREF(time); |
Victor Stinner | ddc120f | 2016-12-09 15:35:40 +0100 | [diff] [blame] | 1699 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /* --------------------------------------------------------------------------- |
| 1704 | * Miscellaneous helpers. |
| 1705 | */ |
| 1706 | |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1707 | /* The comparisons here all most naturally compute a cmp()-like result. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1708 | * This little helper turns that into a bool result for rich comparisons. |
| 1709 | */ |
| 1710 | static PyObject * |
| 1711 | diff_to_bool(int diff, int op) |
| 1712 | { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1713 | Py_RETURN_RICHCOMPARE(diff, 0, op); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1716 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1717 | static PyObject * |
| 1718 | cmperror(PyObject *a, PyObject *b) |
| 1719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | PyErr_Format(PyExc_TypeError, |
| 1721 | "can't compare %s to %s", |
| 1722 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1723 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1726 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1727 | * Cached Python objects; these are set by the module init function. |
| 1728 | */ |
| 1729 | |
| 1730 | /* Conversion factors. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1732 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1733 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1734 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1735 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1736 | 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] | 1737 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1738 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1739 | /* --------------------------------------------------------------------------- |
| 1740 | * Class implementations. |
| 1741 | */ |
| 1742 | |
| 1743 | /* |
| 1744 | * PyDateTime_Delta implementation. |
| 1745 | */ |
| 1746 | |
| 1747 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1749 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1750 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1751 | * due to ubiquitous overflow possibilities. |
| 1752 | */ |
| 1753 | static PyObject * |
| 1754 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1756 | PyObject *x1 = NULL; |
| 1757 | PyObject *x2 = NULL; |
| 1758 | PyObject *x3 = NULL; |
| 1759 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1762 | if (x1 == NULL) |
| 1763 | goto Done; |
| 1764 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1765 | if (x2 == NULL) |
| 1766 | goto Done; |
| 1767 | Py_DECREF(x1); |
| 1768 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | /* x2 has days in seconds */ |
| 1771 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1772 | if (x1 == NULL) |
| 1773 | goto Done; |
| 1774 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1775 | if (x3 == NULL) |
| 1776 | goto Done; |
| 1777 | Py_DECREF(x1); |
| 1778 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1779 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | /* x3 has days+seconds in seconds */ |
| 1782 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1783 | if (x1 == NULL) |
| 1784 | goto Done; |
| 1785 | Py_DECREF(x3); |
| 1786 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | /* x1 has days+seconds in us */ |
| 1789 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1790 | if (x2 == NULL) |
| 1791 | goto Done; |
| 1792 | result = PyNumber_Add(x1, x2); |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 1793 | assert(result == NULL || PyLong_CheckExact(result)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1794 | |
| 1795 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | Py_XDECREF(x1); |
| 1797 | Py_XDECREF(x2); |
| 1798 | Py_XDECREF(x3); |
| 1799 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1802 | static PyObject * |
| 1803 | checked_divmod(PyObject *a, PyObject *b) |
| 1804 | { |
| 1805 | PyObject *result = PyNumber_Divmod(a, b); |
| 1806 | if (result != NULL) { |
| 1807 | if (!PyTuple_Check(result)) { |
| 1808 | PyErr_Format(PyExc_TypeError, |
| 1809 | "divmod() returned non-tuple (type %.200s)", |
| 1810 | result->ob_type->tp_name); |
| 1811 | Py_DECREF(result); |
| 1812 | return NULL; |
| 1813 | } |
| 1814 | if (PyTuple_GET_SIZE(result) != 2) { |
| 1815 | PyErr_Format(PyExc_TypeError, |
| 1816 | "divmod() returned a tuple of size %zd", |
| 1817 | PyTuple_GET_SIZE(result)); |
| 1818 | Py_DECREF(result); |
| 1819 | return NULL; |
| 1820 | } |
| 1821 | } |
| 1822 | return result; |
| 1823 | } |
| 1824 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1825 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1826 | */ |
| 1827 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1828 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | int us; |
| 1831 | int s; |
| 1832 | int d; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | PyObject *tuple = NULL; |
| 1835 | PyObject *num = NULL; |
| 1836 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1837 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1838 | tuple = checked_divmod(pyus, us_per_second); |
| 1839 | if (tuple == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | goto Done; |
| 1841 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1842 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1843 | num = PyTuple_GET_ITEM(tuple, 1); /* us */ |
| 1844 | us = _PyLong_AsInt(num); |
| 1845 | num = NULL; |
| 1846 | if (us == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | goto Done; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1848 | } |
| 1849 | if (!(0 <= us && us < 1000000)) { |
| 1850 | goto BadDivmod; |
| 1851 | } |
| 1852 | |
| 1853 | num = PyTuple_GET_ITEM(tuple, 0); /* leftover seconds */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | Py_INCREF(num); |
| 1855 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1856 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1857 | tuple = checked_divmod(num, seconds_per_day); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | if (tuple == NULL) |
| 1859 | goto Done; |
| 1860 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1861 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1862 | num = PyTuple_GET_ITEM(tuple, 1); /* seconds */ |
| 1863 | s = _PyLong_AsInt(num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | num = NULL; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1865 | if (s == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1866 | goto Done; |
| 1867 | } |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1868 | if (!(0 <= s && s < 24*3600)) { |
| 1869 | goto BadDivmod; |
| 1870 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1871 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1872 | num = PyTuple_GET_ITEM(tuple, 0); /* leftover days */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | Py_INCREF(num); |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1874 | d = _PyLong_AsInt(num); |
| 1875 | if (d == -1 && PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | goto Done; |
| 1877 | } |
| 1878 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1879 | |
| 1880 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1881 | Py_XDECREF(tuple); |
| 1882 | Py_XDECREF(num); |
| 1883 | return result; |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1884 | |
| 1885 | BadDivmod: |
| 1886 | PyErr_SetString(PyExc_TypeError, |
| 1887 | "divmod() returned a value out of range"); |
| 1888 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | #define microseconds_to_delta(pymicros) \ |
| 1892 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1893 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1894 | static PyObject * |
| 1895 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1896 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | PyObject *pyus_in; |
| 1898 | PyObject *pyus_out; |
| 1899 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | pyus_in = delta_to_microseconds(delta); |
| 1902 | if (pyus_in == NULL) |
| 1903 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1904 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 1905 | pyus_out = PyNumber_Multiply(intobj, pyus_in); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | Py_DECREF(pyus_in); |
| 1907 | if (pyus_out == NULL) |
| 1908 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | result = microseconds_to_delta(pyus_out); |
| 1911 | Py_DECREF(pyus_out); |
| 1912 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | static PyObject * |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1916 | get_float_as_integer_ratio(PyObject *floatobj) |
| 1917 | { |
| 1918 | PyObject *ratio; |
| 1919 | |
| 1920 | assert(floatobj && PyFloat_Check(floatobj)); |
| 1921 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
| 1922 | if (ratio == NULL) { |
| 1923 | return NULL; |
| 1924 | } |
| 1925 | if (!PyTuple_Check(ratio)) { |
| 1926 | PyErr_Format(PyExc_TypeError, |
| 1927 | "unexpected return type from as_integer_ratio(): " |
| 1928 | "expected tuple, got '%.200s'", |
| 1929 | Py_TYPE(ratio)->tp_name); |
| 1930 | Py_DECREF(ratio); |
| 1931 | return NULL; |
| 1932 | } |
| 1933 | if (PyTuple_Size(ratio) != 2) { |
| 1934 | PyErr_SetString(PyExc_ValueError, |
| 1935 | "as_integer_ratio() must return a 2-tuple"); |
| 1936 | Py_DECREF(ratio); |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | return ratio; |
| 1940 | } |
| 1941 | |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1942 | /* op is 0 for multiplication, 1 for division */ |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1943 | static PyObject * |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1944 | multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj, int op) |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1945 | { |
| 1946 | PyObject *result = NULL; |
| 1947 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1948 | PyObject *ratio = NULL; |
| 1949 | |
| 1950 | pyus_in = delta_to_microseconds(delta); |
| 1951 | if (pyus_in == NULL) |
| 1952 | return NULL; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1953 | ratio = get_float_as_integer_ratio(floatobj); |
| 1954 | if (ratio == NULL) { |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1955 | goto error; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1956 | } |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1957 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1958 | Py_DECREF(pyus_in); |
| 1959 | pyus_in = NULL; |
| 1960 | if (temp == NULL) |
| 1961 | goto error; |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1962 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1963 | Py_DECREF(temp); |
| 1964 | if (pyus_out == NULL) |
| 1965 | goto error; |
| 1966 | result = microseconds_to_delta(pyus_out); |
| 1967 | Py_DECREF(pyus_out); |
| 1968 | error: |
| 1969 | Py_XDECREF(pyus_in); |
| 1970 | Py_XDECREF(ratio); |
| 1971 | |
| 1972 | return result; |
| 1973 | } |
| 1974 | |
| 1975 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1976 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1977 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | PyObject *pyus_in; |
| 1979 | PyObject *pyus_out; |
| 1980 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | pyus_in = delta_to_microseconds(delta); |
| 1983 | if (pyus_in == NULL) |
| 1984 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1987 | Py_DECREF(pyus_in); |
| 1988 | if (pyus_out == NULL) |
| 1989 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | result = microseconds_to_delta(pyus_out); |
| 1992 | Py_DECREF(pyus_out); |
| 1993 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1997 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1998 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | PyObject *pyus_left; |
| 2000 | PyObject *pyus_right; |
| 2001 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | pyus_left = delta_to_microseconds(left); |
| 2004 | if (pyus_left == NULL) |
| 2005 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | pyus_right = delta_to_microseconds(right); |
| 2008 | if (pyus_right == NULL) { |
| 2009 | Py_DECREF(pyus_left); |
| 2010 | return NULL; |
| 2011 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 2014 | Py_DECREF(pyus_left); |
| 2015 | Py_DECREF(pyus_right); |
| 2016 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | static PyObject * |
| 2020 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 2021 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | PyObject *pyus_left; |
| 2023 | PyObject *pyus_right; |
| 2024 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | pyus_left = delta_to_microseconds(left); |
| 2027 | if (pyus_left == NULL) |
| 2028 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2030 | pyus_right = delta_to_microseconds(right); |
| 2031 | if (pyus_right == NULL) { |
| 2032 | Py_DECREF(pyus_left); |
| 2033 | return NULL; |
| 2034 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 2037 | Py_DECREF(pyus_left); |
| 2038 | Py_DECREF(pyus_right); |
| 2039 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2043 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 2044 | { |
| 2045 | PyObject *result; |
| 2046 | PyObject *pyus_in, *pyus_out; |
| 2047 | pyus_in = delta_to_microseconds(delta); |
| 2048 | if (pyus_in == NULL) |
| 2049 | return NULL; |
| 2050 | pyus_out = divide_nearest(pyus_in, i); |
| 2051 | Py_DECREF(pyus_in); |
| 2052 | if (pyus_out == NULL) |
| 2053 | return NULL; |
| 2054 | result = microseconds_to_delta(pyus_out); |
| 2055 | Py_DECREF(pyus_out); |
| 2056 | |
| 2057 | return result; |
| 2058 | } |
| 2059 | |
| 2060 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2061 | delta_add(PyObject *left, PyObject *right) |
| 2062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2066 | /* delta + delta */ |
| 2067 | /* The C-level additions can't overflow because of the |
| 2068 | * invariant bounds. |
| 2069 | */ |
| 2070 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 2071 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 2072 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 2073 | GET_TD_MICROSECONDS(right); |
| 2074 | result = new_delta(days, seconds, microseconds, 1); |
| 2075 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2077 | if (result == Py_NotImplemented) |
| 2078 | Py_INCREF(result); |
| 2079 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | static PyObject * |
| 2083 | delta_negative(PyDateTime_Delta *self) |
| 2084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | return new_delta(-GET_TD_DAYS(self), |
| 2086 | -GET_TD_SECONDS(self), |
| 2087 | -GET_TD_MICROSECONDS(self), |
| 2088 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | static PyObject * |
| 2092 | delta_positive(PyDateTime_Delta *self) |
| 2093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2094 | /* Could optimize this (by returning self) if this isn't a |
| 2095 | * subclass -- but who uses unary + ? Approximately nobody. |
| 2096 | */ |
| 2097 | return new_delta(GET_TD_DAYS(self), |
| 2098 | GET_TD_SECONDS(self), |
| 2099 | GET_TD_MICROSECONDS(self), |
| 2100 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | static PyObject * |
| 2104 | delta_abs(PyDateTime_Delta *self) |
| 2105 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 2109 | assert(GET_TD_SECONDS(self) >= 0); |
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 | if (GET_TD_DAYS(self) < 0) |
| 2112 | result = delta_negative(self); |
| 2113 | else |
| 2114 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | static PyObject * |
| 2120 | delta_subtract(PyObject *left, PyObject *right) |
| 2121 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2125 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 2126 | /* The C-level additions can't overflow because of the |
| 2127 | * invariant bounds. |
| 2128 | */ |
| 2129 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 2130 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 2131 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 2132 | GET_TD_MICROSECONDS(right); |
| 2133 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | if (result == Py_NotImplemented) |
| 2137 | Py_INCREF(result); |
| 2138 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2141 | static int |
| 2142 | delta_cmp(PyObject *self, PyObject *other) |
| 2143 | { |
| 2144 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 2145 | if (diff == 0) { |
| 2146 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 2147 | if (diff == 0) |
| 2148 | diff = GET_TD_MICROSECONDS(self) - |
| 2149 | GET_TD_MICROSECONDS(other); |
| 2150 | } |
| 2151 | return diff; |
| 2152 | } |
| 2153 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2154 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2155 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2157 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2158 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | return diff_to_bool(diff, op); |
| 2160 | } |
| 2161 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2162 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2163 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 2167 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2168 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2169 | delta_hash(PyDateTime_Delta *self) |
| 2170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 | if (self->hashcode == -1) { |
| 2172 | PyObject *temp = delta_getstate(self); |
| 2173 | if (temp != NULL) { |
| 2174 | self->hashcode = PyObject_Hash(temp); |
| 2175 | Py_DECREF(temp); |
| 2176 | } |
| 2177 | } |
| 2178 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | static PyObject * |
| 2182 | delta_multiply(PyObject *left, PyObject *right) |
| 2183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2186 | if (PyDelta_Check(left)) { |
| 2187 | /* delta * ??? */ |
| 2188 | if (PyLong_Check(right)) |
| 2189 | result = multiply_int_timedelta(right, |
| 2190 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2191 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2192 | result = multiply_truedivide_timedelta_float( |
| 2193 | (PyDateTime_Delta *) left, right, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | } |
| 2195 | else if (PyLong_Check(left)) |
| 2196 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2197 | (PyDateTime_Delta *) right); |
| 2198 | else if (PyFloat_Check(left)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2199 | result = multiply_truedivide_timedelta_float( |
| 2200 | (PyDateTime_Delta *) right, left, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | if (result == Py_NotImplemented) |
| 2203 | Py_INCREF(result); |
| 2204 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | static PyObject * |
| 2208 | delta_divide(PyObject *left, PyObject *right) |
| 2209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | if (PyDelta_Check(left)) { |
| 2213 | /* delta * ??? */ |
| 2214 | if (PyLong_Check(right)) |
| 2215 | result = divide_timedelta_int( |
| 2216 | (PyDateTime_Delta *)left, |
| 2217 | right); |
| 2218 | else if (PyDelta_Check(right)) |
| 2219 | result = divide_timedelta_timedelta( |
| 2220 | (PyDateTime_Delta *)left, |
| 2221 | (PyDateTime_Delta *)right); |
| 2222 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | if (result == Py_NotImplemented) |
| 2225 | Py_INCREF(result); |
| 2226 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2229 | static PyObject * |
| 2230 | delta_truedivide(PyObject *left, PyObject *right) |
| 2231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | if (PyDelta_Check(left)) { |
| 2235 | if (PyDelta_Check(right)) |
| 2236 | result = truedivide_timedelta_timedelta( |
| 2237 | (PyDateTime_Delta *)left, |
| 2238 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2239 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2240 | result = multiply_truedivide_timedelta_float( |
| 2241 | (PyDateTime_Delta *)left, right, 1); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2242 | else if (PyLong_Check(right)) |
| 2243 | result = truedivide_timedelta_int( |
| 2244 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | if (result == Py_NotImplemented) |
| 2248 | Py_INCREF(result); |
| 2249 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | static PyObject * |
| 2253 | delta_remainder(PyObject *left, PyObject *right) |
| 2254 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | PyObject *pyus_left; |
| 2256 | PyObject *pyus_right; |
| 2257 | PyObject *pyus_remainder; |
| 2258 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2259 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2260 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2261 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2264 | if (pyus_left == NULL) |
| 2265 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2268 | if (pyus_right == NULL) { |
| 2269 | Py_DECREF(pyus_left); |
| 2270 | return NULL; |
| 2271 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2273 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 2274 | Py_DECREF(pyus_left); |
| 2275 | Py_DECREF(pyus_right); |
| 2276 | if (pyus_remainder == NULL) |
| 2277 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2279 | remainder = microseconds_to_delta(pyus_remainder); |
| 2280 | Py_DECREF(pyus_remainder); |
| 2281 | if (remainder == NULL) |
| 2282 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | static PyObject * |
| 2288 | delta_divmod(PyObject *left, PyObject *right) |
| 2289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | PyObject *pyus_left; |
| 2291 | PyObject *pyus_right; |
| 2292 | PyObject *divmod; |
| 2293 | PyObject *delta; |
| 2294 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2295 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2296 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2297 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2300 | if (pyus_left == NULL) |
| 2301 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2303 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2304 | if (pyus_right == NULL) { |
| 2305 | Py_DECREF(pyus_left); |
| 2306 | return NULL; |
| 2307 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2308 | |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 2309 | divmod = checked_divmod(pyus_left, pyus_right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | Py_DECREF(pyus_left); |
| 2311 | Py_DECREF(pyus_right); |
| 2312 | if (divmod == NULL) |
| 2313 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 2316 | if (delta == NULL) { |
| 2317 | Py_DECREF(divmod); |
| 2318 | return NULL; |
| 2319 | } |
| 2320 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 2321 | Py_DECREF(delta); |
| 2322 | Py_DECREF(divmod); |
| 2323 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2326 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 2327 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 2328 | * so far, and there are factor microseconds per current unit, the number |
| 2329 | * of which is given by num. num * factor is added to sofar in a |
| 2330 | * numerically careful way, and that's the result. Any fractional |
| 2331 | * microseconds left over (this can happen if num is a float type) are |
| 2332 | * added into *leftover. |
| 2333 | * Note that there are many ways this can give an error (NULL) return. |
| 2334 | */ |
| 2335 | static PyObject * |
| 2336 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2337 | double *leftover) |
| 2338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | PyObject *prod; |
| 2340 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | if (PyLong_Check(num)) { |
Serhiy Storchaka | 3ec0f49 | 2018-11-20 20:41:09 +0200 | [diff] [blame] | 2345 | prod = PyNumber_Multiply(num, factor); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | if (prod == NULL) |
| 2347 | return NULL; |
| 2348 | sum = PyNumber_Add(sofar, prod); |
| 2349 | Py_DECREF(prod); |
| 2350 | return sum; |
| 2351 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2353 | if (PyFloat_Check(num)) { |
| 2354 | double dnum; |
| 2355 | double fracpart; |
| 2356 | double intpart; |
| 2357 | PyObject *x; |
| 2358 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | /* The Plan: decompose num into an integer part and a |
| 2361 | * fractional part, num = intpart + fracpart. |
| 2362 | * Then num * factor == |
| 2363 | * intpart * factor + fracpart * factor |
| 2364 | * and the LHS can be computed exactly in long arithmetic. |
| 2365 | * The RHS is again broken into an int part and frac part. |
| 2366 | * and the frac part is added into *leftover. |
| 2367 | */ |
| 2368 | dnum = PyFloat_AsDouble(num); |
| 2369 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2370 | return NULL; |
| 2371 | fracpart = modf(dnum, &intpart); |
| 2372 | x = PyLong_FromDouble(intpart); |
| 2373 | if (x == NULL) |
| 2374 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2376 | prod = PyNumber_Multiply(x, factor); |
| 2377 | Py_DECREF(x); |
| 2378 | if (prod == NULL) |
| 2379 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | sum = PyNumber_Add(sofar, prod); |
| 2382 | Py_DECREF(prod); |
| 2383 | if (sum == NULL) |
| 2384 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | if (fracpart == 0.0) |
| 2387 | return sum; |
| 2388 | /* So far we've lost no information. Dealing with the |
| 2389 | * fractional part requires float arithmetic, and may |
| 2390 | * lose a little info. |
| 2391 | */ |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2392 | assert(PyLong_CheckExact(factor)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | dnum *= fracpart; |
| 2396 | fracpart = modf(dnum, &intpart); |
| 2397 | x = PyLong_FromDouble(intpart); |
| 2398 | if (x == NULL) { |
| 2399 | Py_DECREF(sum); |
| 2400 | return NULL; |
| 2401 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | y = PyNumber_Add(sum, x); |
| 2404 | Py_DECREF(sum); |
| 2405 | Py_DECREF(x); |
| 2406 | *leftover += fracpart; |
| 2407 | return y; |
| 2408 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | PyErr_Format(PyExc_TypeError, |
| 2411 | "unsupported type for timedelta %s component: %s", |
| 2412 | tag, Py_TYPE(num)->tp_name); |
| 2413 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | static PyObject * |
| 2417 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | /* Argument objects. */ |
| 2422 | PyObject *day = NULL; |
| 2423 | PyObject *second = NULL; |
| 2424 | PyObject *us = NULL; |
| 2425 | PyObject *ms = NULL; |
| 2426 | PyObject *minute = NULL; |
| 2427 | PyObject *hour = NULL; |
| 2428 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2430 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2431 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2432 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2434 | static char *keywords[] = { |
| 2435 | "days", "seconds", "microseconds", "milliseconds", |
| 2436 | "minutes", "hours", "weeks", NULL |
| 2437 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2440 | keywords, |
| 2441 | &day, &second, &us, |
| 2442 | &ms, &minute, &hour, &week) == 0) |
| 2443 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | x = PyLong_FromLong(0); |
| 2446 | if (x == NULL) |
| 2447 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | #define CLEANUP \ |
| 2450 | Py_DECREF(x); \ |
| 2451 | x = y; \ |
| 2452 | if (x == NULL) \ |
| 2453 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 | if (us) { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2456 | y = accum("microseconds", x, us, _PyLong_One, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | CLEANUP; |
| 2458 | } |
| 2459 | if (ms) { |
| 2460 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2461 | CLEANUP; |
| 2462 | } |
| 2463 | if (second) { |
| 2464 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2465 | CLEANUP; |
| 2466 | } |
| 2467 | if (minute) { |
| 2468 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2469 | CLEANUP; |
| 2470 | } |
| 2471 | if (hour) { |
| 2472 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2473 | CLEANUP; |
| 2474 | } |
| 2475 | if (day) { |
| 2476 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2477 | CLEANUP; |
| 2478 | } |
| 2479 | if (week) { |
| 2480 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2481 | CLEANUP; |
| 2482 | } |
| 2483 | if (leftover_us) { |
| 2484 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2485 | double whole_us = round(leftover_us); |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2486 | int x_is_odd; |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2487 | PyObject *temp; |
| 2488 | |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2489 | whole_us = round(leftover_us); |
| 2490 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2491 | /* We're exactly halfway between two integers. In order |
| 2492 | * to do round-half-to-even, we must determine whether x |
| 2493 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2494 | * code below uses bitwise and operation to check the last |
| 2495 | * bit. */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2496 | temp = PyNumber_And(x, _PyLong_One); /* temp <- x & 1 */ |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2497 | if (temp == NULL) { |
| 2498 | Py_DECREF(x); |
| 2499 | goto Done; |
| 2500 | } |
| 2501 | x_is_odd = PyObject_IsTrue(temp); |
| 2502 | Py_DECREF(temp); |
| 2503 | if (x_is_odd == -1) { |
| 2504 | Py_DECREF(x); |
| 2505 | goto Done; |
| 2506 | } |
| 2507 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2508 | } |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2509 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2510 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | if (temp == NULL) { |
| 2513 | Py_DECREF(x); |
| 2514 | goto Done; |
| 2515 | } |
| 2516 | y = PyNumber_Add(x, temp); |
| 2517 | Py_DECREF(temp); |
| 2518 | CLEANUP; |
| 2519 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | self = microseconds_to_delta_ex(x, type); |
| 2522 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2523 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2525 | |
| 2526 | #undef CLEANUP |
| 2527 | } |
| 2528 | |
| 2529 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2530 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2532 | return (GET_TD_DAYS(self) != 0 |
| 2533 | || GET_TD_SECONDS(self) != 0 |
| 2534 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | static PyObject * |
| 2538 | delta_repr(PyDateTime_Delta *self) |
| 2539 | { |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2540 | PyObject *args = PyUnicode_FromString(""); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2541 | |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2542 | if (args == NULL) { |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | |
| 2546 | const char *sep = ""; |
| 2547 | |
| 2548 | if (GET_TD_DAYS(self) != 0) { |
| 2549 | Py_SETREF(args, PyUnicode_FromFormat("days=%d", GET_TD_DAYS(self))); |
| 2550 | if (args == NULL) { |
| 2551 | return NULL; |
| 2552 | } |
| 2553 | sep = ", "; |
| 2554 | } |
| 2555 | |
| 2556 | if (GET_TD_SECONDS(self) != 0) { |
| 2557 | Py_SETREF(args, PyUnicode_FromFormat("%U%sseconds=%d", args, sep, |
| 2558 | GET_TD_SECONDS(self))); |
| 2559 | if (args == NULL) { |
| 2560 | return NULL; |
| 2561 | } |
| 2562 | sep = ", "; |
| 2563 | } |
| 2564 | |
| 2565 | if (GET_TD_MICROSECONDS(self) != 0) { |
| 2566 | Py_SETREF(args, PyUnicode_FromFormat("%U%smicroseconds=%d", args, sep, |
| 2567 | GET_TD_MICROSECONDS(self))); |
| 2568 | if (args == NULL) { |
| 2569 | return NULL; |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | if (PyUnicode_GET_LENGTH(args) == 0) { |
| 2574 | Py_SETREF(args, PyUnicode_FromString("0")); |
| 2575 | if (args == NULL) { |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | PyObject *repr = PyUnicode_FromFormat("%s(%S)", Py_TYPE(self)->tp_name, |
| 2581 | args); |
| 2582 | Py_DECREF(args); |
| 2583 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | static PyObject * |
| 2587 | delta_str(PyDateTime_Delta *self) |
| 2588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2589 | int us = GET_TD_MICROSECONDS(self); |
| 2590 | int seconds = GET_TD_SECONDS(self); |
| 2591 | int minutes = divmod(seconds, 60, &seconds); |
| 2592 | int hours = divmod(minutes, 60, &minutes); |
| 2593 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | if (days) { |
| 2596 | if (us) |
| 2597 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2598 | days, (days == 1 || days == -1) ? "" : "s", |
| 2599 | hours, minutes, seconds, us); |
| 2600 | else |
| 2601 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2602 | days, (days == 1 || days == -1) ? "" : "s", |
| 2603 | hours, minutes, seconds); |
| 2604 | } else { |
| 2605 | if (us) |
| 2606 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2607 | hours, minutes, seconds, us); |
| 2608 | else |
| 2609 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2610 | hours, minutes, seconds); |
| 2611 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2612 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2615 | /* Pickle support, a simple use of __reduce__. */ |
| 2616 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2617 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2618 | static PyObject * |
| 2619 | delta_getstate(PyDateTime_Delta *self) |
| 2620 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2622 | GET_TD_SECONDS(self), |
| 2623 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2626 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2627 | delta_total_seconds(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | PyObject *total_seconds; |
| 2630 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2633 | if (total_microseconds == NULL) |
| 2634 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2635 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2636 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2639 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2643 | delta_reduce(PyDateTime_Delta* self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2649 | |
| 2650 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | {"days", T_INT, OFFSET(days), READONLY, |
| 2653 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2656 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2658 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2659 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2660 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2661 | }; |
| 2662 | |
| 2663 | static PyMethodDef delta_methods[] = { |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2664 | {"total_seconds", delta_total_seconds, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2665 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2668 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2671 | }; |
| 2672 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2673 | static const char delta_doc[] = |
Chris Barker | d6a61f2 | 2018-10-19 15:43:24 -0700 | [diff] [blame] | 2674 | PyDoc_STR("Difference between two datetime values.\n\n" |
| 2675 | "timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, " |
| 2676 | "minutes=0, hours=0, weeks=0)\n\n" |
| 2677 | "All arguments are optional and default to 0.\n" |
| 2678 | "Arguments may be integers or floats, and may be positive or negative."); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2679 | |
| 2680 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | delta_add, /* nb_add */ |
| 2682 | delta_subtract, /* nb_subtract */ |
| 2683 | delta_multiply, /* nb_multiply */ |
| 2684 | delta_remainder, /* nb_remainder */ |
| 2685 | delta_divmod, /* nb_divmod */ |
| 2686 | 0, /* nb_power */ |
| 2687 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2688 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2689 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2690 | (inquiry)delta_bool, /* nb_bool */ |
| 2691 | 0, /*nb_invert*/ |
| 2692 | 0, /*nb_lshift*/ |
| 2693 | 0, /*nb_rshift*/ |
| 2694 | 0, /*nb_and*/ |
| 2695 | 0, /*nb_xor*/ |
| 2696 | 0, /*nb_or*/ |
| 2697 | 0, /*nb_int*/ |
| 2698 | 0, /*nb_reserved*/ |
| 2699 | 0, /*nb_float*/ |
| 2700 | 0, /*nb_inplace_add*/ |
| 2701 | 0, /*nb_inplace_subtract*/ |
| 2702 | 0, /*nb_inplace_multiply*/ |
| 2703 | 0, /*nb_inplace_remainder*/ |
| 2704 | 0, /*nb_inplace_power*/ |
| 2705 | 0, /*nb_inplace_lshift*/ |
| 2706 | 0, /*nb_inplace_rshift*/ |
| 2707 | 0, /*nb_inplace_and*/ |
| 2708 | 0, /*nb_inplace_xor*/ |
| 2709 | 0, /*nb_inplace_or*/ |
| 2710 | delta_divide, /* nb_floor_divide */ |
| 2711 | delta_truedivide, /* nb_true_divide */ |
| 2712 | 0, /* nb_inplace_floor_divide */ |
| 2713 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2714 | }; |
| 2715 | |
| 2716 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2718 | "datetime.timedelta", /* tp_name */ |
| 2719 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2720 | 0, /* tp_itemsize */ |
| 2721 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2722 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | 0, /* tp_getattr */ |
| 2724 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2725 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | (reprfunc)delta_repr, /* tp_repr */ |
| 2727 | &delta_as_number, /* tp_as_number */ |
| 2728 | 0, /* tp_as_sequence */ |
| 2729 | 0, /* tp_as_mapping */ |
| 2730 | (hashfunc)delta_hash, /* tp_hash */ |
| 2731 | 0, /* tp_call */ |
| 2732 | (reprfunc)delta_str, /* tp_str */ |
| 2733 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2734 | 0, /* tp_setattro */ |
| 2735 | 0, /* tp_as_buffer */ |
| 2736 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2737 | delta_doc, /* tp_doc */ |
| 2738 | 0, /* tp_traverse */ |
| 2739 | 0, /* tp_clear */ |
| 2740 | delta_richcompare, /* tp_richcompare */ |
| 2741 | 0, /* tp_weaklistoffset */ |
| 2742 | 0, /* tp_iter */ |
| 2743 | 0, /* tp_iternext */ |
| 2744 | delta_methods, /* tp_methods */ |
| 2745 | delta_members, /* tp_members */ |
| 2746 | 0, /* tp_getset */ |
| 2747 | 0, /* tp_base */ |
| 2748 | 0, /* tp_dict */ |
| 2749 | 0, /* tp_descr_get */ |
| 2750 | 0, /* tp_descr_set */ |
| 2751 | 0, /* tp_dictoffset */ |
| 2752 | 0, /* tp_init */ |
| 2753 | 0, /* tp_alloc */ |
| 2754 | delta_new, /* tp_new */ |
| 2755 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2756 | }; |
| 2757 | |
| 2758 | /* |
| 2759 | * PyDateTime_Date implementation. |
| 2760 | */ |
| 2761 | |
| 2762 | /* Accessor properties. */ |
| 2763 | |
| 2764 | static PyObject * |
| 2765 | date_year(PyDateTime_Date *self, void *unused) |
| 2766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | static PyObject * |
| 2771 | date_month(PyDateTime_Date *self, void *unused) |
| 2772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | static PyObject * |
| 2777 | date_day(PyDateTime_Date *self, void *unused) |
| 2778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | {"year", (getter)date_year}, |
| 2784 | {"month", (getter)date_month}, |
| 2785 | {"day", (getter)date_day}, |
| 2786 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2787 | }; |
| 2788 | |
| 2789 | /* Constructors. */ |
| 2790 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2791 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2792 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2793 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2794 | date_from_pickle(PyTypeObject *type, PyObject *state) |
| 2795 | { |
| 2796 | PyDateTime_Date *me; |
| 2797 | |
| 2798 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2799 | if (me != NULL) { |
| 2800 | const char *pdata = PyBytes_AS_STRING(state); |
| 2801 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2802 | me->hashcode = -1; |
| 2803 | } |
| 2804 | return (PyObject *)me; |
| 2805 | } |
| 2806 | |
| 2807 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2808 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | int year; |
| 2812 | int month; |
| 2813 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 1133a8c | 2018-12-07 16:48:21 +0200 | [diff] [blame] | 2816 | if (PyTuple_GET_SIZE(args) == 1) { |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2817 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 2818 | if (PyBytes_Check(state)) { |
| 2819 | if (PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2820 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2821 | { |
| 2822 | return date_from_pickle(type, state); |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 2823 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 2824 | } |
| 2825 | else if (PyUnicode_Check(state)) { |
| 2826 | if (PyUnicode_READY(state)) { |
| 2827 | return NULL; |
| 2828 | } |
| 2829 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATE_DATASIZE && |
| 2830 | MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2))) |
| 2831 | { |
| 2832 | state = PyUnicode_AsLatin1String(state); |
| 2833 | if (state == NULL) { |
| 2834 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 2835 | /* More informative error message. */ |
| 2836 | PyErr_SetString(PyExc_ValueError, |
| 2837 | "Failed to encode latin1 string when unpickling " |
| 2838 | "a date object. " |
| 2839 | "pickle.load(data, encoding='latin1') is assumed."); |
| 2840 | } |
| 2841 | return NULL; |
| 2842 | } |
| 2843 | self = date_from_pickle(type, state); |
| 2844 | Py_DECREF(state); |
| 2845 | return self; |
| 2846 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2851 | &year, &month, &day)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | self = new_date_ex(year, month, day, type); |
| 2853 | } |
| 2854 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2857 | static PyObject * |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2858 | date_fromtimestamp(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2859 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2860 | struct tm tm; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2862 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2863 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2865 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 2866 | if (_PyTime_localtime(t, &tm) != 0) |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2867 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2868 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2869 | return new_date_subclass_ex(tm.tm_year + 1900, |
| 2870 | tm.tm_mon + 1, |
| 2871 | tm.tm_mday, |
| 2872 | cls); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | /* Return new date from current time. |
| 2876 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2877 | * only way to be sure of that is to *call* time.time(). That's not |
| 2878 | * generally the same as calling C's time. |
| 2879 | */ |
| 2880 | static PyObject * |
| 2881 | date_today(PyObject *cls, PyObject *dummy) |
| 2882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | PyObject *time; |
| 2884 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2885 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | time = time_time(); |
| 2888 | if (time == NULL) |
| 2889 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | /* Note well: today() is a class method, so this may not call |
| 2892 | * date.fromtimestamp. For example, it may call |
| 2893 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2894 | * time.time() delivers; if someone were gonzo about optimization, |
| 2895 | * date.today() could get away with plain C time(). |
| 2896 | */ |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 2897 | result = _PyObject_CallMethodIdObjArgs(cls, &PyId_fromtimestamp, |
| 2898 | time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | Py_DECREF(time); |
| 2900 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2903 | /*[clinic input] |
| 2904 | @classmethod |
| 2905 | datetime.date.fromtimestamp |
| 2906 | |
| 2907 | timestamp: object |
| 2908 | / |
| 2909 | |
| 2910 | Create a date from a POSIX timestamp. |
| 2911 | |
| 2912 | The timestamp is a number, e.g. created via time.time(), that is interpreted |
| 2913 | as local time. |
| 2914 | [clinic start generated code]*/ |
| 2915 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2916 | static PyObject * |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2917 | datetime_date_fromtimestamp(PyTypeObject *type, PyObject *timestamp) |
| 2918 | /*[clinic end generated code: output=fd045fda58168869 input=eabb3fe7f40491fe]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2919 | { |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 2920 | return date_fromtimestamp((PyObject *) type, timestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2923 | /* bpo-36025: This is a wrapper for API compatibility with the public C API, |
| 2924 | * which expects a function that takes an *args tuple, whereas the argument |
| 2925 | * clinic generates code that takes METH_O. |
| 2926 | */ |
| 2927 | static PyObject * |
| 2928 | datetime_date_fromtimestamp_capi(PyObject *cls, PyObject *args) |
| 2929 | { |
| 2930 | PyObject *timestamp; |
| 2931 | PyObject *result = NULL; |
| 2932 | |
| 2933 | if (PyArg_UnpackTuple(args, "fromtimestamp", 1, 1, ×tamp)) { |
| 2934 | result = date_fromtimestamp(cls, timestamp); |
| 2935 | } |
| 2936 | |
| 2937 | return result; |
| 2938 | } |
| 2939 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2940 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2941 | * the ordinal is out of range. |
| 2942 | */ |
| 2943 | static PyObject * |
| 2944 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | PyObject *result = NULL; |
| 2947 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2950 | int year; |
| 2951 | int month; |
| 2952 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | if (ordinal < 1) |
| 2955 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2956 | ">= 1"); |
| 2957 | else { |
| 2958 | ord_to_ymd(ordinal, &year, &month, &day); |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2959 | result = new_date_subclass_ex(year, month, day, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2965 | /* Return the new date from a string as generated by date.isoformat() */ |
| 2966 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2967 | date_fromisoformat(PyObject *cls, PyObject *dtstr) |
| 2968 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2969 | assert(dtstr != NULL); |
| 2970 | |
| 2971 | if (!PyUnicode_Check(dtstr)) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2972 | PyErr_SetString(PyExc_TypeError, |
| 2973 | "fromisoformat: argument must be str"); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2974 | return NULL; |
| 2975 | } |
| 2976 | |
| 2977 | Py_ssize_t len; |
| 2978 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2979 | const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 2980 | if (dt_ptr == NULL) { |
| 2981 | goto invalid_string_error; |
| 2982 | } |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2983 | |
| 2984 | int year = 0, month = 0, day = 0; |
| 2985 | |
| 2986 | int rv; |
| 2987 | if (len == 10) { |
| 2988 | rv = parse_isoformat_date(dt_ptr, &year, &month, &day); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 2989 | } |
| 2990 | else { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2991 | rv = -1; |
| 2992 | } |
| 2993 | |
| 2994 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 2995 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2996 | } |
| 2997 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2998 | return new_date_subclass_ex(year, month, day, cls); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 2999 | |
| 3000 | invalid_string_error: |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 3001 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 3002 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 3003 | } |
| 3004 | |
Paul Ganssle | 88c0937 | 2019-04-29 09:22:03 -0400 | [diff] [blame] | 3005 | |
| 3006 | static PyObject * |
| 3007 | date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw) |
| 3008 | { |
| 3009 | static char *keywords[] = { |
| 3010 | "year", "week", "day", NULL |
| 3011 | }; |
| 3012 | |
| 3013 | int year, week, day; |
| 3014 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii:fromisocalendar", |
| 3015 | keywords, |
| 3016 | &year, &week, &day) == 0) { |
| 3017 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 3018 | PyErr_Format(PyExc_ValueError, |
| 3019 | "ISO calendar component out of range"); |
| 3020 | |
| 3021 | } |
| 3022 | return NULL; |
| 3023 | } |
| 3024 | |
| 3025 | // Year is bounded to 0 < year < 10000 because 9999-12-31 is (9999, 52, 5) |
| 3026 | if (year < MINYEAR || year > MAXYEAR) { |
| 3027 | PyErr_Format(PyExc_ValueError, "Year is out of range: %d", year); |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | |
| 3031 | if (week <= 0 || week >= 53) { |
| 3032 | int out_of_range = 1; |
| 3033 | if (week == 53) { |
| 3034 | // ISO years have 53 weeks in it on years starting with a Thursday |
| 3035 | // and on leap years starting on Wednesday |
| 3036 | int first_weekday = weekday(year, 1, 1); |
| 3037 | if (first_weekday == 3 || (first_weekday == 2 && is_leap(year))) { |
| 3038 | out_of_range = 0; |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | if (out_of_range) { |
| 3043 | PyErr_Format(PyExc_ValueError, "Invalid week: %d", week); |
| 3044 | return NULL; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | if (day <= 0 || day >= 8) { |
| 3049 | PyErr_Format(PyExc_ValueError, "Invalid day: %d (range is [1, 7])", |
| 3050 | day); |
| 3051 | return NULL; |
| 3052 | } |
| 3053 | |
| 3054 | // Convert (Y, W, D) to (Y, M, D) in-place |
| 3055 | int day_1 = iso_week1_monday(year); |
| 3056 | |
| 3057 | int month = week; |
| 3058 | int day_offset = (month - 1)*7 + day - 1; |
| 3059 | |
| 3060 | ord_to_ymd(day_1 + day_offset, &year, &month, &day); |
| 3061 | |
| 3062 | return new_date_subclass_ex(year, month, day, cls); |
| 3063 | } |
| 3064 | |
| 3065 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3066 | /* |
| 3067 | * Date arithmetic. |
| 3068 | */ |
| 3069 | |
| 3070 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 3071 | * instead. |
| 3072 | */ |
| 3073 | static PyObject * |
| 3074 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 3075 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | PyObject *result = NULL; |
| 3077 | int year = GET_YEAR(date); |
| 3078 | int month = GET_MONTH(date); |
| 3079 | int deltadays = GET_TD_DAYS(delta); |
| 3080 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 3081 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | if (normalize_date(&year, &month, &day) >= 0) |
Paul Ganssle | 89427cd | 2019-02-04 14:42:04 -0500 | [diff] [blame] | 3084 | result = new_date_subclass_ex(year, month, day, |
| 3085 | (PyObject* )Py_TYPE(date)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | static PyObject * |
| 3090 | date_add(PyObject *left, PyObject *right) |
| 3091 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3092 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 3093 | Py_RETURN_NOTIMPLEMENTED; |
| 3094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3095 | if (PyDate_Check(left)) { |
| 3096 | /* date + ??? */ |
| 3097 | if (PyDelta_Check(right)) |
| 3098 | /* date + delta */ |
| 3099 | return add_date_timedelta((PyDateTime_Date *) left, |
| 3100 | (PyDateTime_Delta *) right, |
| 3101 | 0); |
| 3102 | } |
| 3103 | else { |
| 3104 | /* ??? + date |
| 3105 | * 'right' must be one of us, or we wouldn't have been called |
| 3106 | */ |
| 3107 | if (PyDelta_Check(left)) |
| 3108 | /* delta + date */ |
| 3109 | return add_date_timedelta((PyDateTime_Date *) right, |
| 3110 | (PyDateTime_Delta *) left, |
| 3111 | 0); |
| 3112 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3113 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | static PyObject * |
| 3117 | date_subtract(PyObject *left, PyObject *right) |
| 3118 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3119 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 3120 | Py_RETURN_NOTIMPLEMENTED; |
| 3121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | if (PyDate_Check(left)) { |
| 3123 | if (PyDate_Check(right)) { |
| 3124 | /* date - date */ |
| 3125 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 3126 | GET_MONTH(left), |
| 3127 | GET_DAY(left)); |
| 3128 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 3129 | GET_MONTH(right), |
| 3130 | GET_DAY(right)); |
| 3131 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 3132 | } |
| 3133 | if (PyDelta_Check(right)) { |
| 3134 | /* date - delta */ |
| 3135 | return add_date_timedelta((PyDateTime_Date *) left, |
| 3136 | (PyDateTime_Delta *) right, |
| 3137 | 1); |
| 3138 | } |
| 3139 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3140 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | |
| 3144 | /* Various ways to turn a date into a string. */ |
| 3145 | |
| 3146 | static PyObject * |
| 3147 | date_repr(PyDateTime_Date *self) |
| 3148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3150 | Py_TYPE(self)->tp_name, |
| 3151 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3155 | date_isoformat(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 3158 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 3161 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3162 | static PyObject * |
| 3163 | date_str(PyDateTime_Date *self) |
| 3164 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3165 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | |
| 3169 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3170 | date_ctime(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | static PyObject * |
| 3176 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3177 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | /* This method can be inherited, and needs to call the |
| 3179 | * timetuple() method appropriate to self's class. |
| 3180 | */ |
| 3181 | PyObject *result; |
| 3182 | PyObject *tuple; |
| 3183 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3184 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3188 | &format)) |
| 3189 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3190 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3191 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | if (tuple == NULL) |
| 3193 | return NULL; |
| 3194 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3195 | (PyObject *)self); |
| 3196 | Py_DECREF(tuple); |
| 3197 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3200 | static PyObject * |
| 3201 | date_format(PyDateTime_Date *self, PyObject *args) |
| 3202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3203 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 3206 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 3209 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3211 | |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 3212 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_strftime, |
| 3213 | format, NULL); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3216 | /* ISO methods. */ |
| 3217 | |
| 3218 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3219 | date_isoweekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3227 | date_isocalendar(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | int year = GET_YEAR(self); |
| 3230 | int week1_monday = iso_week1_monday(year); |
| 3231 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 3232 | int week; |
| 3233 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | week = divmod(today - week1_monday, 7, &day); |
| 3236 | if (week < 0) { |
| 3237 | --year; |
| 3238 | week1_monday = iso_week1_monday(year); |
| 3239 | week = divmod(today - week1_monday, 7, &day); |
| 3240 | } |
| 3241 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 3242 | ++year; |
| 3243 | week = 0; |
| 3244 | } |
| 3245 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | /* Miscellaneous methods. */ |
| 3249 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3250 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3251 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | if (PyDate_Check(other)) { |
| 3254 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 3255 | ((PyDateTime_Date *)other)->data, |
| 3256 | _PyDateTime_DATE_DATASIZE); |
| 3257 | return diff_to_bool(diff, op); |
| 3258 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3259 | else |
| 3260 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3264 | date_timetuple(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | return build_struct_time(GET_YEAR(self), |
| 3267 | GET_MONTH(self), |
| 3268 | GET_DAY(self), |
| 3269 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3272 | static PyObject * |
| 3273 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | PyObject *clone; |
| 3276 | PyObject *tuple; |
| 3277 | int year = GET_YEAR(self); |
| 3278 | int month = GET_MONTH(self); |
| 3279 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 3282 | &year, &month, &day)) |
| 3283 | return NULL; |
| 3284 | tuple = Py_BuildValue("iii", year, month, day); |
| 3285 | if (tuple == NULL) |
| 3286 | return NULL; |
| 3287 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 3288 | Py_DECREF(tuple); |
| 3289 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3292 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3293 | generic_hash(unsigned char *data, int len) |
| 3294 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 3295 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | |
| 3299 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3300 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3301 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3302 | date_hash(PyDateTime_Date *self) |
| 3303 | { |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3304 | if (self->hashcode == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | self->hashcode = generic_hash( |
| 3306 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3307 | } |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3313 | date_toordinal(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 3316 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3320 | date_weekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3327 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3328 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3329 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3330 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3331 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3333 | PyObject* field; |
| 3334 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 3335 | _PyDateTime_DATE_DATASIZE); |
| 3336 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3340 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3347 | /* Class methods: */ |
Tim Hoffmann | a0fd7f1 | 2018-09-24 10:39:02 +0200 | [diff] [blame] | 3348 | DATETIME_DATE_FROMTIMESTAMP_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3350 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 3351 | METH_CLASS, |
| 3352 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 3353 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3354 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 3355 | {"fromisoformat", (PyCFunction)date_fromisoformat, METH_O | |
| 3356 | METH_CLASS, |
| 3357 | PyDoc_STR("str -> Construct a date from the output of date.isoformat()")}, |
| 3358 | |
Paul Ganssle | 88c0937 | 2019-04-29 09:22:03 -0400 | [diff] [blame] | 3359 | {"fromisocalendar", (PyCFunction)(void(*)(void))date_fromisocalendar, |
| 3360 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 3361 | PyDoc_STR("int, int, int -> Construct a date from the ISO year, week " |
| 3362 | "number and weekday.\n\n" |
| 3363 | "This is the inverse of the date.isocalendar() function")}, |
| 3364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 3366 | PyDoc_STR("Current date or datetime: same as " |
| 3367 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 3372 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3373 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 3374 | {"strftime", (PyCFunction)(void(*)(void))date_strftime, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3377 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3378 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3380 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 3381 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3383 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 3384 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 3385 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3387 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 3388 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 3391 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3392 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3394 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 3395 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 3396 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 3399 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3400 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3401 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 3402 | {"replace", (PyCFunction)(void(*)(void))date_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3403 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 3406 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3408 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3409 | }; |
| 3410 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3411 | static const char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3412 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3413 | |
| 3414 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3415 | date_add, /* nb_add */ |
| 3416 | date_subtract, /* nb_subtract */ |
| 3417 | 0, /* nb_multiply */ |
| 3418 | 0, /* nb_remainder */ |
| 3419 | 0, /* nb_divmod */ |
| 3420 | 0, /* nb_power */ |
| 3421 | 0, /* nb_negative */ |
| 3422 | 0, /* nb_positive */ |
| 3423 | 0, /* nb_absolute */ |
| 3424 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3425 | }; |
| 3426 | |
| 3427 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3429 | "datetime.date", /* tp_name */ |
| 3430 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 3431 | 0, /* tp_itemsize */ |
| 3432 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3433 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | 0, /* tp_getattr */ |
| 3435 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3436 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | (reprfunc)date_repr, /* tp_repr */ |
| 3438 | &date_as_number, /* tp_as_number */ |
| 3439 | 0, /* tp_as_sequence */ |
| 3440 | 0, /* tp_as_mapping */ |
| 3441 | (hashfunc)date_hash, /* tp_hash */ |
| 3442 | 0, /* tp_call */ |
| 3443 | (reprfunc)date_str, /* tp_str */ |
| 3444 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3445 | 0, /* tp_setattro */ |
| 3446 | 0, /* tp_as_buffer */ |
| 3447 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3448 | date_doc, /* tp_doc */ |
| 3449 | 0, /* tp_traverse */ |
| 3450 | 0, /* tp_clear */ |
| 3451 | date_richcompare, /* tp_richcompare */ |
| 3452 | 0, /* tp_weaklistoffset */ |
| 3453 | 0, /* tp_iter */ |
| 3454 | 0, /* tp_iternext */ |
| 3455 | date_methods, /* tp_methods */ |
| 3456 | 0, /* tp_members */ |
| 3457 | date_getset, /* tp_getset */ |
| 3458 | 0, /* tp_base */ |
| 3459 | 0, /* tp_dict */ |
| 3460 | 0, /* tp_descr_get */ |
| 3461 | 0, /* tp_descr_set */ |
| 3462 | 0, /* tp_dictoffset */ |
| 3463 | 0, /* tp_init */ |
| 3464 | 0, /* tp_alloc */ |
| 3465 | date_new, /* tp_new */ |
| 3466 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3467 | }; |
| 3468 | |
| 3469 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3470 | * PyDateTime_TZInfo implementation. |
| 3471 | */ |
| 3472 | |
| 3473 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 3474 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 3475 | * 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] | 3476 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3477 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 3478 | * |
| 3479 | * Note: For reasons having to do with pickling of subclasses, we have |
| 3480 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 3481 | * in the Python implementation (__init__() could raise NotImplementedError |
| 3482 | * there without ill effect), but doing so in the C implementation hit a |
| 3483 | * brick wall. |
| 3484 | */ |
| 3485 | |
| 3486 | static PyObject * |
| 3487 | tzinfo_nogo(const char* methodname) |
| 3488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | PyErr_Format(PyExc_NotImplementedError, |
| 3490 | "a tzinfo subclass must implement %s()", |
| 3491 | methodname); |
| 3492 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | /* Methods. A subclass must implement these. */ |
| 3496 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3497 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3498 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 3499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3503 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3504 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 3505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3509 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3510 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 3511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3512 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3515 | |
| 3516 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 3517 | PyDateTime_Delta *delta, |
| 3518 | int factor); |
| 3519 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 3520 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 3521 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3522 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3523 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3524 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3525 | PyObject *result = NULL; |
| 3526 | PyObject *off = NULL, *dst = NULL; |
| 3527 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3528 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3529 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | PyErr_SetString(PyExc_TypeError, |
| 3531 | "fromutc: argument must be a datetime"); |
| 3532 | return NULL; |
| 3533 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3534 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3535 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3536 | "is not self"); |
| 3537 | return NULL; |
| 3538 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3539 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3540 | off = datetime_utcoffset(dt, NULL); |
| 3541 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3542 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3543 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3545 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3546 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3548 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3549 | dst = datetime_dst(dt, NULL); |
| 3550 | if (dst == NULL) |
| 3551 | goto Fail; |
| 3552 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3553 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3554 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3555 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3556 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3557 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3558 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3559 | if (delta == NULL) |
| 3560 | goto Fail; |
| 3561 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3564 | |
| 3565 | Py_DECREF(dst); |
| 3566 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3567 | if (dst == NULL) |
| 3568 | goto Fail; |
| 3569 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | goto Inconsistent; |
Alexander Belopolsky | c79447b | 2015-09-27 21:41:55 -0400 | [diff] [blame] | 3571 | if (delta_bool((PyDateTime_Delta *)dst) != 0) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3572 | Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 3573 | (PyDateTime_Delta *)dst, 1)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3574 | if (result == NULL) |
| 3575 | goto Fail; |
| 3576 | } |
| 3577 | Py_DECREF(delta); |
| 3578 | Py_DECREF(dst); |
| 3579 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3581 | |
| 3582 | Inconsistent: |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 3583 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3584 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3585 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 3586 | /* fall through to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3587 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3588 | Py_XDECREF(off); |
| 3589 | Py_XDECREF(dst); |
| 3590 | Py_XDECREF(delta); |
| 3591 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3592 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3595 | /* |
| 3596 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3597 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3598 | */ |
| 3599 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3600 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3601 | tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3602 | { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3603 | PyObject *args, *state; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3604 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3605 | _Py_IDENTIFIER(__getinitargs__); |
| 3606 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3607 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3608 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | if (getinitargs != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3610 | args = _PyObject_CallNoArg(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3611 | Py_DECREF(getinitargs); |
| 3612 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | return NULL; |
| 3614 | } |
| 3615 | } |
| 3616 | else { |
| 3617 | PyErr_Clear(); |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3618 | |
| 3619 | args = PyTuple_New(0); |
| 3620 | if (args == NULL) { |
| 3621 | return NULL; |
| 3622 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3623 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3624 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3625 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3626 | if (getstate != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3627 | state = _PyObject_CallNoArg(getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | Py_DECREF(getstate); |
| 3629 | if (state == NULL) { |
| 3630 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | return NULL; |
| 3632 | } |
| 3633 | } |
| 3634 | else { |
| 3635 | PyObject **dictptr; |
| 3636 | PyErr_Clear(); |
| 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; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3747 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 3748 | if (op == Py_EQ) |
| 3749 | Py_RETURN_FALSE; |
| 3750 | else |
| 3751 | Py_RETURN_TRUE; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3752 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3753 | return delta_richcompare(self->offset, other->offset, op); |
| 3754 | } |
| 3755 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3756 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3757 | timezone_hash(PyDateTime_TimeZone *self) |
| 3758 | { |
| 3759 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3760 | } |
| 3761 | |
| 3762 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3763 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3764 | otherwise. |
| 3765 | */ |
| 3766 | static int |
| 3767 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3768 | { |
| 3769 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3770 | return 0; |
| 3771 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3772 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3773 | return -1; |
| 3774 | } |
| 3775 | |
| 3776 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3777 | timezone_repr(PyDateTime_TimeZone *self) |
| 3778 | { |
| 3779 | /* Note that although timezone is not subclassable, it is convenient |
| 3780 | to use Py_TYPE(self)->tp_name here. */ |
| 3781 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3782 | |
| 3783 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3784 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3785 | |
| 3786 | if (self->name == NULL) |
| 3787 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3788 | |
| 3789 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3790 | self->name); |
| 3791 | } |
| 3792 | |
| 3793 | |
| 3794 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3795 | timezone_str(PyDateTime_TimeZone *self) |
| 3796 | { |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3797 | int hours, minutes, seconds, microseconds; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3798 | PyObject *offset; |
| 3799 | char sign; |
| 3800 | |
| 3801 | if (self->name != NULL) { |
| 3802 | Py_INCREF(self->name); |
| 3803 | return self->name; |
| 3804 | } |
Victor Stinner | 90fd895 | 2015-09-08 00:12:49 +0200 | [diff] [blame] | 3805 | if ((PyObject *)self == PyDateTime_TimeZone_UTC || |
Alexander Belopolsky | 7827a5b | 2015-09-06 13:07:21 -0400 | [diff] [blame] | 3806 | (GET_TD_DAYS(self->offset) == 0 && |
| 3807 | GET_TD_SECONDS(self->offset) == 0 && |
| 3808 | GET_TD_MICROSECONDS(self->offset) == 0)) |
| 3809 | return PyUnicode_FromString("UTC"); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3810 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3811 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3812 | sign = '-'; |
| 3813 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3814 | if (offset == NULL) |
| 3815 | return NULL; |
| 3816 | } |
| 3817 | else { |
| 3818 | sign = '+'; |
| 3819 | offset = self->offset; |
| 3820 | Py_INCREF(offset); |
| 3821 | } |
| 3822 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3823 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3824 | seconds = GET_TD_SECONDS(offset); |
| 3825 | Py_DECREF(offset); |
| 3826 | minutes = divmod(seconds, 60, &seconds); |
| 3827 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3828 | if (microseconds != 0) { |
| 3829 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d.%06d", |
| 3830 | sign, hours, minutes, |
| 3831 | seconds, microseconds); |
| 3832 | } |
| 3833 | if (seconds != 0) { |
| 3834 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d", |
| 3835 | sign, hours, minutes, seconds); |
| 3836 | } |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3837 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | static PyObject * |
| 3841 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3842 | { |
| 3843 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3844 | return NULL; |
| 3845 | |
| 3846 | return timezone_str(self); |
| 3847 | } |
| 3848 | |
| 3849 | static PyObject * |
| 3850 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3851 | { |
| 3852 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3853 | return NULL; |
| 3854 | |
| 3855 | Py_INCREF(self->offset); |
| 3856 | return self->offset; |
| 3857 | } |
| 3858 | |
| 3859 | static PyObject * |
| 3860 | timezone_dst(PyObject *self, PyObject *dt) |
| 3861 | { |
| 3862 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3863 | return NULL; |
| 3864 | |
| 3865 | Py_RETURN_NONE; |
| 3866 | } |
| 3867 | |
| 3868 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3869 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3870 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3871 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3872 | PyErr_SetString(PyExc_TypeError, |
| 3873 | "fromutc: argument must be a datetime"); |
| 3874 | return NULL; |
| 3875 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3876 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3877 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3878 | "is not self"); |
| 3879 | return NULL; |
| 3880 | } |
| 3881 | |
| 3882 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3883 | } |
| 3884 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3885 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3886 | timezone_getinitargs(PyDateTime_TimeZone *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3887 | { |
| 3888 | if (self->name == NULL) |
| 3889 | return Py_BuildValue("(O)", self->offset); |
| 3890 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3891 | } |
| 3892 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3893 | static PyMethodDef timezone_methods[] = { |
| 3894 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3895 | 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] | 3896 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3897 | |
| 3898 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3899 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3900 | |
| 3901 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3902 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3903 | |
| 3904 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3905 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3906 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3907 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3908 | PyDoc_STR("pickle support")}, |
| 3909 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3910 | {NULL, NULL} |
| 3911 | }; |
| 3912 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3913 | static const char timezone_doc[] = |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3914 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3915 | |
| 3916 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3917 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3918 | "datetime.timezone", /* tp_name */ |
| 3919 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3920 | 0, /* tp_itemsize */ |
| 3921 | (destructor)timezone_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3922 | 0, /* tp_vectorcall_offset */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3923 | 0, /* tp_getattr */ |
| 3924 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3925 | 0, /* tp_as_async */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3926 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3927 | 0, /* tp_as_number */ |
| 3928 | 0, /* tp_as_sequence */ |
| 3929 | 0, /* tp_as_mapping */ |
| 3930 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3931 | 0, /* tp_call */ |
| 3932 | (reprfunc)timezone_str, /* tp_str */ |
| 3933 | 0, /* tp_getattro */ |
| 3934 | 0, /* tp_setattro */ |
| 3935 | 0, /* tp_as_buffer */ |
| 3936 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3937 | timezone_doc, /* tp_doc */ |
| 3938 | 0, /* tp_traverse */ |
| 3939 | 0, /* tp_clear */ |
| 3940 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3941 | 0, /* tp_weaklistoffset */ |
| 3942 | 0, /* tp_iter */ |
| 3943 | 0, /* tp_iternext */ |
| 3944 | timezone_methods, /* tp_methods */ |
| 3945 | 0, /* tp_members */ |
| 3946 | 0, /* tp_getset */ |
| 3947 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3948 | 0, /* tp_dict */ |
| 3949 | 0, /* tp_descr_get */ |
| 3950 | 0, /* tp_descr_set */ |
| 3951 | 0, /* tp_dictoffset */ |
| 3952 | 0, /* tp_init */ |
| 3953 | 0, /* tp_alloc */ |
| 3954 | timezone_new, /* tp_new */ |
| 3955 | }; |
| 3956 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3957 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3958 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3959 | */ |
| 3960 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3961 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3962 | */ |
| 3963 | |
| 3964 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3965 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3967 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3970 | static PyObject * |
| 3971 | time_minute(PyDateTime_Time *self, void *unused) |
| 3972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3973 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | /* The name time_second conflicted with some platform header file. */ |
| 3977 | static PyObject * |
| 3978 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3980 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | static PyObject * |
| 3984 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3985 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3986 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | static PyObject * |
| 3990 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3991 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3992 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3993 | Py_INCREF(result); |
| 3994 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3997 | static PyObject * |
| 3998 | time_fold(PyDateTime_Time *self, void *unused) |
| 3999 | { |
| 4000 | return PyLong_FromLong(TIME_GET_FOLD(self)); |
| 4001 | } |
| 4002 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4003 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | {"hour", (getter)time_hour}, |
| 4005 | {"minute", (getter)time_minute}, |
| 4006 | {"second", (getter)py_time_second}, |
| 4007 | {"microsecond", (getter)time_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4008 | {"tzinfo", (getter)time_tzinfo}, |
| 4009 | {"fold", (getter)time_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4011 | }; |
| 4012 | |
| 4013 | /* |
| 4014 | * Constructors. |
| 4015 | */ |
| 4016 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4017 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4018 | "tzinfo", "fold", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4019 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4020 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4021 | time_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo) |
| 4022 | { |
| 4023 | PyDateTime_Time *me; |
| 4024 | char aware = (char)(tzinfo != Py_None); |
| 4025 | |
| 4026 | if (aware && check_tzinfo_subclass(tzinfo) < 0) { |
| 4027 | PyErr_SetString(PyExc_TypeError, "bad tzinfo state arg"); |
| 4028 | return NULL; |
| 4029 | } |
| 4030 | |
| 4031 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 4032 | if (me != NULL) { |
| 4033 | const char *pdata = PyBytes_AS_STRING(state); |
| 4034 | |
| 4035 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 4036 | me->hashcode = -1; |
| 4037 | me->hastzinfo = aware; |
| 4038 | if (aware) { |
| 4039 | Py_INCREF(tzinfo); |
| 4040 | me->tzinfo = tzinfo; |
| 4041 | } |
| 4042 | if (pdata[0] & (1 << 7)) { |
| 4043 | me->data[0] -= 128; |
| 4044 | me->fold = 1; |
| 4045 | } |
| 4046 | else { |
| 4047 | me->fold = 0; |
| 4048 | } |
| 4049 | } |
| 4050 | return (PyObject *)me; |
| 4051 | } |
| 4052 | |
| 4053 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4054 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4057 | int hour = 0; |
| 4058 | int minute = 0; |
| 4059 | int second = 0; |
| 4060 | int usecond = 0; |
| 4061 | PyObject *tzinfo = Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4062 | int fold = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4064 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4065 | if (PyTuple_GET_SIZE(args) >= 1 && PyTuple_GET_SIZE(args) <= 2) { |
| 4066 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 4067 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4068 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4069 | } |
| 4070 | if (PyBytes_Check(state)) { |
| 4071 | if (PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 4072 | (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) |
| 4073 | { |
| 4074 | return time_from_pickle(type, state, tzinfo); |
| 4075 | } |
| 4076 | } |
| 4077 | else if (PyUnicode_Check(state)) { |
| 4078 | if (PyUnicode_READY(state)) { |
| 4079 | return NULL; |
| 4080 | } |
| 4081 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_TIME_DATASIZE && |
| 4082 | (0x7F & PyUnicode_READ_CHAR(state, 2)) < 24) |
| 4083 | { |
| 4084 | state = PyUnicode_AsLatin1String(state); |
| 4085 | if (state == NULL) { |
| 4086 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4087 | /* More informative error message. */ |
| 4088 | PyErr_SetString(PyExc_ValueError, |
| 4089 | "Failed to encode latin1 string when unpickling " |
| 4090 | "a time object. " |
| 4091 | "pickle.load(data, encoding='latin1') is assumed."); |
| 4092 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 4093 | return NULL; |
| 4094 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4095 | self = time_from_pickle(type, state, tzinfo); |
| 4096 | Py_DECREF(state); |
| 4097 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4098 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4099 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4100 | tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4102 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4103 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | &hour, &minute, &second, &usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4105 | &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4106 | self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, |
| 4107 | type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4108 | } |
| 4109 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | /* |
| 4113 | * Destructor. |
| 4114 | */ |
| 4115 | |
| 4116 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4117 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4118 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4119 | if (HASTZINFO(self)) { |
| 4120 | Py_XDECREF(self->tzinfo); |
| 4121 | } |
| 4122 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4126 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4127 | */ |
| 4128 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4129 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4130 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4131 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 4132 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
| 4135 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4136 | time_dst(PyObject *self, PyObject *unused) { |
| 4137 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4141 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4142 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4146 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4147 | */ |
| 4148 | |
| 4149 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4150 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4152 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4153 | int h = TIME_GET_HOUR(self); |
| 4154 | int m = TIME_GET_MINUTE(self); |
| 4155 | int s = TIME_GET_SECOND(self); |
| 4156 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4157 | int fold = TIME_GET_FOLD(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4160 | if (us) |
| 4161 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 4162 | type_name, h, m, s, us); |
| 4163 | else if (s) |
| 4164 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 4165 | type_name, h, m, s); |
| 4166 | else |
| 4167 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 4168 | if (result != NULL && HASTZINFO(self)) |
| 4169 | result = append_keyword_tzinfo(result, self->tzinfo); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4170 | if (result != NULL && fold) |
| 4171 | result = append_keyword_fold(result, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4175 | static PyObject * |
| 4176 | time_str(PyDateTime_Time *self) |
| 4177 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 4178 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4179 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4180 | |
| 4181 | static PyObject * |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4182 | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | char buf[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4185 | char *timespec = NULL; |
| 4186 | static char *keywords[] = {"timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4187 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 4188 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4189 | static char *specs[][2] = { |
| 4190 | {"hours", "%02d"}, |
| 4191 | {"minutes", "%02d:%02d"}, |
| 4192 | {"seconds", "%02d:%02d:%02d"}, |
| 4193 | {"milliseconds", "%02d:%02d:%02d.%03d"}, |
| 4194 | {"microseconds", "%02d:%02d:%02d.%06d"}, |
| 4195 | }; |
| 4196 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4197 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4198 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) |
| 4199 | return NULL; |
| 4200 | |
| 4201 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 4202 | if (us == 0) { |
| 4203 | /* seconds */ |
| 4204 | given_spec = 2; |
| 4205 | } |
| 4206 | else { |
| 4207 | /* microseconds */ |
| 4208 | given_spec = 4; |
| 4209 | } |
| 4210 | } |
| 4211 | else { |
| 4212 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 4213 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 4214 | if (given_spec == 3) { |
| 4215 | /* milliseconds */ |
| 4216 | us = us / 1000; |
| 4217 | } |
| 4218 | break; |
| 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 4224 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 4225 | return NULL; |
| 4226 | } |
| 4227 | else { |
| 4228 | result = PyUnicode_FromFormat(specs[given_spec][1], |
| 4229 | TIME_GET_HOUR(self), TIME_GET_MINUTE(self), |
| 4230 | TIME_GET_SECOND(self), us); |
| 4231 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4232 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4233 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4234 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4236 | /* We need to append the UTC offset. */ |
| 4237 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 4238 | Py_None) < 0) { |
| 4239 | Py_DECREF(result); |
| 4240 | return NULL; |
| 4241 | } |
| 4242 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 4243 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4246 | static PyObject * |
| 4247 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 4248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4249 | PyObject *result; |
| 4250 | PyObject *tuple; |
| 4251 | PyObject *format; |
| 4252 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4254 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 4255 | &format)) |
| 4256 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | /* Python's strftime does insane things with the year part of the |
| 4259 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 4260 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4261 | */ |
| 4262 | tuple = Py_BuildValue("iiiiiiiii", |
| 4263 | 1900, 1, 1, /* year, month, day */ |
| 4264 | TIME_GET_HOUR(self), |
| 4265 | TIME_GET_MINUTE(self), |
| 4266 | TIME_GET_SECOND(self), |
| 4267 | 0, 1, -1); /* weekday, daynum, dst */ |
| 4268 | if (tuple == NULL) |
| 4269 | return NULL; |
| 4270 | assert(PyTuple_Size(tuple) == 9); |
| 4271 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 4272 | Py_None); |
| 4273 | Py_DECREF(tuple); |
| 4274 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4275 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4276 | |
| 4277 | /* |
| 4278 | * Miscellaneous methods. |
| 4279 | */ |
| 4280 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4281 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4282 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4283 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4284 | PyObject *result = NULL; |
| 4285 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4286 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4287 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4288 | if (! PyTime_Check(other)) |
| 4289 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4290 | |
| 4291 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4292 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4293 | ((PyDateTime_Time *)other)->data, |
| 4294 | _PyDateTime_TIME_DATASIZE); |
| 4295 | return diff_to_bool(diff, op); |
| 4296 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4297 | offset1 = time_utcoffset(self, NULL); |
| 4298 | if (offset1 == NULL) |
| 4299 | return NULL; |
| 4300 | offset2 = time_utcoffset(other, NULL); |
| 4301 | if (offset2 == NULL) |
| 4302 | goto done; |
| 4303 | /* If they're both naive, or both aware and have the same offsets, |
| 4304 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4305 | * offset2 == Py_None at this point. |
| 4306 | */ |
| 4307 | if ((offset1 == offset2) || |
| 4308 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4309 | delta_cmp(offset1, offset2) == 0)) { |
| 4310 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4311 | ((PyDateTime_Time *)other)->data, |
| 4312 | _PyDateTime_TIME_DATASIZE); |
| 4313 | result = diff_to_bool(diff, op); |
| 4314 | } |
| 4315 | /* The hard case: both aware with different UTC offsets */ |
| 4316 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 4317 | int offsecs1, offsecs2; |
| 4318 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 4319 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 4320 | TIME_GET_MINUTE(self) * 60 + |
| 4321 | TIME_GET_SECOND(self) - |
| 4322 | GET_TD_DAYS(offset1) * 86400 - |
| 4323 | GET_TD_SECONDS(offset1); |
| 4324 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 4325 | TIME_GET_MINUTE(other) * 60 + |
| 4326 | TIME_GET_SECOND(other) - |
| 4327 | GET_TD_DAYS(offset2) * 86400 - |
| 4328 | GET_TD_SECONDS(offset2); |
| 4329 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4330 | if (diff == 0) |
| 4331 | diff = TIME_GET_MICROSECOND(self) - |
| 4332 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4333 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4334 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4335 | else if (op == Py_EQ) { |
| 4336 | result = Py_False; |
| 4337 | Py_INCREF(result); |
| 4338 | } |
| 4339 | else if (op == Py_NE) { |
| 4340 | result = Py_True; |
| 4341 | Py_INCREF(result); |
| 4342 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4343 | else { |
| 4344 | PyErr_SetString(PyExc_TypeError, |
| 4345 | "can't compare offset-naive and " |
| 4346 | "offset-aware times"); |
| 4347 | } |
| 4348 | done: |
| 4349 | Py_DECREF(offset1); |
| 4350 | Py_XDECREF(offset2); |
| 4351 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4354 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4355 | time_hash(PyDateTime_Time *self) |
| 4356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4358 | PyObject *offset, *self0; |
Victor Stinner | 423c16b | 2017-01-03 23:47:12 +0100 | [diff] [blame] | 4359 | if (TIME_GET_FOLD(self)) { |
| 4360 | self0 = new_time_ex2(TIME_GET_HOUR(self), |
| 4361 | TIME_GET_MINUTE(self), |
| 4362 | TIME_GET_SECOND(self), |
| 4363 | TIME_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4364 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4365 | 0, Py_TYPE(self)); |
| 4366 | if (self0 == NULL) |
| 4367 | return -1; |
| 4368 | } |
| 4369 | else { |
| 4370 | self0 = (PyObject *)self; |
| 4371 | Py_INCREF(self0); |
| 4372 | } |
| 4373 | offset = time_utcoffset(self0, NULL); |
| 4374 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4375 | |
| 4376 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4380 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4381 | self->hashcode = generic_hash( |
| 4382 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4384 | PyObject *temp1, *temp2; |
| 4385 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4386 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4387 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 4388 | TIME_GET_MINUTE(self) * 60 + |
| 4389 | TIME_GET_SECOND(self); |
| 4390 | microseconds = TIME_GET_MICROSECOND(self); |
| 4391 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 4392 | if (temp1 == NULL) { |
| 4393 | Py_DECREF(offset); |
| 4394 | return -1; |
| 4395 | } |
| 4396 | temp2 = delta_subtract(temp1, offset); |
| 4397 | Py_DECREF(temp1); |
| 4398 | if (temp2 == NULL) { |
| 4399 | Py_DECREF(offset); |
| 4400 | return -1; |
| 4401 | } |
| 4402 | self->hashcode = PyObject_Hash(temp2); |
| 4403 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4405 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4406 | } |
| 4407 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4408 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4409 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4410 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4411 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | PyObject *clone; |
| 4414 | PyObject *tuple; |
| 4415 | int hh = TIME_GET_HOUR(self); |
| 4416 | int mm = TIME_GET_MINUTE(self); |
| 4417 | int ss = TIME_GET_SECOND(self); |
| 4418 | int us = TIME_GET_MICROSECOND(self); |
| 4419 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4420 | int fold = TIME_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4421 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4422 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | time_kws, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4424 | &hh, &mm, &ss, &us, &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 4426 | if (fold != 0 && fold != 1) { |
| 4427 | PyErr_SetString(PyExc_ValueError, |
| 4428 | "fold must be either 0 or 1"); |
| 4429 | return NULL; |
| 4430 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 4432 | if (tuple == NULL) |
| 4433 | return NULL; |
| 4434 | clone = time_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4435 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4436 | TIME_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4437 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | Py_DECREF(tuple); |
| 4439 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4440 | } |
| 4441 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4442 | static PyObject * |
| 4443 | time_fromisoformat(PyObject *cls, PyObject *tstr) { |
| 4444 | assert(tstr != NULL); |
| 4445 | |
| 4446 | if (!PyUnicode_Check(tstr)) { |
| 4447 | PyErr_SetString(PyExc_TypeError, "fromisoformat: argument must be str"); |
| 4448 | return NULL; |
| 4449 | } |
| 4450 | |
| 4451 | Py_ssize_t len; |
| 4452 | const char *p = PyUnicode_AsUTF8AndSize(tstr, &len); |
| 4453 | |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4454 | if (p == NULL) { |
| 4455 | goto invalid_string_error; |
| 4456 | } |
| 4457 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4458 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 4459 | int tzoffset, tzimicrosecond = 0; |
| 4460 | int rv = parse_isoformat_time(p, len, |
| 4461 | &hour, &minute, &second, µsecond, |
| 4462 | &tzoffset, &tzimicrosecond); |
| 4463 | |
| 4464 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4465 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4466 | } |
| 4467 | |
| 4468 | PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, |
| 4469 | tzimicrosecond); |
| 4470 | |
| 4471 | if (tzinfo == NULL) { |
| 4472 | return NULL; |
| 4473 | } |
| 4474 | |
| 4475 | PyObject *t; |
| 4476 | if ( (PyTypeObject *)cls == &PyDateTime_TimeType ) { |
| 4477 | t = new_time(hour, minute, second, microsecond, tzinfo, 0); |
| 4478 | } else { |
| 4479 | t = PyObject_CallFunction(cls, "iiiiO", |
| 4480 | hour, minute, second, microsecond, tzinfo); |
| 4481 | } |
| 4482 | |
| 4483 | Py_DECREF(tzinfo); |
| 4484 | return t; |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 4485 | |
| 4486 | invalid_string_error: |
| 4487 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", tstr); |
| 4488 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4492 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4493 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4494 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4495 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4496 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4497 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4498 | */ |
| 4499 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4500 | time_getstate(PyDateTime_Time *self, int proto) |
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 | PyObject *basestate; |
| 4503 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 4506 | _PyDateTime_TIME_DATASIZE); |
| 4507 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4508 | if (proto > 3 && TIME_GET_FOLD(self)) |
| 4509 | /* Set the first bit of the first byte */ |
| 4510 | PyBytes_AS_STRING(basestate)[0] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 4512 | result = PyTuple_Pack(1, basestate); |
| 4513 | else |
| 4514 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 4515 | Py_DECREF(basestate); |
| 4516 | } |
| 4517 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4521 | time_reduce_ex(PyDateTime_Time *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4522 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4523 | int proto; |
| 4524 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4525 | return NULL; |
| 4526 | |
| 4527 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4530 | static PyObject * |
| 4531 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
| 4532 | { |
| 4533 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); |
| 4534 | } |
| 4535 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4536 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4537 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4538 | {"isoformat", (PyCFunction)(void(*)(void))time_isoformat, METH_VARARGS | METH_KEYWORDS, |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4539 | PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" |
| 4540 | "[+HH:MM].\n\n" |
| 4541 | "timespec specifies what components of the time to include.\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4542 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4543 | {"strftime", (PyCFunction)(void(*)(void))time_strftime, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4544 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 4547 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 4548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 4550 | PyDoc_STR("Return self.tzinfo.utcoffset(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 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 4553 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4555 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 4556 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4557 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 4558 | {"replace", (PyCFunction)(void(*)(void))time_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4560 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4561 | {"fromisoformat", (PyCFunction)time_fromisoformat, METH_O | METH_CLASS, |
| 4562 | PyDoc_STR("string -> time from time.isoformat() output")}, |
| 4563 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4564 | {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4565 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4566 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4567 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 4568 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 4569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4570 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4571 | }; |
| 4572 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4573 | static const char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4574 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 4575 | \n\ |
| 4576 | 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] | 4577 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4578 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4579 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4581 | "datetime.time", /* tp_name */ |
| 4582 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 4583 | 0, /* tp_itemsize */ |
| 4584 | (destructor)time_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4585 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | 0, /* tp_getattr */ |
| 4587 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4588 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | (reprfunc)time_repr, /* tp_repr */ |
Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 4590 | 0, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | 0, /* tp_as_sequence */ |
| 4592 | 0, /* tp_as_mapping */ |
| 4593 | (hashfunc)time_hash, /* tp_hash */ |
| 4594 | 0, /* tp_call */ |
| 4595 | (reprfunc)time_str, /* tp_str */ |
| 4596 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4597 | 0, /* tp_setattro */ |
| 4598 | 0, /* tp_as_buffer */ |
| 4599 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4600 | time_doc, /* tp_doc */ |
| 4601 | 0, /* tp_traverse */ |
| 4602 | 0, /* tp_clear */ |
| 4603 | time_richcompare, /* tp_richcompare */ |
| 4604 | 0, /* tp_weaklistoffset */ |
| 4605 | 0, /* tp_iter */ |
| 4606 | 0, /* tp_iternext */ |
| 4607 | time_methods, /* tp_methods */ |
| 4608 | 0, /* tp_members */ |
| 4609 | time_getset, /* tp_getset */ |
| 4610 | 0, /* tp_base */ |
| 4611 | 0, /* tp_dict */ |
| 4612 | 0, /* tp_descr_get */ |
| 4613 | 0, /* tp_descr_set */ |
| 4614 | 0, /* tp_dictoffset */ |
| 4615 | 0, /* tp_init */ |
| 4616 | time_alloc, /* tp_alloc */ |
| 4617 | time_new, /* tp_new */ |
| 4618 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4619 | }; |
| 4620 | |
| 4621 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4622 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4623 | */ |
| 4624 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4625 | /* Accessor properties. Properties for day, month, and year are inherited |
| 4626 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4627 | */ |
| 4628 | |
| 4629 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4630 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4632 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4635 | static PyObject * |
| 4636 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 4637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | static PyObject * |
| 4642 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 4643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4644 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | static PyObject * |
| 4648 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 4649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | static PyObject * |
| 4654 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 4655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4656 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 4657 | Py_INCREF(result); |
| 4658 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4661 | static PyObject * |
| 4662 | datetime_fold(PyDateTime_DateTime *self, void *unused) |
| 4663 | { |
| 4664 | return PyLong_FromLong(DATE_GET_FOLD(self)); |
| 4665 | } |
| 4666 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4667 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4668 | {"hour", (getter)datetime_hour}, |
| 4669 | {"minute", (getter)datetime_minute}, |
| 4670 | {"second", (getter)datetime_second}, |
| 4671 | {"microsecond", (getter)datetime_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4672 | {"tzinfo", (getter)datetime_tzinfo}, |
| 4673 | {"fold", (getter)datetime_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4675 | }; |
| 4676 | |
| 4677 | /* |
| 4678 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4679 | */ |
| 4680 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4681 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | "year", "month", "day", "hour", "minute", "second", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4683 | "microsecond", "tzinfo", "fold", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4684 | }; |
| 4685 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4686 | static PyObject * |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4687 | datetime_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo) |
| 4688 | { |
| 4689 | PyDateTime_DateTime *me; |
| 4690 | char aware = (char)(tzinfo != Py_None); |
| 4691 | |
| 4692 | if (aware && check_tzinfo_subclass(tzinfo) < 0) { |
| 4693 | PyErr_SetString(PyExc_TypeError, "bad tzinfo state arg"); |
| 4694 | return NULL; |
| 4695 | } |
| 4696 | |
| 4697 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4698 | if (me != NULL) { |
| 4699 | const char *pdata = PyBytes_AS_STRING(state); |
| 4700 | |
| 4701 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4702 | me->hashcode = -1; |
| 4703 | me->hastzinfo = aware; |
| 4704 | if (aware) { |
| 4705 | Py_INCREF(tzinfo); |
| 4706 | me->tzinfo = tzinfo; |
| 4707 | } |
| 4708 | if (pdata[2] & (1 << 7)) { |
| 4709 | me->data[2] -= 128; |
| 4710 | me->fold = 1; |
| 4711 | } |
| 4712 | else { |
| 4713 | me->fold = 0; |
| 4714 | } |
| 4715 | } |
| 4716 | return (PyObject *)me; |
| 4717 | } |
| 4718 | |
| 4719 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4720 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4721 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4722 | PyObject *self = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4723 | int year; |
| 4724 | int month; |
| 4725 | int day; |
| 4726 | int hour = 0; |
| 4727 | int minute = 0; |
| 4728 | int second = 0; |
| 4729 | int usecond = 0; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4730 | int fold = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4731 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | /* Check for invocation from pickle with __getstate__ state */ |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4734 | if (PyTuple_GET_SIZE(args) >= 1 && PyTuple_GET_SIZE(args) <= 2) { |
| 4735 | PyObject *state = PyTuple_GET_ITEM(args, 0); |
| 4736 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4737 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4738 | } |
| 4739 | if (PyBytes_Check(state)) { |
| 4740 | if (PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4741 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) |
| 4742 | { |
| 4743 | return datetime_from_pickle(type, state, tzinfo); |
| 4744 | } |
| 4745 | } |
| 4746 | else if (PyUnicode_Check(state)) { |
| 4747 | if (PyUnicode_READY(state)) { |
| 4748 | return NULL; |
| 4749 | } |
| 4750 | if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4751 | MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2) & 0x7F)) |
| 4752 | { |
| 4753 | state = PyUnicode_AsLatin1String(state); |
| 4754 | if (state == NULL) { |
| 4755 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 4756 | /* More informative error message. */ |
| 4757 | PyErr_SetString(PyExc_ValueError, |
| 4758 | "Failed to encode latin1 string when unpickling " |
| 4759 | "a datetime object. " |
| 4760 | "pickle.load(data, encoding='latin1') is assumed."); |
| 4761 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 4762 | return NULL; |
| 4763 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4764 | self = datetime_from_pickle(type, state, tzinfo); |
| 4765 | Py_DECREF(state); |
| 4766 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4768 | } |
Serhiy Storchaka | 8452ca1 | 2018-12-07 13:42:10 +0200 | [diff] [blame] | 4769 | tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4770 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4771 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4772 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4773 | &year, &month, &day, &hour, &minute, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4774 | &second, &usecond, &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4775 | self = new_datetime_ex2(year, month, day, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | hour, minute, second, usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4777 | tzinfo, fold, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | } |
| 4779 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4782 | /* TM_FUNC is the shared type of _PyTime_localtime() and |
| 4783 | * _PyTime_gmtime(). */ |
| 4784 | typedef int (*TM_FUNC)(time_t timer, struct tm*); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4785 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4786 | /* As of version 2015f max fold in IANA database is |
| 4787 | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4788 | static long long max_fold_seconds = 24 * 3600; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4789 | /* NB: date(1970,1,1).toordinal() == 719163 */ |
Benjamin Peterson | ac965ca | 2016-09-18 18:12:21 -0700 | [diff] [blame] | 4790 | static long long epoch = 719163LL * 24 * 60 * 60; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4791 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4792 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4793 | utc_to_seconds(int year, int month, int day, |
| 4794 | int hour, int minute, int second) |
| 4795 | { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4796 | long long ordinal; |
| 4797 | |
| 4798 | /* ymd_to_ord() doesn't support year <= 0 */ |
| 4799 | if (year < MINYEAR || year > MAXYEAR) { |
| 4800 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
| 4801 | return -1; |
| 4802 | } |
| 4803 | |
| 4804 | ordinal = ymd_to_ord(year, month, day); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4805 | return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; |
| 4806 | } |
| 4807 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4808 | static long long |
| 4809 | local(long long u) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4810 | { |
| 4811 | struct tm local_time; |
Alexander Belopolsky | 8e1d3a2 | 2016-07-25 13:54:51 -0400 | [diff] [blame] | 4812 | time_t t; |
| 4813 | u -= epoch; |
| 4814 | t = u; |
| 4815 | if (t != u) { |
| 4816 | PyErr_SetString(PyExc_OverflowError, |
| 4817 | "timestamp out of range for platform time_t"); |
| 4818 | return -1; |
| 4819 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4820 | if (_PyTime_localtime(t, &local_time) != 0) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4821 | return -1; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4822 | return utc_to_seconds(local_time.tm_year + 1900, |
| 4823 | local_time.tm_mon + 1, |
| 4824 | local_time.tm_mday, |
| 4825 | local_time.tm_hour, |
| 4826 | local_time.tm_min, |
| 4827 | local_time.tm_sec); |
| 4828 | } |
| 4829 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4830 | /* Internal helper. |
| 4831 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4832 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4833 | */ |
| 4834 | static PyObject * |
| 4835 | 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] | 4836 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4837 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4838 | struct tm tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4839 | int year, month, day, hour, minute, second, fold = 0; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4840 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4841 | if (f(timet, &tm) != 0) |
| 4842 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4843 | |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4844 | year = tm.tm_year + 1900; |
| 4845 | month = tm.tm_mon + 1; |
| 4846 | day = tm.tm_mday; |
| 4847 | hour = tm.tm_hour; |
| 4848 | minute = tm.tm_min; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4849 | /* The platform localtime/gmtime may insert leap seconds, |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4850 | * indicated by tm.tm_sec > 59. We don't care about them, |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4851 | * except to the extent that passing them on to the datetime |
| 4852 | * constructor would raise ValueError for a reason that |
| 4853 | * made no sense to the user. |
| 4854 | */ |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4855 | second = Py_MIN(59, tm.tm_sec); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4856 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4857 | /* local timezone requires to compute fold */ |
Ammar Askar | 96d1e69 | 2018-07-25 09:54:58 -0700 | [diff] [blame] | 4858 | if (tzinfo == Py_None && f == _PyTime_localtime |
| 4859 | /* On Windows, passing a negative value to local results |
| 4860 | * in an OSError because localtime_s on Windows does |
| 4861 | * not support negative timestamps. Unfortunately this |
| 4862 | * means that fold detection for time values between |
| 4863 | * 0 and max_fold_seconds will result in an identical |
| 4864 | * error since we subtract max_fold_seconds to detect a |
| 4865 | * fold. However, since we know there haven't been any |
| 4866 | * folds in the interval [0, max_fold_seconds) in any |
| 4867 | * timezone, we can hackily just forego fold detection |
| 4868 | * for this time range. |
| 4869 | */ |
| 4870 | #ifdef MS_WINDOWS |
| 4871 | && (timet - max_fold_seconds > 0) |
| 4872 | #endif |
| 4873 | ) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4874 | long long probe_seconds, result_seconds, transition; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4875 | |
| 4876 | result_seconds = utc_to_seconds(year, month, day, |
| 4877 | hour, minute, second); |
| 4878 | /* Probe max_fold_seconds to detect a fold. */ |
| 4879 | probe_seconds = local(epoch + timet - max_fold_seconds); |
| 4880 | if (probe_seconds == -1) |
| 4881 | return NULL; |
| 4882 | transition = result_seconds - probe_seconds - max_fold_seconds; |
| 4883 | if (transition < 0) { |
| 4884 | probe_seconds = local(epoch + timet + transition); |
| 4885 | if (probe_seconds == -1) |
| 4886 | return NULL; |
| 4887 | if (probe_seconds == result_seconds) |
| 4888 | fold = 1; |
| 4889 | } |
| 4890 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 4891 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 4892 | second, us, tzinfo, fold, cls); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | /* Internal helper. |
| 4896 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4897 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4898 | * have enough bits to cover a datetime's full range of precision, it's |
| 4899 | * better to call datetime_from_timet_and_us provided you have a way |
| 4900 | * to get that much precision (e.g., C time() isn't good enough). |
| 4901 | */ |
| 4902 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4903 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4904 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4905 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4907 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4908 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 4909 | if (_PyTime_ObjectToTimeval(timestamp, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 4910 | &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4911 | return NULL; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4912 | |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4913 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | /* Internal helper. |
| 4917 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4918 | * gmtime for f as appropriate. |
| 4919 | */ |
| 4920 | static PyObject * |
| 4921 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4922 | { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4923 | _PyTime_t ts = _PyTime_GetSystemClock(); |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4924 | time_t secs; |
| 4925 | int us; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4926 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4927 | if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4928 | return NULL; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4929 | assert(0 <= us && us <= 999999); |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4930 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4931 | return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4934 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4935 | |
| 4936 | @classmethod |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4937 | datetime.datetime.now |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4938 | |
| 4939 | tz: object = None |
| 4940 | Timezone object. |
| 4941 | |
| 4942 | Returns new datetime object representing current time local to tz. |
| 4943 | |
| 4944 | If no tz is specified, uses local timezone. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4945 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4946 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4947 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4948 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4949 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4950 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4951 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4952 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4953 | /* Return best possible local time -- this isn't constrained by the |
| 4954 | * precision of a timestamp. |
| 4955 | */ |
| 4956 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4957 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4958 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4959 | self = datetime_best_possible((PyObject *)type, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4960 | tz == Py_None ? _PyTime_localtime : |
| 4961 | _PyTime_gmtime, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4962 | tz); |
| 4963 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4964 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4965 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4966 | } |
| 4967 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4970 | /* Return best possible UTC time -- this isn't constrained by the |
| 4971 | * precision of a timestamp. |
| 4972 | */ |
| 4973 | static PyObject * |
| 4974 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4975 | { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4976 | return datetime_best_possible(cls, _PyTime_gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4979 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4980 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4981 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4984 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | PyObject *tzinfo = Py_None; |
| 4986 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4987 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4988 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | keywords, ×tamp, &tzinfo)) |
| 4990 | return NULL; |
| 4991 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4992 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4994 | self = datetime_from_timestamp(cls, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4995 | tzinfo == Py_None ? _PyTime_localtime : |
| 4996 | _PyTime_gmtime, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4997 | timestamp, |
| 4998 | tzinfo); |
| 4999 | if (self != NULL && tzinfo != Py_None) { |
| 5000 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 5001 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 | } |
| 5003 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5006 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 5007 | static PyObject * |
| 5008 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 5009 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5010 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5011 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5012 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5013 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5014 | result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | Py_None); |
| 5016 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5019 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5020 | static PyObject * |
| 5021 | datetime_strptime(PyObject *cls, PyObject *args) |
| 5022 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5024 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5025 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5027 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5029 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5030 | if (module == NULL) { |
| 5031 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 5032 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 5033 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5034 | } |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 5035 | return _PyObject_CallMethodIdObjArgs(module, &PyId__strptime_datetime, |
| 5036 | cls, string, format, NULL); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5037 | } |
| 5038 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5039 | /* Return new datetime from date/datetime and time arguments. */ |
| 5040 | static PyObject * |
| 5041 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 5042 | { |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5043 | static char *keywords[] = {"date", "time", "tzinfo", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | PyObject *date; |
| 5045 | PyObject *time; |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5046 | PyObject *tzinfo = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5048 | |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5049 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | &PyDateTime_DateType, &date, |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 5051 | &PyDateTime_TimeType, &time, &tzinfo)) { |
| 5052 | if (tzinfo == NULL) { |
| 5053 | if (HASTZINFO(time)) |
| 5054 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 5055 | else |
| 5056 | tzinfo = Py_None; |
| 5057 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 5058 | result = new_datetime_subclass_fold_ex(GET_YEAR(date), |
| 5059 | GET_MONTH(date), |
| 5060 | GET_DAY(date), |
| 5061 | TIME_GET_HOUR(time), |
| 5062 | TIME_GET_MINUTE(time), |
| 5063 | TIME_GET_SECOND(time), |
| 5064 | TIME_GET_MICROSECOND(time), |
| 5065 | tzinfo, |
| 5066 | TIME_GET_FOLD(time), |
| 5067 | cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | } |
| 5069 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5070 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5071 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5072 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5073 | _sanitize_isoformat_str(PyObject *dtstr) |
| 5074 | { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5075 | // `fromisoformat` allows surrogate characters in exactly one position, |
| 5076 | // the separator; to allow datetime_fromisoformat to make the simplifying |
| 5077 | // assumption that all valid strings can be encoded in UTF-8, this function |
| 5078 | // replaces any surrogate character separators with `T`. |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5079 | // |
| 5080 | // The result of this, if not NULL, returns a new reference |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5081 | Py_ssize_t len = PyUnicode_GetLength(dtstr); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5082 | if (len < 0) { |
| 5083 | return NULL; |
| 5084 | } |
| 5085 | |
| 5086 | if (len <= 10 || |
| 5087 | !Py_UNICODE_IS_SURROGATE(PyUnicode_READ_CHAR(dtstr, 10))) { |
| 5088 | Py_INCREF(dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5089 | return dtstr; |
| 5090 | } |
| 5091 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5092 | PyObject *str_out = _PyUnicode_Copy(dtstr); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5093 | if (str_out == NULL) { |
| 5094 | return NULL; |
| 5095 | } |
| 5096 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5097 | if (PyUnicode_WriteChar(str_out, 10, (Py_UCS4)'T')) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5098 | Py_DECREF(str_out); |
| 5099 | return NULL; |
| 5100 | } |
| 5101 | |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5102 | return str_out; |
| 5103 | } |
| 5104 | |
| 5105 | static PyObject * |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5106 | datetime_fromisoformat(PyObject *cls, PyObject *dtstr) |
| 5107 | { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5108 | assert(dtstr != NULL); |
| 5109 | |
| 5110 | if (!PyUnicode_Check(dtstr)) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5111 | PyErr_SetString(PyExc_TypeError, |
| 5112 | "fromisoformat: argument must be str"); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5113 | return NULL; |
| 5114 | } |
| 5115 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5116 | PyObject *dtstr_clean = _sanitize_isoformat_str(dtstr); |
| 5117 | if (dtstr_clean == NULL) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5118 | goto error; |
| 5119 | } |
| 5120 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5121 | Py_ssize_t len; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5122 | const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr_clean, &len); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5123 | |
| 5124 | if (dt_ptr == NULL) { |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5125 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 5126 | // Encoding errors are invalid string errors at this point |
| 5127 | goto invalid_string_error; |
| 5128 | } |
| 5129 | else { |
| 5130 | goto error; |
| 5131 | } |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | const char *p = dt_ptr; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5135 | |
| 5136 | int year = 0, month = 0, day = 0; |
| 5137 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 5138 | int tzoffset = 0, tzusec = 0; |
| 5139 | |
| 5140 | // date has a fixed length of 10 |
| 5141 | int rv = parse_isoformat_date(p, &year, &month, &day); |
| 5142 | |
| 5143 | if (!rv && len > 10) { |
| 5144 | // In UTF-8, the length of multi-byte characters is encoded in the MSB |
| 5145 | if ((p[10] & 0x80) == 0) { |
| 5146 | p += 11; |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5147 | } |
| 5148 | else { |
| 5149 | switch (p[10] & 0xf0) { |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5150 | case 0xe0: |
| 5151 | p += 13; |
| 5152 | break; |
| 5153 | case 0xf0: |
| 5154 | p += 14; |
| 5155 | break; |
| 5156 | default: |
| 5157 | p += 12; |
| 5158 | break; |
| 5159 | } |
| 5160 | } |
| 5161 | |
| 5162 | len -= (p - dt_ptr); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5163 | rv = parse_isoformat_time(p, len, &hour, &minute, &second, |
| 5164 | µsecond, &tzoffset, &tzusec); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5165 | } |
| 5166 | if (rv < 0) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5167 | goto invalid_string_error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5168 | } |
| 5169 | |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5170 | PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, tzusec); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5171 | if (tzinfo == NULL) { |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5172 | goto error; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5173 | } |
| 5174 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 5175 | PyObject *dt = new_datetime_subclass_ex(year, month, day, hour, minute, |
| 5176 | second, microsecond, tzinfo, cls); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5177 | |
| 5178 | Py_DECREF(tzinfo); |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5179 | Py_DECREF(dtstr_clean); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5180 | return dt; |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5181 | |
| 5182 | invalid_string_error: |
| 5183 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", dtstr); |
| 5184 | |
| 5185 | error: |
Paul Ganssle | 3df8540 | 2018-10-22 12:32:52 -0400 | [diff] [blame] | 5186 | Py_XDECREF(dtstr_clean); |
Paul Ganssle | 096329f | 2018-08-23 11:06:20 -0400 | [diff] [blame] | 5187 | |
| 5188 | return NULL; |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5189 | } |
| 5190 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5191 | /* |
| 5192 | * Destructor. |
| 5193 | */ |
| 5194 | |
| 5195 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5196 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | if (HASTZINFO(self)) { |
| 5199 | Py_XDECREF(self->tzinfo); |
| 5200 | } |
| 5201 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | /* |
| 5205 | * Indirect access to tzinfo methods. |
| 5206 | */ |
| 5207 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5208 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 5209 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5210 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 5211 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5215 | datetime_dst(PyObject *self, PyObject *unused) { |
| 5216 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5220 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 5221 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5225 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5226 | */ |
| 5227 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5228 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 5229 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5230 | */ |
| 5231 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5232 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5233 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5235 | /* Note that the C-level additions can't overflow, because of |
| 5236 | * invariant bounds on the member values. |
| 5237 | */ |
| 5238 | int year = GET_YEAR(date); |
| 5239 | int month = GET_MONTH(date); |
| 5240 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 5241 | int hour = DATE_GET_HOUR(date); |
| 5242 | int minute = DATE_GET_MINUTE(date); |
| 5243 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 5244 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 5245 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | assert(factor == 1 || factor == -1); |
| 5248 | if (normalize_datetime(&year, &month, &day, |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 5249 | &hour, &minute, &second, µsecond) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | return NULL; |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 5251 | } |
| 5252 | |
Paul Ganssle | 89427cd | 2019-02-04 14:42:04 -0500 | [diff] [blame] | 5253 | return new_datetime_subclass_ex(year, month, day, |
| 5254 | hour, minute, second, microsecond, |
| 5255 | HASTZINFO(date) ? date->tzinfo : Py_None, |
| 5256 | (PyObject *)Py_TYPE(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
| 5259 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5260 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | if (PyDateTime_Check(left)) { |
| 5263 | /* datetime + ??? */ |
| 5264 | if (PyDelta_Check(right)) |
| 5265 | /* datetime + delta */ |
| 5266 | return add_datetime_timedelta( |
| 5267 | (PyDateTime_DateTime *)left, |
| 5268 | (PyDateTime_Delta *)right, |
| 5269 | 1); |
| 5270 | } |
| 5271 | else if (PyDelta_Check(left)) { |
| 5272 | /* delta + datetime */ |
| 5273 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 5274 | (PyDateTime_Delta *) left, |
| 5275 | 1); |
| 5276 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5277 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5281 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5285 | if (PyDateTime_Check(left)) { |
| 5286 | /* datetime - ??? */ |
| 5287 | if (PyDateTime_Check(right)) { |
| 5288 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5289 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5290 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5291 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5292 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 5293 | offset2 = offset1 = Py_None; |
| 5294 | Py_INCREF(offset1); |
| 5295 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5296 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5297 | else { |
| 5298 | offset1 = datetime_utcoffset(left, NULL); |
| 5299 | if (offset1 == NULL) |
| 5300 | return NULL; |
| 5301 | offset2 = datetime_utcoffset(right, NULL); |
| 5302 | if (offset2 == NULL) { |
| 5303 | Py_DECREF(offset1); |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 5307 | PyErr_SetString(PyExc_TypeError, |
| 5308 | "can't subtract offset-naive and " |
| 5309 | "offset-aware datetimes"); |
| 5310 | Py_DECREF(offset1); |
| 5311 | Py_DECREF(offset2); |
| 5312 | return NULL; |
| 5313 | } |
| 5314 | } |
| 5315 | if ((offset1 != offset2) && |
| 5316 | delta_cmp(offset1, offset2) != 0) { |
| 5317 | offdiff = delta_subtract(offset1, offset2); |
| 5318 | if (offdiff == NULL) { |
| 5319 | Py_DECREF(offset1); |
| 5320 | Py_DECREF(offset2); |
| 5321 | return NULL; |
| 5322 | } |
| 5323 | } |
| 5324 | Py_DECREF(offset1); |
| 5325 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5326 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 5327 | GET_MONTH(left), |
| 5328 | GET_DAY(left)) - |
| 5329 | ymd_to_ord(GET_YEAR(right), |
| 5330 | GET_MONTH(right), |
| 5331 | GET_DAY(right)); |
| 5332 | /* These can't overflow, since the values are |
| 5333 | * normalized. At most this gives the number of |
| 5334 | * seconds in one day. |
| 5335 | */ |
| 5336 | delta_s = (DATE_GET_HOUR(left) - |
| 5337 | DATE_GET_HOUR(right)) * 3600 + |
| 5338 | (DATE_GET_MINUTE(left) - |
| 5339 | DATE_GET_MINUTE(right)) * 60 + |
| 5340 | (DATE_GET_SECOND(left) - |
| 5341 | DATE_GET_SECOND(right)); |
| 5342 | delta_us = DATE_GET_MICROSECOND(left) - |
| 5343 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 5345 | if (result == NULL) |
| 5346 | return NULL; |
| 5347 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5348 | if (offdiff != NULL) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 5349 | Py_SETREF(result, delta_subtract(result, offdiff)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5350 | Py_DECREF(offdiff); |
| 5351 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5352 | } |
| 5353 | else if (PyDelta_Check(right)) { |
| 5354 | /* datetime - delta */ |
| 5355 | result = add_datetime_timedelta( |
| 5356 | (PyDateTime_DateTime *)left, |
| 5357 | (PyDateTime_Delta *)right, |
| 5358 | -1); |
| 5359 | } |
| 5360 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5362 | if (result == Py_NotImplemented) |
| 5363 | Py_INCREF(result); |
| 5364 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | /* Various ways to turn a datetime into a string. */ |
| 5368 | |
| 5369 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5370 | datetime_repr(PyDateTime_DateTime *self) |
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 | const char *type_name = Py_TYPE(self)->tp_name; |
| 5373 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | if (DATE_GET_MICROSECOND(self)) { |
| 5376 | baserepr = PyUnicode_FromFormat( |
| 5377 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 5378 | type_name, |
| 5379 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5380 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5381 | DATE_GET_SECOND(self), |
| 5382 | DATE_GET_MICROSECOND(self)); |
| 5383 | } |
| 5384 | else if (DATE_GET_SECOND(self)) { |
| 5385 | baserepr = PyUnicode_FromFormat( |
| 5386 | "%s(%d, %d, %d, %d, %d, %d)", |
| 5387 | type_name, |
| 5388 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5389 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5390 | DATE_GET_SECOND(self)); |
| 5391 | } |
| 5392 | else { |
| 5393 | baserepr = PyUnicode_FromFormat( |
| 5394 | "%s(%d, %d, %d, %d, %d)", |
| 5395 | type_name, |
| 5396 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5397 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 5398 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5399 | if (baserepr != NULL && DATE_GET_FOLD(self) != 0) |
| 5400 | baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5401 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 5402 | return baserepr; |
| 5403 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5406 | static PyObject * |
| 5407 | datetime_str(PyDateTime_DateTime *self) |
| 5408 | { |
Victor Stinner | 4c38154 | 2016-12-09 00:33:39 +0100 | [diff] [blame] | 5409 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5410 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5411 | |
| 5412 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5413 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5415 | int sep = 'T'; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5416 | char *timespec = NULL; |
| 5417 | static char *keywords[] = {"sep", "timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5418 | char buffer[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5419 | PyObject *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | int us = DATE_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5421 | static char *specs[][2] = { |
| 5422 | {"hours", "%04d-%02d-%02d%c%02d"}, |
| 5423 | {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, |
| 5424 | {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, |
| 5425 | {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, |
| 5426 | {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, |
| 5427 | }; |
| 5428 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5429 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5430 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5431 | return NULL; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5432 | |
| 5433 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 5434 | if (us == 0) { |
| 5435 | /* seconds */ |
| 5436 | given_spec = 2; |
| 5437 | } |
| 5438 | else { |
| 5439 | /* microseconds */ |
| 5440 | given_spec = 4; |
| 5441 | } |
| 5442 | } |
| 5443 | else { |
| 5444 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 5445 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 5446 | if (given_spec == 3) { |
| 5447 | us = us / 1000; |
| 5448 | } |
| 5449 | break; |
| 5450 | } |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 5455 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 5456 | return NULL; |
| 5457 | } |
| 5458 | else { |
| 5459 | result = PyUnicode_FromFormat(specs[given_spec][1], |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5460 | GET_YEAR(self), GET_MONTH(self), |
| 5461 | GET_DAY(self), (int)sep, |
| 5462 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5463 | DATE_GET_SECOND(self), us); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5464 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 5465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | if (!result || !HASTZINFO(self)) |
| 5467 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | /* We need to append the UTC offset. */ |
| 5470 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 5471 | (PyObject *)self) < 0) { |
| 5472 | Py_DECREF(result); |
| 5473 | return NULL; |
| 5474 | } |
| 5475 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 5476 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5479 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5480 | datetime_ctime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5482 | return format_ctime((PyDateTime_Date *)self, |
| 5483 | DATE_GET_HOUR(self), |
| 5484 | DATE_GET_MINUTE(self), |
| 5485 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5488 | /* Miscellaneous methods. */ |
| 5489 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5490 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5491 | flip_fold(PyObject *dt) |
| 5492 | { |
| 5493 | return new_datetime_ex2(GET_YEAR(dt), |
| 5494 | GET_MONTH(dt), |
| 5495 | GET_DAY(dt), |
| 5496 | DATE_GET_HOUR(dt), |
| 5497 | DATE_GET_MINUTE(dt), |
| 5498 | DATE_GET_SECOND(dt), |
| 5499 | DATE_GET_MICROSECOND(dt), |
| 5500 | HASTZINFO(dt) ? |
| 5501 | ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, |
| 5502 | !DATE_GET_FOLD(dt), |
| 5503 | Py_TYPE(dt)); |
| 5504 | } |
| 5505 | |
| 5506 | static PyObject * |
| 5507 | get_flip_fold_offset(PyObject *dt) |
| 5508 | { |
| 5509 | PyObject *result, *flip_dt; |
| 5510 | |
| 5511 | flip_dt = flip_fold(dt); |
| 5512 | if (flip_dt == NULL) |
| 5513 | return NULL; |
| 5514 | result = datetime_utcoffset(flip_dt, NULL); |
| 5515 | Py_DECREF(flip_dt); |
| 5516 | return result; |
| 5517 | } |
| 5518 | |
| 5519 | /* PEP 495 exception: Whenever one or both of the operands in |
| 5520 | * inter-zone comparison is such that its utcoffset() depends |
Serhiy Storchaka | bac2d5b | 2018-03-28 22:14:26 +0300 | [diff] [blame] | 5521 | * on the value of its fold attribute, the result is False. |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5522 | * |
| 5523 | * Return 1 if exception applies, 0 if not, and -1 on error. |
| 5524 | */ |
| 5525 | static int |
| 5526 | pep495_eq_exception(PyObject *self, PyObject *other, |
| 5527 | PyObject *offset_self, PyObject *offset_other) |
| 5528 | { |
| 5529 | int result = 0; |
| 5530 | PyObject *flip_offset; |
| 5531 | |
| 5532 | flip_offset = get_flip_fold_offset(self); |
| 5533 | if (flip_offset == NULL) |
| 5534 | return -1; |
| 5535 | if (flip_offset != offset_self && |
| 5536 | delta_cmp(flip_offset, offset_self)) |
| 5537 | { |
| 5538 | result = 1; |
| 5539 | goto done; |
| 5540 | } |
| 5541 | Py_DECREF(flip_offset); |
| 5542 | |
| 5543 | flip_offset = get_flip_fold_offset(other); |
| 5544 | if (flip_offset == NULL) |
| 5545 | return -1; |
| 5546 | if (flip_offset != offset_other && |
| 5547 | delta_cmp(flip_offset, offset_other)) |
| 5548 | result = 1; |
| 5549 | done: |
| 5550 | Py_DECREF(flip_offset); |
| 5551 | return result; |
| 5552 | } |
| 5553 | |
| 5554 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 5555 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5556 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5557 | PyObject *result = NULL; |
| 5558 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5559 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5561 | if (! PyDateTime_Check(other)) { |
| 5562 | if (PyDate_Check(other)) { |
| 5563 | /* Prevent invocation of date_richcompare. We want to |
| 5564 | return NotImplemented here to give the other object |
| 5565 | a chance. But since DateTime is a subclass of |
| 5566 | Date, if the other object is a Date, it would |
| 5567 | compute an ordering based on the date part alone, |
| 5568 | and we don't want that. So force unequal or |
| 5569 | uncomparable here in that case. */ |
| 5570 | if (op == Py_EQ) |
| 5571 | Py_RETURN_FALSE; |
| 5572 | if (op == Py_NE) |
| 5573 | Py_RETURN_TRUE; |
| 5574 | return cmperror(self, other); |
| 5575 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5576 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5577 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5578 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5579 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5580 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5581 | ((PyDateTime_DateTime *)other)->data, |
| 5582 | _PyDateTime_DATETIME_DATASIZE); |
| 5583 | return diff_to_bool(diff, op); |
| 5584 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5585 | offset1 = datetime_utcoffset(self, NULL); |
| 5586 | if (offset1 == NULL) |
| 5587 | return NULL; |
| 5588 | offset2 = datetime_utcoffset(other, NULL); |
| 5589 | if (offset2 == NULL) |
| 5590 | goto done; |
| 5591 | /* If they're both naive, or both aware and have the same offsets, |
| 5592 | * we get off cheap. Note that if they're both naive, offset1 == |
| 5593 | * offset2 == Py_None at this point. |
| 5594 | */ |
| 5595 | if ((offset1 == offset2) || |
| 5596 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 5597 | delta_cmp(offset1, offset2) == 0)) { |
| 5598 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5599 | ((PyDateTime_DateTime *)other)->data, |
| 5600 | _PyDateTime_DATETIME_DATASIZE); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5601 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5602 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5603 | if (ex == -1) |
| 5604 | goto done; |
| 5605 | if (ex) |
| 5606 | diff = 1; |
| 5607 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5608 | result = diff_to_bool(diff, op); |
| 5609 | } |
| 5610 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5611 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5612 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5613 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5614 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 5615 | other); |
| 5616 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5617 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5618 | diff = GET_TD_DAYS(delta); |
| 5619 | if (diff == 0) |
| 5620 | diff = GET_TD_SECONDS(delta) | |
| 5621 | GET_TD_MICROSECONDS(delta); |
| 5622 | Py_DECREF(delta); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5623 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5624 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5625 | if (ex == -1) |
| 5626 | goto done; |
| 5627 | if (ex) |
| 5628 | diff = 1; |
| 5629 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5630 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5631 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 5632 | else if (op == Py_EQ) { |
| 5633 | result = Py_False; |
| 5634 | Py_INCREF(result); |
| 5635 | } |
| 5636 | else if (op == Py_NE) { |
| 5637 | result = Py_True; |
| 5638 | Py_INCREF(result); |
| 5639 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5640 | else { |
| 5641 | PyErr_SetString(PyExc_TypeError, |
| 5642 | "can't compare offset-naive and " |
| 5643 | "offset-aware datetimes"); |
| 5644 | } |
| 5645 | done: |
| 5646 | Py_DECREF(offset1); |
| 5647 | Py_XDECREF(offset2); |
| 5648 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5651 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5652 | datetime_hash(PyDateTime_DateTime *self) |
| 5653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5654 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5655 | PyObject *offset, *self0; |
| 5656 | if (DATE_GET_FOLD(self)) { |
| 5657 | self0 = new_datetime_ex2(GET_YEAR(self), |
| 5658 | GET_MONTH(self), |
| 5659 | GET_DAY(self), |
| 5660 | DATE_GET_HOUR(self), |
| 5661 | DATE_GET_MINUTE(self), |
| 5662 | DATE_GET_SECOND(self), |
| 5663 | DATE_GET_MICROSECOND(self), |
| 5664 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 5665 | 0, Py_TYPE(self)); |
| 5666 | if (self0 == NULL) |
| 5667 | return -1; |
| 5668 | } |
| 5669 | else { |
| 5670 | self0 = (PyObject *)self; |
| 5671 | Py_INCREF(self0); |
| 5672 | } |
| 5673 | offset = datetime_utcoffset(self0, NULL); |
| 5674 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5675 | |
| 5676 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5677 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5679 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5680 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5681 | self->hashcode = generic_hash( |
| 5682 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5684 | PyObject *temp1, *temp2; |
| 5685 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5687 | assert(HASTZINFO(self)); |
| 5688 | days = ymd_to_ord(GET_YEAR(self), |
| 5689 | GET_MONTH(self), |
| 5690 | GET_DAY(self)); |
| 5691 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5692 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5693 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5694 | temp1 = new_delta(days, seconds, |
| 5695 | DATE_GET_MICROSECOND(self), |
| 5696 | 1); |
| 5697 | if (temp1 == NULL) { |
| 5698 | Py_DECREF(offset); |
| 5699 | return -1; |
| 5700 | } |
| 5701 | temp2 = delta_subtract(temp1, offset); |
| 5702 | Py_DECREF(temp1); |
| 5703 | if (temp2 == NULL) { |
| 5704 | Py_DECREF(offset); |
| 5705 | return -1; |
| 5706 | } |
| 5707 | self->hashcode = PyObject_Hash(temp2); |
| 5708 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5709 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5710 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5711 | } |
| 5712 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5713 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5714 | |
| 5715 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5716 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5718 | PyObject *clone; |
| 5719 | PyObject *tuple; |
| 5720 | int y = GET_YEAR(self); |
| 5721 | int m = GET_MONTH(self); |
| 5722 | int d = GET_DAY(self); |
| 5723 | int hh = DATE_GET_HOUR(self); |
| 5724 | int mm = DATE_GET_MINUTE(self); |
| 5725 | int ss = DATE_GET_SECOND(self); |
| 5726 | int us = DATE_GET_MICROSECOND(self); |
| 5727 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5728 | int fold = DATE_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5729 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5730 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5731 | datetime_kws, |
| 5732 | &y, &m, &d, &hh, &mm, &ss, &us, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5733 | &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5734 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 5735 | if (fold != 0 && fold != 1) { |
| 5736 | PyErr_SetString(PyExc_ValueError, |
| 5737 | "fold must be either 0 or 1"); |
| 5738 | return NULL; |
| 5739 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5740 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 5741 | if (tuple == NULL) |
| 5742 | return NULL; |
| 5743 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5744 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5745 | DATE_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5746 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5747 | Py_DECREF(tuple); |
| 5748 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5752 | local_timezone_from_timestamp(time_t timestamp) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5753 | { |
| 5754 | PyObject *result = NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5755 | PyObject *delta; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5756 | struct tm local_time_tm; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5757 | PyObject *nameo = NULL; |
| 5758 | const char *zone = NULL; |
| 5759 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5760 | if (_PyTime_localtime(timestamp, &local_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5761 | return NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5762 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5763 | zone = local_time_tm.tm_zone; |
| 5764 | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5765 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 5766 | { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5767 | PyObject *local_time, *utc_time; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5768 | struct tm utc_time_tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5769 | char buf[100]; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5770 | strftime(buf, sizeof(buf), "%Z", &local_time_tm); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5771 | zone = buf; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5772 | local_time = new_datetime(local_time_tm.tm_year + 1900, |
| 5773 | local_time_tm.tm_mon + 1, |
| 5774 | local_time_tm.tm_mday, |
| 5775 | local_time_tm.tm_hour, |
| 5776 | local_time_tm.tm_min, |
| 5777 | local_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5778 | if (local_time == NULL) { |
| 5779 | return NULL; |
| 5780 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5781 | if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5782 | return NULL; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5783 | utc_time = new_datetime(utc_time_tm.tm_year + 1900, |
| 5784 | utc_time_tm.tm_mon + 1, |
| 5785 | utc_time_tm.tm_mday, |
| 5786 | utc_time_tm.tm_hour, |
| 5787 | utc_time_tm.tm_min, |
| 5788 | utc_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5789 | if (utc_time == NULL) { |
| 5790 | Py_DECREF(local_time); |
| 5791 | return NULL; |
| 5792 | } |
| 5793 | delta = datetime_subtract(local_time, utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5794 | Py_DECREF(local_time); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5795 | Py_DECREF(utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5796 | } |
| 5797 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5798 | if (delta == NULL) { |
| 5799 | return NULL; |
| 5800 | } |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5801 | if (zone != NULL) { |
| 5802 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 5803 | if (nameo == NULL) |
| 5804 | goto error; |
| 5805 | } |
| 5806 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 5807 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5808 | error: |
| 5809 | Py_DECREF(delta); |
| 5810 | return result; |
| 5811 | } |
| 5812 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5813 | static PyObject * |
| 5814 | local_timezone(PyDateTime_DateTime *utc_time) |
| 5815 | { |
| 5816 | time_t timestamp; |
| 5817 | PyObject *delta; |
| 5818 | PyObject *one_second; |
| 5819 | PyObject *seconds; |
| 5820 | |
| 5821 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 5822 | if (delta == NULL) |
| 5823 | return NULL; |
| 5824 | one_second = new_delta(0, 1, 0, 0); |
| 5825 | if (one_second == NULL) { |
| 5826 | Py_DECREF(delta); |
| 5827 | return NULL; |
| 5828 | } |
| 5829 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 5830 | (PyDateTime_Delta *)one_second); |
| 5831 | Py_DECREF(one_second); |
| 5832 | Py_DECREF(delta); |
| 5833 | if (seconds == NULL) |
| 5834 | return NULL; |
| 5835 | timestamp = _PyLong_AsTime_t(seconds); |
| 5836 | Py_DECREF(seconds); |
| 5837 | if (timestamp == -1 && PyErr_Occurred()) |
| 5838 | return NULL; |
| 5839 | return local_timezone_from_timestamp(timestamp); |
| 5840 | } |
| 5841 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5842 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5843 | local_to_seconds(int year, int month, int day, |
| 5844 | int hour, int minute, int second, int fold); |
| 5845 | |
| 5846 | static PyObject * |
| 5847 | local_timezone_from_local(PyDateTime_DateTime *local_dt) |
| 5848 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5849 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5850 | time_t timestamp; |
| 5851 | seconds = local_to_seconds(GET_YEAR(local_dt), |
| 5852 | GET_MONTH(local_dt), |
| 5853 | GET_DAY(local_dt), |
| 5854 | DATE_GET_HOUR(local_dt), |
| 5855 | DATE_GET_MINUTE(local_dt), |
| 5856 | DATE_GET_SECOND(local_dt), |
| 5857 | DATE_GET_FOLD(local_dt)); |
| 5858 | if (seconds == -1) |
| 5859 | return NULL; |
| 5860 | /* XXX: add bounds check */ |
| 5861 | timestamp = seconds - epoch; |
| 5862 | return local_timezone_from_timestamp(timestamp); |
| 5863 | } |
| 5864 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5865 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5866 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5867 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5868 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5869 | PyObject *offset; |
| 5870 | PyObject *temp; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5871 | PyObject *self_tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5872 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5873 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5874 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5875 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 5876 | &tzinfo)) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5877 | return NULL; |
| 5878 | |
| 5879 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5880 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5881 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5882 | if (!HASTZINFO(self) || self->tzinfo == Py_None) { |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 5883 | naive: |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5884 | self_tzinfo = local_timezone_from_local(self); |
| 5885 | if (self_tzinfo == NULL) |
| 5886 | return NULL; |
| 5887 | } else { |
| 5888 | self_tzinfo = self->tzinfo; |
| 5889 | Py_INCREF(self_tzinfo); |
| 5890 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5892 | /* Conversion to self's own time zone is a NOP. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5893 | if (self_tzinfo == tzinfo) { |
| 5894 | Py_DECREF(self_tzinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5895 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5896 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5897 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5899 | /* Convert self to UTC. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5900 | offset = call_utcoffset(self_tzinfo, (PyObject *)self); |
| 5901 | Py_DECREF(self_tzinfo); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5902 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5903 | return NULL; |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 5904 | else if(offset == Py_None) { |
| 5905 | Py_DECREF(offset); |
| 5906 | goto naive; |
| 5907 | } |
| 5908 | else if (!PyDelta_Check(offset)) { |
| 5909 | Py_DECREF(offset); |
| 5910 | PyErr_Format(PyExc_TypeError, "utcoffset() returned %.200s," |
| 5911 | " expected timedelta or None", Py_TYPE(offset)->tp_name); |
| 5912 | return NULL; |
| 5913 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5914 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5915 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5916 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5917 | Py_DECREF(offset); |
| 5918 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5919 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5920 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5921 | /* Make sure result is aware and UTC. */ |
| 5922 | if (!HASTZINFO(result)) { |
| 5923 | temp = (PyObject *)result; |
| 5924 | result = (PyDateTime_DateTime *) |
| 5925 | new_datetime_ex2(GET_YEAR(result), |
| 5926 | GET_MONTH(result), |
| 5927 | GET_DAY(result), |
| 5928 | DATE_GET_HOUR(result), |
| 5929 | DATE_GET_MINUTE(result), |
| 5930 | DATE_GET_SECOND(result), |
| 5931 | DATE_GET_MICROSECOND(result), |
| 5932 | PyDateTime_TimeZone_UTC, |
| 5933 | DATE_GET_FOLD(result), |
| 5934 | Py_TYPE(result)); |
| 5935 | Py_DECREF(temp); |
| 5936 | if (result == NULL) |
| 5937 | return NULL; |
| 5938 | } |
| 5939 | else { |
| 5940 | /* Result is already aware - just replace tzinfo. */ |
| 5941 | temp = result->tzinfo; |
| 5942 | result->tzinfo = PyDateTime_TimeZone_UTC; |
| 5943 | Py_INCREF(result->tzinfo); |
| 5944 | Py_DECREF(temp); |
| 5945 | } |
| 5946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5947 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5948 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5949 | if (tzinfo == Py_None) { |
| 5950 | tzinfo = local_timezone(result); |
| 5951 | if (tzinfo == NULL) { |
| 5952 | Py_DECREF(result); |
| 5953 | return NULL; |
| 5954 | } |
| 5955 | } |
| 5956 | else |
| 5957 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5958 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5959 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5960 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5961 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5962 | result = (PyDateTime_DateTime *) |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 5963 | _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_fromutc, temp, NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5964 | Py_DECREF(temp); |
| 5965 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5966 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
| 5969 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5970 | datetime_timetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5971 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5972 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5974 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5975 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5976 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5977 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 5978 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5979 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5980 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5981 | if (dst != Py_None) |
| 5982 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 5983 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5984 | } |
| 5985 | return build_struct_time(GET_YEAR(self), |
| 5986 | GET_MONTH(self), |
| 5987 | GET_DAY(self), |
| 5988 | DATE_GET_HOUR(self), |
| 5989 | DATE_GET_MINUTE(self), |
| 5990 | DATE_GET_SECOND(self), |
| 5991 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5992 | } |
| 5993 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5994 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5995 | local_to_seconds(int year, int month, int day, |
| 5996 | int hour, int minute, int second, int fold) |
| 5997 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5998 | long long t, a, b, u1, u2, t1, t2, lt; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5999 | t = utc_to_seconds(year, month, day, hour, minute, second); |
| 6000 | /* Our goal is to solve t = local(u) for u. */ |
| 6001 | lt = local(t); |
| 6002 | if (lt == -1) |
| 6003 | return -1; |
| 6004 | a = lt - t; |
| 6005 | u1 = t - a; |
| 6006 | t1 = local(u1); |
| 6007 | if (t1 == -1) |
| 6008 | return -1; |
| 6009 | if (t1 == t) { |
| 6010 | /* We found one solution, but it may not be the one we need. |
| 6011 | * Look for an earlier solution (if `fold` is 0), or a |
| 6012 | * later one (if `fold` is 1). */ |
| 6013 | if (fold) |
| 6014 | u2 = u1 + max_fold_seconds; |
| 6015 | else |
| 6016 | u2 = u1 - max_fold_seconds; |
| 6017 | lt = local(u2); |
| 6018 | if (lt == -1) |
| 6019 | return -1; |
| 6020 | b = lt - u2; |
| 6021 | if (a == b) |
| 6022 | return u1; |
| 6023 | } |
| 6024 | else { |
| 6025 | b = t1 - u1; |
| 6026 | assert(a != b); |
| 6027 | } |
| 6028 | u2 = t - b; |
| 6029 | t2 = local(u2); |
| 6030 | if (t2 == -1) |
| 6031 | return -1; |
| 6032 | if (t2 == t) |
| 6033 | return u2; |
| 6034 | if (t1 == t) |
| 6035 | return u1; |
| 6036 | /* We have found both offsets a and b, but neither t - a nor t - b is |
| 6037 | * a solution. This means t is in the gap. */ |
| 6038 | return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); |
| 6039 | } |
| 6040 | |
| 6041 | /* date(1970,1,1).toordinal() == 719163 */ |
| 6042 | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) |
| 6043 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6044 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6045 | datetime_timestamp(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6046 | { |
| 6047 | PyObject *result; |
| 6048 | |
| 6049 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 6050 | PyObject *delta; |
| 6051 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 6052 | if (delta == NULL) |
| 6053 | return NULL; |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6054 | result = delta_total_seconds(delta, NULL); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6055 | Py_DECREF(delta); |
| 6056 | } |
| 6057 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 6058 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6059 | seconds = local_to_seconds(GET_YEAR(self), |
| 6060 | GET_MONTH(self), |
| 6061 | GET_DAY(self), |
| 6062 | DATE_GET_HOUR(self), |
| 6063 | DATE_GET_MINUTE(self), |
| 6064 | DATE_GET_SECOND(self), |
| 6065 | DATE_GET_FOLD(self)); |
| 6066 | if (seconds == -1) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6067 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6068 | result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + |
| 6069 | DATE_GET_MICROSECOND(self) / 1e6); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6070 | } |
| 6071 | return result; |
| 6072 | } |
| 6073 | |
| 6074 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6075 | datetime_getdate(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6077 | return new_date(GET_YEAR(self), |
| 6078 | GET_MONTH(self), |
| 6079 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6080 | } |
| 6081 | |
| 6082 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6083 | datetime_gettime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6085 | return new_time(DATE_GET_HOUR(self), |
| 6086 | DATE_GET_MINUTE(self), |
| 6087 | DATE_GET_SECOND(self), |
| 6088 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6089 | Py_None, |
| 6090 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6091 | } |
| 6092 | |
| 6093 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6094 | datetime_gettimetz(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6095 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6096 | return new_time(DATE_GET_HOUR(self), |
| 6097 | DATE_GET_MINUTE(self), |
| 6098 | DATE_GET_SECOND(self), |
| 6099 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6100 | GET_DT_TZINFO(self), |
| 6101 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
| 6104 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 6105 | datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6106 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6107 | int y, m, d, hh, mm, ss; |
| 6108 | PyObject *tzinfo; |
| 6109 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6110 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6111 | tzinfo = GET_DT_TZINFO(self); |
| 6112 | if (tzinfo == Py_None) { |
| 6113 | utcself = self; |
| 6114 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6115 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6116 | else { |
| 6117 | PyObject *offset; |
| 6118 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 6119 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 6120 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6121 | if (offset == Py_None) { |
| 6122 | Py_DECREF(offset); |
| 6123 | utcself = self; |
| 6124 | Py_INCREF(utcself); |
| 6125 | } |
| 6126 | else { |
| 6127 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 6128 | (PyDateTime_Delta *)offset, -1); |
| 6129 | Py_DECREF(offset); |
| 6130 | if (utcself == NULL) |
| 6131 | return NULL; |
| 6132 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6133 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 6134 | y = GET_YEAR(utcself); |
| 6135 | m = GET_MONTH(utcself); |
| 6136 | d = GET_DAY(utcself); |
| 6137 | hh = DATE_GET_HOUR(utcself); |
| 6138 | mm = DATE_GET_MINUTE(utcself); |
| 6139 | ss = DATE_GET_SECOND(utcself); |
| 6140 | |
| 6141 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6142 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 6145 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 6146 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6147 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6148 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 6149 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 6150 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6151 | */ |
| 6152 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6153 | datetime_getstate(PyDateTime_DateTime *self, int proto) |
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 | PyObject *basestate; |
| 6156 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6158 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 6159 | _PyDateTime_DATETIME_DATASIZE); |
| 6160 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6161 | if (proto > 3 && DATE_GET_FOLD(self)) |
| 6162 | /* Set the first bit of the third byte */ |
| 6163 | PyBytes_AS_STRING(basestate)[2] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6164 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 6165 | result = PyTuple_Pack(1, basestate); |
| 6166 | else |
| 6167 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 6168 | Py_DECREF(basestate); |
| 6169 | } |
| 6170 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6174 | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6175 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6176 | int proto; |
| 6177 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6178 | return NULL; |
| 6179 | |
| 6180 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6181 | } |
| 6182 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6183 | static PyObject * |
| 6184 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
| 6185 | { |
| 6186 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); |
| 6187 | } |
| 6188 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6189 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6191 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6192 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 6193 | DATETIME_DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6195 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 6196 | METH_NOARGS | METH_CLASS, |
| 6197 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6198 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6199 | {"fromtimestamp", (PyCFunction)(void(*)(void))datetime_fromtimestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6200 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 6201 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6203 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 6204 | METH_VARARGS | METH_CLASS, |
Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 6205 | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6207 | {"strptime", (PyCFunction)datetime_strptime, |
| 6208 | METH_VARARGS | METH_CLASS, |
| 6209 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 6210 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 6211 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6212 | {"combine", (PyCFunction)(void(*)(void))datetime_combine, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6213 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 6214 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6215 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 6216 | {"fromisoformat", (PyCFunction)datetime_fromisoformat, |
| 6217 | METH_O | METH_CLASS, |
| 6218 | PyDoc_STR("string -> datetime from datetime.isoformat() output")}, |
| 6219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6220 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6222 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 6223 | PyDoc_STR("Return date object with same year, month and day.")}, |
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 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 6226 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
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 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 6229 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
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 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 6232 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6234 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 6235 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6236 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6237 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 6238 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 6239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6240 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 6241 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6242 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6243 | {"isoformat", (PyCFunction)(void(*)(void))datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6244 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 6245 | "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6246 | "sep is used to separate the year from the time, and " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 6247 | "defaults to 'T'.\n" |
| 6248 | "timespec specifies what components of the time to include" |
| 6249 | " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," |
| 6250 | " 'milliseconds', and 'microseconds').\n")}, |
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 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 6253 | PyDoc_STR("Return self.tzinfo.utcoffset(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 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 6256 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6258 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 6259 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6260 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6261 | {"replace", (PyCFunction)(void(*)(void))datetime_replace, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6262 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 6263 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6264 | {"astimezone", (PyCFunction)(void(*)(void))datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6265 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 6266 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6267 | {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6268 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 6269 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 6270 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 6271 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 6272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6273 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6274 | }; |
| 6275 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 6276 | static const char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 6277 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 6278 | \n\ |
| 6279 | 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] | 6280 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6281 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6282 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6283 | datetime_add, /* nb_add */ |
| 6284 | datetime_subtract, /* nb_subtract */ |
| 6285 | 0, /* nb_multiply */ |
| 6286 | 0, /* nb_remainder */ |
| 6287 | 0, /* nb_divmod */ |
| 6288 | 0, /* nb_power */ |
| 6289 | 0, /* nb_negative */ |
| 6290 | 0, /* nb_positive */ |
| 6291 | 0, /* nb_absolute */ |
| 6292 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6293 | }; |
| 6294 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 6295 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6296 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6297 | "datetime.datetime", /* tp_name */ |
| 6298 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 6299 | 0, /* tp_itemsize */ |
| 6300 | (destructor)datetime_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6301 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6302 | 0, /* tp_getattr */ |
| 6303 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6304 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6305 | (reprfunc)datetime_repr, /* tp_repr */ |
| 6306 | &datetime_as_number, /* tp_as_number */ |
| 6307 | 0, /* tp_as_sequence */ |
| 6308 | 0, /* tp_as_mapping */ |
| 6309 | (hashfunc)datetime_hash, /* tp_hash */ |
| 6310 | 0, /* tp_call */ |
| 6311 | (reprfunc)datetime_str, /* tp_str */ |
| 6312 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6313 | 0, /* tp_setattro */ |
| 6314 | 0, /* tp_as_buffer */ |
| 6315 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6316 | datetime_doc, /* tp_doc */ |
| 6317 | 0, /* tp_traverse */ |
| 6318 | 0, /* tp_clear */ |
| 6319 | datetime_richcompare, /* tp_richcompare */ |
| 6320 | 0, /* tp_weaklistoffset */ |
| 6321 | 0, /* tp_iter */ |
| 6322 | 0, /* tp_iternext */ |
| 6323 | datetime_methods, /* tp_methods */ |
| 6324 | 0, /* tp_members */ |
| 6325 | datetime_getset, /* tp_getset */ |
| 6326 | &PyDateTime_DateType, /* tp_base */ |
| 6327 | 0, /* tp_dict */ |
| 6328 | 0, /* tp_descr_get */ |
| 6329 | 0, /* tp_descr_set */ |
| 6330 | 0, /* tp_dictoffset */ |
| 6331 | 0, /* tp_init */ |
| 6332 | datetime_alloc, /* tp_alloc */ |
| 6333 | datetime_new, /* tp_new */ |
| 6334 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6335 | }; |
| 6336 | |
| 6337 | /* --------------------------------------------------------------------------- |
| 6338 | * Module methods and initialization. |
| 6339 | */ |
| 6340 | |
| 6341 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6342 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6343 | }; |
| 6344 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6345 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 6346 | * datetime.h. |
| 6347 | */ |
| 6348 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6349 | &PyDateTime_DateType, |
| 6350 | &PyDateTime_DateTimeType, |
| 6351 | &PyDateTime_TimeType, |
| 6352 | &PyDateTime_DeltaType, |
| 6353 | &PyDateTime_TZInfoType, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6354 | NULL, // PyDatetime_TimeZone_UTC not initialized yet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6355 | new_date_ex, |
| 6356 | new_datetime_ex, |
| 6357 | new_time_ex, |
| 6358 | new_delta_ex, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6359 | new_timezone, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6360 | datetime_fromtimestamp, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 6361 | datetime_date_fromtimestamp_capi, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6362 | new_datetime_ex2, |
| 6363 | new_time_ex2 |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6364 | }; |
| 6365 | |
| 6366 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6367 | |
| 6368 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6370 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6371 | "Fast implementation of the datetime type.", |
| 6372 | -1, |
| 6373 | module_methods, |
| 6374 | NULL, |
| 6375 | NULL, |
| 6376 | NULL, |
| 6377 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6378 | }; |
| 6379 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6380 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6381 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6383 | PyObject *m; /* a module object */ |
| 6384 | PyObject *d; /* its dict */ |
| 6385 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6386 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6388 | m = PyModule_Create(&datetimemodule); |
| 6389 | if (m == NULL) |
| 6390 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6392 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 6393 | return NULL; |
| 6394 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 6395 | return NULL; |
| 6396 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 6397 | return NULL; |
| 6398 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 6399 | return NULL; |
| 6400 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 6401 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6402 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 6403 | return NULL; |
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 | /* timedelta values */ |
| 6406 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6408 | x = new_delta(0, 0, 1, 0); |
| 6409 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6410 | return NULL; |
| 6411 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6413 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 6414 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6415 | return NULL; |
| 6416 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6418 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 6419 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6420 | return NULL; |
| 6421 | Py_DECREF(x); |
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 | /* date values */ |
| 6424 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6426 | x = new_date(1, 1, 1); |
| 6427 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6428 | return NULL; |
| 6429 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6431 | x = new_date(MAXYEAR, 12, 31); |
| 6432 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6433 | return NULL; |
| 6434 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6436 | x = new_delta(1, 0, 0, 0); |
| 6437 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6438 | return NULL; |
| 6439 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6441 | /* time values */ |
| 6442 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6443 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6444 | x = new_time(0, 0, 0, 0, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6445 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6446 | return NULL; |
| 6447 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6448 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6449 | x = new_time(23, 59, 59, 999999, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6450 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6451 | return NULL; |
| 6452 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6454 | x = new_delta(0, 0, 1, 0); |
| 6455 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6456 | return NULL; |
| 6457 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6459 | /* datetime values */ |
| 6460 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6461 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6462 | 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] | 6463 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6464 | return NULL; |
| 6465 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6466 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6467 | 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] | 6468 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6469 | return NULL; |
| 6470 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6472 | x = new_delta(0, 0, 1, 0); |
| 6473 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6474 | return NULL; |
| 6475 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6476 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6477 | /* timezone values */ |
| 6478 | d = PyDateTime_TimeZoneType.tp_dict; |
| 6479 | |
| 6480 | delta = new_delta(0, 0, 0, 0); |
| 6481 | if (delta == NULL) |
| 6482 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6483 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6484 | Py_DECREF(delta); |
| 6485 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 6486 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 6487 | PyDateTime_TimeZone_UTC = x; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6488 | CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6489 | |
| 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 | --------------------------------------------------------------------------- */ |