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