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 | |
| 107 | /* --------------------------------------------------------------------------- |
| 108 | * Math utilities. |
| 109 | */ |
| 110 | |
| 111 | /* k = i+j overflows iff k differs in sign from both inputs, |
| 112 | * iff k^i has sign bit set and k^j has sign bit set, |
| 113 | * iff (k^i)&(k^j) has sign bit set. |
| 114 | */ |
| 115 | #define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 117 | |
| 118 | /* Compute Python divmod(x, y), returning the quotient and storing the |
| 119 | * remainder into *r. The quotient is the floor of x/y, and that's |
| 120 | * the real point of this. C will probably truncate instead (C99 |
| 121 | * requires truncation; C89 left it implementation-defined). |
| 122 | * Simplification: we *require* that y > 0 here. That's appropriate |
| 123 | * for all the uses made of it. This simplifies the code and makes |
| 124 | * the overflow case impossible (divmod(LONG_MIN, -1) is the only |
| 125 | * overflow case). |
| 126 | */ |
| 127 | static int |
| 128 | divmod(int x, int y, int *r) |
| 129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | int quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | assert(y > 0); |
| 133 | quo = x / y; |
| 134 | *r = x - quo * y; |
| 135 | if (*r < 0) { |
| 136 | --quo; |
| 137 | *r += y; |
| 138 | } |
| 139 | assert(0 <= *r && *r < y); |
| 140 | return quo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 143 | /* Nearest integer to m / n for integers m and n. Half-integer results |
| 144 | * are rounded to even. |
| 145 | */ |
| 146 | static PyObject * |
| 147 | divide_nearest(PyObject *m, PyObject *n) |
| 148 | { |
| 149 | PyObject *result; |
| 150 | PyObject *temp; |
| 151 | |
Mark Dickinson | fa68a61 | 2010-06-07 18:47:09 +0000 | [diff] [blame] | 152 | temp = _PyLong_DivmodNear(m, n); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 153 | if (temp == NULL) |
| 154 | return NULL; |
| 155 | result = PyTuple_GET_ITEM(temp, 0); |
| 156 | Py_INCREF(result); |
| 157 | Py_DECREF(temp); |
| 158 | |
| 159 | return result; |
| 160 | } |
| 161 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 162 | /* --------------------------------------------------------------------------- |
| 163 | * General calendrical helper functions |
| 164 | */ |
| 165 | |
| 166 | /* For each month ordinal in 1..12, the number of days in that month, |
| 167 | * and the number of days before that month in the same year. These |
| 168 | * are correct for non-leap years only. |
| 169 | */ |
| 170 | static int _days_in_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | 0, /* unused; this vector uses 1-based indexing */ |
| 172 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | static int _days_before_month[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | 0, /* unused; this vector uses 1-based indexing */ |
| 177 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | /* year -> 1 if leap year, else 0. */ |
| 181 | static int |
| 182 | is_leap(int year) |
| 183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | /* Cast year to unsigned. The result is the same either way, but |
| 185 | * C can generate faster code for unsigned mod than for signed |
| 186 | * mod (especially for % 4 -- a good compiler should just grab |
| 187 | * the last 2 bits when the LHS is unsigned). |
| 188 | */ |
| 189 | const unsigned int ayear = (unsigned int)year; |
| 190 | return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* year, month -> number of days in that month in that year */ |
| 194 | static int |
| 195 | days_in_month(int year, int month) |
| 196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | assert(month >= 1); |
| 198 | assert(month <= 12); |
| 199 | if (month == 2 && is_leap(year)) |
| 200 | return 29; |
| 201 | else |
| 202 | return _days_in_month[month]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* year, month -> number of days in year preceeding first day of month */ |
| 206 | static int |
| 207 | days_before_month(int year, int month) |
| 208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | int days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | assert(month >= 1); |
| 212 | assert(month <= 12); |
| 213 | days = _days_before_month[month]; |
| 214 | if (month > 2 && is_leap(year)) |
| 215 | ++days; |
| 216 | return days; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* year -> number of days before January 1st of year. Remember that we |
| 220 | * start with year 1, so days_before_year(1) == 0. |
| 221 | */ |
| 222 | static int |
| 223 | days_before_year(int year) |
| 224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | int y = year - 1; |
| 226 | /* This is incorrect if year <= 0; we really want the floor |
| 227 | * here. But so long as MINYEAR is 1, the smallest year this |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 228 | * can see is 1. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 230 | assert (year >= 1); |
| 231 | return y*365 + y/4 - y/100 + y/400; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Number of days in 4, 100, and 400 year cycles. That these have |
| 235 | * the correct values is asserted in the module init function. |
| 236 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | #define DI4Y 1461 /* days_before_year(5); days in 4 years */ |
| 238 | #define DI100Y 36524 /* days_before_year(101); days in 100 years */ |
| 239 | #define DI400Y 146097 /* days_before_year(401); days in 400 years */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 240 | |
| 241 | /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ |
| 242 | static void |
| 243 | ord_to_ymd(int ordinal, int *year, int *month, int *day) |
| 244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | int n, n1, n4, n100, n400, leapyear, preceding; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of |
| 248 | * leap years repeats exactly every 400 years. The basic strategy is |
| 249 | * to find the closest 400-year boundary at or before ordinal, then |
| 250 | * work with the offset from that boundary to ordinal. Life is much |
| 251 | * clearer if we subtract 1 from ordinal first -- then the values |
| 252 | * of ordinal at 400-year boundaries are exactly those divisible |
| 253 | * by DI400Y: |
| 254 | * |
| 255 | * D M Y n n-1 |
| 256 | * -- --- ---- ---------- ---------------- |
| 257 | * 31 Dec -400 -DI400Y -DI400Y -1 |
| 258 | * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary |
| 259 | * ... |
| 260 | * 30 Dec 000 -1 -2 |
| 261 | * 31 Dec 000 0 -1 |
| 262 | * 1 Jan 001 1 0 400-year boundary |
| 263 | * 2 Jan 001 2 1 |
| 264 | * 3 Jan 001 3 2 |
| 265 | * ... |
| 266 | * 31 Dec 400 DI400Y DI400Y -1 |
| 267 | * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary |
| 268 | */ |
| 269 | assert(ordinal >= 1); |
| 270 | --ordinal; |
| 271 | n400 = ordinal / DI400Y; |
| 272 | n = ordinal % DI400Y; |
| 273 | *year = n400 * 400 + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | /* Now n is the (non-negative) offset, in days, from January 1 of |
| 276 | * year, to the desired date. Now compute how many 100-year cycles |
| 277 | * precede n. |
| 278 | * Note that it's possible for n100 to equal 4! In that case 4 full |
| 279 | * 100-year cycles precede the desired day, which implies the |
| 280 | * desired day is December 31 at the end of a 400-year cycle. |
| 281 | */ |
| 282 | n100 = n / DI100Y; |
| 283 | n = n % DI100Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | /* Now compute how many 4-year cycles precede it. */ |
| 286 | n4 = n / DI4Y; |
| 287 | n = n % DI4Y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | /* And now how many single years. Again n1 can be 4, and again |
| 290 | * meaning that the desired day is December 31 at the end of the |
| 291 | * 4-year cycle. |
| 292 | */ |
| 293 | n1 = n / 365; |
| 294 | n = n % 365; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | *year += n100 * 100 + n4 * 4 + n1; |
| 297 | if (n1 == 4 || n100 == 4) { |
| 298 | assert(n == 0); |
| 299 | *year -= 1; |
| 300 | *month = 12; |
| 301 | *day = 31; |
| 302 | return; |
| 303 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | /* Now the year is correct, and n is the offset from January 1. We |
| 306 | * find the month via an estimate that's either exact or one too |
| 307 | * large. |
| 308 | */ |
| 309 | leapyear = n1 == 3 && (n4 != 24 || n100 == 3); |
| 310 | assert(leapyear == is_leap(*year)); |
| 311 | *month = (n + 50) >> 5; |
| 312 | preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); |
| 313 | if (preceding > n) { |
| 314 | /* estimate is too large */ |
| 315 | *month -= 1; |
| 316 | preceding -= days_in_month(*year, *month); |
| 317 | } |
| 318 | n -= preceding; |
| 319 | assert(0 <= n); |
| 320 | assert(n < days_in_month(*year, *month)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | *day = n + 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ |
| 326 | static int |
| 327 | ymd_to_ord(int year, int month, int day) |
| 328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | return days_before_year(year) + days_before_month(year, month) + day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */ |
| 333 | static int |
| 334 | weekday(int year, int month, int day) |
| 335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | return (ymd_to_ord(year, month, day) + 6) % 7; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the |
| 340 | * first calendar week containing a Thursday. |
| 341 | */ |
| 342 | static int |
| 343 | iso_week1_monday(int year) |
| 344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ |
| 346 | /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ |
| 347 | int first_weekday = (first_day + 6) % 7; |
| 348 | /* ordinal of closest Monday at or before 1/1 */ |
| 349 | int week1_monday = first_day - first_weekday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ |
| 352 | week1_monday += 7; |
| 353 | return week1_monday; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* --------------------------------------------------------------------------- |
| 357 | * Range checkers. |
| 358 | */ |
| 359 | |
| 360 | /* Check that -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS. If so, return 0. |
| 361 | * If not, raise OverflowError and return -1. |
| 362 | */ |
| 363 | static int |
| 364 | check_delta_day_range(int days) |
| 365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS) |
| 367 | return 0; |
| 368 | PyErr_Format(PyExc_OverflowError, |
| 369 | "days=%d; must have magnitude <= %d", |
| 370 | days, MAX_DELTA_DAYS); |
| 371 | return -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* Check that date arguments are in range. Return 0 if they are. If they |
| 375 | * aren't, raise ValueError and return -1. |
| 376 | */ |
| 377 | static int |
| 378 | check_date_args(int year, int month, int day) |
| 379 | { |
| 380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | if (year < MINYEAR || year > MAXYEAR) { |
| 382 | PyErr_SetString(PyExc_ValueError, |
| 383 | "year is out of range"); |
| 384 | return -1; |
| 385 | } |
| 386 | if (month < 1 || month > 12) { |
| 387 | PyErr_SetString(PyExc_ValueError, |
| 388 | "month must be in 1..12"); |
| 389 | return -1; |
| 390 | } |
| 391 | if (day < 1 || day > days_in_month(year, month)) { |
| 392 | PyErr_SetString(PyExc_ValueError, |
| 393 | "day is out of range for month"); |
| 394 | return -1; |
| 395 | } |
| 396 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Check that time arguments are in range. Return 0 if they are. If they |
| 400 | * aren't, raise ValueError and return -1. |
| 401 | */ |
| 402 | static int |
| 403 | check_time_args(int h, int m, int s, int us) |
| 404 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | if (h < 0 || h > 23) { |
| 406 | PyErr_SetString(PyExc_ValueError, |
| 407 | "hour must be in 0..23"); |
| 408 | return -1; |
| 409 | } |
| 410 | if (m < 0 || m > 59) { |
| 411 | PyErr_SetString(PyExc_ValueError, |
| 412 | "minute must be in 0..59"); |
| 413 | return -1; |
| 414 | } |
| 415 | if (s < 0 || s > 59) { |
| 416 | PyErr_SetString(PyExc_ValueError, |
| 417 | "second must be in 0..59"); |
| 418 | return -1; |
| 419 | } |
| 420 | if (us < 0 || us > 999999) { |
| 421 | PyErr_SetString(PyExc_ValueError, |
| 422 | "microsecond must be in 0..999999"); |
| 423 | return -1; |
| 424 | } |
| 425 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* --------------------------------------------------------------------------- |
| 429 | * Normalization utilities. |
| 430 | */ |
| 431 | |
| 432 | /* One step of a mixed-radix conversion. A "hi" unit is equivalent to |
| 433 | * factor "lo" units. factor must be > 0. If *lo is less than 0, or |
| 434 | * at least factor, enough of *lo is converted into "hi" units so that |
| 435 | * 0 <= *lo < factor. The input values must be such that int overflow |
| 436 | * is impossible. |
| 437 | */ |
| 438 | static void |
| 439 | normalize_pair(int *hi, int *lo, int factor) |
| 440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | assert(factor > 0); |
| 442 | assert(lo != hi); |
| 443 | if (*lo < 0 || *lo >= factor) { |
| 444 | const int num_hi = divmod(*lo, factor, lo); |
| 445 | const int new_hi = *hi + num_hi; |
| 446 | assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi)); |
| 447 | *hi = new_hi; |
| 448 | } |
| 449 | assert(0 <= *lo && *lo < factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Fiddle days (d), seconds (s), and microseconds (us) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | * 0 <= *s < 24*3600 |
| 454 | * 0 <= *us < 1000000 |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 455 | * The input values must be such that the internals don't overflow. |
| 456 | * The way this routine is used, we don't get close. |
| 457 | */ |
| 458 | static void |
| 459 | normalize_d_s_us(int *d, int *s, int *us) |
| 460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | if (*us < 0 || *us >= 1000000) { |
| 462 | normalize_pair(s, us, 1000000); |
| 463 | /* |s| can't be bigger than about |
| 464 | * |original s| + |original us|/1000000 now. |
| 465 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | } |
| 468 | if (*s < 0 || *s >= 24*3600) { |
| 469 | normalize_pair(d, s, 24*3600); |
| 470 | /* |d| can't be bigger than about |
| 471 | * |original d| + |
| 472 | * (|original s| + |original us|/1000000) / (24*3600) now. |
| 473 | */ |
| 474 | } |
| 475 | assert(0 <= *s && *s < 24*3600); |
| 476 | assert(0 <= *us && *us < 1000000); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Fiddle years (y), months (m), and days (d) so that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | * 1 <= *m <= 12 |
| 481 | * 1 <= *d <= days_in_month(*y, *m) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 482 | * The input values must be such that the internals don't overflow. |
| 483 | * The way this routine is used, we don't get close. |
| 484 | */ |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 485 | static int |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 486 | normalize_y_m_d(int *y, int *m, int *d) |
| 487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | int dim; /* # of days in month */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 489 | |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 490 | /* In actual use, m is always the month component extracted from a |
| 491 | * date/datetime object. Therefore it is always in [1, 12] range. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | */ |
Alexander Belopolsky | 59a289d | 2010-10-13 22:54:34 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | assert(1 <= *m && *m <= 12); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | /* Now only day can be out of bounds (year may also be out of bounds |
| 497 | * for a datetime object, but we don't care about that here). |
| 498 | * If day is out of bounds, what to do is arguable, but at least the |
| 499 | * method here is principled and explainable. |
| 500 | */ |
| 501 | dim = days_in_month(*y, *m); |
| 502 | if (*d < 1 || *d > dim) { |
| 503 | /* Move day-1 days from the first of the month. First try to |
| 504 | * get off cheap if we're only one day out of range |
| 505 | * (adjustments for timezone alone can't be worse than that). |
| 506 | */ |
| 507 | if (*d == 0) { |
| 508 | --*m; |
| 509 | if (*m > 0) |
| 510 | *d = days_in_month(*y, *m); |
| 511 | else { |
| 512 | --*y; |
| 513 | *m = 12; |
| 514 | *d = 31; |
| 515 | } |
| 516 | } |
| 517 | else if (*d == dim + 1) { |
| 518 | /* move forward a day */ |
| 519 | ++*m; |
| 520 | *d = 1; |
| 521 | if (*m > 12) { |
| 522 | *m = 1; |
| 523 | ++*y; |
| 524 | } |
| 525 | } |
| 526 | else { |
| 527 | int ordinal = ymd_to_ord(*y, *m, 1) + |
| 528 | *d - 1; |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 529 | if (ordinal < 1 || ordinal > MAXORDINAL) { |
| 530 | goto error; |
| 531 | } else { |
| 532 | ord_to_ymd(ordinal, y, m, d); |
| 533 | return 0; |
| 534 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | assert(*m > 0); |
| 538 | assert(*d > 0); |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 539 | if (MINYEAR <= *y && *y <= MAXYEAR) |
| 540 | return 0; |
| 541 | error: |
| 542 | PyErr_SetString(PyExc_OverflowError, |
| 543 | "date value out of range"); |
| 544 | return -1; |
| 545 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* Fiddle out-of-bounds months and days so that the result makes some kind |
| 549 | * of sense. The parameters are both inputs and outputs. Returns < 0 on |
| 550 | * failure, where failure means the adjusted year is out of bounds. |
| 551 | */ |
| 552 | static int |
| 553 | normalize_date(int *year, int *month, int *day) |
| 554 | { |
Alexander Belopolsky | f03a616 | 2010-05-27 21:42:58 +0000 | [diff] [blame] | 555 | return normalize_y_m_d(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Force all the datetime fields into range. The parameters are both |
| 559 | * inputs and outputs. Returns < 0 on error. |
| 560 | */ |
| 561 | static int |
| 562 | normalize_datetime(int *year, int *month, int *day, |
| 563 | int *hour, int *minute, int *second, |
| 564 | int *microsecond) |
| 565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | normalize_pair(second, microsecond, 1000000); |
| 567 | normalize_pair(minute, second, 60); |
| 568 | normalize_pair(hour, minute, 60); |
| 569 | normalize_pair(day, hour, 24); |
| 570 | return normalize_date(year, month, day); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* --------------------------------------------------------------------------- |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 574 | * Basic object allocation: tp_alloc implementations. These allocate |
| 575 | * Python objects of the right size and type, and do the Python object- |
| 576 | * initialization bit. If there's not enough memory, they return NULL after |
| 577 | * setting MemoryError. All data members remain uninitialized trash. |
| 578 | * |
| 579 | * We abuse the tp_alloc "nitems" argument to communicate whether a tzinfo |
Tim Peters | 03eaf8b | 2003-05-18 02:24:46 +0000 | [diff] [blame] | 580 | * member is needed. This is ugly, imprecise, and possibly insecure. |
| 581 | * tp_basicsize for the time and datetime types is set to the size of the |
| 582 | * struct that has room for the tzinfo member, so subclasses in Python will |
| 583 | * allocate enough space for a tzinfo member whether or not one is actually |
| 584 | * needed. That's the "ugly and imprecise" parts. The "possibly insecure" |
| 585 | * part is that PyType_GenericAlloc() (which subclasses in Python end up |
| 586 | * using) just happens today to effectively ignore the nitems argument |
| 587 | * when tp_itemsize is 0, which it is for these type objects. If that |
| 588 | * changes, perhaps the callers of tp_alloc slots in this file should |
| 589 | * be changed to force a 0 nitems argument unless the type being allocated |
| 590 | * is a base type implemented in this file (so that tp_alloc is time_alloc |
| 591 | * or datetime_alloc below, which know about the nitems abuse). |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 592 | */ |
| 593 | |
| 594 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 595 | time_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | self = (PyObject *) |
| 600 | PyObject_MALLOC(aware ? |
| 601 | sizeof(PyDateTime_Time) : |
| 602 | sizeof(_PyDateTime_BaseTime)); |
| 603 | if (self == NULL) |
| 604 | return (PyObject *)PyErr_NoMemory(); |
| 605 | PyObject_INIT(self, type); |
| 606 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 610 | datetime_alloc(PyTypeObject *type, Py_ssize_t aware) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | PyObject *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | self = (PyObject *) |
| 615 | PyObject_MALLOC(aware ? |
| 616 | sizeof(PyDateTime_DateTime) : |
| 617 | sizeof(_PyDateTime_BaseDateTime)); |
| 618 | if (self == NULL) |
| 619 | return (PyObject *)PyErr_NoMemory(); |
| 620 | PyObject_INIT(self, type); |
| 621 | return self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* --------------------------------------------------------------------------- |
| 625 | * Helpers for setting object fields. These work on pointers to the |
| 626 | * appropriate base class. |
| 627 | */ |
| 628 | |
| 629 | /* For date and datetime. */ |
| 630 | static void |
| 631 | set_date_fields(PyDateTime_Date *self, int y, int m, int d) |
| 632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | self->hashcode = -1; |
| 634 | SET_YEAR(self, y); |
| 635 | SET_MONTH(self, m); |
| 636 | SET_DAY(self, d); |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* --------------------------------------------------------------------------- |
| 640 | * Create various objects, mostly without range checking. |
| 641 | */ |
| 642 | |
| 643 | /* Create a date instance with no range checking. */ |
| 644 | static PyObject * |
| 645 | new_date_ex(int year, int month, int day, PyTypeObject *type) |
| 646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | PyDateTime_Date *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | self = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 650 | if (self != NULL) |
| 651 | set_date_fields(self, year, month, day); |
| 652 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | #define new_date(year, month, day) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | new_date_ex(year, month, day, &PyDateTime_DateType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 657 | |
| 658 | /* Create a datetime instance with no range checking. */ |
| 659 | static PyObject * |
| 660 | 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] | 661 | int second, int usecond, PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | PyDateTime_DateTime *self; |
| 664 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware)); |
| 667 | if (self != NULL) { |
| 668 | self->hastzinfo = aware; |
| 669 | set_date_fields((PyDateTime_Date *)self, year, month, day); |
| 670 | DATE_SET_HOUR(self, hour); |
| 671 | DATE_SET_MINUTE(self, minute); |
| 672 | DATE_SET_SECOND(self, second); |
| 673 | DATE_SET_MICROSECOND(self, usecond); |
| 674 | if (aware) { |
| 675 | Py_INCREF(tzinfo); |
| 676 | self->tzinfo = tzinfo; |
| 677 | } |
| 678 | } |
| 679 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | #define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \ |
| 683 | new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \ |
| 684 | &PyDateTime_DateTimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 685 | |
| 686 | /* Create a time instance with no range checking. */ |
| 687 | static PyObject * |
| 688 | new_time_ex(int hour, int minute, int second, int usecond, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | PyObject *tzinfo, PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | PyDateTime_Time *self; |
| 692 | char aware = tzinfo != Py_None; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | self = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 695 | if (self != NULL) { |
| 696 | self->hastzinfo = aware; |
| 697 | self->hashcode = -1; |
| 698 | TIME_SET_HOUR(self, hour); |
| 699 | TIME_SET_MINUTE(self, minute); |
| 700 | TIME_SET_SECOND(self, second); |
| 701 | TIME_SET_MICROSECOND(self, usecond); |
| 702 | if (aware) { |
| 703 | Py_INCREF(tzinfo); |
| 704 | self->tzinfo = tzinfo; |
| 705 | } |
| 706 | } |
| 707 | return (PyObject *)self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | #define new_time(hh, mm, ss, us, tzinfo) \ |
| 711 | new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 712 | |
| 713 | /* Create a timedelta instance. Normalize the members iff normalize is |
| 714 | * true. Passing false is a speed optimization, if you know for sure |
| 715 | * that seconds and microseconds are already in their proper ranges. In any |
| 716 | * case, raises OverflowError and returns NULL if the normalized days is out |
| 717 | * of range). |
| 718 | */ |
| 719 | static PyObject * |
| 720 | new_delta_ex(int days, int seconds, int microseconds, int normalize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | PyTypeObject *type) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | PyDateTime_Delta *self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | if (normalize) |
| 726 | normalize_d_s_us(&days, &seconds, µseconds); |
| 727 | assert(0 <= seconds && seconds < 24*3600); |
| 728 | assert(0 <= microseconds && microseconds < 1000000); |
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 (check_delta_day_range(days) < 0) |
| 731 | return NULL; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | self = (PyDateTime_Delta *) (type->tp_alloc(type, 0)); |
| 734 | if (self != NULL) { |
| 735 | self->hashcode = -1; |
| 736 | SET_TD_DAYS(self, days); |
| 737 | SET_TD_SECONDS(self, seconds); |
| 738 | SET_TD_MICROSECONDS(self, microseconds); |
| 739 | } |
| 740 | return (PyObject *) self; |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | #define new_delta(d, s, us, normalize) \ |
| 744 | new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 745 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 746 | |
| 747 | typedef struct |
| 748 | { |
| 749 | PyObject_HEAD |
| 750 | PyObject *offset; |
| 751 | PyObject *name; |
| 752 | } PyDateTime_TimeZone; |
| 753 | |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 754 | /* The interned UTC timezone instance */ |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 755 | static PyObject *PyDateTime_TimeZone_UTC; |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 756 | /* The interned Epoch datetime instance */ |
| 757 | static PyObject *PyDateTime_Epoch; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 758 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 759 | /* Create new timezone instance checking offset range. This |
| 760 | function does not check the name argument. Caller must assure |
| 761 | that offset is a timedelta instance and name is either NULL |
| 762 | or a unicode object. */ |
| 763 | static PyObject * |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 764 | create_timezone(PyObject *offset, PyObject *name) |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 765 | { |
| 766 | PyDateTime_TimeZone *self; |
| 767 | PyTypeObject *type = &PyDateTime_TimeZoneType; |
| 768 | |
| 769 | assert(offset != NULL); |
| 770 | assert(PyDelta_Check(offset)); |
| 771 | assert(name == NULL || PyUnicode_Check(name)); |
| 772 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 773 | self = (PyDateTime_TimeZone *)(type->tp_alloc(type, 0)); |
| 774 | if (self == NULL) { |
| 775 | return NULL; |
| 776 | } |
| 777 | Py_INCREF(offset); |
| 778 | self->offset = offset; |
| 779 | Py_XINCREF(name); |
| 780 | self->name = name; |
| 781 | return (PyObject *)self; |
| 782 | } |
| 783 | |
| 784 | static int delta_bool(PyDateTime_Delta *self); |
| 785 | |
| 786 | static PyObject * |
| 787 | new_timezone(PyObject *offset, PyObject *name) |
| 788 | { |
| 789 | assert(offset != NULL); |
| 790 | assert(PyDelta_Check(offset)); |
| 791 | assert(name == NULL || PyUnicode_Check(name)); |
| 792 | |
| 793 | if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) { |
| 794 | Py_INCREF(PyDateTime_TimeZone_UTC); |
| 795 | return PyDateTime_TimeZone_UTC; |
| 796 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 797 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 798 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 799 | " representing a whole number of minutes," |
| 800 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 801 | return NULL; |
| 802 | } |
| 803 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 804 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 805 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 806 | " strictly between -timedelta(hours=24) and" |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 807 | " timedelta(hours=24)," |
| 808 | " not %R.", offset); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 809 | return NULL; |
| 810 | } |
| 811 | |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 812 | return create_timezone(offset, name); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 815 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 816 | * tzinfo helpers. |
| 817 | */ |
| 818 | |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 819 | /* Ensure that p is None or of a tzinfo subclass. Return 0 if OK; if not |
| 820 | * raise TypeError and return -1. |
| 821 | */ |
| 822 | static int |
| 823 | check_tzinfo_subclass(PyObject *p) |
| 824 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | if (p == Py_None || PyTZInfo_Check(p)) |
| 826 | return 0; |
| 827 | PyErr_Format(PyExc_TypeError, |
| 828 | "tzinfo argument must be None or of a tzinfo subclass, " |
| 829 | "not type '%s'", |
| 830 | Py_TYPE(p)->tp_name); |
| 831 | return -1; |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 834 | /* If self has a tzinfo member, return a BORROWED reference to it. Else |
| 835 | * return NULL, which is NOT AN ERROR. There are no error returns here, |
| 836 | * and the caller must not decref the result. |
| 837 | */ |
| 838 | static PyObject * |
| 839 | get_tzinfo_member(PyObject *self) |
| 840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | PyObject *tzinfo = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | if (PyDateTime_Check(self) && HASTZINFO(self)) |
| 844 | tzinfo = ((PyDateTime_DateTime *)self)->tzinfo; |
| 845 | else if (PyTime_Check(self) && HASTZINFO(self)) |
| 846 | tzinfo = ((PyDateTime_Time *)self)->tzinfo; |
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 | return tzinfo; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 851 | /* Call getattr(tzinfo, name)(tzinfoarg), and check the result. tzinfo must |
| 852 | * be an instance of the tzinfo class. If the method returns None, this |
| 853 | * returns None. If the method doesn't return None or timedelta, TypeError is |
| 854 | * raised and this returns NULL. If it returns a timedelta and the value is |
| 855 | * out of range or isn't a whole number of minutes, ValueError is raised and |
| 856 | * this returns NULL. Else result is returned. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 857 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 858 | static PyObject * |
| 859 | call_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 860 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 861 | PyObject *offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | assert(tzinfo != NULL); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 864 | assert(PyTZInfo_Check(tzinfo) || tzinfo == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 866 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 867 | if (tzinfo == Py_None) |
| 868 | Py_RETURN_NONE; |
| 869 | offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); |
| 870 | if (offset == Py_None || offset == NULL) |
| 871 | return offset; |
| 872 | if (PyDelta_Check(offset)) { |
| 873 | if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { |
| 874 | Py_DECREF(offset); |
| 875 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 876 | " representing a whole number of minutes"); |
| 877 | return NULL; |
| 878 | } |
| 879 | if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || |
| 880 | GET_TD_DAYS(offset) < -1 || GET_TD_DAYS(offset) >= 1) { |
| 881 | Py_DECREF(offset); |
| 882 | PyErr_Format(PyExc_ValueError, "offset must be a timedelta" |
| 883 | " strictly between -timedelta(hours=24) and" |
| 884 | " timedelta(hours=24)."); |
| 885 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 889 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | PyErr_Format(PyExc_TypeError, |
| 891 | "tzinfo.%s() must return None or " |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 892 | "timedelta, not '%.200s'", |
| 893 | name, Py_TYPE(offset)->tp_name); |
| 894 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 896 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 897 | return offset; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the |
| 901 | * result. tzinfo must be an instance of the tzinfo class. If utcoffset() |
| 902 | * 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] | 903 | * doesn't return None or timedelta, TypeError is raised and this returns -1. |
| 904 | * If utcoffset() returns an invalid timedelta (out of range, or not a whole |
| 905 | * # of minutes), ValueError is raised and this returns -1. Else *none is |
| 906 | * 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] | 907 | */ |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 908 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 909 | call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg) |
| 910 | { |
| 911 | return call_tzinfo_method(tzinfo, "utcoffset", tzinfoarg); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 914 | /* Call tzinfo.dst(tzinfoarg), and extract an integer from the |
| 915 | * result. tzinfo must be an instance of the tzinfo class. If dst() |
| 916 | * 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] | 917 | & doesn't return None or timedelta, TypeError is raised and this |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 918 | * returns -1. If dst() returns an invalid timedelta for a UTC offset, |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 919 | * ValueError is raised and this returns -1. Else *none is set to 0 and |
| 920 | * the offset is returned (as an int # of minutes east of UTC). |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 921 | */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 922 | static PyObject * |
| 923 | call_dst(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 924 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 925 | return call_tzinfo_method(tzinfo, "dst", tzinfoarg); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 928 | /* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 929 | * 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] | 930 | * 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] | 931 | * returns NULL. If the result is a string, we ensure it is a Unicode |
| 932 | * string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 933 | */ |
| 934 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 935 | call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 938 | _Py_IDENTIFIER(tzname); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | assert(tzinfo != NULL); |
| 941 | assert(check_tzinfo_subclass(tzinfo) >= 0); |
| 942 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | if (tzinfo == Py_None) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 945 | Py_RETURN_NONE; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 946 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 947 | result = _PyObject_CallMethodId(tzinfo, &PyId_tzname, "O", tzinfoarg); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 948 | |
| 949 | if (result == NULL || result == Py_None) |
| 950 | return result; |
| 951 | |
| 952 | if (!PyUnicode_Check(result)) { |
| 953 | PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " |
| 954 | "return None or a string, not '%s'", |
| 955 | Py_TYPE(result)->tp_name); |
| 956 | Py_DECREF(result); |
| 957 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 959 | |
| 960 | return result; |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 963 | /* repr is like "someclass(arg1, arg2)". If tzinfo isn't None, |
| 964 | * stuff |
| 965 | * ", tzinfo=" + repr(tzinfo) |
| 966 | * before the closing ")". |
| 967 | */ |
| 968 | static PyObject * |
| 969 | append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) |
| 970 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | PyObject *temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | assert(PyUnicode_Check(repr)); |
| 974 | assert(tzinfo); |
| 975 | if (tzinfo == Py_None) |
| 976 | return repr; |
| 977 | /* Get rid of the trailing ')'. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 978 | assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); |
| 979 | temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | Py_DECREF(repr); |
| 981 | if (temp == NULL) |
| 982 | return NULL; |
| 983 | repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo); |
| 984 | Py_DECREF(temp); |
| 985 | return repr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /* --------------------------------------------------------------------------- |
| 989 | * String format helpers. |
| 990 | */ |
| 991 | |
| 992 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 993 | format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | static const char *DayNames[] = { |
| 996 | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
| 997 | }; |
| 998 | static const char *MonthNames[] = { |
| 999 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 1000 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 1001 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d", |
| 1006 | DayNames[wday], MonthNames[GET_MONTH(date)-1], |
| 1007 | GET_DAY(date), hours, minutes, seconds, |
| 1008 | GET_YEAR(date)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1011 | static PyObject *delta_negative(PyDateTime_Delta *self); |
| 1012 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1013 | /* Add an hours & minutes UTC offset string to buf. buf has no more than |
| 1014 | * buflen bytes remaining. The UTC offset is gotten by calling |
| 1015 | * tzinfo.uctoffset(tzinfoarg). If that returns None, \0 is stored into |
| 1016 | * *buf, and that's all. Else the returned value is checked for sanity (an |
| 1017 | * integer in range), and if that's OK it's converted to an hours & minutes |
| 1018 | * string of the form |
| 1019 | * sign HH sep MM |
| 1020 | * Returns 0 if everything is OK. If the return value from utcoffset() is |
| 1021 | * bogus, an appropriate exception is set and -1 is returned. |
| 1022 | */ |
| 1023 | static int |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1024 | format_utcoffset(char *buf, size_t buflen, const char *sep, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | PyObject *tzinfo, PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1026 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1027 | PyObject *offset; |
| 1028 | int hours, minutes, seconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | char sign; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | assert(buflen >= 1); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1032 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1033 | offset = call_utcoffset(tzinfo, tzinfoarg); |
| 1034 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1036 | if (offset == Py_None) { |
| 1037 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | *buf = '\0'; |
| 1039 | return 0; |
| 1040 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1041 | /* Offset is normalized, so it is negative if days < 0 */ |
| 1042 | if (GET_TD_DAYS(offset) < 0) { |
| 1043 | PyObject *temp = offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | sign = '-'; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1045 | offset = delta_negative((PyDateTime_Delta *)offset); |
| 1046 | Py_DECREF(temp); |
| 1047 | if (offset == NULL) |
| 1048 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1050 | else { |
| 1051 | sign = '+'; |
| 1052 | } |
| 1053 | /* Offset is not negative here. */ |
| 1054 | seconds = GET_TD_SECONDS(offset); |
| 1055 | Py_DECREF(offset); |
| 1056 | minutes = divmod(seconds, 60, &seconds); |
| 1057 | hours = divmod(minutes, 60, &minutes); |
| 1058 | assert(seconds == 0); |
| 1059 | /* XXX ignore sub-minute data, curently not allowed. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | return 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1065 | static PyObject * |
| 1066 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | PyObject *temp; |
| 1069 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1070 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1071 | _Py_IDENTIFIER(replace); |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 1072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | if (Zreplacement == NULL) |
| 1074 | return NULL; |
| 1075 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1076 | return Zreplacement; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | assert(tzinfoarg != NULL); |
| 1079 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1080 | if (temp == NULL) |
| 1081 | goto Error; |
| 1082 | if (temp == Py_None) { |
| 1083 | Py_DECREF(temp); |
| 1084 | return Zreplacement; |
| 1085 | } |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | assert(PyUnicode_Check(temp)); |
| 1088 | /* Since the tzname is getting stuffed into the |
| 1089 | * format, we have to double any % signs so that |
| 1090 | * strftime doesn't treat them as format codes. |
| 1091 | */ |
| 1092 | Py_DECREF(Zreplacement); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1093 | Zreplacement = _PyObject_CallMethodId(temp, &PyId_replace, "ss", "%", "%%"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | Py_DECREF(temp); |
| 1095 | if (Zreplacement == NULL) |
| 1096 | return NULL; |
| 1097 | if (!PyUnicode_Check(Zreplacement)) { |
| 1098 | PyErr_SetString(PyExc_TypeError, |
| 1099 | "tzname.replace() did not return a string"); |
| 1100 | goto Error; |
| 1101 | } |
| 1102 | return Zreplacement; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1103 | |
| 1104 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | Py_DECREF(Zreplacement); |
| 1106 | return NULL; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1109 | static PyObject * |
| 1110 | make_freplacement(PyObject *object) |
| 1111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | char freplacement[64]; |
| 1113 | if (PyTime_Check(object)) |
| 1114 | sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object)); |
| 1115 | else if (PyDateTime_Check(object)) |
| 1116 | sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object)); |
| 1117 | else |
| 1118 | sprintf(freplacement, "%06d", 0); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | return PyBytes_FromStringAndSize(freplacement, strlen(freplacement)); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1123 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1124 | * 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] | 1125 | * giving special meanings to the %z, %Z and %f format codes via a |
| 1126 | * preprocessing step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1127 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1128 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1129 | */ |
| 1130 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1131 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | PyObject *result = NULL; /* guilty until proved innocent */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1137 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1138 | PyObject *freplacement = NULL; /* py string, replacement for %f */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | const char *pin; /* pointer to next char in input format */ |
| 1141 | Py_ssize_t flen; /* length of input format */ |
| 1142 | char ch; /* next char in input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1145 | char *pnew; /* pointer to available byte in output format */ |
| 1146 | size_t totalnew; /* number bytes total in output format buffer, |
| 1147 | exclusive of trailing \0 */ |
| 1148 | size_t usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | const char *ptoappend; /* ptr to string to append to output buffer */ |
| 1151 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | assert(object && format && timetuple); |
| 1154 | assert(PyUnicode_Check(format)); |
| 1155 | /* Convert the input format to a C string and size */ |
| 1156 | pin = _PyUnicode_AsStringAndSize(format, &flen); |
| 1157 | if (!pin) |
| 1158 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | /* Scan the input format, looking for %z/%Z/%f escapes, building |
| 1161 | * a new format. Since computing the replacements for those codes |
| 1162 | * is expensive, don't unless they're actually used. |
| 1163 | */ |
| 1164 | if (flen > INT_MAX - 1) { |
| 1165 | PyErr_NoMemory(); |
| 1166 | goto Done; |
| 1167 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
| 1170 | newfmt = PyBytes_FromStringAndSize(NULL, totalnew); |
| 1171 | if (newfmt == NULL) goto Done; |
| 1172 | pnew = PyBytes_AsString(newfmt); |
| 1173 | usednew = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | while ((ch = *pin++) != '\0') { |
| 1176 | if (ch != '%') { |
| 1177 | ptoappend = pin - 1; |
| 1178 | ntoappend = 1; |
| 1179 | } |
| 1180 | else if ((ch = *pin++) == '\0') { |
| 1181 | /* There's a lone trailing %; doesn't make sense. */ |
| 1182 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1183 | "ends with raw %"); |
| 1184 | goto Done; |
| 1185 | } |
| 1186 | /* A % has been seen and ch is the character after it. */ |
| 1187 | else if (ch == 'z') { |
| 1188 | if (zreplacement == NULL) { |
| 1189 | /* format utcoffset */ |
| 1190 | char buf[100]; |
| 1191 | PyObject *tzinfo = get_tzinfo_member(object); |
| 1192 | zreplacement = PyBytes_FromStringAndSize("", 0); |
| 1193 | if (zreplacement == NULL) goto Done; |
| 1194 | if (tzinfo != Py_None && tzinfo != NULL) { |
| 1195 | assert(tzinfoarg != NULL); |
| 1196 | if (format_utcoffset(buf, |
| 1197 | sizeof(buf), |
| 1198 | "", |
| 1199 | tzinfo, |
| 1200 | tzinfoarg) < 0) |
| 1201 | goto Done; |
| 1202 | Py_DECREF(zreplacement); |
| 1203 | zreplacement = |
| 1204 | PyBytes_FromStringAndSize(buf, |
| 1205 | strlen(buf)); |
| 1206 | if (zreplacement == NULL) |
| 1207 | goto Done; |
| 1208 | } |
| 1209 | } |
| 1210 | assert(zreplacement != NULL); |
| 1211 | ptoappend = PyBytes_AS_STRING(zreplacement); |
| 1212 | ntoappend = PyBytes_GET_SIZE(zreplacement); |
| 1213 | } |
| 1214 | else if (ch == 'Z') { |
| 1215 | /* format tzname */ |
| 1216 | if (Zreplacement == NULL) { |
| 1217 | Zreplacement = make_Zreplacement(object, |
| 1218 | tzinfoarg); |
| 1219 | if (Zreplacement == NULL) |
| 1220 | goto Done; |
| 1221 | } |
| 1222 | assert(Zreplacement != NULL); |
| 1223 | assert(PyUnicode_Check(Zreplacement)); |
| 1224 | ptoappend = _PyUnicode_AsStringAndSize(Zreplacement, |
| 1225 | &ntoappend); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1226 | if (ptoappend == NULL) |
| 1227 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | } |
| 1229 | else if (ch == 'f') { |
| 1230 | /* format microseconds */ |
| 1231 | if (freplacement == NULL) { |
| 1232 | freplacement = make_freplacement(object); |
| 1233 | if (freplacement == NULL) |
| 1234 | goto Done; |
| 1235 | } |
| 1236 | assert(freplacement != NULL); |
| 1237 | assert(PyBytes_Check(freplacement)); |
| 1238 | ptoappend = PyBytes_AS_STRING(freplacement); |
| 1239 | ntoappend = PyBytes_GET_SIZE(freplacement); |
| 1240 | } |
| 1241 | else { |
| 1242 | /* percent followed by neither z nor Z */ |
| 1243 | ptoappend = pin - 2; |
| 1244 | ntoappend = 2; |
| 1245 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | /* Append the ntoappend chars starting at ptoappend to |
| 1248 | * the new format. |
| 1249 | */ |
| 1250 | if (ntoappend == 0) |
| 1251 | continue; |
| 1252 | assert(ptoappend != NULL); |
| 1253 | assert(ntoappend > 0); |
| 1254 | while (usednew + ntoappend > totalnew) { |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1255 | if (totalnew > (PY_SSIZE_T_MAX >> 1)) { /* overflow */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | PyErr_NoMemory(); |
| 1257 | goto Done; |
| 1258 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1259 | totalnew <<= 1; |
| 1260 | if (_PyBytes_Resize(&newfmt, totalnew) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | pnew = PyBytes_AsString(newfmt) + usednew; |
| 1263 | } |
| 1264 | memcpy(pnew, ptoappend, ntoappend); |
| 1265 | pnew += ntoappend; |
| 1266 | usednew += ntoappend; |
| 1267 | assert(usednew <= totalnew); |
| 1268 | } /* end while() */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | if (_PyBytes_Resize(&newfmt, usednew) < 0) |
| 1271 | goto Done; |
| 1272 | { |
| 1273 | PyObject *format; |
| 1274 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | if (time == NULL) |
| 1277 | goto Done; |
| 1278 | format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); |
| 1279 | if (format != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1280 | _Py_IDENTIFIER(strftime); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1281 | |
| 1282 | result = _PyObject_CallMethodId(time, &PyId_strftime, "OO", |
| 1283 | format, timetuple, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | Py_DECREF(format); |
| 1285 | } |
| 1286 | Py_DECREF(time); |
| 1287 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1288 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | Py_XDECREF(freplacement); |
| 1290 | Py_XDECREF(zreplacement); |
| 1291 | Py_XDECREF(Zreplacement); |
| 1292 | Py_XDECREF(newfmt); |
| 1293 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1296 | /* --------------------------------------------------------------------------- |
| 1297 | * Wrap functions from the time module. These aren't directly available |
| 1298 | * from C. Perhaps they should be. |
| 1299 | */ |
| 1300 | |
| 1301 | /* Call time.time() and return its result (a Python float). */ |
| 1302 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1303 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | PyObject *result = NULL; |
| 1306 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
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 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1309 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1310 | |
| 1311 | result = _PyObject_CallMethodId(time, &PyId_time, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | Py_DECREF(time); |
| 1313 | } |
| 1314 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1318 | * computed from the y,m,d args. |
| 1319 | */ |
| 1320 | static PyObject * |
| 1321 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | PyObject *time; |
| 1324 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | time = PyImport_ImportModuleNoBlock("time"); |
| 1327 | if (time != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1328 | _Py_IDENTIFIER(struct_time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1329 | |
| 1330 | result = _PyObject_CallMethodId(time, &PyId_struct_time, |
| 1331 | "((iiiiiiiii))", |
| 1332 | y, m, d, |
| 1333 | hh, mm, ss, |
| 1334 | weekday(y, m, d), |
| 1335 | days_before_month(y, m) + d, |
| 1336 | dstflag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | Py_DECREF(time); |
| 1338 | } |
| 1339 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* --------------------------------------------------------------------------- |
| 1343 | * Miscellaneous helpers. |
| 1344 | */ |
| 1345 | |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1346 | /* 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] | 1347 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1348 | * This little helper turns that into a bool result for rich comparisons. |
| 1349 | */ |
| 1350 | static PyObject * |
| 1351 | diff_to_bool(int diff, int op) |
| 1352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | PyObject *result; |
| 1354 | int istrue; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | switch (op) { |
| 1357 | case Py_EQ: istrue = diff == 0; break; |
| 1358 | case Py_NE: istrue = diff != 0; break; |
| 1359 | case Py_LE: istrue = diff <= 0; break; |
| 1360 | case Py_GE: istrue = diff >= 0; break; |
| 1361 | case Py_LT: istrue = diff < 0; break; |
| 1362 | case Py_GT: istrue = diff > 0; break; |
| 1363 | default: |
| 1364 | assert(! "op unknown"); |
| 1365 | istrue = 0; /* To shut up compiler */ |
| 1366 | } |
| 1367 | result = istrue ? Py_True : Py_False; |
| 1368 | Py_INCREF(result); |
| 1369 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1372 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1373 | static PyObject * |
| 1374 | cmperror(PyObject *a, PyObject *b) |
| 1375 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | PyErr_Format(PyExc_TypeError, |
| 1377 | "can't compare %s to %s", |
| 1378 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1379 | return NULL; |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1382 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1383 | * Cached Python objects; these are set by the module init function. |
| 1384 | */ |
| 1385 | |
| 1386 | /* Conversion factors. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 1387 | static PyObject *one = NULL; /* 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1389 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1390 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1391 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python int */ |
| 1392 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python int */ |
| 1393 | 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] | 1394 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1395 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1396 | /* --------------------------------------------------------------------------- |
| 1397 | * Class implementations. |
| 1398 | */ |
| 1399 | |
| 1400 | /* |
| 1401 | * PyDateTime_Delta implementation. |
| 1402 | */ |
| 1403 | |
| 1404 | /* Convert a timedelta to a number of us, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1406 | * as a Python int. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1407 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1408 | * due to ubiquitous overflow possibilities. |
| 1409 | */ |
| 1410 | static PyObject * |
| 1411 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | PyObject *x1 = NULL; |
| 1414 | PyObject *x2 = NULL; |
| 1415 | PyObject *x3 = NULL; |
| 1416 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
| 1419 | if (x1 == NULL) |
| 1420 | goto Done; |
| 1421 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1422 | if (x2 == NULL) |
| 1423 | goto Done; |
| 1424 | Py_DECREF(x1); |
| 1425 | x1 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | /* x2 has days in seconds */ |
| 1428 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
| 1429 | if (x1 == NULL) |
| 1430 | goto Done; |
| 1431 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1432 | if (x3 == NULL) |
| 1433 | goto Done; |
| 1434 | Py_DECREF(x1); |
| 1435 | Py_DECREF(x2); |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1436 | /* x1 = */ x2 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | /* x3 has days+seconds in seconds */ |
| 1439 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1440 | if (x1 == NULL) |
| 1441 | goto Done; |
| 1442 | Py_DECREF(x3); |
| 1443 | x3 = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | /* x1 has days+seconds in us */ |
| 1446 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
| 1447 | if (x2 == NULL) |
| 1448 | goto Done; |
| 1449 | result = PyNumber_Add(x1, x2); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1450 | |
| 1451 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | Py_XDECREF(x1); |
| 1453 | Py_XDECREF(x2); |
| 1454 | Py_XDECREF(x3); |
| 1455 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1458 | /* Convert a number of us (as a Python int) to a timedelta. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1459 | */ |
| 1460 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1461 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1462 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | int us; |
| 1464 | int s; |
| 1465 | int d; |
| 1466 | long temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | PyObject *tuple = NULL; |
| 1469 | PyObject *num = NULL; |
| 1470 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1473 | if (tuple == NULL) |
| 1474 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1477 | if (num == NULL) |
| 1478 | goto Done; |
| 1479 | temp = PyLong_AsLong(num); |
| 1480 | num = NULL; |
| 1481 | if (temp == -1 && PyErr_Occurred()) |
| 1482 | goto Done; |
| 1483 | assert(0 <= temp && temp < 1000000); |
| 1484 | us = (int)temp; |
| 1485 | if (us < 0) { |
| 1486 | /* The divisor was positive, so this must be an error. */ |
| 1487 | assert(PyErr_Occurred()); |
| 1488 | goto Done; |
| 1489 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1492 | if (num == NULL) |
| 1493 | goto Done; |
| 1494 | Py_INCREF(num); |
| 1495 | Py_DECREF(tuple); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1498 | if (tuple == NULL) |
| 1499 | goto Done; |
| 1500 | Py_DECREF(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1503 | if (num == NULL) |
| 1504 | goto Done; |
| 1505 | temp = PyLong_AsLong(num); |
| 1506 | num = NULL; |
| 1507 | if (temp == -1 && PyErr_Occurred()) |
| 1508 | goto Done; |
| 1509 | assert(0 <= temp && temp < 24*3600); |
| 1510 | s = (int)temp; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | if (s < 0) { |
| 1513 | /* The divisor was positive, so this must be an error. */ |
| 1514 | assert(PyErr_Occurred()); |
| 1515 | goto Done; |
| 1516 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1519 | if (num == NULL) |
| 1520 | goto Done; |
| 1521 | Py_INCREF(num); |
| 1522 | temp = PyLong_AsLong(num); |
| 1523 | if (temp == -1 && PyErr_Occurred()) |
| 1524 | goto Done; |
| 1525 | d = (int)temp; |
| 1526 | if ((long)d != temp) { |
| 1527 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1528 | "large to fit in a C int"); |
| 1529 | goto Done; |
| 1530 | } |
| 1531 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1532 | |
| 1533 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | Py_XDECREF(tuple); |
| 1535 | Py_XDECREF(num); |
| 1536 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | #define microseconds_to_delta(pymicros) \ |
| 1540 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1541 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1542 | static PyObject * |
| 1543 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | PyObject *pyus_in; |
| 1546 | PyObject *pyus_out; |
| 1547 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | pyus_in = delta_to_microseconds(delta); |
| 1550 | if (pyus_in == NULL) |
| 1551 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1553 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1554 | Py_DECREF(pyus_in); |
| 1555 | if (pyus_out == NULL) |
| 1556 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | result = microseconds_to_delta(pyus_out); |
| 1559 | Py_DECREF(pyus_out); |
| 1560 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1564 | multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) |
| 1565 | { |
| 1566 | PyObject *result = NULL; |
| 1567 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1568 | PyObject *ratio = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1569 | _Py_IDENTIFIER(as_integer_ratio); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1570 | |
| 1571 | pyus_in = delta_to_microseconds(delta); |
| 1572 | if (pyus_in == NULL) |
| 1573 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1574 | ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1575 | if (ratio == NULL) |
| 1576 | goto error; |
| 1577 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0)); |
| 1578 | Py_DECREF(pyus_in); |
| 1579 | pyus_in = NULL; |
| 1580 | if (temp == NULL) |
| 1581 | goto error; |
| 1582 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1)); |
| 1583 | Py_DECREF(temp); |
| 1584 | if (pyus_out == NULL) |
| 1585 | goto error; |
| 1586 | result = microseconds_to_delta(pyus_out); |
| 1587 | Py_DECREF(pyus_out); |
| 1588 | error: |
| 1589 | Py_XDECREF(pyus_in); |
| 1590 | Py_XDECREF(ratio); |
| 1591 | |
| 1592 | return result; |
| 1593 | } |
| 1594 | |
| 1595 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1596 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | PyObject *pyus_in; |
| 1599 | PyObject *pyus_out; |
| 1600 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | pyus_in = delta_to_microseconds(delta); |
| 1603 | if (pyus_in == NULL) |
| 1604 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1607 | Py_DECREF(pyus_in); |
| 1608 | if (pyus_out == NULL) |
| 1609 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | result = microseconds_to_delta(pyus_out); |
| 1612 | Py_DECREF(pyus_out); |
| 1613 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | static PyObject * |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1617 | divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | PyObject *pyus_left; |
| 1620 | PyObject *pyus_right; |
| 1621 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | pyus_left = delta_to_microseconds(left); |
| 1624 | if (pyus_left == NULL) |
| 1625 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | pyus_right = delta_to_microseconds(right); |
| 1628 | if (pyus_right == NULL) { |
| 1629 | Py_DECREF(pyus_left); |
| 1630 | return NULL; |
| 1631 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | result = PyNumber_FloorDivide(pyus_left, pyus_right); |
| 1634 | Py_DECREF(pyus_left); |
| 1635 | Py_DECREF(pyus_right); |
| 1636 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | static PyObject * |
| 1640 | truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) |
| 1641 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | PyObject *pyus_left; |
| 1643 | PyObject *pyus_right; |
| 1644 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1646 | pyus_left = delta_to_microseconds(left); |
| 1647 | if (pyus_left == NULL) |
| 1648 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | pyus_right = delta_to_microseconds(right); |
| 1651 | if (pyus_right == NULL) { |
| 1652 | Py_DECREF(pyus_left); |
| 1653 | return NULL; |
| 1654 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | result = PyNumber_TrueDivide(pyus_left, pyus_right); |
| 1657 | Py_DECREF(pyus_left); |
| 1658 | Py_DECREF(pyus_right); |
| 1659 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | static PyObject * |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1663 | truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f) |
| 1664 | { |
| 1665 | PyObject *result = NULL; |
| 1666 | PyObject *pyus_in = NULL, *temp, *pyus_out; |
| 1667 | PyObject *ratio = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1668 | _Py_IDENTIFIER(as_integer_ratio); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1669 | |
| 1670 | pyus_in = delta_to_microseconds(delta); |
| 1671 | if (pyus_in == NULL) |
| 1672 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1673 | ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1674 | if (ratio == NULL) |
| 1675 | goto error; |
| 1676 | temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1)); |
| 1677 | Py_DECREF(pyus_in); |
| 1678 | pyus_in = NULL; |
| 1679 | if (temp == NULL) |
| 1680 | goto error; |
| 1681 | pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0)); |
| 1682 | Py_DECREF(temp); |
| 1683 | if (pyus_out == NULL) |
| 1684 | goto error; |
| 1685 | result = microseconds_to_delta(pyus_out); |
| 1686 | Py_DECREF(pyus_out); |
| 1687 | error: |
| 1688 | Py_XDECREF(pyus_in); |
| 1689 | Py_XDECREF(ratio); |
| 1690 | |
| 1691 | return result; |
| 1692 | } |
| 1693 | |
| 1694 | static PyObject * |
| 1695 | truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) |
| 1696 | { |
| 1697 | PyObject *result; |
| 1698 | PyObject *pyus_in, *pyus_out; |
| 1699 | pyus_in = delta_to_microseconds(delta); |
| 1700 | if (pyus_in == NULL) |
| 1701 | return NULL; |
| 1702 | pyus_out = divide_nearest(pyus_in, i); |
| 1703 | Py_DECREF(pyus_in); |
| 1704 | if (pyus_out == NULL) |
| 1705 | return NULL; |
| 1706 | result = microseconds_to_delta(pyus_out); |
| 1707 | Py_DECREF(pyus_out); |
| 1708 | |
| 1709 | return result; |
| 1710 | } |
| 1711 | |
| 1712 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1713 | delta_add(PyObject *left, PyObject *right) |
| 1714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1718 | /* delta + delta */ |
| 1719 | /* The C-level additions can't overflow because of the |
| 1720 | * invariant bounds. |
| 1721 | */ |
| 1722 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 1723 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 1724 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 1725 | GET_TD_MICROSECONDS(right); |
| 1726 | result = new_delta(days, seconds, microseconds, 1); |
| 1727 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | if (result == Py_NotImplemented) |
| 1730 | Py_INCREF(result); |
| 1731 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static PyObject * |
| 1735 | delta_negative(PyDateTime_Delta *self) |
| 1736 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | return new_delta(-GET_TD_DAYS(self), |
| 1738 | -GET_TD_SECONDS(self), |
| 1739 | -GET_TD_MICROSECONDS(self), |
| 1740 | 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | static PyObject * |
| 1744 | delta_positive(PyDateTime_Delta *self) |
| 1745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1746 | /* Could optimize this (by returning self) if this isn't a |
| 1747 | * subclass -- but who uses unary + ? Approximately nobody. |
| 1748 | */ |
| 1749 | return new_delta(GET_TD_DAYS(self), |
| 1750 | GET_TD_SECONDS(self), |
| 1751 | GET_TD_MICROSECONDS(self), |
| 1752 | 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | static PyObject * |
| 1756 | delta_abs(PyDateTime_Delta *self) |
| 1757 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | PyObject *result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1761 | assert(GET_TD_SECONDS(self) >= 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | if (GET_TD_DAYS(self) < 0) |
| 1764 | result = delta_negative(self); |
| 1765 | else |
| 1766 | result = delta_positive(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | static PyObject * |
| 1772 | delta_subtract(PyObject *left, PyObject *right) |
| 1773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1777 | /* delta - delta */ |
Alexander Belopolsky | b6f5ec7 | 2011-04-05 20:07:38 -0400 | [diff] [blame] | 1778 | /* The C-level additions can't overflow because of the |
| 1779 | * invariant bounds. |
| 1780 | */ |
| 1781 | int days = GET_TD_DAYS(left) - GET_TD_DAYS(right); |
| 1782 | int seconds = GET_TD_SECONDS(left) - GET_TD_SECONDS(right); |
| 1783 | int microseconds = GET_TD_MICROSECONDS(left) - |
| 1784 | GET_TD_MICROSECONDS(right); |
| 1785 | result = new_delta(days, seconds, microseconds, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | if (result == Py_NotImplemented) |
| 1789 | Py_INCREF(result); |
| 1790 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1793 | static int |
| 1794 | delta_cmp(PyObject *self, PyObject *other) |
| 1795 | { |
| 1796 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
| 1797 | if (diff == 0) { |
| 1798 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 1799 | if (diff == 0) |
| 1800 | diff = GET_TD_MICROSECONDS(self) - |
| 1801 | GET_TD_MICROSECONDS(other); |
| 1802 | } |
| 1803 | return diff; |
| 1804 | } |
| 1805 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1806 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1807 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | if (PyDelta_Check(other)) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 1810 | int diff = delta_cmp(self, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | return diff_to_bool(diff, op); |
| 1812 | } |
| 1813 | else { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1814 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1819 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1820 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1821 | delta_hash(PyDateTime_Delta *self) |
| 1822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | if (self->hashcode == -1) { |
| 1824 | PyObject *temp = delta_getstate(self); |
| 1825 | if (temp != NULL) { |
| 1826 | self->hashcode = PyObject_Hash(temp); |
| 1827 | Py_DECREF(temp); |
| 1828 | } |
| 1829 | } |
| 1830 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | static PyObject * |
| 1834 | delta_multiply(PyObject *left, PyObject *right) |
| 1835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | if (PyDelta_Check(left)) { |
| 1839 | /* delta * ??? */ |
| 1840 | if (PyLong_Check(right)) |
| 1841 | result = multiply_int_timedelta(right, |
| 1842 | (PyDateTime_Delta *) left); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1843 | else if (PyFloat_Check(right)) |
| 1844 | result = multiply_float_timedelta(right, |
| 1845 | (PyDateTime_Delta *) left); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | } |
| 1847 | else if (PyLong_Check(left)) |
| 1848 | result = multiply_int_timedelta(left, |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1849 | (PyDateTime_Delta *) right); |
| 1850 | else if (PyFloat_Check(left)) |
| 1851 | result = multiply_float_timedelta(left, |
| 1852 | (PyDateTime_Delta *) right); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | if (result == Py_NotImplemented) |
| 1855 | Py_INCREF(result); |
| 1856 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | static PyObject * |
| 1860 | delta_divide(PyObject *left, PyObject *right) |
| 1861 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | if (PyDelta_Check(left)) { |
| 1865 | /* delta * ??? */ |
| 1866 | if (PyLong_Check(right)) |
| 1867 | result = divide_timedelta_int( |
| 1868 | (PyDateTime_Delta *)left, |
| 1869 | right); |
| 1870 | else if (PyDelta_Check(right)) |
| 1871 | result = divide_timedelta_timedelta( |
| 1872 | (PyDateTime_Delta *)left, |
| 1873 | (PyDateTime_Delta *)right); |
| 1874 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | if (result == Py_NotImplemented) |
| 1877 | Py_INCREF(result); |
| 1878 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1881 | static PyObject * |
| 1882 | delta_truedivide(PyObject *left, PyObject *right) |
| 1883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1884 | PyObject *result = Py_NotImplemented; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | if (PyDelta_Check(left)) { |
| 1887 | if (PyDelta_Check(right)) |
| 1888 | result = truedivide_timedelta_timedelta( |
| 1889 | (PyDateTime_Delta *)left, |
| 1890 | (PyDateTime_Delta *)right); |
Alexander Belopolsky | 1790bc4 | 2010-05-31 17:33:47 +0000 | [diff] [blame] | 1891 | else if (PyFloat_Check(right)) |
| 1892 | result = truedivide_timedelta_float( |
| 1893 | (PyDateTime_Delta *)left, right); |
| 1894 | else if (PyLong_Check(right)) |
| 1895 | result = truedivide_timedelta_int( |
| 1896 | (PyDateTime_Delta *)left, right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | if (result == Py_NotImplemented) |
| 1900 | Py_INCREF(result); |
| 1901 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static PyObject * |
| 1905 | delta_remainder(PyObject *left, PyObject *right) |
| 1906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | PyObject *pyus_left; |
| 1908 | PyObject *pyus_right; |
| 1909 | PyObject *pyus_remainder; |
| 1910 | PyObject *remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1911 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1912 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1913 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1916 | if (pyus_left == NULL) |
| 1917 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1920 | if (pyus_right == NULL) { |
| 1921 | Py_DECREF(pyus_left); |
| 1922 | return NULL; |
| 1923 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | pyus_remainder = PyNumber_Remainder(pyus_left, pyus_right); |
| 1926 | Py_DECREF(pyus_left); |
| 1927 | Py_DECREF(pyus_right); |
| 1928 | if (pyus_remainder == NULL) |
| 1929 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1931 | remainder = microseconds_to_delta(pyus_remainder); |
| 1932 | Py_DECREF(pyus_remainder); |
| 1933 | if (remainder == NULL) |
| 1934 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | return remainder; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | static PyObject * |
| 1940 | delta_divmod(PyObject *left, PyObject *right) |
| 1941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1942 | PyObject *pyus_left; |
| 1943 | PyObject *pyus_right; |
| 1944 | PyObject *divmod; |
| 1945 | PyObject *delta; |
| 1946 | PyObject *result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1947 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1948 | if (!PyDelta_Check(left) || !PyDelta_Check(right)) |
| 1949 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | pyus_left = delta_to_microseconds((PyDateTime_Delta *)left); |
| 1952 | if (pyus_left == NULL) |
| 1953 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | pyus_right = delta_to_microseconds((PyDateTime_Delta *)right); |
| 1956 | if (pyus_right == NULL) { |
| 1957 | Py_DECREF(pyus_left); |
| 1958 | return NULL; |
| 1959 | } |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | divmod = PyNumber_Divmod(pyus_left, pyus_right); |
| 1962 | Py_DECREF(pyus_left); |
| 1963 | Py_DECREF(pyus_right); |
| 1964 | if (divmod == NULL) |
| 1965 | return NULL; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1967 | assert(PyTuple_Size(divmod) == 2); |
| 1968 | delta = microseconds_to_delta(PyTuple_GET_ITEM(divmod, 1)); |
| 1969 | if (delta == NULL) { |
| 1970 | Py_DECREF(divmod); |
| 1971 | return NULL; |
| 1972 | } |
| 1973 | result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta); |
| 1974 | Py_DECREF(delta); |
| 1975 | Py_DECREF(divmod); |
| 1976 | return result; |
Mark Dickinson | 7c186e2 | 2010-04-20 22:32:49 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1979 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 1980 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 1981 | * so far, and there are factor microseconds per current unit, the number |
| 1982 | * of which is given by num. num * factor is added to sofar in a |
| 1983 | * numerically careful way, and that's the result. Any fractional |
| 1984 | * microseconds left over (this can happen if num is a float type) are |
| 1985 | * added into *leftover. |
| 1986 | * Note that there are many ways this can give an error (NULL) return. |
| 1987 | */ |
| 1988 | static PyObject * |
| 1989 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 1990 | double *leftover) |
| 1991 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | PyObject *prod; |
| 1993 | PyObject *sum; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | assert(num != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | if (PyLong_Check(num)) { |
| 1998 | prod = PyNumber_Multiply(num, factor); |
| 1999 | if (prod == NULL) |
| 2000 | return NULL; |
| 2001 | sum = PyNumber_Add(sofar, prod); |
| 2002 | Py_DECREF(prod); |
| 2003 | return sum; |
| 2004 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | if (PyFloat_Check(num)) { |
| 2007 | double dnum; |
| 2008 | double fracpart; |
| 2009 | double intpart; |
| 2010 | PyObject *x; |
| 2011 | PyObject *y; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2013 | /* The Plan: decompose num into an integer part and a |
| 2014 | * fractional part, num = intpart + fracpart. |
| 2015 | * Then num * factor == |
| 2016 | * intpart * factor + fracpart * factor |
| 2017 | * and the LHS can be computed exactly in long arithmetic. |
| 2018 | * The RHS is again broken into an int part and frac part. |
| 2019 | * and the frac part is added into *leftover. |
| 2020 | */ |
| 2021 | dnum = PyFloat_AsDouble(num); |
| 2022 | if (dnum == -1.0 && PyErr_Occurred()) |
| 2023 | return NULL; |
| 2024 | fracpart = modf(dnum, &intpart); |
| 2025 | x = PyLong_FromDouble(intpart); |
| 2026 | if (x == NULL) |
| 2027 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | prod = PyNumber_Multiply(x, factor); |
| 2030 | Py_DECREF(x); |
| 2031 | if (prod == NULL) |
| 2032 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | sum = PyNumber_Add(sofar, prod); |
| 2035 | Py_DECREF(prod); |
| 2036 | if (sum == NULL) |
| 2037 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | if (fracpart == 0.0) |
| 2040 | return sum; |
| 2041 | /* So far we've lost no information. Dealing with the |
| 2042 | * fractional part requires float arithmetic, and may |
| 2043 | * lose a little info. |
| 2044 | */ |
| 2045 | assert(PyLong_Check(factor)); |
| 2046 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | dnum *= fracpart; |
| 2049 | fracpart = modf(dnum, &intpart); |
| 2050 | x = PyLong_FromDouble(intpart); |
| 2051 | if (x == NULL) { |
| 2052 | Py_DECREF(sum); |
| 2053 | return NULL; |
| 2054 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | y = PyNumber_Add(sum, x); |
| 2057 | Py_DECREF(sum); |
| 2058 | Py_DECREF(x); |
| 2059 | *leftover += fracpart; |
| 2060 | return y; |
| 2061 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | PyErr_Format(PyExc_TypeError, |
| 2064 | "unsupported type for timedelta %s component: %s", |
| 2065 | tag, Py_TYPE(num)->tp_name); |
| 2066 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | static PyObject * |
| 2070 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2071 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2072 | PyObject *self = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | /* Argument objects. */ |
| 2075 | PyObject *day = NULL; |
| 2076 | PyObject *second = NULL; |
| 2077 | PyObject *us = NULL; |
| 2078 | PyObject *ms = NULL; |
| 2079 | PyObject *minute = NULL; |
| 2080 | PyObject *hour = NULL; |
| 2081 | PyObject *week = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | PyObject *x = NULL; /* running sum of microseconds */ |
| 2084 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 2085 | double leftover_us = 0.0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | static char *keywords[] = { |
| 2088 | "days", "seconds", "microseconds", "milliseconds", |
| 2089 | "minutes", "hours", "weeks", NULL |
| 2090 | }; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 2093 | keywords, |
| 2094 | &day, &second, &us, |
| 2095 | &ms, &minute, &hour, &week) == 0) |
| 2096 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | x = PyLong_FromLong(0); |
| 2099 | if (x == NULL) |
| 2100 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | #define CLEANUP \ |
| 2103 | Py_DECREF(x); \ |
| 2104 | x = y; \ |
| 2105 | if (x == NULL) \ |
| 2106 | goto Done |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | if (us) { |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2109 | y = accum("microseconds", x, us, one, &leftover_us); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 | CLEANUP; |
| 2111 | } |
| 2112 | if (ms) { |
| 2113 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 2114 | CLEANUP; |
| 2115 | } |
| 2116 | if (second) { |
| 2117 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 2118 | CLEANUP; |
| 2119 | } |
| 2120 | if (minute) { |
| 2121 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 2122 | CLEANUP; |
| 2123 | } |
| 2124 | if (hour) { |
| 2125 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 2126 | CLEANUP; |
| 2127 | } |
| 2128 | if (day) { |
| 2129 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 2130 | CLEANUP; |
| 2131 | } |
| 2132 | if (week) { |
| 2133 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 2134 | CLEANUP; |
| 2135 | } |
| 2136 | if (leftover_us) { |
| 2137 | /* Round to nearest whole # of us, and add into x. */ |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2138 | double whole_us = round(leftover_us); |
| 2139 | int x_is_odd; |
| 2140 | PyObject *temp; |
| 2141 | |
| 2142 | whole_us = round(leftover_us); |
| 2143 | if (fabs(whole_us - leftover_us) == 0.5) { |
| 2144 | /* We're exactly halfway between two integers. In order |
| 2145 | * to do round-half-to-even, we must determine whether x |
| 2146 | * is odd. Note that x is odd when it's last bit is 1. The |
| 2147 | * code below uses bitwise and operation to check the last |
| 2148 | * bit. */ |
| 2149 | temp = PyNumber_And(x, one); /* temp <- x & 1 */ |
| 2150 | if (temp == NULL) { |
| 2151 | Py_DECREF(x); |
| 2152 | goto Done; |
| 2153 | } |
| 2154 | x_is_odd = PyObject_IsTrue(temp); |
| 2155 | Py_DECREF(temp); |
| 2156 | if (x_is_odd == -1) { |
| 2157 | Py_DECREF(x); |
| 2158 | goto Done; |
| 2159 | } |
| 2160 | whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; |
| 2161 | } |
| 2162 | |
Victor Stinner | 36a5a06 | 2013-08-28 01:53:39 +0200 | [diff] [blame] | 2163 | temp = PyLong_FromLong((long)whole_us); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 2164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | if (temp == NULL) { |
| 2166 | Py_DECREF(x); |
| 2167 | goto Done; |
| 2168 | } |
| 2169 | y = PyNumber_Add(x, temp); |
| 2170 | Py_DECREF(temp); |
| 2171 | CLEANUP; |
| 2172 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | self = microseconds_to_delta_ex(x, type); |
| 2175 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2176 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2178 | |
| 2179 | #undef CLEANUP |
| 2180 | } |
| 2181 | |
| 2182 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2183 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | return (GET_TD_DAYS(self) != 0 |
| 2186 | || GET_TD_SECONDS(self) != 0 |
| 2187 | || GET_TD_MICROSECONDS(self) != 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | static PyObject * |
| 2191 | delta_repr(PyDateTime_Delta *self) |
| 2192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2193 | if (GET_TD_MICROSECONDS(self) != 0) |
| 2194 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2195 | Py_TYPE(self)->tp_name, |
| 2196 | GET_TD_DAYS(self), |
| 2197 | GET_TD_SECONDS(self), |
| 2198 | GET_TD_MICROSECONDS(self)); |
| 2199 | if (GET_TD_SECONDS(self) != 0) |
| 2200 | return PyUnicode_FromFormat("%s(%d, %d)", |
| 2201 | Py_TYPE(self)->tp_name, |
| 2202 | GET_TD_DAYS(self), |
| 2203 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | return PyUnicode_FromFormat("%s(%d)", |
| 2206 | Py_TYPE(self)->tp_name, |
| 2207 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | static PyObject * |
| 2211 | delta_str(PyDateTime_Delta *self) |
| 2212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | int us = GET_TD_MICROSECONDS(self); |
| 2214 | int seconds = GET_TD_SECONDS(self); |
| 2215 | int minutes = divmod(seconds, 60, &seconds); |
| 2216 | int hours = divmod(minutes, 60, &minutes); |
| 2217 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | if (days) { |
| 2220 | if (us) |
| 2221 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2222 | days, (days == 1 || days == -1) ? "" : "s", |
| 2223 | hours, minutes, seconds, us); |
| 2224 | else |
| 2225 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2226 | days, (days == 1 || days == -1) ? "" : "s", |
| 2227 | hours, minutes, seconds); |
| 2228 | } else { |
| 2229 | if (us) |
| 2230 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2231 | hours, minutes, seconds, us); |
| 2232 | else |
| 2233 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2234 | hours, minutes, seconds); |
| 2235 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2236 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2239 | /* Pickle support, a simple use of __reduce__. */ |
| 2240 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2241 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2242 | static PyObject * |
| 2243 | delta_getstate(PyDateTime_Delta *self) |
| 2244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2246 | GET_TD_SECONDS(self), |
| 2247 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2250 | static PyObject * |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2251 | delta_total_seconds(PyObject *self) |
| 2252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | PyObject *total_seconds; |
| 2254 | PyObject *total_microseconds; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | total_microseconds = delta_to_microseconds((PyDateTime_Delta *)self); |
| 2257 | if (total_microseconds == NULL) |
| 2258 | return NULL; |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2259 | |
Alexander Belopolsky | df7027b | 2013-08-04 15:18:58 -0400 | [diff] [blame] | 2260 | total_seconds = PyNumber_TrueDivide(total_microseconds, us_per_second); |
Mark Dickinson | 0381e3f | 2010-05-08 14:35:02 +0000 | [diff] [blame] | 2261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | Py_DECREF(total_microseconds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | return total_seconds; |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2267 | delta_reduce(PyDateTime_Delta* self) |
| 2268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2273 | |
| 2274 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | {"days", T_INT, OFFSET(days), READONLY, |
| 2277 | PyDoc_STR("Number of days.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2279 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
| 2280 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
| 2283 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2284 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2285 | }; |
| 2286 | |
| 2287 | static PyMethodDef delta_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | {"total_seconds", (PyCFunction)delta_total_seconds, METH_NOARGS, |
| 2289 | PyDoc_STR("Total seconds in the duration.")}, |
Antoine Pitrou | be6859d | 2009-11-25 23:02:32 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2292 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2294 | {NULL, NULL}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2295 | }; |
| 2296 | |
| 2297 | static char delta_doc[] = |
| 2298 | PyDoc_STR("Difference between two datetime values."); |
| 2299 | |
| 2300 | static PyNumberMethods delta_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | delta_add, /* nb_add */ |
| 2302 | delta_subtract, /* nb_subtract */ |
| 2303 | delta_multiply, /* nb_multiply */ |
| 2304 | delta_remainder, /* nb_remainder */ |
| 2305 | delta_divmod, /* nb_divmod */ |
| 2306 | 0, /* nb_power */ |
| 2307 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2308 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2309 | (unaryfunc)delta_abs, /* nb_absolute */ |
| 2310 | (inquiry)delta_bool, /* nb_bool */ |
| 2311 | 0, /*nb_invert*/ |
| 2312 | 0, /*nb_lshift*/ |
| 2313 | 0, /*nb_rshift*/ |
| 2314 | 0, /*nb_and*/ |
| 2315 | 0, /*nb_xor*/ |
| 2316 | 0, /*nb_or*/ |
| 2317 | 0, /*nb_int*/ |
| 2318 | 0, /*nb_reserved*/ |
| 2319 | 0, /*nb_float*/ |
| 2320 | 0, /*nb_inplace_add*/ |
| 2321 | 0, /*nb_inplace_subtract*/ |
| 2322 | 0, /*nb_inplace_multiply*/ |
| 2323 | 0, /*nb_inplace_remainder*/ |
| 2324 | 0, /*nb_inplace_power*/ |
| 2325 | 0, /*nb_inplace_lshift*/ |
| 2326 | 0, /*nb_inplace_rshift*/ |
| 2327 | 0, /*nb_inplace_and*/ |
| 2328 | 0, /*nb_inplace_xor*/ |
| 2329 | 0, /*nb_inplace_or*/ |
| 2330 | delta_divide, /* nb_floor_divide */ |
| 2331 | delta_truedivide, /* nb_true_divide */ |
| 2332 | 0, /* nb_inplace_floor_divide */ |
| 2333 | 0, /* nb_inplace_true_divide */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2334 | }; |
| 2335 | |
| 2336 | static PyTypeObject PyDateTime_DeltaType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2338 | "datetime.timedelta", /* tp_name */ |
| 2339 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2340 | 0, /* tp_itemsize */ |
| 2341 | 0, /* tp_dealloc */ |
| 2342 | 0, /* tp_print */ |
| 2343 | 0, /* tp_getattr */ |
| 2344 | 0, /* tp_setattr */ |
| 2345 | 0, /* tp_reserved */ |
| 2346 | (reprfunc)delta_repr, /* tp_repr */ |
| 2347 | &delta_as_number, /* tp_as_number */ |
| 2348 | 0, /* tp_as_sequence */ |
| 2349 | 0, /* tp_as_mapping */ |
| 2350 | (hashfunc)delta_hash, /* tp_hash */ |
| 2351 | 0, /* tp_call */ |
| 2352 | (reprfunc)delta_str, /* tp_str */ |
| 2353 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2354 | 0, /* tp_setattro */ |
| 2355 | 0, /* tp_as_buffer */ |
| 2356 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2357 | delta_doc, /* tp_doc */ |
| 2358 | 0, /* tp_traverse */ |
| 2359 | 0, /* tp_clear */ |
| 2360 | delta_richcompare, /* tp_richcompare */ |
| 2361 | 0, /* tp_weaklistoffset */ |
| 2362 | 0, /* tp_iter */ |
| 2363 | 0, /* tp_iternext */ |
| 2364 | delta_methods, /* tp_methods */ |
| 2365 | delta_members, /* tp_members */ |
| 2366 | 0, /* tp_getset */ |
| 2367 | 0, /* tp_base */ |
| 2368 | 0, /* tp_dict */ |
| 2369 | 0, /* tp_descr_get */ |
| 2370 | 0, /* tp_descr_set */ |
| 2371 | 0, /* tp_dictoffset */ |
| 2372 | 0, /* tp_init */ |
| 2373 | 0, /* tp_alloc */ |
| 2374 | delta_new, /* tp_new */ |
| 2375 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2376 | }; |
| 2377 | |
| 2378 | /* |
| 2379 | * PyDateTime_Date implementation. |
| 2380 | */ |
| 2381 | |
| 2382 | /* Accessor properties. */ |
| 2383 | |
| 2384 | static PyObject * |
| 2385 | date_year(PyDateTime_Date *self, void *unused) |
| 2386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | static PyObject * |
| 2391 | date_month(PyDateTime_Date *self, void *unused) |
| 2392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | static PyObject * |
| 2397 | date_day(PyDateTime_Date *self, void *unused) |
| 2398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | static PyGetSetDef date_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | {"year", (getter)date_year}, |
| 2404 | {"month", (getter)date_month}, |
| 2405 | {"day", (getter)date_day}, |
| 2406 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2407 | }; |
| 2408 | |
| 2409 | /* Constructors. */ |
| 2410 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2411 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2412 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2413 | static PyObject * |
| 2414 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2416 | PyObject *self = NULL; |
| 2417 | PyObject *state; |
| 2418 | int year; |
| 2419 | int month; |
| 2420 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | /* Check for invocation from pickle with __getstate__ state */ |
| 2423 | if (PyTuple_GET_SIZE(args) == 1 && |
| 2424 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2425 | PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2426 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 2427 | { |
| 2428 | PyDateTime_Date *me; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2430 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
| 2431 | if (me != NULL) { |
| 2432 | char *pdata = PyBytes_AS_STRING(state); |
| 2433 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2434 | me->hashcode = -1; |
| 2435 | } |
| 2436 | return (PyObject *)me; |
| 2437 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
| 2440 | &year, &month, &day)) { |
| 2441 | if (check_date_args(year, month, day) < 0) |
| 2442 | return NULL; |
| 2443 | self = new_date_ex(year, month, day, type); |
| 2444 | } |
| 2445 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | /* Return new date from localtime(t). */ |
| 2449 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2450 | date_local_from_object(PyObject *cls, PyObject *obj) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | struct tm *tm; |
| 2453 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2454 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2455 | if (_PyTime_ObjectToTime_t(obj, &t) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2456 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | tm = localtime(&t); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 2459 | if (tm == NULL) { |
| 2460 | /* unconvertible time */ |
| 2461 | #ifdef EINVAL |
| 2462 | if (errno == 0) |
| 2463 | errno = EINVAL; |
| 2464 | #endif |
| 2465 | PyErr_SetFromErrno(PyExc_OSError); |
| 2466 | return NULL; |
| 2467 | } |
| 2468 | |
| 2469 | return PyObject_CallFunction(cls, "iii", |
| 2470 | tm->tm_year + 1900, |
| 2471 | tm->tm_mon + 1, |
| 2472 | tm->tm_mday); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | /* Return new date from current time. |
| 2476 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2477 | * only way to be sure of that is to *call* time.time(). That's not |
| 2478 | * generally the same as calling C's time. |
| 2479 | */ |
| 2480 | static PyObject * |
| 2481 | date_today(PyObject *cls, PyObject *dummy) |
| 2482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2483 | PyObject *time; |
| 2484 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2485 | _Py_IDENTIFIER(fromtimestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | time = time_time(); |
| 2488 | if (time == NULL) |
| 2489 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2491 | /* Note well: today() is a class method, so this may not call |
| 2492 | * date.fromtimestamp. For example, it may call |
| 2493 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2494 | * time.time() delivers; if someone were gonzo about optimization, |
| 2495 | * date.today() could get away with plain C time(). |
| 2496 | */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2497 | result = _PyObject_CallMethodId(cls, &PyId_fromtimestamp, "O", time); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | Py_DECREF(time); |
| 2499 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2503 | static PyObject * |
| 2504 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2505 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2506 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2508 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2509 | if (PyArg_ParseTuple(args, "O:fromtimestamp", ×tamp)) |
| 2510 | result = date_local_from_object(cls, timestamp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2515 | * the ordinal is out of range. |
| 2516 | */ |
| 2517 | static PyObject * |
| 2518 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2520 | PyObject *result = NULL; |
| 2521 | int ordinal; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2524 | int year; |
| 2525 | int month; |
| 2526 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | if (ordinal < 1) |
| 2529 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2530 | ">= 1"); |
| 2531 | else { |
| 2532 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2533 | result = PyObject_CallFunction(cls, "iii", |
| 2534 | year, month, day); |
| 2535 | } |
| 2536 | } |
| 2537 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | /* |
| 2541 | * Date arithmetic. |
| 2542 | */ |
| 2543 | |
| 2544 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2545 | * instead. |
| 2546 | */ |
| 2547 | static PyObject * |
| 2548 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | PyObject *result = NULL; |
| 2551 | int year = GET_YEAR(date); |
| 2552 | int month = GET_MONTH(date); |
| 2553 | int deltadays = GET_TD_DAYS(delta); |
| 2554 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2555 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 | if (normalize_date(&year, &month, &day) >= 0) |
| 2558 | result = new_date(year, month, day); |
| 2559 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | static PyObject * |
| 2563 | date_add(PyObject *left, PyObject *right) |
| 2564 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2565 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2566 | Py_RETURN_NOTIMPLEMENTED; |
| 2567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2568 | if (PyDate_Check(left)) { |
| 2569 | /* date + ??? */ |
| 2570 | if (PyDelta_Check(right)) |
| 2571 | /* date + delta */ |
| 2572 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2573 | (PyDateTime_Delta *) right, |
| 2574 | 0); |
| 2575 | } |
| 2576 | else { |
| 2577 | /* ??? + date |
| 2578 | * 'right' must be one of us, or we wouldn't have been called |
| 2579 | */ |
| 2580 | if (PyDelta_Check(left)) |
| 2581 | /* delta + date */ |
| 2582 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2583 | (PyDateTime_Delta *) left, |
| 2584 | 0); |
| 2585 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2586 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | static PyObject * |
| 2590 | date_subtract(PyObject *left, PyObject *right) |
| 2591 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2592 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) |
| 2593 | Py_RETURN_NOTIMPLEMENTED; |
| 2594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | if (PyDate_Check(left)) { |
| 2596 | if (PyDate_Check(right)) { |
| 2597 | /* date - date */ |
| 2598 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2599 | GET_MONTH(left), |
| 2600 | GET_DAY(left)); |
| 2601 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2602 | GET_MONTH(right), |
| 2603 | GET_DAY(right)); |
| 2604 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2605 | } |
| 2606 | if (PyDelta_Check(right)) { |
| 2607 | /* date - delta */ |
| 2608 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2609 | (PyDateTime_Delta *) right, |
| 2610 | 1); |
| 2611 | } |
| 2612 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2613 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | |
| 2617 | /* Various ways to turn a date into a string. */ |
| 2618 | |
| 2619 | static PyObject * |
| 2620 | date_repr(PyDateTime_Date *self) |
| 2621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 2623 | Py_TYPE(self)->tp_name, |
| 2624 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | static PyObject * |
| 2628 | date_isoformat(PyDateTime_Date *self) |
| 2629 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2630 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2631 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2634 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2635 | static PyObject * |
| 2636 | date_str(PyDateTime_Date *self) |
| 2637 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2638 | _Py_IDENTIFIER(isoformat); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2639 | |
| 2640 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | |
| 2644 | static PyObject * |
| 2645 | date_ctime(PyDateTime_Date *self) |
| 2646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | return format_ctime(self, 0, 0, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | static PyObject * |
| 2651 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | /* This method can be inherited, and needs to call the |
| 2654 | * timetuple() method appropriate to self's class. |
| 2655 | */ |
| 2656 | PyObject *result; |
| 2657 | PyObject *tuple; |
| 2658 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2659 | _Py_IDENTIFIER(timetuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 2663 | &format)) |
| 2664 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2665 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2666 | tuple = _PyObject_CallMethodId((PyObject *)self, &PyId_timetuple, "()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | if (tuple == NULL) |
| 2668 | return NULL; |
| 2669 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2670 | (PyObject *)self); |
| 2671 | Py_DECREF(tuple); |
| 2672 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2675 | static PyObject * |
| 2676 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2677 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2678 | PyObject *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2679 | _Py_IDENTIFIER(strftime); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2682 | return NULL; |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | /* if the format is zero length, return str(self) */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 2685 | if (PyUnicode_GetLength(format) == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2687 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2688 | return _PyObject_CallMethodId((PyObject *)self, &PyId_strftime, "O", format); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2691 | /* ISO methods. */ |
| 2692 | |
| 2693 | static PyObject * |
| 2694 | date_isoweekday(PyDateTime_Date *self) |
| 2695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | static PyObject * |
| 2702 | date_isocalendar(PyDateTime_Date *self) |
| 2703 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2704 | int year = GET_YEAR(self); |
| 2705 | int week1_monday = iso_week1_monday(year); |
| 2706 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2707 | int week; |
| 2708 | int day; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2710 | week = divmod(today - week1_monday, 7, &day); |
| 2711 | if (week < 0) { |
| 2712 | --year; |
| 2713 | week1_monday = iso_week1_monday(year); |
| 2714 | week = divmod(today - week1_monday, 7, &day); |
| 2715 | } |
| 2716 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2717 | ++year; |
| 2718 | week = 0; |
| 2719 | } |
| 2720 | return Py_BuildValue("iii", year, week + 1, day + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /* Miscellaneous methods. */ |
| 2724 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2725 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2726 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | if (PyDate_Check(other)) { |
| 2729 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2730 | ((PyDateTime_Date *)other)->data, |
| 2731 | _PyDateTime_DATE_DATASIZE); |
| 2732 | return diff_to_bool(diff, op); |
| 2733 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 2734 | else |
| 2735 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | static PyObject * |
| 2739 | date_timetuple(PyDateTime_Date *self) |
| 2740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | return build_struct_time(GET_YEAR(self), |
| 2742 | GET_MONTH(self), |
| 2743 | GET_DAY(self), |
| 2744 | 0, 0, 0, -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2747 | static PyObject * |
| 2748 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | PyObject *clone; |
| 2751 | PyObject *tuple; |
| 2752 | int year = GET_YEAR(self); |
| 2753 | int month = GET_MONTH(self); |
| 2754 | int day = GET_DAY(self); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2757 | &year, &month, &day)) |
| 2758 | return NULL; |
| 2759 | tuple = Py_BuildValue("iii", year, month, day); |
| 2760 | if (tuple == NULL) |
| 2761 | return NULL; |
| 2762 | clone = date_new(Py_TYPE(self), tuple, NULL); |
| 2763 | Py_DECREF(tuple); |
| 2764 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2767 | static Py_hash_t |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2768 | generic_hash(unsigned char *data, int len) |
| 2769 | { |
Gregory P. Smith | 5831bd2 | 2012-01-14 14:31:13 -0800 | [diff] [blame] | 2770 | return _Py_HashBytes(data, len); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | |
| 2774 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2775 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2776 | static Py_hash_t |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2777 | date_hash(PyDateTime_Date *self) |
| 2778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | if (self->hashcode == -1) |
| 2780 | self->hashcode = generic_hash( |
| 2781 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | return self->hashcode; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | static PyObject * |
| 2787 | date_toordinal(PyDateTime_Date *self) |
| 2788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
| 2790 | GET_DAY(self))); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | static PyObject * |
| 2794 | date_weekday(PyDateTime_Date *self) |
| 2795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2801 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2802 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2803 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2804 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2805 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 | PyObject* field; |
| 2808 | field = PyBytes_FromStringAndSize((char*)self->data, |
| 2809 | _PyDateTime_DATE_DATASIZE); |
| 2810 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2814 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | static PyMethodDef date_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 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2824 | METH_CLASS, |
| 2825 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2826 | "time.time()).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2829 | METH_CLASS, |
| 2830 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2831 | "ordinal.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2834 | PyDoc_STR("Current date or datetime: same as " |
| 2835 | "self.__class__.fromtimestamp(time.time()).")}, |
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 | /* Instance methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2840 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
| 2843 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2846 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2849 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2852 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2853 | "weekday.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2856 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2859 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2860 | "Monday == 1 ... Sunday == 7")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2863 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2864 | "1 is day 1.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2867 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2868 | "Monday == 0 ... Sunday == 6")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
| 2871 | PyDoc_STR("Return date with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2874 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2877 | }; |
| 2878 | |
| 2879 | static char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2880 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2881 | |
| 2882 | static PyNumberMethods date_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | date_add, /* nb_add */ |
| 2884 | date_subtract, /* nb_subtract */ |
| 2885 | 0, /* nb_multiply */ |
| 2886 | 0, /* nb_remainder */ |
| 2887 | 0, /* nb_divmod */ |
| 2888 | 0, /* nb_power */ |
| 2889 | 0, /* nb_negative */ |
| 2890 | 0, /* nb_positive */ |
| 2891 | 0, /* nb_absolute */ |
| 2892 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2893 | }; |
| 2894 | |
| 2895 | static PyTypeObject PyDateTime_DateType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2896 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2897 | "datetime.date", /* tp_name */ |
| 2898 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2899 | 0, /* tp_itemsize */ |
| 2900 | 0, /* tp_dealloc */ |
| 2901 | 0, /* tp_print */ |
| 2902 | 0, /* tp_getattr */ |
| 2903 | 0, /* tp_setattr */ |
| 2904 | 0, /* tp_reserved */ |
| 2905 | (reprfunc)date_repr, /* tp_repr */ |
| 2906 | &date_as_number, /* tp_as_number */ |
| 2907 | 0, /* tp_as_sequence */ |
| 2908 | 0, /* tp_as_mapping */ |
| 2909 | (hashfunc)date_hash, /* tp_hash */ |
| 2910 | 0, /* tp_call */ |
| 2911 | (reprfunc)date_str, /* tp_str */ |
| 2912 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2913 | 0, /* tp_setattro */ |
| 2914 | 0, /* tp_as_buffer */ |
| 2915 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2916 | date_doc, /* tp_doc */ |
| 2917 | 0, /* tp_traverse */ |
| 2918 | 0, /* tp_clear */ |
| 2919 | date_richcompare, /* tp_richcompare */ |
| 2920 | 0, /* tp_weaklistoffset */ |
| 2921 | 0, /* tp_iter */ |
| 2922 | 0, /* tp_iternext */ |
| 2923 | date_methods, /* tp_methods */ |
| 2924 | 0, /* tp_members */ |
| 2925 | date_getset, /* tp_getset */ |
| 2926 | 0, /* tp_base */ |
| 2927 | 0, /* tp_dict */ |
| 2928 | 0, /* tp_descr_get */ |
| 2929 | 0, /* tp_descr_set */ |
| 2930 | 0, /* tp_dictoffset */ |
| 2931 | 0, /* tp_init */ |
| 2932 | 0, /* tp_alloc */ |
| 2933 | date_new, /* tp_new */ |
| 2934 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2935 | }; |
| 2936 | |
| 2937 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2938 | * PyDateTime_TZInfo implementation. |
| 2939 | */ |
| 2940 | |
| 2941 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 2942 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 2943 | * 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] | 2944 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2945 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 2946 | * |
| 2947 | * Note: For reasons having to do with pickling of subclasses, we have |
| 2948 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 2949 | * in the Python implementation (__init__() could raise NotImplementedError |
| 2950 | * there without ill effect), but doing so in the C implementation hit a |
| 2951 | * brick wall. |
| 2952 | */ |
| 2953 | |
| 2954 | static PyObject * |
| 2955 | tzinfo_nogo(const char* methodname) |
| 2956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | PyErr_Format(PyExc_NotImplementedError, |
| 2958 | "a tzinfo subclass must implement %s()", |
| 2959 | methodname); |
| 2960 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /* Methods. A subclass must implement these. */ |
| 2964 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2965 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2966 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 2967 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2968 | return tzinfo_nogo("tzname"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2971 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2972 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 2973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | return tzinfo_nogo("utcoffset"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2977 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2978 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 2979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | return tzinfo_nogo("dst"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2983 | |
| 2984 | static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, |
| 2985 | PyDateTime_Delta *delta, |
| 2986 | int factor); |
| 2987 | static PyObject *datetime_utcoffset(PyObject *self, PyObject *); |
| 2988 | static PyObject *datetime_dst(PyObject *self, PyObject *); |
| 2989 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2990 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2991 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2992 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2993 | PyObject *result = NULL; |
| 2994 | PyObject *off = NULL, *dst = NULL; |
| 2995 | PyDateTime_Delta *delta = NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2996 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 2997 | if (!PyDateTime_Check(dt)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2998 | PyErr_SetString(PyExc_TypeError, |
| 2999 | "fromutc: argument must be a datetime"); |
| 3000 | return NULL; |
| 3001 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3002 | if (GET_DT_TZINFO(dt) != (PyObject *)self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3003 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3004 | "is not self"); |
| 3005 | return NULL; |
| 3006 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3007 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3008 | off = datetime_utcoffset(dt, NULL); |
| 3009 | if (off == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3011 | if (off == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3012 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3013 | "utcoffset() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3014 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3015 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3016 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3017 | dst = datetime_dst(dt, NULL); |
| 3018 | if (dst == NULL) |
| 3019 | goto Fail; |
| 3020 | if (dst == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 3022 | "dst() result required"); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3023 | goto Fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | } |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3025 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3026 | delta = (PyDateTime_Delta *)delta_subtract(off, dst); |
| 3027 | if (delta == NULL) |
| 3028 | goto Fail; |
| 3029 | result = add_datetime_timedelta((PyDateTime_DateTime *)dt, delta, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3030 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | goto Fail; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3032 | |
| 3033 | Py_DECREF(dst); |
| 3034 | dst = call_dst(GET_DT_TZINFO(dt), result); |
| 3035 | if (dst == NULL) |
| 3036 | goto Fail; |
| 3037 | if (dst == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | goto Inconsistent; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3039 | if (delta_bool(delta) != 0) { |
| 3040 | PyObject *temp = result; |
| 3041 | result = add_datetime_timedelta((PyDateTime_DateTime *)result, |
| 3042 | (PyDateTime_Delta *)dst, 1); |
| 3043 | Py_DECREF(temp); |
| 3044 | if (result == NULL) |
| 3045 | goto Fail; |
| 3046 | } |
| 3047 | Py_DECREF(delta); |
| 3048 | Py_DECREF(dst); |
| 3049 | Py_DECREF(off); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 | return result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3051 | |
| 3052 | Inconsistent: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3053 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 3054 | "inconsistent results; cannot convert"); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | /* fall thru to failure */ |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3057 | Fail: |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3058 | Py_XDECREF(off); |
| 3059 | Py_XDECREF(dst); |
| 3060 | Py_XDECREF(delta); |
| 3061 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3065 | /* |
| 3066 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3067 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3068 | */ |
| 3069 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3070 | static PyObject * |
| 3071 | tzinfo_reduce(PyObject *self) |
| 3072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | PyObject *args, *state, *tmp; |
| 3074 | PyObject *getinitargs, *getstate; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3075 | _Py_IDENTIFIER(__getinitargs__); |
| 3076 | _Py_IDENTIFIER(__getstate__); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | tmp = PyTuple_New(0); |
| 3079 | if (tmp == NULL) |
| 3080 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3081 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3082 | getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | if (getinitargs != NULL) { |
| 3084 | args = PyObject_CallObject(getinitargs, tmp); |
| 3085 | Py_DECREF(getinitargs); |
| 3086 | if (args == NULL) { |
| 3087 | Py_DECREF(tmp); |
| 3088 | return NULL; |
| 3089 | } |
| 3090 | } |
| 3091 | else { |
| 3092 | PyErr_Clear(); |
| 3093 | args = tmp; |
| 3094 | Py_INCREF(args); |
| 3095 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3096 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 3097 | getstate = _PyObject_GetAttrId(self, &PyId___getstate__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | if (getstate != NULL) { |
| 3099 | state = PyObject_CallObject(getstate, tmp); |
| 3100 | Py_DECREF(getstate); |
| 3101 | if (state == NULL) { |
| 3102 | Py_DECREF(args); |
| 3103 | Py_DECREF(tmp); |
| 3104 | return NULL; |
| 3105 | } |
| 3106 | } |
| 3107 | else { |
| 3108 | PyObject **dictptr; |
| 3109 | PyErr_Clear(); |
| 3110 | state = Py_None; |
| 3111 | dictptr = _PyObject_GetDictPtr(self); |
| 3112 | if (dictptr && *dictptr && PyDict_Size(*dictptr)) |
| 3113 | state = *dictptr; |
| 3114 | Py_INCREF(state); |
| 3115 | } |
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 | Py_DECREF(tmp); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3119 | if (state == Py_None) { |
| 3120 | Py_DECREF(state); |
| 3121 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
| 3122 | } |
| 3123 | else |
| 3124 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3125 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3126 | |
| 3127 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 3130 | PyDoc_STR("datetime -> string name of time zone.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
Sean Reifscheider | deda8cb | 2010-06-04 01:51:38 +0000 | [diff] [blame] | 3133 | PyDoc_STR("datetime -> timedelta showing offset from UTC, negative " |
| 3134 | "values indicating West of UTC")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 3137 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
Alexander Belopolsky | 2f194b9 | 2010-07-03 03:35:27 +0000 | [diff] [blame] | 3140 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 3141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 3143 | PyDoc_STR("-> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3146 | }; |
| 3147 | |
| 3148 | static char tzinfo_doc[] = |
| 3149 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 3150 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3151 | static PyTypeObject PyDateTime_TZInfoType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3153 | "datetime.tzinfo", /* tp_name */ |
| 3154 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 3155 | 0, /* tp_itemsize */ |
| 3156 | 0, /* tp_dealloc */ |
| 3157 | 0, /* tp_print */ |
| 3158 | 0, /* tp_getattr */ |
| 3159 | 0, /* tp_setattr */ |
| 3160 | 0, /* tp_reserved */ |
| 3161 | 0, /* tp_repr */ |
| 3162 | 0, /* tp_as_number */ |
| 3163 | 0, /* tp_as_sequence */ |
| 3164 | 0, /* tp_as_mapping */ |
| 3165 | 0, /* tp_hash */ |
| 3166 | 0, /* tp_call */ |
| 3167 | 0, /* tp_str */ |
| 3168 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3169 | 0, /* tp_setattro */ |
| 3170 | 0, /* tp_as_buffer */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3171 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | tzinfo_doc, /* tp_doc */ |
| 3173 | 0, /* tp_traverse */ |
| 3174 | 0, /* tp_clear */ |
| 3175 | 0, /* tp_richcompare */ |
| 3176 | 0, /* tp_weaklistoffset */ |
| 3177 | 0, /* tp_iter */ |
| 3178 | 0, /* tp_iternext */ |
| 3179 | tzinfo_methods, /* tp_methods */ |
| 3180 | 0, /* tp_members */ |
| 3181 | 0, /* tp_getset */ |
| 3182 | 0, /* tp_base */ |
| 3183 | 0, /* tp_dict */ |
| 3184 | 0, /* tp_descr_get */ |
| 3185 | 0, /* tp_descr_set */ |
| 3186 | 0, /* tp_dictoffset */ |
| 3187 | 0, /* tp_init */ |
| 3188 | 0, /* tp_alloc */ |
| 3189 | PyType_GenericNew, /* tp_new */ |
| 3190 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3191 | }; |
| 3192 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3193 | static char *timezone_kws[] = {"offset", "name", NULL}; |
| 3194 | |
| 3195 | static PyObject * |
| 3196 | timezone_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 3197 | { |
| 3198 | PyObject *offset; |
| 3199 | PyObject *name = NULL; |
| 3200 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!|O!:timezone", timezone_kws, |
| 3201 | &PyDateTime_DeltaType, &offset, |
| 3202 | &PyUnicode_Type, &name)) |
| 3203 | return new_timezone(offset, name); |
| 3204 | |
| 3205 | return NULL; |
| 3206 | } |
| 3207 | |
| 3208 | static void |
| 3209 | timezone_dealloc(PyDateTime_TimeZone *self) |
| 3210 | { |
| 3211 | Py_CLEAR(self->offset); |
| 3212 | Py_CLEAR(self->name); |
| 3213 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3214 | } |
| 3215 | |
| 3216 | static PyObject * |
| 3217 | timezone_richcompare(PyDateTime_TimeZone *self, |
| 3218 | PyDateTime_TimeZone *other, int op) |
| 3219 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3220 | if (op != Py_EQ && op != Py_NE) |
| 3221 | Py_RETURN_NOTIMPLEMENTED; |
Georg Brandl | 0085a24 | 2012-09-22 09:23:12 +0200 | [diff] [blame] | 3222 | if (Py_TYPE(other) != &PyDateTime_TimeZoneType) { |
| 3223 | if (op == Py_EQ) |
| 3224 | Py_RETURN_FALSE; |
| 3225 | else |
| 3226 | Py_RETURN_TRUE; |
| 3227 | } |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3228 | return delta_richcompare(self->offset, other->offset, op); |
| 3229 | } |
| 3230 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3231 | static Py_hash_t |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3232 | timezone_hash(PyDateTime_TimeZone *self) |
| 3233 | { |
| 3234 | return delta_hash((PyDateTime_Delta *)self->offset); |
| 3235 | } |
| 3236 | |
| 3237 | /* Check argument type passed to tzname, utcoffset, or dst methods. |
| 3238 | Returns 0 for good argument. Returns -1 and sets exception info |
| 3239 | otherwise. |
| 3240 | */ |
| 3241 | static int |
| 3242 | _timezone_check_argument(PyObject *dt, const char *meth) |
| 3243 | { |
| 3244 | if (dt == Py_None || PyDateTime_Check(dt)) |
| 3245 | return 0; |
| 3246 | PyErr_Format(PyExc_TypeError, "%s(dt) argument must be a datetime instance" |
| 3247 | " or None, not %.200s", meth, Py_TYPE(dt)->tp_name); |
| 3248 | return -1; |
| 3249 | } |
| 3250 | |
| 3251 | static PyObject * |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3252 | timezone_repr(PyDateTime_TimeZone *self) |
| 3253 | { |
| 3254 | /* Note that although timezone is not subclassable, it is convenient |
| 3255 | to use Py_TYPE(self)->tp_name here. */ |
| 3256 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3257 | |
| 3258 | if (((PyObject *)self) == PyDateTime_TimeZone_UTC) |
| 3259 | return PyUnicode_FromFormat("%s.utc", type_name); |
| 3260 | |
| 3261 | if (self->name == NULL) |
| 3262 | return PyUnicode_FromFormat("%s(%R)", type_name, self->offset); |
| 3263 | |
| 3264 | return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset, |
| 3265 | self->name); |
| 3266 | } |
| 3267 | |
| 3268 | |
| 3269 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3270 | timezone_str(PyDateTime_TimeZone *self) |
| 3271 | { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3272 | int hours, minutes, seconds; |
| 3273 | PyObject *offset; |
| 3274 | char sign; |
| 3275 | |
| 3276 | if (self->name != NULL) { |
| 3277 | Py_INCREF(self->name); |
| 3278 | return self->name; |
| 3279 | } |
| 3280 | /* Offset is normalized, so it is negative if days < 0 */ |
| 3281 | if (GET_TD_DAYS(self->offset) < 0) { |
| 3282 | sign = '-'; |
| 3283 | offset = delta_negative((PyDateTime_Delta *)self->offset); |
| 3284 | if (offset == NULL) |
| 3285 | return NULL; |
| 3286 | } |
| 3287 | else { |
| 3288 | sign = '+'; |
| 3289 | offset = self->offset; |
| 3290 | Py_INCREF(offset); |
| 3291 | } |
| 3292 | /* Offset is not negative here. */ |
| 3293 | seconds = GET_TD_SECONDS(offset); |
| 3294 | Py_DECREF(offset); |
| 3295 | minutes = divmod(seconds, 60, &seconds); |
| 3296 | hours = divmod(minutes, 60, &minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3297 | /* XXX ignore sub-minute data, curently not allowed. */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 3298 | assert(seconds == 0); |
| 3299 | return PyUnicode_FromFormat("UTC%c%02d:%02d", sign, hours, minutes); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | static PyObject * |
| 3303 | timezone_tzname(PyDateTime_TimeZone *self, PyObject *dt) |
| 3304 | { |
| 3305 | if (_timezone_check_argument(dt, "tzname") == -1) |
| 3306 | return NULL; |
| 3307 | |
| 3308 | return timezone_str(self); |
| 3309 | } |
| 3310 | |
| 3311 | static PyObject * |
| 3312 | timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt) |
| 3313 | { |
| 3314 | if (_timezone_check_argument(dt, "utcoffset") == -1) |
| 3315 | return NULL; |
| 3316 | |
| 3317 | Py_INCREF(self->offset); |
| 3318 | return self->offset; |
| 3319 | } |
| 3320 | |
| 3321 | static PyObject * |
| 3322 | timezone_dst(PyObject *self, PyObject *dt) |
| 3323 | { |
| 3324 | if (_timezone_check_argument(dt, "dst") == -1) |
| 3325 | return NULL; |
| 3326 | |
| 3327 | Py_RETURN_NONE; |
| 3328 | } |
| 3329 | |
| 3330 | static PyObject * |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3331 | timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt) |
| 3332 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3333 | if (!PyDateTime_Check(dt)) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3334 | PyErr_SetString(PyExc_TypeError, |
| 3335 | "fromutc: argument must be a datetime"); |
| 3336 | return NULL; |
| 3337 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3338 | if (!HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3339 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 3340 | "is not self"); |
| 3341 | return NULL; |
| 3342 | } |
| 3343 | |
| 3344 | return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1); |
| 3345 | } |
| 3346 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3347 | static PyObject * |
| 3348 | timezone_getinitargs(PyDateTime_TimeZone *self) |
| 3349 | { |
| 3350 | if (self->name == NULL) |
| 3351 | return Py_BuildValue("(O)", self->offset); |
| 3352 | return Py_BuildValue("(OO)", self->offset, self->name); |
| 3353 | } |
| 3354 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3355 | static PyMethodDef timezone_methods[] = { |
| 3356 | {"tzname", (PyCFunction)timezone_tzname, METH_O, |
| 3357 | 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] | 3358 | " Otherwise returns offset as 'UTC(+|-)HH:MM'.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3359 | |
| 3360 | {"utcoffset", (PyCFunction)timezone_utcoffset, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3361 | PyDoc_STR("Return fixed offset.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3362 | |
| 3363 | {"dst", (PyCFunction)timezone_dst, METH_O, |
Alexander Belopolsky | b39a0c2 | 2010-06-15 19:24:52 +0000 | [diff] [blame] | 3364 | PyDoc_STR("Return None.")}, |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3365 | |
| 3366 | {"fromutc", (PyCFunction)timezone_fromutc, METH_O, |
| 3367 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 3368 | |
Alexander Belopolsky | 1b7046b | 2010-06-23 21:40:15 +0000 | [diff] [blame] | 3369 | {"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS, |
| 3370 | PyDoc_STR("pickle support")}, |
| 3371 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3372 | {NULL, NULL} |
| 3373 | }; |
| 3374 | |
| 3375 | static char timezone_doc[] = |
| 3376 | PyDoc_STR("Fixed offset from UTC implementation of tzinfo."); |
| 3377 | |
| 3378 | static PyTypeObject PyDateTime_TimeZoneType = { |
| 3379 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3380 | "datetime.timezone", /* tp_name */ |
| 3381 | sizeof(PyDateTime_TimeZone), /* tp_basicsize */ |
| 3382 | 0, /* tp_itemsize */ |
| 3383 | (destructor)timezone_dealloc, /* tp_dealloc */ |
| 3384 | 0, /* tp_print */ |
| 3385 | 0, /* tp_getattr */ |
| 3386 | 0, /* tp_setattr */ |
| 3387 | 0, /* tp_reserved */ |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 3388 | (reprfunc)timezone_repr, /* tp_repr */ |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 3389 | 0, /* tp_as_number */ |
| 3390 | 0, /* tp_as_sequence */ |
| 3391 | 0, /* tp_as_mapping */ |
| 3392 | (hashfunc)timezone_hash, /* tp_hash */ |
| 3393 | 0, /* tp_call */ |
| 3394 | (reprfunc)timezone_str, /* tp_str */ |
| 3395 | 0, /* tp_getattro */ |
| 3396 | 0, /* tp_setattro */ |
| 3397 | 0, /* tp_as_buffer */ |
| 3398 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3399 | timezone_doc, /* tp_doc */ |
| 3400 | 0, /* tp_traverse */ |
| 3401 | 0, /* tp_clear */ |
| 3402 | (richcmpfunc)timezone_richcompare,/* tp_richcompare */ |
| 3403 | 0, /* tp_weaklistoffset */ |
| 3404 | 0, /* tp_iter */ |
| 3405 | 0, /* tp_iternext */ |
| 3406 | timezone_methods, /* tp_methods */ |
| 3407 | 0, /* tp_members */ |
| 3408 | 0, /* tp_getset */ |
| 3409 | &PyDateTime_TZInfoType, /* tp_base */ |
| 3410 | 0, /* tp_dict */ |
| 3411 | 0, /* tp_descr_get */ |
| 3412 | 0, /* tp_descr_set */ |
| 3413 | 0, /* tp_dictoffset */ |
| 3414 | 0, /* tp_init */ |
| 3415 | 0, /* tp_alloc */ |
| 3416 | timezone_new, /* tp_new */ |
| 3417 | }; |
| 3418 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3419 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3420 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3421 | */ |
| 3422 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3423 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3424 | */ |
| 3425 | |
| 3426 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3427 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3432 | static PyObject * |
| 3433 | time_minute(PyDateTime_Time *self, void *unused) |
| 3434 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* The name time_second conflicted with some platform header file. */ |
| 3439 | static PyObject * |
| 3440 | py_time_second(PyDateTime_Time *self, void *unused) |
| 3441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | static PyObject * |
| 3446 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 3447 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | static PyObject * |
| 3452 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3455 | Py_INCREF(result); |
| 3456 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | static PyGetSetDef time_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | {"hour", (getter)time_hour}, |
| 3461 | {"minute", (getter)time_minute}, |
| 3462 | {"second", (getter)py_time_second}, |
| 3463 | {"microsecond", (getter)time_microsecond}, |
| 3464 | {"tzinfo", (getter)time_tzinfo}, |
| 3465 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3466 | }; |
| 3467 | |
| 3468 | /* |
| 3469 | * Constructors. |
| 3470 | */ |
| 3471 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3472 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | "tzinfo", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3474 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3475 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3476 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3478 | PyObject *self = NULL; |
| 3479 | PyObject *state; |
| 3480 | int hour = 0; |
| 3481 | int minute = 0; |
| 3482 | int second = 0; |
| 3483 | int usecond = 0; |
| 3484 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | /* Check for invocation from pickle with __getstate__ state */ |
| 3487 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3488 | PyTuple_GET_SIZE(args) <= 2 && |
| 3489 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3490 | PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 3491 | ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24) |
| 3492 | { |
| 3493 | PyDateTime_Time *me; |
| 3494 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | if (PyTuple_GET_SIZE(args) == 2) { |
| 3497 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 3498 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3499 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3500 | "tzinfo state arg"); |
| 3501 | return NULL; |
| 3502 | } |
| 3503 | } |
| 3504 | aware = (char)(tzinfo != Py_None); |
| 3505 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
| 3506 | if (me != NULL) { |
| 3507 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3510 | me->hashcode = -1; |
| 3511 | me->hastzinfo = aware; |
| 3512 | if (aware) { |
| 3513 | Py_INCREF(tzinfo); |
| 3514 | me->tzinfo = tzinfo; |
| 3515 | } |
| 3516 | } |
| 3517 | return (PyObject *)me; |
| 3518 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3520 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, |
| 3521 | &hour, &minute, &second, &usecond, |
| 3522 | &tzinfo)) { |
| 3523 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3524 | return NULL; |
| 3525 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3526 | return NULL; |
| 3527 | self = new_time_ex(hour, minute, second, usecond, tzinfo, |
| 3528 | type); |
| 3529 | } |
| 3530 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | /* |
| 3534 | * Destructor. |
| 3535 | */ |
| 3536 | |
| 3537 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3538 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | if (HASTZINFO(self)) { |
| 3541 | Py_XDECREF(self->tzinfo); |
| 3542 | } |
| 3543 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3547 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3548 | */ |
| 3549 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3550 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3551 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3552 | time_utcoffset(PyObject *self, PyObject *unused) { |
| 3553 | return call_utcoffset(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3557 | time_dst(PyObject *self, PyObject *unused) { |
| 3558 | return call_dst(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3562 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3563 | return call_tzname(GET_TIME_TZINFO(self), Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3567 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3568 | */ |
| 3569 | |
| 3570 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3571 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | const char *type_name = Py_TYPE(self)->tp_name; |
| 3574 | int h = TIME_GET_HOUR(self); |
| 3575 | int m = TIME_GET_MINUTE(self); |
| 3576 | int s = TIME_GET_SECOND(self); |
| 3577 | int us = TIME_GET_MICROSECOND(self); |
| 3578 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | if (us) |
| 3581 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3582 | type_name, h, m, s, us); |
| 3583 | else if (s) |
| 3584 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3585 | type_name, h, m, s); |
| 3586 | else |
| 3587 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
| 3588 | if (result != NULL && HASTZINFO(self)) |
| 3589 | result = append_keyword_tzinfo(result, self->tzinfo); |
| 3590 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3593 | static PyObject * |
| 3594 | time_str(PyDateTime_Time *self) |
| 3595 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3596 | _Py_IDENTIFIER(isoformat); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3597 | |
| 3598 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "()"); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3599 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3600 | |
| 3601 | static PyObject * |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3602 | time_isoformat(PyDateTime_Time *self, PyObject *unused) |
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 | char buf[100]; |
| 3605 | PyObject *result; |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 3606 | int us = TIME_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | if (us) |
| 3609 | result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d", |
| 3610 | TIME_GET_HOUR(self), |
| 3611 | TIME_GET_MINUTE(self), |
| 3612 | TIME_GET_SECOND(self), |
| 3613 | us); |
| 3614 | else |
| 3615 | result = PyUnicode_FromFormat("%02d:%02d:%02d", |
| 3616 | TIME_GET_HOUR(self), |
| 3617 | TIME_GET_MINUTE(self), |
| 3618 | TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3619 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3620 | if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3623 | /* We need to append the UTC offset. */ |
| 3624 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
| 3625 | Py_None) < 0) { |
| 3626 | Py_DECREF(result); |
| 3627 | return NULL; |
| 3628 | } |
| 3629 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
| 3630 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3633 | static PyObject * |
| 3634 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3635 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3636 | PyObject *result; |
| 3637 | PyObject *tuple; |
| 3638 | PyObject *format; |
| 3639 | static char *keywords[] = {"format", 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 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
| 3642 | &format)) |
| 3643 | return NULL; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | /* Python's strftime does insane things with the year part of the |
| 3646 | * timetuple. The year is forced to (the otherwise nonsensical) |
Alexander Belopolsky | b8bb466 | 2011-01-08 00:13:34 +0000 | [diff] [blame] | 3647 | * 1900 to work around that. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | */ |
| 3649 | tuple = Py_BuildValue("iiiiiiiii", |
| 3650 | 1900, 1, 1, /* year, month, day */ |
| 3651 | TIME_GET_HOUR(self), |
| 3652 | TIME_GET_MINUTE(self), |
| 3653 | TIME_GET_SECOND(self), |
| 3654 | 0, 1, -1); /* weekday, daynum, dst */ |
| 3655 | if (tuple == NULL) |
| 3656 | return NULL; |
| 3657 | assert(PyTuple_Size(tuple) == 9); |
| 3658 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 3659 | Py_None); |
| 3660 | Py_DECREF(tuple); |
| 3661 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3662 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3663 | |
| 3664 | /* |
| 3665 | * Miscellaneous methods. |
| 3666 | */ |
| 3667 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3668 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3669 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3670 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3671 | PyObject *result = NULL; |
| 3672 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3673 | int diff; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3674 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3675 | if (! PyTime_Check(other)) |
| 3676 | Py_RETURN_NOTIMPLEMENTED; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3677 | |
| 3678 | if (GET_TIME_TZINFO(self) == GET_TIME_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3679 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3680 | ((PyDateTime_Time *)other)->data, |
| 3681 | _PyDateTime_TIME_DATASIZE); |
| 3682 | return diff_to_bool(diff, op); |
| 3683 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3684 | offset1 = time_utcoffset(self, NULL); |
| 3685 | if (offset1 == NULL) |
| 3686 | return NULL; |
| 3687 | offset2 = time_utcoffset(other, NULL); |
| 3688 | if (offset2 == NULL) |
| 3689 | goto done; |
| 3690 | /* If they're both naive, or both aware and have the same offsets, |
| 3691 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3692 | * offset2 == Py_None at this point. |
| 3693 | */ |
| 3694 | if ((offset1 == offset2) || |
| 3695 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 3696 | delta_cmp(offset1, offset2) == 0)) { |
| 3697 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3698 | ((PyDateTime_Time *)other)->data, |
| 3699 | _PyDateTime_TIME_DATASIZE); |
| 3700 | result = diff_to_bool(diff, op); |
| 3701 | } |
| 3702 | /* The hard case: both aware with different UTC offsets */ |
| 3703 | else if (offset1 != Py_None && offset2 != Py_None) { |
| 3704 | int offsecs1, offsecs2; |
| 3705 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3706 | offsecs1 = TIME_GET_HOUR(self) * 3600 + |
| 3707 | TIME_GET_MINUTE(self) * 60 + |
| 3708 | TIME_GET_SECOND(self) - |
| 3709 | GET_TD_DAYS(offset1) * 86400 - |
| 3710 | GET_TD_SECONDS(offset1); |
| 3711 | offsecs2 = TIME_GET_HOUR(other) * 3600 + |
| 3712 | TIME_GET_MINUTE(other) * 60 + |
| 3713 | TIME_GET_SECOND(other) - |
| 3714 | GET_TD_DAYS(offset2) * 86400 - |
| 3715 | GET_TD_SECONDS(offset2); |
| 3716 | diff = offsecs1 - offsecs2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3717 | if (diff == 0) |
| 3718 | diff = TIME_GET_MICROSECOND(self) - |
| 3719 | TIME_GET_MICROSECOND(other); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3720 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3721 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 3722 | else if (op == Py_EQ) { |
| 3723 | result = Py_False; |
| 3724 | Py_INCREF(result); |
| 3725 | } |
| 3726 | else if (op == Py_NE) { |
| 3727 | result = Py_True; |
| 3728 | Py_INCREF(result); |
| 3729 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3730 | else { |
| 3731 | PyErr_SetString(PyExc_TypeError, |
| 3732 | "can't compare offset-naive and " |
| 3733 | "offset-aware times"); |
| 3734 | } |
| 3735 | done: |
| 3736 | Py_DECREF(offset1); |
| 3737 | Py_XDECREF(offset2); |
| 3738 | return result; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3741 | static Py_hash_t |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3742 | time_hash(PyDateTime_Time *self) |
| 3743 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3744 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3745 | PyObject *offset; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3746 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3747 | offset = time_utcoffset((PyObject *)self, NULL); |
| 3748 | |
| 3749 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | return -1; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3753 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | self->hashcode = generic_hash( |
| 3755 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3756 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3757 | PyObject *temp1, *temp2; |
| 3758 | int seconds, microseconds; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | assert(HASTZINFO(self)); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3760 | seconds = TIME_GET_HOUR(self) * 3600 + |
| 3761 | TIME_GET_MINUTE(self) * 60 + |
| 3762 | TIME_GET_SECOND(self); |
| 3763 | microseconds = TIME_GET_MICROSECOND(self); |
| 3764 | temp1 = new_delta(0, seconds, microseconds, 1); |
| 3765 | if (temp1 == NULL) { |
| 3766 | Py_DECREF(offset); |
| 3767 | return -1; |
| 3768 | } |
| 3769 | temp2 = delta_subtract(temp1, offset); |
| 3770 | Py_DECREF(temp1); |
| 3771 | if (temp2 == NULL) { |
| 3772 | Py_DECREF(offset); |
| 3773 | return -1; |
| 3774 | } |
| 3775 | self->hashcode = PyObject_Hash(temp2); |
| 3776 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3777 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3778 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3779 | } |
| 3780 | return self->hashcode; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3781 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3782 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3783 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3784 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3786 | PyObject *clone; |
| 3787 | PyObject *tuple; |
| 3788 | int hh = TIME_GET_HOUR(self); |
| 3789 | int mm = TIME_GET_MINUTE(self); |
| 3790 | int ss = TIME_GET_SECOND(self); |
| 3791 | int us = TIME_GET_MICROSECOND(self); |
| 3792 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3794 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", |
| 3795 | time_kws, |
| 3796 | &hh, &mm, &ss, &us, &tzinfo)) |
| 3797 | return NULL; |
| 3798 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3799 | if (tuple == NULL) |
| 3800 | return NULL; |
| 3801 | clone = time_new(Py_TYPE(self), tuple, NULL); |
| 3802 | Py_DECREF(tuple); |
| 3803 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3806 | static int |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3807 | time_bool(PyObject *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3808 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3809 | PyObject *offset, *tzinfo; |
| 3810 | int offsecs = 0; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3812 | if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) { |
| 3813 | /* Since utcoffset is in whole minutes, nothing can |
| 3814 | * alter the conclusion that this is nonzero. |
| 3815 | */ |
| 3816 | return 1; |
| 3817 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3818 | tzinfo = GET_TIME_TZINFO(self); |
| 3819 | if (tzinfo != Py_None) { |
| 3820 | offset = call_utcoffset(tzinfo, Py_None); |
| 3821 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3822 | return -1; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3823 | offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset); |
| 3824 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 3826 | 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] | 3827 | } |
| 3828 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3829 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3830 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3831 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3832 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3833 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3834 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3835 | */ |
| 3836 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3837 | time_getstate(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3839 | PyObject *basestate; |
| 3840 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 3843 | _PyDateTime_TIME_DATASIZE); |
| 3844 | if (basestate != NULL) { |
| 3845 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 3846 | result = PyTuple_Pack(1, basestate); |
| 3847 | else |
| 3848 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 3849 | Py_DECREF(basestate); |
| 3850 | } |
| 3851 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3855 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3857 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3860 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3862 | {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, |
| 3863 | PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" |
| 3864 | "[+HH:MM].")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
| 3867 | PyDoc_STR("format -> strftime() style string.")}, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3869 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 3870 | PyDoc_STR("Formats self with strftime.")}, |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
| 3873 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
| 3876 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3878 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
| 3879 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3880 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3881 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
| 3882 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3884 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 3885 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3887 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3888 | }; |
| 3889 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3890 | static char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3891 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 3892 | \n\ |
| 3893 | 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] | 3894 | a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3895 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3896 | static PyNumberMethods time_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3897 | 0, /* nb_add */ |
| 3898 | 0, /* nb_subtract */ |
| 3899 | 0, /* nb_multiply */ |
| 3900 | 0, /* nb_remainder */ |
| 3901 | 0, /* nb_divmod */ |
| 3902 | 0, /* nb_power */ |
| 3903 | 0, /* nb_negative */ |
| 3904 | 0, /* nb_positive */ |
| 3905 | 0, /* nb_absolute */ |
| 3906 | (inquiry)time_bool, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3907 | }; |
| 3908 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3909 | static PyTypeObject PyDateTime_TimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3910 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3911 | "datetime.time", /* tp_name */ |
| 3912 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
| 3913 | 0, /* tp_itemsize */ |
| 3914 | (destructor)time_dealloc, /* tp_dealloc */ |
| 3915 | 0, /* tp_print */ |
| 3916 | 0, /* tp_getattr */ |
| 3917 | 0, /* tp_setattr */ |
| 3918 | 0, /* tp_reserved */ |
| 3919 | (reprfunc)time_repr, /* tp_repr */ |
| 3920 | &time_as_number, /* tp_as_number */ |
| 3921 | 0, /* tp_as_sequence */ |
| 3922 | 0, /* tp_as_mapping */ |
| 3923 | (hashfunc)time_hash, /* tp_hash */ |
| 3924 | 0, /* tp_call */ |
| 3925 | (reprfunc)time_str, /* tp_str */ |
| 3926 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3927 | 0, /* tp_setattro */ |
| 3928 | 0, /* tp_as_buffer */ |
| 3929 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3930 | time_doc, /* tp_doc */ |
| 3931 | 0, /* tp_traverse */ |
| 3932 | 0, /* tp_clear */ |
| 3933 | time_richcompare, /* tp_richcompare */ |
| 3934 | 0, /* tp_weaklistoffset */ |
| 3935 | 0, /* tp_iter */ |
| 3936 | 0, /* tp_iternext */ |
| 3937 | time_methods, /* tp_methods */ |
| 3938 | 0, /* tp_members */ |
| 3939 | time_getset, /* tp_getset */ |
| 3940 | 0, /* tp_base */ |
| 3941 | 0, /* tp_dict */ |
| 3942 | 0, /* tp_descr_get */ |
| 3943 | 0, /* tp_descr_set */ |
| 3944 | 0, /* tp_dictoffset */ |
| 3945 | 0, /* tp_init */ |
| 3946 | time_alloc, /* tp_alloc */ |
| 3947 | time_new, /* tp_new */ |
| 3948 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3949 | }; |
| 3950 | |
| 3951 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3952 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3953 | */ |
| 3954 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3955 | /* Accessor properties. Properties for day, month, and year are inherited |
| 3956 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3957 | */ |
| 3958 | |
| 3959 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3960 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3961 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3962 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3965 | static PyObject * |
| 3966 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 3967 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | static PyObject * |
| 3972 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 3973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3974 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | static PyObject * |
| 3978 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 3979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3980 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | static PyObject * |
| 3984 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 3985 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3986 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3987 | Py_INCREF(result); |
| 3988 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | static PyGetSetDef datetime_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3992 | {"hour", (getter)datetime_hour}, |
| 3993 | {"minute", (getter)datetime_minute}, |
| 3994 | {"second", (getter)datetime_second}, |
| 3995 | {"microsecond", (getter)datetime_microsecond}, |
| 3996 | {"tzinfo", (getter)datetime_tzinfo}, |
| 3997 | {NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3998 | }; |
| 3999 | |
| 4000 | /* |
| 4001 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4002 | */ |
| 4003 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4004 | static char *datetime_kws[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | "year", "month", "day", "hour", "minute", "second", |
| 4006 | "microsecond", "tzinfo", NULL |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4007 | }; |
| 4008 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4009 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4010 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4011 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4012 | PyObject *self = NULL; |
| 4013 | PyObject *state; |
| 4014 | int year; |
| 4015 | int month; |
| 4016 | int day; |
| 4017 | int hour = 0; |
| 4018 | int minute = 0; |
| 4019 | int second = 0; |
| 4020 | int usecond = 0; |
| 4021 | PyObject *tzinfo = Py_None; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4023 | /* Check for invocation from pickle with __getstate__ state */ |
| 4024 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 4025 | PyTuple_GET_SIZE(args) <= 2 && |
| 4026 | PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 4027 | PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 4028 | MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) |
| 4029 | { |
| 4030 | PyDateTime_DateTime *me; |
| 4031 | char aware; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4033 | if (PyTuple_GET_SIZE(args) == 2) { |
| 4034 | tzinfo = PyTuple_GET_ITEM(args, 1); |
| 4035 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 4036 | PyErr_SetString(PyExc_TypeError, "bad " |
| 4037 | "tzinfo state arg"); |
| 4038 | return NULL; |
| 4039 | } |
| 4040 | } |
| 4041 | aware = (char)(tzinfo != Py_None); |
| 4042 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
| 4043 | if (me != NULL) { |
| 4044 | char *pdata = PyBytes_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 4045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4046 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 4047 | me->hashcode = -1; |
| 4048 | me->hastzinfo = aware; |
| 4049 | if (aware) { |
| 4050 | Py_INCREF(tzinfo); |
| 4051 | me->tzinfo = tzinfo; |
| 4052 | } |
| 4053 | } |
| 4054 | return (PyObject *)me; |
| 4055 | } |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4057 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, |
| 4058 | &year, &month, &day, &hour, &minute, |
| 4059 | &second, &usecond, &tzinfo)) { |
| 4060 | if (check_date_args(year, month, day) < 0) |
| 4061 | return NULL; |
| 4062 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 4063 | return NULL; |
| 4064 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4065 | return NULL; |
| 4066 | self = new_datetime_ex(year, month, day, |
| 4067 | hour, minute, second, usecond, |
| 4068 | tzinfo, type); |
| 4069 | } |
| 4070 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4073 | /* TM_FUNC is the shared type of localtime() and gmtime(). */ |
| 4074 | typedef struct tm *(*TM_FUNC)(const time_t *timer); |
| 4075 | |
| 4076 | /* Internal helper. |
| 4077 | * Build datetime from a time_t and a distinct count of microseconds. |
| 4078 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 4079 | */ |
| 4080 | static PyObject * |
| 4081 | 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] | 4082 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4083 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4084 | struct tm *tm; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4086 | tm = f(&timet); |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4087 | if (tm == NULL) { |
| 4088 | #ifdef EINVAL |
| 4089 | if (errno == 0) |
| 4090 | errno = EINVAL; |
| 4091 | #endif |
| 4092 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4093 | } |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4094 | |
| 4095 | /* The platform localtime/gmtime may insert leap seconds, |
| 4096 | * indicated by tm->tm_sec > 59. We don't care about them, |
| 4097 | * except to the extent that passing them on to the datetime |
| 4098 | * constructor would raise ValueError for a reason that |
| 4099 | * made no sense to the user. |
| 4100 | */ |
| 4101 | if (tm->tm_sec > 59) |
| 4102 | tm->tm_sec = 59; |
| 4103 | return PyObject_CallFunction(cls, "iiiiiiiO", |
| 4104 | tm->tm_year + 1900, |
| 4105 | tm->tm_mon + 1, |
| 4106 | tm->tm_mday, |
| 4107 | tm->tm_hour, |
| 4108 | tm->tm_min, |
| 4109 | tm->tm_sec, |
| 4110 | us, |
| 4111 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | /* Internal helper. |
| 4115 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 4116 | * to control the interpretation of the timestamp. Since a double doesn't |
| 4117 | * have enough bits to cover a datetime's full range of precision, it's |
| 4118 | * better to call datetime_from_timet_and_us provided you have a way |
| 4119 | * to get that much precision (e.g., C time() isn't good enough). |
| 4120 | */ |
| 4121 | static PyObject * |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4122 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4123 | PyObject *tzinfo) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4125 | time_t timet; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4126 | long us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4127 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4128 | if (_PyTime_ObjectToTimeval(timestamp, &timet, &us) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4129 | return NULL; |
Victor Stinner | 21f5893 | 2012-03-14 00:15:40 +0100 | [diff] [blame] | 4130 | return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | /* Internal helper. |
| 4134 | * Build most accurate possible datetime for current time. Pass localtime or |
| 4135 | * gmtime for f as appropriate. |
| 4136 | */ |
| 4137 | static PyObject * |
| 4138 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 4139 | { |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 4140 | _PyTime_timeval t; |
| 4141 | _PyTime_gettimeofday(&t); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 | return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec, |
| 4143 | tzinfo); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4146 | /*[clinic] |
| 4147 | module datetime |
| 4148 | |
| 4149 | @classmethod |
| 4150 | datetime.now |
| 4151 | |
| 4152 | tz: object = None |
| 4153 | Timezone object. |
| 4154 | |
| 4155 | Returns new datetime object representing current time local to tz. |
| 4156 | |
| 4157 | If no tz is specified, uses local timezone. |
| 4158 | [clinic]*/ |
| 4159 | |
| 4160 | PyDoc_STRVAR(datetime_now__doc__, |
| 4161 | "Returns new datetime object representing current time local to tz.\n" |
| 4162 | "\n" |
| 4163 | "datetime.now(tz=None)\n" |
| 4164 | " tz\n" |
| 4165 | " Timezone object.\n" |
| 4166 | "\n" |
| 4167 | "If no tz is specified, uses local timezone."); |
| 4168 | |
| 4169 | #define DATETIME_NOW_METHODDEF \ |
| 4170 | {"now", (PyCFunction)datetime_now, METH_VARARGS|METH_KEYWORDS|METH_CLASS, datetime_now__doc__}, |
| 4171 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4172 | static PyObject * |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4173 | datetime_now_impl(PyObject *cls, PyObject *tz); |
| 4174 | |
| 4175 | static PyObject * |
| 4176 | datetime_now(PyObject *cls, PyObject *args, PyObject *kwargs) |
| 4177 | { |
| 4178 | PyObject *return_value = NULL; |
| 4179 | static char *_keywords[] = {"tz", NULL}; |
| 4180 | PyObject *tz = Py_None; |
| 4181 | |
| 4182 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4183 | "|O:now", _keywords, |
| 4184 | &tz)) |
| 4185 | goto exit; |
| 4186 | return_value = datetime_now_impl(cls, tz); |
| 4187 | |
| 4188 | exit: |
| 4189 | return return_value; |
| 4190 | } |
| 4191 | |
| 4192 | static PyObject * |
| 4193 | datetime_now_impl(PyObject *cls, PyObject *tz) |
| 4194 | /*[clinic checksum: 328b54387f4c2f8cb534997e1bd55f8cb38c4992]*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4197 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4198 | /* Return best possible local time -- this isn't constrained by the |
| 4199 | * precision of a timestamp. |
| 4200 | */ |
| 4201 | if (check_tzinfo_subclass(tz) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | return NULL; |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 4203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4204 | self = datetime_best_possible(cls, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4205 | tz == Py_None ? localtime : gmtime, |
| 4206 | tz); |
| 4207 | if (self != NULL && tz != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | /* Convert UTC to tzinfo's zone. */ |
| 4209 | PyObject *temp = self; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4210 | _Py_IDENTIFIER(fromutc); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4211 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 4212 | self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4213 | Py_DECREF(temp); |
| 4214 | } |
| 4215 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4218 | /* Return best possible UTC time -- this isn't constrained by the |
| 4219 | * precision of a timestamp. |
| 4220 | */ |
| 4221 | static PyObject * |
| 4222 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 4223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 | return datetime_best_possible(cls, gmtime, Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4227 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 4228 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4229 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4231 | PyObject *self; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4232 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | PyObject *tzinfo = Py_None; |
| 4234 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4235 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4236 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4237 | keywords, ×tamp, &tzinfo)) |
| 4238 | return NULL; |
| 4239 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 4240 | return NULL; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4242 | self = datetime_from_timestamp(cls, |
| 4243 | tzinfo == Py_None ? localtime : gmtime, |
| 4244 | timestamp, |
| 4245 | tzinfo); |
| 4246 | if (self != NULL && tzinfo != Py_None) { |
| 4247 | /* Convert UTC to tzinfo's zone. */ |
| 4248 | PyObject *temp = self; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4249 | _Py_IDENTIFIER(fromutc); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4250 | |
| 4251 | self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4252 | Py_DECREF(temp); |
| 4253 | } |
| 4254 | return self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4257 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 4258 | static PyObject * |
| 4259 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 4260 | { |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4261 | PyObject *timestamp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4262 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4263 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4264 | if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4265 | result = datetime_from_timestamp(cls, gmtime, timestamp, |
| 4266 | Py_None); |
| 4267 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4270 | /* Return new datetime from _strptime.strptime_datetime(). */ |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4271 | static PyObject * |
| 4272 | datetime_strptime(PyObject *cls, PyObject *args) |
| 4273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4274 | static PyObject *module = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4275 | PyObject *string, *format; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4276 | _Py_IDENTIFIER(_strptime_datetime); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4278 | if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | return NULL; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4280 | |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4281 | if (module == NULL) { |
| 4282 | module = PyImport_ImportModuleNoBlock("_strptime"); |
Alexander Belopolsky | 311d2a9 | 2010-06-28 14:36:55 +0000 | [diff] [blame] | 4283 | if (module == NULL) |
Alexander Belopolsky | ca94f55 | 2010-06-17 18:30:34 +0000 | [diff] [blame] | 4284 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4286 | return _PyObject_CallMethodId(module, &PyId__strptime_datetime, "OOO", |
| 4287 | cls, string, format); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4290 | /* Return new datetime from date/datetime and time arguments. */ |
| 4291 | static PyObject * |
| 4292 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 4293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4294 | static char *keywords[] = {"date", "time", NULL}; |
| 4295 | PyObject *date; |
| 4296 | PyObject *time; |
| 4297 | PyObject *result = NULL; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, |
| 4300 | &PyDateTime_DateType, &date, |
| 4301 | &PyDateTime_TimeType, &time)) { |
| 4302 | PyObject *tzinfo = Py_None; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4304 | if (HASTZINFO(time)) |
| 4305 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 4306 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 4307 | GET_YEAR(date), |
| 4308 | GET_MONTH(date), |
| 4309 | GET_DAY(date), |
| 4310 | TIME_GET_HOUR(time), |
| 4311 | TIME_GET_MINUTE(time), |
| 4312 | TIME_GET_SECOND(time), |
| 4313 | TIME_GET_MICROSECOND(time), |
| 4314 | tzinfo); |
| 4315 | } |
| 4316 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4317 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4318 | |
| 4319 | /* |
| 4320 | * Destructor. |
| 4321 | */ |
| 4322 | |
| 4323 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4324 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4326 | if (HASTZINFO(self)) { |
| 4327 | Py_XDECREF(self->tzinfo); |
| 4328 | } |
| 4329 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | /* |
| 4333 | * Indirect access to tzinfo methods. |
| 4334 | */ |
| 4335 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4336 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 4337 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4338 | datetime_utcoffset(PyObject *self, PyObject *unused) { |
| 4339 | return call_utcoffset(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4343 | datetime_dst(PyObject *self, PyObject *unused) { |
| 4344 | return call_dst(GET_DT_TZINFO(self), self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | static PyObject * |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4348 | datetime_tzname(PyObject *self, PyObject *unused) { |
| 4349 | return call_tzname(GET_DT_TZINFO(self), self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4353 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4354 | */ |
| 4355 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4356 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 4357 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4358 | */ |
| 4359 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4360 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | /* Note that the C-level additions can't overflow, because of |
| 4364 | * invariant bounds on the member values. |
| 4365 | */ |
| 4366 | int year = GET_YEAR(date); |
| 4367 | int month = GET_MONTH(date); |
| 4368 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 4369 | int hour = DATE_GET_HOUR(date); |
| 4370 | int minute = DATE_GET_MINUTE(date); |
| 4371 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 4372 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 4373 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | assert(factor == 1 || factor == -1); |
| 4376 | if (normalize_datetime(&year, &month, &day, |
| 4377 | &hour, &minute, &second, µsecond) < 0) |
| 4378 | return NULL; |
| 4379 | else |
| 4380 | return new_datetime(year, month, day, |
| 4381 | hour, minute, second, microsecond, |
| 4382 | HASTZINFO(date) ? date->tzinfo : Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4386 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | if (PyDateTime_Check(left)) { |
| 4389 | /* datetime + ??? */ |
| 4390 | if (PyDelta_Check(right)) |
| 4391 | /* datetime + delta */ |
| 4392 | return add_datetime_timedelta( |
| 4393 | (PyDateTime_DateTime *)left, |
| 4394 | (PyDateTime_Delta *)right, |
| 4395 | 1); |
| 4396 | } |
| 4397 | else if (PyDelta_Check(left)) { |
| 4398 | /* delta + datetime */ |
| 4399 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 4400 | (PyDateTime_Delta *) left, |
| 4401 | 1); |
| 4402 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4403 | Py_RETURN_NOTIMPLEMENTED; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4407 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | PyObject *result = Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | if (PyDateTime_Check(left)) { |
| 4412 | /* datetime - ??? */ |
| 4413 | if (PyDateTime_Check(right)) { |
| 4414 | /* datetime - datetime */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4415 | PyObject *offset1, *offset2, *offdiff = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4417 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4418 | if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) { |
| 4419 | offset2 = offset1 = Py_None; |
| 4420 | Py_INCREF(offset1); |
| 4421 | Py_INCREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4423 | else { |
| 4424 | offset1 = datetime_utcoffset(left, NULL); |
| 4425 | if (offset1 == NULL) |
| 4426 | return NULL; |
| 4427 | offset2 = datetime_utcoffset(right, NULL); |
| 4428 | if (offset2 == NULL) { |
| 4429 | Py_DECREF(offset1); |
| 4430 | return NULL; |
| 4431 | } |
| 4432 | if ((offset1 != Py_None) != (offset2 != Py_None)) { |
| 4433 | PyErr_SetString(PyExc_TypeError, |
| 4434 | "can't subtract offset-naive and " |
| 4435 | "offset-aware datetimes"); |
| 4436 | Py_DECREF(offset1); |
| 4437 | Py_DECREF(offset2); |
| 4438 | return NULL; |
| 4439 | } |
| 4440 | } |
| 4441 | if ((offset1 != offset2) && |
| 4442 | delta_cmp(offset1, offset2) != 0) { |
| 4443 | offdiff = delta_subtract(offset1, offset2); |
| 4444 | if (offdiff == NULL) { |
| 4445 | Py_DECREF(offset1); |
| 4446 | Py_DECREF(offset2); |
| 4447 | return NULL; |
| 4448 | } |
| 4449 | } |
| 4450 | Py_DECREF(offset1); |
| 4451 | Py_DECREF(offset2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4453 | GET_MONTH(left), |
| 4454 | GET_DAY(left)) - |
| 4455 | ymd_to_ord(GET_YEAR(right), |
| 4456 | GET_MONTH(right), |
| 4457 | GET_DAY(right)); |
| 4458 | /* These can't overflow, since the values are |
| 4459 | * normalized. At most this gives the number of |
| 4460 | * seconds in one day. |
| 4461 | */ |
| 4462 | delta_s = (DATE_GET_HOUR(left) - |
| 4463 | DATE_GET_HOUR(right)) * 3600 + |
| 4464 | (DATE_GET_MINUTE(left) - |
| 4465 | DATE_GET_MINUTE(right)) * 60 + |
| 4466 | (DATE_GET_SECOND(left) - |
| 4467 | DATE_GET_SECOND(right)); |
| 4468 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4469 | DATE_GET_MICROSECOND(right); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4470 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4471 | if (offdiff != NULL) { |
| 4472 | PyObject *temp = result; |
| 4473 | result = delta_subtract(result, offdiff); |
| 4474 | Py_DECREF(temp); |
| 4475 | Py_DECREF(offdiff); |
| 4476 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | } |
| 4478 | else if (PyDelta_Check(right)) { |
| 4479 | /* datetime - delta */ |
| 4480 | result = add_datetime_timedelta( |
| 4481 | (PyDateTime_DateTime *)left, |
| 4482 | (PyDateTime_Delta *)right, |
| 4483 | -1); |
| 4484 | } |
| 4485 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | if (result == Py_NotImplemented) |
| 4488 | Py_INCREF(result); |
| 4489 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | /* Various ways to turn a datetime into a string. */ |
| 4493 | |
| 4494 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4495 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | const char *type_name = Py_TYPE(self)->tp_name; |
| 4498 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | if (DATE_GET_MICROSECOND(self)) { |
| 4501 | baserepr = PyUnicode_FromFormat( |
| 4502 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
| 4503 | type_name, |
| 4504 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4505 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4506 | DATE_GET_SECOND(self), |
| 4507 | DATE_GET_MICROSECOND(self)); |
| 4508 | } |
| 4509 | else if (DATE_GET_SECOND(self)) { |
| 4510 | baserepr = PyUnicode_FromFormat( |
| 4511 | "%s(%d, %d, %d, %d, %d, %d)", |
| 4512 | type_name, |
| 4513 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4514 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4515 | DATE_GET_SECOND(self)); |
| 4516 | } |
| 4517 | else { |
| 4518 | baserepr = PyUnicode_FromFormat( |
| 4519 | "%s(%d, %d, %d, %d, %d)", |
| 4520 | type_name, |
| 4521 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4522 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4523 | } |
| 4524 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4525 | return baserepr; |
| 4526 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4529 | static PyObject * |
| 4530 | datetime_str(PyDateTime_DateTime *self) |
| 4531 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4532 | _Py_IDENTIFIER(isoformat); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4533 | |
| 4534 | return _PyObject_CallMethodId((PyObject *)self, &PyId_isoformat, "(s)", " "); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4535 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4536 | |
| 4537 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4538 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4540 | int sep = 'T'; |
| 4541 | static char *keywords[] = {"sep", NULL}; |
| 4542 | char buffer[100]; |
| 4543 | PyObject *result; |
| 4544 | int us = DATE_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep)) |
| 4547 | return NULL; |
| 4548 | if (us) |
| 4549 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d", |
| 4550 | GET_YEAR(self), GET_MONTH(self), |
| 4551 | GET_DAY(self), (int)sep, |
| 4552 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4553 | DATE_GET_SECOND(self), us); |
| 4554 | else |
| 4555 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d", |
| 4556 | GET_YEAR(self), GET_MONTH(self), |
| 4557 | GET_DAY(self), (int)sep, |
| 4558 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4559 | DATE_GET_SECOND(self)); |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | if (!result || !HASTZINFO(self)) |
| 4562 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 | /* We need to append the UTC offset. */ |
| 4565 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
| 4566 | (PyObject *)self) < 0) { |
| 4567 | Py_DECREF(result); |
| 4568 | return NULL; |
| 4569 | } |
| 4570 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
| 4571 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4574 | static PyObject * |
| 4575 | datetime_ctime(PyDateTime_DateTime *self) |
| 4576 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4577 | return format_ctime((PyDateTime_Date *)self, |
| 4578 | DATE_GET_HOUR(self), |
| 4579 | DATE_GET_MINUTE(self), |
| 4580 | DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4583 | /* Miscellaneous methods. */ |
| 4584 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4585 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4586 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4587 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4588 | PyObject *result = NULL; |
| 4589 | PyObject *offset1, *offset2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | int diff; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | if (! PyDateTime_Check(other)) { |
| 4593 | if (PyDate_Check(other)) { |
| 4594 | /* Prevent invocation of date_richcompare. We want to |
| 4595 | return NotImplemented here to give the other object |
| 4596 | a chance. But since DateTime is a subclass of |
| 4597 | Date, if the other object is a Date, it would |
| 4598 | compute an ordering based on the date part alone, |
| 4599 | and we don't want that. So force unequal or |
| 4600 | uncomparable here in that case. */ |
| 4601 | if (op == Py_EQ) |
| 4602 | Py_RETURN_FALSE; |
| 4603 | if (op == Py_NE) |
| 4604 | Py_RETURN_TRUE; |
| 4605 | return cmperror(self, other); |
| 4606 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 4607 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4608 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4609 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4610 | if (GET_DT_TZINFO(self) == GET_DT_TZINFO(other)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4611 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4612 | ((PyDateTime_DateTime *)other)->data, |
| 4613 | _PyDateTime_DATETIME_DATASIZE); |
| 4614 | return diff_to_bool(diff, op); |
| 4615 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4616 | offset1 = datetime_utcoffset(self, NULL); |
| 4617 | if (offset1 == NULL) |
| 4618 | return NULL; |
| 4619 | offset2 = datetime_utcoffset(other, NULL); |
| 4620 | if (offset2 == NULL) |
| 4621 | goto done; |
| 4622 | /* If they're both naive, or both aware and have the same offsets, |
| 4623 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4624 | * offset2 == Py_None at this point. |
| 4625 | */ |
| 4626 | if ((offset1 == offset2) || |
| 4627 | (PyDelta_Check(offset1) && PyDelta_Check(offset2) && |
| 4628 | delta_cmp(offset1, offset2) == 0)) { |
| 4629 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4630 | ((PyDateTime_DateTime *)other)->data, |
| 4631 | _PyDateTime_DATETIME_DATASIZE); |
| 4632 | result = diff_to_bool(diff, op); |
| 4633 | } |
| 4634 | else if (offset1 != Py_None && offset2 != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | PyDateTime_Delta *delta; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4636 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4637 | assert(offset1 != offset2); /* else last "if" handled it */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4639 | other); |
| 4640 | if (delta == NULL) |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4641 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4642 | diff = GET_TD_DAYS(delta); |
| 4643 | if (diff == 0) |
| 4644 | diff = GET_TD_SECONDS(delta) | |
| 4645 | GET_TD_MICROSECONDS(delta); |
| 4646 | Py_DECREF(delta); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4647 | result = diff_to_bool(diff, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4648 | } |
Alexander Belopolsky | 0831382 | 2012-06-15 20:19:47 -0400 | [diff] [blame] | 4649 | else if (op == Py_EQ) { |
| 4650 | result = Py_False; |
| 4651 | Py_INCREF(result); |
| 4652 | } |
| 4653 | else if (op == Py_NE) { |
| 4654 | result = Py_True; |
| 4655 | Py_INCREF(result); |
| 4656 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4657 | else { |
| 4658 | PyErr_SetString(PyExc_TypeError, |
| 4659 | "can't compare offset-naive and " |
| 4660 | "offset-aware datetimes"); |
| 4661 | } |
| 4662 | done: |
| 4663 | Py_DECREF(offset1); |
| 4664 | Py_XDECREF(offset2); |
| 4665 | return result; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 4668 | static Py_hash_t |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4669 | datetime_hash(PyDateTime_DateTime *self) |
| 4670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4671 | if (self->hashcode == -1) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4672 | PyObject *offset; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4673 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4674 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4675 | |
| 4676 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | return -1; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | /* Reduce this to a hash of another object. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4680 | if (offset == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | self->hashcode = generic_hash( |
| 4682 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | else { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4684 | PyObject *temp1, *temp2; |
| 4685 | int days, seconds; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4687 | assert(HASTZINFO(self)); |
| 4688 | days = ymd_to_ord(GET_YEAR(self), |
| 4689 | GET_MONTH(self), |
| 4690 | GET_DAY(self)); |
| 4691 | seconds = DATE_GET_HOUR(self) * 3600 + |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4692 | DATE_GET_MINUTE(self) * 60 + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4693 | DATE_GET_SECOND(self); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4694 | temp1 = new_delta(days, seconds, |
| 4695 | DATE_GET_MICROSECOND(self), |
| 4696 | 1); |
| 4697 | if (temp1 == NULL) { |
| 4698 | Py_DECREF(offset); |
| 4699 | return -1; |
| 4700 | } |
| 4701 | temp2 = delta_subtract(temp1, offset); |
| 4702 | Py_DECREF(temp1); |
| 4703 | if (temp2 == NULL) { |
| 4704 | Py_DECREF(offset); |
| 4705 | return -1; |
| 4706 | } |
| 4707 | self->hashcode = PyObject_Hash(temp2); |
| 4708 | Py_DECREF(temp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4710 | Py_DECREF(offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4711 | } |
| 4712 | return self->hashcode; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4713 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4714 | |
| 4715 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4716 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | PyObject *clone; |
| 4719 | PyObject *tuple; |
| 4720 | int y = GET_YEAR(self); |
| 4721 | int m = GET_MONTH(self); |
| 4722 | int d = GET_DAY(self); |
| 4723 | int hh = DATE_GET_HOUR(self); |
| 4724 | int mm = DATE_GET_MINUTE(self); |
| 4725 | int ss = DATE_GET_SECOND(self); |
| 4726 | int us = DATE_GET_MICROSECOND(self); |
| 4727 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4729 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", |
| 4730 | datetime_kws, |
| 4731 | &y, &m, &d, &hh, &mm, &ss, &us, |
| 4732 | &tzinfo)) |
| 4733 | return NULL; |
| 4734 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 4735 | if (tuple == NULL) |
| 4736 | return NULL; |
| 4737 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
| 4738 | Py_DECREF(tuple); |
| 4739 | return clone; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | static PyObject * |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4743 | local_timezone(PyDateTime_DateTime *utc_time) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4744 | { |
| 4745 | PyObject *result = NULL; |
| 4746 | struct tm *timep; |
| 4747 | time_t timestamp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4748 | PyObject *delta; |
| 4749 | PyObject *one_second; |
| 4750 | PyObject *seconds; |
| 4751 | PyObject *nameo = NULL; |
| 4752 | const char *zone = NULL; |
| 4753 | |
| 4754 | delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); |
| 4755 | if (delta == NULL) |
| 4756 | return NULL; |
| 4757 | one_second = new_delta(0, 1, 0, 0); |
| 4758 | if (one_second == NULL) |
| 4759 | goto error; |
| 4760 | seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, |
| 4761 | (PyDateTime_Delta *)one_second); |
| 4762 | Py_DECREF(one_second); |
| 4763 | if (seconds == NULL) |
| 4764 | goto error; |
| 4765 | Py_DECREF(delta); |
| 4766 | timestamp = PyLong_AsLong(seconds); |
| 4767 | Py_DECREF(seconds); |
| 4768 | if (timestamp == -1 && PyErr_Occurred()) |
| 4769 | return NULL; |
| 4770 | timep = localtime(×tamp); |
| 4771 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4772 | zone = timep->tm_zone; |
| 4773 | delta = new_delta(0, timep->tm_gmtoff, 0, 1); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4774 | #else /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4775 | { |
| 4776 | PyObject *local_time; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4777 | local_time = new_datetime(timep->tm_year + 1900, timep->tm_mon + 1, |
| 4778 | timep->tm_mday, timep->tm_hour, timep->tm_min, |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4779 | timep->tm_sec, DATE_GET_MICROSECOND(utc_time), |
| 4780 | utc_time->tzinfo); |
| 4781 | if (local_time == NULL) |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4782 | goto error; |
Alexander Belopolsky | 93c9cd0 | 2012-06-22 16:04:19 -0400 | [diff] [blame] | 4783 | delta = datetime_subtract(local_time, (PyObject*)utc_time); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4784 | /* XXX: before relying on tzname, we should compare delta |
| 4785 | to the offset implied by timezone/altzone */ |
| 4786 | if (daylight && timep->tm_isdst >= 0) |
| 4787 | zone = tzname[timep->tm_isdst % 2]; |
| 4788 | else |
| 4789 | zone = tzname[0]; |
| 4790 | Py_DECREF(local_time); |
| 4791 | } |
| 4792 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
| 4793 | if (zone != NULL) { |
| 4794 | nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); |
| 4795 | if (nameo == NULL) |
| 4796 | goto error; |
| 4797 | } |
| 4798 | result = new_timezone(delta, nameo); |
Christian Heimes | b91ffaa | 2013-06-29 20:52:33 +0200 | [diff] [blame] | 4799 | Py_XDECREF(nameo); |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4800 | error: |
| 4801 | Py_DECREF(delta); |
| 4802 | return result; |
| 4803 | } |
| 4804 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4805 | static PyDateTime_DateTime * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4806 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4807 | { |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4808 | PyDateTime_DateTime *result; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4809 | PyObject *offset; |
| 4810 | PyObject *temp; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4811 | PyObject *tzinfo = Py_None; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4812 | _Py_IDENTIFIER(fromutc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4813 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4814 | |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4815 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:astimezone", keywords, |
| 4816 | &tzinfo)) |
| 4817 | return NULL; |
| 4818 | |
| 4819 | if (check_tzinfo_subclass(tzinfo) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4820 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4822 | if (!HASTZINFO(self) || self->tzinfo == Py_None) |
| 4823 | goto NeedAware; |
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 | /* Conversion to self's own time zone is a NOP. */ |
| 4826 | if (self->tzinfo == tzinfo) { |
| 4827 | Py_INCREF(self); |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4828 | return self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4829 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4831 | /* Convert self to UTC. */ |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4832 | offset = datetime_utcoffset((PyObject *)self, NULL); |
| 4833 | if (offset == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4834 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4835 | if (offset == Py_None) { |
| 4836 | Py_DECREF(offset); |
| 4837 | NeedAware: |
| 4838 | PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " |
| 4839 | "a naive datetime"); |
| 4840 | return NULL; |
| 4841 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4842 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4843 | /* result = self - offset */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4844 | result = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4845 | (PyDateTime_Delta *)offset, -1); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4846 | Py_DECREF(offset); |
| 4847 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4848 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4850 | /* Attach new tzinfo and let fromutc() do the rest. */ |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4851 | temp = result->tzinfo; |
Alexander Belopolsky | fdc860f | 2012-06-22 12:23:23 -0400 | [diff] [blame] | 4852 | if (tzinfo == Py_None) { |
| 4853 | tzinfo = local_timezone(result); |
| 4854 | if (tzinfo == NULL) { |
| 4855 | Py_DECREF(result); |
| 4856 | return NULL; |
| 4857 | } |
| 4858 | } |
| 4859 | else |
| 4860 | Py_INCREF(tzinfo); |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4861 | result->tzinfo = tzinfo; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4862 | Py_DECREF(temp); |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4863 | |
Alexander Belopolsky | 31227ca | 2012-06-22 13:23:21 -0400 | [diff] [blame] | 4864 | temp = (PyObject *)result; |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4865 | result = (PyDateTime_DateTime *) |
| 4866 | _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", temp); |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4867 | Py_DECREF(temp); |
| 4868 | |
Alexander Belopolsky | 878054e | 2012-06-22 14:11:58 -0400 | [diff] [blame] | 4869 | return result; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4873 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4874 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4875 | int dstflag = -1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4877 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4878 | PyObject * dst; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4879 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4880 | dst = call_dst(self->tzinfo, (PyObject *)self); |
| 4881 | if (dst == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4882 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4883 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4884 | if (dst != Py_None) |
| 4885 | dstflag = delta_bool((PyDateTime_Delta *)dst); |
| 4886 | Py_DECREF(dst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4887 | } |
| 4888 | return build_struct_time(GET_YEAR(self), |
| 4889 | GET_MONTH(self), |
| 4890 | GET_DAY(self), |
| 4891 | DATE_GET_HOUR(self), |
| 4892 | DATE_GET_MINUTE(self), |
| 4893 | DATE_GET_SECOND(self), |
| 4894 | dstflag); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | static PyObject * |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 4898 | datetime_timestamp(PyDateTime_DateTime *self) |
| 4899 | { |
| 4900 | PyObject *result; |
| 4901 | |
| 4902 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
| 4903 | PyObject *delta; |
| 4904 | delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); |
| 4905 | if (delta == NULL) |
| 4906 | return NULL; |
| 4907 | result = delta_total_seconds(delta); |
| 4908 | Py_DECREF(delta); |
| 4909 | } |
| 4910 | else { |
| 4911 | struct tm time; |
| 4912 | time_t timestamp; |
| 4913 | memset((void *) &time, '\0', sizeof(struct tm)); |
| 4914 | time.tm_year = GET_YEAR(self) - 1900; |
| 4915 | time.tm_mon = GET_MONTH(self) - 1; |
| 4916 | time.tm_mday = GET_DAY(self); |
| 4917 | time.tm_hour = DATE_GET_HOUR(self); |
| 4918 | time.tm_min = DATE_GET_MINUTE(self); |
| 4919 | time.tm_sec = DATE_GET_SECOND(self); |
| 4920 | time.tm_wday = -1; |
| 4921 | time.tm_isdst = -1; |
| 4922 | timestamp = mktime(&time); |
Victor Stinner | 9303749 | 2013-06-25 22:54:35 +0200 | [diff] [blame] | 4923 | if (timestamp == (time_t)(-1) |
| 4924 | #ifndef _AIX |
| 4925 | /* Return value of -1 does not necessarily mean an error, |
| 4926 | * but tm_wday cannot remain set to -1 if mktime succeeded. */ |
| 4927 | && time.tm_wday == -1 |
| 4928 | #else |
| 4929 | /* on AIX, tm_wday is always sets, even on error */ |
| 4930 | #endif |
| 4931 | ) |
| 4932 | { |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 4933 | PyErr_SetString(PyExc_OverflowError, |
| 4934 | "timestamp out of range"); |
| 4935 | return NULL; |
| 4936 | } |
| 4937 | result = PyFloat_FromDouble(timestamp + DATE_GET_MICROSECOND(self) / 1e6); |
| 4938 | } |
| 4939 | return result; |
| 4940 | } |
| 4941 | |
| 4942 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4943 | datetime_getdate(PyDateTime_DateTime *self) |
| 4944 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4945 | return new_date(GET_YEAR(self), |
| 4946 | GET_MONTH(self), |
| 4947 | GET_DAY(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | static PyObject * |
| 4951 | datetime_gettime(PyDateTime_DateTime *self) |
| 4952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4953 | return new_time(DATE_GET_HOUR(self), |
| 4954 | DATE_GET_MINUTE(self), |
| 4955 | DATE_GET_SECOND(self), |
| 4956 | DATE_GET_MICROSECOND(self), |
| 4957 | Py_None); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | static PyObject * |
| 4961 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 4962 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4963 | return new_time(DATE_GET_HOUR(self), |
| 4964 | DATE_GET_MINUTE(self), |
| 4965 | DATE_GET_SECOND(self), |
| 4966 | DATE_GET_MICROSECOND(self), |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4967 | GET_DT_TZINFO(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | static PyObject * |
| 4971 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4972 | { |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4973 | int y, m, d, hh, mm, ss; |
| 4974 | PyObject *tzinfo; |
| 4975 | PyDateTime_DateTime *utcself; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4976 | |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4977 | tzinfo = GET_DT_TZINFO(self); |
| 4978 | if (tzinfo == Py_None) { |
| 4979 | utcself = self; |
| 4980 | Py_INCREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4982 | else { |
| 4983 | PyObject *offset; |
| 4984 | offset = call_utcoffset(tzinfo, (PyObject *)self); |
| 4985 | if (offset == NULL) |
Alexander Belopolsky | 75f94c2 | 2010-06-21 15:21:14 +0000 | [diff] [blame] | 4986 | return NULL; |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 4987 | if (offset == Py_None) { |
| 4988 | Py_DECREF(offset); |
| 4989 | utcself = self; |
| 4990 | Py_INCREF(utcself); |
| 4991 | } |
| 4992 | else { |
| 4993 | utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self, |
| 4994 | (PyDateTime_Delta *)offset, -1); |
| 4995 | Py_DECREF(offset); |
| 4996 | if (utcself == NULL) |
| 4997 | return NULL; |
| 4998 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4999 | } |
Alexander Belopolsky | 73ca440 | 2010-07-07 23:56:38 +0000 | [diff] [blame] | 5000 | y = GET_YEAR(utcself); |
| 5001 | m = GET_MONTH(utcself); |
| 5002 | d = GET_DAY(utcself); |
| 5003 | hh = DATE_GET_HOUR(utcself); |
| 5004 | mm = DATE_GET_MINUTE(utcself); |
| 5005 | ss = DATE_GET_SECOND(utcself); |
| 5006 | |
| 5007 | Py_DECREF(utcself); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5008 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 5011 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 5012 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5013 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5014 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 5015 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 5016 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5017 | */ |
| 5018 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5019 | datetime_getstate(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5020 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | PyObject *basestate; |
| 5022 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5024 | basestate = PyBytes_FromStringAndSize((char *)self->data, |
| 5025 | _PyDateTime_DATETIME_DATASIZE); |
| 5026 | if (basestate != NULL) { |
| 5027 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
| 5028 | result = PyTuple_Pack(1, basestate); |
| 5029 | else |
| 5030 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
| 5031 | Py_DECREF(basestate); |
| 5032 | } |
| 5033 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5037 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5038 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5042 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5045 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 5046 | DATETIME_NOW_METHODDEF |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 5049 | METH_NOARGS | METH_CLASS, |
| 5050 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5052 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
| 5053 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5054 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5056 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 5057 | METH_VARARGS | METH_CLASS, |
| 5058 | PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " |
| 5059 | "(like time.time()).")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5061 | {"strptime", (PyCFunction)datetime_strptime, |
| 5062 | METH_VARARGS | METH_CLASS, |
| 5063 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 5064 | "(like time.strptime()).")}, |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 5065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5066 | {"combine", (PyCFunction)datetime_combine, |
| 5067 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 5068 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5072 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 5073 | PyDoc_STR("Return date object with same year, month and day.")}, |
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 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 5076 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
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 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 5079 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5080 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5081 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 5082 | PyDoc_STR("Return ctime() style string.")}, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5084 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
| 5085 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5086 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5087 | {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, |
| 5088 | PyDoc_STR("Return POSIX timestamp as float.")}, |
| 5089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5090 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
| 5091 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
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 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
| 5094 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
| 5095 | "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" |
| 5096 | "sep is used to separate the year from the time, and " |
| 5097 | "defaults to 'T'.")}, |
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 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
| 5100 | PyDoc_STR("Return self.tzinfo.utcoffset(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 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
| 5103 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5105 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
| 5106 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5108 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
| 5109 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 5110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5111 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
| 5112 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 5113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5114 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 5115 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 5116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5117 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5118 | }; |
| 5119 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5120 | static char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 5121 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 5122 | \n\ |
| 5123 | 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] | 5124 | instance of a tzinfo subclass. The remaining arguments may be ints.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5125 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5126 | static PyNumberMethods datetime_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5127 | datetime_add, /* nb_add */ |
| 5128 | datetime_subtract, /* nb_subtract */ |
| 5129 | 0, /* nb_multiply */ |
| 5130 | 0, /* nb_remainder */ |
| 5131 | 0, /* nb_divmod */ |
| 5132 | 0, /* nb_power */ |
| 5133 | 0, /* nb_negative */ |
| 5134 | 0, /* nb_positive */ |
| 5135 | 0, /* nb_absolute */ |
| 5136 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5137 | }; |
| 5138 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5139 | static PyTypeObject PyDateTime_DateTimeType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5140 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5141 | "datetime.datetime", /* tp_name */ |
| 5142 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
| 5143 | 0, /* tp_itemsize */ |
| 5144 | (destructor)datetime_dealloc, /* tp_dealloc */ |
| 5145 | 0, /* tp_print */ |
| 5146 | 0, /* tp_getattr */ |
| 5147 | 0, /* tp_setattr */ |
| 5148 | 0, /* tp_reserved */ |
| 5149 | (reprfunc)datetime_repr, /* tp_repr */ |
| 5150 | &datetime_as_number, /* tp_as_number */ |
| 5151 | 0, /* tp_as_sequence */ |
| 5152 | 0, /* tp_as_mapping */ |
| 5153 | (hashfunc)datetime_hash, /* tp_hash */ |
| 5154 | 0, /* tp_call */ |
| 5155 | (reprfunc)datetime_str, /* tp_str */ |
| 5156 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5157 | 0, /* tp_setattro */ |
| 5158 | 0, /* tp_as_buffer */ |
| 5159 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5160 | datetime_doc, /* tp_doc */ |
| 5161 | 0, /* tp_traverse */ |
| 5162 | 0, /* tp_clear */ |
| 5163 | datetime_richcompare, /* tp_richcompare */ |
| 5164 | 0, /* tp_weaklistoffset */ |
| 5165 | 0, /* tp_iter */ |
| 5166 | 0, /* tp_iternext */ |
| 5167 | datetime_methods, /* tp_methods */ |
| 5168 | 0, /* tp_members */ |
| 5169 | datetime_getset, /* tp_getset */ |
| 5170 | &PyDateTime_DateType, /* tp_base */ |
| 5171 | 0, /* tp_dict */ |
| 5172 | 0, /* tp_descr_get */ |
| 5173 | 0, /* tp_descr_set */ |
| 5174 | 0, /* tp_dictoffset */ |
| 5175 | 0, /* tp_init */ |
| 5176 | datetime_alloc, /* tp_alloc */ |
| 5177 | datetime_new, /* tp_new */ |
| 5178 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5179 | }; |
| 5180 | |
| 5181 | /* --------------------------------------------------------------------------- |
| 5182 | * Module methods and initialization. |
| 5183 | */ |
| 5184 | |
| 5185 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5186 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5187 | }; |
| 5188 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5189 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 5190 | * datetime.h. |
| 5191 | */ |
| 5192 | static PyDateTime_CAPI CAPI = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | &PyDateTime_DateType, |
| 5194 | &PyDateTime_DateTimeType, |
| 5195 | &PyDateTime_TimeType, |
| 5196 | &PyDateTime_DeltaType, |
| 5197 | &PyDateTime_TZInfoType, |
| 5198 | new_date_ex, |
| 5199 | new_datetime_ex, |
| 5200 | new_time_ex, |
| 5201 | new_delta_ex, |
| 5202 | datetime_fromtimestamp, |
| 5203 | date_fromtimestamp |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5204 | }; |
| 5205 | |
| 5206 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5207 | |
| 5208 | static struct PyModuleDef datetimemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | PyModuleDef_HEAD_INIT, |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5210 | "_datetime", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5211 | "Fast implementation of the datetime type.", |
| 5212 | -1, |
| 5213 | module_methods, |
| 5214 | NULL, |
| 5215 | NULL, |
| 5216 | NULL, |
| 5217 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5218 | }; |
| 5219 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5220 | PyMODINIT_FUNC |
Alexander Belopolsky | cf86e36 | 2010-07-23 19:25:47 +0000 | [diff] [blame] | 5221 | PyInit__datetime(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5223 | PyObject *m; /* a module object */ |
| 5224 | PyObject *d; /* its dict */ |
| 5225 | PyObject *x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5226 | PyObject *delta; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5228 | m = PyModule_Create(&datetimemodule); |
| 5229 | if (m == NULL) |
| 5230 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 5233 | return NULL; |
| 5234 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 5235 | return NULL; |
| 5236 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 5237 | return NULL; |
| 5238 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 5239 | return NULL; |
| 5240 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 5241 | return NULL; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5242 | if (PyType_Ready(&PyDateTime_TimeZoneType) < 0) |
| 5243 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5245 | /* timedelta values */ |
| 5246 | d = PyDateTime_DeltaType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5248 | x = new_delta(0, 0, 1, 0); |
| 5249 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5250 | return NULL; |
| 5251 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5253 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 5254 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5255 | return NULL; |
| 5256 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5258 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 5259 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5260 | return NULL; |
| 5261 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5263 | /* date values */ |
| 5264 | d = PyDateTime_DateType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | x = new_date(1, 1, 1); |
| 5267 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5268 | return NULL; |
| 5269 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | x = new_date(MAXYEAR, 12, 31); |
| 5272 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5273 | return NULL; |
| 5274 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5276 | x = new_delta(1, 0, 0, 0); |
| 5277 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5278 | return NULL; |
| 5279 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | /* time values */ |
| 5282 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5284 | x = new_time(0, 0, 0, 0, Py_None); |
| 5285 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5286 | return NULL; |
| 5287 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5289 | x = new_time(23, 59, 59, 999999, Py_None); |
| 5290 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5291 | return NULL; |
| 5292 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5294 | x = new_delta(0, 0, 1, 0); |
| 5295 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5296 | return NULL; |
| 5297 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5299 | /* datetime values */ |
| 5300 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5302 | x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); |
| 5303 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5304 | return NULL; |
| 5305 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5307 | x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); |
| 5308 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5309 | return NULL; |
| 5310 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | x = new_delta(0, 0, 1, 0); |
| 5313 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 5314 | return NULL; |
| 5315 | Py_DECREF(x); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5316 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5317 | /* timezone values */ |
| 5318 | d = PyDateTime_TimeZoneType.tp_dict; |
| 5319 | |
| 5320 | delta = new_delta(0, 0, 0, 0); |
| 5321 | if (delta == NULL) |
| 5322 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5323 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5324 | Py_DECREF(delta); |
| 5325 | if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) |
| 5326 | return NULL; |
Alexander Belopolsky | a11d8c0 | 2010-07-06 23:19:45 +0000 | [diff] [blame] | 5327 | PyDateTime_TimeZone_UTC = x; |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5328 | |
| 5329 | delta = new_delta(-1, 60, 0, 1); /* -23:59 */ |
| 5330 | if (delta == NULL) |
| 5331 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5332 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5333 | Py_DECREF(delta); |
| 5334 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 5335 | return NULL; |
| 5336 | Py_DECREF(x); |
| 5337 | |
| 5338 | delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ |
| 5339 | if (delta == NULL) |
| 5340 | return NULL; |
Alexander Belopolsky | 1bcbaab | 2010-10-14 17:03:51 +0000 | [diff] [blame] | 5341 | x = create_timezone(delta, NULL); |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5342 | Py_DECREF(delta); |
| 5343 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 5344 | return NULL; |
| 5345 | Py_DECREF(x); |
| 5346 | |
Alexander Belopolsky | a441514 | 2012-06-08 12:33:09 -0400 | [diff] [blame] | 5347 | /* Epoch */ |
| 5348 | PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, |
| 5349 | PyDateTime_TimeZone_UTC); |
| 5350 | if (PyDateTime_Epoch == NULL) |
| 5351 | return NULL; |
| 5352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5353 | /* module initialization */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5354 | PyModule_AddIntMacro(m, MINYEAR); |
| 5355 | PyModule_AddIntMacro(m, MAXYEAR); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5357 | Py_INCREF(&PyDateTime_DateType); |
| 5358 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5360 | Py_INCREF(&PyDateTime_DateTimeType); |
| 5361 | PyModule_AddObject(m, "datetime", |
| 5362 | (PyObject *)&PyDateTime_DateTimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5364 | Py_INCREF(&PyDateTime_TimeType); |
| 5365 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5367 | Py_INCREF(&PyDateTime_DeltaType); |
| 5368 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5370 | Py_INCREF(&PyDateTime_TZInfoType); |
| 5371 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5372 | |
Alexander Belopolsky | 4e749a1 | 2010-06-14 14:15:50 +0000 | [diff] [blame] | 5373 | Py_INCREF(&PyDateTime_TimeZoneType); |
| 5374 | PyModule_AddObject(m, "timezone", (PyObject *) &PyDateTime_TimeZoneType); |
| 5375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5376 | x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL); |
| 5377 | if (x == NULL) |
| 5378 | return NULL; |
| 5379 | PyModule_AddObject(m, "datetime_CAPI", x); |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 5380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5381 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 5382 | * pasting together 4 single years. |
| 5383 | */ |
| 5384 | assert(DI4Y == 4 * 365 + 1); |
| 5385 | assert(DI4Y == days_before_year(4+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 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 5388 | * get from pasting together 4 100-year cycles. |
| 5389 | */ |
| 5390 | assert(DI400Y == 4 * DI100Y + 1); |
| 5391 | assert(DI400Y == days_before_year(400+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5393 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 5394 | * pasting together 25 4-year cycles. |
| 5395 | */ |
| 5396 | assert(DI100Y == 25 * DI4Y - 1); |
| 5397 | assert(DI100Y == days_before_year(100+1)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5398 | |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5399 | one = PyLong_FromLong(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5400 | us_per_ms = PyLong_FromLong(1000); |
| 5401 | us_per_second = PyLong_FromLong(1000000); |
| 5402 | us_per_minute = PyLong_FromLong(60000000); |
| 5403 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Alexander Belopolsky | 790d269 | 2013-08-04 14:51:35 -0400 | [diff] [blame] | 5404 | if (one == NULL || us_per_ms == NULL || us_per_second == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5405 | us_per_minute == NULL || seconds_per_day == NULL) |
| 5406 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5408 | /* The rest are too big for 32-bit ints, but even |
| 5409 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 5410 | */ |
| 5411 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 5412 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 5413 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 5414 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 5415 | return NULL; |
| 5416 | return m; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 5417 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5418 | |
| 5419 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5420 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5421 | x.n = x stripped of its timezone -- its naive time. |
| 5422 | 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] | 5423 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5424 | 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] | 5425 | return None |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5426 | x.s = x's standard offset, x.o - x.d |
| 5427 | |
| 5428 | Now some derived rules, where k is a duration (timedelta). |
| 5429 | |
| 5430 | 1. x.o = x.s + x.d |
| 5431 | This follows from the definition of x.s. |
| 5432 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5433 | 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] | 5434 | This is actually a requirement, an assumption we need to make about |
| 5435 | sane tzinfo classes. |
| 5436 | |
| 5437 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 5438 | This is again a requirement for a sane tzinfo class. |
| 5439 | |
| 5440 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5441 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5442 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5443 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5444 | Again follows from how arithmetic is defined. |
| 5445 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5446 | 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] | 5447 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 5448 | None when called). |
| 5449 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 5450 | 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] | 5451 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5452 | |
| 5453 | By #3, we want |
| 5454 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5455 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5456 | |
| 5457 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 5458 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 5459 | becomes true; in effect, we want to solve [2] for k: |
| 5460 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5461 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5462 | |
| 5463 | By #1, this is the same as |
| 5464 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5465 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5466 | |
| 5467 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 5468 | Substituting that into [3], |
| 5469 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5470 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 5471 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 5472 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 5473 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5474 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5475 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 5476 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 5477 | very large, since all offset-returning methods return a duration of magnitude |
| 5478 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 5479 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5480 | |
| 5481 | In any case, the new value is |
| 5482 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5483 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5484 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5485 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 5486 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5487 | |
| 5488 | At this point, if |
| 5489 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5490 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5491 | |
| 5492 | 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] | 5493 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 5494 | 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] | 5495 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 5496 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 5497 | 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] | 5498 | the only spelling that makes sense on the local wall clock. |
| 5499 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5500 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 5501 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 5502 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5503 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5504 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5505 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5506 | Now |
| 5507 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5508 | (y + y.s).n = by #5 |
| 5509 | y.n + y.s = since y.n = x.n |
| 5510 | x.n + y.s = since z and y are have the same tzinfo member, |
| 5511 | y.s = z.s by #2 |
| 5512 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5513 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5514 | Plugging that back into [6] gives |
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 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5517 | x.n - ((x.n + z.s) - z.o) = expanding |
| 5518 | x.n - x.n - z.s + z.o = cancelling |
| 5519 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5520 | z.d |
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 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5523 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5524 | 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] | 5525 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 5526 | 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] | 5527 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5528 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 5529 | 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] | 5530 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5531 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 5532 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5533 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5534 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5535 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5536 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5537 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5538 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 5539 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5540 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 5541 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 5542 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 5543 | the justifications for the kinds of substitutions we've done several times |
| 5544 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5545 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5546 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 5548 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 5549 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 5550 | - z.n + z.n - z.o + z'.o = cancel z.n |
| 5551 | - z.o + z'.o = #1 twice |
| 5552 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 5553 | z'.d - z.d |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5554 | |
| 5555 | 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] | 5556 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 5557 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5558 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5559 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 5560 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 5561 | would have to change the result dst() returns: we start in DST, and moving |
| 5562 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 5563 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5564 | There isn't a sane case where this can happen. The closest it gets is at |
| 5565 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 5566 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 5567 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 5568 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 5569 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 5570 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 5571 | standard time. Since that's what the local clock *does*, we want to map both |
| 5572 | 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] | 5573 | in local time, but so it goes -- it's the way the local clock works. |
| 5574 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5575 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 5576 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 5577 | 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] | 5578 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 5579 | |
| 5580 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 5581 | 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] | 5582 | 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] | 5583 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 5584 | but the reasoning doesn't depend on the example -- it depends on there being |
| 5585 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 5586 | z' must be in standard time, and is the spelling we want in this case. |
| 5587 | |
| 5588 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 5589 | concerned (because it takes z' as being in standard time rather than the |
| 5590 | daylight time we intend here), but returning it gives the real-life "local |
| 5591 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 5592 | tz. |
| 5593 | |
| 5594 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 5595 | the 1:MM standard time spelling we want. |
| 5596 | |
| 5597 | So how can this break? One of the assumptions must be violated. Two |
| 5598 | possibilities: |
| 5599 | |
| 5600 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 5601 | time zone. This isn't true if, for political reasons or continental drift, |
| 5602 | a region decides to change its base offset from UTC. |
| 5603 | |
| 5604 | 2) There may be versions of "double daylight" time where the tail end of |
| 5605 | the analysis gives up a step too early. I haven't thought about that |
| 5606 | enough to say. |
| 5607 | |
| 5608 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 5609 | "almost all" time zones: so long as the standard offset is invariant, it |
| 5610 | doesn't matter if daylight time transition points change from year to year, or |
| 5611 | if daylight time is skipped in some years; it doesn't matter how large or |
| 5612 | small dst() may get within its bounds; and it doesn't even matter if some |
| 5613 | perverse time zone returns a negative dst()). So a breaking case must be |
| 5614 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 5615 | --------------------------------------------------------------------------- */ |