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 | /* --------------------------------------------------------------------------- |
| 667 | * Create various objects, mostly without range checking. |
| 668 | */ |
| 669 | |
| 670 | /* Create a date instance with no range checking. */ |
| 671 | static PyObject * |
| 672 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 675 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 676 | if (check_date_args(year, month, day) < 0) { |
| 677 | return NULL; |
| 678 | } |
| 679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 681 | if (self != NULL) |
| 682 | set_date_fields(self, year, month, day); |
| 683 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 688 | |
| 689 | /* Create a datetime instance with no range checking. */ |
| 690 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 691 | new_datetime_ex2(int year, int month, int day, int hour, int minute, |
| 692 | int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | PyDateTime_DateTime *self; |
| 695 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 696 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 697 | if (check_date_args(year, month, day) < 0) { |
| 698 | return NULL; |
| 699 | } |
| 700 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 701 | return NULL; |
| 702 | } |
| 703 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 704 | return NULL; |
| 705 | } |
| 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 708 | if (self != NULL) { |
| 709 | self->hastzinfo = aware; |
| 710 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 711 | DATE_SET_HOUR(self, hour); |
| 712 | DATE_SET_MINUTE(self, minute); |
| 713 | DATE_SET_SECOND(self, second); |
| 714 | DATE_SET_MICROSECOND(self, usecond); |
| 715 | if (aware) { |
| 716 | Py_INCREF(tzinfo); |
| 717 | self->tzinfo = tzinfo; |
| 718 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 719 | DATE_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | } |
| 721 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 724 | static PyObject * |
| 725 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
| 726 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
| 727 | { |
| 728 | return new_datetime_ex2(year, month, day, hour, minute, second, usecond, |
| 729 | tzinfo, 0, type); |
| 730 | } |
| 731 | |
| 732 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ |
| 733 | new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 735 | |
| 736 | /* Create a time instance with no range checking. */ |
| 737 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 738 | new_time_ex2(int hour, int minute, int second, int usecond, |
| 739 | PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | PyDateTime_Time *self; |
| 742 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 743 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 744 | if (check_time_args(hour, minute, second, usecond, fold) < 0) { |
| 745 | return NULL; |
| 746 | } |
| 747 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 748 | return NULL; |
| 749 | } |
| 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 752 | if (self != NULL) { |
| 753 | self->hastzinfo = aware; |
| 754 | self->hashcode = -1; |
| 755 | TIME_SET_HOUR(self, hour); |
| 756 | TIME_SET_MINUTE(self, minute); |
| 757 | TIME_SET_SECOND(self, second); |
| 758 | TIME_SET_MICROSECOND(self, usecond); |
| 759 | if (aware) { |
| 760 | Py_INCREF(tzinfo); |
| 761 | self->tzinfo = tzinfo; |
| 762 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 763 | TIME_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | } |
| 765 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 768 | static PyObject * |
| 769 | new_time_ex(int hour, int minute, int second, int usecond, |
| 770 | PyObject *tzinfo, PyTypeObject *type) |
| 771 | { |
| 772 | return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); |
| 773 | } |
| 774 | |
| 775 | #define new_time(hh, mm, ss, us, tzinfo, fold) \ |
| 776 | new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 777 | |
| 778 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 779 | * true. Passing false is a speed optimization, if you know for sure |
| 780 | * that seconds and microseconds are already in their proper ranges. In any |
| 781 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 782 | * of range). |
| 783 | */ |
| 784 | static PyObject * |
| 785 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | if (normalize) |
| 791 | normalize_d_s_us(&days, &seconds, µseconds); |
| 792 | assert(0 <= seconds && seconds < 24*3600); |
| 793 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | if (check_delta_day_range(days) < 0) |
| 796 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 799 | if (self != NULL) { |
| 800 | self->hashcode = -1; |
| 801 | SET_TD_DAYS(self, days); |
| 802 | SET_TD_SECONDS(self, seconds); |
| 803 | SET_TD_MICROSECONDS(self, microseconds); |
| 804 | } |
| 805 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | #define new_delta(d, s, us, normalize) \ |
| 809 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 810 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 811 | |
| 812 | typedef struct |
| 813 | { |
| 814 | PyObject_HEAD |
| 815 | PyObject *offset; |
| 816 | PyObject *name; |
| 817 | } PyDateTime_TimeZone; |
| 818 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 819 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 820 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 821 | /* The interned Epoch datetime instance */ |
| 822 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 823 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 824 | /* Create new timezone instance checking offset range. This |
| 825 | function does not check the name argument. Caller must assure |
| 826 | that offset is a timedelta instance and name is either NULL |
| 827 | or a unicode object. */ |
| 828 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 829 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 830 | { |
| 831 | PyDateTime_TimeZone *self; |
| 832 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 833 | |
| 834 | assert(offset != NULL); |
| 835 | assert(PyDelta_Check(offset)); |
| 836 | assert(name == NULL || PyUnicode_Check(name)); |
| 837 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 838 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 839 | if (self == NULL) { |
| 840 | return NULL; |
| 841 | } |
| 842 | Py_INCREF(offset); |
| 843 | self->offset = offset; |
| 844 | Py_XINCREF(name); |
| 845 | self->name = name; |
| 846 | return (PyObject *)self; |
| 847 | } |
| 848 | |
| 849 | static int delta_bool(PyDateTime_Delta *self); |
| 850 | |
| 851 | static PyObject * |
| 852 | new_timezone(PyObject *offset, PyObject *name) |
| 853 | { |
| 854 | assert(offset != NULL); |
| 855 | assert(PyDelta_Check(offset)); |
| 856 | assert(name == NULL || PyUnicode_Check(name)); |
| 857 | |
| 858 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 859 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 860 | return PyDateTime_TimeZone_UTC; |
| 861 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 862 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 863 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 864 | " representing a whole number of minutes," |
| 865 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 866 | return NULL; |
| 867 | } |
| 868 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 869 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 870 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 871 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 872 | " timedelta(hours=24)," |
| 873 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 874 | return NULL; |
| 875 | } |
| 876 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 877 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 880 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 881 | * tzinfo helpers. |
| 882 | */ |
| 883 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 884 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 885 | * raise TypeError and return -1. |
| 886 | */ |
| 887 | static int |
| 888 | check_tzinfo_subclass(PyObject *p) |
| 889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | if (p == Py_None || PyTZInfo_Check(p)) |
| 891 | return 0; |
| 892 | PyErr_Format(PyExc_TypeError, |
| 893 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 894 | "not type '%s'", |
| 895 | Py_TYPE(p)->tp_name); |
| 896 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 899 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 900 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 901 | * and the caller must not decref the result. |
| 902 | */ |
| 903 | static PyObject * |
| 904 | get_tzinfo_member(PyObject *self) |
| 905 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 909 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 910 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 911 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 916 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 917 | * be an instance of the tzinfo class. If the method returns None, this |
| 918 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 919 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 920 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 921 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 922 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 923 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 924 | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 925 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 926 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 929 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 931 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 932 | if (tzinfo == Py_None) |
| 933 | Py_RETURN_NONE; |
| 934 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 935 | if (offset == Py_None || offset == NULL) |
| 936 | return offset; |
| 937 | if (PyDelta_Check(offset)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 938 | if (GET_TD_MICROSECONDS(offset) != 0) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 939 | Py_DECREF(offset); |
| 940 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 941 | " representing a whole number of seconds"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 942 | return NULL; |
| 943 | } |
| 944 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 945 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 946 | Py_DECREF(offset); |
| 947 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 948 | " strictly between -timedelta(hours=24) and" |
| 949 | " timedelta(hours=24)."); |
| 950 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | else { |
| 954 | PyErr_Format(PyExc_TypeError, |
| 955 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 956 | "timedelta, not '%.200s'", |
| 957 | name, Py_TYPE(offset)->tp_name); |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 958 | Py_DECREF(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 959 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 961 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 962 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 966 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 967 | * 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] | 968 | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
| 969 | * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
| 970 | * # of minutes), ValueError is raised and this returns -1. Else *none is |
| 971 | * set to 0 and the offset is returned (as int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 972 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 973 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 974 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 975 | { |
| 976 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 979 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 980 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 981 | * returns None, call_dst returns 0 and sets *none to 1. If dst() |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 982 | & doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 983 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 984 | * ValueError is raised and this returns -1. Else *none is set to 0 and |
| 985 | * the offset is returned (as an int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 986 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 987 | static PyObject * |
| 988 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 989 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 990 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 993 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 994 | * 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] | 995 | * 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] | 996 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 997 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 998 | */ |
| 999 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1000 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1001 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1003 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | assert(tzinfo != NULL); |
| 1006 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 1007 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1010 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1011 | |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1012 | result = _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_tzname, |
| 1013 | tzinfoarg, NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1014 | |
| 1015 | if (result == NULL || result == Py_None) |
| 1016 | return result; |
| 1017 | |
| 1018 | if (!PyUnicode_Check(result)) { |
| 1019 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 1020 | "return None or a string, not '%s'", |
| 1021 | Py_TYPE(result)->tp_name); |
| 1022 | Py_DECREF(result); |
| 1023 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1025 | |
| 1026 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1029 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 1030 | * stuff |
| 1031 | * ", tzinfo=" + repr(tzinfo) |
| 1032 | * before the closing ")". |
| 1033 | */ |
| 1034 | static PyObject * |
| 1035 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 1036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | assert(PyUnicode_Check(repr)); |
| 1040 | assert(tzinfo); |
| 1041 | if (tzinfo == Py_None) |
| 1042 | return repr; |
| 1043 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1045 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | Py_DECREF(repr); |
| 1047 | if (temp == NULL) |
| 1048 | return NULL; |
| 1049 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 1050 | Py_DECREF(temp); |
| 1051 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1054 | /* repr is like "someclass(arg1, arg2)". If fold isn't 0, |
| 1055 | * stuff |
| 1056 | * ", fold=" + repr(tzinfo) |
| 1057 | * before the closing ")". |
| 1058 | */ |
| 1059 | static PyObject * |
| 1060 | append_keyword_fold(PyObject *repr, int fold) |
| 1061 | { |
| 1062 | PyObject *temp; |
| 1063 | |
| 1064 | assert(PyUnicode_Check(repr)); |
| 1065 | if (fold == 0) |
| 1066 | return repr; |
| 1067 | /* Get rid of the trailing ')'. */ |
| 1068 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1069 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
| 1070 | Py_DECREF(repr); |
| 1071 | if (temp == NULL) |
| 1072 | return NULL; |
| 1073 | repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); |
| 1074 | Py_DECREF(temp); |
| 1075 | return repr; |
| 1076 | } |
| 1077 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1078 | /* --------------------------------------------------------------------------- |
| 1079 | * String format helpers. |
| 1080 | */ |
| 1081 | |
| 1082 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1083 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1084 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1085 | static const char * const DayNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1087 | }; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1088 | static const char * const MonthNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1090 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1091 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1096 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1097 | GET_DAY(date), hours, minutes, seconds, |
| 1098 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1101 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1102 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1103 | /* Add an hours & minutes UTC offset string to buf. buf has no more than |
| 1104 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1105 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1106 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1107 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1108 | * string of the form |
| 1109 | * sign HH sep MM |
| 1110 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1111 | * bogus, an appropriate exception is set and -1 is returned. |
| 1112 | */ |
| 1113 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1114 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1116 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1117 | PyObject *offset; |
| 1118 | int hours, minutes, seconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1122 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1123 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1124 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1126 | if (offset == Py_None) { |
| 1127 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | *buf = '\0'; |
| 1129 | return 0; |
| 1130 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1131 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1132 | if (GET_TD_DAYS(offset) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | sign = '-'; |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 1134 | Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1135 | if (offset == NULL) |
| 1136 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1138 | else { |
| 1139 | sign = '+'; |
| 1140 | } |
| 1141 | /* Offset is not negative here. */ |
| 1142 | seconds = GET_TD_SECONDS(offset); |
| 1143 | Py_DECREF(offset); |
| 1144 | minutes = divmod(seconds, 60, &seconds); |
| 1145 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1146 | if (seconds == 0) |
| 1147 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
| 1148 | else |
| 1149 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, |
| 1150 | sep, minutes, sep, seconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1154 | static PyObject * |
| 1155 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | PyObject *temp; |
| 1158 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1159 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1160 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | if (Zreplacement == NULL) |
| 1163 | return NULL; |
| 1164 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1165 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | assert(tzinfoarg != NULL); |
| 1168 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1169 | if (temp == NULL) |
| 1170 | goto Error; |
| 1171 | if (temp == Py_None) { |
| 1172 | Py_DECREF(temp); |
| 1173 | return Zreplacement; |
| 1174 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1176 | assert(PyUnicode_Check(temp)); |
| 1177 | /* Since the tzname is getting stuffed into the |
| 1178 | * format, we have to double any % signs so that |
| 1179 | * strftime doesn't treat them as format codes. |
| 1180 | */ |
| 1181 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1182 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | Py_DECREF(temp); |
| 1184 | if (Zreplacement == NULL) |
| 1185 | return NULL; |
| 1186 | if (!PyUnicode_Check(Zreplacement)) { |
| 1187 | PyErr_SetString(PyExc_TypeError, |
| 1188 | "tzname.replace() did not return a string"); |
| 1189 | goto Error; |
| 1190 | } |
| 1191 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1192 | |
| 1193 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | Py_DECREF(Zreplacement); |
| 1195 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1198 | static PyObject * |
| 1199 | make_freplacement(PyObject *object) |
| 1200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | char freplacement[64]; |
| 1202 | if (PyTime_Check(object)) |
| 1203 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1204 | else if (PyDateTime_Check(object)) |
| 1205 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1206 | else |
| 1207 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1212 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1213 | * 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] | 1214 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1215 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1216 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1217 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1218 | */ |
| 1219 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1220 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1226 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1227 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | const char *pin; /* pointer to next char in input format */ |
| 1230 | Py_ssize_t flen; /* length of input format */ |
| 1231 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1234 | char *pnew; /* pointer to available byte in output format */ |
| 1235 | size_t totalnew; /* number bytes total in output format buffer, |
| 1236 | exclusive of trailing \0 */ |
| 1237 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1240 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | assert(object && format && timetuple); |
| 1243 | assert(PyUnicode_Check(format)); |
| 1244 | /* Convert the input format to a C string and size */ |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1245 | pin = PyUnicode_AsUTF8AndSize(format, &flen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | if (!pin) |
| 1247 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1250 | * a new format. Since computing the replacements for those codes |
| 1251 | * is expensive, don't unless they're actually used. |
| 1252 | */ |
| 1253 | if (flen > INT_MAX - 1) { |
| 1254 | PyErr_NoMemory(); |
| 1255 | goto Done; |
| 1256 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1259 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1260 | if (newfmt == NULL) goto Done; |
| 1261 | pnew = PyBytes_AsString(newfmt); |
| 1262 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | while ((ch = *pin++) != '\0') { |
| 1265 | if (ch != '%') { |
| 1266 | ptoappend = pin - 1; |
| 1267 | ntoappend = 1; |
| 1268 | } |
| 1269 | else if ((ch = *pin++) == '\0') { |
| 1270 | /* There's a lone trailing %; doesn't make sense. */ |
| 1271 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1272 | "ends with raw %"); |
| 1273 | goto Done; |
| 1274 | } |
| 1275 | /* A % has been seen and ch is the character after it. */ |
| 1276 | else if (ch == 'z') { |
| 1277 | if (zreplacement == NULL) { |
| 1278 | /* format utcoffset */ |
| 1279 | char buf[100]; |
| 1280 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1281 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1282 | if (zreplacement == NULL) goto Done; |
| 1283 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1284 | assert(tzinfoarg != NULL); |
| 1285 | if (format_utcoffset(buf, |
| 1286 | sizeof(buf), |
| 1287 | "", |
| 1288 | tzinfo, |
| 1289 | tzinfoarg) < 0) |
| 1290 | goto Done; |
| 1291 | Py_DECREF(zreplacement); |
| 1292 | zreplacement = |
| 1293 | PyBytes_FromStringAndSize(buf, |
| 1294 | strlen(buf)); |
| 1295 | if (zreplacement == NULL) |
| 1296 | goto Done; |
| 1297 | } |
| 1298 | } |
| 1299 | assert(zreplacement != NULL); |
| 1300 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1301 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1302 | } |
| 1303 | else if (ch == 'Z') { |
| 1304 | /* format tzname */ |
| 1305 | if (Zreplacement == NULL) { |
| 1306 | Zreplacement = make_Zreplacement(object, |
| 1307 | tzinfoarg); |
| 1308 | if (Zreplacement == NULL) |
| 1309 | goto Done; |
| 1310 | } |
| 1311 | assert(Zreplacement != NULL); |
| 1312 | assert(PyUnicode_Check(Zreplacement)); |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1313 | ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1315 | if (ptoappend == NULL) |
| 1316 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | } |
| 1318 | else if (ch == 'f') { |
| 1319 | /* format microseconds */ |
| 1320 | if (freplacement == NULL) { |
| 1321 | freplacement = make_freplacement(object); |
| 1322 | if (freplacement == NULL) |
| 1323 | goto Done; |
| 1324 | } |
| 1325 | assert(freplacement != NULL); |
| 1326 | assert(PyBytes_Check(freplacement)); |
| 1327 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1328 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1329 | } |
| 1330 | else { |
| 1331 | /* percent followed by neither z nor Z */ |
| 1332 | ptoappend = pin - 2; |
| 1333 | ntoappend = 2; |
| 1334 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | /* Append the ntoappend chars starting at ptoappend to |
| 1337 | * the new format. |
| 1338 | */ |
| 1339 | if (ntoappend == 0) |
| 1340 | continue; |
| 1341 | assert(ptoappend != NULL); |
| 1342 | assert(ntoappend > 0); |
| 1343 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1344 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | PyErr_NoMemory(); |
| 1346 | goto Done; |
| 1347 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1348 | totalnew <<= 1; |
| 1349 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1352 | } |
| 1353 | memcpy(pnew, ptoappend, ntoappend); |
| 1354 | pnew += ntoappend; |
| 1355 | usednew += ntoappend; |
| 1356 | assert(usednew <= totalnew); |
| 1357 | } /* end while() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1360 | goto Done; |
| 1361 | { |
| 1362 | PyObject *format; |
| 1363 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | if (time == NULL) |
| 1366 | goto Done; |
| 1367 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1368 | if (format != NULL) { |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 1369 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime, |
| 1370 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1371 | Py_DECREF(format); |
| 1372 | } |
| 1373 | Py_DECREF(time); |
| 1374 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1375 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | Py_XDECREF(freplacement); |
| 1377 | Py_XDECREF(zreplacement); |
| 1378 | Py_XDECREF(Zreplacement); |
| 1379 | Py_XDECREF(newfmt); |
| 1380 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1383 | /* --------------------------------------------------------------------------- |
| 1384 | * Wrap functions from the time module. These aren't directly available |
| 1385 | * from C. Perhaps they should be. |
| 1386 | */ |
| 1387 | |
| 1388 | /* Call time.time() and return its result (a Python float). */ |
| 1389 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1390 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | PyObject *result = NULL; |
| 1393 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1396 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1397 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 1398 | result = _PyObject_CallMethodId(time, &PyId_time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | Py_DECREF(time); |
| 1400 | } |
| 1401 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1405 | * computed from the y,m,d args. |
| 1406 | */ |
| 1407 | static PyObject * |
| 1408 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | PyObject *time; |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1411 | PyObject *result; |
| 1412 | _Py_IDENTIFIER(struct_time); |
| 1413 | PyObject *args; |
| 1414 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | time = PyImport_ImportModuleNoBlock("time"); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1417 | if (time == NULL) { |
| 1418 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | } |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1420 | |
| 1421 | args = Py_BuildValue("iiiiiiiii", |
| 1422 | y, m, d, |
| 1423 | hh, mm, ss, |
| 1424 | weekday(y, m, d), |
| 1425 | days_before_month(y, m) + d, |
| 1426 | dstflag); |
| 1427 | if (args == NULL) { |
| 1428 | Py_DECREF(time); |
| 1429 | return NULL; |
| 1430 | } |
| 1431 | |
| 1432 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_struct_time, |
| 1433 | args, NULL); |
| 1434 | Py_DECREF(time); |
Victor Stinner | ddc120f | 2016-12-09 15:35:40 +0100 | [diff] [blame] | 1435 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /* --------------------------------------------------------------------------- |
| 1440 | * Miscellaneous helpers. |
| 1441 | */ |
| 1442 | |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1443 | /* 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] | 1444 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1445 | * This little helper turns that into a bool result for rich comparisons. |
| 1446 | */ |
| 1447 | static PyObject * |
| 1448 | diff_to_bool(int diff, int op) |
| 1449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | PyObject *result; |
| 1451 | int istrue; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | switch (op) { |
| 1454 | case Py_EQ: istrue = diff == 0; break; |
| 1455 | case Py_NE: istrue = diff != 0; break; |
| 1456 | case Py_LE: istrue = diff <= 0; break; |
| 1457 | case Py_GE: istrue = diff >= 0; break; |
| 1458 | case Py_LT: istrue = diff < 0; break; |
| 1459 | case Py_GT: istrue = diff > 0; break; |
| 1460 | default: |
| 1461 | assert(! "op unknown"); |
| 1462 | istrue = 0; /* To shut up compiler */ |
| 1463 | } |
| 1464 | result = istrue ? Py_True : Py_False; |
| 1465 | Py_INCREF(result); |
| 1466 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1469 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1470 | static PyObject * |
| 1471 | cmperror(PyObject *a, PyObject *b) |
| 1472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | PyErr_Format(PyExc_TypeError, |
| 1474 | "can't compare %s to %s", |
| 1475 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1476 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1479 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1480 | * Cached Python objects; these are set by the module init function. |
| 1481 | */ |
| 1482 | |
| 1483 | /* Conversion factors. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1485 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1486 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1487 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1488 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1489 | 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] | 1490 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1491 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1492 | /* --------------------------------------------------------------------------- |
| 1493 | * Class implementations. |
| 1494 | */ |
| 1495 | |
| 1496 | /* |
| 1497 | * PyDateTime_Delta implementation. |
| 1498 | */ |
| 1499 | |
| 1500 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1502 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1503 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1504 | * due to ubiquitous overflow possibilities. |
| 1505 | */ |
| 1506 | static PyObject * |
| 1507 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | PyObject *x1 = NULL; |
| 1510 | PyObject *x2 = NULL; |
| 1511 | PyObject *x3 = NULL; |
| 1512 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1515 | if (x1 == NULL) |
| 1516 | goto Done; |
| 1517 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1518 | if (x2 == NULL) |
| 1519 | goto Done; |
| 1520 | Py_DECREF(x1); |
| 1521 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | /* x2 has days in seconds */ |
| 1524 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1525 | if (x1 == NULL) |
| 1526 | goto Done; |
| 1527 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1528 | if (x3 == NULL) |
| 1529 | goto Done; |
| 1530 | Py_DECREF(x1); |
| 1531 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1532 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | /* x3 has days+seconds in seconds */ |
| 1535 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1536 | if (x1 == NULL) |
| 1537 | goto Done; |
| 1538 | Py_DECREF(x3); |
| 1539 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | /* x1 has days+seconds in us */ |
| 1542 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1543 | if (x2 == NULL) |
| 1544 | goto Done; |
| 1545 | result = PyNumber_Add(x1, x2); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1546 | |
| 1547 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | Py_XDECREF(x1); |
| 1549 | Py_XDECREF(x2); |
| 1550 | Py_XDECREF(x3); |
| 1551 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1554 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1555 | */ |
| 1556 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1557 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | int us; |
| 1560 | int s; |
| 1561 | int d; |
| 1562 | long temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | PyObject *tuple = NULL; |
| 1565 | PyObject *num = NULL; |
| 1566 | PyObject *result = NULL; |
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 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1569 | if (tuple == NULL) |
| 1570 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1573 | if (num == NULL) |
| 1574 | goto Done; |
| 1575 | temp = PyLong_AsLong(num); |
| 1576 | num = NULL; |
| 1577 | if (temp == -1 && PyErr_Occurred()) |
| 1578 | goto Done; |
| 1579 | assert(0 <= temp && temp < 1000000); |
| 1580 | us = (int)temp; |
| 1581 | if (us < 0) { |
| 1582 | /* The divisor was positive, so this must be an error. */ |
| 1583 | assert(PyErr_Occurred()); |
| 1584 | goto Done; |
| 1585 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1588 | if (num == NULL) |
| 1589 | goto Done; |
| 1590 | Py_INCREF(num); |
| 1591 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1594 | if (tuple == NULL) |
| 1595 | goto Done; |
| 1596 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1599 | if (num == NULL) |
| 1600 | goto Done; |
| 1601 | temp = PyLong_AsLong(num); |
| 1602 | num = NULL; |
| 1603 | if (temp == -1 && PyErr_Occurred()) |
| 1604 | goto Done; |
| 1605 | assert(0 <= temp && temp < 24*3600); |
| 1606 | s = (int)temp; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | if (s < 0) { |
| 1609 | /* The divisor was positive, so this must be an error. */ |
| 1610 | assert(PyErr_Occurred()); |
| 1611 | goto Done; |
| 1612 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1615 | if (num == NULL) |
| 1616 | goto Done; |
| 1617 | Py_INCREF(num); |
| 1618 | temp = PyLong_AsLong(num); |
| 1619 | if (temp == -1 && PyErr_Occurred()) |
| 1620 | goto Done; |
| 1621 | d = (int)temp; |
| 1622 | if ((long)d != temp) { |
| 1623 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1624 | "large to fit in a C int"); |
| 1625 | goto Done; |
| 1626 | } |
| 1627 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1628 | |
| 1629 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | Py_XDECREF(tuple); |
| 1631 | Py_XDECREF(num); |
| 1632 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | #define microseconds_to_delta(pymicros) \ |
| 1636 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1637 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1638 | static PyObject * |
| 1639 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | PyObject *pyus_in; |
| 1642 | PyObject *pyus_out; |
| 1643 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | pyus_in = delta_to_microseconds(delta); |
| 1646 | if (pyus_in == NULL) |
| 1647 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1650 | Py_DECREF(pyus_in); |
| 1651 | if (pyus_out == NULL) |
| 1652 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | result = microseconds_to_delta(pyus_out); |
| 1655 | Py_DECREF(pyus_out); |
| 1656 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1660 | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) |
| 1661 | { |
| 1662 | PyObject *result = NULL; |
| 1663 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1664 | PyObject *ratio = NULL; |
| 1665 | |
| 1666 | pyus_in = delta_to_microseconds(delta); |
| 1667 | if (pyus_in == NULL) |
| 1668 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1669 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1670 | if (ratio == NULL) |
| 1671 | goto error; |
| 1672 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); |
| 1673 | Py_DECREF(pyus_in); |
| 1674 | pyus_in = NULL; |
| 1675 | if (temp == NULL) |
| 1676 | goto error; |
| 1677 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); |
| 1678 | Py_DECREF(temp); |
| 1679 | if (pyus_out == NULL) |
| 1680 | goto error; |
| 1681 | result = microseconds_to_delta(pyus_out); |
| 1682 | Py_DECREF(pyus_out); |
| 1683 | error: |
| 1684 | Py_XDECREF(pyus_in); |
| 1685 | Py_XDECREF(ratio); |
| 1686 | |
| 1687 | return result; |
| 1688 | } |
| 1689 | |
| 1690 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1691 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1692 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | PyObject *pyus_in; |
| 1694 | PyObject *pyus_out; |
| 1695 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | pyus_in = delta_to_microseconds(delta); |
| 1698 | if (pyus_in == NULL) |
| 1699 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1702 | Py_DECREF(pyus_in); |
| 1703 | if (pyus_out == NULL) |
| 1704 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | result = microseconds_to_delta(pyus_out); |
| 1707 | Py_DECREF(pyus_out); |
| 1708 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1712 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | PyObject *pyus_left; |
| 1715 | PyObject *pyus_right; |
| 1716 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | pyus_left = delta_to_microseconds(left); |
| 1719 | if (pyus_left == NULL) |
| 1720 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | pyus_right = delta_to_microseconds(right); |
| 1723 | if (pyus_right == NULL) { |
| 1724 | Py_DECREF(pyus_left); |
| 1725 | return NULL; |
| 1726 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1729 | Py_DECREF(pyus_left); |
| 1730 | Py_DECREF(pyus_right); |
| 1731 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static PyObject * |
| 1735 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1736 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | PyObject *pyus_left; |
| 1738 | PyObject *pyus_right; |
| 1739 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | pyus_left = delta_to_microseconds(left); |
| 1742 | if (pyus_left == NULL) |
| 1743 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | pyus_right = delta_to_microseconds(right); |
| 1746 | if (pyus_right == NULL) { |
| 1747 | Py_DECREF(pyus_left); |
| 1748 | return NULL; |
| 1749 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1752 | Py_DECREF(pyus_left); |
| 1753 | Py_DECREF(pyus_right); |
| 1754 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1758 | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) |
| 1759 | { |
| 1760 | PyObject *result = NULL; |
| 1761 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1762 | PyObject *ratio = NULL; |
| 1763 | |
| 1764 | pyus_in = delta_to_microseconds(delta); |
| 1765 | if (pyus_in == NULL) |
| 1766 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1767 | ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1768 | if (ratio == NULL) |
| 1769 | goto error; |
| 1770 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1)); |
| 1771 | Py_DECREF(pyus_in); |
| 1772 | pyus_in = NULL; |
| 1773 | if (temp == NULL) |
| 1774 | goto error; |
| 1775 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0)); |
| 1776 | Py_DECREF(temp); |
| 1777 | if (pyus_out == NULL) |
| 1778 | goto error; |
| 1779 | result = microseconds_to_delta(pyus_out); |
| 1780 | Py_DECREF(pyus_out); |
| 1781 | error: |
| 1782 | Py_XDECREF(pyus_in); |
| 1783 | Py_XDECREF(ratio); |
| 1784 | |
| 1785 | return result; |
| 1786 | } |
| 1787 | |
| 1788 | static PyObject * |
| 1789 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 1790 | { |
| 1791 | PyObject *result; |
| 1792 | PyObject *pyus_in, *pyus_out; |
| 1793 | pyus_in = delta_to_microseconds(delta); |
| 1794 | if (pyus_in == NULL) |
| 1795 | return NULL; |
| 1796 | pyus_out = divide_nearest(pyus_in, i); |
| 1797 | Py_DECREF(pyus_in); |
| 1798 | if (pyus_out == NULL) |
| 1799 | return NULL; |
| 1800 | result = microseconds_to_delta(pyus_out); |
| 1801 | Py_DECREF(pyus_out); |
| 1802 | |
| 1803 | return result; |
| 1804 | } |
| 1805 | |
| 1806 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1807 | delta_add(PyObject *left, PyObject *right) |
| 1808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | PyObject *result = Py_NotImplemented; |
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 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1812 | /* delta + delta */ |
| 1813 | /* The C-level additions can't overflow because of the |
| 1814 | * invariant bounds. |
| 1815 | */ |
| 1816 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 1817 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 1818 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 1819 | GET_TD_MICROSECONDS(right); |
| 1820 | result = new_delta(days, seconds, microseconds, 1); |
| 1821 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | if (result == Py_NotImplemented) |
| 1824 | Py_INCREF(result); |
| 1825 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | static PyObject * |
| 1829 | delta_negative(PyDateTime_Delta *self) |
| 1830 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1831 | return new_delta(-GET_TD_DAYS(self), |
| 1832 | -GET_TD_SECONDS(self), |
| 1833 | -GET_TD_MICROSECONDS(self), |
| 1834 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | static PyObject * |
| 1838 | delta_positive(PyDateTime_Delta *self) |
| 1839 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | /* Could optimize this (by returning self) if this isn't a |
| 1841 | * subclass -- but who uses unary + ? Approximately nobody. |
| 1842 | */ |
| 1843 | return new_delta(GET_TD_DAYS(self), |
| 1844 | GET_TD_SECONDS(self), |
| 1845 | GET_TD_MICROSECONDS(self), |
| 1846 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | static PyObject * |
| 1850 | delta_abs(PyDateTime_Delta *self) |
| 1851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1855 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | if (GET_TD_DAYS(self) < 0) |
| 1858 | result = delta_negative(self); |
| 1859 | else |
| 1860 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | static PyObject * |
| 1866 | delta_subtract(PyObject *left, PyObject *right) |
| 1867 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1868 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1871 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 1872 | /* The C-level additions can't overflow because of the |
| 1873 | * invariant bounds. |
| 1874 | */ |
| 1875 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 1876 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 1877 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 1878 | GET_TD_MICROSECONDS(right); |
| 1879 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | if (result == Py_NotImplemented) |
| 1883 | Py_INCREF(result); |
| 1884 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1887 | static int |
| 1888 | delta_cmp(PyObject *self, PyObject *other) |
| 1889 | { |
| 1890 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 1891 | if (diff == 0) { |
| 1892 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 1893 | if (diff == 0) |
| 1894 | diff = GET_TD_MICROSECONDS(self) - |
| 1895 | GET_TD_MICROSECONDS(other); |
| 1896 | } |
| 1897 | return diff; |
| 1898 | } |
| 1899 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1900 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1901 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1904 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | return diff_to_bool(diff, op); |
| 1906 | } |
| 1907 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1908 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1913 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1914 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1915 | delta_hash(PyDateTime_Delta *self) |
| 1916 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1917 | if (self->hashcode == -1) { |
| 1918 | PyObject *temp = delta_getstate(self); |
| 1919 | if (temp != NULL) { |
| 1920 | self->hashcode = PyObject_Hash(temp); |
| 1921 | Py_DECREF(temp); |
| 1922 | } |
| 1923 | } |
| 1924 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | static PyObject * |
| 1928 | delta_multiply(PyObject *left, PyObject *right) |
| 1929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | if (PyDelta_Check(left)) { |
| 1933 | /* delta * ??? */ |
| 1934 | if (PyLong_Check(right)) |
| 1935 | result = multiply_int_timedelta(right, |
| 1936 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1937 | else if (PyFloat_Check(right)) |
| 1938 | result = multiply_float_timedelta(right, |
| 1939 | (PyDateTime_Delta *) left); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | } |
| 1941 | else if (PyLong_Check(left)) |
| 1942 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1943 | (PyDateTime_Delta *) right); |
| 1944 | else if (PyFloat_Check(left)) |
| 1945 | result = multiply_float_timedelta(left, |
| 1946 | (PyDateTime_Delta *) right); |
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 | if (result == Py_NotImplemented) |
| 1949 | Py_INCREF(result); |
| 1950 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | static PyObject * |
| 1954 | delta_divide(PyObject *left, PyObject *right) |
| 1955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | if (PyDelta_Check(left)) { |
| 1959 | /* delta * ??? */ |
| 1960 | if (PyLong_Check(right)) |
| 1961 | result = divide_timedelta_int( |
| 1962 | (PyDateTime_Delta *)left, |
| 1963 | right); |
| 1964 | else if (PyDelta_Check(right)) |
| 1965 | result = divide_timedelta_timedelta( |
| 1966 | (PyDateTime_Delta *)left, |
| 1967 | (PyDateTime_Delta *)right); |
| 1968 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | if (result == Py_NotImplemented) |
| 1971 | Py_INCREF(result); |
| 1972 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1975 | static PyObject * |
| 1976 | delta_truedivide(PyObject *left, PyObject *right) |
| 1977 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | if (PyDelta_Check(left)) { |
| 1981 | if (PyDelta_Check(right)) |
| 1982 | result = truedivide_timedelta_timedelta( |
| 1983 | (PyDateTime_Delta *)left, |
| 1984 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1985 | else if (PyFloat_Check(right)) |
| 1986 | result = truedivide_timedelta_float( |
| 1987 | (PyDateTime_Delta *)left, right); |
| 1988 | else if (PyLong_Check(right)) |
| 1989 | result = truedivide_timedelta_int( |
| 1990 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | if (result == Py_NotImplemented) |
| 1994 | Py_INCREF(result); |
| 1995 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | static PyObject * |
| 1999 | delta_remainder(PyObject *left, PyObject *right) |
| 2000 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | PyObject *pyus_left; |
| 2002 | PyObject *pyus_right; |
| 2003 | PyObject *pyus_remainder; |
| 2004 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2005 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2006 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2007 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2010 | if (pyus_left == NULL) |
| 2011 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2014 | if (pyus_right == NULL) { |
| 2015 | Py_DECREF(pyus_left); |
| 2016 | return NULL; |
| 2017 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 2020 | Py_DECREF(pyus_left); |
| 2021 | Py_DECREF(pyus_right); |
| 2022 | if (pyus_remainder == NULL) |
| 2023 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | remainder = microseconds_to_delta(pyus_remainder); |
| 2026 | Py_DECREF(pyus_remainder); |
| 2027 | if (remainder == NULL) |
| 2028 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2030 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | static PyObject * |
| 2034 | delta_divmod(PyObject *left, PyObject *right) |
| 2035 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | PyObject *pyus_left; |
| 2037 | PyObject *pyus_right; |
| 2038 | PyObject *divmod; |
| 2039 | PyObject *delta; |
| 2040 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2041 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2042 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2043 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2046 | if (pyus_left == NULL) |
| 2047 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2050 | if (pyus_right == NULL) { |
| 2051 | Py_DECREF(pyus_left); |
| 2052 | return NULL; |
| 2053 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
| 2056 | Py_DECREF(pyus_left); |
| 2057 | Py_DECREF(pyus_right); |
| 2058 | if (divmod == NULL) |
| 2059 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | assert(PyTuple_Size(divmod) == 2); |
| 2062 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 2063 | if (delta == NULL) { |
| 2064 | Py_DECREF(divmod); |
| 2065 | return NULL; |
| 2066 | } |
| 2067 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 2068 | Py_DECREF(delta); |
| 2069 | Py_DECREF(divmod); |
| 2070 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2073 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 2074 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 2075 | * so far, and there are factor microseconds per current unit, the number |
| 2076 | * of which is given by num. num * factor is added to sofar in a |
| 2077 | * numerically careful way, and that's the result. Any fractional |
| 2078 | * microseconds left over (this can happen if num is a float type) are |
| 2079 | * added into *leftover. |
| 2080 | * Note that there are many ways this can give an error (NULL) return. |
| 2081 | */ |
| 2082 | static PyObject * |
| 2083 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2084 | double *leftover) |
| 2085 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | PyObject *prod; |
| 2087 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | if (PyLong_Check(num)) { |
| 2092 | prod = PyNumber_Multiply(num, factor); |
| 2093 | if (prod == NULL) |
| 2094 | return NULL; |
| 2095 | sum = PyNumber_Add(sofar, prod); |
| 2096 | Py_DECREF(prod); |
| 2097 | return sum; |
| 2098 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | if (PyFloat_Check(num)) { |
| 2101 | double dnum; |
| 2102 | double fracpart; |
| 2103 | double intpart; |
| 2104 | PyObject *x; |
| 2105 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | /* The Plan: decompose num into an integer part and a |
| 2108 | * fractional part, num = intpart + fracpart. |
| 2109 | * Then num * factor == |
| 2110 | * intpart * factor + fracpart * factor |
| 2111 | * and the LHS can be computed exactly in long arithmetic. |
| 2112 | * The RHS is again broken into an int part and frac part. |
| 2113 | * and the frac part is added into *leftover. |
| 2114 | */ |
| 2115 | dnum = PyFloat_AsDouble(num); |
| 2116 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2117 | return NULL; |
| 2118 | fracpart = modf(dnum, &intpart); |
| 2119 | x = PyLong_FromDouble(intpart); |
| 2120 | if (x == NULL) |
| 2121 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | prod = PyNumber_Multiply(x, factor); |
| 2124 | Py_DECREF(x); |
| 2125 | if (prod == NULL) |
| 2126 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | sum = PyNumber_Add(sofar, prod); |
| 2129 | Py_DECREF(prod); |
| 2130 | if (sum == NULL) |
| 2131 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | if (fracpart == 0.0) |
| 2134 | return sum; |
| 2135 | /* So far we've lost no information. Dealing with the |
| 2136 | * fractional part requires float arithmetic, and may |
| 2137 | * lose a little info. |
| 2138 | */ |
| 2139 | assert(PyLong_Check(factor)); |
| 2140 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | dnum *= fracpart; |
| 2143 | fracpart = modf(dnum, &intpart); |
| 2144 | x = PyLong_FromDouble(intpart); |
| 2145 | if (x == NULL) { |
| 2146 | Py_DECREF(sum); |
| 2147 | return NULL; |
| 2148 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | y = PyNumber_Add(sum, x); |
| 2151 | Py_DECREF(sum); |
| 2152 | Py_DECREF(x); |
| 2153 | *leftover += fracpart; |
| 2154 | return y; |
| 2155 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2157 | PyErr_Format(PyExc_TypeError, |
| 2158 | "unsupported type for timedelta %s component: %s", |
| 2159 | tag, Py_TYPE(num)->tp_name); |
| 2160 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | static PyObject * |
| 2164 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | /* Argument objects. */ |
| 2169 | PyObject *day = NULL; |
| 2170 | PyObject *second = NULL; |
| 2171 | PyObject *us = NULL; |
| 2172 | PyObject *ms = NULL; |
| 2173 | PyObject *minute = NULL; |
| 2174 | PyObject *hour = NULL; |
| 2175 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2178 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2179 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | static char *keywords[] = { |
| 2182 | "days", "seconds", "microseconds", "milliseconds", |
| 2183 | "minutes", "hours", "weeks", NULL |
| 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 (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2187 | keywords, |
| 2188 | &day, &second, &us, |
| 2189 | &ms, &minute, &hour, &week) == 0) |
| 2190 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | x = PyLong_FromLong(0); |
| 2193 | if (x == NULL) |
| 2194 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | #define CLEANUP \ |
| 2197 | Py_DECREF(x); \ |
| 2198 | x = y; \ |
| 2199 | if (x == NULL) \ |
| 2200 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | if (us) { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2203 | y = accum("microseconds", x, us, _PyLong_One, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | CLEANUP; |
| 2205 | } |
| 2206 | if (ms) { |
| 2207 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2208 | CLEANUP; |
| 2209 | } |
| 2210 | if (second) { |
| 2211 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2212 | CLEANUP; |
| 2213 | } |
| 2214 | if (minute) { |
| 2215 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2216 | CLEANUP; |
| 2217 | } |
| 2218 | if (hour) { |
| 2219 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2220 | CLEANUP; |
| 2221 | } |
| 2222 | if (day) { |
| 2223 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2224 | CLEANUP; |
| 2225 | } |
| 2226 | if (week) { |
| 2227 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2228 | CLEANUP; |
| 2229 | } |
| 2230 | if (leftover_us) { |
| 2231 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2232 | double whole_us = round(leftover_us); |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2233 | int x_is_odd; |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2234 | PyObject *temp; |
| 2235 | |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2236 | whole_us = round(leftover_us); |
| 2237 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2238 | /* We're exactly halfway between two integers. In order |
| 2239 | * to do round-half-to-even, we must determine whether x |
| 2240 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2241 | * code below uses bitwise and operation to check the last |
| 2242 | * bit. */ |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 2243 | temp = PyNumber_And(x, _PyLong_One); /* temp <- x & 1 */ |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2244 | if (temp == NULL) { |
| 2245 | Py_DECREF(x); |
| 2246 | goto Done; |
| 2247 | } |
| 2248 | x_is_odd = PyObject_IsTrue(temp); |
| 2249 | Py_DECREF(temp); |
| 2250 | if (x_is_odd == -1) { |
| 2251 | Py_DECREF(x); |
| 2252 | goto Done; |
| 2253 | } |
| 2254 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2255 | } |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2256 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2257 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | if (temp == NULL) { |
| 2260 | Py_DECREF(x); |
| 2261 | goto Done; |
| 2262 | } |
| 2263 | y = PyNumber_Add(x, temp); |
| 2264 | Py_DECREF(temp); |
| 2265 | CLEANUP; |
| 2266 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | self = microseconds_to_delta_ex(x, type); |
| 2269 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2270 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2272 | |
| 2273 | #undef CLEANUP |
| 2274 | } |
| 2275 | |
| 2276 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2277 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2278 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2279 | return (GET_TD_DAYS(self) != 0 |
| 2280 | || GET_TD_SECONDS(self) != 0 |
| 2281 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | static PyObject * |
| 2285 | delta_repr(PyDateTime_Delta *self) |
| 2286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | if (GET_TD_MICROSECONDS(self) != 0) |
| 2288 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2289 | Py_TYPE(self)->tp_name, |
| 2290 | GET_TD_DAYS(self), |
| 2291 | GET_TD_SECONDS(self), |
| 2292 | GET_TD_MICROSECONDS(self)); |
| 2293 | if (GET_TD_SECONDS(self) != 0) |
| 2294 | return PyUnicode_FromFormat("%s(%d, %d)", |
| 2295 | Py_TYPE(self)->tp_name, |
| 2296 | GET_TD_DAYS(self), |
| 2297 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | return PyUnicode_FromFormat("%s(%d)", |
| 2300 | Py_TYPE(self)->tp_name, |
| 2301 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | static PyObject * |
| 2305 | delta_str(PyDateTime_Delta *self) |
| 2306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | int us = GET_TD_MICROSECONDS(self); |
| 2308 | int seconds = GET_TD_SECONDS(self); |
| 2309 | int minutes = divmod(seconds, 60, &seconds); |
| 2310 | int hours = divmod(minutes, 60, &minutes); |
| 2311 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | if (days) { |
| 2314 | if (us) |
| 2315 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2316 | days, (days == 1 || days == -1) ? "" : "s", |
| 2317 | hours, minutes, seconds, us); |
| 2318 | else |
| 2319 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2320 | days, (days == 1 || days == -1) ? "" : "s", |
| 2321 | hours, minutes, seconds); |
| 2322 | } else { |
| 2323 | if (us) |
| 2324 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2325 | hours, minutes, seconds, us); |
| 2326 | else |
| 2327 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2328 | hours, minutes, seconds); |
| 2329 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2330 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2333 | /* Pickle support, a simple use of __reduce__. */ |
| 2334 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2335 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2336 | static PyObject * |
| 2337 | delta_getstate(PyDateTime_Delta *self) |
| 2338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2340 | GET_TD_SECONDS(self), |
| 2341 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2344 | static PyObject * |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2345 | delta_total_seconds(PyObject *self) |
| 2346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | PyObject *total_seconds; |
| 2348 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2351 | if (total_microseconds == NULL) |
| 2352 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2353 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2354 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2361 | delta_reduce(PyDateTime_Delta* self) |
| 2362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2367 | |
| 2368 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | {"days", T_INT, OFFSET(days), READONLY, |
| 2371 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2374 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
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 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2377 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2378 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2379 | }; |
| 2380 | |
| 2381 | static PyMethodDef delta_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
| 2383 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2386 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2389 | }; |
| 2390 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2391 | static const char delta_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2392 | PyDoc_STR("Difference between two datetime values."); |
| 2393 | |
| 2394 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | delta_add, /* nb_add */ |
| 2396 | delta_subtract, /* nb_subtract */ |
| 2397 | delta_multiply, /* nb_multiply */ |
| 2398 | delta_remainder, /* nb_remainder */ |
| 2399 | delta_divmod, /* nb_divmod */ |
| 2400 | 0, /* nb_power */ |
| 2401 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2402 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2403 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2404 | (inquiry)delta_bool, /* nb_bool */ |
| 2405 | 0, /*nb_invert*/ |
| 2406 | 0, /*nb_lshift*/ |
| 2407 | 0, /*nb_rshift*/ |
| 2408 | 0, /*nb_and*/ |
| 2409 | 0, /*nb_xor*/ |
| 2410 | 0, /*nb_or*/ |
| 2411 | 0, /*nb_int*/ |
| 2412 | 0, /*nb_reserved*/ |
| 2413 | 0, /*nb_float*/ |
| 2414 | 0, /*nb_inplace_add*/ |
| 2415 | 0, /*nb_inplace_subtract*/ |
| 2416 | 0, /*nb_inplace_multiply*/ |
| 2417 | 0, /*nb_inplace_remainder*/ |
| 2418 | 0, /*nb_inplace_power*/ |
| 2419 | 0, /*nb_inplace_lshift*/ |
| 2420 | 0, /*nb_inplace_rshift*/ |
| 2421 | 0, /*nb_inplace_and*/ |
| 2422 | 0, /*nb_inplace_xor*/ |
| 2423 | 0, /*nb_inplace_or*/ |
| 2424 | delta_divide, /* nb_floor_divide */ |
| 2425 | delta_truedivide, /* nb_true_divide */ |
| 2426 | 0, /* nb_inplace_floor_divide */ |
| 2427 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2428 | }; |
| 2429 | |
| 2430 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2432 | "datetime.timedelta", /* tp_name */ |
| 2433 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2434 | 0, /* tp_itemsize */ |
| 2435 | 0, /* tp_dealloc */ |
| 2436 | 0, /* tp_print */ |
| 2437 | 0, /* tp_getattr */ |
| 2438 | 0, /* tp_setattr */ |
| 2439 | 0, /* tp_reserved */ |
| 2440 | (reprfunc)delta_repr, /* tp_repr */ |
| 2441 | &delta_as_number, /* tp_as_number */ |
| 2442 | 0, /* tp_as_sequence */ |
| 2443 | 0, /* tp_as_mapping */ |
| 2444 | (hashfunc)delta_hash, /* tp_hash */ |
| 2445 | 0, /* tp_call */ |
| 2446 | (reprfunc)delta_str, /* tp_str */ |
| 2447 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2448 | 0, /* tp_setattro */ |
| 2449 | 0, /* tp_as_buffer */ |
| 2450 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2451 | delta_doc, /* tp_doc */ |
| 2452 | 0, /* tp_traverse */ |
| 2453 | 0, /* tp_clear */ |
| 2454 | delta_richcompare, /* tp_richcompare */ |
| 2455 | 0, /* tp_weaklistoffset */ |
| 2456 | 0, /* tp_iter */ |
| 2457 | 0, /* tp_iternext */ |
| 2458 | delta_methods, /* tp_methods */ |
| 2459 | delta_members, /* tp_members */ |
| 2460 | 0, /* tp_getset */ |
| 2461 | 0, /* tp_base */ |
| 2462 | 0, /* tp_dict */ |
| 2463 | 0, /* tp_descr_get */ |
| 2464 | 0, /* tp_descr_set */ |
| 2465 | 0, /* tp_dictoffset */ |
| 2466 | 0, /* tp_init */ |
| 2467 | 0, /* tp_alloc */ |
| 2468 | delta_new, /* tp_new */ |
| 2469 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2470 | }; |
| 2471 | |
| 2472 | /* |
| 2473 | * PyDateTime_Date implementation. |
| 2474 | */ |
| 2475 | |
| 2476 | /* Accessor properties. */ |
| 2477 | |
| 2478 | static PyObject * |
| 2479 | date_year(PyDateTime_Date *self, void *unused) |
| 2480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2481 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | static PyObject * |
| 2485 | date_month(PyDateTime_Date *self, void *unused) |
| 2486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | static PyObject * |
| 2491 | date_day(PyDateTime_Date *self, void *unused) |
| 2492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | {"year", (getter)date_year}, |
| 2498 | {"month", (getter)date_month}, |
| 2499 | {"day", (getter)date_day}, |
| 2500 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2501 | }; |
| 2502 | |
| 2503 | /* Constructors. */ |
| 2504 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2505 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2506 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2507 | static PyObject * |
| 2508 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2510 | PyObject *self = NULL; |
| 2511 | PyObject *state; |
| 2512 | int year; |
| 2513 | int month; |
| 2514 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | /* Check for invocation from pickle with __getstate__ state */ |
| 2517 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2518 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2519 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2520 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2521 | { |
| 2522 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2525 | if (me != NULL) { |
| 2526 | char *pdata = PyBytes_AS_STRING(state); |
| 2527 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2528 | me->hashcode = -1; |
| 2529 | } |
| 2530 | return (PyObject *)me; |
| 2531 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2534 | &year, &month, &day)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | self = new_date_ex(year, month, day, type); |
| 2536 | } |
| 2537 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | /* Return new date from localtime(t). */ |
| 2541 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2542 | date_local_from_object(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2543 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2544 | struct tm tm; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2546 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2547 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2549 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 2550 | if (_PyTime_localtime(t, &tm) != 0) |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2551 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2552 | |
| 2553 | return PyObject_CallFunction(cls, "iii", |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2554 | tm.tm_year + 1900, |
| 2555 | tm.tm_mon + 1, |
| 2556 | tm.tm_mday); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | /* Return new date from current time. |
| 2560 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2561 | * only way to be sure of that is to *call* time.time(). That's not |
| 2562 | * generally the same as calling C's time. |
| 2563 | */ |
| 2564 | static PyObject * |
| 2565 | date_today(PyObject *cls, PyObject *dummy) |
| 2566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | PyObject *time; |
| 2568 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2569 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2571 | time = time_time(); |
| 2572 | if (time == NULL) |
| 2573 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | /* Note well: today() is a class method, so this may not call |
| 2576 | * date.fromtimestamp. For example, it may call |
| 2577 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2578 | * time.time() delivers; if someone were gonzo about optimization, |
| 2579 | * date.today() could get away with plain C time(). |
| 2580 | */ |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 2581 | result = _PyObject_CallMethodIdObjArgs(cls, &PyId_fromtimestamp, |
| 2582 | time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | Py_DECREF(time); |
| 2584 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2588 | static PyObject * |
| 2589 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2590 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2591 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2593 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2594 | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
| 2595 | result = date_local_from_object(cls, timestamp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2600 | * the ordinal is out of range. |
| 2601 | */ |
| 2602 | static PyObject * |
| 2603 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2604 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | PyObject *result = NULL; |
| 2606 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2609 | int year; |
| 2610 | int month; |
| 2611 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | if (ordinal < 1) |
| 2614 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2615 | ">= 1"); |
| 2616 | else { |
| 2617 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2618 | result = PyObject_CallFunction(cls, "iii", |
| 2619 | year, month, day); |
| 2620 | } |
| 2621 | } |
| 2622 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | * Date arithmetic. |
| 2627 | */ |
| 2628 | |
| 2629 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2630 | * instead. |
| 2631 | */ |
| 2632 | static PyObject * |
| 2633 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2635 | PyObject *result = NULL; |
| 2636 | int year = GET_YEAR(date); |
| 2637 | int month = GET_MONTH(date); |
| 2638 | int deltadays = GET_TD_DAYS(delta); |
| 2639 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2640 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | if (normalize_date(&year, &month, &day) >= 0) |
| 2643 | result = new_date(year, month, day); |
| 2644 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | static PyObject * |
| 2648 | date_add(PyObject *left, PyObject *right) |
| 2649 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2650 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2651 | Py_RETURN_NOTIMPLEMENTED; |
| 2652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | if (PyDate_Check(left)) { |
| 2654 | /* date + ??? */ |
| 2655 | if (PyDelta_Check(right)) |
| 2656 | /* date + delta */ |
| 2657 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2658 | (PyDateTime_Delta *) right, |
| 2659 | 0); |
| 2660 | } |
| 2661 | else { |
| 2662 | /* ??? + date |
| 2663 | * 'right' must be one of us, or we wouldn't have been called |
| 2664 | */ |
| 2665 | if (PyDelta_Check(left)) |
| 2666 | /* delta + date */ |
| 2667 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2668 | (PyDateTime_Delta *) left, |
| 2669 | 0); |
| 2670 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2671 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | static PyObject * |
| 2675 | date_subtract(PyObject *left, PyObject *right) |
| 2676 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2677 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2678 | Py_RETURN_NOTIMPLEMENTED; |
| 2679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | if (PyDate_Check(left)) { |
| 2681 | if (PyDate_Check(right)) { |
| 2682 | /* date - date */ |
| 2683 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2684 | GET_MONTH(left), |
| 2685 | GET_DAY(left)); |
| 2686 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2687 | GET_MONTH(right), |
| 2688 | GET_DAY(right)); |
| 2689 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2690 | } |
| 2691 | if (PyDelta_Check(right)) { |
| 2692 | /* date - delta */ |
| 2693 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2694 | (PyDateTime_Delta *) right, |
| 2695 | 1); |
| 2696 | } |
| 2697 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2698 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | |
| 2702 | /* Various ways to turn a date into a string. */ |
| 2703 | |
| 2704 | static PyObject * |
| 2705 | date_repr(PyDateTime_Date *self) |
| 2706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2708 | Py_TYPE(self)->tp_name, |
| 2709 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | static PyObject * |
| 2713 | date_isoformat(PyDateTime_Date *self) |
| 2714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2716 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2719 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2720 | static PyObject * |
| 2721 | date_str(PyDateTime_Date *self) |
| 2722 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 2723 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | |
| 2727 | static PyObject * |
| 2728 | date_ctime(PyDateTime_Date *self) |
| 2729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | static PyObject * |
| 2734 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | /* This method can be inherited, and needs to call the |
| 2737 | * timetuple() method appropriate to self's class. |
| 2738 | */ |
| 2739 | PyObject *result; |
| 2740 | PyObject *tuple; |
| 2741 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2742 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 2746 | &format)) |
| 2747 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2748 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 2749 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | if (tuple == NULL) |
| 2751 | return NULL; |
| 2752 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2753 | (PyObject *)self); |
| 2754 | Py_DECREF(tuple); |
| 2755 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2758 | static PyObject * |
| 2759 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2764 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2767 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2768 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2769 | |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 2770 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_strftime, |
| 2771 | format, NULL); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2774 | /* ISO methods. */ |
| 2775 | |
| 2776 | static PyObject * |
| 2777 | date_isoweekday(PyDateTime_Date *self) |
| 2778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | static PyObject * |
| 2785 | date_isocalendar(PyDateTime_Date *self) |
| 2786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | int year = GET_YEAR(self); |
| 2788 | int week1_monday = iso_week1_monday(year); |
| 2789 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2790 | int week; |
| 2791 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | week = divmod(today - week1_monday, 7, &day); |
| 2794 | if (week < 0) { |
| 2795 | --year; |
| 2796 | week1_monday = iso_week1_monday(year); |
| 2797 | week = divmod(today - week1_monday, 7, &day); |
| 2798 | } |
| 2799 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2800 | ++year; |
| 2801 | week = 0; |
| 2802 | } |
| 2803 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /* Miscellaneous methods. */ |
| 2807 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2808 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2809 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | if (PyDate_Check(other)) { |
| 2812 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2813 | ((PyDateTime_Date *)other)->data, |
| 2814 | _PyDateTime_DATE_DATASIZE); |
| 2815 | return diff_to_bool(diff, op); |
| 2816 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2817 | else |
| 2818 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | static PyObject * |
| 2822 | date_timetuple(PyDateTime_Date *self) |
| 2823 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | return build_struct_time(GET_YEAR(self), |
| 2825 | GET_MONTH(self), |
| 2826 | GET_DAY(self), |
| 2827 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2830 | static PyObject * |
| 2831 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | PyObject *clone; |
| 2834 | PyObject *tuple; |
| 2835 | int year = GET_YEAR(self); |
| 2836 | int month = GET_MONTH(self); |
| 2837 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2840 | &year, &month, &day)) |
| 2841 | return NULL; |
| 2842 | tuple = Py_BuildValue("iii", year, month, day); |
| 2843 | if (tuple == NULL) |
| 2844 | return NULL; |
| 2845 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 2846 | Py_DECREF(tuple); |
| 2847 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2850 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2851 | generic_hash(unsigned char *data, int len) |
| 2852 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 2853 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | |
| 2857 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2858 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2859 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2860 | date_hash(PyDateTime_Date *self) |
| 2861 | { |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 2862 | if (self->hashcode == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | self->hashcode = generic_hash( |
| 2864 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 2865 | } |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | static PyObject * |
| 2871 | date_toordinal(PyDateTime_Date *self) |
| 2872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 2874 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | static PyObject * |
| 2878 | date_weekday(PyDateTime_Date *self) |
| 2879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2885 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2886 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2887 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2888 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2889 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | PyObject* field; |
| 2892 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 2893 | _PyDateTime_DATE_DATASIZE); |
| 2894 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2898 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2905 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2908 | METH_CLASS, |
| 2909 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2910 | "time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2912 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2913 | METH_CLASS, |
| 2914 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2915 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2918 | PyDoc_STR("Current date or datetime: same as " |
| 2919 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | /* Instance methods: */ |
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 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2924 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 2927 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2929 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2930 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2933 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2936 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2937 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2940 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2942 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2943 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2944 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2947 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2948 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2950 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2951 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2952 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 2955 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2958 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2960 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2961 | }; |
| 2962 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2963 | static const char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2964 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2965 | |
| 2966 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | date_add, /* nb_add */ |
| 2968 | date_subtract, /* nb_subtract */ |
| 2969 | 0, /* nb_multiply */ |
| 2970 | 0, /* nb_remainder */ |
| 2971 | 0, /* nb_divmod */ |
| 2972 | 0, /* nb_power */ |
| 2973 | 0, /* nb_negative */ |
| 2974 | 0, /* nb_positive */ |
| 2975 | 0, /* nb_absolute */ |
| 2976 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2977 | }; |
| 2978 | |
| 2979 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2981 | "datetime.date", /* tp_name */ |
| 2982 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2983 | 0, /* tp_itemsize */ |
| 2984 | 0, /* tp_dealloc */ |
| 2985 | 0, /* tp_print */ |
| 2986 | 0, /* tp_getattr */ |
| 2987 | 0, /* tp_setattr */ |
| 2988 | 0, /* tp_reserved */ |
| 2989 | (reprfunc)date_repr, /* tp_repr */ |
| 2990 | &date_as_number, /* tp_as_number */ |
| 2991 | 0, /* tp_as_sequence */ |
| 2992 | 0, /* tp_as_mapping */ |
| 2993 | (hashfunc)date_hash, /* tp_hash */ |
| 2994 | 0, /* tp_call */ |
| 2995 | (reprfunc)date_str, /* tp_str */ |
| 2996 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2997 | 0, /* tp_setattro */ |
| 2998 | 0, /* tp_as_buffer */ |
| 2999 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3000 | date_doc, /* tp_doc */ |
| 3001 | 0, /* tp_traverse */ |
| 3002 | 0, /* tp_clear */ |
| 3003 | date_richcompare, /* tp_richcompare */ |
| 3004 | 0, /* tp_weaklistoffset */ |
| 3005 | 0, /* tp_iter */ |
| 3006 | 0, /* tp_iternext */ |
| 3007 | date_methods, /* tp_methods */ |
| 3008 | 0, /* tp_members */ |
| 3009 | date_getset, /* tp_getset */ |
| 3010 | 0, /* tp_base */ |
| 3011 | 0, /* tp_dict */ |
| 3012 | 0, /* tp_descr_get */ |
| 3013 | 0, /* tp_descr_set */ |
| 3014 | 0, /* tp_dictoffset */ |
| 3015 | 0, /* tp_init */ |
| 3016 | 0, /* tp_alloc */ |
| 3017 | date_new, /* tp_new */ |
| 3018 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3019 | }; |
| 3020 | |
| 3021 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3022 | * PyDateTime_TZInfo implementation. |
| 3023 | */ |
| 3024 | |
| 3025 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 3026 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 3027 | * 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] | 3028 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3029 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 3030 | * |
| 3031 | * Note: For reasons having to do with pickling of subclasses, we have |
| 3032 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 3033 | * in the Python implementation (__init__() could raise NotImplementedError |
| 3034 | * there without ill effect), but doing so in the C implementation hit a |
| 3035 | * brick wall. |
| 3036 | */ |
| 3037 | |
| 3038 | static PyObject * |
| 3039 | tzinfo_nogo(const char* methodname) |
| 3040 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3041 | PyErr_Format(PyExc_NotImplementedError, |
| 3042 | "a tzinfo subclass must implement %s()", |
| 3043 | methodname); |
| 3044 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | /* Methods. A subclass must implement these. */ |
| 3048 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3049 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3050 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 3051 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3055 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3056 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 3057 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3061 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3062 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 3063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3067 | |
| 3068 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 3069 | PyDateTime_Delta *delta, |
| 3070 | int factor); |
| 3071 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 3072 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 3073 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3074 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3075 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3076 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3077 | PyObject *result = NULL; |
| 3078 | PyObject *off = NULL, *dst = NULL; |
| 3079 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3080 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3081 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | PyErr_SetString(PyExc_TypeError, |
| 3083 | "fromutc: argument must be a datetime"); |
| 3084 | return NULL; |
| 3085 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3086 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3088 | "is not self"); |
| 3089 | return NULL; |
| 3090 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3091 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3092 | off = datetime_utcoffset(dt, NULL); |
| 3093 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3095 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3097 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3098 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3099 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3100 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3101 | dst = datetime_dst(dt, NULL); |
| 3102 | if (dst == NULL) |
| 3103 | goto Fail; |
| 3104 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3106 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3107 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3109 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3110 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3111 | if (delta == NULL) |
| 3112 | goto Fail; |
| 3113 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3116 | |
| 3117 | Py_DECREF(dst); |
| 3118 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3119 | if (dst == NULL) |
| 3120 | goto Fail; |
| 3121 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | goto Inconsistent; |
Alexander Belopolsky | c79447b | 2015-09-27 21:41:55 -0400 | [diff] [blame] | 3123 | if (delta_bool((PyDateTime_Delta *)dst) != 0) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3124 | Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 3125 | (PyDateTime_Delta *)dst, 1)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3126 | if (result == NULL) |
| 3127 | goto Fail; |
| 3128 | } |
| 3129 | Py_DECREF(delta); |
| 3130 | Py_DECREF(dst); |
| 3131 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3133 | |
| 3134 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3136 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | /* fall thru to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3139 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3140 | Py_XDECREF(off); |
| 3141 | Py_XDECREF(dst); |
| 3142 | Py_XDECREF(delta); |
| 3143 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3144 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3147 | /* |
| 3148 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3149 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3150 | */ |
| 3151 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3152 | static PyObject * |
| 3153 | tzinfo_reduce(PyObject *self) |
| 3154 | { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3155 | PyObject *args, *state; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3157 | _Py_IDENTIFIER(__getinitargs__); |
| 3158 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3159 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3160 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | if (getinitargs != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3162 | args = _PyObject_CallNoArg(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | Py_DECREF(getinitargs); |
| 3164 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | return NULL; |
| 3166 | } |
| 3167 | } |
| 3168 | else { |
| 3169 | PyErr_Clear(); |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3170 | |
| 3171 | args = PyTuple_New(0); |
| 3172 | if (args == NULL) { |
| 3173 | return NULL; |
| 3174 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3176 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3177 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | if (getstate != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3179 | state = _PyObject_CallNoArg(getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | Py_DECREF(getstate); |
| 3181 | if (state == NULL) { |
| 3182 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | return NULL; |
| 3184 | } |
| 3185 | } |
| 3186 | else { |
| 3187 | PyObject **dictptr; |
| 3188 | PyErr_Clear(); |
| 3189 | state = Py_None; |
| 3190 | dictptr = _PyObject_GetDictPtr(self); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3191 | if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | state = *dictptr; |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3193 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | Py_INCREF(state); |
| 3195 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3197 | if (state == Py_None) { |
| 3198 | Py_DECREF(state); |
| 3199 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3200 | } |
| 3201 | else |
| 3202 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3203 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3204 | |
| 3205 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3208 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3211 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3212 | "values indicating West of UTC")}, |
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 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 3215 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3218 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 3221 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3224 | }; |
| 3225 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3226 | static const char tzinfo_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3227 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3228 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3229 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3231 | "datetime.tzinfo", /* tp_name */ |
| 3232 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3233 | 0, /* tp_itemsize */ |
| 3234 | 0, /* tp_dealloc */ |
| 3235 | 0, /* tp_print */ |
| 3236 | 0, /* tp_getattr */ |
| 3237 | 0, /* tp_setattr */ |
| 3238 | 0, /* tp_reserved */ |
| 3239 | 0, /* tp_repr */ |
| 3240 | 0, /* tp_as_number */ |
| 3241 | 0, /* tp_as_sequence */ |
| 3242 | 0, /* tp_as_mapping */ |
| 3243 | 0, /* tp_hash */ |
| 3244 | 0, /* tp_call */ |
| 3245 | 0, /* tp_str */ |
| 3246 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3247 | 0, /* tp_setattro */ |
| 3248 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3249 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | tzinfo_doc, /* tp_doc */ |
| 3251 | 0, /* tp_traverse */ |
| 3252 | 0, /* tp_clear */ |
| 3253 | 0, /* tp_richcompare */ |
| 3254 | 0, /* tp_weaklistoffset */ |
| 3255 | 0, /* tp_iter */ |
| 3256 | 0, /* tp_iternext */ |
| 3257 | tzinfo_methods, /* tp_methods */ |
| 3258 | 0, /* tp_members */ |
| 3259 | 0, /* tp_getset */ |
| 3260 | 0, /* tp_base */ |
| 3261 | 0, /* tp_dict */ |
| 3262 | 0, /* tp_descr_get */ |
| 3263 | 0, /* tp_descr_set */ |
| 3264 | 0, /* tp_dictoffset */ |
| 3265 | 0, /* tp_init */ |
| 3266 | 0, /* tp_alloc */ |
| 3267 | PyType_GenericNew, /* tp_new */ |
| 3268 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3269 | }; |
| 3270 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3271 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3272 | |
| 3273 | static PyObject * |
| 3274 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3275 | { |
| 3276 | PyObject *offset; |
| 3277 | PyObject *name = NULL; |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 3278 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|U:timezone", timezone_kws, |
| 3279 | &PyDateTime_DeltaType, &offset, &name)) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3280 | return new_timezone(offset, name); |
| 3281 | |
| 3282 | return NULL; |
| 3283 | } |
| 3284 | |
| 3285 | static void |
| 3286 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3287 | { |
| 3288 | Py_CLEAR(self->offset); |
| 3289 | Py_CLEAR(self->name); |
| 3290 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3291 | } |
| 3292 | |
| 3293 | static PyObject * |
| 3294 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3295 | PyDateTime_TimeZone *other, int op) |
| 3296 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3297 | if (op != Py_EQ && op != Py_NE) |
| 3298 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3299 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 3300 | if (op == Py_EQ) |
| 3301 | Py_RETURN_FALSE; |
| 3302 | else |
| 3303 | Py_RETURN_TRUE; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3304 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3305 | return delta_richcompare(self->offset, other->offset, op); |
| 3306 | } |
| 3307 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3308 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3309 | timezone_hash(PyDateTime_TimeZone *self) |
| 3310 | { |
| 3311 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3312 | } |
| 3313 | |
| 3314 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3315 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3316 | otherwise. |
| 3317 | */ |
| 3318 | static int |
| 3319 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3320 | { |
| 3321 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3322 | return 0; |
| 3323 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3324 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3325 | return -1; |
| 3326 | } |
| 3327 | |
| 3328 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3329 | timezone_repr(PyDateTime_TimeZone *self) |
| 3330 | { |
| 3331 | /* Note that although timezone is not subclassable, it is convenient |
| 3332 | to use Py_TYPE(self)->tp_name here. */ |
| 3333 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3334 | |
| 3335 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3336 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3337 | |
| 3338 | if (self->name == NULL) |
| 3339 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3340 | |
| 3341 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3342 | self->name); |
| 3343 | } |
| 3344 | |
| 3345 | |
| 3346 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3347 | timezone_str(PyDateTime_TimeZone *self) |
| 3348 | { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3349 | int hours, minutes, seconds; |
| 3350 | PyObject *offset; |
| 3351 | char sign; |
| 3352 | |
| 3353 | if (self->name != NULL) { |
| 3354 | Py_INCREF(self->name); |
| 3355 | return self->name; |
| 3356 | } |
Victor Stinner | 90fd895 | 2015-09-08 00:12:49 +0200 | [diff] [blame] | 3357 | if ((PyObject *)self == PyDateTime_TimeZone_UTC || |
Alexander Belopolsky | 7827a5b | 2015-09-06 13:07:21 -0400 | [diff] [blame] | 3358 | (GET_TD_DAYS(self->offset) == 0 && |
| 3359 | GET_TD_SECONDS(self->offset) == 0 && |
| 3360 | GET_TD_MICROSECONDS(self->offset) == 0)) |
| 3361 | return PyUnicode_FromString("UTC"); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3362 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3363 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3364 | sign = '-'; |
| 3365 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3366 | if (offset == NULL) |
| 3367 | return NULL; |
| 3368 | } |
| 3369 | else { |
| 3370 | sign = '+'; |
| 3371 | offset = self->offset; |
| 3372 | Py_INCREF(offset); |
| 3373 | } |
| 3374 | /* Offset is not negative here. */ |
| 3375 | seconds = GET_TD_SECONDS(offset); |
| 3376 | Py_DECREF(offset); |
| 3377 | minutes = divmod(seconds, 60, &seconds); |
| 3378 | hours = divmod(minutes, 60, &minutes); |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 3379 | /* XXX ignore sub-minute data, currently not allowed. */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3380 | assert(seconds == 0); |
| 3381 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | static PyObject * |
| 3385 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3386 | { |
| 3387 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3388 | return NULL; |
| 3389 | |
| 3390 | return timezone_str(self); |
| 3391 | } |
| 3392 | |
| 3393 | static PyObject * |
| 3394 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3395 | { |
| 3396 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3397 | return NULL; |
| 3398 | |
| 3399 | Py_INCREF(self->offset); |
| 3400 | return self->offset; |
| 3401 | } |
| 3402 | |
| 3403 | static PyObject * |
| 3404 | timezone_dst(PyObject *self, PyObject *dt) |
| 3405 | { |
| 3406 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3407 | return NULL; |
| 3408 | |
| 3409 | Py_RETURN_NONE; |
| 3410 | } |
| 3411 | |
| 3412 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3413 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3414 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3415 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3416 | PyErr_SetString(PyExc_TypeError, |
| 3417 | "fromutc: argument must be a datetime"); |
| 3418 | return NULL; |
| 3419 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3420 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3421 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3422 | "is not self"); |
| 3423 | return NULL; |
| 3424 | } |
| 3425 | |
| 3426 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3427 | } |
| 3428 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3429 | static PyObject * |
| 3430 | timezone_getinitargs(PyDateTime_TimeZone *self) |
| 3431 | { |
| 3432 | if (self->name == NULL) |
| 3433 | return Py_BuildValue("(O)", self->offset); |
| 3434 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3435 | } |
| 3436 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3437 | static PyMethodDef timezone_methods[] = { |
| 3438 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3439 | 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] | 3440 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3441 | |
| 3442 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3443 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3444 | |
| 3445 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3446 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3447 | |
| 3448 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3449 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3450 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3451 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3452 | PyDoc_STR("pickle support")}, |
| 3453 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3454 | {NULL, NULL} |
| 3455 | }; |
| 3456 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3457 | static const char timezone_doc[] = |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3458 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3459 | |
| 3460 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3461 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3462 | "datetime.timezone", /* tp_name */ |
| 3463 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3464 | 0, /* tp_itemsize */ |
| 3465 | (destructor)timezone_dealloc, /* tp_dealloc */ |
| 3466 | 0, /* tp_print */ |
| 3467 | 0, /* tp_getattr */ |
| 3468 | 0, /* tp_setattr */ |
| 3469 | 0, /* tp_reserved */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3470 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3471 | 0, /* tp_as_number */ |
| 3472 | 0, /* tp_as_sequence */ |
| 3473 | 0, /* tp_as_mapping */ |
| 3474 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3475 | 0, /* tp_call */ |
| 3476 | (reprfunc)timezone_str, /* tp_str */ |
| 3477 | 0, /* tp_getattro */ |
| 3478 | 0, /* tp_setattro */ |
| 3479 | 0, /* tp_as_buffer */ |
| 3480 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3481 | timezone_doc, /* tp_doc */ |
| 3482 | 0, /* tp_traverse */ |
| 3483 | 0, /* tp_clear */ |
| 3484 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3485 | 0, /* tp_weaklistoffset */ |
| 3486 | 0, /* tp_iter */ |
| 3487 | 0, /* tp_iternext */ |
| 3488 | timezone_methods, /* tp_methods */ |
| 3489 | 0, /* tp_members */ |
| 3490 | 0, /* tp_getset */ |
| 3491 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3492 | 0, /* tp_dict */ |
| 3493 | 0, /* tp_descr_get */ |
| 3494 | 0, /* tp_descr_set */ |
| 3495 | 0, /* tp_dictoffset */ |
| 3496 | 0, /* tp_init */ |
| 3497 | 0, /* tp_alloc */ |
| 3498 | timezone_new, /* tp_new */ |
| 3499 | }; |
| 3500 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3501 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3502 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3503 | */ |
| 3504 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3505 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3506 | */ |
| 3507 | |
| 3508 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3509 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3514 | static PyObject * |
| 3515 | time_minute(PyDateTime_Time *self, void *unused) |
| 3516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | /* The name time_second conflicted with some platform header file. */ |
| 3521 | static PyObject * |
| 3522 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | static PyObject * |
| 3528 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | static PyObject * |
| 3534 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3535 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3537 | Py_INCREF(result); |
| 3538 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3541 | static PyObject * |
| 3542 | time_fold(PyDateTime_Time *self, void *unused) |
| 3543 | { |
| 3544 | return PyLong_FromLong(TIME_GET_FOLD(self)); |
| 3545 | } |
| 3546 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3547 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | {"hour", (getter)time_hour}, |
| 3549 | {"minute", (getter)time_minute}, |
| 3550 | {"second", (getter)py_time_second}, |
| 3551 | {"microsecond", (getter)time_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3552 | {"tzinfo", (getter)time_tzinfo}, |
| 3553 | {"fold", (getter)time_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3555 | }; |
| 3556 | |
| 3557 | /* |
| 3558 | * Constructors. |
| 3559 | */ |
| 3560 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3561 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3562 | "tzinfo", "fold", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3563 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3564 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3565 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | PyObject *self = NULL; |
| 3568 | PyObject *state; |
| 3569 | int hour = 0; |
| 3570 | int minute = 0; |
| 3571 | int second = 0; |
| 3572 | int usecond = 0; |
| 3573 | PyObject *tzinfo = Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3574 | int fold = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | /* Check for invocation from pickle with __getstate__ state */ |
| 3577 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3578 | PyTuple_GET_SIZE(args) <= 2 && |
| 3579 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3580 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3581 | (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | { |
| 3583 | PyDateTime_Time *me; |
| 3584 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3586 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3587 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3588 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3589 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3590 | "tzinfo state arg"); |
| 3591 | return NULL; |
| 3592 | } |
| 3593 | } |
| 3594 | aware = (char)(tzinfo != Py_None); |
| 3595 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3596 | if (me != NULL) { |
| 3597 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3600 | me->hashcode = -1; |
| 3601 | me->hastzinfo = aware; |
| 3602 | if (aware) { |
| 3603 | Py_INCREF(tzinfo); |
| 3604 | me->tzinfo = tzinfo; |
| 3605 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3606 | if (pdata[0] & (1 << 7)) { |
| 3607 | me->data[0] -= 128; |
| 3608 | me->fold = 1; |
| 3609 | } |
| 3610 | else { |
| 3611 | me->fold = 0; |
| 3612 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | } |
| 3614 | return (PyObject *)me; |
| 3615 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3616 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3617 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | &hour, &minute, &second, &usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3619 | &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3620 | self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, |
| 3621 | type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | } |
| 3623 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | /* |
| 3627 | * Destructor. |
| 3628 | */ |
| 3629 | |
| 3630 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3631 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3633 | if (HASTZINFO(self)) { |
| 3634 | Py_XDECREF(self->tzinfo); |
| 3635 | } |
| 3636 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3640 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3641 | */ |
| 3642 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3643 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3644 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3645 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 3646 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3650 | time_dst(PyObject *self, PyObject *unused) { |
| 3651 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3655 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3656 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3660 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3661 | */ |
| 3662 | |
| 3663 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3664 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3666 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3667 | int h = TIME_GET_HOUR(self); |
| 3668 | int m = TIME_GET_MINUTE(self); |
| 3669 | int s = TIME_GET_SECOND(self); |
| 3670 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3671 | int fold = TIME_GET_FOLD(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3672 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 | if (us) |
| 3675 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3676 | type_name, h, m, s, us); |
| 3677 | else if (s) |
| 3678 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3679 | type_name, h, m, s); |
| 3680 | else |
| 3681 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3682 | if (result != NULL && HASTZINFO(self)) |
| 3683 | result = append_keyword_tzinfo(result, self->tzinfo); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3684 | if (result != NULL && fold) |
| 3685 | result = append_keyword_fold(result, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3686 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3689 | static PyObject * |
| 3690 | time_str(PyDateTime_Time *self) |
| 3691 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3692 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3693 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3694 | |
| 3695 | static PyObject * |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3696 | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | char buf[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3699 | char *timespec = NULL; |
| 3700 | static char *keywords[] = {"timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3701 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3702 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3703 | static char *specs[][2] = { |
| 3704 | {"hours", "%02d"}, |
| 3705 | {"minutes", "%02d:%02d"}, |
| 3706 | {"seconds", "%02d:%02d:%02d"}, |
| 3707 | {"milliseconds", "%02d:%02d:%02d.%03d"}, |
| 3708 | {"microseconds", "%02d:%02d:%02d.%06d"}, |
| 3709 | }; |
| 3710 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3711 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3712 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) |
| 3713 | return NULL; |
| 3714 | |
| 3715 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 3716 | if (us == 0) { |
| 3717 | /* seconds */ |
| 3718 | given_spec = 2; |
| 3719 | } |
| 3720 | else { |
| 3721 | /* microseconds */ |
| 3722 | given_spec = 4; |
| 3723 | } |
| 3724 | } |
| 3725 | else { |
| 3726 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 3727 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 3728 | if (given_spec == 3) { |
| 3729 | /* milliseconds */ |
| 3730 | us = us / 1000; |
| 3731 | } |
| 3732 | break; |
| 3733 | } |
| 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 3738 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 3739 | return NULL; |
| 3740 | } |
| 3741 | else { |
| 3742 | result = PyUnicode_FromFormat(specs[given_spec][1], |
| 3743 | TIME_GET_HOUR(self), TIME_GET_MINUTE(self), |
| 3744 | TIME_GET_SECOND(self), us); |
| 3745 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3746 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3747 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3748 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | /* We need to append the UTC offset. */ |
| 3751 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 3752 | Py_None) < 0) { |
| 3753 | Py_DECREF(result); |
| 3754 | return NULL; |
| 3755 | } |
| 3756 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 3757 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3760 | static PyObject * |
| 3761 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3763 | PyObject *result; |
| 3764 | PyObject *tuple; |
| 3765 | PyObject *format; |
| 3766 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3768 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3769 | &format)) |
| 3770 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | /* Python's strftime does insane things with the year part of the |
| 3773 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 3774 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3775 | */ |
| 3776 | tuple = Py_BuildValue("iiiiiiiii", |
| 3777 | 1900, 1, 1, /* year, month, day */ |
| 3778 | TIME_GET_HOUR(self), |
| 3779 | TIME_GET_MINUTE(self), |
| 3780 | TIME_GET_SECOND(self), |
| 3781 | 0, 1, -1); /* weekday, daynum, dst */ |
| 3782 | if (tuple == NULL) |
| 3783 | return NULL; |
| 3784 | assert(PyTuple_Size(tuple) == 9); |
| 3785 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3786 | Py_None); |
| 3787 | Py_DECREF(tuple); |
| 3788 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3789 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3790 | |
| 3791 | /* |
| 3792 | * Miscellaneous methods. |
| 3793 | */ |
| 3794 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3795 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3796 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3797 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3798 | PyObject *result = NULL; |
| 3799 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3800 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3801 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3802 | if (! PyTime_Check(other)) |
| 3803 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3804 | |
| 3805 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3806 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3807 | ((PyDateTime_Time *)other)->data, |
| 3808 | _PyDateTime_TIME_DATASIZE); |
| 3809 | return diff_to_bool(diff, op); |
| 3810 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3811 | offset1 = time_utcoffset(self, NULL); |
| 3812 | if (offset1 == NULL) |
| 3813 | return NULL; |
| 3814 | offset2 = time_utcoffset(other, NULL); |
| 3815 | if (offset2 == NULL) |
| 3816 | goto done; |
| 3817 | /* If they're both naive, or both aware and have the same offsets, |
| 3818 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3819 | * offset2 == Py_None at this point. |
| 3820 | */ |
| 3821 | if ((offset1 == offset2) || |
| 3822 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 3823 | delta_cmp(offset1, offset2) == 0)) { |
| 3824 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3825 | ((PyDateTime_Time *)other)->data, |
| 3826 | _PyDateTime_TIME_DATASIZE); |
| 3827 | result = diff_to_bool(diff, op); |
| 3828 | } |
| 3829 | /* The hard case: both aware with different UTC offsets */ |
| 3830 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 3831 | int offsecs1, offsecs2; |
| 3832 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3833 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 3834 | TIME_GET_MINUTE(self) * 60 + |
| 3835 | TIME_GET_SECOND(self) - |
| 3836 | GET_TD_DAYS(offset1) * 86400 - |
| 3837 | GET_TD_SECONDS(offset1); |
| 3838 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 3839 | TIME_GET_MINUTE(other) * 60 + |
| 3840 | TIME_GET_SECOND(other) - |
| 3841 | GET_TD_DAYS(offset2) * 86400 - |
| 3842 | GET_TD_SECONDS(offset2); |
| 3843 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3844 | if (diff == 0) |
| 3845 | diff = TIME_GET_MICROSECOND(self) - |
| 3846 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3847 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3848 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 3849 | else if (op == Py_EQ) { |
| 3850 | result = Py_False; |
| 3851 | Py_INCREF(result); |
| 3852 | } |
| 3853 | else if (op == Py_NE) { |
| 3854 | result = Py_True; |
| 3855 | Py_INCREF(result); |
| 3856 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3857 | else { |
| 3858 | PyErr_SetString(PyExc_TypeError, |
| 3859 | "can't compare offset-naive and " |
| 3860 | "offset-aware times"); |
| 3861 | } |
| 3862 | done: |
| 3863 | Py_DECREF(offset1); |
| 3864 | Py_XDECREF(offset2); |
| 3865 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3868 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3869 | time_hash(PyDateTime_Time *self) |
| 3870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3871 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3872 | PyObject *offset, *self0; |
Victor Stinner | 423c16b | 2017-01-03 23:47:12 +0100 | [diff] [blame] | 3873 | if (TIME_GET_FOLD(self)) { |
| 3874 | self0 = new_time_ex2(TIME_GET_HOUR(self), |
| 3875 | TIME_GET_MINUTE(self), |
| 3876 | TIME_GET_SECOND(self), |
| 3877 | TIME_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3878 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3879 | 0, Py_TYPE(self)); |
| 3880 | if (self0 == NULL) |
| 3881 | return -1; |
| 3882 | } |
| 3883 | else { |
| 3884 | self0 = (PyObject *)self; |
| 3885 | Py_INCREF(self0); |
| 3886 | } |
| 3887 | offset = time_utcoffset(self0, NULL); |
| 3888 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3889 | |
| 3890 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3894 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3895 | self->hashcode = generic_hash( |
| 3896 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3897 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3898 | PyObject *temp1, *temp2; |
| 3899 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3901 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 3902 | TIME_GET_MINUTE(self) * 60 + |
| 3903 | TIME_GET_SECOND(self); |
| 3904 | microseconds = TIME_GET_MICROSECOND(self); |
| 3905 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 3906 | if (temp1 == NULL) { |
| 3907 | Py_DECREF(offset); |
| 3908 | return -1; |
| 3909 | } |
| 3910 | temp2 = delta_subtract(temp1, offset); |
| 3911 | Py_DECREF(temp1); |
| 3912 | if (temp2 == NULL) { |
| 3913 | Py_DECREF(offset); |
| 3914 | return -1; |
| 3915 | } |
| 3916 | self->hashcode = PyObject_Hash(temp2); |
| 3917 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3918 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3919 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3920 | } |
| 3921 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3922 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3923 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3924 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3925 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3926 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3927 | PyObject *clone; |
| 3928 | PyObject *tuple; |
| 3929 | int hh = TIME_GET_HOUR(self); |
| 3930 | int mm = TIME_GET_MINUTE(self); |
| 3931 | int ss = TIME_GET_SECOND(self); |
| 3932 | int us = TIME_GET_MICROSECOND(self); |
| 3933 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3934 | int fold = TIME_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3935 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3936 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3937 | time_kws, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3938 | &hh, &mm, &ss, &us, &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3939 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 3940 | if (fold != 0 && fold != 1) { |
| 3941 | PyErr_SetString(PyExc_ValueError, |
| 3942 | "fold must be either 0 or 1"); |
| 3943 | return NULL; |
| 3944 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3945 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3946 | if (tuple == NULL) |
| 3947 | return NULL; |
| 3948 | clone = time_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3949 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3950 | TIME_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3951 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | Py_DECREF(tuple); |
| 3953 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3956 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3957 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3958 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3959 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3960 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3961 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3962 | */ |
| 3963 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3964 | time_getstate(PyDateTime_Time *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3965 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3966 | PyObject *basestate; |
| 3967 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 3970 | _PyDateTime_TIME_DATASIZE); |
| 3971 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3972 | if (proto > 3 && TIME_GET_FOLD(self)) |
| 3973 | /* Set the first bit of the first byte */ |
| 3974 | PyBytes_AS_STRING(basestate)[0] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3975 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3976 | result = PyTuple_Pack(1, basestate); |
| 3977 | else |
| 3978 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 3979 | Py_DECREF(basestate); |
| 3980 | } |
| 3981 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 3985 | time_reduce_ex(PyDateTime_Time *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3986 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 3987 | int proto; |
| 3988 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3989 | return NULL; |
| 3990 | |
| 3991 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 3994 | static PyObject * |
| 3995 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
| 3996 | { |
| 3997 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); |
| 3998 | } |
| 3999 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4000 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4001 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4002 | {"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 4003 | PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" |
| 4004 | "[+HH:MM].\n\n" |
| 4005 | "timespec specifies what components of the time to include.\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4007 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 4008 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 4011 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 4012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4013 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 4014 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4016 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 4017 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4019 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 4020 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4022 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 4023 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4024 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4025 | {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4026 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4027 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4028 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 4029 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 4030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4031 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4032 | }; |
| 4033 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4034 | static const char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4035 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 4036 | \n\ |
| 4037 | 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] | 4038 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4039 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4040 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4041 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4042 | "datetime.time", /* tp_name */ |
| 4043 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 4044 | 0, /* tp_itemsize */ |
| 4045 | (destructor)time_dealloc, /* tp_dealloc */ |
| 4046 | 0, /* tp_print */ |
| 4047 | 0, /* tp_getattr */ |
| 4048 | 0, /* tp_setattr */ |
| 4049 | 0, /* tp_reserved */ |
| 4050 | (reprfunc)time_repr, /* tp_repr */ |
Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 4051 | 0, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | 0, /* tp_as_sequence */ |
| 4053 | 0, /* tp_as_mapping */ |
| 4054 | (hashfunc)time_hash, /* tp_hash */ |
| 4055 | 0, /* tp_call */ |
| 4056 | (reprfunc)time_str, /* tp_str */ |
| 4057 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4058 | 0, /* tp_setattro */ |
| 4059 | 0, /* tp_as_buffer */ |
| 4060 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4061 | time_doc, /* tp_doc */ |
| 4062 | 0, /* tp_traverse */ |
| 4063 | 0, /* tp_clear */ |
| 4064 | time_richcompare, /* tp_richcompare */ |
| 4065 | 0, /* tp_weaklistoffset */ |
| 4066 | 0, /* tp_iter */ |
| 4067 | 0, /* tp_iternext */ |
| 4068 | time_methods, /* tp_methods */ |
| 4069 | 0, /* tp_members */ |
| 4070 | time_getset, /* tp_getset */ |
| 4071 | 0, /* tp_base */ |
| 4072 | 0, /* tp_dict */ |
| 4073 | 0, /* tp_descr_get */ |
| 4074 | 0, /* tp_descr_set */ |
| 4075 | 0, /* tp_dictoffset */ |
| 4076 | 0, /* tp_init */ |
| 4077 | time_alloc, /* tp_alloc */ |
| 4078 | time_new, /* tp_new */ |
| 4079 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4080 | }; |
| 4081 | |
| 4082 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4083 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4084 | */ |
| 4085 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4086 | /* Accessor properties. Properties for day, month, and year are inherited |
| 4087 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4088 | */ |
| 4089 | |
| 4090 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4091 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4092 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4093 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4096 | static PyObject * |
| 4097 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 4098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4099 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | static PyObject * |
| 4103 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 4104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | static PyObject * |
| 4109 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 4110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4111 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | static PyObject * |
| 4115 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 4116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 4118 | Py_INCREF(result); |
| 4119 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4122 | static PyObject * |
| 4123 | datetime_fold(PyDateTime_DateTime *self, void *unused) |
| 4124 | { |
| 4125 | return PyLong_FromLong(DATE_GET_FOLD(self)); |
| 4126 | } |
| 4127 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4128 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4129 | {"hour", (getter)datetime_hour}, |
| 4130 | {"minute", (getter)datetime_minute}, |
| 4131 | {"second", (getter)datetime_second}, |
| 4132 | {"microsecond", (getter)datetime_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4133 | {"tzinfo", (getter)datetime_tzinfo}, |
| 4134 | {"fold", (getter)datetime_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4135 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4136 | }; |
| 4137 | |
| 4138 | /* |
| 4139 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4140 | */ |
| 4141 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4142 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4143 | "year", "month", "day", "hour", "minute", "second", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4144 | "microsecond", "tzinfo", "fold", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4145 | }; |
| 4146 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4147 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4148 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4149 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | PyObject *self = NULL; |
| 4151 | PyObject *state; |
| 4152 | int year; |
| 4153 | int month; |
| 4154 | int day; |
| 4155 | int hour = 0; |
| 4156 | int minute = 0; |
| 4157 | int second = 0; |
| 4158 | int usecond = 0; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4159 | int fold = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4160 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4162 | /* Check for invocation from pickle with __getstate__ state */ |
| 4163 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 4164 | PyTuple_GET_SIZE(args) <= 2 && |
| 4165 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 4166 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4167 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4168 | { |
| 4169 | PyDateTime_DateTime *me; |
| 4170 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4173 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4174 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 4175 | PyErr_SetString(PyExc_TypeError, "bad " |
| 4176 | "tzinfo state arg"); |
| 4177 | return NULL; |
| 4178 | } |
| 4179 | } |
| 4180 | aware = (char)(tzinfo != Py_None); |
| 4181 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4182 | if (me != NULL) { |
| 4183 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4186 | me->hashcode = -1; |
| 4187 | me->hastzinfo = aware; |
| 4188 | if (aware) { |
| 4189 | Py_INCREF(tzinfo); |
| 4190 | me->tzinfo = tzinfo; |
| 4191 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4192 | if (pdata[2] & (1 << 7)) { |
| 4193 | me->data[2] -= 128; |
| 4194 | me->fold = 1; |
| 4195 | } |
| 4196 | else { |
| 4197 | me->fold = 0; |
| 4198 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | } |
| 4200 | return (PyObject *)me; |
| 4201 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4202 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4203 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4204 | &year, &month, &day, &hour, &minute, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4205 | &second, &usecond, &tzinfo, &fold)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4206 | self = new_datetime_ex2(year, month, day, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4207 | hour, minute, second, usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4208 | tzinfo, fold, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4209 | } |
| 4210 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4213 | /* TM_FUNC is the shared type of _PyTime_localtime() and |
| 4214 | * _PyTime_gmtime(). */ |
| 4215 | typedef int (*TM_FUNC)(time_t timer, struct tm*); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4216 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4217 | /* As of version 2015f max fold in IANA database is |
| 4218 | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4219 | static long long max_fold_seconds = 24 * 3600; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4220 | /* NB: date(1970,1,1).toordinal() == 719163 */ |
Benjamin Peterson | ac965ca | 2016-09-18 18:12:21 -0700 | [diff] [blame] | 4221 | static long long epoch = 719163LL * 24 * 60 * 60; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4222 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4223 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4224 | utc_to_seconds(int year, int month, int day, |
| 4225 | int hour, int minute, int second) |
| 4226 | { |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4227 | long long ordinal; |
| 4228 | |
| 4229 | /* ymd_to_ord() doesn't support year <= 0 */ |
| 4230 | if (year < MINYEAR || year > MAXYEAR) { |
| 4231 | PyErr_Format(PyExc_ValueError, "year %i is out of range", year); |
| 4232 | return -1; |
| 4233 | } |
| 4234 | |
| 4235 | ordinal = ymd_to_ord(year, month, day); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4236 | return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; |
| 4237 | } |
| 4238 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4239 | static long long |
| 4240 | local(long long u) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4241 | { |
| 4242 | struct tm local_time; |
Alexander Belopolsky | 8e1d3a2 | 2016-07-25 13:54:51 -0400 | [diff] [blame] | 4243 | time_t t; |
| 4244 | u -= epoch; |
| 4245 | t = u; |
| 4246 | if (t != u) { |
| 4247 | PyErr_SetString(PyExc_OverflowError, |
| 4248 | "timestamp out of range for platform time_t"); |
| 4249 | return -1; |
| 4250 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4251 | if (_PyTime_localtime(t, &local_time) != 0) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4252 | return -1; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4253 | return utc_to_seconds(local_time.tm_year + 1900, |
| 4254 | local_time.tm_mon + 1, |
| 4255 | local_time.tm_mday, |
| 4256 | local_time.tm_hour, |
| 4257 | local_time.tm_min, |
| 4258 | local_time.tm_sec); |
| 4259 | } |
| 4260 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4261 | /* Internal helper. |
| 4262 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4263 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4264 | */ |
| 4265 | static PyObject * |
| 4266 | 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] | 4267 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4268 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4269 | struct tm tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4270 | int year, month, day, hour, minute, second, fold = 0; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4271 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4272 | if (f(timet, &tm) != 0) |
| 4273 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4274 | |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4275 | year = tm.tm_year + 1900; |
| 4276 | month = tm.tm_mon + 1; |
| 4277 | day = tm.tm_mday; |
| 4278 | hour = tm.tm_hour; |
| 4279 | minute = tm.tm_min; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4280 | /* The platform localtime/gmtime may insert leap seconds, |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4281 | * indicated by tm.tm_sec > 59. We don't care about them, |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4282 | * except to the extent that passing them on to the datetime |
| 4283 | * constructor would raise ValueError for a reason that |
| 4284 | * made no sense to the user. |
| 4285 | */ |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4286 | second = Py_MIN(59, tm.tm_sec); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4287 | |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4288 | /* local timezone requires to compute fold */ |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4289 | if (tzinfo == Py_None && f == _PyTime_localtime) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4290 | long long probe_seconds, result_seconds, transition; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4291 | |
| 4292 | result_seconds = utc_to_seconds(year, month, day, |
| 4293 | hour, minute, second); |
| 4294 | /* Probe max_fold_seconds to detect a fold. */ |
| 4295 | probe_seconds = local(epoch + timet - max_fold_seconds); |
| 4296 | if (probe_seconds == -1) |
| 4297 | return NULL; |
| 4298 | transition = result_seconds - probe_seconds - max_fold_seconds; |
| 4299 | if (transition < 0) { |
| 4300 | probe_seconds = local(epoch + timet + transition); |
| 4301 | if (probe_seconds == -1) |
| 4302 | return NULL; |
| 4303 | if (probe_seconds == result_seconds) |
| 4304 | fold = 1; |
| 4305 | } |
| 4306 | } |
| 4307 | return new_datetime_ex2(year, month, day, hour, |
| 4308 | minute, second, us, tzinfo, fold, |
| 4309 | (PyTypeObject *)cls); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | /* Internal helper. |
| 4313 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4314 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4315 | * have enough bits to cover a datetime's full range of precision, it's |
| 4316 | * better to call datetime_from_timet_and_us provided you have a way |
| 4317 | * to get that much precision (e.g., C time() isn't good enough). |
| 4318 | */ |
| 4319 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4320 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4321 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4324 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4325 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 4326 | if (_PyTime_ObjectToTimeval(timestamp, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 4327 | &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | return NULL; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4329 | |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4330 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | /* Internal helper. |
| 4334 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4335 | * gmtime for f as appropriate. |
| 4336 | */ |
| 4337 | static PyObject * |
| 4338 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4339 | { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4340 | _PyTime_t ts = _PyTime_GetSystemClock(); |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4341 | time_t secs; |
| 4342 | int us; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4343 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4344 | if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4345 | return NULL; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4346 | assert(0 <= us && us <= 999999); |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4347 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4348 | return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4351 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4352 | |
| 4353 | @classmethod |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4354 | datetime.datetime.now |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4355 | |
| 4356 | tz: object = None |
| 4357 | Timezone object. |
| 4358 | |
| 4359 | Returns new datetime object representing current time local to tz. |
| 4360 | |
| 4361 | If no tz is specified, uses local timezone. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4362 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4363 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4364 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4365 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4366 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4368 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4369 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4370 | /* Return best possible local time -- this isn't constrained by the |
| 4371 | * precision of a timestamp. |
| 4372 | */ |
| 4373 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4375 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4376 | self = datetime_best_possible((PyObject *)type, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4377 | tz == Py_None ? _PyTime_localtime : |
| 4378 | _PyTime_gmtime, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4379 | tz); |
| 4380 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4381 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4382 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | } |
| 4384 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4387 | /* Return best possible UTC time -- this isn't constrained by the |
| 4388 | * precision of a timestamp. |
| 4389 | */ |
| 4390 | static PyObject * |
| 4391 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4392 | { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4393 | return datetime_best_possible(cls, _PyTime_gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4396 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4397 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4398 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4401 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | PyObject *tzinfo = Py_None; |
| 4403 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4404 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4405 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4406 | keywords, ×tamp, &tzinfo)) |
| 4407 | return NULL; |
| 4408 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4409 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | self = datetime_from_timestamp(cls, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4412 | tzinfo == Py_None ? _PyTime_localtime : |
| 4413 | _PyTime_gmtime, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | timestamp, |
| 4415 | tzinfo); |
| 4416 | if (self != NULL && tzinfo != Py_None) { |
| 4417 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4418 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4419 | } |
| 4420 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4421 | } |
| 4422 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4423 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 4424 | static PyObject * |
| 4425 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 4426 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4427 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4428 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4429 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4430 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4431 | result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | Py_None); |
| 4433 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4436 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4437 | static PyObject * |
| 4438 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4440 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4441 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4442 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4444 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4445 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4446 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4447 | if (module == NULL) { |
| 4448 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4449 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4450 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | } |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 4452 | return _PyObject_CallMethodIdObjArgs(module, &PyId__strptime_datetime, |
| 4453 | cls, string, format, NULL); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4456 | /* Return new datetime from date/datetime and time arguments. */ |
| 4457 | static PyObject * |
| 4458 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4459 | { |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4460 | static char *keywords[] = {"date", "time", "tzinfo", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | PyObject *date; |
| 4462 | PyObject *time; |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4463 | PyObject *tzinfo = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4465 | |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4466 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4467 | &PyDateTime_DateType, &date, |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4468 | &PyDateTime_TimeType, &time, &tzinfo)) { |
| 4469 | if (tzinfo == NULL) { |
| 4470 | if (HASTZINFO(time)) |
| 4471 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4472 | else |
| 4473 | tzinfo = Py_None; |
| 4474 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4476 | GET_YEAR(date), |
| 4477 | GET_MONTH(date), |
| 4478 | GET_DAY(date), |
| 4479 | TIME_GET_HOUR(time), |
| 4480 | TIME_GET_MINUTE(time), |
| 4481 | TIME_GET_SECOND(time), |
| 4482 | TIME_GET_MICROSECOND(time), |
| 4483 | tzinfo); |
| 4484 | if (result) |
| 4485 | DATE_SET_FOLD(result, TIME_GET_FOLD(time)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4486 | } |
| 4487 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4488 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4489 | |
| 4490 | /* |
| 4491 | * Destructor. |
| 4492 | */ |
| 4493 | |
| 4494 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4495 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | if (HASTZINFO(self)) { |
| 4498 | Py_XDECREF(self->tzinfo); |
| 4499 | } |
| 4500 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /* |
| 4504 | * Indirect access to tzinfo methods. |
| 4505 | */ |
| 4506 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4507 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4508 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4509 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 4510 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4514 | datetime_dst(PyObject *self, PyObject *unused) { |
| 4515 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4519 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 4520 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4524 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4525 | */ |
| 4526 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4527 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4528 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4529 | */ |
| 4530 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4531 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4532 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4534 | /* Note that the C-level additions can't overflow, because of |
| 4535 | * invariant bounds on the member values. |
| 4536 | */ |
| 4537 | int year = GET_YEAR(date); |
| 4538 | int month = GET_MONTH(date); |
| 4539 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4540 | int hour = DATE_GET_HOUR(date); |
| 4541 | int minute = DATE_GET_MINUTE(date); |
| 4542 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4543 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4544 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | assert(factor == 1 || factor == -1); |
| 4547 | if (normalize_datetime(&year, &month, &day, |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4548 | &hour, &minute, &second, µsecond) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | return NULL; |
Victor Stinner | b67f096 | 2017-02-10 10:34:02 +0100 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | return new_datetime(year, month, day, |
| 4553 | hour, minute, second, microsecond, |
| 4554 | HASTZINFO(date) ? date->tzinfo : Py_None, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4558 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4560 | if (PyDateTime_Check(left)) { |
| 4561 | /* datetime + ??? */ |
| 4562 | if (PyDelta_Check(right)) |
| 4563 | /* datetime + delta */ |
| 4564 | return add_datetime_timedelta( |
| 4565 | (PyDateTime_DateTime *)left, |
| 4566 | (PyDateTime_Delta *)right, |
| 4567 | 1); |
| 4568 | } |
| 4569 | else if (PyDelta_Check(left)) { |
| 4570 | /* delta + datetime */ |
| 4571 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4572 | (PyDateTime_Delta *) left, |
| 4573 | 1); |
| 4574 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4575 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4579 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4583 | if (PyDateTime_Check(left)) { |
| 4584 | /* datetime - ??? */ |
| 4585 | if (PyDateTime_Check(right)) { |
| 4586 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4587 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4589 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4590 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 4591 | offset2 = offset1 = Py_None; |
| 4592 | Py_INCREF(offset1); |
| 4593 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4594 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4595 | else { |
| 4596 | offset1 = datetime_utcoffset(left, NULL); |
| 4597 | if (offset1 == NULL) |
| 4598 | return NULL; |
| 4599 | offset2 = datetime_utcoffset(right, NULL); |
| 4600 | if (offset2 == NULL) { |
| 4601 | Py_DECREF(offset1); |
| 4602 | return NULL; |
| 4603 | } |
| 4604 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 4605 | PyErr_SetString(PyExc_TypeError, |
| 4606 | "can't subtract offset-naive and " |
| 4607 | "offset-aware datetimes"); |
| 4608 | Py_DECREF(offset1); |
| 4609 | Py_DECREF(offset2); |
| 4610 | return NULL; |
| 4611 | } |
| 4612 | } |
| 4613 | if ((offset1 != offset2) && |
| 4614 | delta_cmp(offset1, offset2) != 0) { |
| 4615 | offdiff = delta_subtract(offset1, offset2); |
| 4616 | if (offdiff == NULL) { |
| 4617 | Py_DECREF(offset1); |
| 4618 | Py_DECREF(offset2); |
| 4619 | return NULL; |
| 4620 | } |
| 4621 | } |
| 4622 | Py_DECREF(offset1); |
| 4623 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4624 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4625 | GET_MONTH(left), |
| 4626 | GET_DAY(left)) - |
| 4627 | ymd_to_ord(GET_YEAR(right), |
| 4628 | GET_MONTH(right), |
| 4629 | GET_DAY(right)); |
| 4630 | /* These can't overflow, since the values are |
| 4631 | * normalized. At most this gives the number of |
| 4632 | * seconds in one day. |
| 4633 | */ |
| 4634 | delta_s = (DATE_GET_HOUR(left) - |
| 4635 | DATE_GET_HOUR(right)) * 3600 + |
| 4636 | (DATE_GET_MINUTE(left) - |
| 4637 | DATE_GET_MINUTE(right)) * 60 + |
| 4638 | (DATE_GET_SECOND(left) - |
| 4639 | DATE_GET_SECOND(right)); |
| 4640 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4641 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4642 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 4643 | if (result == NULL) |
| 4644 | return NULL; |
| 4645 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4646 | if (offdiff != NULL) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 4647 | Py_SETREF(result, delta_subtract(result, offdiff)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4648 | Py_DECREF(offdiff); |
| 4649 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | } |
| 4651 | else if (PyDelta_Check(right)) { |
| 4652 | /* datetime - delta */ |
| 4653 | result = add_datetime_timedelta( |
| 4654 | (PyDateTime_DateTime *)left, |
| 4655 | (PyDateTime_Delta *)right, |
| 4656 | -1); |
| 4657 | } |
| 4658 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | if (result == Py_NotImplemented) |
| 4661 | Py_INCREF(result); |
| 4662 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | /* Various ways to turn a datetime into a string. */ |
| 4666 | |
| 4667 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4668 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4670 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4671 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | if (DATE_GET_MICROSECOND(self)) { |
| 4674 | baserepr = PyUnicode_FromFormat( |
| 4675 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 4676 | type_name, |
| 4677 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4678 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4679 | DATE_GET_SECOND(self), |
| 4680 | DATE_GET_MICROSECOND(self)); |
| 4681 | } |
| 4682 | else if (DATE_GET_SECOND(self)) { |
| 4683 | baserepr = PyUnicode_FromFormat( |
| 4684 | "%s(%d, %d, %d, %d, %d, %d)", |
| 4685 | type_name, |
| 4686 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4687 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4688 | DATE_GET_SECOND(self)); |
| 4689 | } |
| 4690 | else { |
| 4691 | baserepr = PyUnicode_FromFormat( |
| 4692 | "%s(%d, %d, %d, %d, %d)", |
| 4693 | type_name, |
| 4694 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4695 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4696 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4697 | if (baserepr != NULL && DATE_GET_FOLD(self) != 0) |
| 4698 | baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4700 | return baserepr; |
| 4701 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4704 | static PyObject * |
| 4705 | datetime_str(PyDateTime_DateTime *self) |
| 4706 | { |
Victor Stinner | 4c38154 | 2016-12-09 00:33:39 +0100 | [diff] [blame] | 4707 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4708 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4709 | |
| 4710 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4711 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | int sep = 'T'; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4714 | char *timespec = NULL; |
| 4715 | static char *keywords[] = {"sep", "timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4716 | char buffer[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4717 | PyObject *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | int us = DATE_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4719 | static char *specs[][2] = { |
| 4720 | {"hours", "%04d-%02d-%02d%c%02d"}, |
| 4721 | {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, |
| 4722 | {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, |
| 4723 | {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, |
| 4724 | {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, |
| 4725 | }; |
| 4726 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4727 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4728 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4729 | return NULL; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4730 | |
| 4731 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 4732 | if (us == 0) { |
| 4733 | /* seconds */ |
| 4734 | given_spec = 2; |
| 4735 | } |
| 4736 | else { |
| 4737 | /* microseconds */ |
| 4738 | given_spec = 4; |
| 4739 | } |
| 4740 | } |
| 4741 | else { |
| 4742 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 4743 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 4744 | if (given_spec == 3) { |
| 4745 | us = us / 1000; |
| 4746 | } |
| 4747 | break; |
| 4748 | } |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 4753 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 4754 | return NULL; |
| 4755 | } |
| 4756 | else { |
| 4757 | result = PyUnicode_FromFormat(specs[given_spec][1], |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4758 | GET_YEAR(self), GET_MONTH(self), |
| 4759 | GET_DAY(self), (int)sep, |
| 4760 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4761 | DATE_GET_SECOND(self), us); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4762 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | if (!result || !HASTZINFO(self)) |
| 4765 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | /* We need to append the UTC offset. */ |
| 4768 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 4769 | (PyObject *)self) < 0) { |
| 4770 | Py_DECREF(result); |
| 4771 | return NULL; |
| 4772 | } |
| 4773 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 4774 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4777 | static PyObject * |
| 4778 | datetime_ctime(PyDateTime_DateTime *self) |
| 4779 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | return format_ctime((PyDateTime_Date *)self, |
| 4781 | DATE_GET_HOUR(self), |
| 4782 | DATE_GET_MINUTE(self), |
| 4783 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4786 | /* Miscellaneous methods. */ |
| 4787 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4788 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4789 | flip_fold(PyObject *dt) |
| 4790 | { |
| 4791 | return new_datetime_ex2(GET_YEAR(dt), |
| 4792 | GET_MONTH(dt), |
| 4793 | GET_DAY(dt), |
| 4794 | DATE_GET_HOUR(dt), |
| 4795 | DATE_GET_MINUTE(dt), |
| 4796 | DATE_GET_SECOND(dt), |
| 4797 | DATE_GET_MICROSECOND(dt), |
| 4798 | HASTZINFO(dt) ? |
| 4799 | ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, |
| 4800 | !DATE_GET_FOLD(dt), |
| 4801 | Py_TYPE(dt)); |
| 4802 | } |
| 4803 | |
| 4804 | static PyObject * |
| 4805 | get_flip_fold_offset(PyObject *dt) |
| 4806 | { |
| 4807 | PyObject *result, *flip_dt; |
| 4808 | |
| 4809 | flip_dt = flip_fold(dt); |
| 4810 | if (flip_dt == NULL) |
| 4811 | return NULL; |
| 4812 | result = datetime_utcoffset(flip_dt, NULL); |
| 4813 | Py_DECREF(flip_dt); |
| 4814 | return result; |
| 4815 | } |
| 4816 | |
| 4817 | /* PEP 495 exception: Whenever one or both of the operands in |
| 4818 | * inter-zone comparison is such that its utcoffset() depends |
| 4819 | * on the value of its fold fold attribute, the result is False. |
| 4820 | * |
| 4821 | * Return 1 if exception applies, 0 if not, and -1 on error. |
| 4822 | */ |
| 4823 | static int |
| 4824 | pep495_eq_exception(PyObject *self, PyObject *other, |
| 4825 | PyObject *offset_self, PyObject *offset_other) |
| 4826 | { |
| 4827 | int result = 0; |
| 4828 | PyObject *flip_offset; |
| 4829 | |
| 4830 | flip_offset = get_flip_fold_offset(self); |
| 4831 | if (flip_offset == NULL) |
| 4832 | return -1; |
| 4833 | if (flip_offset != offset_self && |
| 4834 | delta_cmp(flip_offset, offset_self)) |
| 4835 | { |
| 4836 | result = 1; |
| 4837 | goto done; |
| 4838 | } |
| 4839 | Py_DECREF(flip_offset); |
| 4840 | |
| 4841 | flip_offset = get_flip_fold_offset(other); |
| 4842 | if (flip_offset == NULL) |
| 4843 | return -1; |
| 4844 | if (flip_offset != offset_other && |
| 4845 | delta_cmp(flip_offset, offset_other)) |
| 4846 | result = 1; |
| 4847 | done: |
| 4848 | Py_DECREF(flip_offset); |
| 4849 | return result; |
| 4850 | } |
| 4851 | |
| 4852 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4853 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4854 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4855 | PyObject *result = NULL; |
| 4856 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4859 | if (! PyDateTime_Check(other)) { |
| 4860 | if (PyDate_Check(other)) { |
| 4861 | /* Prevent invocation of date_richcompare. We want to |
| 4862 | return NotImplemented here to give the other object |
| 4863 | a chance. But since DateTime is a subclass of |
| 4864 | Date, if the other object is a Date, it would |
| 4865 | compute an ordering based on the date part alone, |
| 4866 | and we don't want that. So force unequal or |
| 4867 | uncomparable here in that case. */ |
| 4868 | if (op == Py_EQ) |
| 4869 | Py_RETURN_FALSE; |
| 4870 | if (op == Py_NE) |
| 4871 | Py_RETURN_TRUE; |
| 4872 | return cmperror(self, other); |
| 4873 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4874 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4875 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4876 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4877 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4879 | ((PyDateTime_DateTime *)other)->data, |
| 4880 | _PyDateTime_DATETIME_DATASIZE); |
| 4881 | return diff_to_bool(diff, op); |
| 4882 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4883 | offset1 = datetime_utcoffset(self, NULL); |
| 4884 | if (offset1 == NULL) |
| 4885 | return NULL; |
| 4886 | offset2 = datetime_utcoffset(other, NULL); |
| 4887 | if (offset2 == NULL) |
| 4888 | goto done; |
| 4889 | /* If they're both naive, or both aware and have the same offsets, |
| 4890 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4891 | * offset2 == Py_None at this point. |
| 4892 | */ |
| 4893 | if ((offset1 == offset2) || |
| 4894 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4895 | delta_cmp(offset1, offset2) == 0)) { |
| 4896 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4897 | ((PyDateTime_DateTime *)other)->data, |
| 4898 | _PyDateTime_DATETIME_DATASIZE); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4899 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 4900 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 4901 | if (ex == -1) |
| 4902 | goto done; |
| 4903 | if (ex) |
| 4904 | diff = 1; |
| 4905 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4906 | result = diff_to_bool(diff, op); |
| 4907 | } |
| 4908 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4909 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4910 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4911 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4912 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4913 | other); |
| 4914 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4915 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4916 | diff = GET_TD_DAYS(delta); |
| 4917 | if (diff == 0) |
| 4918 | diff = GET_TD_SECONDS(delta) | |
| 4919 | GET_TD_MICROSECONDS(delta); |
| 4920 | Py_DECREF(delta); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4921 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 4922 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 4923 | if (ex == -1) |
| 4924 | goto done; |
| 4925 | if (ex) |
| 4926 | diff = 1; |
| 4927 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4928 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4929 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4930 | else if (op == Py_EQ) { |
| 4931 | result = Py_False; |
| 4932 | Py_INCREF(result); |
| 4933 | } |
| 4934 | else if (op == Py_NE) { |
| 4935 | result = Py_True; |
| 4936 | Py_INCREF(result); |
| 4937 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4938 | else { |
| 4939 | PyErr_SetString(PyExc_TypeError, |
| 4940 | "can't compare offset-naive and " |
| 4941 | "offset-aware datetimes"); |
| 4942 | } |
| 4943 | done: |
| 4944 | Py_DECREF(offset1); |
| 4945 | Py_XDECREF(offset2); |
| 4946 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4949 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4950 | datetime_hash(PyDateTime_DateTime *self) |
| 4951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4952 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4953 | PyObject *offset, *self0; |
| 4954 | if (DATE_GET_FOLD(self)) { |
| 4955 | self0 = new_datetime_ex2(GET_YEAR(self), |
| 4956 | GET_MONTH(self), |
| 4957 | GET_DAY(self), |
| 4958 | DATE_GET_HOUR(self), |
| 4959 | DATE_GET_MINUTE(self), |
| 4960 | DATE_GET_SECOND(self), |
| 4961 | DATE_GET_MICROSECOND(self), |
| 4962 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4963 | 0, Py_TYPE(self)); |
| 4964 | if (self0 == NULL) |
| 4965 | return -1; |
| 4966 | } |
| 4967 | else { |
| 4968 | self0 = (PyObject *)self; |
| 4969 | Py_INCREF(self0); |
| 4970 | } |
| 4971 | offset = datetime_utcoffset(self0, NULL); |
| 4972 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4973 | |
| 4974 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4975 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4978 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 | self->hashcode = generic_hash( |
| 4980 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4982 | PyObject *temp1, *temp2; |
| 4983 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | assert(HASTZINFO(self)); |
| 4986 | days = ymd_to_ord(GET_YEAR(self), |
| 4987 | GET_MONTH(self), |
| 4988 | GET_DAY(self)); |
| 4989 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4990 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4991 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4992 | temp1 = new_delta(days, seconds, |
| 4993 | DATE_GET_MICROSECOND(self), |
| 4994 | 1); |
| 4995 | if (temp1 == NULL) { |
| 4996 | Py_DECREF(offset); |
| 4997 | return -1; |
| 4998 | } |
| 4999 | temp2 = delta_subtract(temp1, offset); |
| 5000 | Py_DECREF(temp1); |
| 5001 | if (temp2 == NULL) { |
| 5002 | Py_DECREF(offset); |
| 5003 | return -1; |
| 5004 | } |
| 5005 | self->hashcode = PyObject_Hash(temp2); |
| 5006 | Py_DECREF(temp2); |
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 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | } |
| 5010 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5011 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5012 | |
| 5013 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5014 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5015 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 | PyObject *clone; |
| 5017 | PyObject *tuple; |
| 5018 | int y = GET_YEAR(self); |
| 5019 | int m = GET_MONTH(self); |
| 5020 | int d = GET_DAY(self); |
| 5021 | int hh = DATE_GET_HOUR(self); |
| 5022 | int mm = DATE_GET_MINUTE(self); |
| 5023 | int ss = DATE_GET_SECOND(self); |
| 5024 | int us = DATE_GET_MICROSECOND(self); |
| 5025 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5026 | int fold = DATE_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5027 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5028 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5029 | datetime_kws, |
| 5030 | &y, &m, &d, &hh, &mm, &ss, &us, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5031 | &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | return NULL; |
Serhiy Storchaka | 314d6fc | 2017-03-31 22:48:16 +0300 | [diff] [blame] | 5033 | if (fold != 0 && fold != 1) { |
| 5034 | PyErr_SetString(PyExc_ValueError, |
| 5035 | "fold must be either 0 or 1"); |
| 5036 | return NULL; |
| 5037 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 5039 | if (tuple == NULL) |
| 5040 | return NULL; |
| 5041 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5042 | if (clone != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5043 | DATE_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5044 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | Py_DECREF(tuple); |
| 5046 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5050 | local_timezone_from_timestamp(time_t timestamp) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5051 | { |
| 5052 | PyObject *result = NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5053 | PyObject *delta; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5054 | struct tm local_time_tm; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5055 | PyObject *nameo = NULL; |
| 5056 | const char *zone = NULL; |
| 5057 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5058 | if (_PyTime_localtime(timestamp, &local_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5059 | return NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5060 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5061 | zone = local_time_tm.tm_zone; |
| 5062 | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5063 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 5064 | { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5065 | PyObject *local_time, *utc_time; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5066 | struct tm utc_time_tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5067 | char buf[100]; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5068 | strftime(buf, sizeof(buf), "%Z", &local_time_tm); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5069 | zone = buf; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5070 | local_time = new_datetime(local_time_tm.tm_year + 1900, |
| 5071 | local_time_tm.tm_mon + 1, |
| 5072 | local_time_tm.tm_mday, |
| 5073 | local_time_tm.tm_hour, |
| 5074 | local_time_tm.tm_min, |
| 5075 | local_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5076 | if (local_time == NULL) { |
| 5077 | return NULL; |
| 5078 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5079 | if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5080 | return NULL; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5081 | utc_time = new_datetime(utc_time_tm.tm_year + 1900, |
| 5082 | utc_time_tm.tm_mon + 1, |
| 5083 | utc_time_tm.tm_mday, |
| 5084 | utc_time_tm.tm_hour, |
| 5085 | utc_time_tm.tm_min, |
| 5086 | utc_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5087 | if (utc_time == NULL) { |
| 5088 | Py_DECREF(local_time); |
| 5089 | return NULL; |
| 5090 | } |
| 5091 | delta = datetime_subtract(local_time, utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5092 | Py_DECREF(local_time); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5093 | Py_DECREF(utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5094 | } |
| 5095 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5096 | if (delta == NULL) { |
| 5097 | return NULL; |
| 5098 | } |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5099 | if (zone != NULL) { |
| 5100 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 5101 | if (nameo == NULL) |
| 5102 | goto error; |
| 5103 | } |
| 5104 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 5105 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5106 | error: |
| 5107 | Py_DECREF(delta); |
| 5108 | return result; |
| 5109 | } |
| 5110 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5111 | static PyObject * |
| 5112 | local_timezone(PyDateTime_DateTime *utc_time) |
| 5113 | { |
| 5114 | time_t timestamp; |
| 5115 | PyObject *delta; |
| 5116 | PyObject *one_second; |
| 5117 | PyObject *seconds; |
| 5118 | |
| 5119 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 5120 | if (delta == NULL) |
| 5121 | return NULL; |
| 5122 | one_second = new_delta(0, 1, 0, 0); |
| 5123 | if (one_second == NULL) { |
| 5124 | Py_DECREF(delta); |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 5128 | (PyDateTime_Delta *)one_second); |
| 5129 | Py_DECREF(one_second); |
| 5130 | Py_DECREF(delta); |
| 5131 | if (seconds == NULL) |
| 5132 | return NULL; |
| 5133 | timestamp = _PyLong_AsTime_t(seconds); |
| 5134 | Py_DECREF(seconds); |
| 5135 | if (timestamp == -1 && PyErr_Occurred()) |
| 5136 | return NULL; |
| 5137 | return local_timezone_from_timestamp(timestamp); |
| 5138 | } |
| 5139 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5140 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5141 | local_to_seconds(int year, int month, int day, |
| 5142 | int hour, int minute, int second, int fold); |
| 5143 | |
| 5144 | static PyObject * |
| 5145 | local_timezone_from_local(PyDateTime_DateTime *local_dt) |
| 5146 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5147 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5148 | time_t timestamp; |
| 5149 | seconds = local_to_seconds(GET_YEAR(local_dt), |
| 5150 | GET_MONTH(local_dt), |
| 5151 | GET_DAY(local_dt), |
| 5152 | DATE_GET_HOUR(local_dt), |
| 5153 | DATE_GET_MINUTE(local_dt), |
| 5154 | DATE_GET_SECOND(local_dt), |
| 5155 | DATE_GET_FOLD(local_dt)); |
| 5156 | if (seconds == -1) |
| 5157 | return NULL; |
| 5158 | /* XXX: add bounds check */ |
| 5159 | timestamp = seconds - epoch; |
| 5160 | return local_timezone_from_timestamp(timestamp); |
| 5161 | } |
| 5162 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5163 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5164 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5165 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5166 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5167 | PyObject *offset; |
| 5168 | PyObject *temp; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5169 | PyObject *self_tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5170 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5172 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5173 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 5174 | &tzinfo)) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5175 | return NULL; |
| 5176 | |
| 5177 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5178 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5179 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5180 | if (!HASTZINFO(self) || self->tzinfo == Py_None) { |
| 5181 | self_tzinfo = local_timezone_from_local(self); |
| 5182 | if (self_tzinfo == NULL) |
| 5183 | return NULL; |
| 5184 | } else { |
| 5185 | self_tzinfo = self->tzinfo; |
| 5186 | Py_INCREF(self_tzinfo); |
| 5187 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5189 | /* Conversion to self's own time zone is a NOP. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5190 | if (self_tzinfo == tzinfo) { |
| 5191 | Py_DECREF(self_tzinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5192 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5193 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5194 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5196 | /* Convert self to UTC. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5197 | offset = call_utcoffset(self_tzinfo, (PyObject *)self); |
| 5198 | Py_DECREF(self_tzinfo); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5199 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5200 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5201 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5202 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5203 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5204 | Py_DECREF(offset); |
| 5205 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5207 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5208 | /* Make sure result is aware and UTC. */ |
| 5209 | if (!HASTZINFO(result)) { |
| 5210 | temp = (PyObject *)result; |
| 5211 | result = (PyDateTime_DateTime *) |
| 5212 | new_datetime_ex2(GET_YEAR(result), |
| 5213 | GET_MONTH(result), |
| 5214 | GET_DAY(result), |
| 5215 | DATE_GET_HOUR(result), |
| 5216 | DATE_GET_MINUTE(result), |
| 5217 | DATE_GET_SECOND(result), |
| 5218 | DATE_GET_MICROSECOND(result), |
| 5219 | PyDateTime_TimeZone_UTC, |
| 5220 | DATE_GET_FOLD(result), |
| 5221 | Py_TYPE(result)); |
| 5222 | Py_DECREF(temp); |
| 5223 | if (result == NULL) |
| 5224 | return NULL; |
| 5225 | } |
| 5226 | else { |
| 5227 | /* Result is already aware - just replace tzinfo. */ |
| 5228 | temp = result->tzinfo; |
| 5229 | result->tzinfo = PyDateTime_TimeZone_UTC; |
| 5230 | Py_INCREF(result->tzinfo); |
| 5231 | Py_DECREF(temp); |
| 5232 | } |
| 5233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5234 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5235 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5236 | if (tzinfo == Py_None) { |
| 5237 | tzinfo = local_timezone(result); |
| 5238 | if (tzinfo == NULL) { |
| 5239 | Py_DECREF(result); |
| 5240 | return NULL; |
| 5241 | } |
| 5242 | } |
| 5243 | else |
| 5244 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5245 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5246 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5247 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5248 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5249 | result = (PyDateTime_DateTime *) |
Victor Stinner | 20401de | 2016-12-09 15:24:31 +0100 | [diff] [blame] | 5250 | _PyObject_CallMethodIdObjArgs(tzinfo, &PyId_fromutc, temp, NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5251 | Py_DECREF(temp); |
| 5252 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5253 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5257 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5261 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5262 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5263 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5264 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 5265 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5267 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5268 | if (dst != Py_None) |
| 5269 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 5270 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | } |
| 5272 | return build_struct_time(GET_YEAR(self), |
| 5273 | GET_MONTH(self), |
| 5274 | GET_DAY(self), |
| 5275 | DATE_GET_HOUR(self), |
| 5276 | DATE_GET_MINUTE(self), |
| 5277 | DATE_GET_SECOND(self), |
| 5278 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5281 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5282 | local_to_seconds(int year, int month, int day, |
| 5283 | int hour, int minute, int second, int fold) |
| 5284 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5285 | long long t, a, b, u1, u2, t1, t2, lt; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5286 | t = utc_to_seconds(year, month, day, hour, minute, second); |
| 5287 | /* Our goal is to solve t = local(u) for u. */ |
| 5288 | lt = local(t); |
| 5289 | if (lt == -1) |
| 5290 | return -1; |
| 5291 | a = lt - t; |
| 5292 | u1 = t - a; |
| 5293 | t1 = local(u1); |
| 5294 | if (t1 == -1) |
| 5295 | return -1; |
| 5296 | if (t1 == t) { |
| 5297 | /* We found one solution, but it may not be the one we need. |
| 5298 | * Look for an earlier solution (if `fold` is 0), or a |
| 5299 | * later one (if `fold` is 1). */ |
| 5300 | if (fold) |
| 5301 | u2 = u1 + max_fold_seconds; |
| 5302 | else |
| 5303 | u2 = u1 - max_fold_seconds; |
| 5304 | lt = local(u2); |
| 5305 | if (lt == -1) |
| 5306 | return -1; |
| 5307 | b = lt - u2; |
| 5308 | if (a == b) |
| 5309 | return u1; |
| 5310 | } |
| 5311 | else { |
| 5312 | b = t1 - u1; |
| 5313 | assert(a != b); |
| 5314 | } |
| 5315 | u2 = t - b; |
| 5316 | t2 = local(u2); |
| 5317 | if (t2 == -1) |
| 5318 | return -1; |
| 5319 | if (t2 == t) |
| 5320 | return u2; |
| 5321 | if (t1 == t) |
| 5322 | return u1; |
| 5323 | /* We have found both offsets a and b, but neither t - a nor t - b is |
| 5324 | * a solution. This means t is in the gap. */ |
| 5325 | return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); |
| 5326 | } |
| 5327 | |
| 5328 | /* date(1970,1,1).toordinal() == 719163 */ |
| 5329 | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) |
| 5330 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5331 | static PyObject * |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5332 | datetime_timestamp(PyDateTime_DateTime *self) |
| 5333 | { |
| 5334 | PyObject *result; |
| 5335 | |
| 5336 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 5337 | PyObject *delta; |
| 5338 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 5339 | if (delta == NULL) |
| 5340 | return NULL; |
| 5341 | result = delta_total_seconds(delta); |
| 5342 | Py_DECREF(delta); |
| 5343 | } |
| 5344 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5345 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5346 | seconds = local_to_seconds(GET_YEAR(self), |
| 5347 | GET_MONTH(self), |
| 5348 | GET_DAY(self), |
| 5349 | DATE_GET_HOUR(self), |
| 5350 | DATE_GET_MINUTE(self), |
| 5351 | DATE_GET_SECOND(self), |
| 5352 | DATE_GET_FOLD(self)); |
| 5353 | if (seconds == -1) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5354 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5355 | result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + |
| 5356 | DATE_GET_MICROSECOND(self) / 1e6); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5357 | } |
| 5358 | return result; |
| 5359 | } |
| 5360 | |
| 5361 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5362 | datetime_getdate(PyDateTime_DateTime *self) |
| 5363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5364 | return new_date(GET_YEAR(self), |
| 5365 | GET_MONTH(self), |
| 5366 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | static PyObject * |
| 5370 | datetime_gettime(PyDateTime_DateTime *self) |
| 5371 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5372 | return new_time(DATE_GET_HOUR(self), |
| 5373 | DATE_GET_MINUTE(self), |
| 5374 | DATE_GET_SECOND(self), |
| 5375 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5376 | Py_None, |
| 5377 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | static PyObject * |
| 5381 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 5382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5383 | return new_time(DATE_GET_HOUR(self), |
| 5384 | DATE_GET_MINUTE(self), |
| 5385 | DATE_GET_SECOND(self), |
| 5386 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5387 | GET_DT_TZINFO(self), |
| 5388 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | static PyObject * |
| 5392 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5393 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5394 | int y, m, d, hh, mm, ss; |
| 5395 | PyObject *tzinfo; |
| 5396 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5397 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5398 | tzinfo = GET_DT_TZINFO(self); |
| 5399 | if (tzinfo == Py_None) { |
| 5400 | utcself = self; |
| 5401 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5402 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5403 | else { |
| 5404 | PyObject *offset; |
| 5405 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 5406 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 5407 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5408 | if (offset == Py_None) { |
| 5409 | Py_DECREF(offset); |
| 5410 | utcself = self; |
| 5411 | Py_INCREF(utcself); |
| 5412 | } |
| 5413 | else { |
| 5414 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5415 | (PyDateTime_Delta *)offset, -1); |
| 5416 | Py_DECREF(offset); |
| 5417 | if (utcself == NULL) |
| 5418 | return NULL; |
| 5419 | } |
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 | y = GET_YEAR(utcself); |
| 5422 | m = GET_MONTH(utcself); |
| 5423 | d = GET_DAY(utcself); |
| 5424 | hh = DATE_GET_HOUR(utcself); |
| 5425 | mm = DATE_GET_MINUTE(utcself); |
| 5426 | ss = DATE_GET_SECOND(utcself); |
| 5427 | |
| 5428 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5429 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 5432 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 5433 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5434 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5435 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 5436 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 5437 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5438 | */ |
| 5439 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5440 | datetime_getstate(PyDateTime_DateTime *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | PyObject *basestate; |
| 5443 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 5446 | _PyDateTime_DATETIME_DATASIZE); |
| 5447 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5448 | if (proto > 3 && DATE_GET_FOLD(self)) |
| 5449 | /* Set the first bit of the third byte */ |
| 5450 | PyBytes_AS_STRING(basestate)[2] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5451 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 5452 | result = PyTuple_Pack(1, basestate); |
| 5453 | else |
| 5454 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 5455 | Py_DECREF(basestate); |
| 5456 | } |
| 5457 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5461 | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5462 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5463 | int proto; |
| 5464 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5465 | return NULL; |
| 5466 | |
| 5467 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5470 | static PyObject * |
| 5471 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
| 5472 | { |
| 5473 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); |
| 5474 | } |
| 5475 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5476 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5478 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5479 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 5480 | DATETIME_DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5482 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 5483 | METH_NOARGS | METH_CLASS, |
| 5484 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5486 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 5487 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5488 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5490 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 5491 | METH_VARARGS | METH_CLASS, |
Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 5492 | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 | {"strptime", (PyCFunction)datetime_strptime, |
| 5495 | METH_VARARGS | METH_CLASS, |
| 5496 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 5497 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5499 | {"combine", (PyCFunction)datetime_combine, |
| 5500 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5501 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5505 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 5506 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5508 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 5509 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5511 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 5512 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5514 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 5515 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5517 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 5518 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5519 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5520 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 5521 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 5522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5523 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 5524 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5526 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 5527 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5528 | "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5529 | "sep is used to separate the year from the time, and " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5530 | "defaults to 'T'.\n" |
| 5531 | "timespec specifies what components of the time to include" |
| 5532 | " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," |
| 5533 | " 'milliseconds', and 'microseconds').\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5535 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 5536 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5538 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 5539 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 5542 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 5545 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 5548 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5549 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5550 | {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5551 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5552 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5553 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 5554 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 5555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5557 | }; |
| 5558 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 5559 | static const char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5560 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 5561 | \n\ |
| 5562 | 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] | 5563 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5564 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5565 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | datetime_add, /* nb_add */ |
| 5567 | datetime_subtract, /* nb_subtract */ |
| 5568 | 0, /* nb_multiply */ |
| 5569 | 0, /* nb_remainder */ |
| 5570 | 0, /* nb_divmod */ |
| 5571 | 0, /* nb_power */ |
| 5572 | 0, /* nb_negative */ |
| 5573 | 0, /* nb_positive */ |
| 5574 | 0, /* nb_absolute */ |
| 5575 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5576 | }; |
| 5577 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5578 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5579 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5580 | "datetime.datetime", /* tp_name */ |
| 5581 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 5582 | 0, /* tp_itemsize */ |
| 5583 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 5584 | 0, /* tp_print */ |
| 5585 | 0, /* tp_getattr */ |
| 5586 | 0, /* tp_setattr */ |
| 5587 | 0, /* tp_reserved */ |
| 5588 | (reprfunc)datetime_repr, /* tp_repr */ |
| 5589 | &datetime_as_number, /* tp_as_number */ |
| 5590 | 0, /* tp_as_sequence */ |
| 5591 | 0, /* tp_as_mapping */ |
| 5592 | (hashfunc)datetime_hash, /* tp_hash */ |
| 5593 | 0, /* tp_call */ |
| 5594 | (reprfunc)datetime_str, /* tp_str */ |
| 5595 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5596 | 0, /* tp_setattro */ |
| 5597 | 0, /* tp_as_buffer */ |
| 5598 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5599 | datetime_doc, /* tp_doc */ |
| 5600 | 0, /* tp_traverse */ |
| 5601 | 0, /* tp_clear */ |
| 5602 | datetime_richcompare, /* tp_richcompare */ |
| 5603 | 0, /* tp_weaklistoffset */ |
| 5604 | 0, /* tp_iter */ |
| 5605 | 0, /* tp_iternext */ |
| 5606 | datetime_methods, /* tp_methods */ |
| 5607 | 0, /* tp_members */ |
| 5608 | datetime_getset, /* tp_getset */ |
| 5609 | &PyDateTime_DateType, /* tp_base */ |
| 5610 | 0, /* tp_dict */ |
| 5611 | 0, /* tp_descr_get */ |
| 5612 | 0, /* tp_descr_set */ |
| 5613 | 0, /* tp_dictoffset */ |
| 5614 | 0, /* tp_init */ |
| 5615 | datetime_alloc, /* tp_alloc */ |
| 5616 | datetime_new, /* tp_new */ |
| 5617 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5618 | }; |
| 5619 | |
| 5620 | /* --------------------------------------------------------------------------- |
| 5621 | * Module methods and initialization. |
| 5622 | */ |
| 5623 | |
| 5624 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5625 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5626 | }; |
| 5627 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5628 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 5629 | * datetime.h. |
| 5630 | */ |
| 5631 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5632 | &PyDateTime_DateType, |
| 5633 | &PyDateTime_DateTimeType, |
| 5634 | &PyDateTime_TimeType, |
| 5635 | &PyDateTime_DeltaType, |
| 5636 | &PyDateTime_TZInfoType, |
| 5637 | new_date_ex, |
| 5638 | new_datetime_ex, |
| 5639 | new_time_ex, |
| 5640 | new_delta_ex, |
| 5641 | datetime_fromtimestamp, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5642 | date_fromtimestamp, |
| 5643 | new_datetime_ex2, |
| 5644 | new_time_ex2 |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5645 | }; |
| 5646 | |
| 5647 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5648 | |
| 5649 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5650 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5651 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5652 | "Fast implementation of the datetime type.", |
| 5653 | -1, |
| 5654 | module_methods, |
| 5655 | NULL, |
| 5656 | NULL, |
| 5657 | NULL, |
| 5658 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5659 | }; |
| 5660 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5661 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5662 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5664 | PyObject *m; /* a module object */ |
| 5665 | PyObject *d; /* its dict */ |
| 5666 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5667 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5669 | m = PyModule_Create(&datetimemodule); |
| 5670 | if (m == NULL) |
| 5671 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5673 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 5674 | return NULL; |
| 5675 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 5676 | return NULL; |
| 5677 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 5678 | return NULL; |
| 5679 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 5680 | return NULL; |
| 5681 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 5682 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5683 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 5684 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5686 | /* timedelta values */ |
| 5687 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5689 | x = new_delta(0, 0, 1, 0); |
| 5690 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5691 | return NULL; |
| 5692 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 5695 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5696 | return NULL; |
| 5697 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5699 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 5700 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5701 | return NULL; |
| 5702 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5704 | /* date values */ |
| 5705 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5707 | x = new_date(1, 1, 1); |
| 5708 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5709 | return NULL; |
| 5710 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | x = new_date(MAXYEAR, 12, 31); |
| 5713 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5714 | return NULL; |
| 5715 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5717 | x = new_delta(1, 0, 0, 0); |
| 5718 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5719 | return NULL; |
| 5720 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5722 | /* time values */ |
| 5723 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5724 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5725 | x = new_time(0, 0, 0, 0, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5726 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5727 | return NULL; |
| 5728 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5729 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5730 | x = new_time(23, 59, 59, 999999, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5731 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5732 | return NULL; |
| 5733 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5735 | x = new_delta(0, 0, 1, 0); |
| 5736 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5737 | return NULL; |
| 5738 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5740 | /* datetime values */ |
| 5741 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5742 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5743 | 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] | 5744 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5745 | return NULL; |
| 5746 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5747 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5748 | 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] | 5749 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5750 | return NULL; |
| 5751 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | x = new_delta(0, 0, 1, 0); |
| 5754 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5755 | return NULL; |
| 5756 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5757 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5758 | /* timezone values */ |
| 5759 | d = PyDateTime_TimeZoneType.tp_dict; |
| 5760 | |
| 5761 | delta = new_delta(0, 0, 0, 0); |
| 5762 | if (delta == NULL) |
| 5763 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5764 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5765 | Py_DECREF(delta); |
| 5766 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 5767 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 5768 | PyDateTime_TimeZone_UTC = x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5769 | |
| 5770 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 5771 | if (delta == NULL) |
| 5772 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5773 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5774 | Py_DECREF(delta); |
| 5775 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5776 | return NULL; |
| 5777 | Py_DECREF(x); |
| 5778 | |
| 5779 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 5780 | if (delta == NULL) |
| 5781 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5782 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5783 | Py_DECREF(delta); |
| 5784 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5785 | return NULL; |
| 5786 | Py_DECREF(x); |
| 5787 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5788 | /* Epoch */ |
| 5789 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5790 | PyDateTime_TimeZone_UTC, 0); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5791 | if (PyDateTime_Epoch == NULL) |
| 5792 | return NULL; |
| 5793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5794 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5795 | PyModule_AddIntMacro(m, MINYEAR); |
| 5796 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5798 | Py_INCREF(&PyDateTime_DateType); |
| 5799 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5801 | Py_INCREF(&PyDateTime_DateTimeType); |
| 5802 | PyModule_AddObject(m, "datetime", |
| 5803 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5805 | Py_INCREF(&PyDateTime_TimeType); |
| 5806 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | Py_INCREF(&PyDateTime_DeltaType); |
| 5809 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5811 | Py_INCREF(&PyDateTime_TZInfoType); |
| 5812 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5813 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5814 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 5815 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 5816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5817 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 5818 | if (x == NULL) |
| 5819 | return NULL; |
| 5820 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 5823 | * pasting together 4 single years. |
| 5824 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5825 | Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5826 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5828 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 5829 | * get from pasting together 4 100-year cycles. |
| 5830 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5831 | Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5832 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5834 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 5835 | * pasting together 25 4-year cycles. |
| 5836 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5837 | Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5838 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5840 | us_per_ms = PyLong_FromLong(1000); |
| 5841 | us_per_second = PyLong_FromLong(1000000); |
| 5842 | us_per_minute = PyLong_FromLong(60000000); |
| 5843 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 5844 | if (us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5845 | us_per_minute == NULL || seconds_per_day == NULL) |
| 5846 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5848 | /* The rest are too big for 32-bit ints, but even |
| 5849 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 5850 | */ |
| 5851 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 5852 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 5853 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 5854 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 5855 | return NULL; |
| 5856 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5857 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5858 | |
| 5859 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5860 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5861 | x.n = x stripped of its timezone -- its naive time. |
| 5862 | 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] | 5863 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5864 | 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] | 5865 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5866 | x.s = x's standard offset, x.o - x.d |
| 5867 | |
| 5868 | Now some derived rules, where k is a duration (timedelta). |
| 5869 | |
| 5870 | 1. x.o = x.s + x.d |
| 5871 | This follows from the definition of x.s. |
| 5872 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5873 | 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] | 5874 | This is actually a requirement, an assumption we need to make about |
| 5875 | sane tzinfo classes. |
| 5876 | |
| 5877 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 5878 | This is again a requirement for a sane tzinfo class. |
| 5879 | |
| 5880 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5881 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5882 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5883 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5884 | Again follows from how arithmetic is defined. |
| 5885 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5886 | 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] | 5887 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 5888 | None when called). |
| 5889 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5890 | 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] | 5891 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5892 | |
| 5893 | By #3, we want |
| 5894 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5895 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5896 | |
| 5897 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 5898 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 5899 | becomes true; in effect, we want to solve [2] for k: |
| 5900 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5901 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5902 | |
| 5903 | By #1, this is the same as |
| 5904 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5905 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5906 | |
| 5907 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 5908 | Substituting that into [3], |
| 5909 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5910 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 5911 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 5912 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 5913 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5914 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5915 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 5916 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 5917 | very large, since all offset-returning methods return a duration of magnitude |
| 5918 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 5919 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5920 | |
| 5921 | In any case, the new value is |
| 5922 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5923 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5924 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5925 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 5926 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5927 | |
| 5928 | At this point, if |
| 5929 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5930 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5931 | |
| 5932 | 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] | 5933 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 5934 | 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] | 5935 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 5936 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 5937 | 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] | 5938 | the only spelling that makes sense on the local wall clock. |
| 5939 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5940 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 5941 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 5942 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5943 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5944 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5945 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5946 | Now |
| 5947 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5948 | (y + y.s).n = by #5 |
| 5949 | y.n + y.s = since y.n = x.n |
| 5950 | x.n + y.s = since z and y are have the same tzinfo member, |
| 5951 | y.s = z.s by #2 |
| 5952 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5953 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5954 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5955 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5956 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5957 | x.n - ((x.n + z.s) - z.o) = expanding |
| 5958 | x.n - x.n - z.s + z.o = cancelling |
| 5959 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5960 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5961 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5962 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5963 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5964 | 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] | 5965 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 5966 | 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] | 5967 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5968 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 5969 | 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] | 5970 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5971 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5972 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5973 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5974 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5975 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5976 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5977 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5978 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5979 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5980 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 5981 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 5982 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 5983 | the justifications for the kinds of substitutions we've done several times |
| 5984 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5985 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5986 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5987 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 5988 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 5989 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 5990 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 5991 | - z.o + z'.o = #1 twice |
| 5992 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 5993 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5994 | |
| 5995 | 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] | 5996 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 5997 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5998 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5999 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 6000 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 6001 | would have to change the result dst() returns: we start in DST, and moving |
| 6002 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 6003 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6004 | There isn't a sane case where this can happen. The closest it gets is at |
| 6005 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 6006 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 6007 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 6008 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 6009 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 6010 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 6011 | standard time. Since that's what the local clock *does*, we want to map both |
| 6012 | 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] | 6013 | in local time, but so it goes -- it's the way the local clock works. |
| 6014 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6015 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 6016 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 6017 | 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] | 6018 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 6019 | |
| 6020 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 6021 | 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] | 6022 | 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] | 6023 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 6024 | but the reasoning doesn't depend on the example -- it depends on there being |
| 6025 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6026 | z' must be in standard time, and is the spelling we want in this case. |
| 6027 | |
| 6028 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 6029 | concerned (because it takes z' as being in standard time rather than the |
| 6030 | daylight time we intend here), but returning it gives the real-life "local |
| 6031 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 6032 | tz. |
| 6033 | |
| 6034 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 6035 | the 1:MM standard time spelling we want. |
| 6036 | |
| 6037 | So how can this break? One of the assumptions must be violated. Two |
| 6038 | possibilities: |
| 6039 | |
| 6040 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 6041 | time zone. This isn't true if, for political reasons or continental drift, |
| 6042 | a region decides to change its base offset from UTC. |
| 6043 | |
| 6044 | 2) There may be versions of "double daylight" time where the tail end of |
| 6045 | the analysis gives up a step too early. I haven't thought about that |
| 6046 | enough to say. |
| 6047 | |
| 6048 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 6049 | "almost all" time zones: so long as the standard offset is invariant, it |
| 6050 | doesn't matter if daylight time transition points change from year to year, or |
| 6051 | if daylight time is skipped in some years; it doesn't matter how large or |
| 6052 | small dst() may get within its bounds; and it doesn't even matter if some |
| 6053 | perverse time zone returns a negative dst()). So a breaking case must be |
| 6054 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6055 | --------------------------------------------------------------------------- */ |