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