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