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