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 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 59 | |
| 60 | /* Date accessors for date and datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
| 62 | ((o)->data[1] = ((v) & 0x00ff))) |
| 63 | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
| 64 | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 65 | |
| 66 | /* Date/Time accessors for datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
| 68 | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
| 69 | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
| 70 | #define DATE_SET_MICROSECOND(o, v) \ |
| 71 | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
| 72 | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
| 73 | ((o)->data[9] = ((v) & 0x0000ff))) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 74 | |
| 75 | /* Time accessors for time. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
| 77 | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
| 78 | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
| 79 | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
| 80 | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
| 81 | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
| 82 | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
| 83 | #define TIME_SET_MICROSECOND(o, v) \ |
| 84 | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
| 85 | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
| 86 | ((o)->data[5] = ((v) & 0x0000ff))) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 87 | |
| 88 | /* Delta accessors for timedelta. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
| 90 | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
| 91 | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
| 94 | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 95 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
| 96 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 97 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
| 98 | * p->hastzinfo. |
| 99 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 100 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
| 101 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
| 102 | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
| 103 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
| 104 | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 105 | /* M is a char or int claiming to be a valid month. The macro is equivalent |
| 106 | * to the two-sided Python test |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | * 1 <= M <= 12 |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 108 | */ |
| 109 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
| 110 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 111 | /* Forward declarations. */ |
| 112 | static PyTypeObject PyDateTime_DateType; |
| 113 | static PyTypeObject PyDateTime_DateTimeType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 114 | static PyTypeObject PyDateTime_DeltaType; |
| 115 | static PyTypeObject PyDateTime_TimeType; |
| 116 | static PyTypeObject PyDateTime_TZInfoType; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 117 | static PyTypeObject PyDateTime_TimeZoneType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 118 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 119 | _Py_IDENTIFIER(as_integer_ratio); |
| 120 | _Py_IDENTIFIER(fromutc); |
| 121 | _Py_IDENTIFIER(isoformat); |
| 122 | _Py_IDENTIFIER(strftime); |
| 123 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 124 | /* --------------------------------------------------------------------------- |
| 125 | * Math utilities. |
| 126 | */ |
| 127 | |
| 128 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 129 | * iff k^i has sign bit set and k^j has sign bit set, |
| 130 | * iff (k^i)&(k^j) has sign bit set. |
| 131 | */ |
| 132 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 134 | |
| 135 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 136 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 137 | * the real point of this. C will probably truncate instead (C99 |
| 138 | * requires truncation; C89 left it implementation-defined). |
| 139 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 140 | * for all the uses made of it. This simplifies the code and makes |
| 141 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 142 | * overflow case). |
| 143 | */ |
| 144 | static int |
| 145 | divmod(int x, int y, int *r) |
| 146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | assert(y > 0); |
| 150 | quo = x / y; |
| 151 | *r = x - quo * y; |
| 152 | if (*r < 0) { |
| 153 | --quo; |
| 154 | *r += y; |
| 155 | } |
| 156 | assert(0 <= *r && *r < y); |
| 157 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 160 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 161 | * are rounded to even. |
| 162 | */ |
| 163 | static PyObject * |
| 164 | divide_nearest(PyObject *m, PyObject *n) |
| 165 | { |
| 166 | PyObject *result; |
| 167 | PyObject *temp; |
| 168 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 169 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 170 | if (temp == NULL) |
| 171 | return NULL; |
| 172 | result = PyTuple_GET_ITEM(temp, 0); |
| 173 | Py_INCREF(result); |
| 174 | Py_DECREF(temp); |
| 175 | |
| 176 | return result; |
| 177 | } |
| 178 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 179 | /* --------------------------------------------------------------------------- |
| 180 | * General calendrical helper functions |
| 181 | */ |
| 182 | |
| 183 | /* For each month ordinal in 1..12, the number of days in that month, |
| 184 | * and the number of days before that month in the same year. These |
| 185 | * are correct for non-leap years only. |
| 186 | */ |
| 187 | static int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | 0, /* unused; this vector uses 1-based indexing */ |
| 189 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | 0, /* unused; this vector uses 1-based indexing */ |
| 194 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /* year -> 1 if leap year, else 0. */ |
| 198 | static int |
| 199 | is_leap(int year) |
| 200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | /* Cast year to unsigned. The result is the same either way, but |
| 202 | * C can generate faster code for unsigned mod than for signed |
| 203 | * mod (especially for % 4 -- a good compiler should just grab |
| 204 | * the last 2 bits when the LHS is unsigned). |
| 205 | */ |
| 206 | const unsigned int ayear = (unsigned int)year; |
| 207 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* year, month -> number of days in that month in that year */ |
| 211 | static int |
| 212 | days_in_month(int year, int month) |
| 213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | assert(month >= 1); |
| 215 | assert(month <= 12); |
| 216 | if (month == 2 && is_leap(year)) |
| 217 | return 29; |
| 218 | else |
| 219 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* year, month -> number of days in year preceeding first day of month */ |
| 223 | static int |
| 224 | days_before_month(int year, int month) |
| 225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | assert(month >= 1); |
| 229 | assert(month <= 12); |
| 230 | days = _days_before_month[month]; |
| 231 | if (month > 2 && is_leap(year)) |
| 232 | ++days; |
| 233 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* year -> number of days before January 1st of year. Remember that we |
| 237 | * start with year 1, so days_before_year(1) == 0. |
| 238 | */ |
| 239 | static int |
| 240 | days_before_year(int year) |
| 241 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | int y = year - 1; |
| 243 | /* This is incorrect if year <= 0; we really want the floor |
| 244 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 245 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 247 | assert (year >= 1); |
| 248 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 252 | * the correct values is asserted in the module init function. |
| 253 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 255 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 256 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 257 | |
| 258 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 259 | static void |
| 260 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 265 | * leap years repeats exactly every 400 years. The basic strategy is |
| 266 | * to find the closest 400-year boundary at or before ordinal, then |
| 267 | * work with the offset from that boundary to ordinal. Life is much |
| 268 | * clearer if we subtract 1 from ordinal first -- then the values |
| 269 | * of ordinal at 400-year boundaries are exactly those divisible |
| 270 | * by DI400Y: |
| 271 | * |
| 272 | * D M Y n n-1 |
| 273 | * -- --- ---- ---------- ---------------- |
| 274 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 275 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 276 | * ... |
| 277 | * 30 Dec 000 -1 -2 |
| 278 | * 31 Dec 000 0 -1 |
| 279 | * 1 Jan 001 1 0 400-year boundary |
| 280 | * 2 Jan 001 2 1 |
| 281 | * 3 Jan 001 3 2 |
| 282 | * ... |
| 283 | * 31 Dec 400 DI400Y DI400Y -1 |
| 284 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 285 | */ |
| 286 | assert(ordinal >= 1); |
| 287 | --ordinal; |
| 288 | n400 = ordinal / DI400Y; |
| 289 | n = ordinal % DI400Y; |
| 290 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 293 | * year, to the desired date. Now compute how many 100-year cycles |
| 294 | * precede n. |
| 295 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 296 | * 100-year cycles precede the desired day, which implies the |
| 297 | * desired day is December 31 at the end of a 400-year cycle. |
| 298 | */ |
| 299 | n100 = n / DI100Y; |
| 300 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | /* Now compute how many 4-year cycles precede it. */ |
| 303 | n4 = n / DI4Y; |
| 304 | n = n % DI4Y; |
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 | /* And now how many single years. Again n1 can be 4, and again |
| 307 | * meaning that the desired day is December 31 at the end of the |
| 308 | * 4-year cycle. |
| 309 | */ |
| 310 | n1 = n / 365; |
| 311 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | *year += n100 * 100 + n4 * 4 + n1; |
| 314 | if (n1 == 4 || n100 == 4) { |
| 315 | assert(n == 0); |
| 316 | *year -= 1; |
| 317 | *month = 12; |
| 318 | *day = 31; |
| 319 | return; |
| 320 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | /* Now the year is correct, and n is the offset from January 1. We |
| 323 | * find the month via an estimate that's either exact or one too |
| 324 | * large. |
| 325 | */ |
| 326 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 327 | assert(leapyear == is_leap(*year)); |
| 328 | *month = (n + 50) >> 5; |
| 329 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 330 | if (preceding > n) { |
| 331 | /* estimate is too large */ |
| 332 | *month -= 1; |
| 333 | preceding -= days_in_month(*year, *month); |
| 334 | } |
| 335 | n -= preceding; |
| 336 | assert(0 <= n); |
| 337 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 343 | static int |
| 344 | ymd_to_ord(int year, int month, int day) |
| 345 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 350 | static int |
| 351 | weekday(int year, int month, int day) |
| 352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 357 | * first calendar week containing a Thursday. |
| 358 | */ |
| 359 | static int |
| 360 | iso_week1_monday(int year) |
| 361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 363 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 364 | int first_weekday = (first_day + 6) % 7; |
| 365 | /* ordinal of closest Monday at or before 1/1 */ |
| 366 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 369 | week1_monday += 7; |
| 370 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* --------------------------------------------------------------------------- |
| 374 | * Range checkers. |
| 375 | */ |
| 376 | |
| 377 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 378 | * If not, raise OverflowError and return -1. |
| 379 | */ |
| 380 | static int |
| 381 | check_delta_day_range(int days) |
| 382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 384 | return 0; |
| 385 | PyErr_Format(PyExc_OverflowError, |
| 386 | "days=%d; must have magnitude <= %d", |
| 387 | days, MAX_DELTA_DAYS); |
| 388 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 392 | * aren't, raise ValueError and return -1. |
| 393 | */ |
| 394 | static int |
| 395 | check_date_args(int year, int month, int day) |
| 396 | { |
| 397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | if (year < MINYEAR || year > MAXYEAR) { |
| 399 | PyErr_SetString(PyExc_ValueError, |
| 400 | "year is out of range"); |
| 401 | return -1; |
| 402 | } |
| 403 | if (month < 1 || month > 12) { |
| 404 | PyErr_SetString(PyExc_ValueError, |
| 405 | "month must be in 1..12"); |
| 406 | return -1; |
| 407 | } |
| 408 | if (day < 1 || day > days_in_month(year, month)) { |
| 409 | PyErr_SetString(PyExc_ValueError, |
| 410 | "day is out of range for month"); |
| 411 | return -1; |
| 412 | } |
| 413 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 417 | * aren't, raise ValueError and return -1. |
| 418 | */ |
| 419 | static int |
| 420 | check_time_args(int h, int m, int s, int us) |
| 421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | if (h < 0 || h > 23) { |
| 423 | PyErr_SetString(PyExc_ValueError, |
| 424 | "hour must be in 0..23"); |
| 425 | return -1; |
| 426 | } |
| 427 | if (m < 0 || m > 59) { |
| 428 | PyErr_SetString(PyExc_ValueError, |
| 429 | "minute must be in 0..59"); |
| 430 | return -1; |
| 431 | } |
| 432 | if (s < 0 || s > 59) { |
| 433 | PyErr_SetString(PyExc_ValueError, |
| 434 | "second must be in 0..59"); |
| 435 | return -1; |
| 436 | } |
| 437 | if (us < 0 || us > 999999) { |
| 438 | PyErr_SetString(PyExc_ValueError, |
| 439 | "microsecond must be in 0..999999"); |
| 440 | return -1; |
| 441 | } |
| 442 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* --------------------------------------------------------------------------- |
| 446 | * Normalization utilities. |
| 447 | */ |
| 448 | |
| 449 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 450 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 451 | * at least factor, enough of *lo is converted into "hi" units so that |
| 452 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 453 | * is impossible. |
| 454 | */ |
| 455 | static void |
| 456 | normalize_pair(int *hi, int *lo, int factor) |
| 457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | assert(factor > 0); |
| 459 | assert(lo != hi); |
| 460 | if (*lo < 0 || *lo >= factor) { |
| 461 | const int num_hi = divmod(*lo, factor, lo); |
| 462 | const int new_hi = *hi + num_hi; |
| 463 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 464 | *hi = new_hi; |
| 465 | } |
| 466 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | * 0 <= *s < 24*3600 |
| 471 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 472 | * The input values must be such that the internals don't overflow. |
| 473 | * The way this routine is used, we don't get close. |
| 474 | */ |
| 475 | static void |
| 476 | normalize_d_s_us(int *d, int *s, int *us) |
| 477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | if (*us < 0 || *us >= 1000000) { |
| 479 | normalize_pair(s, us, 1000000); |
| 480 | /* |s| can't be bigger than about |
| 481 | * |original s| + |original us|/1000000 now. |
| 482 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | } |
| 485 | if (*s < 0 || *s >= 24*3600) { |
| 486 | normalize_pair(d, s, 24*3600); |
| 487 | /* |d| can't be bigger than about |
| 488 | * |original d| + |
| 489 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 490 | */ |
| 491 | } |
| 492 | assert(0 <= *s && *s < 24*3600); |
| 493 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | * 1 <= *m <= 12 |
| 498 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 499 | * The input values must be such that the internals don't overflow. |
| 500 | * The way this routine is used, we don't get close. |
| 501 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 502 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 503 | normalize_y_m_d(int *y, int *m, int *d) |
| 504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 506 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 507 | /* In actual use, m is always the month component extracted from a |
| 508 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | /* Now only day can be out of bounds (year may also be out of bounds |
| 514 | * for a datetime object, but we don't care about that here). |
| 515 | * If day is out of bounds, what to do is arguable, but at least the |
| 516 | * method here is principled and explainable. |
| 517 | */ |
| 518 | dim = days_in_month(*y, *m); |
| 519 | if (*d < 1 || *d > dim) { |
| 520 | /* Move day-1 days from the first of the month. First try to |
| 521 | * get off cheap if we're only one day out of range |
| 522 | * (adjustments for timezone alone can't be worse than that). |
| 523 | */ |
| 524 | if (*d == 0) { |
| 525 | --*m; |
| 526 | if (*m > 0) |
| 527 | *d = days_in_month(*y, *m); |
| 528 | else { |
| 529 | --*y; |
| 530 | *m = 12; |
| 531 | *d = 31; |
| 532 | } |
| 533 | } |
| 534 | else if (*d == dim + 1) { |
| 535 | /* move forward a day */ |
| 536 | ++*m; |
| 537 | *d = 1; |
| 538 | if (*m > 12) { |
| 539 | *m = 1; |
| 540 | ++*y; |
| 541 | } |
| 542 | } |
| 543 | else { |
| 544 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 545 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 546 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 547 | goto error; |
| 548 | } else { |
| 549 | ord_to_ymd(ordinal, y, m, d); |
| 550 | return 0; |
| 551 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | assert(*m > 0); |
| 555 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 556 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 557 | return 0; |
| 558 | error: |
| 559 | PyErr_SetString(PyExc_OverflowError, |
| 560 | "date value out of range"); |
| 561 | return -1; |
| 562 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 566 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 567 | * failure, where failure means the adjusted year is out of bounds. |
| 568 | */ |
| 569 | static int |
| 570 | normalize_date(int *year, int *month, int *day) |
| 571 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 572 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* Force all the datetime fields into range. The parameters are both |
| 576 | * inputs and outputs. Returns < 0 on error. |
| 577 | */ |
| 578 | static int |
| 579 | normalize_datetime(int *year, int *month, int *day, |
| 580 | int *hour, int *minute, int *second, |
| 581 | int *microsecond) |
| 582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | normalize_pair(second, microsecond, 1000000); |
| 584 | normalize_pair(minute, second, 60); |
| 585 | normalize_pair(hour, minute, 60); |
| 586 | normalize_pair(day, hour, 24); |
| 587 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 591 | * Basic object allocation: tp_alloc implementations. These allocate |
| 592 | * Python objects of the right size and type, and do the Python object- |
| 593 | * initialization bit. If there's not enough memory, they return NULL after |
| 594 | * setting MemoryError. All data members remain uninitialized trash. |
| 595 | * |
| 596 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 597 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 598 | * tp_basicsize for the time and datetime types is set to the size of the |
| 599 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 600 | * allocate enough space for a tzinfo member whether or not one is actually |
| 601 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 602 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 603 | * using) just happens today to effectively ignore the nitems argument |
| 604 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 605 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 606 | * be changed to force a 0 nitems argument unless the type being allocated |
| 607 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 608 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 609 | */ |
| 610 | |
| 611 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 612 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | self = (PyObject *) |
| 617 | PyObject_MALLOC(aware ? |
| 618 | sizeof(PyDateTime_Time) : |
| 619 | sizeof(_PyDateTime_BaseTime)); |
| 620 | if (self == NULL) |
| 621 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 622 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 627 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | self = (PyObject *) |
| 632 | PyObject_MALLOC(aware ? |
| 633 | sizeof(PyDateTime_DateTime) : |
| 634 | sizeof(_PyDateTime_BaseDateTime)); |
| 635 | if (self == NULL) |
| 636 | return (PyObject *)PyErr_NoMemory(); |
Christian Heimes | ecb4e6a | 2013-12-04 09:34:29 +0100 | [diff] [blame] | 637 | (void)PyObject_INIT(self, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* --------------------------------------------------------------------------- |
| 642 | * Helpers for setting object fields. These work on pointers to the |
| 643 | * appropriate base class. |
| 644 | */ |
| 645 | |
| 646 | /* For date and datetime. */ |
| 647 | static void |
| 648 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | self->hashcode = -1; |
| 651 | SET_YEAR(self, y); |
| 652 | SET_MONTH(self, m); |
| 653 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /* --------------------------------------------------------------------------- |
| 657 | * Create various objects, mostly without range checking. |
| 658 | */ |
| 659 | |
| 660 | /* Create a date instance with no range checking. */ |
| 661 | static PyObject * |
| 662 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 667 | if (self != NULL) |
| 668 | set_date_fields(self, year, month, day); |
| 669 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 674 | |
| 675 | /* Create a datetime instance with no range checking. */ |
| 676 | static PyObject * |
| 677 | new_datetime_ex(int year, int month, int day, int hour, int minute, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | PyDateTime_DateTime *self; |
| 681 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 684 | if (self != NULL) { |
| 685 | self->hastzinfo = aware; |
| 686 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 687 | DATE_SET_HOUR(self, hour); |
| 688 | DATE_SET_MINUTE(self, minute); |
| 689 | DATE_SET_SECOND(self, second); |
| 690 | DATE_SET_MICROSECOND(self, usecond); |
| 691 | if (aware) { |
| 692 | Py_INCREF(tzinfo); |
| 693 | self->tzinfo = tzinfo; |
| 694 | } |
| 695 | } |
| 696 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \ |
| 700 | new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \ |
| 701 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 702 | |
| 703 | /* Create a time instance with no range checking. */ |
| 704 | static PyObject * |
| 705 | new_time_ex(int hour, int minute, int second, int usecond, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 707 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | PyDateTime_Time *self; |
| 709 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 712 | if (self != NULL) { |
| 713 | self->hastzinfo = aware; |
| 714 | self->hashcode = -1; |
| 715 | TIME_SET_HOUR(self, hour); |
| 716 | TIME_SET_MINUTE(self, minute); |
| 717 | TIME_SET_SECOND(self, second); |
| 718 | TIME_SET_MICROSECOND(self, usecond); |
| 719 | if (aware) { |
| 720 | Py_INCREF(tzinfo); |
| 721 | self->tzinfo = tzinfo; |
| 722 | } |
| 723 | } |
| 724 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 727 | #define new_time(hh, mm, ss, us, tzinfo) \ |
| 728 | new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 729 | |
| 730 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 731 | * true. Passing false is a speed optimization, if you know for sure |
| 732 | * that seconds and microseconds are already in their proper ranges. In any |
| 733 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 734 | * of range). |
| 735 | */ |
| 736 | static PyObject * |
| 737 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | if (normalize) |
| 743 | normalize_d_s_us(&days, &seconds, µseconds); |
| 744 | assert(0 <= seconds && seconds < 24*3600); |
| 745 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | if (check_delta_day_range(days) < 0) |
| 748 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 751 | if (self != NULL) { |
| 752 | self->hashcode = -1; |
| 753 | SET_TD_DAYS(self, days); |
| 754 | SET_TD_SECONDS(self, seconds); |
| 755 | SET_TD_MICROSECONDS(self, microseconds); |
| 756 | } |
| 757 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | #define new_delta(d, s, us, normalize) \ |
| 761 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 762 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 763 | |
| 764 | typedef struct |
| 765 | { |
| 766 | PyObject_HEAD |
| 767 | PyObject *offset; |
| 768 | PyObject *name; |
| 769 | } PyDateTime_TimeZone; |
| 770 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 771 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 772 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 773 | /* The interned Epoch datetime instance */ |
| 774 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 775 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 776 | /* Create new timezone instance checking offset range. This |
| 777 | function does not check the name argument. Caller must assure |
| 778 | that offset is a timedelta instance and name is either NULL |
| 779 | or a unicode object. */ |
| 780 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 781 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 782 | { |
| 783 | PyDateTime_TimeZone *self; |
| 784 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 785 | |
| 786 | assert(offset != NULL); |
| 787 | assert(PyDelta_Check(offset)); |
| 788 | assert(name == NULL || PyUnicode_Check(name)); |
| 789 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 790 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 791 | if (self == NULL) { |
| 792 | return NULL; |
| 793 | } |
| 794 | Py_INCREF(offset); |
| 795 | self->offset = offset; |
| 796 | Py_XINCREF(name); |
| 797 | self->name = name; |
| 798 | return (PyObject *)self; |
| 799 | } |
| 800 | |
| 801 | static int delta_bool(PyDateTime_Delta *self); |
| 802 | |
| 803 | static PyObject * |
| 804 | new_timezone(PyObject *offset, PyObject *name) |
| 805 | { |
| 806 | assert(offset != NULL); |
| 807 | assert(PyDelta_Check(offset)); |
| 808 | assert(name == NULL || PyUnicode_Check(name)); |
| 809 | |
| 810 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 811 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 812 | return PyDateTime_TimeZone_UTC; |
| 813 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 814 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 815 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 816 | " representing a whole number of minutes," |
| 817 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 818 | return NULL; |
| 819 | } |
| 820 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 821 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 822 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 823 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 824 | " timedelta(hours=24)," |
| 825 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 826 | return NULL; |
| 827 | } |
| 828 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 829 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 832 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 833 | * tzinfo helpers. |
| 834 | */ |
| 835 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 836 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 837 | * raise TypeError and return -1. |
| 838 | */ |
| 839 | static int |
| 840 | check_tzinfo_subclass(PyObject *p) |
| 841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | if (p == Py_None || PyTZInfo_Check(p)) |
| 843 | return 0; |
| 844 | PyErr_Format(PyExc_TypeError, |
| 845 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 846 | "not type '%s'", |
| 847 | Py_TYPE(p)->tp_name); |
| 848 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 851 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 852 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 853 | * and the caller must not decref the result. |
| 854 | */ |
| 855 | static PyObject * |
| 856 | get_tzinfo_member(PyObject *self) |
| 857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 861 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 862 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 863 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 868 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 869 | * be an instance of the tzinfo class. If the method returns None, this |
| 870 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 871 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 872 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 873 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 874 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 875 | static PyObject * |
| 876 | call_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 877 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 878 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 881 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 883 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 884 | if (tzinfo == Py_None) |
| 885 | Py_RETURN_NONE; |
| 886 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 887 | if (offset == Py_None || offset == NULL) |
| 888 | return offset; |
| 889 | if (PyDelta_Check(offset)) { |
| 890 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 891 | Py_DECREF(offset); |
| 892 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 893 | " representing a whole number of minutes"); |
| 894 | return NULL; |
| 895 | } |
| 896 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 897 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 898 | Py_DECREF(offset); |
| 899 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 900 | " strictly between -timedelta(hours=24) and" |
| 901 | " timedelta(hours=24)."); |
| 902 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | else { |
| 906 | PyErr_Format(PyExc_TypeError, |
| 907 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 908 | "timedelta, not '%.200s'", |
| 909 | name, Py_TYPE(offset)->tp_name); |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 910 | Py_DECREF(offset); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 911 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 913 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 914 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 918 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 919 | * 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] | 920 | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
| 921 | * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
| 922 | * # of minutes), ValueError is raised and this returns -1. Else *none is |
| 923 | * 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] | 924 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 925 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 926 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 927 | { |
| 928 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 931 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 932 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 933 | * 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] | 934 | & doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 935 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 936 | * ValueError is raised and this returns -1. Else *none is set to 0 and |
| 937 | * the offset is returned (as an int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 938 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 939 | static PyObject * |
| 940 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 941 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 942 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 945 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 946 | * 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] | 947 | * 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] | 948 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 949 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 950 | */ |
| 951 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 952 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 955 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | assert(tzinfo != NULL); |
| 958 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 959 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 962 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 963 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 964 | result = _PyObject_CallMethodId(tzinfo, &PyId_tzname, "O", tzinfoarg); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 965 | |
| 966 | if (result == NULL || result == Py_None) |
| 967 | return result; |
| 968 | |
| 969 | if (!PyUnicode_Check(result)) { |
| 970 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 971 | "return None or a string, not '%s'", |
| 972 | Py_TYPE(result)->tp_name); |
| 973 | Py_DECREF(result); |
| 974 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 976 | |
| 977 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 980 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 981 | * stuff |
| 982 | * ", tzinfo=" + repr(tzinfo) |
| 983 | * before the closing ")". |
| 984 | */ |
| 985 | static PyObject * |
| 986 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | assert(PyUnicode_Check(repr)); |
| 991 | assert(tzinfo); |
| 992 | if (tzinfo == Py_None) |
| 993 | return repr; |
| 994 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 995 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 996 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | Py_DECREF(repr); |
| 998 | if (temp == NULL) |
| 999 | return NULL; |
| 1000 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 1001 | Py_DECREF(temp); |
| 1002 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* --------------------------------------------------------------------------- |
| 1006 | * String format helpers. |
| 1007 | */ |
| 1008 | |
| 1009 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 1010 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1011 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | static const char *DayNames[] = { |
| 1013 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1014 | }; |
| 1015 | static const char *MonthNames[] = { |
| 1016 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1017 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1018 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1023 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1024 | GET_DAY(date), hours, minutes, seconds, |
| 1025 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1028 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1029 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1030 | /* Add an hours & minutes UTC offset string to buf. buf has no more than |
| 1031 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1032 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1033 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1034 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1035 | * string of the form |
| 1036 | * sign HH sep MM |
| 1037 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1038 | * bogus, an appropriate exception is set and -1 is returned. |
| 1039 | */ |
| 1040 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1041 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1043 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1044 | PyObject *offset; |
| 1045 | int hours, minutes, seconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1049 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1050 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1051 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1053 | if (offset == Py_None) { |
| 1054 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | *buf = '\0'; |
| 1056 | return 0; |
| 1057 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1058 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1059 | if (GET_TD_DAYS(offset) < 0) { |
| 1060 | PyObject *temp = offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | sign = '-'; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1062 | offset = delta_negative((PyDateTime_Delta *)offset); |
| 1063 | Py_DECREF(temp); |
| 1064 | if (offset == NULL) |
| 1065 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1067 | else { |
| 1068 | sign = '+'; |
| 1069 | } |
| 1070 | /* Offset is not negative here. */ |
| 1071 | seconds = GET_TD_SECONDS(offset); |
| 1072 | Py_DECREF(offset); |
| 1073 | minutes = divmod(seconds, 60, &seconds); |
| 1074 | hours = divmod(minutes, 60, &minutes); |
| 1075 | assert(seconds == 0); |
| 1076 | /* XXX ignore sub-minute data, curently not allowed. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1082 | static PyObject * |
| 1083 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | PyObject *temp; |
| 1086 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1087 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1088 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | if (Zreplacement == NULL) |
| 1091 | return NULL; |
| 1092 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1093 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | assert(tzinfoarg != NULL); |
| 1096 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1097 | if (temp == NULL) |
| 1098 | goto Error; |
| 1099 | if (temp == Py_None) { |
| 1100 | Py_DECREF(temp); |
| 1101 | return Zreplacement; |
| 1102 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | assert(PyUnicode_Check(temp)); |
| 1105 | /* Since the tzname is getting stuffed into the |
| 1106 | * format, we have to double any % signs so that |
| 1107 | * strftime doesn't treat them as format codes. |
| 1108 | */ |
| 1109 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1110 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | Py_DECREF(temp); |
| 1112 | if (Zreplacement == NULL) |
| 1113 | return NULL; |
| 1114 | if (!PyUnicode_Check(Zreplacement)) { |
| 1115 | PyErr_SetString(PyExc_TypeError, |
| 1116 | "tzname.replace() did not return a string"); |
| 1117 | goto Error; |
| 1118 | } |
| 1119 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1120 | |
| 1121 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | Py_DECREF(Zreplacement); |
| 1123 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1126 | static PyObject * |
| 1127 | make_freplacement(PyObject *object) |
| 1128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | char freplacement[64]; |
| 1130 | if (PyTime_Check(object)) |
| 1131 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1132 | else if (PyDateTime_Check(object)) |
| 1133 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1134 | else |
| 1135 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1140 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1141 | * 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] | 1142 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1143 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1144 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1145 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1146 | */ |
| 1147 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1148 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1154 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1155 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | const char *pin; /* pointer to next char in input format */ |
| 1158 | Py_ssize_t flen; /* length of input format */ |
| 1159 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1162 | char *pnew; /* pointer to available byte in output format */ |
| 1163 | size_t totalnew; /* number bytes total in output format buffer, |
| 1164 | exclusive of trailing \0 */ |
| 1165 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1168 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | assert(object && format && timetuple); |
| 1171 | assert(PyUnicode_Check(format)); |
| 1172 | /* Convert the input format to a C string and size */ |
| 1173 | pin = _PyUnicode_AsStringAndSize(format, &flen); |
| 1174 | if (!pin) |
| 1175 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1178 | * a new format. Since computing the replacements for those codes |
| 1179 | * is expensive, don't unless they're actually used. |
| 1180 | */ |
| 1181 | if (flen > INT_MAX - 1) { |
| 1182 | PyErr_NoMemory(); |
| 1183 | goto Done; |
| 1184 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1187 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1188 | if (newfmt == NULL) goto Done; |
| 1189 | pnew = PyBytes_AsString(newfmt); |
| 1190 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | while ((ch = *pin++) != '\0') { |
| 1193 | if (ch != '%') { |
| 1194 | ptoappend = pin - 1; |
| 1195 | ntoappend = 1; |
| 1196 | } |
| 1197 | else if ((ch = *pin++) == '\0') { |
| 1198 | /* There's a lone trailing %; doesn't make sense. */ |
| 1199 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1200 | "ends with raw %"); |
| 1201 | goto Done; |
| 1202 | } |
| 1203 | /* A % has been seen and ch is the character after it. */ |
| 1204 | else if (ch == 'z') { |
| 1205 | if (zreplacement == NULL) { |
| 1206 | /* format utcoffset */ |
| 1207 | char buf[100]; |
| 1208 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1209 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1210 | if (zreplacement == NULL) goto Done; |
| 1211 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1212 | assert(tzinfoarg != NULL); |
| 1213 | if (format_utcoffset(buf, |
| 1214 | sizeof(buf), |
| 1215 | "", |
| 1216 | tzinfo, |
| 1217 | tzinfoarg) < 0) |
| 1218 | goto Done; |
| 1219 | Py_DECREF(zreplacement); |
| 1220 | zreplacement = |
| 1221 | PyBytes_FromStringAndSize(buf, |
| 1222 | strlen(buf)); |
| 1223 | if (zreplacement == NULL) |
| 1224 | goto Done; |
| 1225 | } |
| 1226 | } |
| 1227 | assert(zreplacement != NULL); |
| 1228 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1229 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1230 | } |
| 1231 | else if (ch == 'Z') { |
| 1232 | /* format tzname */ |
| 1233 | if (Zreplacement == NULL) { |
| 1234 | Zreplacement = make_Zreplacement(object, |
| 1235 | tzinfoarg); |
| 1236 | if (Zreplacement == NULL) |
| 1237 | goto Done; |
| 1238 | } |
| 1239 | assert(Zreplacement != NULL); |
| 1240 | assert(PyUnicode_Check(Zreplacement)); |
| 1241 | ptoappend = _PyUnicode_AsStringAndSize(Zreplacement, |
| 1242 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1243 | if (ptoappend == NULL) |
| 1244 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | } |
| 1246 | else if (ch == 'f') { |
| 1247 | /* format microseconds */ |
| 1248 | if (freplacement == NULL) { |
| 1249 | freplacement = make_freplacement(object); |
| 1250 | if (freplacement == NULL) |
| 1251 | goto Done; |
| 1252 | } |
| 1253 | assert(freplacement != NULL); |
| 1254 | assert(PyBytes_Check(freplacement)); |
| 1255 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1256 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1257 | } |
| 1258 | else { |
| 1259 | /* percent followed by neither z nor Z */ |
| 1260 | ptoappend = pin - 2; |
| 1261 | ntoappend = 2; |
| 1262 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | /* Append the ntoappend chars starting at ptoappend to |
| 1265 | * the new format. |
| 1266 | */ |
| 1267 | if (ntoappend == 0) |
| 1268 | continue; |
| 1269 | assert(ptoappend != NULL); |
| 1270 | assert(ntoappend > 0); |
| 1271 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1272 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | PyErr_NoMemory(); |
| 1274 | goto Done; |
| 1275 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1276 | totalnew <<= 1; |
| 1277 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1280 | } |
| 1281 | memcpy(pnew, ptoappend, ntoappend); |
| 1282 | pnew += ntoappend; |
| 1283 | usednew += ntoappend; |
| 1284 | assert(usednew <= totalnew); |
| 1285 | } /* end while() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1288 | goto Done; |
| 1289 | { |
| 1290 | PyObject *format; |
| 1291 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | if (time == NULL) |
| 1294 | goto Done; |
| 1295 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1296 | if (format != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1297 | result = _PyObject_CallMethodId(time, &PyId_strftime, "OO", |
| 1298 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | Py_DECREF(format); |
| 1300 | } |
| 1301 | Py_DECREF(time); |
| 1302 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1303 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1304 | Py_XDECREF(freplacement); |
| 1305 | Py_XDECREF(zreplacement); |
| 1306 | Py_XDECREF(Zreplacement); |
| 1307 | Py_XDECREF(newfmt); |
| 1308 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1311 | /* --------------------------------------------------------------------------- |
| 1312 | * Wrap functions from the time module. These aren't directly available |
| 1313 | * from C. Perhaps they should be. |
| 1314 | */ |
| 1315 | |
| 1316 | /* Call time.time() and return its result (a Python float). */ |
| 1317 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1318 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | PyObject *result = NULL; |
| 1321 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1324 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1325 | |
| 1326 | result = _PyObject_CallMethodId(time, &PyId_time, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | Py_DECREF(time); |
| 1328 | } |
| 1329 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1333 | * computed from the y,m,d args. |
| 1334 | */ |
| 1335 | static PyObject * |
| 1336 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1337 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1338 | PyObject *time; |
| 1339 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | time = PyImport_ImportModuleNoBlock("time"); |
| 1342 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1343 | _Py_IDENTIFIER(struct_time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1344 | |
| 1345 | result = _PyObject_CallMethodId(time, &PyId_struct_time, |
| 1346 | "((iiiiiiiii))", |
| 1347 | y, m, d, |
| 1348 | hh, mm, ss, |
| 1349 | weekday(y, m, d), |
| 1350 | days_before_month(y, m) + d, |
| 1351 | dstflag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | Py_DECREF(time); |
| 1353 | } |
| 1354 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | /* --------------------------------------------------------------------------- |
| 1358 | * Miscellaneous helpers. |
| 1359 | */ |
| 1360 | |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1361 | /* 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] | 1362 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1363 | * This little helper turns that into a bool result for rich comparisons. |
| 1364 | */ |
| 1365 | static PyObject * |
| 1366 | diff_to_bool(int diff, int op) |
| 1367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | PyObject *result; |
| 1369 | int istrue; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1371 | switch (op) { |
| 1372 | case Py_EQ: istrue = diff == 0; break; |
| 1373 | case Py_NE: istrue = diff != 0; break; |
| 1374 | case Py_LE: istrue = diff <= 0; break; |
| 1375 | case Py_GE: istrue = diff >= 0; break; |
| 1376 | case Py_LT: istrue = diff < 0; break; |
| 1377 | case Py_GT: istrue = diff > 0; break; |
| 1378 | default: |
| 1379 | assert(! "op unknown"); |
| 1380 | istrue = 0; /* To shut up compiler */ |
| 1381 | } |
| 1382 | result = istrue ? Py_True : Py_False; |
| 1383 | Py_INCREF(result); |
| 1384 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1387 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1388 | static PyObject * |
| 1389 | cmperror(PyObject *a, PyObject *b) |
| 1390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | PyErr_Format(PyExc_TypeError, |
| 1392 | "can't compare %s to %s", |
| 1393 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1394 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1397 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1398 | * Cached Python objects; these are set by the module init function. |
| 1399 | */ |
| 1400 | |
| 1401 | /* Conversion factors. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 1402 | static PyObject *one = NULL; /* 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1404 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1405 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1406 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1407 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1408 | 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] | 1409 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1410 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1411 | /* --------------------------------------------------------------------------- |
| 1412 | * Class implementations. |
| 1413 | */ |
| 1414 | |
| 1415 | /* |
| 1416 | * PyDateTime_Delta implementation. |
| 1417 | */ |
| 1418 | |
| 1419 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1421 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1422 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1423 | * due to ubiquitous overflow possibilities. |
| 1424 | */ |
| 1425 | static PyObject * |
| 1426 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1427 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | PyObject *x1 = NULL; |
| 1429 | PyObject *x2 = NULL; |
| 1430 | PyObject *x3 = NULL; |
| 1431 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1434 | if (x1 == NULL) |
| 1435 | goto Done; |
| 1436 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1437 | if (x2 == NULL) |
| 1438 | goto Done; |
| 1439 | Py_DECREF(x1); |
| 1440 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | /* x2 has days in seconds */ |
| 1443 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1444 | if (x1 == NULL) |
| 1445 | goto Done; |
| 1446 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1447 | if (x3 == NULL) |
| 1448 | goto Done; |
| 1449 | Py_DECREF(x1); |
| 1450 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1451 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | /* x3 has days+seconds in seconds */ |
| 1454 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1455 | if (x1 == NULL) |
| 1456 | goto Done; |
| 1457 | Py_DECREF(x3); |
| 1458 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | /* x1 has days+seconds in us */ |
| 1461 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1462 | if (x2 == NULL) |
| 1463 | goto Done; |
| 1464 | result = PyNumber_Add(x1, x2); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1465 | |
| 1466 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | Py_XDECREF(x1); |
| 1468 | Py_XDECREF(x2); |
| 1469 | Py_XDECREF(x3); |
| 1470 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1473 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1474 | */ |
| 1475 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1476 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | int us; |
| 1479 | int s; |
| 1480 | int d; |
| 1481 | long temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | PyObject *tuple = NULL; |
| 1484 | PyObject *num = NULL; |
| 1485 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1488 | if (tuple == NULL) |
| 1489 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1492 | if (num == NULL) |
| 1493 | goto Done; |
| 1494 | temp = PyLong_AsLong(num); |
| 1495 | num = NULL; |
| 1496 | if (temp == -1 && PyErr_Occurred()) |
| 1497 | goto Done; |
| 1498 | assert(0 <= temp && temp < 1000000); |
| 1499 | us = (int)temp; |
| 1500 | if (us < 0) { |
| 1501 | /* The divisor was positive, so this must be an error. */ |
| 1502 | assert(PyErr_Occurred()); |
| 1503 | goto Done; |
| 1504 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1507 | if (num == NULL) |
| 1508 | goto Done; |
| 1509 | Py_INCREF(num); |
| 1510 | Py_DECREF(tuple); |
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 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1513 | if (tuple == NULL) |
| 1514 | goto Done; |
| 1515 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1518 | if (num == NULL) |
| 1519 | goto Done; |
| 1520 | temp = PyLong_AsLong(num); |
| 1521 | num = NULL; |
| 1522 | if (temp == -1 && PyErr_Occurred()) |
| 1523 | goto Done; |
| 1524 | assert(0 <= temp && temp < 24*3600); |
| 1525 | s = (int)temp; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | if (s < 0) { |
| 1528 | /* The divisor was positive, so this must be an error. */ |
| 1529 | assert(PyErr_Occurred()); |
| 1530 | goto Done; |
| 1531 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1534 | if (num == NULL) |
| 1535 | goto Done; |
| 1536 | Py_INCREF(num); |
| 1537 | temp = PyLong_AsLong(num); |
| 1538 | if (temp == -1 && PyErr_Occurred()) |
| 1539 | goto Done; |
| 1540 | d = (int)temp; |
| 1541 | if ((long)d != temp) { |
| 1542 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1543 | "large to fit in a C int"); |
| 1544 | goto Done; |
| 1545 | } |
| 1546 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1547 | |
| 1548 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | Py_XDECREF(tuple); |
| 1550 | Py_XDECREF(num); |
| 1551 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1554 | #define microseconds_to_delta(pymicros) \ |
| 1555 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1556 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1557 | static PyObject * |
| 1558 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | PyObject *pyus_in; |
| 1561 | PyObject *pyus_out; |
| 1562 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | pyus_in = delta_to_microseconds(delta); |
| 1565 | if (pyus_in == NULL) |
| 1566 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1569 | Py_DECREF(pyus_in); |
| 1570 | if (pyus_out == NULL) |
| 1571 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | result = microseconds_to_delta(pyus_out); |
| 1574 | Py_DECREF(pyus_out); |
| 1575 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1579 | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) |
| 1580 | { |
| 1581 | PyObject *result = NULL; |
| 1582 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1583 | PyObject *ratio = NULL; |
| 1584 | |
| 1585 | pyus_in = delta_to_microseconds(delta); |
| 1586 | if (pyus_in == NULL) |
| 1587 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1588 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1589 | if (ratio == NULL) |
| 1590 | goto error; |
| 1591 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); |
| 1592 | Py_DECREF(pyus_in); |
| 1593 | pyus_in = NULL; |
| 1594 | if (temp == NULL) |
| 1595 | goto error; |
| 1596 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); |
| 1597 | Py_DECREF(temp); |
| 1598 | if (pyus_out == NULL) |
| 1599 | goto error; |
| 1600 | result = microseconds_to_delta(pyus_out); |
| 1601 | Py_DECREF(pyus_out); |
| 1602 | error: |
| 1603 | Py_XDECREF(pyus_in); |
| 1604 | Py_XDECREF(ratio); |
| 1605 | |
| 1606 | return result; |
| 1607 | } |
| 1608 | |
| 1609 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1610 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | PyObject *pyus_in; |
| 1613 | PyObject *pyus_out; |
| 1614 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | pyus_in = delta_to_microseconds(delta); |
| 1617 | if (pyus_in == NULL) |
| 1618 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1621 | Py_DECREF(pyus_in); |
| 1622 | if (pyus_out == NULL) |
| 1623 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | result = microseconds_to_delta(pyus_out); |
| 1626 | Py_DECREF(pyus_out); |
| 1627 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1631 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | PyObject *pyus_left; |
| 1634 | PyObject *pyus_right; |
| 1635 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | pyus_left = delta_to_microseconds(left); |
| 1638 | if (pyus_left == NULL) |
| 1639 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | pyus_right = delta_to_microseconds(right); |
| 1642 | if (pyus_right == NULL) { |
| 1643 | Py_DECREF(pyus_left); |
| 1644 | return NULL; |
| 1645 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1648 | Py_DECREF(pyus_left); |
| 1649 | Py_DECREF(pyus_right); |
| 1650 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | static PyObject * |
| 1654 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | PyObject *pyus_left; |
| 1657 | PyObject *pyus_right; |
| 1658 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | pyus_left = delta_to_microseconds(left); |
| 1661 | if (pyus_left == NULL) |
| 1662 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | pyus_right = delta_to_microseconds(right); |
| 1665 | if (pyus_right == NULL) { |
| 1666 | Py_DECREF(pyus_left); |
| 1667 | return NULL; |
| 1668 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1671 | Py_DECREF(pyus_left); |
| 1672 | Py_DECREF(pyus_right); |
| 1673 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1677 | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) |
| 1678 | { |
| 1679 | PyObject *result = NULL; |
| 1680 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1681 | PyObject *ratio = NULL; |
| 1682 | |
| 1683 | pyus_in = delta_to_microseconds(delta); |
| 1684 | if (pyus_in == NULL) |
| 1685 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1686 | ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1687 | if (ratio == NULL) |
| 1688 | goto error; |
| 1689 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1)); |
| 1690 | Py_DECREF(pyus_in); |
| 1691 | pyus_in = NULL; |
| 1692 | if (temp == NULL) |
| 1693 | goto error; |
| 1694 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0)); |
| 1695 | Py_DECREF(temp); |
| 1696 | if (pyus_out == NULL) |
| 1697 | goto error; |
| 1698 | result = microseconds_to_delta(pyus_out); |
| 1699 | Py_DECREF(pyus_out); |
| 1700 | error: |
| 1701 | Py_XDECREF(pyus_in); |
| 1702 | Py_XDECREF(ratio); |
| 1703 | |
| 1704 | return result; |
| 1705 | } |
| 1706 | |
| 1707 | static PyObject * |
| 1708 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 1709 | { |
| 1710 | PyObject *result; |
| 1711 | PyObject *pyus_in, *pyus_out; |
| 1712 | pyus_in = delta_to_microseconds(delta); |
| 1713 | if (pyus_in == NULL) |
| 1714 | return NULL; |
| 1715 | pyus_out = divide_nearest(pyus_in, i); |
| 1716 | Py_DECREF(pyus_in); |
| 1717 | if (pyus_out == NULL) |
| 1718 | return NULL; |
| 1719 | result = microseconds_to_delta(pyus_out); |
| 1720 | Py_DECREF(pyus_out); |
| 1721 | |
| 1722 | return result; |
| 1723 | } |
| 1724 | |
| 1725 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1726 | delta_add(PyObject *left, PyObject *right) |
| 1727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1731 | /* delta + delta */ |
| 1732 | /* The C-level additions can't overflow because of the |
| 1733 | * invariant bounds. |
| 1734 | */ |
| 1735 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 1736 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 1737 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 1738 | GET_TD_MICROSECONDS(right); |
| 1739 | result = new_delta(days, seconds, microseconds, 1); |
| 1740 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | if (result == Py_NotImplemented) |
| 1743 | Py_INCREF(result); |
| 1744 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static PyObject * |
| 1748 | delta_negative(PyDateTime_Delta *self) |
| 1749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | return new_delta(-GET_TD_DAYS(self), |
| 1751 | -GET_TD_SECONDS(self), |
| 1752 | -GET_TD_MICROSECONDS(self), |
| 1753 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | static PyObject * |
| 1757 | delta_positive(PyDateTime_Delta *self) |
| 1758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | /* Could optimize this (by returning self) if this isn't a |
| 1760 | * subclass -- but who uses unary + ? Approximately nobody. |
| 1761 | */ |
| 1762 | return new_delta(GET_TD_DAYS(self), |
| 1763 | GET_TD_SECONDS(self), |
| 1764 | GET_TD_MICROSECONDS(self), |
| 1765 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | static PyObject * |
| 1769 | delta_abs(PyDateTime_Delta *self) |
| 1770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1771 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1774 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | if (GET_TD_DAYS(self) < 0) |
| 1777 | result = delta_negative(self); |
| 1778 | else |
| 1779 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | static PyObject * |
| 1785 | delta_subtract(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 */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 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); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1806 | static int |
| 1807 | delta_cmp(PyObject *self, PyObject *other) |
| 1808 | { |
| 1809 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 1810 | if (diff == 0) { |
| 1811 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 1812 | if (diff == 0) |
| 1813 | diff = GET_TD_MICROSECONDS(self) - |
| 1814 | GET_TD_MICROSECONDS(other); |
| 1815 | } |
| 1816 | return diff; |
| 1817 | } |
| 1818 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1819 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1820 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1823 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | return diff_to_bool(diff, op); |
| 1825 | } |
| 1826 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1827 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1832 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1833 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1834 | delta_hash(PyDateTime_Delta *self) |
| 1835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | if (self->hashcode == -1) { |
| 1837 | PyObject *temp = delta_getstate(self); |
| 1838 | if (temp != NULL) { |
| 1839 | self->hashcode = PyObject_Hash(temp); |
| 1840 | Py_DECREF(temp); |
| 1841 | } |
| 1842 | } |
| 1843 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | static PyObject * |
| 1847 | delta_multiply(PyObject *left, PyObject *right) |
| 1848 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | if (PyDelta_Check(left)) { |
| 1852 | /* delta * ??? */ |
| 1853 | if (PyLong_Check(right)) |
| 1854 | result = multiply_int_timedelta(right, |
| 1855 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1856 | else if (PyFloat_Check(right)) |
| 1857 | result = multiply_float_timedelta(right, |
| 1858 | (PyDateTime_Delta *) left); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | } |
| 1860 | else if (PyLong_Check(left)) |
| 1861 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1862 | (PyDateTime_Delta *) right); |
| 1863 | else if (PyFloat_Check(left)) |
| 1864 | result = multiply_float_timedelta(left, |
| 1865 | (PyDateTime_Delta *) right); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | if (result == Py_NotImplemented) |
| 1868 | Py_INCREF(result); |
| 1869 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static PyObject * |
| 1873 | delta_divide(PyObject *left, PyObject *right) |
| 1874 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | if (PyDelta_Check(left)) { |
| 1878 | /* delta * ??? */ |
| 1879 | if (PyLong_Check(right)) |
| 1880 | result = divide_timedelta_int( |
| 1881 | (PyDateTime_Delta *)left, |
| 1882 | right); |
| 1883 | else if (PyDelta_Check(right)) |
| 1884 | result = divide_timedelta_timedelta( |
| 1885 | (PyDateTime_Delta *)left, |
| 1886 | (PyDateTime_Delta *)right); |
| 1887 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | if (result == Py_NotImplemented) |
| 1890 | Py_INCREF(result); |
| 1891 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1894 | static PyObject * |
| 1895 | delta_truedivide(PyObject *left, PyObject *right) |
| 1896 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | if (PyDelta_Check(left)) { |
| 1900 | if (PyDelta_Check(right)) |
| 1901 | result = truedivide_timedelta_timedelta( |
| 1902 | (PyDateTime_Delta *)left, |
| 1903 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1904 | else if (PyFloat_Check(right)) |
| 1905 | result = truedivide_timedelta_float( |
| 1906 | (PyDateTime_Delta *)left, right); |
| 1907 | else if (PyLong_Check(right)) |
| 1908 | result = truedivide_timedelta_int( |
| 1909 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | if (result == Py_NotImplemented) |
| 1913 | Py_INCREF(result); |
| 1914 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static PyObject * |
| 1918 | delta_remainder(PyObject *left, PyObject *right) |
| 1919 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | PyObject *pyus_left; |
| 1921 | PyObject *pyus_right; |
| 1922 | PyObject *pyus_remainder; |
| 1923 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1924 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1925 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1926 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1928 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1929 | if (pyus_left == NULL) |
| 1930 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1933 | if (pyus_right == NULL) { |
| 1934 | Py_DECREF(pyus_left); |
| 1935 | return NULL; |
| 1936 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 1939 | Py_DECREF(pyus_left); |
| 1940 | Py_DECREF(pyus_right); |
| 1941 | if (pyus_remainder == NULL) |
| 1942 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | remainder = microseconds_to_delta(pyus_remainder); |
| 1945 | Py_DECREF(pyus_remainder); |
| 1946 | if (remainder == NULL) |
| 1947 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | static PyObject * |
| 1953 | delta_divmod(PyObject *left, PyObject *right) |
| 1954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | PyObject *pyus_left; |
| 1956 | PyObject *pyus_right; |
| 1957 | PyObject *divmod; |
| 1958 | PyObject *delta; |
| 1959 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1960 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1961 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1962 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1965 | if (pyus_left == NULL) |
| 1966 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1968 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1969 | if (pyus_right == NULL) { |
| 1970 | Py_DECREF(pyus_left); |
| 1971 | return NULL; |
| 1972 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
| 1975 | Py_DECREF(pyus_left); |
| 1976 | Py_DECREF(pyus_right); |
| 1977 | if (divmod == NULL) |
| 1978 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | assert(PyTuple_Size(divmod) == 2); |
| 1981 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 1982 | if (delta == NULL) { |
| 1983 | Py_DECREF(divmod); |
| 1984 | return NULL; |
| 1985 | } |
| 1986 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 1987 | Py_DECREF(delta); |
| 1988 | Py_DECREF(divmod); |
| 1989 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1992 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 1993 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 1994 | * so far, and there are factor microseconds per current unit, the number |
| 1995 | * of which is given by num. num * factor is added to sofar in a |
| 1996 | * numerically careful way, and that's the result. Any fractional |
| 1997 | * microseconds left over (this can happen if num is a float type) are |
| 1998 | * added into *leftover. |
| 1999 | * Note that there are many ways this can give an error (NULL) return. |
| 2000 | */ |
| 2001 | static PyObject * |
| 2002 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 2003 | double *leftover) |
| 2004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | PyObject *prod; |
| 2006 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | if (PyLong_Check(num)) { |
| 2011 | prod = PyNumber_Multiply(num, factor); |
| 2012 | if (prod == NULL) |
| 2013 | return NULL; |
| 2014 | sum = PyNumber_Add(sofar, prod); |
| 2015 | Py_DECREF(prod); |
| 2016 | return sum; |
| 2017 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | if (PyFloat_Check(num)) { |
| 2020 | double dnum; |
| 2021 | double fracpart; |
| 2022 | double intpart; |
| 2023 | PyObject *x; |
| 2024 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | /* The Plan: decompose num into an integer part and a |
| 2027 | * fractional part, num = intpart + fracpart. |
| 2028 | * Then num * factor == |
| 2029 | * intpart * factor + fracpart * factor |
| 2030 | * and the LHS can be computed exactly in long arithmetic. |
| 2031 | * The RHS is again broken into an int part and frac part. |
| 2032 | * and the frac part is added into *leftover. |
| 2033 | */ |
| 2034 | dnum = PyFloat_AsDouble(num); |
| 2035 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2036 | return NULL; |
| 2037 | fracpart = modf(dnum, &intpart); |
| 2038 | x = PyLong_FromDouble(intpart); |
| 2039 | if (x == NULL) |
| 2040 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | prod = PyNumber_Multiply(x, factor); |
| 2043 | Py_DECREF(x); |
| 2044 | if (prod == NULL) |
| 2045 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | sum = PyNumber_Add(sofar, prod); |
| 2048 | Py_DECREF(prod); |
| 2049 | if (sum == NULL) |
| 2050 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | if (fracpart == 0.0) |
| 2053 | return sum; |
| 2054 | /* So far we've lost no information. Dealing with the |
| 2055 | * fractional part requires float arithmetic, and may |
| 2056 | * lose a little info. |
| 2057 | */ |
| 2058 | assert(PyLong_Check(factor)); |
| 2059 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | dnum *= fracpart; |
| 2062 | fracpart = modf(dnum, &intpart); |
| 2063 | x = PyLong_FromDouble(intpart); |
| 2064 | if (x == NULL) { |
| 2065 | Py_DECREF(sum); |
| 2066 | return NULL; |
| 2067 | } |
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 | y = PyNumber_Add(sum, x); |
| 2070 | Py_DECREF(sum); |
| 2071 | Py_DECREF(x); |
| 2072 | *leftover += fracpart; |
| 2073 | return y; |
| 2074 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | PyErr_Format(PyExc_TypeError, |
| 2077 | "unsupported type for timedelta %s component: %s", |
| 2078 | tag, Py_TYPE(num)->tp_name); |
| 2079 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | static PyObject * |
| 2083 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | /* Argument objects. */ |
| 2088 | PyObject *day = NULL; |
| 2089 | PyObject *second = NULL; |
| 2090 | PyObject *us = NULL; |
| 2091 | PyObject *ms = NULL; |
| 2092 | PyObject *minute = NULL; |
| 2093 | PyObject *hour = NULL; |
| 2094 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2097 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2098 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | static char *keywords[] = { |
| 2101 | "days", "seconds", "microseconds", "milliseconds", |
| 2102 | "minutes", "hours", "weeks", NULL |
| 2103 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2106 | keywords, |
| 2107 | &day, &second, &us, |
| 2108 | &ms, &minute, &hour, &week) == 0) |
| 2109 | goto Done; |
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 | x = PyLong_FromLong(0); |
| 2112 | if (x == NULL) |
| 2113 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2115 | #define CLEANUP \ |
| 2116 | Py_DECREF(x); \ |
| 2117 | x = y; \ |
| 2118 | if (x == NULL) \ |
| 2119 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | if (us) { |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2122 | y = accum("microseconds", x, us, one, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | CLEANUP; |
| 2124 | } |
| 2125 | if (ms) { |
| 2126 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2127 | CLEANUP; |
| 2128 | } |
| 2129 | if (second) { |
| 2130 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2131 | CLEANUP; |
| 2132 | } |
| 2133 | if (minute) { |
| 2134 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2135 | CLEANUP; |
| 2136 | } |
| 2137 | if (hour) { |
| 2138 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2139 | CLEANUP; |
| 2140 | } |
| 2141 | if (day) { |
| 2142 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2143 | CLEANUP; |
| 2144 | } |
| 2145 | if (week) { |
| 2146 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2147 | CLEANUP; |
| 2148 | } |
| 2149 | if (leftover_us) { |
| 2150 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2151 | double whole_us = round(leftover_us); |
| 2152 | int x_is_odd; |
| 2153 | PyObject *temp; |
| 2154 | |
| 2155 | whole_us = round(leftover_us); |
| 2156 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2157 | /* We're exactly halfway between two integers. In order |
| 2158 | * to do round-half-to-even, we must determine whether x |
| 2159 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2160 | * code below uses bitwise and operation to check the last |
| 2161 | * bit. */ |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 2162 | temp = PyNumber_And(x, one); /* temp <- x & 1 */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2163 | if (temp == NULL) { |
| 2164 | Py_DECREF(x); |
| 2165 | goto Done; |
| 2166 | } |
| 2167 | x_is_odd = PyObject_IsTrue(temp); |
| 2168 | Py_DECREF(temp); |
| 2169 | if (x_is_odd == -1) { |
| 2170 | Py_DECREF(x); |
| 2171 | goto Done; |
| 2172 | } |
| 2173 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2174 | } |
| 2175 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2176 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | if (temp == NULL) { |
| 2179 | Py_DECREF(x); |
| 2180 | goto Done; |
| 2181 | } |
| 2182 | y = PyNumber_Add(x, temp); |
| 2183 | Py_DECREF(temp); |
| 2184 | CLEANUP; |
| 2185 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | self = microseconds_to_delta_ex(x, type); |
| 2188 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2189 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2191 | |
| 2192 | #undef CLEANUP |
| 2193 | } |
| 2194 | |
| 2195 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2196 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | return (GET_TD_DAYS(self) != 0 |
| 2199 | || GET_TD_SECONDS(self) != 0 |
| 2200 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | static PyObject * |
| 2204 | delta_repr(PyDateTime_Delta *self) |
| 2205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | if (GET_TD_MICROSECONDS(self) != 0) |
| 2207 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2208 | Py_TYPE(self)->tp_name, |
| 2209 | GET_TD_DAYS(self), |
| 2210 | GET_TD_SECONDS(self), |
| 2211 | GET_TD_MICROSECONDS(self)); |
| 2212 | if (GET_TD_SECONDS(self) != 0) |
| 2213 | return PyUnicode_FromFormat("%s(%d, %d)", |
| 2214 | Py_TYPE(self)->tp_name, |
| 2215 | GET_TD_DAYS(self), |
| 2216 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | return PyUnicode_FromFormat("%s(%d)", |
| 2219 | Py_TYPE(self)->tp_name, |
| 2220 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | static PyObject * |
| 2224 | delta_str(PyDateTime_Delta *self) |
| 2225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | int us = GET_TD_MICROSECONDS(self); |
| 2227 | int seconds = GET_TD_SECONDS(self); |
| 2228 | int minutes = divmod(seconds, 60, &seconds); |
| 2229 | int hours = divmod(minutes, 60, &minutes); |
| 2230 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | if (days) { |
| 2233 | if (us) |
| 2234 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2235 | days, (days == 1 || days == -1) ? "" : "s", |
| 2236 | hours, minutes, seconds, us); |
| 2237 | else |
| 2238 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2239 | days, (days == 1 || days == -1) ? "" : "s", |
| 2240 | hours, minutes, seconds); |
| 2241 | } else { |
| 2242 | if (us) |
| 2243 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2244 | hours, minutes, seconds, us); |
| 2245 | else |
| 2246 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2247 | hours, minutes, seconds); |
| 2248 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2249 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2252 | /* Pickle support, a simple use of __reduce__. */ |
| 2253 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2254 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2255 | static PyObject * |
| 2256 | delta_getstate(PyDateTime_Delta *self) |
| 2257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2259 | GET_TD_SECONDS(self), |
| 2260 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2263 | static PyObject * |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2264 | delta_total_seconds(PyObject *self) |
| 2265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | PyObject *total_seconds; |
| 2267 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2270 | if (total_microseconds == NULL) |
| 2271 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2272 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2273 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2280 | delta_reduce(PyDateTime_Delta* self) |
| 2281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2286 | |
| 2287 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | {"days", T_INT, OFFSET(days), READONLY, |
| 2290 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2293 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2296 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2297 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2298 | }; |
| 2299 | |
| 2300 | static PyMethodDef delta_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
| 2302 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2305 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2308 | }; |
| 2309 | |
| 2310 | static char delta_doc[] = |
| 2311 | PyDoc_STR("Difference between two datetime values."); |
| 2312 | |
| 2313 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | delta_add, /* nb_add */ |
| 2315 | delta_subtract, /* nb_subtract */ |
| 2316 | delta_multiply, /* nb_multiply */ |
| 2317 | delta_remainder, /* nb_remainder */ |
| 2318 | delta_divmod, /* nb_divmod */ |
| 2319 | 0, /* nb_power */ |
| 2320 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2321 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2322 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2323 | (inquiry)delta_bool, /* nb_bool */ |
| 2324 | 0, /*nb_invert*/ |
| 2325 | 0, /*nb_lshift*/ |
| 2326 | 0, /*nb_rshift*/ |
| 2327 | 0, /*nb_and*/ |
| 2328 | 0, /*nb_xor*/ |
| 2329 | 0, /*nb_or*/ |
| 2330 | 0, /*nb_int*/ |
| 2331 | 0, /*nb_reserved*/ |
| 2332 | 0, /*nb_float*/ |
| 2333 | 0, /*nb_inplace_add*/ |
| 2334 | 0, /*nb_inplace_subtract*/ |
| 2335 | 0, /*nb_inplace_multiply*/ |
| 2336 | 0, /*nb_inplace_remainder*/ |
| 2337 | 0, /*nb_inplace_power*/ |
| 2338 | 0, /*nb_inplace_lshift*/ |
| 2339 | 0, /*nb_inplace_rshift*/ |
| 2340 | 0, /*nb_inplace_and*/ |
| 2341 | 0, /*nb_inplace_xor*/ |
| 2342 | 0, /*nb_inplace_or*/ |
| 2343 | delta_divide, /* nb_floor_divide */ |
| 2344 | delta_truedivide, /* nb_true_divide */ |
| 2345 | 0, /* nb_inplace_floor_divide */ |
| 2346 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2347 | }; |
| 2348 | |
| 2349 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2351 | "datetime.timedelta", /* tp_name */ |
| 2352 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2353 | 0, /* tp_itemsize */ |
| 2354 | 0, /* tp_dealloc */ |
| 2355 | 0, /* tp_print */ |
| 2356 | 0, /* tp_getattr */ |
| 2357 | 0, /* tp_setattr */ |
| 2358 | 0, /* tp_reserved */ |
| 2359 | (reprfunc)delta_repr, /* tp_repr */ |
| 2360 | &delta_as_number, /* tp_as_number */ |
| 2361 | 0, /* tp_as_sequence */ |
| 2362 | 0, /* tp_as_mapping */ |
| 2363 | (hashfunc)delta_hash, /* tp_hash */ |
| 2364 | 0, /* tp_call */ |
| 2365 | (reprfunc)delta_str, /* tp_str */ |
| 2366 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2367 | 0, /* tp_setattro */ |
| 2368 | 0, /* tp_as_buffer */ |
| 2369 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2370 | delta_doc, /* tp_doc */ |
| 2371 | 0, /* tp_traverse */ |
| 2372 | 0, /* tp_clear */ |
| 2373 | delta_richcompare, /* tp_richcompare */ |
| 2374 | 0, /* tp_weaklistoffset */ |
| 2375 | 0, /* tp_iter */ |
| 2376 | 0, /* tp_iternext */ |
| 2377 | delta_methods, /* tp_methods */ |
| 2378 | delta_members, /* tp_members */ |
| 2379 | 0, /* tp_getset */ |
| 2380 | 0, /* tp_base */ |
| 2381 | 0, /* tp_dict */ |
| 2382 | 0, /* tp_descr_get */ |
| 2383 | 0, /* tp_descr_set */ |
| 2384 | 0, /* tp_dictoffset */ |
| 2385 | 0, /* tp_init */ |
| 2386 | 0, /* tp_alloc */ |
| 2387 | delta_new, /* tp_new */ |
| 2388 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2389 | }; |
| 2390 | |
| 2391 | /* |
| 2392 | * PyDateTime_Date implementation. |
| 2393 | */ |
| 2394 | |
| 2395 | /* Accessor properties. */ |
| 2396 | |
| 2397 | static PyObject * |
| 2398 | date_year(PyDateTime_Date *self, void *unused) |
| 2399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | static PyObject * |
| 2404 | date_month(PyDateTime_Date *self, void *unused) |
| 2405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | static PyObject * |
| 2410 | date_day(PyDateTime_Date *self, void *unused) |
| 2411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2416 | {"year", (getter)date_year}, |
| 2417 | {"month", (getter)date_month}, |
| 2418 | {"day", (getter)date_day}, |
| 2419 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2420 | }; |
| 2421 | |
| 2422 | /* Constructors. */ |
| 2423 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2424 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2425 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2426 | static PyObject * |
| 2427 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | PyObject *self = NULL; |
| 2430 | PyObject *state; |
| 2431 | int year; |
| 2432 | int month; |
| 2433 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | /* Check for invocation from pickle with __getstate__ state */ |
| 2436 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2437 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2438 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2439 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2440 | { |
| 2441 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2443 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2444 | if (me != NULL) { |
| 2445 | char *pdata = PyBytes_AS_STRING(state); |
| 2446 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2447 | me->hashcode = -1; |
| 2448 | } |
| 2449 | return (PyObject *)me; |
| 2450 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2453 | &year, &month, &day)) { |
| 2454 | if (check_date_args(year, month, day) < 0) |
| 2455 | return NULL; |
| 2456 | self = new_date_ex(year, month, day, type); |
| 2457 | } |
| 2458 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | /* Return new date from localtime(t). */ |
| 2462 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2463 | date_local_from_object(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | struct tm *tm; |
| 2466 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2467 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 2468 | if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | tm = localtime(&t); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2472 | if (tm == NULL) { |
| 2473 | /* unconvertible time */ |
| 2474 | #ifdef EINVAL |
| 2475 | if (errno == 0) |
| 2476 | errno = EINVAL; |
| 2477 | #endif |
| 2478 | PyErr_SetFromErrno(PyExc_OSError); |
| 2479 | return NULL; |
| 2480 | } |
| 2481 | |
| 2482 | return PyObject_CallFunction(cls, "iii", |
| 2483 | tm->tm_year + 1900, |
| 2484 | tm->tm_mon + 1, |
| 2485 | tm->tm_mday); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | /* Return new date from current time. |
| 2489 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2490 | * only way to be sure of that is to *call* time.time(). That's not |
| 2491 | * generally the same as calling C's time. |
| 2492 | */ |
| 2493 | static PyObject * |
| 2494 | date_today(PyObject *cls, PyObject *dummy) |
| 2495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | PyObject *time; |
| 2497 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2498 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2500 | time = time_time(); |
| 2501 | if (time == NULL) |
| 2502 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2504 | /* Note well: today() is a class method, so this may not call |
| 2505 | * date.fromtimestamp. For example, it may call |
| 2506 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2507 | * time.time() delivers; if someone were gonzo about optimization, |
| 2508 | * date.today() could get away with plain C time(). |
| 2509 | */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2510 | result = _PyObject_CallMethodId(cls, &PyId_fromtimestamp, "O", time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | Py_DECREF(time); |
| 2512 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2516 | static PyObject * |
| 2517 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2518 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2519 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2520 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2521 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2522 | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
| 2523 | result = date_local_from_object(cls, timestamp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2528 | * the ordinal is out of range. |
| 2529 | */ |
| 2530 | static PyObject * |
| 2531 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2532 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | PyObject *result = NULL; |
| 2534 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2536 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2537 | int year; |
| 2538 | int month; |
| 2539 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | if (ordinal < 1) |
| 2542 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2543 | ">= 1"); |
| 2544 | else { |
| 2545 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2546 | result = PyObject_CallFunction(cls, "iii", |
| 2547 | year, month, day); |
| 2548 | } |
| 2549 | } |
| 2550 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | /* |
| 2554 | * Date arithmetic. |
| 2555 | */ |
| 2556 | |
| 2557 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2558 | * instead. |
| 2559 | */ |
| 2560 | static PyObject * |
| 2561 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | PyObject *result = NULL; |
| 2564 | int year = GET_YEAR(date); |
| 2565 | int month = GET_MONTH(date); |
| 2566 | int deltadays = GET_TD_DAYS(delta); |
| 2567 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2568 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | if (normalize_date(&year, &month, &day) >= 0) |
| 2571 | result = new_date(year, month, day); |
| 2572 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | static PyObject * |
| 2576 | date_add(PyObject *left, PyObject *right) |
| 2577 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2578 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2579 | Py_RETURN_NOTIMPLEMENTED; |
| 2580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2581 | if (PyDate_Check(left)) { |
| 2582 | /* date + ??? */ |
| 2583 | if (PyDelta_Check(right)) |
| 2584 | /* date + delta */ |
| 2585 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2586 | (PyDateTime_Delta *) right, |
| 2587 | 0); |
| 2588 | } |
| 2589 | else { |
| 2590 | /* ??? + date |
| 2591 | * 'right' must be one of us, or we wouldn't have been called |
| 2592 | */ |
| 2593 | if (PyDelta_Check(left)) |
| 2594 | /* delta + date */ |
| 2595 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2596 | (PyDateTime_Delta *) left, |
| 2597 | 0); |
| 2598 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2599 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | static PyObject * |
| 2603 | date_subtract(PyObject *left, PyObject *right) |
| 2604 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2605 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2606 | Py_RETURN_NOTIMPLEMENTED; |
| 2607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | if (PyDate_Check(left)) { |
| 2609 | if (PyDate_Check(right)) { |
| 2610 | /* date - date */ |
| 2611 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2612 | GET_MONTH(left), |
| 2613 | GET_DAY(left)); |
| 2614 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2615 | GET_MONTH(right), |
| 2616 | GET_DAY(right)); |
| 2617 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2618 | } |
| 2619 | if (PyDelta_Check(right)) { |
| 2620 | /* date - delta */ |
| 2621 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2622 | (PyDateTime_Delta *) right, |
| 2623 | 1); |
| 2624 | } |
| 2625 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2626 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | |
| 2630 | /* Various ways to turn a date into a string. */ |
| 2631 | |
| 2632 | static PyObject * |
| 2633 | date_repr(PyDateTime_Date *self) |
| 2634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2635 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2636 | Py_TYPE(self)->tp_name, |
| 2637 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | static PyObject * |
| 2641 | date_isoformat(PyDateTime_Date *self) |
| 2642 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2644 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2647 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2648 | static PyObject * |
| 2649 | date_str(PyDateTime_Date *self) |
| 2650 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2651 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
| 2655 | static PyObject * |
| 2656 | date_ctime(PyDateTime_Date *self) |
| 2657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2658 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | static PyObject * |
| 2662 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | /* This method can be inherited, and needs to call the |
| 2665 | * timetuple() method appropriate to self's class. |
| 2666 | */ |
| 2667 | PyObject *result; |
| 2668 | PyObject *tuple; |
| 2669 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2670 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2671 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 2674 | &format)) |
| 2675 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2676 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2677 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2678 | if (tuple == NULL) |
| 2679 | return NULL; |
| 2680 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2681 | (PyObject *)self); |
| 2682 | Py_DECREF(tuple); |
| 2683 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2686 | static PyObject * |
| 2687 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2692 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2695 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2697 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2698 | return _PyObject_CallMethodId((PyObject *)self, &PyId_strftime, "O", format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2701 | /* ISO methods. */ |
| 2702 | |
| 2703 | static PyObject * |
| 2704 | date_isoweekday(PyDateTime_Date *self) |
| 2705 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2708 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | static PyObject * |
| 2712 | date_isocalendar(PyDateTime_Date *self) |
| 2713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2714 | int year = GET_YEAR(self); |
| 2715 | int week1_monday = iso_week1_monday(year); |
| 2716 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2717 | int week; |
| 2718 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2720 | week = divmod(today - week1_monday, 7, &day); |
| 2721 | if (week < 0) { |
| 2722 | --year; |
| 2723 | week1_monday = iso_week1_monday(year); |
| 2724 | week = divmod(today - week1_monday, 7, &day); |
| 2725 | } |
| 2726 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2727 | ++year; |
| 2728 | week = 0; |
| 2729 | } |
| 2730 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | /* Miscellaneous methods. */ |
| 2734 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2735 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2736 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | if (PyDate_Check(other)) { |
| 2739 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2740 | ((PyDateTime_Date *)other)->data, |
| 2741 | _PyDateTime_DATE_DATASIZE); |
| 2742 | return diff_to_bool(diff, op); |
| 2743 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2744 | else |
| 2745 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static PyObject * |
| 2749 | date_timetuple(PyDateTime_Date *self) |
| 2750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | return build_struct_time(GET_YEAR(self), |
| 2752 | GET_MONTH(self), |
| 2753 | GET_DAY(self), |
| 2754 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2757 | static PyObject * |
| 2758 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | PyObject *clone; |
| 2761 | PyObject *tuple; |
| 2762 | int year = GET_YEAR(self); |
| 2763 | int month = GET_MONTH(self); |
| 2764 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2767 | &year, &month, &day)) |
| 2768 | return NULL; |
| 2769 | tuple = Py_BuildValue("iii", year, month, day); |
| 2770 | if (tuple == NULL) |
| 2771 | return NULL; |
| 2772 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 2773 | Py_DECREF(tuple); |
| 2774 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2777 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2778 | generic_hash(unsigned char *data, int len) |
| 2779 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 2780 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | |
| 2784 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2785 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2786 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2787 | date_hash(PyDateTime_Date *self) |
| 2788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | if (self->hashcode == -1) |
| 2790 | self->hashcode = generic_hash( |
| 2791 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | static PyObject * |
| 2797 | date_toordinal(PyDateTime_Date *self) |
| 2798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 2800 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | static PyObject * |
| 2804 | date_weekday(PyDateTime_Date *self) |
| 2805 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2811 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2812 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2813 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2814 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2815 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2817 | PyObject* field; |
| 2818 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 2819 | _PyDateTime_DATE_DATASIZE); |
| 2820 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2824 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2834 | METH_CLASS, |
| 2835 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2836 | "time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2839 | METH_CLASS, |
| 2840 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2841 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2844 | PyDoc_STR("Current date or datetime: same as " |
| 2845 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2850 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 2853 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2856 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2859 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2862 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2863 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2866 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2869 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2870 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2873 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2874 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2877 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2878 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 2881 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2884 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2886 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2887 | }; |
| 2888 | |
| 2889 | static char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2890 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2891 | |
| 2892 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | date_add, /* nb_add */ |
| 2894 | date_subtract, /* nb_subtract */ |
| 2895 | 0, /* nb_multiply */ |
| 2896 | 0, /* nb_remainder */ |
| 2897 | 0, /* nb_divmod */ |
| 2898 | 0, /* nb_power */ |
| 2899 | 0, /* nb_negative */ |
| 2900 | 0, /* nb_positive */ |
| 2901 | 0, /* nb_absolute */ |
| 2902 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2903 | }; |
| 2904 | |
| 2905 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2907 | "datetime.date", /* tp_name */ |
| 2908 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2909 | 0, /* tp_itemsize */ |
| 2910 | 0, /* tp_dealloc */ |
| 2911 | 0, /* tp_print */ |
| 2912 | 0, /* tp_getattr */ |
| 2913 | 0, /* tp_setattr */ |
| 2914 | 0, /* tp_reserved */ |
| 2915 | (reprfunc)date_repr, /* tp_repr */ |
| 2916 | &date_as_number, /* tp_as_number */ |
| 2917 | 0, /* tp_as_sequence */ |
| 2918 | 0, /* tp_as_mapping */ |
| 2919 | (hashfunc)date_hash, /* tp_hash */ |
| 2920 | 0, /* tp_call */ |
| 2921 | (reprfunc)date_str, /* tp_str */ |
| 2922 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2923 | 0, /* tp_setattro */ |
| 2924 | 0, /* tp_as_buffer */ |
| 2925 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2926 | date_doc, /* tp_doc */ |
| 2927 | 0, /* tp_traverse */ |
| 2928 | 0, /* tp_clear */ |
| 2929 | date_richcompare, /* tp_richcompare */ |
| 2930 | 0, /* tp_weaklistoffset */ |
| 2931 | 0, /* tp_iter */ |
| 2932 | 0, /* tp_iternext */ |
| 2933 | date_methods, /* tp_methods */ |
| 2934 | 0, /* tp_members */ |
| 2935 | date_getset, /* tp_getset */ |
| 2936 | 0, /* tp_base */ |
| 2937 | 0, /* tp_dict */ |
| 2938 | 0, /* tp_descr_get */ |
| 2939 | 0, /* tp_descr_set */ |
| 2940 | 0, /* tp_dictoffset */ |
| 2941 | 0, /* tp_init */ |
| 2942 | 0, /* tp_alloc */ |
| 2943 | date_new, /* tp_new */ |
| 2944 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2945 | }; |
| 2946 | |
| 2947 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2948 | * PyDateTime_TZInfo implementation. |
| 2949 | */ |
| 2950 | |
| 2951 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 2952 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 2953 | * 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] | 2954 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2955 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 2956 | * |
| 2957 | * Note: For reasons having to do with pickling of subclasses, we have |
| 2958 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 2959 | * in the Python implementation (__init__() could raise NotImplementedError |
| 2960 | * there without ill effect), but doing so in the C implementation hit a |
| 2961 | * brick wall. |
| 2962 | */ |
| 2963 | |
| 2964 | static PyObject * |
| 2965 | tzinfo_nogo(const char* methodname) |
| 2966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | PyErr_Format(PyExc_NotImplementedError, |
| 2968 | "a tzinfo subclass must implement %s()", |
| 2969 | methodname); |
| 2970 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | /* Methods. A subclass must implement these. */ |
| 2974 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2975 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2976 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 2977 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2981 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2982 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 2983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2987 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2988 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 2989 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2993 | |
| 2994 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 2995 | PyDateTime_Delta *delta, |
| 2996 | int factor); |
| 2997 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 2998 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 2999 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3000 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3001 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3002 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3003 | PyObject *result = NULL; |
| 3004 | PyObject *off = NULL, *dst = NULL; |
| 3005 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3006 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3007 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | PyErr_SetString(PyExc_TypeError, |
| 3009 | "fromutc: argument must be a datetime"); |
| 3010 | return NULL; |
| 3011 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3012 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3014 | "is not self"); |
| 3015 | return NULL; |
| 3016 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3017 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3018 | off = datetime_utcoffset(dt, NULL); |
| 3019 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3020 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3021 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3023 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3024 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3026 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3027 | dst = datetime_dst(dt, NULL); |
| 3028 | if (dst == NULL) |
| 3029 | goto Fail; |
| 3030 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3032 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3033 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3035 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3036 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3037 | if (delta == NULL) |
| 3038 | goto Fail; |
| 3039 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3041 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3042 | |
| 3043 | Py_DECREF(dst); |
| 3044 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3045 | if (dst == NULL) |
| 3046 | goto Fail; |
| 3047 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | goto Inconsistent; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3049 | if (delta_bool(delta) != 0) { |
| 3050 | PyObject *temp = result; |
| 3051 | result = add_datetime_timedelta((PyDateTime_DateTime *)result, |
| 3052 | (PyDateTime_Delta *)dst, 1); |
| 3053 | Py_DECREF(temp); |
| 3054 | if (result == NULL) |
| 3055 | goto Fail; |
| 3056 | } |
| 3057 | Py_DECREF(delta); |
| 3058 | Py_DECREF(dst); |
| 3059 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3061 | |
| 3062 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3064 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | /* fall thru to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3067 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3068 | Py_XDECREF(off); |
| 3069 | Py_XDECREF(dst); |
| 3070 | Py_XDECREF(delta); |
| 3071 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3075 | /* |
| 3076 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3077 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3078 | */ |
| 3079 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3080 | static PyObject * |
| 3081 | tzinfo_reduce(PyObject *self) |
| 3082 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | PyObject *args, *state, *tmp; |
| 3084 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3085 | _Py_IDENTIFIER(__getinitargs__); |
| 3086 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | tmp = PyTuple_New(0); |
| 3089 | if (tmp == NULL) |
| 3090 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3091 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3092 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | if (getinitargs != NULL) { |
| 3094 | args = PyObject_CallObject(getinitargs, tmp); |
| 3095 | Py_DECREF(getinitargs); |
| 3096 | if (args == NULL) { |
| 3097 | Py_DECREF(tmp); |
| 3098 | return NULL; |
| 3099 | } |
| 3100 | } |
| 3101 | else { |
| 3102 | PyErr_Clear(); |
| 3103 | args = tmp; |
| 3104 | Py_INCREF(args); |
| 3105 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3106 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3107 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | if (getstate != NULL) { |
| 3109 | state = PyObject_CallObject(getstate, tmp); |
| 3110 | Py_DECREF(getstate); |
| 3111 | if (state == NULL) { |
| 3112 | Py_DECREF(args); |
| 3113 | Py_DECREF(tmp); |
| 3114 | return NULL; |
| 3115 | } |
| 3116 | } |
| 3117 | else { |
| 3118 | PyObject **dictptr; |
| 3119 | PyErr_Clear(); |
| 3120 | state = Py_None; |
| 3121 | dictptr = _PyObject_GetDictPtr(self); |
| 3122 | if (dictptr && *dictptr && PyDict_Size(*dictptr)) |
| 3123 | state = *dictptr; |
| 3124 | Py_INCREF(state); |
| 3125 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | Py_DECREF(tmp); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | if (state == Py_None) { |
| 3130 | Py_DECREF(state); |
| 3131 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3132 | } |
| 3133 | else |
| 3134 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3135 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3136 | |
| 3137 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3140 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3143 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3144 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 3147 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3150 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 3153 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3156 | }; |
| 3157 | |
| 3158 | static char tzinfo_doc[] = |
| 3159 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3160 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3161 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3163 | "datetime.tzinfo", /* tp_name */ |
| 3164 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3165 | 0, /* tp_itemsize */ |
| 3166 | 0, /* tp_dealloc */ |
| 3167 | 0, /* tp_print */ |
| 3168 | 0, /* tp_getattr */ |
| 3169 | 0, /* tp_setattr */ |
| 3170 | 0, /* tp_reserved */ |
| 3171 | 0, /* tp_repr */ |
| 3172 | 0, /* tp_as_number */ |
| 3173 | 0, /* tp_as_sequence */ |
| 3174 | 0, /* tp_as_mapping */ |
| 3175 | 0, /* tp_hash */ |
| 3176 | 0, /* tp_call */ |
| 3177 | 0, /* tp_str */ |
| 3178 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3179 | 0, /* tp_setattro */ |
| 3180 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3181 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | tzinfo_doc, /* tp_doc */ |
| 3183 | 0, /* tp_traverse */ |
| 3184 | 0, /* tp_clear */ |
| 3185 | 0, /* tp_richcompare */ |
| 3186 | 0, /* tp_weaklistoffset */ |
| 3187 | 0, /* tp_iter */ |
| 3188 | 0, /* tp_iternext */ |
| 3189 | tzinfo_methods, /* tp_methods */ |
| 3190 | 0, /* tp_members */ |
| 3191 | 0, /* tp_getset */ |
| 3192 | 0, /* tp_base */ |
| 3193 | 0, /* tp_dict */ |
| 3194 | 0, /* tp_descr_get */ |
| 3195 | 0, /* tp_descr_set */ |
| 3196 | 0, /* tp_dictoffset */ |
| 3197 | 0, /* tp_init */ |
| 3198 | 0, /* tp_alloc */ |
| 3199 | PyType_GenericNew, /* tp_new */ |
| 3200 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3201 | }; |
| 3202 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3203 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3204 | |
| 3205 | static PyObject * |
| 3206 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3207 | { |
| 3208 | PyObject *offset; |
| 3209 | PyObject *name = NULL; |
| 3210 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|O!:timezone", timezone_kws, |
| 3211 | &PyDateTime_DeltaType, &offset, |
| 3212 | &PyUnicode_Type, &name)) |
| 3213 | return new_timezone(offset, name); |
| 3214 | |
| 3215 | return NULL; |
| 3216 | } |
| 3217 | |
| 3218 | static void |
| 3219 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3220 | { |
| 3221 | Py_CLEAR(self->offset); |
| 3222 | Py_CLEAR(self->name); |
| 3223 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3224 | } |
| 3225 | |
| 3226 | static PyObject * |
| 3227 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3228 | PyDateTime_TimeZone *other, int op) |
| 3229 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3230 | if (op != Py_EQ && op != Py_NE) |
| 3231 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3232 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 3233 | if (op == Py_EQ) |
| 3234 | Py_RETURN_FALSE; |
| 3235 | else |
| 3236 | Py_RETURN_TRUE; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3237 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3238 | return delta_richcompare(self->offset, other->offset, op); |
| 3239 | } |
| 3240 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3241 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3242 | timezone_hash(PyDateTime_TimeZone *self) |
| 3243 | { |
| 3244 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3245 | } |
| 3246 | |
| 3247 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3248 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3249 | otherwise. |
| 3250 | */ |
| 3251 | static int |
| 3252 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3253 | { |
| 3254 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3255 | return 0; |
| 3256 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3257 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3258 | return -1; |
| 3259 | } |
| 3260 | |
| 3261 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3262 | timezone_repr(PyDateTime_TimeZone *self) |
| 3263 | { |
| 3264 | /* Note that although timezone is not subclassable, it is convenient |
| 3265 | to use Py_TYPE(self)->tp_name here. */ |
| 3266 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3267 | |
| 3268 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3269 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3270 | |
| 3271 | if (self->name == NULL) |
| 3272 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3273 | |
| 3274 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3275 | self->name); |
| 3276 | } |
| 3277 | |
| 3278 | |
| 3279 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3280 | timezone_str(PyDateTime_TimeZone *self) |
| 3281 | { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3282 | int hours, minutes, seconds; |
| 3283 | PyObject *offset; |
| 3284 | char sign; |
| 3285 | |
| 3286 | if (self->name != NULL) { |
| 3287 | Py_INCREF(self->name); |
| 3288 | return self->name; |
| 3289 | } |
| 3290 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3291 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3292 | sign = '-'; |
| 3293 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3294 | if (offset == NULL) |
| 3295 | return NULL; |
| 3296 | } |
| 3297 | else { |
| 3298 | sign = '+'; |
| 3299 | offset = self->offset; |
| 3300 | Py_INCREF(offset); |
| 3301 | } |
| 3302 | /* Offset is not negative here. */ |
| 3303 | seconds = GET_TD_SECONDS(offset); |
| 3304 | Py_DECREF(offset); |
| 3305 | minutes = divmod(seconds, 60, &seconds); |
| 3306 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3307 | /* XXX ignore sub-minute data, curently not allowed. */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3308 | assert(seconds == 0); |
| 3309 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | static PyObject * |
| 3313 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3314 | { |
| 3315 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3316 | return NULL; |
| 3317 | |
| 3318 | return timezone_str(self); |
| 3319 | } |
| 3320 | |
| 3321 | static PyObject * |
| 3322 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3323 | { |
| 3324 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3325 | return NULL; |
| 3326 | |
| 3327 | Py_INCREF(self->offset); |
| 3328 | return self->offset; |
| 3329 | } |
| 3330 | |
| 3331 | static PyObject * |
| 3332 | timezone_dst(PyObject *self, PyObject *dt) |
| 3333 | { |
| 3334 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3335 | return NULL; |
| 3336 | |
| 3337 | Py_RETURN_NONE; |
| 3338 | } |
| 3339 | |
| 3340 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3341 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3342 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3343 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3344 | PyErr_SetString(PyExc_TypeError, |
| 3345 | "fromutc: argument must be a datetime"); |
| 3346 | return NULL; |
| 3347 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3348 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3349 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3350 | "is not self"); |
| 3351 | return NULL; |
| 3352 | } |
| 3353 | |
| 3354 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3355 | } |
| 3356 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3357 | static PyObject * |
| 3358 | timezone_getinitargs(PyDateTime_TimeZone *self) |
| 3359 | { |
| 3360 | if (self->name == NULL) |
| 3361 | return Py_BuildValue("(O)", self->offset); |
| 3362 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3363 | } |
| 3364 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3365 | static PyMethodDef timezone_methods[] = { |
| 3366 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3367 | 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] | 3368 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3369 | |
| 3370 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3371 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3372 | |
| 3373 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3374 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3375 | |
| 3376 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3377 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3378 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3379 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3380 | PyDoc_STR("pickle support")}, |
| 3381 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3382 | {NULL, NULL} |
| 3383 | }; |
| 3384 | |
| 3385 | static char timezone_doc[] = |
| 3386 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3387 | |
| 3388 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3389 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3390 | "datetime.timezone", /* tp_name */ |
| 3391 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3392 | 0, /* tp_itemsize */ |
| 3393 | (destructor)timezone_dealloc, /* tp_dealloc */ |
| 3394 | 0, /* tp_print */ |
| 3395 | 0, /* tp_getattr */ |
| 3396 | 0, /* tp_setattr */ |
| 3397 | 0, /* tp_reserved */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3398 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3399 | 0, /* tp_as_number */ |
| 3400 | 0, /* tp_as_sequence */ |
| 3401 | 0, /* tp_as_mapping */ |
| 3402 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3403 | 0, /* tp_call */ |
| 3404 | (reprfunc)timezone_str, /* tp_str */ |
| 3405 | 0, /* tp_getattro */ |
| 3406 | 0, /* tp_setattro */ |
| 3407 | 0, /* tp_as_buffer */ |
| 3408 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3409 | timezone_doc, /* tp_doc */ |
| 3410 | 0, /* tp_traverse */ |
| 3411 | 0, /* tp_clear */ |
| 3412 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3413 | 0, /* tp_weaklistoffset */ |
| 3414 | 0, /* tp_iter */ |
| 3415 | 0, /* tp_iternext */ |
| 3416 | timezone_methods, /* tp_methods */ |
| 3417 | 0, /* tp_members */ |
| 3418 | 0, /* tp_getset */ |
| 3419 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3420 | 0, /* tp_dict */ |
| 3421 | 0, /* tp_descr_get */ |
| 3422 | 0, /* tp_descr_set */ |
| 3423 | 0, /* tp_dictoffset */ |
| 3424 | 0, /* tp_init */ |
| 3425 | 0, /* tp_alloc */ |
| 3426 | timezone_new, /* tp_new */ |
| 3427 | }; |
| 3428 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3429 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3430 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3431 | */ |
| 3432 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3433 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3434 | */ |
| 3435 | |
| 3436 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3437 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3442 | static PyObject * |
| 3443 | time_minute(PyDateTime_Time *self, void *unused) |
| 3444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | /* The name time_second conflicted with some platform header file. */ |
| 3449 | static PyObject * |
| 3450 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | static PyObject * |
| 3456 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | static PyObject * |
| 3462 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3465 | Py_INCREF(result); |
| 3466 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | {"hour", (getter)time_hour}, |
| 3471 | {"minute", (getter)time_minute}, |
| 3472 | {"second", (getter)py_time_second}, |
| 3473 | {"microsecond", (getter)time_microsecond}, |
| 3474 | {"tzinfo", (getter)time_tzinfo}, |
| 3475 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3476 | }; |
| 3477 | |
| 3478 | /* |
| 3479 | * Constructors. |
| 3480 | */ |
| 3481 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3482 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3483 | "tzinfo", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3484 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3485 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3486 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3488 | PyObject *self = NULL; |
| 3489 | PyObject *state; |
| 3490 | int hour = 0; |
| 3491 | int minute = 0; |
| 3492 | int second = 0; |
| 3493 | int usecond = 0; |
| 3494 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | /* Check for invocation from pickle with __getstate__ state */ |
| 3497 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3498 | PyTuple_GET_SIZE(args) <= 2 && |
| 3499 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3500 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 3501 | ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24) |
| 3502 | { |
| 3503 | PyDateTime_Time *me; |
| 3504 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3507 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3508 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3509 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3510 | "tzinfo state arg"); |
| 3511 | return NULL; |
| 3512 | } |
| 3513 | } |
| 3514 | aware = (char)(tzinfo != Py_None); |
| 3515 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3516 | if (me != NULL) { |
| 3517 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3520 | me->hashcode = -1; |
| 3521 | me->hastzinfo = aware; |
| 3522 | if (aware) { |
| 3523 | Py_INCREF(tzinfo); |
| 3524 | me->tzinfo = tzinfo; |
| 3525 | } |
| 3526 | } |
| 3527 | return (PyObject *)me; |
| 3528 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, |
| 3531 | &hour, &minute, &second, &usecond, |
| 3532 | &tzinfo)) { |
| 3533 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3534 | return NULL; |
| 3535 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3536 | return NULL; |
| 3537 | self = new_time_ex(hour, minute, second, usecond, tzinfo, |
| 3538 | type); |
| 3539 | } |
| 3540 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | /* |
| 3544 | * Destructor. |
| 3545 | */ |
| 3546 | |
| 3547 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3548 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3550 | if (HASTZINFO(self)) { |
| 3551 | Py_XDECREF(self->tzinfo); |
| 3552 | } |
| 3553 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3557 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3558 | */ |
| 3559 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3560 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3561 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3562 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 3563 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3567 | time_dst(PyObject *self, PyObject *unused) { |
| 3568 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3572 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3573 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3577 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3578 | */ |
| 3579 | |
| 3580 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3581 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3584 | int h = TIME_GET_HOUR(self); |
| 3585 | int m = TIME_GET_MINUTE(self); |
| 3586 | int s = TIME_GET_SECOND(self); |
| 3587 | int us = TIME_GET_MICROSECOND(self); |
| 3588 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3590 | if (us) |
| 3591 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3592 | type_name, h, m, s, us); |
| 3593 | else if (s) |
| 3594 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3595 | type_name, h, m, s); |
| 3596 | else |
| 3597 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3598 | if (result != NULL && HASTZINFO(self)) |
| 3599 | result = append_keyword_tzinfo(result, self->tzinfo); |
| 3600 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3603 | static PyObject * |
| 3604 | time_str(PyDateTime_Time *self) |
| 3605 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3606 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3607 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3608 | |
| 3609 | static PyObject * |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3610 | time_isoformat(PyDateTime_Time *self, PyObject *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3612 | char buf[100]; |
| 3613 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3614 | int us = TIME_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3616 | if (us) |
| 3617 | result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d", |
| 3618 | TIME_GET_HOUR(self), |
| 3619 | TIME_GET_MINUTE(self), |
| 3620 | TIME_GET_SECOND(self), |
| 3621 | us); |
| 3622 | else |
| 3623 | result = PyUnicode_FromFormat("%02d:%02d:%02d", |
| 3624 | TIME_GET_HOUR(self), |
| 3625 | TIME_GET_MINUTE(self), |
| 3626 | TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3627 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3628 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | /* We need to append the UTC offset. */ |
| 3632 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 3633 | Py_None) < 0) { |
| 3634 | Py_DECREF(result); |
| 3635 | return NULL; |
| 3636 | } |
| 3637 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 3638 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3641 | static PyObject * |
| 3642 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | PyObject *result; |
| 3645 | PyObject *tuple; |
| 3646 | PyObject *format; |
| 3647 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3649 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3650 | &format)) |
| 3651 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | /* Python's strftime does insane things with the year part of the |
| 3654 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 3655 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3656 | */ |
| 3657 | tuple = Py_BuildValue("iiiiiiiii", |
| 3658 | 1900, 1, 1, /* year, month, day */ |
| 3659 | TIME_GET_HOUR(self), |
| 3660 | TIME_GET_MINUTE(self), |
| 3661 | TIME_GET_SECOND(self), |
| 3662 | 0, 1, -1); /* weekday, daynum, dst */ |
| 3663 | if (tuple == NULL) |
| 3664 | return NULL; |
| 3665 | assert(PyTuple_Size(tuple) == 9); |
| 3666 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3667 | Py_None); |
| 3668 | Py_DECREF(tuple); |
| 3669 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3670 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3671 | |
| 3672 | /* |
| 3673 | * Miscellaneous methods. |
| 3674 | */ |
| 3675 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3676 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3677 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3678 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3679 | PyObject *result = NULL; |
| 3680 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3682 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3683 | if (! PyTime_Check(other)) |
| 3684 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3685 | |
| 3686 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3687 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3688 | ((PyDateTime_Time *)other)->data, |
| 3689 | _PyDateTime_TIME_DATASIZE); |
| 3690 | return diff_to_bool(diff, op); |
| 3691 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3692 | offset1 = time_utcoffset(self, NULL); |
| 3693 | if (offset1 == NULL) |
| 3694 | return NULL; |
| 3695 | offset2 = time_utcoffset(other, NULL); |
| 3696 | if (offset2 == NULL) |
| 3697 | goto done; |
| 3698 | /* If they're both naive, or both aware and have the same offsets, |
| 3699 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3700 | * offset2 == Py_None at this point. |
| 3701 | */ |
| 3702 | if ((offset1 == offset2) || |
| 3703 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 3704 | delta_cmp(offset1, offset2) == 0)) { |
| 3705 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3706 | ((PyDateTime_Time *)other)->data, |
| 3707 | _PyDateTime_TIME_DATASIZE); |
| 3708 | result = diff_to_bool(diff, op); |
| 3709 | } |
| 3710 | /* The hard case: both aware with different UTC offsets */ |
| 3711 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 3712 | int offsecs1, offsecs2; |
| 3713 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3714 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 3715 | TIME_GET_MINUTE(self) * 60 + |
| 3716 | TIME_GET_SECOND(self) - |
| 3717 | GET_TD_DAYS(offset1) * 86400 - |
| 3718 | GET_TD_SECONDS(offset1); |
| 3719 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 3720 | TIME_GET_MINUTE(other) * 60 + |
| 3721 | TIME_GET_SECOND(other) - |
| 3722 | GET_TD_DAYS(offset2) * 86400 - |
| 3723 | GET_TD_SECONDS(offset2); |
| 3724 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3725 | if (diff == 0) |
| 3726 | diff = TIME_GET_MICROSECOND(self) - |
| 3727 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3728 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3729 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 3730 | else if (op == Py_EQ) { |
| 3731 | result = Py_False; |
| 3732 | Py_INCREF(result); |
| 3733 | } |
| 3734 | else if (op == Py_NE) { |
| 3735 | result = Py_True; |
| 3736 | Py_INCREF(result); |
| 3737 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3738 | else { |
| 3739 | PyErr_SetString(PyExc_TypeError, |
| 3740 | "can't compare offset-naive and " |
| 3741 | "offset-aware times"); |
| 3742 | } |
| 3743 | done: |
| 3744 | Py_DECREF(offset1); |
| 3745 | Py_XDECREF(offset2); |
| 3746 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3749 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3750 | time_hash(PyDateTime_Time *self) |
| 3751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3753 | PyObject *offset; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3754 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3755 | offset = time_utcoffset((PyObject *)self, NULL); |
| 3756 | |
| 3757 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3761 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3762 | self->hashcode = generic_hash( |
| 3763 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3764 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3765 | PyObject *temp1, *temp2; |
| 3766 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3768 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 3769 | TIME_GET_MINUTE(self) * 60 + |
| 3770 | TIME_GET_SECOND(self); |
| 3771 | microseconds = TIME_GET_MICROSECOND(self); |
| 3772 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 3773 | if (temp1 == NULL) { |
| 3774 | Py_DECREF(offset); |
| 3775 | return -1; |
| 3776 | } |
| 3777 | temp2 = delta_subtract(temp1, offset); |
| 3778 | Py_DECREF(temp1); |
| 3779 | if (temp2 == NULL) { |
| 3780 | Py_DECREF(offset); |
| 3781 | return -1; |
| 3782 | } |
| 3783 | self->hashcode = PyObject_Hash(temp2); |
| 3784 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3785 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3786 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3787 | } |
| 3788 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3789 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3790 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3791 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3792 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3794 | PyObject *clone; |
| 3795 | PyObject *tuple; |
| 3796 | int hh = TIME_GET_HOUR(self); |
| 3797 | int mm = TIME_GET_MINUTE(self); |
| 3798 | int ss = TIME_GET_SECOND(self); |
| 3799 | int us = TIME_GET_MICROSECOND(self); |
| 3800 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3802 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", |
| 3803 | time_kws, |
| 3804 | &hh, &mm, &ss, &us, &tzinfo)) |
| 3805 | return NULL; |
| 3806 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3807 | if (tuple == NULL) |
| 3808 | return NULL; |
| 3809 | clone = time_new(Py_TYPE(self), tuple, NULL); |
| 3810 | Py_DECREF(tuple); |
| 3811 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3814 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3815 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3816 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3817 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3818 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3819 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3820 | */ |
| 3821 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3822 | time_getstate(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3823 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3824 | PyObject *basestate; |
| 3825 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3827 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 3828 | _PyDateTime_TIME_DATASIZE); |
| 3829 | if (basestate != NULL) { |
| 3830 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3831 | result = PyTuple_Pack(1, basestate); |
| 3832 | else |
| 3833 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 3834 | Py_DECREF(basestate); |
| 3835 | } |
| 3836 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3840 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3845 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3847 | {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, |
| 3848 | PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" |
| 3849 | "[+HH:MM].")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3851 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 3852 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3854 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3855 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3857 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 3858 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 3861 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3863 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 3864 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 3867 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3869 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 3870 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3873 | }; |
| 3874 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3875 | static char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3876 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 3877 | \n\ |
| 3878 | 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] | 3879 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3880 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3881 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3883 | "datetime.time", /* tp_name */ |
| 3884 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 3885 | 0, /* tp_itemsize */ |
| 3886 | (destructor)time_dealloc, /* tp_dealloc */ |
| 3887 | 0, /* tp_print */ |
| 3888 | 0, /* tp_getattr */ |
| 3889 | 0, /* tp_setattr */ |
| 3890 | 0, /* tp_reserved */ |
| 3891 | (reprfunc)time_repr, /* tp_repr */ |
Benjamin Peterson | ee6bdc0 | 2014-03-20 18:00:35 -0500 | [diff] [blame] | 3892 | 0, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | 0, /* tp_as_sequence */ |
| 3894 | 0, /* tp_as_mapping */ |
| 3895 | (hashfunc)time_hash, /* tp_hash */ |
| 3896 | 0, /* tp_call */ |
| 3897 | (reprfunc)time_str, /* tp_str */ |
| 3898 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3899 | 0, /* tp_setattro */ |
| 3900 | 0, /* tp_as_buffer */ |
| 3901 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3902 | time_doc, /* tp_doc */ |
| 3903 | 0, /* tp_traverse */ |
| 3904 | 0, /* tp_clear */ |
| 3905 | time_richcompare, /* tp_richcompare */ |
| 3906 | 0, /* tp_weaklistoffset */ |
| 3907 | 0, /* tp_iter */ |
| 3908 | 0, /* tp_iternext */ |
| 3909 | time_methods, /* tp_methods */ |
| 3910 | 0, /* tp_members */ |
| 3911 | time_getset, /* tp_getset */ |
| 3912 | 0, /* tp_base */ |
| 3913 | 0, /* tp_dict */ |
| 3914 | 0, /* tp_descr_get */ |
| 3915 | 0, /* tp_descr_set */ |
| 3916 | 0, /* tp_dictoffset */ |
| 3917 | 0, /* tp_init */ |
| 3918 | time_alloc, /* tp_alloc */ |
| 3919 | time_new, /* tp_new */ |
| 3920 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3921 | }; |
| 3922 | |
| 3923 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3924 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3925 | */ |
| 3926 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3927 | /* Accessor properties. Properties for day, month, and year are inherited |
| 3928 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3929 | */ |
| 3930 | |
| 3931 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3932 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3934 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3937 | static PyObject * |
| 3938 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 3939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3940 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | static PyObject * |
| 3944 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 3945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3946 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | static PyObject * |
| 3950 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 3951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | static PyObject * |
| 3956 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 3957 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3959 | Py_INCREF(result); |
| 3960 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | {"hour", (getter)datetime_hour}, |
| 3965 | {"minute", (getter)datetime_minute}, |
| 3966 | {"second", (getter)datetime_second}, |
| 3967 | {"microsecond", (getter)datetime_microsecond}, |
| 3968 | {"tzinfo", (getter)datetime_tzinfo}, |
| 3969 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3970 | }; |
| 3971 | |
| 3972 | /* |
| 3973 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3974 | */ |
| 3975 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3976 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3977 | "year", "month", "day", "hour", "minute", "second", |
| 3978 | "microsecond", "tzinfo", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3979 | }; |
| 3980 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3981 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3982 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3984 | PyObject *self = NULL; |
| 3985 | PyObject *state; |
| 3986 | int year; |
| 3987 | int month; |
| 3988 | int day; |
| 3989 | int hour = 0; |
| 3990 | int minute = 0; |
| 3991 | int second = 0; |
| 3992 | int usecond = 0; |
| 3993 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | /* Check for invocation from pickle with __getstate__ state */ |
| 3996 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3997 | PyTuple_GET_SIZE(args) <= 2 && |
| 3998 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3999 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4000 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 4001 | { |
| 4002 | PyDateTime_DateTime *me; |
| 4003 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4006 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4007 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 4008 | PyErr_SetString(PyExc_TypeError, "bad " |
| 4009 | "tzinfo state arg"); |
| 4010 | return NULL; |
| 4011 | } |
| 4012 | } |
| 4013 | aware = (char)(tzinfo != Py_None); |
| 4014 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4015 | if (me != NULL) { |
| 4016 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4018 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4019 | me->hashcode = -1; |
| 4020 | me->hastzinfo = aware; |
| 4021 | if (aware) { |
| 4022 | Py_INCREF(tzinfo); |
| 4023 | me->tzinfo = tzinfo; |
| 4024 | } |
| 4025 | } |
| 4026 | return (PyObject *)me; |
| 4027 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4029 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, |
| 4030 | &year, &month, &day, &hour, &minute, |
| 4031 | &second, &usecond, &tzinfo)) { |
| 4032 | if (check_date_args(year, month, day) < 0) |
| 4033 | return NULL; |
| 4034 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 4035 | return NULL; |
| 4036 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4037 | return NULL; |
| 4038 | self = new_datetime_ex(year, month, day, |
| 4039 | hour, minute, second, usecond, |
| 4040 | tzinfo, type); |
| 4041 | } |
| 4042 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4045 | /* TM_FUNC is the shared type of localtime() and gmtime(). */ |
| 4046 | typedef struct tm *(*TM_FUNC)(const time_t *timer); |
| 4047 | |
| 4048 | /* Internal helper. |
| 4049 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4050 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4051 | */ |
| 4052 | static PyObject * |
| 4053 | 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] | 4054 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4056 | struct tm *tm; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4058 | tm = f(&timet); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4059 | if (tm == NULL) { |
| 4060 | #ifdef EINVAL |
| 4061 | if (errno == 0) |
| 4062 | errno = EINVAL; |
| 4063 | #endif |
| 4064 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | } |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4066 | |
| 4067 | /* The platform localtime/gmtime may insert leap seconds, |
| 4068 | * indicated by tm->tm_sec > 59. We don't care about them, |
| 4069 | * except to the extent that passing them on to the datetime |
| 4070 | * constructor would raise ValueError for a reason that |
| 4071 | * made no sense to the user. |
| 4072 | */ |
| 4073 | if (tm->tm_sec > 59) |
| 4074 | tm->tm_sec = 59; |
| 4075 | return PyObject_CallFunction(cls, "iiiiiiiO", |
| 4076 | tm->tm_year + 1900, |
| 4077 | tm->tm_mon + 1, |
| 4078 | tm->tm_mday, |
| 4079 | tm->tm_hour, |
| 4080 | tm->tm_min, |
| 4081 | tm->tm_sec, |
| 4082 | us, |
| 4083 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | /* Internal helper. |
| 4087 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4088 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4089 | * have enough bits to cover a datetime's full range of precision, it's |
| 4090 | * better to call datetime_from_timet_and_us provided you have a way |
| 4091 | * to get that much precision (e.g., C time() isn't good enough). |
| 4092 | */ |
| 4093 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4094 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4096 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4097 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4098 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4099 | |
Victor Stinner | e4a994d | 2015-03-30 01:10:14 +0200 | [diff] [blame] | 4100 | if (_PyTime_ObjectToTimeval(timestamp, |
| 4101 | &timet, &us, _PyTime_ROUND_FLOOR) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4102 | return NULL; |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4103 | assert(0 <= us && us <= 999999); |
| 4104 | |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4105 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | /* Internal helper. |
| 4109 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4110 | * gmtime for f as appropriate. |
| 4111 | */ |
| 4112 | static PyObject * |
| 4113 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4114 | { |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 4115 | _PyTime_t ts = _PyTime_GetSystemClock(); |
| 4116 | struct timeval tv; |
| 4117 | |
| 4118 | if (_PyTime_AsTimeval(ts, &tv, _PyTime_ROUND_FLOOR) < 0) |
| 4119 | return NULL; |
| 4120 | assert(0 <= tv.tv_usec && tv.tv_usec <= 999999); |
| 4121 | |
| 4122 | return datetime_from_timet_and_us(cls, f, tv.tv_sec, tv.tv_usec, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4125 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4126 | |
| 4127 | @classmethod |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4128 | datetime.datetime.now |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4129 | |
| 4130 | tz: object = None |
| 4131 | Timezone object. |
| 4132 | |
| 4133 | Returns new datetime object representing current time local to tz. |
| 4134 | |
| 4135 | If no tz is specified, uses local timezone. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4136 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4137 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4138 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4139 | datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 4140 | /*[clinic end generated code: output=b3386e5345e2b47a input=80d09869c5267d00]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4143 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4144 | /* Return best possible local time -- this isn't constrained by the |
| 4145 | * precision of a timestamp. |
| 4146 | */ |
| 4147 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4148 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4149 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 4150 | self = datetime_best_possible((PyObject *)type, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4151 | tz == Py_None ? localtime : gmtime, |
| 4152 | tz); |
| 4153 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4154 | /* Convert UTC to tzinfo's zone. */ |
| 4155 | PyObject *temp = self; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4156 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4157 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | Py_DECREF(temp); |
| 4159 | } |
| 4160 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4163 | /* Return best possible UTC time -- this isn't constrained by the |
| 4164 | * precision of a timestamp. |
| 4165 | */ |
| 4166 | static PyObject * |
| 4167 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4168 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4169 | return datetime_best_possible(cls, gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4172 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4173 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4174 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4176 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4177 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4178 | PyObject *tzinfo = Py_None; |
| 4179 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4180 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4181 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4182 | keywords, ×tamp, &tzinfo)) |
| 4183 | return NULL; |
| 4184 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4185 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4187 | self = datetime_from_timestamp(cls, |
| 4188 | tzinfo == Py_None ? localtime : gmtime, |
| 4189 | timestamp, |
| 4190 | tzinfo); |
| 4191 | if (self != NULL && tzinfo != Py_None) { |
| 4192 | /* Convert UTC to tzinfo's zone. */ |
| 4193 | PyObject *temp = self; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4194 | |
| 4195 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 | Py_DECREF(temp); |
| 4197 | } |
| 4198 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4201 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 4202 | static PyObject * |
| 4203 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 4204 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4205 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4206 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4207 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4208 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4209 | result = datetime_from_timestamp(cls, gmtime, timestamp, |
| 4210 | Py_None); |
| 4211 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4214 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4215 | static PyObject * |
| 4216 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4218 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4219 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4220 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4221 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4222 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4223 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4224 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4225 | if (module == NULL) { |
| 4226 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4227 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4228 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4229 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4230 | return _PyObject_CallMethodId(module, &PyId__strptime_datetime, "OOO", |
| 4231 | cls, string, format); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4234 | /* Return new datetime from date/datetime and time arguments. */ |
| 4235 | static PyObject * |
| 4236 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | static char *keywords[] = {"date", "time", NULL}; |
| 4239 | PyObject *date; |
| 4240 | PyObject *time; |
| 4241 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, |
| 4244 | &PyDateTime_DateType, &date, |
| 4245 | &PyDateTime_TimeType, &time)) { |
| 4246 | PyObject *tzinfo = Py_None; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | if (HASTZINFO(time)) |
| 4249 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4250 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 4251 | GET_YEAR(date), |
| 4252 | GET_MONTH(date), |
| 4253 | GET_DAY(date), |
| 4254 | TIME_GET_HOUR(time), |
| 4255 | TIME_GET_MINUTE(time), |
| 4256 | TIME_GET_SECOND(time), |
| 4257 | TIME_GET_MICROSECOND(time), |
| 4258 | tzinfo); |
| 4259 | } |
| 4260 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4261 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4262 | |
| 4263 | /* |
| 4264 | * Destructor. |
| 4265 | */ |
| 4266 | |
| 4267 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4268 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4270 | if (HASTZINFO(self)) { |
| 4271 | Py_XDECREF(self->tzinfo); |
| 4272 | } |
| 4273 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | /* |
| 4277 | * Indirect access to tzinfo methods. |
| 4278 | */ |
| 4279 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4280 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4281 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4282 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 4283 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4287 | datetime_dst(PyObject *self, PyObject *unused) { |
| 4288 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4292 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 4293 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4297 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4298 | */ |
| 4299 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4300 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4301 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4302 | */ |
| 4303 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4304 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4305 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | /* Note that the C-level additions can't overflow, because of |
| 4308 | * invariant bounds on the member values. |
| 4309 | */ |
| 4310 | int year = GET_YEAR(date); |
| 4311 | int month = GET_MONTH(date); |
| 4312 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4313 | int hour = DATE_GET_HOUR(date); |
| 4314 | int minute = DATE_GET_MINUTE(date); |
| 4315 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4316 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4317 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | assert(factor == 1 || factor == -1); |
| 4320 | if (normalize_datetime(&year, &month, &day, |
| 4321 | &hour, &minute, &second, µsecond) < 0) |
| 4322 | return NULL; |
| 4323 | else |
| 4324 | return new_datetime(year, month, day, |
| 4325 | hour, minute, second, microsecond, |
| 4326 | HASTZINFO(date) ? date->tzinfo : Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4330 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4332 | if (PyDateTime_Check(left)) { |
| 4333 | /* datetime + ??? */ |
| 4334 | if (PyDelta_Check(right)) |
| 4335 | /* datetime + delta */ |
| 4336 | return add_datetime_timedelta( |
| 4337 | (PyDateTime_DateTime *)left, |
| 4338 | (PyDateTime_Delta *)right, |
| 4339 | 1); |
| 4340 | } |
| 4341 | else if (PyDelta_Check(left)) { |
| 4342 | /* delta + datetime */ |
| 4343 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4344 | (PyDateTime_Delta *) left, |
| 4345 | 1); |
| 4346 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4347 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4351 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4355 | if (PyDateTime_Check(left)) { |
| 4356 | /* datetime - ??? */ |
| 4357 | if (PyDateTime_Check(right)) { |
| 4358 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4359 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4360 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4361 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4362 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 4363 | offset2 = offset1 = Py_None; |
| 4364 | Py_INCREF(offset1); |
| 4365 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4367 | else { |
| 4368 | offset1 = datetime_utcoffset(left, NULL); |
| 4369 | if (offset1 == NULL) |
| 4370 | return NULL; |
| 4371 | offset2 = datetime_utcoffset(right, NULL); |
| 4372 | if (offset2 == NULL) { |
| 4373 | Py_DECREF(offset1); |
| 4374 | return NULL; |
| 4375 | } |
| 4376 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 4377 | PyErr_SetString(PyExc_TypeError, |
| 4378 | "can't subtract offset-naive and " |
| 4379 | "offset-aware datetimes"); |
| 4380 | Py_DECREF(offset1); |
| 4381 | Py_DECREF(offset2); |
| 4382 | return NULL; |
| 4383 | } |
| 4384 | } |
| 4385 | if ((offset1 != offset2) && |
| 4386 | delta_cmp(offset1, offset2) != 0) { |
| 4387 | offdiff = delta_subtract(offset1, offset2); |
| 4388 | if (offdiff == NULL) { |
| 4389 | Py_DECREF(offset1); |
| 4390 | Py_DECREF(offset2); |
| 4391 | return NULL; |
| 4392 | } |
| 4393 | } |
| 4394 | Py_DECREF(offset1); |
| 4395 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4397 | GET_MONTH(left), |
| 4398 | GET_DAY(left)) - |
| 4399 | ymd_to_ord(GET_YEAR(right), |
| 4400 | GET_MONTH(right), |
| 4401 | GET_DAY(right)); |
| 4402 | /* These can't overflow, since the values are |
| 4403 | * normalized. At most this gives the number of |
| 4404 | * seconds in one day. |
| 4405 | */ |
| 4406 | delta_s = (DATE_GET_HOUR(left) - |
| 4407 | DATE_GET_HOUR(right)) * 3600 + |
| 4408 | (DATE_GET_MINUTE(left) - |
| 4409 | DATE_GET_MINUTE(right)) * 60 + |
| 4410 | (DATE_GET_SECOND(left) - |
| 4411 | DATE_GET_SECOND(right)); |
| 4412 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4413 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame] | 4415 | if (result == NULL) |
| 4416 | return NULL; |
| 4417 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4418 | if (offdiff != NULL) { |
| 4419 | PyObject *temp = result; |
| 4420 | result = delta_subtract(result, offdiff); |
| 4421 | Py_DECREF(temp); |
| 4422 | Py_DECREF(offdiff); |
| 4423 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4424 | } |
| 4425 | else if (PyDelta_Check(right)) { |
| 4426 | /* datetime - delta */ |
| 4427 | result = add_datetime_timedelta( |
| 4428 | (PyDateTime_DateTime *)left, |
| 4429 | (PyDateTime_Delta *)right, |
| 4430 | -1); |
| 4431 | } |
| 4432 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4434 | if (result == Py_NotImplemented) |
| 4435 | Py_INCREF(result); |
| 4436 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | /* Various ways to turn a datetime into a string. */ |
| 4440 | |
| 4441 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4442 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4445 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | if (DATE_GET_MICROSECOND(self)) { |
| 4448 | baserepr = PyUnicode_FromFormat( |
| 4449 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 4450 | type_name, |
| 4451 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4452 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4453 | DATE_GET_SECOND(self), |
| 4454 | DATE_GET_MICROSECOND(self)); |
| 4455 | } |
| 4456 | else if (DATE_GET_SECOND(self)) { |
| 4457 | baserepr = PyUnicode_FromFormat( |
| 4458 | "%s(%d, %d, %d, %d, %d, %d)", |
| 4459 | type_name, |
| 4460 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4461 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4462 | DATE_GET_SECOND(self)); |
| 4463 | } |
| 4464 | else { |
| 4465 | baserepr = PyUnicode_FromFormat( |
| 4466 | "%s(%d, %d, %d, %d, %d)", |
| 4467 | type_name, |
| 4468 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4469 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4470 | } |
| 4471 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4472 | return baserepr; |
| 4473 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4476 | static PyObject * |
| 4477 | datetime_str(PyDateTime_DateTime *self) |
| 4478 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4479 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "(s)", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4480 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4481 | |
| 4482 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4483 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4484 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | int sep = 'T'; |
| 4486 | static char *keywords[] = {"sep", NULL}; |
| 4487 | char buffer[100]; |
| 4488 | PyObject *result; |
| 4489 | int us = DATE_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep)) |
| 4492 | return NULL; |
| 4493 | if (us) |
| 4494 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d", |
| 4495 | GET_YEAR(self), GET_MONTH(self), |
| 4496 | GET_DAY(self), (int)sep, |
| 4497 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4498 | DATE_GET_SECOND(self), us); |
| 4499 | else |
| 4500 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d", |
| 4501 | GET_YEAR(self), GET_MONTH(self), |
| 4502 | GET_DAY(self), (int)sep, |
| 4503 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4504 | DATE_GET_SECOND(self)); |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | if (!result || !HASTZINFO(self)) |
| 4507 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4509 | /* We need to append the UTC offset. */ |
| 4510 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 4511 | (PyObject *)self) < 0) { |
| 4512 | Py_DECREF(result); |
| 4513 | return NULL; |
| 4514 | } |
| 4515 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 4516 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4519 | static PyObject * |
| 4520 | datetime_ctime(PyDateTime_DateTime *self) |
| 4521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4522 | return format_ctime((PyDateTime_Date *)self, |
| 4523 | DATE_GET_HOUR(self), |
| 4524 | DATE_GET_MINUTE(self), |
| 4525 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4528 | /* Miscellaneous methods. */ |
| 4529 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4530 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4531 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4532 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4533 | PyObject *result = NULL; |
| 4534 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4537 | if (! PyDateTime_Check(other)) { |
| 4538 | if (PyDate_Check(other)) { |
| 4539 | /* Prevent invocation of date_richcompare. We want to |
| 4540 | return NotImplemented here to give the other object |
| 4541 | a chance. But since DateTime is a subclass of |
| 4542 | Date, if the other object is a Date, it would |
| 4543 | compute an ordering based on the date part alone, |
| 4544 | and we don't want that. So force unequal or |
| 4545 | uncomparable here in that case. */ |
| 4546 | if (op == Py_EQ) |
| 4547 | Py_RETURN_FALSE; |
| 4548 | if (op == Py_NE) |
| 4549 | Py_RETURN_TRUE; |
| 4550 | return cmperror(self, other); |
| 4551 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4552 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4554 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4555 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4556 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4557 | ((PyDateTime_DateTime *)other)->data, |
| 4558 | _PyDateTime_DATETIME_DATASIZE); |
| 4559 | return diff_to_bool(diff, op); |
| 4560 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4561 | offset1 = datetime_utcoffset(self, NULL); |
| 4562 | if (offset1 == NULL) |
| 4563 | return NULL; |
| 4564 | offset2 = datetime_utcoffset(other, NULL); |
| 4565 | if (offset2 == NULL) |
| 4566 | goto done; |
| 4567 | /* If they're both naive, or both aware and have the same offsets, |
| 4568 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4569 | * offset2 == Py_None at this point. |
| 4570 | */ |
| 4571 | if ((offset1 == offset2) || |
| 4572 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4573 | delta_cmp(offset1, offset2) == 0)) { |
| 4574 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4575 | ((PyDateTime_DateTime *)other)->data, |
| 4576 | _PyDateTime_DATETIME_DATASIZE); |
| 4577 | result = diff_to_bool(diff, op); |
| 4578 | } |
| 4579 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4581 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4582 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4583 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4584 | other); |
| 4585 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4586 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4587 | diff = GET_TD_DAYS(delta); |
| 4588 | if (diff == 0) |
| 4589 | diff = GET_TD_SECONDS(delta) | |
| 4590 | GET_TD_MICROSECONDS(delta); |
| 4591 | Py_DECREF(delta); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4592 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4593 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4594 | else if (op == Py_EQ) { |
| 4595 | result = Py_False; |
| 4596 | Py_INCREF(result); |
| 4597 | } |
| 4598 | else if (op == Py_NE) { |
| 4599 | result = Py_True; |
| 4600 | Py_INCREF(result); |
| 4601 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4602 | else { |
| 4603 | PyErr_SetString(PyExc_TypeError, |
| 4604 | "can't compare offset-naive and " |
| 4605 | "offset-aware datetimes"); |
| 4606 | } |
| 4607 | done: |
| 4608 | Py_DECREF(offset1); |
| 4609 | Py_XDECREF(offset2); |
| 4610 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4613 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4614 | datetime_hash(PyDateTime_DateTime *self) |
| 4615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4617 | PyObject *offset; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4618 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4619 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4620 | |
| 4621 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4624 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4625 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | self->hashcode = generic_hash( |
| 4627 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4628 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4629 | PyObject *temp1, *temp2; |
| 4630 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4632 | assert(HASTZINFO(self)); |
| 4633 | days = ymd_to_ord(GET_YEAR(self), |
| 4634 | GET_MONTH(self), |
| 4635 | GET_DAY(self)); |
| 4636 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4637 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4639 | temp1 = new_delta(days, seconds, |
| 4640 | DATE_GET_MICROSECOND(self), |
| 4641 | 1); |
| 4642 | if (temp1 == NULL) { |
| 4643 | Py_DECREF(offset); |
| 4644 | return -1; |
| 4645 | } |
| 4646 | temp2 = delta_subtract(temp1, offset); |
| 4647 | Py_DECREF(temp1); |
| 4648 | if (temp2 == NULL) { |
| 4649 | Py_DECREF(offset); |
| 4650 | return -1; |
| 4651 | } |
| 4652 | self->hashcode = PyObject_Hash(temp2); |
| 4653 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4655 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4656 | } |
| 4657 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4658 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4659 | |
| 4660 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4661 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | PyObject *clone; |
| 4664 | PyObject *tuple; |
| 4665 | int y = GET_YEAR(self); |
| 4666 | int m = GET_MONTH(self); |
| 4667 | int d = GET_DAY(self); |
| 4668 | int hh = DATE_GET_HOUR(self); |
| 4669 | int mm = DATE_GET_MINUTE(self); |
| 4670 | int ss = DATE_GET_SECOND(self); |
| 4671 | int us = DATE_GET_MICROSECOND(self); |
| 4672 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", |
| 4675 | datetime_kws, |
| 4676 | &y, &m, &d, &hh, &mm, &ss, &us, |
| 4677 | &tzinfo)) |
| 4678 | return NULL; |
| 4679 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 4680 | if (tuple == NULL) |
| 4681 | return NULL; |
| 4682 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
| 4683 | Py_DECREF(tuple); |
| 4684 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | static PyObject * |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4688 | local_timezone(PyDateTime_DateTime *utc_time) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4689 | { |
| 4690 | PyObject *result = NULL; |
| 4691 | struct tm *timep; |
| 4692 | time_t timestamp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4693 | PyObject *delta; |
| 4694 | PyObject *one_second; |
| 4695 | PyObject *seconds; |
| 4696 | PyObject *nameo = NULL; |
| 4697 | const char *zone = NULL; |
| 4698 | |
| 4699 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 4700 | if (delta == NULL) |
| 4701 | return NULL; |
| 4702 | one_second = new_delta(0, 1, 0, 0); |
| 4703 | if (one_second == NULL) |
| 4704 | goto error; |
| 4705 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 4706 | (PyDateTime_Delta *)one_second); |
| 4707 | Py_DECREF(one_second); |
| 4708 | if (seconds == NULL) |
| 4709 | goto error; |
| 4710 | Py_DECREF(delta); |
| 4711 | timestamp = PyLong_AsLong(seconds); |
| 4712 | Py_DECREF(seconds); |
| 4713 | if (timestamp == -1 && PyErr_Occurred()) |
| 4714 | return NULL; |
| 4715 | timep = localtime(×tamp); |
| 4716 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4717 | zone = timep->tm_zone; |
| 4718 | delta = new_delta(0, timep->tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4719 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4720 | { |
| 4721 | PyObject *local_time; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4722 | local_time = new_datetime(timep->tm_year + 1900, timep->tm_mon + 1, |
| 4723 | timep->tm_mday, timep->tm_hour, timep->tm_min, |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4724 | timep->tm_sec, DATE_GET_MICROSECOND(utc_time), |
| 4725 | utc_time->tzinfo); |
| 4726 | if (local_time == NULL) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4727 | goto error; |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4728 | delta = datetime_subtract(local_time, (PyObject*)utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4729 | /* XXX: before relying on tzname, we should compare delta |
| 4730 | to the offset implied by timezone/altzone */ |
| 4731 | if (daylight && timep->tm_isdst >= 0) |
| 4732 | zone = tzname[timep->tm_isdst % 2]; |
| 4733 | else |
| 4734 | zone = tzname[0]; |
| 4735 | Py_DECREF(local_time); |
| 4736 | } |
| 4737 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4738 | if (zone != NULL) { |
| 4739 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 4740 | if (nameo == NULL) |
| 4741 | goto error; |
| 4742 | } |
| 4743 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 4744 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4745 | error: |
| 4746 | Py_DECREF(delta); |
| 4747 | return result; |
| 4748 | } |
| 4749 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4750 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4751 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4752 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4753 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4754 | PyObject *offset; |
| 4755 | PyObject *temp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4756 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4757 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4758 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4759 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
Raymond Hettinger | 5a2146a | 2014-07-25 14:59:48 -0700 | [diff] [blame] | 4760 | &tzinfo)) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4761 | return NULL; |
| 4762 | |
| 4763 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4766 | if (!HASTZINFO(self) || self->tzinfo == Py_None) |
| 4767 | goto NeedAware; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4769 | /* Conversion to self's own time zone is a NOP. */ |
| 4770 | if (self->tzinfo == tzinfo) { |
| 4771 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4772 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4773 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | /* Convert self to UTC. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4776 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4777 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4779 | if (offset == Py_None) { |
| 4780 | Py_DECREF(offset); |
| 4781 | NeedAware: |
| 4782 | PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " |
| 4783 | "a naive datetime"); |
| 4784 | return NULL; |
| 4785 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4786 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4787 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4788 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4789 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4790 | Py_DECREF(offset); |
| 4791 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4792 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4794 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4795 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4796 | if (tzinfo == Py_None) { |
| 4797 | tzinfo = local_timezone(result); |
| 4798 | if (tzinfo == NULL) { |
| 4799 | Py_DECREF(result); |
| 4800 | return NULL; |
| 4801 | } |
| 4802 | } |
| 4803 | else |
| 4804 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4805 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4806 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4807 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4808 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4809 | result = (PyDateTime_DateTime *) |
| 4810 | _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", temp); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4811 | Py_DECREF(temp); |
| 4812 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4813 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4817 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4818 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4819 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4821 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4822 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4823 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4824 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 4825 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4826 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4827 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4828 | if (dst != Py_None) |
| 4829 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 4830 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4831 | } |
| 4832 | return build_struct_time(GET_YEAR(self), |
| 4833 | GET_MONTH(self), |
| 4834 | GET_DAY(self), |
| 4835 | DATE_GET_HOUR(self), |
| 4836 | DATE_GET_MINUTE(self), |
| 4837 | DATE_GET_SECOND(self), |
| 4838 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | static PyObject * |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 4842 | datetime_timestamp(PyDateTime_DateTime *self) |
| 4843 | { |
| 4844 | PyObject *result; |
| 4845 | |
| 4846 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 4847 | PyObject *delta; |
| 4848 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 4849 | if (delta == NULL) |
| 4850 | return NULL; |
| 4851 | result = delta_total_seconds(delta); |
| 4852 | Py_DECREF(delta); |
| 4853 | } |
| 4854 | else { |
| 4855 | struct tm time; |
| 4856 | time_t timestamp; |
| 4857 | memset((void *) &time, '\0', sizeof(struct tm)); |
| 4858 | time.tm_year = GET_YEAR(self) - 1900; |
| 4859 | time.tm_mon = GET_MONTH(self) - 1; |
| 4860 | time.tm_mday = GET_DAY(self); |
| 4861 | time.tm_hour = DATE_GET_HOUR(self); |
| 4862 | time.tm_min = DATE_GET_MINUTE(self); |
| 4863 | time.tm_sec = DATE_GET_SECOND(self); |
| 4864 | time.tm_wday = -1; |
| 4865 | time.tm_isdst = -1; |
| 4866 | timestamp = mktime(&time); |
Victor Stinner | 9303749 | 2013-06-25 22:54:35 +0200 | [diff] [blame] | 4867 | if (timestamp == (time_t)(-1) |
| 4868 | #ifndef _AIX |
| 4869 | /* Return value of -1 does not necessarily mean an error, |
| 4870 | * but tm_wday cannot remain set to -1 if mktime succeeded. */ |
| 4871 | && time.tm_wday == -1 |
| 4872 | #else |
| 4873 | /* on AIX, tm_wday is always sets, even on error */ |
| 4874 | #endif |
| 4875 | ) |
| 4876 | { |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 4877 | PyErr_SetString(PyExc_OverflowError, |
| 4878 | "timestamp out of range"); |
| 4879 | return NULL; |
| 4880 | } |
| 4881 | result = PyFloat_FromDouble(timestamp + DATE_GET_MICROSECOND(self) / 1e6); |
| 4882 | } |
| 4883 | return result; |
| 4884 | } |
| 4885 | |
| 4886 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4887 | datetime_getdate(PyDateTime_DateTime *self) |
| 4888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | return new_date(GET_YEAR(self), |
| 4890 | GET_MONTH(self), |
| 4891 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | static PyObject * |
| 4895 | datetime_gettime(PyDateTime_DateTime *self) |
| 4896 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4897 | return new_time(DATE_GET_HOUR(self), |
| 4898 | DATE_GET_MINUTE(self), |
| 4899 | DATE_GET_SECOND(self), |
| 4900 | DATE_GET_MICROSECOND(self), |
| 4901 | Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | static PyObject * |
| 4905 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 4906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4907 | return new_time(DATE_GET_HOUR(self), |
| 4908 | DATE_GET_MINUTE(self), |
| 4909 | DATE_GET_SECOND(self), |
| 4910 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4911 | GET_DT_TZINFO(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | static PyObject * |
| 4915 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4916 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4917 | int y, m, d, hh, mm, ss; |
| 4918 | PyObject *tzinfo; |
| 4919 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4920 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4921 | tzinfo = GET_DT_TZINFO(self); |
| 4922 | if (tzinfo == Py_None) { |
| 4923 | utcself = self; |
| 4924 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4925 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4926 | else { |
| 4927 | PyObject *offset; |
| 4928 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 4929 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 4930 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4931 | if (offset == Py_None) { |
| 4932 | Py_DECREF(offset); |
| 4933 | utcself = self; |
| 4934 | Py_INCREF(utcself); |
| 4935 | } |
| 4936 | else { |
| 4937 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4938 | (PyDateTime_Delta *)offset, -1); |
| 4939 | Py_DECREF(offset); |
| 4940 | if (utcself == NULL) |
| 4941 | return NULL; |
| 4942 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4944 | y = GET_YEAR(utcself); |
| 4945 | m = GET_MONTH(utcself); |
| 4946 | d = GET_DAY(utcself); |
| 4947 | hh = DATE_GET_HOUR(utcself); |
| 4948 | mm = DATE_GET_MINUTE(utcself); |
| 4949 | ss = DATE_GET_SECOND(utcself); |
| 4950 | |
| 4951 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4952 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4955 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4956 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4957 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4958 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4959 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4960 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4961 | */ |
| 4962 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4963 | datetime_getstate(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4964 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4965 | PyObject *basestate; |
| 4966 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4968 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 4969 | _PyDateTime_DATETIME_DATASIZE); |
| 4970 | if (basestate != NULL) { |
| 4971 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 4972 | result = PyTuple_Pack(1, basestate); |
| 4973 | else |
| 4974 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 4975 | Py_DECREF(basestate); |
| 4976 | } |
| 4977 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
| 4980 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4981 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4986 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4988 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4989 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 4990 | DATETIME_DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 4993 | METH_NOARGS | METH_CLASS, |
| 4994 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 4997 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 4998 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5000 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 5001 | METH_VARARGS | METH_CLASS, |
Alexander Belopolsky | e2e178e | 2015-03-01 14:52:07 -0500 | [diff] [blame] | 5002 | PyDoc_STR("Construct a naive UTC datetime from a POSIX timestamp.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | {"strptime", (PyCFunction)datetime_strptime, |
| 5005 | METH_VARARGS | METH_CLASS, |
| 5006 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 5007 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | {"combine", (PyCFunction)datetime_combine, |
| 5010 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5011 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 5016 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5018 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 5019 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 5022 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5024 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 5025 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5027 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 5028 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5029 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5030 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 5031 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 5032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5033 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 5034 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5036 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 5037 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
| 5038 | "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" |
| 5039 | "sep is used to separate the year from the time, and " |
| 5040 | "defaults to 'T'.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5042 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 5043 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5045 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 5046 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 5049 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5051 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 5052 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5054 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 5055 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5057 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 5058 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5060 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5061 | }; |
| 5062 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5063 | static char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5064 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 5065 | \n\ |
| 5066 | 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] | 5067 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5068 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5069 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | datetime_add, /* nb_add */ |
| 5071 | datetime_subtract, /* nb_subtract */ |
| 5072 | 0, /* nb_multiply */ |
| 5073 | 0, /* nb_remainder */ |
| 5074 | 0, /* nb_divmod */ |
| 5075 | 0, /* nb_power */ |
| 5076 | 0, /* nb_negative */ |
| 5077 | 0, /* nb_positive */ |
| 5078 | 0, /* nb_absolute */ |
| 5079 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5080 | }; |
| 5081 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5082 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5084 | "datetime.datetime", /* tp_name */ |
| 5085 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 5086 | 0, /* tp_itemsize */ |
| 5087 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 5088 | 0, /* tp_print */ |
| 5089 | 0, /* tp_getattr */ |
| 5090 | 0, /* tp_setattr */ |
| 5091 | 0, /* tp_reserved */ |
| 5092 | (reprfunc)datetime_repr, /* tp_repr */ |
| 5093 | &datetime_as_number, /* tp_as_number */ |
| 5094 | 0, /* tp_as_sequence */ |
| 5095 | 0, /* tp_as_mapping */ |
| 5096 | (hashfunc)datetime_hash, /* tp_hash */ |
| 5097 | 0, /* tp_call */ |
| 5098 | (reprfunc)datetime_str, /* tp_str */ |
| 5099 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5100 | 0, /* tp_setattro */ |
| 5101 | 0, /* tp_as_buffer */ |
| 5102 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5103 | datetime_doc, /* tp_doc */ |
| 5104 | 0, /* tp_traverse */ |
| 5105 | 0, /* tp_clear */ |
| 5106 | datetime_richcompare, /* tp_richcompare */ |
| 5107 | 0, /* tp_weaklistoffset */ |
| 5108 | 0, /* tp_iter */ |
| 5109 | 0, /* tp_iternext */ |
| 5110 | datetime_methods, /* tp_methods */ |
| 5111 | 0, /* tp_members */ |
| 5112 | datetime_getset, /* tp_getset */ |
| 5113 | &PyDateTime_DateType, /* tp_base */ |
| 5114 | 0, /* tp_dict */ |
| 5115 | 0, /* tp_descr_get */ |
| 5116 | 0, /* tp_descr_set */ |
| 5117 | 0, /* tp_dictoffset */ |
| 5118 | 0, /* tp_init */ |
| 5119 | datetime_alloc, /* tp_alloc */ |
| 5120 | datetime_new, /* tp_new */ |
| 5121 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5122 | }; |
| 5123 | |
| 5124 | /* --------------------------------------------------------------------------- |
| 5125 | * Module methods and initialization. |
| 5126 | */ |
| 5127 | |
| 5128 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5129 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5130 | }; |
| 5131 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5132 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 5133 | * datetime.h. |
| 5134 | */ |
| 5135 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5136 | &PyDateTime_DateType, |
| 5137 | &PyDateTime_DateTimeType, |
| 5138 | &PyDateTime_TimeType, |
| 5139 | &PyDateTime_DeltaType, |
| 5140 | &PyDateTime_TZInfoType, |
| 5141 | new_date_ex, |
| 5142 | new_datetime_ex, |
| 5143 | new_time_ex, |
| 5144 | new_delta_ex, |
| 5145 | datetime_fromtimestamp, |
| 5146 | date_fromtimestamp |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5147 | }; |
| 5148 | |
| 5149 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5150 | |
| 5151 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5153 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5154 | "Fast implementation of the datetime type.", |
| 5155 | -1, |
| 5156 | module_methods, |
| 5157 | NULL, |
| 5158 | NULL, |
| 5159 | NULL, |
| 5160 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5161 | }; |
| 5162 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5163 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5164 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5166 | PyObject *m; /* a module object */ |
| 5167 | PyObject *d; /* its dict */ |
| 5168 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5169 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | m = PyModule_Create(&datetimemodule); |
| 5172 | if (m == NULL) |
| 5173 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 5176 | return NULL; |
| 5177 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 5178 | return NULL; |
| 5179 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 5180 | return NULL; |
| 5181 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 5182 | return NULL; |
| 5183 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 5184 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5185 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 5186 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5188 | /* timedelta values */ |
| 5189 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5191 | x = new_delta(0, 0, 1, 0); |
| 5192 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5193 | return NULL; |
| 5194 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5196 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 5197 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5198 | return NULL; |
| 5199 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5201 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 5202 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5203 | return NULL; |
| 5204 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | /* date values */ |
| 5207 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | x = new_date(1, 1, 1); |
| 5210 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5211 | return NULL; |
| 5212 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5214 | x = new_date(MAXYEAR, 12, 31); |
| 5215 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5216 | return NULL; |
| 5217 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5219 | x = new_delta(1, 0, 0, 0); |
| 5220 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5221 | return NULL; |
| 5222 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5224 | /* time values */ |
| 5225 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5227 | x = new_time(0, 0, 0, 0, Py_None); |
| 5228 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5229 | return NULL; |
| 5230 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | x = new_time(23, 59, 59, 999999, Py_None); |
| 5233 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5234 | return NULL; |
| 5235 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5237 | x = new_delta(0, 0, 1, 0); |
| 5238 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5239 | return NULL; |
| 5240 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5242 | /* datetime values */ |
| 5243 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5245 | x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); |
| 5246 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5247 | return NULL; |
| 5248 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); |
| 5251 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5252 | return NULL; |
| 5253 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | x = new_delta(0, 0, 1, 0); |
| 5256 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5257 | return NULL; |
| 5258 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5259 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5260 | /* timezone values */ |
| 5261 | d = PyDateTime_TimeZoneType.tp_dict; |
| 5262 | |
| 5263 | delta = new_delta(0, 0, 0, 0); |
| 5264 | if (delta == NULL) |
| 5265 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5266 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5267 | Py_DECREF(delta); |
| 5268 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 5269 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 5270 | PyDateTime_TimeZone_UTC = x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5271 | |
| 5272 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 5273 | if (delta == NULL) |
| 5274 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5275 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5276 | Py_DECREF(delta); |
| 5277 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5278 | return NULL; |
| 5279 | Py_DECREF(x); |
| 5280 | |
| 5281 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 5282 | if (delta == NULL) |
| 5283 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5284 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5285 | Py_DECREF(delta); |
| 5286 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5287 | return NULL; |
| 5288 | Py_DECREF(x); |
| 5289 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5290 | /* Epoch */ |
| 5291 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
| 5292 | PyDateTime_TimeZone_UTC); |
| 5293 | if (PyDateTime_Epoch == NULL) |
| 5294 | return NULL; |
| 5295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5296 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5297 | PyModule_AddIntMacro(m, MINYEAR); |
| 5298 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5300 | Py_INCREF(&PyDateTime_DateType); |
| 5301 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5303 | Py_INCREF(&PyDateTime_DateTimeType); |
| 5304 | PyModule_AddObject(m, "datetime", |
| 5305 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5307 | Py_INCREF(&PyDateTime_TimeType); |
| 5308 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | Py_INCREF(&PyDateTime_DeltaType); |
| 5311 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5313 | Py_INCREF(&PyDateTime_TZInfoType); |
| 5314 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5315 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5316 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 5317 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 5318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5319 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 5320 | if (x == NULL) |
| 5321 | return NULL; |
| 5322 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 5325 | * pasting together 4 single years. |
| 5326 | */ |
| 5327 | assert(DI4Y == 4 * 365 + 1); |
| 5328 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 5331 | * get from pasting together 4 100-year cycles. |
| 5332 | */ |
| 5333 | assert(DI400Y == 4 * DI100Y + 1); |
| 5334 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5336 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 5337 | * pasting together 25 4-year cycles. |
| 5338 | */ |
| 5339 | assert(DI100Y == 25 * DI4Y - 1); |
| 5340 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5341 | |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5342 | one = PyLong_FromLong(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5343 | us_per_ms = PyLong_FromLong(1000); |
| 5344 | us_per_second = PyLong_FromLong(1000000); |
| 5345 | us_per_minute = PyLong_FromLong(60000000); |
| 5346 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5347 | if (one == NULL || us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5348 | us_per_minute == NULL || seconds_per_day == NULL) |
| 5349 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5351 | /* The rest are too big for 32-bit ints, but even |
| 5352 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 5353 | */ |
| 5354 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 5355 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 5356 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 5357 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 5358 | return NULL; |
| 5359 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5360 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5361 | |
| 5362 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5363 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5364 | x.n = x stripped of its timezone -- its naive time. |
| 5365 | 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] | 5366 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5367 | 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] | 5368 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5369 | x.s = x's standard offset, x.o - x.d |
| 5370 | |
| 5371 | Now some derived rules, where k is a duration (timedelta). |
| 5372 | |
| 5373 | 1. x.o = x.s + x.d |
| 5374 | This follows from the definition of x.s. |
| 5375 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5376 | 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] | 5377 | This is actually a requirement, an assumption we need to make about |
| 5378 | sane tzinfo classes. |
| 5379 | |
| 5380 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 5381 | This is again a requirement for a sane tzinfo class. |
| 5382 | |
| 5383 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5384 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5385 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5386 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5387 | Again follows from how arithmetic is defined. |
| 5388 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5389 | 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] | 5390 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 5391 | None when called). |
| 5392 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5393 | 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] | 5394 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5395 | |
| 5396 | By #3, we want |
| 5397 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5398 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5399 | |
| 5400 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 5401 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 5402 | becomes true; in effect, we want to solve [2] for k: |
| 5403 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5404 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5405 | |
| 5406 | By #1, this is the same as |
| 5407 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5408 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5409 | |
| 5410 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 5411 | Substituting that into [3], |
| 5412 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5413 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 5414 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 5415 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 5416 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5417 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5418 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 5419 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 5420 | very large, since all offset-returning methods return a duration of magnitude |
| 5421 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 5422 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5423 | |
| 5424 | In any case, the new value is |
| 5425 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5426 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5427 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5428 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 5429 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5430 | |
| 5431 | At this point, if |
| 5432 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5433 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5434 | |
| 5435 | 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] | 5436 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 5437 | 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] | 5438 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 5439 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 5440 | 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] | 5441 | the only spelling that makes sense on the local wall clock. |
| 5442 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5443 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 5444 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 5445 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5446 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5447 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5448 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5449 | Now |
| 5450 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5451 | (y + y.s).n = by #5 |
| 5452 | y.n + y.s = since y.n = x.n |
| 5453 | x.n + y.s = since z and y are have the same tzinfo member, |
| 5454 | y.s = z.s by #2 |
| 5455 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5456 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5457 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5458 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5459 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5460 | x.n - ((x.n + z.s) - z.o) = expanding |
| 5461 | x.n - x.n - z.s + z.o = cancelling |
| 5462 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5463 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5464 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5465 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5466 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5467 | 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] | 5468 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 5469 | 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] | 5470 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5471 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 5472 | 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] | 5473 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5474 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5475 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5476 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5477 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5478 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5479 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5480 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5481 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5482 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5483 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 5484 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 5485 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 5486 | the justifications for the kinds of substitutions we've done several times |
| 5487 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5488 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5489 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5490 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 5491 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 5492 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 5493 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 5494 | - z.o + z'.o = #1 twice |
| 5495 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 5496 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5497 | |
| 5498 | 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] | 5499 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 5500 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5501 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5502 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 5503 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 5504 | would have to change the result dst() returns: we start in DST, and moving |
| 5505 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5506 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5507 | There isn't a sane case where this can happen. The closest it gets is at |
| 5508 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 5509 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 5510 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 5511 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 5512 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 5513 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 5514 | standard time. Since that's what the local clock *does*, we want to map both |
| 5515 | 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] | 5516 | in local time, but so it goes -- it's the way the local clock works. |
| 5517 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5518 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 5519 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 5520 | 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] | 5521 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 5522 | |
| 5523 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 5524 | 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] | 5525 | 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] | 5526 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 5527 | but the reasoning doesn't depend on the example -- it depends on there being |
| 5528 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5529 | z' must be in standard time, and is the spelling we want in this case. |
| 5530 | |
| 5531 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 5532 | concerned (because it takes z' as being in standard time rather than the |
| 5533 | daylight time we intend here), but returning it gives the real-life "local |
| 5534 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 5535 | tz. |
| 5536 | |
| 5537 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 5538 | the 1:MM standard time spelling we want. |
| 5539 | |
| 5540 | So how can this break? One of the assumptions must be violated. Two |
| 5541 | possibilities: |
| 5542 | |
| 5543 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 5544 | time zone. This isn't true if, for political reasons or continental drift, |
| 5545 | a region decides to change its base offset from UTC. |
| 5546 | |
| 5547 | 2) There may be versions of "double daylight" time where the tail end of |
| 5548 | the analysis gives up a step too early. I haven't thought about that |
| 5549 | enough to say. |
| 5550 | |
| 5551 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 5552 | "almost all" time zones: so long as the standard offset is invariant, it |
| 5553 | doesn't matter if daylight time transition points change from year to year, or |
| 5554 | if daylight time is skipped in some years; it doesn't matter how large or |
| 5555 | small dst() may get within its bounds; and it doesn't even matter if some |
| 5556 | perverse time zone returns a negative dst()). So a breaking case must be |
| 5557 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5558 | --------------------------------------------------------------------------- */ |