Bug Summary

File:Modules/_datetimemodule.c
Location:line 1464, column 5
Description:Value stored to 'x1' is never read

Annotated Source Code

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