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 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 10 | /* Differentiate between building the core module and building extension |
| 11 | * modules. |
| 12 | */ |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 13 | #ifndef Py_BUILD_CORE |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 14 | #define Py_BUILD_CORE |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 15 | #endif |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 16 | #include "datetime.h" |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 17 | #undef Py_BUILD_CORE |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 18 | |
| 19 | /* We require that C int be at least 32 bits, and use int virtually |
| 20 | * everywhere. In just a few cases we use a temp long, where a Python |
| 21 | * API returns a C long. In such cases, we have to ensure that the |
| 22 | * final result fits in a C int (this can be an issue on 64-bit boxes). |
| 23 | */ |
| 24 | #if SIZEOF_INT < 4 |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 25 | # error "_datetime.c requires that C int have at least 32 bits" |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #define MINYEAR 1 |
| 29 | #define MAXYEAR 9999 |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 30 | #define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 31 | |
| 32 | /* Nine decimal digits is easy to communicate, and leaves enough room |
| 33 | * so that two delta days can be added w/o fear of overflowing a signed |
| 34 | * 32-bit int, and with plenty of room left over to absorb any possible |
| 35 | * carries from adding seconds. |
| 36 | */ |
| 37 | #define MAX_DELTA_DAYS 999999999 |
| 38 | |
| 39 | /* Rename the long macros in datetime.h to more reasonable short names. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | #define GET_YEAR PyDateTime_GET_YEAR |
| 41 | #define GET_MONTH PyDateTime_GET_MONTH |
| 42 | #define GET_DAY PyDateTime_GET_DAY |
| 43 | #define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR |
| 44 | #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE |
| 45 | #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND |
| 46 | #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 47 | |
| 48 | /* Date accessors for date and datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ |
| 50 | ((o)->data[1] = ((v) & 0x00ff))) |
| 51 | #define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v)) |
| 52 | #define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 53 | |
| 54 | /* Date/Time accessors for datetime. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | #define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v)) |
| 56 | #define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v)) |
| 57 | #define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v)) |
| 58 | #define DATE_SET_MICROSECOND(o, v) \ |
| 59 | (((o)->data[7] = ((v) & 0xff0000) >> 16), \ |
| 60 | ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ |
| 61 | ((o)->data[9] = ((v) & 0x0000ff))) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 62 | |
| 63 | /* Time accessors for time. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR |
| 65 | #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE |
| 66 | #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND |
| 67 | #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND |
| 68 | #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) |
| 69 | #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) |
| 70 | #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) |
| 71 | #define TIME_SET_MICROSECOND(o, v) \ |
| 72 | (((o)->data[3] = ((v) & 0xff0000) >> 16), \ |
| 73 | ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ |
| 74 | ((o)->data[5] = ((v) & 0x0000ff))) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 75 | |
| 76 | /* Delta accessors for timedelta. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) |
| 78 | #define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) |
| 79 | #define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | #define SET_TD_DAYS(o, v) ((o)->days = (v)) |
| 82 | #define SET_TD_SECONDS(o, v) ((o)->seconds = (v)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 83 | #define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v)) |
| 84 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 85 | /* p is a pointer to a time or a datetime object; HASTZINFO(p) returns |
| 86 | * p->hastzinfo. |
| 87 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 88 | #define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo) |
| 89 | #define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \ |
| 90 | ((PyDateTime_Time *)(p))->tzinfo : Py_None) |
| 91 | #define GET_DT_TZINFO(p) (HASTZINFO(p) ? \ |
| 92 | ((PyDateTime_DateTime *)(p))->tzinfo : Py_None) |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 93 | /* M is a char or int claiming to be a valid month. The macro is equivalent |
| 94 | * to the two-sided Python test |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | * 1 <= M <= 12 |
Tim Peters | 3f60629 | 2004-03-21 23:38:41 +0000 | [diff] [blame] | 96 | */ |
| 97 | #define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12) |
| 98 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 99 | /* Forward declarations. */ |
| 100 | static PyTypeObject PyDateTime_DateType; |
| 101 | static PyTypeObject PyDateTime_DateTimeType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 102 | static PyTypeObject PyDateTime_DeltaType; |
| 103 | static PyTypeObject PyDateTime_TimeType; |
| 104 | static PyTypeObject PyDateTime_TZInfoType; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 105 | static PyTypeObject PyDateTime_TimeZoneType; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 106 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 107 | _Py_IDENTIFIER(as_integer_ratio); |
| 108 | _Py_IDENTIFIER(fromutc); |
| 109 | _Py_IDENTIFIER(isoformat); |
| 110 | _Py_IDENTIFIER(strftime); |
| 111 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 112 | /* --------------------------------------------------------------------------- |
| 113 | * Math utilities. |
| 114 | */ |
| 115 | |
| 116 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 117 | * iff k^i has sign bit set and k^j has sign bit set, |
| 118 | * iff (k^i)&(k^j) has sign bit set. |
| 119 | */ |
| 120 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 122 | |
| 123 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 124 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 125 | * the real point of this. C will probably truncate instead (C99 |
| 126 | * requires truncation; C89 left it implementation-defined). |
| 127 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 128 | * for all the uses made of it. This simplifies the code and makes |
| 129 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 130 | * overflow case). |
| 131 | */ |
| 132 | static int |
| 133 | divmod(int x, int y, int *r) |
| 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | assert(y > 0); |
| 138 | quo = x / y; |
| 139 | *r = x - quo * y; |
| 140 | if (*r < 0) { |
| 141 | --quo; |
| 142 | *r += y; |
| 143 | } |
| 144 | assert(0 <= *r && *r < y); |
| 145 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 148 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 149 | * are rounded to even. |
| 150 | */ |
| 151 | static PyObject * |
| 152 | divide_nearest(PyObject *m, PyObject *n) |
| 153 | { |
| 154 | PyObject *result; |
| 155 | PyObject *temp; |
| 156 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 157 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 158 | if (temp == NULL) |
| 159 | return NULL; |
| 160 | result = PyTuple_GET_ITEM(temp, 0); |
| 161 | Py_INCREF(result); |
| 162 | Py_DECREF(temp); |
| 163 | |
| 164 | return result; |
| 165 | } |
| 166 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 167 | /* --------------------------------------------------------------------------- |
| 168 | * General calendrical helper functions |
| 169 | */ |
| 170 | |
| 171 | /* For each month ordinal in 1..12, the number of days in that month, |
| 172 | * and the number of days before that month in the same year. These |
| 173 | * are correct for non-leap years only. |
| 174 | */ |
| 175 | static int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | 0, /* unused; this vector uses 1-based indexing */ |
| 177 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | static int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | 0, /* unused; this vector uses 1-based indexing */ |
| 182 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | /* year -> 1 if leap year, else 0. */ |
| 186 | static int |
| 187 | is_leap(int year) |
| 188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* Cast year to unsigned. The result is the same either way, but |
| 190 | * C can generate faster code for unsigned mod than for signed |
| 191 | * mod (especially for % 4 -- a good compiler should just grab |
| 192 | * the last 2 bits when the LHS is unsigned). |
| 193 | */ |
| 194 | const unsigned int ayear = (unsigned int)year; |
| 195 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* year, month -> number of days in that month in that year */ |
| 199 | static int |
| 200 | days_in_month(int year, int month) |
| 201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | assert(month >= 1); |
| 203 | assert(month <= 12); |
| 204 | if (month == 2 && is_leap(year)) |
| 205 | return 29; |
| 206 | else |
| 207 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* year, month -> number of days in year preceeding first day of month */ |
| 211 | static int |
| 212 | days_before_month(int year, int month) |
| 213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | assert(month >= 1); |
| 217 | assert(month <= 12); |
| 218 | days = _days_before_month[month]; |
| 219 | if (month > 2 && is_leap(year)) |
| 220 | ++days; |
| 221 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* year -> number of days before January 1st of year. Remember that we |
| 225 | * start with year 1, so days_before_year(1) == 0. |
| 226 | */ |
| 227 | static int |
| 228 | days_before_year(int year) |
| 229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | int y = year - 1; |
| 231 | /* This is incorrect if year <= 0; we really want the floor |
| 232 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 233 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 235 | assert (year >= 1); |
| 236 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 240 | * the correct values is asserted in the module init function. |
| 241 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 243 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 244 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 245 | |
| 246 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 247 | static void |
| 248 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 253 | * leap years repeats exactly every 400 years. The basic strategy is |
| 254 | * to find the closest 400-year boundary at or before ordinal, then |
| 255 | * work with the offset from that boundary to ordinal. Life is much |
| 256 | * clearer if we subtract 1 from ordinal first -- then the values |
| 257 | * of ordinal at 400-year boundaries are exactly those divisible |
| 258 | * by DI400Y: |
| 259 | * |
| 260 | * D M Y n n-1 |
| 261 | * -- --- ---- ---------- ---------------- |
| 262 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 263 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 264 | * ... |
| 265 | * 30 Dec 000 -1 -2 |
| 266 | * 31 Dec 000 0 -1 |
| 267 | * 1 Jan 001 1 0 400-year boundary |
| 268 | * 2 Jan 001 2 1 |
| 269 | * 3 Jan 001 3 2 |
| 270 | * ... |
| 271 | * 31 Dec 400 DI400Y DI400Y -1 |
| 272 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 273 | */ |
| 274 | assert(ordinal >= 1); |
| 275 | --ordinal; |
| 276 | n400 = ordinal / DI400Y; |
| 277 | n = ordinal % DI400Y; |
| 278 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 281 | * year, to the desired date. Now compute how many 100-year cycles |
| 282 | * precede n. |
| 283 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 284 | * 100-year cycles precede the desired day, which implies the |
| 285 | * desired day is December 31 at the end of a 400-year cycle. |
| 286 | */ |
| 287 | n100 = n / DI100Y; |
| 288 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | /* Now compute how many 4-year cycles precede it. */ |
| 291 | n4 = n / DI4Y; |
| 292 | n = n % DI4Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | /* And now how many single years. Again n1 can be 4, and again |
| 295 | * meaning that the desired day is December 31 at the end of the |
| 296 | * 4-year cycle. |
| 297 | */ |
| 298 | n1 = n / 365; |
| 299 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | *year += n100 * 100 + n4 * 4 + n1; |
| 302 | if (n1 == 4 || n100 == 4) { |
| 303 | assert(n == 0); |
| 304 | *year -= 1; |
| 305 | *month = 12; |
| 306 | *day = 31; |
| 307 | return; |
| 308 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | /* Now the year is correct, and n is the offset from January 1. We |
| 311 | * find the month via an estimate that's either exact or one too |
| 312 | * large. |
| 313 | */ |
| 314 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 315 | assert(leapyear == is_leap(*year)); |
| 316 | *month = (n + 50) >> 5; |
| 317 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 318 | if (preceding > n) { |
| 319 | /* estimate is too large */ |
| 320 | *month -= 1; |
| 321 | preceding -= days_in_month(*year, *month); |
| 322 | } |
| 323 | n -= preceding; |
| 324 | assert(0 <= n); |
| 325 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 331 | static int |
| 332 | ymd_to_ord(int year, int month, int day) |
| 333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 338 | static int |
| 339 | weekday(int year, int month, int day) |
| 340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 345 | * first calendar week containing a Thursday. |
| 346 | */ |
| 347 | static int |
| 348 | iso_week1_monday(int year) |
| 349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 351 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 352 | int first_weekday = (first_day + 6) % 7; |
| 353 | /* ordinal of closest Monday at or before 1/1 */ |
| 354 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 357 | week1_monday += 7; |
| 358 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* --------------------------------------------------------------------------- |
| 362 | * Range checkers. |
| 363 | */ |
| 364 | |
| 365 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 366 | * If not, raise OverflowError and return -1. |
| 367 | */ |
| 368 | static int |
| 369 | check_delta_day_range(int days) |
| 370 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 372 | return 0; |
| 373 | PyErr_Format(PyExc_OverflowError, |
| 374 | "days=%d; must have magnitude <= %d", |
| 375 | days, MAX_DELTA_DAYS); |
| 376 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 380 | * aren't, raise ValueError and return -1. |
| 381 | */ |
| 382 | static int |
| 383 | check_date_args(int year, int month, int day) |
| 384 | { |
| 385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | if (year < MINYEAR || year > MAXYEAR) { |
| 387 | PyErr_SetString(PyExc_ValueError, |
| 388 | "year is out of range"); |
| 389 | return -1; |
| 390 | } |
| 391 | if (month < 1 || month > 12) { |
| 392 | PyErr_SetString(PyExc_ValueError, |
| 393 | "month must be in 1..12"); |
| 394 | return -1; |
| 395 | } |
| 396 | if (day < 1 || day > days_in_month(year, month)) { |
| 397 | PyErr_SetString(PyExc_ValueError, |
| 398 | "day is out of range for month"); |
| 399 | return -1; |
| 400 | } |
| 401 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 405 | * aren't, raise ValueError and return -1. |
| 406 | */ |
| 407 | static int |
| 408 | check_time_args(int h, int m, int s, int us) |
| 409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | if (h < 0 || h > 23) { |
| 411 | PyErr_SetString(PyExc_ValueError, |
| 412 | "hour must be in 0..23"); |
| 413 | return -1; |
| 414 | } |
| 415 | if (m < 0 || m > 59) { |
| 416 | PyErr_SetString(PyExc_ValueError, |
| 417 | "minute must be in 0..59"); |
| 418 | return -1; |
| 419 | } |
| 420 | if (s < 0 || s > 59) { |
| 421 | PyErr_SetString(PyExc_ValueError, |
| 422 | "second must be in 0..59"); |
| 423 | return -1; |
| 424 | } |
| 425 | if (us < 0 || us > 999999) { |
| 426 | PyErr_SetString(PyExc_ValueError, |
| 427 | "microsecond must be in 0..999999"); |
| 428 | return -1; |
| 429 | } |
| 430 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* --------------------------------------------------------------------------- |
| 434 | * Normalization utilities. |
| 435 | */ |
| 436 | |
| 437 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 438 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 439 | * at least factor, enough of *lo is converted into "hi" units so that |
| 440 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 441 | * is impossible. |
| 442 | */ |
| 443 | static void |
| 444 | normalize_pair(int *hi, int *lo, int factor) |
| 445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | assert(factor > 0); |
| 447 | assert(lo != hi); |
| 448 | if (*lo < 0 || *lo >= factor) { |
| 449 | const int num_hi = divmod(*lo, factor, lo); |
| 450 | const int new_hi = *hi + num_hi; |
| 451 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 452 | *hi = new_hi; |
| 453 | } |
| 454 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | * 0 <= *s < 24*3600 |
| 459 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 460 | * The input values must be such that the internals don't overflow. |
| 461 | * The way this routine is used, we don't get close. |
| 462 | */ |
| 463 | static void |
| 464 | normalize_d_s_us(int *d, int *s, int *us) |
| 465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | if (*us < 0 || *us >= 1000000) { |
| 467 | normalize_pair(s, us, 1000000); |
| 468 | /* |s| can't be bigger than about |
| 469 | * |original s| + |original us|/1000000 now. |
| 470 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | } |
| 473 | if (*s < 0 || *s >= 24*3600) { |
| 474 | normalize_pair(d, s, 24*3600); |
| 475 | /* |d| can't be bigger than about |
| 476 | * |original d| + |
| 477 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 478 | */ |
| 479 | } |
| 480 | assert(0 <= *s && *s < 24*3600); |
| 481 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | * 1 <= *m <= 12 |
| 486 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 487 | * The input values must be such that the internals don't overflow. |
| 488 | * The way this routine is used, we don't get close. |
| 489 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 490 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 491 | normalize_y_m_d(int *y, int *m, int *d) |
| 492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 494 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 495 | /* In actual use, m is always the month component extracted from a |
| 496 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | /* Now only day can be out of bounds (year may also be out of bounds |
| 502 | * for a datetime object, but we don't care about that here). |
| 503 | * If day is out of bounds, what to do is arguable, but at least the |
| 504 | * method here is principled and explainable. |
| 505 | */ |
| 506 | dim = days_in_month(*y, *m); |
| 507 | if (*d < 1 || *d > dim) { |
| 508 | /* Move day-1 days from the first of the month. First try to |
| 509 | * get off cheap if we're only one day out of range |
| 510 | * (adjustments for timezone alone can't be worse than that). |
| 511 | */ |
| 512 | if (*d == 0) { |
| 513 | --*m; |
| 514 | if (*m > 0) |
| 515 | *d = days_in_month(*y, *m); |
| 516 | else { |
| 517 | --*y; |
| 518 | *m = 12; |
| 519 | *d = 31; |
| 520 | } |
| 521 | } |
| 522 | else if (*d == dim + 1) { |
| 523 | /* move forward a day */ |
| 524 | ++*m; |
| 525 | *d = 1; |
| 526 | if (*m > 12) { |
| 527 | *m = 1; |
| 528 | ++*y; |
| 529 | } |
| 530 | } |
| 531 | else { |
| 532 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 533 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 534 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 535 | goto error; |
| 536 | } else { |
| 537 | ord_to_ymd(ordinal, y, m, d); |
| 538 | return 0; |
| 539 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | assert(*m > 0); |
| 543 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 544 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 545 | return 0; |
| 546 | error: |
| 547 | PyErr_SetString(PyExc_OverflowError, |
| 548 | "date value out of range"); |
| 549 | return -1; |
| 550 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 554 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 555 | * failure, where failure means the adjusted year is out of bounds. |
| 556 | */ |
| 557 | static int |
| 558 | normalize_date(int *year, int *month, int *day) |
| 559 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 560 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* Force all the datetime fields into range. The parameters are both |
| 564 | * inputs and outputs. Returns < 0 on error. |
| 565 | */ |
| 566 | static int |
| 567 | normalize_datetime(int *year, int *month, int *day, |
| 568 | int *hour, int *minute, int *second, |
| 569 | int *microsecond) |
| 570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | normalize_pair(second, microsecond, 1000000); |
| 572 | normalize_pair(minute, second, 60); |
| 573 | normalize_pair(hour, minute, 60); |
| 574 | normalize_pair(day, hour, 24); |
| 575 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 579 | * Basic object allocation: tp_alloc implementations. These allocate |
| 580 | * Python objects of the right size and type, and do the Python object- |
| 581 | * initialization bit. If there's not enough memory, they return NULL after |
| 582 | * setting MemoryError. All data members remain uninitialized trash. |
| 583 | * |
| 584 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 585 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 586 | * tp_basicsize for the time and datetime types is set to the size of the |
| 587 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 588 | * allocate enough space for a tzinfo member whether or not one is actually |
| 589 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 590 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 591 | * using) just happens today to effectively ignore the nitems argument |
| 592 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 593 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 594 | * be changed to force a 0 nitems argument unless the type being allocated |
| 595 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 596 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 597 | */ |
| 598 | |
| 599 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 600 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | self = (PyObject *) |
| 605 | PyObject_MALLOC(aware ? |
| 606 | sizeof(PyDateTime_Time) : |
| 607 | sizeof(_PyDateTime_BaseTime)); |
| 608 | if (self == NULL) |
| 609 | return (PyObject *)PyErr_NoMemory(); |
| 610 | PyObject_INIT(self, type); |
| 611 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 615 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | self = (PyObject *) |
| 620 | PyObject_MALLOC(aware ? |
| 621 | sizeof(PyDateTime_DateTime) : |
| 622 | sizeof(_PyDateTime_BaseDateTime)); |
| 623 | if (self == NULL) |
| 624 | return (PyObject *)PyErr_NoMemory(); |
| 625 | PyObject_INIT(self, type); |
| 626 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* --------------------------------------------------------------------------- |
| 630 | * Helpers for setting object fields. These work on pointers to the |
| 631 | * appropriate base class. |
| 632 | */ |
| 633 | |
| 634 | /* For date and datetime. */ |
| 635 | static void |
| 636 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | self->hashcode = -1; |
| 639 | SET_YEAR(self, y); |
| 640 | SET_MONTH(self, m); |
| 641 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* --------------------------------------------------------------------------- |
| 645 | * Create various objects, mostly without range checking. |
| 646 | */ |
| 647 | |
| 648 | /* Create a date instance with no range checking. */ |
| 649 | static PyObject * |
| 650 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 651 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 655 | if (self != NULL) |
| 656 | set_date_fields(self, year, month, day); |
| 657 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 662 | |
| 663 | /* Create a datetime instance with no range checking. */ |
| 664 | static PyObject * |
| 665 | 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] | 666 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | PyDateTime_DateTime *self; |
| 669 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 672 | if (self != NULL) { |
| 673 | self->hastzinfo = aware; |
| 674 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 675 | DATE_SET_HOUR(self, hour); |
| 676 | DATE_SET_MINUTE(self, minute); |
| 677 | DATE_SET_SECOND(self, second); |
| 678 | DATE_SET_MICROSECOND(self, usecond); |
| 679 | if (aware) { |
| 680 | Py_INCREF(tzinfo); |
| 681 | self->tzinfo = tzinfo; |
| 682 | } |
| 683 | } |
| 684 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \ |
| 688 | new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \ |
| 689 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 690 | |
| 691 | /* Create a time instance with no range checking. */ |
| 692 | static PyObject * |
| 693 | new_time_ex(int hour, int minute, int second, int usecond, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | PyDateTime_Time *self; |
| 697 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 700 | if (self != NULL) { |
| 701 | self->hastzinfo = aware; |
| 702 | self->hashcode = -1; |
| 703 | TIME_SET_HOUR(self, hour); |
| 704 | TIME_SET_MINUTE(self, minute); |
| 705 | TIME_SET_SECOND(self, second); |
| 706 | TIME_SET_MICROSECOND(self, usecond); |
| 707 | if (aware) { |
| 708 | Py_INCREF(tzinfo); |
| 709 | self->tzinfo = tzinfo; |
| 710 | } |
| 711 | } |
| 712 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | #define new_time(hh, mm, ss, us, tzinfo) \ |
| 716 | new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 717 | |
| 718 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 719 | * true. Passing false is a speed optimization, if you know for sure |
| 720 | * that seconds and microseconds are already in their proper ranges. In any |
| 721 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 722 | * of range). |
| 723 | */ |
| 724 | static PyObject * |
| 725 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | if (normalize) |
| 731 | normalize_d_s_us(&days, &seconds, µseconds); |
| 732 | assert(0 <= seconds && seconds < 24*3600); |
| 733 | assert(0 <= microseconds && microseconds < 1000000); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | if (check_delta_day_range(days) < 0) |
| 736 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 739 | if (self != NULL) { |
| 740 | self->hashcode = -1; |
| 741 | SET_TD_DAYS(self, days); |
| 742 | SET_TD_SECONDS(self, seconds); |
| 743 | SET_TD_MICROSECONDS(self, microseconds); |
| 744 | } |
| 745 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | #define new_delta(d, s, us, normalize) \ |
| 749 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 750 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 751 | |
| 752 | typedef struct |
| 753 | { |
| 754 | PyObject_HEAD |
| 755 | PyObject *offset; |
| 756 | PyObject *name; |
| 757 | } PyDateTime_TimeZone; |
| 758 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 759 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 760 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 761 | /* The interned Epoch datetime instance */ |
| 762 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 763 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 764 | /* Create new timezone instance checking offset range. This |
| 765 | function does not check the name argument. Caller must assure |
| 766 | that offset is a timedelta instance and name is either NULL |
| 767 | or a unicode object. */ |
| 768 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 769 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 770 | { |
| 771 | PyDateTime_TimeZone *self; |
| 772 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 773 | |
| 774 | assert(offset != NULL); |
| 775 | assert(PyDelta_Check(offset)); |
| 776 | assert(name == NULL || PyUnicode_Check(name)); |
| 777 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 778 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 779 | if (self == NULL) { |
| 780 | return NULL; |
| 781 | } |
| 782 | Py_INCREF(offset); |
| 783 | self->offset = offset; |
| 784 | Py_XINCREF(name); |
| 785 | self->name = name; |
| 786 | return (PyObject *)self; |
| 787 | } |
| 788 | |
| 789 | static int delta_bool(PyDateTime_Delta *self); |
| 790 | |
| 791 | static PyObject * |
| 792 | new_timezone(PyObject *offset, PyObject *name) |
| 793 | { |
| 794 | assert(offset != NULL); |
| 795 | assert(PyDelta_Check(offset)); |
| 796 | assert(name == NULL || PyUnicode_Check(name)); |
| 797 | |
| 798 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 799 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 800 | return PyDateTime_TimeZone_UTC; |
| 801 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 802 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 803 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 804 | " representing a whole number of minutes," |
| 805 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 806 | return NULL; |
| 807 | } |
| 808 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 809 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 810 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 811 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 812 | " timedelta(hours=24)," |
| 813 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 814 | return NULL; |
| 815 | } |
| 816 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 817 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 820 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 821 | * tzinfo helpers. |
| 822 | */ |
| 823 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 824 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 825 | * raise TypeError and return -1. |
| 826 | */ |
| 827 | static int |
| 828 | check_tzinfo_subclass(PyObject *p) |
| 829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | if (p == Py_None || PyTZInfo_Check(p)) |
| 831 | return 0; |
| 832 | PyErr_Format(PyExc_TypeError, |
| 833 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 834 | "not type '%s'", |
| 835 | Py_TYPE(p)->tp_name); |
| 836 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 839 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 840 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 841 | * and the caller must not decref the result. |
| 842 | */ |
| 843 | static PyObject * |
| 844 | get_tzinfo_member(PyObject *self) |
| 845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 849 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 850 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 851 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 856 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 857 | * be an instance of the tzinfo class. If the method returns None, this |
| 858 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 859 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 860 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 861 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 862 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 863 | static PyObject * |
| 864 | call_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 865 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 866 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 869 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 871 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 872 | if (tzinfo == Py_None) |
| 873 | Py_RETURN_NONE; |
| 874 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 875 | if (offset == Py_None || offset == NULL) |
| 876 | return offset; |
| 877 | if (PyDelta_Check(offset)) { |
| 878 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 879 | Py_DECREF(offset); |
| 880 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 881 | " representing a whole number of minutes"); |
| 882 | return NULL; |
| 883 | } |
| 884 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 885 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 886 | Py_DECREF(offset); |
| 887 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 888 | " strictly between -timedelta(hours=24) and" |
| 889 | " timedelta(hours=24)."); |
| 890 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 894 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | PyErr_Format(PyExc_TypeError, |
| 896 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 897 | "timedelta, not '%.200s'", |
| 898 | name, Py_TYPE(offset)->tp_name); |
| 899 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 901 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 902 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 906 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 907 | * 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] | 908 | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
| 909 | * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
| 910 | * # of minutes), ValueError is raised and this returns -1. Else *none is |
| 911 | * 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] | 912 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 913 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 914 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 915 | { |
| 916 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 919 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 920 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 921 | * 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] | 922 | & doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 923 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 924 | * ValueError is raised and this returns -1. Else *none is set to 0 and |
| 925 | * the offset is returned (as an int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 926 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 927 | static PyObject * |
| 928 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 929 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 930 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 933 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 934 | * 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] | 935 | * 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] | 936 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 937 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 938 | */ |
| 939 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 940 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 943 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | assert(tzinfo != NULL); |
| 946 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 947 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 950 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 951 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 952 | result = _PyObject_CallMethodId(tzinfo, &PyId_tzname, "O", tzinfoarg); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 953 | |
| 954 | if (result == NULL || result == Py_None) |
| 955 | return result; |
| 956 | |
| 957 | if (!PyUnicode_Check(result)) { |
| 958 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 959 | "return None or a string, not '%s'", |
| 960 | Py_TYPE(result)->tp_name); |
| 961 | Py_DECREF(result); |
| 962 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 964 | |
| 965 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 968 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 969 | * stuff |
| 970 | * ", tzinfo=" + repr(tzinfo) |
| 971 | * before the closing ")". |
| 972 | */ |
| 973 | static PyObject * |
| 974 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | assert(PyUnicode_Check(repr)); |
| 979 | assert(tzinfo); |
| 980 | if (tzinfo == Py_None) |
| 981 | return repr; |
| 982 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 983 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 984 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | Py_DECREF(repr); |
| 986 | if (temp == NULL) |
| 987 | return NULL; |
| 988 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 989 | Py_DECREF(temp); |
| 990 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* --------------------------------------------------------------------------- |
| 994 | * String format helpers. |
| 995 | */ |
| 996 | |
| 997 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 998 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | static const char *DayNames[] = { |
| 1001 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 1002 | }; |
| 1003 | static const char *MonthNames[] = { |
| 1004 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1005 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1006 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1011 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1012 | GET_DAY(date), hours, minutes, seconds, |
| 1013 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1016 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1017 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1018 | /* Add an hours & minutes UTC offset string to buf. buf has no more than |
| 1019 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1020 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1021 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1022 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1023 | * string of the form |
| 1024 | * sign HH sep MM |
| 1025 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1026 | * bogus, an appropriate exception is set and -1 is returned. |
| 1027 | */ |
| 1028 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1029 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1031 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1032 | PyObject *offset; |
| 1033 | int hours, minutes, seconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1037 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1038 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1039 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1041 | if (offset == Py_None) { |
| 1042 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | *buf = '\0'; |
| 1044 | return 0; |
| 1045 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1046 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1047 | if (GET_TD_DAYS(offset) < 0) { |
| 1048 | PyObject *temp = offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | sign = '-'; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1050 | offset = delta_negative((PyDateTime_Delta *)offset); |
| 1051 | Py_DECREF(temp); |
| 1052 | if (offset == NULL) |
| 1053 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1055 | else { |
| 1056 | sign = '+'; |
| 1057 | } |
| 1058 | /* Offset is not negative here. */ |
| 1059 | seconds = GET_TD_SECONDS(offset); |
| 1060 | Py_DECREF(offset); |
| 1061 | minutes = divmod(seconds, 60, &seconds); |
| 1062 | hours = divmod(minutes, 60, &minutes); |
| 1063 | assert(seconds == 0); |
| 1064 | /* XXX ignore sub-minute data, curently not allowed. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1070 | static PyObject * |
| 1071 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | PyObject *temp; |
| 1074 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1075 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1076 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | if (Zreplacement == NULL) |
| 1079 | return NULL; |
| 1080 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1081 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | assert(tzinfoarg != NULL); |
| 1084 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1085 | if (temp == NULL) |
| 1086 | goto Error; |
| 1087 | if (temp == Py_None) { |
| 1088 | Py_DECREF(temp); |
| 1089 | return Zreplacement; |
| 1090 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | assert(PyUnicode_Check(temp)); |
| 1093 | /* Since the tzname is getting stuffed into the |
| 1094 | * format, we have to double any % signs so that |
| 1095 | * strftime doesn't treat them as format codes. |
| 1096 | */ |
| 1097 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1098 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | Py_DECREF(temp); |
| 1100 | if (Zreplacement == NULL) |
| 1101 | return NULL; |
| 1102 | if (!PyUnicode_Check(Zreplacement)) { |
| 1103 | PyErr_SetString(PyExc_TypeError, |
| 1104 | "tzname.replace() did not return a string"); |
| 1105 | goto Error; |
| 1106 | } |
| 1107 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1108 | |
| 1109 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | Py_DECREF(Zreplacement); |
| 1111 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1114 | static PyObject * |
| 1115 | make_freplacement(PyObject *object) |
| 1116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | char freplacement[64]; |
| 1118 | if (PyTime_Check(object)) |
| 1119 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1120 | else if (PyDateTime_Check(object)) |
| 1121 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1122 | else |
| 1123 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1128 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1129 | * 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] | 1130 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1131 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1132 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1133 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1134 | */ |
| 1135 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1136 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1142 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1143 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | const char *pin; /* pointer to next char in input format */ |
| 1146 | Py_ssize_t flen; /* length of input format */ |
| 1147 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1150 | char *pnew; /* pointer to available byte in output format */ |
| 1151 | size_t totalnew; /* number bytes total in output format buffer, |
| 1152 | exclusive of trailing \0 */ |
| 1153 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1156 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | assert(object && format && timetuple); |
| 1159 | assert(PyUnicode_Check(format)); |
| 1160 | /* Convert the input format to a C string and size */ |
| 1161 | pin = _PyUnicode_AsStringAndSize(format, &flen); |
| 1162 | if (!pin) |
| 1163 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1166 | * a new format. Since computing the replacements for those codes |
| 1167 | * is expensive, don't unless they're actually used. |
| 1168 | */ |
| 1169 | if (flen > INT_MAX - 1) { |
| 1170 | PyErr_NoMemory(); |
| 1171 | goto Done; |
| 1172 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1175 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1176 | if (newfmt == NULL) goto Done; |
| 1177 | pnew = PyBytes_AsString(newfmt); |
| 1178 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | while ((ch = *pin++) != '\0') { |
| 1181 | if (ch != '%') { |
| 1182 | ptoappend = pin - 1; |
| 1183 | ntoappend = 1; |
| 1184 | } |
| 1185 | else if ((ch = *pin++) == '\0') { |
| 1186 | /* There's a lone trailing %; doesn't make sense. */ |
| 1187 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1188 | "ends with raw %"); |
| 1189 | goto Done; |
| 1190 | } |
| 1191 | /* A % has been seen and ch is the character after it. */ |
| 1192 | else if (ch == 'z') { |
| 1193 | if (zreplacement == NULL) { |
| 1194 | /* format utcoffset */ |
| 1195 | char buf[100]; |
| 1196 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1197 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1198 | if (zreplacement == NULL) goto Done; |
| 1199 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1200 | assert(tzinfoarg != NULL); |
| 1201 | if (format_utcoffset(buf, |
| 1202 | sizeof(buf), |
| 1203 | "", |
| 1204 | tzinfo, |
| 1205 | tzinfoarg) < 0) |
| 1206 | goto Done; |
| 1207 | Py_DECREF(zreplacement); |
| 1208 | zreplacement = |
| 1209 | PyBytes_FromStringAndSize(buf, |
| 1210 | strlen(buf)); |
| 1211 | if (zreplacement == NULL) |
| 1212 | goto Done; |
| 1213 | } |
| 1214 | } |
| 1215 | assert(zreplacement != NULL); |
| 1216 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1217 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1218 | } |
| 1219 | else if (ch == 'Z') { |
| 1220 | /* format tzname */ |
| 1221 | if (Zreplacement == NULL) { |
| 1222 | Zreplacement = make_Zreplacement(object, |
| 1223 | tzinfoarg); |
| 1224 | if (Zreplacement == NULL) |
| 1225 | goto Done; |
| 1226 | } |
| 1227 | assert(Zreplacement != NULL); |
| 1228 | assert(PyUnicode_Check(Zreplacement)); |
| 1229 | ptoappend = _PyUnicode_AsStringAndSize(Zreplacement, |
| 1230 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1231 | if (ptoappend == NULL) |
| 1232 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | } |
| 1234 | else if (ch == 'f') { |
| 1235 | /* format microseconds */ |
| 1236 | if (freplacement == NULL) { |
| 1237 | freplacement = make_freplacement(object); |
| 1238 | if (freplacement == NULL) |
| 1239 | goto Done; |
| 1240 | } |
| 1241 | assert(freplacement != NULL); |
| 1242 | assert(PyBytes_Check(freplacement)); |
| 1243 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1244 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1245 | } |
| 1246 | else { |
| 1247 | /* percent followed by neither z nor Z */ |
| 1248 | ptoappend = pin - 2; |
| 1249 | ntoappend = 2; |
| 1250 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | /* Append the ntoappend chars starting at ptoappend to |
| 1253 | * the new format. |
| 1254 | */ |
| 1255 | if (ntoappend == 0) |
| 1256 | continue; |
| 1257 | assert(ptoappend != NULL); |
| 1258 | assert(ntoappend > 0); |
| 1259 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1260 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | PyErr_NoMemory(); |
| 1262 | goto Done; |
| 1263 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1264 | totalnew <<= 1; |
| 1265 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1268 | } |
| 1269 | memcpy(pnew, ptoappend, ntoappend); |
| 1270 | pnew += ntoappend; |
| 1271 | usednew += ntoappend; |
| 1272 | assert(usednew <= totalnew); |
| 1273 | } /* end while() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1276 | goto Done; |
| 1277 | { |
| 1278 | PyObject *format; |
| 1279 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | if (time == NULL) |
| 1282 | goto Done; |
| 1283 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1284 | if (format != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1285 | result = _PyObject_CallMethodId(time, &PyId_strftime, "OO", |
| 1286 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | Py_DECREF(format); |
| 1288 | } |
| 1289 | Py_DECREF(time); |
| 1290 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1291 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | Py_XDECREF(freplacement); |
| 1293 | Py_XDECREF(zreplacement); |
| 1294 | Py_XDECREF(Zreplacement); |
| 1295 | Py_XDECREF(newfmt); |
| 1296 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1299 | /* --------------------------------------------------------------------------- |
| 1300 | * Wrap functions from the time module. These aren't directly available |
| 1301 | * from C. Perhaps they should be. |
| 1302 | */ |
| 1303 | |
| 1304 | /* Call time.time() and return its result (a Python float). */ |
| 1305 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1306 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | PyObject *result = NULL; |
| 1309 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1312 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1313 | |
| 1314 | result = _PyObject_CallMethodId(time, &PyId_time, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | Py_DECREF(time); |
| 1316 | } |
| 1317 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1321 | * computed from the y,m,d args. |
| 1322 | */ |
| 1323 | static PyObject * |
| 1324 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | PyObject *time; |
| 1327 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | time = PyImport_ImportModuleNoBlock("time"); |
| 1330 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1331 | _Py_IDENTIFIER(struct_time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1332 | |
| 1333 | result = _PyObject_CallMethodId(time, &PyId_struct_time, |
| 1334 | "((iiiiiiiii))", |
| 1335 | y, m, d, |
| 1336 | hh, mm, ss, |
| 1337 | weekday(y, m, d), |
| 1338 | days_before_month(y, m) + d, |
| 1339 | dstflag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | Py_DECREF(time); |
| 1341 | } |
| 1342 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | /* --------------------------------------------------------------------------- |
| 1346 | * Miscellaneous helpers. |
| 1347 | */ |
| 1348 | |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1349 | /* 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] | 1350 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1351 | * This little helper turns that into a bool result for rich comparisons. |
| 1352 | */ |
| 1353 | static PyObject * |
| 1354 | diff_to_bool(int diff, int op) |
| 1355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | PyObject *result; |
| 1357 | int istrue; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | switch (op) { |
| 1360 | case Py_EQ: istrue = diff == 0; break; |
| 1361 | case Py_NE: istrue = diff != 0; break; |
| 1362 | case Py_LE: istrue = diff <= 0; break; |
| 1363 | case Py_GE: istrue = diff >= 0; break; |
| 1364 | case Py_LT: istrue = diff < 0; break; |
| 1365 | case Py_GT: istrue = diff > 0; break; |
| 1366 | default: |
| 1367 | assert(! "op unknown"); |
| 1368 | istrue = 0; /* To shut up compiler */ |
| 1369 | } |
| 1370 | result = istrue ? Py_True : Py_False; |
| 1371 | Py_INCREF(result); |
| 1372 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1375 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1376 | static PyObject * |
| 1377 | cmperror(PyObject *a, PyObject *b) |
| 1378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | PyErr_Format(PyExc_TypeError, |
| 1380 | "can't compare %s to %s", |
| 1381 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1382 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1385 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1386 | * Cached Python objects; these are set by the module init function. |
| 1387 | */ |
| 1388 | |
| 1389 | /* Conversion factors. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 1390 | static PyObject *one = NULL; /* 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1392 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1393 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1394 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1395 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1396 | 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] | 1397 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1398 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1399 | /* --------------------------------------------------------------------------- |
| 1400 | * Class implementations. |
| 1401 | */ |
| 1402 | |
| 1403 | /* |
| 1404 | * PyDateTime_Delta implementation. |
| 1405 | */ |
| 1406 | |
| 1407 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1409 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1410 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1411 | * due to ubiquitous overflow possibilities. |
| 1412 | */ |
| 1413 | static PyObject * |
| 1414 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | PyObject *x1 = NULL; |
| 1417 | PyObject *x2 = NULL; |
| 1418 | PyObject *x3 = NULL; |
| 1419 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1422 | if (x1 == NULL) |
| 1423 | goto Done; |
| 1424 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1425 | if (x2 == NULL) |
| 1426 | goto Done; |
| 1427 | Py_DECREF(x1); |
| 1428 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | /* x2 has days in seconds */ |
| 1431 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1432 | if (x1 == NULL) |
| 1433 | goto Done; |
| 1434 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1435 | if (x3 == NULL) |
| 1436 | goto Done; |
| 1437 | Py_DECREF(x1); |
| 1438 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1439 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | /* x3 has days+seconds in seconds */ |
| 1442 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1443 | if (x1 == NULL) |
| 1444 | goto Done; |
| 1445 | Py_DECREF(x3); |
| 1446 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | /* x1 has days+seconds in us */ |
| 1449 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1450 | if (x2 == NULL) |
| 1451 | goto Done; |
| 1452 | result = PyNumber_Add(x1, x2); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1453 | |
| 1454 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | Py_XDECREF(x1); |
| 1456 | Py_XDECREF(x2); |
| 1457 | Py_XDECREF(x3); |
| 1458 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1461 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1462 | */ |
| 1463 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1464 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | int us; |
| 1467 | int s; |
| 1468 | int d; |
| 1469 | long temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | PyObject *tuple = NULL; |
| 1472 | PyObject *num = NULL; |
| 1473 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1476 | if (tuple == NULL) |
| 1477 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1480 | if (num == NULL) |
| 1481 | goto Done; |
| 1482 | temp = PyLong_AsLong(num); |
| 1483 | num = NULL; |
| 1484 | if (temp == -1 && PyErr_Occurred()) |
| 1485 | goto Done; |
| 1486 | assert(0 <= temp && temp < 1000000); |
| 1487 | us = (int)temp; |
| 1488 | if (us < 0) { |
| 1489 | /* The divisor was positive, so this must be an error. */ |
| 1490 | assert(PyErr_Occurred()); |
| 1491 | goto Done; |
| 1492 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1495 | if (num == NULL) |
| 1496 | goto Done; |
| 1497 | Py_INCREF(num); |
| 1498 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1501 | if (tuple == NULL) |
| 1502 | goto Done; |
| 1503 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1506 | if (num == NULL) |
| 1507 | goto Done; |
| 1508 | temp = PyLong_AsLong(num); |
| 1509 | num = NULL; |
| 1510 | if (temp == -1 && PyErr_Occurred()) |
| 1511 | goto Done; |
| 1512 | assert(0 <= temp && temp < 24*3600); |
| 1513 | s = (int)temp; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | if (s < 0) { |
| 1516 | /* The divisor was positive, so this must be an error. */ |
| 1517 | assert(PyErr_Occurred()); |
| 1518 | goto Done; |
| 1519 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1522 | if (num == NULL) |
| 1523 | goto Done; |
| 1524 | Py_INCREF(num); |
| 1525 | temp = PyLong_AsLong(num); |
| 1526 | if (temp == -1 && PyErr_Occurred()) |
| 1527 | goto Done; |
| 1528 | d = (int)temp; |
| 1529 | if ((long)d != temp) { |
| 1530 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1531 | "large to fit in a C int"); |
| 1532 | goto Done; |
| 1533 | } |
| 1534 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1535 | |
| 1536 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | Py_XDECREF(tuple); |
| 1538 | Py_XDECREF(num); |
| 1539 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | #define microseconds_to_delta(pymicros) \ |
| 1543 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1544 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1545 | static PyObject * |
| 1546 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | PyObject *pyus_in; |
| 1549 | PyObject *pyus_out; |
| 1550 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | pyus_in = delta_to_microseconds(delta); |
| 1553 | if (pyus_in == NULL) |
| 1554 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1557 | Py_DECREF(pyus_in); |
| 1558 | if (pyus_out == NULL) |
| 1559 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | result = microseconds_to_delta(pyus_out); |
| 1562 | Py_DECREF(pyus_out); |
| 1563 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1567 | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) |
| 1568 | { |
| 1569 | PyObject *result = NULL; |
| 1570 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1571 | PyObject *ratio = NULL; |
| 1572 | |
| 1573 | pyus_in = delta_to_microseconds(delta); |
| 1574 | if (pyus_in == NULL) |
| 1575 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1576 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1577 | if (ratio == NULL) |
| 1578 | goto error; |
| 1579 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); |
| 1580 | Py_DECREF(pyus_in); |
| 1581 | pyus_in = NULL; |
| 1582 | if (temp == NULL) |
| 1583 | goto error; |
| 1584 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); |
| 1585 | Py_DECREF(temp); |
| 1586 | if (pyus_out == NULL) |
| 1587 | goto error; |
| 1588 | result = microseconds_to_delta(pyus_out); |
| 1589 | Py_DECREF(pyus_out); |
| 1590 | error: |
| 1591 | Py_XDECREF(pyus_in); |
| 1592 | Py_XDECREF(ratio); |
| 1593 | |
| 1594 | return result; |
| 1595 | } |
| 1596 | |
| 1597 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1598 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | PyObject *pyus_in; |
| 1601 | PyObject *pyus_out; |
| 1602 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | pyus_in = delta_to_microseconds(delta); |
| 1605 | if (pyus_in == NULL) |
| 1606 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1609 | Py_DECREF(pyus_in); |
| 1610 | if (pyus_out == NULL) |
| 1611 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | result = microseconds_to_delta(pyus_out); |
| 1614 | Py_DECREF(pyus_out); |
| 1615 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1619 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1620 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | PyObject *pyus_left; |
| 1622 | PyObject *pyus_right; |
| 1623 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | pyus_left = delta_to_microseconds(left); |
| 1626 | if (pyus_left == NULL) |
| 1627 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | pyus_right = delta_to_microseconds(right); |
| 1630 | if (pyus_right == NULL) { |
| 1631 | Py_DECREF(pyus_left); |
| 1632 | return NULL; |
| 1633 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1636 | Py_DECREF(pyus_left); |
| 1637 | Py_DECREF(pyus_right); |
| 1638 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static PyObject * |
| 1642 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | PyObject *pyus_left; |
| 1645 | PyObject *pyus_right; |
| 1646 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | pyus_left = delta_to_microseconds(left); |
| 1649 | if (pyus_left == NULL) |
| 1650 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | pyus_right = delta_to_microseconds(right); |
| 1653 | if (pyus_right == NULL) { |
| 1654 | Py_DECREF(pyus_left); |
| 1655 | return NULL; |
| 1656 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1659 | Py_DECREF(pyus_left); |
| 1660 | Py_DECREF(pyus_right); |
| 1661 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1665 | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) |
| 1666 | { |
| 1667 | PyObject *result = NULL; |
| 1668 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1669 | PyObject *ratio = NULL; |
| 1670 | |
| 1671 | pyus_in = delta_to_microseconds(delta); |
| 1672 | if (pyus_in == NULL) |
| 1673 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1674 | ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1675 | if (ratio == NULL) |
| 1676 | goto error; |
| 1677 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1)); |
| 1678 | Py_DECREF(pyus_in); |
| 1679 | pyus_in = NULL; |
| 1680 | if (temp == NULL) |
| 1681 | goto error; |
| 1682 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0)); |
| 1683 | Py_DECREF(temp); |
| 1684 | if (pyus_out == NULL) |
| 1685 | goto error; |
| 1686 | result = microseconds_to_delta(pyus_out); |
| 1687 | Py_DECREF(pyus_out); |
| 1688 | error: |
| 1689 | Py_XDECREF(pyus_in); |
| 1690 | Py_XDECREF(ratio); |
| 1691 | |
| 1692 | return result; |
| 1693 | } |
| 1694 | |
| 1695 | static PyObject * |
| 1696 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 1697 | { |
| 1698 | PyObject *result; |
| 1699 | PyObject *pyus_in, *pyus_out; |
| 1700 | pyus_in = delta_to_microseconds(delta); |
| 1701 | if (pyus_in == NULL) |
| 1702 | return NULL; |
| 1703 | pyus_out = divide_nearest(pyus_in, i); |
| 1704 | Py_DECREF(pyus_in); |
| 1705 | if (pyus_out == NULL) |
| 1706 | return NULL; |
| 1707 | result = microseconds_to_delta(pyus_out); |
| 1708 | Py_DECREF(pyus_out); |
| 1709 | |
| 1710 | return result; |
| 1711 | } |
| 1712 | |
| 1713 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1714 | delta_add(PyObject *left, PyObject *right) |
| 1715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1719 | /* delta + delta */ |
| 1720 | /* The C-level additions can't overflow because of the |
| 1721 | * invariant bounds. |
| 1722 | */ |
| 1723 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 1724 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 1725 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 1726 | GET_TD_MICROSECONDS(right); |
| 1727 | result = new_delta(days, seconds, microseconds, 1); |
| 1728 | } |
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 (result == Py_NotImplemented) |
| 1731 | Py_INCREF(result); |
| 1732 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | static PyObject * |
| 1736 | delta_negative(PyDateTime_Delta *self) |
| 1737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | return new_delta(-GET_TD_DAYS(self), |
| 1739 | -GET_TD_SECONDS(self), |
| 1740 | -GET_TD_MICROSECONDS(self), |
| 1741 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | static PyObject * |
| 1745 | delta_positive(PyDateTime_Delta *self) |
| 1746 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | /* Could optimize this (by returning self) if this isn't a |
| 1748 | * subclass -- but who uses unary + ? Approximately nobody. |
| 1749 | */ |
| 1750 | return new_delta(GET_TD_DAYS(self), |
| 1751 | GET_TD_SECONDS(self), |
| 1752 | GET_TD_MICROSECONDS(self), |
| 1753 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | static PyObject * |
| 1757 | delta_abs(PyDateTime_Delta *self) |
| 1758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1762 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | if (GET_TD_DAYS(self) < 0) |
| 1765 | result = delta_negative(self); |
| 1766 | else |
| 1767 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | static PyObject * |
| 1773 | delta_subtract(PyObject *left, PyObject *right) |
| 1774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1778 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 1779 | /* The C-level additions can't overflow because of the |
| 1780 | * invariant bounds. |
| 1781 | */ |
| 1782 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 1783 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 1784 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 1785 | GET_TD_MICROSECONDS(right); |
| 1786 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | } |
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 (result == Py_NotImplemented) |
| 1790 | Py_INCREF(result); |
| 1791 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1794 | static int |
| 1795 | delta_cmp(PyObject *self, PyObject *other) |
| 1796 | { |
| 1797 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 1798 | if (diff == 0) { |
| 1799 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 1800 | if (diff == 0) |
| 1801 | diff = GET_TD_MICROSECONDS(self) - |
| 1802 | GET_TD_MICROSECONDS(other); |
| 1803 | } |
| 1804 | return diff; |
| 1805 | } |
| 1806 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1807 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1808 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1811 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1812 | return diff_to_bool(diff, op); |
| 1813 | } |
| 1814 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1815 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1820 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1821 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1822 | delta_hash(PyDateTime_Delta *self) |
| 1823 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | if (self->hashcode == -1) { |
| 1825 | PyObject *temp = delta_getstate(self); |
| 1826 | if (temp != NULL) { |
| 1827 | self->hashcode = PyObject_Hash(temp); |
| 1828 | Py_DECREF(temp); |
| 1829 | } |
| 1830 | } |
| 1831 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | static PyObject * |
| 1835 | delta_multiply(PyObject *left, PyObject *right) |
| 1836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | if (PyDelta_Check(left)) { |
| 1840 | /* delta * ??? */ |
| 1841 | if (PyLong_Check(right)) |
| 1842 | result = multiply_int_timedelta(right, |
| 1843 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1844 | else if (PyFloat_Check(right)) |
| 1845 | result = multiply_float_timedelta(right, |
| 1846 | (PyDateTime_Delta *) left); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | } |
| 1848 | else if (PyLong_Check(left)) |
| 1849 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1850 | (PyDateTime_Delta *) right); |
| 1851 | else if (PyFloat_Check(left)) |
| 1852 | result = multiply_float_timedelta(left, |
| 1853 | (PyDateTime_Delta *) right); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | if (result == Py_NotImplemented) |
| 1856 | Py_INCREF(result); |
| 1857 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static PyObject * |
| 1861 | delta_divide(PyObject *left, PyObject *right) |
| 1862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | if (PyDelta_Check(left)) { |
| 1866 | /* delta * ??? */ |
| 1867 | if (PyLong_Check(right)) |
| 1868 | result = divide_timedelta_int( |
| 1869 | (PyDateTime_Delta *)left, |
| 1870 | right); |
| 1871 | else if (PyDelta_Check(right)) |
| 1872 | result = divide_timedelta_timedelta( |
| 1873 | (PyDateTime_Delta *)left, |
| 1874 | (PyDateTime_Delta *)right); |
| 1875 | } |
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 (result == Py_NotImplemented) |
| 1878 | Py_INCREF(result); |
| 1879 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1882 | static PyObject * |
| 1883 | delta_truedivide(PyObject *left, PyObject *right) |
| 1884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | if (PyDelta_Check(left)) { |
| 1888 | if (PyDelta_Check(right)) |
| 1889 | result = truedivide_timedelta_timedelta( |
| 1890 | (PyDateTime_Delta *)left, |
| 1891 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1892 | else if (PyFloat_Check(right)) |
| 1893 | result = truedivide_timedelta_float( |
| 1894 | (PyDateTime_Delta *)left, right); |
| 1895 | else if (PyLong_Check(right)) |
| 1896 | result = truedivide_timedelta_int( |
| 1897 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | if (result == Py_NotImplemented) |
| 1901 | Py_INCREF(result); |
| 1902 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | static PyObject * |
| 1906 | delta_remainder(PyObject *left, PyObject *right) |
| 1907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | PyObject *pyus_left; |
| 1909 | PyObject *pyus_right; |
| 1910 | PyObject *pyus_remainder; |
| 1911 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1912 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1913 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1914 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1917 | if (pyus_left == NULL) |
| 1918 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1921 | if (pyus_right == NULL) { |
| 1922 | Py_DECREF(pyus_left); |
| 1923 | return NULL; |
| 1924 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 1927 | Py_DECREF(pyus_left); |
| 1928 | Py_DECREF(pyus_right); |
| 1929 | if (pyus_remainder == 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 | remainder = microseconds_to_delta(pyus_remainder); |
| 1933 | Py_DECREF(pyus_remainder); |
| 1934 | if (remainder == NULL) |
| 1935 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | static PyObject * |
| 1941 | delta_divmod(PyObject *left, PyObject *right) |
| 1942 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1943 | PyObject *pyus_left; |
| 1944 | PyObject *pyus_right; |
| 1945 | PyObject *divmod; |
| 1946 | PyObject *delta; |
| 1947 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1948 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1949 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1950 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1953 | if (pyus_left == NULL) |
| 1954 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1957 | if (pyus_right == NULL) { |
| 1958 | Py_DECREF(pyus_left); |
| 1959 | return NULL; |
| 1960 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
| 1963 | Py_DECREF(pyus_left); |
| 1964 | Py_DECREF(pyus_right); |
| 1965 | if (divmod == 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 | assert(PyTuple_Size(divmod) == 2); |
| 1969 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 1970 | if (delta == NULL) { |
| 1971 | Py_DECREF(divmod); |
| 1972 | return NULL; |
| 1973 | } |
| 1974 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 1975 | Py_DECREF(delta); |
| 1976 | Py_DECREF(divmod); |
| 1977 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1980 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 1981 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 1982 | * so far, and there are factor microseconds per current unit, the number |
| 1983 | * of which is given by num. num * factor is added to sofar in a |
| 1984 | * numerically careful way, and that's the result. Any fractional |
| 1985 | * microseconds left over (this can happen if num is a float type) are |
| 1986 | * added into *leftover. |
| 1987 | * Note that there are many ways this can give an error (NULL) return. |
| 1988 | */ |
| 1989 | static PyObject * |
| 1990 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 1991 | double *leftover) |
| 1992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | PyObject *prod; |
| 1994 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | if (PyLong_Check(num)) { |
| 1999 | prod = PyNumber_Multiply(num, factor); |
| 2000 | if (prod == NULL) |
| 2001 | return NULL; |
| 2002 | sum = PyNumber_Add(sofar, prod); |
| 2003 | Py_DECREF(prod); |
| 2004 | return sum; |
| 2005 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | if (PyFloat_Check(num)) { |
| 2008 | double dnum; |
| 2009 | double fracpart; |
| 2010 | double intpart; |
| 2011 | PyObject *x; |
| 2012 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2014 | /* The Plan: decompose num into an integer part and a |
| 2015 | * fractional part, num = intpart + fracpart. |
| 2016 | * Then num * factor == |
| 2017 | * intpart * factor + fracpart * factor |
| 2018 | * and the LHS can be computed exactly in long arithmetic. |
| 2019 | * The RHS is again broken into an int part and frac part. |
| 2020 | * and the frac part is added into *leftover. |
| 2021 | */ |
| 2022 | dnum = PyFloat_AsDouble(num); |
| 2023 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2024 | return NULL; |
| 2025 | fracpart = modf(dnum, &intpart); |
| 2026 | x = PyLong_FromDouble(intpart); |
| 2027 | if (x == NULL) |
| 2028 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2030 | prod = PyNumber_Multiply(x, factor); |
| 2031 | Py_DECREF(x); |
| 2032 | if (prod == NULL) |
| 2033 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | sum = PyNumber_Add(sofar, prod); |
| 2036 | Py_DECREF(prod); |
| 2037 | if (sum == NULL) |
| 2038 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | if (fracpart == 0.0) |
| 2041 | return sum; |
| 2042 | /* So far we've lost no information. Dealing with the |
| 2043 | * fractional part requires float arithmetic, and may |
| 2044 | * lose a little info. |
| 2045 | */ |
| 2046 | assert(PyLong_Check(factor)); |
| 2047 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | dnum *= fracpart; |
| 2050 | fracpart = modf(dnum, &intpart); |
| 2051 | x = PyLong_FromDouble(intpart); |
| 2052 | if (x == NULL) { |
| 2053 | Py_DECREF(sum); |
| 2054 | return NULL; |
| 2055 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 | y = PyNumber_Add(sum, x); |
| 2058 | Py_DECREF(sum); |
| 2059 | Py_DECREF(x); |
| 2060 | *leftover += fracpart; |
| 2061 | return y; |
| 2062 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | PyErr_Format(PyExc_TypeError, |
| 2065 | "unsupported type for timedelta %s component: %s", |
| 2066 | tag, Py_TYPE(num)->tp_name); |
| 2067 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | static PyObject * |
| 2071 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | /* Argument objects. */ |
| 2076 | PyObject *day = NULL; |
| 2077 | PyObject *second = NULL; |
| 2078 | PyObject *us = NULL; |
| 2079 | PyObject *ms = NULL; |
| 2080 | PyObject *minute = NULL; |
| 2081 | PyObject *hour = NULL; |
| 2082 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2085 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2086 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | static char *keywords[] = { |
| 2089 | "days", "seconds", "microseconds", "milliseconds", |
| 2090 | "minutes", "hours", "weeks", NULL |
| 2091 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2094 | keywords, |
| 2095 | &day, &second, &us, |
| 2096 | &ms, &minute, &hour, &week) == 0) |
| 2097 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | x = PyLong_FromLong(0); |
| 2100 | if (x == NULL) |
| 2101 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2103 | #define CLEANUP \ |
| 2104 | Py_DECREF(x); \ |
| 2105 | x = y; \ |
| 2106 | if (x == NULL) \ |
| 2107 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | if (us) { |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2110 | y = accum("microseconds", x, us, one, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | CLEANUP; |
| 2112 | } |
| 2113 | if (ms) { |
| 2114 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2115 | CLEANUP; |
| 2116 | } |
| 2117 | if (second) { |
| 2118 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2119 | CLEANUP; |
| 2120 | } |
| 2121 | if (minute) { |
| 2122 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2123 | CLEANUP; |
| 2124 | } |
| 2125 | if (hour) { |
| 2126 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2127 | CLEANUP; |
| 2128 | } |
| 2129 | if (day) { |
| 2130 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2131 | CLEANUP; |
| 2132 | } |
| 2133 | if (week) { |
| 2134 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2135 | CLEANUP; |
| 2136 | } |
| 2137 | if (leftover_us) { |
| 2138 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2139 | double whole_us = round(leftover_us); |
| 2140 | int x_is_odd; |
| 2141 | PyObject *temp; |
| 2142 | |
| 2143 | whole_us = round(leftover_us); |
| 2144 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2145 | /* We're exactly halfway between two integers. In order |
| 2146 | * to do round-half-to-even, we must determine whether x |
| 2147 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2148 | * code below uses bitwise and operation to check the last |
| 2149 | * bit. */ |
| 2150 | temp = PyNumber_And(x, one); /* temp <- x & 1 */ |
| 2151 | if (temp == NULL) { |
| 2152 | Py_DECREF(x); |
| 2153 | goto Done; |
| 2154 | } |
| 2155 | x_is_odd = PyObject_IsTrue(temp); |
| 2156 | Py_DECREF(temp); |
| 2157 | if (x_is_odd == -1) { |
| 2158 | Py_DECREF(x); |
| 2159 | goto Done; |
| 2160 | } |
| 2161 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2162 | } |
| 2163 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2164 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | if (temp == NULL) { |
| 2167 | Py_DECREF(x); |
| 2168 | goto Done; |
| 2169 | } |
| 2170 | y = PyNumber_Add(x, temp); |
| 2171 | Py_DECREF(temp); |
| 2172 | CLEANUP; |
| 2173 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | self = microseconds_to_delta_ex(x, type); |
| 2176 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2177 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2179 | |
| 2180 | #undef CLEANUP |
| 2181 | } |
| 2182 | |
| 2183 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2184 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2186 | return (GET_TD_DAYS(self) != 0 |
| 2187 | || GET_TD_SECONDS(self) != 0 |
| 2188 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | static PyObject * |
| 2192 | delta_repr(PyDateTime_Delta *self) |
| 2193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | if (GET_TD_MICROSECONDS(self) != 0) |
| 2195 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2196 | Py_TYPE(self)->tp_name, |
| 2197 | GET_TD_DAYS(self), |
| 2198 | GET_TD_SECONDS(self), |
| 2199 | GET_TD_MICROSECONDS(self)); |
| 2200 | if (GET_TD_SECONDS(self) != 0) |
| 2201 | return PyUnicode_FromFormat("%s(%d, %d)", |
| 2202 | Py_TYPE(self)->tp_name, |
| 2203 | GET_TD_DAYS(self), |
| 2204 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | return PyUnicode_FromFormat("%s(%d)", |
| 2207 | Py_TYPE(self)->tp_name, |
| 2208 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | static PyObject * |
| 2212 | delta_str(PyDateTime_Delta *self) |
| 2213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | int us = GET_TD_MICROSECONDS(self); |
| 2215 | int seconds = GET_TD_SECONDS(self); |
| 2216 | int minutes = divmod(seconds, 60, &seconds); |
| 2217 | int hours = divmod(minutes, 60, &minutes); |
| 2218 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2220 | if (days) { |
| 2221 | if (us) |
| 2222 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2223 | days, (days == 1 || days == -1) ? "" : "s", |
| 2224 | hours, minutes, seconds, us); |
| 2225 | else |
| 2226 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2227 | days, (days == 1 || days == -1) ? "" : "s", |
| 2228 | hours, minutes, seconds); |
| 2229 | } else { |
| 2230 | if (us) |
| 2231 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2232 | hours, minutes, seconds, us); |
| 2233 | else |
| 2234 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2235 | hours, minutes, seconds); |
| 2236 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2237 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2240 | /* Pickle support, a simple use of __reduce__. */ |
| 2241 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2242 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2243 | static PyObject * |
| 2244 | delta_getstate(PyDateTime_Delta *self) |
| 2245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2247 | GET_TD_SECONDS(self), |
| 2248 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2251 | static PyObject * |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2252 | delta_total_seconds(PyObject *self) |
| 2253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | PyObject *total_seconds; |
| 2255 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2258 | if (total_microseconds == NULL) |
| 2259 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2260 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2261 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2268 | delta_reduce(PyDateTime_Delta* self) |
| 2269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2274 | |
| 2275 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | {"days", T_INT, OFFSET(days), READONLY, |
| 2278 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2281 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2284 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2285 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2286 | }; |
| 2287 | |
| 2288 | static PyMethodDef delta_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
| 2290 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2293 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2296 | }; |
| 2297 | |
| 2298 | static char delta_doc[] = |
| 2299 | PyDoc_STR("Difference between two datetime values."); |
| 2300 | |
| 2301 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | delta_add, /* nb_add */ |
| 2303 | delta_subtract, /* nb_subtract */ |
| 2304 | delta_multiply, /* nb_multiply */ |
| 2305 | delta_remainder, /* nb_remainder */ |
| 2306 | delta_divmod, /* nb_divmod */ |
| 2307 | 0, /* nb_power */ |
| 2308 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2309 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2310 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2311 | (inquiry)delta_bool, /* nb_bool */ |
| 2312 | 0, /*nb_invert*/ |
| 2313 | 0, /*nb_lshift*/ |
| 2314 | 0, /*nb_rshift*/ |
| 2315 | 0, /*nb_and*/ |
| 2316 | 0, /*nb_xor*/ |
| 2317 | 0, /*nb_or*/ |
| 2318 | 0, /*nb_int*/ |
| 2319 | 0, /*nb_reserved*/ |
| 2320 | 0, /*nb_float*/ |
| 2321 | 0, /*nb_inplace_add*/ |
| 2322 | 0, /*nb_inplace_subtract*/ |
| 2323 | 0, /*nb_inplace_multiply*/ |
| 2324 | 0, /*nb_inplace_remainder*/ |
| 2325 | 0, /*nb_inplace_power*/ |
| 2326 | 0, /*nb_inplace_lshift*/ |
| 2327 | 0, /*nb_inplace_rshift*/ |
| 2328 | 0, /*nb_inplace_and*/ |
| 2329 | 0, /*nb_inplace_xor*/ |
| 2330 | 0, /*nb_inplace_or*/ |
| 2331 | delta_divide, /* nb_floor_divide */ |
| 2332 | delta_truedivide, /* nb_true_divide */ |
| 2333 | 0, /* nb_inplace_floor_divide */ |
| 2334 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2335 | }; |
| 2336 | |
| 2337 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2339 | "datetime.timedelta", /* tp_name */ |
| 2340 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2341 | 0, /* tp_itemsize */ |
| 2342 | 0, /* tp_dealloc */ |
| 2343 | 0, /* tp_print */ |
| 2344 | 0, /* tp_getattr */ |
| 2345 | 0, /* tp_setattr */ |
| 2346 | 0, /* tp_reserved */ |
| 2347 | (reprfunc)delta_repr, /* tp_repr */ |
| 2348 | &delta_as_number, /* tp_as_number */ |
| 2349 | 0, /* tp_as_sequence */ |
| 2350 | 0, /* tp_as_mapping */ |
| 2351 | (hashfunc)delta_hash, /* tp_hash */ |
| 2352 | 0, /* tp_call */ |
| 2353 | (reprfunc)delta_str, /* tp_str */ |
| 2354 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2355 | 0, /* tp_setattro */ |
| 2356 | 0, /* tp_as_buffer */ |
| 2357 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2358 | delta_doc, /* tp_doc */ |
| 2359 | 0, /* tp_traverse */ |
| 2360 | 0, /* tp_clear */ |
| 2361 | delta_richcompare, /* tp_richcompare */ |
| 2362 | 0, /* tp_weaklistoffset */ |
| 2363 | 0, /* tp_iter */ |
| 2364 | 0, /* tp_iternext */ |
| 2365 | delta_methods, /* tp_methods */ |
| 2366 | delta_members, /* tp_members */ |
| 2367 | 0, /* tp_getset */ |
| 2368 | 0, /* tp_base */ |
| 2369 | 0, /* tp_dict */ |
| 2370 | 0, /* tp_descr_get */ |
| 2371 | 0, /* tp_descr_set */ |
| 2372 | 0, /* tp_dictoffset */ |
| 2373 | 0, /* tp_init */ |
| 2374 | 0, /* tp_alloc */ |
| 2375 | delta_new, /* tp_new */ |
| 2376 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2377 | }; |
| 2378 | |
| 2379 | /* |
| 2380 | * PyDateTime_Date implementation. |
| 2381 | */ |
| 2382 | |
| 2383 | /* Accessor properties. */ |
| 2384 | |
| 2385 | static PyObject * |
| 2386 | date_year(PyDateTime_Date *self, void *unused) |
| 2387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | static PyObject * |
| 2392 | date_month(PyDateTime_Date *self, void *unused) |
| 2393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | static PyObject * |
| 2398 | date_day(PyDateTime_Date *self, void *unused) |
| 2399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2404 | {"year", (getter)date_year}, |
| 2405 | {"month", (getter)date_month}, |
| 2406 | {"day", (getter)date_day}, |
| 2407 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2408 | }; |
| 2409 | |
| 2410 | /* Constructors. */ |
| 2411 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2412 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2413 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2414 | static PyObject * |
| 2415 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2417 | PyObject *self = NULL; |
| 2418 | PyObject *state; |
| 2419 | int year; |
| 2420 | int month; |
| 2421 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2423 | /* Check for invocation from pickle with __getstate__ state */ |
| 2424 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2425 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2426 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2427 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2428 | { |
| 2429 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2432 | if (me != NULL) { |
| 2433 | char *pdata = PyBytes_AS_STRING(state); |
| 2434 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2435 | me->hashcode = -1; |
| 2436 | } |
| 2437 | return (PyObject *)me; |
| 2438 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2440 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2441 | &year, &month, &day)) { |
| 2442 | if (check_date_args(year, month, day) < 0) |
| 2443 | return NULL; |
| 2444 | self = new_date_ex(year, month, day, type); |
| 2445 | } |
| 2446 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | /* Return new date from localtime(t). */ |
| 2450 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2451 | date_local_from_object(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | struct tm *tm; |
| 2454 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2455 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2456 | if (_PyTime_ObjectToTime_t(obj, &t) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | tm = localtime(&t); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2460 | if (tm == NULL) { |
| 2461 | /* unconvertible time */ |
| 2462 | #ifdef EINVAL |
| 2463 | if (errno == 0) |
| 2464 | errno = EINVAL; |
| 2465 | #endif |
| 2466 | PyErr_SetFromErrno(PyExc_OSError); |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | |
| 2470 | return PyObject_CallFunction(cls, "iii", |
| 2471 | tm->tm_year + 1900, |
| 2472 | tm->tm_mon + 1, |
| 2473 | tm->tm_mday); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | /* Return new date from current time. |
| 2477 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2478 | * only way to be sure of that is to *call* time.time(). That's not |
| 2479 | * generally the same as calling C's time. |
| 2480 | */ |
| 2481 | static PyObject * |
| 2482 | date_today(PyObject *cls, PyObject *dummy) |
| 2483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 | PyObject *time; |
| 2485 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2486 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | time = time_time(); |
| 2489 | if (time == NULL) |
| 2490 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2492 | /* Note well: today() is a class method, so this may not call |
| 2493 | * date.fromtimestamp. For example, it may call |
| 2494 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2495 | * time.time() delivers; if someone were gonzo about optimization, |
| 2496 | * date.today() could get away with plain C time(). |
| 2497 | */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2498 | result = _PyObject_CallMethodId(cls, &PyId_fromtimestamp, "O", time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2499 | Py_DECREF(time); |
| 2500 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2504 | static PyObject * |
| 2505 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2506 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2507 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2508 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2509 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2510 | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
| 2511 | result = date_local_from_object(cls, timestamp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2516 | * the ordinal is out of range. |
| 2517 | */ |
| 2518 | static PyObject * |
| 2519 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | PyObject *result = NULL; |
| 2522 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2525 | int year; |
| 2526 | int month; |
| 2527 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | if (ordinal < 1) |
| 2530 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2531 | ">= 1"); |
| 2532 | else { |
| 2533 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2534 | result = PyObject_CallFunction(cls, "iii", |
| 2535 | year, month, day); |
| 2536 | } |
| 2537 | } |
| 2538 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * Date arithmetic. |
| 2543 | */ |
| 2544 | |
| 2545 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2546 | * instead. |
| 2547 | */ |
| 2548 | static PyObject * |
| 2549 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2550 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | PyObject *result = NULL; |
| 2552 | int year = GET_YEAR(date); |
| 2553 | int month = GET_MONTH(date); |
| 2554 | int deltadays = GET_TD_DAYS(delta); |
| 2555 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2556 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2558 | if (normalize_date(&year, &month, &day) >= 0) |
| 2559 | result = new_date(year, month, day); |
| 2560 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | static PyObject * |
| 2564 | date_add(PyObject *left, PyObject *right) |
| 2565 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2566 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2567 | Py_RETURN_NOTIMPLEMENTED; |
| 2568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | if (PyDate_Check(left)) { |
| 2570 | /* date + ??? */ |
| 2571 | if (PyDelta_Check(right)) |
| 2572 | /* date + delta */ |
| 2573 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2574 | (PyDateTime_Delta *) right, |
| 2575 | 0); |
| 2576 | } |
| 2577 | else { |
| 2578 | /* ??? + date |
| 2579 | * 'right' must be one of us, or we wouldn't have been called |
| 2580 | */ |
| 2581 | if (PyDelta_Check(left)) |
| 2582 | /* delta + date */ |
| 2583 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2584 | (PyDateTime_Delta *) left, |
| 2585 | 0); |
| 2586 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2587 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | static PyObject * |
| 2591 | date_subtract(PyObject *left, PyObject *right) |
| 2592 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2593 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2594 | Py_RETURN_NOTIMPLEMENTED; |
| 2595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | if (PyDate_Check(left)) { |
| 2597 | if (PyDate_Check(right)) { |
| 2598 | /* date - date */ |
| 2599 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2600 | GET_MONTH(left), |
| 2601 | GET_DAY(left)); |
| 2602 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2603 | GET_MONTH(right), |
| 2604 | GET_DAY(right)); |
| 2605 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2606 | } |
| 2607 | if (PyDelta_Check(right)) { |
| 2608 | /* date - delta */ |
| 2609 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2610 | (PyDateTime_Delta *) right, |
| 2611 | 1); |
| 2612 | } |
| 2613 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2614 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | |
| 2618 | /* Various ways to turn a date into a string. */ |
| 2619 | |
| 2620 | static PyObject * |
| 2621 | date_repr(PyDateTime_Date *self) |
| 2622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2623 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2624 | Py_TYPE(self)->tp_name, |
| 2625 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | static PyObject * |
| 2629 | date_isoformat(PyDateTime_Date *self) |
| 2630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2632 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2635 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2636 | static PyObject * |
| 2637 | date_str(PyDateTime_Date *self) |
| 2638 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2639 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | |
| 2643 | static PyObject * |
| 2644 | date_ctime(PyDateTime_Date *self) |
| 2645 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2646 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | static PyObject * |
| 2650 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2651 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | /* This method can be inherited, and needs to call the |
| 2653 | * timetuple() method appropriate to self's class. |
| 2654 | */ |
| 2655 | PyObject *result; |
| 2656 | PyObject *tuple; |
| 2657 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2658 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 2662 | &format)) |
| 2663 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2664 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2665 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | if (tuple == NULL) |
| 2667 | return NULL; |
| 2668 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2669 | (PyObject *)self); |
| 2670 | Py_DECREF(tuple); |
| 2671 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2674 | static PyObject * |
| 2675 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | PyObject *format; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2680 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2683 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2685 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2686 | return _PyObject_CallMethodId((PyObject *)self, &PyId_strftime, "O", format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2689 | /* ISO methods. */ |
| 2690 | |
| 2691 | static PyObject * |
| 2692 | date_isoweekday(PyDateTime_Date *self) |
| 2693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | static PyObject * |
| 2700 | date_isocalendar(PyDateTime_Date *self) |
| 2701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | int year = GET_YEAR(self); |
| 2703 | int week1_monday = iso_week1_monday(year); |
| 2704 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2705 | int week; |
| 2706 | int day; |
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 | week = divmod(today - week1_monday, 7, &day); |
| 2709 | if (week < 0) { |
| 2710 | --year; |
| 2711 | week1_monday = iso_week1_monday(year); |
| 2712 | week = divmod(today - week1_monday, 7, &day); |
| 2713 | } |
| 2714 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2715 | ++year; |
| 2716 | week = 0; |
| 2717 | } |
| 2718 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* Miscellaneous methods. */ |
| 2722 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2723 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2724 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | if (PyDate_Check(other)) { |
| 2727 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2728 | ((PyDateTime_Date *)other)->data, |
| 2729 | _PyDateTime_DATE_DATASIZE); |
| 2730 | return diff_to_bool(diff, op); |
| 2731 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2732 | else |
| 2733 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | static PyObject * |
| 2737 | date_timetuple(PyDateTime_Date *self) |
| 2738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | return build_struct_time(GET_YEAR(self), |
| 2740 | GET_MONTH(self), |
| 2741 | GET_DAY(self), |
| 2742 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2745 | static PyObject * |
| 2746 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | PyObject *clone; |
| 2749 | PyObject *tuple; |
| 2750 | int year = GET_YEAR(self); |
| 2751 | int month = GET_MONTH(self); |
| 2752 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2755 | &year, &month, &day)) |
| 2756 | return NULL; |
| 2757 | tuple = Py_BuildValue("iii", year, month, day); |
| 2758 | if (tuple == NULL) |
| 2759 | return NULL; |
| 2760 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 2761 | Py_DECREF(tuple); |
| 2762 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2765 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2766 | generic_hash(unsigned char *data, int len) |
| 2767 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 2768 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | |
| 2772 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2773 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2774 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2775 | date_hash(PyDateTime_Date *self) |
| 2776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | if (self->hashcode == -1) |
| 2778 | self->hashcode = generic_hash( |
| 2779 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | static PyObject * |
| 2785 | date_toordinal(PyDateTime_Date *self) |
| 2786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 2788 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | static PyObject * |
| 2792 | date_weekday(PyDateTime_Date *self) |
| 2793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2799 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2800 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2801 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2802 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2803 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2805 | PyObject* field; |
| 2806 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 2807 | _PyDateTime_DATE_DATASIZE); |
| 2808 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2812 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2821 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2822 | METH_CLASS, |
| 2823 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2824 | "time.time()).")}, |
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 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2827 | METH_CLASS, |
| 2828 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2829 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2832 | PyDoc_STR("Current date or datetime: same as " |
| 2833 | "self.__class__.fromtimestamp(time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2838 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 2841 | PyDoc_STR("format -> strftime() style string.")}, |
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 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2844 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2847 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
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 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2850 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2851 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2854 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2857 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2858 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2861 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2862 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2865 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2866 | "Monday == 0 ... Sunday == 6")}, |
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 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 2869 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2872 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2875 | }; |
| 2876 | |
| 2877 | static char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2878 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2879 | |
| 2880 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2881 | date_add, /* nb_add */ |
| 2882 | date_subtract, /* nb_subtract */ |
| 2883 | 0, /* nb_multiply */ |
| 2884 | 0, /* nb_remainder */ |
| 2885 | 0, /* nb_divmod */ |
| 2886 | 0, /* nb_power */ |
| 2887 | 0, /* nb_negative */ |
| 2888 | 0, /* nb_positive */ |
| 2889 | 0, /* nb_absolute */ |
| 2890 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2891 | }; |
| 2892 | |
| 2893 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2895 | "datetime.date", /* tp_name */ |
| 2896 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2897 | 0, /* tp_itemsize */ |
| 2898 | 0, /* tp_dealloc */ |
| 2899 | 0, /* tp_print */ |
| 2900 | 0, /* tp_getattr */ |
| 2901 | 0, /* tp_setattr */ |
| 2902 | 0, /* tp_reserved */ |
| 2903 | (reprfunc)date_repr, /* tp_repr */ |
| 2904 | &date_as_number, /* tp_as_number */ |
| 2905 | 0, /* tp_as_sequence */ |
| 2906 | 0, /* tp_as_mapping */ |
| 2907 | (hashfunc)date_hash, /* tp_hash */ |
| 2908 | 0, /* tp_call */ |
| 2909 | (reprfunc)date_str, /* tp_str */ |
| 2910 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2911 | 0, /* tp_setattro */ |
| 2912 | 0, /* tp_as_buffer */ |
| 2913 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2914 | date_doc, /* tp_doc */ |
| 2915 | 0, /* tp_traverse */ |
| 2916 | 0, /* tp_clear */ |
| 2917 | date_richcompare, /* tp_richcompare */ |
| 2918 | 0, /* tp_weaklistoffset */ |
| 2919 | 0, /* tp_iter */ |
| 2920 | 0, /* tp_iternext */ |
| 2921 | date_methods, /* tp_methods */ |
| 2922 | 0, /* tp_members */ |
| 2923 | date_getset, /* tp_getset */ |
| 2924 | 0, /* tp_base */ |
| 2925 | 0, /* tp_dict */ |
| 2926 | 0, /* tp_descr_get */ |
| 2927 | 0, /* tp_descr_set */ |
| 2928 | 0, /* tp_dictoffset */ |
| 2929 | 0, /* tp_init */ |
| 2930 | 0, /* tp_alloc */ |
| 2931 | date_new, /* tp_new */ |
| 2932 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2933 | }; |
| 2934 | |
| 2935 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2936 | * PyDateTime_TZInfo implementation. |
| 2937 | */ |
| 2938 | |
| 2939 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 2940 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 2941 | * 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] | 2942 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2943 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 2944 | * |
| 2945 | * Note: For reasons having to do with pickling of subclasses, we have |
| 2946 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 2947 | * in the Python implementation (__init__() could raise NotImplementedError |
| 2948 | * there without ill effect), but doing so in the C implementation hit a |
| 2949 | * brick wall. |
| 2950 | */ |
| 2951 | |
| 2952 | static PyObject * |
| 2953 | tzinfo_nogo(const char* methodname) |
| 2954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | PyErr_Format(PyExc_NotImplementedError, |
| 2956 | "a tzinfo subclass must implement %s()", |
| 2957 | methodname); |
| 2958 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | /* Methods. A subclass must implement these. */ |
| 2962 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2963 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2964 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 2965 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2969 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2970 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 2971 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2973 | } |
| 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_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 2977 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2981 | |
| 2982 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 2983 | PyDateTime_Delta *delta, |
| 2984 | int factor); |
| 2985 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 2986 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 2987 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2988 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2989 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2990 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2991 | PyObject *result = NULL; |
| 2992 | PyObject *off = NULL, *dst = NULL; |
| 2993 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2994 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2995 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | PyErr_SetString(PyExc_TypeError, |
| 2997 | "fromutc: argument must be a datetime"); |
| 2998 | return NULL; |
| 2999 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3000 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3002 | "is not self"); |
| 3003 | return NULL; |
| 3004 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3005 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3006 | off = datetime_utcoffset(dt, NULL); |
| 3007 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3009 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3011 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3012 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3014 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3015 | dst = datetime_dst(dt, NULL); |
| 3016 | if (dst == NULL) |
| 3017 | goto Fail; |
| 3018 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3020 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3021 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3023 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3024 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3025 | if (delta == NULL) |
| 3026 | goto Fail; |
| 3027 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3029 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3030 | |
| 3031 | Py_DECREF(dst); |
| 3032 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3033 | if (dst == NULL) |
| 3034 | goto Fail; |
| 3035 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | goto Inconsistent; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3037 | if (delta_bool(delta) != 0) { |
| 3038 | PyObject *temp = result; |
| 3039 | result = add_datetime_timedelta((PyDateTime_DateTime *)result, |
| 3040 | (PyDateTime_Delta *)dst, 1); |
| 3041 | Py_DECREF(temp); |
| 3042 | if (result == NULL) |
| 3043 | goto Fail; |
| 3044 | } |
| 3045 | Py_DECREF(delta); |
| 3046 | Py_DECREF(dst); |
| 3047 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3049 | |
| 3050 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3052 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | /* fall thru to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3055 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3056 | Py_XDECREF(off); |
| 3057 | Py_XDECREF(dst); |
| 3058 | Py_XDECREF(delta); |
| 3059 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3063 | /* |
| 3064 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3065 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3066 | */ |
| 3067 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3068 | static PyObject * |
| 3069 | tzinfo_reduce(PyObject *self) |
| 3070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | PyObject *args, *state, *tmp; |
| 3072 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3073 | _Py_IDENTIFIER(__getinitargs__); |
| 3074 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | tmp = PyTuple_New(0); |
| 3077 | if (tmp == NULL) |
| 3078 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3079 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3080 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | if (getinitargs != NULL) { |
| 3082 | args = PyObject_CallObject(getinitargs, tmp); |
| 3083 | Py_DECREF(getinitargs); |
| 3084 | if (args == NULL) { |
| 3085 | Py_DECREF(tmp); |
| 3086 | return NULL; |
| 3087 | } |
| 3088 | } |
| 3089 | else { |
| 3090 | PyErr_Clear(); |
| 3091 | args = tmp; |
| 3092 | Py_INCREF(args); |
| 3093 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3094 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3095 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | if (getstate != NULL) { |
| 3097 | state = PyObject_CallObject(getstate, tmp); |
| 3098 | Py_DECREF(getstate); |
| 3099 | if (state == NULL) { |
| 3100 | Py_DECREF(args); |
| 3101 | Py_DECREF(tmp); |
| 3102 | return NULL; |
| 3103 | } |
| 3104 | } |
| 3105 | else { |
| 3106 | PyObject **dictptr; |
| 3107 | PyErr_Clear(); |
| 3108 | state = Py_None; |
| 3109 | dictptr = _PyObject_GetDictPtr(self); |
| 3110 | if (dictptr && *dictptr && PyDict_Size(*dictptr)) |
| 3111 | state = *dictptr; |
| 3112 | Py_INCREF(state); |
| 3113 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | Py_DECREF(tmp); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | if (state == Py_None) { |
| 3118 | Py_DECREF(state); |
| 3119 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3120 | } |
| 3121 | else |
| 3122 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3123 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3124 | |
| 3125 | static PyMethodDef tzinfo_methods[] = { |
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 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3128 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3130 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3131 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3132 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 3135 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3138 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 3141 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3144 | }; |
| 3145 | |
| 3146 | static char tzinfo_doc[] = |
| 3147 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3148 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3149 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3151 | "datetime.tzinfo", /* tp_name */ |
| 3152 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3153 | 0, /* tp_itemsize */ |
| 3154 | 0, /* tp_dealloc */ |
| 3155 | 0, /* tp_print */ |
| 3156 | 0, /* tp_getattr */ |
| 3157 | 0, /* tp_setattr */ |
| 3158 | 0, /* tp_reserved */ |
| 3159 | 0, /* tp_repr */ |
| 3160 | 0, /* tp_as_number */ |
| 3161 | 0, /* tp_as_sequence */ |
| 3162 | 0, /* tp_as_mapping */ |
| 3163 | 0, /* tp_hash */ |
| 3164 | 0, /* tp_call */ |
| 3165 | 0, /* tp_str */ |
| 3166 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3167 | 0, /* tp_setattro */ |
| 3168 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3169 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | tzinfo_doc, /* tp_doc */ |
| 3171 | 0, /* tp_traverse */ |
| 3172 | 0, /* tp_clear */ |
| 3173 | 0, /* tp_richcompare */ |
| 3174 | 0, /* tp_weaklistoffset */ |
| 3175 | 0, /* tp_iter */ |
| 3176 | 0, /* tp_iternext */ |
| 3177 | tzinfo_methods, /* tp_methods */ |
| 3178 | 0, /* tp_members */ |
| 3179 | 0, /* tp_getset */ |
| 3180 | 0, /* tp_base */ |
| 3181 | 0, /* tp_dict */ |
| 3182 | 0, /* tp_descr_get */ |
| 3183 | 0, /* tp_descr_set */ |
| 3184 | 0, /* tp_dictoffset */ |
| 3185 | 0, /* tp_init */ |
| 3186 | 0, /* tp_alloc */ |
| 3187 | PyType_GenericNew, /* tp_new */ |
| 3188 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3189 | }; |
| 3190 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3191 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3192 | |
| 3193 | static PyObject * |
| 3194 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3195 | { |
| 3196 | PyObject *offset; |
| 3197 | PyObject *name = NULL; |
| 3198 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|O!:timezone", timezone_kws, |
| 3199 | &PyDateTime_DeltaType, &offset, |
| 3200 | &PyUnicode_Type, &name)) |
| 3201 | return new_timezone(offset, name); |
| 3202 | |
| 3203 | return NULL; |
| 3204 | } |
| 3205 | |
| 3206 | static void |
| 3207 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3208 | { |
| 3209 | Py_CLEAR(self->offset); |
| 3210 | Py_CLEAR(self->name); |
| 3211 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3212 | } |
| 3213 | |
| 3214 | static PyObject * |
| 3215 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3216 | PyDateTime_TimeZone *other, int op) |
| 3217 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3218 | if (op != Py_EQ && op != Py_NE) |
| 3219 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3220 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
| 3221 | if (op == Py_EQ) |
| 3222 | Py_RETURN_FALSE; |
| 3223 | else |
| 3224 | Py_RETURN_TRUE; |
| 3225 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3226 | return delta_richcompare(self->offset, other->offset, op); |
| 3227 | } |
| 3228 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3229 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3230 | timezone_hash(PyDateTime_TimeZone *self) |
| 3231 | { |
| 3232 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3233 | } |
| 3234 | |
| 3235 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3236 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3237 | otherwise. |
| 3238 | */ |
| 3239 | static int |
| 3240 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3241 | { |
| 3242 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3243 | return 0; |
| 3244 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3245 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3246 | return -1; |
| 3247 | } |
| 3248 | |
| 3249 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3250 | timezone_repr(PyDateTime_TimeZone *self) |
| 3251 | { |
| 3252 | /* Note that although timezone is not subclassable, it is convenient |
| 3253 | to use Py_TYPE(self)->tp_name here. */ |
| 3254 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3255 | |
| 3256 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3257 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3258 | |
| 3259 | if (self->name == NULL) |
| 3260 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3261 | |
| 3262 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3263 | self->name); |
| 3264 | } |
| 3265 | |
| 3266 | |
| 3267 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3268 | timezone_str(PyDateTime_TimeZone *self) |
| 3269 | { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3270 | int hours, minutes, seconds; |
| 3271 | PyObject *offset; |
| 3272 | char sign; |
| 3273 | |
| 3274 | if (self->name != NULL) { |
| 3275 | Py_INCREF(self->name); |
| 3276 | return self->name; |
| 3277 | } |
| 3278 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3279 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3280 | sign = '-'; |
| 3281 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3282 | if (offset == NULL) |
| 3283 | return NULL; |
| 3284 | } |
| 3285 | else { |
| 3286 | sign = '+'; |
| 3287 | offset = self->offset; |
| 3288 | Py_INCREF(offset); |
| 3289 | } |
| 3290 | /* Offset is not negative here. */ |
| 3291 | seconds = GET_TD_SECONDS(offset); |
| 3292 | Py_DECREF(offset); |
| 3293 | minutes = divmod(seconds, 60, &seconds); |
| 3294 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3295 | /* XXX ignore sub-minute data, curently not allowed. */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3296 | assert(seconds == 0); |
| 3297 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | static PyObject * |
| 3301 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3302 | { |
| 3303 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3304 | return NULL; |
| 3305 | |
| 3306 | return timezone_str(self); |
| 3307 | } |
| 3308 | |
| 3309 | static PyObject * |
| 3310 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3311 | { |
| 3312 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3313 | return NULL; |
| 3314 | |
| 3315 | Py_INCREF(self->offset); |
| 3316 | return self->offset; |
| 3317 | } |
| 3318 | |
| 3319 | static PyObject * |
| 3320 | timezone_dst(PyObject *self, PyObject *dt) |
| 3321 | { |
| 3322 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3323 | return NULL; |
| 3324 | |
| 3325 | Py_RETURN_NONE; |
| 3326 | } |
| 3327 | |
| 3328 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3329 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3330 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3331 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3332 | PyErr_SetString(PyExc_TypeError, |
| 3333 | "fromutc: argument must be a datetime"); |
| 3334 | return NULL; |
| 3335 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3336 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3337 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3338 | "is not self"); |
| 3339 | return NULL; |
| 3340 | } |
| 3341 | |
| 3342 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3343 | } |
| 3344 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3345 | static PyObject * |
| 3346 | timezone_getinitargs(PyDateTime_TimeZone *self) |
| 3347 | { |
| 3348 | if (self->name == NULL) |
| 3349 | return Py_BuildValue("(O)", self->offset); |
| 3350 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3351 | } |
| 3352 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3353 | static PyMethodDef timezone_methods[] = { |
| 3354 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3355 | 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] | 3356 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3357 | |
| 3358 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3359 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3360 | |
| 3361 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3362 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3363 | |
| 3364 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3365 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3366 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3367 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3368 | PyDoc_STR("pickle support")}, |
| 3369 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3370 | {NULL, NULL} |
| 3371 | }; |
| 3372 | |
| 3373 | static char timezone_doc[] = |
| 3374 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3375 | |
| 3376 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3377 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3378 | "datetime.timezone", /* tp_name */ |
| 3379 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3380 | 0, /* tp_itemsize */ |
| 3381 | (destructor)timezone_dealloc, /* tp_dealloc */ |
| 3382 | 0, /* tp_print */ |
| 3383 | 0, /* tp_getattr */ |
| 3384 | 0, /* tp_setattr */ |
| 3385 | 0, /* tp_reserved */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3386 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3387 | 0, /* tp_as_number */ |
| 3388 | 0, /* tp_as_sequence */ |
| 3389 | 0, /* tp_as_mapping */ |
| 3390 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3391 | 0, /* tp_call */ |
| 3392 | (reprfunc)timezone_str, /* tp_str */ |
| 3393 | 0, /* tp_getattro */ |
| 3394 | 0, /* tp_setattro */ |
| 3395 | 0, /* tp_as_buffer */ |
| 3396 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3397 | timezone_doc, /* tp_doc */ |
| 3398 | 0, /* tp_traverse */ |
| 3399 | 0, /* tp_clear */ |
| 3400 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3401 | 0, /* tp_weaklistoffset */ |
| 3402 | 0, /* tp_iter */ |
| 3403 | 0, /* tp_iternext */ |
| 3404 | timezone_methods, /* tp_methods */ |
| 3405 | 0, /* tp_members */ |
| 3406 | 0, /* tp_getset */ |
| 3407 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3408 | 0, /* tp_dict */ |
| 3409 | 0, /* tp_descr_get */ |
| 3410 | 0, /* tp_descr_set */ |
| 3411 | 0, /* tp_dictoffset */ |
| 3412 | 0, /* tp_init */ |
| 3413 | 0, /* tp_alloc */ |
| 3414 | timezone_new, /* tp_new */ |
| 3415 | }; |
| 3416 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3417 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3418 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3419 | */ |
| 3420 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3421 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3422 | */ |
| 3423 | |
| 3424 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3425 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3426 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3430 | static PyObject * |
| 3431 | time_minute(PyDateTime_Time *self, void *unused) |
| 3432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | /* The name time_second conflicted with some platform header file. */ |
| 3437 | static PyObject * |
| 3438 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3440 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | static PyObject * |
| 3444 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | static PyObject * |
| 3450 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3453 | Py_INCREF(result); |
| 3454 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | {"hour", (getter)time_hour}, |
| 3459 | {"minute", (getter)time_minute}, |
| 3460 | {"second", (getter)py_time_second}, |
| 3461 | {"microsecond", (getter)time_microsecond}, |
| 3462 | {"tzinfo", (getter)time_tzinfo}, |
| 3463 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3464 | }; |
| 3465 | |
| 3466 | /* |
| 3467 | * Constructors. |
| 3468 | */ |
| 3469 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3470 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3471 | "tzinfo", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3472 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3473 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3474 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | PyObject *self = NULL; |
| 3477 | PyObject *state; |
| 3478 | int hour = 0; |
| 3479 | int minute = 0; |
| 3480 | int second = 0; |
| 3481 | int usecond = 0; |
| 3482 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3484 | /* Check for invocation from pickle with __getstate__ state */ |
| 3485 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3486 | PyTuple_GET_SIZE(args) <= 2 && |
| 3487 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3488 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 3489 | ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24) |
| 3490 | { |
| 3491 | PyDateTime_Time *me; |
| 3492 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3494 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3495 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3496 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3497 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3498 | "tzinfo state arg"); |
| 3499 | return NULL; |
| 3500 | } |
| 3501 | } |
| 3502 | aware = (char)(tzinfo != Py_None); |
| 3503 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3504 | if (me != NULL) { |
| 3505 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3508 | me->hashcode = -1; |
| 3509 | me->hastzinfo = aware; |
| 3510 | if (aware) { |
| 3511 | Py_INCREF(tzinfo); |
| 3512 | me->tzinfo = tzinfo; |
| 3513 | } |
| 3514 | } |
| 3515 | return (PyObject *)me; |
| 3516 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3518 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, |
| 3519 | &hour, &minute, &second, &usecond, |
| 3520 | &tzinfo)) { |
| 3521 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3522 | return NULL; |
| 3523 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3524 | return NULL; |
| 3525 | self = new_time_ex(hour, minute, second, usecond, tzinfo, |
| 3526 | type); |
| 3527 | } |
| 3528 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | /* |
| 3532 | * Destructor. |
| 3533 | */ |
| 3534 | |
| 3535 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3536 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3538 | if (HASTZINFO(self)) { |
| 3539 | Py_XDECREF(self->tzinfo); |
| 3540 | } |
| 3541 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3545 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3546 | */ |
| 3547 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3548 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3549 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3550 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 3551 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3555 | time_dst(PyObject *self, PyObject *unused) { |
| 3556 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3560 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3561 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3565 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3566 | */ |
| 3567 | |
| 3568 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3569 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3572 | int h = TIME_GET_HOUR(self); |
| 3573 | int m = TIME_GET_MINUTE(self); |
| 3574 | int s = TIME_GET_SECOND(self); |
| 3575 | int us = TIME_GET_MICROSECOND(self); |
| 3576 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | if (us) |
| 3579 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3580 | type_name, h, m, s, us); |
| 3581 | else if (s) |
| 3582 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3583 | type_name, h, m, s); |
| 3584 | else |
| 3585 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3586 | if (result != NULL && HASTZINFO(self)) |
| 3587 | result = append_keyword_tzinfo(result, self->tzinfo); |
| 3588 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3591 | static PyObject * |
| 3592 | time_str(PyDateTime_Time *self) |
| 3593 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3594 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3595 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3596 | |
| 3597 | static PyObject * |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3598 | time_isoformat(PyDateTime_Time *self, PyObject *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3600 | char buf[100]; |
| 3601 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3602 | int us = TIME_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3604 | if (us) |
| 3605 | result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d", |
| 3606 | TIME_GET_HOUR(self), |
| 3607 | TIME_GET_MINUTE(self), |
| 3608 | TIME_GET_SECOND(self), |
| 3609 | us); |
| 3610 | else |
| 3611 | result = PyUnicode_FromFormat("%02d:%02d:%02d", |
| 3612 | TIME_GET_HOUR(self), |
| 3613 | TIME_GET_MINUTE(self), |
| 3614 | TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3615 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3616 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3619 | /* We need to append the UTC offset. */ |
| 3620 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 3621 | Py_None) < 0) { |
| 3622 | Py_DECREF(result); |
| 3623 | return NULL; |
| 3624 | } |
| 3625 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 3626 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3629 | static PyObject * |
| 3630 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | PyObject *result; |
| 3633 | PyObject *tuple; |
| 3634 | PyObject *format; |
| 3635 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3638 | &format)) |
| 3639 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3641 | /* Python's strftime does insane things with the year part of the |
| 3642 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 3643 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | */ |
| 3645 | tuple = Py_BuildValue("iiiiiiiii", |
| 3646 | 1900, 1, 1, /* year, month, day */ |
| 3647 | TIME_GET_HOUR(self), |
| 3648 | TIME_GET_MINUTE(self), |
| 3649 | TIME_GET_SECOND(self), |
| 3650 | 0, 1, -1); /* weekday, daynum, dst */ |
| 3651 | if (tuple == NULL) |
| 3652 | return NULL; |
| 3653 | assert(PyTuple_Size(tuple) == 9); |
| 3654 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3655 | Py_None); |
| 3656 | Py_DECREF(tuple); |
| 3657 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3658 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3659 | |
| 3660 | /* |
| 3661 | * Miscellaneous methods. |
| 3662 | */ |
| 3663 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3664 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3665 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3666 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3667 | PyObject *result = NULL; |
| 3668 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3670 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3671 | if (! PyTime_Check(other)) |
| 3672 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3673 | |
| 3674 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3676 | ((PyDateTime_Time *)other)->data, |
| 3677 | _PyDateTime_TIME_DATASIZE); |
| 3678 | return diff_to_bool(diff, op); |
| 3679 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3680 | offset1 = time_utcoffset(self, NULL); |
| 3681 | if (offset1 == NULL) |
| 3682 | return NULL; |
| 3683 | offset2 = time_utcoffset(other, NULL); |
| 3684 | if (offset2 == NULL) |
| 3685 | goto done; |
| 3686 | /* If they're both naive, or both aware and have the same offsets, |
| 3687 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3688 | * offset2 == Py_None at this point. |
| 3689 | */ |
| 3690 | if ((offset1 == offset2) || |
| 3691 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 3692 | delta_cmp(offset1, offset2) == 0)) { |
| 3693 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3694 | ((PyDateTime_Time *)other)->data, |
| 3695 | _PyDateTime_TIME_DATASIZE); |
| 3696 | result = diff_to_bool(diff, op); |
| 3697 | } |
| 3698 | /* The hard case: both aware with different UTC offsets */ |
| 3699 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 3700 | int offsecs1, offsecs2; |
| 3701 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3702 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 3703 | TIME_GET_MINUTE(self) * 60 + |
| 3704 | TIME_GET_SECOND(self) - |
| 3705 | GET_TD_DAYS(offset1) * 86400 - |
| 3706 | GET_TD_SECONDS(offset1); |
| 3707 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 3708 | TIME_GET_MINUTE(other) * 60 + |
| 3709 | TIME_GET_SECOND(other) - |
| 3710 | GET_TD_DAYS(offset2) * 86400 - |
| 3711 | GET_TD_SECONDS(offset2); |
| 3712 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3713 | if (diff == 0) |
| 3714 | diff = TIME_GET_MICROSECOND(self) - |
| 3715 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3716 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3717 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 3718 | else if (op == Py_EQ) { |
| 3719 | result = Py_False; |
| 3720 | Py_INCREF(result); |
| 3721 | } |
| 3722 | else if (op == Py_NE) { |
| 3723 | result = Py_True; |
| 3724 | Py_INCREF(result); |
| 3725 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3726 | else { |
| 3727 | PyErr_SetString(PyExc_TypeError, |
| 3728 | "can't compare offset-naive and " |
| 3729 | "offset-aware times"); |
| 3730 | } |
| 3731 | done: |
| 3732 | Py_DECREF(offset1); |
| 3733 | Py_XDECREF(offset2); |
| 3734 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3737 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3738 | time_hash(PyDateTime_Time *self) |
| 3739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3740 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3741 | PyObject *offset; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3742 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3743 | offset = time_utcoffset((PyObject *)self, NULL); |
| 3744 | |
| 3745 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3746 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3748 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3749 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | self->hashcode = generic_hash( |
| 3751 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3753 | PyObject *temp1, *temp2; |
| 3754 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3756 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 3757 | TIME_GET_MINUTE(self) * 60 + |
| 3758 | TIME_GET_SECOND(self); |
| 3759 | microseconds = TIME_GET_MICROSECOND(self); |
| 3760 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 3761 | if (temp1 == NULL) { |
| 3762 | Py_DECREF(offset); |
| 3763 | return -1; |
| 3764 | } |
| 3765 | temp2 = delta_subtract(temp1, offset); |
| 3766 | Py_DECREF(temp1); |
| 3767 | if (temp2 == NULL) { |
| 3768 | Py_DECREF(offset); |
| 3769 | return -1; |
| 3770 | } |
| 3771 | self->hashcode = PyObject_Hash(temp2); |
| 3772 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3773 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3774 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3775 | } |
| 3776 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3777 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3778 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3779 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3780 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | PyObject *clone; |
| 3783 | PyObject *tuple; |
| 3784 | int hh = TIME_GET_HOUR(self); |
| 3785 | int mm = TIME_GET_MINUTE(self); |
| 3786 | int ss = TIME_GET_SECOND(self); |
| 3787 | int us = TIME_GET_MICROSECOND(self); |
| 3788 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", |
| 3791 | time_kws, |
| 3792 | &hh, &mm, &ss, &us, &tzinfo)) |
| 3793 | return NULL; |
| 3794 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3795 | if (tuple == NULL) |
| 3796 | return NULL; |
| 3797 | clone = time_new(Py_TYPE(self), tuple, NULL); |
| 3798 | Py_DECREF(tuple); |
| 3799 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3802 | static int |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3803 | time_bool(PyObject *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3804 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3805 | PyObject *offset, *tzinfo; |
| 3806 | int offsecs = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) { |
| 3809 | /* Since utcoffset is in whole minutes, nothing can |
| 3810 | * alter the conclusion that this is nonzero. |
| 3811 | */ |
| 3812 | return 1; |
| 3813 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3814 | tzinfo = GET_TIME_TZINFO(self); |
| 3815 | if (tzinfo != Py_None) { |
| 3816 | offset = call_utcoffset(tzinfo, Py_None); |
| 3817 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3818 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3819 | offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset); |
| 3820 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3821 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3822 | return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3825 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3826 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3827 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3828 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3829 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3830 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3831 | */ |
| 3832 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3833 | time_getstate(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3834 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | PyObject *basestate; |
| 3836 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 3839 | _PyDateTime_TIME_DATASIZE); |
| 3840 | if (basestate != NULL) { |
| 3841 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3842 | result = PyTuple_Pack(1, basestate); |
| 3843 | else |
| 3844 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 3845 | Py_DECREF(basestate); |
| 3846 | } |
| 3847 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3851 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3856 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, |
| 3859 | PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" |
| 3860 | "[+HH:MM].")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3862 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 3863 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3866 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 3869 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3871 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 3872 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3874 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 3875 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 3878 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 3881 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3884 | }; |
| 3885 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3886 | static char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3887 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 3888 | \n\ |
| 3889 | 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] | 3890 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3891 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3892 | static PyNumberMethods time_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | 0, /* nb_add */ |
| 3894 | 0, /* nb_subtract */ |
| 3895 | 0, /* nb_multiply */ |
| 3896 | 0, /* nb_remainder */ |
| 3897 | 0, /* nb_divmod */ |
| 3898 | 0, /* nb_power */ |
| 3899 | 0, /* nb_negative */ |
| 3900 | 0, /* nb_positive */ |
| 3901 | 0, /* nb_absolute */ |
| 3902 | (inquiry)time_bool, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3903 | }; |
| 3904 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3905 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3906 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3907 | "datetime.time", /* tp_name */ |
| 3908 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 3909 | 0, /* tp_itemsize */ |
| 3910 | (destructor)time_dealloc, /* tp_dealloc */ |
| 3911 | 0, /* tp_print */ |
| 3912 | 0, /* tp_getattr */ |
| 3913 | 0, /* tp_setattr */ |
| 3914 | 0, /* tp_reserved */ |
| 3915 | (reprfunc)time_repr, /* tp_repr */ |
| 3916 | &time_as_number, /* tp_as_number */ |
| 3917 | 0, /* tp_as_sequence */ |
| 3918 | 0, /* tp_as_mapping */ |
| 3919 | (hashfunc)time_hash, /* tp_hash */ |
| 3920 | 0, /* tp_call */ |
| 3921 | (reprfunc)time_str, /* tp_str */ |
| 3922 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3923 | 0, /* tp_setattro */ |
| 3924 | 0, /* tp_as_buffer */ |
| 3925 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3926 | time_doc, /* tp_doc */ |
| 3927 | 0, /* tp_traverse */ |
| 3928 | 0, /* tp_clear */ |
| 3929 | time_richcompare, /* tp_richcompare */ |
| 3930 | 0, /* tp_weaklistoffset */ |
| 3931 | 0, /* tp_iter */ |
| 3932 | 0, /* tp_iternext */ |
| 3933 | time_methods, /* tp_methods */ |
| 3934 | 0, /* tp_members */ |
| 3935 | time_getset, /* tp_getset */ |
| 3936 | 0, /* tp_base */ |
| 3937 | 0, /* tp_dict */ |
| 3938 | 0, /* tp_descr_get */ |
| 3939 | 0, /* tp_descr_set */ |
| 3940 | 0, /* tp_dictoffset */ |
| 3941 | 0, /* tp_init */ |
| 3942 | time_alloc, /* tp_alloc */ |
| 3943 | time_new, /* tp_new */ |
| 3944 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3945 | }; |
| 3946 | |
| 3947 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3948 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3949 | */ |
| 3950 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3951 | /* Accessor properties. Properties for day, month, and year are inherited |
| 3952 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3953 | */ |
| 3954 | |
| 3955 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3956 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3957 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3961 | static PyObject * |
| 3962 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 3963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | static PyObject * |
| 3968 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 3969 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3970 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | static PyObject * |
| 3974 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 3975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | static PyObject * |
| 3980 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 3981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3982 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3983 | Py_INCREF(result); |
| 3984 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | {"hour", (getter)datetime_hour}, |
| 3989 | {"minute", (getter)datetime_minute}, |
| 3990 | {"second", (getter)datetime_second}, |
| 3991 | {"microsecond", (getter)datetime_microsecond}, |
| 3992 | {"tzinfo", (getter)datetime_tzinfo}, |
| 3993 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3994 | }; |
| 3995 | |
| 3996 | /* |
| 3997 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3998 | */ |
| 3999 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4000 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4001 | "year", "month", "day", "hour", "minute", "second", |
| 4002 | "microsecond", "tzinfo", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4003 | }; |
| 4004 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4005 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4006 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4007 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4008 | PyObject *self = NULL; |
| 4009 | PyObject *state; |
| 4010 | int year; |
| 4011 | int month; |
| 4012 | int day; |
| 4013 | int hour = 0; |
| 4014 | int minute = 0; |
| 4015 | int second = 0; |
| 4016 | int usecond = 0; |
| 4017 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4019 | /* Check for invocation from pickle with __getstate__ state */ |
| 4020 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 4021 | PyTuple_GET_SIZE(args) <= 2 && |
| 4022 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 4023 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4024 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 4025 | { |
| 4026 | PyDateTime_DateTime *me; |
| 4027 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4029 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4030 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4031 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 4032 | PyErr_SetString(PyExc_TypeError, "bad " |
| 4033 | "tzinfo state arg"); |
| 4034 | return NULL; |
| 4035 | } |
| 4036 | } |
| 4037 | aware = (char)(tzinfo != Py_None); |
| 4038 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4039 | if (me != NULL) { |
| 4040 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4042 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4043 | me->hashcode = -1; |
| 4044 | me->hastzinfo = aware; |
| 4045 | if (aware) { |
| 4046 | Py_INCREF(tzinfo); |
| 4047 | me->tzinfo = tzinfo; |
| 4048 | } |
| 4049 | } |
| 4050 | return (PyObject *)me; |
| 4051 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, |
| 4054 | &year, &month, &day, &hour, &minute, |
| 4055 | &second, &usecond, &tzinfo)) { |
| 4056 | if (check_date_args(year, month, day) < 0) |
| 4057 | return NULL; |
| 4058 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 4059 | return NULL; |
| 4060 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4061 | return NULL; |
| 4062 | self = new_datetime_ex(year, month, day, |
| 4063 | hour, minute, second, usecond, |
| 4064 | tzinfo, type); |
| 4065 | } |
| 4066 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4069 | /* TM_FUNC is the shared type of localtime() and gmtime(). */ |
| 4070 | typedef struct tm *(*TM_FUNC)(const time_t *timer); |
| 4071 | |
| 4072 | /* Internal helper. |
| 4073 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4074 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4075 | */ |
| 4076 | static PyObject * |
| 4077 | 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] | 4078 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4079 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4080 | struct tm *tm; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | tm = f(&timet); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4083 | if (tm == NULL) { |
| 4084 | #ifdef EINVAL |
| 4085 | if (errno == 0) |
| 4086 | errno = EINVAL; |
| 4087 | #endif |
| 4088 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | } |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4090 | |
| 4091 | /* The platform localtime/gmtime may insert leap seconds, |
| 4092 | * indicated by tm->tm_sec > 59. We don't care about them, |
| 4093 | * except to the extent that passing them on to the datetime |
| 4094 | * constructor would raise ValueError for a reason that |
| 4095 | * made no sense to the user. |
| 4096 | */ |
| 4097 | if (tm->tm_sec > 59) |
| 4098 | tm->tm_sec = 59; |
| 4099 | return PyObject_CallFunction(cls, "iiiiiiiO", |
| 4100 | tm->tm_year + 1900, |
| 4101 | tm->tm_mon + 1, |
| 4102 | tm->tm_mday, |
| 4103 | tm->tm_hour, |
| 4104 | tm->tm_min, |
| 4105 | tm->tm_sec, |
| 4106 | us, |
| 4107 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | /* Internal helper. |
| 4111 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4112 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4113 | * have enough bits to cover a datetime's full range of precision, it's |
| 4114 | * better to call datetime_from_timet_and_us provided you have a way |
| 4115 | * to get that much precision (e.g., C time() isn't good enough). |
| 4116 | */ |
| 4117 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4118 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4119 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4121 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4122 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4123 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4124 | if (_PyTime_ObjectToTimeval(timestamp, &timet, &us) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4125 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4126 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | /* Internal helper. |
| 4130 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4131 | * gmtime for f as appropriate. |
| 4132 | */ |
| 4133 | static PyObject * |
| 4134 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4135 | { |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 4136 | _PyTime_timeval t; |
| 4137 | _PyTime_gettimeofday(&t); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4138 | return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec, |
| 4139 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4142 | /*[clinic] |
| 4143 | module datetime |
| 4144 | |
| 4145 | @classmethod |
| 4146 | datetime.now |
| 4147 | |
| 4148 | tz: object = None |
| 4149 | Timezone object. |
| 4150 | |
| 4151 | Returns new datetime object representing current time local to tz. |
| 4152 | |
| 4153 | If no tz is specified, uses local timezone. |
| 4154 | [clinic]*/ |
| 4155 | |
| 4156 | PyDoc_STRVAR(datetime_now__doc__, |
| 4157 | "Returns new datetime object representing current time local to tz.\n" |
| 4158 | "\n" |
| 4159 | "datetime.now(tz=None)\n" |
| 4160 | " tz\n" |
| 4161 | " Timezone object.\n" |
| 4162 | "\n" |
| 4163 | "If no tz is specified, uses local timezone."); |
| 4164 | |
| 4165 | #define DATETIME_NOW_METHODDEF \ |
| 4166 | {"now", (PyCFunction)datetime_now, METH_VARARGS|METH_KEYWORDS|METH_CLASS, datetime_now__doc__}, |
| 4167 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4168 | static PyObject * |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4169 | datetime_now_impl(PyObject *cls, PyObject *tz); |
| 4170 | |
| 4171 | static PyObject * |
| 4172 | datetime_now(PyObject *cls, PyObject *args, PyObject *kwargs) |
| 4173 | { |
| 4174 | PyObject *return_value = NULL; |
| 4175 | static char *_keywords[] = {"tz", NULL}; |
| 4176 | PyObject *tz = Py_None; |
| 4177 | |
| 4178 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4179 | "|O:now", _keywords, |
| 4180 | &tz)) |
| 4181 | goto exit; |
| 4182 | return_value = datetime_now_impl(cls, tz); |
| 4183 | |
| 4184 | exit: |
| 4185 | return return_value; |
| 4186 | } |
| 4187 | |
| 4188 | static PyObject * |
| 4189 | datetime_now_impl(PyObject *cls, PyObject *tz) |
| 4190 | /*[clinic checksum: 328b54387f4c2f8cb534997e1bd55f8cb38c4992]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4191 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4192 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4193 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4194 | /* Return best possible local time -- this isn't constrained by the |
| 4195 | * precision of a timestamp. |
| 4196 | */ |
| 4197 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4198 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4200 | self = datetime_best_possible(cls, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4201 | tz == Py_None ? localtime : gmtime, |
| 4202 | tz); |
| 4203 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4204 | /* Convert UTC to tzinfo's zone. */ |
| 4205 | PyObject *temp = self; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4206 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4207 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | Py_DECREF(temp); |
| 4209 | } |
| 4210 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4213 | /* Return best possible UTC time -- this isn't constrained by the |
| 4214 | * precision of a timestamp. |
| 4215 | */ |
| 4216 | static PyObject * |
| 4217 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | return datetime_best_possible(cls, gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4222 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4223 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4224 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4227 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4228 | PyObject *tzinfo = Py_None; |
| 4229 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4230 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4231 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | keywords, ×tamp, &tzinfo)) |
| 4233 | return NULL; |
| 4234 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4235 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4237 | self = datetime_from_timestamp(cls, |
| 4238 | tzinfo == Py_None ? localtime : gmtime, |
| 4239 | timestamp, |
| 4240 | tzinfo); |
| 4241 | if (self != NULL && tzinfo != Py_None) { |
| 4242 | /* Convert UTC to tzinfo's zone. */ |
| 4243 | PyObject *temp = self; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4244 | |
| 4245 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4246 | Py_DECREF(temp); |
| 4247 | } |
| 4248 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4251 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 4252 | static PyObject * |
| 4253 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 4254 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4255 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4256 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4257 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4258 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4259 | result = datetime_from_timestamp(cls, gmtime, timestamp, |
| 4260 | Py_None); |
| 4261 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4264 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4265 | static PyObject * |
| 4266 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4268 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4269 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4270 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4271 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4272 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4273 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4274 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4275 | if (module == NULL) { |
| 4276 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4277 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4278 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4280 | return _PyObject_CallMethodId(module, &PyId__strptime_datetime, "OOO", |
| 4281 | cls, string, format); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4284 | /* Return new datetime from date/datetime and time arguments. */ |
| 4285 | static PyObject * |
| 4286 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4288 | static char *keywords[] = {"date", "time", NULL}; |
| 4289 | PyObject *date; |
| 4290 | PyObject *time; |
| 4291 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, |
| 4294 | &PyDateTime_DateType, &date, |
| 4295 | &PyDateTime_TimeType, &time)) { |
| 4296 | PyObject *tzinfo = Py_None; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | if (HASTZINFO(time)) |
| 4299 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4300 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 4301 | GET_YEAR(date), |
| 4302 | GET_MONTH(date), |
| 4303 | GET_DAY(date), |
| 4304 | TIME_GET_HOUR(time), |
| 4305 | TIME_GET_MINUTE(time), |
| 4306 | TIME_GET_SECOND(time), |
| 4307 | TIME_GET_MICROSECOND(time), |
| 4308 | tzinfo); |
| 4309 | } |
| 4310 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4311 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4312 | |
| 4313 | /* |
| 4314 | * Destructor. |
| 4315 | */ |
| 4316 | |
| 4317 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4318 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | if (HASTZINFO(self)) { |
| 4321 | Py_XDECREF(self->tzinfo); |
| 4322 | } |
| 4323 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | /* |
| 4327 | * Indirect access to tzinfo methods. |
| 4328 | */ |
| 4329 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4330 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4331 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4332 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 4333 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4337 | datetime_dst(PyObject *self, PyObject *unused) { |
| 4338 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4342 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 4343 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4347 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4348 | */ |
| 4349 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4350 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4351 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4352 | */ |
| 4353 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4354 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4355 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | /* Note that the C-level additions can't overflow, because of |
| 4358 | * invariant bounds on the member values. |
| 4359 | */ |
| 4360 | int year = GET_YEAR(date); |
| 4361 | int month = GET_MONTH(date); |
| 4362 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4363 | int hour = DATE_GET_HOUR(date); |
| 4364 | int minute = DATE_GET_MINUTE(date); |
| 4365 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4366 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4367 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4369 | assert(factor == 1 || factor == -1); |
| 4370 | if (normalize_datetime(&year, &month, &day, |
| 4371 | &hour, &minute, &second, µsecond) < 0) |
| 4372 | return NULL; |
| 4373 | else |
| 4374 | return new_datetime(year, month, day, |
| 4375 | hour, minute, second, microsecond, |
| 4376 | HASTZINFO(date) ? date->tzinfo : Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4380 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | if (PyDateTime_Check(left)) { |
| 4383 | /* datetime + ??? */ |
| 4384 | if (PyDelta_Check(right)) |
| 4385 | /* datetime + delta */ |
| 4386 | return add_datetime_timedelta( |
| 4387 | (PyDateTime_DateTime *)left, |
| 4388 | (PyDateTime_Delta *)right, |
| 4389 | 1); |
| 4390 | } |
| 4391 | else if (PyDelta_Check(left)) { |
| 4392 | /* delta + datetime */ |
| 4393 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4394 | (PyDateTime_Delta *) left, |
| 4395 | 1); |
| 4396 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4397 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
| 4400 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4401 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4405 | if (PyDateTime_Check(left)) { |
| 4406 | /* datetime - ??? */ |
| 4407 | if (PyDateTime_Check(right)) { |
| 4408 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4409 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4411 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4412 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 4413 | offset2 = offset1 = Py_None; |
| 4414 | Py_INCREF(offset1); |
| 4415 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4417 | else { |
| 4418 | offset1 = datetime_utcoffset(left, NULL); |
| 4419 | if (offset1 == NULL) |
| 4420 | return NULL; |
| 4421 | offset2 = datetime_utcoffset(right, NULL); |
| 4422 | if (offset2 == NULL) { |
| 4423 | Py_DECREF(offset1); |
| 4424 | return NULL; |
| 4425 | } |
| 4426 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 4427 | PyErr_SetString(PyExc_TypeError, |
| 4428 | "can't subtract offset-naive and " |
| 4429 | "offset-aware datetimes"); |
| 4430 | Py_DECREF(offset1); |
| 4431 | Py_DECREF(offset2); |
| 4432 | return NULL; |
| 4433 | } |
| 4434 | } |
| 4435 | if ((offset1 != offset2) && |
| 4436 | delta_cmp(offset1, offset2) != 0) { |
| 4437 | offdiff = delta_subtract(offset1, offset2); |
| 4438 | if (offdiff == NULL) { |
| 4439 | Py_DECREF(offset1); |
| 4440 | Py_DECREF(offset2); |
| 4441 | return NULL; |
| 4442 | } |
| 4443 | } |
| 4444 | Py_DECREF(offset1); |
| 4445 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4446 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4447 | GET_MONTH(left), |
| 4448 | GET_DAY(left)) - |
| 4449 | ymd_to_ord(GET_YEAR(right), |
| 4450 | GET_MONTH(right), |
| 4451 | GET_DAY(right)); |
| 4452 | /* These can't overflow, since the values are |
| 4453 | * normalized. At most this gives the number of |
| 4454 | * seconds in one day. |
| 4455 | */ |
| 4456 | delta_s = (DATE_GET_HOUR(left) - |
| 4457 | DATE_GET_HOUR(right)) * 3600 + |
| 4458 | (DATE_GET_MINUTE(left) - |
| 4459 | DATE_GET_MINUTE(right)) * 60 + |
| 4460 | (DATE_GET_SECOND(left) - |
| 4461 | DATE_GET_SECOND(right)); |
| 4462 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4463 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Victor Stinner | 70e11ac | 2013-11-08 00:50:58 +0100 | [diff] [blame^] | 4465 | if (result == NULL) |
| 4466 | return NULL; |
| 4467 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4468 | if (offdiff != NULL) { |
| 4469 | PyObject *temp = result; |
| 4470 | result = delta_subtract(result, offdiff); |
| 4471 | Py_DECREF(temp); |
| 4472 | Py_DECREF(offdiff); |
| 4473 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4474 | } |
| 4475 | else if (PyDelta_Check(right)) { |
| 4476 | /* datetime - delta */ |
| 4477 | result = add_datetime_timedelta( |
| 4478 | (PyDateTime_DateTime *)left, |
| 4479 | (PyDateTime_Delta *)right, |
| 4480 | -1); |
| 4481 | } |
| 4482 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 | if (result == Py_NotImplemented) |
| 4485 | Py_INCREF(result); |
| 4486 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* Various ways to turn a datetime into a string. */ |
| 4490 | |
| 4491 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4492 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4495 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | if (DATE_GET_MICROSECOND(self)) { |
| 4498 | baserepr = PyUnicode_FromFormat( |
| 4499 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 4500 | type_name, |
| 4501 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4502 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4503 | DATE_GET_SECOND(self), |
| 4504 | DATE_GET_MICROSECOND(self)); |
| 4505 | } |
| 4506 | else if (DATE_GET_SECOND(self)) { |
| 4507 | baserepr = PyUnicode_FromFormat( |
| 4508 | "%s(%d, %d, %d, %d, %d, %d)", |
| 4509 | type_name, |
| 4510 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4511 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4512 | DATE_GET_SECOND(self)); |
| 4513 | } |
| 4514 | else { |
| 4515 | baserepr = PyUnicode_FromFormat( |
| 4516 | "%s(%d, %d, %d, %d, %d)", |
| 4517 | type_name, |
| 4518 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4519 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4520 | } |
| 4521 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4522 | return baserepr; |
| 4523 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4526 | static PyObject * |
| 4527 | datetime_str(PyDateTime_DateTime *self) |
| 4528 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4529 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "(s)", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4530 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4531 | |
| 4532 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4533 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | int sep = 'T'; |
| 4536 | static char *keywords[] = {"sep", NULL}; |
| 4537 | char buffer[100]; |
| 4538 | PyObject *result; |
| 4539 | int us = DATE_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep)) |
| 4542 | return NULL; |
| 4543 | if (us) |
| 4544 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d", |
| 4545 | GET_YEAR(self), GET_MONTH(self), |
| 4546 | GET_DAY(self), (int)sep, |
| 4547 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4548 | DATE_GET_SECOND(self), us); |
| 4549 | else |
| 4550 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d", |
| 4551 | GET_YEAR(self), GET_MONTH(self), |
| 4552 | GET_DAY(self), (int)sep, |
| 4553 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4554 | DATE_GET_SECOND(self)); |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4556 | if (!result || !HASTZINFO(self)) |
| 4557 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | /* We need to append the UTC offset. */ |
| 4560 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 4561 | (PyObject *)self) < 0) { |
| 4562 | Py_DECREF(result); |
| 4563 | return NULL; |
| 4564 | } |
| 4565 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 4566 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4569 | static PyObject * |
| 4570 | datetime_ctime(PyDateTime_DateTime *self) |
| 4571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4572 | return format_ctime((PyDateTime_Date *)self, |
| 4573 | DATE_GET_HOUR(self), |
| 4574 | DATE_GET_MINUTE(self), |
| 4575 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4578 | /* Miscellaneous methods. */ |
| 4579 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4580 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4581 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4582 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4583 | PyObject *result = NULL; |
| 4584 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4587 | if (! PyDateTime_Check(other)) { |
| 4588 | if (PyDate_Check(other)) { |
| 4589 | /* Prevent invocation of date_richcompare. We want to |
| 4590 | return NotImplemented here to give the other object |
| 4591 | a chance. But since DateTime is a subclass of |
| 4592 | Date, if the other object is a Date, it would |
| 4593 | compute an ordering based on the date part alone, |
| 4594 | and we don't want that. So force unequal or |
| 4595 | uncomparable here in that case. */ |
| 4596 | if (op == Py_EQ) |
| 4597 | Py_RETURN_FALSE; |
| 4598 | if (op == Py_NE) |
| 4599 | Py_RETURN_TRUE; |
| 4600 | return cmperror(self, other); |
| 4601 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4602 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4604 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4605 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4607 | ((PyDateTime_DateTime *)other)->data, |
| 4608 | _PyDateTime_DATETIME_DATASIZE); |
| 4609 | return diff_to_bool(diff, op); |
| 4610 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4611 | offset1 = datetime_utcoffset(self, NULL); |
| 4612 | if (offset1 == NULL) |
| 4613 | return NULL; |
| 4614 | offset2 = datetime_utcoffset(other, NULL); |
| 4615 | if (offset2 == NULL) |
| 4616 | goto done; |
| 4617 | /* If they're both naive, or both aware and have the same offsets, |
| 4618 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4619 | * offset2 == Py_None at this point. |
| 4620 | */ |
| 4621 | if ((offset1 == offset2) || |
| 4622 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4623 | delta_cmp(offset1, offset2) == 0)) { |
| 4624 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4625 | ((PyDateTime_DateTime *)other)->data, |
| 4626 | _PyDateTime_DATETIME_DATASIZE); |
| 4627 | result = diff_to_bool(diff, op); |
| 4628 | } |
| 4629 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4630 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4631 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4632 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4633 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4634 | other); |
| 4635 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4636 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | diff = GET_TD_DAYS(delta); |
| 4638 | if (diff == 0) |
| 4639 | diff = GET_TD_SECONDS(delta) | |
| 4640 | GET_TD_MICROSECONDS(delta); |
| 4641 | Py_DECREF(delta); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4642 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4643 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4644 | else if (op == Py_EQ) { |
| 4645 | result = Py_False; |
| 4646 | Py_INCREF(result); |
| 4647 | } |
| 4648 | else if (op == Py_NE) { |
| 4649 | result = Py_True; |
| 4650 | Py_INCREF(result); |
| 4651 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4652 | else { |
| 4653 | PyErr_SetString(PyExc_TypeError, |
| 4654 | "can't compare offset-naive and " |
| 4655 | "offset-aware datetimes"); |
| 4656 | } |
| 4657 | done: |
| 4658 | Py_DECREF(offset1); |
| 4659 | Py_XDECREF(offset2); |
| 4660 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4663 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4664 | datetime_hash(PyDateTime_DateTime *self) |
| 4665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4667 | PyObject *offset; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4668 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4669 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4670 | |
| 4671 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4675 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | self->hashcode = generic_hash( |
| 4677 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4679 | PyObject *temp1, *temp2; |
| 4680 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | assert(HASTZINFO(self)); |
| 4683 | days = ymd_to_ord(GET_YEAR(self), |
| 4684 | GET_MONTH(self), |
| 4685 | GET_DAY(self)); |
| 4686 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4687 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4689 | temp1 = new_delta(days, seconds, |
| 4690 | DATE_GET_MICROSECOND(self), |
| 4691 | 1); |
| 4692 | if (temp1 == NULL) { |
| 4693 | Py_DECREF(offset); |
| 4694 | return -1; |
| 4695 | } |
| 4696 | temp2 = delta_subtract(temp1, offset); |
| 4697 | Py_DECREF(temp1); |
| 4698 | if (temp2 == NULL) { |
| 4699 | Py_DECREF(offset); |
| 4700 | return -1; |
| 4701 | } |
| 4702 | self->hashcode = PyObject_Hash(temp2); |
| 4703 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4704 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4705 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | } |
| 4707 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4708 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4709 | |
| 4710 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4711 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | PyObject *clone; |
| 4714 | PyObject *tuple; |
| 4715 | int y = GET_YEAR(self); |
| 4716 | int m = GET_MONTH(self); |
| 4717 | int d = GET_DAY(self); |
| 4718 | int hh = DATE_GET_HOUR(self); |
| 4719 | int mm = DATE_GET_MINUTE(self); |
| 4720 | int ss = DATE_GET_SECOND(self); |
| 4721 | int us = DATE_GET_MICROSECOND(self); |
| 4722 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4724 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", |
| 4725 | datetime_kws, |
| 4726 | &y, &m, &d, &hh, &mm, &ss, &us, |
| 4727 | &tzinfo)) |
| 4728 | return NULL; |
| 4729 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 4730 | if (tuple == NULL) |
| 4731 | return NULL; |
| 4732 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
| 4733 | Py_DECREF(tuple); |
| 4734 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | static PyObject * |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4738 | local_timezone(PyDateTime_DateTime *utc_time) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4739 | { |
| 4740 | PyObject *result = NULL; |
| 4741 | struct tm *timep; |
| 4742 | time_t timestamp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4743 | PyObject *delta; |
| 4744 | PyObject *one_second; |
| 4745 | PyObject *seconds; |
| 4746 | PyObject *nameo = NULL; |
| 4747 | const char *zone = NULL; |
| 4748 | |
| 4749 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 4750 | if (delta == NULL) |
| 4751 | return NULL; |
| 4752 | one_second = new_delta(0, 1, 0, 0); |
| 4753 | if (one_second == NULL) |
| 4754 | goto error; |
| 4755 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 4756 | (PyDateTime_Delta *)one_second); |
| 4757 | Py_DECREF(one_second); |
| 4758 | if (seconds == NULL) |
| 4759 | goto error; |
| 4760 | Py_DECREF(delta); |
| 4761 | timestamp = PyLong_AsLong(seconds); |
| 4762 | Py_DECREF(seconds); |
| 4763 | if (timestamp == -1 && PyErr_Occurred()) |
| 4764 | return NULL; |
| 4765 | timep = localtime(×tamp); |
| 4766 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4767 | zone = timep->tm_zone; |
| 4768 | delta = new_delta(0, timep->tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4769 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4770 | { |
| 4771 | PyObject *local_time; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4772 | local_time = new_datetime(timep->tm_year + 1900, timep->tm_mon + 1, |
| 4773 | timep->tm_mday, timep->tm_hour, timep->tm_min, |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4774 | timep->tm_sec, DATE_GET_MICROSECOND(utc_time), |
| 4775 | utc_time->tzinfo); |
| 4776 | if (local_time == NULL) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4777 | goto error; |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4778 | delta = datetime_subtract(local_time, (PyObject*)utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4779 | /* XXX: before relying on tzname, we should compare delta |
| 4780 | to the offset implied by timezone/altzone */ |
| 4781 | if (daylight && timep->tm_isdst >= 0) |
| 4782 | zone = tzname[timep->tm_isdst % 2]; |
| 4783 | else |
| 4784 | zone = tzname[0]; |
| 4785 | Py_DECREF(local_time); |
| 4786 | } |
| 4787 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4788 | if (zone != NULL) { |
| 4789 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 4790 | if (nameo == NULL) |
| 4791 | goto error; |
| 4792 | } |
| 4793 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 4794 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4795 | error: |
| 4796 | Py_DECREF(delta); |
| 4797 | return result; |
| 4798 | } |
| 4799 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4800 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4801 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4802 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4803 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4804 | PyObject *offset; |
| 4805 | PyObject *temp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4806 | PyObject *tzinfo = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4807 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4808 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4809 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
| 4810 | &tzinfo)) |
| 4811 | return NULL; |
| 4812 | |
| 4813 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4814 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4816 | if (!HASTZINFO(self) || self->tzinfo == Py_None) |
| 4817 | goto NeedAware; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4819 | /* Conversion to self's own time zone is a NOP. */ |
| 4820 | if (self->tzinfo == tzinfo) { |
| 4821 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4822 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4823 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4825 | /* Convert self to UTC. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4826 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4827 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4829 | if (offset == Py_None) { |
| 4830 | Py_DECREF(offset); |
| 4831 | NeedAware: |
| 4832 | PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " |
| 4833 | "a naive datetime"); |
| 4834 | return NULL; |
| 4835 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4836 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4837 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4838 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4839 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4840 | Py_DECREF(offset); |
| 4841 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4842 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4844 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4845 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4846 | if (tzinfo == Py_None) { |
| 4847 | tzinfo = local_timezone(result); |
| 4848 | if (tzinfo == NULL) { |
| 4849 | Py_DECREF(result); |
| 4850 | return NULL; |
| 4851 | } |
| 4852 | } |
| 4853 | else |
| 4854 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4855 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4856 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4857 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4858 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4859 | result = (PyDateTime_DateTime *) |
| 4860 | _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", temp); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4861 | Py_DECREF(temp); |
| 4862 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4863 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4867 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4869 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4871 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4872 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4873 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4874 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 4875 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4876 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4877 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4878 | if (dst != Py_None) |
| 4879 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 4880 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4881 | } |
| 4882 | return build_struct_time(GET_YEAR(self), |
| 4883 | GET_MONTH(self), |
| 4884 | GET_DAY(self), |
| 4885 | DATE_GET_HOUR(self), |
| 4886 | DATE_GET_MINUTE(self), |
| 4887 | DATE_GET_SECOND(self), |
| 4888 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | static PyObject * |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 4892 | datetime_timestamp(PyDateTime_DateTime *self) |
| 4893 | { |
| 4894 | PyObject *result; |
| 4895 | |
| 4896 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 4897 | PyObject *delta; |
| 4898 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 4899 | if (delta == NULL) |
| 4900 | return NULL; |
| 4901 | result = delta_total_seconds(delta); |
| 4902 | Py_DECREF(delta); |
| 4903 | } |
| 4904 | else { |
| 4905 | struct tm time; |
| 4906 | time_t timestamp; |
| 4907 | memset((void *) &time, '\0', sizeof(struct tm)); |
| 4908 | time.tm_year = GET_YEAR(self) - 1900; |
| 4909 | time.tm_mon = GET_MONTH(self) - 1; |
| 4910 | time.tm_mday = GET_DAY(self); |
| 4911 | time.tm_hour = DATE_GET_HOUR(self); |
| 4912 | time.tm_min = DATE_GET_MINUTE(self); |
| 4913 | time.tm_sec = DATE_GET_SECOND(self); |
| 4914 | time.tm_wday = -1; |
| 4915 | time.tm_isdst = -1; |
| 4916 | timestamp = mktime(&time); |
Victor Stinner | 9303749 | 2013-06-25 22:54:35 +0200 | [diff] [blame] | 4917 | if (timestamp == (time_t)(-1) |
| 4918 | #ifndef _AIX |
| 4919 | /* Return value of -1 does not necessarily mean an error, |
| 4920 | * but tm_wday cannot remain set to -1 if mktime succeeded. */ |
| 4921 | && time.tm_wday == -1 |
| 4922 | #else |
| 4923 | /* on AIX, tm_wday is always sets, even on error */ |
| 4924 | #endif |
| 4925 | ) |
| 4926 | { |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 4927 | PyErr_SetString(PyExc_OverflowError, |
| 4928 | "timestamp out of range"); |
| 4929 | return NULL; |
| 4930 | } |
| 4931 | result = PyFloat_FromDouble(timestamp + DATE_GET_MICROSECOND(self) / 1e6); |
| 4932 | } |
| 4933 | return result; |
| 4934 | } |
| 4935 | |
| 4936 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4937 | datetime_getdate(PyDateTime_DateTime *self) |
| 4938 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4939 | return new_date(GET_YEAR(self), |
| 4940 | GET_MONTH(self), |
| 4941 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | static PyObject * |
| 4945 | datetime_gettime(PyDateTime_DateTime *self) |
| 4946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | return new_time(DATE_GET_HOUR(self), |
| 4948 | DATE_GET_MINUTE(self), |
| 4949 | DATE_GET_SECOND(self), |
| 4950 | DATE_GET_MICROSECOND(self), |
| 4951 | Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | static PyObject * |
| 4955 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 4956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4957 | return new_time(DATE_GET_HOUR(self), |
| 4958 | DATE_GET_MINUTE(self), |
| 4959 | DATE_GET_SECOND(self), |
| 4960 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4961 | GET_DT_TZINFO(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | static PyObject * |
| 4965 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4966 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4967 | int y, m, d, hh, mm, ss; |
| 4968 | PyObject *tzinfo; |
| 4969 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4970 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4971 | tzinfo = GET_DT_TZINFO(self); |
| 4972 | if (tzinfo == Py_None) { |
| 4973 | utcself = self; |
| 4974 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4975 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4976 | else { |
| 4977 | PyObject *offset; |
| 4978 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 4979 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 4980 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4981 | if (offset == Py_None) { |
| 4982 | Py_DECREF(offset); |
| 4983 | utcself = self; |
| 4984 | Py_INCREF(utcself); |
| 4985 | } |
| 4986 | else { |
| 4987 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4988 | (PyDateTime_Delta *)offset, -1); |
| 4989 | Py_DECREF(offset); |
| 4990 | if (utcself == NULL) |
| 4991 | return NULL; |
| 4992 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4994 | y = GET_YEAR(utcself); |
| 4995 | m = GET_MONTH(utcself); |
| 4996 | d = GET_DAY(utcself); |
| 4997 | hh = DATE_GET_HOUR(utcself); |
| 4998 | mm = DATE_GET_MINUTE(utcself); |
| 4999 | ss = DATE_GET_SECOND(utcself); |
| 5000 | |
| 5001 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 5005 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 5006 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5007 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5008 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 5009 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 5010 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5011 | */ |
| 5012 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5013 | datetime_getstate(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5014 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | PyObject *basestate; |
| 5016 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5018 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 5019 | _PyDateTime_DATETIME_DATASIZE); |
| 5020 | if (basestate != NULL) { |
| 5021 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 5022 | result = PyTuple_Pack(1, basestate); |
| 5023 | else |
| 5024 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 5025 | Py_DECREF(basestate); |
| 5026 | } |
| 5027 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5031 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5033 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5036 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5039 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5040 | DATETIME_NOW_METHODDEF |
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 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 5043 | METH_NOARGS | METH_CLASS, |
| 5044 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5046 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 5047 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5048 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 5051 | METH_VARARGS | METH_CLASS, |
| 5052 | PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " |
| 5053 | "(like time.time()).")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5055 | {"strptime", (PyCFunction)datetime_strptime, |
| 5056 | METH_VARARGS | METH_CLASS, |
| 5057 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 5058 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5060 | {"combine", (PyCFunction)datetime_combine, |
| 5061 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5062 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5064 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5066 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 5067 | PyDoc_STR("Return date object with same year, month and day.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5069 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 5070 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5072 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 5073 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5075 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 5076 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5078 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 5079 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5080 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5081 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 5082 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 5083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5084 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 5085 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 5088 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
| 5089 | "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" |
| 5090 | "sep is used to separate the year from the time, and " |
| 5091 | "defaults to 'T'.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 5094 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 5097 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5099 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 5100 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5102 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 5103 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5105 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 5106 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5108 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 5109 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5111 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5112 | }; |
| 5113 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5114 | static char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5115 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 5116 | \n\ |
| 5117 | 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] | 5118 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5119 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5120 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5121 | datetime_add, /* nb_add */ |
| 5122 | datetime_subtract, /* nb_subtract */ |
| 5123 | 0, /* nb_multiply */ |
| 5124 | 0, /* nb_remainder */ |
| 5125 | 0, /* nb_divmod */ |
| 5126 | 0, /* nb_power */ |
| 5127 | 0, /* nb_negative */ |
| 5128 | 0, /* nb_positive */ |
| 5129 | 0, /* nb_absolute */ |
| 5130 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5131 | }; |
| 5132 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5133 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5135 | "datetime.datetime", /* tp_name */ |
| 5136 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 5137 | 0, /* tp_itemsize */ |
| 5138 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 5139 | 0, /* tp_print */ |
| 5140 | 0, /* tp_getattr */ |
| 5141 | 0, /* tp_setattr */ |
| 5142 | 0, /* tp_reserved */ |
| 5143 | (reprfunc)datetime_repr, /* tp_repr */ |
| 5144 | &datetime_as_number, /* tp_as_number */ |
| 5145 | 0, /* tp_as_sequence */ |
| 5146 | 0, /* tp_as_mapping */ |
| 5147 | (hashfunc)datetime_hash, /* tp_hash */ |
| 5148 | 0, /* tp_call */ |
| 5149 | (reprfunc)datetime_str, /* tp_str */ |
| 5150 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5151 | 0, /* tp_setattro */ |
| 5152 | 0, /* tp_as_buffer */ |
| 5153 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5154 | datetime_doc, /* tp_doc */ |
| 5155 | 0, /* tp_traverse */ |
| 5156 | 0, /* tp_clear */ |
| 5157 | datetime_richcompare, /* tp_richcompare */ |
| 5158 | 0, /* tp_weaklistoffset */ |
| 5159 | 0, /* tp_iter */ |
| 5160 | 0, /* tp_iternext */ |
| 5161 | datetime_methods, /* tp_methods */ |
| 5162 | 0, /* tp_members */ |
| 5163 | datetime_getset, /* tp_getset */ |
| 5164 | &PyDateTime_DateType, /* tp_base */ |
| 5165 | 0, /* tp_dict */ |
| 5166 | 0, /* tp_descr_get */ |
| 5167 | 0, /* tp_descr_set */ |
| 5168 | 0, /* tp_dictoffset */ |
| 5169 | 0, /* tp_init */ |
| 5170 | datetime_alloc, /* tp_alloc */ |
| 5171 | datetime_new, /* tp_new */ |
| 5172 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5173 | }; |
| 5174 | |
| 5175 | /* --------------------------------------------------------------------------- |
| 5176 | * Module methods and initialization. |
| 5177 | */ |
| 5178 | |
| 5179 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5181 | }; |
| 5182 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5183 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 5184 | * datetime.h. |
| 5185 | */ |
| 5186 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5187 | &PyDateTime_DateType, |
| 5188 | &PyDateTime_DateTimeType, |
| 5189 | &PyDateTime_TimeType, |
| 5190 | &PyDateTime_DeltaType, |
| 5191 | &PyDateTime_TZInfoType, |
| 5192 | new_date_ex, |
| 5193 | new_datetime_ex, |
| 5194 | new_time_ex, |
| 5195 | new_delta_ex, |
| 5196 | datetime_fromtimestamp, |
| 5197 | date_fromtimestamp |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5198 | }; |
| 5199 | |
| 5200 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5201 | |
| 5202 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5204 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5205 | "Fast implementation of the datetime type.", |
| 5206 | -1, |
| 5207 | module_methods, |
| 5208 | NULL, |
| 5209 | NULL, |
| 5210 | NULL, |
| 5211 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5212 | }; |
| 5213 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5214 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5215 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | PyObject *m; /* a module object */ |
| 5218 | PyObject *d; /* its dict */ |
| 5219 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5220 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5222 | m = PyModule_Create(&datetimemodule); |
| 5223 | if (m == NULL) |
| 5224 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5226 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 5227 | return NULL; |
| 5228 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 5229 | return NULL; |
| 5230 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 5231 | return NULL; |
| 5232 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 5233 | return NULL; |
| 5234 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 5235 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5236 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 5237 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5239 | /* timedelta values */ |
| 5240 | d = PyDateTime_DeltaType.tp_dict; |
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 | x = new_delta(0, 0, 1, 0); |
| 5243 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5244 | return NULL; |
| 5245 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 5248 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5249 | return NULL; |
| 5250 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5252 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 5253 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5254 | return NULL; |
| 5255 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5257 | /* date values */ |
| 5258 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5260 | x = new_date(1, 1, 1); |
| 5261 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5262 | return NULL; |
| 5263 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5265 | x = new_date(MAXYEAR, 12, 31); |
| 5266 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5267 | return NULL; |
| 5268 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5270 | x = new_delta(1, 0, 0, 0); |
| 5271 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5272 | return NULL; |
| 5273 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5275 | /* time values */ |
| 5276 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5278 | x = new_time(0, 0, 0, 0, Py_None); |
| 5279 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5280 | return NULL; |
| 5281 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | x = new_time(23, 59, 59, 999999, Py_None); |
| 5284 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5285 | return NULL; |
| 5286 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5288 | x = new_delta(0, 0, 1, 0); |
| 5289 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5290 | return NULL; |
| 5291 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5293 | /* datetime values */ |
| 5294 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5296 | x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); |
| 5297 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5298 | return NULL; |
| 5299 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5301 | x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); |
| 5302 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5303 | return NULL; |
| 5304 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5306 | x = new_delta(0, 0, 1, 0); |
| 5307 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5308 | return NULL; |
| 5309 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5310 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5311 | /* timezone values */ |
| 5312 | d = PyDateTime_TimeZoneType.tp_dict; |
| 5313 | |
| 5314 | delta = new_delta(0, 0, 0, 0); |
| 5315 | if (delta == NULL) |
| 5316 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5317 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5318 | Py_DECREF(delta); |
| 5319 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 5320 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 5321 | PyDateTime_TimeZone_UTC = x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5322 | |
| 5323 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 5324 | if (delta == NULL) |
| 5325 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5326 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5327 | Py_DECREF(delta); |
| 5328 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5329 | return NULL; |
| 5330 | Py_DECREF(x); |
| 5331 | |
| 5332 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 5333 | if (delta == NULL) |
| 5334 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5335 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5336 | Py_DECREF(delta); |
| 5337 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5338 | return NULL; |
| 5339 | Py_DECREF(x); |
| 5340 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5341 | /* Epoch */ |
| 5342 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
| 5343 | PyDateTime_TimeZone_UTC); |
| 5344 | if (PyDateTime_Epoch == NULL) |
| 5345 | return NULL; |
| 5346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5347 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5348 | PyModule_AddIntMacro(m, MINYEAR); |
| 5349 | PyModule_AddIntMacro(m, MAXYEAR); |
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 | Py_INCREF(&PyDateTime_DateType); |
| 5352 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5354 | Py_INCREF(&PyDateTime_DateTimeType); |
| 5355 | PyModule_AddObject(m, "datetime", |
| 5356 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5358 | Py_INCREF(&PyDateTime_TimeType); |
| 5359 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5361 | Py_INCREF(&PyDateTime_DeltaType); |
| 5362 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5364 | Py_INCREF(&PyDateTime_TZInfoType); |
| 5365 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5366 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5367 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 5368 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 5369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5370 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 5371 | if (x == NULL) |
| 5372 | return NULL; |
| 5373 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 5376 | * pasting together 4 single years. |
| 5377 | */ |
| 5378 | assert(DI4Y == 4 * 365 + 1); |
| 5379 | assert(DI4Y == days_before_year(4+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5381 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 5382 | * get from pasting together 4 100-year cycles. |
| 5383 | */ |
| 5384 | assert(DI400Y == 4 * DI100Y + 1); |
| 5385 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 5388 | * pasting together 25 4-year cycles. |
| 5389 | */ |
| 5390 | assert(DI100Y == 25 * DI4Y - 1); |
| 5391 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5392 | |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5393 | one = PyLong_FromLong(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | us_per_ms = PyLong_FromLong(1000); |
| 5395 | us_per_second = PyLong_FromLong(1000000); |
| 5396 | us_per_minute = PyLong_FromLong(60000000); |
| 5397 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5398 | if (one == NULL || us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5399 | us_per_minute == NULL || seconds_per_day == NULL) |
| 5400 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5402 | /* The rest are too big for 32-bit ints, but even |
| 5403 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 5404 | */ |
| 5405 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 5406 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 5407 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 5408 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 5409 | return NULL; |
| 5410 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5411 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5412 | |
| 5413 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5414 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5415 | x.n = x stripped of its timezone -- its naive time. |
| 5416 | 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] | 5417 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5418 | 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] | 5419 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5420 | x.s = x's standard offset, x.o - x.d |
| 5421 | |
| 5422 | Now some derived rules, where k is a duration (timedelta). |
| 5423 | |
| 5424 | 1. x.o = x.s + x.d |
| 5425 | This follows from the definition of x.s. |
| 5426 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5427 | 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] | 5428 | This is actually a requirement, an assumption we need to make about |
| 5429 | sane tzinfo classes. |
| 5430 | |
| 5431 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 5432 | This is again a requirement for a sane tzinfo class. |
| 5433 | |
| 5434 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5435 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5436 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5437 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5438 | Again follows from how arithmetic is defined. |
| 5439 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5440 | 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] | 5441 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 5442 | None when called). |
| 5443 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5444 | 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] | 5445 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5446 | |
| 5447 | By #3, we want |
| 5448 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5449 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5450 | |
| 5451 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 5452 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 5453 | becomes true; in effect, we want to solve [2] for k: |
| 5454 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5455 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5456 | |
| 5457 | By #1, this is the same as |
| 5458 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5459 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5460 | |
| 5461 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 5462 | Substituting that into [3], |
| 5463 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5464 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 5465 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 5466 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 5467 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5468 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5469 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 5470 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 5471 | very large, since all offset-returning methods return a duration of magnitude |
| 5472 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 5473 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5474 | |
| 5475 | In any case, the new value is |
| 5476 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5477 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5478 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5479 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 5480 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5481 | |
| 5482 | At this point, if |
| 5483 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5484 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5485 | |
| 5486 | 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] | 5487 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 5488 | 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] | 5489 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 5490 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 5491 | 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] | 5492 | the only spelling that makes sense on the local wall clock. |
| 5493 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5494 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 5495 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 5496 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5497 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5498 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5499 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5500 | Now |
| 5501 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5502 | (y + y.s).n = by #5 |
| 5503 | y.n + y.s = since y.n = x.n |
| 5504 | x.n + y.s = since z and y are have the same tzinfo member, |
| 5505 | y.s = z.s by #2 |
| 5506 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5507 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5508 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5509 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5510 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5511 | x.n - ((x.n + z.s) - z.o) = expanding |
| 5512 | x.n - x.n - z.s + z.o = cancelling |
| 5513 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5514 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5515 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5516 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5517 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5518 | 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] | 5519 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 5520 | 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] | 5521 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5522 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 5523 | 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] | 5524 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5525 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5526 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5527 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5528 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5529 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5530 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5531 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5532 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5533 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5534 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 5535 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 5536 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 5537 | the justifications for the kinds of substitutions we've done several times |
| 5538 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5539 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5540 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 5542 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 5543 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 5544 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 5545 | - z.o + z'.o = #1 twice |
| 5546 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 5547 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5548 | |
| 5549 | 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] | 5550 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 5551 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5552 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5553 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 5554 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 5555 | would have to change the result dst() returns: we start in DST, and moving |
| 5556 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5557 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5558 | There isn't a sane case where this can happen. The closest it gets is at |
| 5559 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 5560 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 5561 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 5562 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 5563 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 5564 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 5565 | standard time. Since that's what the local clock *does*, we want to map both |
| 5566 | 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] | 5567 | in local time, but so it goes -- it's the way the local clock works. |
| 5568 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5569 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 5570 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 5571 | 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] | 5572 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 5573 | |
| 5574 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 5575 | 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] | 5576 | 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] | 5577 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 5578 | but the reasoning doesn't depend on the example -- it depends on there being |
| 5579 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5580 | z' must be in standard time, and is the spelling we want in this case. |
| 5581 | |
| 5582 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 5583 | concerned (because it takes z' as being in standard time rather than the |
| 5584 | daylight time we intend here), but returning it gives the real-life "local |
| 5585 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 5586 | tz. |
| 5587 | |
| 5588 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 5589 | the 1:MM standard time spelling we want. |
| 5590 | |
| 5591 | So how can this break? One of the assumptions must be violated. Two |
| 5592 | possibilities: |
| 5593 | |
| 5594 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 5595 | time zone. This isn't true if, for political reasons or continental drift, |
| 5596 | a region decides to change its base offset from UTC. |
| 5597 | |
| 5598 | 2) There may be versions of "double daylight" time where the tail end of |
| 5599 | the analysis gives up a step too early. I haven't thought about that |
| 5600 | enough to say. |
| 5601 | |
| 5602 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 5603 | "almost all" time zones: so long as the standard offset is invariant, it |
| 5604 | doesn't matter if daylight time transition points change from year to year, or |
| 5605 | if daylight time is skipped in some years; it doesn't matter how large or |
| 5606 | small dst() may get within its bounds; and it doesn't even matter if some |
| 5607 | perverse time zone returns a negative dst()). So a breaking case must be |
| 5608 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5609 | --------------------------------------------------------------------------- */ |