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 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 123 | _Py_IDENTIFIER(as_integer_ratio); |
| 124 | _Py_IDENTIFIER(fromutc); |
| 125 | _Py_IDENTIFIER(isoformat); |
| 126 | _Py_IDENTIFIER(strftime); |
| 127 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 128 | /* --------------------------------------------------------------------------- |
| 129 | * Math utilities. |
| 130 | */ |
| 131 | |
| 132 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 133 | * iff k^i has sign bit set and k^j has sign bit set, |
| 134 | * iff (k^i)&(k^j) has sign bit set. |
| 135 | */ |
| 136 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 138 | |
| 139 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 140 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 141 | * the real point of this. C will probably truncate instead (C99 |
| 142 | * requires truncation; C89 left it implementation-defined). |
| 143 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 144 | * for all the uses made of it. This simplifies the code and makes |
| 145 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 146 | * overflow case). |
| 147 | */ |
| 148 | static int |
| 149 | divmod(int x, int y, int *r) |
| 150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | assert(y > 0); |
| 154 | quo = x / y; |
| 155 | *r = x - quo * y; |
| 156 | if (*r < 0) { |
| 157 | --quo; |
| 158 | *r += y; |
| 159 | } |
| 160 | assert(0 <= *r && *r < y); |
| 161 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 164 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 165 | * are rounded to even. |
| 166 | */ |
| 167 | static PyObject * |
| 168 | divide_nearest(PyObject *m, PyObject *n) |
| 169 | { |
| 170 | PyObject *result; |
| 171 | PyObject *temp; |
| 172 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 173 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 174 | if (temp == NULL) |
| 175 | return NULL; |
| 176 | result = PyTuple_GET_ITEM(temp, 0); |
| 177 | Py_INCREF(result); |
| 178 | Py_DECREF(temp); |
| 179 | |
| 180 | return result; |
| 181 | } |
| 182 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 183 | /* --------------------------------------------------------------------------- |
| 184 | * General calendrical helper functions |
| 185 | */ |
| 186 | |
| 187 | /* For each month ordinal in 1..12, the number of days in that month, |
| 188 | * and the number of days before that month in the same year. These |
| 189 | * are correct for non-leap years only. |
| 190 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 191 | static const int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | 0, /* unused; this vector uses 1-based indexing */ |
| 193 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 196 | static const int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | 0, /* unused; this vector uses 1-based indexing */ |
| 198 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | /* year -> 1 if leap year, else 0. */ |
| 202 | static int |
| 203 | is_leap(int year) |
| 204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | /* Cast year to unsigned. The result is the same either way, but |
| 206 | * C can generate faster code for unsigned mod than for signed |
| 207 | * mod (especially for % 4 -- a good compiler should just grab |
| 208 | * the last 2 bits when the LHS is unsigned). |
| 209 | */ |
| 210 | const unsigned int ayear = (unsigned int)year; |
| 211 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* year, month -> number of days in that month in that year */ |
| 215 | static int |
| 216 | days_in_month(int year, int month) |
| 217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | assert(month >= 1); |
| 219 | assert(month <= 12); |
| 220 | if (month == 2 && is_leap(year)) |
| 221 | return 29; |
| 222 | else |
| 223 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 226 | /* year, month -> number of days in year preceding first day of month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 227 | static int |
| 228 | days_before_month(int year, int month) |
| 229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | assert(month >= 1); |
| 233 | assert(month <= 12); |
| 234 | days = _days_before_month[month]; |
| 235 | if (month > 2 && is_leap(year)) |
| 236 | ++days; |
| 237 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* year -> number of days before January 1st of year. Remember that we |
| 241 | * start with year 1, so days_before_year(1) == 0. |
| 242 | */ |
| 243 | static int |
| 244 | days_before_year(int year) |
| 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | int y = year - 1; |
| 247 | /* This is incorrect if year <= 0; we really want the floor |
| 248 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 249 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 251 | assert (year >= 1); |
| 252 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 256 | * the correct values is asserted in the module init function. |
| 257 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 259 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 260 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 261 | |
| 262 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 263 | static void |
| 264 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 269 | * leap years repeats exactly every 400 years. The basic strategy is |
| 270 | * to find the closest 400-year boundary at or before ordinal, then |
| 271 | * work with the offset from that boundary to ordinal. Life is much |
| 272 | * clearer if we subtract 1 from ordinal first -- then the values |
| 273 | * of ordinal at 400-year boundaries are exactly those divisible |
| 274 | * by DI400Y: |
| 275 | * |
| 276 | * D M Y n n-1 |
| 277 | * -- --- ---- ---------- ---------------- |
| 278 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 279 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 280 | * ... |
| 281 | * 30 Dec 000 -1 -2 |
| 282 | * 31 Dec 000 0 -1 |
| 283 | * 1 Jan 001 1 0 400-year boundary |
| 284 | * 2 Jan 001 2 1 |
| 285 | * 3 Jan 001 3 2 |
| 286 | * ... |
| 287 | * 31 Dec 400 DI400Y DI400Y -1 |
| 288 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 289 | */ |
| 290 | assert(ordinal >= 1); |
| 291 | --ordinal; |
| 292 | n400 = ordinal / DI400Y; |
| 293 | n = ordinal % DI400Y; |
| 294 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 297 | * year, to the desired date. Now compute how many 100-year cycles |
| 298 | * precede n. |
| 299 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 300 | * 100-year cycles precede the desired day, which implies the |
| 301 | * desired day is December 31 at the end of a 400-year cycle. |
| 302 | */ |
| 303 | n100 = n / DI100Y; |
| 304 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | /* Now compute how many 4-year cycles precede it. */ |
| 307 | n4 = n / DI4Y; |
| 308 | n = n % DI4Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | /* And now how many single years. Again n1 can be 4, and again |
| 311 | * meaning that the desired day is December 31 at the end of the |
| 312 | * 4-year cycle. |
| 313 | */ |
| 314 | n1 = n / 365; |
| 315 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | *year += n100 * 100 + n4 * 4 + n1; |
| 318 | if (n1 == 4 || n100 == 4) { |
| 319 | assert(n == 0); |
| 320 | *year -= 1; |
| 321 | *month = 12; |
| 322 | *day = 31; |
| 323 | return; |
| 324 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | /* Now the year is correct, and n is the offset from January 1. We |
| 327 | * find the month via an estimate that's either exact or one too |
| 328 | * large. |
| 329 | */ |
| 330 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 331 | assert(leapyear == is_leap(*year)); |
| 332 | *month = (n + 50) >> 5; |
| 333 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 334 | if (preceding > n) { |
| 335 | /* estimate is too large */ |
| 336 | *month -= 1; |
| 337 | preceding -= days_in_month(*year, *month); |
| 338 | } |
| 339 | n -= preceding; |
| 340 | assert(0 <= n); |
| 341 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 347 | static int |
| 348 | ymd_to_ord(int year, int month, int day) |
| 349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 354 | static int |
| 355 | weekday(int year, int month, int day) |
| 356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 361 | * first calendar week containing a Thursday. |
| 362 | */ |
| 363 | static int |
| 364 | iso_week1_monday(int year) |
| 365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 367 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 368 | int first_weekday = (first_day + 6) % 7; |
| 369 | /* ordinal of closest Monday at or before 1/1 */ |
| 370 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 373 | week1_monday += 7; |
| 374 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* --------------------------------------------------------------------------- |
| 378 | * Range checkers. |
| 379 | */ |
| 380 | |
| 381 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 382 | * If not, raise OverflowError and return -1. |
| 383 | */ |
| 384 | static int |
| 385 | check_delta_day_range(int days) |
| 386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 388 | return 0; |
| 389 | PyErr_Format(PyExc_OverflowError, |
| 390 | "days=%d; must have magnitude <= %d", |
| 391 | days, MAX_DELTA_DAYS); |
| 392 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 396 | * aren't, raise ValueError and return -1. |
| 397 | */ |
| 398 | static int |
| 399 | check_date_args(int year, int month, int day) |
| 400 | { |
| 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (year < MINYEAR || year > MAXYEAR) { |
| 403 | PyErr_SetString(PyExc_ValueError, |
| 404 | "year is out of range"); |
| 405 | return -1; |
| 406 | } |
| 407 | if (month < 1 || month > 12) { |
| 408 | PyErr_SetString(PyExc_ValueError, |
| 409 | "month must be in 1..12"); |
| 410 | return -1; |
| 411 | } |
| 412 | if (day < 1 || day > days_in_month(year, month)) { |
| 413 | PyErr_SetString(PyExc_ValueError, |
| 414 | "day is out of range for month"); |
| 415 | return -1; |
| 416 | } |
| 417 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 421 | * aren't, raise ValueError and return -1. |
| 422 | */ |
| 423 | static int |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 424 | 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] | 425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | if (h < 0 || h > 23) { |
| 427 | PyErr_SetString(PyExc_ValueError, |
| 428 | "hour must be in 0..23"); |
| 429 | return -1; |
| 430 | } |
| 431 | if (m < 0 || m > 59) { |
| 432 | PyErr_SetString(PyExc_ValueError, |
| 433 | "minute must be in 0..59"); |
| 434 | return -1; |
| 435 | } |
| 436 | if (s < 0 || s > 59) { |
| 437 | PyErr_SetString(PyExc_ValueError, |
| 438 | "second must be in 0..59"); |
| 439 | return -1; |
| 440 | } |
| 441 | if (us < 0 || us > 999999) { |
| 442 | PyErr_SetString(PyExc_ValueError, |
| 443 | "microsecond must be in 0..999999"); |
| 444 | return -1; |
| 445 | } |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 446 | if (fold != 0 && fold != 1) { |
| 447 | PyErr_SetString(PyExc_ValueError, |
| 448 | "fold must be either 0 or 1"); |
| 449 | return -1; |
| 450 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* --------------------------------------------------------------------------- |
| 455 | * Normalization utilities. |
| 456 | */ |
| 457 | |
| 458 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 459 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 460 | * at least factor, enough of *lo is converted into "hi" units so that |
| 461 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 462 | * is impossible. |
| 463 | */ |
| 464 | static void |
| 465 | normalize_pair(int *hi, int *lo, int factor) |
| 466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | assert(factor > 0); |
| 468 | assert(lo != hi); |
| 469 | if (*lo < 0 || *lo >= factor) { |
| 470 | const int num_hi = divmod(*lo, factor, lo); |
| 471 | const int new_hi = *hi + num_hi; |
| 472 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 473 | *hi = new_hi; |
| 474 | } |
| 475 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | * 0 <= *s < 24*3600 |
| 480 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 481 | * The input values must be such that the internals don't overflow. |
| 482 | * The way this routine is used, we don't get close. |
| 483 | */ |
| 484 | static void |
| 485 | normalize_d_s_us(int *d, int *s, int *us) |
| 486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | if (*us < 0 || *us >= 1000000) { |
| 488 | normalize_pair(s, us, 1000000); |
| 489 | /* |s| can't be bigger than about |
| 490 | * |original s| + |original us|/1000000 now. |
| 491 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | } |
| 494 | if (*s < 0 || *s >= 24*3600) { |
| 495 | normalize_pair(d, s, 24*3600); |
| 496 | /* |d| can't be bigger than about |
| 497 | * |original d| + |
| 498 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 499 | */ |
| 500 | } |
| 501 | assert(0 <= *s && *s < 24*3600); |
| 502 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | * 1 <= *m <= 12 |
| 507 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 508 | * The input values must be such that the internals don't overflow. |
| 509 | * The way this routine is used, we don't get close. |
| 510 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 511 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 512 | normalize_y_m_d(int *y, int *m, int *d) |
| 513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 515 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 516 | /* In actual use, m is always the month component extracted from a |
| 517 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | /* Now only day can be out of bounds (year may also be out of bounds |
| 523 | * for a datetime object, but we don't care about that here). |
| 524 | * If day is out of bounds, what to do is arguable, but at least the |
| 525 | * method here is principled and explainable. |
| 526 | */ |
| 527 | dim = days_in_month(*y, *m); |
| 528 | if (*d < 1 || *d > dim) { |
| 529 | /* Move day-1 days from the first of the month. First try to |
| 530 | * get off cheap if we're only one day out of range |
| 531 | * (adjustments for timezone alone can't be worse than that). |
| 532 | */ |
| 533 | if (*d == 0) { |
| 534 | --*m; |
| 535 | if (*m > 0) |
| 536 | *d = days_in_month(*y, *m); |
| 537 | else { |
| 538 | --*y; |
| 539 | *m = 12; |
| 540 | *d = 31; |
| 541 | } |
| 542 | } |
| 543 | else if (*d == dim + 1) { |
| 544 | /* move forward a day */ |
| 545 | ++*m; |
| 546 | *d = 1; |
| 547 | if (*m > 12) { |
| 548 | *m = 1; |
| 549 | ++*y; |
| 550 | } |
| 551 | } |
| 552 | else { |
| 553 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 554 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 555 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 556 | goto error; |
| 557 | } else { |
| 558 | ord_to_ymd(ordinal, y, m, d); |
| 559 | return 0; |
| 560 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | assert(*m > 0); |
| 564 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 565 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 566 | return 0; |
| 567 | error: |
| 568 | PyErr_SetString(PyExc_OverflowError, |
| 569 | "date value out of range"); |
| 570 | return -1; |
| 571 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 575 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 576 | * failure, where failure means the adjusted year is out of bounds. |
| 577 | */ |
| 578 | static int |
| 579 | normalize_date(int *year, int *month, int *day) |
| 580 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 581 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* Force all the datetime fields into range. The parameters are both |
| 585 | * inputs and outputs. Returns < 0 on error. |
| 586 | */ |
| 587 | static int |
| 588 | normalize_datetime(int *year, int *month, int *day, |
| 589 | int *hour, int *minute, int *second, |
| 590 | int *microsecond) |
| 591 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | normalize_pair(second, microsecond, 1000000); |
| 593 | normalize_pair(minute, second, 60); |
| 594 | normalize_pair(hour, minute, 60); |
| 595 | normalize_pair(day, hour, 24); |
| 596 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 600 | * Basic object allocation: tp_alloc implementations. These allocate |
| 601 | * Python objects of the right size and type, and do the Python object- |
| 602 | * initialization bit. If there's not enough memory, they return NULL after |
| 603 | * setting MemoryError. All data members remain uninitialized trash. |
| 604 | * |
| 605 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 606 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 607 | * tp_basicsize for the time and datetime types is set to the size of the |
| 608 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 609 | * allocate enough space for a tzinfo member whether or not one is actually |
| 610 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 611 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 612 | * using) just happens today to effectively ignore the nitems argument |
| 613 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 614 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 615 | * be changed to force a 0 nitems argument unless the type being allocated |
| 616 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 617 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 618 | */ |
| 619 | |
| 620 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 621 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | self = (PyObject *) |
| 626 | PyObject_MALLOC(aware ? |
| 627 | sizeof(PyDateTime_Time) : |
| 628 | sizeof(_PyDateTime_BaseTime)); |
| 629 | if (self == NULL) |
| 630 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 631 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 636 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | self = (PyObject *) |
| 641 | PyObject_MALLOC(aware ? |
| 642 | sizeof(PyDateTime_DateTime) : |
| 643 | sizeof(_PyDateTime_BaseDateTime)); |
| 644 | if (self == NULL) |
| 645 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 646 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* --------------------------------------------------------------------------- |
| 651 | * Helpers for setting object fields. These work on pointers to the |
| 652 | * appropriate base class. |
| 653 | */ |
| 654 | |
| 655 | /* For date and datetime. */ |
| 656 | static void |
| 657 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 658 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | self->hashcode = -1; |
| 660 | SET_YEAR(self, y); |
| 661 | SET_MONTH(self, m); |
| 662 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* --------------------------------------------------------------------------- |
| 666 | * Create various objects, mostly without range checking. |
| 667 | */ |
| 668 | |
| 669 | /* Create a date instance with no range checking. */ |
| 670 | static PyObject * |
| 671 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 676 | if (self != NULL) |
| 677 | set_date_fields(self, year, month, day); |
| 678 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 683 | |
| 684 | /* Create a datetime instance with no range checking. */ |
| 685 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 686 | new_datetime_ex2(int year, int month, int day, int hour, int minute, |
| 687 | int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | PyDateTime_DateTime *self; |
| 690 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 693 | if (self != NULL) { |
| 694 | self->hastzinfo = aware; |
| 695 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 696 | DATE_SET_HOUR(self, hour); |
| 697 | DATE_SET_MINUTE(self, minute); |
| 698 | DATE_SET_SECOND(self, second); |
| 699 | DATE_SET_MICROSECOND(self, usecond); |
| 700 | if (aware) { |
| 701 | Py_INCREF(tzinfo); |
| 702 | self->tzinfo = tzinfo; |
| 703 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 704 | DATE_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | } |
| 706 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 709 | static PyObject * |
| 710 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
| 711 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
| 712 | { |
| 713 | return new_datetime_ex2(year, month, day, hour, minute, second, usecond, |
| 714 | tzinfo, 0, type); |
| 715 | } |
| 716 | |
| 717 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ |
| 718 | new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 720 | |
| 721 | /* Create a time instance with no range checking. */ |
| 722 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 723 | new_time_ex2(int hour, int minute, int second, int usecond, |
| 724 | PyObject *tzinfo, int fold, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | PyDateTime_Time *self; |
| 727 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 730 | if (self != NULL) { |
| 731 | self->hastzinfo = aware; |
| 732 | self->hashcode = -1; |
| 733 | TIME_SET_HOUR(self, hour); |
| 734 | TIME_SET_MINUTE(self, minute); |
| 735 | TIME_SET_SECOND(self, second); |
| 736 | TIME_SET_MICROSECOND(self, usecond); |
| 737 | if (aware) { |
| 738 | Py_INCREF(tzinfo); |
| 739 | self->tzinfo = tzinfo; |
| 740 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 741 | TIME_SET_FOLD(self, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | } |
| 743 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 746 | static PyObject * |
| 747 | new_time_ex(int hour, int minute, int second, int usecond, |
| 748 | PyObject *tzinfo, PyTypeObject *type) |
| 749 | { |
| 750 | return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); |
| 751 | } |
| 752 | |
| 753 | #define new_time(hh, mm, ss, us, tzinfo, fold) \ |
| 754 | new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 755 | |
| 756 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 757 | * true. Passing false is a speed optimization, if you know for sure |
| 758 | * that seconds and microseconds are already in their proper ranges. In any |
| 759 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 760 | * of range). |
| 761 | */ |
| 762 | static PyObject * |
| 763 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 765 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | if (normalize) |
| 769 | normalize_d_s_us(&days, &seconds, µseconds); |
| 770 | assert(0 <= seconds && seconds < 24*3600); |
| 771 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | if (check_delta_day_range(days) < 0) |
| 774 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 777 | if (self != NULL) { |
| 778 | self->hashcode = -1; |
| 779 | SET_TD_DAYS(self, days); |
| 780 | SET_TD_SECONDS(self, seconds); |
| 781 | SET_TD_MICROSECONDS(self, microseconds); |
| 782 | } |
| 783 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | #define new_delta(d, s, us, normalize) \ |
| 787 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 788 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 789 | |
| 790 | typedef struct |
| 791 | { |
| 792 | PyObject_HEAD |
| 793 | PyObject *offset; |
| 794 | PyObject *name; |
| 795 | } PyDateTime_TimeZone; |
| 796 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 797 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 798 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 799 | /* The interned Epoch datetime instance */ |
| 800 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 801 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 802 | /* Create new timezone instance checking offset range. This |
| 803 | function does not check the name argument. Caller must assure |
| 804 | that offset is a timedelta instance and name is either NULL |
| 805 | or a unicode object. */ |
| 806 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 807 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 808 | { |
| 809 | PyDateTime_TimeZone *self; |
| 810 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 811 | |
| 812 | assert(offset != NULL); |
| 813 | assert(PyDelta_Check(offset)); |
| 814 | assert(name == NULL || PyUnicode_Check(name)); |
| 815 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 816 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 817 | if (self == NULL) { |
| 818 | return NULL; |
| 819 | } |
| 820 | Py_INCREF(offset); |
| 821 | self->offset = offset; |
| 822 | Py_XINCREF(name); |
| 823 | self->name = name; |
| 824 | return (PyObject *)self; |
| 825 | } |
| 826 | |
| 827 | static int delta_bool(PyDateTime_Delta *self); |
| 828 | |
| 829 | static PyObject * |
| 830 | new_timezone(PyObject *offset, PyObject *name) |
| 831 | { |
| 832 | assert(offset != NULL); |
| 833 | assert(PyDelta_Check(offset)); |
| 834 | assert(name == NULL || PyUnicode_Check(name)); |
| 835 | |
| 836 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 837 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 838 | return PyDateTime_TimeZone_UTC; |
| 839 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 840 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 841 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 842 | " representing a whole number of minutes," |
| 843 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 844 | return NULL; |
| 845 | } |
| 846 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 847 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 848 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 849 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 850 | " timedelta(hours=24)," |
| 851 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 852 | return NULL; |
| 853 | } |
| 854 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 855 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 858 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 859 | * tzinfo helpers. |
| 860 | */ |
| 861 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 862 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 863 | * raise TypeError and return -1. |
| 864 | */ |
| 865 | static int |
| 866 | check_tzinfo_subclass(PyObject *p) |
| 867 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | if (p == Py_None || PyTZInfo_Check(p)) |
| 869 | return 0; |
| 870 | PyErr_Format(PyExc_TypeError, |
| 871 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 872 | "not type '%s'", |
| 873 | Py_TYPE(p)->tp_name); |
| 874 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 877 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 878 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 879 | * and the caller must not decref the result. |
| 880 | */ |
| 881 | static PyObject * |
| 882 | get_tzinfo_member(PyObject *self) |
| 883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 887 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 888 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 889 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 894 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 895 | * be an instance of the tzinfo class. If the method returns None, this |
| 896 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 897 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 898 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 899 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 900 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 901 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 902 | call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 903 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 904 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 907 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 909 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 910 | if (tzinfo == Py_None) |
| 911 | Py_RETURN_NONE; |
| 912 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 913 | if (offset == Py_None || offset == NULL) |
| 914 | return offset; |
| 915 | if (PyDelta_Check(offset)) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 916 | if (GET_TD_MICROSECONDS(offset) != 0) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 917 | Py_DECREF(offset); |
| 918 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 919 | " representing a whole number of seconds"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 920 | return NULL; |
| 921 | } |
| 922 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 923 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 924 | Py_DECREF(offset); |
| 925 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 926 | " strictly between -timedelta(hours=24) and" |
| 927 | " timedelta(hours=24)."); |
| 928 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | else { |
| 932 | PyErr_Format(PyExc_TypeError, |
| 933 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 934 | "timedelta, not '%.200s'", |
| 935 | name, Py_TYPE(offset)->tp_name); |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 936 | Py_DECREF(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 937 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 939 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 940 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 944 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 945 | * 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] | 946 | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
| 947 | * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
| 948 | * # of minutes), ValueError is raised and this returns -1. Else *none is |
| 949 | * 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] | 950 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 951 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 952 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 953 | { |
| 954 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 957 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 958 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 959 | * 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] | 960 | & doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 961 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 962 | * ValueError is raised and this returns -1. Else *none is set to 0 and |
| 963 | * the offset is returned (as an int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 964 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 965 | static PyObject * |
| 966 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 967 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 968 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 971 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 972 | * 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] | 973 | * 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] | 974 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 975 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 976 | */ |
| 977 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 978 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 981 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | assert(tzinfo != NULL); |
| 984 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 985 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 988 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 989 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 990 | result = _PyObject_CallMethodId(tzinfo, &PyId_tzname, "O", tzinfoarg); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 991 | |
| 992 | if (result == NULL || result == Py_None) |
| 993 | return result; |
| 994 | |
| 995 | if (!PyUnicode_Check(result)) { |
| 996 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 997 | "return None or a string, not '%s'", |
| 998 | Py_TYPE(result)->tp_name); |
| 999 | Py_DECREF(result); |
| 1000 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1002 | |
| 1003 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1006 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 1007 | * stuff |
| 1008 | * ", tzinfo=" + repr(tzinfo) |
| 1009 | * before the closing ")". |
| 1010 | */ |
| 1011 | static PyObject * |
| 1012 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 1013 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | assert(PyUnicode_Check(repr)); |
| 1017 | assert(tzinfo); |
| 1018 | if (tzinfo == Py_None) |
| 1019 | return repr; |
| 1020 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1021 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1022 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | Py_DECREF(repr); |
| 1024 | if (temp == NULL) |
| 1025 | return NULL; |
| 1026 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 1027 | Py_DECREF(temp); |
| 1028 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1031 | /* repr is like "someclass(arg1, arg2)". If fold isn't 0, |
| 1032 | * stuff |
| 1033 | * ", fold=" + repr(tzinfo) |
| 1034 | * before the closing ")". |
| 1035 | */ |
| 1036 | static PyObject * |
| 1037 | append_keyword_fold(PyObject *repr, int fold) |
| 1038 | { |
| 1039 | PyObject *temp; |
| 1040 | |
| 1041 | assert(PyUnicode_Check(repr)); |
| 1042 | if (fold == 0) |
| 1043 | return repr; |
| 1044 | /* Get rid of the trailing ')'. */ |
| 1045 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 1046 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
| 1047 | Py_DECREF(repr); |
| 1048 | if (temp == NULL) |
| 1049 | return NULL; |
| 1050 | repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); |
| 1051 | Py_DECREF(temp); |
| 1052 | return repr; |
| 1053 | } |
| 1054 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1055 | /* --------------------------------------------------------------------------- |
| 1056 | * String format helpers. |
| 1057 | */ |
| 1058 | |
| 1059 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1060 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1061 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1062 | static const char * const DayNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1064 | }; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1065 | static const char * const MonthNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1067 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1068 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1073 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1074 | GET_DAY(date), hours, minutes, seconds, |
| 1075 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1078 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1079 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1080 | /* Add an hours & minutes UTC offset string to buf. buf has no more than |
| 1081 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1082 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1083 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1084 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1085 | * string of the form |
| 1086 | * sign HH sep MM |
| 1087 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1088 | * bogus, an appropriate exception is set and -1 is returned. |
| 1089 | */ |
| 1090 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1091 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1093 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1094 | PyObject *offset; |
| 1095 | int hours, minutes, seconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1099 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1100 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1101 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1103 | if (offset == Py_None) { |
| 1104 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | *buf = '\0'; |
| 1106 | return 0; |
| 1107 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1108 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1109 | if (GET_TD_DAYS(offset) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | sign = '-'; |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 1111 | Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1112 | if (offset == NULL) |
| 1113 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1115 | else { |
| 1116 | sign = '+'; |
| 1117 | } |
| 1118 | /* Offset is not negative here. */ |
| 1119 | seconds = GET_TD_SECONDS(offset); |
| 1120 | Py_DECREF(offset); |
| 1121 | minutes = divmod(seconds, 60, &seconds); |
| 1122 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 1123 | if (seconds == 0) |
| 1124 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
| 1125 | else |
| 1126 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, |
| 1127 | sep, minutes, sep, seconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1131 | static PyObject * |
| 1132 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | PyObject *temp; |
| 1135 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1136 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1137 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | if (Zreplacement == NULL) |
| 1140 | return NULL; |
| 1141 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1142 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | assert(tzinfoarg != NULL); |
| 1145 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1146 | if (temp == NULL) |
| 1147 | goto Error; |
| 1148 | if (temp == Py_None) { |
| 1149 | Py_DECREF(temp); |
| 1150 | return Zreplacement; |
| 1151 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | assert(PyUnicode_Check(temp)); |
| 1154 | /* Since the tzname is getting stuffed into the |
| 1155 | * format, we have to double any % signs so that |
| 1156 | * strftime doesn't treat them as format codes. |
| 1157 | */ |
| 1158 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1159 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | Py_DECREF(temp); |
| 1161 | if (Zreplacement == NULL) |
| 1162 | return NULL; |
| 1163 | if (!PyUnicode_Check(Zreplacement)) { |
| 1164 | PyErr_SetString(PyExc_TypeError, |
| 1165 | "tzname.replace() did not return a string"); |
| 1166 | goto Error; |
| 1167 | } |
| 1168 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1169 | |
| 1170 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | Py_DECREF(Zreplacement); |
| 1172 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1175 | static PyObject * |
| 1176 | make_freplacement(PyObject *object) |
| 1177 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | char freplacement[64]; |
| 1179 | if (PyTime_Check(object)) |
| 1180 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1181 | else if (PyDateTime_Check(object)) |
| 1182 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1183 | else |
| 1184 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1189 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1190 | * 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] | 1191 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1192 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1193 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1194 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1195 | */ |
| 1196 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1197 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1199 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1203 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1204 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | const char *pin; /* pointer to next char in input format */ |
| 1207 | Py_ssize_t flen; /* length of input format */ |
| 1208 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1211 | char *pnew; /* pointer to available byte in output format */ |
| 1212 | size_t totalnew; /* number bytes total in output format buffer, |
| 1213 | exclusive of trailing \0 */ |
| 1214 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1217 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | assert(object && format && timetuple); |
| 1220 | assert(PyUnicode_Check(format)); |
| 1221 | /* Convert the input format to a C string and size */ |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1222 | pin = PyUnicode_AsUTF8AndSize(format, &flen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | if (!pin) |
| 1224 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1227 | * a new format. Since computing the replacements for those codes |
| 1228 | * is expensive, don't unless they're actually used. |
| 1229 | */ |
| 1230 | if (flen > INT_MAX - 1) { |
| 1231 | PyErr_NoMemory(); |
| 1232 | goto Done; |
| 1233 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1236 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1237 | if (newfmt == NULL) goto Done; |
| 1238 | pnew = PyBytes_AsString(newfmt); |
| 1239 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | while ((ch = *pin++) != '\0') { |
| 1242 | if (ch != '%') { |
| 1243 | ptoappend = pin - 1; |
| 1244 | ntoappend = 1; |
| 1245 | } |
| 1246 | else if ((ch = *pin++) == '\0') { |
| 1247 | /* There's a lone trailing %; doesn't make sense. */ |
| 1248 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1249 | "ends with raw %"); |
| 1250 | goto Done; |
| 1251 | } |
| 1252 | /* A % has been seen and ch is the character after it. */ |
| 1253 | else if (ch == 'z') { |
| 1254 | if (zreplacement == NULL) { |
| 1255 | /* format utcoffset */ |
| 1256 | char buf[100]; |
| 1257 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1258 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1259 | if (zreplacement == NULL) goto Done; |
| 1260 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1261 | assert(tzinfoarg != NULL); |
| 1262 | if (format_utcoffset(buf, |
| 1263 | sizeof(buf), |
| 1264 | "", |
| 1265 | tzinfo, |
| 1266 | tzinfoarg) < 0) |
| 1267 | goto Done; |
| 1268 | Py_DECREF(zreplacement); |
| 1269 | zreplacement = |
| 1270 | PyBytes_FromStringAndSize(buf, |
| 1271 | strlen(buf)); |
| 1272 | if (zreplacement == NULL) |
| 1273 | goto Done; |
| 1274 | } |
| 1275 | } |
| 1276 | assert(zreplacement != NULL); |
| 1277 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1278 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1279 | } |
| 1280 | else if (ch == 'Z') { |
| 1281 | /* format tzname */ |
| 1282 | if (Zreplacement == NULL) { |
| 1283 | Zreplacement = make_Zreplacement(object, |
| 1284 | tzinfoarg); |
| 1285 | if (Zreplacement == NULL) |
| 1286 | goto Done; |
| 1287 | } |
| 1288 | assert(Zreplacement != NULL); |
| 1289 | assert(PyUnicode_Check(Zreplacement)); |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1290 | ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1292 | if (ptoappend == NULL) |
| 1293 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | } |
| 1295 | else if (ch == 'f') { |
| 1296 | /* format microseconds */ |
| 1297 | if (freplacement == NULL) { |
| 1298 | freplacement = make_freplacement(object); |
| 1299 | if (freplacement == NULL) |
| 1300 | goto Done; |
| 1301 | } |
| 1302 | assert(freplacement != NULL); |
| 1303 | assert(PyBytes_Check(freplacement)); |
| 1304 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1305 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1306 | } |
| 1307 | else { |
| 1308 | /* percent followed by neither z nor Z */ |
| 1309 | ptoappend = pin - 2; |
| 1310 | ntoappend = 2; |
| 1311 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | /* Append the ntoappend chars starting at ptoappend to |
| 1314 | * the new format. |
| 1315 | */ |
| 1316 | if (ntoappend == 0) |
| 1317 | continue; |
| 1318 | assert(ptoappend != NULL); |
| 1319 | assert(ntoappend > 0); |
| 1320 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1321 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | PyErr_NoMemory(); |
| 1323 | goto Done; |
| 1324 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1325 | totalnew <<= 1; |
| 1326 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1329 | } |
| 1330 | memcpy(pnew, ptoappend, ntoappend); |
| 1331 | pnew += ntoappend; |
| 1332 | usednew += ntoappend; |
| 1333 | assert(usednew <= totalnew); |
| 1334 | } /* end while() */ |
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 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1337 | goto Done; |
| 1338 | { |
| 1339 | PyObject *format; |
| 1340 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | if (time == NULL) |
| 1343 | goto Done; |
| 1344 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1345 | if (format != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1346 | result = _PyObject_CallMethodId(time, &PyId_strftime, "OO", |
| 1347 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | Py_DECREF(format); |
| 1349 | } |
| 1350 | Py_DECREF(time); |
| 1351 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1352 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | Py_XDECREF(freplacement); |
| 1354 | Py_XDECREF(zreplacement); |
| 1355 | Py_XDECREF(Zreplacement); |
| 1356 | Py_XDECREF(newfmt); |
| 1357 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1360 | /* --------------------------------------------------------------------------- |
| 1361 | * Wrap functions from the time module. These aren't directly available |
| 1362 | * from C. Perhaps they should be. |
| 1363 | */ |
| 1364 | |
| 1365 | /* Call time.time() and return its result (a Python float). */ |
| 1366 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1367 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | PyObject *result = NULL; |
| 1370 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1373 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1374 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 1375 | result = _PyObject_CallMethodId(time, &PyId_time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | Py_DECREF(time); |
| 1377 | } |
| 1378 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1382 | * computed from the y,m,d args. |
| 1383 | */ |
| 1384 | static PyObject * |
| 1385 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | PyObject *time; |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1388 | PyObject *result; |
| 1389 | _Py_IDENTIFIER(struct_time); |
| 1390 | PyObject *args; |
| 1391 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | time = PyImport_ImportModuleNoBlock("time"); |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1394 | if (time == NULL) { |
| 1395 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | } |
Victor Stinner | 2b63597 | 2016-12-09 00:38:16 +0100 | [diff] [blame] | 1397 | |
| 1398 | args = Py_BuildValue("iiiiiiiii", |
| 1399 | y, m, d, |
| 1400 | hh, mm, ss, |
| 1401 | weekday(y, m, d), |
| 1402 | days_before_month(y, m) + d, |
| 1403 | dstflag); |
| 1404 | if (args == NULL) { |
| 1405 | Py_DECREF(time); |
| 1406 | return NULL; |
| 1407 | } |
| 1408 | |
| 1409 | result = _PyObject_CallMethodIdObjArgs(time, &PyId_struct_time, |
| 1410 | args, NULL); |
| 1411 | Py_DECREF(time); |
Victor Stinner | ddc120f | 2016-12-09 15:35:40 +0100 | [diff] [blame^] | 1412 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /* --------------------------------------------------------------------------- |
| 1417 | * Miscellaneous helpers. |
| 1418 | */ |
| 1419 | |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1420 | /* 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] | 1421 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1422 | * This little helper turns that into a bool result for rich comparisons. |
| 1423 | */ |
| 1424 | static PyObject * |
| 1425 | diff_to_bool(int diff, int op) |
| 1426 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | PyObject *result; |
| 1428 | int istrue; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | switch (op) { |
| 1431 | case Py_EQ: istrue = diff == 0; break; |
| 1432 | case Py_NE: istrue = diff != 0; break; |
| 1433 | case Py_LE: istrue = diff <= 0; break; |
| 1434 | case Py_GE: istrue = diff >= 0; break; |
| 1435 | case Py_LT: istrue = diff < 0; break; |
| 1436 | case Py_GT: istrue = diff > 0; break; |
| 1437 | default: |
| 1438 | assert(! "op unknown"); |
| 1439 | istrue = 0; /* To shut up compiler */ |
| 1440 | } |
| 1441 | result = istrue ? Py_True : Py_False; |
| 1442 | Py_INCREF(result); |
| 1443 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1446 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1447 | static PyObject * |
| 1448 | cmperror(PyObject *a, PyObject *b) |
| 1449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | PyErr_Format(PyExc_TypeError, |
| 1451 | "can't compare %s to %s", |
| 1452 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1453 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1456 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1457 | * Cached Python objects; these are set by the module init function. |
| 1458 | */ |
| 1459 | |
| 1460 | /* Conversion factors. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 1461 | static PyObject *one = NULL; /* 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1463 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1464 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1465 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1466 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1467 | 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] | 1468 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1469 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1470 | /* --------------------------------------------------------------------------- |
| 1471 | * Class implementations. |
| 1472 | */ |
| 1473 | |
| 1474 | /* |
| 1475 | * PyDateTime_Delta implementation. |
| 1476 | */ |
| 1477 | |
| 1478 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1480 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1481 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1482 | * due to ubiquitous overflow possibilities. |
| 1483 | */ |
| 1484 | static PyObject * |
| 1485 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | PyObject *x1 = NULL; |
| 1488 | PyObject *x2 = NULL; |
| 1489 | PyObject *x3 = NULL; |
| 1490 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1493 | if (x1 == NULL) |
| 1494 | goto Done; |
| 1495 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1496 | if (x2 == NULL) |
| 1497 | goto Done; |
| 1498 | Py_DECREF(x1); |
| 1499 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | /* x2 has days in seconds */ |
| 1502 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1503 | if (x1 == NULL) |
| 1504 | goto Done; |
| 1505 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1506 | if (x3 == NULL) |
| 1507 | goto Done; |
| 1508 | Py_DECREF(x1); |
| 1509 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1510 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | /* x3 has days+seconds in seconds */ |
| 1513 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1514 | if (x1 == NULL) |
| 1515 | goto Done; |
| 1516 | Py_DECREF(x3); |
| 1517 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | /* x1 has days+seconds in us */ |
| 1520 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1521 | if (x2 == NULL) |
| 1522 | goto Done; |
| 1523 | result = PyNumber_Add(x1, x2); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1524 | |
| 1525 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | Py_XDECREF(x1); |
| 1527 | Py_XDECREF(x2); |
| 1528 | Py_XDECREF(x3); |
| 1529 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1532 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1533 | */ |
| 1534 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1535 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1536 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | int us; |
| 1538 | int s; |
| 1539 | int d; |
| 1540 | long temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | PyObject *tuple = NULL; |
| 1543 | PyObject *num = NULL; |
| 1544 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1547 | if (tuple == NULL) |
| 1548 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1551 | if (num == NULL) |
| 1552 | goto Done; |
| 1553 | temp = PyLong_AsLong(num); |
| 1554 | num = NULL; |
| 1555 | if (temp == -1 && PyErr_Occurred()) |
| 1556 | goto Done; |
| 1557 | assert(0 <= temp && temp < 1000000); |
| 1558 | us = (int)temp; |
| 1559 | if (us < 0) { |
| 1560 | /* The divisor was positive, so this must be an error. */ |
| 1561 | assert(PyErr_Occurred()); |
| 1562 | goto Done; |
| 1563 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1566 | if (num == NULL) |
| 1567 | goto Done; |
| 1568 | Py_INCREF(num); |
| 1569 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1572 | if (tuple == NULL) |
| 1573 | goto Done; |
| 1574 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1577 | if (num == NULL) |
| 1578 | goto Done; |
| 1579 | temp = PyLong_AsLong(num); |
| 1580 | num = NULL; |
| 1581 | if (temp == -1 && PyErr_Occurred()) |
| 1582 | goto Done; |
| 1583 | assert(0 <= temp && temp < 24*3600); |
| 1584 | s = (int)temp; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | if (s < 0) { |
| 1587 | /* The divisor was positive, so this must be an error. */ |
| 1588 | assert(PyErr_Occurred()); |
| 1589 | goto Done; |
| 1590 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1593 | if (num == NULL) |
| 1594 | goto Done; |
| 1595 | Py_INCREF(num); |
| 1596 | temp = PyLong_AsLong(num); |
| 1597 | if (temp == -1 && PyErr_Occurred()) |
| 1598 | goto Done; |
| 1599 | d = (int)temp; |
| 1600 | if ((long)d != temp) { |
| 1601 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1602 | "large to fit in a C int"); |
| 1603 | goto Done; |
| 1604 | } |
| 1605 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1606 | |
| 1607 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | Py_XDECREF(tuple); |
| 1609 | Py_XDECREF(num); |
| 1610 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | #define microseconds_to_delta(pymicros) \ |
| 1614 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1615 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1616 | static PyObject * |
| 1617 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | PyObject *pyus_in; |
| 1620 | PyObject *pyus_out; |
| 1621 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | pyus_in = delta_to_microseconds(delta); |
| 1624 | if (pyus_in == NULL) |
| 1625 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1628 | Py_DECREF(pyus_in); |
| 1629 | if (pyus_out == NULL) |
| 1630 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | result = microseconds_to_delta(pyus_out); |
| 1633 | Py_DECREF(pyus_out); |
| 1634 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1638 | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) |
| 1639 | { |
| 1640 | PyObject *result = NULL; |
| 1641 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1642 | PyObject *ratio = NULL; |
| 1643 | |
| 1644 | pyus_in = delta_to_microseconds(delta); |
| 1645 | if (pyus_in == NULL) |
| 1646 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1647 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1648 | if (ratio == NULL) |
| 1649 | goto error; |
| 1650 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); |
| 1651 | Py_DECREF(pyus_in); |
| 1652 | pyus_in = NULL; |
| 1653 | if (temp == NULL) |
| 1654 | goto error; |
| 1655 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); |
| 1656 | Py_DECREF(temp); |
| 1657 | if (pyus_out == NULL) |
| 1658 | goto error; |
| 1659 | result = microseconds_to_delta(pyus_out); |
| 1660 | Py_DECREF(pyus_out); |
| 1661 | error: |
| 1662 | Py_XDECREF(pyus_in); |
| 1663 | Py_XDECREF(ratio); |
| 1664 | |
| 1665 | return result; |
| 1666 | } |
| 1667 | |
| 1668 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1669 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | PyObject *pyus_in; |
| 1672 | PyObject *pyus_out; |
| 1673 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | pyus_in = delta_to_microseconds(delta); |
| 1676 | if (pyus_in == NULL) |
| 1677 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1680 | Py_DECREF(pyus_in); |
| 1681 | if (pyus_out == NULL) |
| 1682 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | result = microseconds_to_delta(pyus_out); |
| 1685 | Py_DECREF(pyus_out); |
| 1686 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1690 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1692 | PyObject *pyus_left; |
| 1693 | PyObject *pyus_right; |
| 1694 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | pyus_left = delta_to_microseconds(left); |
| 1697 | if (pyus_left == NULL) |
| 1698 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | pyus_right = delta_to_microseconds(right); |
| 1701 | if (pyus_right == NULL) { |
| 1702 | Py_DECREF(pyus_left); |
| 1703 | return NULL; |
| 1704 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1707 | Py_DECREF(pyus_left); |
| 1708 | Py_DECREF(pyus_right); |
| 1709 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static PyObject * |
| 1713 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | PyObject *pyus_left; |
| 1716 | PyObject *pyus_right; |
| 1717 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | pyus_left = delta_to_microseconds(left); |
| 1720 | if (pyus_left == NULL) |
| 1721 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | pyus_right = delta_to_microseconds(right); |
| 1724 | if (pyus_right == NULL) { |
| 1725 | Py_DECREF(pyus_left); |
| 1726 | return NULL; |
| 1727 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1730 | Py_DECREF(pyus_left); |
| 1731 | Py_DECREF(pyus_right); |
| 1732 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1736 | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) |
| 1737 | { |
| 1738 | PyObject *result = NULL; |
| 1739 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1740 | PyObject *ratio = NULL; |
| 1741 | |
| 1742 | pyus_in = delta_to_microseconds(delta); |
| 1743 | if (pyus_in == NULL) |
| 1744 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1745 | ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1746 | if (ratio == NULL) |
| 1747 | goto error; |
| 1748 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1)); |
| 1749 | Py_DECREF(pyus_in); |
| 1750 | pyus_in = NULL; |
| 1751 | if (temp == NULL) |
| 1752 | goto error; |
| 1753 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0)); |
| 1754 | Py_DECREF(temp); |
| 1755 | if (pyus_out == NULL) |
| 1756 | goto error; |
| 1757 | result = microseconds_to_delta(pyus_out); |
| 1758 | Py_DECREF(pyus_out); |
| 1759 | error: |
| 1760 | Py_XDECREF(pyus_in); |
| 1761 | Py_XDECREF(ratio); |
| 1762 | |
| 1763 | return result; |
| 1764 | } |
| 1765 | |
| 1766 | static PyObject * |
| 1767 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 1768 | { |
| 1769 | PyObject *result; |
| 1770 | PyObject *pyus_in, *pyus_out; |
| 1771 | pyus_in = delta_to_microseconds(delta); |
| 1772 | if (pyus_in == NULL) |
| 1773 | return NULL; |
| 1774 | pyus_out = divide_nearest(pyus_in, i); |
| 1775 | Py_DECREF(pyus_in); |
| 1776 | if (pyus_out == NULL) |
| 1777 | return NULL; |
| 1778 | result = microseconds_to_delta(pyus_out); |
| 1779 | Py_DECREF(pyus_out); |
| 1780 | |
| 1781 | return result; |
| 1782 | } |
| 1783 | |
| 1784 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1785 | delta_add(PyObject *left, PyObject *right) |
| 1786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1790 | /* delta + delta */ |
| 1791 | /* The C-level additions can't overflow because of the |
| 1792 | * invariant bounds. |
| 1793 | */ |
| 1794 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 1795 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 1796 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 1797 | GET_TD_MICROSECONDS(right); |
| 1798 | result = new_delta(days, seconds, microseconds, 1); |
| 1799 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | if (result == Py_NotImplemented) |
| 1802 | Py_INCREF(result); |
| 1803 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | static PyObject * |
| 1807 | delta_negative(PyDateTime_Delta *self) |
| 1808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | return new_delta(-GET_TD_DAYS(self), |
| 1810 | -GET_TD_SECONDS(self), |
| 1811 | -GET_TD_MICROSECONDS(self), |
| 1812 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | static PyObject * |
| 1816 | delta_positive(PyDateTime_Delta *self) |
| 1817 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | /* Could optimize this (by returning self) if this isn't a |
| 1819 | * subclass -- but who uses unary + ? Approximately nobody. |
| 1820 | */ |
| 1821 | return new_delta(GET_TD_DAYS(self), |
| 1822 | GET_TD_SECONDS(self), |
| 1823 | GET_TD_MICROSECONDS(self), |
| 1824 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | static PyObject * |
| 1828 | delta_abs(PyDateTime_Delta *self) |
| 1829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1833 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | if (GET_TD_DAYS(self) < 0) |
| 1836 | result = delta_negative(self); |
| 1837 | else |
| 1838 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | static PyObject * |
| 1844 | delta_subtract(PyObject *left, PyObject *right) |
| 1845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1849 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 1850 | /* The C-level additions can't overflow because of the |
| 1851 | * invariant bounds. |
| 1852 | */ |
| 1853 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 1854 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 1855 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 1856 | GET_TD_MICROSECONDS(right); |
| 1857 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | if (result == Py_NotImplemented) |
| 1861 | Py_INCREF(result); |
| 1862 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1865 | static int |
| 1866 | delta_cmp(PyObject *self, PyObject *other) |
| 1867 | { |
| 1868 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 1869 | if (diff == 0) { |
| 1870 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 1871 | if (diff == 0) |
| 1872 | diff = GET_TD_MICROSECONDS(self) - |
| 1873 | GET_TD_MICROSECONDS(other); |
| 1874 | } |
| 1875 | return diff; |
| 1876 | } |
| 1877 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1878 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1879 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1880 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1881 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1882 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | return diff_to_bool(diff, op); |
| 1884 | } |
| 1885 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1886 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1891 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1892 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1893 | delta_hash(PyDateTime_Delta *self) |
| 1894 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | if (self->hashcode == -1) { |
| 1896 | PyObject *temp = delta_getstate(self); |
| 1897 | if (temp != NULL) { |
| 1898 | self->hashcode = PyObject_Hash(temp); |
| 1899 | Py_DECREF(temp); |
| 1900 | } |
| 1901 | } |
| 1902 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | static PyObject * |
| 1906 | delta_multiply(PyObject *left, PyObject *right) |
| 1907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | if (PyDelta_Check(left)) { |
| 1911 | /* delta * ??? */ |
| 1912 | if (PyLong_Check(right)) |
| 1913 | result = multiply_int_timedelta(right, |
| 1914 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1915 | else if (PyFloat_Check(right)) |
| 1916 | result = multiply_float_timedelta(right, |
| 1917 | (PyDateTime_Delta *) left); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | } |
| 1919 | else if (PyLong_Check(left)) |
| 1920 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1921 | (PyDateTime_Delta *) right); |
| 1922 | else if (PyFloat_Check(left)) |
| 1923 | result = multiply_float_timedelta(left, |
| 1924 | (PyDateTime_Delta *) right); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | if (result == Py_NotImplemented) |
| 1927 | Py_INCREF(result); |
| 1928 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | static PyObject * |
| 1932 | delta_divide(PyObject *left, PyObject *right) |
| 1933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | if (PyDelta_Check(left)) { |
| 1937 | /* delta * ??? */ |
| 1938 | if (PyLong_Check(right)) |
| 1939 | result = divide_timedelta_int( |
| 1940 | (PyDateTime_Delta *)left, |
| 1941 | right); |
| 1942 | else if (PyDelta_Check(right)) |
| 1943 | result = divide_timedelta_timedelta( |
| 1944 | (PyDateTime_Delta *)left, |
| 1945 | (PyDateTime_Delta *)right); |
| 1946 | } |
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 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1953 | static PyObject * |
| 1954 | delta_truedivide(PyObject *left, PyObject *right) |
| 1955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | if (PyDelta_Check(left)) { |
| 1959 | if (PyDelta_Check(right)) |
| 1960 | result = truedivide_timedelta_timedelta( |
| 1961 | (PyDateTime_Delta *)left, |
| 1962 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1963 | else if (PyFloat_Check(right)) |
| 1964 | result = truedivide_timedelta_float( |
| 1965 | (PyDateTime_Delta *)left, right); |
| 1966 | else if (PyLong_Check(right)) |
| 1967 | result = truedivide_timedelta_int( |
| 1968 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | if (result == Py_NotImplemented) |
| 1972 | Py_INCREF(result); |
| 1973 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | static PyObject * |
| 1977 | delta_remainder(PyObject *left, PyObject *right) |
| 1978 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | PyObject *pyus_left; |
| 1980 | PyObject *pyus_right; |
| 1981 | PyObject *pyus_remainder; |
| 1982 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1983 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1984 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1985 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1988 | if (pyus_left == NULL) |
| 1989 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1992 | if (pyus_right == NULL) { |
| 1993 | Py_DECREF(pyus_left); |
| 1994 | return NULL; |
| 1995 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 1998 | Py_DECREF(pyus_left); |
| 1999 | Py_DECREF(pyus_right); |
| 2000 | if (pyus_remainder == NULL) |
| 2001 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | remainder = microseconds_to_delta(pyus_remainder); |
| 2004 | Py_DECREF(pyus_remainder); |
| 2005 | if (remainder == NULL) |
| 2006 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | static PyObject * |
| 2012 | delta_divmod(PyObject *left, PyObject *right) |
| 2013 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2014 | PyObject *pyus_left; |
| 2015 | PyObject *pyus_right; |
| 2016 | PyObject *divmod; |
| 2017 | PyObject *delta; |
| 2018 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2019 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2020 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 2021 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 2024 | if (pyus_left == NULL) |
| 2025 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 2028 | if (pyus_right == NULL) { |
| 2029 | Py_DECREF(pyus_left); |
| 2030 | return NULL; |
| 2031 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
| 2034 | Py_DECREF(pyus_left); |
| 2035 | Py_DECREF(pyus_right); |
| 2036 | if (divmod == NULL) |
| 2037 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | assert(PyTuple_Size(divmod) == 2); |
| 2040 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 2041 | if (delta == NULL) { |
| 2042 | Py_DECREF(divmod); |
| 2043 | return NULL; |
| 2044 | } |
| 2045 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 2046 | Py_DECREF(delta); |
| 2047 | Py_DECREF(divmod); |
| 2048 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2051 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 2052 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 2053 | * so far, and there are factor microseconds per current unit, the number |
| 2054 | * of which is given by num. num * factor is added to sofar in a |
| 2055 | * numerically careful way, and that's the result. Any fractional |
| 2056 | * microseconds left over (this can happen if num is a float type) are |
| 2057 | * added into *leftover. |
| 2058 | * Note that there are many ways this can give an error (NULL) return. |
| 2059 | */ |
| 2060 | static PyObject * |
| 2061 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2062 | double *leftover) |
| 2063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | PyObject *prod; |
| 2065 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | if (PyLong_Check(num)) { |
| 2070 | prod = PyNumber_Multiply(num, factor); |
| 2071 | if (prod == NULL) |
| 2072 | return NULL; |
| 2073 | sum = PyNumber_Add(sofar, prod); |
| 2074 | Py_DECREF(prod); |
| 2075 | return sum; |
| 2076 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | if (PyFloat_Check(num)) { |
| 2079 | double dnum; |
| 2080 | double fracpart; |
| 2081 | double intpart; |
| 2082 | PyObject *x; |
| 2083 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | /* The Plan: decompose num into an integer part and a |
| 2086 | * fractional part, num = intpart + fracpart. |
| 2087 | * Then num * factor == |
| 2088 | * intpart * factor + fracpart * factor |
| 2089 | * and the LHS can be computed exactly in long arithmetic. |
| 2090 | * The RHS is again broken into an int part and frac part. |
| 2091 | * and the frac part is added into *leftover. |
| 2092 | */ |
| 2093 | dnum = PyFloat_AsDouble(num); |
| 2094 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2095 | return NULL; |
| 2096 | fracpart = modf(dnum, &intpart); |
| 2097 | x = PyLong_FromDouble(intpart); |
| 2098 | if (x == NULL) |
| 2099 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | prod = PyNumber_Multiply(x, factor); |
| 2102 | Py_DECREF(x); |
| 2103 | if (prod == NULL) |
| 2104 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | sum = PyNumber_Add(sofar, prod); |
| 2107 | Py_DECREF(prod); |
| 2108 | if (sum == NULL) |
| 2109 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | if (fracpart == 0.0) |
| 2112 | return sum; |
| 2113 | /* So far we've lost no information. Dealing with the |
| 2114 | * fractional part requires float arithmetic, and may |
| 2115 | * lose a little info. |
| 2116 | */ |
| 2117 | assert(PyLong_Check(factor)); |
| 2118 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | dnum *= fracpart; |
| 2121 | fracpart = modf(dnum, &intpart); |
| 2122 | x = PyLong_FromDouble(intpart); |
| 2123 | if (x == NULL) { |
| 2124 | Py_DECREF(sum); |
| 2125 | return NULL; |
| 2126 | } |
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 | y = PyNumber_Add(sum, x); |
| 2129 | Py_DECREF(sum); |
| 2130 | Py_DECREF(x); |
| 2131 | *leftover += fracpart; |
| 2132 | return y; |
| 2133 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | PyErr_Format(PyExc_TypeError, |
| 2136 | "unsupported type for timedelta %s component: %s", |
| 2137 | tag, Py_TYPE(num)->tp_name); |
| 2138 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | static PyObject * |
| 2142 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | /* Argument objects. */ |
| 2147 | PyObject *day = NULL; |
| 2148 | PyObject *second = NULL; |
| 2149 | PyObject *us = NULL; |
| 2150 | PyObject *ms = NULL; |
| 2151 | PyObject *minute = NULL; |
| 2152 | PyObject *hour = NULL; |
| 2153 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2156 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2157 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | static char *keywords[] = { |
| 2160 | "days", "seconds", "microseconds", "milliseconds", |
| 2161 | "minutes", "hours", "weeks", NULL |
| 2162 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2165 | keywords, |
| 2166 | &day, &second, &us, |
| 2167 | &ms, &minute, &hour, &week) == 0) |
| 2168 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | x = PyLong_FromLong(0); |
| 2171 | if (x == NULL) |
| 2172 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | #define CLEANUP \ |
| 2175 | Py_DECREF(x); \ |
| 2176 | x = y; \ |
| 2177 | if (x == NULL) \ |
| 2178 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | if (us) { |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2181 | y = accum("microseconds", x, us, one, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2182 | CLEANUP; |
| 2183 | } |
| 2184 | if (ms) { |
| 2185 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2186 | CLEANUP; |
| 2187 | } |
| 2188 | if (second) { |
| 2189 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2190 | CLEANUP; |
| 2191 | } |
| 2192 | if (minute) { |
| 2193 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2194 | CLEANUP; |
| 2195 | } |
| 2196 | if (hour) { |
| 2197 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2198 | CLEANUP; |
| 2199 | } |
| 2200 | if (day) { |
| 2201 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2202 | CLEANUP; |
| 2203 | } |
| 2204 | if (week) { |
| 2205 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2206 | CLEANUP; |
| 2207 | } |
| 2208 | if (leftover_us) { |
| 2209 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2210 | double whole_us = round(leftover_us); |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2211 | int x_is_odd; |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2212 | PyObject *temp; |
| 2213 | |
Victor Stinner | 69cc487 | 2015-09-08 23:58:54 +0200 | [diff] [blame] | 2214 | whole_us = round(leftover_us); |
| 2215 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2216 | /* We're exactly halfway between two integers. In order |
| 2217 | * to do round-half-to-even, we must determine whether x |
| 2218 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2219 | * code below uses bitwise and operation to check the last |
| 2220 | * bit. */ |
| 2221 | temp = PyNumber_And(x, one); /* temp <- x & 1 */ |
| 2222 | if (temp == NULL) { |
| 2223 | Py_DECREF(x); |
| 2224 | goto Done; |
| 2225 | } |
| 2226 | x_is_odd = PyObject_IsTrue(temp); |
| 2227 | Py_DECREF(temp); |
| 2228 | if (x_is_odd == -1) { |
| 2229 | Py_DECREF(x); |
| 2230 | goto Done; |
| 2231 | } |
| 2232 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2233 | } |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2234 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2235 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | if (temp == NULL) { |
| 2238 | Py_DECREF(x); |
| 2239 | goto Done; |
| 2240 | } |
| 2241 | y = PyNumber_Add(x, temp); |
| 2242 | Py_DECREF(temp); |
| 2243 | CLEANUP; |
| 2244 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | self = microseconds_to_delta_ex(x, type); |
| 2247 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2248 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2249 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2250 | |
| 2251 | #undef CLEANUP |
| 2252 | } |
| 2253 | |
| 2254 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2255 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | return (GET_TD_DAYS(self) != 0 |
| 2258 | || GET_TD_SECONDS(self) != 0 |
| 2259 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | static PyObject * |
| 2263 | delta_repr(PyDateTime_Delta *self) |
| 2264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | if (GET_TD_MICROSECONDS(self) != 0) |
| 2266 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2267 | Py_TYPE(self)->tp_name, |
| 2268 | GET_TD_DAYS(self), |
| 2269 | GET_TD_SECONDS(self), |
| 2270 | GET_TD_MICROSECONDS(self)); |
| 2271 | if (GET_TD_SECONDS(self) != 0) |
| 2272 | return PyUnicode_FromFormat("%s(%d, %d)", |
| 2273 | Py_TYPE(self)->tp_name, |
| 2274 | GET_TD_DAYS(self), |
| 2275 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | return PyUnicode_FromFormat("%s(%d)", |
| 2278 | Py_TYPE(self)->tp_name, |
| 2279 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | static PyObject * |
| 2283 | delta_str(PyDateTime_Delta *self) |
| 2284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2285 | int us = GET_TD_MICROSECONDS(self); |
| 2286 | int seconds = GET_TD_SECONDS(self); |
| 2287 | int minutes = divmod(seconds, 60, &seconds); |
| 2288 | int hours = divmod(minutes, 60, &minutes); |
| 2289 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | if (days) { |
| 2292 | if (us) |
| 2293 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2294 | days, (days == 1 || days == -1) ? "" : "s", |
| 2295 | hours, minutes, seconds, us); |
| 2296 | else |
| 2297 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2298 | days, (days == 1 || days == -1) ? "" : "s", |
| 2299 | hours, minutes, seconds); |
| 2300 | } else { |
| 2301 | if (us) |
| 2302 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2303 | hours, minutes, seconds, us); |
| 2304 | else |
| 2305 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2306 | hours, minutes, seconds); |
| 2307 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2308 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2311 | /* Pickle support, a simple use of __reduce__. */ |
| 2312 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2313 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2314 | static PyObject * |
| 2315 | delta_getstate(PyDateTime_Delta *self) |
| 2316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2318 | GET_TD_SECONDS(self), |
| 2319 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2322 | static PyObject * |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2323 | delta_total_seconds(PyObject *self) |
| 2324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | PyObject *total_seconds; |
| 2326 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2329 | if (total_microseconds == NULL) |
| 2330 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2331 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2332 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2339 | delta_reduce(PyDateTime_Delta* self) |
| 2340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2345 | |
| 2346 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | {"days", T_INT, OFFSET(days), READONLY, |
| 2349 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2352 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2355 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2356 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2357 | }; |
| 2358 | |
| 2359 | static PyMethodDef delta_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
| 2361 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2364 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2367 | }; |
| 2368 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2369 | static const char delta_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2370 | PyDoc_STR("Difference between two datetime values."); |
| 2371 | |
| 2372 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | delta_add, /* nb_add */ |
| 2374 | delta_subtract, /* nb_subtract */ |
| 2375 | delta_multiply, /* nb_multiply */ |
| 2376 | delta_remainder, /* nb_remainder */ |
| 2377 | delta_divmod, /* nb_divmod */ |
| 2378 | 0, /* nb_power */ |
| 2379 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2380 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2381 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2382 | (inquiry)delta_bool, /* nb_bool */ |
| 2383 | 0, /*nb_invert*/ |
| 2384 | 0, /*nb_lshift*/ |
| 2385 | 0, /*nb_rshift*/ |
| 2386 | 0, /*nb_and*/ |
| 2387 | 0, /*nb_xor*/ |
| 2388 | 0, /*nb_or*/ |
| 2389 | 0, /*nb_int*/ |
| 2390 | 0, /*nb_reserved*/ |
| 2391 | 0, /*nb_float*/ |
| 2392 | 0, /*nb_inplace_add*/ |
| 2393 | 0, /*nb_inplace_subtract*/ |
| 2394 | 0, /*nb_inplace_multiply*/ |
| 2395 | 0, /*nb_inplace_remainder*/ |
| 2396 | 0, /*nb_inplace_power*/ |
| 2397 | 0, /*nb_inplace_lshift*/ |
| 2398 | 0, /*nb_inplace_rshift*/ |
| 2399 | 0, /*nb_inplace_and*/ |
| 2400 | 0, /*nb_inplace_xor*/ |
| 2401 | 0, /*nb_inplace_or*/ |
| 2402 | delta_divide, /* nb_floor_divide */ |
| 2403 | delta_truedivide, /* nb_true_divide */ |
| 2404 | 0, /* nb_inplace_floor_divide */ |
| 2405 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2406 | }; |
| 2407 | |
| 2408 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2409 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2410 | "datetime.timedelta", /* tp_name */ |
| 2411 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2412 | 0, /* tp_itemsize */ |
| 2413 | 0, /* tp_dealloc */ |
| 2414 | 0, /* tp_print */ |
| 2415 | 0, /* tp_getattr */ |
| 2416 | 0, /* tp_setattr */ |
| 2417 | 0, /* tp_reserved */ |
| 2418 | (reprfunc)delta_repr, /* tp_repr */ |
| 2419 | &delta_as_number, /* tp_as_number */ |
| 2420 | 0, /* tp_as_sequence */ |
| 2421 | 0, /* tp_as_mapping */ |
| 2422 | (hashfunc)delta_hash, /* tp_hash */ |
| 2423 | 0, /* tp_call */ |
| 2424 | (reprfunc)delta_str, /* tp_str */ |
| 2425 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2426 | 0, /* tp_setattro */ |
| 2427 | 0, /* tp_as_buffer */ |
| 2428 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2429 | delta_doc, /* tp_doc */ |
| 2430 | 0, /* tp_traverse */ |
| 2431 | 0, /* tp_clear */ |
| 2432 | delta_richcompare, /* tp_richcompare */ |
| 2433 | 0, /* tp_weaklistoffset */ |
| 2434 | 0, /* tp_iter */ |
| 2435 | 0, /* tp_iternext */ |
| 2436 | delta_methods, /* tp_methods */ |
| 2437 | delta_members, /* tp_members */ |
| 2438 | 0, /* tp_getset */ |
| 2439 | 0, /* tp_base */ |
| 2440 | 0, /* tp_dict */ |
| 2441 | 0, /* tp_descr_get */ |
| 2442 | 0, /* tp_descr_set */ |
| 2443 | 0, /* tp_dictoffset */ |
| 2444 | 0, /* tp_init */ |
| 2445 | 0, /* tp_alloc */ |
| 2446 | delta_new, /* tp_new */ |
| 2447 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2448 | }; |
| 2449 | |
| 2450 | /* |
| 2451 | * PyDateTime_Date implementation. |
| 2452 | */ |
| 2453 | |
| 2454 | /* Accessor properties. */ |
| 2455 | |
| 2456 | static PyObject * |
| 2457 | date_year(PyDateTime_Date *self, void *unused) |
| 2458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | static PyObject * |
| 2463 | date_month(PyDateTime_Date *self, void *unused) |
| 2464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | static PyObject * |
| 2469 | date_day(PyDateTime_Date *self, void *unused) |
| 2470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | {"year", (getter)date_year}, |
| 2476 | {"month", (getter)date_month}, |
| 2477 | {"day", (getter)date_day}, |
| 2478 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2479 | }; |
| 2480 | |
| 2481 | /* Constructors. */ |
| 2482 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2483 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2484 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2485 | static PyObject * |
| 2486 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | PyObject *self = NULL; |
| 2489 | PyObject *state; |
| 2490 | int year; |
| 2491 | int month; |
| 2492 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2494 | /* Check for invocation from pickle with __getstate__ state */ |
| 2495 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2496 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2497 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2498 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2499 | { |
| 2500 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2503 | if (me != NULL) { |
| 2504 | char *pdata = PyBytes_AS_STRING(state); |
| 2505 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2506 | me->hashcode = -1; |
| 2507 | } |
| 2508 | return (PyObject *)me; |
| 2509 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2512 | &year, &month, &day)) { |
| 2513 | if (check_date_args(year, month, day) < 0) |
| 2514 | return NULL; |
| 2515 | self = new_date_ex(year, month, day, type); |
| 2516 | } |
| 2517 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | /* Return new date from localtime(t). */ |
| 2521 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2522 | date_local_from_object(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2523 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2524 | struct tm tm; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2525 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2526 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2527 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2529 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 2530 | if (_PyTime_localtime(t, &tm) != 0) |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2531 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2532 | |
| 2533 | return PyObject_CallFunction(cls, "iii", |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 2534 | tm.tm_year + 1900, |
| 2535 | tm.tm_mon + 1, |
| 2536 | tm.tm_mday); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | /* Return new date from current time. |
| 2540 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2541 | * only way to be sure of that is to *call* time.time(). That's not |
| 2542 | * generally the same as calling C's time. |
| 2543 | */ |
| 2544 | static PyObject * |
| 2545 | date_today(PyObject *cls, PyObject *dummy) |
| 2546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | PyObject *time; |
| 2548 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2549 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | time = time_time(); |
| 2552 | if (time == NULL) |
| 2553 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | /* Note well: today() is a class method, so this may not call |
| 2556 | * date.fromtimestamp. For example, it may call |
| 2557 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2558 | * time.time() delivers; if someone were gonzo about optimization, |
| 2559 | * date.today() could get away with plain C time(). |
| 2560 | */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2561 | result = _PyObject_CallMethodId(cls, &PyId_fromtimestamp, "O", time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2562 | Py_DECREF(time); |
| 2563 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2567 | static PyObject * |
| 2568 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2569 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2570 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2571 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2572 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2573 | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
| 2574 | result = date_local_from_object(cls, timestamp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2579 | * the ordinal is out of range. |
| 2580 | */ |
| 2581 | static PyObject * |
| 2582 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 | PyObject *result = NULL; |
| 2585 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2587 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2588 | int year; |
| 2589 | int month; |
| 2590 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | if (ordinal < 1) |
| 2593 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2594 | ">= 1"); |
| 2595 | else { |
| 2596 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2597 | result = PyObject_CallFunction(cls, "iii", |
| 2598 | year, month, day); |
| 2599 | } |
| 2600 | } |
| 2601 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | /* |
| 2605 | * Date arithmetic. |
| 2606 | */ |
| 2607 | |
| 2608 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2609 | * instead. |
| 2610 | */ |
| 2611 | static PyObject * |
| 2612 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | PyObject *result = NULL; |
| 2615 | int year = GET_YEAR(date); |
| 2616 | int month = GET_MONTH(date); |
| 2617 | int deltadays = GET_TD_DAYS(delta); |
| 2618 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2619 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | if (normalize_date(&year, &month, &day) >= 0) |
| 2622 | result = new_date(year, month, day); |
| 2623 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | static PyObject * |
| 2627 | date_add(PyObject *left, PyObject *right) |
| 2628 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2629 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2630 | Py_RETURN_NOTIMPLEMENTED; |
| 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | if (PyDate_Check(left)) { |
| 2633 | /* date + ??? */ |
| 2634 | if (PyDelta_Check(right)) |
| 2635 | /* date + delta */ |
| 2636 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2637 | (PyDateTime_Delta *) right, |
| 2638 | 0); |
| 2639 | } |
| 2640 | else { |
| 2641 | /* ??? + date |
| 2642 | * 'right' must be one of us, or we wouldn't have been called |
| 2643 | */ |
| 2644 | if (PyDelta_Check(left)) |
| 2645 | /* delta + date */ |
| 2646 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2647 | (PyDateTime_Delta *) left, |
| 2648 | 0); |
| 2649 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2650 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | static PyObject * |
| 2654 | date_subtract(PyObject *left, PyObject *right) |
| 2655 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2656 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2657 | Py_RETURN_NOTIMPLEMENTED; |
| 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | if (PyDate_Check(left)) { |
| 2660 | if (PyDate_Check(right)) { |
| 2661 | /* date - date */ |
| 2662 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2663 | GET_MONTH(left), |
| 2664 | GET_DAY(left)); |
| 2665 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2666 | GET_MONTH(right), |
| 2667 | GET_DAY(right)); |
| 2668 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2669 | } |
| 2670 | if (PyDelta_Check(right)) { |
| 2671 | /* date - delta */ |
| 2672 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2673 | (PyDateTime_Delta *) right, |
| 2674 | 1); |
| 2675 | } |
| 2676 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2677 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | |
| 2681 | /* Various ways to turn a date into a string. */ |
| 2682 | |
| 2683 | static PyObject * |
| 2684 | date_repr(PyDateTime_Date *self) |
| 2685 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2687 | Py_TYPE(self)->tp_name, |
| 2688 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | static PyObject * |
| 2692 | date_isoformat(PyDateTime_Date *self) |
| 2693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2695 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2698 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2699 | static PyObject * |
| 2700 | date_str(PyDateTime_Date *self) |
| 2701 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 2702 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | static PyObject * |
| 2707 | date_ctime(PyDateTime_Date *self) |
| 2708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | static PyObject * |
| 2713 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | /* This method can be inherited, and needs to call the |
| 2716 | * timetuple() method appropriate to self's class. |
| 2717 | */ |
| 2718 | PyObject *result; |
| 2719 | PyObject *tuple; |
| 2720 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2721 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2722 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 2725 | &format)) |
| 2726 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2727 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 2728 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | if (tuple == NULL) |
| 2730 | return NULL; |
| 2731 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2732 | (PyObject *)self); |
| 2733 | Py_DECREF(tuple); |
| 2734 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2737 | static PyObject * |
| 2738 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2740 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2743 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2746 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2747 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2748 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2749 | return _PyObject_CallMethodId((PyObject *)self, &PyId_strftime, "O", format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2752 | /* ISO methods. */ |
| 2753 | |
| 2754 | static PyObject * |
| 2755 | date_isoweekday(PyDateTime_Date *self) |
| 2756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2759 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | static PyObject * |
| 2763 | date_isocalendar(PyDateTime_Date *self) |
| 2764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | int year = GET_YEAR(self); |
| 2766 | int week1_monday = iso_week1_monday(year); |
| 2767 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2768 | int week; |
| 2769 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | week = divmod(today - week1_monday, 7, &day); |
| 2772 | if (week < 0) { |
| 2773 | --year; |
| 2774 | week1_monday = iso_week1_monday(year); |
| 2775 | week = divmod(today - week1_monday, 7, &day); |
| 2776 | } |
| 2777 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2778 | ++year; |
| 2779 | week = 0; |
| 2780 | } |
| 2781 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | /* Miscellaneous methods. */ |
| 2785 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2786 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2787 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | if (PyDate_Check(other)) { |
| 2790 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2791 | ((PyDateTime_Date *)other)->data, |
| 2792 | _PyDateTime_DATE_DATASIZE); |
| 2793 | return diff_to_bool(diff, op); |
| 2794 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2795 | else |
| 2796 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | static PyObject * |
| 2800 | date_timetuple(PyDateTime_Date *self) |
| 2801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2802 | return build_struct_time(GET_YEAR(self), |
| 2803 | GET_MONTH(self), |
| 2804 | GET_DAY(self), |
| 2805 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2808 | static PyObject * |
| 2809 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | PyObject *clone; |
| 2812 | PyObject *tuple; |
| 2813 | int year = GET_YEAR(self); |
| 2814 | int month = GET_MONTH(self); |
| 2815 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2817 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2818 | &year, &month, &day)) |
| 2819 | return NULL; |
| 2820 | tuple = Py_BuildValue("iii", year, month, day); |
| 2821 | if (tuple == NULL) |
| 2822 | return NULL; |
| 2823 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 2824 | Py_DECREF(tuple); |
| 2825 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2828 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2829 | generic_hash(unsigned char *data, int len) |
| 2830 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 2831 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2836 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2837 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2838 | date_hash(PyDateTime_Date *self) |
| 2839 | { |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 2840 | if (self->hashcode == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | self->hashcode = generic_hash( |
| 2842 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Benjamin Peterson | dec2df3 | 2016-09-09 17:46:24 -0700 | [diff] [blame] | 2843 | } |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | static PyObject * |
| 2849 | date_toordinal(PyDateTime_Date *self) |
| 2850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 2852 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | static PyObject * |
| 2856 | date_weekday(PyDateTime_Date *self) |
| 2857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2863 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2864 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2865 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2866 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2867 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | PyObject* field; |
| 2870 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 2871 | _PyDateTime_DATE_DATASIZE); |
| 2872 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2876 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2886 | METH_CLASS, |
| 2887 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2888 | "time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2891 | METH_CLASS, |
| 2892 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2893 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2896 | PyDoc_STR("Current date or datetime: same as " |
| 2897 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2902 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 2905 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2908 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2910 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2911 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2914 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2915 | "weekday.")}, |
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 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2918 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2920 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2921 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2922 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2924 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2925 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2926 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2929 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2930 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 2933 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2936 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2939 | }; |
| 2940 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2941 | static const char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2942 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2943 | |
| 2944 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | date_add, /* nb_add */ |
| 2946 | date_subtract, /* nb_subtract */ |
| 2947 | 0, /* nb_multiply */ |
| 2948 | 0, /* nb_remainder */ |
| 2949 | 0, /* nb_divmod */ |
| 2950 | 0, /* nb_power */ |
| 2951 | 0, /* nb_negative */ |
| 2952 | 0, /* nb_positive */ |
| 2953 | 0, /* nb_absolute */ |
| 2954 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2955 | }; |
| 2956 | |
| 2957 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2959 | "datetime.date", /* tp_name */ |
| 2960 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2961 | 0, /* tp_itemsize */ |
| 2962 | 0, /* tp_dealloc */ |
| 2963 | 0, /* tp_print */ |
| 2964 | 0, /* tp_getattr */ |
| 2965 | 0, /* tp_setattr */ |
| 2966 | 0, /* tp_reserved */ |
| 2967 | (reprfunc)date_repr, /* tp_repr */ |
| 2968 | &date_as_number, /* tp_as_number */ |
| 2969 | 0, /* tp_as_sequence */ |
| 2970 | 0, /* tp_as_mapping */ |
| 2971 | (hashfunc)date_hash, /* tp_hash */ |
| 2972 | 0, /* tp_call */ |
| 2973 | (reprfunc)date_str, /* tp_str */ |
| 2974 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2975 | 0, /* tp_setattro */ |
| 2976 | 0, /* tp_as_buffer */ |
| 2977 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2978 | date_doc, /* tp_doc */ |
| 2979 | 0, /* tp_traverse */ |
| 2980 | 0, /* tp_clear */ |
| 2981 | date_richcompare, /* tp_richcompare */ |
| 2982 | 0, /* tp_weaklistoffset */ |
| 2983 | 0, /* tp_iter */ |
| 2984 | 0, /* tp_iternext */ |
| 2985 | date_methods, /* tp_methods */ |
| 2986 | 0, /* tp_members */ |
| 2987 | date_getset, /* tp_getset */ |
| 2988 | 0, /* tp_base */ |
| 2989 | 0, /* tp_dict */ |
| 2990 | 0, /* tp_descr_get */ |
| 2991 | 0, /* tp_descr_set */ |
| 2992 | 0, /* tp_dictoffset */ |
| 2993 | 0, /* tp_init */ |
| 2994 | 0, /* tp_alloc */ |
| 2995 | date_new, /* tp_new */ |
| 2996 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2997 | }; |
| 2998 | |
| 2999 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3000 | * PyDateTime_TZInfo implementation. |
| 3001 | */ |
| 3002 | |
| 3003 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 3004 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 3005 | * 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] | 3006 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3007 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 3008 | * |
| 3009 | * Note: For reasons having to do with pickling of subclasses, we have |
| 3010 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 3011 | * in the Python implementation (__init__() could raise NotImplementedError |
| 3012 | * there without ill effect), but doing so in the C implementation hit a |
| 3013 | * brick wall. |
| 3014 | */ |
| 3015 | |
| 3016 | static PyObject * |
| 3017 | tzinfo_nogo(const char* methodname) |
| 3018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | PyErr_Format(PyExc_NotImplementedError, |
| 3020 | "a tzinfo subclass must implement %s()", |
| 3021 | methodname); |
| 3022 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | /* Methods. A subclass must implement these. */ |
| 3026 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3027 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3028 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 3029 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3030 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3033 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3034 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 3035 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3039 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3040 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 3041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3045 | |
| 3046 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 3047 | PyDateTime_Delta *delta, |
| 3048 | int factor); |
| 3049 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 3050 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 3051 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3052 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3053 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3054 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3055 | PyObject *result = NULL; |
| 3056 | PyObject *off = NULL, *dst = NULL; |
| 3057 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3058 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3059 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | PyErr_SetString(PyExc_TypeError, |
| 3061 | "fromutc: argument must be a datetime"); |
| 3062 | return NULL; |
| 3063 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3064 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3066 | "is not self"); |
| 3067 | return NULL; |
| 3068 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3069 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3070 | off = datetime_utcoffset(dt, NULL); |
| 3071 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3073 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3075 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3076 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3078 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3079 | dst = datetime_dst(dt, NULL); |
| 3080 | if (dst == NULL) |
| 3081 | goto Fail; |
| 3082 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3084 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3085 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3087 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3088 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3089 | if (delta == NULL) |
| 3090 | goto Fail; |
| 3091 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3094 | |
| 3095 | Py_DECREF(dst); |
| 3096 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3097 | if (dst == NULL) |
| 3098 | goto Fail; |
| 3099 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | goto Inconsistent; |
Alexander Belopolsky | c79447b | 2015-09-27 21:41:55 -0400 | [diff] [blame] | 3101 | if (delta_bool((PyDateTime_Delta *)dst) != 0) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3102 | Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 3103 | (PyDateTime_Delta *)dst, 1)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3104 | if (result == NULL) |
| 3105 | goto Fail; |
| 3106 | } |
| 3107 | Py_DECREF(delta); |
| 3108 | Py_DECREF(dst); |
| 3109 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3111 | |
| 3112 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3114 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3116 | /* fall thru to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3117 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3118 | Py_XDECREF(off); |
| 3119 | Py_XDECREF(dst); |
| 3120 | Py_XDECREF(delta); |
| 3121 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3125 | /* |
| 3126 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3127 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3128 | */ |
| 3129 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3130 | static PyObject * |
| 3131 | tzinfo_reduce(PyObject *self) |
| 3132 | { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3133 | PyObject *args, *state; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3135 | _Py_IDENTIFIER(__getinitargs__); |
| 3136 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3137 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3138 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | if (getinitargs != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3140 | args = _PyObject_CallNoArg(getinitargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | Py_DECREF(getinitargs); |
| 3142 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | return NULL; |
| 3144 | } |
| 3145 | } |
| 3146 | else { |
| 3147 | PyErr_Clear(); |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3148 | |
| 3149 | args = PyTuple_New(0); |
| 3150 | if (args == NULL) { |
| 3151 | return NULL; |
| 3152 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3154 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3155 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | if (getstate != NULL) { |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3157 | state = _PyObject_CallNoArg(getstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | Py_DECREF(getstate); |
| 3159 | if (state == NULL) { |
| 3160 | Py_DECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | return NULL; |
| 3162 | } |
| 3163 | } |
| 3164 | else { |
| 3165 | PyObject **dictptr; |
| 3166 | PyErr_Clear(); |
| 3167 | state = Py_None; |
| 3168 | dictptr = _PyObject_GetDictPtr(self); |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3169 | if (dictptr && *dictptr && PyDict_Size(*dictptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | state = *dictptr; |
Victor Stinner | d1584d3 | 2016-08-23 00:11:04 +0200 | [diff] [blame] | 3171 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | Py_INCREF(state); |
| 3173 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | if (state == Py_None) { |
| 3176 | Py_DECREF(state); |
| 3177 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3178 | } |
| 3179 | else |
| 3180 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3181 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3182 | |
| 3183 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3186 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3189 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3190 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 3193 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3196 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 3199 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3201 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3202 | }; |
| 3203 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3204 | static const char tzinfo_doc[] = |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3205 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3206 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3207 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3209 | "datetime.tzinfo", /* tp_name */ |
| 3210 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3211 | 0, /* tp_itemsize */ |
| 3212 | 0, /* tp_dealloc */ |
| 3213 | 0, /* tp_print */ |
| 3214 | 0, /* tp_getattr */ |
| 3215 | 0, /* tp_setattr */ |
| 3216 | 0, /* tp_reserved */ |
| 3217 | 0, /* tp_repr */ |
| 3218 | 0, /* tp_as_number */ |
| 3219 | 0, /* tp_as_sequence */ |
| 3220 | 0, /* tp_as_mapping */ |
| 3221 | 0, /* tp_hash */ |
| 3222 | 0, /* tp_call */ |
| 3223 | 0, /* tp_str */ |
| 3224 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3225 | 0, /* tp_setattro */ |
| 3226 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3227 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3228 | tzinfo_doc, /* tp_doc */ |
| 3229 | 0, /* tp_traverse */ |
| 3230 | 0, /* tp_clear */ |
| 3231 | 0, /* tp_richcompare */ |
| 3232 | 0, /* tp_weaklistoffset */ |
| 3233 | 0, /* tp_iter */ |
| 3234 | 0, /* tp_iternext */ |
| 3235 | tzinfo_methods, /* tp_methods */ |
| 3236 | 0, /* tp_members */ |
| 3237 | 0, /* tp_getset */ |
| 3238 | 0, /* tp_base */ |
| 3239 | 0, /* tp_dict */ |
| 3240 | 0, /* tp_descr_get */ |
| 3241 | 0, /* tp_descr_set */ |
| 3242 | 0, /* tp_dictoffset */ |
| 3243 | 0, /* tp_init */ |
| 3244 | 0, /* tp_alloc */ |
| 3245 | PyType_GenericNew, /* tp_new */ |
| 3246 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3247 | }; |
| 3248 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3249 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3250 | |
| 3251 | static PyObject * |
| 3252 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3253 | { |
| 3254 | PyObject *offset; |
| 3255 | PyObject *name = NULL; |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 3256 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|U:timezone", timezone_kws, |
| 3257 | &PyDateTime_DeltaType, &offset, &name)) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3258 | return new_timezone(offset, name); |
| 3259 | |
| 3260 | return NULL; |
| 3261 | } |
| 3262 | |
| 3263 | static void |
| 3264 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3265 | { |
| 3266 | Py_CLEAR(self->offset); |
| 3267 | Py_CLEAR(self->name); |
| 3268 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3269 | } |
| 3270 | |
| 3271 | static PyObject * |
| 3272 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3273 | PyDateTime_TimeZone *other, int op) |
| 3274 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3275 | if (op != Py_EQ && op != Py_NE) |
| 3276 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3277 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 3278 | if (op == Py_EQ) |
| 3279 | Py_RETURN_FALSE; |
| 3280 | else |
| 3281 | Py_RETURN_TRUE; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3282 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3283 | return delta_richcompare(self->offset, other->offset, op); |
| 3284 | } |
| 3285 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3286 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3287 | timezone_hash(PyDateTime_TimeZone *self) |
| 3288 | { |
| 3289 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3290 | } |
| 3291 | |
| 3292 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3293 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3294 | otherwise. |
| 3295 | */ |
| 3296 | static int |
| 3297 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3298 | { |
| 3299 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3300 | return 0; |
| 3301 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3302 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3303 | return -1; |
| 3304 | } |
| 3305 | |
| 3306 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3307 | timezone_repr(PyDateTime_TimeZone *self) |
| 3308 | { |
| 3309 | /* Note that although timezone is not subclassable, it is convenient |
| 3310 | to use Py_TYPE(self)->tp_name here. */ |
| 3311 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3312 | |
| 3313 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3314 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3315 | |
| 3316 | if (self->name == NULL) |
| 3317 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3318 | |
| 3319 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3320 | self->name); |
| 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3325 | timezone_str(PyDateTime_TimeZone *self) |
| 3326 | { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3327 | int hours, minutes, seconds; |
| 3328 | PyObject *offset; |
| 3329 | char sign; |
| 3330 | |
| 3331 | if (self->name != NULL) { |
| 3332 | Py_INCREF(self->name); |
| 3333 | return self->name; |
| 3334 | } |
Victor Stinner | 90fd895 | 2015-09-08 00:12:49 +0200 | [diff] [blame] | 3335 | if ((PyObject *)self == PyDateTime_TimeZone_UTC || |
Alexander Belopolsky | 7827a5b | 2015-09-06 13:07:21 -0400 | [diff] [blame] | 3336 | (GET_TD_DAYS(self->offset) == 0 && |
| 3337 | GET_TD_SECONDS(self->offset) == 0 && |
| 3338 | GET_TD_MICROSECONDS(self->offset) == 0)) |
| 3339 | return PyUnicode_FromString("UTC"); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3340 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3341 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3342 | sign = '-'; |
| 3343 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3344 | if (offset == NULL) |
| 3345 | return NULL; |
| 3346 | } |
| 3347 | else { |
| 3348 | sign = '+'; |
| 3349 | offset = self->offset; |
| 3350 | Py_INCREF(offset); |
| 3351 | } |
| 3352 | /* Offset is not negative here. */ |
| 3353 | seconds = GET_TD_SECONDS(offset); |
| 3354 | Py_DECREF(offset); |
| 3355 | minutes = divmod(seconds, 60, &seconds); |
| 3356 | hours = divmod(minutes, 60, &minutes); |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 3357 | /* XXX ignore sub-minute data, currently not allowed. */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3358 | assert(seconds == 0); |
| 3359 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | static PyObject * |
| 3363 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3364 | { |
| 3365 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3366 | return NULL; |
| 3367 | |
| 3368 | return timezone_str(self); |
| 3369 | } |
| 3370 | |
| 3371 | static PyObject * |
| 3372 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3373 | { |
| 3374 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3375 | return NULL; |
| 3376 | |
| 3377 | Py_INCREF(self->offset); |
| 3378 | return self->offset; |
| 3379 | } |
| 3380 | |
| 3381 | static PyObject * |
| 3382 | timezone_dst(PyObject *self, PyObject *dt) |
| 3383 | { |
| 3384 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3385 | return NULL; |
| 3386 | |
| 3387 | Py_RETURN_NONE; |
| 3388 | } |
| 3389 | |
| 3390 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3391 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3392 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3393 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3394 | PyErr_SetString(PyExc_TypeError, |
| 3395 | "fromutc: argument must be a datetime"); |
| 3396 | return NULL; |
| 3397 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3398 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3399 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3400 | "is not self"); |
| 3401 | return NULL; |
| 3402 | } |
| 3403 | |
| 3404 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3405 | } |
| 3406 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3407 | static PyObject * |
| 3408 | timezone_getinitargs(PyDateTime_TimeZone *self) |
| 3409 | { |
| 3410 | if (self->name == NULL) |
| 3411 | return Py_BuildValue("(O)", self->offset); |
| 3412 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3413 | } |
| 3414 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3415 | static PyMethodDef timezone_methods[] = { |
| 3416 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3417 | 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] | 3418 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3419 | |
| 3420 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3421 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3422 | |
| 3423 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3424 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3425 | |
| 3426 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3427 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3428 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3429 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3430 | PyDoc_STR("pickle support")}, |
| 3431 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3432 | {NULL, NULL} |
| 3433 | }; |
| 3434 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 3435 | static const char timezone_doc[] = |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3436 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3437 | |
| 3438 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3439 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3440 | "datetime.timezone", /* tp_name */ |
| 3441 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3442 | 0, /* tp_itemsize */ |
| 3443 | (destructor)timezone_dealloc, /* tp_dealloc */ |
| 3444 | 0, /* tp_print */ |
| 3445 | 0, /* tp_getattr */ |
| 3446 | 0, /* tp_setattr */ |
| 3447 | 0, /* tp_reserved */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3448 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3449 | 0, /* tp_as_number */ |
| 3450 | 0, /* tp_as_sequence */ |
| 3451 | 0, /* tp_as_mapping */ |
| 3452 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3453 | 0, /* tp_call */ |
| 3454 | (reprfunc)timezone_str, /* tp_str */ |
| 3455 | 0, /* tp_getattro */ |
| 3456 | 0, /* tp_setattro */ |
| 3457 | 0, /* tp_as_buffer */ |
| 3458 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3459 | timezone_doc, /* tp_doc */ |
| 3460 | 0, /* tp_traverse */ |
| 3461 | 0, /* tp_clear */ |
| 3462 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3463 | 0, /* tp_weaklistoffset */ |
| 3464 | 0, /* tp_iter */ |
| 3465 | 0, /* tp_iternext */ |
| 3466 | timezone_methods, /* tp_methods */ |
| 3467 | 0, /* tp_members */ |
| 3468 | 0, /* tp_getset */ |
| 3469 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3470 | 0, /* tp_dict */ |
| 3471 | 0, /* tp_descr_get */ |
| 3472 | 0, /* tp_descr_set */ |
| 3473 | 0, /* tp_dictoffset */ |
| 3474 | 0, /* tp_init */ |
| 3475 | 0, /* tp_alloc */ |
| 3476 | timezone_new, /* tp_new */ |
| 3477 | }; |
| 3478 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3479 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3480 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3481 | */ |
| 3482 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3483 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3484 | */ |
| 3485 | |
| 3486 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3487 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3492 | static PyObject * |
| 3493 | time_minute(PyDateTime_Time *self, void *unused) |
| 3494 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | /* The name time_second conflicted with some platform header file. */ |
| 3499 | static PyObject * |
| 3500 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3502 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | static PyObject * |
| 3506 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3507 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | static PyObject * |
| 3512 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3515 | Py_INCREF(result); |
| 3516 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3519 | static PyObject * |
| 3520 | time_fold(PyDateTime_Time *self, void *unused) |
| 3521 | { |
| 3522 | return PyLong_FromLong(TIME_GET_FOLD(self)); |
| 3523 | } |
| 3524 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3525 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3526 | {"hour", (getter)time_hour}, |
| 3527 | {"minute", (getter)time_minute}, |
| 3528 | {"second", (getter)py_time_second}, |
| 3529 | {"microsecond", (getter)time_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3530 | {"tzinfo", (getter)time_tzinfo}, |
| 3531 | {"fold", (getter)time_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3532 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3533 | }; |
| 3534 | |
| 3535 | /* |
| 3536 | * Constructors. |
| 3537 | */ |
| 3538 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3539 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3540 | "tzinfo", "fold", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3541 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3542 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3543 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3545 | PyObject *self = NULL; |
| 3546 | PyObject *state; |
| 3547 | int hour = 0; |
| 3548 | int minute = 0; |
| 3549 | int second = 0; |
| 3550 | int usecond = 0; |
| 3551 | PyObject *tzinfo = Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3552 | int fold = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | /* Check for invocation from pickle with __getstate__ state */ |
| 3555 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3556 | PyTuple_GET_SIZE(args) <= 2 && |
| 3557 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3558 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3559 | (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3560 | { |
| 3561 | PyDateTime_Time *me; |
| 3562 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3565 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3566 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3567 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3568 | "tzinfo state arg"); |
| 3569 | return NULL; |
| 3570 | } |
| 3571 | } |
| 3572 | aware = (char)(tzinfo != Py_None); |
| 3573 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3574 | if (me != NULL) { |
| 3575 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3577 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3578 | me->hashcode = -1; |
| 3579 | me->hastzinfo = aware; |
| 3580 | if (aware) { |
| 3581 | Py_INCREF(tzinfo); |
| 3582 | me->tzinfo = tzinfo; |
| 3583 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3584 | if (pdata[0] & (1 << 7)) { |
| 3585 | me->data[0] -= 128; |
| 3586 | me->fold = 1; |
| 3587 | } |
| 3588 | else { |
| 3589 | me->fold = 0; |
| 3590 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3591 | } |
| 3592 | return (PyObject *)me; |
| 3593 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3594 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3595 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3596 | &hour, &minute, &second, &usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3597 | &tzinfo, &fold)) { |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3598 | if (check_time_args(hour, minute, second, usecond, fold) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | return NULL; |
| 3600 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3601 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3602 | self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, |
| 3603 | type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3604 | } |
| 3605 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | /* |
| 3609 | * Destructor. |
| 3610 | */ |
| 3611 | |
| 3612 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3613 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3614 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3615 | if (HASTZINFO(self)) { |
| 3616 | Py_XDECREF(self->tzinfo); |
| 3617 | } |
| 3618 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3622 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3623 | */ |
| 3624 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3625 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3626 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3627 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 3628 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3632 | time_dst(PyObject *self, PyObject *unused) { |
| 3633 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3637 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3638 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3642 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3643 | */ |
| 3644 | |
| 3645 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3646 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3649 | int h = TIME_GET_HOUR(self); |
| 3650 | int m = TIME_GET_MINUTE(self); |
| 3651 | int s = TIME_GET_SECOND(self); |
| 3652 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3653 | int fold = TIME_GET_FOLD(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3656 | if (us) |
| 3657 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3658 | type_name, h, m, s, us); |
| 3659 | else if (s) |
| 3660 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3661 | type_name, h, m, s); |
| 3662 | else |
| 3663 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3664 | if (result != NULL && HASTZINFO(self)) |
| 3665 | result = append_keyword_tzinfo(result, self->tzinfo); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3666 | if (result != NULL && fold) |
| 3667 | result = append_keyword_fold(result, fold); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3671 | static PyObject * |
| 3672 | time_str(PyDateTime_Time *self) |
| 3673 | { |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3674 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, NULL); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3675 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3676 | |
| 3677 | static PyObject * |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3678 | time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3680 | char buf[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3681 | char *timespec = NULL; |
| 3682 | static char *keywords[] = {"timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3683 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3684 | int us = TIME_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3685 | static char *specs[][2] = { |
| 3686 | {"hours", "%02d"}, |
| 3687 | {"minutes", "%02d:%02d"}, |
| 3688 | {"seconds", "%02d:%02d:%02d"}, |
| 3689 | {"milliseconds", "%02d:%02d:%02d.%03d"}, |
| 3690 | {"microseconds", "%02d:%02d:%02d.%06d"}, |
| 3691 | }; |
| 3692 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3693 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3694 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, ×pec)) |
| 3695 | return NULL; |
| 3696 | |
| 3697 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 3698 | if (us == 0) { |
| 3699 | /* seconds */ |
| 3700 | given_spec = 2; |
| 3701 | } |
| 3702 | else { |
| 3703 | /* microseconds */ |
| 3704 | given_spec = 4; |
| 3705 | } |
| 3706 | } |
| 3707 | else { |
| 3708 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 3709 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 3710 | if (given_spec == 3) { |
| 3711 | /* milliseconds */ |
| 3712 | us = us / 1000; |
| 3713 | } |
| 3714 | break; |
| 3715 | } |
| 3716 | } |
| 3717 | } |
| 3718 | |
| 3719 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 3720 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 3721 | return NULL; |
| 3722 | } |
| 3723 | else { |
| 3724 | result = PyUnicode_FromFormat(specs[given_spec][1], |
| 3725 | TIME_GET_HOUR(self), TIME_GET_MINUTE(self), |
| 3726 | TIME_GET_SECOND(self), us); |
| 3727 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3728 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3729 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3730 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3732 | /* We need to append the UTC offset. */ |
| 3733 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 3734 | Py_None) < 0) { |
| 3735 | Py_DECREF(result); |
| 3736 | return NULL; |
| 3737 | } |
| 3738 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 3739 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3742 | static PyObject * |
| 3743 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 | PyObject *result; |
| 3746 | PyObject *tuple; |
| 3747 | PyObject *format; |
| 3748 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3751 | &format)) |
| 3752 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | /* Python's strftime does insane things with the year part of the |
| 3755 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 3756 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3757 | */ |
| 3758 | tuple = Py_BuildValue("iiiiiiiii", |
| 3759 | 1900, 1, 1, /* year, month, day */ |
| 3760 | TIME_GET_HOUR(self), |
| 3761 | TIME_GET_MINUTE(self), |
| 3762 | TIME_GET_SECOND(self), |
| 3763 | 0, 1, -1); /* weekday, daynum, dst */ |
| 3764 | if (tuple == NULL) |
| 3765 | return NULL; |
| 3766 | assert(PyTuple_Size(tuple) == 9); |
| 3767 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3768 | Py_None); |
| 3769 | Py_DECREF(tuple); |
| 3770 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3771 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3772 | |
| 3773 | /* |
| 3774 | * Miscellaneous methods. |
| 3775 | */ |
| 3776 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3777 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3778 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3779 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3780 | PyObject *result = NULL; |
| 3781 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3783 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3784 | if (! PyTime_Check(other)) |
| 3785 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3786 | |
| 3787 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3788 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3789 | ((PyDateTime_Time *)other)->data, |
| 3790 | _PyDateTime_TIME_DATASIZE); |
| 3791 | return diff_to_bool(diff, op); |
| 3792 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3793 | offset1 = time_utcoffset(self, NULL); |
| 3794 | if (offset1 == NULL) |
| 3795 | return NULL; |
| 3796 | offset2 = time_utcoffset(other, NULL); |
| 3797 | if (offset2 == NULL) |
| 3798 | goto done; |
| 3799 | /* If they're both naive, or both aware and have the same offsets, |
| 3800 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3801 | * offset2 == Py_None at this point. |
| 3802 | */ |
| 3803 | if ((offset1 == offset2) || |
| 3804 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 3805 | delta_cmp(offset1, offset2) == 0)) { |
| 3806 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3807 | ((PyDateTime_Time *)other)->data, |
| 3808 | _PyDateTime_TIME_DATASIZE); |
| 3809 | result = diff_to_bool(diff, op); |
| 3810 | } |
| 3811 | /* The hard case: both aware with different UTC offsets */ |
| 3812 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 3813 | int offsecs1, offsecs2; |
| 3814 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3815 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 3816 | TIME_GET_MINUTE(self) * 60 + |
| 3817 | TIME_GET_SECOND(self) - |
| 3818 | GET_TD_DAYS(offset1) * 86400 - |
| 3819 | GET_TD_SECONDS(offset1); |
| 3820 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 3821 | TIME_GET_MINUTE(other) * 60 + |
| 3822 | TIME_GET_SECOND(other) - |
| 3823 | GET_TD_DAYS(offset2) * 86400 - |
| 3824 | GET_TD_SECONDS(offset2); |
| 3825 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3826 | if (diff == 0) |
| 3827 | diff = TIME_GET_MICROSECOND(self) - |
| 3828 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3829 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3830 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 3831 | else if (op == Py_EQ) { |
| 3832 | result = Py_False; |
| 3833 | Py_INCREF(result); |
| 3834 | } |
| 3835 | else if (op == Py_NE) { |
| 3836 | result = Py_True; |
| 3837 | Py_INCREF(result); |
| 3838 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3839 | else { |
| 3840 | PyErr_SetString(PyExc_TypeError, |
| 3841 | "can't compare offset-naive and " |
| 3842 | "offset-aware times"); |
| 3843 | } |
| 3844 | done: |
| 3845 | Py_DECREF(offset1); |
| 3846 | Py_XDECREF(offset2); |
| 3847 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3850 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3851 | time_hash(PyDateTime_Time *self) |
| 3852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3854 | PyObject *offset, *self0; |
| 3855 | if (DATE_GET_FOLD(self)) { |
| 3856 | self0 = new_time_ex2(DATE_GET_HOUR(self), |
| 3857 | DATE_GET_MINUTE(self), |
| 3858 | DATE_GET_SECOND(self), |
| 3859 | DATE_GET_MICROSECOND(self), |
| 3860 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3861 | 0, Py_TYPE(self)); |
| 3862 | if (self0 == NULL) |
| 3863 | return -1; |
| 3864 | } |
| 3865 | else { |
| 3866 | self0 = (PyObject *)self; |
| 3867 | Py_INCREF(self0); |
| 3868 | } |
| 3869 | offset = time_utcoffset(self0, NULL); |
| 3870 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3871 | |
| 3872 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3876 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | self->hashcode = generic_hash( |
| 3878 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3879 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3880 | PyObject *temp1, *temp2; |
| 3881 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3883 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 3884 | TIME_GET_MINUTE(self) * 60 + |
| 3885 | TIME_GET_SECOND(self); |
| 3886 | microseconds = TIME_GET_MICROSECOND(self); |
| 3887 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 3888 | if (temp1 == NULL) { |
| 3889 | Py_DECREF(offset); |
| 3890 | return -1; |
| 3891 | } |
| 3892 | temp2 = delta_subtract(temp1, offset); |
| 3893 | Py_DECREF(temp1); |
| 3894 | if (temp2 == NULL) { |
| 3895 | Py_DECREF(offset); |
| 3896 | return -1; |
| 3897 | } |
| 3898 | self->hashcode = PyObject_Hash(temp2); |
| 3899 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3901 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3902 | } |
| 3903 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3904 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3905 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3906 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3907 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3909 | PyObject *clone; |
| 3910 | PyObject *tuple; |
| 3911 | int hh = TIME_GET_HOUR(self); |
| 3912 | int mm = TIME_GET_MINUTE(self); |
| 3913 | int ss = TIME_GET_SECOND(self); |
| 3914 | int us = TIME_GET_MICROSECOND(self); |
| 3915 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3916 | int fold = TIME_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3917 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3918 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3919 | time_kws, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3920 | &hh, &mm, &ss, &us, &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3921 | return NULL; |
| 3922 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3923 | if (tuple == NULL) |
| 3924 | return NULL; |
| 3925 | clone = time_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3926 | if (clone != NULL) { |
| 3927 | if (fold != 0 && fold != 1) { |
| 3928 | PyErr_SetString(PyExc_ValueError, |
| 3929 | "fold must be either 0 or 1"); |
| 3930 | return NULL; |
| 3931 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3932 | TIME_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 3933 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3934 | Py_DECREF(tuple); |
| 3935 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3938 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3939 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3940 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3941 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3942 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3943 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3944 | */ |
| 3945 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3946 | time_getstate(PyDateTime_Time *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3947 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3948 | PyObject *basestate; |
| 3949 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3951 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 3952 | _PyDateTime_TIME_DATASIZE); |
| 3953 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3954 | if (proto > 3 && TIME_GET_FOLD(self)) |
| 3955 | /* Set the first bit of the first byte */ |
| 3956 | PyBytes_AS_STRING(basestate)[0] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3958 | result = PyTuple_Pack(1, basestate); |
| 3959 | else |
| 3960 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 3961 | Py_DECREF(basestate); |
| 3962 | } |
| 3963 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 3967 | time_reduce_ex(PyDateTime_Time *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3968 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 3969 | int proto; |
| 3970 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 3971 | return NULL; |
| 3972 | |
| 3973 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 3976 | static PyObject * |
| 3977 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
| 3978 | { |
| 3979 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2)); |
| 3980 | } |
| 3981 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3982 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3983 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 3984 | {"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 3985 | PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]" |
| 3986 | "[+HH:MM].\n\n" |
| 3987 | "timespec specifies what components of the time to include.\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3989 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 3990 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3992 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3993 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 3996 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3998 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 3999 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4001 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 4002 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 4005 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4006 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4007 | {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4008 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4009 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 4010 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 4011 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 4012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4013 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4014 | }; |
| 4015 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4016 | static const char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4017 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 4018 | \n\ |
| 4019 | 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] | 4020 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4021 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4022 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4023 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4024 | "datetime.time", /* tp_name */ |
| 4025 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 4026 | 0, /* tp_itemsize */ |
| 4027 | (destructor)time_dealloc, /* tp_dealloc */ |
| 4028 | 0, /* tp_print */ |
| 4029 | 0, /* tp_getattr */ |
| 4030 | 0, /* tp_setattr */ |
| 4031 | 0, /* tp_reserved */ |
| 4032 | (reprfunc)time_repr, /* tp_repr */ |
Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 4033 | 0, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4034 | 0, /* tp_as_sequence */ |
| 4035 | 0, /* tp_as_mapping */ |
| 4036 | (hashfunc)time_hash, /* tp_hash */ |
| 4037 | 0, /* tp_call */ |
| 4038 | (reprfunc)time_str, /* tp_str */ |
| 4039 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4040 | 0, /* tp_setattro */ |
| 4041 | 0, /* tp_as_buffer */ |
| 4042 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4043 | time_doc, /* tp_doc */ |
| 4044 | 0, /* tp_traverse */ |
| 4045 | 0, /* tp_clear */ |
| 4046 | time_richcompare, /* tp_richcompare */ |
| 4047 | 0, /* tp_weaklistoffset */ |
| 4048 | 0, /* tp_iter */ |
| 4049 | 0, /* tp_iternext */ |
| 4050 | time_methods, /* tp_methods */ |
| 4051 | 0, /* tp_members */ |
| 4052 | time_getset, /* tp_getset */ |
| 4053 | 0, /* tp_base */ |
| 4054 | 0, /* tp_dict */ |
| 4055 | 0, /* tp_descr_get */ |
| 4056 | 0, /* tp_descr_set */ |
| 4057 | 0, /* tp_dictoffset */ |
| 4058 | 0, /* tp_init */ |
| 4059 | time_alloc, /* tp_alloc */ |
| 4060 | time_new, /* tp_new */ |
| 4061 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4062 | }; |
| 4063 | |
| 4064 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4065 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4066 | */ |
| 4067 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4068 | /* Accessor properties. Properties for day, month, and year are inherited |
| 4069 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4070 | */ |
| 4071 | |
| 4072 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4073 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4074 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4075 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4078 | static PyObject * |
| 4079 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 4080 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4081 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | static PyObject * |
| 4085 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 4086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4087 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | static PyObject * |
| 4091 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 4092 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4093 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | static PyObject * |
| 4097 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 4098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4099 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 4100 | Py_INCREF(result); |
| 4101 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4104 | static PyObject * |
| 4105 | datetime_fold(PyDateTime_DateTime *self, void *unused) |
| 4106 | { |
| 4107 | return PyLong_FromLong(DATE_GET_FOLD(self)); |
| 4108 | } |
| 4109 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4110 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4111 | {"hour", (getter)datetime_hour}, |
| 4112 | {"minute", (getter)datetime_minute}, |
| 4113 | {"second", (getter)datetime_second}, |
| 4114 | {"microsecond", (getter)datetime_microsecond}, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4115 | {"tzinfo", (getter)datetime_tzinfo}, |
| 4116 | {"fold", (getter)datetime_fold}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4118 | }; |
| 4119 | |
| 4120 | /* |
| 4121 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4122 | */ |
| 4123 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4124 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4125 | "year", "month", "day", "hour", "minute", "second", |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4126 | "microsecond", "tzinfo", "fold", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4127 | }; |
| 4128 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4129 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4130 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4132 | PyObject *self = NULL; |
| 4133 | PyObject *state; |
| 4134 | int year; |
| 4135 | int month; |
| 4136 | int day; |
| 4137 | int hour = 0; |
| 4138 | int minute = 0; |
| 4139 | int second = 0; |
| 4140 | int usecond = 0; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4141 | int fold = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4144 | /* Check for invocation from pickle with __getstate__ state */ |
| 4145 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 4146 | PyTuple_GET_SIZE(args) <= 2 && |
| 4147 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 4148 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4149 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | { |
| 4151 | PyDateTime_DateTime *me; |
| 4152 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4154 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4155 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4156 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 4157 | PyErr_SetString(PyExc_TypeError, "bad " |
| 4158 | "tzinfo state arg"); |
| 4159 | return NULL; |
| 4160 | } |
| 4161 | } |
| 4162 | aware = (char)(tzinfo != Py_None); |
| 4163 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4164 | if (me != NULL) { |
| 4165 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4167 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4168 | me->hashcode = -1; |
| 4169 | me->hastzinfo = aware; |
| 4170 | if (aware) { |
| 4171 | Py_INCREF(tzinfo); |
| 4172 | me->tzinfo = tzinfo; |
| 4173 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4174 | if (pdata[2] & (1 << 7)) { |
| 4175 | me->data[2] -= 128; |
| 4176 | me->fold = 1; |
| 4177 | } |
| 4178 | else { |
| 4179 | me->fold = 0; |
| 4180 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4181 | } |
| 4182 | return (PyObject *)me; |
| 4183 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4184 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4185 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | &year, &month, &day, &hour, &minute, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4187 | &second, &usecond, &tzinfo, &fold)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4188 | if (check_date_args(year, month, day) < 0) |
| 4189 | return NULL; |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 4190 | if (check_time_args(hour, minute, second, usecond, fold) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4191 | return NULL; |
| 4192 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4193 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4194 | self = new_datetime_ex2(year, month, day, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4195 | hour, minute, second, usecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4196 | tzinfo, fold, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4197 | } |
| 4198 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4201 | /* TM_FUNC is the shared type of _PyTime_localtime() and |
| 4202 | * _PyTime_gmtime(). */ |
| 4203 | typedef int (*TM_FUNC)(time_t timer, struct tm*); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4204 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4205 | /* As of version 2015f max fold in IANA database is |
| 4206 | * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4207 | static long long max_fold_seconds = 24 * 3600; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4208 | /* NB: date(1970,1,1).toordinal() == 719163 */ |
Benjamin Peterson | ac965ca | 2016-09-18 18:12:21 -0700 | [diff] [blame] | 4209 | static long long epoch = 719163LL * 24 * 60 * 60; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4210 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4211 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4212 | utc_to_seconds(int year, int month, int day, |
| 4213 | int hour, int minute, int second) |
| 4214 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4215 | long long ordinal = ymd_to_ord(year, month, day); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4216 | return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; |
| 4217 | } |
| 4218 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4219 | static long long |
| 4220 | local(long long u) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4221 | { |
| 4222 | struct tm local_time; |
Alexander Belopolsky | 8e1d3a2 | 2016-07-25 13:54:51 -0400 | [diff] [blame] | 4223 | time_t t; |
| 4224 | u -= epoch; |
| 4225 | t = u; |
| 4226 | if (t != u) { |
| 4227 | PyErr_SetString(PyExc_OverflowError, |
| 4228 | "timestamp out of range for platform time_t"); |
| 4229 | return -1; |
| 4230 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4231 | /* XXX: add bounds checking */ |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4232 | if (_PyTime_localtime(t, &local_time) != 0) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4233 | return -1; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4234 | return utc_to_seconds(local_time.tm_year + 1900, |
| 4235 | local_time.tm_mon + 1, |
| 4236 | local_time.tm_mday, |
| 4237 | local_time.tm_hour, |
| 4238 | local_time.tm_min, |
| 4239 | local_time.tm_sec); |
| 4240 | } |
| 4241 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4242 | /* Internal helper. |
| 4243 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4244 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4245 | */ |
| 4246 | static PyObject * |
| 4247 | 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] | 4248 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4249 | { |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4250 | struct tm tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4251 | int year, month, day, hour, minute, second, fold = 0; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4252 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4253 | if (f(timet, &tm) != 0) |
| 4254 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4255 | |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4256 | year = tm.tm_year + 1900; |
| 4257 | month = tm.tm_mon + 1; |
| 4258 | day = tm.tm_mday; |
| 4259 | hour = tm.tm_hour; |
| 4260 | minute = tm.tm_min; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4261 | /* The platform localtime/gmtime may insert leap seconds, |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4262 | * indicated by tm.tm_sec > 59. We don't care about them, |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4263 | * except to the extent that passing them on to the datetime |
| 4264 | * constructor would raise ValueError for a reason that |
| 4265 | * made no sense to the user. |
| 4266 | */ |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 4267 | second = Py_MIN(59, tm.tm_sec); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4268 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4269 | if (tzinfo == Py_None && f == _PyTime_localtime) { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4270 | long long probe_seconds, result_seconds, transition; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4271 | |
| 4272 | result_seconds = utc_to_seconds(year, month, day, |
| 4273 | hour, minute, second); |
| 4274 | /* Probe max_fold_seconds to detect a fold. */ |
| 4275 | probe_seconds = local(epoch + timet - max_fold_seconds); |
| 4276 | if (probe_seconds == -1) |
| 4277 | return NULL; |
| 4278 | transition = result_seconds - probe_seconds - max_fold_seconds; |
| 4279 | if (transition < 0) { |
| 4280 | probe_seconds = local(epoch + timet + transition); |
| 4281 | if (probe_seconds == -1) |
| 4282 | return NULL; |
| 4283 | if (probe_seconds == result_seconds) |
| 4284 | fold = 1; |
| 4285 | } |
| 4286 | } |
| 4287 | return new_datetime_ex2(year, month, day, hour, |
| 4288 | minute, second, us, tzinfo, fold, |
| 4289 | (PyTypeObject *)cls); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | /* Internal helper. |
| 4293 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4294 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4295 | * have enough bits to cover a datetime's full range of precision, it's |
| 4296 | * better to call datetime_from_timet_and_us provided you have a way |
| 4297 | * to get that much precision (e.g., C time() isn't good enough). |
| 4298 | */ |
| 4299 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4300 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4301 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4303 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4304 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4305 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 4306 | if (_PyTime_ObjectToTimeval(timestamp, |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 4307 | &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4308 | return NULL; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4309 | |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4310 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | /* Internal helper. |
| 4314 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4315 | * gmtime for f as appropriate. |
| 4316 | */ |
| 4317 | static PyObject * |
| 4318 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4319 | { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4320 | _PyTime_t ts = _PyTime_GetSystemClock(); |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4321 | time_t secs; |
| 4322 | int us; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4323 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4324 | if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0) |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4325 | return NULL; |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4326 | assert(0 <= us && us <= 999999); |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4327 | |
Victor Stinner | 1e2b688 | 2015-09-18 13:23:02 +0200 | [diff] [blame] | 4328 | return datetime_from_timet_and_us(cls, f, secs, us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4331 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4332 | |
| 4333 | @classmethod |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4334 | datetime.datetime.now |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4335 | |
| 4336 | tz: object = None |
| 4337 | Timezone object. |
| 4338 | |
| 4339 | Returns new datetime object representing current time local to tz. |
| 4340 | |
| 4341 | If no tz is specified, uses local timezone. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4342 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4343 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4344 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4345 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4346 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4348 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4349 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4350 | /* Return best possible local time -- this isn't constrained by the |
| 4351 | * precision of a timestamp. |
| 4352 | */ |
| 4353 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4355 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4356 | self = datetime_best_possible((PyObject *)type, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4357 | tz == Py_None ? _PyTime_localtime : |
| 4358 | _PyTime_gmtime, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4359 | tz); |
| 4360 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4362 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | } |
| 4364 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4367 | /* Return best possible UTC time -- this isn't constrained by the |
| 4368 | * precision of a timestamp. |
| 4369 | */ |
| 4370 | static PyObject * |
| 4371 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4372 | { |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4373 | return datetime_best_possible(cls, _PyTime_gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4376 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4377 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4378 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4381 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | PyObject *tzinfo = Py_None; |
| 4383 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4384 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4385 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4386 | keywords, ×tamp, &tzinfo)) |
| 4387 | return NULL; |
| 4388 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4389 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | self = datetime_from_timestamp(cls, |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4392 | tzinfo == Py_None ? _PyTime_localtime : |
| 4393 | _PyTime_gmtime, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4394 | timestamp, |
| 4395 | tzinfo); |
| 4396 | if (self != NULL && tzinfo != Py_None) { |
| 4397 | /* Convert UTC to tzinfo's zone. */ |
Serhiy Storchaka | 576f132 | 2016-01-05 21:27:54 +0200 | [diff] [blame] | 4398 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4399 | } |
| 4400 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4403 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 4404 | static PyObject * |
| 4405 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 4406 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4407 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4409 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4410 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 4411 | result = datetime_from_timestamp(cls, _PyTime_gmtime, timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | Py_None); |
| 4413 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4416 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4417 | static PyObject * |
| 4418 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4421 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4422 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4424 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4426 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4427 | if (module == NULL) { |
| 4428 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4429 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4430 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4432 | return _PyObject_CallMethodId(module, &PyId__strptime_datetime, "OOO", |
| 4433 | cls, string, format); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4436 | /* Return new datetime from date/datetime and time arguments. */ |
| 4437 | static PyObject * |
| 4438 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4439 | { |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4440 | static char *keywords[] = {"date", "time", "tzinfo", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | PyObject *date; |
| 4442 | PyObject *time; |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4443 | PyObject *tzinfo = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4445 | |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4446 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | &PyDateTime_DateType, &date, |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4448 | &PyDateTime_TimeType, &time, &tzinfo)) { |
| 4449 | if (tzinfo == NULL) { |
| 4450 | if (HASTZINFO(time)) |
| 4451 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4452 | else |
| 4453 | tzinfo = Py_None; |
| 4454 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4455 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
Alexander Belopolsky | 43746c3 | 2016-08-02 17:49:30 -0400 | [diff] [blame] | 4456 | GET_YEAR(date), |
| 4457 | GET_MONTH(date), |
| 4458 | GET_DAY(date), |
| 4459 | TIME_GET_HOUR(time), |
| 4460 | TIME_GET_MINUTE(time), |
| 4461 | TIME_GET_SECOND(time), |
| 4462 | TIME_GET_MICROSECOND(time), |
| 4463 | tzinfo); |
| 4464 | if (result) |
| 4465 | DATE_SET_FOLD(result, TIME_GET_FOLD(time)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4466 | } |
| 4467 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4468 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4469 | |
| 4470 | /* |
| 4471 | * Destructor. |
| 4472 | */ |
| 4473 | |
| 4474 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4475 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | if (HASTZINFO(self)) { |
| 4478 | Py_XDECREF(self->tzinfo); |
| 4479 | } |
| 4480 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | /* |
| 4484 | * Indirect access to tzinfo methods. |
| 4485 | */ |
| 4486 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4487 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4488 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4489 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 4490 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4494 | datetime_dst(PyObject *self, PyObject *unused) { |
| 4495 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4499 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 4500 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4504 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4505 | */ |
| 4506 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4507 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4508 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4509 | */ |
| 4510 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4511 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | /* Note that the C-level additions can't overflow, because of |
| 4515 | * invariant bounds on the member values. |
| 4516 | */ |
| 4517 | int year = GET_YEAR(date); |
| 4518 | int month = GET_MONTH(date); |
| 4519 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4520 | int hour = DATE_GET_HOUR(date); |
| 4521 | int minute = DATE_GET_MINUTE(date); |
| 4522 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4523 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4524 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4526 | assert(factor == 1 || factor == -1); |
| 4527 | if (normalize_datetime(&year, &month, &day, |
| 4528 | &hour, &minute, &second, µsecond) < 0) |
| 4529 | return NULL; |
| 4530 | else |
| 4531 | return new_datetime(year, month, day, |
| 4532 | hour, minute, second, microsecond, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4533 | HASTZINFO(date) ? date->tzinfo : Py_None, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4537 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4539 | if (PyDateTime_Check(left)) { |
| 4540 | /* datetime + ??? */ |
| 4541 | if (PyDelta_Check(right)) |
| 4542 | /* datetime + delta */ |
| 4543 | return add_datetime_timedelta( |
| 4544 | (PyDateTime_DateTime *)left, |
| 4545 | (PyDateTime_Delta *)right, |
| 4546 | 1); |
| 4547 | } |
| 4548 | else if (PyDelta_Check(left)) { |
| 4549 | /* delta + datetime */ |
| 4550 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4551 | (PyDateTime_Delta *) left, |
| 4552 | 1); |
| 4553 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4554 | Py_RETURN_NOTIMPLEMENTED; |
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_subtract(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 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | if (PyDateTime_Check(left)) { |
| 4563 | /* datetime - ??? */ |
| 4564 | if (PyDateTime_Check(right)) { |
| 4565 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4566 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4568 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4569 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 4570 | offset2 = offset1 = Py_None; |
| 4571 | Py_INCREF(offset1); |
| 4572 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4573 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4574 | else { |
| 4575 | offset1 = datetime_utcoffset(left, NULL); |
| 4576 | if (offset1 == NULL) |
| 4577 | return NULL; |
| 4578 | offset2 = datetime_utcoffset(right, NULL); |
| 4579 | if (offset2 == NULL) { |
| 4580 | Py_DECREF(offset1); |
| 4581 | return NULL; |
| 4582 | } |
| 4583 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 4584 | PyErr_SetString(PyExc_TypeError, |
| 4585 | "can't subtract offset-naive and " |
| 4586 | "offset-aware datetimes"); |
| 4587 | Py_DECREF(offset1); |
| 4588 | Py_DECREF(offset2); |
| 4589 | return NULL; |
| 4590 | } |
| 4591 | } |
| 4592 | if ((offset1 != offset2) && |
| 4593 | delta_cmp(offset1, offset2) != 0) { |
| 4594 | offdiff = delta_subtract(offset1, offset2); |
| 4595 | if (offdiff == NULL) { |
| 4596 | Py_DECREF(offset1); |
| 4597 | Py_DECREF(offset2); |
| 4598 | return NULL; |
| 4599 | } |
| 4600 | } |
| 4601 | Py_DECREF(offset1); |
| 4602 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4604 | GET_MONTH(left), |
| 4605 | GET_DAY(left)) - |
| 4606 | ymd_to_ord(GET_YEAR(right), |
| 4607 | GET_MONTH(right), |
| 4608 | GET_DAY(right)); |
| 4609 | /* These can't overflow, since the values are |
| 4610 | * normalized. At most this gives the number of |
| 4611 | * seconds in one day. |
| 4612 | */ |
| 4613 | delta_s = (DATE_GET_HOUR(left) - |
| 4614 | DATE_GET_HOUR(right)) * 3600 + |
| 4615 | (DATE_GET_MINUTE(left) - |
| 4616 | DATE_GET_MINUTE(right)) * 60 + |
| 4617 | (DATE_GET_SECOND(left) - |
| 4618 | DATE_GET_SECOND(right)); |
| 4619 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4620 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4621 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 4622 | if (result == NULL) |
| 4623 | return NULL; |
| 4624 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4625 | if (offdiff != NULL) { |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 4626 | Py_SETREF(result, delta_subtract(result, offdiff)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4627 | Py_DECREF(offdiff); |
| 4628 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | } |
| 4630 | else if (PyDelta_Check(right)) { |
| 4631 | /* datetime - delta */ |
| 4632 | result = add_datetime_timedelta( |
| 4633 | (PyDateTime_DateTime *)left, |
| 4634 | (PyDateTime_Delta *)right, |
| 4635 | -1); |
| 4636 | } |
| 4637 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4639 | if (result == Py_NotImplemented) |
| 4640 | Py_INCREF(result); |
| 4641 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | /* Various ways to turn a datetime into a string. */ |
| 4645 | |
| 4646 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4647 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4649 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4650 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4652 | if (DATE_GET_MICROSECOND(self)) { |
| 4653 | baserepr = PyUnicode_FromFormat( |
| 4654 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 4655 | type_name, |
| 4656 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4657 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4658 | DATE_GET_SECOND(self), |
| 4659 | DATE_GET_MICROSECOND(self)); |
| 4660 | } |
| 4661 | else if (DATE_GET_SECOND(self)) { |
| 4662 | baserepr = PyUnicode_FromFormat( |
| 4663 | "%s(%d, %d, %d, %d, %d, %d)", |
| 4664 | type_name, |
| 4665 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4666 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4667 | DATE_GET_SECOND(self)); |
| 4668 | } |
| 4669 | else { |
| 4670 | baserepr = PyUnicode_FromFormat( |
| 4671 | "%s(%d, %d, %d, %d, %d)", |
| 4672 | type_name, |
| 4673 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4674 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4675 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4676 | if (baserepr != NULL && DATE_GET_FOLD(self) != 0) |
| 4677 | baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4679 | return baserepr; |
| 4680 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4683 | static PyObject * |
| 4684 | datetime_str(PyDateTime_DateTime *self) |
| 4685 | { |
Victor Stinner | 4c38154 | 2016-12-09 00:33:39 +0100 | [diff] [blame] | 4686 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "s", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4687 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4688 | |
| 4689 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4690 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | int sep = 'T'; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4693 | char *timespec = NULL; |
| 4694 | static char *keywords[] = {"sep", "timespec", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4695 | char buffer[100]; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4696 | PyObject *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4697 | int us = DATE_GET_MICROSECOND(self); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4698 | static char *specs[][2] = { |
| 4699 | {"hours", "%04d-%02d-%02d%c%02d"}, |
| 4700 | {"minutes", "%04d-%02d-%02d%c%02d:%02d"}, |
| 4701 | {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"}, |
| 4702 | {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"}, |
| 4703 | {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"}, |
| 4704 | }; |
| 4705 | size_t given_spec; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4706 | |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4707 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, ×pec)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4708 | return NULL; |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4709 | |
| 4710 | if (timespec == NULL || strcmp(timespec, "auto") == 0) { |
| 4711 | if (us == 0) { |
| 4712 | /* seconds */ |
| 4713 | given_spec = 2; |
| 4714 | } |
| 4715 | else { |
| 4716 | /* microseconds */ |
| 4717 | given_spec = 4; |
| 4718 | } |
| 4719 | } |
| 4720 | else { |
| 4721 | for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) { |
| 4722 | if (strcmp(timespec, specs[given_spec][0]) == 0) { |
| 4723 | if (given_spec == 3) { |
| 4724 | us = us / 1000; |
| 4725 | } |
| 4726 | break; |
| 4727 | } |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | if (given_spec == Py_ARRAY_LENGTH(specs)) { |
| 4732 | PyErr_Format(PyExc_ValueError, "Unknown timespec value"); |
| 4733 | return NULL; |
| 4734 | } |
| 4735 | else { |
| 4736 | result = PyUnicode_FromFormat(specs[given_spec][1], |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4737 | GET_YEAR(self), GET_MONTH(self), |
| 4738 | GET_DAY(self), (int)sep, |
| 4739 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4740 | DATE_GET_SECOND(self), us); |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 4741 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4743 | if (!result || !HASTZINFO(self)) |
| 4744 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | /* We need to append the UTC offset. */ |
| 4747 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 4748 | (PyObject *)self) < 0) { |
| 4749 | Py_DECREF(result); |
| 4750 | return NULL; |
| 4751 | } |
| 4752 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 4753 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4756 | static PyObject * |
| 4757 | datetime_ctime(PyDateTime_DateTime *self) |
| 4758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | return format_ctime((PyDateTime_Date *)self, |
| 4760 | DATE_GET_HOUR(self), |
| 4761 | DATE_GET_MINUTE(self), |
| 4762 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4765 | /* Miscellaneous methods. */ |
| 4766 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4767 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4768 | flip_fold(PyObject *dt) |
| 4769 | { |
| 4770 | return new_datetime_ex2(GET_YEAR(dt), |
| 4771 | GET_MONTH(dt), |
| 4772 | GET_DAY(dt), |
| 4773 | DATE_GET_HOUR(dt), |
| 4774 | DATE_GET_MINUTE(dt), |
| 4775 | DATE_GET_SECOND(dt), |
| 4776 | DATE_GET_MICROSECOND(dt), |
| 4777 | HASTZINFO(dt) ? |
| 4778 | ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, |
| 4779 | !DATE_GET_FOLD(dt), |
| 4780 | Py_TYPE(dt)); |
| 4781 | } |
| 4782 | |
| 4783 | static PyObject * |
| 4784 | get_flip_fold_offset(PyObject *dt) |
| 4785 | { |
| 4786 | PyObject *result, *flip_dt; |
| 4787 | |
| 4788 | flip_dt = flip_fold(dt); |
| 4789 | if (flip_dt == NULL) |
| 4790 | return NULL; |
| 4791 | result = datetime_utcoffset(flip_dt, NULL); |
| 4792 | Py_DECREF(flip_dt); |
| 4793 | return result; |
| 4794 | } |
| 4795 | |
| 4796 | /* PEP 495 exception: Whenever one or both of the operands in |
| 4797 | * inter-zone comparison is such that its utcoffset() depends |
| 4798 | * on the value of its fold fold attribute, the result is False. |
| 4799 | * |
| 4800 | * Return 1 if exception applies, 0 if not, and -1 on error. |
| 4801 | */ |
| 4802 | static int |
| 4803 | pep495_eq_exception(PyObject *self, PyObject *other, |
| 4804 | PyObject *offset_self, PyObject *offset_other) |
| 4805 | { |
| 4806 | int result = 0; |
| 4807 | PyObject *flip_offset; |
| 4808 | |
| 4809 | flip_offset = get_flip_fold_offset(self); |
| 4810 | if (flip_offset == NULL) |
| 4811 | return -1; |
| 4812 | if (flip_offset != offset_self && |
| 4813 | delta_cmp(flip_offset, offset_self)) |
| 4814 | { |
| 4815 | result = 1; |
| 4816 | goto done; |
| 4817 | } |
| 4818 | Py_DECREF(flip_offset); |
| 4819 | |
| 4820 | flip_offset = get_flip_fold_offset(other); |
| 4821 | if (flip_offset == NULL) |
| 4822 | return -1; |
| 4823 | if (flip_offset != offset_other && |
| 4824 | delta_cmp(flip_offset, offset_other)) |
| 4825 | result = 1; |
| 4826 | done: |
| 4827 | Py_DECREF(flip_offset); |
| 4828 | return result; |
| 4829 | } |
| 4830 | |
| 4831 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4832 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4833 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4834 | PyObject *result = NULL; |
| 4835 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4836 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | if (! PyDateTime_Check(other)) { |
| 4839 | if (PyDate_Check(other)) { |
| 4840 | /* Prevent invocation of date_richcompare. We want to |
| 4841 | return NotImplemented here to give the other object |
| 4842 | a chance. But since DateTime is a subclass of |
| 4843 | Date, if the other object is a Date, it would |
| 4844 | compute an ordering based on the date part alone, |
| 4845 | and we don't want that. So force unequal or |
| 4846 | uncomparable here in that case. */ |
| 4847 | if (op == Py_EQ) |
| 4848 | Py_RETURN_FALSE; |
| 4849 | if (op == Py_NE) |
| 4850 | Py_RETURN_TRUE; |
| 4851 | return cmperror(self, other); |
| 4852 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4853 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4854 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4855 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4856 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4858 | ((PyDateTime_DateTime *)other)->data, |
| 4859 | _PyDateTime_DATETIME_DATASIZE); |
| 4860 | return diff_to_bool(diff, op); |
| 4861 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4862 | offset1 = datetime_utcoffset(self, NULL); |
| 4863 | if (offset1 == NULL) |
| 4864 | return NULL; |
| 4865 | offset2 = datetime_utcoffset(other, NULL); |
| 4866 | if (offset2 == NULL) |
| 4867 | goto done; |
| 4868 | /* If they're both naive, or both aware and have the same offsets, |
| 4869 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4870 | * offset2 == Py_None at this point. |
| 4871 | */ |
| 4872 | if ((offset1 == offset2) || |
| 4873 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4874 | delta_cmp(offset1, offset2) == 0)) { |
| 4875 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4876 | ((PyDateTime_DateTime *)other)->data, |
| 4877 | _PyDateTime_DATETIME_DATASIZE); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4878 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 4879 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 4880 | if (ex == -1) |
| 4881 | goto done; |
| 4882 | if (ex) |
| 4883 | diff = 1; |
| 4884 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4885 | result = diff_to_bool(diff, op); |
| 4886 | } |
| 4887 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4888 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4889 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4890 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4891 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4892 | other); |
| 4893 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4894 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4895 | diff = GET_TD_DAYS(delta); |
| 4896 | if (diff == 0) |
| 4897 | diff = GET_TD_SECONDS(delta) | |
| 4898 | GET_TD_MICROSECONDS(delta); |
| 4899 | Py_DECREF(delta); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4900 | if ((op == Py_EQ || op == Py_NE) && diff == 0) { |
| 4901 | int ex = pep495_eq_exception(self, other, offset1, offset2); |
| 4902 | if (ex == -1) |
| 4903 | goto done; |
| 4904 | if (ex) |
| 4905 | diff = 1; |
| 4906 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4907 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4909 | else if (op == Py_EQ) { |
| 4910 | result = Py_False; |
| 4911 | Py_INCREF(result); |
| 4912 | } |
| 4913 | else if (op == Py_NE) { |
| 4914 | result = Py_True; |
| 4915 | Py_INCREF(result); |
| 4916 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4917 | else { |
| 4918 | PyErr_SetString(PyExc_TypeError, |
| 4919 | "can't compare offset-naive and " |
| 4920 | "offset-aware datetimes"); |
| 4921 | } |
| 4922 | done: |
| 4923 | Py_DECREF(offset1); |
| 4924 | Py_XDECREF(offset2); |
| 4925 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4928 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4929 | datetime_hash(PyDateTime_DateTime *self) |
| 4930 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | if (self->hashcode == -1) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 4932 | PyObject *offset, *self0; |
| 4933 | if (DATE_GET_FOLD(self)) { |
| 4934 | self0 = new_datetime_ex2(GET_YEAR(self), |
| 4935 | GET_MONTH(self), |
| 4936 | GET_DAY(self), |
| 4937 | DATE_GET_HOUR(self), |
| 4938 | DATE_GET_MINUTE(self), |
| 4939 | DATE_GET_SECOND(self), |
| 4940 | DATE_GET_MICROSECOND(self), |
| 4941 | HASTZINFO(self) ? self->tzinfo : Py_None, |
| 4942 | 0, Py_TYPE(self)); |
| 4943 | if (self0 == NULL) |
| 4944 | return -1; |
| 4945 | } |
| 4946 | else { |
| 4947 | self0 = (PyObject *)self; |
| 4948 | Py_INCREF(self0); |
| 4949 | } |
| 4950 | offset = datetime_utcoffset(self0, NULL); |
| 4951 | Py_DECREF(self0); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4952 | |
| 4953 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4956 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4957 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4958 | self->hashcode = generic_hash( |
| 4959 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4960 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4961 | PyObject *temp1, *temp2; |
| 4962 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4964 | assert(HASTZINFO(self)); |
| 4965 | days = ymd_to_ord(GET_YEAR(self), |
| 4966 | GET_MONTH(self), |
| 4967 | GET_DAY(self)); |
| 4968 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4969 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4970 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4971 | temp1 = new_delta(days, seconds, |
| 4972 | DATE_GET_MICROSECOND(self), |
| 4973 | 1); |
| 4974 | if (temp1 == NULL) { |
| 4975 | Py_DECREF(offset); |
| 4976 | return -1; |
| 4977 | } |
| 4978 | temp2 = delta_subtract(temp1, offset); |
| 4979 | Py_DECREF(temp1); |
| 4980 | if (temp2 == NULL) { |
| 4981 | Py_DECREF(offset); |
| 4982 | return -1; |
| 4983 | } |
| 4984 | self->hashcode = PyObject_Hash(temp2); |
| 4985 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4986 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4987 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4988 | } |
| 4989 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4990 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4991 | |
| 4992 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4993 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4995 | PyObject *clone; |
| 4996 | PyObject *tuple; |
| 4997 | int y = GET_YEAR(self); |
| 4998 | int m = GET_MONTH(self); |
| 4999 | int d = GET_DAY(self); |
| 5000 | int hh = DATE_GET_HOUR(self); |
| 5001 | int mm = DATE_GET_MINUTE(self); |
| 5002 | int ss = DATE_GET_SECOND(self); |
| 5003 | int us = DATE_GET_MICROSECOND(self); |
| 5004 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5005 | int fold = DATE_GET_FOLD(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5006 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5007 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5008 | datetime_kws, |
| 5009 | &y, &m, &d, &hh, &mm, &ss, &us, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5010 | &tzinfo, &fold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5011 | return NULL; |
| 5012 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 5013 | if (tuple == NULL) |
| 5014 | return NULL; |
| 5015 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5016 | |
| 5017 | if (clone != NULL) { |
| 5018 | if (fold != 0 && fold != 1) { |
| 5019 | PyErr_SetString(PyExc_ValueError, |
| 5020 | "fold must be either 0 or 1"); |
| 5021 | return NULL; |
| 5022 | } |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5023 | DATE_SET_FOLD(clone, fold); |
Alexander Belopolsky | 47649ab | 2016-08-08 17:05:40 -0400 | [diff] [blame] | 5024 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5025 | Py_DECREF(tuple); |
| 5026 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5030 | local_timezone_from_timestamp(time_t timestamp) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5031 | { |
| 5032 | PyObject *result = NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5033 | PyObject *delta; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5034 | struct tm local_time_tm; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5035 | PyObject *nameo = NULL; |
| 5036 | const char *zone = NULL; |
| 5037 | |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5038 | if (_PyTime_localtime(timestamp, &local_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5039 | return NULL; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5040 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5041 | zone = local_time_tm.tm_zone; |
| 5042 | delta = new_delta(0, local_time_tm.tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5043 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 5044 | { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5045 | PyObject *local_time, *utc_time; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5046 | struct tm utc_time_tm; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5047 | char buf[100]; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5048 | strftime(buf, sizeof(buf), "%Z", &local_time_tm); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5049 | zone = buf; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5050 | local_time = new_datetime(local_time_tm.tm_year + 1900, |
| 5051 | local_time_tm.tm_mon + 1, |
| 5052 | local_time_tm.tm_mday, |
| 5053 | local_time_tm.tm_hour, |
| 5054 | local_time_tm.tm_min, |
| 5055 | local_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5056 | if (local_time == NULL) { |
| 5057 | return NULL; |
| 5058 | } |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 5059 | if (_PyTime_gmtime(timestamp, &utc_time_tm) != 0) |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5060 | return NULL; |
Alexander Belopolsky | 6d88fa5 | 2016-09-10 15:58:31 -0400 | [diff] [blame] | 5061 | utc_time = new_datetime(utc_time_tm.tm_year + 1900, |
| 5062 | utc_time_tm.tm_mon + 1, |
| 5063 | utc_time_tm.tm_mday, |
| 5064 | utc_time_tm.tm_hour, |
| 5065 | utc_time_tm.tm_min, |
| 5066 | utc_time_tm.tm_sec, 0, Py_None, 0); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5067 | if (utc_time == NULL) { |
| 5068 | Py_DECREF(local_time); |
| 5069 | return NULL; |
| 5070 | } |
| 5071 | delta = datetime_subtract(local_time, utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5072 | Py_DECREF(local_time); |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5073 | Py_DECREF(utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5074 | } |
| 5075 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5076 | if (delta == NULL) { |
| 5077 | return NULL; |
| 5078 | } |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5079 | if (zone != NULL) { |
| 5080 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 5081 | if (nameo == NULL) |
| 5082 | goto error; |
| 5083 | } |
| 5084 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 5085 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5086 | error: |
| 5087 | Py_DECREF(delta); |
| 5088 | return result; |
| 5089 | } |
| 5090 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5091 | static PyObject * |
| 5092 | local_timezone(PyDateTime_DateTime *utc_time) |
| 5093 | { |
| 5094 | time_t timestamp; |
| 5095 | PyObject *delta; |
| 5096 | PyObject *one_second; |
| 5097 | PyObject *seconds; |
| 5098 | |
| 5099 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 5100 | if (delta == NULL) |
| 5101 | return NULL; |
| 5102 | one_second = new_delta(0, 1, 0, 0); |
| 5103 | if (one_second == NULL) { |
| 5104 | Py_DECREF(delta); |
| 5105 | return NULL; |
| 5106 | } |
| 5107 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 5108 | (PyDateTime_Delta *)one_second); |
| 5109 | Py_DECREF(one_second); |
| 5110 | Py_DECREF(delta); |
| 5111 | if (seconds == NULL) |
| 5112 | return NULL; |
| 5113 | timestamp = _PyLong_AsTime_t(seconds); |
| 5114 | Py_DECREF(seconds); |
| 5115 | if (timestamp == -1 && PyErr_Occurred()) |
| 5116 | return NULL; |
| 5117 | return local_timezone_from_timestamp(timestamp); |
| 5118 | } |
| 5119 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5120 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5121 | local_to_seconds(int year, int month, int day, |
| 5122 | int hour, int minute, int second, int fold); |
| 5123 | |
| 5124 | static PyObject * |
| 5125 | local_timezone_from_local(PyDateTime_DateTime *local_dt) |
| 5126 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5127 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5128 | time_t timestamp; |
| 5129 | seconds = local_to_seconds(GET_YEAR(local_dt), |
| 5130 | GET_MONTH(local_dt), |
| 5131 | GET_DAY(local_dt), |
| 5132 | DATE_GET_HOUR(local_dt), |
| 5133 | DATE_GET_MINUTE(local_dt), |
| 5134 | DATE_GET_SECOND(local_dt), |
| 5135 | DATE_GET_FOLD(local_dt)); |
| 5136 | if (seconds == -1) |
| 5137 | return NULL; |
| 5138 | /* XXX: add bounds check */ |
| 5139 | timestamp = seconds - epoch; |
| 5140 | return local_timezone_from_timestamp(timestamp); |
| 5141 | } |
| 5142 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5143 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5144 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5145 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5146 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5147 | PyObject *offset; |
| 5148 | PyObject *temp; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5149 | PyObject *self_tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5150 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5151 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5152 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5153 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 5154 | &tzinfo)) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5155 | return NULL; |
| 5156 | |
| 5157 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5158 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5159 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5160 | if (!HASTZINFO(self) || self->tzinfo == Py_None) { |
| 5161 | self_tzinfo = local_timezone_from_local(self); |
| 5162 | if (self_tzinfo == NULL) |
| 5163 | return NULL; |
| 5164 | } else { |
| 5165 | self_tzinfo = self->tzinfo; |
| 5166 | Py_INCREF(self_tzinfo); |
| 5167 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5169 | /* Conversion to self's own time zone is a NOP. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5170 | if (self_tzinfo == tzinfo) { |
| 5171 | Py_DECREF(self_tzinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5172 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5173 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 5175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5176 | /* Convert self to UTC. */ |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5177 | offset = call_utcoffset(self_tzinfo, (PyObject *)self); |
| 5178 | Py_DECREF(self_tzinfo); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5179 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5181 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5182 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5183 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5184 | Py_DECREF(offset); |
| 5185 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5186 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5187 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5188 | /* Make sure result is aware and UTC. */ |
| 5189 | if (!HASTZINFO(result)) { |
| 5190 | temp = (PyObject *)result; |
| 5191 | result = (PyDateTime_DateTime *) |
| 5192 | new_datetime_ex2(GET_YEAR(result), |
| 5193 | GET_MONTH(result), |
| 5194 | GET_DAY(result), |
| 5195 | DATE_GET_HOUR(result), |
| 5196 | DATE_GET_MINUTE(result), |
| 5197 | DATE_GET_SECOND(result), |
| 5198 | DATE_GET_MICROSECOND(result), |
| 5199 | PyDateTime_TimeZone_UTC, |
| 5200 | DATE_GET_FOLD(result), |
| 5201 | Py_TYPE(result)); |
| 5202 | Py_DECREF(temp); |
| 5203 | if (result == NULL) |
| 5204 | return NULL; |
| 5205 | } |
| 5206 | else { |
| 5207 | /* Result is already aware - just replace tzinfo. */ |
| 5208 | temp = result->tzinfo; |
| 5209 | result->tzinfo = PyDateTime_TimeZone_UTC; |
| 5210 | Py_INCREF(result->tzinfo); |
| 5211 | Py_DECREF(temp); |
| 5212 | } |
| 5213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5214 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5215 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 5216 | if (tzinfo == Py_None) { |
| 5217 | tzinfo = local_timezone(result); |
| 5218 | if (tzinfo == NULL) { |
| 5219 | Py_DECREF(result); |
| 5220 | return NULL; |
| 5221 | } |
| 5222 | } |
| 5223 | else |
| 5224 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5225 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5226 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 5227 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 5228 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5229 | result = (PyDateTime_DateTime *) |
| 5230 | _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", temp); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5231 | Py_DECREF(temp); |
| 5232 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 5233 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5237 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5239 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5241 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5242 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5243 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5244 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 5245 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5246 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5247 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5248 | if (dst != Py_None) |
| 5249 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 5250 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5251 | } |
| 5252 | return build_struct_time(GET_YEAR(self), |
| 5253 | GET_MONTH(self), |
| 5254 | GET_DAY(self), |
| 5255 | DATE_GET_HOUR(self), |
| 5256 | DATE_GET_MINUTE(self), |
| 5257 | DATE_GET_SECOND(self), |
| 5258 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5261 | static long long |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5262 | local_to_seconds(int year, int month, int day, |
| 5263 | int hour, int minute, int second, int fold) |
| 5264 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5265 | long long t, a, b, u1, u2, t1, t2, lt; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5266 | t = utc_to_seconds(year, month, day, hour, minute, second); |
| 5267 | /* Our goal is to solve t = local(u) for u. */ |
| 5268 | lt = local(t); |
| 5269 | if (lt == -1) |
| 5270 | return -1; |
| 5271 | a = lt - t; |
| 5272 | u1 = t - a; |
| 5273 | t1 = local(u1); |
| 5274 | if (t1 == -1) |
| 5275 | return -1; |
| 5276 | if (t1 == t) { |
| 5277 | /* We found one solution, but it may not be the one we need. |
| 5278 | * Look for an earlier solution (if `fold` is 0), or a |
| 5279 | * later one (if `fold` is 1). */ |
| 5280 | if (fold) |
| 5281 | u2 = u1 + max_fold_seconds; |
| 5282 | else |
| 5283 | u2 = u1 - max_fold_seconds; |
| 5284 | lt = local(u2); |
| 5285 | if (lt == -1) |
| 5286 | return -1; |
| 5287 | b = lt - u2; |
| 5288 | if (a == b) |
| 5289 | return u1; |
| 5290 | } |
| 5291 | else { |
| 5292 | b = t1 - u1; |
| 5293 | assert(a != b); |
| 5294 | } |
| 5295 | u2 = t - b; |
| 5296 | t2 = local(u2); |
| 5297 | if (t2 == -1) |
| 5298 | return -1; |
| 5299 | if (t2 == t) |
| 5300 | return u2; |
| 5301 | if (t1 == t) |
| 5302 | return u1; |
| 5303 | /* We have found both offsets a and b, but neither t - a nor t - b is |
| 5304 | * a solution. This means t is in the gap. */ |
| 5305 | return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); |
| 5306 | } |
| 5307 | |
| 5308 | /* date(1970,1,1).toordinal() == 719163 */ |
| 5309 | #define EPOCH_SECONDS (719163LL * 24 * 60 * 60) |
| 5310 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5311 | static PyObject * |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5312 | datetime_timestamp(PyDateTime_DateTime *self) |
| 5313 | { |
| 5314 | PyObject *result; |
| 5315 | |
| 5316 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 5317 | PyObject *delta; |
| 5318 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 5319 | if (delta == NULL) |
| 5320 | return NULL; |
| 5321 | result = delta_total_seconds(delta); |
| 5322 | Py_DECREF(delta); |
| 5323 | } |
| 5324 | else { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5325 | long long seconds; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5326 | seconds = local_to_seconds(GET_YEAR(self), |
| 5327 | GET_MONTH(self), |
| 5328 | GET_DAY(self), |
| 5329 | DATE_GET_HOUR(self), |
| 5330 | DATE_GET_MINUTE(self), |
| 5331 | DATE_GET_SECOND(self), |
| 5332 | DATE_GET_FOLD(self)); |
| 5333 | if (seconds == -1) |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5334 | return NULL; |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5335 | result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + |
| 5336 | DATE_GET_MICROSECOND(self) / 1e6); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5337 | } |
| 5338 | return result; |
| 5339 | } |
| 5340 | |
| 5341 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5342 | datetime_getdate(PyDateTime_DateTime *self) |
| 5343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | return new_date(GET_YEAR(self), |
| 5345 | GET_MONTH(self), |
| 5346 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | static PyObject * |
| 5350 | datetime_gettime(PyDateTime_DateTime *self) |
| 5351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5352 | return new_time(DATE_GET_HOUR(self), |
| 5353 | DATE_GET_MINUTE(self), |
| 5354 | DATE_GET_SECOND(self), |
| 5355 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5356 | Py_None, |
| 5357 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
| 5360 | static PyObject * |
| 5361 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 5362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5363 | return new_time(DATE_GET_HOUR(self), |
| 5364 | DATE_GET_MINUTE(self), |
| 5365 | DATE_GET_SECOND(self), |
| 5366 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5367 | GET_DT_TZINFO(self), |
| 5368 | DATE_GET_FOLD(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | static PyObject * |
| 5372 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5373 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5374 | int y, m, d, hh, mm, ss; |
| 5375 | PyObject *tzinfo; |
| 5376 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5377 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5378 | tzinfo = GET_DT_TZINFO(self); |
| 5379 | if (tzinfo == Py_None) { |
| 5380 | utcself = self; |
| 5381 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5382 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5383 | else { |
| 5384 | PyObject *offset; |
| 5385 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 5386 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 5387 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5388 | if (offset == Py_None) { |
| 5389 | Py_DECREF(offset); |
| 5390 | utcself = self; |
| 5391 | Py_INCREF(utcself); |
| 5392 | } |
| 5393 | else { |
| 5394 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 5395 | (PyDateTime_Delta *)offset, -1); |
| 5396 | Py_DECREF(offset); |
| 5397 | if (utcself == NULL) |
| 5398 | return NULL; |
| 5399 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5400 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5401 | y = GET_YEAR(utcself); |
| 5402 | m = GET_MONTH(utcself); |
| 5403 | d = GET_DAY(utcself); |
| 5404 | hh = DATE_GET_HOUR(utcself); |
| 5405 | mm = DATE_GET_MINUTE(utcself); |
| 5406 | ss = DATE_GET_SECOND(utcself); |
| 5407 | |
| 5408 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 5412 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 5413 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5414 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5415 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 5416 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 5417 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5418 | */ |
| 5419 | static PyObject * |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5420 | datetime_getstate(PyDateTime_DateTime *self, int proto) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5422 | PyObject *basestate; |
| 5423 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 5426 | _PyDateTime_DATETIME_DATASIZE); |
| 5427 | if (basestate != NULL) { |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5428 | if (proto > 3 && DATE_GET_FOLD(self)) |
| 5429 | /* Set the first bit of the third byte */ |
| 5430 | PyBytes_AS_STRING(basestate)[2] |= (1 << 7); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5431 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 5432 | result = PyTuple_Pack(1, basestate); |
| 5433 | else |
| 5434 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 5435 | Py_DECREF(basestate); |
| 5436 | } |
| 5437 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | static PyObject * |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5441 | datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5442 | { |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5443 | int proto; |
| 5444 | if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto)) |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5445 | return NULL; |
| 5446 | |
| 5447 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5450 | static PyObject * |
| 5451 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
| 5452 | { |
| 5453 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2)); |
| 5454 | } |
| 5455 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5456 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5458 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5459 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 5460 | DATETIME_DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 5463 | METH_NOARGS | METH_CLASS, |
| 5464 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 5467 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5468 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5470 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 5471 | METH_VARARGS | METH_CLASS, |
Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 5472 | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5474 | {"strptime", (PyCFunction)datetime_strptime, |
| 5475 | METH_VARARGS | METH_CLASS, |
| 5476 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 5477 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 | {"combine", (PyCFunction)datetime_combine, |
| 5480 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5481 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5483 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5485 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 5486 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5488 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 5489 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5491 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 5492 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
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 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 5495 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5497 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 5498 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5499 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5500 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 5501 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 5502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 5504 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5506 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 5507 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5508 | "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5509 | "sep is used to separate the year from the time, and " |
Alexander Belopolsky | a2998a6 | 2016-03-06 14:58:43 -0500 | [diff] [blame] | 5510 | "defaults to 'T'.\n" |
| 5511 | "timespec specifies what components of the time to include" |
| 5512 | " (allowed values are 'auto', 'hours', 'minutes', 'seconds'," |
| 5513 | " 'milliseconds', and 'microseconds').\n")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5515 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 5516 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5518 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 5519 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5521 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 5522 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5524 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 5525 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5527 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 5528 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5529 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5530 | {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5531 | PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5532 | |
Serhiy Storchaka | 546ce65 | 2016-11-22 00:29:42 +0200 | [diff] [blame] | 5533 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 5534 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 5535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5537 | }; |
| 5538 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 5539 | static const char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5540 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 5541 | \n\ |
| 5542 | 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] | 5543 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5544 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5545 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5546 | datetime_add, /* nb_add */ |
| 5547 | datetime_subtract, /* nb_subtract */ |
| 5548 | 0, /* nb_multiply */ |
| 5549 | 0, /* nb_remainder */ |
| 5550 | 0, /* nb_divmod */ |
| 5551 | 0, /* nb_power */ |
| 5552 | 0, /* nb_negative */ |
| 5553 | 0, /* nb_positive */ |
| 5554 | 0, /* nb_absolute */ |
| 5555 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5556 | }; |
| 5557 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5558 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5559 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5560 | "datetime.datetime", /* tp_name */ |
| 5561 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 5562 | 0, /* tp_itemsize */ |
| 5563 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 5564 | 0, /* tp_print */ |
| 5565 | 0, /* tp_getattr */ |
| 5566 | 0, /* tp_setattr */ |
| 5567 | 0, /* tp_reserved */ |
| 5568 | (reprfunc)datetime_repr, /* tp_repr */ |
| 5569 | &datetime_as_number, /* tp_as_number */ |
| 5570 | 0, /* tp_as_sequence */ |
| 5571 | 0, /* tp_as_mapping */ |
| 5572 | (hashfunc)datetime_hash, /* tp_hash */ |
| 5573 | 0, /* tp_call */ |
| 5574 | (reprfunc)datetime_str, /* tp_str */ |
| 5575 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5576 | 0, /* tp_setattro */ |
| 5577 | 0, /* tp_as_buffer */ |
| 5578 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5579 | datetime_doc, /* tp_doc */ |
| 5580 | 0, /* tp_traverse */ |
| 5581 | 0, /* tp_clear */ |
| 5582 | datetime_richcompare, /* tp_richcompare */ |
| 5583 | 0, /* tp_weaklistoffset */ |
| 5584 | 0, /* tp_iter */ |
| 5585 | 0, /* tp_iternext */ |
| 5586 | datetime_methods, /* tp_methods */ |
| 5587 | 0, /* tp_members */ |
| 5588 | datetime_getset, /* tp_getset */ |
| 5589 | &PyDateTime_DateType, /* tp_base */ |
| 5590 | 0, /* tp_dict */ |
| 5591 | 0, /* tp_descr_get */ |
| 5592 | 0, /* tp_descr_set */ |
| 5593 | 0, /* tp_dictoffset */ |
| 5594 | 0, /* tp_init */ |
| 5595 | datetime_alloc, /* tp_alloc */ |
| 5596 | datetime_new, /* tp_new */ |
| 5597 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5598 | }; |
| 5599 | |
| 5600 | /* --------------------------------------------------------------------------- |
| 5601 | * Module methods and initialization. |
| 5602 | */ |
| 5603 | |
| 5604 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5605 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5606 | }; |
| 5607 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5608 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 5609 | * datetime.h. |
| 5610 | */ |
| 5611 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | &PyDateTime_DateType, |
| 5613 | &PyDateTime_DateTimeType, |
| 5614 | &PyDateTime_TimeType, |
| 5615 | &PyDateTime_DeltaType, |
| 5616 | &PyDateTime_TZInfoType, |
| 5617 | new_date_ex, |
| 5618 | new_datetime_ex, |
| 5619 | new_time_ex, |
| 5620 | new_delta_ex, |
| 5621 | datetime_fromtimestamp, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5622 | date_fromtimestamp, |
| 5623 | new_datetime_ex2, |
| 5624 | new_time_ex2 |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5625 | }; |
| 5626 | |
| 5627 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5628 | |
| 5629 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5631 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5632 | "Fast implementation of the datetime type.", |
| 5633 | -1, |
| 5634 | module_methods, |
| 5635 | NULL, |
| 5636 | NULL, |
| 5637 | NULL, |
| 5638 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5639 | }; |
| 5640 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5641 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5642 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5644 | PyObject *m; /* a module object */ |
| 5645 | PyObject *d; /* its dict */ |
| 5646 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5647 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5649 | m = PyModule_Create(&datetimemodule); |
| 5650 | if (m == NULL) |
| 5651 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5653 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 5654 | return NULL; |
| 5655 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 5656 | return NULL; |
| 5657 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 5658 | return NULL; |
| 5659 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 5660 | return NULL; |
| 5661 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 5662 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5663 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 5664 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5666 | /* timedelta values */ |
| 5667 | d = PyDateTime_DeltaType.tp_dict; |
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 | x = new_delta(0, 0, 1, 0); |
| 5670 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5671 | return NULL; |
| 5672 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 5675 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5676 | return NULL; |
| 5677 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5679 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 5680 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5681 | return NULL; |
| 5682 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 | /* date values */ |
| 5685 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5687 | x = new_date(1, 1, 1); |
| 5688 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5689 | return NULL; |
| 5690 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | x = new_date(MAXYEAR, 12, 31); |
| 5693 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5694 | return NULL; |
| 5695 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5697 | x = new_delta(1, 0, 0, 0); |
| 5698 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5699 | return NULL; |
| 5700 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5702 | /* time values */ |
| 5703 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5704 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5705 | x = new_time(0, 0, 0, 0, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5707 | return NULL; |
| 5708 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5709 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5710 | x = new_time(23, 59, 59, 999999, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5711 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5712 | return NULL; |
| 5713 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | x = new_delta(0, 0, 1, 0); |
| 5716 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5717 | return NULL; |
| 5718 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5720 | /* datetime values */ |
| 5721 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5722 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5723 | 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] | 5724 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5725 | return NULL; |
| 5726 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5727 | |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5728 | 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] | 5729 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5730 | return NULL; |
| 5731 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5733 | x = new_delta(0, 0, 1, 0); |
| 5734 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5735 | return NULL; |
| 5736 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5737 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5738 | /* timezone values */ |
| 5739 | d = PyDateTime_TimeZoneType.tp_dict; |
| 5740 | |
| 5741 | delta = new_delta(0, 0, 0, 0); |
| 5742 | if (delta == NULL) |
| 5743 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5744 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5745 | Py_DECREF(delta); |
| 5746 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 5747 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 5748 | PyDateTime_TimeZone_UTC = x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5749 | |
| 5750 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 5751 | if (delta == NULL) |
| 5752 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5753 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5754 | Py_DECREF(delta); |
| 5755 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5756 | return NULL; |
| 5757 | Py_DECREF(x); |
| 5758 | |
| 5759 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 5760 | if (delta == NULL) |
| 5761 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5762 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5763 | Py_DECREF(delta); |
| 5764 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5765 | return NULL; |
| 5766 | Py_DECREF(x); |
| 5767 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5768 | /* Epoch */ |
| 5769 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
Alexander Belopolsky | 5d0c598 | 2016-07-22 18:47:04 -0400 | [diff] [blame] | 5770 | PyDateTime_TimeZone_UTC, 0); |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5771 | if (PyDateTime_Epoch == NULL) |
| 5772 | return NULL; |
| 5773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5774 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5775 | PyModule_AddIntMacro(m, MINYEAR); |
| 5776 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5778 | Py_INCREF(&PyDateTime_DateType); |
| 5779 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5781 | Py_INCREF(&PyDateTime_DateTimeType); |
| 5782 | PyModule_AddObject(m, "datetime", |
| 5783 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5785 | Py_INCREF(&PyDateTime_TimeType); |
| 5786 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5788 | Py_INCREF(&PyDateTime_DeltaType); |
| 5789 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5791 | Py_INCREF(&PyDateTime_TZInfoType); |
| 5792 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5793 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5794 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 5795 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 5796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 5798 | if (x == NULL) |
| 5799 | return NULL; |
| 5800 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5802 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 5803 | * pasting together 4 single years. |
| 5804 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5805 | Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5806 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 5809 | * get from pasting together 4 100-year cycles. |
| 5810 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5811 | Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5814 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 5815 | * pasting together 25 4-year cycles. |
| 5816 | */ |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 5817 | Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5818 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5819 | |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5820 | one = PyLong_FromLong(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5821 | us_per_ms = PyLong_FromLong(1000); |
| 5822 | us_per_second = PyLong_FromLong(1000000); |
| 5823 | us_per_minute = PyLong_FromLong(60000000); |
| 5824 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5825 | if (one == NULL || us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5826 | us_per_minute == NULL || seconds_per_day == NULL) |
| 5827 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5829 | /* The rest are too big for 32-bit ints, but even |
| 5830 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 5831 | */ |
| 5832 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 5833 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 5834 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 5835 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 5836 | return NULL; |
| 5837 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5838 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5839 | |
| 5840 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5841 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5842 | x.n = x stripped of its timezone -- its naive time. |
| 5843 | 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] | 5844 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5845 | 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] | 5846 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5847 | x.s = x's standard offset, x.o - x.d |
| 5848 | |
| 5849 | Now some derived rules, where k is a duration (timedelta). |
| 5850 | |
| 5851 | 1. x.o = x.s + x.d |
| 5852 | This follows from the definition of x.s. |
| 5853 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5854 | 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] | 5855 | This is actually a requirement, an assumption we need to make about |
| 5856 | sane tzinfo classes. |
| 5857 | |
| 5858 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 5859 | This is again a requirement for a sane tzinfo class. |
| 5860 | |
| 5861 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5862 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5863 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5864 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5865 | Again follows from how arithmetic is defined. |
| 5866 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5867 | 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] | 5868 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 5869 | None when called). |
| 5870 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5871 | 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] | 5872 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5873 | |
| 5874 | By #3, we want |
| 5875 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5876 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5877 | |
| 5878 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 5879 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 5880 | becomes true; in effect, we want to solve [2] for k: |
| 5881 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5882 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5883 | |
| 5884 | By #1, this is the same as |
| 5885 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5886 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5887 | |
| 5888 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 5889 | Substituting that into [3], |
| 5890 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5891 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 5892 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 5893 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 5894 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5895 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5896 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 5897 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 5898 | very large, since all offset-returning methods return a duration of magnitude |
| 5899 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 5900 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5901 | |
| 5902 | In any case, the new value is |
| 5903 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5904 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5905 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5906 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 5907 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5908 | |
| 5909 | At this point, if |
| 5910 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5911 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5912 | |
| 5913 | 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] | 5914 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 5915 | 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] | 5916 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 5917 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 5918 | 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] | 5919 | the only spelling that makes sense on the local wall clock. |
| 5920 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5921 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 5922 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 5923 | difference between the LHS and RHS of [5]? Let |
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 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5926 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5927 | Now |
| 5928 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5929 | (y + y.s).n = by #5 |
| 5930 | y.n + y.s = since y.n = x.n |
| 5931 | x.n + y.s = since z and y are have the same tzinfo member, |
| 5932 | y.s = z.s by #2 |
| 5933 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5934 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5935 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5936 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5937 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5938 | x.n - ((x.n + z.s) - z.o) = expanding |
| 5939 | x.n - x.n - z.s + z.o = cancelling |
| 5940 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5941 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5942 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5943 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5944 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5945 | 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] | 5946 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 5947 | 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] | 5948 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5949 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 5950 | 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] | 5951 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5952 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5953 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5954 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5955 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5956 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5957 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5958 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5959 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5960 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5961 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 5962 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 5963 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 5964 | the justifications for the kinds of substitutions we've done several times |
| 5965 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5966 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5967 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5968 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 5969 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 5970 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 5971 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 5972 | - z.o + z'.o = #1 twice |
| 5973 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 5974 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5975 | |
| 5976 | 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] | 5977 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 5978 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5979 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5980 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 5981 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 5982 | would have to change the result dst() returns: we start in DST, and moving |
| 5983 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5984 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5985 | There isn't a sane case where this can happen. The closest it gets is at |
| 5986 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 5987 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 5988 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 5989 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 5990 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 5991 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 5992 | standard time. Since that's what the local clock *does*, we want to map both |
| 5993 | 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] | 5994 | in local time, but so it goes -- it's the way the local clock works. |
| 5995 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5996 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 5997 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 5998 | 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] | 5999 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 6000 | |
| 6001 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 6002 | 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] | 6003 | 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] | 6004 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 6005 | but the reasoning doesn't depend on the example -- it depends on there being |
| 6006 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 6007 | z' must be in standard time, and is the spelling we want in this case. |
| 6008 | |
| 6009 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 6010 | concerned (because it takes z' as being in standard time rather than the |
| 6011 | daylight time we intend here), but returning it gives the real-life "local |
| 6012 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 6013 | tz. |
| 6014 | |
| 6015 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 6016 | the 1:MM standard time spelling we want. |
| 6017 | |
| 6018 | So how can this break? One of the assumptions must be violated. Two |
| 6019 | possibilities: |
| 6020 | |
| 6021 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 6022 | time zone. This isn't true if, for political reasons or continental drift, |
| 6023 | a region decides to change its base offset from UTC. |
| 6024 | |
| 6025 | 2) There may be versions of "double daylight" time where the tail end of |
| 6026 | the analysis gives up a step too early. I haven't thought about that |
| 6027 | enough to say. |
| 6028 | |
| 6029 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 6030 | "almost all" time zones: so long as the standard offset is invariant, it |
| 6031 | doesn't matter if daylight time transition points change from year to year, or |
| 6032 | if daylight time is skipped in some years; it doesn't matter how large or |
| 6033 | small dst() may get within its bounds; and it doesn't even matter if some |
| 6034 | perverse time zone returns a negative dst()). So a breaking case must be |
| 6035 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 6036 | --------------------------------------------------------------------------- */ |