Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1 | /* C implementation for the date/time type documented at |
| 2 | * http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage |
| 3 | */ |
| 4 | |
| 5 | #include "Python.h" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6 | #include "structmember.h" |
| 7 | |
| 8 | #include <time.h> |
| 9 | |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 10 | #ifdef MS_WINDOWS |
| 11 | # include <winsock2.h> /* struct timeval */ |
| 12 | #endif |
| 13 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 14 | /* Differentiate between building the core module and building extension |
| 15 | * modules. |
| 16 | */ |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 17 | #ifndef Py_BUILD_CORE |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 18 | #define Py_BUILD_CORE |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 19 | #endif |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 20 | #include "datetime.h" |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 21 | #undef Py_BUILD_CORE |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 22 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 23 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 24 | module datetime |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 25 | class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 26 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 27 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=78142cb64b9e98bc]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 28 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 29 | #include "clinic/_datetimemodule.c.h" |
| 30 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 31 | /* We require that C int be at least 32 bits, and use int virtually |
| 32 | * everywhere. In just a few cases we use a temp long, where a Python |
| 33 | * API returns a C long. In such cases, we have to ensure that the |
| 34 | * final result fits in a C int (this can be an issue on 64-bit boxes). |
| 35 | */ |
| 36 | #if SIZEOF_INT < 4 |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 37 | # error "_datetime.c requires that C int have at least 32 bits" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | #define MINYEAR 1 |
| 41 | #define MAXYEAR 9999 |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 42 | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 43 | |
| 44 | /* Nine decimal digits is easy to communicate, and leaves enough room |
| 45 | * so that two delta days can be added w/o fear of overflowing a signed |
| 46 | * 32-bit int, and with plenty of room left over to absorb any possible |
| 47 | * carries from adding seconds. |
| 48 | */ |
| 49 | #define MAX_DELTA_DAYS 999999999 |
| 50 | |
| 51 | /* Rename the long macros in datetime.h to more reasonable short names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | #define GET_YEAR PyDateTime_GET_YEAR |
| 53 | #define GET_MONTH PyDateTime_GET_MONTH |
| 54 | #define GET_DAY PyDateTime_GET_DAY |
| 55 | #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
| 56 | #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
| 57 | #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
| 58 | #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 59 | #define DATE_GET_FOLD PyDateTime_DATE_GET_FOLD |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 60 | |
| 61 | /* Date accessors for date and datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
| 63 | ((o)->data[1] = ((v) & 0x00ff))) |
| 64 | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
| 65 | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 66 | |
| 67 | /* Date/Time accessors for datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
| 69 | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
| 70 | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
| 71 | #define DATE_SET_MICROSECOND(o, v) \ |
| 72 | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
| 73 | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
| 74 | ((o)->data[9] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 75 | #define DATE_SET_FOLD(o, v) (PyDateTime_DATE_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 76 | |
| 77 | /* Time accessors for time. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
| 79 | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
| 80 | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
| 81 | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 82 | #define TIME_GET_FOLD PyDateTime_TIME_GET_FOLD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
| 84 | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
| 85 | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
| 86 | #define TIME_SET_MICROSECOND(o, v) \ |
| 87 | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
| 88 | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
| 89 | ((o)->data[5] = ((v) & 0x0000ff))) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 90 | #define TIME_SET_FOLD(o, v) (PyDateTime_TIME_GET_FOLD(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 91 | |
| 92 | /* Delta accessors for timedelta. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
| 94 | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
| 95 | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
| 98 | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 99 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
| 100 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 101 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
| 102 | * p->hastzinfo. |
| 103 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 104 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
| 105 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
| 106 | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
| 107 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
| 108 | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 109 | /* M is a char or int claiming to be a valid month. The macro is equivalent |
| 110 | * to the two-sided Python test |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | * 1 <= M <= 12 |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 112 | */ |
| 113 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
| 114 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 115 | /* Forward declarations. */ |
| 116 | static PyTypeObject PyDateTime_DateType; |
| 117 | static PyTypeObject PyDateTime_DateTimeType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 118 | static PyTypeObject PyDateTime_DeltaType; |
| 119 | static PyTypeObject PyDateTime_TimeType; |
| 120 | static PyTypeObject PyDateTime_TZInfoType; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 121 | static PyTypeObject PyDateTime_TimeZoneType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 122 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 123 | static int check_tzinfo_subclass(PyObject *p); |
| 124 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 125 | _Py_IDENTIFIER(as_integer_ratio); |
| 126 | _Py_IDENTIFIER(fromutc); |
| 127 | _Py_IDENTIFIER(isoformat); |
| 128 | _Py_IDENTIFIER(strftime); |
| 129 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 130 | /* --------------------------------------------------------------------------- |
| 131 | * Math utilities. |
| 132 | */ |
| 133 | |
| 134 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 135 | * iff k^i has sign bit set and k^j has sign bit set, |
| 136 | * iff (k^i)&(k^j) has sign bit set. |
| 137 | */ |
| 138 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 140 | |
| 141 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 142 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 143 | * the real point of this. C will probably truncate instead (C99 |
| 144 | * requires truncation; C89 left it implementation-defined). |
| 145 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 146 | * for all the uses made of it. This simplifies the code and makes |
| 147 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 148 | * overflow case). |
| 149 | */ |
| 150 | static int |
| 151 | divmod(int x, int y, int *r) |
| 152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | assert(y > 0); |
| 156 | quo = x / y; |
| 157 | *r = x - quo * y; |
| 158 | if (*r < 0) { |
| 159 | --quo; |
| 160 | *r += y; |
| 161 | } |
| 162 | assert(0 <= *r && *r < y); |
| 163 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 166 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 167 | * are rounded to even. |
| 168 | */ |
| 169 | static PyObject * |
| 170 | divide_nearest(PyObject *m, PyObject *n) |
| 171 | { |
| 172 | PyObject *result; |
| 173 | PyObject *temp; |
| 174 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 175 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 176 | if (temp == NULL) |
| 177 | return NULL; |
| 178 | result = PyTuple_GET_ITEM(temp, 0); |
| 179 | Py_INCREF(result); |
| 180 | Py_DECREF(temp); |
| 181 | |
| 182 | return result; |
| 183 | } |
| 184 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 185 | /* --------------------------------------------------------------------------- |
| 186 | * General calendrical helper functions |
| 187 | */ |
| 188 | |
| 189 | /* For each month ordinal in 1..12, the number of days in that month, |
| 190 | * and the number of days before that month in the same year. These |
| 191 | * are correct for non-leap years only. |
| 192 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 193 | static const int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | 0, /* unused; this vector uses 1-based indexing */ |
| 195 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 198 | static const int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | 0, /* unused; this vector uses 1-based indexing */ |
| 200 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | /* year -> 1 if leap year, else 0. */ |
| 204 | static int |
| 205 | is_leap(int year) |
| 206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | /* Cast year to unsigned. The result is the same either way, but |
| 208 | * C can generate faster code for unsigned mod than for signed |
| 209 | * mod (especially for % 4 -- a good compiler should just grab |
| 210 | * the last 2 bits when the LHS is unsigned). |
| 211 | */ |
| 212 | const unsigned int ayear = (unsigned int)year; |
| 213 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* year, month -> number of days in that month in that year */ |
| 217 | static int |
| 218 | days_in_month(int year, int month) |
| 219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | assert(month >= 1); |
| 221 | assert(month <= 12); |
| 222 | if (month == 2 && is_leap(year)) |
| 223 | return 29; |
| 224 | else |
| 225 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 228 | /* year, month -> number of days in year preceding first day of month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 229 | static int |
| 230 | days_before_month(int year, int month) |
| 231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | assert(month >= 1); |
| 235 | assert(month <= 12); |
| 236 | days = _days_before_month[month]; |
| 237 | if (month > 2 && is_leap(year)) |
| 238 | ++days; |
| 239 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* year -> number of days before January 1st of year. Remember that we |
| 243 | * start with year 1, so days_before_year(1) == 0. |
| 244 | */ |
| 245 | static int |
| 246 | days_before_year(int year) |
| 247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | int y = year - 1; |
| 249 | /* This is incorrect if year <= 0; we really want the floor |
| 250 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 251 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 253 | assert (year >= 1); |
| 254 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 258 | * the correct values is asserted in the module init function. |
| 259 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 261 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 262 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 263 | |
| 264 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 265 | static void |
| 266 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 271 | * leap years repeats exactly every 400 years. The basic strategy is |
| 272 | * to find the closest 400-year boundary at or before ordinal, then |
| 273 | * work with the offset from that boundary to ordinal. Life is much |
| 274 | * clearer if we subtract 1 from ordinal first -- then the values |
| 275 | * of ordinal at 400-year boundaries are exactly those divisible |
| 276 | * by DI400Y: |
| 277 | * |
| 278 | * D M Y n n-1 |
| 279 | * -- --- ---- ---------- ---------------- |
| 280 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 281 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 282 | * ... |
| 283 | * 30 Dec 000 -1 -2 |
| 284 | * 31 Dec 000 0 -1 |
| 285 | * 1 Jan 001 1 0 400-year boundary |
| 286 | * 2 Jan 001 2 1 |
| 287 | * 3 Jan 001 3 2 |
| 288 | * ... |
| 289 | * 31 Dec 400 DI400Y DI400Y -1 |
| 290 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 291 | */ |
| 292 | assert(ordinal >= 1); |
| 293 | --ordinal; |
| 294 | n400 = ordinal / DI400Y; |
| 295 | n = ordinal % DI400Y; |
| 296 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 299 | * year, to the desired date. Now compute how many 100-year cycles |
| 300 | * precede n. |
| 301 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 302 | * 100-year cycles precede the desired day, which implies the |
| 303 | * desired day is December 31 at the end of a 400-year cycle. |
| 304 | */ |
| 305 | n100 = n / DI100Y; |
| 306 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | /* Now compute how many 4-year cycles precede it. */ |
| 309 | n4 = n / DI4Y; |
| 310 | n = n % DI4Y; |
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 | /* And now how many single years. Again n1 can be 4, and again |
| 313 | * meaning that the desired day is December 31 at the end of the |
| 314 | * 4-year cycle. |
| 315 | */ |
| 316 | n1 = n / 365; |
| 317 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | *year += n100 * 100 + n4 * 4 + n1; |
| 320 | if (n1 == 4 || n100 == 4) { |
| 321 | assert(n == 0); |
| 322 | *year -= 1; |
| 323 | *month = 12; |
| 324 | *day = 31; |
| 325 | return; |
| 326 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | /* Now the year is correct, and n is the offset from January 1. We |
| 329 | * find the month via an estimate that's either exact or one too |
| 330 | * large. |
| 331 | */ |
| 332 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 333 | assert(leapyear == is_leap(*year)); |
| 334 | *month = (n + 50) >> 5; |
| 335 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 336 | if (preceding > n) { |
| 337 | /* estimate is too large */ |
| 338 | *month -= 1; |
| 339 | preceding -= days_in_month(*year, *month); |
| 340 | } |
| 341 | n -= preceding; |
| 342 | assert(0 <= n); |
| 343 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 349 | static int |
| 350 | ymd_to_ord(int year, int month, int day) |
| 351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 356 | static int |
| 357 | weekday(int year, int month, int day) |
| 358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 363 | * first calendar week containing a Thursday. |
| 364 | */ |
| 365 | static int |
| 366 | iso_week1_monday(int year) |
| 367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 369 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 370 | int first_weekday = (first_day + 6) % 7; |
| 371 | /* ordinal of closest Monday at or before 1/1 */ |
| 372 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 375 | week1_monday += 7; |
| 376 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* --------------------------------------------------------------------------- |
| 380 | * Range checkers. |
| 381 | */ |
| 382 | |
| 383 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 384 | * If not, raise OverflowError and return -1. |
| 385 | */ |
| 386 | static int |
| 387 | check_delta_day_range(int days) |
| 388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 390 | return 0; |
| 391 | PyErr_Format(PyExc_OverflowError, |
| 392 | "days=%d; must have magnitude <= %d", |
| 393 | days, MAX_DELTA_DAYS); |
| 394 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 398 | * aren't, raise ValueError and return -1. |
| 399 | */ |
| 400 | static int |
| 401 | check_date_args(int year, int month, int day) |
| 402 | { |
| 403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | if (year < MINYEAR || year > MAXYEAR) { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 405 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | return -1; |
| 407 | } |
| 408 | if (month < 1 || month > 12) { |
| 409 | PyErr_SetString(PyExc_ValueError, |
| 410 | "month must be in 1..12"); |
| 411 | return -1; |
| 412 | } |
| 413 | if (day < 1 || day > days_in_month(year, month)) { |
| 414 | PyErr_SetString(PyExc_ValueError, |
| 415 | "day is out of range for month"); |
| 416 | return -1; |
| 417 | } |
| 418 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 422 | * aren't, raise ValueError and return -1. |
| 423 | */ |
| 424 | static int |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 425 | 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] | 426 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | if (h < 0 || h > 23) { |
| 428 | PyErr_SetString(PyExc_ValueError, |
| 429 | "hour must be in 0..23"); |
| 430 | return -1; |
| 431 | } |
| 432 | if (m < 0 || m > 59) { |
| 433 | PyErr_SetString(PyExc_ValueError, |
| 434 | "minute must be in 0..59"); |
| 435 | return -1; |
| 436 | } |
| 437 | if (s < 0 || s > 59) { |
| 438 | PyErr_SetString(PyExc_ValueError, |
| 439 | "second must be in 0..59"); |
| 440 | return -1; |
| 441 | } |
| 442 | if (us < 0 || us > 999999) { |
| 443 | PyErr_SetString(PyExc_ValueError, |
| 444 | "microsecond must be in 0..999999"); |
| 445 | return -1; |
| 446 | } |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 447 | if (fold != 0 && fold != 1) { |
| 448 | PyErr_SetString(PyExc_ValueError, |
| 449 | "fold must be either 0 or 1"); |
| 450 | return -1; |
| 451 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* --------------------------------------------------------------------------- |
| 456 | * Normalization utilities. |
| 457 | */ |
| 458 | |
| 459 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 460 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 461 | * at least factor, enough of *lo is converted into "hi" units so that |
| 462 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 463 | * is impossible. |
| 464 | */ |
| 465 | static void |
| 466 | normalize_pair(int *hi, int *lo, int factor) |
| 467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | assert(factor > 0); |
| 469 | assert(lo != hi); |
| 470 | if (*lo < 0 || *lo >= factor) { |
| 471 | const int num_hi = divmod(*lo, factor, lo); |
| 472 | const int new_hi = *hi + num_hi; |
| 473 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 474 | *hi = new_hi; |
| 475 | } |
| 476 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | * 0 <= *s < 24*3600 |
| 481 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 482 | * The input values must be such that the internals don't overflow. |
| 483 | * The way this routine is used, we don't get close. |
| 484 | */ |
| 485 | static void |
| 486 | normalize_d_s_us(int *d, int *s, int *us) |
| 487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | if (*us < 0 || *us >= 1000000) { |
| 489 | normalize_pair(s, us, 1000000); |
| 490 | /* |s| can't be bigger than about |
| 491 | * |original s| + |original us|/1000000 now. |
| 492 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | } |
| 495 | if (*s < 0 || *s >= 24*3600) { |
| 496 | normalize_pair(d, s, 24*3600); |
| 497 | /* |d| can't be bigger than about |
| 498 | * |original d| + |
| 499 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 500 | */ |
| 501 | } |
| 502 | assert(0 <= *s && *s < 24*3600); |
| 503 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | * 1 <= *m <= 12 |
| 508 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 509 | * The input values must be such that the internals don't overflow. |
| 510 | * The way this routine is used, we don't get close. |
| 511 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 512 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 513 | normalize_y_m_d(int *y, int *m, int *d) |
| 514 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 516 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 517 | /* In actual use, m is always the month component extracted from a |
| 518 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | /* Now only day can be out of bounds (year may also be out of bounds |
| 524 | * for a datetime object, but we don't care about that here). |
| 525 | * If day is out of bounds, what to do is arguable, but at least the |
| 526 | * method here is principled and explainable. |
| 527 | */ |
| 528 | dim = days_in_month(*y, *m); |
| 529 | if (*d < 1 || *d > dim) { |
| 530 | /* Move day-1 days from the first of the month. First try to |
| 531 | * get off cheap if we're only one day out of range |
| 532 | * (adjustments for timezone alone can't be worse than that). |
| 533 | */ |
| 534 | if (*d == 0) { |
| 535 | --*m; |
| 536 | if (*m > 0) |
| 537 | *d = days_in_month(*y, *m); |
| 538 | else { |
| 539 | --*y; |
| 540 | *m = 12; |
| 541 | *d = 31; |
| 542 | } |
| 543 | } |
| 544 | else if (*d == dim + 1) { |
| 545 | /* move forward a day */ |
| 546 | ++*m; |
| 547 | *d = 1; |
| 548 | if (*m > 12) { |
| 549 | *m = 1; |
| 550 | ++*y; |
| 551 | } |
| 552 | } |
| 553 | else { |
| 554 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 555 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 556 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 557 | goto error; |
| 558 | } else { |
| 559 | ord_to_ymd(ordinal, y, m, d); |
| 560 | return 0; |
| 561 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | assert(*m > 0); |
| 565 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 566 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 567 | return 0; |
| 568 | error: |
| 569 | PyErr_SetString(PyExc_OverflowError, |
| 570 | "date value out of range"); |
| 571 | return -1; |
| 572 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 576 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 577 | * failure, where failure means the adjusted year is out of bounds. |
| 578 | */ |
| 579 | static int |
| 580 | normalize_date(int *year, int *month, int *day) |
| 581 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 582 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* Force all the datetime fields into range. The parameters are both |
| 586 | * inputs and outputs. Returns < 0 on error. |
| 587 | */ |
| 588 | static int |
| 589 | normalize_datetime(int *year, int *month, int *day, |
| 590 | int *hour, int *minute, int *second, |
| 591 | int *microsecond) |
| 592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | normalize_pair(second, microsecond, 1000000); |
| 594 | normalize_pair(minute, second, 60); |
| 595 | normalize_pair(hour, minute, 60); |
| 596 | normalize_pair(day, hour, 24); |
| 597 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 601 | * Basic object allocation: tp_alloc implementations. These allocate |
| 602 | * Python objects of the right size and type, and do the Python object- |
| 603 | * initialization bit. If there's not enough memory, they return NULL after |
| 604 | * setting MemoryError. All data members remain uninitialized trash. |
| 605 | * |
| 606 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 607 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 608 | * tp_basicsize for the time and datetime types is set to the size of the |
| 609 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 610 | * allocate enough space for a tzinfo member whether or not one is actually |
| 611 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 612 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 613 | * using) just happens today to effectively ignore the nitems argument |
| 614 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 615 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 616 | * be changed to force a 0 nitems argument unless the type being allocated |
| 617 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 618 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 619 | */ |
| 620 | |
| 621 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 622 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 623 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | self = (PyObject *) |
| 627 | PyObject_MALLOC(aware ? |
| 628 | sizeof(PyDateTime_Time) : |
| 629 | sizeof(_PyDateTime_BaseTime)); |
| 630 | if (self == NULL) |
| 631 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 632 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 637 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 638 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | self = (PyObject *) |
| 642 | PyObject_MALLOC(aware ? |
| 643 | sizeof(PyDateTime_DateTime) : |
| 644 | sizeof(_PyDateTime_BaseDateTime)); |
| 645 | if (self == NULL) |
| 646 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 647 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* --------------------------------------------------------------------------- |
| 652 | * Helpers for setting object fields. These work on pointers to the |
| 653 | * appropriate base class. |
| 654 | */ |
| 655 | |
| 656 | /* For date and datetime. */ |
| 657 | static void |
| 658 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 659 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | self->hashcode = -1; |
| 661 | SET_YEAR(self, y); |
| 662 | SET_MONTH(self, m); |
| 663 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* --------------------------------------------------------------------------- |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 667 | * String parsing utilities and helper functions |
| 668 | */ |
| 669 | |
| 670 | static const char* |
| 671 | parse_digits(const char* ptr, int* var, size_t num_digits) |
| 672 | { |
| 673 | for (size_t i = 0; i < num_digits; ++i) { |
| 674 | unsigned int tmp = (unsigned int)(*(ptr++) - '0'); |
| 675 | if (tmp > 9) { |
| 676 | return NULL; |
| 677 | } |
| 678 | *var *= 10; |
| 679 | *var += (signed int)tmp; |
| 680 | } |
| 681 | |
| 682 | return ptr; |
| 683 | } |
| 684 | |
| 685 | static int parse_isoformat_date(const char *dtstr, |
| 686 | int* year, int *month, int* day) { |
| 687 | /* Parse the date components of the result of date.isoformat() |
| 688 | * |
| 689 | * Return codes: |
| 690 | * 0: Success |
| 691 | * -1: Failed to parse date component |
| 692 | * -2: Failed to parse dateseparator |
| 693 | */ |
| 694 | const char *p = dtstr; |
| 695 | p = parse_digits(p, year, 4); |
| 696 | if (NULL == p) { |
| 697 | return -1; |
| 698 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 699 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 700 | if (*(p++) != '-') { |
| 701 | return -2; |
| 702 | } |
| 703 | |
| 704 | p = parse_digits(p, month, 2); |
| 705 | if (NULL == p) { |
| 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | if (*(p++) != '-') { |
| 710 | return -2; |
| 711 | } |
| 712 | |
| 713 | p = parse_digits(p, day, 2); |
| 714 | if (p == NULL) { |
| 715 | return -1; |
| 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static int |
| 722 | parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, |
| 723 | int* hour, int* minute, int *second, int *microsecond) { |
| 724 | const char *p = tstr; |
| 725 | const char *p_end = tstr_end; |
| 726 | int *vals[3] = {hour, minute, second}; |
| 727 | |
| 728 | // Parse [HH[:MM[:SS]]] |
| 729 | for (size_t i = 0; i < 3; ++i) { |
| 730 | p = parse_digits(p, vals[i], 2); |
| 731 | if (NULL == p) { |
| 732 | return -3; |
| 733 | } |
| 734 | |
| 735 | char c = *(p++); |
| 736 | if (p >= p_end) { |
| 737 | return c != '\0'; |
| 738 | } else if (c == ':') { |
| 739 | continue; |
| 740 | } else if (c == '.') { |
| 741 | break; |
| 742 | } else { |
| 743 | return -4; // Malformed time separator |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | // Parse .fff[fff] |
| 748 | size_t len_remains = p_end - p; |
| 749 | if (!(len_remains == 6 || len_remains == 3)) { |
| 750 | return -3; |
| 751 | } |
| 752 | |
| 753 | p = parse_digits(p, microsecond, len_remains); |
| 754 | if (NULL == p) { |
| 755 | return -3; |
| 756 | } |
| 757 | |
| 758 | if (len_remains == 3) { |
| 759 | *microsecond *= 1000; |
| 760 | } |
| 761 | |
| 762 | // Return 1 if it's not the end of the string |
| 763 | return *p != '\0'; |
| 764 | } |
| 765 | |
| 766 | static int |
| 767 | parse_isoformat_time(const char *dtstr, size_t dtlen, |
| 768 | int* hour, int *minute, int *second, int *microsecond, |
| 769 | int* tzoffset, int *tzmicrosecond) { |
| 770 | // Parse the time portion of a datetime.isoformat() string |
| 771 | // |
| 772 | // Return codes: |
| 773 | // 0: Success (no tzoffset) |
| 774 | // 1: Success (with tzoffset) |
| 775 | // -3: Failed to parse time component |
| 776 | // -4: Failed to parse time separator |
| 777 | // -5: Malformed timezone string |
| 778 | |
| 779 | const char *p = dtstr; |
| 780 | const char *p_end = dtstr + dtlen; |
| 781 | |
| 782 | const char *tzinfo_pos = p; |
| 783 | do { |
| 784 | if (*tzinfo_pos == '+' || *tzinfo_pos == '-') { |
| 785 | break; |
| 786 | } |
| 787 | } while(++tzinfo_pos < p_end); |
| 788 | |
| 789 | int rv = parse_hh_mm_ss_ff(dtstr, tzinfo_pos, |
| 790 | hour, minute, second, microsecond); |
| 791 | |
| 792 | if (rv < 0) { |
| 793 | return rv; |
| 794 | } else if (tzinfo_pos == p_end) { |
| 795 | // We know that there's no time zone, so if there's stuff at the |
| 796 | // end of the string it's an error. |
| 797 | if (rv == 1) { |
| 798 | return -5; |
| 799 | } else { |
| 800 | return 0; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | // Parse time zone component |
| 805 | // Valid formats are: |
| 806 | // - +HH:MM (len 6) |
| 807 | // - +HH:MM:SS (len 9) |
| 808 | // - +HH:MM:SS.ffffff (len 16) |
| 809 | size_t tzlen = p_end - tzinfo_pos; |
| 810 | if (!(tzlen == 6 || tzlen == 9 || tzlen == 16)) { |
| 811 | return -5; |
| 812 | } |
| 813 | |
| 814 | int tzsign = (*tzinfo_pos == '-')?-1:1; |
| 815 | tzinfo_pos++; |
| 816 | int tzhour = 0, tzminute = 0, tzsecond = 0; |
| 817 | rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, |
| 818 | &tzhour, &tzminute, &tzsecond, tzmicrosecond); |
| 819 | |
| 820 | *tzoffset = tzsign * ((tzhour * 3600) + (tzminute * 60) + tzsecond); |
| 821 | *tzmicrosecond *= tzsign; |
| 822 | |
| 823 | return rv?-5:1; |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 828 | * Create various objects, mostly without range checking. |
| 829 | */ |
| 830 | |
| 831 | /* Create a date instance with no range checking. */ |
| 832 | static PyObject * |
| 833 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 834 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 836 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 837 | if (check_date_args(year, month, day) < 0) { |
| 838 | return NULL; |
| 839 | } |
| 840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 842 | if (self != NULL) |
| 843 | set_date_fields(self, year, month, day); |
| 844 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 849 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 850 | // Forward declaration |
| 851 | static PyObject * new_datetime_ex(int, int, int, int, int, int, int, |
| 852 | PyObject*, PyTypeObject*); |
| 853 | |
| 854 | /* Create date instance with no range checking, or call subclass constructor */ |
| 855 | static PyObject * |
| 856 | new_date_subclass_ex(int year, int month, int day, PyObject *cls) { |
| 857 | PyObject *result; |
| 858 | // We have "fast path" constructors for two subclasses: date and datetime |
| 859 | if ((PyTypeObject *)cls == &PyDateTime_DateType) { |
| 860 | result = new_date_ex(year, month, day, (PyTypeObject *)cls); |
| 861 | } else if ((PyTypeObject *)cls == &PyDateTime_DateTimeType) { |
| 862 | result = new_datetime_ex(year, month, day, 0, 0, 0, 0, Py_None, |
| 863 | (PyTypeObject *)cls); |
| 864 | } else { |
| 865 | result = PyObject_CallFunction(cls, "iii", year, month, day); |
| 866 | } |
| 867 | |
| 868 | return result; |
| 869 | } |
| 870 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 871 | /* Create a datetime instance with no range checking. */ |
| 872 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 873 | new_datetime_ex2(int year, int month, int day, int hour, int minute, |
| 874 | int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 875 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | PyDateTime_DateTime *self; |
| 877 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 878 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 879 | if (check_date_args(year, month, day) < 0) { |
| 880 | return NULL; |
| 881 | } |
| 882 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 883 | return NULL; |
| 884 | } |
| 885 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 886 | return NULL; |
| 887 | } |
| 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 890 | if (self != NULL) { |
| 891 | self->hastzinfo = aware; |
| 892 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 893 | DATE_SET_HOUR(self, hour); |
| 894 | DATE_SET_MINUTE(self, minute); |
| 895 | DATE_SET_SECOND(self, second); |
| 896 | DATE_SET_MICROSECOND(self, usecond); |
| 897 | if (aware) { |
| 898 | Py_INCREF(tzinfo); |
| 899 | self->tzinfo = tzinfo; |
| 900 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 901 | DATE_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | } |
| 903 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 906 | static PyObject * |
| 907 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
| 908 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
| 909 | { |
| 910 | return new_datetime_ex2(year, month, day, hour, minute, second, usecond, |
| 911 | tzinfo, 0, type); |
| 912 | } |
| 913 | |
| 914 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ |
| 915 | new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 917 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 918 | static PyObject * |
| 919 | new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute, |
| 920 | int second, int usecond, PyObject *tzinfo, |
| 921 | int fold, PyObject *cls) { |
| 922 | PyObject* dt; |
| 923 | if ((PyTypeObject*)cls == &PyDateTime_DateTimeType) { |
| 924 | // Use the fast path constructor |
| 925 | dt = new_datetime(year, month, day, hour, minute, second, usecond, |
| 926 | tzinfo, fold); |
| 927 | } else { |
| 928 | // Subclass |
| 929 | dt = PyObject_CallFunction(cls, "iiiiiiiO", |
| 930 | year, |
| 931 | month, |
| 932 | day, |
| 933 | hour, |
| 934 | minute, |
| 935 | second, |
| 936 | usecond, |
| 937 | tzinfo); |
| 938 | } |
| 939 | |
| 940 | return dt; |
| 941 | } |
| 942 | |
| 943 | static PyObject * |
| 944 | new_datetime_subclass_ex(int year, int month, int day, int hour, int minute, |
| 945 | int second, int usecond, PyObject *tzinfo, |
| 946 | PyObject *cls) { |
| 947 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 948 | second, usecond, tzinfo, 0, |
| 949 | cls); |
| 950 | } |
| 951 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 952 | /* Create a time instance with no range checking. */ |
| 953 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 954 | new_time_ex2(int hour, int minute, int second, int usecond, |
| 955 | PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | PyDateTime_Time *self; |
| 958 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 959 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 960 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 961 | return NULL; |
| 962 | } |
| 963 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 964 | return NULL; |
| 965 | } |
| 966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 968 | if (self != NULL) { |
| 969 | self->hastzinfo = aware; |
| 970 | self->hashcode = -1; |
| 971 | TIME_SET_HOUR(self, hour); |
| 972 | TIME_SET_MINUTE(self, minute); |
| 973 | TIME_SET_SECOND(self, second); |
| 974 | TIME_SET_MICROSECOND(self, usecond); |
| 975 | if (aware) { |
| 976 | Py_INCREF(tzinfo); |
| 977 | self->tzinfo = tzinfo; |
| 978 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 979 | TIME_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | } |
| 981 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 984 | static PyObject * |
| 985 | new_time_ex(int hour, int minute, int second, int usecond, |
| 986 | PyObject *tzinfo, PyTypeObject *type) |
| 987 | { |
| 988 | return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); |
| 989 | } |
| 990 | |
| 991 | #define new_time(hh, mm, ss, us, tzinfo, fold) \ |
| 992 | new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 993 | |
| 994 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 995 | * true. Passing false is a speed optimization, if you know for sure |
| 996 | * that seconds and microseconds are already in their proper ranges. In any |
| 997 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 998 | * of range). |
| 999 | */ |
| 1000 | static PyObject * |
| 1001 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | if (normalize) |
| 1007 | normalize_d_s_us(&days, &seconds, µseconds); |
| 1008 | assert(0 <= seconds && seconds < 24*3600); |
| 1009 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | if (check_delta_day_range(days) < 0) |
| 1012 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 1015 | if (self != NULL) { |
| 1016 | self->hashcode = -1; |
| 1017 | SET_TD_DAYS(self, days); |
| 1018 | SET_TD_SECONDS(self, seconds); |
| 1019 | SET_TD_MICROSECONDS(self, microseconds); |
| 1020 | } |
| 1021 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | #define new_delta(d, s, us, normalize) \ |
| 1025 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1026 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1027 | |
| 1028 | typedef struct |
| 1029 | { |
| 1030 | PyObject_HEAD |
| 1031 | PyObject *offset; |
| 1032 | PyObject *name; |
| 1033 | } PyDateTime_TimeZone; |
| 1034 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1035 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1036 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 1037 | /* The interned Epoch datetime instance */ |
| 1038 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 1039 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1040 | /* Create new timezone instance checking offset range. This |
| 1041 | function does not check the name argument. Caller must assure |
| 1042 | that offset is a timedelta instance and name is either NULL |
| 1043 | or a unicode object. */ |
| 1044 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1045 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1046 | { |
| 1047 | PyDateTime_TimeZone *self; |
| 1048 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 1049 | |
| 1050 | assert(offset != NULL); |
| 1051 | assert(PyDelta_Check(offset)); |
| 1052 | assert(name == NULL || PyUnicode_Check(name)); |
| 1053 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1054 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 1055 | if (self == NULL) { |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | Py_INCREF(offset); |
| 1059 | self->offset = offset; |
| 1060 | Py_XINCREF(name); |
| 1061 | self->name = name; |
| 1062 | return (PyObject *)self; |
| 1063 | } |
| 1064 | |
| 1065 | static int delta_bool(PyDateTime_Delta *self); |
| 1066 | |
| 1067 | static PyObject * |
| 1068 | new_timezone(PyObject *offset, PyObject *name) |
| 1069 | { |
| 1070 | assert(offset != NULL); |
| 1071 | assert(PyDelta_Check(offset)); |
| 1072 | assert(name == NULL || PyUnicode_Check(name)); |
| 1073 | |
| 1074 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 1075 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1076 | return PyDateTime_TimeZone_UTC; |
| 1077 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1078 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 1079 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1080 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1081 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 1082 | " timedelta(hours=24)," |
| 1083 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1084 | return NULL; |
| 1085 | } |
| 1086 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 1087 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1090 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1091 | * tzinfo helpers. |
| 1092 | */ |
| 1093 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1094 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 1095 | * raise TypeError and return -1. |
| 1096 | */ |
| 1097 | static int |
| 1098 | check_tzinfo_subclass(PyObject *p) |
| 1099 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | if (p == Py_None || PyTZInfo_Check(p)) |
| 1101 | return 0; |
| 1102 | PyErr_Format(PyExc_TypeError, |
| 1103 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 1104 | "not type '%s'", |
| 1105 | Py_TYPE(p)->tp_name); |
| 1106 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1109 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 1110 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 1111 | * and the caller must not decref the result. |
| 1112 | */ |
| 1113 | static PyObject * |
| 1114 | get_tzinfo_member(PyObject *self) |
| 1115 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 1119 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 1120 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 1121 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1126 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 1127 | * be an instance of the tzinfo class. If the method returns None, this |
| 1128 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 1129 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 1130 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 1131 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1132 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1133 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1134 | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1135 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1136 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1139 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1141 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1142 | if (tzinfo == Py_None) |
| 1143 | Py_RETURN_NONE; |
| 1144 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 1145 | if (offset == Py_None || offset == NULL) |
| 1146 | return offset; |
| 1147 | if (PyDelta_Check(offset)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1148 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 1149 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 1150 | Py_DECREF(offset); |
| 1151 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 1152 | " strictly between -timedelta(hours=24) and" |
| 1153 | " timedelta(hours=24)."); |
| 1154 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | else { |
| 1158 | PyErr_Format(PyExc_TypeError, |
| 1159 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1160 | "timedelta, not '%.200s'", |
| 1161 | name, Py_TYPE(offset)->tp_name); |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 1162 | Py_DECREF(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1163 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | } |
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 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 1170 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 1171 | * 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] | 1172 | * 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] | 1173 | * If utcoffset() returns an out of range timedelta, |
| 1174 | * ValueError is raised and this returns -1. Else *none is |
| 1175 | * 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] | 1176 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1177 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1178 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 1179 | { |
| 1180 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1183 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 1184 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 1185 | * 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] | 1186 | * doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 1187 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 1188 | * 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] | 1189 | * the offset is returned (as timedelta, positive east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1190 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1191 | static PyObject * |
| 1192 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1193 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1194 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1197 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 1198 | * 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] | 1199 | * 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] | 1200 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 1201 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1202 | */ |
| 1203 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1204 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1207 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | assert(tzinfo != NULL); |
| 1210 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 1211 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1214 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1215 | |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1216 | result = _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_tzname, |
| 1217 | tzinfoarg, NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1218 | |
| 1219 | if (result == NULL || result == Py_None) |
| 1220 | return result; |
| 1221 | |
| 1222 | if (!PyUnicode_Check(result)) { |
| 1223 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 1224 | "return None or a string, not '%s'", |
| 1225 | Py_TYPE(result)->tp_name); |
| 1226 | Py_DECREF(result); |
| 1227 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1229 | |
| 1230 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1233 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 1234 | * stuff |
| 1235 | * ", tzinfo=" + repr(tzinfo) |
| 1236 | * before the closing ")". |
| 1237 | */ |
| 1238 | static PyObject * |
| 1239 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 1240 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | assert(PyUnicode_Check(repr)); |
| 1244 | assert(tzinfo); |
| 1245 | if (tzinfo == Py_None) |
| 1246 | return repr; |
| 1247 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1248 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1249 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | Py_DECREF(repr); |
| 1251 | if (temp == NULL) |
| 1252 | return NULL; |
| 1253 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 1254 | Py_DECREF(temp); |
| 1255 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1258 | /* repr is like "someclass(arg1, arg2)". If fold isn't 0, |
| 1259 | * stuff |
| 1260 | * ", fold=" + repr(tzinfo) |
| 1261 | * before the closing ")". |
| 1262 | */ |
| 1263 | static PyObject * |
| 1264 | append_keyword_fold(PyObject *repr, int fold) |
| 1265 | { |
| 1266 | PyObject *temp; |
| 1267 | |
| 1268 | assert(PyUnicode_Check(repr)); |
| 1269 | if (fold == 0) |
| 1270 | return repr; |
| 1271 | /* Get rid of the trailing ')'. */ |
| 1272 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1273 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
| 1274 | Py_DECREF(repr); |
| 1275 | if (temp == NULL) |
| 1276 | return NULL; |
| 1277 | repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); |
| 1278 | Py_DECREF(temp); |
| 1279 | return repr; |
| 1280 | } |
| 1281 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 1282 | static inline PyObject * |
| 1283 | tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds) { |
| 1284 | PyObject *tzinfo; |
| 1285 | if (rv == 1) { |
| 1286 | // Create a timezone from offset in seconds (0 returns UTC) |
| 1287 | if (tzoffset == 0) { |
| 1288 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 1289 | return PyDateTime_TimeZone_UTC; |
| 1290 | } |
| 1291 | |
| 1292 | PyObject *delta = new_delta(0, tzoffset, tz_useconds, 1); |
| 1293 | tzinfo = new_timezone(delta, NULL); |
| 1294 | Py_XDECREF(delta); |
| 1295 | } else { |
| 1296 | tzinfo = Py_None; |
| 1297 | Py_INCREF(Py_None); |
| 1298 | } |
| 1299 | |
| 1300 | return tzinfo; |
| 1301 | } |
| 1302 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1303 | /* --------------------------------------------------------------------------- |
| 1304 | * String format helpers. |
| 1305 | */ |
| 1306 | |
| 1307 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1308 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1309 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1310 | static const char * const DayNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1312 | }; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1313 | static const char * const MonthNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1315 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1316 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1321 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1322 | GET_DAY(date), hours, minutes, seconds, |
| 1323 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1326 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1327 | |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1328 | /* Add formatted UTC offset string to buf. buf has no more than |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1329 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1330 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1331 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1332 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1333 | * string of the form |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1334 | * sign HH sep MM [sep SS [. UUUUUU]] |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1335 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1336 | * bogus, an appropriate exception is set and -1 is returned. |
| 1337 | */ |
| 1338 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1339 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1341 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1342 | PyObject *offset; |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1343 | int hours, minutes, seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1347 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1348 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1349 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1351 | if (offset == Py_None) { |
| 1352 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | *buf = '\0'; |
| 1354 | return 0; |
| 1355 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1356 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1357 | if (GET_TD_DAYS(offset) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | sign = '-'; |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 1359 | Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1360 | if (offset == NULL) |
| 1361 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1363 | else { |
| 1364 | sign = '+'; |
| 1365 | } |
| 1366 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1367 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1368 | seconds = GET_TD_SECONDS(offset); |
| 1369 | Py_DECREF(offset); |
| 1370 | minutes = divmod(seconds, 60, &seconds); |
| 1371 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1372 | if (microseconds) { |
| 1373 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d.%06d", sign, |
| 1374 | hours, sep, minutes, sep, seconds, microseconds); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | if (seconds) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1378 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, |
| 1379 | sep, minutes, sep, seconds); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1386 | static PyObject * |
| 1387 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | PyObject *temp; |
| 1390 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1391 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1392 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | if (Zreplacement == NULL) |
| 1395 | return NULL; |
| 1396 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1397 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | assert(tzinfoarg != NULL); |
| 1400 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1401 | if (temp == NULL) |
| 1402 | goto Error; |
| 1403 | if (temp == Py_None) { |
| 1404 | Py_DECREF(temp); |
| 1405 | return Zreplacement; |
| 1406 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | assert(PyUnicode_Check(temp)); |
| 1409 | /* Since the tzname is getting stuffed into the |
| 1410 | * format, we have to double any % signs so that |
| 1411 | * strftime doesn't treat them as format codes. |
| 1412 | */ |
| 1413 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1414 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | Py_DECREF(temp); |
| 1416 | if (Zreplacement == NULL) |
| 1417 | return NULL; |
| 1418 | if (!PyUnicode_Check(Zreplacement)) { |
| 1419 | PyErr_SetString(PyExc_TypeError, |
| 1420 | "tzname.replace() did not return a string"); |
| 1421 | goto Error; |
| 1422 | } |
| 1423 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1424 | |
| 1425 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | Py_DECREF(Zreplacement); |
| 1427 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1430 | static PyObject * |
| 1431 | make_freplacement(PyObject *object) |
| 1432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | char freplacement[64]; |
| 1434 | if (PyTime_Check(object)) |
| 1435 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1436 | else if (PyDateTime_Check(object)) |
| 1437 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1438 | else |
| 1439 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1444 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1445 | * 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] | 1446 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1447 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1448 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1449 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1450 | */ |
| 1451 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1452 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1458 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1459 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | const char *pin; /* pointer to next char in input format */ |
| 1462 | Py_ssize_t flen; /* length of input format */ |
| 1463 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1466 | char *pnew; /* pointer to available byte in output format */ |
| 1467 | size_t totalnew; /* number bytes total in output format buffer, |
| 1468 | exclusive of trailing \0 */ |
| 1469 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1472 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | assert(object && format && timetuple); |
| 1475 | assert(PyUnicode_Check(format)); |
| 1476 | /* Convert the input format to a C string and size */ |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1477 | pin = PyUnicode_AsUTF8AndSize(format, &flen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | if (!pin) |
| 1479 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1482 | * a new format. Since computing the replacements for those codes |
| 1483 | * is expensive, don't unless they're actually used. |
| 1484 | */ |
| 1485 | if (flen > INT_MAX - 1) { |
| 1486 | PyErr_NoMemory(); |
| 1487 | goto Done; |
| 1488 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1491 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1492 | if (newfmt == NULL) goto Done; |
| 1493 | pnew = PyBytes_AsString(newfmt); |
| 1494 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | while ((ch = *pin++) != '\0') { |
| 1497 | if (ch != '%') { |
| 1498 | ptoappend = pin - 1; |
| 1499 | ntoappend = 1; |
| 1500 | } |
| 1501 | else if ((ch = *pin++) == '\0') { |
| 1502 | /* There's a lone trailing %; doesn't make sense. */ |
| 1503 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1504 | "ends with raw %"); |
| 1505 | goto Done; |
| 1506 | } |
| 1507 | /* A % has been seen and ch is the character after it. */ |
| 1508 | else if (ch == 'z') { |
| 1509 | if (zreplacement == NULL) { |
| 1510 | /* format utcoffset */ |
| 1511 | char buf[100]; |
| 1512 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1513 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1514 | if (zreplacement == NULL) goto Done; |
| 1515 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1516 | assert(tzinfoarg != NULL); |
| 1517 | if (format_utcoffset(buf, |
| 1518 | sizeof(buf), |
| 1519 | "", |
| 1520 | tzinfo, |
| 1521 | tzinfoarg) < 0) |
| 1522 | goto Done; |
| 1523 | Py_DECREF(zreplacement); |
| 1524 | zreplacement = |
| 1525 | PyBytes_FromStringAndSize(buf, |
| 1526 | strlen(buf)); |
| 1527 | if (zreplacement == NULL) |
| 1528 | goto Done; |
| 1529 | } |
| 1530 | } |
| 1531 | assert(zreplacement != NULL); |
| 1532 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1533 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1534 | } |
| 1535 | else if (ch == 'Z') { |
| 1536 | /* format tzname */ |
| 1537 | if (Zreplacement == NULL) { |
| 1538 | Zreplacement = make_Zreplacement(object, |
| 1539 | tzinfoarg); |
| 1540 | if (Zreplacement == NULL) |
| 1541 | goto Done; |
| 1542 | } |
| 1543 | assert(Zreplacement != NULL); |
| 1544 | assert(PyUnicode_Check(Zreplacement)); |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1545 | ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1547 | if (ptoappend == NULL) |
| 1548 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | } |
| 1550 | else if (ch == 'f') { |
| 1551 | /* format microseconds */ |
| 1552 | if (freplacement == NULL) { |
| 1553 | freplacement = make_freplacement(object); |
| 1554 | if (freplacement == NULL) |
| 1555 | goto Done; |
| 1556 | } |
| 1557 | assert(freplacement != NULL); |
| 1558 | assert(PyBytes_Check(freplacement)); |
| 1559 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1560 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1561 | } |
| 1562 | else { |
| 1563 | /* percent followed by neither z nor Z */ |
| 1564 | ptoappend = pin - 2; |
| 1565 | ntoappend = 2; |
| 1566 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | /* Append the ntoappend chars starting at ptoappend to |
| 1569 | * the new format. |
| 1570 | */ |
| 1571 | if (ntoappend == 0) |
| 1572 | continue; |
| 1573 | assert(ptoappend != NULL); |
| 1574 | assert(ntoappend > 0); |
| 1575 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1576 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | PyErr_NoMemory(); |
| 1578 | goto Done; |
| 1579 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1580 | totalnew <<= 1; |
| 1581 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1584 | } |
| 1585 | memcpy(pnew, ptoappend, ntoappend); |
| 1586 | pnew += ntoappend; |
| 1587 | usednew += ntoappend; |
| 1588 | assert(usednew <= totalnew); |
| 1589 | } /* end while() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1592 | goto Done; |
| 1593 | { |
| 1594 | PyObject *format; |
| 1595 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | if (time == NULL) |
| 1598 | goto Done; |
| 1599 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1600 | if (format != NULL) { |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1601 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime, |
| 1602 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | Py_DECREF(format); |
| 1604 | } |
| 1605 | Py_DECREF(time); |
| 1606 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1607 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | Py_XDECREF(freplacement); |
| 1609 | Py_XDECREF(zreplacement); |
| 1610 | Py_XDECREF(Zreplacement); |
| 1611 | Py_XDECREF(newfmt); |
| 1612 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1615 | /* --------------------------------------------------------------------------- |
| 1616 | * Wrap functions from the time module. These aren't directly available |
| 1617 | * from C. Perhaps they should be. |
| 1618 | */ |
| 1619 | |
| 1620 | /* Call time.time() and return its result (a Python float). */ |
| 1621 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1622 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1623 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | PyObject *result = NULL; |
| 1625 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1628 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1629 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 1630 | result = _PyObject_CallMethodId(time, &PyId_time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | Py_DECREF(time); |
| 1632 | } |
| 1633 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1637 | * computed from the y,m,d args. |
| 1638 | */ |
| 1639 | static PyObject * |
| 1640 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1641 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | PyObject *time; |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1643 | PyObject *result; |
| 1644 | _Py_IDENTIFIER(struct_time); |
| 1645 | PyObject *args; |
| 1646 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | time = PyImport_ImportModuleNoBlock("time"); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1649 | if (time == NULL) { |
| 1650 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | } |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1652 | |
| 1653 | args = Py_BuildValue("iiiiiiiii", |
| 1654 | y, m, d, |
| 1655 | hh, mm, ss, |
| 1656 | weekday(y, m, d), |
| 1657 | days_before_month(y, m) + d, |
| 1658 | dstflag); |
| 1659 | if (args == NULL) { |
| 1660 | Py_DECREF(time); |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | |
| 1664 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_struct_time, |
| 1665 | args, NULL); |
| 1666 | Py_DECREF(time); |
Victor Stinner | ddc120f | 2016-12-09 15:35:40 +0100 | [diff] [blame] | 1667 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | /* --------------------------------------------------------------------------- |
| 1672 | * Miscellaneous helpers. |
| 1673 | */ |
| 1674 | |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1675 | /* For various reasons, we need to use tp_richcompare instead of tp_reserved. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1676 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1677 | * This little helper turns that into a bool result for rich comparisons. |
| 1678 | */ |
| 1679 | static PyObject * |
| 1680 | diff_to_bool(int diff, int op) |
| 1681 | { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1682 | Py_RETURN_RICHCOMPARE(diff, 0, op); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1685 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1686 | static PyObject * |
| 1687 | cmperror(PyObject *a, PyObject *b) |
| 1688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | PyErr_Format(PyExc_TypeError, |
| 1690 | "can't compare %s to %s", |
| 1691 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1692 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1695 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1696 | * Cached Python objects; these are set by the module init function. |
| 1697 | */ |
| 1698 | |
| 1699 | /* Conversion factors. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1701 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1702 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1703 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1704 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1705 | 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] | 1706 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1707 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1708 | /* --------------------------------------------------------------------------- |
| 1709 | * Class implementations. |
| 1710 | */ |
| 1711 | |
| 1712 | /* |
| 1713 | * PyDateTime_Delta implementation. |
| 1714 | */ |
| 1715 | |
| 1716 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1718 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1719 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1720 | * due to ubiquitous overflow possibilities. |
| 1721 | */ |
| 1722 | static PyObject * |
| 1723 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | PyObject *x1 = NULL; |
| 1726 | PyObject *x2 = NULL; |
| 1727 | PyObject *x3 = NULL; |
| 1728 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1731 | if (x1 == NULL) |
| 1732 | goto Done; |
| 1733 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1734 | if (x2 == NULL) |
| 1735 | goto Done; |
| 1736 | Py_DECREF(x1); |
| 1737 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | /* x2 has days in seconds */ |
| 1740 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1741 | if (x1 == NULL) |
| 1742 | goto Done; |
| 1743 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1744 | if (x3 == NULL) |
| 1745 | goto Done; |
| 1746 | Py_DECREF(x1); |
| 1747 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1748 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | /* x3 has days+seconds in seconds */ |
| 1751 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1752 | if (x1 == NULL) |
| 1753 | goto Done; |
| 1754 | Py_DECREF(x3); |
| 1755 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | /* x1 has days+seconds in us */ |
| 1758 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1759 | if (x2 == NULL) |
| 1760 | goto Done; |
| 1761 | result = PyNumber_Add(x1, x2); |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 1762 | assert(result == NULL || PyLong_CheckExact(result)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1763 | |
| 1764 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | Py_XDECREF(x1); |
| 1766 | Py_XDECREF(x2); |
| 1767 | Py_XDECREF(x3); |
| 1768 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1771 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1772 | */ |
| 1773 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1774 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1775 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | int us; |
| 1777 | int s; |
| 1778 | int d; |
| 1779 | long temp; |
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 | PyObject *tuple = NULL; |
| 1782 | PyObject *num = NULL; |
| 1783 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1784 | |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 1785 | assert(PyLong_CheckExact(pyus)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1787 | if (tuple == NULL) |
| 1788 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1791 | if (num == NULL) |
| 1792 | goto Done; |
| 1793 | temp = PyLong_AsLong(num); |
| 1794 | num = NULL; |
| 1795 | if (temp == -1 && PyErr_Occurred()) |
| 1796 | goto Done; |
| 1797 | assert(0 <= temp && temp < 1000000); |
| 1798 | us = (int)temp; |
| 1799 | if (us < 0) { |
| 1800 | /* The divisor was positive, so this must be an error. */ |
| 1801 | assert(PyErr_Occurred()); |
| 1802 | goto Done; |
| 1803 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1806 | if (num == NULL) |
| 1807 | goto Done; |
| 1808 | Py_INCREF(num); |
| 1809 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1812 | if (tuple == NULL) |
| 1813 | goto Done; |
| 1814 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1817 | if (num == NULL) |
| 1818 | goto Done; |
| 1819 | temp = PyLong_AsLong(num); |
| 1820 | num = NULL; |
| 1821 | if (temp == -1 && PyErr_Occurred()) |
| 1822 | goto Done; |
| 1823 | assert(0 <= temp && temp < 24*3600); |
| 1824 | s = (int)temp; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | if (s < 0) { |
| 1827 | /* The divisor was positive, so this must be an error. */ |
| 1828 | assert(PyErr_Occurred()); |
| 1829 | goto Done; |
| 1830 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1833 | if (num == NULL) |
| 1834 | goto Done; |
| 1835 | Py_INCREF(num); |
| 1836 | temp = PyLong_AsLong(num); |
| 1837 | if (temp == -1 && PyErr_Occurred()) |
| 1838 | goto Done; |
| 1839 | d = (int)temp; |
| 1840 | if ((long)d != temp) { |
| 1841 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1842 | "large to fit in a C int"); |
| 1843 | goto Done; |
| 1844 | } |
| 1845 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1846 | |
| 1847 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | Py_XDECREF(tuple); |
| 1849 | Py_XDECREF(num); |
| 1850 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | #define microseconds_to_delta(pymicros) \ |
| 1854 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1855 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1856 | static PyObject * |
| 1857 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | PyObject *pyus_in; |
| 1860 | PyObject *pyus_out; |
| 1861 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | pyus_in = delta_to_microseconds(delta); |
| 1864 | if (pyus_in == NULL) |
| 1865 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1868 | Py_DECREF(pyus_in); |
| 1869 | if (pyus_out == NULL) |
| 1870 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | result = microseconds_to_delta(pyus_out); |
| 1873 | Py_DECREF(pyus_out); |
| 1874 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | static PyObject * |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1878 | get_float_as_integer_ratio(PyObject *floatobj) |
| 1879 | { |
| 1880 | PyObject *ratio; |
| 1881 | |
| 1882 | assert(floatobj && PyFloat_Check(floatobj)); |
| 1883 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
| 1884 | if (ratio == NULL) { |
| 1885 | return NULL; |
| 1886 | } |
| 1887 | if (!PyTuple_Check(ratio)) { |
| 1888 | PyErr_Format(PyExc_TypeError, |
| 1889 | "unexpected return type from as_integer_ratio(): " |
| 1890 | "expected tuple, got '%.200s'", |
| 1891 | Py_TYPE(ratio)->tp_name); |
| 1892 | Py_DECREF(ratio); |
| 1893 | return NULL; |
| 1894 | } |
| 1895 | if (PyTuple_Size(ratio) != 2) { |
| 1896 | PyErr_SetString(PyExc_ValueError, |
| 1897 | "as_integer_ratio() must return a 2-tuple"); |
| 1898 | Py_DECREF(ratio); |
| 1899 | return NULL; |
| 1900 | } |
| 1901 | return ratio; |
| 1902 | } |
| 1903 | |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1904 | /* op is 0 for multiplication, 1 for division */ |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1905 | static PyObject * |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1906 | multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj, int op) |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1907 | { |
| 1908 | PyObject *result = NULL; |
| 1909 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1910 | PyObject *ratio = NULL; |
| 1911 | |
| 1912 | pyus_in = delta_to_microseconds(delta); |
| 1913 | if (pyus_in == NULL) |
| 1914 | return NULL; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1915 | ratio = get_float_as_integer_ratio(floatobj); |
| 1916 | if (ratio == NULL) { |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1917 | goto error; |
Oren Milman | 865e4b4 | 2017-09-19 15:58:11 +0300 | [diff] [blame] | 1918 | } |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1919 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1920 | Py_DECREF(pyus_in); |
| 1921 | pyus_in = NULL; |
| 1922 | if (temp == NULL) |
| 1923 | goto error; |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 1924 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op)); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1925 | Py_DECREF(temp); |
| 1926 | if (pyus_out == NULL) |
| 1927 | goto error; |
| 1928 | result = microseconds_to_delta(pyus_out); |
| 1929 | Py_DECREF(pyus_out); |
| 1930 | error: |
| 1931 | Py_XDECREF(pyus_in); |
| 1932 | Py_XDECREF(ratio); |
| 1933 | |
| 1934 | return result; |
| 1935 | } |
| 1936 | |
| 1937 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1938 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | PyObject *pyus_in; |
| 1941 | PyObject *pyus_out; |
| 1942 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | pyus_in = delta_to_microseconds(delta); |
| 1945 | if (pyus_in == NULL) |
| 1946 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1949 | Py_DECREF(pyus_in); |
| 1950 | if (pyus_out == NULL) |
| 1951 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | result = microseconds_to_delta(pyus_out); |
| 1954 | Py_DECREF(pyus_out); |
| 1955 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1959 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | PyObject *pyus_left; |
| 1962 | PyObject *pyus_right; |
| 1963 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | pyus_left = delta_to_microseconds(left); |
| 1966 | if (pyus_left == NULL) |
| 1967 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | pyus_right = delta_to_microseconds(right); |
| 1970 | if (pyus_right == NULL) { |
| 1971 | Py_DECREF(pyus_left); |
| 1972 | return NULL; |
| 1973 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1976 | Py_DECREF(pyus_left); |
| 1977 | Py_DECREF(pyus_right); |
| 1978 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | static PyObject * |
| 1982 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | PyObject *pyus_left; |
| 1985 | PyObject *pyus_right; |
| 1986 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | pyus_left = delta_to_microseconds(left); |
| 1989 | if (pyus_left == NULL) |
| 1990 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | pyus_right = delta_to_microseconds(right); |
| 1993 | if (pyus_right == NULL) { |
| 1994 | Py_DECREF(pyus_left); |
| 1995 | return NULL; |
| 1996 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1999 | Py_DECREF(pyus_left); |
| 2000 | Py_DECREF(pyus_right); |
| 2001 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2005 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 2006 | { |
| 2007 | PyObject *result; |
| 2008 | PyObject *pyus_in, *pyus_out; |
| 2009 | pyus_in = delta_to_microseconds(delta); |
| 2010 | if (pyus_in == NULL) |
| 2011 | return NULL; |
| 2012 | pyus_out = divide_nearest(pyus_in, i); |
| 2013 | Py_DECREF(pyus_in); |
| 2014 | if (pyus_out == NULL) |
| 2015 | return NULL; |
| 2016 | result = microseconds_to_delta(pyus_out); |
| 2017 | Py_DECREF(pyus_out); |
| 2018 | |
| 2019 | return result; |
| 2020 | } |
| 2021 | |
| 2022 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2023 | delta_add(PyObject *left, PyObject *right) |
| 2024 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2028 | /* delta + delta */ |
| 2029 | /* The C-level additions can't overflow because of the |
| 2030 | * invariant bounds. |
| 2031 | */ |
| 2032 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 2033 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 2034 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 2035 | GET_TD_MICROSECONDS(right); |
| 2036 | result = new_delta(days, seconds, microseconds, 1); |
| 2037 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | if (result == Py_NotImplemented) |
| 2040 | Py_INCREF(result); |
| 2041 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | static PyObject * |
| 2045 | delta_negative(PyDateTime_Delta *self) |
| 2046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | return new_delta(-GET_TD_DAYS(self), |
| 2048 | -GET_TD_SECONDS(self), |
| 2049 | -GET_TD_MICROSECONDS(self), |
| 2050 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | static PyObject * |
| 2054 | delta_positive(PyDateTime_Delta *self) |
| 2055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | /* Could optimize this (by returning self) if this isn't a |
| 2057 | * subclass -- but who uses unary + ? Approximately nobody. |
| 2058 | */ |
| 2059 | return new_delta(GET_TD_DAYS(self), |
| 2060 | GET_TD_SECONDS(self), |
| 2061 | GET_TD_MICROSECONDS(self), |
| 2062 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | static PyObject * |
| 2066 | delta_abs(PyDateTime_Delta *self) |
| 2067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 2071 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | if (GET_TD_DAYS(self) < 0) |
| 2074 | result = delta_negative(self); |
| 2075 | else |
| 2076 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | static PyObject * |
| 2082 | delta_subtract(PyObject *left, PyObject *right) |
| 2083 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 2087 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 2088 | /* The C-level additions can't overflow because of the |
| 2089 | * invariant bounds. |
| 2090 | */ |
| 2091 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 2092 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 2093 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 2094 | GET_TD_MICROSECONDS(right); |
| 2095 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | if (result == Py_NotImplemented) |
| 2099 | Py_INCREF(result); |
| 2100 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2103 | static int |
| 2104 | delta_cmp(PyObject *self, PyObject *other) |
| 2105 | { |
| 2106 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 2107 | if (diff == 0) { |
| 2108 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 2109 | if (diff == 0) |
| 2110 | diff = GET_TD_MICROSECONDS(self) - |
| 2111 | GET_TD_MICROSECONDS(other); |
| 2112 | } |
| 2113 | return diff; |
| 2114 | } |
| 2115 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2116 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2117 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2118 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2120 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | return diff_to_bool(diff, op); |
| 2122 | } |
| 2123 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2124 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 2129 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2130 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2131 | delta_hash(PyDateTime_Delta *self) |
| 2132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | if (self->hashcode == -1) { |
| 2134 | PyObject *temp = delta_getstate(self); |
| 2135 | if (temp != NULL) { |
| 2136 | self->hashcode = PyObject_Hash(temp); |
| 2137 | Py_DECREF(temp); |
| 2138 | } |
| 2139 | } |
| 2140 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | static PyObject * |
| 2144 | delta_multiply(PyObject *left, PyObject *right) |
| 2145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | if (PyDelta_Check(left)) { |
| 2149 | /* delta * ??? */ |
| 2150 | if (PyLong_Check(right)) |
| 2151 | result = multiply_int_timedelta(right, |
| 2152 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2153 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2154 | result = multiply_truedivide_timedelta_float( |
| 2155 | (PyDateTime_Delta *) left, right, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | } |
| 2157 | else if (PyLong_Check(left)) |
| 2158 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2159 | (PyDateTime_Delta *) right); |
| 2160 | else if (PyFloat_Check(left)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2161 | result = multiply_truedivide_timedelta_float( |
| 2162 | (PyDateTime_Delta *) right, left, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | if (result == Py_NotImplemented) |
| 2165 | Py_INCREF(result); |
| 2166 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | static PyObject * |
| 2170 | delta_divide(PyObject *left, PyObject *right) |
| 2171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | if (PyDelta_Check(left)) { |
| 2175 | /* delta * ??? */ |
| 2176 | if (PyLong_Check(right)) |
| 2177 | result = divide_timedelta_int( |
| 2178 | (PyDateTime_Delta *)left, |
| 2179 | right); |
| 2180 | else if (PyDelta_Check(right)) |
| 2181 | result = divide_timedelta_timedelta( |
| 2182 | (PyDateTime_Delta *)left, |
| 2183 | (PyDateTime_Delta *)right); |
| 2184 | } |
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 (result == Py_NotImplemented) |
| 2187 | Py_INCREF(result); |
| 2188 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2191 | static PyObject * |
| 2192 | delta_truedivide(PyObject *left, PyObject *right) |
| 2193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | if (PyDelta_Check(left)) { |
| 2197 | if (PyDelta_Check(right)) |
| 2198 | result = truedivide_timedelta_timedelta( |
| 2199 | (PyDateTime_Delta *)left, |
| 2200 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2201 | else if (PyFloat_Check(right)) |
Serhiy Storchaka | db12ef7 | 2017-10-04 20:30:09 +0300 | [diff] [blame] | 2202 | result = multiply_truedivide_timedelta_float( |
| 2203 | (PyDateTime_Delta *)left, right, 1); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 2204 | else if (PyLong_Check(right)) |
| 2205 | result = truedivide_timedelta_int( |
| 2206 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | if (result == Py_NotImplemented) |
| 2210 | Py_INCREF(result); |
| 2211 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | static PyObject * |
| 2215 | delta_remainder(PyObject *left, PyObject *right) |
| 2216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2217 | PyObject *pyus_left; |
| 2218 | PyObject *pyus_right; |
| 2219 | PyObject *pyus_remainder; |
| 2220 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2221 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2222 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2223 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2226 | if (pyus_left == NULL) |
| 2227 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2230 | if (pyus_right == NULL) { |
| 2231 | Py_DECREF(pyus_left); |
| 2232 | return NULL; |
| 2233 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 2236 | Py_DECREF(pyus_left); |
| 2237 | Py_DECREF(pyus_right); |
| 2238 | if (pyus_remainder == NULL) |
| 2239 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | remainder = microseconds_to_delta(pyus_remainder); |
| 2242 | Py_DECREF(pyus_remainder); |
| 2243 | if (remainder == NULL) |
| 2244 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | static PyObject * |
| 2250 | delta_divmod(PyObject *left, PyObject *right) |
| 2251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | PyObject *pyus_left; |
| 2253 | PyObject *pyus_right; |
| 2254 | PyObject *divmod; |
| 2255 | PyObject *delta; |
| 2256 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2257 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2258 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2259 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2262 | if (pyus_left == NULL) |
| 2263 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2266 | if (pyus_right == NULL) { |
| 2267 | Py_DECREF(pyus_left); |
| 2268 | return NULL; |
| 2269 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
| 2272 | Py_DECREF(pyus_left); |
| 2273 | Py_DECREF(pyus_right); |
| 2274 | if (divmod == NULL) |
| 2275 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | assert(PyTuple_Size(divmod) == 2); |
| 2278 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 2279 | if (delta == NULL) { |
| 2280 | Py_DECREF(divmod); |
| 2281 | return NULL; |
| 2282 | } |
| 2283 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 2284 | Py_DECREF(delta); |
| 2285 | Py_DECREF(divmod); |
| 2286 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2289 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 2290 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 2291 | * so far, and there are factor microseconds per current unit, the number |
| 2292 | * of which is given by num. num * factor is added to sofar in a |
| 2293 | * numerically careful way, and that's the result. Any fractional |
| 2294 | * microseconds left over (this can happen if num is a float type) are |
| 2295 | * added into *leftover. |
| 2296 | * Note that there are many ways this can give an error (NULL) return. |
| 2297 | */ |
| 2298 | static PyObject * |
| 2299 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2300 | double *leftover) |
| 2301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | PyObject *prod; |
| 2303 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | if (PyLong_Check(num)) { |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2308 | prod = PyNumber_Multiply(factor, num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | if (prod == NULL) |
| 2310 | return NULL; |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2311 | assert(PyLong_CheckExact(prod)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | sum = PyNumber_Add(sofar, prod); |
| 2313 | Py_DECREF(prod); |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2314 | assert(sum == NULL || PyLong_CheckExact(sum)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | return sum; |
| 2316 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | if (PyFloat_Check(num)) { |
| 2319 | double dnum; |
| 2320 | double fracpart; |
| 2321 | double intpart; |
| 2322 | PyObject *x; |
| 2323 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | /* The Plan: decompose num into an integer part and a |
| 2326 | * fractional part, num = intpart + fracpart. |
| 2327 | * Then num * factor == |
| 2328 | * intpart * factor + fracpart * factor |
| 2329 | * and the LHS can be computed exactly in long arithmetic. |
| 2330 | * The RHS is again broken into an int part and frac part. |
| 2331 | * and the frac part is added into *leftover. |
| 2332 | */ |
| 2333 | dnum = PyFloat_AsDouble(num); |
| 2334 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2335 | return NULL; |
| 2336 | fracpart = modf(dnum, &intpart); |
| 2337 | x = PyLong_FromDouble(intpart); |
| 2338 | if (x == NULL) |
| 2339 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | prod = PyNumber_Multiply(x, factor); |
| 2342 | Py_DECREF(x); |
| 2343 | if (prod == NULL) |
| 2344 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | sum = PyNumber_Add(sofar, prod); |
| 2347 | Py_DECREF(prod); |
| 2348 | if (sum == NULL) |
| 2349 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | if (fracpart == 0.0) |
| 2352 | return sum; |
| 2353 | /* So far we've lost no information. Dealing with the |
| 2354 | * fractional part requires float arithmetic, and may |
| 2355 | * lose a little info. |
| 2356 | */ |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2357 | assert(PyLong_CheckExact(factor)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | dnum = PyLong_AsDouble(factor); |
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 | dnum *= fracpart; |
| 2361 | fracpart = modf(dnum, &intpart); |
| 2362 | x = PyLong_FromDouble(intpart); |
| 2363 | if (x == NULL) { |
| 2364 | Py_DECREF(sum); |
| 2365 | return NULL; |
| 2366 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | y = PyNumber_Add(sum, x); |
| 2369 | Py_DECREF(sum); |
| 2370 | Py_DECREF(x); |
| 2371 | *leftover += fracpart; |
Serhiy Storchaka | 4ffd465 | 2017-10-23 17:12:28 +0300 | [diff] [blame] | 2372 | assert(y == NULL || PyLong_CheckExact(y)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | return y; |
| 2374 | } |
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 | PyErr_Format(PyExc_TypeError, |
| 2377 | "unsupported type for timedelta %s component: %s", |
| 2378 | tag, Py_TYPE(num)->tp_name); |
| 2379 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | static PyObject * |
| 2383 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | /* Argument objects. */ |
| 2388 | PyObject *day = NULL; |
| 2389 | PyObject *second = NULL; |
| 2390 | PyObject *us = NULL; |
| 2391 | PyObject *ms = NULL; |
| 2392 | PyObject *minute = NULL; |
| 2393 | PyObject *hour = NULL; |
| 2394 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2397 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2398 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | static char *keywords[] = { |
| 2401 | "days", "seconds", "microseconds", "milliseconds", |
| 2402 | "minutes", "hours", "weeks", NULL |
| 2403 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2405 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2406 | keywords, |
| 2407 | &day, &second, &us, |
| 2408 | &ms, &minute, &hour, &week) == 0) |
| 2409 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | x = PyLong_FromLong(0); |
| 2412 | if (x == NULL) |
| 2413 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | #define CLEANUP \ |
| 2416 | Py_DECREF(x); \ |
| 2417 | x = y; \ |
| 2418 | if (x == NULL) \ |
| 2419 | goto Done |
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 | if (us) { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2422 | y = accum("microseconds", x, us, _PyLong_One, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2423 | CLEANUP; |
| 2424 | } |
| 2425 | if (ms) { |
| 2426 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2427 | CLEANUP; |
| 2428 | } |
| 2429 | if (second) { |
| 2430 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2431 | CLEANUP; |
| 2432 | } |
| 2433 | if (minute) { |
| 2434 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2435 | CLEANUP; |
| 2436 | } |
| 2437 | if (hour) { |
| 2438 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2439 | CLEANUP; |
| 2440 | } |
| 2441 | if (day) { |
| 2442 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2443 | CLEANUP; |
| 2444 | } |
| 2445 | if (week) { |
| 2446 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2447 | CLEANUP; |
| 2448 | } |
| 2449 | if (leftover_us) { |
| 2450 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2451 | double whole_us = round(leftover_us); |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2452 | int x_is_odd; |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2453 | PyObject *temp; |
| 2454 | |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2455 | whole_us = round(leftover_us); |
| 2456 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2457 | /* We're exactly halfway between two integers. In order |
| 2458 | * to do round-half-to-even, we must determine whether x |
| 2459 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2460 | * code below uses bitwise and operation to check the last |
| 2461 | * bit. */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2462 | temp = PyNumber_And(x, _PyLong_One); /* temp <- x & 1 */ |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2463 | if (temp == NULL) { |
| 2464 | Py_DECREF(x); |
| 2465 | goto Done; |
| 2466 | } |
| 2467 | x_is_odd = PyObject_IsTrue(temp); |
| 2468 | Py_DECREF(temp); |
| 2469 | if (x_is_odd == -1) { |
| 2470 | Py_DECREF(x); |
| 2471 | goto Done; |
| 2472 | } |
| 2473 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2474 | } |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2475 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2476 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | if (temp == NULL) { |
| 2479 | Py_DECREF(x); |
| 2480 | goto Done; |
| 2481 | } |
| 2482 | y = PyNumber_Add(x, temp); |
| 2483 | Py_DECREF(temp); |
| 2484 | CLEANUP; |
| 2485 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | self = microseconds_to_delta_ex(x, type); |
| 2488 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2489 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2490 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2491 | |
| 2492 | #undef CLEANUP |
| 2493 | } |
| 2494 | |
| 2495 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2496 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | return (GET_TD_DAYS(self) != 0 |
| 2499 | || GET_TD_SECONDS(self) != 0 |
| 2500 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | static PyObject * |
| 2504 | delta_repr(PyDateTime_Delta *self) |
| 2505 | { |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2506 | PyObject *args = PyUnicode_FromString(""); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2507 | |
Utkarsh Upadhyay | cc5a65c | 2017-07-25 23:51:33 +0200 | [diff] [blame] | 2508 | if (args == NULL) { |
| 2509 | return NULL; |
| 2510 | } |
| 2511 | |
| 2512 | const char *sep = ""; |
| 2513 | |
| 2514 | if (GET_TD_DAYS(self) != 0) { |
| 2515 | Py_SETREF(args, PyUnicode_FromFormat("days=%d", GET_TD_DAYS(self))); |
| 2516 | if (args == NULL) { |
| 2517 | return NULL; |
| 2518 | } |
| 2519 | sep = ", "; |
| 2520 | } |
| 2521 | |
| 2522 | if (GET_TD_SECONDS(self) != 0) { |
| 2523 | Py_SETREF(args, PyUnicode_FromFormat("%U%sseconds=%d", args, sep, |
| 2524 | GET_TD_SECONDS(self))); |
| 2525 | if (args == NULL) { |
| 2526 | return NULL; |
| 2527 | } |
| 2528 | sep = ", "; |
| 2529 | } |
| 2530 | |
| 2531 | if (GET_TD_MICROSECONDS(self) != 0) { |
| 2532 | Py_SETREF(args, PyUnicode_FromFormat("%U%smicroseconds=%d", args, sep, |
| 2533 | GET_TD_MICROSECONDS(self))); |
| 2534 | if (args == NULL) { |
| 2535 | return NULL; |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | if (PyUnicode_GET_LENGTH(args) == 0) { |
| 2540 | Py_SETREF(args, PyUnicode_FromString("0")); |
| 2541 | if (args == NULL) { |
| 2542 | return NULL; |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | PyObject *repr = PyUnicode_FromFormat("%s(%S)", Py_TYPE(self)->tp_name, |
| 2547 | args); |
| 2548 | Py_DECREF(args); |
| 2549 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | static PyObject * |
| 2553 | delta_str(PyDateTime_Delta *self) |
| 2554 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | int us = GET_TD_MICROSECONDS(self); |
| 2556 | int seconds = GET_TD_SECONDS(self); |
| 2557 | int minutes = divmod(seconds, 60, &seconds); |
| 2558 | int hours = divmod(minutes, 60, &minutes); |
| 2559 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 | if (days) { |
| 2562 | if (us) |
| 2563 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2564 | days, (days == 1 || days == -1) ? "" : "s", |
| 2565 | hours, minutes, seconds, us); |
| 2566 | else |
| 2567 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2568 | days, (days == 1 || days == -1) ? "" : "s", |
| 2569 | hours, minutes, seconds); |
| 2570 | } else { |
| 2571 | if (us) |
| 2572 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2573 | hours, minutes, seconds, us); |
| 2574 | else |
| 2575 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2576 | hours, minutes, seconds); |
| 2577 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2578 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2581 | /* Pickle support, a simple use of __reduce__. */ |
| 2582 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2583 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2584 | static PyObject * |
| 2585 | delta_getstate(PyDateTime_Delta *self) |
| 2586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2587 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2588 | GET_TD_SECONDS(self), |
| 2589 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2592 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2593 | delta_total_seconds(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2594 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | PyObject *total_seconds; |
| 2596 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2598 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2599 | if (total_microseconds == NULL) |
| 2600 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2601 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2602 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2609 | delta_reduce(PyDateTime_Delta* self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2611 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2615 | |
| 2616 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | {"days", T_INT, OFFSET(days), READONLY, |
| 2619 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2622 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2625 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2626 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2627 | }; |
| 2628 | |
| 2629 | static PyMethodDef delta_methods[] = { |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2630 | {"total_seconds", delta_total_seconds, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2633 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2634 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2637 | }; |
| 2638 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2639 | static const char delta_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2640 | PyDoc_STR("Difference between two datetime values."); |
| 2641 | |
| 2642 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | delta_add, /* nb_add */ |
| 2644 | delta_subtract, /* nb_subtract */ |
| 2645 | delta_multiply, /* nb_multiply */ |
| 2646 | delta_remainder, /* nb_remainder */ |
| 2647 | delta_divmod, /* nb_divmod */ |
| 2648 | 0, /* nb_power */ |
| 2649 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2650 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2651 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2652 | (inquiry)delta_bool, /* nb_bool */ |
| 2653 | 0, /*nb_invert*/ |
| 2654 | 0, /*nb_lshift*/ |
| 2655 | 0, /*nb_rshift*/ |
| 2656 | 0, /*nb_and*/ |
| 2657 | 0, /*nb_xor*/ |
| 2658 | 0, /*nb_or*/ |
| 2659 | 0, /*nb_int*/ |
| 2660 | 0, /*nb_reserved*/ |
| 2661 | 0, /*nb_float*/ |
| 2662 | 0, /*nb_inplace_add*/ |
| 2663 | 0, /*nb_inplace_subtract*/ |
| 2664 | 0, /*nb_inplace_multiply*/ |
| 2665 | 0, /*nb_inplace_remainder*/ |
| 2666 | 0, /*nb_inplace_power*/ |
| 2667 | 0, /*nb_inplace_lshift*/ |
| 2668 | 0, /*nb_inplace_rshift*/ |
| 2669 | 0, /*nb_inplace_and*/ |
| 2670 | 0, /*nb_inplace_xor*/ |
| 2671 | 0, /*nb_inplace_or*/ |
| 2672 | delta_divide, /* nb_floor_divide */ |
| 2673 | delta_truedivide, /* nb_true_divide */ |
| 2674 | 0, /* nb_inplace_floor_divide */ |
| 2675 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2676 | }; |
| 2677 | |
| 2678 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2680 | "datetime.timedelta", /* tp_name */ |
| 2681 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2682 | 0, /* tp_itemsize */ |
| 2683 | 0, /* tp_dealloc */ |
| 2684 | 0, /* tp_print */ |
| 2685 | 0, /* tp_getattr */ |
| 2686 | 0, /* tp_setattr */ |
| 2687 | 0, /* tp_reserved */ |
| 2688 | (reprfunc)delta_repr, /* tp_repr */ |
| 2689 | &delta_as_number, /* tp_as_number */ |
| 2690 | 0, /* tp_as_sequence */ |
| 2691 | 0, /* tp_as_mapping */ |
| 2692 | (hashfunc)delta_hash, /* tp_hash */ |
| 2693 | 0, /* tp_call */ |
| 2694 | (reprfunc)delta_str, /* tp_str */ |
| 2695 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2696 | 0, /* tp_setattro */ |
| 2697 | 0, /* tp_as_buffer */ |
| 2698 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2699 | delta_doc, /* tp_doc */ |
| 2700 | 0, /* tp_traverse */ |
| 2701 | 0, /* tp_clear */ |
| 2702 | delta_richcompare, /* tp_richcompare */ |
| 2703 | 0, /* tp_weaklistoffset */ |
| 2704 | 0, /* tp_iter */ |
| 2705 | 0, /* tp_iternext */ |
| 2706 | delta_methods, /* tp_methods */ |
| 2707 | delta_members, /* tp_members */ |
| 2708 | 0, /* tp_getset */ |
| 2709 | 0, /* tp_base */ |
| 2710 | 0, /* tp_dict */ |
| 2711 | 0, /* tp_descr_get */ |
| 2712 | 0, /* tp_descr_set */ |
| 2713 | 0, /* tp_dictoffset */ |
| 2714 | 0, /* tp_init */ |
| 2715 | 0, /* tp_alloc */ |
| 2716 | delta_new, /* tp_new */ |
| 2717 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2718 | }; |
| 2719 | |
| 2720 | /* |
| 2721 | * PyDateTime_Date implementation. |
| 2722 | */ |
| 2723 | |
| 2724 | /* Accessor properties. */ |
| 2725 | |
| 2726 | static PyObject * |
| 2727 | date_year(PyDateTime_Date *self, void *unused) |
| 2728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | static PyObject * |
| 2733 | date_month(PyDateTime_Date *self, void *unused) |
| 2734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | static PyObject * |
| 2739 | date_day(PyDateTime_Date *self, void *unused) |
| 2740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | {"year", (getter)date_year}, |
| 2746 | {"month", (getter)date_month}, |
| 2747 | {"day", (getter)date_day}, |
| 2748 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2749 | }; |
| 2750 | |
| 2751 | /* Constructors. */ |
| 2752 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2753 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2754 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2755 | static PyObject * |
| 2756 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2757 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | PyObject *self = NULL; |
| 2759 | PyObject *state; |
| 2760 | int year; |
| 2761 | int month; |
| 2762 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | /* Check for invocation from pickle with __getstate__ state */ |
| 2765 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2766 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2767 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2768 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2769 | { |
| 2770 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2773 | if (me != NULL) { |
| 2774 | char *pdata = PyBytes_AS_STRING(state); |
| 2775 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2776 | me->hashcode = -1; |
| 2777 | } |
| 2778 | return (PyObject *)me; |
| 2779 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2782 | &year, &month, &day)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | self = new_date_ex(year, month, day, type); |
| 2784 | } |
| 2785 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | /* Return new date from localtime(t). */ |
| 2789 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2790 | date_local_from_object(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2791 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2792 | struct tm tm; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2794 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2795 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2797 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 2798 | if (_PyTime_localtime(t, &tm) != 0) |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2799 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2800 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2801 | return new_date_subclass_ex(tm.tm_year + 1900, |
| 2802 | tm.tm_mon + 1, |
| 2803 | tm.tm_mday, |
| 2804 | cls); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | /* Return new date from current time. |
| 2808 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2809 | * only way to be sure of that is to *call* time.time(). That's not |
| 2810 | * generally the same as calling C's time. |
| 2811 | */ |
| 2812 | static PyObject * |
| 2813 | date_today(PyObject *cls, PyObject *dummy) |
| 2814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | PyObject *time; |
| 2816 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2817 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | time = time_time(); |
| 2820 | if (time == NULL) |
| 2821 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | /* Note well: today() is a class method, so this may not call |
| 2824 | * date.fromtimestamp. For example, it may call |
| 2825 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2826 | * time.time() delivers; if someone were gonzo about optimization, |
| 2827 | * date.today() could get away with plain C time(). |
| 2828 | */ |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 2829 | result = _PyObject_CallMethodIdObjArgs(cls, &PyId_fromtimestamp, |
| 2830 | time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | Py_DECREF(time); |
| 2832 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2836 | static PyObject * |
| 2837 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2838 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2839 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2841 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2842 | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
| 2843 | result = date_local_from_object(cls, timestamp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2844 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2847 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2848 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2849 | * the ordinal is out of range. |
| 2850 | */ |
| 2851 | static PyObject * |
| 2852 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | PyObject *result = NULL; |
| 2855 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2858 | int year; |
| 2859 | int month; |
| 2860 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | if (ordinal < 1) |
| 2863 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2864 | ">= 1"); |
| 2865 | else { |
| 2866 | ord_to_ymd(ordinal, &year, &month, &day); |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2867 | result = new_date_subclass_ex(year, month, day, cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2873 | /* Return the new date from a string as generated by date.isoformat() */ |
| 2874 | static PyObject * |
| 2875 | date_fromisoformat(PyObject *cls, PyObject *dtstr) { |
| 2876 | assert(dtstr != NULL); |
| 2877 | |
| 2878 | if (!PyUnicode_Check(dtstr)) { |
| 2879 | PyErr_SetString(PyExc_TypeError, "fromisoformat: argument must be str"); |
| 2880 | return NULL; |
| 2881 | } |
| 2882 | |
| 2883 | Py_ssize_t len; |
| 2884 | |
| 2885 | const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len); |
| 2886 | |
| 2887 | int year = 0, month = 0, day = 0; |
| 2888 | |
| 2889 | int rv; |
| 2890 | if (len == 10) { |
| 2891 | rv = parse_isoformat_date(dt_ptr, &year, &month, &day); |
| 2892 | } else { |
| 2893 | rv = -1; |
| 2894 | } |
| 2895 | |
| 2896 | if (rv < 0) { |
| 2897 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %s", |
| 2898 | dt_ptr); |
| 2899 | return NULL; |
| 2900 | } |
| 2901 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 2902 | return new_date_subclass_ex(year, month, day, cls); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2906 | /* |
| 2907 | * Date arithmetic. |
| 2908 | */ |
| 2909 | |
| 2910 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2911 | * instead. |
| 2912 | */ |
| 2913 | static PyObject * |
| 2914 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2916 | PyObject *result = NULL; |
| 2917 | int year = GET_YEAR(date); |
| 2918 | int month = GET_MONTH(date); |
| 2919 | int deltadays = GET_TD_DAYS(delta); |
| 2920 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2921 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | if (normalize_date(&year, &month, &day) >= 0) |
| 2924 | result = new_date(year, month, day); |
| 2925 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | static PyObject * |
| 2929 | date_add(PyObject *left, PyObject *right) |
| 2930 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2931 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2932 | Py_RETURN_NOTIMPLEMENTED; |
| 2933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | if (PyDate_Check(left)) { |
| 2935 | /* date + ??? */ |
| 2936 | if (PyDelta_Check(right)) |
| 2937 | /* date + delta */ |
| 2938 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2939 | (PyDateTime_Delta *) right, |
| 2940 | 0); |
| 2941 | } |
| 2942 | else { |
| 2943 | /* ??? + date |
| 2944 | * 'right' must be one of us, or we wouldn't have been called |
| 2945 | */ |
| 2946 | if (PyDelta_Check(left)) |
| 2947 | /* delta + date */ |
| 2948 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2949 | (PyDateTime_Delta *) left, |
| 2950 | 0); |
| 2951 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2952 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | static PyObject * |
| 2956 | date_subtract(PyObject *left, PyObject *right) |
| 2957 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2958 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2959 | Py_RETURN_NOTIMPLEMENTED; |
| 2960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | if (PyDate_Check(left)) { |
| 2962 | if (PyDate_Check(right)) { |
| 2963 | /* date - date */ |
| 2964 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2965 | GET_MONTH(left), |
| 2966 | GET_DAY(left)); |
| 2967 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2968 | GET_MONTH(right), |
| 2969 | GET_DAY(right)); |
| 2970 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2971 | } |
| 2972 | if (PyDelta_Check(right)) { |
| 2973 | /* date - delta */ |
| 2974 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2975 | (PyDateTime_Delta *) right, |
| 2976 | 1); |
| 2977 | } |
| 2978 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2979 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | |
| 2983 | /* Various ways to turn a date into a string. */ |
| 2984 | |
| 2985 | static PyObject * |
| 2986 | date_repr(PyDateTime_Date *self) |
| 2987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2989 | Py_TYPE(self)->tp_name, |
| 2990 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2994 | date_isoformat(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2995 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2997 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 3000 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3001 | static PyObject * |
| 3002 | date_str(PyDateTime_Date *self) |
| 3003 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3004 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | |
| 3008 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3009 | date_ctime(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | static PyObject * |
| 3015 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3016 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | /* This method can be inherited, and needs to call the |
| 3018 | * timetuple() method appropriate to self's class. |
| 3019 | */ |
| 3020 | PyObject *result; |
| 3021 | PyObject *tuple; |
| 3022 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3023 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3027 | &format)) |
| 3028 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3029 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3030 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | if (tuple == NULL) |
| 3032 | return NULL; |
| 3033 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3034 | (PyObject *)self); |
| 3035 | Py_DECREF(tuple); |
| 3036 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3039 | static PyObject * |
| 3040 | date_format(PyDateTime_Date *self, PyObject *args) |
| 3041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3044 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 3045 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 3048 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3049 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3050 | |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 3051 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_strftime, |
| 3052 | format, NULL); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3055 | /* ISO methods. */ |
| 3056 | |
| 3057 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3058 | date_isoweekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3059 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3066 | date_isocalendar(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | int year = GET_YEAR(self); |
| 3069 | int week1_monday = iso_week1_monday(year); |
| 3070 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 3071 | int week; |
| 3072 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | week = divmod(today - week1_monday, 7, &day); |
| 3075 | if (week < 0) { |
| 3076 | --year; |
| 3077 | week1_monday = iso_week1_monday(year); |
| 3078 | week = divmod(today - week1_monday, 7, &day); |
| 3079 | } |
| 3080 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 3081 | ++year; |
| 3082 | week = 0; |
| 3083 | } |
| 3084 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | /* Miscellaneous methods. */ |
| 3088 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3089 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3090 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3091 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | if (PyDate_Check(other)) { |
| 3093 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 3094 | ((PyDateTime_Date *)other)->data, |
| 3095 | _PyDateTime_DATE_DATASIZE); |
| 3096 | return diff_to_bool(diff, op); |
| 3097 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3098 | else |
| 3099 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3103 | date_timetuple(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | return build_struct_time(GET_YEAR(self), |
| 3106 | GET_MONTH(self), |
| 3107 | GET_DAY(self), |
| 3108 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3111 | static PyObject * |
| 3112 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 3113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | PyObject *clone; |
| 3115 | PyObject *tuple; |
| 3116 | int year = GET_YEAR(self); |
| 3117 | int month = GET_MONTH(self); |
| 3118 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 3121 | &year, &month, &day)) |
| 3122 | return NULL; |
| 3123 | tuple = Py_BuildValue("iii", year, month, day); |
| 3124 | if (tuple == NULL) |
| 3125 | return NULL; |
| 3126 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 3127 | Py_DECREF(tuple); |
| 3128 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3131 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3132 | generic_hash(unsigned char *data, int len) |
| 3133 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 3134 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | |
| 3138 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3139 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3140 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3141 | date_hash(PyDateTime_Date *self) |
| 3142 | { |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3143 | if (self->hashcode == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3144 | self->hashcode = generic_hash( |
| 3145 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 3146 | } |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3152 | date_toordinal(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 3155 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3159 | date_weekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3166 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3167 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3168 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3169 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3170 | date_getstate(PyDateTime_Date *self) |
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 | PyObject* field; |
| 3173 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 3174 | _PyDateTime_DATE_DATASIZE); |
| 3175 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3179 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 3189 | METH_CLASS, |
| 3190 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 3191 | "time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 3194 | METH_CLASS, |
| 3195 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 3196 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3197 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 3198 | {"fromisoformat", (PyCFunction)date_fromisoformat, METH_O | |
| 3199 | METH_CLASS, |
| 3200 | PyDoc_STR("str -> Construct a date from the output of date.isoformat()")}, |
| 3201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 3203 | PyDoc_STR("Current date or datetime: same as " |
| 3204 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 3209 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 3212 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3215 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 3218 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 3221 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 3222 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 3225 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 3228 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3229 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3231 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 3232 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 3233 | "1 is day 1.")}, |
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 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 3236 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 3237 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 3240 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3242 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 3243 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3246 | }; |
| 3247 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3248 | static const char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3249 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3250 | |
| 3251 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3252 | date_add, /* nb_add */ |
| 3253 | date_subtract, /* nb_subtract */ |
| 3254 | 0, /* nb_multiply */ |
| 3255 | 0, /* nb_remainder */ |
| 3256 | 0, /* nb_divmod */ |
| 3257 | 0, /* nb_power */ |
| 3258 | 0, /* nb_negative */ |
| 3259 | 0, /* nb_positive */ |
| 3260 | 0, /* nb_absolute */ |
| 3261 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3262 | }; |
| 3263 | |
| 3264 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3266 | "datetime.date", /* tp_name */ |
| 3267 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 3268 | 0, /* tp_itemsize */ |
| 3269 | 0, /* tp_dealloc */ |
| 3270 | 0, /* tp_print */ |
| 3271 | 0, /* tp_getattr */ |
| 3272 | 0, /* tp_setattr */ |
| 3273 | 0, /* tp_reserved */ |
| 3274 | (reprfunc)date_repr, /* tp_repr */ |
| 3275 | &date_as_number, /* tp_as_number */ |
| 3276 | 0, /* tp_as_sequence */ |
| 3277 | 0, /* tp_as_mapping */ |
| 3278 | (hashfunc)date_hash, /* tp_hash */ |
| 3279 | 0, /* tp_call */ |
| 3280 | (reprfunc)date_str, /* tp_str */ |
| 3281 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3282 | 0, /* tp_setattro */ |
| 3283 | 0, /* tp_as_buffer */ |
| 3284 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3285 | date_doc, /* tp_doc */ |
| 3286 | 0, /* tp_traverse */ |
| 3287 | 0, /* tp_clear */ |
| 3288 | date_richcompare, /* tp_richcompare */ |
| 3289 | 0, /* tp_weaklistoffset */ |
| 3290 | 0, /* tp_iter */ |
| 3291 | 0, /* tp_iternext */ |
| 3292 | date_methods, /* tp_methods */ |
| 3293 | 0, /* tp_members */ |
| 3294 | date_getset, /* tp_getset */ |
| 3295 | 0, /* tp_base */ |
| 3296 | 0, /* tp_dict */ |
| 3297 | 0, /* tp_descr_get */ |
| 3298 | 0, /* tp_descr_set */ |
| 3299 | 0, /* tp_dictoffset */ |
| 3300 | 0, /* tp_init */ |
| 3301 | 0, /* tp_alloc */ |
| 3302 | date_new, /* tp_new */ |
| 3303 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3304 | }; |
| 3305 | |
| 3306 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3307 | * PyDateTime_TZInfo implementation. |
| 3308 | */ |
| 3309 | |
| 3310 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 3311 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 3312 | * 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] | 3313 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3314 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 3315 | * |
| 3316 | * Note: For reasons having to do with pickling of subclasses, we have |
| 3317 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 3318 | * in the Python implementation (__init__() could raise NotImplementedError |
| 3319 | * there without ill effect), but doing so in the C implementation hit a |
| 3320 | * brick wall. |
| 3321 | */ |
| 3322 | |
| 3323 | static PyObject * |
| 3324 | tzinfo_nogo(const char* methodname) |
| 3325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | PyErr_Format(PyExc_NotImplementedError, |
| 3327 | "a tzinfo subclass must implement %s()", |
| 3328 | methodname); |
| 3329 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | /* Methods. A subclass must implement these. */ |
| 3333 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3334 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3335 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 3336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3340 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3341 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 3342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3346 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3347 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 3348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3352 | |
| 3353 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 3354 | PyDateTime_Delta *delta, |
| 3355 | int factor); |
| 3356 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 3357 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 3358 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3359 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3360 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3361 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3362 | PyObject *result = NULL; |
| 3363 | PyObject *off = NULL, *dst = NULL; |
| 3364 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3365 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3366 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | PyErr_SetString(PyExc_TypeError, |
| 3368 | "fromutc: argument must be a datetime"); |
| 3369 | return NULL; |
| 3370 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3371 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3373 | "is not self"); |
| 3374 | return NULL; |
| 3375 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3376 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3377 | off = datetime_utcoffset(dt, NULL); |
| 3378 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3379 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3380 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3382 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3383 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3384 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3385 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3386 | dst = datetime_dst(dt, NULL); |
| 3387 | if (dst == NULL) |
| 3388 | goto Fail; |
| 3389 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3391 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3392 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3394 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3395 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3396 | if (delta == NULL) |
| 3397 | goto Fail; |
| 3398 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3399 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3401 | |
| 3402 | Py_DECREF(dst); |
| 3403 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3404 | if (dst == NULL) |
| 3405 | goto Fail; |
| 3406 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3407 | goto Inconsistent; |
Alexander Belopolsky | c79447b | 2015-09-27 21:41:55 -0400 | [diff] [blame] | 3408 | if (delta_bool((PyDateTime_Delta *)dst) != 0) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3409 | Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 3410 | (PyDateTime_Delta *)dst, 1)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3411 | if (result == NULL) |
| 3412 | goto Fail; |
| 3413 | } |
| 3414 | Py_DECREF(delta); |
| 3415 | Py_DECREF(dst); |
| 3416 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3417 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3418 | |
| 3419 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3421 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3422 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 3423 | /* fall through to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3424 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3425 | Py_XDECREF(off); |
| 3426 | Py_XDECREF(dst); |
| 3427 | Py_XDECREF(delta); |
| 3428 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3432 | /* |
| 3433 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3434 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3435 | */ |
| 3436 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3437 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3438 | tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3439 | { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3440 | PyObject *args, *state; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3442 | _Py_IDENTIFIER(__getinitargs__); |
| 3443 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3444 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3445 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | if (getinitargs != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3447 | args = _PyObject_CallNoArg(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | Py_DECREF(getinitargs); |
| 3449 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3450 | return NULL; |
| 3451 | } |
| 3452 | } |
| 3453 | else { |
| 3454 | PyErr_Clear(); |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3455 | |
| 3456 | args = PyTuple_New(0); |
| 3457 | if (args == NULL) { |
| 3458 | return NULL; |
| 3459 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3461 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3462 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | if (getstate != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3464 | state = _PyObject_CallNoArg(getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | Py_DECREF(getstate); |
| 3466 | if (state == NULL) { |
| 3467 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | return NULL; |
| 3469 | } |
| 3470 | } |
| 3471 | else { |
| 3472 | PyObject **dictptr; |
| 3473 | PyErr_Clear(); |
| 3474 | state = Py_None; |
| 3475 | dictptr = _PyObject_GetDictPtr(self); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3476 | if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3477 | state = *dictptr; |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3478 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3479 | Py_INCREF(state); |
| 3480 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3482 | if (state == Py_None) { |
| 3483 | Py_DECREF(state); |
| 3484 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3485 | } |
| 3486 | else |
| 3487 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3488 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3489 | |
| 3490 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3492 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3493 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3496 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3497 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3499 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3500 | PyDoc_STR("datetime -> DST offset as timedelta positive east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3502 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3503 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3504 | |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3505 | {"__reduce__", tzinfo_reduce, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3509 | }; |
| 3510 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3511 | static const char tzinfo_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3512 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3513 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3514 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3515 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3516 | "datetime.tzinfo", /* tp_name */ |
| 3517 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3518 | 0, /* tp_itemsize */ |
| 3519 | 0, /* tp_dealloc */ |
| 3520 | 0, /* tp_print */ |
| 3521 | 0, /* tp_getattr */ |
| 3522 | 0, /* tp_setattr */ |
| 3523 | 0, /* tp_reserved */ |
| 3524 | 0, /* tp_repr */ |
| 3525 | 0, /* tp_as_number */ |
| 3526 | 0, /* tp_as_sequence */ |
| 3527 | 0, /* tp_as_mapping */ |
| 3528 | 0, /* tp_hash */ |
| 3529 | 0, /* tp_call */ |
| 3530 | 0, /* tp_str */ |
| 3531 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3532 | 0, /* tp_setattro */ |
| 3533 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3534 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3535 | tzinfo_doc, /* tp_doc */ |
| 3536 | 0, /* tp_traverse */ |
| 3537 | 0, /* tp_clear */ |
| 3538 | 0, /* tp_richcompare */ |
| 3539 | 0, /* tp_weaklistoffset */ |
| 3540 | 0, /* tp_iter */ |
| 3541 | 0, /* tp_iternext */ |
| 3542 | tzinfo_methods, /* tp_methods */ |
| 3543 | 0, /* tp_members */ |
| 3544 | 0, /* tp_getset */ |
| 3545 | 0, /* tp_base */ |
| 3546 | 0, /* tp_dict */ |
| 3547 | 0, /* tp_descr_get */ |
| 3548 | 0, /* tp_descr_set */ |
| 3549 | 0, /* tp_dictoffset */ |
| 3550 | 0, /* tp_init */ |
| 3551 | 0, /* tp_alloc */ |
| 3552 | PyType_GenericNew, /* tp_new */ |
| 3553 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3554 | }; |
| 3555 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3556 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3557 | |
| 3558 | static PyObject * |
| 3559 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3560 | { |
| 3561 | PyObject *offset; |
| 3562 | PyObject *name = NULL; |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 3563 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|U:timezone", timezone_kws, |
| 3564 | &PyDateTime_DeltaType, &offset, &name)) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3565 | return new_timezone(offset, name); |
| 3566 | |
| 3567 | return NULL; |
| 3568 | } |
| 3569 | |
| 3570 | static void |
| 3571 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3572 | { |
| 3573 | Py_CLEAR(self->offset); |
| 3574 | Py_CLEAR(self->name); |
| 3575 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3576 | } |
| 3577 | |
| 3578 | static PyObject * |
| 3579 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3580 | PyDateTime_TimeZone *other, int op) |
| 3581 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3582 | if (op != Py_EQ && op != Py_NE) |
| 3583 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3584 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 3585 | if (op == Py_EQ) |
| 3586 | Py_RETURN_FALSE; |
| 3587 | else |
| 3588 | Py_RETURN_TRUE; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3589 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3590 | return delta_richcompare(self->offset, other->offset, op); |
| 3591 | } |
| 3592 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3593 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3594 | timezone_hash(PyDateTime_TimeZone *self) |
| 3595 | { |
| 3596 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3597 | } |
| 3598 | |
| 3599 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3600 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3601 | otherwise. |
| 3602 | */ |
| 3603 | static int |
| 3604 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3605 | { |
| 3606 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3607 | return 0; |
| 3608 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3609 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3610 | return -1; |
| 3611 | } |
| 3612 | |
| 3613 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3614 | timezone_repr(PyDateTime_TimeZone *self) |
| 3615 | { |
| 3616 | /* Note that although timezone is not subclassable, it is convenient |
| 3617 | to use Py_TYPE(self)->tp_name here. */ |
| 3618 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3619 | |
| 3620 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3621 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3622 | |
| 3623 | if (self->name == NULL) |
| 3624 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3625 | |
| 3626 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3627 | self->name); |
| 3628 | } |
| 3629 | |
| 3630 | |
| 3631 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3632 | timezone_str(PyDateTime_TimeZone *self) |
| 3633 | { |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3634 | int hours, minutes, seconds, microseconds; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3635 | PyObject *offset; |
| 3636 | char sign; |
| 3637 | |
| 3638 | if (self->name != NULL) { |
| 3639 | Py_INCREF(self->name); |
| 3640 | return self->name; |
| 3641 | } |
Victor Stinner | 90fd895 | 2015-09-08 00:12:49 +0200 | [diff] [blame] | 3642 | if ((PyObject *)self == PyDateTime_TimeZone_UTC || |
Alexander Belopolsky | 7827a5b | 2015-09-06 13:07:21 -0400 | [diff] [blame] | 3643 | (GET_TD_DAYS(self->offset) == 0 && |
| 3644 | GET_TD_SECONDS(self->offset) == 0 && |
| 3645 | GET_TD_MICROSECONDS(self->offset) == 0)) |
| 3646 | return PyUnicode_FromString("UTC"); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3647 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3648 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3649 | sign = '-'; |
| 3650 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3651 | if (offset == NULL) |
| 3652 | return NULL; |
| 3653 | } |
| 3654 | else { |
| 3655 | sign = '+'; |
| 3656 | offset = self->offset; |
| 3657 | Py_INCREF(offset); |
| 3658 | } |
| 3659 | /* Offset is not negative here. */ |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3660 | microseconds = GET_TD_MICROSECONDS(offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3661 | seconds = GET_TD_SECONDS(offset); |
| 3662 | Py_DECREF(offset); |
| 3663 | minutes = divmod(seconds, 60, &seconds); |
| 3664 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 018d353 | 2017-07-31 10:26:50 -0400 | [diff] [blame] | 3665 | if (microseconds != 0) { |
| 3666 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d.%06d", |
| 3667 | sign, hours, minutes, |
| 3668 | seconds, microseconds); |
| 3669 | } |
| 3670 | if (seconds != 0) { |
| 3671 | return PyUnicode_FromFormat("UTC%c%02d:%02d:%02d", |
| 3672 | sign, hours, minutes, seconds); |
| 3673 | } |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3674 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | static PyObject * |
| 3678 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3679 | { |
| 3680 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3681 | return NULL; |
| 3682 | |
| 3683 | return timezone_str(self); |
| 3684 | } |
| 3685 | |
| 3686 | static PyObject * |
| 3687 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3688 | { |
| 3689 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3690 | return NULL; |
| 3691 | |
| 3692 | Py_INCREF(self->offset); |
| 3693 | return self->offset; |
| 3694 | } |
| 3695 | |
| 3696 | static PyObject * |
| 3697 | timezone_dst(PyObject *self, PyObject *dt) |
| 3698 | { |
| 3699 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3700 | return NULL; |
| 3701 | |
| 3702 | Py_RETURN_NONE; |
| 3703 | } |
| 3704 | |
| 3705 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3706 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3707 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3708 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3709 | PyErr_SetString(PyExc_TypeError, |
| 3710 | "fromutc: argument must be a datetime"); |
| 3711 | return NULL; |
| 3712 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3713 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3714 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3715 | "is not self"); |
| 3716 | return NULL; |
| 3717 | } |
| 3718 | |
| 3719 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3720 | } |
| 3721 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3722 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3723 | timezone_getinitargs(PyDateTime_TimeZone *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3724 | { |
| 3725 | if (self->name == NULL) |
| 3726 | return Py_BuildValue("(O)", self->offset); |
| 3727 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3728 | } |
| 3729 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3730 | static PyMethodDef timezone_methods[] = { |
| 3731 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3732 | 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] | 3733 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3734 | |
| 3735 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3736 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3737 | |
| 3738 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3739 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3740 | |
| 3741 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3742 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3743 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3744 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3745 | PyDoc_STR("pickle support")}, |
| 3746 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3747 | {NULL, NULL} |
| 3748 | }; |
| 3749 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3750 | static const char timezone_doc[] = |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3751 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3752 | |
| 3753 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3754 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3755 | "datetime.timezone", /* tp_name */ |
| 3756 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3757 | 0, /* tp_itemsize */ |
| 3758 | (destructor)timezone_dealloc, /* tp_dealloc */ |
| 3759 | 0, /* tp_print */ |
| 3760 | 0, /* tp_getattr */ |
| 3761 | 0, /* tp_setattr */ |
| 3762 | 0, /* tp_reserved */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3763 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3764 | 0, /* tp_as_number */ |
| 3765 | 0, /* tp_as_sequence */ |
| 3766 | 0, /* tp_as_mapping */ |
| 3767 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3768 | 0, /* tp_call */ |
| 3769 | (reprfunc)timezone_str, /* tp_str */ |
| 3770 | 0, /* tp_getattro */ |
| 3771 | 0, /* tp_setattro */ |
| 3772 | 0, /* tp_as_buffer */ |
| 3773 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3774 | timezone_doc, /* tp_doc */ |
| 3775 | 0, /* tp_traverse */ |
| 3776 | 0, /* tp_clear */ |
| 3777 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3778 | 0, /* tp_weaklistoffset */ |
| 3779 | 0, /* tp_iter */ |
| 3780 | 0, /* tp_iternext */ |
| 3781 | timezone_methods, /* tp_methods */ |
| 3782 | 0, /* tp_members */ |
| 3783 | 0, /* tp_getset */ |
| 3784 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3785 | 0, /* tp_dict */ |
| 3786 | 0, /* tp_descr_get */ |
| 3787 | 0, /* tp_descr_set */ |
| 3788 | 0, /* tp_dictoffset */ |
| 3789 | 0, /* tp_init */ |
| 3790 | 0, /* tp_alloc */ |
| 3791 | timezone_new, /* tp_new */ |
| 3792 | }; |
| 3793 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3794 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3795 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3796 | */ |
| 3797 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3798 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3799 | */ |
| 3800 | |
| 3801 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3802 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3804 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3807 | static PyObject * |
| 3808 | time_minute(PyDateTime_Time *self, void *unused) |
| 3809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /* The name time_second conflicted with some platform header file. */ |
| 3814 | static PyObject * |
| 3815 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3817 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | static PyObject * |
| 3821 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | static PyObject * |
| 3827 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3830 | Py_INCREF(result); |
| 3831 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3834 | static PyObject * |
| 3835 | time_fold(PyDateTime_Time *self, void *unused) |
| 3836 | { |
| 3837 | return PyLong_FromLong(TIME_GET_FOLD(self)); |
| 3838 | } |
| 3839 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3840 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3841 | {"hour", (getter)time_hour}, |
| 3842 | {"minute", (getter)time_minute}, |
| 3843 | {"second", (getter)py_time_second}, |
| 3844 | {"microsecond", (getter)time_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3845 | {"tzinfo", (getter)time_tzinfo}, |
| 3846 | {"fold", (getter)time_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3847 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3848 | }; |
| 3849 | |
| 3850 | /* |
| 3851 | * Constructors. |
| 3852 | */ |
| 3853 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3854 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3855 | "tzinfo", "fold", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3856 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3857 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3858 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3859 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | PyObject *self = NULL; |
| 3861 | PyObject *state; |
| 3862 | int hour = 0; |
| 3863 | int minute = 0; |
| 3864 | int second = 0; |
| 3865 | int usecond = 0; |
| 3866 | PyObject *tzinfo = Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3867 | int fold = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3869 | /* Check for invocation from pickle with __getstate__ state */ |
| 3870 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3871 | PyTuple_GET_SIZE(args) <= 2 && |
| 3872 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3873 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3874 | (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | { |
| 3876 | PyDateTime_Time *me; |
| 3877 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3879 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3880 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3881 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3882 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3883 | "tzinfo state arg"); |
| 3884 | return NULL; |
| 3885 | } |
| 3886 | } |
| 3887 | aware = (char)(tzinfo != Py_None); |
| 3888 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3889 | if (me != NULL) { |
| 3890 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3892 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3893 | me->hashcode = -1; |
| 3894 | me->hastzinfo = aware; |
| 3895 | if (aware) { |
| 3896 | Py_INCREF(tzinfo); |
| 3897 | me->tzinfo = tzinfo; |
| 3898 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3899 | if (pdata[0] & (1 << 7)) { |
| 3900 | me->data[0] -= 128; |
| 3901 | me->fold = 1; |
| 3902 | } |
| 3903 | else { |
| 3904 | me->fold = 0; |
| 3905 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3906 | } |
| 3907 | return (PyObject *)me; |
| 3908 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3909 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3910 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3911 | &hour, &minute, &second, &usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3912 | &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3913 | self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, |
| 3914 | type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3915 | } |
| 3916 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | /* |
| 3920 | * Destructor. |
| 3921 | */ |
| 3922 | |
| 3923 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3924 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3925 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3926 | if (HASTZINFO(self)) { |
| 3927 | Py_XDECREF(self->tzinfo); |
| 3928 | } |
| 3929 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3933 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3934 | */ |
| 3935 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3936 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3937 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3938 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 3939 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
| 3942 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3943 | time_dst(PyObject *self, PyObject *unused) { |
| 3944 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3948 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3949 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3953 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3954 | */ |
| 3955 | |
| 3956 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3957 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3958 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3959 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3960 | int h = TIME_GET_HOUR(self); |
| 3961 | int m = TIME_GET_MINUTE(self); |
| 3962 | int s = TIME_GET_SECOND(self); |
| 3963 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3964 | int fold = TIME_GET_FOLD(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3965 | PyObject *result = NULL; |
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 | if (us) |
| 3968 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3969 | type_name, h, m, s, us); |
| 3970 | else if (s) |
| 3971 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3972 | type_name, h, m, s); |
| 3973 | else |
| 3974 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3975 | if (result != NULL && HASTZINFO(self)) |
| 3976 | result = append_keyword_tzinfo(result, self->tzinfo); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3977 | if (result != NULL && fold) |
| 3978 | result = append_keyword_fold(result, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3979 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3982 | static PyObject * |
| 3983 | time_str(PyDateTime_Time *self) |
| 3984 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3985 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3986 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3987 | |
| 3988 | static PyObject * |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3989 | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3990 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3991 | char buf[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3992 | char *timespec = NULL; |
| 3993 | static char *keywords[] = {"timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3994 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3995 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3996 | static char *specs[][2] = { |
| 3997 | {"hours", "%02d"}, |
| 3998 | {"minutes", "%02d:%02d"}, |
| 3999 | {"seconds", "%02d:%02d:%02d"}, |
| 4000 | {"milliseconds", "%02d:%02d:%02d.%03d"}, |
| 4001 | {"microseconds", "%02d:%02d:%02d.%06d"}, |
| 4002 | }; |
| 4003 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4004 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4005 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) |
| 4006 | return NULL; |
| 4007 | |
| 4008 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 4009 | if (us == 0) { |
| 4010 | /* seconds */ |
| 4011 | given_spec = 2; |
| 4012 | } |
| 4013 | else { |
| 4014 | /* microseconds */ |
| 4015 | given_spec = 4; |
| 4016 | } |
| 4017 | } |
| 4018 | else { |
| 4019 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 4020 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 4021 | if (given_spec == 3) { |
| 4022 | /* milliseconds */ |
| 4023 | us = us / 1000; |
| 4024 | } |
| 4025 | break; |
| 4026 | } |
| 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 4031 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 4032 | return NULL; |
| 4033 | } |
| 4034 | else { |
| 4035 | result = PyUnicode_FromFormat(specs[given_spec][1], |
| 4036 | TIME_GET_HOUR(self), TIME_GET_MINUTE(self), |
| 4037 | TIME_GET_SECOND(self), us); |
| 4038 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4039 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4040 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4041 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | /* We need to append the UTC offset. */ |
| 4044 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 4045 | Py_None) < 0) { |
| 4046 | Py_DECREF(result); |
| 4047 | return NULL; |
| 4048 | } |
| 4049 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 4050 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4053 | static PyObject * |
| 4054 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 4055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | PyObject *result; |
| 4057 | PyObject *tuple; |
| 4058 | PyObject *format; |
| 4059 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4061 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 4062 | &format)) |
| 4063 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | /* Python's strftime does insane things with the year part of the |
| 4066 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 4067 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4068 | */ |
| 4069 | tuple = Py_BuildValue("iiiiiiiii", |
| 4070 | 1900, 1, 1, /* year, month, day */ |
| 4071 | TIME_GET_HOUR(self), |
| 4072 | TIME_GET_MINUTE(self), |
| 4073 | TIME_GET_SECOND(self), |
| 4074 | 0, 1, -1); /* weekday, daynum, dst */ |
| 4075 | if (tuple == NULL) |
| 4076 | return NULL; |
| 4077 | assert(PyTuple_Size(tuple) == 9); |
| 4078 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 4079 | Py_None); |
| 4080 | Py_DECREF(tuple); |
| 4081 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4082 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4083 | |
| 4084 | /* |
| 4085 | * Miscellaneous methods. |
| 4086 | */ |
| 4087 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4088 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4089 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4090 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4091 | PyObject *result = NULL; |
| 4092 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4093 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4094 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4095 | if (! PyTime_Check(other)) |
| 4096 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4097 | |
| 4098 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4099 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4100 | ((PyDateTime_Time *)other)->data, |
| 4101 | _PyDateTime_TIME_DATASIZE); |
| 4102 | return diff_to_bool(diff, op); |
| 4103 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4104 | offset1 = time_utcoffset(self, NULL); |
| 4105 | if (offset1 == NULL) |
| 4106 | return NULL; |
| 4107 | offset2 = time_utcoffset(other, NULL); |
| 4108 | if (offset2 == NULL) |
| 4109 | goto done; |
| 4110 | /* If they're both naive, or both aware and have the same offsets, |
| 4111 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4112 | * offset2 == Py_None at this point. |
| 4113 | */ |
| 4114 | if ((offset1 == offset2) || |
| 4115 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4116 | delta_cmp(offset1, offset2) == 0)) { |
| 4117 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 4118 | ((PyDateTime_Time *)other)->data, |
| 4119 | _PyDateTime_TIME_DATASIZE); |
| 4120 | result = diff_to_bool(diff, op); |
| 4121 | } |
| 4122 | /* The hard case: both aware with different UTC offsets */ |
| 4123 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 4124 | int offsecs1, offsecs2; |
| 4125 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 4126 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 4127 | TIME_GET_MINUTE(self) * 60 + |
| 4128 | TIME_GET_SECOND(self) - |
| 4129 | GET_TD_DAYS(offset1) * 86400 - |
| 4130 | GET_TD_SECONDS(offset1); |
| 4131 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 4132 | TIME_GET_MINUTE(other) * 60 + |
| 4133 | TIME_GET_SECOND(other) - |
| 4134 | GET_TD_DAYS(offset2) * 86400 - |
| 4135 | GET_TD_SECONDS(offset2); |
| 4136 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4137 | if (diff == 0) |
| 4138 | diff = TIME_GET_MICROSECOND(self) - |
| 4139 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4140 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4142 | else if (op == Py_EQ) { |
| 4143 | result = Py_False; |
| 4144 | Py_INCREF(result); |
| 4145 | } |
| 4146 | else if (op == Py_NE) { |
| 4147 | result = Py_True; |
| 4148 | Py_INCREF(result); |
| 4149 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4150 | else { |
| 4151 | PyErr_SetString(PyExc_TypeError, |
| 4152 | "can't compare offset-naive and " |
| 4153 | "offset-aware times"); |
| 4154 | } |
| 4155 | done: |
| 4156 | Py_DECREF(offset1); |
| 4157 | Py_XDECREF(offset2); |
| 4158 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4161 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4162 | time_hash(PyDateTime_Time *self) |
| 4163 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4164 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4165 | PyObject *offset, *self0; |
Victor Stinner | 423c16b | 2017-01-03 23:47:12 +0100 | [diff] [blame] | 4166 | if (TIME_GET_FOLD(self)) { |
| 4167 | self0 = new_time_ex2(TIME_GET_HOUR(self), |
| 4168 | TIME_GET_MINUTE(self), |
| 4169 | TIME_GET_SECOND(self), |
| 4170 | TIME_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4171 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4172 | 0, Py_TYPE(self)); |
| 4173 | if (self0 == NULL) |
| 4174 | return -1; |
| 4175 | } |
| 4176 | else { |
| 4177 | self0 = (PyObject *)self; |
| 4178 | Py_INCREF(self0); |
| 4179 | } |
| 4180 | offset = time_utcoffset(self0, NULL); |
| 4181 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4182 | |
| 4183 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4187 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4188 | self->hashcode = generic_hash( |
| 4189 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4190 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4191 | PyObject *temp1, *temp2; |
| 4192 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4193 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4194 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 4195 | TIME_GET_MINUTE(self) * 60 + |
| 4196 | TIME_GET_SECOND(self); |
| 4197 | microseconds = TIME_GET_MICROSECOND(self); |
| 4198 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 4199 | if (temp1 == NULL) { |
| 4200 | Py_DECREF(offset); |
| 4201 | return -1; |
| 4202 | } |
| 4203 | temp2 = delta_subtract(temp1, offset); |
| 4204 | Py_DECREF(temp1); |
| 4205 | if (temp2 == NULL) { |
| 4206 | Py_DECREF(offset); |
| 4207 | return -1; |
| 4208 | } |
| 4209 | self->hashcode = PyObject_Hash(temp2); |
| 4210 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4212 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4213 | } |
| 4214 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4215 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4216 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4217 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4218 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4220 | PyObject *clone; |
| 4221 | PyObject *tuple; |
| 4222 | int hh = TIME_GET_HOUR(self); |
| 4223 | int mm = TIME_GET_MINUTE(self); |
| 4224 | int ss = TIME_GET_SECOND(self); |
| 4225 | int us = TIME_GET_MICROSECOND(self); |
| 4226 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4227 | int fold = TIME_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4228 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4229 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4230 | time_kws, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4231 | &hh, &mm, &ss, &us, &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 4233 | if (fold != 0 && fold != 1) { |
| 4234 | PyErr_SetString(PyExc_ValueError, |
| 4235 | "fold must be either 0 or 1"); |
| 4236 | return NULL; |
| 4237 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 4239 | if (tuple == NULL) |
| 4240 | return NULL; |
| 4241 | clone = time_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4242 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4243 | TIME_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4244 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4245 | Py_DECREF(tuple); |
| 4246 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4249 | static PyObject * |
| 4250 | time_fromisoformat(PyObject *cls, PyObject *tstr) { |
| 4251 | assert(tstr != NULL); |
| 4252 | |
| 4253 | if (!PyUnicode_Check(tstr)) { |
| 4254 | PyErr_SetString(PyExc_TypeError, "fromisoformat: argument must be str"); |
| 4255 | return NULL; |
| 4256 | } |
| 4257 | |
| 4258 | Py_ssize_t len; |
| 4259 | const char *p = PyUnicode_AsUTF8AndSize(tstr, &len); |
| 4260 | |
| 4261 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 4262 | int tzoffset, tzimicrosecond = 0; |
| 4263 | int rv = parse_isoformat_time(p, len, |
| 4264 | &hour, &minute, &second, µsecond, |
| 4265 | &tzoffset, &tzimicrosecond); |
| 4266 | |
| 4267 | if (rv < 0) { |
| 4268 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %s", p); |
| 4269 | return NULL; |
| 4270 | } |
| 4271 | |
| 4272 | PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, |
| 4273 | tzimicrosecond); |
| 4274 | |
| 4275 | if (tzinfo == NULL) { |
| 4276 | return NULL; |
| 4277 | } |
| 4278 | |
| 4279 | PyObject *t; |
| 4280 | if ( (PyTypeObject *)cls == &PyDateTime_TimeType ) { |
| 4281 | t = new_time(hour, minute, second, microsecond, tzinfo, 0); |
| 4282 | } else { |
| 4283 | t = PyObject_CallFunction(cls, "iiiiO", |
| 4284 | hour, minute, second, microsecond, tzinfo); |
| 4285 | } |
| 4286 | |
| 4287 | Py_DECREF(tzinfo); |
| 4288 | return t; |
| 4289 | } |
| 4290 | |
| 4291 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4292 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4293 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4294 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4295 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4296 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4297 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4298 | */ |
| 4299 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4300 | time_getstate(PyDateTime_Time *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | PyObject *basestate; |
| 4303 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4305 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 4306 | _PyDateTime_TIME_DATASIZE); |
| 4307 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4308 | if (proto > 3 && TIME_GET_FOLD(self)) |
| 4309 | /* Set the first bit of the first byte */ |
| 4310 | PyBytes_AS_STRING(basestate)[0] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4311 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 4312 | result = PyTuple_Pack(1, basestate); |
| 4313 | else |
| 4314 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 4315 | Py_DECREF(basestate); |
| 4316 | } |
| 4317 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4321 | time_reduce_ex(PyDateTime_Time *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4322 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4323 | int proto; |
| 4324 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4325 | return NULL; |
| 4326 | |
| 4327 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4330 | static PyObject * |
| 4331 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
| 4332 | { |
| 4333 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); |
| 4334 | } |
| 4335 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4336 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4337 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4338 | {"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 4339 | PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" |
| 4340 | "[+HH:MM].\n\n" |
| 4341 | "timespec specifies what components of the time to include.\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4343 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 4344 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4346 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 4347 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 4348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 4350 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 4353 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4355 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 4356 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4358 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 4359 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4360 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4361 | {"fromisoformat", (PyCFunction)time_fromisoformat, METH_O | METH_CLASS, |
| 4362 | PyDoc_STR("string -> time from time.isoformat() output")}, |
| 4363 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4364 | {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4365 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4366 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4367 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 4368 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 4369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4371 | }; |
| 4372 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4373 | static const char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4374 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 4375 | \n\ |
| 4376 | 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] | 4377 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4378 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4379 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4381 | "datetime.time", /* tp_name */ |
| 4382 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 4383 | 0, /* tp_itemsize */ |
| 4384 | (destructor)time_dealloc, /* tp_dealloc */ |
| 4385 | 0, /* tp_print */ |
| 4386 | 0, /* tp_getattr */ |
| 4387 | 0, /* tp_setattr */ |
| 4388 | 0, /* tp_reserved */ |
| 4389 | (reprfunc)time_repr, /* tp_repr */ |
Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 4390 | 0, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | 0, /* tp_as_sequence */ |
| 4392 | 0, /* tp_as_mapping */ |
| 4393 | (hashfunc)time_hash, /* tp_hash */ |
| 4394 | 0, /* tp_call */ |
| 4395 | (reprfunc)time_str, /* tp_str */ |
| 4396 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4397 | 0, /* tp_setattro */ |
| 4398 | 0, /* tp_as_buffer */ |
| 4399 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4400 | time_doc, /* tp_doc */ |
| 4401 | 0, /* tp_traverse */ |
| 4402 | 0, /* tp_clear */ |
| 4403 | time_richcompare, /* tp_richcompare */ |
| 4404 | 0, /* tp_weaklistoffset */ |
| 4405 | 0, /* tp_iter */ |
| 4406 | 0, /* tp_iternext */ |
| 4407 | time_methods, /* tp_methods */ |
| 4408 | 0, /* tp_members */ |
| 4409 | time_getset, /* tp_getset */ |
| 4410 | 0, /* tp_base */ |
| 4411 | 0, /* tp_dict */ |
| 4412 | 0, /* tp_descr_get */ |
| 4413 | 0, /* tp_descr_set */ |
| 4414 | 0, /* tp_dictoffset */ |
| 4415 | 0, /* tp_init */ |
| 4416 | time_alloc, /* tp_alloc */ |
| 4417 | time_new, /* tp_new */ |
| 4418 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4419 | }; |
| 4420 | |
| 4421 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4422 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4423 | */ |
| 4424 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4425 | /* Accessor properties. Properties for day, month, and year are inherited |
| 4426 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4427 | */ |
| 4428 | |
| 4429 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4430 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4435 | static PyObject * |
| 4436 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 4437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | static PyObject * |
| 4442 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 4443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | static PyObject * |
| 4448 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 4449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4450 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | static PyObject * |
| 4454 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 4455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4456 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 4457 | Py_INCREF(result); |
| 4458 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4461 | static PyObject * |
| 4462 | datetime_fold(PyDateTime_DateTime *self, void *unused) |
| 4463 | { |
| 4464 | return PyLong_FromLong(DATE_GET_FOLD(self)); |
| 4465 | } |
| 4466 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4467 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | {"hour", (getter)datetime_hour}, |
| 4469 | {"minute", (getter)datetime_minute}, |
| 4470 | {"second", (getter)datetime_second}, |
| 4471 | {"microsecond", (getter)datetime_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4472 | {"tzinfo", (getter)datetime_tzinfo}, |
| 4473 | {"fold", (getter)datetime_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4474 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4475 | }; |
| 4476 | |
| 4477 | /* |
| 4478 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4479 | */ |
| 4480 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4481 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | "year", "month", "day", "hour", "minute", "second", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4483 | "microsecond", "tzinfo", "fold", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4484 | }; |
| 4485 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4486 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4487 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | PyObject *self = NULL; |
| 4490 | PyObject *state; |
| 4491 | int year; |
| 4492 | int month; |
| 4493 | int day; |
| 4494 | int hour = 0; |
| 4495 | int minute = 0; |
| 4496 | int second = 0; |
| 4497 | int usecond = 0; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4498 | int fold = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | /* Check for invocation from pickle with __getstate__ state */ |
| 4502 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 4503 | PyTuple_GET_SIZE(args) <= 2 && |
| 4504 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 4505 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4506 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4507 | { |
| 4508 | PyDateTime_DateTime *me; |
| 4509 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4512 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4513 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 4514 | PyErr_SetString(PyExc_TypeError, "bad " |
| 4515 | "tzinfo state arg"); |
| 4516 | return NULL; |
| 4517 | } |
| 4518 | } |
| 4519 | aware = (char)(tzinfo != Py_None); |
| 4520 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4521 | if (me != NULL) { |
| 4522 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4525 | me->hashcode = -1; |
| 4526 | me->hastzinfo = aware; |
| 4527 | if (aware) { |
| 4528 | Py_INCREF(tzinfo); |
| 4529 | me->tzinfo = tzinfo; |
| 4530 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4531 | if (pdata[2] & (1 << 7)) { |
| 4532 | me->data[2] -= 128; |
| 4533 | me->fold = 1; |
| 4534 | } |
| 4535 | else { |
| 4536 | me->fold = 0; |
| 4537 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | } |
| 4539 | return (PyObject *)me; |
| 4540 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4541 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4542 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | &year, &month, &day, &hour, &minute, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4544 | &second, &usecond, &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4545 | self = new_datetime_ex2(year, month, day, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | hour, minute, second, usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4547 | tzinfo, fold, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4548 | } |
| 4549 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4552 | /* TM_FUNC is the shared type of _PyTime_localtime() and |
| 4553 | * _PyTime_gmtime(). */ |
| 4554 | typedef int (*TM_FUNC)(time_t timer, struct tm*); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4555 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4556 | /* As of version 2015f max fold in IANA database is |
| 4557 | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4558 | static long long max_fold_seconds = 24 * 3600; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4559 | /* NB: date(1970,1,1).toordinal() == 719163 */ |
Benjamin Peterson | ac965ca | 2016-09-18 18:12:21 -0700 | [diff] [blame] | 4560 | static long long epoch = 719163LL * 24 * 60 * 60; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4561 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4562 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4563 | utc_to_seconds(int year, int month, int day, |
| 4564 | int hour, int minute, int second) |
| 4565 | { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4566 | long long ordinal; |
| 4567 | |
| 4568 | /* ymd_to_ord() doesn't support year <= 0 */ |
| 4569 | if (year < MINYEAR || year > MAXYEAR) { |
| 4570 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
| 4571 | return -1; |
| 4572 | } |
| 4573 | |
| 4574 | ordinal = ymd_to_ord(year, month, day); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4575 | return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; |
| 4576 | } |
| 4577 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4578 | static long long |
| 4579 | local(long long u) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4580 | { |
| 4581 | struct tm local_time; |
Alexander Belopolsky | 8e1d3a2 | 2016-07-25 13:54:51 -0400 | [diff] [blame] | 4582 | time_t t; |
| 4583 | u -= epoch; |
| 4584 | t = u; |
| 4585 | if (t != u) { |
| 4586 | PyErr_SetString(PyExc_OverflowError, |
| 4587 | "timestamp out of range for platform time_t"); |
| 4588 | return -1; |
| 4589 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4590 | if (_PyTime_localtime(t, &local_time) != 0) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4591 | return -1; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4592 | return utc_to_seconds(local_time.tm_year + 1900, |
| 4593 | local_time.tm_mon + 1, |
| 4594 | local_time.tm_mday, |
| 4595 | local_time.tm_hour, |
| 4596 | local_time.tm_min, |
| 4597 | local_time.tm_sec); |
| 4598 | } |
| 4599 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4600 | /* Internal helper. |
| 4601 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4602 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4603 | */ |
| 4604 | static PyObject * |
| 4605 | 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] | 4606 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4607 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4608 | struct tm tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4609 | int year, month, day, hour, minute, second, fold = 0; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4610 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4611 | if (f(timet, &tm) != 0) |
| 4612 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4613 | |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4614 | year = tm.tm_year + 1900; |
| 4615 | month = tm.tm_mon + 1; |
| 4616 | day = tm.tm_mday; |
| 4617 | hour = tm.tm_hour; |
| 4618 | minute = tm.tm_min; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4619 | /* The platform localtime/gmtime may insert leap seconds, |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4620 | * indicated by tm.tm_sec > 59. We don't care about them, |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4621 | * except to the extent that passing them on to the datetime |
| 4622 | * constructor would raise ValueError for a reason that |
| 4623 | * made no sense to the user. |
| 4624 | */ |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4625 | second = Py_MIN(59, tm.tm_sec); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4626 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4627 | /* local timezone requires to compute fold */ |
Ammar Askar | 96d1e69 | 2018-07-25 09:54:58 -0700 | [diff] [blame^] | 4628 | if (tzinfo == Py_None && f == _PyTime_localtime |
| 4629 | /* On Windows, passing a negative value to local results |
| 4630 | * in an OSError because localtime_s on Windows does |
| 4631 | * not support negative timestamps. Unfortunately this |
| 4632 | * means that fold detection for time values between |
| 4633 | * 0 and max_fold_seconds will result in an identical |
| 4634 | * error since we subtract max_fold_seconds to detect a |
| 4635 | * fold. However, since we know there haven't been any |
| 4636 | * folds in the interval [0, max_fold_seconds) in any |
| 4637 | * timezone, we can hackily just forego fold detection |
| 4638 | * for this time range. |
| 4639 | */ |
| 4640 | #ifdef MS_WINDOWS |
| 4641 | && (timet - max_fold_seconds > 0) |
| 4642 | #endif |
| 4643 | ) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4644 | long long probe_seconds, result_seconds, transition; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4645 | |
| 4646 | result_seconds = utc_to_seconds(year, month, day, |
| 4647 | hour, minute, second); |
| 4648 | /* Probe max_fold_seconds to detect a fold. */ |
| 4649 | probe_seconds = local(epoch + timet - max_fold_seconds); |
| 4650 | if (probe_seconds == -1) |
| 4651 | return NULL; |
| 4652 | transition = result_seconds - probe_seconds - max_fold_seconds; |
| 4653 | if (transition < 0) { |
| 4654 | probe_seconds = local(epoch + timet + transition); |
| 4655 | if (probe_seconds == -1) |
| 4656 | return NULL; |
| 4657 | if (probe_seconds == result_seconds) |
| 4658 | fold = 1; |
| 4659 | } |
| 4660 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 4661 | return new_datetime_subclass_fold_ex(year, month, day, hour, minute, |
| 4662 | second, us, tzinfo, fold, cls); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | /* Internal helper. |
| 4666 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4667 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4668 | * have enough bits to cover a datetime's full range of precision, it's |
| 4669 | * better to call datetime_from_timet_and_us provided you have a way |
| 4670 | * to get that much precision (e.g., C time() isn't good enough). |
| 4671 | */ |
| 4672 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4673 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4677 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4678 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 4679 | if (_PyTime_ObjectToTimeval(timestamp, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 4680 | &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | return NULL; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4682 | |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4683 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | /* Internal helper. |
| 4687 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4688 | * gmtime for f as appropriate. |
| 4689 | */ |
| 4690 | static PyObject * |
| 4691 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4692 | { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4693 | _PyTime_t ts = _PyTime_GetSystemClock(); |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4694 | time_t secs; |
| 4695 | int us; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4696 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4697 | if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4698 | return NULL; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4699 | assert(0 <= us && us <= 999999); |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4700 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4701 | return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4704 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4705 | |
| 4706 | @classmethod |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4707 | datetime.datetime.now |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4708 | |
| 4709 | tz: object = None |
| 4710 | Timezone object. |
| 4711 | |
| 4712 | Returns new datetime object representing current time local to tz. |
| 4713 | |
| 4714 | If no tz is specified, uses local timezone. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4715 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4716 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4717 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4718 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4719 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4722 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4723 | /* Return best possible local time -- this isn't constrained by the |
| 4724 | * precision of a timestamp. |
| 4725 | */ |
| 4726 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4727 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4728 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4729 | self = datetime_best_possible((PyObject *)type, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4730 | tz == Py_None ? _PyTime_localtime : |
| 4731 | _PyTime_gmtime, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4732 | tz); |
| 4733 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4734 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4735 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4736 | } |
| 4737 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4740 | /* Return best possible UTC time -- this isn't constrained by the |
| 4741 | * precision of a timestamp. |
| 4742 | */ |
| 4743 | static PyObject * |
| 4744 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4745 | { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4746 | return datetime_best_possible(cls, _PyTime_gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4749 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4750 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4751 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4753 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4754 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4755 | PyObject *tzinfo = Py_None; |
| 4756 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4757 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4758 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | keywords, ×tamp, &tzinfo)) |
| 4760 | return NULL; |
| 4761 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4762 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | self = datetime_from_timestamp(cls, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4765 | tzinfo == Py_None ? _PyTime_localtime : |
| 4766 | _PyTime_gmtime, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | timestamp, |
| 4768 | tzinfo); |
| 4769 | if (self != NULL && tzinfo != Py_None) { |
| 4770 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4771 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4772 | } |
| 4773 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4776 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 4777 | static PyObject * |
| 4778 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 4779 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4780 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4781 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4782 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4783 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4784 | result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | Py_None); |
| 4786 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4789 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4790 | static PyObject * |
| 4791 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4794 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4795 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4796 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4797 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4798 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4799 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4800 | if (module == NULL) { |
| 4801 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4802 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4803 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | } |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 4805 | return _PyObject_CallMethodIdObjArgs(module, &PyId__strptime_datetime, |
| 4806 | cls, string, format, NULL); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4809 | /* Return new datetime from date/datetime and time arguments. */ |
| 4810 | static PyObject * |
| 4811 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4812 | { |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4813 | static char *keywords[] = {"date", "time", "tzinfo", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4814 | PyObject *date; |
| 4815 | PyObject *time; |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4816 | PyObject *tzinfo = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4817 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4818 | |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4819 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4820 | &PyDateTime_DateType, &date, |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4821 | &PyDateTime_TimeType, &time, &tzinfo)) { |
| 4822 | if (tzinfo == NULL) { |
| 4823 | if (HASTZINFO(time)) |
| 4824 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4825 | else |
| 4826 | tzinfo = Py_None; |
| 4827 | } |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 4828 | result = new_datetime_subclass_fold_ex(GET_YEAR(date), |
| 4829 | GET_MONTH(date), |
| 4830 | GET_DAY(date), |
| 4831 | TIME_GET_HOUR(time), |
| 4832 | TIME_GET_MINUTE(time), |
| 4833 | TIME_GET_SECOND(time), |
| 4834 | TIME_GET_MICROSECOND(time), |
| 4835 | tzinfo, |
| 4836 | TIME_GET_FOLD(time), |
| 4837 | cls); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | } |
| 4839 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4840 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4841 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4842 | static PyObject * |
| 4843 | datetime_fromisoformat(PyObject* cls, PyObject *dtstr) { |
| 4844 | assert(dtstr != NULL); |
| 4845 | |
| 4846 | if (!PyUnicode_Check(dtstr)) { |
| 4847 | PyErr_SetString(PyExc_TypeError, "fromisoformat: argument must be str"); |
| 4848 | return NULL; |
| 4849 | } |
| 4850 | |
| 4851 | Py_ssize_t len; |
| 4852 | const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len); |
| 4853 | const char * p = dt_ptr; |
| 4854 | |
| 4855 | int year = 0, month = 0, day = 0; |
| 4856 | int hour = 0, minute = 0, second = 0, microsecond = 0; |
| 4857 | int tzoffset = 0, tzusec = 0; |
| 4858 | |
| 4859 | // date has a fixed length of 10 |
| 4860 | int rv = parse_isoformat_date(p, &year, &month, &day); |
| 4861 | |
| 4862 | if (!rv && len > 10) { |
| 4863 | // In UTF-8, the length of multi-byte characters is encoded in the MSB |
| 4864 | if ((p[10] & 0x80) == 0) { |
| 4865 | p += 11; |
| 4866 | } else { |
| 4867 | switch(p[10] & 0xf0) { |
| 4868 | case 0xe0: |
| 4869 | p += 13; |
| 4870 | break; |
| 4871 | case 0xf0: |
| 4872 | p += 14; |
| 4873 | break; |
| 4874 | default: |
| 4875 | p += 12; |
| 4876 | break; |
| 4877 | } |
| 4878 | } |
| 4879 | |
| 4880 | len -= (p - dt_ptr); |
| 4881 | rv = parse_isoformat_time(p, len, |
| 4882 | &hour, &minute, &second, µsecond, |
| 4883 | &tzoffset, &tzusec); |
| 4884 | } |
| 4885 | if (rv < 0) { |
| 4886 | PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %s", dt_ptr); |
| 4887 | return NULL; |
| 4888 | } |
| 4889 | |
| 4890 | PyObject* tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, tzusec); |
| 4891 | if (tzinfo == NULL) { |
| 4892 | return NULL; |
| 4893 | } |
| 4894 | |
Paul Ganssle | 9f1b7b9 | 2018-01-16 13:06:31 -0500 | [diff] [blame] | 4895 | PyObject *dt = new_datetime_subclass_ex(year, month, day, hour, minute, |
| 4896 | second, microsecond, tzinfo, cls); |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 4897 | |
| 4898 | Py_DECREF(tzinfo); |
| 4899 | return dt; |
| 4900 | } |
| 4901 | |
| 4902 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4903 | /* |
| 4904 | * Destructor. |
| 4905 | */ |
| 4906 | |
| 4907 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4908 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | if (HASTZINFO(self)) { |
| 4911 | Py_XDECREF(self->tzinfo); |
| 4912 | } |
| 4913 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | /* |
| 4917 | * Indirect access to tzinfo methods. |
| 4918 | */ |
| 4919 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4920 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4921 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4922 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 4923 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4927 | datetime_dst(PyObject *self, PyObject *unused) { |
| 4928 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4932 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 4933 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4937 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4938 | */ |
| 4939 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4940 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4941 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4942 | */ |
| 4943 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4944 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4945 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | /* Note that the C-level additions can't overflow, because of |
| 4948 | * invariant bounds on the member values. |
| 4949 | */ |
| 4950 | int year = GET_YEAR(date); |
| 4951 | int month = GET_MONTH(date); |
| 4952 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4953 | int hour = DATE_GET_HOUR(date); |
| 4954 | int minute = DATE_GET_MINUTE(date); |
| 4955 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4956 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4957 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4959 | assert(factor == 1 || factor == -1); |
| 4960 | if (normalize_datetime(&year, &month, &day, |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4961 | &hour, &minute, &second, µsecond) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4962 | return NULL; |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | return new_datetime(year, month, day, |
| 4966 | hour, minute, second, microsecond, |
| 4967 | HASTZINFO(date) ? date->tzinfo : Py_None, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4971 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4973 | if (PyDateTime_Check(left)) { |
| 4974 | /* datetime + ??? */ |
| 4975 | if (PyDelta_Check(right)) |
| 4976 | /* datetime + delta */ |
| 4977 | return add_datetime_timedelta( |
| 4978 | (PyDateTime_DateTime *)left, |
| 4979 | (PyDateTime_Delta *)right, |
| 4980 | 1); |
| 4981 | } |
| 4982 | else if (PyDelta_Check(left)) { |
| 4983 | /* delta + datetime */ |
| 4984 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4985 | (PyDateTime_Delta *) left, |
| 4986 | 1); |
| 4987 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4988 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
| 4991 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4992 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4993 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4994 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | if (PyDateTime_Check(left)) { |
| 4997 | /* datetime - ??? */ |
| 4998 | if (PyDateTime_Check(right)) { |
| 4999 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5000 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5001 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5002 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5003 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 5004 | offset2 = offset1 = Py_None; |
| 5005 | Py_INCREF(offset1); |
| 5006 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5007 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5008 | else { |
| 5009 | offset1 = datetime_utcoffset(left, NULL); |
| 5010 | if (offset1 == NULL) |
| 5011 | return NULL; |
| 5012 | offset2 = datetime_utcoffset(right, NULL); |
| 5013 | if (offset2 == NULL) { |
| 5014 | Py_DECREF(offset1); |
| 5015 | return NULL; |
| 5016 | } |
| 5017 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 5018 | PyErr_SetString(PyExc_TypeError, |
| 5019 | "can't subtract offset-naive and " |
| 5020 | "offset-aware datetimes"); |
| 5021 | Py_DECREF(offset1); |
| 5022 | Py_DECREF(offset2); |
| 5023 | return NULL; |
| 5024 | } |
| 5025 | } |
| 5026 | if ((offset1 != offset2) && |
| 5027 | delta_cmp(offset1, offset2) != 0) { |
| 5028 | offdiff = delta_subtract(offset1, offset2); |
| 5029 | if (offdiff == NULL) { |
| 5030 | Py_DECREF(offset1); |
| 5031 | Py_DECREF(offset2); |
| 5032 | return NULL; |
| 5033 | } |
| 5034 | } |
| 5035 | Py_DECREF(offset1); |
| 5036 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5037 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 5038 | GET_MONTH(left), |
| 5039 | GET_DAY(left)) - |
| 5040 | ymd_to_ord(GET_YEAR(right), |
| 5041 | GET_MONTH(right), |
| 5042 | GET_DAY(right)); |
| 5043 | /* These can't overflow, since the values are |
| 5044 | * normalized. At most this gives the number of |
| 5045 | * seconds in one day. |
| 5046 | */ |
| 5047 | delta_s = (DATE_GET_HOUR(left) - |
| 5048 | DATE_GET_HOUR(right)) * 3600 + |
| 5049 | (DATE_GET_MINUTE(left) - |
| 5050 | DATE_GET_MINUTE(right)) * 60 + |
| 5051 | (DATE_GET_SECOND(left) - |
| 5052 | DATE_GET_SECOND(right)); |
| 5053 | delta_us = DATE_GET_MICROSECOND(left) - |
| 5054 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5055 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 5056 | if (result == NULL) |
| 5057 | return NULL; |
| 5058 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5059 | if (offdiff != NULL) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 5060 | Py_SETREF(result, delta_subtract(result, offdiff)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5061 | Py_DECREF(offdiff); |
| 5062 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | } |
| 5064 | else if (PyDelta_Check(right)) { |
| 5065 | /* datetime - delta */ |
| 5066 | result = add_datetime_timedelta( |
| 5067 | (PyDateTime_DateTime *)left, |
| 5068 | (PyDateTime_Delta *)right, |
| 5069 | -1); |
| 5070 | } |
| 5071 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5073 | if (result == Py_NotImplemented) |
| 5074 | Py_INCREF(result); |
| 5075 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | /* Various ways to turn a datetime into a string. */ |
| 5079 | |
| 5080 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5081 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5082 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | const char *type_name = Py_TYPE(self)->tp_name; |
| 5084 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5086 | if (DATE_GET_MICROSECOND(self)) { |
| 5087 | baserepr = PyUnicode_FromFormat( |
| 5088 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 5089 | type_name, |
| 5090 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5091 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5092 | DATE_GET_SECOND(self), |
| 5093 | DATE_GET_MICROSECOND(self)); |
| 5094 | } |
| 5095 | else if (DATE_GET_SECOND(self)) { |
| 5096 | baserepr = PyUnicode_FromFormat( |
| 5097 | "%s(%d, %d, %d, %d, %d, %d)", |
| 5098 | type_name, |
| 5099 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5100 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5101 | DATE_GET_SECOND(self)); |
| 5102 | } |
| 5103 | else { |
| 5104 | baserepr = PyUnicode_FromFormat( |
| 5105 | "%s(%d, %d, %d, %d, %d)", |
| 5106 | type_name, |
| 5107 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 5108 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 5109 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5110 | if (baserepr != NULL && DATE_GET_FOLD(self) != 0) |
| 5111 | baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5112 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 5113 | return baserepr; |
| 5114 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5117 | static PyObject * |
| 5118 | datetime_str(PyDateTime_DateTime *self) |
| 5119 | { |
Victor Stinner | 4c38154 | 2016-12-09 00:33:39 +0100 | [diff] [blame] | 5120 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5121 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5122 | |
| 5123 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5124 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | int sep = 'T'; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5127 | char *timespec = NULL; |
| 5128 | static char *keywords[] = {"sep", "timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5129 | char buffer[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5130 | PyObject *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5131 | int us = DATE_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5132 | static char *specs[][2] = { |
| 5133 | {"hours", "%04d-%02d-%02d%c%02d"}, |
| 5134 | {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, |
| 5135 | {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, |
| 5136 | {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, |
| 5137 | {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, |
| 5138 | }; |
| 5139 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5140 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5141 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5142 | return NULL; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5143 | |
| 5144 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 5145 | if (us == 0) { |
| 5146 | /* seconds */ |
| 5147 | given_spec = 2; |
| 5148 | } |
| 5149 | else { |
| 5150 | /* microseconds */ |
| 5151 | given_spec = 4; |
| 5152 | } |
| 5153 | } |
| 5154 | else { |
| 5155 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 5156 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 5157 | if (given_spec == 3) { |
| 5158 | us = us / 1000; |
| 5159 | } |
| 5160 | break; |
| 5161 | } |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 5166 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 5167 | return NULL; |
| 5168 | } |
| 5169 | else { |
| 5170 | result = PyUnicode_FromFormat(specs[given_spec][1], |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | GET_YEAR(self), GET_MONTH(self), |
| 5172 | GET_DAY(self), (int)sep, |
| 5173 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 5174 | DATE_GET_SECOND(self), us); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5175 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 5176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5177 | if (!result || !HASTZINFO(self)) |
| 5178 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | /* We need to append the UTC offset. */ |
| 5181 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 5182 | (PyObject *)self) < 0) { |
| 5183 | Py_DECREF(result); |
| 5184 | return NULL; |
| 5185 | } |
| 5186 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 5187 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5190 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5191 | datetime_ctime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | return format_ctime((PyDateTime_Date *)self, |
| 5194 | DATE_GET_HOUR(self), |
| 5195 | DATE_GET_MINUTE(self), |
| 5196 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5199 | /* Miscellaneous methods. */ |
| 5200 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5201 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5202 | flip_fold(PyObject *dt) |
| 5203 | { |
| 5204 | return new_datetime_ex2(GET_YEAR(dt), |
| 5205 | GET_MONTH(dt), |
| 5206 | GET_DAY(dt), |
| 5207 | DATE_GET_HOUR(dt), |
| 5208 | DATE_GET_MINUTE(dt), |
| 5209 | DATE_GET_SECOND(dt), |
| 5210 | DATE_GET_MICROSECOND(dt), |
| 5211 | HASTZINFO(dt) ? |
| 5212 | ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, |
| 5213 | !DATE_GET_FOLD(dt), |
| 5214 | Py_TYPE(dt)); |
| 5215 | } |
| 5216 | |
| 5217 | static PyObject * |
| 5218 | get_flip_fold_offset(PyObject *dt) |
| 5219 | { |
| 5220 | PyObject *result, *flip_dt; |
| 5221 | |
| 5222 | flip_dt = flip_fold(dt); |
| 5223 | if (flip_dt == NULL) |
| 5224 | return NULL; |
| 5225 | result = datetime_utcoffset(flip_dt, NULL); |
| 5226 | Py_DECREF(flip_dt); |
| 5227 | return result; |
| 5228 | } |
| 5229 | |
| 5230 | /* PEP 495 exception: Whenever one or both of the operands in |
| 5231 | * inter-zone comparison is such that its utcoffset() depends |
Serhiy Storchaka | bac2d5b | 2018-03-28 22:14:26 +0300 | [diff] [blame] | 5232 | * on the value of its fold attribute, the result is False. |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5233 | * |
| 5234 | * Return 1 if exception applies, 0 if not, and -1 on error. |
| 5235 | */ |
| 5236 | static int |
| 5237 | pep495_eq_exception(PyObject *self, PyObject *other, |
| 5238 | PyObject *offset_self, PyObject *offset_other) |
| 5239 | { |
| 5240 | int result = 0; |
| 5241 | PyObject *flip_offset; |
| 5242 | |
| 5243 | flip_offset = get_flip_fold_offset(self); |
| 5244 | if (flip_offset == NULL) |
| 5245 | return -1; |
| 5246 | if (flip_offset != offset_self && |
| 5247 | delta_cmp(flip_offset, offset_self)) |
| 5248 | { |
| 5249 | result = 1; |
| 5250 | goto done; |
| 5251 | } |
| 5252 | Py_DECREF(flip_offset); |
| 5253 | |
| 5254 | flip_offset = get_flip_fold_offset(other); |
| 5255 | if (flip_offset == NULL) |
| 5256 | return -1; |
| 5257 | if (flip_offset != offset_other && |
| 5258 | delta_cmp(flip_offset, offset_other)) |
| 5259 | result = 1; |
| 5260 | done: |
| 5261 | Py_DECREF(flip_offset); |
| 5262 | return result; |
| 5263 | } |
| 5264 | |
| 5265 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 5266 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5267 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5268 | PyObject *result = NULL; |
| 5269 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5270 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5272 | if (! PyDateTime_Check(other)) { |
| 5273 | if (PyDate_Check(other)) { |
| 5274 | /* Prevent invocation of date_richcompare. We want to |
| 5275 | return NotImplemented here to give the other object |
| 5276 | a chance. But since DateTime is a subclass of |
| 5277 | Date, if the other object is a Date, it would |
| 5278 | compute an ordering based on the date part alone, |
| 5279 | and we don't want that. So force unequal or |
| 5280 | uncomparable here in that case. */ |
| 5281 | if (op == Py_EQ) |
| 5282 | Py_RETURN_FALSE; |
| 5283 | if (op == Py_NE) |
| 5284 | Py_RETURN_TRUE; |
| 5285 | return cmperror(self, other); |
| 5286 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 5287 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5288 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5289 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5290 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5291 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5292 | ((PyDateTime_DateTime *)other)->data, |
| 5293 | _PyDateTime_DATETIME_DATASIZE); |
| 5294 | return diff_to_bool(diff, op); |
| 5295 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5296 | offset1 = datetime_utcoffset(self, NULL); |
| 5297 | if (offset1 == NULL) |
| 5298 | return NULL; |
| 5299 | offset2 = datetime_utcoffset(other, NULL); |
| 5300 | if (offset2 == NULL) |
| 5301 | goto done; |
| 5302 | /* If they're both naive, or both aware and have the same offsets, |
| 5303 | * we get off cheap. Note that if they're both naive, offset1 == |
| 5304 | * offset2 == Py_None at this point. |
| 5305 | */ |
| 5306 | if ((offset1 == offset2) || |
| 5307 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 5308 | delta_cmp(offset1, offset2) == 0)) { |
| 5309 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 5310 | ((PyDateTime_DateTime *)other)->data, |
| 5311 | _PyDateTime_DATETIME_DATASIZE); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5312 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5313 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5314 | if (ex == -1) |
| 5315 | goto done; |
| 5316 | if (ex) |
| 5317 | diff = 1; |
| 5318 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5319 | result = diff_to_bool(diff, op); |
| 5320 | } |
| 5321 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5322 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5323 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5324 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5325 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 5326 | other); |
| 5327 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5328 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5329 | diff = GET_TD_DAYS(delta); |
| 5330 | if (diff == 0) |
| 5331 | diff = GET_TD_SECONDS(delta) | |
| 5332 | GET_TD_MICROSECONDS(delta); |
| 5333 | Py_DECREF(delta); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5334 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 5335 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 5336 | if (ex == -1) |
| 5337 | goto done; |
| 5338 | if (ex) |
| 5339 | diff = 1; |
| 5340 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5341 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5342 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 5343 | else if (op == Py_EQ) { |
| 5344 | result = Py_False; |
| 5345 | Py_INCREF(result); |
| 5346 | } |
| 5347 | else if (op == Py_NE) { |
| 5348 | result = Py_True; |
| 5349 | Py_INCREF(result); |
| 5350 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5351 | else { |
| 5352 | PyErr_SetString(PyExc_TypeError, |
| 5353 | "can't compare offset-naive and " |
| 5354 | "offset-aware datetimes"); |
| 5355 | } |
| 5356 | done: |
| 5357 | Py_DECREF(offset1); |
| 5358 | Py_XDECREF(offset2); |
| 5359 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 5362 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5363 | datetime_hash(PyDateTime_DateTime *self) |
| 5364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5365 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5366 | PyObject *offset, *self0; |
| 5367 | if (DATE_GET_FOLD(self)) { |
| 5368 | self0 = new_datetime_ex2(GET_YEAR(self), |
| 5369 | GET_MONTH(self), |
| 5370 | GET_DAY(self), |
| 5371 | DATE_GET_HOUR(self), |
| 5372 | DATE_GET_MINUTE(self), |
| 5373 | DATE_GET_SECOND(self), |
| 5374 | DATE_GET_MICROSECOND(self), |
| 5375 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 5376 | 0, Py_TYPE(self)); |
| 5377 | if (self0 == NULL) |
| 5378 | return -1; |
| 5379 | } |
| 5380 | else { |
| 5381 | self0 = (PyObject *)self; |
| 5382 | Py_INCREF(self0); |
| 5383 | } |
| 5384 | offset = datetime_utcoffset(self0, NULL); |
| 5385 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5386 | |
| 5387 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5388 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5391 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | self->hashcode = generic_hash( |
| 5393 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5395 | PyObject *temp1, *temp2; |
| 5396 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | assert(HASTZINFO(self)); |
| 5399 | days = ymd_to_ord(GET_YEAR(self), |
| 5400 | GET_MONTH(self), |
| 5401 | GET_DAY(self)); |
| 5402 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5403 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5404 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5405 | temp1 = new_delta(days, seconds, |
| 5406 | DATE_GET_MICROSECOND(self), |
| 5407 | 1); |
| 5408 | if (temp1 == NULL) { |
| 5409 | Py_DECREF(offset); |
| 5410 | return -1; |
| 5411 | } |
| 5412 | temp2 = delta_subtract(temp1, offset); |
| 5413 | Py_DECREF(temp1); |
| 5414 | if (temp2 == NULL) { |
| 5415 | Py_DECREF(offset); |
| 5416 | return -1; |
| 5417 | } |
| 5418 | self->hashcode = PyObject_Hash(temp2); |
| 5419 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5421 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5422 | } |
| 5423 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5424 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5425 | |
| 5426 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5427 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5429 | PyObject *clone; |
| 5430 | PyObject *tuple; |
| 5431 | int y = GET_YEAR(self); |
| 5432 | int m = GET_MONTH(self); |
| 5433 | int d = GET_DAY(self); |
| 5434 | int hh = DATE_GET_HOUR(self); |
| 5435 | int mm = DATE_GET_MINUTE(self); |
| 5436 | int ss = DATE_GET_SECOND(self); |
| 5437 | int us = DATE_GET_MICROSECOND(self); |
| 5438 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5439 | int fold = DATE_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5440 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5441 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | datetime_kws, |
| 5443 | &y, &m, &d, &hh, &mm, &ss, &us, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5444 | &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 5446 | if (fold != 0 && fold != 1) { |
| 5447 | PyErr_SetString(PyExc_ValueError, |
| 5448 | "fold must be either 0 or 1"); |
| 5449 | return NULL; |
| 5450 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5451 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 5452 | if (tuple == NULL) |
| 5453 | return NULL; |
| 5454 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5455 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5456 | DATE_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5457 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5458 | Py_DECREF(tuple); |
| 5459 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5463 | local_timezone_from_timestamp(time_t timestamp) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5464 | { |
| 5465 | PyObject *result = NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5466 | PyObject *delta; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5467 | struct tm local_time_tm; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5468 | PyObject *nameo = NULL; |
| 5469 | const char *zone = NULL; |
| 5470 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5471 | if (_PyTime_localtime(timestamp, &local_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5472 | return NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5473 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5474 | zone = local_time_tm.tm_zone; |
| 5475 | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5476 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 5477 | { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5478 | PyObject *local_time, *utc_time; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5479 | struct tm utc_time_tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5480 | char buf[100]; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5481 | strftime(buf, sizeof(buf), "%Z", &local_time_tm); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5482 | zone = buf; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5483 | local_time = new_datetime(local_time_tm.tm_year + 1900, |
| 5484 | local_time_tm.tm_mon + 1, |
| 5485 | local_time_tm.tm_mday, |
| 5486 | local_time_tm.tm_hour, |
| 5487 | local_time_tm.tm_min, |
| 5488 | local_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5489 | if (local_time == NULL) { |
| 5490 | return NULL; |
| 5491 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5492 | if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5493 | return NULL; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5494 | utc_time = new_datetime(utc_time_tm.tm_year + 1900, |
| 5495 | utc_time_tm.tm_mon + 1, |
| 5496 | utc_time_tm.tm_mday, |
| 5497 | utc_time_tm.tm_hour, |
| 5498 | utc_time_tm.tm_min, |
| 5499 | utc_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5500 | if (utc_time == NULL) { |
| 5501 | Py_DECREF(local_time); |
| 5502 | return NULL; |
| 5503 | } |
| 5504 | delta = datetime_subtract(local_time, utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5505 | Py_DECREF(local_time); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5506 | Py_DECREF(utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5507 | } |
| 5508 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5509 | if (delta == NULL) { |
| 5510 | return NULL; |
| 5511 | } |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5512 | if (zone != NULL) { |
| 5513 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 5514 | if (nameo == NULL) |
| 5515 | goto error; |
| 5516 | } |
| 5517 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 5518 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5519 | error: |
| 5520 | Py_DECREF(delta); |
| 5521 | return result; |
| 5522 | } |
| 5523 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5524 | static PyObject * |
| 5525 | local_timezone(PyDateTime_DateTime *utc_time) |
| 5526 | { |
| 5527 | time_t timestamp; |
| 5528 | PyObject *delta; |
| 5529 | PyObject *one_second; |
| 5530 | PyObject *seconds; |
| 5531 | |
| 5532 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 5533 | if (delta == NULL) |
| 5534 | return NULL; |
| 5535 | one_second = new_delta(0, 1, 0, 0); |
| 5536 | if (one_second == NULL) { |
| 5537 | Py_DECREF(delta); |
| 5538 | return NULL; |
| 5539 | } |
| 5540 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 5541 | (PyDateTime_Delta *)one_second); |
| 5542 | Py_DECREF(one_second); |
| 5543 | Py_DECREF(delta); |
| 5544 | if (seconds == NULL) |
| 5545 | return NULL; |
| 5546 | timestamp = _PyLong_AsTime_t(seconds); |
| 5547 | Py_DECREF(seconds); |
| 5548 | if (timestamp == -1 && PyErr_Occurred()) |
| 5549 | return NULL; |
| 5550 | return local_timezone_from_timestamp(timestamp); |
| 5551 | } |
| 5552 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5553 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5554 | local_to_seconds(int year, int month, int day, |
| 5555 | int hour, int minute, int second, int fold); |
| 5556 | |
| 5557 | static PyObject * |
| 5558 | local_timezone_from_local(PyDateTime_DateTime *local_dt) |
| 5559 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5560 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5561 | time_t timestamp; |
| 5562 | seconds = local_to_seconds(GET_YEAR(local_dt), |
| 5563 | GET_MONTH(local_dt), |
| 5564 | GET_DAY(local_dt), |
| 5565 | DATE_GET_HOUR(local_dt), |
| 5566 | DATE_GET_MINUTE(local_dt), |
| 5567 | DATE_GET_SECOND(local_dt), |
| 5568 | DATE_GET_FOLD(local_dt)); |
| 5569 | if (seconds == -1) |
| 5570 | return NULL; |
| 5571 | /* XXX: add bounds check */ |
| 5572 | timestamp = seconds - epoch; |
| 5573 | return local_timezone_from_timestamp(timestamp); |
| 5574 | } |
| 5575 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5576 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5577 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5578 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5579 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5580 | PyObject *offset; |
| 5581 | PyObject *temp; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5582 | PyObject *self_tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5583 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5584 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5585 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5586 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 5587 | &tzinfo)) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5588 | return NULL; |
| 5589 | |
| 5590 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5591 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5592 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5593 | if (!HASTZINFO(self) || self->tzinfo == Py_None) { |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 5594 | naive: |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5595 | self_tzinfo = local_timezone_from_local(self); |
| 5596 | if (self_tzinfo == NULL) |
| 5597 | return NULL; |
| 5598 | } else { |
| 5599 | self_tzinfo = self->tzinfo; |
| 5600 | Py_INCREF(self_tzinfo); |
| 5601 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5603 | /* Conversion to self's own time zone is a NOP. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5604 | if (self_tzinfo == tzinfo) { |
| 5605 | Py_DECREF(self_tzinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5606 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5607 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5608 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5610 | /* Convert self to UTC. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5611 | offset = call_utcoffset(self_tzinfo, (PyObject *)self); |
| 5612 | Py_DECREF(self_tzinfo); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5613 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5614 | return NULL; |
Alexander Belopolsky | 877b232 | 2018-06-10 17:02:58 -0400 | [diff] [blame] | 5615 | else if(offset == Py_None) { |
| 5616 | Py_DECREF(offset); |
| 5617 | goto naive; |
| 5618 | } |
| 5619 | else if (!PyDelta_Check(offset)) { |
| 5620 | Py_DECREF(offset); |
| 5621 | PyErr_Format(PyExc_TypeError, "utcoffset() returned %.200s," |
| 5622 | " expected timedelta or None", Py_TYPE(offset)->tp_name); |
| 5623 | return NULL; |
| 5624 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5625 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5626 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5627 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5628 | Py_DECREF(offset); |
| 5629 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5631 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5632 | /* Make sure result is aware and UTC. */ |
| 5633 | if (!HASTZINFO(result)) { |
| 5634 | temp = (PyObject *)result; |
| 5635 | result = (PyDateTime_DateTime *) |
| 5636 | new_datetime_ex2(GET_YEAR(result), |
| 5637 | GET_MONTH(result), |
| 5638 | GET_DAY(result), |
| 5639 | DATE_GET_HOUR(result), |
| 5640 | DATE_GET_MINUTE(result), |
| 5641 | DATE_GET_SECOND(result), |
| 5642 | DATE_GET_MICROSECOND(result), |
| 5643 | PyDateTime_TimeZone_UTC, |
| 5644 | DATE_GET_FOLD(result), |
| 5645 | Py_TYPE(result)); |
| 5646 | Py_DECREF(temp); |
| 5647 | if (result == NULL) |
| 5648 | return NULL; |
| 5649 | } |
| 5650 | else { |
| 5651 | /* Result is already aware - just replace tzinfo. */ |
| 5652 | temp = result->tzinfo; |
| 5653 | result->tzinfo = PyDateTime_TimeZone_UTC; |
| 5654 | Py_INCREF(result->tzinfo); |
| 5655 | Py_DECREF(temp); |
| 5656 | } |
| 5657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5658 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5659 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5660 | if (tzinfo == Py_None) { |
| 5661 | tzinfo = local_timezone(result); |
| 5662 | if (tzinfo == NULL) { |
| 5663 | Py_DECREF(result); |
| 5664 | return NULL; |
| 5665 | } |
| 5666 | } |
| 5667 | else |
| 5668 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5669 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5670 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5671 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5672 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5673 | result = (PyDateTime_DateTime *) |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 5674 | _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_fromutc, temp, NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5675 | Py_DECREF(temp); |
| 5676 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5677 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5678 | } |
| 5679 | |
| 5680 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5681 | datetime_timetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5682 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5685 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5686 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5687 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5688 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 5689 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5690 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5691 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5692 | if (dst != Py_None) |
| 5693 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 5694 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5695 | } |
| 5696 | return build_struct_time(GET_YEAR(self), |
| 5697 | GET_MONTH(self), |
| 5698 | GET_DAY(self), |
| 5699 | DATE_GET_HOUR(self), |
| 5700 | DATE_GET_MINUTE(self), |
| 5701 | DATE_GET_SECOND(self), |
| 5702 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5705 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5706 | local_to_seconds(int year, int month, int day, |
| 5707 | int hour, int minute, int second, int fold) |
| 5708 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5709 | long long t, a, b, u1, u2, t1, t2, lt; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5710 | t = utc_to_seconds(year, month, day, hour, minute, second); |
| 5711 | /* Our goal is to solve t = local(u) for u. */ |
| 5712 | lt = local(t); |
| 5713 | if (lt == -1) |
| 5714 | return -1; |
| 5715 | a = lt - t; |
| 5716 | u1 = t - a; |
| 5717 | t1 = local(u1); |
| 5718 | if (t1 == -1) |
| 5719 | return -1; |
| 5720 | if (t1 == t) { |
| 5721 | /* We found one solution, but it may not be the one we need. |
| 5722 | * Look for an earlier solution (if `fold` is 0), or a |
| 5723 | * later one (if `fold` is 1). */ |
| 5724 | if (fold) |
| 5725 | u2 = u1 + max_fold_seconds; |
| 5726 | else |
| 5727 | u2 = u1 - max_fold_seconds; |
| 5728 | lt = local(u2); |
| 5729 | if (lt == -1) |
| 5730 | return -1; |
| 5731 | b = lt - u2; |
| 5732 | if (a == b) |
| 5733 | return u1; |
| 5734 | } |
| 5735 | else { |
| 5736 | b = t1 - u1; |
| 5737 | assert(a != b); |
| 5738 | } |
| 5739 | u2 = t - b; |
| 5740 | t2 = local(u2); |
| 5741 | if (t2 == -1) |
| 5742 | return -1; |
| 5743 | if (t2 == t) |
| 5744 | return u2; |
| 5745 | if (t1 == t) |
| 5746 | return u1; |
| 5747 | /* We have found both offsets a and b, but neither t - a nor t - b is |
| 5748 | * a solution. This means t is in the gap. */ |
| 5749 | return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); |
| 5750 | } |
| 5751 | |
| 5752 | /* date(1970,1,1).toordinal() == 719163 */ |
| 5753 | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) |
| 5754 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5755 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5756 | datetime_timestamp(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5757 | { |
| 5758 | PyObject *result; |
| 5759 | |
| 5760 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 5761 | PyObject *delta; |
| 5762 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 5763 | if (delta == NULL) |
| 5764 | return NULL; |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5765 | result = delta_total_seconds(delta, NULL); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5766 | Py_DECREF(delta); |
| 5767 | } |
| 5768 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5769 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5770 | seconds = local_to_seconds(GET_YEAR(self), |
| 5771 | GET_MONTH(self), |
| 5772 | GET_DAY(self), |
| 5773 | DATE_GET_HOUR(self), |
| 5774 | DATE_GET_MINUTE(self), |
| 5775 | DATE_GET_SECOND(self), |
| 5776 | DATE_GET_FOLD(self)); |
| 5777 | if (seconds == -1) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5778 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5779 | result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + |
| 5780 | DATE_GET_MICROSECOND(self) / 1e6); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5781 | } |
| 5782 | return result; |
| 5783 | } |
| 5784 | |
| 5785 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5786 | datetime_getdate(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5788 | return new_date(GET_YEAR(self), |
| 5789 | GET_MONTH(self), |
| 5790 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5791 | } |
| 5792 | |
| 5793 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5794 | datetime_gettime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5796 | return new_time(DATE_GET_HOUR(self), |
| 5797 | DATE_GET_MINUTE(self), |
| 5798 | DATE_GET_SECOND(self), |
| 5799 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5800 | Py_None, |
| 5801 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5805 | datetime_gettimetz(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5807 | return new_time(DATE_GET_HOUR(self), |
| 5808 | DATE_GET_MINUTE(self), |
| 5809 | DATE_GET_SECOND(self), |
| 5810 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5811 | GET_DT_TZINFO(self), |
| 5812 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5816 | datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5817 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5818 | int y, m, d, hh, mm, ss; |
| 5819 | PyObject *tzinfo; |
| 5820 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5821 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5822 | tzinfo = GET_DT_TZINFO(self); |
| 5823 | if (tzinfo == Py_None) { |
| 5824 | utcself = self; |
| 5825 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5826 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5827 | else { |
| 5828 | PyObject *offset; |
| 5829 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 5830 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 5831 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5832 | if (offset == Py_None) { |
| 5833 | Py_DECREF(offset); |
| 5834 | utcself = self; |
| 5835 | Py_INCREF(utcself); |
| 5836 | } |
| 5837 | else { |
| 5838 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5839 | (PyDateTime_Delta *)offset, -1); |
| 5840 | Py_DECREF(offset); |
| 5841 | if (utcself == NULL) |
| 5842 | return NULL; |
| 5843 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5845 | y = GET_YEAR(utcself); |
| 5846 | m = GET_MONTH(utcself); |
| 5847 | d = GET_DAY(utcself); |
| 5848 | hh = DATE_GET_HOUR(utcself); |
| 5849 | mm = DATE_GET_MINUTE(utcself); |
| 5850 | ss = DATE_GET_SECOND(utcself); |
| 5851 | |
| 5852 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5853 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 5856 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 5857 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5858 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5859 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 5860 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 5861 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5862 | */ |
| 5863 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5864 | datetime_getstate(PyDateTime_DateTime *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5865 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5866 | PyObject *basestate; |
| 5867 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5869 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 5870 | _PyDateTime_DATETIME_DATASIZE); |
| 5871 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5872 | if (proto > 3 && DATE_GET_FOLD(self)) |
| 5873 | /* Set the first bit of the third byte */ |
| 5874 | PyBytes_AS_STRING(basestate)[2] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5875 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 5876 | result = PyTuple_Pack(1, basestate); |
| 5877 | else |
| 5878 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 5879 | Py_DECREF(basestate); |
| 5880 | } |
| 5881 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5885 | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5886 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5887 | int proto; |
| 5888 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5889 | return NULL; |
| 5890 | |
| 5891 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5894 | static PyObject * |
| 5895 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
| 5896 | { |
| 5897 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); |
| 5898 | } |
| 5899 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5900 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5902 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5903 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 5904 | DATETIME_DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5906 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 5907 | METH_NOARGS | METH_CLASS, |
| 5908 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5910 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 5911 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5912 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5914 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 5915 | METH_VARARGS | METH_CLASS, |
Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 5916 | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5918 | {"strptime", (PyCFunction)datetime_strptime, |
| 5919 | METH_VARARGS | METH_CLASS, |
| 5920 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 5921 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5923 | {"combine", (PyCFunction)datetime_combine, |
| 5924 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5925 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5926 | |
Paul Ganssle | 09dc2f5 | 2017-12-21 00:33:49 -0500 | [diff] [blame] | 5927 | {"fromisoformat", (PyCFunction)datetime_fromisoformat, |
| 5928 | METH_O | METH_CLASS, |
| 5929 | PyDoc_STR("string -> datetime from datetime.isoformat() output")}, |
| 5930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5931 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5933 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 5934 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 5937 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5939 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 5940 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 5943 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5945 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 5946 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5947 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5948 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 5949 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 5950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5951 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 5952 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5954 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 5955 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5956 | "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5957 | "sep is used to separate the year from the time, and " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5958 | "defaults to 'T'.\n" |
| 5959 | "timespec specifies what components of the time to include" |
| 5960 | " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," |
| 5961 | " 'milliseconds', and 'microseconds').\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5963 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 5964 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5966 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 5967 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 5970 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
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 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 5973 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5975 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 5976 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5977 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5978 | {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5979 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5980 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5981 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 5982 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 5983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5984 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5985 | }; |
| 5986 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 5987 | static const char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5988 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 5989 | \n\ |
| 5990 | 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] | 5991 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5992 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5993 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5994 | datetime_add, /* nb_add */ |
| 5995 | datetime_subtract, /* nb_subtract */ |
| 5996 | 0, /* nb_multiply */ |
| 5997 | 0, /* nb_remainder */ |
| 5998 | 0, /* nb_divmod */ |
| 5999 | 0, /* nb_power */ |
| 6000 | 0, /* nb_negative */ |
| 6001 | 0, /* nb_positive */ |
| 6002 | 0, /* nb_absolute */ |
| 6003 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6004 | }; |
| 6005 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 6006 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6007 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6008 | "datetime.datetime", /* tp_name */ |
| 6009 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 6010 | 0, /* tp_itemsize */ |
| 6011 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 6012 | 0, /* tp_print */ |
| 6013 | 0, /* tp_getattr */ |
| 6014 | 0, /* tp_setattr */ |
| 6015 | 0, /* tp_reserved */ |
| 6016 | (reprfunc)datetime_repr, /* tp_repr */ |
| 6017 | &datetime_as_number, /* tp_as_number */ |
| 6018 | 0, /* tp_as_sequence */ |
| 6019 | 0, /* tp_as_mapping */ |
| 6020 | (hashfunc)datetime_hash, /* tp_hash */ |
| 6021 | 0, /* tp_call */ |
| 6022 | (reprfunc)datetime_str, /* tp_str */ |
| 6023 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6024 | 0, /* tp_setattro */ |
| 6025 | 0, /* tp_as_buffer */ |
| 6026 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6027 | datetime_doc, /* tp_doc */ |
| 6028 | 0, /* tp_traverse */ |
| 6029 | 0, /* tp_clear */ |
| 6030 | datetime_richcompare, /* tp_richcompare */ |
| 6031 | 0, /* tp_weaklistoffset */ |
| 6032 | 0, /* tp_iter */ |
| 6033 | 0, /* tp_iternext */ |
| 6034 | datetime_methods, /* tp_methods */ |
| 6035 | 0, /* tp_members */ |
| 6036 | datetime_getset, /* tp_getset */ |
| 6037 | &PyDateTime_DateType, /* tp_base */ |
| 6038 | 0, /* tp_dict */ |
| 6039 | 0, /* tp_descr_get */ |
| 6040 | 0, /* tp_descr_set */ |
| 6041 | 0, /* tp_dictoffset */ |
| 6042 | 0, /* tp_init */ |
| 6043 | datetime_alloc, /* tp_alloc */ |
| 6044 | datetime_new, /* tp_new */ |
| 6045 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6046 | }; |
| 6047 | |
| 6048 | /* --------------------------------------------------------------------------- |
| 6049 | * Module methods and initialization. |
| 6050 | */ |
| 6051 | |
| 6052 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6053 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6054 | }; |
| 6055 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6056 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 6057 | * datetime.h. |
| 6058 | */ |
| 6059 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6060 | &PyDateTime_DateType, |
| 6061 | &PyDateTime_DateTimeType, |
| 6062 | &PyDateTime_TimeType, |
| 6063 | &PyDateTime_DeltaType, |
| 6064 | &PyDateTime_TZInfoType, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6065 | NULL, // PyDatetime_TimeZone_UTC not initialized yet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6066 | new_date_ex, |
| 6067 | new_datetime_ex, |
| 6068 | new_time_ex, |
| 6069 | new_delta_ex, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6070 | new_timezone, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6071 | datetime_fromtimestamp, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6072 | date_fromtimestamp, |
| 6073 | new_datetime_ex2, |
| 6074 | new_time_ex2 |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6075 | }; |
| 6076 | |
| 6077 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6078 | |
| 6079 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6080 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6081 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6082 | "Fast implementation of the datetime type.", |
| 6083 | -1, |
| 6084 | module_methods, |
| 6085 | NULL, |
| 6086 | NULL, |
| 6087 | NULL, |
| 6088 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6089 | }; |
| 6090 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6091 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 6092 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6094 | PyObject *m; /* a module object */ |
| 6095 | PyObject *d; /* its dict */ |
| 6096 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6097 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6099 | m = PyModule_Create(&datetimemodule); |
| 6100 | if (m == NULL) |
| 6101 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6103 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 6104 | return NULL; |
| 6105 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 6106 | return NULL; |
| 6107 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 6108 | return NULL; |
| 6109 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 6110 | return NULL; |
| 6111 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 6112 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6113 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 6114 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6116 | /* timedelta values */ |
| 6117 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6119 | x = new_delta(0, 0, 1, 0); |
| 6120 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6121 | return NULL; |
| 6122 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6124 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 6125 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6126 | return NULL; |
| 6127 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 6130 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6131 | return NULL; |
| 6132 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6134 | /* date values */ |
| 6135 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6137 | x = new_date(1, 1, 1); |
| 6138 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6139 | return NULL; |
| 6140 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6142 | x = new_date(MAXYEAR, 12, 31); |
| 6143 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6144 | return NULL; |
| 6145 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6147 | x = new_delta(1, 0, 0, 0); |
| 6148 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6149 | return NULL; |
| 6150 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6152 | /* time values */ |
| 6153 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6154 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6155 | x = new_time(0, 0, 0, 0, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6156 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6157 | return NULL; |
| 6158 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6159 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6160 | x = new_time(23, 59, 59, 999999, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6161 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6162 | return NULL; |
| 6163 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6165 | x = new_delta(0, 0, 1, 0); |
| 6166 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6167 | return NULL; |
| 6168 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6170 | /* datetime values */ |
| 6171 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6172 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6173 | 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] | 6174 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6175 | return NULL; |
| 6176 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6177 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6178 | 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] | 6179 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6180 | return NULL; |
| 6181 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6183 | x = new_delta(0, 0, 1, 0); |
| 6184 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 6185 | return NULL; |
| 6186 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6187 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6188 | /* timezone values */ |
| 6189 | d = PyDateTime_TimeZoneType.tp_dict; |
| 6190 | |
| 6191 | delta = new_delta(0, 0, 0, 0); |
| 6192 | if (delta == NULL) |
| 6193 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6194 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6195 | Py_DECREF(delta); |
| 6196 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 6197 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 6198 | PyDateTime_TimeZone_UTC = x; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 6199 | CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6200 | |
| 6201 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 6202 | if (delta == NULL) |
| 6203 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6204 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6205 | Py_DECREF(delta); |
| 6206 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 6207 | return NULL; |
| 6208 | Py_DECREF(x); |
| 6209 | |
| 6210 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 6211 | if (delta == NULL) |
| 6212 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 6213 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6214 | Py_DECREF(delta); |
| 6215 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 6216 | return NULL; |
| 6217 | Py_DECREF(x); |
| 6218 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6219 | /* Epoch */ |
| 6220 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 6221 | PyDateTime_TimeZone_UTC, 0); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 6222 | if (PyDateTime_Epoch == NULL) |
| 6223 | return NULL; |
| 6224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6225 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6226 | PyModule_AddIntMacro(m, MINYEAR); |
| 6227 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6229 | Py_INCREF(&PyDateTime_DateType); |
| 6230 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6232 | Py_INCREF(&PyDateTime_DateTimeType); |
| 6233 | PyModule_AddObject(m, "datetime", |
| 6234 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6236 | Py_INCREF(&PyDateTime_TimeType); |
| 6237 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6239 | Py_INCREF(&PyDateTime_DeltaType); |
| 6240 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6242 | Py_INCREF(&PyDateTime_TZInfoType); |
| 6243 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6244 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 6245 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 6246 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 6247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6248 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 6249 | if (x == NULL) |
| 6250 | return NULL; |
| 6251 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 6252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6253 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 6254 | * pasting together 4 single years. |
| 6255 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6256 | Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6257 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6259 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 6260 | * get from pasting together 4 100-year cycles. |
| 6261 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6262 | Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6263 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6265 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 6266 | * pasting together 25 4-year cycles. |
| 6267 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 6268 | Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6269 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6271 | us_per_ms = PyLong_FromLong(1000); |
| 6272 | us_per_second = PyLong_FromLong(1000000); |
| 6273 | us_per_minute = PyLong_FromLong(60000000); |
| 6274 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 6275 | if (us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6276 | us_per_minute == NULL || seconds_per_day == NULL) |
| 6277 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6279 | /* The rest are too big for 32-bit ints, but even |
| 6280 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 6281 | */ |
| 6282 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 6283 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 6284 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 6285 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 6286 | return NULL; |
| 6287 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 6288 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6289 | |
| 6290 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6291 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6292 | x.n = x stripped of its timezone -- its naive time. |
| 6293 | 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] | 6294 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6295 | 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] | 6296 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6297 | x.s = x's standard offset, x.o - x.d |
| 6298 | |
| 6299 | Now some derived rules, where k is a duration (timedelta). |
| 6300 | |
| 6301 | 1. x.o = x.s + x.d |
| 6302 | This follows from the definition of x.s. |
| 6303 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6304 | 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] | 6305 | This is actually a requirement, an assumption we need to make about |
| 6306 | sane tzinfo classes. |
| 6307 | |
| 6308 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 6309 | This is again a requirement for a sane tzinfo class. |
| 6310 | |
| 6311 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6312 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6313 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6314 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6315 | Again follows from how arithmetic is defined. |
| 6316 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6317 | 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] | 6318 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 6319 | None when called). |
| 6320 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 6321 | 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] | 6322 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6323 | |
| 6324 | By #3, we want |
| 6325 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6326 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6327 | |
| 6328 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 6329 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 6330 | becomes true; in effect, we want to solve [2] for k: |
| 6331 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6332 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6333 | |
| 6334 | By #1, this is the same as |
| 6335 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6336 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6337 | |
| 6338 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 6339 | Substituting that into [3], |
| 6340 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6341 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 6342 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 6343 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 6344 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6345 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6346 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 6347 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 6348 | very large, since all offset-returning methods return a duration of magnitude |
| 6349 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 6350 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6351 | |
| 6352 | In any case, the new value is |
| 6353 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6354 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6355 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6356 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 6357 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6358 | |
| 6359 | At this point, if |
| 6360 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6361 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6362 | |
| 6363 | 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] | 6364 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 6365 | 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] | 6366 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 6367 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 6368 | 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] | 6369 | the only spelling that makes sense on the local wall clock. |
| 6370 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6371 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 6372 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 6373 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6374 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6375 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6376 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6377 | Now |
| 6378 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6379 | (y + y.s).n = by #5 |
| 6380 | y.n + y.s = since y.n = x.n |
| 6381 | x.n + y.s = since z and y are have the same tzinfo member, |
| 6382 | y.s = z.s by #2 |
| 6383 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6384 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6385 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6386 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6387 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6388 | x.n - ((x.n + z.s) - z.o) = expanding |
| 6389 | x.n - x.n - z.s + z.o = cancelling |
| 6390 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6391 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6392 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6393 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6394 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6395 | 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] | 6396 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 6397 | 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] | 6398 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6399 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 6400 | 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] | 6401 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6402 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 6403 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6404 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6405 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6406 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6407 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6408 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6409 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 6410 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6411 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 6412 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 6413 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 6414 | the justifications for the kinds of substitutions we've done several times |
| 6415 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6416 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6417 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6418 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 6419 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 6420 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 6421 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 6422 | - z.o + z'.o = #1 twice |
| 6423 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 6424 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6425 | |
| 6426 | 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] | 6427 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 6428 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6429 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6430 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 6431 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 6432 | would have to change the result dst() returns: we start in DST, and moving |
| 6433 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6434 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6435 | There isn't a sane case where this can happen. The closest it gets is at |
| 6436 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 6437 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 6438 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 6439 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 6440 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 6441 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 6442 | standard time. Since that's what the local clock *does*, we want to map both |
| 6443 | 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] | 6444 | in local time, but so it goes -- it's the way the local clock works. |
| 6445 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6446 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 6447 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 6448 | 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] | 6449 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 6450 | |
| 6451 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 6452 | 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] | 6453 | 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] | 6454 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 6455 | but the reasoning doesn't depend on the example -- it depends on there being |
| 6456 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6457 | z' must be in standard time, and is the spelling we want in this case. |
| 6458 | |
| 6459 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 6460 | concerned (because it takes z' as being in standard time rather than the |
| 6461 | daylight time we intend here), but returning it gives the real-life "local |
| 6462 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 6463 | tz. |
| 6464 | |
| 6465 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 6466 | the 1:MM standard time spelling we want. |
| 6467 | |
| 6468 | So how can this break? One of the assumptions must be violated. Two |
| 6469 | possibilities: |
| 6470 | |
| 6471 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 6472 | time zone. This isn't true if, for political reasons or continental drift, |
| 6473 | a region decides to change its base offset from UTC. |
| 6474 | |
| 6475 | 2) There may be versions of "double daylight" time where the tail end of |
| 6476 | the analysis gives up a step too early. I haven't thought about that |
| 6477 | enough to say. |
| 6478 | |
| 6479 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 6480 | "almost all" time zones: so long as the standard offset is invariant, it |
| 6481 | doesn't matter if daylight time transition points change from year to year, or |
| 6482 | if daylight time is skipped in some years; it doesn't matter how large or |
| 6483 | small dst() may get within its bounds; and it doesn't even matter if some |
| 6484 | perverse time zone returns a negative dst()). So a breaking case must be |
| 6485 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6486 | --------------------------------------------------------------------------- */ |