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 | |
| 1114 | offset = call_utcoffset(tzinfo, tzinfoarg, &none); |
| 1115 | if (offset == -1 && PyErr_Occurred()) |
| 1116 | return -1; |
| 1117 | if (none) { |
| 1118 | *buf = '\0'; |
| 1119 | return 0; |
| 1120 | } |
| 1121 | sign = '+'; |
| 1122 | if (offset < 0) { |
| 1123 | sign = '-'; |
| 1124 | offset = - offset; |
| 1125 | } |
| 1126 | hours = divmod(offset, 60, &minutes); |
| 1127 | PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1131 | static PyObject * |
| 1132 | make_Zreplacement(PyObject *object, PyObject *tzinfoarg) |
| 1133 | { |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1134 | PyObject *temp; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1135 | PyObject *tzinfo = get_tzinfo_member(object); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1136 | PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1137 | if (Zreplacement == NULL) |
| 1138 | return NULL; |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1139 | if (tzinfo == Py_None || tzinfo == NULL) |
| 1140 | return Zreplacement; |
| 1141 | |
| 1142 | assert(tzinfoarg != NULL); |
| 1143 | temp = call_tzname(tzinfo, tzinfoarg); |
| 1144 | if (temp == NULL) |
| 1145 | goto Error; |
| 1146 | if (temp == Py_None) { |
| 1147 | Py_DECREF(temp); |
| 1148 | return Zreplacement; |
| 1149 | } |
| 1150 | |
| 1151 | assert(PyUnicode_Check(temp)); |
| 1152 | /* Since the tzname is getting stuffed into the |
| 1153 | * format, we have to double any % signs so that |
| 1154 | * strftime doesn't treat them as format codes. |
| 1155 | */ |
| 1156 | Py_DECREF(Zreplacement); |
| 1157 | Zreplacement = PyObject_CallMethod(temp, "replace", "ss", "%", "%%"); |
| 1158 | Py_DECREF(temp); |
| 1159 | if (Zreplacement == NULL) |
| 1160 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1161 | if (!PyUnicode_Check(Zreplacement)) { |
Neal Norwitz | aea70e0 | 2007-08-12 04:32:26 +0000 | [diff] [blame] | 1162 | PyErr_SetString(PyExc_TypeError, |
| 1163 | "tzname.replace() did not return a string"); |
| 1164 | goto Error; |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1165 | } |
| 1166 | return Zreplacement; |
| 1167 | |
| 1168 | Error: |
| 1169 | Py_DECREF(Zreplacement); |
| 1170 | return NULL; |
| 1171 | } |
| 1172 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1173 | /* I sure don't want to reproduce the strftime code from the time module, |
| 1174 | * so this imports the module and calls it. All the hair is due to |
| 1175 | * giving special meanings to the %z and %Z format codes via a preprocessing |
| 1176 | * step on the format string. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1177 | * tzinfoarg is the argument to pass to the object's tzinfo method, if |
| 1178 | * needed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1179 | */ |
| 1180 | static PyObject * |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1181 | wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, |
| 1182 | PyObject *tzinfoarg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1183 | { |
| 1184 | PyObject *result = NULL; /* guilty until proved innocent */ |
| 1185 | |
| 1186 | PyObject *zreplacement = NULL; /* py string, replacement for %z */ |
| 1187 | PyObject *Zreplacement = NULL; /* py string, replacement for %Z */ |
| 1188 | |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 1189 | const char *pin;/* pointer to next char in input format */ |
| 1190 | Py_ssize_t flen;/* length of input format */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1191 | char ch; /* next char in input format */ |
| 1192 | |
| 1193 | PyObject *newfmt = NULL; /* py string, the output format */ |
| 1194 | char *pnew; /* pointer to available byte in output format */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1195 | int totalnew; /* number bytes total in output format buffer, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1196 | exclusive of trailing \0 */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1197 | int usednew; /* number bytes used so far in output format buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1198 | |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 1199 | const char *ptoappend;/* pointer to string to append to output buffer */ |
Brett Cannon | 27da812 | 2007-11-06 23:15:11 +0000 | [diff] [blame] | 1200 | Py_ssize_t ntoappend; /* # of bytes to append to output buffer */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1201 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1202 | assert(object && format && timetuple); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 1203 | assert(PyUnicode_Check(format)); |
Neal Norwitz | 908c871 | 2007-08-27 04:58:38 +0000 | [diff] [blame] | 1204 | /* Convert the input format to a C string and size */ |
| 1205 | pin = PyUnicode_AsString(format); |
| 1206 | if (!pin) |
| 1207 | return NULL; |
| 1208 | flen = PyUnicode_GetSize(format); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1209 | |
Tim Peters | d684415 | 2002-12-22 20:58:42 +0000 | [diff] [blame] | 1210 | /* Give up if the year is before 1900. |
| 1211 | * Python strftime() plays games with the year, and different |
| 1212 | * games depending on whether envar PYTHON2K is set. This makes |
| 1213 | * years before 1900 a nightmare, even if the platform strftime |
| 1214 | * supports them (and not all do). |
| 1215 | * We could get a lot farther here by avoiding Python's strftime |
| 1216 | * wrapper and calling the C strftime() directly, but that isn't |
| 1217 | * an option in the Python implementation of this module. |
| 1218 | */ |
| 1219 | { |
| 1220 | long year; |
| 1221 | PyObject *pyyear = PySequence_GetItem(timetuple, 0); |
| 1222 | if (pyyear == NULL) return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1223 | assert(PyLong_Check(pyyear)); |
| 1224 | year = PyLong_AsLong(pyyear); |
Tim Peters | d684415 | 2002-12-22 20:58:42 +0000 | [diff] [blame] | 1225 | Py_DECREF(pyyear); |
| 1226 | if (year < 1900) { |
| 1227 | PyErr_Format(PyExc_ValueError, "year=%ld is before " |
| 1228 | "1900; the datetime strftime() " |
| 1229 | "methods require year >= 1900", |
| 1230 | year); |
| 1231 | return NULL; |
| 1232 | } |
| 1233 | } |
| 1234 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1235 | /* Scan the input format, looking for %z and %Z escapes, building |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1236 | * a new format. Since computing the replacements for those codes |
| 1237 | * is expensive, don't unless they're actually used. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1238 | */ |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 1239 | totalnew = flen + 1; /* realistic if no %z/%Z */ |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1240 | newfmt = PyString_FromStringAndSize(NULL, totalnew); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1241 | if (newfmt == NULL) goto Done; |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1242 | pnew = PyString_AsString(newfmt); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1243 | usednew = 0; |
| 1244 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1245 | while ((ch = *pin++) != '\0') { |
| 1246 | if (ch != '%') { |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1247 | ptoappend = pin - 1; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1248 | ntoappend = 1; |
| 1249 | } |
| 1250 | else if ((ch = *pin++) == '\0') { |
| 1251 | /* There's a lone trailing %; doesn't make sense. */ |
| 1252 | PyErr_SetString(PyExc_ValueError, "strftime format " |
| 1253 | "ends with raw %"); |
| 1254 | goto Done; |
| 1255 | } |
| 1256 | /* A % has been seen and ch is the character after it. */ |
| 1257 | else if (ch == 'z') { |
| 1258 | if (zreplacement == NULL) { |
| 1259 | /* format utcoffset */ |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1260 | char buf[100]; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1261 | PyObject *tzinfo = get_tzinfo_member(object); |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1262 | zreplacement = PyString_FromStringAndSize("", 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1263 | if (zreplacement == NULL) goto Done; |
| 1264 | if (tzinfo != Py_None && tzinfo != NULL) { |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1265 | assert(tzinfoarg != NULL); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1266 | if (format_utcoffset(buf, |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1267 | sizeof(buf), |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1268 | "", |
| 1269 | tzinfo, |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1270 | tzinfoarg) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1271 | goto Done; |
| 1272 | Py_DECREF(zreplacement); |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 1273 | zreplacement = |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1274 | PyString_FromStringAndSize(buf, |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 1275 | strlen(buf)); |
| 1276 | if (zreplacement == NULL) |
| 1277 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | assert(zreplacement != NULL); |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1281 | ptoappend = PyString_AS_STRING(zreplacement); |
| 1282 | ntoappend = PyString_GET_SIZE(zreplacement); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1283 | } |
| 1284 | else if (ch == 'Z') { |
| 1285 | /* format tzname */ |
| 1286 | if (Zreplacement == NULL) { |
Guido van Rossum | d8595fe | 2007-05-23 21:36:49 +0000 | [diff] [blame] | 1287 | Zreplacement = make_Zreplacement(object, |
| 1288 | tzinfoarg); |
| 1289 | if (Zreplacement == NULL) |
| 1290 | goto Done; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1291 | } |
| 1292 | assert(Zreplacement != NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1293 | assert(PyUnicode_Check(Zreplacement)); |
| 1294 | ptoappend = PyUnicode_AsStringAndSize(Zreplacement, |
| 1295 | &ntoappend); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1296 | ntoappend = Py_SIZE(Zreplacement); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1297 | } |
| 1298 | else { |
Tim Peters | 328fff7 | 2002-12-20 01:31:27 +0000 | [diff] [blame] | 1299 | /* percent followed by neither z nor Z */ |
| 1300 | ptoappend = pin - 2; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1301 | ntoappend = 2; |
| 1302 | } |
| 1303 | |
| 1304 | /* Append the ntoappend chars starting at ptoappend to |
| 1305 | * the new format. |
| 1306 | */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1307 | if (ntoappend == 0) |
| 1308 | continue; |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 1309 | assert(ptoappend != NULL); |
| 1310 | assert(ntoappend > 0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1311 | while (usednew + ntoappend > totalnew) { |
| 1312 | int bigger = totalnew << 1; |
| 1313 | if ((bigger >> 1) != totalnew) { /* overflow */ |
| 1314 | PyErr_NoMemory(); |
| 1315 | goto Done; |
| 1316 | } |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1317 | if (_PyString_Resize(&newfmt, bigger) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1318 | goto Done; |
| 1319 | totalnew = bigger; |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1320 | pnew = PyString_AsString(newfmt) + usednew; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1321 | } |
| 1322 | memcpy(pnew, ptoappend, ntoappend); |
| 1323 | pnew += ntoappend; |
| 1324 | usednew += ntoappend; |
| 1325 | assert(usednew <= totalnew); |
| 1326 | } /* end while() */ |
| 1327 | |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1328 | if (_PyString_Resize(&newfmt, usednew) < 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1329 | goto Done; |
| 1330 | { |
Neal Norwitz | 908c871 | 2007-08-27 04:58:38 +0000 | [diff] [blame] | 1331 | PyObject *format; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1332 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1333 | if (time == NULL) |
| 1334 | goto Done; |
Walter Dörwald | 6bd238c | 2007-11-22 09:38:52 +0000 | [diff] [blame] | 1335 | format = PyUnicode_FromString(PyString_AS_STRING(newfmt)); |
Neal Norwitz | 908c871 | 2007-08-27 04:58:38 +0000 | [diff] [blame] | 1336 | if (format != NULL) { |
| 1337 | result = PyObject_CallMethod(time, "strftime", "OO", |
| 1338 | format, timetuple); |
| 1339 | Py_DECREF(format); |
| 1340 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1341 | Py_DECREF(time); |
| 1342 | } |
| 1343 | Done: |
| 1344 | Py_XDECREF(zreplacement); |
| 1345 | Py_XDECREF(Zreplacement); |
| 1346 | Py_XDECREF(newfmt); |
| 1347 | return result; |
| 1348 | } |
| 1349 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1350 | /* --------------------------------------------------------------------------- |
| 1351 | * Wrap functions from the time module. These aren't directly available |
| 1352 | * from C. Perhaps they should be. |
| 1353 | */ |
| 1354 | |
| 1355 | /* Call time.time() and return its result (a Python float). */ |
| 1356 | static PyObject * |
Guido van Rossum | bd43e91 | 2002-12-16 20:34:55 +0000 | [diff] [blame] | 1357 | time_time(void) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1358 | { |
| 1359 | PyObject *result = NULL; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1360 | PyObject *time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1361 | |
| 1362 | if (time != NULL) { |
| 1363 | result = PyObject_CallMethod(time, "time", "()"); |
| 1364 | Py_DECREF(time); |
| 1365 | } |
| 1366 | return result; |
| 1367 | } |
| 1368 | |
| 1369 | /* Build a time.struct_time. The weekday and day number are automatically |
| 1370 | * computed from the y,m,d args. |
| 1371 | */ |
| 1372 | static PyObject * |
| 1373 | build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) |
| 1374 | { |
| 1375 | PyObject *time; |
| 1376 | PyObject *result = NULL; |
| 1377 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1378 | time = PyImport_ImportModuleNoBlock("time"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1379 | if (time != NULL) { |
| 1380 | result = PyObject_CallMethod(time, "struct_time", |
| 1381 | "((iiiiiiiii))", |
| 1382 | y, m, d, |
| 1383 | hh, mm, ss, |
| 1384 | weekday(y, m, d), |
| 1385 | days_before_month(y, m) + d, |
| 1386 | dstflag); |
| 1387 | Py_DECREF(time); |
| 1388 | } |
| 1389 | return result; |
| 1390 | } |
| 1391 | |
| 1392 | /* --------------------------------------------------------------------------- |
| 1393 | * Miscellaneous helpers. |
| 1394 | */ |
| 1395 | |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1396 | /* For various reasons, we need to use tp_richcompare instead of tp_compare. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1397 | * The comparisons here all most naturally compute a cmp()-like result. |
| 1398 | * This little helper turns that into a bool result for rich comparisons. |
| 1399 | */ |
| 1400 | static PyObject * |
| 1401 | diff_to_bool(int diff, int op) |
| 1402 | { |
| 1403 | PyObject *result; |
| 1404 | int istrue; |
| 1405 | |
| 1406 | switch (op) { |
| 1407 | case Py_EQ: istrue = diff == 0; break; |
| 1408 | case Py_NE: istrue = diff != 0; break; |
| 1409 | case Py_LE: istrue = diff <= 0; break; |
| 1410 | case Py_GE: istrue = diff >= 0; break; |
| 1411 | case Py_LT: istrue = diff < 0; break; |
| 1412 | case Py_GT: istrue = diff > 0; break; |
| 1413 | default: |
| 1414 | assert(! "op unknown"); |
| 1415 | istrue = 0; /* To shut up compiler */ |
| 1416 | } |
| 1417 | result = istrue ? Py_True : Py_False; |
| 1418 | Py_INCREF(result); |
| 1419 | return result; |
| 1420 | } |
| 1421 | |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1422 | /* Raises a "can't compare" TypeError and returns NULL. */ |
| 1423 | static PyObject * |
| 1424 | cmperror(PyObject *a, PyObject *b) |
| 1425 | { |
| 1426 | PyErr_Format(PyExc_TypeError, |
| 1427 | "can't compare %s to %s", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1428 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1429 | return NULL; |
| 1430 | } |
| 1431 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1432 | /* --------------------------------------------------------------------------- |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1433 | * Cached Python objects; these are set by the module init function. |
| 1434 | */ |
| 1435 | |
| 1436 | /* Conversion factors. */ |
| 1437 | static PyObject *us_per_us = NULL; /* 1 */ |
| 1438 | static PyObject *us_per_ms = NULL; /* 1000 */ |
| 1439 | static PyObject *us_per_second = NULL; /* 1000000 */ |
| 1440 | static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */ |
| 1441 | static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python long */ |
| 1442 | static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python long */ |
| 1443 | static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python long */ |
| 1444 | static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ |
| 1445 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1446 | /* --------------------------------------------------------------------------- |
| 1447 | * Class implementations. |
| 1448 | */ |
| 1449 | |
| 1450 | /* |
| 1451 | * PyDateTime_Delta implementation. |
| 1452 | */ |
| 1453 | |
| 1454 | /* Convert a timedelta to a number of us, |
| 1455 | * (24*3600*self.days + self.seconds)*1000000 + self.microseconds |
| 1456 | * as a Python int or long. |
| 1457 | * Doing mixed-radix arithmetic by hand instead is excruciating in C, |
| 1458 | * due to ubiquitous overflow possibilities. |
| 1459 | */ |
| 1460 | static PyObject * |
| 1461 | delta_to_microseconds(PyDateTime_Delta *self) |
| 1462 | { |
| 1463 | PyObject *x1 = NULL; |
| 1464 | PyObject *x2 = NULL; |
| 1465 | PyObject *x3 = NULL; |
| 1466 | PyObject *result = NULL; |
| 1467 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1468 | x1 = PyLong_FromLong(GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1469 | if (x1 == NULL) |
| 1470 | goto Done; |
| 1471 | x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ |
| 1472 | if (x2 == NULL) |
| 1473 | goto Done; |
| 1474 | Py_DECREF(x1); |
| 1475 | x1 = NULL; |
| 1476 | |
| 1477 | /* x2 has days in seconds */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1478 | x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1479 | if (x1 == NULL) |
| 1480 | goto Done; |
| 1481 | x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */ |
| 1482 | if (x3 == NULL) |
| 1483 | goto Done; |
| 1484 | Py_DECREF(x1); |
| 1485 | Py_DECREF(x2); |
| 1486 | x1 = x2 = NULL; |
| 1487 | |
| 1488 | /* x3 has days+seconds in seconds */ |
| 1489 | x1 = PyNumber_Multiply(x3, us_per_second); /* us */ |
| 1490 | if (x1 == NULL) |
| 1491 | goto Done; |
| 1492 | Py_DECREF(x3); |
| 1493 | x3 = NULL; |
| 1494 | |
| 1495 | /* x1 has days+seconds in us */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1496 | x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1497 | if (x2 == NULL) |
| 1498 | goto Done; |
| 1499 | result = PyNumber_Add(x1, x2); |
| 1500 | |
| 1501 | Done: |
| 1502 | Py_XDECREF(x1); |
| 1503 | Py_XDECREF(x2); |
| 1504 | Py_XDECREF(x3); |
| 1505 | return result; |
| 1506 | } |
| 1507 | |
| 1508 | /* Convert a number of us (as a Python int or long) to a timedelta. |
| 1509 | */ |
| 1510 | static PyObject * |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1511 | microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1512 | { |
| 1513 | int us; |
| 1514 | int s; |
| 1515 | int d; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1516 | long temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1517 | |
| 1518 | PyObject *tuple = NULL; |
| 1519 | PyObject *num = NULL; |
| 1520 | PyObject *result = NULL; |
| 1521 | |
| 1522 | tuple = PyNumber_Divmod(pyus, us_per_second); |
| 1523 | if (tuple == NULL) |
| 1524 | goto Done; |
| 1525 | |
| 1526 | num = PyTuple_GetItem(tuple, 1); /* us */ |
| 1527 | if (num == NULL) |
| 1528 | goto Done; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1529 | temp = PyLong_AsLong(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1530 | num = NULL; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1531 | if (temp == -1 && PyErr_Occurred()) |
| 1532 | goto Done; |
| 1533 | assert(0 <= temp && temp < 1000000); |
| 1534 | us = (int)temp; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1535 | if (us < 0) { |
| 1536 | /* The divisor was positive, so this must be an error. */ |
| 1537 | assert(PyErr_Occurred()); |
| 1538 | goto Done; |
| 1539 | } |
| 1540 | |
| 1541 | num = PyTuple_GetItem(tuple, 0); /* leftover seconds */ |
| 1542 | if (num == NULL) |
| 1543 | goto Done; |
| 1544 | Py_INCREF(num); |
| 1545 | Py_DECREF(tuple); |
| 1546 | |
| 1547 | tuple = PyNumber_Divmod(num, seconds_per_day); |
| 1548 | if (tuple == NULL) |
| 1549 | goto Done; |
| 1550 | Py_DECREF(num); |
| 1551 | |
| 1552 | num = PyTuple_GetItem(tuple, 1); /* seconds */ |
| 1553 | if (num == NULL) |
| 1554 | goto Done; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1555 | temp = PyLong_AsLong(num); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1556 | num = NULL; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1557 | if (temp == -1 && PyErr_Occurred()) |
| 1558 | goto Done; |
| 1559 | assert(0 <= temp && temp < 24*3600); |
| 1560 | s = (int)temp; |
| 1561 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1562 | if (s < 0) { |
| 1563 | /* The divisor was positive, so this must be an error. */ |
| 1564 | assert(PyErr_Occurred()); |
| 1565 | goto Done; |
| 1566 | } |
| 1567 | |
| 1568 | num = PyTuple_GetItem(tuple, 0); /* leftover days */ |
| 1569 | if (num == NULL) |
| 1570 | goto Done; |
| 1571 | Py_INCREF(num); |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1572 | temp = PyLong_AsLong(num); |
| 1573 | if (temp == -1 && PyErr_Occurred()) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1574 | goto Done; |
Tim Peters | 0b0f41c | 2002-12-19 01:44:38 +0000 | [diff] [blame] | 1575 | d = (int)temp; |
| 1576 | if ((long)d != temp) { |
| 1577 | PyErr_SetString(PyExc_OverflowError, "normalized days too " |
| 1578 | "large to fit in a C int"); |
| 1579 | goto Done; |
| 1580 | } |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1581 | result = new_delta_ex(d, s, us, 0, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1582 | |
| 1583 | Done: |
| 1584 | Py_XDECREF(tuple); |
| 1585 | Py_XDECREF(num); |
| 1586 | return result; |
| 1587 | } |
| 1588 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1589 | #define microseconds_to_delta(pymicros) \ |
| 1590 | microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType) |
| 1591 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1592 | static PyObject * |
| 1593 | multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta) |
| 1594 | { |
| 1595 | PyObject *pyus_in; |
| 1596 | PyObject *pyus_out; |
| 1597 | PyObject *result; |
| 1598 | |
| 1599 | pyus_in = delta_to_microseconds(delta); |
| 1600 | if (pyus_in == NULL) |
| 1601 | return NULL; |
| 1602 | |
| 1603 | pyus_out = PyNumber_Multiply(pyus_in, intobj); |
| 1604 | Py_DECREF(pyus_in); |
| 1605 | if (pyus_out == NULL) |
| 1606 | return NULL; |
| 1607 | |
| 1608 | result = microseconds_to_delta(pyus_out); |
| 1609 | Py_DECREF(pyus_out); |
| 1610 | return result; |
| 1611 | } |
| 1612 | |
| 1613 | static PyObject * |
| 1614 | divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj) |
| 1615 | { |
| 1616 | PyObject *pyus_in; |
| 1617 | PyObject *pyus_out; |
| 1618 | PyObject *result; |
| 1619 | |
| 1620 | pyus_in = delta_to_microseconds(delta); |
| 1621 | if (pyus_in == NULL) |
| 1622 | return NULL; |
| 1623 | |
| 1624 | pyus_out = PyNumber_FloorDivide(pyus_in, intobj); |
| 1625 | Py_DECREF(pyus_in); |
| 1626 | if (pyus_out == NULL) |
| 1627 | return NULL; |
| 1628 | |
| 1629 | result = microseconds_to_delta(pyus_out); |
| 1630 | Py_DECREF(pyus_out); |
| 1631 | return result; |
| 1632 | } |
| 1633 | |
| 1634 | static PyObject * |
| 1635 | delta_add(PyObject *left, PyObject *right) |
| 1636 | { |
| 1637 | PyObject *result = Py_NotImplemented; |
| 1638 | |
| 1639 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1640 | /* delta + delta */ |
| 1641 | /* The C-level additions can't overflow because of the |
| 1642 | * invariant bounds. |
| 1643 | */ |
| 1644 | int days = GET_TD_DAYS(left) + GET_TD_DAYS(right); |
| 1645 | int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right); |
| 1646 | int microseconds = GET_TD_MICROSECONDS(left) + |
| 1647 | GET_TD_MICROSECONDS(right); |
| 1648 | result = new_delta(days, seconds, microseconds, 1); |
| 1649 | } |
| 1650 | |
| 1651 | if (result == Py_NotImplemented) |
| 1652 | Py_INCREF(result); |
| 1653 | return result; |
| 1654 | } |
| 1655 | |
| 1656 | static PyObject * |
| 1657 | delta_negative(PyDateTime_Delta *self) |
| 1658 | { |
| 1659 | return new_delta(-GET_TD_DAYS(self), |
| 1660 | -GET_TD_SECONDS(self), |
| 1661 | -GET_TD_MICROSECONDS(self), |
| 1662 | 1); |
| 1663 | } |
| 1664 | |
| 1665 | static PyObject * |
| 1666 | delta_positive(PyDateTime_Delta *self) |
| 1667 | { |
| 1668 | /* Could optimize this (by returning self) if this isn't a |
| 1669 | * subclass -- but who uses unary + ? Approximately nobody. |
| 1670 | */ |
| 1671 | return new_delta(GET_TD_DAYS(self), |
| 1672 | GET_TD_SECONDS(self), |
| 1673 | GET_TD_MICROSECONDS(self), |
| 1674 | 0); |
| 1675 | } |
| 1676 | |
| 1677 | static PyObject * |
| 1678 | delta_abs(PyDateTime_Delta *self) |
| 1679 | { |
| 1680 | PyObject *result; |
| 1681 | |
| 1682 | assert(GET_TD_MICROSECONDS(self) >= 0); |
| 1683 | assert(GET_TD_SECONDS(self) >= 0); |
| 1684 | |
| 1685 | if (GET_TD_DAYS(self) < 0) |
| 1686 | result = delta_negative(self); |
| 1687 | else |
| 1688 | result = delta_positive(self); |
| 1689 | |
| 1690 | return result; |
| 1691 | } |
| 1692 | |
| 1693 | static PyObject * |
| 1694 | delta_subtract(PyObject *left, PyObject *right) |
| 1695 | { |
| 1696 | PyObject *result = Py_NotImplemented; |
| 1697 | |
| 1698 | if (PyDelta_Check(left) && PyDelta_Check(right)) { |
| 1699 | /* delta - delta */ |
| 1700 | PyObject *minus_right = PyNumber_Negative(right); |
| 1701 | if (minus_right) { |
| 1702 | result = delta_add(left, minus_right); |
| 1703 | Py_DECREF(minus_right); |
| 1704 | } |
| 1705 | else |
| 1706 | result = NULL; |
| 1707 | } |
| 1708 | |
| 1709 | if (result == Py_NotImplemented) |
| 1710 | Py_INCREF(result); |
| 1711 | return result; |
| 1712 | } |
| 1713 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1714 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1715 | delta_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1716 | { |
Tim Peters | aa7d849 | 2003-02-08 03:28:59 +0000 | [diff] [blame] | 1717 | if (PyDelta_Check(other)) { |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1718 | int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other); |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1719 | if (diff == 0) { |
| 1720 | diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other); |
| 1721 | if (diff == 0) |
| 1722 | diff = GET_TD_MICROSECONDS(self) - |
| 1723 | GET_TD_MICROSECONDS(other); |
| 1724 | } |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1725 | return diff_to_bool(diff, op); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1726 | } |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 1727 | else { |
| 1728 | Py_INCREF(Py_NotImplemented); |
| 1729 | return Py_NotImplemented; |
| 1730 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | static PyObject *delta_getstate(PyDateTime_Delta *self); |
| 1734 | |
| 1735 | static long |
| 1736 | delta_hash(PyDateTime_Delta *self) |
| 1737 | { |
| 1738 | if (self->hashcode == -1) { |
| 1739 | PyObject *temp = delta_getstate(self); |
| 1740 | if (temp != NULL) { |
| 1741 | self->hashcode = PyObject_Hash(temp); |
| 1742 | Py_DECREF(temp); |
| 1743 | } |
| 1744 | } |
| 1745 | return self->hashcode; |
| 1746 | } |
| 1747 | |
| 1748 | static PyObject * |
| 1749 | delta_multiply(PyObject *left, PyObject *right) |
| 1750 | { |
| 1751 | PyObject *result = Py_NotImplemented; |
| 1752 | |
| 1753 | if (PyDelta_Check(left)) { |
| 1754 | /* delta * ??? */ |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 1755 | if (PyLong_Check(right)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1756 | result = multiply_int_timedelta(right, |
| 1757 | (PyDateTime_Delta *) left); |
| 1758 | } |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 1759 | else if (PyLong_Check(left)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1760 | result = multiply_int_timedelta(left, |
| 1761 | (PyDateTime_Delta *) right); |
| 1762 | |
| 1763 | if (result == Py_NotImplemented) |
| 1764 | Py_INCREF(result); |
| 1765 | return result; |
| 1766 | } |
| 1767 | |
| 1768 | static PyObject * |
| 1769 | delta_divide(PyObject *left, PyObject *right) |
| 1770 | { |
| 1771 | PyObject *result = Py_NotImplemented; |
| 1772 | |
| 1773 | if (PyDelta_Check(left)) { |
| 1774 | /* delta * ??? */ |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 1775 | if (PyLong_Check(right)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1776 | result = divide_timedelta_int( |
| 1777 | (PyDateTime_Delta *)left, |
| 1778 | right); |
| 1779 | } |
| 1780 | |
| 1781 | if (result == Py_NotImplemented) |
| 1782 | Py_INCREF(result); |
| 1783 | return result; |
| 1784 | } |
| 1785 | |
| 1786 | /* Fold in the value of the tag ("seconds", "weeks", etc) component of a |
| 1787 | * timedelta constructor. sofar is the # of microseconds accounted for |
| 1788 | * so far, and there are factor microseconds per current unit, the number |
| 1789 | * of which is given by num. num * factor is added to sofar in a |
| 1790 | * numerically careful way, and that's the result. Any fractional |
| 1791 | * microseconds left over (this can happen if num is a float type) are |
| 1792 | * added into *leftover. |
| 1793 | * Note that there are many ways this can give an error (NULL) return. |
| 1794 | */ |
| 1795 | static PyObject * |
| 1796 | accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor, |
| 1797 | double *leftover) |
| 1798 | { |
| 1799 | PyObject *prod; |
| 1800 | PyObject *sum; |
| 1801 | |
| 1802 | assert(num != NULL); |
| 1803 | |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 1804 | if (PyLong_Check(num)) { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1805 | prod = PyNumber_Multiply(num, factor); |
| 1806 | if (prod == NULL) |
| 1807 | return NULL; |
| 1808 | sum = PyNumber_Add(sofar, prod); |
| 1809 | Py_DECREF(prod); |
| 1810 | return sum; |
| 1811 | } |
| 1812 | |
| 1813 | if (PyFloat_Check(num)) { |
| 1814 | double dnum; |
| 1815 | double fracpart; |
| 1816 | double intpart; |
| 1817 | PyObject *x; |
| 1818 | PyObject *y; |
| 1819 | |
| 1820 | /* The Plan: decompose num into an integer part and a |
| 1821 | * fractional part, num = intpart + fracpart. |
| 1822 | * Then num * factor == |
| 1823 | * intpart * factor + fracpart * factor |
| 1824 | * and the LHS can be computed exactly in long arithmetic. |
| 1825 | * The RHS is again broken into an int part and frac part. |
| 1826 | * and the frac part is added into *leftover. |
| 1827 | */ |
| 1828 | dnum = PyFloat_AsDouble(num); |
| 1829 | if (dnum == -1.0 && PyErr_Occurred()) |
| 1830 | return NULL; |
| 1831 | fracpart = modf(dnum, &intpart); |
| 1832 | x = PyLong_FromDouble(intpart); |
| 1833 | if (x == NULL) |
| 1834 | return NULL; |
| 1835 | |
| 1836 | prod = PyNumber_Multiply(x, factor); |
| 1837 | Py_DECREF(x); |
| 1838 | if (prod == NULL) |
| 1839 | return NULL; |
| 1840 | |
| 1841 | sum = PyNumber_Add(sofar, prod); |
| 1842 | Py_DECREF(prod); |
| 1843 | if (sum == NULL) |
| 1844 | return NULL; |
| 1845 | |
| 1846 | if (fracpart == 0.0) |
| 1847 | return sum; |
| 1848 | /* So far we've lost no information. Dealing with the |
| 1849 | * fractional part requires float arithmetic, and may |
| 1850 | * lose a little info. |
| 1851 | */ |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 1852 | assert(PyLong_Check(factor)); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1853 | dnum = PyLong_AsDouble(factor); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1854 | |
| 1855 | dnum *= fracpart; |
| 1856 | fracpart = modf(dnum, &intpart); |
| 1857 | x = PyLong_FromDouble(intpart); |
| 1858 | if (x == NULL) { |
| 1859 | Py_DECREF(sum); |
| 1860 | return NULL; |
| 1861 | } |
| 1862 | |
| 1863 | y = PyNumber_Add(sum, x); |
| 1864 | Py_DECREF(sum); |
| 1865 | Py_DECREF(x); |
| 1866 | *leftover += fracpart; |
| 1867 | return y; |
| 1868 | } |
| 1869 | |
| 1870 | PyErr_Format(PyExc_TypeError, |
| 1871 | "unsupported type for timedelta %s component: %s", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1872 | tag, Py_TYPE(num)->tp_name); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1873 | return NULL; |
| 1874 | } |
| 1875 | |
| 1876 | static PyObject * |
| 1877 | delta_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 1878 | { |
| 1879 | PyObject *self = NULL; |
| 1880 | |
| 1881 | /* Argument objects. */ |
| 1882 | PyObject *day = NULL; |
| 1883 | PyObject *second = NULL; |
| 1884 | PyObject *us = NULL; |
| 1885 | PyObject *ms = NULL; |
| 1886 | PyObject *minute = NULL; |
| 1887 | PyObject *hour = NULL; |
| 1888 | PyObject *week = NULL; |
| 1889 | |
| 1890 | PyObject *x = NULL; /* running sum of microseconds */ |
| 1891 | PyObject *y = NULL; /* temp sum of microseconds */ |
| 1892 | double leftover_us = 0.0; |
| 1893 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1894 | static char *keywords[] = { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1895 | "days", "seconds", "microseconds", "milliseconds", |
| 1896 | "minutes", "hours", "weeks", NULL |
| 1897 | }; |
| 1898 | |
| 1899 | if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__", |
| 1900 | keywords, |
| 1901 | &day, &second, &us, |
| 1902 | &ms, &minute, &hour, &week) == 0) |
| 1903 | goto Done; |
| 1904 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1905 | x = PyLong_FromLong(0); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1906 | if (x == NULL) |
| 1907 | goto Done; |
| 1908 | |
| 1909 | #define CLEANUP \ |
| 1910 | Py_DECREF(x); \ |
| 1911 | x = y; \ |
| 1912 | if (x == NULL) \ |
| 1913 | goto Done |
| 1914 | |
| 1915 | if (us) { |
| 1916 | y = accum("microseconds", x, us, us_per_us, &leftover_us); |
| 1917 | CLEANUP; |
| 1918 | } |
| 1919 | if (ms) { |
| 1920 | y = accum("milliseconds", x, ms, us_per_ms, &leftover_us); |
| 1921 | CLEANUP; |
| 1922 | } |
| 1923 | if (second) { |
| 1924 | y = accum("seconds", x, second, us_per_second, &leftover_us); |
| 1925 | CLEANUP; |
| 1926 | } |
| 1927 | if (minute) { |
| 1928 | y = accum("minutes", x, minute, us_per_minute, &leftover_us); |
| 1929 | CLEANUP; |
| 1930 | } |
| 1931 | if (hour) { |
| 1932 | y = accum("hours", x, hour, us_per_hour, &leftover_us); |
| 1933 | CLEANUP; |
| 1934 | } |
| 1935 | if (day) { |
| 1936 | y = accum("days", x, day, us_per_day, &leftover_us); |
| 1937 | CLEANUP; |
| 1938 | } |
| 1939 | if (week) { |
| 1940 | y = accum("weeks", x, week, us_per_week, &leftover_us); |
| 1941 | CLEANUP; |
| 1942 | } |
| 1943 | if (leftover_us) { |
| 1944 | /* Round to nearest whole # of us, and add into x. */ |
Tim Peters | 5d644dd | 2003-01-02 16:32:54 +0000 | [diff] [blame] | 1945 | PyObject *temp = PyLong_FromLong(round_to_long(leftover_us)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1946 | if (temp == NULL) { |
| 1947 | Py_DECREF(x); |
| 1948 | goto Done; |
| 1949 | } |
| 1950 | y = PyNumber_Add(x, temp); |
| 1951 | Py_DECREF(temp); |
| 1952 | CLEANUP; |
| 1953 | } |
| 1954 | |
Tim Peters | b0c854d | 2003-05-17 15:57:00 +0000 | [diff] [blame] | 1955 | self = microseconds_to_delta_ex(x, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1956 | Py_DECREF(x); |
| 1957 | Done: |
| 1958 | return self; |
| 1959 | |
| 1960 | #undef CLEANUP |
| 1961 | } |
| 1962 | |
| 1963 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 1964 | delta_bool(PyDateTime_Delta *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1965 | { |
| 1966 | return (GET_TD_DAYS(self) != 0 |
| 1967 | || GET_TD_SECONDS(self) != 0 |
| 1968 | || GET_TD_MICROSECONDS(self) != 0); |
| 1969 | } |
| 1970 | |
| 1971 | static PyObject * |
| 1972 | delta_repr(PyDateTime_Delta *self) |
| 1973 | { |
| 1974 | if (GET_TD_MICROSECONDS(self) != 0) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1975 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1976 | Py_TYPE(self)->tp_name, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1977 | GET_TD_DAYS(self), |
| 1978 | GET_TD_SECONDS(self), |
| 1979 | GET_TD_MICROSECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1980 | if (GET_TD_SECONDS(self) != 0) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1981 | return PyUnicode_FromFormat("%s(%d, %d)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1982 | Py_TYPE(self)->tp_name, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1983 | GET_TD_DAYS(self), |
| 1984 | GET_TD_SECONDS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1985 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1986 | return PyUnicode_FromFormat("%s(%d)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1987 | Py_TYPE(self)->tp_name, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1988 | GET_TD_DAYS(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | static PyObject * |
| 1992 | delta_str(PyDateTime_Delta *self) |
| 1993 | { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1994 | int us = GET_TD_MICROSECONDS(self); |
Walter Dörwald | baf853c | 2007-05-31 18:42:47 +0000 | [diff] [blame] | 1995 | int seconds = GET_TD_SECONDS(self); |
| 1996 | int minutes = divmod(seconds, 60, &seconds); |
| 1997 | int hours = divmod(minutes, 60, &minutes); |
| 1998 | int days = GET_TD_DAYS(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 1999 | |
| 2000 | if (days) { |
Walter Dörwald | baf853c | 2007-05-31 18:42:47 +0000 | [diff] [blame] | 2001 | if (us) |
| 2002 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d", |
| 2003 | days, (days == 1 || days == -1) ? "" : "s", |
| 2004 | hours, minutes, seconds, us); |
| 2005 | else |
| 2006 | return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d", |
| 2007 | days, (days == 1 || days == -1) ? "" : "s", |
| 2008 | hours, minutes, seconds); |
| 2009 | } else { |
| 2010 | if (us) |
| 2011 | return PyUnicode_FromFormat("%d:%02d:%02d.%06d", |
| 2012 | hours, minutes, seconds, us); |
| 2013 | else |
| 2014 | return PyUnicode_FromFormat("%d:%02d:%02d", |
| 2015 | hours, minutes, seconds); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2020 | /* Pickle support, a simple use of __reduce__. */ |
| 2021 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2022 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2023 | static PyObject * |
| 2024 | delta_getstate(PyDateTime_Delta *self) |
| 2025 | { |
| 2026 | return Py_BuildValue("iii", GET_TD_DAYS(self), |
| 2027 | GET_TD_SECONDS(self), |
| 2028 | GET_TD_MICROSECONDS(self)); |
| 2029 | } |
| 2030 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2031 | static PyObject * |
| 2032 | delta_reduce(PyDateTime_Delta* self) |
| 2033 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2034 | return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | #define OFFSET(field) offsetof(PyDateTime_Delta, field) |
| 2038 | |
| 2039 | static PyMemberDef delta_members[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2040 | |
Neal Norwitz | dfb8086 | 2002-12-19 02:30:56 +0000 | [diff] [blame] | 2041 | {"days", T_INT, OFFSET(days), READONLY, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2042 | PyDoc_STR("Number of days.")}, |
| 2043 | |
Neal Norwitz | dfb8086 | 2002-12-19 02:30:56 +0000 | [diff] [blame] | 2044 | {"seconds", T_INT, OFFSET(seconds), READONLY, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2045 | PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")}, |
| 2046 | |
Neal Norwitz | dfb8086 | 2002-12-19 02:30:56 +0000 | [diff] [blame] | 2047 | {"microseconds", T_INT, OFFSET(microseconds), READONLY, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2048 | PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")}, |
| 2049 | {NULL} |
| 2050 | }; |
| 2051 | |
| 2052 | static PyMethodDef delta_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2053 | {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS, |
| 2054 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 2055 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2056 | {NULL, NULL}, |
| 2057 | }; |
| 2058 | |
| 2059 | static char delta_doc[] = |
| 2060 | PyDoc_STR("Difference between two datetime values."); |
| 2061 | |
| 2062 | static PyNumberMethods delta_as_number = { |
| 2063 | delta_add, /* nb_add */ |
| 2064 | delta_subtract, /* nb_subtract */ |
| 2065 | delta_multiply, /* nb_multiply */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2066 | 0, /* nb_remainder */ |
| 2067 | 0, /* nb_divmod */ |
| 2068 | 0, /* nb_power */ |
| 2069 | (unaryfunc)delta_negative, /* nb_negative */ |
| 2070 | (unaryfunc)delta_positive, /* nb_positive */ |
| 2071 | (unaryfunc)delta_abs, /* nb_absolute */ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2072 | (inquiry)delta_bool, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2073 | 0, /*nb_invert*/ |
| 2074 | 0, /*nb_lshift*/ |
| 2075 | 0, /*nb_rshift*/ |
| 2076 | 0, /*nb_and*/ |
| 2077 | 0, /*nb_xor*/ |
| 2078 | 0, /*nb_or*/ |
Neil Schemenauer | 16c7075 | 2007-09-21 20:19:23 +0000 | [diff] [blame] | 2079 | 0, /*nb_reserved*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2080 | 0, /*nb_int*/ |
| 2081 | 0, /*nb_long*/ |
| 2082 | 0, /*nb_float*/ |
| 2083 | 0, /*nb_oct*/ |
| 2084 | 0, /*nb_hex*/ |
| 2085 | 0, /*nb_inplace_add*/ |
| 2086 | 0, /*nb_inplace_subtract*/ |
| 2087 | 0, /*nb_inplace_multiply*/ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2088 | 0, /*nb_inplace_remainder*/ |
| 2089 | 0, /*nb_inplace_power*/ |
| 2090 | 0, /*nb_inplace_lshift*/ |
| 2091 | 0, /*nb_inplace_rshift*/ |
| 2092 | 0, /*nb_inplace_and*/ |
| 2093 | 0, /*nb_inplace_xor*/ |
| 2094 | 0, /*nb_inplace_or*/ |
| 2095 | delta_divide, /* nb_floor_divide */ |
| 2096 | 0, /* nb_true_divide */ |
| 2097 | 0, /* nb_inplace_floor_divide */ |
| 2098 | 0, /* nb_inplace_true_divide */ |
| 2099 | }; |
| 2100 | |
| 2101 | static PyTypeObject PyDateTime_DeltaType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2102 | PyVarObject_HEAD_INIT(NULL, 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2103 | "datetime.timedelta", /* tp_name */ |
| 2104 | sizeof(PyDateTime_Delta), /* tp_basicsize */ |
| 2105 | 0, /* tp_itemsize */ |
| 2106 | 0, /* tp_dealloc */ |
| 2107 | 0, /* tp_print */ |
| 2108 | 0, /* tp_getattr */ |
| 2109 | 0, /* tp_setattr */ |
| 2110 | 0, /* tp_compare */ |
| 2111 | (reprfunc)delta_repr, /* tp_repr */ |
| 2112 | &delta_as_number, /* tp_as_number */ |
| 2113 | 0, /* tp_as_sequence */ |
| 2114 | 0, /* tp_as_mapping */ |
| 2115 | (hashfunc)delta_hash, /* tp_hash */ |
| 2116 | 0, /* tp_call */ |
| 2117 | (reprfunc)delta_str, /* tp_str */ |
| 2118 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2119 | 0, /* tp_setattro */ |
| 2120 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 2121 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2122 | delta_doc, /* tp_doc */ |
| 2123 | 0, /* tp_traverse */ |
| 2124 | 0, /* tp_clear */ |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2125 | delta_richcompare, /* tp_richcompare */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2126 | 0, /* tp_weaklistoffset */ |
| 2127 | 0, /* tp_iter */ |
| 2128 | 0, /* tp_iternext */ |
| 2129 | delta_methods, /* tp_methods */ |
| 2130 | delta_members, /* tp_members */ |
| 2131 | 0, /* tp_getset */ |
| 2132 | 0, /* tp_base */ |
| 2133 | 0, /* tp_dict */ |
| 2134 | 0, /* tp_descr_get */ |
| 2135 | 0, /* tp_descr_set */ |
| 2136 | 0, /* tp_dictoffset */ |
| 2137 | 0, /* tp_init */ |
| 2138 | 0, /* tp_alloc */ |
| 2139 | delta_new, /* tp_new */ |
Tim Peters | 4c53013 | 2003-05-16 22:44:06 +0000 | [diff] [blame] | 2140 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2141 | }; |
| 2142 | |
| 2143 | /* |
| 2144 | * PyDateTime_Date implementation. |
| 2145 | */ |
| 2146 | |
| 2147 | /* Accessor properties. */ |
| 2148 | |
| 2149 | static PyObject * |
| 2150 | date_year(PyDateTime_Date *self, void *unused) |
| 2151 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2152 | return PyLong_FromLong(GET_YEAR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | static PyObject * |
| 2156 | date_month(PyDateTime_Date *self, void *unused) |
| 2157 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2158 | return PyLong_FromLong(GET_MONTH(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | static PyObject * |
| 2162 | date_day(PyDateTime_Date *self, void *unused) |
| 2163 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2164 | return PyLong_FromLong(GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | static PyGetSetDef date_getset[] = { |
| 2168 | {"year", (getter)date_year}, |
| 2169 | {"month", (getter)date_month}, |
| 2170 | {"day", (getter)date_day}, |
| 2171 | {NULL} |
| 2172 | }; |
| 2173 | |
| 2174 | /* Constructors. */ |
| 2175 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2176 | static char *date_kws[] = {"year", "month", "day", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2177 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2178 | static PyObject * |
| 2179 | date_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 2180 | { |
| 2181 | PyObject *self = NULL; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2182 | PyObject *state; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2183 | int year; |
| 2184 | int month; |
| 2185 | int day; |
| 2186 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2187 | /* Check for invocation from pickle with __getstate__ state */ |
| 2188 | if (PyTuple_GET_SIZE(args) == 1 && |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2189 | PyString_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 2190 | PyString_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE && |
| 2191 | MONTH_IS_SANE(PyString_AS_STRING(state)[2])) |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2192 | { |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2193 | PyDateTime_Date *me; |
| 2194 | |
Tim Peters | 604c013 | 2004-06-07 23:04:33 +0000 | [diff] [blame] | 2195 | me = (PyDateTime_Date *) (type->tp_alloc(type, 0)); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2196 | if (me != NULL) { |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2197 | char *pdata = PyString_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2198 | memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE); |
| 2199 | me->hashcode = -1; |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2200 | } |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 2201 | return (PyObject *)me; |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2204 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2205 | &year, &month, &day)) { |
| 2206 | if (check_date_args(year, month, day) < 0) |
| 2207 | return NULL; |
Guido van Rossum | 8b7a9a3 | 2003-04-14 22:01:58 +0000 | [diff] [blame] | 2208 | self = new_date_ex(year, month, day, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2209 | } |
| 2210 | return self; |
| 2211 | } |
| 2212 | |
| 2213 | /* Return new date from localtime(t). */ |
| 2214 | static PyObject * |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 2215 | date_local_from_time_t(PyObject *cls, double ts) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2216 | { |
| 2217 | struct tm *tm; |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 2218 | time_t t; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2219 | PyObject *result = NULL; |
| 2220 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 2221 | t = _PyTime_DoubleToTimet(ts); |
| 2222 | if (t == (time_t)-1 && PyErr_Occurred()) |
| 2223 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2224 | tm = localtime(&t); |
| 2225 | if (tm) |
| 2226 | result = PyObject_CallFunction(cls, "iii", |
| 2227 | tm->tm_year + 1900, |
| 2228 | tm->tm_mon + 1, |
| 2229 | tm->tm_mday); |
| 2230 | else |
| 2231 | PyErr_SetString(PyExc_ValueError, |
| 2232 | "timestamp out of range for " |
| 2233 | "platform localtime() function"); |
| 2234 | return result; |
| 2235 | } |
| 2236 | |
| 2237 | /* Return new date from current time. |
| 2238 | * We say this is equivalent to fromtimestamp(time.time()), and the |
| 2239 | * only way to be sure of that is to *call* time.time(). That's not |
| 2240 | * generally the same as calling C's time. |
| 2241 | */ |
| 2242 | static PyObject * |
| 2243 | date_today(PyObject *cls, PyObject *dummy) |
| 2244 | { |
| 2245 | PyObject *time; |
| 2246 | PyObject *result; |
| 2247 | |
| 2248 | time = time_time(); |
| 2249 | if (time == NULL) |
| 2250 | return NULL; |
| 2251 | |
| 2252 | /* Note well: today() is a class method, so this may not call |
| 2253 | * date.fromtimestamp. For example, it may call |
| 2254 | * datetime.fromtimestamp. That's why we need all the accuracy |
| 2255 | * time.time() delivers; if someone were gonzo about optimization, |
| 2256 | * date.today() could get away with plain C time(). |
| 2257 | */ |
| 2258 | result = PyObject_CallMethod(cls, "fromtimestamp", "O", time); |
| 2259 | Py_DECREF(time); |
| 2260 | return result; |
| 2261 | } |
| 2262 | |
| 2263 | /* Return new date from given timestamp (Python timestamp -- a double). */ |
| 2264 | static PyObject * |
| 2265 | date_fromtimestamp(PyObject *cls, PyObject *args) |
| 2266 | { |
| 2267 | double timestamp; |
| 2268 | PyObject *result = NULL; |
| 2269 | |
| 2270 | if (PyArg_ParseTuple(args, "d:fromtimestamp", ×tamp)) |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 2271 | result = date_local_from_time_t(cls, timestamp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2272 | return result; |
| 2273 | } |
| 2274 | |
| 2275 | /* Return new date from proleptic Gregorian ordinal. Raises ValueError if |
| 2276 | * the ordinal is out of range. |
| 2277 | */ |
| 2278 | static PyObject * |
| 2279 | date_fromordinal(PyObject *cls, PyObject *args) |
| 2280 | { |
| 2281 | PyObject *result = NULL; |
| 2282 | int ordinal; |
| 2283 | |
| 2284 | if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) { |
| 2285 | int year; |
| 2286 | int month; |
| 2287 | int day; |
| 2288 | |
| 2289 | if (ordinal < 1) |
| 2290 | PyErr_SetString(PyExc_ValueError, "ordinal must be " |
| 2291 | ">= 1"); |
| 2292 | else { |
| 2293 | ord_to_ymd(ordinal, &year, &month, &day); |
| 2294 | result = PyObject_CallFunction(cls, "iii", |
| 2295 | year, month, day); |
| 2296 | } |
| 2297 | } |
| 2298 | return result; |
| 2299 | } |
| 2300 | |
| 2301 | /* |
| 2302 | * Date arithmetic. |
| 2303 | */ |
| 2304 | |
| 2305 | /* date + timedelta -> date. If arg negate is true, subtract the timedelta |
| 2306 | * instead. |
| 2307 | */ |
| 2308 | static PyObject * |
| 2309 | add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate) |
| 2310 | { |
| 2311 | PyObject *result = NULL; |
| 2312 | int year = GET_YEAR(date); |
| 2313 | int month = GET_MONTH(date); |
| 2314 | int deltadays = GET_TD_DAYS(delta); |
| 2315 | /* C-level overflow is impossible because |deltadays| < 1e9. */ |
| 2316 | int day = GET_DAY(date) + (negate ? -deltadays : deltadays); |
| 2317 | |
| 2318 | if (normalize_date(&year, &month, &day) >= 0) |
| 2319 | result = new_date(year, month, day); |
| 2320 | return result; |
| 2321 | } |
| 2322 | |
| 2323 | static PyObject * |
| 2324 | date_add(PyObject *left, PyObject *right) |
| 2325 | { |
| 2326 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) { |
| 2327 | Py_INCREF(Py_NotImplemented); |
| 2328 | return Py_NotImplemented; |
| 2329 | } |
Tim Peters | aa7d849 | 2003-02-08 03:28:59 +0000 | [diff] [blame] | 2330 | if (PyDate_Check(left)) { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2331 | /* date + ??? */ |
| 2332 | if (PyDelta_Check(right)) |
| 2333 | /* date + delta */ |
| 2334 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2335 | (PyDateTime_Delta *) right, |
| 2336 | 0); |
| 2337 | } |
| 2338 | else { |
| 2339 | /* ??? + date |
| 2340 | * 'right' must be one of us, or we wouldn't have been called |
| 2341 | */ |
| 2342 | if (PyDelta_Check(left)) |
| 2343 | /* delta + date */ |
| 2344 | return add_date_timedelta((PyDateTime_Date *) right, |
| 2345 | (PyDateTime_Delta *) left, |
| 2346 | 0); |
| 2347 | } |
| 2348 | Py_INCREF(Py_NotImplemented); |
| 2349 | return Py_NotImplemented; |
| 2350 | } |
| 2351 | |
| 2352 | static PyObject * |
| 2353 | date_subtract(PyObject *left, PyObject *right) |
| 2354 | { |
| 2355 | if (PyDateTime_Check(left) || PyDateTime_Check(right)) { |
| 2356 | Py_INCREF(Py_NotImplemented); |
| 2357 | return Py_NotImplemented; |
| 2358 | } |
Tim Peters | aa7d849 | 2003-02-08 03:28:59 +0000 | [diff] [blame] | 2359 | if (PyDate_Check(left)) { |
| 2360 | if (PyDate_Check(right)) { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2361 | /* date - date */ |
| 2362 | int left_ord = ymd_to_ord(GET_YEAR(left), |
| 2363 | GET_MONTH(left), |
| 2364 | GET_DAY(left)); |
| 2365 | int right_ord = ymd_to_ord(GET_YEAR(right), |
| 2366 | GET_MONTH(right), |
| 2367 | GET_DAY(right)); |
| 2368 | return new_delta(left_ord - right_ord, 0, 0, 0); |
| 2369 | } |
| 2370 | if (PyDelta_Check(right)) { |
| 2371 | /* date - delta */ |
| 2372 | return add_date_timedelta((PyDateTime_Date *) left, |
| 2373 | (PyDateTime_Delta *) right, |
| 2374 | 1); |
| 2375 | } |
| 2376 | } |
| 2377 | Py_INCREF(Py_NotImplemented); |
| 2378 | return Py_NotImplemented; |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | /* Various ways to turn a date into a string. */ |
| 2383 | |
| 2384 | static PyObject * |
| 2385 | date_repr(PyDateTime_Date *self) |
| 2386 | { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 2387 | return PyUnicode_FromFormat("%s(%d, %d, %d)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2388 | Py_TYPE(self)->tp_name, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 2389 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | static PyObject * |
| 2393 | date_isoformat(PyDateTime_Date *self) |
| 2394 | { |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 2395 | return PyUnicode_FromFormat("%04d-%02d-%02d", |
| 2396 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Tim Peters | e2df5ff | 2003-05-02 18:39:55 +0000 | [diff] [blame] | 2399 | /* str() calls the appropriate isoformat() method. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2400 | static PyObject * |
| 2401 | date_str(PyDateTime_Date *self) |
| 2402 | { |
| 2403 | return PyObject_CallMethod((PyObject *)self, "isoformat", "()"); |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | static PyObject * |
| 2408 | date_ctime(PyDateTime_Date *self) |
| 2409 | { |
| 2410 | return format_ctime(self, 0, 0, 0); |
| 2411 | } |
| 2412 | |
| 2413 | static PyObject * |
| 2414 | date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2415 | { |
| 2416 | /* This method can be inherited, and needs to call the |
| 2417 | * timetuple() method appropriate to self's class. |
| 2418 | */ |
| 2419 | PyObject *result; |
| 2420 | PyObject *format; |
| 2421 | PyObject *tuple; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2422 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2423 | |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2424 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 2425 | &format)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2426 | return NULL; |
| 2427 | |
| 2428 | tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()"); |
| 2429 | if (tuple == NULL) |
| 2430 | return NULL; |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 2431 | result = wrap_strftime((PyObject *)self, format, tuple, |
| 2432 | (PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2433 | Py_DECREF(tuple); |
| 2434 | return result; |
| 2435 | } |
| 2436 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2437 | static PyObject * |
| 2438 | date_format(PyDateTime_Date *self, PyObject *args) |
| 2439 | { |
| 2440 | PyObject *format; |
| 2441 | |
| 2442 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 2443 | return NULL; |
| 2444 | |
| 2445 | /* if the format is zero length, return str(self) */ |
| 2446 | if (PyUnicode_GetSize(format) == 0) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2447 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2448 | |
| 2449 | return PyObject_CallMethod((PyObject *)self, "strftime", "O", format); |
| 2450 | } |
| 2451 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2452 | /* ISO methods. */ |
| 2453 | |
| 2454 | static PyObject * |
| 2455 | date_isoweekday(PyDateTime_Date *self) |
| 2456 | { |
| 2457 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
| 2458 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2459 | return PyLong_FromLong(dow + 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | static PyObject * |
| 2463 | date_isocalendar(PyDateTime_Date *self) |
| 2464 | { |
| 2465 | int year = GET_YEAR(self); |
| 2466 | int week1_monday = iso_week1_monday(year); |
| 2467 | int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self)); |
| 2468 | int week; |
| 2469 | int day; |
| 2470 | |
| 2471 | week = divmod(today - week1_monday, 7, &day); |
| 2472 | if (week < 0) { |
| 2473 | --year; |
| 2474 | week1_monday = iso_week1_monday(year); |
| 2475 | week = divmod(today - week1_monday, 7, &day); |
| 2476 | } |
| 2477 | else if (week >= 52 && today >= iso_week1_monday(year + 1)) { |
| 2478 | ++year; |
| 2479 | week = 0; |
| 2480 | } |
| 2481 | return Py_BuildValue("iii", year, week + 1, day + 1); |
| 2482 | } |
| 2483 | |
| 2484 | /* Miscellaneous methods. */ |
| 2485 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2486 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2487 | date_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2488 | { |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2489 | if (PyDate_Check(other)) { |
| 2490 | int diff = memcmp(((PyDateTime_Date *)self)->data, |
| 2491 | ((PyDateTime_Date *)other)->data, |
| 2492 | _PyDateTime_DATE_DATASIZE); |
| 2493 | return diff_to_bool(diff, op); |
| 2494 | } |
| 2495 | else { |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 2496 | Py_INCREF(Py_NotImplemented); |
| 2497 | return Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2498 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | static PyObject * |
| 2502 | date_timetuple(PyDateTime_Date *self) |
| 2503 | { |
| 2504 | return build_struct_time(GET_YEAR(self), |
| 2505 | GET_MONTH(self), |
| 2506 | GET_DAY(self), |
| 2507 | 0, 0, 0, -1); |
| 2508 | } |
| 2509 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2510 | static PyObject * |
| 2511 | date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw) |
| 2512 | { |
| 2513 | PyObject *clone; |
| 2514 | PyObject *tuple; |
| 2515 | int year = GET_YEAR(self); |
| 2516 | int month = GET_MONTH(self); |
| 2517 | int day = GET_DAY(self); |
| 2518 | |
| 2519 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws, |
| 2520 | &year, &month, &day)) |
| 2521 | return NULL; |
| 2522 | tuple = Py_BuildValue("iii", year, month, day); |
| 2523 | if (tuple == NULL) |
| 2524 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2525 | clone = date_new(Py_TYPE(self), tuple, NULL); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2526 | Py_DECREF(tuple); |
| 2527 | return clone; |
| 2528 | } |
| 2529 | |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2530 | /* |
| 2531 | Borrowed from stringobject.c, originally it was string_hash() |
| 2532 | */ |
| 2533 | static long |
| 2534 | generic_hash(unsigned char *data, int len) |
| 2535 | { |
| 2536 | register unsigned char *p; |
| 2537 | register long x; |
| 2538 | |
| 2539 | p = (unsigned char *) data; |
| 2540 | x = *p << 7; |
| 2541 | while (--len >= 0) |
| 2542 | x = (1000003*x) ^ *p++; |
| 2543 | x ^= len; |
| 2544 | if (x == -1) |
| 2545 | x = -2; |
| 2546 | |
| 2547 | return x; |
| 2548 | } |
| 2549 | |
| 2550 | |
| 2551 | static PyObject *date_getstate(PyDateTime_Date *self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2552 | |
| 2553 | static long |
| 2554 | date_hash(PyDateTime_Date *self) |
| 2555 | { |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2556 | if (self->hashcode == -1) |
| 2557 | self->hashcode = generic_hash( |
| 2558 | (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE); |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2559 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2560 | return self->hashcode; |
| 2561 | } |
| 2562 | |
| 2563 | static PyObject * |
| 2564 | date_toordinal(PyDateTime_Date *self) |
| 2565 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2566 | return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self), |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2567 | GET_DAY(self))); |
| 2568 | } |
| 2569 | |
| 2570 | static PyObject * |
| 2571 | date_weekday(PyDateTime_Date *self) |
| 2572 | { |
| 2573 | int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); |
| 2574 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2575 | return PyLong_FromLong(dow); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 2578 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2579 | |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 2580 | /* __getstate__ isn't exposed */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2581 | static PyObject * |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 2582 | date_getstate(PyDateTime_Date *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2583 | { |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 2584 | PyObject* field; |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 2585 | field = PyString_FromStringAndSize((char*)self->data, |
| 2586 | _PyDateTime_DATE_DATASIZE); |
Martin v. Löwis | 10a60b3 | 2007-07-18 02:28:27 +0000 | [diff] [blame] | 2587 | return Py_BuildValue("(N)", field); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2591 | date_reduce(PyDateTime_Date *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2592 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2593 | return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | static PyMethodDef date_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2597 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2598 | /* Class methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2599 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2600 | {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS | |
| 2601 | METH_CLASS, |
| 2602 | PyDoc_STR("timestamp -> local date from a POSIX timestamp (like " |
| 2603 | "time.time()).")}, |
| 2604 | |
| 2605 | {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS | |
| 2606 | METH_CLASS, |
| 2607 | PyDoc_STR("int -> date corresponding to a proleptic Gregorian " |
| 2608 | "ordinal.")}, |
| 2609 | |
| 2610 | {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS, |
| 2611 | PyDoc_STR("Current date or datetime: same as " |
| 2612 | "self.__class__.fromtimestamp(time.time()).")}, |
| 2613 | |
| 2614 | /* Instance methods: */ |
| 2615 | |
| 2616 | {"ctime", (PyCFunction)date_ctime, METH_NOARGS, |
| 2617 | PyDoc_STR("Return ctime() style string.")}, |
| 2618 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2619 | {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2620 | PyDoc_STR("format -> strftime() style string.")}, |
| 2621 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 2622 | {"__format__", (PyCFunction)date_format, METH_VARARGS, |
| 2623 | PyDoc_STR("Formats self with strftime.")}, |
| 2624 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2625 | {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, |
| 2626 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
| 2627 | |
| 2628 | {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS, |
| 2629 | PyDoc_STR("Return a 3-tuple containing ISO year, week number, and " |
| 2630 | "weekday.")}, |
| 2631 | |
| 2632 | {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS, |
| 2633 | PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")}, |
| 2634 | |
| 2635 | {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS, |
| 2636 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2637 | "Monday == 1 ... Sunday == 7")}, |
| 2638 | |
| 2639 | {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS, |
| 2640 | PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year " |
| 2641 | "1 is day 1.")}, |
| 2642 | |
| 2643 | {"weekday", (PyCFunction)date_weekday, METH_NOARGS, |
| 2644 | PyDoc_STR("Return the day of the week represented by the date.\n" |
| 2645 | "Monday == 0 ... Sunday == 6")}, |
| 2646 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2647 | {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 2648 | PyDoc_STR("Return date with new specified fields.")}, |
| 2649 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2650 | {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, |
| 2651 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 2652 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2653 | {NULL, NULL} |
| 2654 | }; |
| 2655 | |
| 2656 | static char date_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 2657 | PyDoc_STR("date(year, month, day) --> date object"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2658 | |
| 2659 | static PyNumberMethods date_as_number = { |
| 2660 | date_add, /* nb_add */ |
| 2661 | date_subtract, /* nb_subtract */ |
| 2662 | 0, /* nb_multiply */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2663 | 0, /* nb_remainder */ |
| 2664 | 0, /* nb_divmod */ |
| 2665 | 0, /* nb_power */ |
| 2666 | 0, /* nb_negative */ |
| 2667 | 0, /* nb_positive */ |
| 2668 | 0, /* nb_absolute */ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 2669 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2670 | }; |
| 2671 | |
| 2672 | static PyTypeObject PyDateTime_DateType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2673 | PyVarObject_HEAD_INIT(NULL, 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2674 | "datetime.date", /* tp_name */ |
| 2675 | sizeof(PyDateTime_Date), /* tp_basicsize */ |
| 2676 | 0, /* tp_itemsize */ |
Guido van Rossum | 8b7a9a3 | 2003-04-14 22:01:58 +0000 | [diff] [blame] | 2677 | 0, /* tp_dealloc */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2678 | 0, /* tp_print */ |
| 2679 | 0, /* tp_getattr */ |
| 2680 | 0, /* tp_setattr */ |
| 2681 | 0, /* tp_compare */ |
| 2682 | (reprfunc)date_repr, /* tp_repr */ |
| 2683 | &date_as_number, /* tp_as_number */ |
| 2684 | 0, /* tp_as_sequence */ |
| 2685 | 0, /* tp_as_mapping */ |
| 2686 | (hashfunc)date_hash, /* tp_hash */ |
| 2687 | 0, /* tp_call */ |
| 2688 | (reprfunc)date_str, /* tp_str */ |
| 2689 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2690 | 0, /* tp_setattro */ |
| 2691 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 2692 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2693 | date_doc, /* tp_doc */ |
| 2694 | 0, /* tp_traverse */ |
| 2695 | 0, /* tp_clear */ |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 2696 | date_richcompare, /* tp_richcompare */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2697 | 0, /* tp_weaklistoffset */ |
| 2698 | 0, /* tp_iter */ |
| 2699 | 0, /* tp_iternext */ |
| 2700 | date_methods, /* tp_methods */ |
| 2701 | 0, /* tp_members */ |
| 2702 | date_getset, /* tp_getset */ |
| 2703 | 0, /* tp_base */ |
| 2704 | 0, /* tp_dict */ |
| 2705 | 0, /* tp_descr_get */ |
| 2706 | 0, /* tp_descr_set */ |
| 2707 | 0, /* tp_dictoffset */ |
| 2708 | 0, /* tp_init */ |
| 2709 | 0, /* tp_alloc */ |
| 2710 | date_new, /* tp_new */ |
Tim Peters | 4c53013 | 2003-05-16 22:44:06 +0000 | [diff] [blame] | 2711 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2712 | }; |
| 2713 | |
| 2714 | /* |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2715 | * PyDateTime_TZInfo implementation. |
| 2716 | */ |
| 2717 | |
| 2718 | /* This is a pure abstract base class, so doesn't do anything beyond |
| 2719 | * raising NotImplemented exceptions. Real tzinfo classes need |
| 2720 | * 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] | 2721 | * datetime and time constructors (their tzinfo arguments need to |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2722 | * be subclasses of this tzinfo class, which is easy and quick to check). |
| 2723 | * |
| 2724 | * Note: For reasons having to do with pickling of subclasses, we have |
| 2725 | * to allow tzinfo objects to be instantiated. This wasn't an issue |
| 2726 | * in the Python implementation (__init__() could raise NotImplementedError |
| 2727 | * there without ill effect), but doing so in the C implementation hit a |
| 2728 | * brick wall. |
| 2729 | */ |
| 2730 | |
| 2731 | static PyObject * |
| 2732 | tzinfo_nogo(const char* methodname) |
| 2733 | { |
| 2734 | PyErr_Format(PyExc_NotImplementedError, |
| 2735 | "a tzinfo subclass must implement %s()", |
| 2736 | methodname); |
| 2737 | return NULL; |
| 2738 | } |
| 2739 | |
| 2740 | /* Methods. A subclass must implement these. */ |
| 2741 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2742 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2743 | tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt) |
| 2744 | { |
| 2745 | return tzinfo_nogo("tzname"); |
| 2746 | } |
| 2747 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2748 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2749 | tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt) |
| 2750 | { |
| 2751 | return tzinfo_nogo("utcoffset"); |
| 2752 | } |
| 2753 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2754 | static PyObject * |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2755 | tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt) |
| 2756 | { |
| 2757 | return tzinfo_nogo("dst"); |
| 2758 | } |
| 2759 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2760 | static PyObject * |
| 2761 | tzinfo_fromutc(PyDateTime_TZInfo *self, PyDateTime_DateTime *dt) |
| 2762 | { |
| 2763 | int y, m, d, hh, mm, ss, us; |
| 2764 | |
| 2765 | PyObject *result; |
| 2766 | int off, dst; |
| 2767 | int none; |
| 2768 | int delta; |
| 2769 | |
| 2770 | if (! PyDateTime_Check(dt)) { |
| 2771 | PyErr_SetString(PyExc_TypeError, |
| 2772 | "fromutc: argument must be a datetime"); |
| 2773 | return NULL; |
| 2774 | } |
| 2775 | if (! HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) { |
| 2776 | PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo " |
| 2777 | "is not self"); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
| 2781 | off = call_utcoffset(dt->tzinfo, (PyObject *)dt, &none); |
| 2782 | if (off == -1 && PyErr_Occurred()) |
| 2783 | return NULL; |
| 2784 | if (none) { |
| 2785 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 2786 | "utcoffset() result required"); |
| 2787 | return NULL; |
| 2788 | } |
| 2789 | |
| 2790 | dst = call_dst(dt->tzinfo, (PyObject *)dt, &none); |
| 2791 | if (dst == -1 && PyErr_Occurred()) |
| 2792 | return NULL; |
| 2793 | if (none) { |
| 2794 | PyErr_SetString(PyExc_ValueError, "fromutc: non-None " |
| 2795 | "dst() result required"); |
| 2796 | return NULL; |
| 2797 | } |
| 2798 | |
| 2799 | y = GET_YEAR(dt); |
| 2800 | m = GET_MONTH(dt); |
| 2801 | d = GET_DAY(dt); |
| 2802 | hh = DATE_GET_HOUR(dt); |
| 2803 | mm = DATE_GET_MINUTE(dt); |
| 2804 | ss = DATE_GET_SECOND(dt); |
| 2805 | us = DATE_GET_MICROSECOND(dt); |
| 2806 | |
| 2807 | delta = off - dst; |
| 2808 | mm += delta; |
| 2809 | if ((mm < 0 || mm >= 60) && |
| 2810 | normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
Tim Peters | b1049e8 | 2003-01-23 17:20:36 +0000 | [diff] [blame] | 2811 | return NULL; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2812 | result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo); |
| 2813 | if (result == NULL) |
| 2814 | return result; |
| 2815 | |
| 2816 | dst = call_dst(dt->tzinfo, result, &none); |
| 2817 | if (dst == -1 && PyErr_Occurred()) |
| 2818 | goto Fail; |
| 2819 | if (none) |
| 2820 | goto Inconsistent; |
| 2821 | if (dst == 0) |
| 2822 | return result; |
| 2823 | |
| 2824 | mm += dst; |
| 2825 | if ((mm < 0 || mm >= 60) && |
| 2826 | normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
| 2827 | goto Fail; |
| 2828 | Py_DECREF(result); |
| 2829 | result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo); |
| 2830 | return result; |
| 2831 | |
| 2832 | Inconsistent: |
| 2833 | PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave" |
| 2834 | "inconsistent results; cannot convert"); |
| 2835 | |
| 2836 | /* fall thru to failure */ |
| 2837 | Fail: |
| 2838 | Py_DECREF(result); |
| 2839 | return NULL; |
| 2840 | } |
| 2841 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2842 | /* |
| 2843 | * Pickle support. This is solely so that tzinfo subclasses can use |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2844 | * pickling -- tzinfo itself is supposed to be uninstantiable. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2845 | */ |
| 2846 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2847 | static PyObject * |
| 2848 | tzinfo_reduce(PyObject *self) |
| 2849 | { |
| 2850 | PyObject *args, *state, *tmp; |
| 2851 | PyObject *getinitargs, *getstate; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2852 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2853 | tmp = PyTuple_New(0); |
| 2854 | if (tmp == NULL) |
| 2855 | return NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2856 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2857 | getinitargs = PyObject_GetAttrString(self, "__getinitargs__"); |
| 2858 | if (getinitargs != NULL) { |
| 2859 | args = PyObject_CallObject(getinitargs, tmp); |
| 2860 | Py_DECREF(getinitargs); |
| 2861 | if (args == NULL) { |
| 2862 | Py_DECREF(tmp); |
| 2863 | return NULL; |
| 2864 | } |
| 2865 | } |
| 2866 | else { |
| 2867 | PyErr_Clear(); |
| 2868 | args = tmp; |
| 2869 | Py_INCREF(args); |
| 2870 | } |
| 2871 | |
| 2872 | getstate = PyObject_GetAttrString(self, "__getstate__"); |
| 2873 | if (getstate != NULL) { |
| 2874 | state = PyObject_CallObject(getstate, tmp); |
| 2875 | Py_DECREF(getstate); |
| 2876 | if (state == NULL) { |
| 2877 | Py_DECREF(args); |
| 2878 | Py_DECREF(tmp); |
| 2879 | return NULL; |
| 2880 | } |
| 2881 | } |
| 2882 | else { |
| 2883 | PyObject **dictptr; |
| 2884 | PyErr_Clear(); |
| 2885 | state = Py_None; |
| 2886 | dictptr = _PyObject_GetDictPtr(self); |
| 2887 | if (dictptr && *dictptr && PyDict_Size(*dictptr)) |
| 2888 | state = *dictptr; |
| 2889 | Py_INCREF(state); |
| 2890 | } |
| 2891 | |
| 2892 | Py_DECREF(tmp); |
| 2893 | |
| 2894 | if (state == Py_None) { |
| 2895 | Py_DECREF(state); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2896 | return Py_BuildValue("(ON)", Py_TYPE(self), args); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2897 | } |
| 2898 | else |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2899 | return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2900 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2901 | |
| 2902 | static PyMethodDef tzinfo_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2903 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2904 | {"tzname", (PyCFunction)tzinfo_tzname, METH_O, |
| 2905 | PyDoc_STR("datetime -> string name of time zone.")}, |
| 2906 | |
| 2907 | {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O, |
| 2908 | PyDoc_STR("datetime -> minutes east of UTC (negative for " |
| 2909 | "west of UTC).")}, |
| 2910 | |
| 2911 | {"dst", (PyCFunction)tzinfo_dst, METH_O, |
| 2912 | PyDoc_STR("datetime -> DST offset in minutes east of UTC.")}, |
| 2913 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 2914 | {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O, |
| 2915 | PyDoc_STR("datetime in UTC -> datetime in local time.")}, |
| 2916 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 2917 | {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS, |
| 2918 | PyDoc_STR("-> (cls, state)")}, |
| 2919 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2920 | {NULL, NULL} |
| 2921 | }; |
| 2922 | |
| 2923 | static char tzinfo_doc[] = |
| 2924 | PyDoc_STR("Abstract base class for time zone info objects."); |
| 2925 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 2926 | static PyTypeObject PyDateTime_TZInfoType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2927 | PyVarObject_HEAD_INIT(NULL, 0) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2928 | "datetime.tzinfo", /* tp_name */ |
| 2929 | sizeof(PyDateTime_TZInfo), /* tp_basicsize */ |
| 2930 | 0, /* tp_itemsize */ |
| 2931 | 0, /* tp_dealloc */ |
| 2932 | 0, /* tp_print */ |
| 2933 | 0, /* tp_getattr */ |
| 2934 | 0, /* tp_setattr */ |
| 2935 | 0, /* tp_compare */ |
| 2936 | 0, /* tp_repr */ |
| 2937 | 0, /* tp_as_number */ |
| 2938 | 0, /* tp_as_sequence */ |
| 2939 | 0, /* tp_as_mapping */ |
| 2940 | 0, /* tp_hash */ |
| 2941 | 0, /* tp_call */ |
| 2942 | 0, /* tp_str */ |
| 2943 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2944 | 0, /* tp_setattro */ |
| 2945 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 2946 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2947 | tzinfo_doc, /* tp_doc */ |
| 2948 | 0, /* tp_traverse */ |
| 2949 | 0, /* tp_clear */ |
| 2950 | 0, /* tp_richcompare */ |
| 2951 | 0, /* tp_weaklistoffset */ |
| 2952 | 0, /* tp_iter */ |
| 2953 | 0, /* tp_iternext */ |
| 2954 | tzinfo_methods, /* tp_methods */ |
| 2955 | 0, /* tp_members */ |
| 2956 | 0, /* tp_getset */ |
| 2957 | 0, /* tp_base */ |
| 2958 | 0, /* tp_dict */ |
| 2959 | 0, /* tp_descr_get */ |
| 2960 | 0, /* tp_descr_set */ |
| 2961 | 0, /* tp_dictoffset */ |
| 2962 | 0, /* tp_init */ |
| 2963 | 0, /* tp_alloc */ |
| 2964 | PyType_GenericNew, /* tp_new */ |
| 2965 | 0, /* tp_free */ |
| 2966 | }; |
| 2967 | |
| 2968 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 2969 | * PyDateTime_Time implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2970 | */ |
| 2971 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 2972 | /* Accessor properties. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2973 | */ |
| 2974 | |
| 2975 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 2976 | time_hour(PyDateTime_Time *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2977 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2978 | return PyLong_FromLong(TIME_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 2981 | static PyObject * |
| 2982 | time_minute(PyDateTime_Time *self, void *unused) |
| 2983 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2984 | return PyLong_FromLong(TIME_GET_MINUTE(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | /* The name time_second conflicted with some platform header file. */ |
| 2988 | static PyObject * |
| 2989 | py_time_second(PyDateTime_Time *self, void *unused) |
| 2990 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2991 | return PyLong_FromLong(TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | static PyObject * |
| 2995 | time_microsecond(PyDateTime_Time *self, void *unused) |
| 2996 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2997 | return PyLong_FromLong(TIME_GET_MICROSECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | static PyObject * |
| 3001 | time_tzinfo(PyDateTime_Time *self, void *unused) |
| 3002 | { |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3003 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3004 | Py_INCREF(result); |
| 3005 | return result; |
| 3006 | } |
| 3007 | |
| 3008 | static PyGetSetDef time_getset[] = { |
| 3009 | {"hour", (getter)time_hour}, |
| 3010 | {"minute", (getter)time_minute}, |
| 3011 | {"second", (getter)py_time_second}, |
| 3012 | {"microsecond", (getter)time_microsecond}, |
| 3013 | {"tzinfo", (getter)time_tzinfo}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3014 | {NULL} |
| 3015 | }; |
| 3016 | |
| 3017 | /* |
| 3018 | * Constructors. |
| 3019 | */ |
| 3020 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3021 | static char *time_kws[] = {"hour", "minute", "second", "microsecond", |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3022 | "tzinfo", NULL}; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3023 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3024 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3025 | time_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3026 | { |
| 3027 | PyObject *self = NULL; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3028 | PyObject *state; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3029 | int hour = 0; |
| 3030 | int minute = 0; |
| 3031 | int second = 0; |
| 3032 | int usecond = 0; |
| 3033 | PyObject *tzinfo = Py_None; |
| 3034 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3035 | /* Check for invocation from pickle with __getstate__ state */ |
| 3036 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3037 | PyTuple_GET_SIZE(args) <= 2 && |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3038 | PyString_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3039 | PyString_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && |
| 3040 | ((unsigned char) (PyString_AS_STRING(state)[0])) < 24) |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3041 | { |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3042 | PyDateTime_Time *me; |
| 3043 | char aware; |
| 3044 | |
| 3045 | if (PyTuple_GET_SIZE(args) == 2) { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3046 | tzinfo = PyTuple_GET_ITEM(args, 1); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3047 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3048 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3049 | "tzinfo state arg"); |
| 3050 | return NULL; |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3051 | } |
| 3052 | } |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3053 | aware = (char)(tzinfo != Py_None); |
Tim Peters | 604c013 | 2004-06-07 23:04:33 +0000 | [diff] [blame] | 3054 | me = (PyDateTime_Time *) (type->tp_alloc(type, aware)); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3055 | if (me != NULL) { |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3056 | char *pdata = PyString_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3057 | |
| 3058 | memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE); |
| 3059 | me->hashcode = -1; |
| 3060 | me->hastzinfo = aware; |
| 3061 | if (aware) { |
| 3062 | Py_INCREF(tzinfo); |
| 3063 | me->tzinfo = tzinfo; |
| 3064 | } |
| 3065 | } |
| 3066 | return (PyObject *)me; |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3069 | if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3070 | &hour, &minute, &second, &usecond, |
| 3071 | &tzinfo)) { |
| 3072 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3073 | return NULL; |
| 3074 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3075 | return NULL; |
Tim Peters | a98924a | 2003-05-17 05:55:19 +0000 | [diff] [blame] | 3076 | self = new_time_ex(hour, minute, second, usecond, tzinfo, |
| 3077 | type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3078 | } |
| 3079 | return self; |
| 3080 | } |
| 3081 | |
| 3082 | /* |
| 3083 | * Destructor. |
| 3084 | */ |
| 3085 | |
| 3086 | static void |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3087 | time_dealloc(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3088 | { |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3089 | if (HASTZINFO(self)) { |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3090 | Py_XDECREF(self->tzinfo); |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 3091 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3092 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | /* |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3096 | * Indirect access to tzinfo methods. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3097 | */ |
| 3098 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3099 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3100 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3101 | time_utcoffset(PyDateTime_Time *self, PyObject *unused) { |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3102 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3103 | "utcoffset", Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3107 | time_dst(PyDateTime_Time *self, PyObject *unused) { |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3108 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3109 | "dst", Py_None); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3113 | time_tzname(PyDateTime_Time *self, PyObject *unused) { |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3114 | return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3115 | Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | /* |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3119 | * Various ways to turn a time into a string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3120 | */ |
| 3121 | |
| 3122 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3123 | time_repr(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3124 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3125 | const char *type_name = Py_TYPE(self)->tp_name; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3126 | int h = TIME_GET_HOUR(self); |
| 3127 | int m = TIME_GET_MINUTE(self); |
| 3128 | int s = TIME_GET_SECOND(self); |
| 3129 | int us = TIME_GET_MICROSECOND(self); |
| 3130 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3131 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3132 | if (us) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 3133 | result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", |
| 3134 | type_name, h, m, s, us); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3135 | else if (s) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 3136 | result = PyUnicode_FromFormat("%s(%d, %d, %d)", |
| 3137 | type_name, h, m, s); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3138 | else |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 3139 | result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3140 | if (result != NULL && HASTZINFO(self)) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3141 | result = append_keyword_tzinfo(result, self->tzinfo); |
| 3142 | return result; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3145 | static PyObject * |
| 3146 | time_str(PyDateTime_Time *self) |
| 3147 | { |
| 3148 | return PyObject_CallMethod((PyObject *)self, "isoformat", "()"); |
| 3149 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3150 | |
| 3151 | static PyObject * |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3152 | time_isoformat(PyDateTime_Time *self, PyObject *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3153 | { |
| 3154 | char buf[100]; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3155 | PyObject *result; |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 3156 | int us = TIME_GET_MICROSECOND(self);; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3157 | |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 3158 | if (us) |
| 3159 | result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d", |
| 3160 | TIME_GET_HOUR(self), |
| 3161 | TIME_GET_MINUTE(self), |
| 3162 | TIME_GET_SECOND(self), |
| 3163 | us); |
| 3164 | else |
| 3165 | result = PyUnicode_FromFormat("%02d:%02d:%02d", |
| 3166 | TIME_GET_HOUR(self), |
| 3167 | TIME_GET_MINUTE(self), |
| 3168 | TIME_GET_SECOND(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3169 | |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3170 | if (result == NULL || ! HASTZINFO(self) || self->tzinfo == Py_None) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3171 | return result; |
| 3172 | |
| 3173 | /* We need to append the UTC offset. */ |
| 3174 | if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 3175 | Py_None) < 0) { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3176 | Py_DECREF(result); |
| 3177 | return NULL; |
| 3178 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 3179 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3180 | return result; |
| 3181 | } |
| 3182 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3183 | static PyObject * |
| 3184 | time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
| 3185 | { |
| 3186 | PyObject *result; |
| 3187 | PyObject *format; |
| 3188 | PyObject *tuple; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3189 | static char *keywords[] = {"format", NULL}; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3190 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3191 | if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 3192 | &format)) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3193 | return NULL; |
| 3194 | |
| 3195 | /* Python's strftime does insane things with the year part of the |
| 3196 | * timetuple. The year is forced to (the otherwise nonsensical) |
| 3197 | * 1900 to worm around that. |
| 3198 | */ |
| 3199 | tuple = Py_BuildValue("iiiiiiiii", |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 3200 | 1900, 1, 1, /* year, month, day */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3201 | TIME_GET_HOUR(self), |
| 3202 | TIME_GET_MINUTE(self), |
| 3203 | TIME_GET_SECOND(self), |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 3204 | 0, 1, -1); /* weekday, daynum, dst */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3205 | if (tuple == NULL) |
| 3206 | return NULL; |
| 3207 | assert(PyTuple_Size(tuple) == 9); |
| 3208 | result = wrap_strftime((PyObject *)self, format, tuple, Py_None); |
| 3209 | Py_DECREF(tuple); |
| 3210 | return result; |
| 3211 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3212 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3213 | static PyObject * |
| 3214 | time_format(PyDateTime_Time *self, PyObject *args) |
| 3215 | { |
| 3216 | PyObject *format; |
| 3217 | |
| 3218 | if (!PyArg_ParseTuple(args, "U:__format__", &format)) |
| 3219 | return NULL; |
| 3220 | |
| 3221 | /* if the format is zero length, return str(self) */ |
| 3222 | if (PyUnicode_GetSize(format) == 0) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 3223 | return PyObject_Str((PyObject *)self); |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3224 | |
| 3225 | return PyObject_CallMethod((PyObject *)self, "strftime", "O", format); |
| 3226 | } |
| 3227 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3228 | /* |
| 3229 | * Miscellaneous methods. |
| 3230 | */ |
| 3231 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3232 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3233 | time_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3234 | { |
| 3235 | int diff; |
| 3236 | naivety n1, n2; |
| 3237 | int offset1, offset2; |
| 3238 | |
| 3239 | if (! PyTime_Check(other)) { |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3240 | Py_INCREF(Py_NotImplemented); |
| 3241 | return Py_NotImplemented; |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3242 | } |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3243 | if (classify_two_utcoffsets(self, &offset1, &n1, Py_None, |
| 3244 | other, &offset2, &n2, Py_None) < 0) |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3245 | return NULL; |
| 3246 | assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
| 3247 | /* If they're both naive, or both aware and have the same offsets, |
| 3248 | * we get off cheap. Note that if they're both naive, offset1 == |
| 3249 | * offset2 == 0 at this point. |
| 3250 | */ |
| 3251 | if (n1 == n2 && offset1 == offset2) { |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3252 | diff = memcmp(((PyDateTime_Time *)self)->data, |
| 3253 | ((PyDateTime_Time *)other)->data, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3254 | _PyDateTime_TIME_DATASIZE); |
| 3255 | return diff_to_bool(diff, op); |
| 3256 | } |
| 3257 | |
| 3258 | if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) { |
| 3259 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 3260 | /* Convert everything except microseconds to seconds. These |
| 3261 | * can't overflow (no more than the # of seconds in 2 days). |
| 3262 | */ |
| 3263 | offset1 = TIME_GET_HOUR(self) * 3600 + |
| 3264 | (TIME_GET_MINUTE(self) - offset1) * 60 + |
| 3265 | TIME_GET_SECOND(self); |
| 3266 | offset2 = TIME_GET_HOUR(other) * 3600 + |
| 3267 | (TIME_GET_MINUTE(other) - offset2) * 60 + |
| 3268 | TIME_GET_SECOND(other); |
| 3269 | diff = offset1 - offset2; |
| 3270 | if (diff == 0) |
| 3271 | diff = TIME_GET_MICROSECOND(self) - |
| 3272 | TIME_GET_MICROSECOND(other); |
| 3273 | return diff_to_bool(diff, op); |
| 3274 | } |
| 3275 | |
| 3276 | assert(n1 != n2); |
| 3277 | PyErr_SetString(PyExc_TypeError, |
| 3278 | "can't compare offset-naive and " |
| 3279 | "offset-aware times"); |
| 3280 | return NULL; |
| 3281 | } |
| 3282 | |
| 3283 | static long |
| 3284 | time_hash(PyDateTime_Time *self) |
| 3285 | { |
| 3286 | if (self->hashcode == -1) { |
| 3287 | naivety n; |
| 3288 | int offset; |
| 3289 | PyObject *temp; |
| 3290 | |
| 3291 | n = classify_utcoffset((PyObject *)self, Py_None, &offset); |
| 3292 | assert(n != OFFSET_UNKNOWN); |
| 3293 | if (n == OFFSET_ERROR) |
| 3294 | return -1; |
| 3295 | |
| 3296 | /* Reduce this to a hash of another object. */ |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3297 | if (offset == 0) { |
| 3298 | self->hashcode = generic_hash( |
| 3299 | (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE); |
| 3300 | return self->hashcode; |
| 3301 | } |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3302 | else { |
| 3303 | int hour; |
| 3304 | int minute; |
| 3305 | |
| 3306 | assert(n == OFFSET_AWARE); |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3307 | assert(HASTZINFO(self)); |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3308 | hour = divmod(TIME_GET_HOUR(self) * 60 + |
| 3309 | TIME_GET_MINUTE(self) - offset, |
| 3310 | 60, |
| 3311 | &minute); |
| 3312 | if (0 <= hour && hour < 24) |
| 3313 | temp = new_time(hour, minute, |
| 3314 | TIME_GET_SECOND(self), |
| 3315 | TIME_GET_MICROSECOND(self), |
| 3316 | Py_None); |
| 3317 | else |
| 3318 | temp = Py_BuildValue("iiii", |
| 3319 | hour, minute, |
| 3320 | TIME_GET_SECOND(self), |
| 3321 | TIME_GET_MICROSECOND(self)); |
| 3322 | } |
| 3323 | if (temp != NULL) { |
| 3324 | self->hashcode = PyObject_Hash(temp); |
| 3325 | Py_DECREF(temp); |
| 3326 | } |
| 3327 | } |
| 3328 | return self->hashcode; |
| 3329 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3330 | |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3331 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3332 | time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3333 | { |
| 3334 | PyObject *clone; |
| 3335 | PyObject *tuple; |
| 3336 | int hh = TIME_GET_HOUR(self); |
| 3337 | int mm = TIME_GET_MINUTE(self); |
| 3338 | int ss = TIME_GET_SECOND(self); |
| 3339 | int us = TIME_GET_MICROSECOND(self); |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3340 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3341 | |
| 3342 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3343 | time_kws, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3344 | &hh, &mm, &ss, &us, &tzinfo)) |
| 3345 | return NULL; |
| 3346 | tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); |
| 3347 | if (tuple == NULL) |
| 3348 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3349 | clone = time_new(Py_TYPE(self), tuple, NULL); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3350 | Py_DECREF(tuple); |
| 3351 | return clone; |
| 3352 | } |
| 3353 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3354 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3355 | time_bool(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3356 | { |
| 3357 | int offset; |
| 3358 | int none; |
| 3359 | |
| 3360 | if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) { |
| 3361 | /* Since utcoffset is in whole minutes, nothing can |
| 3362 | * alter the conclusion that this is nonzero. |
| 3363 | */ |
| 3364 | return 1; |
| 3365 | } |
| 3366 | offset = 0; |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3367 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 3368 | offset = call_utcoffset(self->tzinfo, Py_None, &none); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3369 | if (offset == -1 && PyErr_Occurred()) |
| 3370 | return -1; |
| 3371 | } |
| 3372 | return (TIME_GET_MINUTE(self) - offset + TIME_GET_HOUR(self)*60) != 0; |
| 3373 | } |
| 3374 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 3375 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3376 | |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3377 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3378 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 3379 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 3380 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3381 | */ |
| 3382 | static PyObject * |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3383 | time_getstate(PyDateTime_Time *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3384 | { |
| 3385 | PyObject *basestate; |
| 3386 | PyObject *result = NULL; |
| 3387 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3388 | basestate = PyString_FromStringAndSize((char *)self->data, |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 3389 | _PyDateTime_TIME_DATASIZE); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3390 | if (basestate != NULL) { |
Tim Peters | a032d2e | 2003-01-11 00:15:54 +0000 | [diff] [blame] | 3391 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3392 | result = PyTuple_Pack(1, basestate); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3393 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 3394 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3395 | Py_DECREF(basestate); |
| 3396 | } |
| 3397 | return result; |
| 3398 | } |
| 3399 | |
| 3400 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3401 | time_reduce(PyDateTime_Time *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3402 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3403 | return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3406 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3407 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3408 | {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3409 | PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" |
| 3410 | "[+HH:MM].")}, |
| 3411 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3412 | {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3413 | PyDoc_STR("format -> strftime() style string.")}, |
| 3414 | |
Eric Smith | 1ba3114 | 2007-09-11 18:06:02 +0000 | [diff] [blame] | 3415 | {"__format__", (PyCFunction)time_format, METH_VARARGS, |
| 3416 | PyDoc_STR("Formats self with strftime.")}, |
| 3417 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3418 | {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3419 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
| 3420 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3421 | {"tzname", (PyCFunction)time_tzname, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3422 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
| 3423 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3424 | {"dst", (PyCFunction)time_dst, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3425 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
| 3426 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 3427 | {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3428 | PyDoc_STR("Return time with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3429 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3430 | {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, |
| 3431 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 3432 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3433 | {NULL, NULL} |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3434 | }; |
| 3435 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3436 | static char time_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 3437 | PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\ |
| 3438 | \n\ |
| 3439 | All arguments are optional. tzinfo may be None, or an instance of\n\ |
| 3440 | a tzinfo subclass. The remaining arguments may be ints or longs.\n"); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3441 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3442 | static PyNumberMethods time_as_number = { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3443 | 0, /* nb_add */ |
| 3444 | 0, /* nb_subtract */ |
| 3445 | 0, /* nb_multiply */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3446 | 0, /* nb_remainder */ |
| 3447 | 0, /* nb_divmod */ |
| 3448 | 0, /* nb_power */ |
| 3449 | 0, /* nb_negative */ |
| 3450 | 0, /* nb_positive */ |
| 3451 | 0, /* nb_absolute */ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3452 | (inquiry)time_bool, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3453 | }; |
| 3454 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3455 | static PyTypeObject PyDateTime_TimeType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3456 | PyVarObject_HEAD_INIT(NULL, 0) |
Tim Peters | 0bf60bd | 2003-01-08 20:40:01 +0000 | [diff] [blame] | 3457 | "datetime.time", /* tp_name */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3458 | sizeof(PyDateTime_Time), /* tp_basicsize */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3459 | 0, /* tp_itemsize */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3460 | (destructor)time_dealloc, /* tp_dealloc */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3461 | 0, /* tp_print */ |
| 3462 | 0, /* tp_getattr */ |
| 3463 | 0, /* tp_setattr */ |
| 3464 | 0, /* tp_compare */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3465 | (reprfunc)time_repr, /* tp_repr */ |
| 3466 | &time_as_number, /* tp_as_number */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3467 | 0, /* tp_as_sequence */ |
| 3468 | 0, /* tp_as_mapping */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3469 | (hashfunc)time_hash, /* tp_hash */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3470 | 0, /* tp_call */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3471 | (reprfunc)time_str, /* tp_str */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3472 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3473 | 0, /* tp_setattro */ |
| 3474 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 3475 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3476 | time_doc, /* tp_doc */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3477 | 0, /* tp_traverse */ |
| 3478 | 0, /* tp_clear */ |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 3479 | time_richcompare, /* tp_richcompare */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3480 | 0, /* tp_weaklistoffset */ |
| 3481 | 0, /* tp_iter */ |
| 3482 | 0, /* tp_iternext */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3483 | time_methods, /* tp_methods */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3484 | 0, /* tp_members */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3485 | time_getset, /* tp_getset */ |
| 3486 | 0, /* tp_base */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3487 | 0, /* tp_dict */ |
| 3488 | 0, /* tp_descr_get */ |
| 3489 | 0, /* tp_descr_set */ |
| 3490 | 0, /* tp_dictoffset */ |
| 3491 | 0, /* tp_init */ |
Tim Peters | a98924a | 2003-05-17 05:55:19 +0000 | [diff] [blame] | 3492 | time_alloc, /* tp_alloc */ |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 3493 | time_new, /* tp_new */ |
Tim Peters | 4c53013 | 2003-05-16 22:44:06 +0000 | [diff] [blame] | 3494 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3495 | }; |
| 3496 | |
| 3497 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3498 | * PyDateTime_DateTime implementation. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3499 | */ |
| 3500 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3501 | /* Accessor properties. Properties for day, month, and year are inherited |
| 3502 | * from date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3503 | */ |
| 3504 | |
| 3505 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3506 | datetime_hour(PyDateTime_DateTime *self, void *unused) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3507 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3508 | return PyLong_FromLong(DATE_GET_HOUR(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3511 | static PyObject * |
| 3512 | datetime_minute(PyDateTime_DateTime *self, void *unused) |
| 3513 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3514 | return PyLong_FromLong(DATE_GET_MINUTE(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | static PyObject * |
| 3518 | datetime_second(PyDateTime_DateTime *self, void *unused) |
| 3519 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3520 | return PyLong_FromLong(DATE_GET_SECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | static PyObject * |
| 3524 | datetime_microsecond(PyDateTime_DateTime *self, void *unused) |
| 3525 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 3526 | return PyLong_FromLong(DATE_GET_MICROSECOND(self)); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | static PyObject * |
| 3530 | datetime_tzinfo(PyDateTime_DateTime *self, void *unused) |
| 3531 | { |
| 3532 | PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None; |
| 3533 | Py_INCREF(result); |
| 3534 | return result; |
| 3535 | } |
| 3536 | |
| 3537 | static PyGetSetDef datetime_getset[] = { |
| 3538 | {"hour", (getter)datetime_hour}, |
| 3539 | {"minute", (getter)datetime_minute}, |
| 3540 | {"second", (getter)datetime_second}, |
| 3541 | {"microsecond", (getter)datetime_microsecond}, |
| 3542 | {"tzinfo", (getter)datetime_tzinfo}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3543 | {NULL} |
| 3544 | }; |
| 3545 | |
| 3546 | /* |
| 3547 | * Constructors. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3548 | */ |
| 3549 | |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3550 | static char *datetime_kws[] = { |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 3551 | "year", "month", "day", "hour", "minute", "second", |
| 3552 | "microsecond", "tzinfo", NULL |
| 3553 | }; |
| 3554 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3555 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3556 | datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3557 | { |
| 3558 | PyObject *self = NULL; |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3559 | PyObject *state; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3560 | int year; |
| 3561 | int month; |
| 3562 | int day; |
| 3563 | int hour = 0; |
| 3564 | int minute = 0; |
| 3565 | int second = 0; |
| 3566 | int usecond = 0; |
| 3567 | PyObject *tzinfo = Py_None; |
| 3568 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3569 | /* Check for invocation from pickle with __getstate__ state */ |
| 3570 | if (PyTuple_GET_SIZE(args) >= 1 && |
| 3571 | PyTuple_GET_SIZE(args) <= 2 && |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3572 | PyString_Check(state = PyTuple_GET_ITEM(args, 0)) && |
| 3573 | PyString_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && |
| 3574 | MONTH_IS_SANE(PyString_AS_STRING(state)[2])) |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3575 | { |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3576 | PyDateTime_DateTime *me; |
| 3577 | char aware; |
| 3578 | |
| 3579 | if (PyTuple_GET_SIZE(args) == 2) { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3580 | tzinfo = PyTuple_GET_ITEM(args, 1); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3581 | if (check_tzinfo_subclass(tzinfo) < 0) { |
| 3582 | PyErr_SetString(PyExc_TypeError, "bad " |
| 3583 | "tzinfo state arg"); |
| 3584 | return NULL; |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3585 | } |
| 3586 | } |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3587 | aware = (char)(tzinfo != Py_None); |
Tim Peters | 604c013 | 2004-06-07 23:04:33 +0000 | [diff] [blame] | 3588 | me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware)); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3589 | if (me != NULL) { |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3590 | char *pdata = PyString_AS_STRING(state); |
Tim Peters | 70533e2 | 2003-02-01 04:40:04 +0000 | [diff] [blame] | 3591 | |
| 3592 | memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE); |
| 3593 | me->hashcode = -1; |
| 3594 | me->hastzinfo = aware; |
| 3595 | if (aware) { |
| 3596 | Py_INCREF(tzinfo); |
| 3597 | me->tzinfo = tzinfo; |
| 3598 | } |
| 3599 | } |
| 3600 | return (PyObject *)me; |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3603 | if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3604 | &year, &month, &day, &hour, &minute, |
| 3605 | &second, &usecond, &tzinfo)) { |
| 3606 | if (check_date_args(year, month, day) < 0) |
| 3607 | return NULL; |
| 3608 | if (check_time_args(hour, minute, second, usecond) < 0) |
| 3609 | return NULL; |
| 3610 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3611 | return NULL; |
Tim Peters | a98924a | 2003-05-17 05:55:19 +0000 | [diff] [blame] | 3612 | self = new_datetime_ex(year, month, day, |
| 3613 | hour, minute, second, usecond, |
| 3614 | tzinfo, type); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3615 | } |
| 3616 | return self; |
| 3617 | } |
| 3618 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3619 | /* TM_FUNC is the shared type of localtime() and gmtime(). */ |
| 3620 | typedef struct tm *(*TM_FUNC)(const time_t *timer); |
| 3621 | |
| 3622 | /* Internal helper. |
| 3623 | * Build datetime from a time_t and a distinct count of microseconds. |
| 3624 | * Pass localtime or gmtime for f, to control the interpretation of timet. |
| 3625 | */ |
| 3626 | static PyObject * |
| 3627 | datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, |
| 3628 | PyObject *tzinfo) |
| 3629 | { |
| 3630 | struct tm *tm; |
| 3631 | PyObject *result = NULL; |
| 3632 | |
| 3633 | tm = f(&timet); |
| 3634 | if (tm) { |
| 3635 | /* The platform localtime/gmtime may insert leap seconds, |
| 3636 | * indicated by tm->tm_sec > 59. We don't care about them, |
| 3637 | * except to the extent that passing them on to the datetime |
| 3638 | * constructor would raise ValueError for a reason that |
| 3639 | * made no sense to the user. |
| 3640 | */ |
| 3641 | if (tm->tm_sec > 59) |
| 3642 | tm->tm_sec = 59; |
| 3643 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 3644 | tm->tm_year + 1900, |
| 3645 | tm->tm_mon + 1, |
| 3646 | tm->tm_mday, |
| 3647 | tm->tm_hour, |
| 3648 | tm->tm_min, |
| 3649 | tm->tm_sec, |
| 3650 | us, |
| 3651 | tzinfo); |
| 3652 | } |
| 3653 | else |
| 3654 | PyErr_SetString(PyExc_ValueError, |
| 3655 | "timestamp out of range for " |
| 3656 | "platform localtime()/gmtime() function"); |
| 3657 | return result; |
| 3658 | } |
| 3659 | |
| 3660 | /* Internal helper. |
| 3661 | * Build datetime from a Python timestamp. Pass localtime or gmtime for f, |
| 3662 | * to control the interpretation of the timestamp. Since a double doesn't |
| 3663 | * have enough bits to cover a datetime's full range of precision, it's |
| 3664 | * better to call datetime_from_timet_and_us provided you have a way |
| 3665 | * to get that much precision (e.g., C time() isn't good enough). |
| 3666 | */ |
| 3667 | static PyObject * |
| 3668 | datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp, |
| 3669 | PyObject *tzinfo) |
| 3670 | { |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 3671 | time_t timet; |
| 3672 | double fraction; |
| 3673 | int us; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3674 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 3675 | timet = _PyTime_DoubleToTimet(timestamp); |
| 3676 | if (timet == (time_t)-1 && PyErr_Occurred()) |
| 3677 | return NULL; |
| 3678 | fraction = timestamp - (double)timet; |
| 3679 | us = (int)round_to_long(fraction * 1e6); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3680 | if (us < 0) { |
| 3681 | /* Truncation towards zero is not what we wanted |
| 3682 | for negative numbers (Python's mod semantics) */ |
| 3683 | timet -= 1; |
| 3684 | us += 1000000; |
| 3685 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3686 | /* If timestamp is less than one microsecond smaller than a |
| 3687 | * full second, round up. Otherwise, ValueErrors are raised |
| 3688 | * for some floats. */ |
| 3689 | if (us == 1000000) { |
| 3690 | timet += 1; |
| 3691 | us = 0; |
| 3692 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3693 | return datetime_from_timet_and_us(cls, f, timet, us, tzinfo); |
| 3694 | } |
| 3695 | |
| 3696 | /* Internal helper. |
| 3697 | * Build most accurate possible datetime for current time. Pass localtime or |
| 3698 | * gmtime for f as appropriate. |
| 3699 | */ |
| 3700 | static PyObject * |
| 3701 | datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) |
| 3702 | { |
| 3703 | #ifdef HAVE_GETTIMEOFDAY |
| 3704 | struct timeval t; |
| 3705 | |
| 3706 | #ifdef GETTIMEOFDAY_NO_TZ |
| 3707 | gettimeofday(&t); |
| 3708 | #else |
| 3709 | gettimeofday(&t, (struct timezone *)NULL); |
| 3710 | #endif |
| 3711 | return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec, |
| 3712 | tzinfo); |
| 3713 | |
| 3714 | #else /* ! HAVE_GETTIMEOFDAY */ |
| 3715 | /* No flavor of gettimeofday exists on this platform. Python's |
| 3716 | * time.time() does a lot of other platform tricks to get the |
| 3717 | * best time it can on the platform, and we're not going to do |
| 3718 | * better than that (if we could, the better code would belong |
| 3719 | * in time.time()!) We're limited by the precision of a double, |
| 3720 | * though. |
| 3721 | */ |
| 3722 | PyObject *time; |
| 3723 | double dtime; |
| 3724 | |
| 3725 | time = time_time(); |
| 3726 | if (time == NULL) |
| 3727 | return NULL; |
| 3728 | dtime = PyFloat_AsDouble(time); |
| 3729 | Py_DECREF(time); |
| 3730 | if (dtime == -1.0 && PyErr_Occurred()) |
| 3731 | return NULL; |
| 3732 | return datetime_from_timestamp(cls, f, dtime, tzinfo); |
| 3733 | #endif /* ! HAVE_GETTIMEOFDAY */ |
| 3734 | } |
| 3735 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3736 | /* Return best possible local time -- this isn't constrained by the |
| 3737 | * precision of a timestamp. |
| 3738 | */ |
| 3739 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3740 | datetime_now(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3741 | { |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 3742 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3743 | PyObject *tzinfo = Py_None; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3744 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3745 | |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 3746 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords, |
| 3747 | &tzinfo)) |
| 3748 | return NULL; |
| 3749 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3750 | return NULL; |
| 3751 | |
| 3752 | self = datetime_best_possible(cls, |
| 3753 | tzinfo == Py_None ? localtime : gmtime, |
| 3754 | tzinfo); |
| 3755 | if (self != NULL && tzinfo != Py_None) { |
| 3756 | /* Convert UTC to tzinfo's zone. */ |
| 3757 | PyObject *temp = self; |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 3758 | self = PyObject_CallMethod(tzinfo, "fromutc", "O", self); |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 3759 | Py_DECREF(temp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3760 | } |
| 3761 | return self; |
| 3762 | } |
| 3763 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3764 | /* Return best possible UTC time -- this isn't constrained by the |
| 3765 | * precision of a timestamp. |
| 3766 | */ |
| 3767 | static PyObject * |
| 3768 | datetime_utcnow(PyObject *cls, PyObject *dummy) |
| 3769 | { |
| 3770 | return datetime_best_possible(cls, gmtime, Py_None); |
| 3771 | } |
| 3772 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3773 | /* Return new local datetime from timestamp (Python timestamp -- a double). */ |
| 3774 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3775 | datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3776 | { |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 3777 | PyObject *self; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3778 | double timestamp; |
| 3779 | PyObject *tzinfo = Py_None; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3780 | static char *keywords[] = {"timestamp", "tz", NULL}; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3781 | |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 3782 | if (! PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp", |
| 3783 | keywords, ×tamp, &tzinfo)) |
| 3784 | return NULL; |
| 3785 | if (check_tzinfo_subclass(tzinfo) < 0) |
| 3786 | return NULL; |
| 3787 | |
| 3788 | self = datetime_from_timestamp(cls, |
| 3789 | tzinfo == Py_None ? localtime : gmtime, |
| 3790 | timestamp, |
| 3791 | tzinfo); |
| 3792 | if (self != NULL && tzinfo != Py_None) { |
| 3793 | /* Convert UTC to tzinfo's zone. */ |
| 3794 | PyObject *temp = self; |
| 3795 | self = PyObject_CallMethod(tzinfo, "fromutc", "O", self); |
| 3796 | Py_DECREF(temp); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3797 | } |
| 3798 | return self; |
| 3799 | } |
| 3800 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3801 | /* Return new UTC datetime from timestamp (Python timestamp -- a double). */ |
| 3802 | static PyObject * |
| 3803 | datetime_utcfromtimestamp(PyObject *cls, PyObject *args) |
| 3804 | { |
| 3805 | double timestamp; |
| 3806 | PyObject *result = NULL; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3807 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3808 | if (PyArg_ParseTuple(args, "d:utcfromtimestamp", ×tamp)) |
| 3809 | result = datetime_from_timestamp(cls, gmtime, timestamp, |
| 3810 | Py_None); |
| 3811 | return result; |
| 3812 | } |
| 3813 | |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 3814 | /* Return new datetime from time.strptime(). */ |
| 3815 | static PyObject * |
| 3816 | datetime_strptime(PyObject *cls, PyObject *args) |
| 3817 | { |
| 3818 | PyObject *result = NULL, *obj, *module; |
Guido van Rossum | e8a17aa | 2007-08-29 17:28:42 +0000 | [diff] [blame] | 3819 | const Py_UNICODE *string, *format; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 3820 | |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3821 | if (!PyArg_ParseTuple(args, "uu:strptime", &string, &format)) |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 3822 | return NULL; |
| 3823 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 3824 | if ((module = PyImport_ImportModuleNoBlock("time")) == NULL) |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 3825 | return NULL; |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 3826 | obj = PyObject_CallMethod(module, "strptime", "uu", string, format); |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 3827 | Py_DECREF(module); |
| 3828 | |
| 3829 | if (obj != NULL) { |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 3830 | int i, good_timetuple = 1, overflow; |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 3831 | long int ia[6]; |
| 3832 | if (PySequence_Check(obj) && PySequence_Size(obj) >= 6) |
| 3833 | for (i=0; i < 6; i++) { |
| 3834 | PyObject *p = PySequence_GetItem(obj, i); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3835 | if (p == NULL) { |
| 3836 | Py_DECREF(obj); |
| 3837 | return NULL; |
| 3838 | } |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 3839 | if (PyLong_CheckExact(p)) { |
| 3840 | ia[i] = PyLong_AsLongAndOverflow(p, &overflow); |
| 3841 | if (overflow) |
| 3842 | good_timetuple = 0; |
| 3843 | } |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 3844 | else |
| 3845 | good_timetuple = 0; |
| 3846 | Py_DECREF(p); |
| 3847 | } |
| 3848 | else |
| 3849 | good_timetuple = 0; |
| 3850 | if (good_timetuple) |
| 3851 | result = PyObject_CallFunction(cls, "iiiiii", |
| 3852 | ia[0], ia[1], ia[2], ia[3], ia[4], ia[5]); |
| 3853 | else |
| 3854 | PyErr_SetString(PyExc_ValueError, |
| 3855 | "unexpected value from time.strptime"); |
| 3856 | Py_DECREF(obj); |
| 3857 | } |
| 3858 | return result; |
| 3859 | } |
| 3860 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3861 | /* Return new datetime from date/datetime and time arguments. */ |
| 3862 | static PyObject * |
| 3863 | datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) |
| 3864 | { |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3865 | static char *keywords[] = {"date", "time", NULL}; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3866 | PyObject *date; |
| 3867 | PyObject *time; |
| 3868 | PyObject *result = NULL; |
| 3869 | |
| 3870 | if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, |
| 3871 | &PyDateTime_DateType, &date, |
| 3872 | &PyDateTime_TimeType, &time)) { |
| 3873 | PyObject *tzinfo = Py_None; |
| 3874 | |
| 3875 | if (HASTZINFO(time)) |
| 3876 | tzinfo = ((PyDateTime_Time *)time)->tzinfo; |
| 3877 | result = PyObject_CallFunction(cls, "iiiiiiiO", |
| 3878 | GET_YEAR(date), |
| 3879 | GET_MONTH(date), |
| 3880 | GET_DAY(date), |
| 3881 | TIME_GET_HOUR(time), |
| 3882 | TIME_GET_MINUTE(time), |
| 3883 | TIME_GET_SECOND(time), |
| 3884 | TIME_GET_MICROSECOND(time), |
| 3885 | tzinfo); |
| 3886 | } |
| 3887 | return result; |
| 3888 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3889 | |
| 3890 | /* |
| 3891 | * Destructor. |
| 3892 | */ |
| 3893 | |
| 3894 | static void |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3895 | datetime_dealloc(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3896 | { |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3897 | if (HASTZINFO(self)) { |
| 3898 | Py_XDECREF(self->tzinfo); |
| 3899 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3900 | Py_TYPE(self)->tp_free((PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | /* |
| 3904 | * Indirect access to tzinfo methods. |
| 3905 | */ |
| 3906 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3907 | /* These are all METH_NOARGS, so don't need to check the arglist. */ |
| 3908 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3909 | datetime_utcoffset(PyDateTime_DateTime *self, PyObject *unused) { |
| 3910 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3911 | "utcoffset", (PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3915 | datetime_dst(PyDateTime_DateTime *self, PyObject *unused) { |
| 3916 | return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3917 | "dst", (PyObject *)self); |
Tim Peters | 855fe88 | 2002-12-22 03:43:39 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3921 | datetime_tzname(PyDateTime_DateTime *self, PyObject *unused) { |
| 3922 | return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None, |
| 3923 | (PyObject *)self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | /* |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3927 | * datetime arithmetic. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3928 | */ |
| 3929 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3930 | /* factor must be 1 (to add) or -1 (to subtract). The result inherits |
| 3931 | * the tzinfo state of date. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3932 | */ |
| 3933 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3934 | add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, |
| 3935 | int factor) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3936 | { |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3937 | /* Note that the C-level additions can't overflow, because of |
| 3938 | * invariant bounds on the member values. |
| 3939 | */ |
| 3940 | int year = GET_YEAR(date); |
| 3941 | int month = GET_MONTH(date); |
| 3942 | int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor; |
| 3943 | int hour = DATE_GET_HOUR(date); |
| 3944 | int minute = DATE_GET_MINUTE(date); |
| 3945 | int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor; |
| 3946 | int microsecond = DATE_GET_MICROSECOND(date) + |
| 3947 | GET_TD_MICROSECONDS(delta) * factor; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3948 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3949 | assert(factor == 1 || factor == -1); |
| 3950 | if (normalize_datetime(&year, &month, &day, |
| 3951 | &hour, &minute, &second, µsecond) < 0) |
| 3952 | return NULL; |
| 3953 | else |
| 3954 | return new_datetime(year, month, day, |
| 3955 | hour, minute, second, microsecond, |
| 3956 | HASTZINFO(date) ? date->tzinfo : Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3960 | datetime_add(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3961 | { |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3962 | if (PyDateTime_Check(left)) { |
| 3963 | /* datetime + ??? */ |
| 3964 | if (PyDelta_Check(right)) |
| 3965 | /* datetime + delta */ |
| 3966 | return add_datetime_timedelta( |
| 3967 | (PyDateTime_DateTime *)left, |
| 3968 | (PyDateTime_Delta *)right, |
| 3969 | 1); |
| 3970 | } |
| 3971 | else if (PyDelta_Check(left)) { |
| 3972 | /* delta + datetime */ |
| 3973 | return add_datetime_timedelta((PyDateTime_DateTime *) right, |
| 3974 | (PyDateTime_Delta *) left, |
| 3975 | 1); |
| 3976 | } |
| 3977 | Py_INCREF(Py_NotImplemented); |
| 3978 | return Py_NotImplemented; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3982 | datetime_subtract(PyObject *left, PyObject *right) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3983 | { |
| 3984 | PyObject *result = Py_NotImplemented; |
| 3985 | |
| 3986 | if (PyDateTime_Check(left)) { |
| 3987 | /* datetime - ??? */ |
| 3988 | if (PyDateTime_Check(right)) { |
| 3989 | /* datetime - datetime */ |
| 3990 | naivety n1, n2; |
| 3991 | int offset1, offset2; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 3992 | int delta_d, delta_s, delta_us; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3993 | |
Tim Peters | e39a80c | 2002-12-30 21:28:52 +0000 | [diff] [blame] | 3994 | if (classify_two_utcoffsets(left, &offset1, &n1, left, |
| 3995 | right, &offset2, &n2, |
| 3996 | right) < 0) |
Tim Peters | 0023703 | 2002-12-27 02:21:51 +0000 | [diff] [blame] | 3997 | return NULL; |
Tim Peters | 8702d5f | 2002-12-27 02:26:16 +0000 | [diff] [blame] | 3998 | assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 3999 | if (n1 != n2) { |
| 4000 | PyErr_SetString(PyExc_TypeError, |
| 4001 | "can't subtract offset-naive and " |
| 4002 | "offset-aware datetimes"); |
| 4003 | return NULL; |
| 4004 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4005 | delta_d = ymd_to_ord(GET_YEAR(left), |
| 4006 | GET_MONTH(left), |
| 4007 | GET_DAY(left)) - |
| 4008 | ymd_to_ord(GET_YEAR(right), |
| 4009 | GET_MONTH(right), |
| 4010 | GET_DAY(right)); |
| 4011 | /* These can't overflow, since the values are |
| 4012 | * normalized. At most this gives the number of |
| 4013 | * seconds in one day. |
| 4014 | */ |
| 4015 | delta_s = (DATE_GET_HOUR(left) - |
| 4016 | DATE_GET_HOUR(right)) * 3600 + |
| 4017 | (DATE_GET_MINUTE(left) - |
| 4018 | DATE_GET_MINUTE(right)) * 60 + |
| 4019 | (DATE_GET_SECOND(left) - |
| 4020 | DATE_GET_SECOND(right)); |
| 4021 | delta_us = DATE_GET_MICROSECOND(left) - |
| 4022 | DATE_GET_MICROSECOND(right); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4023 | /* (left - offset1) - (right - offset2) = |
| 4024 | * (left - right) + (offset2 - offset1) |
| 4025 | */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4026 | delta_s += (offset2 - offset1) * 60; |
| 4027 | result = new_delta(delta_d, delta_s, delta_us, 1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4028 | } |
| 4029 | else if (PyDelta_Check(right)) { |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4030 | /* datetime - delta */ |
| 4031 | result = add_datetime_timedelta( |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4032 | (PyDateTime_DateTime *)left, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4033 | (PyDateTime_Delta *)right, |
| 4034 | -1); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4035 | } |
| 4036 | } |
| 4037 | |
| 4038 | if (result == Py_NotImplemented) |
| 4039 | Py_INCREF(result); |
| 4040 | return result; |
| 4041 | } |
| 4042 | |
| 4043 | /* Various ways to turn a datetime into a string. */ |
| 4044 | |
| 4045 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4046 | datetime_repr(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4047 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4048 | const char *type_name = Py_TYPE(self)->tp_name; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4049 | PyObject *baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4050 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4051 | if (DATE_GET_MICROSECOND(self)) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 4052 | baserepr = PyUnicode_FromFormat( |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4053 | "%s(%d, %d, %d, %d, %d, %d, %d)", |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4054 | type_name, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4055 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4056 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4057 | DATE_GET_SECOND(self), |
| 4058 | DATE_GET_MICROSECOND(self)); |
| 4059 | } |
| 4060 | else if (DATE_GET_SECOND(self)) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 4061 | baserepr = PyUnicode_FromFormat( |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4062 | "%s(%d, %d, %d, %d, %d, %d)", |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4063 | type_name, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4064 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4065 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4066 | DATE_GET_SECOND(self)); |
| 4067 | } |
| 4068 | else { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 4069 | baserepr = PyUnicode_FromFormat( |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4070 | "%s(%d, %d, %d, %d, %d)", |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4071 | type_name, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4072 | GET_YEAR(self), GET_MONTH(self), GET_DAY(self), |
| 4073 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); |
| 4074 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4075 | if (baserepr == NULL || ! HASTZINFO(self)) |
| 4076 | return baserepr; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4077 | return append_keyword_tzinfo(baserepr, self->tzinfo); |
| 4078 | } |
| 4079 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4080 | static PyObject * |
| 4081 | datetime_str(PyDateTime_DateTime *self) |
| 4082 | { |
| 4083 | return PyObject_CallMethod((PyObject *)self, "isoformat", "(s)", " "); |
| 4084 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4085 | |
| 4086 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4087 | datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4088 | { |
Walter Dörwald | bc1f886 | 2007-06-20 11:02:38 +0000 | [diff] [blame] | 4089 | int sep = 'T'; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4090 | static char *keywords[] = {"sep", NULL}; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4091 | char buffer[100]; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4092 | PyObject *result; |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4093 | int us = DATE_GET_MICROSECOND(self); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4094 | |
Walter Dörwald | d094130 | 2007-07-01 21:58:22 +0000 | [diff] [blame] | 4095 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep)) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4096 | return NULL; |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4097 | if (us) |
| 4098 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d", |
| 4099 | GET_YEAR(self), GET_MONTH(self), |
| 4100 | GET_DAY(self), (int)sep, |
| 4101 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4102 | DATE_GET_SECOND(self), us); |
| 4103 | else |
| 4104 | result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d", |
| 4105 | GET_YEAR(self), GET_MONTH(self), |
| 4106 | GET_DAY(self), (int)sep, |
| 4107 | DATE_GET_HOUR(self), DATE_GET_MINUTE(self), |
| 4108 | DATE_GET_SECOND(self)); |
| 4109 | |
| 4110 | if (!result || !HASTZINFO(self)) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4111 | return result; |
| 4112 | |
| 4113 | /* We need to append the UTC offset. */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4114 | if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4115 | (PyObject *)self) < 0) { |
| 4116 | Py_DECREF(result); |
| 4117 | return NULL; |
| 4118 | } |
Walter Dörwald | bafa137 | 2007-05-31 17:50:48 +0000 | [diff] [blame] | 4119 | PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4120 | return result; |
| 4121 | } |
| 4122 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4123 | static PyObject * |
| 4124 | datetime_ctime(PyDateTime_DateTime *self) |
| 4125 | { |
| 4126 | return format_ctime((PyDateTime_Date *)self, |
| 4127 | DATE_GET_HOUR(self), |
| 4128 | DATE_GET_MINUTE(self), |
| 4129 | DATE_GET_SECOND(self)); |
| 4130 | } |
| 4131 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4132 | /* Miscellaneous methods. */ |
| 4133 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4134 | static PyObject * |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4135 | datetime_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4136 | { |
| 4137 | int diff; |
| 4138 | naivety n1, n2; |
| 4139 | int offset1, offset2; |
| 4140 | |
| 4141 | if (! PyDateTime_Check(other)) { |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4142 | if (PyDate_Check(other)) { |
| 4143 | /* Prevent invocation of date_richcompare. We want to |
| 4144 | return NotImplemented here to give the other object |
| 4145 | a chance. But since DateTime is a subclass of |
| 4146 | Date, if the other object is a Date, it would |
| 4147 | compute an ordering based on the date part alone, |
| 4148 | and we don't want that. So force unequal or |
| 4149 | uncomparable here in that case. */ |
| 4150 | if (op == Py_EQ) |
| 4151 | Py_RETURN_FALSE; |
| 4152 | if (op == Py_NE) |
| 4153 | Py_RETURN_TRUE; |
| 4154 | return cmperror(self, other); |
Tim Peters | 8d81a01 | 2003-01-24 22:36:34 +0000 | [diff] [blame] | 4155 | } |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4156 | Py_INCREF(Py_NotImplemented); |
| 4157 | return Py_NotImplemented; |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4160 | if (classify_two_utcoffsets(self, &offset1, &n1, self, |
| 4161 | other, &offset2, &n2, other) < 0) |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4162 | return NULL; |
| 4163 | assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN); |
| 4164 | /* If they're both naive, or both aware and have the same offsets, |
| 4165 | * we get off cheap. Note that if they're both naive, offset1 == |
| 4166 | * offset2 == 0 at this point. |
| 4167 | */ |
| 4168 | if (n1 == n2 && offset1 == offset2) { |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4169 | diff = memcmp(((PyDateTime_DateTime *)self)->data, |
| 4170 | ((PyDateTime_DateTime *)other)->data, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4171 | _PyDateTime_DATETIME_DATASIZE); |
| 4172 | return diff_to_bool(diff, op); |
| 4173 | } |
| 4174 | |
| 4175 | if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) { |
| 4176 | PyDateTime_Delta *delta; |
| 4177 | |
| 4178 | assert(offset1 != offset2); /* else last "if" handled it */ |
| 4179 | delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self, |
| 4180 | other); |
| 4181 | if (delta == NULL) |
| 4182 | return NULL; |
| 4183 | diff = GET_TD_DAYS(delta); |
| 4184 | if (diff == 0) |
| 4185 | diff = GET_TD_SECONDS(delta) | |
| 4186 | GET_TD_MICROSECONDS(delta); |
| 4187 | Py_DECREF(delta); |
| 4188 | return diff_to_bool(diff, op); |
| 4189 | } |
| 4190 | |
| 4191 | assert(n1 != n2); |
| 4192 | PyErr_SetString(PyExc_TypeError, |
| 4193 | "can't compare offset-naive and " |
| 4194 | "offset-aware datetimes"); |
| 4195 | return NULL; |
| 4196 | } |
| 4197 | |
| 4198 | static long |
| 4199 | datetime_hash(PyDateTime_DateTime *self) |
| 4200 | { |
| 4201 | if (self->hashcode == -1) { |
| 4202 | naivety n; |
| 4203 | int offset; |
| 4204 | PyObject *temp; |
| 4205 | |
| 4206 | n = classify_utcoffset((PyObject *)self, (PyObject *)self, |
| 4207 | &offset); |
| 4208 | assert(n != OFFSET_UNKNOWN); |
| 4209 | if (n == OFFSET_ERROR) |
| 4210 | return -1; |
| 4211 | |
| 4212 | /* Reduce this to a hash of another object. */ |
Guido van Rossum | fd53fd6 | 2007-08-24 04:05:13 +0000 | [diff] [blame] | 4213 | if (n == OFFSET_NAIVE) { |
| 4214 | self->hashcode = generic_hash( |
| 4215 | (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE); |
| 4216 | return self->hashcode; |
| 4217 | } |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4218 | else { |
| 4219 | int days; |
| 4220 | int seconds; |
| 4221 | |
| 4222 | assert(n == OFFSET_AWARE); |
| 4223 | assert(HASTZINFO(self)); |
| 4224 | days = ymd_to_ord(GET_YEAR(self), |
| 4225 | GET_MONTH(self), |
| 4226 | GET_DAY(self)); |
| 4227 | seconds = DATE_GET_HOUR(self) * 3600 + |
| 4228 | (DATE_GET_MINUTE(self) - offset) * 60 + |
| 4229 | DATE_GET_SECOND(self); |
| 4230 | temp = new_delta(days, |
| 4231 | seconds, |
| 4232 | DATE_GET_MICROSECOND(self), |
| 4233 | 1); |
| 4234 | } |
| 4235 | if (temp != NULL) { |
| 4236 | self->hashcode = PyObject_Hash(temp); |
| 4237 | Py_DECREF(temp); |
| 4238 | } |
| 4239 | } |
| 4240 | return self->hashcode; |
| 4241 | } |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4242 | |
| 4243 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4244 | datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4245 | { |
| 4246 | PyObject *clone; |
| 4247 | PyObject *tuple; |
| 4248 | int y = GET_YEAR(self); |
| 4249 | int m = GET_MONTH(self); |
| 4250 | int d = GET_DAY(self); |
| 4251 | int hh = DATE_GET_HOUR(self); |
| 4252 | int mm = DATE_GET_MINUTE(self); |
| 4253 | int ss = DATE_GET_SECOND(self); |
| 4254 | int us = DATE_GET_MICROSECOND(self); |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4255 | PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4256 | |
| 4257 | if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4258 | datetime_kws, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4259 | &y, &m, &d, &hh, &mm, &ss, &us, |
| 4260 | &tzinfo)) |
| 4261 | return NULL; |
| 4262 | tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); |
| 4263 | if (tuple == NULL) |
| 4264 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4265 | clone = datetime_new(Py_TYPE(self), tuple, NULL); |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4266 | Py_DECREF(tuple); |
| 4267 | return clone; |
| 4268 | } |
| 4269 | |
| 4270 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4271 | datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4272 | { |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4273 | int y, m, d, hh, mm, ss, us; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4274 | PyObject *result; |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4275 | int offset, none; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4276 | |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4277 | PyObject *tzinfo; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4278 | static char *keywords[] = {"tz", NULL}; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4279 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4280 | if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:astimezone", keywords, |
| 4281 | &PyDateTime_TZInfoType, &tzinfo)) |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4282 | return NULL; |
| 4283 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4284 | if (!HASTZINFO(self) || self->tzinfo == Py_None) |
| 4285 | goto NeedAware; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4286 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4287 | /* Conversion to self's own time zone is a NOP. */ |
| 4288 | if (self->tzinfo == tzinfo) { |
| 4289 | Py_INCREF(self); |
| 4290 | return (PyObject *)self; |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 4291 | } |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4292 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4293 | /* Convert self to UTC. */ |
| 4294 | offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none); |
| 4295 | if (offset == -1 && PyErr_Occurred()) |
| 4296 | return NULL; |
| 4297 | if (none) |
| 4298 | goto NeedAware; |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4299 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4300 | y = GET_YEAR(self); |
| 4301 | m = GET_MONTH(self); |
| 4302 | d = GET_DAY(self); |
| 4303 | hh = DATE_GET_HOUR(self); |
| 4304 | mm = DATE_GET_MINUTE(self); |
| 4305 | ss = DATE_GET_SECOND(self); |
| 4306 | us = DATE_GET_MICROSECOND(self); |
| 4307 | |
| 4308 | mm -= offset; |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4309 | if ((mm < 0 || mm >= 60) && |
| 4310 | normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0) |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4311 | return NULL; |
| 4312 | |
| 4313 | /* Attach new tzinfo and let fromutc() do the rest. */ |
| 4314 | result = new_datetime(y, m, d, hh, mm, ss, us, tzinfo); |
| 4315 | if (result != NULL) { |
| 4316 | PyObject *temp = result; |
| 4317 | |
| 4318 | result = PyObject_CallMethod(tzinfo, "fromutc", "O", temp); |
| 4319 | Py_DECREF(temp); |
| 4320 | } |
Tim Peters | adf6420 | 2003-01-04 06:03:15 +0000 | [diff] [blame] | 4321 | return result; |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4322 | |
Tim Peters | 52dcce2 | 2003-01-23 16:36:11 +0000 | [diff] [blame] | 4323 | NeedAware: |
| 4324 | PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " |
| 4325 | "a naive datetime"); |
Tim Peters | 521fc15 | 2002-12-31 17:36:56 +0000 | [diff] [blame] | 4326 | return NULL; |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4330 | datetime_timetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4331 | { |
| 4332 | int dstflag = -1; |
| 4333 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4334 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4335 | int none; |
| 4336 | |
| 4337 | dstflag = call_dst(self->tzinfo, (PyObject *)self, &none); |
| 4338 | if (dstflag == -1 && PyErr_Occurred()) |
| 4339 | return NULL; |
| 4340 | |
| 4341 | if (none) |
| 4342 | dstflag = -1; |
| 4343 | else if (dstflag != 0) |
| 4344 | dstflag = 1; |
| 4345 | |
| 4346 | } |
| 4347 | return build_struct_time(GET_YEAR(self), |
| 4348 | GET_MONTH(self), |
| 4349 | GET_DAY(self), |
| 4350 | DATE_GET_HOUR(self), |
| 4351 | DATE_GET_MINUTE(self), |
| 4352 | DATE_GET_SECOND(self), |
| 4353 | dstflag); |
| 4354 | } |
| 4355 | |
| 4356 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4357 | datetime_getdate(PyDateTime_DateTime *self) |
| 4358 | { |
| 4359 | return new_date(GET_YEAR(self), |
| 4360 | GET_MONTH(self), |
| 4361 | GET_DAY(self)); |
| 4362 | } |
| 4363 | |
| 4364 | static PyObject * |
| 4365 | datetime_gettime(PyDateTime_DateTime *self) |
| 4366 | { |
| 4367 | return new_time(DATE_GET_HOUR(self), |
| 4368 | DATE_GET_MINUTE(self), |
| 4369 | DATE_GET_SECOND(self), |
| 4370 | DATE_GET_MICROSECOND(self), |
| 4371 | Py_None); |
| 4372 | } |
| 4373 | |
| 4374 | static PyObject * |
| 4375 | datetime_gettimetz(PyDateTime_DateTime *self) |
| 4376 | { |
| 4377 | return new_time(DATE_GET_HOUR(self), |
| 4378 | DATE_GET_MINUTE(self), |
| 4379 | DATE_GET_SECOND(self), |
| 4380 | DATE_GET_MICROSECOND(self), |
| 4381 | HASTZINFO(self) ? self->tzinfo : Py_None); |
| 4382 | } |
| 4383 | |
| 4384 | static PyObject * |
| 4385 | datetime_utctimetuple(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4386 | { |
| 4387 | int y = GET_YEAR(self); |
| 4388 | int m = GET_MONTH(self); |
| 4389 | int d = GET_DAY(self); |
| 4390 | int hh = DATE_GET_HOUR(self); |
| 4391 | int mm = DATE_GET_MINUTE(self); |
| 4392 | int ss = DATE_GET_SECOND(self); |
| 4393 | int us = 0; /* microseconds are ignored in a timetuple */ |
| 4394 | int offset = 0; |
| 4395 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4396 | if (HASTZINFO(self) && self->tzinfo != Py_None) { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4397 | int none; |
| 4398 | |
| 4399 | offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none); |
| 4400 | if (offset == -1 && PyErr_Occurred()) |
| 4401 | return NULL; |
| 4402 | } |
| 4403 | /* Even if offset is 0, don't call timetuple() -- tm_isdst should be |
| 4404 | * 0 in a UTC timetuple regardless of what dst() says. |
| 4405 | */ |
| 4406 | if (offset) { |
| 4407 | /* Subtract offset minutes & normalize. */ |
| 4408 | int stat; |
| 4409 | |
| 4410 | mm -= offset; |
| 4411 | stat = normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us); |
| 4412 | if (stat < 0) { |
| 4413 | /* At the edges, it's possible we overflowed |
| 4414 | * beyond MINYEAR or MAXYEAR. |
| 4415 | */ |
| 4416 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 4417 | PyErr_Clear(); |
| 4418 | else |
| 4419 | return NULL; |
| 4420 | } |
| 4421 | } |
| 4422 | return build_struct_time(y, m, d, hh, mm, ss, 0); |
| 4423 | } |
| 4424 | |
Tim Peters | 371935f | 2003-02-01 01:52:50 +0000 | [diff] [blame] | 4425 | /* Pickle support, a simple use of __reduce__. */ |
Tim Peters | 33e0f38 | 2003-01-10 02:05:14 +0000 | [diff] [blame] | 4426 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4427 | /* Let basestate be the non-tzinfo data string. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4428 | * If tzinfo is None, this returns (basestate,), else (basestate, tzinfo). |
| 4429 | * So it's a tuple in any (non-error) case. |
Tim Peters | b57f8f0 | 2003-02-01 02:54:15 +0000 | [diff] [blame] | 4430 | * __getstate__ isn't exposed. |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4431 | */ |
| 4432 | static PyObject * |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4433 | datetime_getstate(PyDateTime_DateTime *self) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4434 | { |
| 4435 | PyObject *basestate; |
| 4436 | PyObject *result = NULL; |
| 4437 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 4438 | basestate = PyString_FromStringAndSize((char *)self->data, |
| 4439 | _PyDateTime_DATETIME_DATASIZE); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4440 | if (basestate != NULL) { |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4441 | if (! HASTZINFO(self) || self->tzinfo == Py_None) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4442 | result = PyTuple_Pack(1, basestate); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4443 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 4444 | result = PyTuple_Pack(2, basestate, self->tzinfo); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4445 | Py_DECREF(basestate); |
| 4446 | } |
| 4447 | return result; |
| 4448 | } |
| 4449 | |
| 4450 | static PyObject * |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4451 | datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4452 | { |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4453 | return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4456 | static PyMethodDef datetime_methods[] = { |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4457 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4458 | /* Class methods: */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4459 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4460 | {"now", (PyCFunction)datetime_now, |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4461 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
Neal Norwitz | 2fbe537 | 2003-01-23 21:09:05 +0000 | [diff] [blame] | 4462 | PyDoc_STR("[tz] -> new datetime with tz's local day and time.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4463 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4464 | {"utcnow", (PyCFunction)datetime_utcnow, |
| 4465 | METH_NOARGS | METH_CLASS, |
| 4466 | PyDoc_STR("Return a new datetime representing UTC day and time.")}, |
| 4467 | |
| 4468 | {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4469 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 4470 | PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4471 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4472 | {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, |
| 4473 | METH_VARARGS | METH_CLASS, |
| 4474 | PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp " |
| 4475 | "(like time.time()).")}, |
| 4476 | |
Skip Montanaro | 0af3ade | 2005-01-13 04:12:31 +0000 | [diff] [blame] | 4477 | {"strptime", (PyCFunction)datetime_strptime, |
| 4478 | METH_VARARGS | METH_CLASS, |
| 4479 | PyDoc_STR("string, format -> new datetime parsed from a string " |
| 4480 | "(like time.strptime()).")}, |
| 4481 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4482 | {"combine", (PyCFunction)datetime_combine, |
| 4483 | METH_VARARGS | METH_KEYWORDS | METH_CLASS, |
| 4484 | PyDoc_STR("date, time -> datetime with same date and time fields")}, |
| 4485 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4486 | /* Instance methods: */ |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4487 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4488 | {"date", (PyCFunction)datetime_getdate, METH_NOARGS, |
| 4489 | PyDoc_STR("Return date object with same year, month and day.")}, |
| 4490 | |
| 4491 | {"time", (PyCFunction)datetime_gettime, METH_NOARGS, |
| 4492 | PyDoc_STR("Return time object with same time but with tzinfo=None.")}, |
| 4493 | |
| 4494 | {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS, |
| 4495 | PyDoc_STR("Return time object with same time and tzinfo.")}, |
| 4496 | |
| 4497 | {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS, |
| 4498 | PyDoc_STR("Return ctime() style string.")}, |
| 4499 | |
| 4500 | {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4501 | PyDoc_STR("Return time tuple, compatible with time.localtime().")}, |
| 4502 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4503 | {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4504 | PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, |
| 4505 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4506 | {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4507 | PyDoc_STR("[sep] -> string in ISO 8601 format, " |
| 4508 | "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" |
| 4509 | "sep is used to separate the year from the time, and " |
| 4510 | "defaults to 'T'.")}, |
| 4511 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4512 | {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4513 | PyDoc_STR("Return self.tzinfo.utcoffset(self).")}, |
| 4514 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4515 | {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4516 | PyDoc_STR("Return self.tzinfo.tzname(self).")}, |
| 4517 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4518 | {"dst", (PyCFunction)datetime_dst, METH_NOARGS, |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4519 | PyDoc_STR("Return self.tzinfo.dst(self).")}, |
| 4520 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4521 | {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4522 | PyDoc_STR("Return datetime with new specified fields.")}, |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 4523 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 4524 | {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 4525 | PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, |
| 4526 | |
Guido van Rossum | 177e41a | 2003-01-30 22:06:23 +0000 | [diff] [blame] | 4527 | {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, |
| 4528 | PyDoc_STR("__reduce__() -> (cls, state)")}, |
| 4529 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4530 | {NULL, NULL} |
| 4531 | }; |
| 4532 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4533 | static char datetime_doc[] = |
Raymond Hettinger | 3a4231d | 2004-12-19 20:13:24 +0000 | [diff] [blame] | 4534 | PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\ |
| 4535 | \n\ |
| 4536 | The year, month and day arguments are required. tzinfo may be None, or an\n\ |
| 4537 | 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] | 4538 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4539 | static PyNumberMethods datetime_as_number = { |
| 4540 | datetime_add, /* nb_add */ |
| 4541 | datetime_subtract, /* nb_subtract */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4542 | 0, /* nb_multiply */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4543 | 0, /* nb_remainder */ |
| 4544 | 0, /* nb_divmod */ |
| 4545 | 0, /* nb_power */ |
| 4546 | 0, /* nb_negative */ |
| 4547 | 0, /* nb_positive */ |
| 4548 | 0, /* nb_absolute */ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4549 | 0, /* nb_bool */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4550 | }; |
| 4551 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 4552 | static PyTypeObject PyDateTime_DateTimeType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4553 | PyVarObject_HEAD_INIT(NULL, 0) |
Tim Peters | 0bf60bd | 2003-01-08 20:40:01 +0000 | [diff] [blame] | 4554 | "datetime.datetime", /* tp_name */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4555 | sizeof(PyDateTime_DateTime), /* tp_basicsize */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4556 | 0, /* tp_itemsize */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4557 | (destructor)datetime_dealloc, /* tp_dealloc */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4558 | 0, /* tp_print */ |
| 4559 | 0, /* tp_getattr */ |
| 4560 | 0, /* tp_setattr */ |
| 4561 | 0, /* tp_compare */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4562 | (reprfunc)datetime_repr, /* tp_repr */ |
| 4563 | &datetime_as_number, /* tp_as_number */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4564 | 0, /* tp_as_sequence */ |
| 4565 | 0, /* tp_as_mapping */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4566 | (hashfunc)datetime_hash, /* tp_hash */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4567 | 0, /* tp_call */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4568 | (reprfunc)datetime_str, /* tp_str */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4569 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4570 | 0, /* tp_setattro */ |
| 4571 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 4572 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4573 | datetime_doc, /* tp_doc */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4574 | 0, /* tp_traverse */ |
| 4575 | 0, /* tp_clear */ |
Guido van Rossum | 1996059 | 2006-08-24 17:29:38 +0000 | [diff] [blame] | 4576 | datetime_richcompare, /* tp_richcompare */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4577 | 0, /* tp_weaklistoffset */ |
| 4578 | 0, /* tp_iter */ |
| 4579 | 0, /* tp_iternext */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4580 | datetime_methods, /* tp_methods */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4581 | 0, /* tp_members */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4582 | datetime_getset, /* tp_getset */ |
| 4583 | &PyDateTime_DateType, /* tp_base */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4584 | 0, /* tp_dict */ |
| 4585 | 0, /* tp_descr_get */ |
| 4586 | 0, /* tp_descr_set */ |
| 4587 | 0, /* tp_dictoffset */ |
| 4588 | 0, /* tp_init */ |
Tim Peters | a98924a | 2003-05-17 05:55:19 +0000 | [diff] [blame] | 4589 | datetime_alloc, /* tp_alloc */ |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4590 | datetime_new, /* tp_new */ |
Tim Peters | 4c53013 | 2003-05-16 22:44:06 +0000 | [diff] [blame] | 4591 | 0, /* tp_free */ |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4592 | }; |
| 4593 | |
| 4594 | /* --------------------------------------------------------------------------- |
| 4595 | * Module methods and initialization. |
| 4596 | */ |
| 4597 | |
| 4598 | static PyMethodDef module_methods[] = { |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4599 | {NULL, NULL} |
| 4600 | }; |
| 4601 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 4602 | /* C API. Clients get at this via PyDateTime_IMPORT, defined in |
| 4603 | * datetime.h. |
| 4604 | */ |
| 4605 | static PyDateTime_CAPI CAPI = { |
| 4606 | &PyDateTime_DateType, |
| 4607 | &PyDateTime_DateTimeType, |
| 4608 | &PyDateTime_TimeType, |
| 4609 | &PyDateTime_DeltaType, |
| 4610 | &PyDateTime_TZInfoType, |
| 4611 | new_date_ex, |
| 4612 | new_datetime_ex, |
| 4613 | new_time_ex, |
| 4614 | new_delta_ex, |
| 4615 | datetime_fromtimestamp, |
| 4616 | date_fromtimestamp |
| 4617 | }; |
| 4618 | |
| 4619 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4620 | PyMODINIT_FUNC |
| 4621 | initdatetime(void) |
| 4622 | { |
| 4623 | PyObject *m; /* a module object */ |
| 4624 | PyObject *d; /* its dict */ |
| 4625 | PyObject *x; |
| 4626 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4627 | m = Py_InitModule3("datetime", module_methods, |
| 4628 | "Fast implementation of the datetime type."); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 4629 | if (m == NULL) |
| 4630 | return; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4631 | |
| 4632 | if (PyType_Ready(&PyDateTime_DateType) < 0) |
| 4633 | return; |
| 4634 | if (PyType_Ready(&PyDateTime_DateTimeType) < 0) |
| 4635 | return; |
| 4636 | if (PyType_Ready(&PyDateTime_DeltaType) < 0) |
| 4637 | return; |
| 4638 | if (PyType_Ready(&PyDateTime_TimeType) < 0) |
| 4639 | return; |
| 4640 | if (PyType_Ready(&PyDateTime_TZInfoType) < 0) |
| 4641 | return; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4642 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4643 | /* timedelta values */ |
| 4644 | d = PyDateTime_DeltaType.tp_dict; |
| 4645 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4646 | x = new_delta(0, 0, 1, 0); |
| 4647 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4648 | return; |
| 4649 | Py_DECREF(x); |
| 4650 | |
| 4651 | x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0); |
| 4652 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4653 | return; |
| 4654 | Py_DECREF(x); |
| 4655 | |
| 4656 | x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0); |
| 4657 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4658 | return; |
| 4659 | Py_DECREF(x); |
| 4660 | |
| 4661 | /* date values */ |
| 4662 | d = PyDateTime_DateType.tp_dict; |
| 4663 | |
| 4664 | x = new_date(1, 1, 1); |
| 4665 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4666 | return; |
| 4667 | Py_DECREF(x); |
| 4668 | |
| 4669 | x = new_date(MAXYEAR, 12, 31); |
| 4670 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4671 | return; |
| 4672 | Py_DECREF(x); |
| 4673 | |
| 4674 | x = new_delta(1, 0, 0, 0); |
| 4675 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4676 | return; |
| 4677 | Py_DECREF(x); |
| 4678 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4679 | /* time values */ |
| 4680 | d = PyDateTime_TimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4681 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4682 | x = new_time(0, 0, 0, 0, Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4683 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4684 | return; |
| 4685 | Py_DECREF(x); |
| 4686 | |
Tim Peters | 37f3982 | 2003-01-10 03:49:02 +0000 | [diff] [blame] | 4687 | x = new_time(23, 59, 59, 999999, Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4688 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4689 | return; |
| 4690 | Py_DECREF(x); |
| 4691 | |
| 4692 | x = new_delta(0, 0, 1, 0); |
| 4693 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4694 | return; |
| 4695 | Py_DECREF(x); |
| 4696 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4697 | /* datetime values */ |
| 4698 | d = PyDateTime_DateTimeType.tp_dict; |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4699 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4700 | x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4701 | if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) |
| 4702 | return; |
| 4703 | Py_DECREF(x); |
| 4704 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4705 | x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4706 | if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) |
| 4707 | return; |
| 4708 | Py_DECREF(x); |
| 4709 | |
| 4710 | x = new_delta(0, 0, 1, 0); |
| 4711 | if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) |
| 4712 | return; |
| 4713 | Py_DECREF(x); |
| 4714 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4715 | /* module initialization */ |
| 4716 | PyModule_AddIntConstant(m, "MINYEAR", MINYEAR); |
| 4717 | PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR); |
| 4718 | |
| 4719 | Py_INCREF(&PyDateTime_DateType); |
| 4720 | PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType); |
| 4721 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4722 | Py_INCREF(&PyDateTime_DateTimeType); |
| 4723 | PyModule_AddObject(m, "datetime", |
| 4724 | (PyObject *)&PyDateTime_DateTimeType); |
| 4725 | |
| 4726 | Py_INCREF(&PyDateTime_TimeType); |
| 4727 | PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType); |
| 4728 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4729 | Py_INCREF(&PyDateTime_DeltaType); |
| 4730 | PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType); |
| 4731 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4732 | Py_INCREF(&PyDateTime_TZInfoType); |
| 4733 | PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType); |
| 4734 | |
Tim Peters | 9ddf40b | 2004-06-20 22:41:32 +0000 | [diff] [blame] | 4735 | x = PyCObject_FromVoidPtrAndDesc(&CAPI, (void*) DATETIME_API_MAGIC, |
| 4736 | NULL); |
| 4737 | if (x == NULL) |
| 4738 | return; |
| 4739 | PyModule_AddObject(m, "datetime_CAPI", x); |
| 4740 | |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4741 | /* A 4-year cycle has an extra leap day over what we'd get from |
| 4742 | * pasting together 4 single years. |
| 4743 | */ |
| 4744 | assert(DI4Y == 4 * 365 + 1); |
| 4745 | assert(DI4Y == days_before_year(4+1)); |
| 4746 | |
| 4747 | /* Similarly, a 400-year cycle has an extra leap day over what we'd |
| 4748 | * get from pasting together 4 100-year cycles. |
| 4749 | */ |
| 4750 | assert(DI400Y == 4 * DI100Y + 1); |
| 4751 | assert(DI400Y == days_before_year(400+1)); |
| 4752 | |
| 4753 | /* OTOH, a 100-year cycle has one fewer leap day than we'd get from |
| 4754 | * pasting together 25 4-year cycles. |
| 4755 | */ |
| 4756 | assert(DI100Y == 25 * DI4Y - 1); |
| 4757 | assert(DI100Y == days_before_year(100+1)); |
| 4758 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4759 | us_per_us = PyLong_FromLong(1); |
| 4760 | us_per_ms = PyLong_FromLong(1000); |
| 4761 | us_per_second = PyLong_FromLong(1000000); |
| 4762 | us_per_minute = PyLong_FromLong(60000000); |
| 4763 | seconds_per_day = PyLong_FromLong(24 * 3600); |
Tim Peters | 2a799bf | 2002-12-16 20:18:38 +0000 | [diff] [blame] | 4764 | if (us_per_us == NULL || us_per_ms == NULL || us_per_second == NULL || |
| 4765 | us_per_minute == NULL || seconds_per_day == NULL) |
| 4766 | return; |
| 4767 | |
| 4768 | /* The rest are too big for 32-bit ints, but even |
| 4769 | * us_per_week fits in 40 bits, so doubles should be exact. |
| 4770 | */ |
| 4771 | us_per_hour = PyLong_FromDouble(3600000000.0); |
| 4772 | us_per_day = PyLong_FromDouble(86400000000.0); |
| 4773 | us_per_week = PyLong_FromDouble(604800000000.0); |
| 4774 | if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) |
| 4775 | return; |
| 4776 | } |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4777 | |
| 4778 | /* --------------------------------------------------------------------------- |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4779 | Some time zone algebra. For a datetime x, let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4780 | x.n = x stripped of its timezone -- its naive time. |
| 4781 | x.o = x.utcoffset(), and assuming that doesn't raise an exception or |
| 4782 | return None |
| 4783 | x.d = x.dst(), and assuming that doesn't raise an exception or |
| 4784 | return None |
| 4785 | x.s = x's standard offset, x.o - x.d |
| 4786 | |
| 4787 | Now some derived rules, where k is a duration (timedelta). |
| 4788 | |
| 4789 | 1. x.o = x.s + x.d |
| 4790 | This follows from the definition of x.s. |
| 4791 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4792 | 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] | 4793 | This is actually a requirement, an assumption we need to make about |
| 4794 | sane tzinfo classes. |
| 4795 | |
| 4796 | 3. The naive UTC time corresponding to x is x.n - x.o. |
| 4797 | This is again a requirement for a sane tzinfo class. |
| 4798 | |
| 4799 | 4. (x+k).s = x.s |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4800 | This follows from #2, and that datimetimetz+timedelta preserves tzinfo. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4801 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4802 | 5. (x+k).n = x.n + k |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4803 | Again follows from how arithmetic is defined. |
| 4804 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4805 | 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] | 4806 | (meaning that the various tzinfo methods exist, and don't blow up or return |
| 4807 | None when called). |
| 4808 | |
Tim Peters | a9bc168 | 2003-01-11 03:39:11 +0000 | [diff] [blame] | 4809 | 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] | 4810 | x is already in UTC. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4811 | |
| 4812 | By #3, we want |
| 4813 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4814 | y.n - y.o = x.n [1] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4815 | |
| 4816 | The algorithm starts by attaching tz to x.n, and calling that y. So |
| 4817 | x.n = y.n at the start. Then it wants to add a duration k to y, so that [1] |
| 4818 | becomes true; in effect, we want to solve [2] for k: |
| 4819 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4820 | (y+k).n - (y+k).o = x.n [2] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4821 | |
| 4822 | By #1, this is the same as |
| 4823 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4824 | (y+k).n - ((y+k).s + (y+k).d) = x.n [3] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4825 | |
| 4826 | By #5, (y+k).n = y.n + k, which equals x.n + k because x.n=y.n at the start. |
| 4827 | Substituting that into [3], |
| 4828 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4829 | x.n + k - (y+k).s - (y+k).d = x.n; the x.n terms cancel, leaving |
| 4830 | k - (y+k).s - (y+k).d = 0; rearranging, |
| 4831 | k = (y+k).s - (y+k).d; by #4, (y+k).s == y.s, so |
| 4832 | k = y.s - (y+k).d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4833 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4834 | On the RHS, (y+k).d can't be computed directly, but y.s can be, and we |
| 4835 | approximate k by ignoring the (y+k).d term at first. Note that k can't be |
| 4836 | very large, since all offset-returning methods return a duration of magnitude |
| 4837 | less than 24 hours. For that reason, if y is firmly in std time, (y+k).d must |
| 4838 | be 0, so ignoring it has no consequence then. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4839 | |
| 4840 | In any case, the new value is |
| 4841 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4842 | z = y + y.s [4] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4843 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4844 | It's helpful to step back at look at [4] from a higher level: it's simply |
| 4845 | mapping from UTC to tz's standard time. |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4846 | |
| 4847 | At this point, if |
| 4848 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4849 | z.n - z.o = x.n [5] |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4850 | |
| 4851 | 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] | 4852 | at the start of daylight time. Picture US Eastern for concreteness. The wall |
| 4853 | 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] | 4854 | sense then. The docs ask that an Eastern tzinfo class consider such a time to |
| 4855 | be EDT (because it's "after 2"), which is a redundant spelling of 1:MM EST |
| 4856 | 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] | 4857 | the only spelling that makes sense on the local wall clock. |
| 4858 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4859 | In fact, if [5] holds at this point, we do have the standard-time spelling, |
| 4860 | but that takes a bit of proof. We first prove a stronger result. What's the |
| 4861 | difference between the LHS and RHS of [5]? Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4862 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4863 | diff = x.n - (z.n - z.o) [6] |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4864 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4865 | Now |
| 4866 | z.n = by [4] |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4867 | (y + y.s).n = by #5 |
| 4868 | y.n + y.s = since y.n = x.n |
| 4869 | x.n + y.s = since z and y are have the same tzinfo member, |
| 4870 | y.s = z.s by #2 |
| 4871 | x.n + z.s |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4872 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4873 | Plugging that back into [6] gives |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4874 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4875 | diff = |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4876 | x.n - ((x.n + z.s) - z.o) = expanding |
| 4877 | x.n - x.n - z.s + z.o = cancelling |
| 4878 | - z.s + z.o = by #2 |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4879 | z.d |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4880 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4881 | So diff = z.d. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4882 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4883 | 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] | 4884 | spelling we wanted in the endcase described above. We're done. Contrarily, |
| 4885 | 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] | 4886 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4887 | If [5] is not true now, diff = z.d != 0, and z.d is the offset we need to |
| 4888 | 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] | 4889 | local clock into tz's daylight time). |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4890 | |
Tim Peters | c5dc4da | 2003-01-02 17:55:03 +0000 | [diff] [blame] | 4891 | Let |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4892 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 4893 | z' = z + z.d = z + diff [7] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 4894 | |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 4895 | and we can again ask whether |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 4896 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4897 | z'.n - z'.o = x.n [8] |
Tim Peters | c3bb26a | 2003-01-02 03:14:59 +0000 | [diff] [blame] | 4898 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4899 | If so, we're done. If not, the tzinfo class is insane, according to the |
| 4900 | assumptions we've made. This also requires a bit of proof. As before, let's |
| 4901 | compute the difference between the LHS and RHS of [8] (and skipping some of |
| 4902 | the justifications for the kinds of substitutions we've done several times |
| 4903 | already): |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 4904 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4905 | diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7] |
| 4906 | x.n - (z.n + diff - z'.o) = replacing diff via [6] |
| 4907 | x.n - (z.n + x.n - (z.n - z.o) - z'.o) = |
| 4908 | x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n |
| 4909 | - z.n + z.n - z.o + z'.o = cancel z.n |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 4910 | - z.o + z'.o = #1 twice |
| 4911 | -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo |
| 4912 | z'.d - z.d |
| 4913 | |
| 4914 | 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] | 4915 | we've found the UTC-equivalent so are done. In fact, we stop with [7] and |
| 4916 | return z', not bothering to compute z'.d. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 4917 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4918 | How could z.d and z'd differ? z' = z + z.d [7], so merely moving z' by |
| 4919 | a dst() offset, and starting *from* a time already in DST (we know z.d != 0), |
| 4920 | would have to change the result dst() returns: we start in DST, and moving |
| 4921 | a little further into it takes us out of DST. |
Tim Peters | 4fede1a | 2003-01-04 00:26:59 +0000 | [diff] [blame] | 4922 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4923 | There isn't a sane case where this can happen. The closest it gets is at |
| 4924 | the end of DST, where there's an hour in UTC with no spelling in a hybrid |
| 4925 | tzinfo class. In US Eastern, that's 5:MM UTC = 0:MM EST = 1:MM EDT. During |
| 4926 | that hour, on an Eastern clock 1:MM is taken as being in standard time (6:MM |
| 4927 | UTC) because the docs insist on that, but 0:MM is taken as being in daylight |
| 4928 | time (4:MM UTC). There is no local time mapping to 5:MM UTC. The local |
| 4929 | clock jumps from 1:59 back to 1:00 again, and repeats the 1:MM hour in |
| 4930 | standard time. Since that's what the local clock *does*, we want to map both |
| 4931 | 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] | 4932 | in local time, but so it goes -- it's the way the local clock works. |
| 4933 | |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4934 | When x = 5:MM UTC is the input to this algorithm, x.o=0, y.o=-5 and y.d=0, |
| 4935 | so z=0:MM. z.d=60 (minutes) then, so [5] doesn't hold and we keep going. |
| 4936 | 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] | 4937 | (correctly) concludes that z' is not UTC-equivalent to x. |
| 4938 | |
| 4939 | Because we know z.d said z was in daylight time (else [5] would have held and |
| 4940 | 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] | 4941 | 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] | 4942 | return in Eastern, it follows that z'.d must be 0 (which it is in the example, |
| 4943 | but the reasoning doesn't depend on the example -- it depends on there being |
| 4944 | two possible dst() outcomes, one zero and the other non-zero). Therefore |
Tim Peters | 8bb5ad2 | 2003-01-24 02:44:45 +0000 | [diff] [blame] | 4945 | z' must be in standard time, and is the spelling we want in this case. |
| 4946 | |
| 4947 | Note again that z' is not UTC-equivalent as far as the hybrid tzinfo class is |
| 4948 | concerned (because it takes z' as being in standard time rather than the |
| 4949 | daylight time we intend here), but returning it gives the real-life "local |
| 4950 | clock repeats an hour" behavior when mapping the "unspellable" UTC hour into |
| 4951 | tz. |
| 4952 | |
| 4953 | When the input is 6:MM, z=1:MM and z.d=0, and we stop at once, again with |
| 4954 | the 1:MM standard time spelling we want. |
| 4955 | |
| 4956 | So how can this break? One of the assumptions must be violated. Two |
| 4957 | possibilities: |
| 4958 | |
| 4959 | 1) [2] effectively says that y.s is invariant across all y belong to a given |
| 4960 | time zone. This isn't true if, for political reasons or continental drift, |
| 4961 | a region decides to change its base offset from UTC. |
| 4962 | |
| 4963 | 2) There may be versions of "double daylight" time where the tail end of |
| 4964 | the analysis gives up a step too early. I haven't thought about that |
| 4965 | enough to say. |
| 4966 | |
| 4967 | In any case, it's clear that the default fromutc() is strong enough to handle |
| 4968 | "almost all" time zones: so long as the standard offset is invariant, it |
| 4969 | doesn't matter if daylight time transition points change from year to year, or |
| 4970 | if daylight time is skipped in some years; it doesn't matter how large or |
| 4971 | small dst() may get within its bounds; and it doesn't even matter if some |
| 4972 | perverse time zone returns a negative dst()). So a breaking case must be |
| 4973 | pretty bizarre, and a tzinfo subclass can override fromutc() if it is. |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 4974 | --------------------------------------------------------------------------- */ |